class library: SynthDef - replaceUGen fixes
[supercollider.git] / server / plugins / OscUGens.cpp
blob23be2cdf9fb065ddd64a6c42b1c80f6979cd6a41
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "SC_PlugIn.h"
22 #include "SC_Altivec.h"
23 #include <limits>
24 #include <string.h>
26 static InterfaceTable *ft;
28 struct BufUnit : public Unit
30 SndBuf *m_buf;
31 float m_fbufnum;
34 struct TableLookup : public BufUnit
36 double m_cpstoinc, m_radtoinc;
37 int32 mTableSize;
38 int32 m_lomask;
41 struct DegreeToKey : public BufUnit
43 SndBuf *m_buf;
44 float m_fbufnum;
45 int32 mPrevIndex;
46 float mPrevKey;
47 int32 mOctave;
50 struct Select : public Unit
54 struct TWindex : public Unit
56 int32 m_prevIndex;
57 float m_trig;
60 struct Index : public BufUnit
64 struct IndexL : public BufUnit
68 struct WrapIndex : public BufUnit
72 struct FoldIndex : public BufUnit
76 struct IndexInBetween : public BufUnit
80 struct DetectIndex : public BufUnit
82 float mPrev;
83 float mPrevIn;
86 struct Shaper : public BufUnit
88 float mOffset;
89 float mPrevIn;
92 struct SigOsc : public BufUnit
94 int32 mTableSize;
95 double m_cpstoinc;
96 float mPhase;
99 struct FSinOsc : public Unit
101 double m_b1, m_y1, m_y2, m_freq;
104 struct PSinGrain : public Unit
106 double m_b1, m_y1, m_y2;
107 double m_level, m_slope, m_curve;
108 int32 mCounter;
111 struct Osc : public TableLookup
113 int32 m_phase;
114 float m_phasein;
117 struct SinOsc : public TableLookup
119 int32 m_phase;
120 float m_phasein;
123 struct SinOscFB : public TableLookup
125 int32 m_phase;
126 float m_prevout, m_feedback;
129 struct OscN : public TableLookup
131 int32 m_phase;
132 float m_phasein;
135 struct COsc : public TableLookup
137 int32 m_phase1, m_phase2;
140 struct VOsc : public Unit
142 double m_cpstoinc, m_radtoinc;
143 int32 mTableSize;
144 int32 m_lomask;
145 int32 m_phase, m_phaseoffset;
146 float m_phasein, m_bufpos;
149 struct VOsc3 : public Unit
151 double m_cpstoinc;
152 int32 mTableSize;
153 int32 m_lomask;
154 int32 m_phase1, m_phase2, m_phase3;
155 float m_bufpos;
158 struct Formant : public Unit
160 int32 m_phase1, m_phase2, m_phase3;
161 double m_cpstoinc;
166 struct Blip : public Unit
168 int32 m_phase, m_numharm, m_N;
169 float m_freqin, m_scale;
170 double m_cpstoinc;
173 struct Saw : public Unit
175 int32 m_phase, m_N;
176 float m_freqin, m_scale, m_y1;
177 double m_cpstoinc;
180 struct Pulse : public Unit
182 int32 m_phase, m_phaseoff, m_N;
183 float m_freqin, m_scale, m_y1;
184 double m_cpstoinc;
187 struct Klang : public Unit
189 float *m_coefs;
190 int32 m_numpartials;
193 struct Klank : public Unit
195 float *m_coefs;
196 float *m_buf;
197 float m_x1, m_x2;
198 int32 m_numpartials;
203 #define xlomask8 0x000003FC
204 #define xlomask9 0x000007FC
205 #define xlomask10 0x00000FFC
206 #define xlomask11 0x00001FFC
207 #define xlomask12 0x00003FFC
208 #define xlomask13 0x00007FFC
210 #define xlomask8i 0x000007F8
211 #define xlomask9i 0x00000FF8
212 #define xlomask10i 0x00001FF8
213 #define xlomask11i 0x00003FF8
214 #define xlomask12i 0x00007FF8
215 #define xlomask13i 0x0000FFF8
217 #define onecyc13 0x20000000
221 //////////////////////////////////////////////////////////////////////////////////////////////////
223 extern "C"
226 void DegreeToKey_Ctor(DegreeToKey *unit);
227 void DegreeToKey_next_1(DegreeToKey *unit, int inNumSamples);
228 void DegreeToKey_next_k(DegreeToKey *unit, int inNumSamples);
229 void DegreeToKey_next_a(DegreeToKey *unit, int inNumSamples);
231 void Select_Ctor(Select *unit);
232 void Select_next_1(Select *unit, int inNumSamples);
233 void Select_next_k(Select *unit, int inNumSamples);
234 void Select_next_a(Select *unit, int inNumSamples);
236 void TWindex_Ctor(TWindex *unit);
237 void TWindex_next_k(TWindex *unit, int inNumSamples);
238 void TWindex_next_ak(TWindex *unit, int inNumSamples);
240 void Index_Ctor(Index *unit);
241 void Index_next_1(Index *unit, int inNumSamples);
242 void Index_next_k(Index *unit, int inNumSamples);
243 void Index_next_a(Index *unit, int inNumSamples);
245 void IndexL_Ctor(IndexL *unit);
246 void IndexL_next_1(IndexL *unit, int inNumSamples);
247 void IndexL_next_k(IndexL *unit, int inNumSamples);
248 void IndexL_next_a(IndexL *unit, int inNumSamples);
250 void FoldIndex_Ctor(FoldIndex *unit);
251 void FoldIndex_next_1(FoldIndex *unit, int inNumSamples);
252 void FoldIndex_next_k(FoldIndex *unit, int inNumSamples);
253 void FoldIndex_next_a(FoldIndex *unit, int inNumSamples);
255 void WrapIndex_Ctor(WrapIndex *unit);
256 void WrapIndex_next_1(WrapIndex *unit, int inNumSamples);
257 void WrapIndex_next_k(WrapIndex *unit, int inNumSamples);
258 void WrapIndex_next_a(WrapIndex *unit, int inNumSamples);
260 void Shaper_Ctor(Shaper *unit);
261 void Shaper_next_1(Shaper *unit, int inNumSamples);
262 void Shaper_next_k(Shaper *unit, int inNumSamples);
263 void Shaper_next_a(Shaper *unit, int inNumSamples);
266 void DetectIndex_Ctor(DetectIndex *unit);
267 void DetectIndex_next_1(DetectIndex *unit, int inNumSamples);
268 void DetectIndex_next_k(DetectIndex *unit, int inNumSamples);
269 void DetectIndex_next_a(DetectIndex *unit, int inNumSamples);
271 void IndexInBetween_Ctor(IndexInBetween *unit);
272 void IndexInBetween_next_1(IndexInBetween *unit, int inNumSamples);
273 void IndexInBetween_next_k(IndexInBetween *unit, int inNumSamples);
274 void IndexInBetween_next_a(IndexInBetween *unit, int inNumSamples);
277 void SigOsc_Ctor(SigOsc *unit);
278 void SigOsc_next_1(SigOsc *unit, int inNumSamples);
279 void SigOsc_next_k(SigOsc *unit, int inNumSamples);
280 void SigOsc_next_a(SigOsc *unit, int inNumSamples);
282 void FSinOsc_Ctor(FSinOsc *unit);
283 void FSinOsc_next(FSinOsc *unit, int inNumSamples);
285 void PSinGrain_Ctor(PSinGrain *unit);
286 void PSinGrain_next(PSinGrain *unit, int inNumSamples);
288 void SinOsc_Ctor(SinOsc *unit);
289 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples);
290 void SinOsc_next_ika(SinOsc *unit, int inNumSamples);
291 void SinOsc_next_iak(SinOsc *unit, int inNumSamples);
292 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples);
294 void Osc_Ctor(Osc *unit);
295 void Osc_next_ikk(Osc *unit, int inNumSamples);
296 void Osc_next_ika(Osc *unit, int inNumSamples);
297 void Osc_next_iak(Osc *unit, int inNumSamples);
298 void Osc_next_iaa(Osc *unit, int inNumSamples);
300 void OscN_Ctor(OscN *unit);
301 void OscN_next_nkk(OscN *unit, int inNumSamples);
302 void OscN_next_nka(OscN *unit, int inNumSamples);
303 void OscN_next_nak(OscN *unit, int inNumSamples);
304 void OscN_next_naa(OscN *unit, int inNumSamples);
306 void COsc_Ctor(COsc *unit);
307 void COsc_next(COsc *unit, int inNumSamples);
309 void VOsc_Ctor(VOsc *unit);
310 void VOsc_next_ik(VOsc *unit, int inNumSamples);
312 void VOsc3_Ctor(VOsc3 *unit);
313 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples);
315 void Formant_Ctor(Formant *unit);
316 void Formant_next(Formant *unit, int inNumSamples);
318 void Blip_Ctor(Blip *unit);
319 void Blip_next(Blip *unit, int inNumSamples);
321 void Saw_Ctor(Saw *unit);
322 void Saw_next(Saw *unit, int inNumSamples);
324 void Pulse_Ctor(Pulse *unit);
325 void Pulse_next(Pulse *unit, int inNumSamples);
327 void Klang_Dtor(Klang *unit);
328 void Klang_Ctor(Klang *unit);
329 void Klang_next(Klang *unit, int inNumSamples);
331 void Klank_Dtor(Klank *unit);
332 void Klank_Ctor(Klank *unit);
333 void Klank_next(Klank *unit, int inNumSamples);
337 //////////////////////////////////////////////////////////////////////////////////////////////////
339 #define GET_TABLE \
340 float fbufnum = ZIN0(0); \
341 if (fbufnum != unit->m_fbufnum) { \
342 uint32 bufnum = (uint32)fbufnum; \
343 World *world = unit->mWorld; \
344 if (bufnum >= world->mNumSndBufs) { \
345 int localBufNum = bufnum - world->mNumSndBufs; \
346 Graph *parent = unit->mParent; \
347 if(localBufNum <= parent->localBufNum) { \
348 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
349 } else { \
350 bufnum = 0; \
351 unit->m_buf = world->mSndBufs + bufnum; \
353 } else { \
354 unit->m_buf = world->mSndBufs + bufnum; \
356 unit->m_fbufnum = fbufnum; \
358 const SndBuf *buf = unit->m_buf; \
359 if(!buf) { \
360 ClearUnitOutputs(unit, inNumSamples); \
361 return; \
363 LOCK_SNDBUF_SHARED(buf); \
364 const float *bufData __attribute__((__unused__)) = buf->data; \
365 if (!bufData) { \
366 ClearUnitOutputs(unit, inNumSamples); \
367 return; \
369 int tableSize = buf->samples;
372 ////////////////////////////////////////////////////////////////////////////////////////////////////////
375 void TableLookup_SetTable(TableLookup* unit, int32 inSize, float* inTable)
377 unit->mTable0 = inTable;
378 unit->mTable1 = inTable + 1;
379 unit->mTableSize = inSize;
380 unit->mMaxIndex = unit->mTableSize - 1;
381 unit->mFMaxIndex = unit->mMaxIndex;
382 unit->m_radtoinc = unit->mTableSize * (rtwopi * 65536.);
383 unit->m_cpstoinc = unit->mTableSize * SAMPLEDUR * 65536.;
384 //Print("TableLookup_SetTable unit->m_radtoinc %g %d %g\n", m_radtoinc, unit->mTableSize, rtwopi);
388 ////////////////////////////////////////////////////////////////////////////////////////////////////////
390 void DegreeToKey_Ctor(DegreeToKey *unit)
392 unit->m_fbufnum = -1e9f;
393 if (BUFLENGTH == 1) {
394 SETCALC(DegreeToKey_next_1);
395 } else if (INRATE(0) == calc_FullRate) {
396 SETCALC(DegreeToKey_next_a);
397 } else {
398 SETCALC(DegreeToKey_next_k);
400 unit->mOctave = (int32)ZIN0(2);
401 unit->mPrevIndex = std::numeric_limits<int32>::max();
402 unit->mPrevKey = 0.;
403 DegreeToKey_next_1(unit, 1);
406 void DegreeToKey_next_1(DegreeToKey *unit, int inNumSamples)
408 // get table
409 GET_TABLE
410 const float *table = bufData;
411 int32 maxindex = tableSize - 1;
413 int32 key, oct;
415 int32 octave = unit->mOctave;
416 float val;
418 int32 index = (int32)floor(ZIN0(1));
419 if (index == unit->mPrevIndex) {
420 val = unit->mPrevKey;
421 } else if (index < 0) {
422 unit->mPrevIndex = index;
423 key = tableSize + index % tableSize;
424 oct = (index + 1) / tableSize - 1;
425 val = unit->mPrevKey = table[key] + octave * oct;
426 } else if (index > maxindex) {
427 unit->mPrevIndex = index;
428 key = index % tableSize;
429 oct = index / tableSize;
430 val = unit->mPrevKey = table[key] + octave * oct;
431 } else {
432 unit->mPrevIndex = index;
433 val = unit->mPrevKey = table[index];
435 ZOUT0(0) = val;
438 void DegreeToKey_next_k(DegreeToKey *unit, int inNumSamples)
440 // get table
441 GET_TABLE
442 const float *table = bufData;
443 int32 maxindex = tableSize - 1;
445 float *out = ZOUT(0);
447 int32 key, oct;
448 float octave = unit->mOctave;
449 float val;
451 int32 index = (int32)floor(ZIN0(1));
452 if (index == unit->mPrevIndex) {
453 val = unit->mPrevKey;
454 } else if (index < 0) {
455 unit->mPrevIndex = index;
456 key = tableSize + index % tableSize;
457 oct = (index + 1) / tableSize - 1;
458 val = unit->mPrevKey = table[key] + octave * oct;
459 } else if (index > maxindex) {
460 unit->mPrevIndex = index;
461 key = index % tableSize;
462 oct = index / tableSize;
463 val = unit->mPrevKey = table[key] + octave * oct;
464 } else {
465 unit->mPrevIndex = index;
466 val = unit->mPrevKey = table[index];
468 LOOP1(inNumSamples,
469 ZXP(out) = val;
474 void DegreeToKey_next_a(DegreeToKey *unit, int inNumSamples)
476 // get table
477 GET_TABLE
478 const float *table = bufData;
479 int32 maxindex = tableSize - 1;
481 float *out = ZOUT(0);
482 float *in = ZIN(1);
483 int32 previndex = unit->mPrevIndex;
484 float prevkey = unit->mPrevKey;
485 int32 key, oct;
487 float octave = unit->mOctave;
489 LOOP1(inNumSamples,
490 int32 index = (int32)floor(ZXP(in));
491 if (index == previndex) {
492 ZXP(out) = prevkey;
493 } else if (index < 0) {
494 previndex = index;
495 key = tableSize + index % tableSize;
496 oct = (index + 1) / tableSize - 1;
497 ZXP(out) = prevkey = table[key] + octave * oct;
498 } else if (index > maxindex) {
499 previndex = index;
500 key = index % tableSize;
501 oct = index / tableSize;
502 ZXP(out) = prevkey = table[key] + octave * oct;
503 } else {
504 previndex = index;
505 ZXP(out) = prevkey = table[index];
508 unit->mPrevIndex = previndex;
509 unit->mPrevKey = prevkey;
512 ////////////////////////////////////////////////////////////////////////////////////
514 void Select_Ctor(Select *unit)
516 if (BUFLENGTH == 1) {
517 SETCALC(Select_next_1);
518 } else if (INRATE(0) == calc_FullRate) {
519 SETCALC(Select_next_a);
520 } else {
521 SETCALC(Select_next_k);
523 Select_next_1(unit, 1);
526 void Select_next_1(Select *unit, int inNumSamples)
528 int32 maxindex = unit->mNumInputs - 1;
529 int32 index = (int32)ZIN0(0) + 1;
530 index = sc_clip(index, 1, maxindex);
531 ZOUT0(0) = ZIN0(index);
534 void Select_next_k(Select *unit, int inNumSamples)
536 int32 maxindex = unit->mNumInputs - 1;
537 int32 index = (int32)ZIN0(0) + 1;
538 index = sc_clip(index, 1, maxindex);
540 float *out = OUT(0);
541 float *in = IN(index);
543 Copy(inNumSamples, out, in);
546 void Select_next_a(Select *unit, int inNumSamples)
548 int32 maxindex = unit->mNumInputs - 1;
550 float *out = ZOUT(0);
551 float *in0 = ZIN(0);
552 float **in = unit->mInBuf;
554 for (int i=0; i<inNumSamples; ++i) {
555 int32 index = (int32)ZXP(in0) + 1;
556 index = sc_clip(index, 1, maxindex);
557 ZXP(out) = in[index][i];
560 ////////////////////////////////////////////////////////////////////////////////////
562 void TWindex_Ctor(TWindex *unit)
564 if (INRATE(0) == calc_FullRate) {
565 SETCALC(TWindex_next_ak); // todo : ar
566 } else {
567 SETCALC(TWindex_next_k);
569 unit->m_prevIndex = 0;
570 unit->m_trig = -1.f; // make it trigger the first time
571 TWindex_next_k(unit, 1);
575 void TWindex_next_k(TWindex *unit, int inNumSamples)
578 int maxindex = unit->mNumInputs;
579 int32 index = maxindex;
580 float sum = 0.f;
581 float maxSum = 0.f;
582 float normalize = ZIN0(1); // switch normalisation on or off
583 float trig = ZIN0(0);
584 float *out = ZOUT(0);
585 if (trig > 0.f && unit->m_trig <= 0.f) {
586 if(normalize == 1) {
587 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
588 } else {
589 maxSum = 1.f;
591 RGen& rgen = *unit->mParent->mRGen;
592 float max = maxSum * rgen.frand();
594 for (int32 k=2; k<maxindex; ++k) {
595 sum += ZIN0(k);
596 if(sum >= max) {
597 index = k - 2;
598 break;
602 unit->m_prevIndex = index;
603 } else {
604 index = unit->m_prevIndex;
607 LOOP1(inNumSamples,
608 ZXP(out) = index;
610 unit->m_trig = trig;
613 void TWindex_next_ak(TWindex *unit, int inNumSamples)
615 int maxindex = unit->mNumInputs;
616 int32 index = maxindex;
618 float sum = 0.f;
619 float maxSum = 0.f;
620 float normalize = ZIN0(1);//switch normalisation on or off
621 float *trig = ZIN(0);
622 float *out = ZOUT(0);
623 float curtrig;
624 if(normalize == 1) {
625 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
626 } else
627 maxSum = 1.f;
628 RGen& rgen = *unit->mParent->mRGen;
630 LOOP1(inNumSamples,
631 curtrig = ZXP(trig);
632 if (curtrig > 0.f && unit->m_trig <= 0.f) {
633 float max = maxSum * rgen.frand();
634 for (int32 k=2; k<maxindex; ++k) {
635 sum += ZIN0(k);
636 if(sum >= max) {
637 index = k - 2;
638 break;
642 unit->m_prevIndex = index;
643 } else
644 index = unit->m_prevIndex;
646 ZXP(out) = index;
647 unit->m_trig = curtrig;
651 ////////////////////////////////////////////////////////////////////////////////////
653 void Index_Ctor(Index *unit)
655 unit->m_fbufnum = -1e9f;
656 if (BUFLENGTH == 1) {
657 SETCALC(Index_next_1);
658 } else if (INRATE(0) == calc_FullRate) {
659 SETCALC(Index_next_a);
660 } else {
661 SETCALC(Index_next_k);
663 Index_next_1(unit, 1);
666 void Index_next_1(Index *unit, int inNumSamples)
668 // get table
669 GET_TABLE
670 const float *table = bufData;
671 int32 maxindex = tableSize - 1;
673 int32 index = (int32)ZIN0(1);
674 index = sc_clip(index, 0, maxindex);
675 ZOUT0(0) = table[index];
678 void Index_next_k(Index *unit, int inNumSamples)
680 // get table
681 GET_TABLE
682 const float *table = bufData;
683 int32 maxindex = tableSize - 1;
685 float *out = ZOUT(0);
687 int32 index = (int32)ZIN0(1);
689 index = sc_clip(index, 0, maxindex);
690 float val = table[index];
691 LOOP1(inNumSamples,
692 ZXP(out) = val;
697 void Index_next_a(Index *unit, int inNumSamples)
699 // get table
700 GET_TABLE
701 const float *table = bufData;
702 int32 maxindex = tableSize - 1;
704 float *out = ZOUT(0);
705 float *in = ZIN(1);
707 LOOP1(inNumSamples,
708 int32 index = (int32)ZXP(in);
709 index = sc_clip(index, 0, maxindex);
710 ZXP(out) = table[index];
715 ////////////////////////////////////////////////////////////////////////////////////
717 void IndexL_Ctor(IndexL *unit)
719 unit->m_fbufnum = -1e9f;
720 if (BUFLENGTH == 1) {
721 SETCALC(IndexL_next_1);
722 } else if (INRATE(0) == calc_FullRate) {
723 SETCALC(IndexL_next_a);
724 } else {
725 SETCALC(IndexL_next_k);
727 IndexL_next_1(unit, 1);
730 void IndexL_next_1(IndexL *unit, int inNumSamples)
732 // get table
733 GET_TABLE
734 const float *table = bufData;
735 int32 maxindex = tableSize - 1;
737 float findex = ZIN0(1);
738 float frac = sc_frac(findex);
740 int32 index = (int32)findex;
741 index = sc_clip(index, 0, maxindex);
743 float a = table[index];
744 float b = table[sc_clip(index + 1, 0, maxindex)];
745 ZOUT0(0) = lininterp(frac, a, b);
748 void IndexL_next_k(IndexL *unit, int inNumSamples)
750 // get table
751 GET_TABLE
752 const float *table = bufData;
753 int32 maxindex = tableSize - 1;
755 float *out = ZOUT(0);
757 float findex = ZIN0(1);
758 float frac = sc_frac(findex);
760 int32 index = (int32)findex;
761 index = sc_clip(index, 0, maxindex);
764 float a = table[index];
765 float b = table[sc_clip(index + 1, 0, maxindex)];
766 float val = lininterp(frac, a, b);
768 LOOP1(inNumSamples,
769 ZXP(out) = val;
774 void IndexL_next_a(IndexL *unit, int inNumSamples)
776 // get table
777 GET_TABLE
778 const float *table = bufData;
779 int32 maxindex = tableSize - 1;
781 float *out = ZOUT(0);
782 float *in = ZIN(1);
784 LOOP1(inNumSamples,
785 float findex = ZXP(in);
786 float frac = sc_frac(findex);
787 int32 i1 = sc_clip((int32)findex, 0, maxindex);
788 int32 i2 = sc_clip(i1 + 1, 0, maxindex);
789 float a = table[i1];
790 float b = table[i2];
791 ZXP(out) = lininterp(frac, a, b);
797 ////////////////////////////////////////////////////////////////////////////////////
800 void FoldIndex_Ctor(FoldIndex *unit)
802 unit->m_fbufnum = -1e9f;
803 if (BUFLENGTH == 1) {
804 SETCALC(FoldIndex_next_1);
805 } else if (INRATE(0) == calc_FullRate) {
806 SETCALC(FoldIndex_next_a);
807 } else {
808 SETCALC(FoldIndex_next_k);
810 FoldIndex_next_1(unit, 1);
813 void FoldIndex_next_1(FoldIndex *unit, int inNumSamples)
815 // get table
816 GET_TABLE
817 const float *table = bufData;
818 int32 maxindex = tableSize - 1;
820 int32 index = (int32)ZIN0(1);
821 index = sc_fold(index, 0, maxindex);
822 ZOUT0(0) = table[index];
825 void FoldIndex_next_k(FoldIndex *unit, int inNumSamples)
827 // get table
828 GET_TABLE
829 const float *table = bufData;
830 int32 maxindex = tableSize - 1;
832 int32 index = (int32)ZIN0(1);
833 float *out = ZOUT(0);
835 index = sc_fold(index, 0, maxindex);
836 float val = table[index];
837 LOOP1(inNumSamples,
838 ZXP(out) = val;
843 void FoldIndex_next_a(FoldIndex *unit, int inNumSamples)
845 // get table
846 GET_TABLE
847 const float *table = bufData;
848 int32 maxindex = tableSize - 1;
850 float *out = ZOUT(0);
851 float *in = ZIN(1);
853 LOOP1(inNumSamples,
854 int32 index = (int32)ZXP(in);
855 index = sc_fold(index, 0, maxindex);
856 ZXP(out) = table[index];
861 ////////////////////////////////////////////////////////////////////////////////////
863 void WrapIndex_Ctor(WrapIndex *unit)
865 unit->m_fbufnum = -1e9f;
866 if (BUFLENGTH == 1) {
867 SETCALC(WrapIndex_next_1);
868 } else if (INRATE(0) == calc_FullRate) {
869 SETCALC(WrapIndex_next_a);
870 } else {
871 SETCALC(WrapIndex_next_k);
873 WrapIndex_next_1(unit, 1);
876 void WrapIndex_next_1(WrapIndex *unit, int inNumSamples)
878 // get table
879 GET_TABLE
880 const float *table = bufData;
881 int32 maxindex = tableSize - 1;
883 int32 index = (int32)floor(ZIN0(1));
884 index = sc_wrap(index, 0, maxindex);
885 ZOUT0(0) = table[index];
888 void WrapIndex_next_k(WrapIndex *unit, int inNumSamples)
890 // get table
891 GET_TABLE
892 const float *table = bufData;
893 int32 maxindex = tableSize - 1;
895 float *out = ZOUT(0);
897 int32 index = (int32)ZIN0(1);
898 index = sc_wrap(index, 0, maxindex);
899 float val = table[index];
900 LOOP1(inNumSamples,
901 ZXP(out) = val;
906 void WrapIndex_next_a(WrapIndex *unit, int inNumSamples)
908 // get table
909 GET_TABLE
910 const float *table = bufData;
911 int32 maxindex = tableSize - 1;
913 float *out = ZOUT(0);
914 float *in = ZIN(1);
916 LOOP1(inNumSamples,
917 int32 index = (int32)ZXP(in);
918 index = sc_wrap(index, 0, maxindex);
919 ZXP(out) = table[index];
923 ////////////////////////////////////////////////////////////////////////////////////
925 static float IndexInBetween_FindIndex(const float* table, float in, int32 maxindex)
927 for(int32 i = 0; i <= maxindex; i++) {
928 if(table[i] > in) {
929 if(i == 0) {
930 return 0.f;
931 } else {
932 return ((in - table[i - 1]) / (table[i] - table[i - 1]) + i - 1);
936 return (float)maxindex;
939 void IndexInBetween_Ctor(IndexInBetween *unit)
941 unit->m_fbufnum = -1e9f;
942 if (BUFLENGTH == 1) {
943 SETCALC(IndexInBetween_next_1);
944 } else if (INRATE(0) == calc_FullRate) {
945 SETCALC(IndexInBetween_next_a);
946 } else {
947 SETCALC(IndexInBetween_next_k);
949 IndexInBetween_next_1(unit, 1);
952 void IndexInBetween_next_1(IndexInBetween *unit, int inNumSamples)
954 // get table
955 GET_TABLE
956 const float *table = bufData;
957 int32 maxindex = tableSize - 1;
959 float in = ZIN0(1);
960 ZOUT0(0) = IndexInBetween_FindIndex(table, in, maxindex);
963 void IndexInBetween_next_k(IndexInBetween *unit, int inNumSamples)
965 // get table
966 GET_TABLE
967 const float *table = bufData;
968 int32 maxindex = tableSize - 1;
970 float *out = ZOUT(0);
971 float in = ZIN0(1);
973 float val = IndexInBetween_FindIndex(table, in, maxindex);
974 LOOP1(inNumSamples,
975 ZXP(out) = val;
980 void IndexInBetween_next_a(IndexInBetween *unit, int inNumSamples)
982 // get table
983 GET_TABLE
984 const float *table = bufData;
985 int32 maxindex = tableSize - 1;
987 float *out = ZOUT(0);
988 float *in = ZIN(1);
990 LOOP1(inNumSamples,
991 ZXP(out) = IndexInBetween_FindIndex(table, ZXP(in), maxindex);
996 ////////////////////////////////////////////////////////////////////////////////////
998 static int32 DetectIndex_FindIndex(const float* table, float in, int32 maxindex)
1000 int32 index;
1001 for(index = 0; index <= maxindex; index+=1) {
1002 if(table[index] == in) {
1003 return index;
1006 return -1;
1009 void DetectIndex_Ctor(DetectIndex *unit)
1011 unit->m_fbufnum = -1e9f;
1012 if (BUFLENGTH == 1) {
1013 SETCALC(DetectIndex_next_1);
1014 } else if (INRATE(0) == calc_FullRate) {
1015 SETCALC(DetectIndex_next_a);
1016 } else {
1017 SETCALC(DetectIndex_next_k);
1019 unit->mPrev = -1.f;
1020 DetectIndex_next_1(unit, 1);
1023 void DetectIndex_next_1(DetectIndex *unit, int inNumSamples)
1025 // get table
1026 GET_TABLE
1027 const float *table = bufData;
1028 int32 maxindex = tableSize - 1;
1030 float in = ZIN0(1);
1031 int32 index;
1032 if(in == unit->mPrevIn) {
1033 index = (int32)unit->mPrev;
1034 } else {
1035 index = DetectIndex_FindIndex(table, in, maxindex);
1036 unit->mPrev = index;
1037 unit->mPrevIn = in;
1039 ZOUT0(0) = (float)index;
1042 void DetectIndex_next_k(DetectIndex *unit, int inNumSamples)
1044 // get table
1045 GET_TABLE
1046 const float *table = bufData;
1047 int32 maxindex = tableSize - 1;
1049 float *out = ZOUT(0);
1050 float in = ZIN0(1);
1051 int32 index;
1052 float val;
1053 if(in == unit->mPrevIn) {
1054 index = (int32)unit->mPrev;
1055 } else {
1056 index = DetectIndex_FindIndex(table, in, maxindex);
1057 unit->mPrev = index;
1058 unit->mPrevIn = in;
1060 val = (float)index;
1061 LOOP1(inNumSamples,
1062 ZXP(out) = val;
1067 void DetectIndex_next_a(DetectIndex *unit, int inNumSamples)
1069 // get table
1070 GET_TABLE
1071 const float *table = bufData;
1072 int32 maxindex = tableSize - 1;
1074 float *out = ZOUT(0);
1075 float *in = ZIN(1);
1076 float prev = unit->mPrevIn;
1077 int32 prevIndex = (int32)unit->mPrev;
1078 float inval;
1080 LOOP1(inNumSamples,
1081 inval = ZXP(in);
1082 if(inval != prev) {
1083 prevIndex = DetectIndex_FindIndex(table, inval, maxindex);
1085 prev = inval;
1086 ZXP(out) = (float)prevIndex;
1089 unit->mPrev = prevIndex;
1090 unit->mPrevIn = inval;
1094 ////////////////////////////////////////////////////////////////////////////////////
1096 void Shaper_Ctor(Shaper *unit)
1098 unit->m_fbufnum = -1e9f;
1099 if (BUFLENGTH == 1) {
1100 SETCALC(Shaper_next_1);
1101 } else if (INRATE(1) == calc_FullRate) {
1102 SETCALC(Shaper_next_a);
1103 } else {
1104 SETCALC(Shaper_next_k);
1106 unit->mPrevIn = ZIN0(0);
1107 Shaper_next_1(unit, 1);
1110 void Shaper_next_1(Shaper *unit, int inNumSamples)
1112 // get table
1113 GET_TABLE
1114 const float *table0 = bufData;
1115 const float *table1 = table0 + 1;
1116 float fmaxindex = (float)(tableSize>>1) - 0.001;
1117 float offset = tableSize * 0.25;
1119 float val;
1121 float fin = ZIN0(1);
1122 float findex = offset + fin * offset;
1123 findex = sc_clip(findex, 0.f, fmaxindex);
1124 int32 index = (int32)findex;
1125 float pfrac = findex - (index - 1);
1126 index <<= 3;
1127 float val1 = *(float*)((char*)table0 + index);
1128 float val2 = *(float*)((char*)table1 + index);
1129 val = val1 + val2 * pfrac;
1130 ZOUT0(0) = val;
1133 void Shaper_next_k(Shaper *unit, int inNumSamples)
1135 // get table
1136 GET_TABLE
1137 const float *table0 = bufData;
1138 const float *table1 = table0 + 1;
1139 float fmaxindex = (float)(tableSize>>1) - 0.001;
1140 float offset = tableSize * 0.25;
1142 float *out = ZOUT(0);
1144 float val;
1146 float fin = ZIN0(1);
1147 float phaseinc = (fin - unit->mPrevIn) * offset;
1148 unit->mPrevIn = fin;
1150 LOOP1(inNumSamples,
1151 float findex = offset + fin * offset;
1152 findex = sc_clip(findex, 0.f, fmaxindex);
1153 int32 index = (int32)findex;
1154 float pfrac = findex - (index - 1);
1155 index <<= 3;
1156 float val1 = *(float*)((char*)table0 + index);
1157 float val2 = *(float*)((char*)table1 + index);
1158 val = val1 + val2 * pfrac;
1159 ZXP(out) = val;
1160 fin += phaseinc;
1164 void Shaper_next_a(Shaper *unit, int inNumSamples)
1166 // get table
1167 GET_TABLE
1168 const float *table0 = bufData;
1169 const float *table1 = table0 + 1;
1170 float fmaxindex = (float)(tableSize>>1) - 0.001;
1171 float offset = tableSize * 0.25;
1173 float *out = ZOUT(0);
1174 float *in = ZIN(1);
1175 float val;
1177 LOOP1(inNumSamples,
1178 float fin = ZXP(in);
1179 float findex = offset + fin * offset;
1180 findex = sc_clip(findex, 0.f, fmaxindex);
1181 int32 index = (int32)findex;
1182 float pfrac = findex - (index - 1);
1183 index <<= 3;
1184 float val1 = *(float*)((char*)table0 + index);
1185 float val2 = *(float*)((char*)table1 + index);
1186 val = val1 + val2 * pfrac;
1187 ZXP(out) = val;
1193 ////////////////////////////////////////////////////////////////////////////////////
1195 void SigOsc_Ctor(SigOsc *unit)
1197 unit->m_fbufnum = -1e9f;
1198 if (BUFLENGTH == 1) {
1199 SETCALC(SigOsc_next_1);
1200 } else if (INRATE(0) == calc_FullRate) {
1201 SETCALC(SigOsc_next_a);
1202 } else {
1203 SETCALC(SigOsc_next_k);
1205 unit->mPhase = 0.f;
1206 SigOsc_next_1(unit, 1);
1210 void SigOsc_next_1(SigOsc *unit, int inNumSamples)
1212 // get table
1213 GET_TABLE
1214 const float *table0 = bufData;
1215 const float *table1 = table0 + 1;
1216 int32 maxindex = tableSize - 1;
1217 float maxphase = (float)maxindex;
1218 if (tableSize != unit->mTableSize) {
1219 unit->mTableSize = tableSize;
1220 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1223 float phase = unit->mPhase;
1225 while (phase < 0.f) phase += maxphase;
1226 while (phase >= maxphase) phase -= maxphase;
1227 int32 iphase = (int32)phase;
1228 float pfrac = phase - iphase;
1229 float val1 = *(float*)((char*)table0 + iphase);
1230 float val2 = *(float*)((char*)table1 + iphase);
1231 float val = lininterp(pfrac, val1, val2);
1232 phase += ZIN0(1) * unit->m_cpstoinc;
1234 ZOUT0(0) = val;
1236 unit->mPhase = phase;
1239 void SigOsc_next_k(SigOsc *unit, int inNumSamples)
1241 // get table
1242 GET_TABLE
1243 const float *table0 = bufData;
1244 const float *table1 = table0 + 1;
1245 int32 maxindex = tableSize - 1;
1246 float maxphase = (float)maxindex;
1247 if (tableSize != unit->mTableSize) {
1248 unit->mTableSize = tableSize;
1249 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1252 float *out = ZOUT(0);
1254 float phase = unit->mPhase;
1256 float freq = ZIN0(1) * unit->m_cpstoinc;
1258 LOOP1(inNumSamples,
1259 while (phase < 0.f) phase += maxphase;
1260 while (phase >= maxphase) phase -= maxphase;
1261 int32 iphase = (int32)phase;
1262 float pfrac = phase - iphase;
1263 float val1 = *(float*)((char*)table0 + iphase);
1264 float val2 = *(float*)((char*)table1 + iphase);
1265 float val = lininterp(pfrac, val1, val2);
1266 phase += freq;
1268 ZXP(out) = val;
1271 unit->mPhase = phase;
1274 void SigOsc_next_a(SigOsc *unit, int inNumSamples)
1276 // get table
1277 GET_TABLE
1278 const float *table0 = bufData;
1279 const float *table1 = table0 + 1;
1280 int32 maxindex = tableSize - 1;
1281 float maxphase = (float)maxindex;
1282 if (tableSize != unit->mTableSize) {
1283 unit->mTableSize = tableSize;
1284 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1287 float *out = ZOUT(0);
1288 float *freqin = ZIN(1);
1289 float phase = unit->mPhase;
1290 float cpstoinc = unit->m_cpstoinc;
1292 LOOP1(inNumSamples,
1293 while (phase < 0.f) phase += maxphase;
1294 while (phase >= maxphase) phase -= maxphase;
1295 int32 iphase = (int32)phase;
1296 float pfrac = phase - iphase;
1297 float val1 = *(float*)((char*)table0 + iphase);
1298 float val2 = *(float*)((char*)table1 + iphase);
1299 float val = lininterp(pfrac, val1, val2);
1300 phase += ZXP(freqin) * cpstoinc;
1302 ZXP(out) = val;
1305 unit->mPhase = phase;
1309 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1311 void FSinOsc_Ctor(FSinOsc *unit)
1313 SETCALC(FSinOsc_next);
1314 unit->m_freq = ZIN0(0);
1315 float iphase = ZIN0(1);
1316 float w = unit->m_freq * unit->mRate->mRadiansPerSample;
1317 unit->m_b1 = 2. * cos(w);
1318 unit->m_y1 = sin(iphase);
1319 unit->m_y2 = sin(iphase - w);
1321 ZOUT0(0) = unit->m_y1;
1324 void FSinOsc_next(FSinOsc *unit, int inNumSamples)
1326 float *out = ZOUT(0);
1327 double freq = ZIN0(0);
1328 double b1;
1329 if (freq != unit->m_freq) {
1330 unit->m_freq = freq;
1331 double w = freq * unit->mRate->mRadiansPerSample;
1332 unit->m_b1 = b1 = 2.f * cos(w);
1333 } else {
1334 b1 = unit->m_b1;
1336 double y0;
1337 double y1 = unit->m_y1;
1338 double y2 = unit->m_y2;
1339 //Print("y %g %g b1 %g\n", y1, y2, b1);
1340 //Print("%d %d\n", unit->mRate->mFilterLoops, unit->mRate->mFilterRemain);
1341 LOOP(unit->mRate->mFilterLoops,
1342 ZXP(out) = y0 = b1 * y1 - y2;
1343 ZXP(out) = y2 = b1 * y0 - y1;
1344 ZXP(out) = y1 = b1 * y2 - y0;
1346 LOOP(unit->mRate->mFilterRemain,
1347 ZXP(out) = y0 = b1 * y1 - y2;
1348 y2 = y1;
1349 y1 = y0;
1351 //Print("y %g %g b1 %g\n", y1, y2, b1);
1352 unit->m_y1 = y1;
1353 unit->m_y2 = y2;
1354 unit->m_b1 = b1;
1357 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1359 void PSinGrain_Ctor(PSinGrain *unit)
1361 SETCALC(PSinGrain_next);
1362 float freq = ZIN0(0);
1363 float dur = ZIN0(1);
1364 float amp = ZIN0(2);
1366 float w = freq * unit->mRate->mRadiansPerSample;
1367 float sdur = SAMPLERATE * dur;
1369 float rdur = 1.f / sdur;
1370 float rdur2 = rdur * rdur;
1372 unit->m_level = 0.f;
1373 unit->m_slope = 4.0 * (rdur - rdur2); // ampslope
1374 unit->m_curve = -8.0 * rdur2; // ampcurve
1375 unit->mCounter = (int32)(sdur + .5);
1377 /* calc feedback param and initial conditions */
1378 unit->m_b1 = 2. * cos(w);
1379 unit->m_y1 = 0.f;
1380 unit->m_y2 = -sin(w) * amp;
1381 ZOUT0(0) = 0.f;
1385 void PSinGrain_next(PSinGrain *unit, int inNumSamples)
1387 float *out = ZOUT(0);
1388 float y0;
1389 float y1 = unit->m_y1;
1390 float y2 = unit->m_y2;
1391 float b1 = unit->m_b1;
1392 float level = unit->m_level;
1393 float slope = unit->m_slope;
1394 float curve = unit->m_curve;
1395 int32 counter = unit->mCounter;
1396 int32 remain = inNumSamples;
1397 int32 nsmps;
1398 do {
1399 if (counter<=0) {
1400 nsmps = remain;
1401 remain = 0;
1402 LOOP(nsmps, ZXP(out) = 0.f;); // can't use Clear bcs might not be aligned
1403 } else {
1404 nsmps = sc_min(remain, counter);
1405 remain -= nsmps;
1406 counter -= nsmps;
1407 if (nsmps == inNumSamples) {
1408 nsmps = unit->mRate->mFilterLoops;
1409 LOOP(nsmps,
1410 y0 = b1 * y1 - y2;
1411 ZXP(out) = y0 * level;
1412 level += slope;
1413 slope += curve;
1414 y2 = b1 * y0 - y1;
1415 ZXP(out) = y2 * level;
1416 level += slope;
1417 slope += curve;
1418 y1 = b1 * y2 - y0;
1419 ZXP(out) = y1 * level;
1420 level += slope;
1421 slope += curve;
1423 nsmps = unit->mRate->mFilterRemain;
1424 LOOP(nsmps,
1425 y0 = b1 * y1 - y2;
1426 y2 = y1;
1427 y1 = y0;
1428 ZXP(out) = y0 * level;
1429 level += slope;
1430 slope += curve;
1432 } else {
1433 LOOP(nsmps,
1434 y0 = b1 * y1 - y2;
1435 y2 = y1;
1436 y1 = y0;
1437 ZXP(out) = y0 * level;
1438 level += slope;
1439 slope += curve;
1442 if (counter == 0) {
1443 NodeEnd(&unit->mParent->mNode);
1446 } while (remain>0);
1447 unit->m_level = level;
1448 unit->m_slope = slope;
1449 unit->m_y1 = y1;
1450 unit->m_y2 = y2;
1453 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1455 //////////////!!!
1457 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1459 float *out = ZOUT(0);
1460 float freqin = ZIN0(0);
1461 float phasein = ZIN0(1);
1463 float *table0 = ft->mSineWavetable;
1464 float *table1 = table0 + 1;
1466 int32 phase = unit->m_phase;
1467 int32 lomask = unit->m_lomask;
1469 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1470 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1471 unit->m_phasein = phasein;
1473 LOOP1(inNumSamples,
1474 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1475 phase += phaseinc;
1477 unit->m_phase = phase;
1480 #if __VEC__
1482 void vSinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1484 vfloat32 *vout = (vfloat32*)OUT(0);
1485 float freqin = ZIN0(0);
1486 float phasein = ZIN0(1);
1488 float *table0 = ft->mSineWavetable;
1489 float *table1 = table0 + 1;
1491 int32 phase = unit->m_phase;
1492 int32 lomask = unit->m_lomask;
1494 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1495 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1496 unit->m_phasein = phasein;
1498 vint32 vphase = vload(phase, phase+phaseinc, phase+2*phaseinc, phase+3*phaseinc);
1499 vint32 vphaseinc = vload(phaseinc << 2);
1500 vint32 v3F800000 = (vint32)vinit(0x3F800000);
1501 vint32 v007FFF80 = (vint32)vinit(0x007FFF80);
1502 vint32 vlomask = vload(lomask);
1503 vuint32 vxlobits1 = (vuint32)vinit(xlobits1);
1504 vuint32 v7 = (vuint32)vinit(7);
1506 vint32 vtable0 = vload((int32)table0); // assuming 32 bit pointers
1507 vint32 vtable1 = vload((int32)table1); // assuming 32 bit pointers
1509 int len = inNumSamples << 2;
1510 for (int i=0; i<len; i+=16) {
1512 vfloat32 vfrac = (vfloat32)(vec_or(v3F800000, vec_and(v007FFF80, vec_sl(vphase, v7))));
1513 vint32 vindex = vec_and(vec_sr(vphase, vxlobits1), vlomask);
1514 vec_union vaddr0, vaddr1;
1515 vaddr0.vi = vec_add(vindex, vtable0);
1516 vaddr1.vi = vec_add(vindex, vtable1);
1518 vec_union vval1, vval2;
1519 vval1.f[0] = *(float*)(vaddr0.i[0]);
1520 vval2.f[0] = *(float*)(vaddr1.i[0]);
1521 vval1.f[1] = *(float*)(vaddr0.i[1]);
1522 vval2.f[1] = *(float*)(vaddr1.i[1]);
1523 vval1.f[2] = *(float*)(vaddr0.i[2]);
1524 vval2.f[2] = *(float*)(vaddr1.i[2]);
1525 vval1.f[3] = *(float*)(vaddr0.i[3]);
1526 vval2.f[3] = *(float*)(vaddr1.i[3]);
1528 vec_st(vec_madd(vval2.vf, vfrac, vval1.vf), i, vout);
1529 vphase = vec_add(vphase, vphaseinc);
1531 unit->m_phase = phase + inNumSamples * phaseinc;
1535 #endif
1537 void SinOsc_next_ika(SinOsc *unit, int inNumSamples)
1539 float *out = ZOUT(0);
1540 float freqin = ZIN0(0);
1541 float *phasein = ZIN(1);
1543 float *table0 = ft->mSineWavetable;
1544 float *table1 = table0 + 1;
1546 int32 phase = unit->m_phase;
1547 int32 lomask = unit->m_lomask;
1549 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1550 float radtoinc = unit->m_radtoinc;
1551 //Print("SinOsc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1552 LOOP1(inNumSamples,
1553 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1554 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1555 phase += freq;
1557 unit->m_phase = phase;
1558 //unit->m_phasein = phasein;
1562 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples)
1564 float *out = ZOUT(0);
1565 float *freqin = ZIN(0);
1566 float *phasein = ZIN(1);
1568 float *table0 = ft->mSineWavetable;
1569 float *table1 = table0 + 1;
1571 int32 phase = unit->m_phase;
1572 int32 lomask = unit->m_lomask;
1574 float cpstoinc = unit->m_cpstoinc;
1575 float radtoinc = unit->m_radtoinc;
1576 //Print("SinOsc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1577 LOOP1(inNumSamples,
1578 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1579 float z = lookupi1(table0, table1, phaseoffset, lomask);
1580 phase += (int32)(cpstoinc * ZXP(freqin));
1581 ZXP(out) = z;
1583 unit->m_phase = phase;
1584 //unit->m_phasein = ZX(phasein);
1588 void SinOsc_next_iak(SinOsc *unit, int inNumSamples)
1590 float *out = ZOUT(0);
1591 float *freqin = ZIN(0);
1592 float phasein = ZIN0(1);
1594 float *table0 = ft->mSineWavetable;
1595 float *table1 = table0 + 1;
1597 int32 phase = unit->m_phase;
1598 int32 lomask = unit->m_lomask;
1600 float cpstoinc = unit->m_cpstoinc;
1601 float radtoinc = unit->m_radtoinc;
1602 float phasemod = unit->m_phasein;
1603 float phaseslope = CALCSLOPE(phasein, phasemod);
1605 LOOP1(inNumSamples,
1606 int32 pphase = phase + (int32)(radtoinc * phasemod);
1607 phasemod += phaseslope;
1608 float z = lookupi1(table0, table1, pphase, lomask);
1609 phase += (int32)(cpstoinc * ZXP(freqin));
1610 ZXP(out) = z;
1612 unit->m_phase = phase;
1613 unit->m_phasein = phasein;
1617 void SinOsc_Ctor(SinOsc *unit)
1619 int tableSize2 = ft->mSineSize;
1620 unit->m_phasein = ZIN0(1);
1621 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1622 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1623 unit->m_lomask = (tableSize2 - 1) << 3;
1625 if (INRATE(0) == calc_FullRate) {
1626 if (INRATE(1) == calc_FullRate) {
1627 //Print("next_iaa\n");
1628 SETCALC(SinOsc_next_iaa);
1629 unit->m_phase = 0;
1630 } else {
1631 //Print("next_iak\n");
1632 SETCALC(SinOsc_next_iak);
1633 unit->m_phase = 0;
1635 } else {
1636 if (INRATE(1) == calc_FullRate) {
1637 //Print("next_ika\n");
1638 SETCALC(SinOsc_next_ika);
1639 unit->m_phase = 0;
1640 } else {
1641 #if __VEC__
1642 if (USEVEC) {
1643 //Print("vSinOsc_next_ikk\n");
1644 SETCALC(vSinOsc_next_ikk);
1645 } else {
1646 //Print("SinOsc_next_ikk\n");
1647 SETCALC(SinOsc_next_ikk);
1649 #else
1650 //Print("next_ikk\n");
1651 SETCALC(SinOsc_next_ikk);
1652 #endif
1653 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1657 SinOsc_next_ikk(unit, 1);
1661 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1662 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1663 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1665 //////////////!!!
1667 void SinOscFB_next_ik(SinOscFB *unit, int inNumSamples)
1669 float *out = ZOUT(0);
1670 float freqin = ZIN0(0);
1672 float feedback = unit->m_feedback;
1673 float nextFeedback = ZIN0(1) * unit->m_radtoinc;
1675 float *table0 = ft->mSineWavetable;
1676 float *table1 = table0 + 1;
1678 int32 phase = unit->m_phase;
1679 int32 lomask = unit->m_lomask;
1680 float prevout = unit->m_prevout;
1681 float feedback_slope = CALCSLOPE(nextFeedback, feedback);
1682 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1684 LooP(inNumSamples) {
1685 prevout = lookupi1(table0, table1, phase + (int32)(feedback * prevout), lomask);
1686 ZXP(out) = prevout;
1687 phase += freq;
1688 feedback += feedback_slope;
1690 unit->m_phase = phase;
1691 unit->m_prevout = prevout;
1692 unit->m_feedback = feedback;
1695 void SinOscFB_Ctor(SinOscFB *unit)
1697 //Print("next_ik\n");
1698 SETCALC(SinOscFB_next_ik);
1700 int tableSize2 = ft->mSineSize;
1701 unit->m_lomask = (tableSize2 - 1) << 3;
1702 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1703 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1704 unit->m_prevout = 0.;
1705 unit->m_feedback = ZIN0(1) * unit->m_radtoinc;
1707 unit->m_phase = 0;
1709 SinOscFB_next_ik(unit, 1);
1713 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1714 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1716 void Osc_Ctor(Osc *unit)
1718 unit->mTableSize = -1;
1720 float fbufnum = ZIN0(0);
1721 uint32 bufnum = (uint32)fbufnum;
1722 World *world = unit->mWorld;
1724 SndBuf *buf;
1725 if (bufnum >= world->mNumSndBufs) {
1726 int localBufNum = bufnum - world->mNumSndBufs;
1727 Graph *parent = unit->mParent;
1728 if(localBufNum <= parent->localBufNum) {
1729 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1730 } else {
1731 buf = unit->m_buf = world->mSndBufs;
1733 } else {
1734 buf = unit->m_buf = world->mSndBufs + bufnum;
1737 int tableSize = buf->samples;
1738 int tableSize2 = tableSize >> 1;
1739 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1741 unit->m_phasein = ZIN0(2);
1743 if (INRATE(1) == calc_FullRate) {
1744 if (INRATE(2) == calc_FullRate) {
1745 //Print("next_iaa\n");
1746 SETCALC(Osc_next_iaa);
1747 unit->m_phase = 0;
1748 } else {
1749 //Print("next_iak\n");
1750 SETCALC(Osc_next_iak);
1751 unit->m_phase = 0;
1753 } else {
1754 if (INRATE(2) == calc_FullRate) {
1755 //Print("next_ika\n");
1756 SETCALC(Osc_next_ika);
1757 unit->m_phase = 0;
1758 } else {
1759 //Print("next_ikk\n");
1760 SETCALC(Osc_next_ikk);
1761 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1765 Osc_next_ikk(unit, 1);
1768 //////////////!!!
1770 void Osc_next_ikk(Osc *unit, int inNumSamples)
1772 // get table
1773 GET_TABLE
1774 const float *table0 = bufData;
1775 const float *table1 = table0 + 1;
1776 if (tableSize != unit->mTableSize) {
1777 unit->mTableSize = tableSize;
1778 int tableSize2 = tableSize >> 1;
1779 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1780 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1781 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1782 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1785 float *out = ZOUT(0);
1786 float freqin = ZIN0(1);
1787 float phasein = ZIN0(2);
1789 int32 phase = unit->m_phase;
1790 int32 lomask = unit->m_lomask;
1792 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1793 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1794 unit->m_phasein = phasein;
1796 LOOP1(inNumSamples,
1797 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1798 phase += phaseinc;
1800 unit->m_phase = phase;
1804 void Osc_next_ika(Osc *unit, int inNumSamples)
1806 // get table
1807 GET_TABLE
1808 const float *table0 = bufData;
1809 const float *table1 = table0 + 1;
1810 if (tableSize != unit->mTableSize) {
1811 unit->mTableSize = tableSize;
1812 int tableSize2 = tableSize >> 1;
1813 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1814 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1815 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1816 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1819 float *out = ZOUT(0);
1820 float freqin = ZIN0(1);
1821 float *phasein = ZIN(2);
1823 int32 phase = unit->m_phase;
1824 int32 lomask = unit->m_lomask;
1826 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1827 float radtoinc = unit->m_radtoinc;
1828 //Print("Osc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1829 LOOP1(inNumSamples,
1830 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1831 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1832 phase += freq;
1834 unit->m_phase = phase;
1835 //unit->m_phasein = phasein;
1838 void Osc_next_iaa(Osc *unit, int inNumSamples)
1840 // get table
1841 GET_TABLE
1842 const float *table0 = bufData;
1843 const float *table1 = table0 + 1;
1844 if (tableSize != unit->mTableSize) {
1845 unit->mTableSize = tableSize;
1846 int tableSize2 = tableSize >> 1;
1847 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1848 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1849 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1850 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1853 float *out = ZOUT(0);
1854 float *freqin = ZIN(1);
1855 float *phasein = ZIN(2);
1857 int32 phase = unit->m_phase;
1858 int32 lomask = unit->m_lomask;
1860 float cpstoinc = unit->m_cpstoinc;
1861 float radtoinc = unit->m_radtoinc;
1862 //Print("Osc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1863 LOOP1(inNumSamples,
1864 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1865 float z = lookupi1(table0, table1, phaseoffset, lomask);
1866 phase += (int32)(cpstoinc * ZXP(freqin));
1867 ZXP(out) = z;
1869 unit->m_phase = phase;
1870 //unit->m_phasein = ZX(phasein);
1874 void Osc_next_iak(Osc *unit, int inNumSamples)
1876 // get table
1877 GET_TABLE
1878 const float *table0 = bufData;
1879 const float *table1 = table0 + 1;
1880 if (tableSize != unit->mTableSize) {
1881 unit->mTableSize = tableSize;
1882 int tableSize2 = tableSize >> 1;
1883 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1884 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1885 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1886 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1889 float *out = ZOUT(0);
1890 float *freqin = ZIN(1);
1891 float phasein = ZIN0(2);
1893 int32 phase = unit->m_phase;
1894 int32 lomask = unit->m_lomask;
1896 float cpstoinc = unit->m_cpstoinc;
1897 float radtoinc = unit->m_radtoinc;
1898 float phasemod = unit->m_phasein;
1899 float phaseslope = CALCSLOPE(phasein, phasemod);
1901 LOOP1(inNumSamples,
1902 int32 pphase = phase + (int32)(radtoinc * phasemod);
1903 phasemod += phaseslope;
1904 float z = lookupi1(table0, table1, pphase, lomask);
1905 phase += (int32)(cpstoinc * ZXP(freqin));
1906 ZXP(out) = z;
1908 unit->m_phase = phase;
1909 unit->m_phasein = phasein;
1912 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1914 void OscN_Ctor(OscN *unit)
1916 unit->mTableSize = -1;
1918 float fbufnum = ZIN0(0);
1919 uint32 bufnum = (uint32)fbufnum;
1920 World *world = unit->mWorld;
1921 SndBuf *buf;
1922 if (bufnum >= world->mNumSndBufs) {
1923 int localBufNum = bufnum - world->mNumSndBufs;
1924 Graph *parent = unit->mParent;
1925 if(localBufNum <= parent->localBufNum) {
1926 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1927 } else {
1928 buf = unit->m_buf = world->mSndBufs;
1930 } else {
1931 buf = unit->m_buf = world->mSndBufs + bufnum;
1934 int tableSize = buf->samples;
1935 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
1937 unit->m_phasein = ZIN0(2);
1938 //Print("OscN_Ctor\n");
1939 if (INRATE(1) == calc_FullRate) {
1940 if (INRATE(2) == calc_FullRate) {
1941 //Print("next_naa\n");
1942 SETCALC(OscN_next_naa);
1943 unit->m_phase = 0;
1944 } else {
1945 //Print("next_nak\n");
1946 SETCALC(OscN_next_nak);
1947 unit->m_phase = 0;
1949 } else {
1950 if (INRATE(2) == calc_FullRate) {
1951 //Print("next_nka\n");
1952 SETCALC(OscN_next_nka);
1953 unit->m_phase = 0;
1954 } else {
1955 //Print("next_nkk\n");
1956 SETCALC(OscN_next_nkk);
1957 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1961 OscN_next_nkk(unit, 1);
1965 void OscN_next_nkk(OscN *unit, int inNumSamples)
1967 // get table
1968 GET_TABLE
1969 const float *table = bufData;
1970 if (tableSize != unit->mTableSize) {
1971 unit->mTableSize = tableSize;
1972 unit->m_lomask = (tableSize - 1) << 2;
1973 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
1974 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1977 float *out = ZOUT(0);
1978 float freqin = ZIN0(1);
1979 float phasein = ZIN0(2);
1981 int32 phase = unit->m_phase;
1982 int32 lomask = unit->m_lomask;
1984 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1985 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1986 unit->m_phasein = phasein;
1988 LOOP1(inNumSamples,
1989 ZXP(out) = *(float*)((char*)table + ((phase >> xlobits) & lomask));
1990 phase += phaseinc;
1992 unit->m_phase = phase;
1997 void OscN_next_nka(OscN *unit, int inNumSamples)
1999 // get table
2000 GET_TABLE
2001 const float *table = bufData;
2002 if (tableSize != unit->mTableSize) {
2003 unit->mTableSize = tableSize;
2004 unit->m_lomask = (tableSize - 1) << 2;
2005 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2006 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2009 float *out = ZOUT(0);
2010 float freqin = ZIN0(1);
2011 float *phasein = ZIN(2);
2013 int32 phase = unit->m_phase;
2014 int32 lomask = unit->m_lomask;
2016 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2017 float radtoinc = unit->m_radtoinc;
2018 LOOP1(inNumSamples,
2019 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2020 ZXP(out) = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2021 phase += freq;
2023 unit->m_phase = phase;
2026 void OscN_next_naa(OscN *unit, int inNumSamples)
2028 // get table
2029 GET_TABLE
2030 const float *table = bufData;
2031 if (tableSize != unit->mTableSize) {
2032 unit->mTableSize = tableSize;
2033 unit->m_lomask = (tableSize - 1) << 2;
2034 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2035 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2038 float *out = ZOUT(0);
2039 float *freqin = ZIN(1);
2040 float *phasein = ZIN(2);
2042 int32 phase = unit->m_phase;
2043 int32 lomask = unit->m_lomask;
2045 float cpstoinc = unit->m_cpstoinc;
2046 float radtoinc = unit->m_radtoinc;
2047 LOOP1(inNumSamples,
2048 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2049 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2050 phase += (int32)(cpstoinc * ZXP(freqin));
2051 ZXP(out) = z;
2053 unit->m_phase = phase;
2057 void OscN_next_nak(OscN *unit, int inNumSamples)
2059 // get table
2060 GET_TABLE
2061 const float *table = bufData;
2062 if (tableSize != unit->mTableSize) {
2063 unit->mTableSize = tableSize;
2064 unit->m_lomask = (tableSize - 1) << 2;
2065 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2066 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2069 float *out = ZOUT(0);
2070 float *freqin = ZIN(1);
2071 float phasein = ZIN0(2);
2073 int32 phase = unit->m_phase;
2074 int32 lomask = unit->m_lomask;
2076 float cpstoinc = unit->m_cpstoinc;
2077 float radtoinc = unit->m_radtoinc;
2078 float phasemod = unit->m_phasein;
2079 float phaseslope = CALCSLOPE(phasein, phasemod);
2081 LOOP1(inNumSamples,
2082 int32 pphase = phase + (int32)(radtoinc * phasemod);
2083 phasemod += phaseslope;
2084 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2085 phase += (int32)(cpstoinc * ZXP(freqin));
2086 ZXP(out) = z;
2088 unit->m_phase = phase;
2089 unit->m_phasein = phasein;
2092 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2094 void COsc_Ctor(COsc *unit)
2096 unit->m_fbufnum = -1e9f;
2097 SETCALC(COsc_next);
2098 unit->m_phase1 = 0;
2099 unit->m_phase2 = 0;
2100 unit->mTableSize = -1;
2101 COsc_next(unit, 1);
2105 void COsc_next(COsc *unit, int inNumSamples)
2107 // get table
2108 GET_TABLE
2109 const float *table0 = bufData;
2110 const float *table1 = table0 + 1;
2111 if (tableSize != unit->mTableSize) {
2112 unit->mTableSize = tableSize;
2113 int tableSize2 = tableSize >> 1;
2114 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
2115 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
2116 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2119 float *out = ZOUT(0);
2120 float freqin = ZIN0(1);
2121 float beats = ZIN0(2) * 0.5f;
2123 int32 phase1 = unit->m_phase1;
2124 int32 phase2 = unit->m_phase2;
2125 int32 lomask = unit->m_lomask;
2127 int32 cfreq = (int32)(unit->m_cpstoinc * freqin);
2128 int32 beatf = (int32)(unit->m_cpstoinc * beats);
2129 int32 freq1 = cfreq + beatf;
2130 int32 freq2 = cfreq - beatf;
2131 LOOP1(inNumSamples,
2132 float a = lookupi1(table0, table1, phase1, lomask);
2133 float b = lookupi1(table0, table1, phase2, lomask);
2134 ZXP(out) = a + b;
2135 phase1 += freq1;
2136 phase2 += freq2;
2138 unit->m_phase1 = phase1;
2139 unit->m_phase2 = phase2;
2142 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2143 #define VOSC_GET_BUF_UNLOCKED \
2144 const SndBuf *bufs;\
2145 if (bufnum+1 >= world->mNumSndBufs) { \
2146 int localBufNum = bufnum - world->mNumSndBufs; \
2147 Graph *parent = unit->mParent; \
2148 if(localBufNum <= parent->localBufNum) { \
2149 bufs = parent->mLocalSndBufs + localBufNum; \
2150 } else { \
2151 bufnum = 0; \
2152 bufs = world->mSndBufs + bufnum; \
2154 } else { \
2155 bufs = world->mSndBufs + sc_max(0, bufnum); \
2158 #define VOSC_GET_BUF \
2159 VOSC_GET_BUF_UNLOCKED \
2160 LOCK_SNDBUF_SHARED(bufs);
2162 void VOsc_Ctor(VOsc *unit)
2164 SETCALC(VOsc_next_ik);
2166 float nextbufpos = ZIN0(0);
2167 unit->m_bufpos = nextbufpos;
2168 uint32 bufnum = (uint32)floor(nextbufpos);
2169 World *world = unit->mWorld;
2171 VOSC_GET_BUF_UNLOCKED
2173 int tableSize = bufs[0].samples;
2175 unit->mTableSize = tableSize;
2176 int tableSize2 = tableSize >> 1;
2177 unit->m_lomask = (tableSize2 - 1) << 3;
2178 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
2179 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2181 unit->m_phasein = ZIN0(2);
2182 unit->m_phaseoffset = (int32)(unit->m_phasein * unit->m_radtoinc);
2183 unit->m_phase = unit->m_phaseoffset;
2185 VOsc_next_ik(unit, 1);
2188 void VOsc_next_ik(VOsc *unit, int inNumSamples)
2190 float *out = ZOUT(0);
2191 float nextbufpos = ZIN0(0);
2192 float freqin = ZIN0(1);
2193 float phasein = ZIN0(2);
2195 float prevbufpos = unit->m_bufpos;
2196 float bufdiff = nextbufpos - prevbufpos;
2198 int32 phase = unit->m_phase;
2199 int32 lomask = unit->m_lomask;
2201 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2202 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
2203 unit->m_phasein = phasein;
2204 int tableSize = unit->mTableSize;
2205 float cur = prevbufpos;
2206 World *world = unit->mWorld;
2208 if (bufdiff == 0.f) {
2209 float level = cur - floor(cur);
2211 uint32 bufnum = (int)floor(cur);
2213 VOSC_GET_BUF
2215 const float *table0 = bufs[0].data;
2216 const float *table2 = bufs[1].data;
2217 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2218 ClearUnitOutputs(unit, inNumSamples);
2219 return;
2222 const float *table1 = table0 + 1;
2223 const float *table3 = table2 + 1;
2225 LOOP1(inNumSamples,
2226 float pfrac = PhaseFrac1(phase);
2227 uint32 index = ((phase >> xlobits1) & lomask);
2228 float val0 = *(float*)((char*)table0 + index);
2229 float val1 = *(float*)((char*)table1 + index);
2230 float val2 = *(float*)((char*)table2 + index);
2231 float val3 = *(float*)((char*)table3 + index);
2232 float a = val0 + val1 * pfrac;
2233 float b = val2 + val3 * pfrac;
2234 ZXP(out) = a + level * (b - a);
2235 phase += phaseinc;
2237 } else {
2238 int nsmps;
2239 int donesmps = 0;
2240 int remain = inNumSamples;
2241 while (remain) {
2242 float level = cur - (float)floor(cur);
2244 float cut;
2245 if (bufdiff > 0.) {
2246 cut = sc_min(nextbufpos, (float)floor(cur+1.f));
2247 } else {
2248 cut = sc_max(nextbufpos, ceil(cur-1.f));
2251 float sweepdiff = cut - cur;
2252 if (cut == nextbufpos) nsmps = remain;
2253 else {
2254 float sweep = (float)inNumSamples / bufdiff;
2255 nsmps = (int)floor(sweep * sweepdiff + 0.5f) - donesmps;
2256 nsmps = sc_clip(nsmps, 1, remain);
2259 float slope = sweepdiff / (float)nsmps;
2261 uint32 bufnum = (int)floor(cur);
2263 VOSC_GET_BUF
2265 const float *table0 = bufs[0].data;
2266 const float *table2 = bufs[1].data;
2267 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2268 ClearUnitOutputs(unit, inNumSamples);
2269 return;
2272 const float *table1 = table0 + 1;
2273 const float *table3 = table2 + 1;
2275 LOOP(nsmps,
2276 float pfrac = PhaseFrac1(phase);
2277 uint32 index = ((phase >> xlobits1) & lomask);
2278 float val0 = *(float*)((char*)table0 + index);
2279 float val1 = *(float*)((char*)table1 + index);
2280 float val2 = *(float*)((char*)table2 + index);
2281 float val3 = *(float*)((char*)table3 + index);
2282 float a = val0 + val1 * pfrac;
2283 float b = val2 + val3 * pfrac;
2284 ZXP(out) = a + level * (b - a);
2285 phase += phaseinc;
2286 level += slope;
2288 donesmps += nsmps;
2289 remain -= nsmps;
2290 cur = cut;
2293 unit->m_bufpos = nextbufpos;
2294 unit->m_phase = phase;
2297 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2299 void VOsc3_Ctor(VOsc3 *unit)
2301 SETCALC(VOsc3_next_ik);
2303 float nextbufpos = ZIN0(0);
2304 unit->m_bufpos = nextbufpos;
2305 uint32 bufnum = (uint32)floor(nextbufpos);
2306 World *world = unit->mWorld;
2308 VOSC_GET_BUF
2309 int tableSize = bufs[0].samples;
2311 unit->mTableSize = tableSize;
2312 int tableSize2 = tableSize >> 1;
2313 unit->m_lomask = (tableSize2 - 1) << 3;
2314 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2316 unit->m_phase1 = 0;
2317 unit->m_phase2 = 0;
2318 unit->m_phase3 = 0;
2320 VOsc3_next_ik(unit, 1);
2323 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples)
2325 float *out = ZOUT(0);
2326 float nextbufpos = ZIN0(0);
2327 float freq1in = ZIN0(1);
2328 float freq2in = ZIN0(2);
2329 float freq3in = ZIN0(3);
2331 float prevbufpos = unit->m_bufpos;
2332 float bufdiff = nextbufpos - prevbufpos;
2334 int32 phase1 = unit->m_phase1;
2335 int32 phase2 = unit->m_phase2;
2336 int32 phase3 = unit->m_phase3;
2338 int32 freq1 = (int32)(unit->m_cpstoinc * freq1in);
2339 int32 freq2 = (int32)(unit->m_cpstoinc * freq2in);
2340 int32 freq3 = (int32)(unit->m_cpstoinc * freq3in);
2342 int32 lomask = unit->m_lomask;
2343 int tableSize = unit->mTableSize;
2344 float cur = prevbufpos;
2345 World *world = unit->mWorld;
2347 if (bufdiff == 0.f) {
2348 float level = cur - floor(cur);
2350 uint32 bufnum = (int)floor(cur);
2352 VOSC_GET_BUF
2354 const float *table0 = bufs[0].data;
2355 const float *table2 = bufs[1].data;
2356 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2357 ClearUnitOutputs(unit, inNumSamples);
2358 return;
2361 const float *table1 = table0 + 1;
2362 const float *table3 = table2 + 1;
2364 LOOP1(inNumSamples,
2366 float pfrac1 = PhaseFrac1(phase1);
2367 float pfrac2 = PhaseFrac1(phase2);
2368 float pfrac3 = PhaseFrac1(phase3);
2370 int index1 = ((phase1 >> xlobits1) & lomask);
2371 int index2 = ((phase2 >> xlobits1) & lomask);
2372 int index3 = ((phase3 >> xlobits1) & lomask);
2374 phase1 += freq1;
2375 phase2 += freq2;
2376 phase3 += freq3;
2378 float val10 = *(float*)((char*)table0 + index1);
2379 float val11 = *(float*)((char*)table1 + index1);
2380 float val12 = *(float*)((char*)table2 + index1);
2381 float val13 = *(float*)((char*)table3 + index1);
2382 float a = val10 + val11 * pfrac1;
2383 float b = val12 + val13 * pfrac1;
2385 float val20 = *(float*)((char*)table0 + index2);
2386 float val21 = *(float*)((char*)table1 + index2);
2387 float val22 = *(float*)((char*)table2 + index2);
2388 float val23 = *(float*)((char*)table3 + index2);
2389 a += val20 + val21 * pfrac2;
2390 b += val22 + val23 * pfrac2;
2392 float val30 = *(float*)((char*)table0 + index3);
2393 float val31 = *(float*)((char*)table1 + index3);
2394 float val32 = *(float*)((char*)table2 + index3);
2395 float val33 = *(float*)((char*)table3 + index3);
2396 a += val30 + val31 * pfrac3;
2397 b += val32 + val33 * pfrac3;
2399 ZXP(out) = a + level * (b - a);
2401 } else {
2402 int nsmps;
2403 int donesmps = 0;
2404 int remain = inNumSamples;
2405 while (remain) {
2406 float level = cur - (float)floor(cur);
2408 float cut;
2409 if (bufdiff > 0.) {
2410 cut = sc_min(nextbufpos, (float)floor(cur+1.f));
2411 } else {
2412 cut = sc_max(nextbufpos, ceil(cur-1.f));
2415 float sweepdiff = cut - cur;
2416 if (cut == nextbufpos) nsmps = remain;
2417 else {
2418 float sweep = (float)inNumSamples / bufdiff;
2419 nsmps = (int)floor(sweep * sweepdiff + 0.5f) - donesmps;
2420 nsmps = sc_clip(nsmps, 1, remain);
2423 float slope = sweepdiff / (float)nsmps;
2425 uint32 bufnum = (int)floor(cur);
2427 VOSC_GET_BUF
2429 const float *table0 = bufs[0].data;
2430 const float *table2 = bufs[1].data;
2431 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2432 ClearUnitOutputs(unit, inNumSamples);
2433 return;
2436 const float *table1 = table0 + 1;
2437 const float *table3 = table2 + 1;
2439 LOOP(nsmps,
2441 float pfrac1 = PhaseFrac1(phase1);
2442 float pfrac2 = PhaseFrac1(phase2);
2443 float pfrac3 = PhaseFrac1(phase3);
2445 int index1 = ((phase1 >> xlobits1) & lomask);
2446 int index2 = ((phase2 >> xlobits1) & lomask);
2447 int index3 = ((phase3 >> xlobits1) & lomask);
2449 phase1 += freq1;
2450 phase2 += freq2;
2451 phase3 += freq3;
2453 float val10 = *(float*)((char*)table0 + index1);
2454 float val11 = *(float*)((char*)table1 + index1);
2455 float val12 = *(float*)((char*)table2 + index1);
2456 float val13 = *(float*)((char*)table3 + index1);
2457 float a = val10 + val11 * pfrac1;
2458 float b = val12 + val13 * pfrac1;
2460 float val20 = *(float*)((char*)table0 + index2);
2461 float val21 = *(float*)((char*)table1 + index2);
2462 float val22 = *(float*)((char*)table2 + index2);
2463 float val23 = *(float*)((char*)table3 + index2);
2464 a += val20 + val21 * pfrac2;
2465 b += val22 + val23 * pfrac2;
2467 float val30 = *(float*)((char*)table0 + index3);
2468 float val31 = *(float*)((char*)table1 + index3);
2469 float val32 = *(float*)((char*)table2 + index3);
2470 float val33 = *(float*)((char*)table3 + index3);
2471 a += val30 + val31 * pfrac3;
2472 b += val32 + val33 * pfrac3;
2474 ZXP(out) = a + level * (b - a);
2475 level += slope;
2477 donesmps += nsmps;
2478 remain -= nsmps;
2479 cur = cut;
2482 unit->m_bufpos = nextbufpos;
2483 unit->m_phase1 = phase1;
2484 unit->m_phase2 = phase2;
2485 unit->m_phase3 = phase3;
2488 //////////////////////////////////////////////////////////////////////////////////////////
2490 void Formant_Ctor(Formant *unit)
2492 SETCALC(Formant_next);
2493 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536.;
2494 unit->m_phase1 = 0;
2495 unit->m_phase2 = 0;
2496 unit->m_phase3 = 0;
2497 Formant_next(unit, 1);
2500 #define tqcyc13 0x18000000
2502 void Formant_next(Formant *unit, int inNumSamples)
2504 float *out = ZOUT(0);
2505 float freq1in = ZIN0(0);
2506 float freq2in = ZIN0(1);
2507 float freq3in = ZIN0(2);
2509 int32 phase1 = unit->m_phase1;
2510 int32 phase2 = unit->m_phase2;
2511 int32 phase3 = unit->m_phase3;
2512 float cpstoinc = unit->m_cpstoinc;
2513 int32 freq1 = (int32)(cpstoinc * freq1in);
2514 int32 freq2 = (int32)(cpstoinc * freq2in);
2515 int32 freq3 = (int32)(cpstoinc * freq3in);
2516 float* sine = ft->mSine;
2517 int32 formfreq = sc_max(freq1, freq3);
2518 LOOP1(inNumSamples,
2519 if (phase3 < onecyc13) {
2520 ZXP(out) = (*(float*)((char*)sine + (((phase3 + tqcyc13) >> xlobits) & xlomask13)) + 1.f)
2521 * *(float*)((char*)sine + ((phase2 >> xlobits) & xlomask13));
2522 phase3 += formfreq;
2523 } else {
2524 ZXP(out) = 0.f;
2526 phase1 += freq1;
2527 phase2 += freq2;
2528 if (phase1 > onecyc13) {
2529 phase1 -= onecyc13;
2530 phase2 = phase1 * freq2 / freq1;
2531 phase3 = phase1 * freq3 / freq1;
2535 unit->m_phase1 = phase1;
2536 unit->m_phase2 = phase2;
2537 unit->m_phase3 = phase3;
2540 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2542 inline float lookup13(float* table, int32 pphase)
2544 float pfrac = PhaseFrac(pphase);
2545 float* tbl = (float*)((char*)table + ((pphase >> xlobits) & xlomask13));
2546 return lininterp(pfrac, tbl[0], tbl[1]);
2549 void Blip_Ctor(Blip *unit)
2551 SETCALC(Blip_next);
2552 unit->m_freqin = ZIN0(0);
2553 unit->m_numharm = (int32)ZIN0(1);
2555 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2556 int32 N = unit->m_numharm;
2557 int32 maxN = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2558 if (N > maxN) N = maxN;
2559 if (N < 1) N = 1;
2560 unit->m_N = N;
2561 unit->m_scale = 0.5/N;
2562 unit->m_phase = 0;
2564 Blip_next(unit, 1);
2567 void Blip_next(Blip *unit, int inNumSamples)
2569 float *out = ZOUT(0);
2570 float freqin = ZIN0(0);
2571 int numharm = (int32)ZIN0(1);
2573 int32 phase = unit->m_phase;
2575 float* numtbl = ft->mSine;
2576 float* dentbl = ft->mCosecant;
2578 int32 freq, N, prevN;
2579 float scale, prevscale;
2580 bool crossfade;
2581 if (numharm != unit->m_numharm || freqin != unit->m_freqin) {
2582 N = numharm;
2583 int32 maxN = (int32)((SAMPLERATE * 0.5) / freqin);
2584 if (N > maxN) {
2585 float maxfreqin;
2586 N = maxN;
2587 maxfreqin = sc_max(unit->m_freqin, freqin);
2588 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2589 } else {
2590 if (N < 1) { N = 1; }
2591 freq = (int32)(unit->m_cpstoinc * freqin);
2593 crossfade = N != unit->m_N;
2594 prevN = unit->m_N;
2595 prevscale = unit->m_scale;
2596 unit->m_N = N;
2597 unit->m_scale = scale = 0.5/N;
2598 } else {
2599 N = unit->m_N;
2600 freq = (int32)(unit->m_cpstoinc * freqin);
2601 scale = unit->m_scale;
2602 crossfade = false;
2604 int32 N2 = 2*N+1;
2606 if (crossfade) {
2607 int32 prevN2 = 2 * prevN + 1;
2608 float xfade_slope = unit->mRate->mSlopeFactor;
2609 float xfade = 0.f;
2610 LOOP1(inNumSamples,
2611 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2612 float t0 = tbl[0];
2613 float t1 = tbl[1];
2614 if (t0 == kBadValue || t1 == kBadValue) {
2615 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2616 t0 = tbl[0];
2617 t1 = tbl[1];
2618 float pfrac = PhaseFrac(phase);
2619 float denom = t0 + (t1 - t0) * pfrac;
2620 if (std::abs(denom) < 0.0005f) {
2621 ZXP(out) = 1.f;
2622 } else {
2623 int32 rphase = phase * prevN2;
2624 pfrac = PhaseFrac(rphase);
2625 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2626 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2627 float n1 = (numer / denom - 1.f) * prevscale;
2629 rphase = phase * N2;
2630 pfrac = PhaseFrac(rphase);
2631 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2632 numer = lininterp(pfrac, tbl[0], tbl[1]);
2633 float n2 = (numer / denom - 1.f) * scale;
2635 ZXP(out) = lininterp(xfade, n1, n2);
2637 } else {
2638 float pfrac = PhaseFrac(phase);
2639 float denom = t0 + (t1 - t0) * pfrac;
2641 int32 rphase = phase * prevN2;
2642 pfrac = PhaseFrac(rphase);
2643 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2644 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2645 float n1 = (numer * denom - 1.f) * prevscale;
2647 rphase = phase * N2;
2648 pfrac = PhaseFrac(rphase);
2649 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2650 numer = lininterp(pfrac, tbl[0], tbl[1]);
2651 float n2 = (numer * denom - 1.f) * scale;
2653 ZXP(out) = lininterp(xfade, n1, n2);
2655 phase += freq;
2656 xfade += xfade_slope;
2658 } else {
2659 // hmm, if freq is above sr/4 then revert to sine table osc w/ no interpolation ?
2660 // why bother, it isn't a common choice for a fundamental.
2661 LOOP1(inNumSamples,
2662 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2663 float t0 = tbl[0];
2664 float t1 = tbl[1];
2665 if (t0 == kBadValue || t1 == kBadValue) {
2666 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2667 t0 = tbl[0];
2668 t1 = tbl[1];
2669 float pfrac = PhaseFrac(phase);
2670 float denom = t0 + (t1 - t0) * pfrac;
2671 if (std::abs(denom) < 0.0005f) {
2672 ZXP(out) = 1.f;
2673 } else {
2674 int32 rphase = phase * N2;
2675 pfrac = PhaseFrac(rphase);
2676 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2677 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2678 ZXP(out) = (numer / denom - 1.f) * scale;
2680 } else {
2681 float pfrac = PhaseFrac(phase);
2682 float denom = t0 + (t1 - t0) * pfrac;
2683 int32 rphase = phase * N2;
2684 pfrac = PhaseFrac(rphase);
2685 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2686 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2687 ZXP(out) = (numer * denom - 1.f) * scale;
2689 phase += freq;
2693 unit->m_phase = phase;
2694 unit->m_freqin = freqin;
2695 unit->m_numharm = numharm;
2699 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2702 void Saw_Ctor(Saw *unit)
2704 SETCALC(Saw_next);
2705 unit->m_freqin = ZIN0(0);
2707 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2708 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2709 unit->m_scale = 0.5/unit->m_N;
2710 unit->m_phase = 0;
2711 unit->m_y1 = -0.46f;
2713 ZOUT0(0) = 0.f;
2716 void Saw_next(Saw *unit, int inNumSamples)
2718 float *out = ZOUT(0);
2719 float freqin = ZIN0(0);
2721 int32 phase = unit->m_phase;
2722 float y1 = unit->m_y1;
2724 float* numtbl = ft->mSine;
2725 float* dentbl = ft->mCosecant;
2727 int32 freq, N, prevN;
2728 float scale, prevscale;
2729 bool crossfade;
2730 if (freqin != unit->m_freqin) {
2731 N = (int32)((SAMPLERATE * 0.5) / freqin);
2732 if (N != unit->m_N) {
2733 float maxfreqin;
2734 maxfreqin = sc_max(unit->m_freqin, freqin);
2735 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2736 crossfade = true;
2737 } else {
2738 freq = (int32)(unit->m_cpstoinc * freqin);
2739 crossfade = false;
2741 prevN = unit->m_N;
2742 prevscale = unit->m_scale;
2743 unit->m_N = N;
2744 unit->m_scale = scale = 0.5/N;
2745 } else {
2746 N = unit->m_N;
2747 freq = (int32)(unit->m_cpstoinc * freqin);
2748 scale = unit->m_scale;
2749 crossfade = false;
2751 int32 N2 = 2*N+1;
2753 if (crossfade) {
2754 int32 prevN2 = 2 * prevN + 1;
2755 float xfade_slope = unit->mRate->mSlopeFactor;
2756 float xfade = 0.f;
2757 LOOP1(inNumSamples,
2758 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2759 float t0 = tbl[0];
2760 float t1 = tbl[1];
2761 if (t0 == kBadValue || t1 == kBadValue) {
2762 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2763 t0 = tbl[0];
2764 t1 = tbl[1];
2765 float pfrac = PhaseFrac(phase);
2766 float denom = t0 + (t1 - t0) * pfrac;
2767 if (std::abs(denom) < 0.0005f) {
2768 ZXP(out) = y1 = 1.f + 0.999f * y1;
2769 } else {
2770 int32 rphase = phase * prevN2;
2771 pfrac = PhaseFrac(rphase);
2772 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2773 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2774 float n1 = (numer / denom - 1.f) * prevscale;
2776 rphase = phase * N2;
2777 pfrac = PhaseFrac(rphase);
2778 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2779 numer = lininterp(pfrac, tbl[0], tbl[1]);
2780 float n2 = (numer / denom - 1.f) * scale;
2782 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2785 } else {
2786 float pfrac = PhaseFrac(phase);
2787 float denom = t0 + (t1 - t0) * pfrac;
2789 int32 rphase = phase * prevN2;
2790 pfrac = PhaseFrac(rphase);
2791 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2792 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2793 float n1 = (numer * denom - 1.f) * prevscale;
2795 rphase = phase * N2;
2796 pfrac = PhaseFrac(rphase);
2797 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2798 numer = lininterp(pfrac, tbl[0], tbl[1]);
2799 float n2 = (numer * denom - 1.f) * scale;
2801 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2803 phase += freq;
2804 xfade += xfade_slope;
2806 } else {
2807 // hmm, if freq is above sr/4 then revert to sine table osc ?
2808 // why bother, it isn't a common choice for a fundamental.
2809 LOOP1(inNumSamples,
2810 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2811 float t0 = tbl[0];
2812 float t1 = tbl[1];
2813 if (t0 == kBadValue || t1 == kBadValue) {
2814 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2815 t0 = tbl[0];
2816 t1 = tbl[1];
2817 float pfrac = PhaseFrac(phase);
2818 float denom = t0 + (t1 - t0) * pfrac;
2819 if (std::abs(denom) < 0.0005f) {
2820 ZXP(out) = y1 = 1.f + 0.999f * y1;
2821 } else {
2822 int32 rphase = phase * N2;
2823 pfrac = PhaseFrac(rphase);
2824 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2825 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2826 ZXP(out) = y1 = (numer / denom - 1.f) * scale + 0.999f * y1;
2828 } else {
2829 float pfrac = PhaseFrac(phase);
2830 float denom = t0 + (t1 - t0) * pfrac;
2831 int32 rphase = phase * N2;
2832 pfrac = PhaseFrac(rphase);
2833 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2834 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2835 ZXP(out) = y1 = (numer * denom - 1.f) * scale + 0.999f * y1;
2837 phase += freq;
2841 unit->m_y1 = y1;
2842 unit->m_phase = phase;
2843 unit->m_freqin = freqin;
2847 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2850 void Pulse_Ctor(Pulse *unit)
2852 SETCALC(Pulse_next);
2853 unit->m_freqin = ZIN0(0);
2855 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2856 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2857 unit->m_scale = 0.5/unit->m_N;
2858 unit->m_phase = 0;
2859 unit->m_phaseoff = 0;
2860 unit->m_y1 = 0.f;
2861 ZOUT0(0) = 0.f;
2864 void Pulse_next(Pulse *unit, int inNumSamples)
2866 float *out = ZOUT(0);
2867 float freqin = ZIN0(0);
2868 float duty = ZIN0(1);
2870 int32 phase = unit->m_phase;
2871 float y1 = unit->m_y1;
2873 float* numtbl = ft->mSine;
2874 float* dentbl = ft->mCosecant;
2876 int32 freq, N, prevN;
2877 float scale, prevscale;
2878 bool crossfade;
2880 if (freqin != unit->m_freqin) {
2881 N = (int32)((SAMPLERATE * 0.5) / freqin);
2882 if (N != unit->m_N) {
2883 float maxfreqin;
2884 maxfreqin = sc_max(unit->m_freqin, freqin);
2885 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2886 crossfade = true;
2887 } else {
2888 freq = (int32)(unit->m_cpstoinc * freqin);
2889 crossfade = false;
2891 prevN = unit->m_N;
2892 prevscale = unit->m_scale;
2893 unit->m_N = N;
2894 unit->m_scale = scale = 0.5/N;
2895 } else {
2896 N = unit->m_N;
2897 freq = (int32)(unit->m_cpstoinc * freqin);
2898 scale = unit->m_scale;
2899 crossfade = false;
2901 int32 N2 = 2*N+1;
2903 int32 phaseoff = unit->m_phaseoff;
2904 int32 next_phaseoff = (int32)(duty * (1L << 28));
2905 int32 phaseoff_slope = (int32)((next_phaseoff - phaseoff) * unit->mRate->mSlopeFactor);
2906 unit->m_phaseoff = next_phaseoff;
2907 float rscale = 1.f / scale + 1.f;
2908 float pul1, pul2;
2910 if (crossfade) {
2911 int32 prevN2 = 2 * prevN + 1;
2912 float xfade_slope = unit->mRate->mSlopeFactor;
2913 float xfade = 0.f;
2914 LOOP1(inNumSamples,
2915 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2916 float t0 = tbl[0];
2917 float t1 = tbl[1];
2918 if (t0 == kBadValue || t1 == kBadValue) {
2919 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2920 t0 = tbl[0];
2921 t1 = tbl[1];
2922 float pfrac = PhaseFrac(phase);
2923 float denom = t0 + (t1 - t0) * pfrac;
2924 if (std::abs(denom) < 0.0005f) {
2925 pul1 = 1.f;
2926 } else {
2927 int32 rphase = phase * prevN2;
2928 pfrac = PhaseFrac(rphase);
2929 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2930 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2931 float n1 = (numer / denom - 1.f) * prevscale;
2933 rphase = phase * N2;
2934 pfrac = PhaseFrac(rphase);
2935 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2936 numer = lininterp(pfrac, tbl[0], tbl[1]);
2937 float n2 = (numer / denom - 1.f) * scale;
2939 pul1 = lininterp(xfade, n1, n2);
2941 } else {
2942 float pfrac = PhaseFrac(phase);
2943 float denom = lininterp(pfrac, t0, t1);
2945 int32 rphase = phase * prevN2;
2946 pfrac = PhaseFrac(rphase);
2947 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2948 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2949 float n1 = (numer * denom - 1.f) * prevscale;
2951 rphase = phase * N2;
2952 pfrac = PhaseFrac(rphase);
2953 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2954 numer = lininterp(pfrac, tbl[0], tbl[1]);
2955 float n2 = (numer * denom - 1.f) * scale;
2957 pul1 = lininterp(xfade, n1, n2);
2960 int32 phase2 = phase + phaseoff;
2961 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
2962 t0 = tbl[0];
2963 t1 = tbl[1];
2964 if (t0 == kBadValue || t1 == kBadValue) {
2965 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
2966 t0 = tbl[0];
2967 t1 = tbl[1];
2968 float pfrac = PhaseFrac(phase2);
2969 float denom = t0 + (t1 - t0) * pfrac;
2970 if (std::abs(denom) < 0.0005f) {
2971 pul2 = 1.f;
2972 } else {
2973 int32 rphase = phase2 * prevN2;
2974 pfrac = PhaseFrac(rphase);
2975 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2976 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2977 float n1 = (numer / denom - 1.f) * prevscale;
2979 rphase = phase2 * N2;
2980 pfrac = PhaseFrac(rphase);
2981 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2982 numer = lininterp(pfrac, tbl[0], tbl[1]);
2983 float n2 = (numer / denom - 1.f) * scale;
2985 pul2 = lininterp(xfade, n1, n2);
2987 } else {
2988 float pfrac = PhaseFrac(phase2);
2989 float denom = t0 + (t1 - t0) * pfrac;
2991 int32 rphase = phase2 * prevN2;
2992 pfrac = PhaseFrac(rphase);
2993 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2994 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2995 float n1 = (numer * denom - 1.f) * prevscale;
2997 rphase = phase2 * N2;
2998 pfrac = PhaseFrac(rphase);
2999 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3000 numer = lininterp(pfrac, tbl[0], tbl[1]);
3001 float n2 = (numer * denom - 1.f) * scale;
3003 pul2 = lininterp(xfade, n1, n2);
3006 ZXP(out) = y1 = pul1 - pul2 + 0.999f * y1;
3007 phase += freq;
3008 phaseoff += phaseoff_slope;
3009 xfade += xfade_slope;
3011 } else {
3012 LOOP1(inNumSamples,
3013 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
3014 float t0 = tbl[0];
3015 float t1 = tbl[1];
3016 if (t0 == kBadValue || t1 == kBadValue) {
3017 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
3018 t0 = tbl[0];
3019 t1 = tbl[1];
3020 float pfrac = PhaseFrac(phase);
3021 float denom = t0 + (t1 - t0) * pfrac;
3022 if (std::abs(denom) < 0.0005f) {
3023 pul1 = rscale;
3024 } else {
3025 int32 rphase = phase * N2;
3026 pfrac = PhaseFrac(rphase);
3027 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3028 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3029 pul1 = numer / denom;
3031 } else {
3032 float pfrac = PhaseFrac(phase);
3033 float denom = t0 + (t1 - t0) * pfrac;
3034 int32 rphase = phase * N2;
3035 pfrac = PhaseFrac(rphase);
3036 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3037 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3038 pul1 = (numer * denom);
3041 int32 phase2 = phase + phaseoff;
3042 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
3043 t0 = tbl[0];
3044 t1 = tbl[1];
3045 if (t0 == kBadValue || t1 == kBadValue) {
3046 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
3047 t0 = tbl[0];
3048 t1 = tbl[1];
3049 float pfrac = PhaseFrac(phase2);
3050 float denom = t0 + (t1 - t0) * pfrac;
3051 if (std::abs(denom) < 0.0005f) {
3052 pul2 = rscale;
3053 } else {
3054 int32 rphase = phase2 * N2;
3055 pfrac = PhaseFrac(rphase);
3056 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3057 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3058 pul2 = numer / denom;
3060 } else {
3061 float pfrac = PhaseFrac(phase2);
3062 float denom = t0 + (t1 - t0) * pfrac;
3063 int32 rphase = phase2 * N2;
3064 pfrac = PhaseFrac(rphase);
3065 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3066 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3068 pul2 = (numer * denom);
3071 ZXP(out) = y1 = (pul1 - pul2) * scale + 0.999f * y1;
3072 phase += freq;
3073 phaseoff += phaseoff_slope;
3077 unit->m_y1 = y1;
3078 unit->m_phase = phase;
3079 unit->m_freqin = freqin;
3082 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3084 void Klang_Dtor(Klang *unit)
3086 RTFree(unit->mWorld, unit->m_coefs);
3089 static float Klang_SetCoefs(Klang *unit);
3091 void Klang_Ctor(Klang *unit)
3093 SETCALC(Klang_next);
3094 ZOUT0(0) = Klang_SetCoefs(unit);
3097 float Klang_SetCoefs(Klang *unit)
3099 unit->m_numpartials = (unit->mNumInputs - 2)/3;
3101 int numcoefs = unit->m_numpartials * 3;
3102 unit->m_coefs = (float*)RTAlloc(unit->mWorld, numcoefs * sizeof(float));
3104 float freqscale = ZIN0(0) * unit->mRate->mRadiansPerSample;
3105 float freqoffset = ZIN0(1) * unit->mRate->mRadiansPerSample;
3107 float outf = 0.;
3108 float* coefs = unit->m_coefs - 1;
3110 for (int i=0,j=2; i<unit->m_numpartials; ++i,j+=3) {
3111 float w = ZIN0(j) * freqscale + freqoffset;
3112 float level = ZIN0(j+1);
3113 float phase = ZIN0(j+2);
3115 if (phase != 0.f) {
3116 outf += *++coefs = level * sin(phase); // y1
3117 *++coefs = level * sin(phase - w); // y2
3118 } else {
3119 outf += *++coefs = 0.f; // y1
3120 *++coefs = level * -sin(w); // y2
3122 *++coefs = 2. * cos(w); // b1
3124 return outf;
3127 void Klang_next(Klang *unit, int inNumSamples)
3129 float *out0 = ZOUT(0);
3131 float* out;
3132 float y0_0, y1_0, y2_0, b1_0;
3133 float y0_1, y1_1, y2_1, b1_1;
3134 float y0_2, y1_2, y2_2, b1_2;
3135 float y0_3, y1_3, y2_3, b1_3;
3136 float outf;
3138 float* coefs = unit->m_coefs - 1;
3139 int32 numpartials = unit->m_numpartials;
3141 switch (numpartials & 3) {
3142 case 3 :
3143 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3144 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3145 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3147 out = out0;
3148 LOOP(unit->mRate->mFilterLoops,
3149 outf = y0_0 = b1_0 * y1_0 - y2_0;
3150 outf += y0_1 = b1_1 * y1_1 - y2_1;
3151 outf += y0_2 = b1_2 * y1_2 - y2_2;
3152 ZXP(out) = outf;
3154 outf = y2_0 = b1_0 * y0_0 - y1_0;
3155 outf += y2_1 = b1_1 * y0_1 - y1_1;
3156 outf += y2_2 = b1_2 * y0_2 - y1_2;
3157 ZXP(out) = outf;
3159 outf = y1_0 = b1_0 * y2_0 - y0_0;
3160 outf += y1_1 = b1_1 * y2_1 - y0_1;
3161 outf += y1_2 = b1_2 * y2_2 - y0_2;
3162 ZXP(out) = outf;
3164 LOOP(unit->mRate->mFilterRemain,
3165 outf = y0_0 = b1_0 * y1_0 - y2_0;
3166 outf += y0_1 = b1_1 * y1_1 - y2_1;
3167 outf += y0_2 = b1_2 * y1_2 - y2_2;
3168 y2_0 = y1_0; y1_0 = y0_0;
3169 y2_1 = y1_1; y1_1 = y0_1;
3170 y2_2 = y1_2; y1_2 = y0_2;
3171 ZXP(out) = outf;
3173 coefs -= 9;
3174 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3175 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3176 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3177 break;
3178 case 2 :
3179 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3180 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3182 out = out0;
3183 LOOP(unit->mRate->mFilterLoops,
3184 outf = y0_0 = b1_0 * y1_0 - y2_0;
3185 outf += y0_1 = b1_1 * y1_1 - y2_1;
3186 ZXP(out) = outf;
3188 outf = y2_0 = b1_0 * y0_0 - y1_0;
3189 outf += y2_1 = b1_1 * y0_1 - y1_1;
3190 ZXP(out) = outf;
3192 outf = y1_0 = b1_0 * y2_0 - y0_0;
3193 outf += y1_1 = b1_1 * y2_1 - y0_1;
3194 ZXP(out) = outf;
3196 LOOP(unit->mRate->mFilterRemain,
3197 outf = y0_0 = b1_0 * y1_0 - y2_0;
3198 outf += y0_1 = b1_1 * y1_1 - y2_1;
3199 y2_0 = y1_0; y1_0 = y0_0;
3200 y2_1 = y1_1; y1_1 = y0_1;
3201 ZXP(out) = outf;
3204 coefs -= 6;
3205 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3206 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3207 break;
3208 case 1 :
3209 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3211 out = out0;
3212 LOOP(unit->mRate->mFilterLoops,
3213 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3215 ZXP(out) = y2_0 = b1_0 * y0_0 - y1_0;
3217 ZXP(out) = y1_0 = b1_0 * y2_0 - y0_0;
3219 LOOP(unit->mRate->mFilterRemain,
3220 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3221 y2_0 = y1_0; y1_0 = y0_0;
3224 coefs -= 3;
3225 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3226 break;
3227 case 0 :
3228 out = out0;
3229 ZClear(inNumSamples, out);
3230 break;
3233 int32 imax = numpartials >> 2;
3235 for (int i=0; i<imax; ++i) {
3236 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3237 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3238 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3239 y1_3 = *++coefs; y2_3 = *++coefs; b1_3 = *++coefs;
3241 out = out0;
3242 LOOP(unit->mRate->mFilterLoops,
3243 outf = y0_0 = b1_0 * y1_0 - y2_0;
3244 outf += y0_1 = b1_1 * y1_1 - y2_1;
3245 outf += y0_2 = b1_2 * y1_2 - y2_2;
3246 outf += y0_3 = b1_3 * y1_3 - y2_3;
3247 ZXP(out) += outf;
3249 outf = y2_0 = b1_0 * y0_0 - y1_0;
3250 outf += y2_1 = b1_1 * y0_1 - y1_1;
3251 outf += y2_2 = b1_2 * y0_2 - y1_2;
3252 outf += y2_3 = b1_3 * y0_3 - y1_3;
3253 ZXP(out) += outf;
3255 outf = y1_0 = b1_0 * y2_0 - y0_0;
3256 outf += y1_1 = b1_1 * y2_1 - y0_1;
3257 outf += y1_2 = b1_2 * y2_2 - y0_2;
3258 outf += y1_3 = b1_3 * y2_3 - y0_3;
3259 ZXP(out) += outf;
3261 LOOP(unit->mRate->mFilterRemain,
3262 outf = y0_0 = b1_0 * y1_0 - y2_0;
3263 outf += y0_1 = b1_1 * y1_1 - y2_1;
3264 outf += y0_2 = b1_2 * y1_2 - y2_2;
3265 outf += y0_3 = b1_3 * y1_3 - y2_3;
3266 y2_0 = y1_0; y1_0 = y0_0;
3267 y2_1 = y1_1; y1_1 = y0_1;
3268 y2_2 = y1_2; y1_2 = y0_2;
3269 y2_3 = y1_3; y1_3 = y0_3;
3270 ZXP(out) += outf;
3272 coefs -= 12;
3273 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3274 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3275 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3276 *++coefs = y1_3; *++coefs = y2_3; ++coefs;
3280 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3284 void Klank_Dtor(Klank *unit)
3286 RTFree(unit->mWorld, unit->m_coefs);
3289 static void Klank_SetCoefs(Klank *unit);
3291 void Klank_Ctor(Klank *unit)
3293 SETCALC(Klank_next);
3294 unit->m_x1 = unit->m_x2 = 0.f;
3295 Klank_SetCoefs(unit);
3296 ZOUT0(0) = 0.f;
3299 void Klank_SetCoefs(Klank *unit)
3301 int numpartials = (unit->mNumInputs - 4) / 3;
3302 unit->m_numpartials = numpartials;
3304 int numcoefs = ((unit->m_numpartials + 3) & ~3) * 5;
3305 unit->m_coefs = (float*)RTAlloc(unit->mWorld, (numcoefs + unit->mWorld->mBufLength) * sizeof(float));
3306 unit->m_buf = unit->m_coefs + numcoefs;
3308 float freqscale = ZIN0(1) * unit->mRate->mRadiansPerSample;
3309 float freqoffset = ZIN0(2) * unit->mRate->mRadiansPerSample;
3310 float decayscale = ZIN0(3);
3312 float* coefs = unit->m_coefs;
3314 float sampleRate = SAMPLERATE;
3316 for (int i=0,j=4; i<numpartials; ++i,j+=3) {
3317 float w = ZIN0(j) * freqscale + freqoffset;
3318 float level = ZIN0(j+1);
3319 float time = ZIN0(j+2) * decayscale;
3321 float R = time == 0.f ? 0.f : exp(log001/(time * sampleRate));
3322 float twoR = 2.f * R;
3323 float R2 = R * R;
3324 float cost = (twoR * cos(w)) / (1.f + R2);
3326 int k = 20 * (i>>2) + (i & 3);
3327 coefs[k+0] = 0.f; // y1
3328 coefs[k+4] = 0.f; // y2
3329 coefs[k+8] = twoR * cost; // b1
3330 coefs[k+12] = -R2; // b2
3331 coefs[k+16] = level * 0.25; // a0
3332 //Print("coefs %d %g %g %g\n", i, twoR * cost, -R2, ampf * 0.25);
3337 void Klank_next(Klank *unit, int inNumSamples)
3339 float *out0 = ZOUT(0);
3340 float *in0 = ZIN(0);
3342 float *in, *out;
3343 float inf;
3344 float y0_0, y1_0, y2_0, a0_0, b1_0, b2_0;
3345 float y0_1, y1_1, y2_1, a0_1, b1_1, b2_1;
3346 float y0_2, y1_2, y2_2, a0_2, b1_2, b2_2;
3347 float y0_3, y1_3, y2_3, a0_3, b1_3, b2_3;
3349 int32 numpartials = unit->m_numpartials;
3350 int32 imax = numpartials >> 2;
3352 float* coefs = unit->m_coefs + imax * 20;
3354 switch (numpartials & 3) {
3355 case 3 :
3356 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3357 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3358 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3360 in = in0;
3361 out = unit->m_buf - 1;
3362 LooP(unit->mRate->mFilterLoops) {
3363 inf = *++in;
3364 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3365 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3366 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3367 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3369 inf = *++in;
3370 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3371 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3372 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3373 *++out = a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2;
3375 inf = *++in;
3376 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3377 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3378 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3379 *++out = a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2;
3381 LooP(unit->mRate->mFilterRemain) {
3382 inf = *++in;
3383 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3384 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3385 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3386 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3387 y2_0 = y1_0; y1_0 = y0_0;
3388 y2_1 = y1_1; y1_1 = y0_1;
3389 y2_2 = y1_2; y1_2 = y0_2;
3391 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3392 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3393 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3394 break;
3395 case 2 :
3396 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3397 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3399 in = in0;
3400 out = unit->m_buf - 1;
3401 LooP(unit->mRate->mFilterLoops) {
3402 inf = *++in;
3403 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3404 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3405 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3407 inf = *++in;
3408 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3409 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3410 *++out = a0_0 * y2_0 + a0_1 * y2_1;
3412 inf = *++in;
3413 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3414 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3415 *++out = a0_0 * y1_0 + a0_1 * y1_1;
3417 LooP(unit->mRate->mFilterRemain) {
3418 inf = *++in;
3419 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3420 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3421 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3422 y2_0 = y1_0; y1_0 = y0_0;
3423 y2_1 = y1_1; y1_1 = y0_1;
3425 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3426 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3427 break;
3428 case 1 :
3429 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3431 //Print("rcoefs %g %g %g %g %g\n", y1_0, y2_0, b1_0, b2_0, a0_0);
3432 in = in0;
3433 out = unit->m_buf - 1;
3434 LooP(unit->mRate->mFilterLoops) {
3435 inf = *++in;
3436 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3437 *++out = a0_0 * y0_0;
3439 inf = *++in;
3440 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3441 *++out = a0_0 * y2_0;
3443 inf = *++in;
3444 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3445 *++out = a0_0 * y1_0;
3446 //Print("out %g %g %g\n", y0_0, y2_0, y1_0);
3448 LooP(unit->mRate->mFilterRemain) {
3449 inf = *++in;
3450 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3451 *++out = a0_0 * y0_0;
3452 y2_0 = y1_0; y1_0 = y0_0;
3453 //Print("out %g\n", y0_0);
3456 coefs[0] = y1_0; coefs[4] = y2_0;
3458 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3459 break;
3460 case 0 :
3461 out = unit->m_buf - 1;
3462 LooP(inNumSamples) { *++out = 0.f; }
3463 break;
3466 coefs = unit->m_coefs;
3468 for (int i=0; i<imax; ++i) {
3469 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3470 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3471 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3472 y1_3 = coefs[3]; y2_3 = coefs[7]; b1_3 = coefs[11]; b2_3 = coefs[15]; a0_3 = coefs[19];
3474 in = in0;
3475 out = unit->m_buf - 1;
3476 LooP(unit->mRate->mFilterLoops) {
3477 inf = *++in;
3478 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3479 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3480 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3481 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3482 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3484 inf = *++in;
3485 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3486 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3487 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3488 y2_3 = inf + b1_3 * y0_3 + b2_3 * y1_3;
3489 *++out += a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2 + a0_3 * y2_3;
3491 inf = *++in;
3492 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3493 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3494 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3495 y1_3 = inf + b1_3 * y2_3 + b2_3 * y0_3;
3496 *++out += a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2 + a0_3 * y1_3;
3498 LooP(unit->mRate->mFilterRemain) {
3499 inf = *++in;
3500 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3501 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3502 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3503 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3504 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3505 y2_0 = y1_0; y1_0 = y0_0;
3506 y2_1 = y1_1; y1_1 = y0_1;
3507 y2_2 = y1_2; y1_2 = y0_2;
3508 y2_3 = y1_3; y1_3 = y0_3;
3510 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3511 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3512 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3513 coefs[3] = zapgremlins(y1_3); coefs[7] = zapgremlins(y2_3);
3514 coefs += 20;
3517 float x0;
3518 float x1 = unit->m_x1;
3519 float x2 = unit->m_x2;
3521 in = unit->m_buf - 1;
3522 out = out0;
3523 LooP(unit->mRate->mFilterLoops) {
3524 x0 = *++in;
3525 *++out = x0 - x2;
3526 x2 = *++in;
3527 *++out = x2 - x1;
3528 x1 = *++in;
3529 *++out = x1 - x0;
3531 LooP(unit->mRate->mFilterRemain) {
3532 x0 = *++in;
3533 *++out = x0 - x2;
3534 x2 = x1;
3535 x1 = x0;
3538 unit->m_x1 = x1;
3539 unit->m_x2 = x2;
3542 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3544 static void normalize_samples(int size, float* data, float peak)
3546 float maxamp = 0.f;
3547 for (int i=0; i<size; ++i) {
3548 float absamp = std::abs(data[i]);
3549 if (absamp > maxamp) maxamp = absamp;
3551 if (maxamp != 0.f && maxamp != peak) {
3552 float ampfac = peak / maxamp;
3553 for (int i=0; i<size; ++i) {
3554 data[i] *= ampfac;
3559 static void normalize_wsamples(int size, float* data, float peak)
3561 float maxamp = 0.f;
3562 for (int i=0; i<size; i+=2) {
3563 float absamp = std::abs(data[i] + data[i+1]);
3564 if (absamp > maxamp) maxamp = absamp;
3566 if (maxamp != 0.f && maxamp != peak) {
3567 float ampfac = peak / maxamp;
3568 for (int i=0; i<size; ++i) {
3569 data[i] *= ampfac;
3574 static void add_partial(int size, float *data, double partial, double amp, double phase)
3576 if (amp == 0.0) return;
3577 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size;
3578 for (int i=0; i<size; ++i) {
3579 data[i] += amp * sin(phase);
3580 phase += w;
3584 static void add_wpartial(int size, float *data, double partial, double amp, double phase)
3586 if (amp == 0.0) return;
3587 int size2 = size >> 1;
3588 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size2;
3589 double cur = amp * sin(phase);
3590 phase += w;
3591 for (int i=0; i<size; i+=2) {
3592 double next = amp * sin(phase);
3593 data[i] += 2 * cur - next;
3594 data[i+1] += next - cur;
3595 cur = next;
3596 phase += w;
3600 static void add_chebyshev(int size, float *data, double partial, double amp)
3602 if (amp == 0.0) return;
3603 double w = 2.0 / (double)size;
3604 double phase = -1.0;
3605 double offset = -amp * cos (partial * pi2);
3606 for (int i=0; i<size; ++i) {
3607 data[i] += amp * cos (partial * acos (phase)) - offset;
3608 phase += w;
3612 static void add_wchebyshev(int size, float *data, double partial, double amp)
3614 if (amp == 0.0) return;
3615 int size2 = size >> 1;
3616 double w = 2.0 / (double)size2;
3617 double phase = -1.0;
3618 double offset = -amp * cos (partial * pi2);
3619 double cur = amp * cos (partial * acos (phase))-offset;
3620 phase += w;
3621 for (int i=0; i<size; i+=2) {
3622 double next = amp * cos (partial * acos (phase)) - offset;
3623 data[i] += 2 * cur - next;
3624 data[i+1] += next - cur;
3625 cur = next;
3626 phase += w;
3630 static void cantorFill(int size, float *data) // long offset, double amp)
3632 // if (amp == 0.0) return;
3633 for (int i=0; i<(size); ++i) {
3634 int j = i;
3635 float flag = 1.f;
3636 while ((j > 0) && (flag == 1.f) ) {
3637 if (j % 3 == 1) { flag = 0.f; }
3638 j = j / 3;
3640 if(flag) { data[i] += 1.f; }
3645 enum {
3646 flag_Normalize = 1,
3647 flag_Wavetable = 2,
3648 flag_Clear = 4
3651 void ChebyFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3653 if (buf->channels != 1) return;
3655 int flags = msg->geti();
3657 int size = buf->samples;
3658 int byteSize = size * sizeof(float);
3659 float *data = (float*)malloc(byteSize);
3661 if (flags & flag_Clear) Fill(size, data, 0.);
3662 else memcpy(data, buf->data, byteSize);
3664 for (int partial=1; msg->remain(); partial++) {
3665 double amp = msg->getf();
3666 if (flags & flag_Wavetable) add_wchebyshev(size, data, partial, amp);
3667 else add_chebyshev(size, data, partial, amp);
3670 if (flags & flag_Normalize) {
3671 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3672 else normalize_samples(size, data, 1.);
3675 memcpy(buf->data, data, byteSize);
3676 free(data);
3680 void SineFill1(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3682 if (buf->channels != 1) return;
3684 int flags = msg->geti();
3686 int size = buf->samples;
3687 int byteSize = size * sizeof(float);
3688 float *data = (float*)malloc(byteSize);
3690 if (flags & flag_Clear) Fill(size, data, 0.);
3691 else memcpy(data, buf->data, byteSize);
3693 for (int partial=1; msg->remain(); partial++) {
3694 double amp = msg->getf();
3695 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3696 else add_partial(size, data, partial, amp, 0.);
3699 if (flags & flag_Normalize) {
3700 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3701 else normalize_samples(size, data, 1.);
3704 memcpy(buf->data, data, byteSize);
3705 free(data);
3708 void SineFill2(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3710 if (buf->channels != 1) return;
3712 int flags = msg->geti();
3714 int size = buf->samples;
3715 int byteSize = size * sizeof(float);
3716 float *data = (float*)malloc(byteSize);
3718 if (flags & flag_Clear) Fill(size, data, 0.);
3719 else memcpy(data, buf->data, byteSize);
3721 while (msg->remain()) {
3722 double partial = msg->getf();
3723 double amp = msg->getf();
3724 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3725 else add_partial(size, data, partial, amp, 0.);
3727 if (flags & flag_Normalize) {
3728 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3729 else normalize_samples(size, data, 1.);
3732 memcpy(buf->data, data, byteSize);
3733 free(data);
3736 void SineFill3(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3738 if (buf->channels != 1) return;
3740 int flags = msg->geti();
3742 int size = buf->samples;
3743 int byteSize = size * sizeof(float);
3744 float *data = (float*)malloc(byteSize);
3746 if (flags & flag_Clear) Fill(size, data, 0.);
3747 else memcpy(data, buf->data, byteSize);
3749 while (msg->remain()) {
3750 double partial = msg->getf();
3751 double amp = msg->getf();
3752 double phase = msg->getf();
3753 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, phase);
3754 else add_partial(size, data, partial, amp, phase);
3756 if (flags & flag_Normalize) {
3757 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3758 else normalize_samples(size, data, 1.);
3761 memcpy(buf->data, data, byteSize);
3762 free(data);
3765 void NormalizeBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3767 float newmax;
3768 if(msg->remain() != 0){
3769 newmax = msg->getf();
3770 }else{
3771 newmax = 1.f;
3773 float *data = buf->data;
3774 int size = buf->samples;
3775 normalize_samples(size, data, newmax);
3778 void NormalizeWaveBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3780 float newmax;
3781 if(msg->remain() != 0){
3782 newmax = msg->getf();
3783 }else{
3784 newmax = 1.f;
3786 float *data = buf->data;
3787 int size = buf->samples;
3788 normalize_wsamples(size, data, newmax);
3791 void CopyBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3793 int frames1 = buf->frames;
3794 int channels1 = buf->channels;
3796 int toPos = msg->geti();
3797 uint32 bufnum2 = msg->geti();
3798 int fromPos = msg->geti();
3799 int length = msg->geti();
3801 if (bufnum2 >= world->mNumSndBufs) bufnum2 = 0;
3802 SndBuf* buf2 = world->mSndBufs + bufnum2;
3803 int frames2 = buf2->frames;
3804 int channels2 = buf2->channels;
3806 if (channels1 != channels2) return;
3808 fromPos = sc_clip(fromPos, 0, frames2-1);
3809 toPos = sc_clip(toPos, 0, frames1-1);
3811 int maxLength = sc_min(frames2 - fromPos, frames1 - toPos);
3812 if (length < 0) {
3813 length = maxLength;
3814 } else {
3815 length = sc_min(length, maxLength);
3818 if (length <= 0) return;
3820 int numbytes = length * sizeof(float) * channels1;
3821 float *data1 = buf->data + toPos * channels1;
3822 float *data2 = buf2->data + fromPos * channels2;
3824 if ((((char*)data1 + numbytes) > (char*)data2) || (((char*)data2 + numbytes) > (char*)data1)) {
3825 memmove(data1, data2, numbytes);
3826 } else {
3827 memcpy(data1, data2, numbytes);
3831 void CantorFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3833 float *data = buf->data;
3834 int size = buf->samples;
3835 // double offset = msg->getf();
3836 // double amp = msg->getf();
3837 // long offs = (long) offset;
3838 cantorFill(size, data);
3843 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3845 PluginLoad(Osc)
3847 ft = inTable;
3849 DefineSimpleUnit(DegreeToKey);
3850 DefineSimpleUnit(Select);
3851 DefineSimpleUnit(TWindex);
3852 DefineSimpleUnit(Index);
3853 DefineSimpleUnit(IndexL);
3854 DefineSimpleUnit(FoldIndex);
3855 DefineSimpleUnit(WrapIndex);
3856 DefineSimpleUnit(IndexInBetween);
3857 DefineSimpleUnit(DetectIndex);
3858 DefineSimpleUnit(Shaper);
3859 DefineSimpleUnit(SigOsc);
3860 DefineSimpleUnit(FSinOsc);
3861 DefineSimpleUnit(SinOsc);
3862 DefineSimpleUnit(SinOscFB);
3863 DefineSimpleUnit(VOsc);
3864 DefineSimpleUnit(VOsc3);
3865 DefineSimpleUnit(Osc);
3866 DefineSimpleUnit(OscN);
3867 DefineSimpleUnit(COsc);
3868 DefineSimpleUnit(Formant);
3869 DefineSimpleUnit(Blip);
3870 DefineSimpleUnit(Saw);
3871 DefineSimpleUnit(Pulse);
3872 DefineDtorUnit(Klang);
3873 DefineDtorUnit(Klank);
3875 DefineBufGen("cheby", ChebyFill);
3876 DefineBufGen("sine1", SineFill1);
3877 DefineBufGen("sine2", SineFill2);
3878 DefineBufGen("sine3", SineFill3);
3879 DefineBufGen("normalize", NormalizeBuf);
3880 DefineBufGen("wnormalize", NormalizeWaveBuf);
3881 DefineBufGen("copy", CopyBuf);
3882 DefineBufGen("cantorFill", CantorFill);
3885 //////////////////////////////////////////////////////////////////////////////////////////////////