SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / server / plugins / OscUGens.cpp
blobbc533ba45deedd8894b486cbec479bf95ec80adc
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);
284 void FSinOsc_next_i(FSinOsc *unit, int inNumSamples);
286 void PSinGrain_Ctor(PSinGrain *unit);
287 void PSinGrain_next(PSinGrain *unit, int inNumSamples);
289 void SinOsc_Ctor(SinOsc *unit);
290 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples);
291 void SinOsc_next_ika(SinOsc *unit, int inNumSamples);
292 void SinOsc_next_iak(SinOsc *unit, int inNumSamples);
293 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples);
295 void Osc_Ctor(Osc *unit);
296 void Osc_next_ikk(Osc *unit, int inNumSamples);
297 void Osc_next_ika(Osc *unit, int inNumSamples);
298 void Osc_next_iak(Osc *unit, int inNumSamples);
299 void Osc_next_iaa(Osc *unit, int inNumSamples);
301 void OscN_Ctor(OscN *unit);
302 void OscN_next_nkk(OscN *unit, int inNumSamples);
303 void OscN_next_nka(OscN *unit, int inNumSamples);
304 void OscN_next_nak(OscN *unit, int inNumSamples);
305 void OscN_next_naa(OscN *unit, int inNumSamples);
307 void COsc_Ctor(COsc *unit);
308 void COsc_next(COsc *unit, int inNumSamples);
310 void VOsc_Ctor(VOsc *unit);
311 void VOsc_next_ik(VOsc *unit, int inNumSamples);
313 void VOsc3_Ctor(VOsc3 *unit);
314 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples);
316 void Formant_Ctor(Formant *unit);
317 void Formant_next(Formant *unit, int inNumSamples);
319 void Blip_Ctor(Blip *unit);
320 void Blip_next(Blip *unit, int inNumSamples);
322 void Saw_Ctor(Saw *unit);
323 void Saw_next(Saw *unit, int inNumSamples);
325 void Pulse_Ctor(Pulse *unit);
326 void Pulse_next(Pulse *unit, int inNumSamples);
328 void Klang_Dtor(Klang *unit);
329 void Klang_Ctor(Klang *unit);
330 void Klang_next(Klang *unit, int inNumSamples);
332 void Klank_Dtor(Klank *unit);
333 void Klank_Ctor(Klank *unit);
334 void Klank_next(Klank *unit, int inNumSamples);
338 //////////////////////////////////////////////////////////////////////////////////////////////////
340 #define GET_TABLE \
341 float fbufnum = ZIN0(0); \
342 if (fbufnum != unit->m_fbufnum) { \
343 uint32 bufnum = (uint32)fbufnum; \
344 World *world = unit->mWorld; \
345 if (bufnum >= world->mNumSndBufs) { \
346 int localBufNum = bufnum - world->mNumSndBufs; \
347 Graph *parent = unit->mParent; \
348 if(localBufNum <= parent->localBufNum) { \
349 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
350 } else { \
351 bufnum = 0; \
352 unit->m_buf = world->mSndBufs + bufnum; \
354 } else { \
355 unit->m_buf = world->mSndBufs + bufnum; \
357 unit->m_fbufnum = fbufnum; \
359 const SndBuf *buf = unit->m_buf; \
360 if(!buf) { \
361 ClearUnitOutputs(unit, inNumSamples); \
362 return; \
364 LOCK_SNDBUF_SHARED(buf); \
365 const float *bufData __attribute__((__unused__)) = buf->data; \
366 if (!bufData) { \
367 ClearUnitOutputs(unit, inNumSamples); \
368 return; \
370 int tableSize = buf->samples;
373 ////////////////////////////////////////////////////////////////////////////////////////////////////////
376 void TableLookup_SetTable(TableLookup* unit, int32 inSize, float* inTable)
378 unit->mTable0 = inTable;
379 unit->mTable1 = inTable + 1;
380 unit->mTableSize = inSize;
381 unit->mMaxIndex = unit->mTableSize - 1;
382 unit->mFMaxIndex = unit->mMaxIndex;
383 unit->m_radtoinc = unit->mTableSize * (rtwopi * 65536.);
384 unit->m_cpstoinc = unit->mTableSize * SAMPLEDUR * 65536.;
385 //Print("TableLookup_SetTable unit->m_radtoinc %g %d %g\n", m_radtoinc, unit->mTableSize, rtwopi);
389 ////////////////////////////////////////////////////////////////////////////////////////////////////////
391 void DegreeToKey_Ctor(DegreeToKey *unit)
393 unit->m_fbufnum = -1e9f;
394 if (BUFLENGTH == 1) {
395 SETCALC(DegreeToKey_next_1);
396 } else if (INRATE(0) == calc_FullRate) {
397 SETCALC(DegreeToKey_next_a);
398 } else {
399 SETCALC(DegreeToKey_next_k);
401 unit->mOctave = (int32)ZIN0(2);
402 unit->mPrevIndex = std::numeric_limits<int32>::max();
403 unit->mPrevKey = 0.;
404 DegreeToKey_next_1(unit, 1);
407 void DegreeToKey_next_1(DegreeToKey *unit, int inNumSamples)
409 // get table
410 GET_TABLE
411 const float *table = bufData;
412 int32 maxindex = tableSize - 1;
414 int32 key, oct;
416 int32 octave = unit->mOctave;
417 float val;
419 int32 index = (int32)floor(ZIN0(1));
420 if (index == unit->mPrevIndex) {
421 val = unit->mPrevKey;
422 } else if (index < 0) {
423 unit->mPrevIndex = index;
424 key = tableSize + index % tableSize;
425 oct = (index + 1) / tableSize - 1;
426 val = unit->mPrevKey = table[key] + octave * oct;
427 } else if (index > maxindex) {
428 unit->mPrevIndex = index;
429 key = index % tableSize;
430 oct = index / tableSize;
431 val = unit->mPrevKey = table[key] + octave * oct;
432 } else {
433 unit->mPrevIndex = index;
434 val = unit->mPrevKey = table[index];
436 ZOUT0(0) = val;
439 void DegreeToKey_next_k(DegreeToKey *unit, int inNumSamples)
441 // get table
442 GET_TABLE
443 const float *table = bufData;
444 int32 maxindex = tableSize - 1;
446 float *out = ZOUT(0);
448 int32 key, oct;
449 float octave = unit->mOctave;
450 float val;
452 int32 index = (int32)floor(ZIN0(1));
453 if (index == unit->mPrevIndex) {
454 val = unit->mPrevKey;
455 } else if (index < 0) {
456 unit->mPrevIndex = index;
457 key = tableSize + index % tableSize;
458 oct = (index + 1) / tableSize - 1;
459 val = unit->mPrevKey = table[key] + octave * oct;
460 } else if (index > maxindex) {
461 unit->mPrevIndex = index;
462 key = index % tableSize;
463 oct = index / tableSize;
464 val = unit->mPrevKey = table[key] + octave * oct;
465 } else {
466 unit->mPrevIndex = index;
467 val = unit->mPrevKey = table[index];
469 LOOP1(inNumSamples,
470 ZXP(out) = val;
475 void DegreeToKey_next_a(DegreeToKey *unit, int inNumSamples)
477 // get table
478 GET_TABLE
479 const float *table = bufData;
480 int32 maxindex = tableSize - 1;
482 float *out = ZOUT(0);
483 float *in = ZIN(1);
484 int32 previndex = unit->mPrevIndex;
485 float prevkey = unit->mPrevKey;
486 int32 key, oct;
488 float octave = unit->mOctave;
490 LOOP1(inNumSamples,
491 int32 index = (int32)floor(ZXP(in));
492 if (index == previndex) {
493 ZXP(out) = prevkey;
494 } else if (index < 0) {
495 previndex = index;
496 key = tableSize + index % tableSize;
497 oct = (index + 1) / tableSize - 1;
498 ZXP(out) = prevkey = table[key] + octave * oct;
499 } else if (index > maxindex) {
500 previndex = index;
501 key = index % tableSize;
502 oct = index / tableSize;
503 ZXP(out) = prevkey = table[key] + octave * oct;
504 } else {
505 previndex = index;
506 ZXP(out) = prevkey = table[index];
509 unit->mPrevIndex = previndex;
510 unit->mPrevKey = prevkey;
513 ////////////////////////////////////////////////////////////////////////////////////
515 void Select_Ctor(Select *unit)
517 if (BUFLENGTH == 1) {
518 SETCALC(Select_next_1);
519 } else if (INRATE(0) == calc_FullRate) {
520 SETCALC(Select_next_a);
521 } else {
522 SETCALC(Select_next_k);
524 Select_next_1(unit, 1);
527 void Select_next_1(Select *unit, int inNumSamples)
529 int32 maxindex = unit->mNumInputs - 1;
530 int32 index = (int32)ZIN0(0) + 1;
531 index = sc_clip(index, 1, maxindex);
532 ZOUT0(0) = ZIN0(index);
535 void Select_next_k(Select *unit, int inNumSamples)
537 int32 maxindex = unit->mNumInputs - 1;
538 int32 index = (int32)ZIN0(0) + 1;
539 index = sc_clip(index, 1, maxindex);
541 float *out = OUT(0);
542 float *in = IN(index);
544 Copy(inNumSamples, out, in);
547 void Select_next_a(Select *unit, int inNumSamples)
549 int32 maxindex = unit->mNumInputs - 1;
551 float *out = ZOUT(0);
552 float *in0 = ZIN(0);
553 float **in = unit->mInBuf;
555 for (int i=0; i<inNumSamples; ++i) {
556 int32 index = (int32)ZXP(in0) + 1;
557 index = sc_clip(index, 1, maxindex);
558 ZXP(out) = in[index][i];
561 ////////////////////////////////////////////////////////////////////////////////////
563 void TWindex_Ctor(TWindex *unit)
565 if (INRATE(0) == calc_FullRate) {
566 SETCALC(TWindex_next_ak); // todo : ar
567 } else {
568 SETCALC(TWindex_next_k);
570 unit->m_prevIndex = 0;
571 unit->m_trig = -1.f; // make it trigger the first time
572 TWindex_next_k(unit, 1);
576 void TWindex_next_k(TWindex *unit, int inNumSamples)
579 int maxindex = unit->mNumInputs;
580 int32 index = maxindex;
581 float sum = 0.f;
582 float maxSum = 0.f;
583 float normalize = ZIN0(1); // switch normalisation on or off
584 float trig = ZIN0(0);
585 float *out = ZOUT(0);
586 if (trig > 0.f && unit->m_trig <= 0.f) {
587 if(normalize == 1) {
588 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
589 } else {
590 maxSum = 1.f;
592 RGen& rgen = *unit->mParent->mRGen;
593 float max = maxSum * rgen.frand();
595 for (int32 k=2; k<maxindex; ++k) {
596 sum += ZIN0(k);
597 if(sum >= max) {
598 index = k - 2;
599 break;
603 unit->m_prevIndex = index;
604 } else {
605 index = unit->m_prevIndex;
608 LOOP1(inNumSamples,
609 ZXP(out) = index;
611 unit->m_trig = trig;
614 void TWindex_next_ak(TWindex *unit, int inNumSamples)
616 int maxindex = unit->mNumInputs;
617 int32 index = maxindex;
619 float sum = 0.f;
620 float maxSum = 0.f;
621 float normalize = ZIN0(1);//switch normalisation on or off
622 float *trig = ZIN(0);
623 float *out = ZOUT(0);
624 float curtrig;
625 if(normalize == 1) {
626 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
627 } else
628 maxSum = 1.f;
629 RGen& rgen = *unit->mParent->mRGen;
631 LOOP1(inNumSamples,
632 curtrig = ZXP(trig);
633 if (curtrig > 0.f && unit->m_trig <= 0.f) {
634 float max = maxSum * rgen.frand();
635 for (int32 k=2; k<maxindex; ++k) {
636 sum += ZIN0(k);
637 if(sum >= max) {
638 index = k - 2;
639 break;
643 unit->m_prevIndex = index;
644 } else
645 index = unit->m_prevIndex;
647 ZXP(out) = index;
648 unit->m_trig = curtrig;
652 ////////////////////////////////////////////////////////////////////////////////////
654 void Index_Ctor(Index *unit)
656 unit->m_fbufnum = -1e9f;
657 if (BUFLENGTH == 1) {
658 SETCALC(Index_next_1);
659 } else if (INRATE(0) == calc_FullRate) {
660 SETCALC(Index_next_a);
661 } else {
662 SETCALC(Index_next_k);
664 Index_next_1(unit, 1);
667 void Index_next_1(Index *unit, int inNumSamples)
669 // get table
670 GET_TABLE
671 const float *table = bufData;
672 int32 maxindex = tableSize - 1;
674 int32 index = (int32)ZIN0(1);
675 index = sc_clip(index, 0, maxindex);
676 ZOUT0(0) = table[index];
679 void Index_next_k(Index *unit, int inNumSamples)
681 // get table
682 GET_TABLE
683 const float *table = bufData;
684 int32 maxindex = tableSize - 1;
686 float *out = ZOUT(0);
688 int32 index = (int32)ZIN0(1);
690 index = sc_clip(index, 0, maxindex);
691 float val = table[index];
692 LOOP1(inNumSamples,
693 ZXP(out) = val;
698 void Index_next_a(Index *unit, int inNumSamples)
700 // get table
701 GET_TABLE
702 const float *table = bufData;
703 int32 maxindex = tableSize - 1;
705 float *out = ZOUT(0);
706 float *in = ZIN(1);
708 LOOP1(inNumSamples,
709 int32 index = (int32)ZXP(in);
710 index = sc_clip(index, 0, maxindex);
711 ZXP(out) = table[index];
716 ////////////////////////////////////////////////////////////////////////////////////
718 void IndexL_Ctor(IndexL *unit)
720 unit->m_fbufnum = -1e9f;
721 if (BUFLENGTH == 1) {
722 SETCALC(IndexL_next_1);
723 } else if (INRATE(0) == calc_FullRate) {
724 SETCALC(IndexL_next_a);
725 } else {
726 SETCALC(IndexL_next_k);
728 IndexL_next_1(unit, 1);
731 void IndexL_next_1(IndexL *unit, int inNumSamples)
733 // get table
734 GET_TABLE
735 const float *table = bufData;
736 int32 maxindex = tableSize - 1;
738 float findex = ZIN0(1);
739 float frac = sc_frac(findex);
741 int32 index = (int32)findex;
742 index = sc_clip(index, 0, maxindex);
744 float a = table[index];
745 float b = table[sc_clip(index + 1, 0, maxindex)];
746 ZOUT0(0) = lininterp(frac, a, b);
749 void IndexL_next_k(IndexL *unit, int inNumSamples)
751 // get table
752 GET_TABLE
753 const float *table = bufData;
754 int32 maxindex = tableSize - 1;
756 float *out = ZOUT(0);
758 float findex = ZIN0(1);
759 float frac = sc_frac(findex);
761 int32 index = (int32)findex;
762 index = sc_clip(index, 0, maxindex);
765 float a = table[index];
766 float b = table[sc_clip(index + 1, 0, maxindex)];
767 float val = lininterp(frac, a, b);
769 LOOP1(inNumSamples,
770 ZXP(out) = val;
775 void IndexL_next_a(IndexL *unit, int inNumSamples)
777 // get table
778 GET_TABLE
779 const float *table = bufData;
780 int32 maxindex = tableSize - 1;
782 float *out = ZOUT(0);
783 float *in = ZIN(1);
785 LOOP1(inNumSamples,
786 float findex = ZXP(in);
787 float frac = sc_frac(findex);
788 int32 i1 = sc_clip((int32)findex, 0, maxindex);
789 int32 i2 = sc_clip(i1 + 1, 0, maxindex);
790 float a = table[i1];
791 float b = table[i2];
792 ZXP(out) = lininterp(frac, a, b);
798 ////////////////////////////////////////////////////////////////////////////////////
801 void FoldIndex_Ctor(FoldIndex *unit)
803 unit->m_fbufnum = -1e9f;
804 if (BUFLENGTH == 1) {
805 SETCALC(FoldIndex_next_1);
806 } else if (INRATE(0) == calc_FullRate) {
807 SETCALC(FoldIndex_next_a);
808 } else {
809 SETCALC(FoldIndex_next_k);
811 FoldIndex_next_1(unit, 1);
814 void FoldIndex_next_1(FoldIndex *unit, int inNumSamples)
816 // get table
817 GET_TABLE
818 const float *table = bufData;
819 int32 maxindex = tableSize - 1;
821 int32 index = (int32)ZIN0(1);
822 index = sc_fold(index, 0, maxindex);
823 ZOUT0(0) = table[index];
826 void FoldIndex_next_k(FoldIndex *unit, int inNumSamples)
828 // get table
829 GET_TABLE
830 const float *table = bufData;
831 int32 maxindex = tableSize - 1;
833 int32 index = (int32)ZIN0(1);
834 float *out = ZOUT(0);
836 index = sc_fold(index, 0, maxindex);
837 float val = table[index];
838 LOOP1(inNumSamples,
839 ZXP(out) = val;
844 void FoldIndex_next_a(FoldIndex *unit, int inNumSamples)
846 // get table
847 GET_TABLE
848 const float *table = bufData;
849 int32 maxindex = tableSize - 1;
851 float *out = ZOUT(0);
852 float *in = ZIN(1);
854 LOOP1(inNumSamples,
855 int32 index = (int32)ZXP(in);
856 index = sc_fold(index, 0, maxindex);
857 ZXP(out) = table[index];
862 ////////////////////////////////////////////////////////////////////////////////////
864 void WrapIndex_Ctor(WrapIndex *unit)
866 unit->m_fbufnum = -1e9f;
867 if (BUFLENGTH == 1) {
868 SETCALC(WrapIndex_next_1);
869 } else if (INRATE(0) == calc_FullRate) {
870 SETCALC(WrapIndex_next_a);
871 } else {
872 SETCALC(WrapIndex_next_k);
874 WrapIndex_next_1(unit, 1);
877 void WrapIndex_next_1(WrapIndex *unit, int inNumSamples)
879 // get table
880 GET_TABLE
881 const float *table = bufData;
882 int32 maxindex = tableSize - 1;
884 int32 index = (int32)floor(ZIN0(1));
885 index = sc_wrap(index, 0, maxindex);
886 ZOUT0(0) = table[index];
889 void WrapIndex_next_k(WrapIndex *unit, int inNumSamples)
891 // get table
892 GET_TABLE
893 const float *table = bufData;
894 int32 maxindex = tableSize - 1;
896 float *out = ZOUT(0);
898 int32 index = (int32)ZIN0(1);
899 index = sc_wrap(index, 0, maxindex);
900 float val = table[index];
901 LOOP1(inNumSamples,
902 ZXP(out) = val;
907 void WrapIndex_next_a(WrapIndex *unit, int inNumSamples)
909 // get table
910 GET_TABLE
911 const float *table = bufData;
912 int32 maxindex = tableSize - 1;
914 float *out = ZOUT(0);
915 float *in = ZIN(1);
917 LOOP1(inNumSamples,
918 int32 index = (int32)ZXP(in);
919 index = sc_wrap(index, 0, maxindex);
920 ZXP(out) = table[index];
924 ////////////////////////////////////////////////////////////////////////////////////
926 static float IndexInBetween_FindIndex(const float* table, float in, int32 maxindex)
928 for(int32 i = 0; i <= maxindex; i++) {
929 if(table[i] > in) {
930 if(i == 0) {
931 return 0.f;
932 } else {
933 return ((in - table[i - 1]) / (table[i] - table[i - 1]) + i - 1);
937 return (float)maxindex;
940 void IndexInBetween_Ctor(IndexInBetween *unit)
942 unit->m_fbufnum = -1e9f;
943 if (BUFLENGTH == 1) {
944 SETCALC(IndexInBetween_next_1);
945 } else if (INRATE(0) == calc_FullRate) {
946 SETCALC(IndexInBetween_next_a);
947 } else {
948 SETCALC(IndexInBetween_next_k);
950 IndexInBetween_next_1(unit, 1);
953 void IndexInBetween_next_1(IndexInBetween *unit, int inNumSamples)
955 // get table
956 GET_TABLE
957 const float *table = bufData;
958 int32 maxindex = tableSize - 1;
960 float in = ZIN0(1);
961 ZOUT0(0) = IndexInBetween_FindIndex(table, in, maxindex);
964 void IndexInBetween_next_k(IndexInBetween *unit, int inNumSamples)
966 // get table
967 GET_TABLE
968 const float *table = bufData;
969 int32 maxindex = tableSize - 1;
971 float *out = ZOUT(0);
972 float in = ZIN0(1);
974 float val = IndexInBetween_FindIndex(table, in, maxindex);
975 LOOP1(inNumSamples,
976 ZXP(out) = val;
981 void IndexInBetween_next_a(IndexInBetween *unit, int inNumSamples)
983 // get table
984 GET_TABLE
985 const float *table = bufData;
986 int32 maxindex = tableSize - 1;
988 float *out = ZOUT(0);
989 float *in = ZIN(1);
991 LOOP1(inNumSamples,
992 ZXP(out) = IndexInBetween_FindIndex(table, ZXP(in), maxindex);
997 ////////////////////////////////////////////////////////////////////////////////////
999 static int32 DetectIndex_FindIndex(const float* table, float in, int32 maxindex)
1001 int32 index;
1002 for(index = 0; index <= maxindex; index+=1) {
1003 if(table[index] == in) {
1004 return index;
1007 return -1;
1010 void DetectIndex_Ctor(DetectIndex *unit)
1012 unit->m_fbufnum = -1e9f;
1013 if (BUFLENGTH == 1) {
1014 SETCALC(DetectIndex_next_1);
1015 } else if (INRATE(0) == calc_FullRate) {
1016 SETCALC(DetectIndex_next_a);
1017 } else {
1018 SETCALC(DetectIndex_next_k);
1020 unit->mPrev = -1.f;
1021 DetectIndex_next_1(unit, 1);
1024 void DetectIndex_next_1(DetectIndex *unit, int inNumSamples)
1026 // get table
1027 GET_TABLE
1028 const float *table = bufData;
1029 int32 maxindex = tableSize - 1;
1031 float in = ZIN0(1);
1032 int32 index;
1033 if(in == unit->mPrevIn) {
1034 index = (int32)unit->mPrev;
1035 } else {
1036 index = DetectIndex_FindIndex(table, in, maxindex);
1037 unit->mPrev = index;
1038 unit->mPrevIn = in;
1040 ZOUT0(0) = (float)index;
1043 void DetectIndex_next_k(DetectIndex *unit, int inNumSamples)
1045 // get table
1046 GET_TABLE
1047 const float *table = bufData;
1048 int32 maxindex = tableSize - 1;
1050 float *out = ZOUT(0);
1051 float in = ZIN0(1);
1052 int32 index;
1053 float val;
1054 if(in == unit->mPrevIn) {
1055 index = (int32)unit->mPrev;
1056 } else {
1057 index = DetectIndex_FindIndex(table, in, maxindex);
1058 unit->mPrev = index;
1059 unit->mPrevIn = in;
1061 val = (float)index;
1062 LOOP1(inNumSamples,
1063 ZXP(out) = val;
1068 void DetectIndex_next_a(DetectIndex *unit, int inNumSamples)
1070 // get table
1071 GET_TABLE
1072 const float *table = bufData;
1073 int32 maxindex = tableSize - 1;
1075 float *out = ZOUT(0);
1076 float *in = ZIN(1);
1077 float prev = unit->mPrevIn;
1078 int32 prevIndex = (int32)unit->mPrev;
1079 float inval;
1081 LOOP1(inNumSamples,
1082 inval = ZXP(in);
1083 if(inval != prev) {
1084 prevIndex = DetectIndex_FindIndex(table, inval, maxindex);
1086 prev = inval;
1087 ZXP(out) = (float)prevIndex;
1090 unit->mPrev = prevIndex;
1091 unit->mPrevIn = inval;
1095 ////////////////////////////////////////////////////////////////////////////////////
1097 void Shaper_Ctor(Shaper *unit)
1099 unit->m_fbufnum = -1e9f;
1100 if (BUFLENGTH == 1) {
1101 SETCALC(Shaper_next_1);
1102 } else if (INRATE(1) == calc_FullRate) {
1103 SETCALC(Shaper_next_a);
1104 } else {
1105 SETCALC(Shaper_next_k);
1107 unit->mPrevIn = ZIN0(0);
1108 Shaper_next_1(unit, 1);
1111 void Shaper_next_1(Shaper *unit, int inNumSamples)
1113 // get table
1114 GET_TABLE
1115 const float *table0 = bufData;
1116 const float *table1 = table0 + 1;
1117 float fmaxindex = (float)(tableSize>>1) - 0.001;
1118 float offset = tableSize * 0.25;
1120 float val;
1122 float fin = ZIN0(1);
1123 float findex = offset + fin * offset;
1124 findex = sc_clip(findex, 0.f, fmaxindex);
1125 int32 index = (int32)findex;
1126 float pfrac = findex - (index - 1);
1127 index <<= 3;
1128 float val1 = *(float*)((char*)table0 + index);
1129 float val2 = *(float*)((char*)table1 + index);
1130 val = val1 + val2 * pfrac;
1131 ZOUT0(0) = val;
1134 void Shaper_next_k(Shaper *unit, int inNumSamples)
1136 // get table
1137 GET_TABLE
1138 const float *table0 = bufData;
1139 const float *table1 = table0 + 1;
1140 float fmaxindex = (float)(tableSize>>1) - 0.001;
1141 float offset = tableSize * 0.25;
1143 float *out = ZOUT(0);
1145 float val;
1147 float fin = ZIN0(1);
1148 float phaseinc = (fin - unit->mPrevIn) * offset;
1149 unit->mPrevIn = fin;
1151 LOOP1(inNumSamples,
1152 float findex = offset + fin * offset;
1153 findex = sc_clip(findex, 0.f, fmaxindex);
1154 int32 index = (int32)findex;
1155 float pfrac = findex - (index - 1);
1156 index <<= 3;
1157 float val1 = *(float*)((char*)table0 + index);
1158 float val2 = *(float*)((char*)table1 + index);
1159 val = val1 + val2 * pfrac;
1160 ZXP(out) = val;
1161 fin += phaseinc;
1165 void Shaper_next_a(Shaper *unit, int inNumSamples)
1167 // get table
1168 GET_TABLE
1169 const float *table0 = bufData;
1170 const float *table1 = table0 + 1;
1171 float fmaxindex = (float)(tableSize>>1) - 0.001;
1172 float offset = tableSize * 0.25;
1174 float *out = ZOUT(0);
1175 float *in = ZIN(1);
1176 float val;
1178 LOOP1(inNumSamples,
1179 float fin = ZXP(in);
1180 float findex = offset + fin * offset;
1181 findex = sc_clip(findex, 0.f, fmaxindex);
1182 int32 index = (int32)findex;
1183 float pfrac = findex - (index - 1);
1184 index <<= 3;
1185 float val1 = *(float*)((char*)table0 + index);
1186 float val2 = *(float*)((char*)table1 + index);
1187 val = val1 + val2 * pfrac;
1188 ZXP(out) = val;
1194 ////////////////////////////////////////////////////////////////////////////////////
1196 void SigOsc_Ctor(SigOsc *unit)
1198 unit->m_fbufnum = -1e9f;
1199 if (BUFLENGTH == 1) {
1200 SETCALC(SigOsc_next_1);
1201 } else if (INRATE(0) == calc_FullRate) {
1202 SETCALC(SigOsc_next_a);
1203 } else {
1204 SETCALC(SigOsc_next_k);
1206 unit->mPhase = 0.f;
1207 SigOsc_next_1(unit, 1);
1211 void SigOsc_next_1(SigOsc *unit, int inNumSamples)
1213 // get table
1214 GET_TABLE
1215 const float *table0 = bufData;
1216 const float *table1 = table0 + 1;
1217 int32 maxindex = tableSize - 1;
1218 float maxphase = (float)maxindex;
1219 if (tableSize != unit->mTableSize) {
1220 unit->mTableSize = tableSize;
1221 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1224 float phase = unit->mPhase;
1226 while (phase < 0.f) phase += maxphase;
1227 while (phase >= maxphase) phase -= maxphase;
1228 int32 iphase = (int32)phase;
1229 float pfrac = phase - iphase;
1230 float val1 = *(float*)((char*)table0 + iphase);
1231 float val2 = *(float*)((char*)table1 + iphase);
1232 float val = lininterp(pfrac, val1, val2);
1233 phase += ZIN0(1) * unit->m_cpstoinc;
1235 ZOUT0(0) = val;
1237 unit->mPhase = phase;
1240 void SigOsc_next_k(SigOsc *unit, int inNumSamples)
1242 // get table
1243 GET_TABLE
1244 const float *table0 = bufData;
1245 const float *table1 = table0 + 1;
1246 int32 maxindex = tableSize - 1;
1247 float maxphase = (float)maxindex;
1248 if (tableSize != unit->mTableSize) {
1249 unit->mTableSize = tableSize;
1250 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1253 float *out = ZOUT(0);
1255 float phase = unit->mPhase;
1257 float freq = ZIN0(1) * unit->m_cpstoinc;
1259 LOOP1(inNumSamples,
1260 while (phase < 0.f) phase += maxphase;
1261 while (phase >= maxphase) phase -= maxphase;
1262 int32 iphase = (int32)phase;
1263 float pfrac = phase - iphase;
1264 float val1 = *(float*)((char*)table0 + iphase);
1265 float val2 = *(float*)((char*)table1 + iphase);
1266 float val = lininterp(pfrac, val1, val2);
1267 phase += freq;
1269 ZXP(out) = val;
1272 unit->mPhase = phase;
1275 void SigOsc_next_a(SigOsc *unit, int inNumSamples)
1277 // get table
1278 GET_TABLE
1279 const float *table0 = bufData;
1280 const float *table1 = table0 + 1;
1281 int32 maxindex = tableSize - 1;
1282 float maxphase = (float)maxindex;
1283 if (tableSize != unit->mTableSize) {
1284 unit->mTableSize = tableSize;
1285 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1288 float *out = ZOUT(0);
1289 float *freqin = ZIN(1);
1290 float phase = unit->mPhase;
1291 float cpstoinc = unit->m_cpstoinc;
1293 LOOP1(inNumSamples,
1294 while (phase < 0.f) phase += maxphase;
1295 while (phase >= maxphase) phase -= maxphase;
1296 int32 iphase = (int32)phase;
1297 float pfrac = phase - iphase;
1298 float val1 = *(float*)((char*)table0 + iphase);
1299 float val2 = *(float*)((char*)table1 + iphase);
1300 float val = lininterp(pfrac, val1, val2);
1301 phase += ZXP(freqin) * cpstoinc;
1303 ZXP(out) = val;
1306 unit->mPhase = phase;
1310 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1312 void FSinOsc_Ctor(FSinOsc *unit)
1314 if (INRATE(0) == calc_ScalarRate)
1315 SETCALC(FSinOsc_next_i);
1316 else
1317 SETCALC(FSinOsc_next);
1318 unit->m_freq = ZIN0(0);
1319 float iphase = ZIN0(1);
1320 float w = unit->m_freq * unit->mRate->mRadiansPerSample;
1321 unit->m_b1 = 2. * cos(w);
1322 unit->m_y1 = sin(iphase);
1323 unit->m_y2 = sin(iphase - w);
1325 ZOUT0(0) = unit->m_y1;
1328 void FSinOsc_next(FSinOsc *unit, int inNumSamples)
1330 float *out = ZOUT(0);
1331 double freq = ZIN0(0);
1332 double b1;
1333 if (freq != unit->m_freq) {
1334 unit->m_freq = freq;
1335 double w = freq * unit->mRate->mRadiansPerSample;
1336 unit->m_b1 = b1 = 2.f * cos(w);
1337 } else {
1338 b1 = unit->m_b1;
1340 double y0;
1341 double y1 = unit->m_y1;
1342 double y2 = unit->m_y2;
1343 //Print("y %g %g b1 %g\n", y1, y2, b1);
1344 //Print("%d %d\n", unit->mRate->mFilterLoops, unit->mRate->mFilterRemain);
1345 LOOP(unit->mRate->mFilterLoops,
1346 ZXP(out) = y0 = b1 * y1 - y2;
1347 ZXP(out) = y2 = b1 * y0 - y1;
1348 ZXP(out) = y1 = b1 * y2 - y0;
1350 LOOP(unit->mRate->mFilterRemain,
1351 ZXP(out) = y0 = b1 * y1 - y2;
1352 y2 = y1;
1353 y1 = y0;
1355 //Print("y %g %g b1 %g\n", y1, y2, b1);
1356 unit->m_y1 = y1;
1357 unit->m_y2 = y2;
1360 void FSinOsc_next_i(FSinOsc *unit, int inNumSamples)
1362 #ifdef __GNUC__
1363 float * __restrict__ out = ZOUT(0);
1364 #else
1365 float * out = ZOUT(0);
1366 #endif
1367 double b1 = unit->m_b1;
1369 double y0;
1370 double y1 = unit->m_y1;
1371 double y2 = unit->m_y2;
1372 //Print("y %g %g b1 %g\n", y1, y2, b1);
1373 //Print("%d %d\n", unit->mRate->mFilterLoops, unit->mRate->mFilterRemain);
1374 LOOP(unit->mRate->mFilterLoops,
1375 y0 = b1 * y1 - y2;
1376 y2 = b1 * y0 - y1;
1377 y1 = b1 * y2 - y0;
1378 ZXP(out) = y0;
1379 ZXP(out) = y2;
1380 ZXP(out) = y1;
1382 LOOP(unit->mRate->mFilterRemain,
1383 ZXP(out) = y0 = b1 * y1 - y2;
1384 y2 = y1;
1385 y1 = y0;
1387 //Print("y %g %g b1 %g\n", y1, y2, b1);
1388 unit->m_y1 = y1;
1389 unit->m_y2 = y2;
1392 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1394 void PSinGrain_Ctor(PSinGrain *unit)
1396 SETCALC(PSinGrain_next);
1397 float freq = ZIN0(0);
1398 float dur = ZIN0(1);
1399 float amp = ZIN0(2);
1401 float w = freq * unit->mRate->mRadiansPerSample;
1402 float sdur = SAMPLERATE * dur;
1404 float rdur = 1.f / sdur;
1405 float rdur2 = rdur * rdur;
1407 unit->m_level = 0.f;
1408 unit->m_slope = 4.0 * (rdur - rdur2); // ampslope
1409 unit->m_curve = -8.0 * rdur2; // ampcurve
1410 unit->mCounter = (int32)(sdur + .5);
1412 /* calc feedback param and initial conditions */
1413 unit->m_b1 = 2. * cos(w);
1414 unit->m_y1 = 0.f;
1415 unit->m_y2 = -sin(w) * amp;
1416 ZOUT0(0) = 0.f;
1420 void PSinGrain_next(PSinGrain *unit, int inNumSamples)
1422 float *out = ZOUT(0);
1423 float y0;
1424 float y1 = unit->m_y1;
1425 float y2 = unit->m_y2;
1426 float b1 = unit->m_b1;
1427 float level = unit->m_level;
1428 float slope = unit->m_slope;
1429 float curve = unit->m_curve;
1430 int32 counter = unit->mCounter;
1431 int32 remain = inNumSamples;
1432 int32 nsmps;
1433 do {
1434 if (counter<=0) {
1435 nsmps = remain;
1436 remain = 0;
1437 LOOP(nsmps, ZXP(out) = 0.f;); // can't use Clear bcs might not be aligned
1438 } else {
1439 nsmps = sc_min(remain, counter);
1440 remain -= nsmps;
1441 counter -= nsmps;
1442 if (nsmps == inNumSamples) {
1443 nsmps = unit->mRate->mFilterLoops;
1444 LOOP(nsmps,
1445 y0 = b1 * y1 - y2;
1446 ZXP(out) = y0 * level;
1447 level += slope;
1448 slope += curve;
1449 y2 = b1 * y0 - y1;
1450 ZXP(out) = y2 * level;
1451 level += slope;
1452 slope += curve;
1453 y1 = b1 * y2 - y0;
1454 ZXP(out) = y1 * level;
1455 level += slope;
1456 slope += curve;
1458 nsmps = unit->mRate->mFilterRemain;
1459 LOOP(nsmps,
1460 y0 = b1 * y1 - y2;
1461 y2 = y1;
1462 y1 = y0;
1463 ZXP(out) = y0 * level;
1464 level += slope;
1465 slope += curve;
1467 } else {
1468 LOOP(nsmps,
1469 y0 = b1 * y1 - y2;
1470 y2 = y1;
1471 y1 = y0;
1472 ZXP(out) = y0 * level;
1473 level += slope;
1474 slope += curve;
1477 if (counter == 0) {
1478 NodeEnd(&unit->mParent->mNode);
1481 } while (remain>0);
1482 unit->m_level = level;
1483 unit->m_slope = slope;
1484 unit->m_y1 = y1;
1485 unit->m_y2 = y2;
1488 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1490 //////////////!!!
1492 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1494 float *out = ZOUT(0);
1495 float freqin = ZIN0(0);
1496 float phasein = ZIN0(1);
1498 float *table0 = ft->mSineWavetable;
1499 float *table1 = table0 + 1;
1501 int32 phase = unit->m_phase;
1502 int32 lomask = unit->m_lomask;
1504 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1505 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1506 unit->m_phasein = phasein;
1508 LOOP1(inNumSamples,
1509 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1510 phase += phaseinc;
1512 unit->m_phase = phase;
1515 #if __VEC__
1517 void vSinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1519 vfloat32 *vout = (vfloat32*)OUT(0);
1520 float freqin = ZIN0(0);
1521 float phasein = ZIN0(1);
1523 float *table0 = ft->mSineWavetable;
1524 float *table1 = table0 + 1;
1526 int32 phase = unit->m_phase;
1527 int32 lomask = unit->m_lomask;
1529 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1530 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1531 unit->m_phasein = phasein;
1533 vint32 vphase = vload(phase, phase+phaseinc, phase+2*phaseinc, phase+3*phaseinc);
1534 vint32 vphaseinc = vload(phaseinc << 2);
1535 vint32 v3F800000 = (vint32)vinit(0x3F800000);
1536 vint32 v007FFF80 = (vint32)vinit(0x007FFF80);
1537 vint32 vlomask = vload(lomask);
1538 vuint32 vxlobits1 = (vuint32)vinit(xlobits1);
1539 vuint32 v7 = (vuint32)vinit(7);
1541 vint32 vtable0 = vload((int32)table0); // assuming 32 bit pointers
1542 vint32 vtable1 = vload((int32)table1); // assuming 32 bit pointers
1544 int len = inNumSamples << 2;
1545 for (int i=0; i<len; i+=16) {
1547 vfloat32 vfrac = (vfloat32)(vec_or(v3F800000, vec_and(v007FFF80, vec_sl(vphase, v7))));
1548 vint32 vindex = vec_and(vec_sr(vphase, vxlobits1), vlomask);
1549 vec_union vaddr0, vaddr1;
1550 vaddr0.vi = vec_add(vindex, vtable0);
1551 vaddr1.vi = vec_add(vindex, vtable1);
1553 vec_union vval1, vval2;
1554 vval1.f[0] = *(float*)(vaddr0.i[0]);
1555 vval2.f[0] = *(float*)(vaddr1.i[0]);
1556 vval1.f[1] = *(float*)(vaddr0.i[1]);
1557 vval2.f[1] = *(float*)(vaddr1.i[1]);
1558 vval1.f[2] = *(float*)(vaddr0.i[2]);
1559 vval2.f[2] = *(float*)(vaddr1.i[2]);
1560 vval1.f[3] = *(float*)(vaddr0.i[3]);
1561 vval2.f[3] = *(float*)(vaddr1.i[3]);
1563 vec_st(vec_madd(vval2.vf, vfrac, vval1.vf), i, vout);
1564 vphase = vec_add(vphase, vphaseinc);
1566 unit->m_phase = phase + inNumSamples * phaseinc;
1570 #endif
1572 void SinOsc_next_ika(SinOsc *unit, int inNumSamples)
1574 float *out = ZOUT(0);
1575 float freqin = ZIN0(0);
1576 float *phasein = ZIN(1);
1578 float *table0 = ft->mSineWavetable;
1579 float *table1 = table0 + 1;
1581 int32 phase = unit->m_phase;
1582 int32 lomask = unit->m_lomask;
1584 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1585 float radtoinc = unit->m_radtoinc;
1586 //Print("SinOsc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1587 LOOP1(inNumSamples,
1588 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1589 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1590 phase += freq;
1592 unit->m_phase = phase;
1593 //unit->m_phasein = phasein;
1597 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples)
1599 float *out = ZOUT(0);
1600 float *freqin = ZIN(0);
1601 float *phasein = ZIN(1);
1603 float *table0 = ft->mSineWavetable;
1604 float *table1 = table0 + 1;
1606 int32 phase = unit->m_phase;
1607 int32 lomask = unit->m_lomask;
1609 float cpstoinc = unit->m_cpstoinc;
1610 float radtoinc = unit->m_radtoinc;
1611 //Print("SinOsc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1612 LOOP1(inNumSamples,
1613 float phaseIn = ZXP(phasein);
1614 float freqIn = ZXP(freqin);
1615 int32 phaseoffset = phase + (int32)(radtoinc * phaseIn);
1616 float z = lookupi1(table0, table1, phaseoffset, lomask);
1617 phase += (int32)(cpstoinc * freqIn);
1618 ZXP(out) = z;
1620 unit->m_phase = phase;
1621 //unit->m_phasein = ZX(phasein);
1625 void SinOsc_next_iak(SinOsc *unit, int inNumSamples)
1627 float *out = ZOUT(0);
1628 float *freqin = ZIN(0);
1629 float phasein = ZIN0(1);
1631 float *table0 = ft->mSineWavetable;
1632 float *table1 = table0 + 1;
1634 int32 phase = unit->m_phase;
1635 int32 lomask = unit->m_lomask;
1637 float cpstoinc = unit->m_cpstoinc;
1638 float radtoinc = unit->m_radtoinc;
1639 float phasemod = unit->m_phasein;
1640 float phaseslope = CALCSLOPE(phasein, phasemod);
1642 LOOP1(inNumSamples,
1643 int32 pphase = phase + (int32)(radtoinc * phasemod);
1644 phasemod += phaseslope;
1645 float z = lookupi1(table0, table1, pphase, lomask);
1646 phase += (int32)(cpstoinc * ZXP(freqin));
1647 ZXP(out) = z;
1649 unit->m_phase = phase;
1650 unit->m_phasein = phasein;
1653 void SinOsc_next_iai(SinOsc *unit, int inNumSamples)
1655 float *out = ZOUT(0);
1656 float *freqin = ZIN(0);
1658 float *table0 = ft->mSineWavetable;
1659 float *table1 = table0 + 1;
1661 int32 phase = unit->m_phase;
1662 int32 lomask = unit->m_lomask;
1664 float cpstoinc = unit->m_cpstoinc;
1665 float radtoinc = unit->m_radtoinc;
1666 float phasemod = unit->m_phasein;
1668 LOOP1(inNumSamples,
1669 int32 pphase = phase + (int32)(radtoinc * phasemod);
1670 float z = lookupi1(table0, table1, pphase, lomask);
1671 phase += (int32)(cpstoinc * ZXP(freqin));
1672 ZXP(out) = z;
1674 unit->m_phase = phase;
1679 void SinOsc_Ctor(SinOsc *unit)
1681 int tableSize2 = ft->mSineSize;
1682 unit->m_phasein = ZIN0(1);
1683 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1684 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1685 unit->m_lomask = (tableSize2 - 1) << 3;
1687 if (INRATE(0) == calc_FullRate) {
1688 if (INRATE(1) == calc_FullRate)
1689 SETCALC(SinOsc_next_iaa);
1690 else if (INRATE(1) == calc_BufRate)
1691 SETCALC(SinOsc_next_iak);
1692 else
1693 SETCALC(SinOsc_next_iai);
1695 unit->m_phase = 0;
1696 } else {
1697 if (INRATE(1) == calc_FullRate) {
1698 //Print("next_ika\n");
1699 SETCALC(SinOsc_next_ika);
1700 unit->m_phase = 0;
1701 } else {
1702 #if __VEC__
1703 if (USEVEC) {
1704 //Print("vSinOsc_next_ikk\n");
1705 SETCALC(vSinOsc_next_ikk);
1706 } else {
1707 //Print("SinOsc_next_ikk\n");
1708 SETCALC(SinOsc_next_ikk);
1710 #else
1711 //Print("next_ikk\n");
1712 SETCALC(SinOsc_next_ikk);
1713 #endif
1714 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1718 SinOsc_next_ikk(unit, 1);
1722 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1723 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1724 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1726 //////////////!!!
1728 void SinOscFB_next_kk(SinOscFB *unit, int inNumSamples)
1730 float *out = ZOUT(0);
1731 float freqin = ZIN0(0);
1733 float feedback = unit->m_feedback;
1734 float nextFeedback = ZIN0(1) * unit->m_radtoinc;
1736 float *table0 = ft->mSineWavetable;
1737 float *table1 = table0 + 1;
1739 int32 phase = unit->m_phase;
1740 int32 lomask = unit->m_lomask;
1741 float prevout = unit->m_prevout;
1742 float feedback_slope = CALCSLOPE(nextFeedback, feedback);
1743 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1745 LooP(inNumSamples) {
1746 prevout = lookupi1(table0, table1, phase + (int32)(feedback * prevout), lomask);
1747 ZXP(out) = prevout;
1748 phase += freq;
1749 feedback += feedback_slope;
1751 unit->m_phase = phase;
1752 unit->m_prevout = prevout;
1753 unit->m_feedback = feedback;
1756 void SinOscFB_Ctor(SinOscFB *unit)
1758 //Print("next_ik\n");
1759 SETCALC(SinOscFB_next_kk);
1761 int tableSize2 = ft->mSineSize;
1762 unit->m_lomask = (tableSize2 - 1) << 3;
1763 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1764 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1765 unit->m_prevout = 0.;
1766 unit->m_feedback = ZIN0(1) * unit->m_radtoinc;
1768 unit->m_phase = 0;
1770 SinOscFB_next_kk(unit, 1);
1774 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1775 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1777 void Osc_Ctor(Osc *unit)
1779 unit->mTableSize = -1;
1781 float fbufnum = ZIN0(0);
1782 uint32 bufnum = (uint32)fbufnum;
1783 World *world = unit->mWorld;
1785 SndBuf *buf;
1786 if (bufnum >= world->mNumSndBufs) {
1787 int localBufNum = bufnum - world->mNumSndBufs;
1788 Graph *parent = unit->mParent;
1789 if(localBufNum <= parent->localBufNum) {
1790 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1791 } else {
1792 buf = unit->m_buf = world->mSndBufs;
1794 } else {
1795 buf = unit->m_buf = world->mSndBufs + bufnum;
1798 int tableSize = buf->samples;
1799 int tableSize2 = tableSize >> 1;
1800 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1802 unit->m_phasein = ZIN0(2);
1804 if (INRATE(1) == calc_FullRate) {
1805 if (INRATE(2) == calc_FullRate) {
1806 //Print("next_iaa\n");
1807 SETCALC(Osc_next_iaa);
1808 unit->m_phase = 0;
1809 } else {
1810 //Print("next_iak\n");
1811 SETCALC(Osc_next_iak);
1812 unit->m_phase = 0;
1814 } else {
1815 if (INRATE(2) == calc_FullRate) {
1816 //Print("next_ika\n");
1817 SETCALC(Osc_next_ika);
1818 unit->m_phase = 0;
1819 } else {
1820 //Print("next_ikk\n");
1821 SETCALC(Osc_next_ikk);
1822 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1826 Osc_next_ikk(unit, 1);
1829 //////////////!!!
1831 void Osc_next_ikk(Osc *unit, int inNumSamples)
1833 // get table
1834 GET_TABLE
1835 const float *table0 = bufData;
1836 const float *table1 = table0 + 1;
1837 if (tableSize != unit->mTableSize) {
1838 unit->mTableSize = tableSize;
1839 int tableSize2 = tableSize >> 1;
1840 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1841 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1842 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1843 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1846 float *out = ZOUT(0);
1847 float freqin = ZIN0(1);
1848 float phasein = ZIN0(2);
1850 int32 phase = unit->m_phase;
1851 int32 lomask = unit->m_lomask;
1853 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1854 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1855 unit->m_phasein = phasein;
1857 LOOP1(inNumSamples,
1858 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1859 phase += phaseinc;
1861 unit->m_phase = phase;
1865 void Osc_next_ika(Osc *unit, int inNumSamples)
1867 // get table
1868 GET_TABLE
1869 const float *table0 = bufData;
1870 const float *table1 = table0 + 1;
1871 if (tableSize != unit->mTableSize) {
1872 unit->mTableSize = tableSize;
1873 int tableSize2 = tableSize >> 1;
1874 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1875 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1876 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1877 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1880 float *out = ZOUT(0);
1881 float freqin = ZIN0(1);
1882 float *phasein = ZIN(2);
1884 int32 phase = unit->m_phase;
1885 int32 lomask = unit->m_lomask;
1887 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1888 float radtoinc = unit->m_radtoinc;
1889 //Print("Osc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1890 LOOP1(inNumSamples,
1891 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1892 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1893 phase += freq;
1895 unit->m_phase = phase;
1896 //unit->m_phasein = phasein;
1899 void Osc_next_iaa(Osc *unit, int inNumSamples)
1901 // get table
1902 GET_TABLE
1903 const float *table0 = bufData;
1904 const float *table1 = table0 + 1;
1905 if (tableSize != unit->mTableSize) {
1906 unit->mTableSize = tableSize;
1907 int tableSize2 = tableSize >> 1;
1908 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1909 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1910 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1911 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1914 float *out = ZOUT(0);
1915 float *freqin = ZIN(1);
1916 float *phasein = ZIN(2);
1918 int32 phase = unit->m_phase;
1919 int32 lomask = unit->m_lomask;
1921 float cpstoinc = unit->m_cpstoinc;
1922 float radtoinc = unit->m_radtoinc;
1923 //Print("Osc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1924 LOOP1(inNumSamples,
1925 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1926 float z = lookupi1(table0, table1, phaseoffset, lomask);
1927 phase += (int32)(cpstoinc * ZXP(freqin));
1928 ZXP(out) = z;
1930 unit->m_phase = phase;
1931 //unit->m_phasein = ZX(phasein);
1935 void Osc_next_iak(Osc *unit, int inNumSamples)
1937 // get table
1938 GET_TABLE
1939 const float *table0 = bufData;
1940 const float *table1 = table0 + 1;
1941 if (tableSize != unit->mTableSize) {
1942 unit->mTableSize = tableSize;
1943 int tableSize2 = tableSize >> 1;
1944 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1945 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1946 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1947 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1950 float *out = ZOUT(0);
1951 float *freqin = ZIN(1);
1952 float phasein = ZIN0(2);
1954 int32 phase = unit->m_phase;
1955 int32 lomask = unit->m_lomask;
1957 float cpstoinc = unit->m_cpstoinc;
1958 float radtoinc = unit->m_radtoinc;
1959 float phasemod = unit->m_phasein;
1960 float phaseslope = CALCSLOPE(phasein, phasemod);
1962 LOOP1(inNumSamples,
1963 int32 pphase = phase + (int32)(radtoinc * phasemod);
1964 phasemod += phaseslope;
1965 float z = lookupi1(table0, table1, pphase, lomask);
1966 phase += (int32)(cpstoinc * ZXP(freqin));
1967 ZXP(out) = z;
1969 unit->m_phase = phase;
1970 unit->m_phasein = phasein;
1973 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1975 void OscN_Ctor(OscN *unit)
1977 unit->mTableSize = -1;
1979 float fbufnum = ZIN0(0);
1980 uint32 bufnum = (uint32)fbufnum;
1981 World *world = unit->mWorld;
1982 SndBuf *buf;
1983 if (bufnum >= world->mNumSndBufs) {
1984 int localBufNum = bufnum - world->mNumSndBufs;
1985 Graph *parent = unit->mParent;
1986 if(localBufNum <= parent->localBufNum) {
1987 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1988 } else {
1989 buf = unit->m_buf = world->mSndBufs;
1991 } else {
1992 buf = unit->m_buf = world->mSndBufs + bufnum;
1995 int tableSize = buf->samples;
1996 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
1998 unit->m_phasein = ZIN0(2);
1999 //Print("OscN_Ctor\n");
2000 if (INRATE(1) == calc_FullRate) {
2001 if (INRATE(2) == calc_FullRate) {
2002 //Print("next_naa\n");
2003 SETCALC(OscN_next_naa);
2004 unit->m_phase = 0;
2005 } else {
2006 //Print("next_nak\n");
2007 SETCALC(OscN_next_nak);
2008 unit->m_phase = 0;
2010 } else {
2011 if (INRATE(2) == calc_FullRate) {
2012 //Print("next_nka\n");
2013 SETCALC(OscN_next_nka);
2014 unit->m_phase = 0;
2015 } else {
2016 //Print("next_nkk\n");
2017 SETCALC(OscN_next_nkk);
2018 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
2022 OscN_next_nkk(unit, 1);
2026 void OscN_next_nkk(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 = ZIN0(1);
2040 float phasein = ZIN0(2);
2042 int32 phase = unit->m_phase;
2043 int32 lomask = unit->m_lomask;
2045 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2046 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
2047 unit->m_phasein = phasein;
2049 LOOP1(inNumSamples,
2050 ZXP(out) = *(float*)((char*)table + ((phase >> xlobits) & lomask));
2051 phase += phaseinc;
2053 unit->m_phase = phase;
2058 void OscN_next_nka(OscN *unit, int inNumSamples)
2060 // get table
2061 GET_TABLE
2062 const float *table = bufData;
2063 if (tableSize != unit->mTableSize) {
2064 unit->mTableSize = tableSize;
2065 unit->m_lomask = (tableSize - 1) << 2;
2066 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2067 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2070 float *out = ZOUT(0);
2071 float freqin = ZIN0(1);
2072 float *phasein = ZIN(2);
2074 int32 phase = unit->m_phase;
2075 int32 lomask = unit->m_lomask;
2077 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2078 float radtoinc = unit->m_radtoinc;
2079 LOOP1(inNumSamples,
2080 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2081 ZXP(out) = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2082 phase += freq;
2084 unit->m_phase = phase;
2087 void OscN_next_naa(OscN *unit, int inNumSamples)
2089 // get table
2090 GET_TABLE
2091 const float *table = bufData;
2092 if (tableSize != unit->mTableSize) {
2093 unit->mTableSize = tableSize;
2094 unit->m_lomask = (tableSize - 1) << 2;
2095 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2096 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2099 float *out = ZOUT(0);
2100 float *freqin = ZIN(1);
2101 float *phasein = ZIN(2);
2103 int32 phase = unit->m_phase;
2104 int32 lomask = unit->m_lomask;
2106 float cpstoinc = unit->m_cpstoinc;
2107 float radtoinc = unit->m_radtoinc;
2108 LOOP1(inNumSamples,
2109 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2110 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2111 phase += (int32)(cpstoinc * ZXP(freqin));
2112 ZXP(out) = z;
2114 unit->m_phase = phase;
2118 void OscN_next_nak(OscN *unit, int inNumSamples)
2120 // get table
2121 GET_TABLE
2122 const float *table = bufData;
2123 if (tableSize != unit->mTableSize) {
2124 unit->mTableSize = tableSize;
2125 unit->m_lomask = (tableSize - 1) << 2;
2126 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2127 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2130 float *out = ZOUT(0);
2131 float *freqin = ZIN(1);
2132 float phasein = ZIN0(2);
2134 int32 phase = unit->m_phase;
2135 int32 lomask = unit->m_lomask;
2137 float cpstoinc = unit->m_cpstoinc;
2138 float radtoinc = unit->m_radtoinc;
2139 float phasemod = unit->m_phasein;
2140 float phaseslope = CALCSLOPE(phasein, phasemod);
2142 LOOP1(inNumSamples,
2143 int32 pphase = phase + (int32)(radtoinc * phasemod);
2144 phasemod += phaseslope;
2145 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2146 phase += (int32)(cpstoinc * ZXP(freqin));
2147 ZXP(out) = z;
2149 unit->m_phase = phase;
2150 unit->m_phasein = phasein;
2153 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2155 void COsc_Ctor(COsc *unit)
2157 unit->m_fbufnum = -1e9f;
2158 SETCALC(COsc_next);
2159 unit->m_phase1 = 0;
2160 unit->m_phase2 = 0;
2161 unit->mTableSize = -1;
2162 COsc_next(unit, 1);
2166 void COsc_next(COsc *unit, int inNumSamples)
2168 // get table
2169 GET_TABLE
2170 const float *table0 = bufData;
2171 const float *table1 = table0 + 1;
2172 if (tableSize != unit->mTableSize) {
2173 unit->mTableSize = tableSize;
2174 int tableSize2 = tableSize >> 1;
2175 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
2176 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
2177 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2180 float *out = ZOUT(0);
2181 float freqin = ZIN0(1);
2182 float beats = ZIN0(2) * 0.5f;
2184 int32 phase1 = unit->m_phase1;
2185 int32 phase2 = unit->m_phase2;
2186 int32 lomask = unit->m_lomask;
2188 int32 cfreq = (int32)(unit->m_cpstoinc * freqin);
2189 int32 beatf = (int32)(unit->m_cpstoinc * beats);
2190 int32 freq1 = cfreq + beatf;
2191 int32 freq2 = cfreq - beatf;
2192 LOOP1(inNumSamples,
2193 float a = lookupi1(table0, table1, phase1, lomask);
2194 float b = lookupi1(table0, table1, phase2, lomask);
2195 ZXP(out) = a + b;
2196 phase1 += freq1;
2197 phase2 += freq2;
2199 unit->m_phase1 = phase1;
2200 unit->m_phase2 = phase2;
2203 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2204 #define VOSC_GET_BUF_UNLOCKED \
2205 const SndBuf *bufs; \
2206 if (bufnum < 0) \
2207 bufnum = 0; \
2209 if (bufnum+1 >= world->mNumSndBufs) { \
2210 int localBufNum = bufnum - world->mNumSndBufs; \
2211 Graph *parent = unit->mParent; \
2212 if(localBufNum <= parent->localBufNum) { \
2213 bufs = parent->mLocalSndBufs + localBufNum; \
2214 } else { \
2215 bufnum = 0; \
2216 bufs = world->mSndBufs + bufnum; \
2218 } else { \
2219 if (bufnum >= world->mNumSndBufs) \
2220 bufnum = 0; \
2221 bufs = world->mSndBufs + sc_max(0, bufnum); \
2224 #define VOSC_GET_BUF \
2225 VOSC_GET_BUF_UNLOCKED \
2226 LOCK_SNDBUF_SHARED(bufs);
2228 void VOsc_Ctor(VOsc *unit)
2230 SETCALC(VOsc_next_ik);
2232 float nextbufpos = ZIN0(0);
2233 unit->m_bufpos = nextbufpos;
2234 int bufnum = sc_floor(nextbufpos);
2235 World *world = unit->mWorld;
2237 VOSC_GET_BUF_UNLOCKED
2239 int tableSize = bufs[0].samples;
2241 unit->mTableSize = tableSize;
2242 int tableSize2 = tableSize >> 1;
2243 unit->m_lomask = (tableSize2 - 1) << 3;
2244 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
2245 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2247 unit->m_phasein = ZIN0(2);
2248 unit->m_phaseoffset = (int32)(unit->m_phasein * unit->m_radtoinc);
2249 unit->m_phase = unit->m_phaseoffset;
2251 VOsc_next_ik(unit, 1);
2254 void VOsc_next_ik(VOsc *unit, int inNumSamples)
2256 float *out = ZOUT(0);
2257 float nextbufpos = ZIN0(0);
2258 float freqin = ZIN0(1);
2259 float phasein = ZIN0(2);
2261 float prevbufpos = unit->m_bufpos;
2262 float bufdiff = nextbufpos - prevbufpos;
2264 int32 phase = unit->m_phase;
2265 int32 lomask = unit->m_lomask;
2267 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2268 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
2269 unit->m_phasein = phasein;
2270 int tableSize = unit->mTableSize;
2271 float cur = prevbufpos;
2272 World *world = unit->mWorld;
2274 if (bufdiff == 0.f) {
2275 float level = cur - sc_floor(cur);
2276 uint32 bufnum = (int)sc_floor(cur);
2278 VOSC_GET_BUF
2280 const float *table0 = bufs[0].data;
2281 const float *table2 = bufs[1].data;
2282 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2283 ClearUnitOutputs(unit, inNumSamples);
2284 return;
2287 const float *table1 = table0 + 1;
2288 const float *table3 = table2 + 1;
2290 LOOP1(inNumSamples,
2291 float pfrac = PhaseFrac1(phase);
2292 uint32 index = ((phase >> xlobits1) & lomask);
2293 float val0 = *(float*)((char*)table0 + index);
2294 float val1 = *(float*)((char*)table1 + index);
2295 float val2 = *(float*)((char*)table2 + index);
2296 float val3 = *(float*)((char*)table3 + index);
2297 float a = val0 + val1 * pfrac;
2298 float b = val2 + val3 * pfrac;
2299 ZXP(out) = a + level * (b - a);
2300 phase += phaseinc;
2302 } else {
2303 int nsmps;
2304 int donesmps = 0;
2305 int remain = inNumSamples;
2306 while (remain) {
2307 float level = cur - sc_floor(cur);
2309 float cut;
2310 if (bufdiff > 0.) {
2311 cut = sc_min(nextbufpos, sc_floor(cur+1.f));
2312 } else {
2313 cut = sc_max(nextbufpos, sc_ceil(cur-1.f));
2316 float sweepdiff = cut - cur;
2317 if (cut == nextbufpos) nsmps = remain;
2318 else {
2319 float sweep = (float)inNumSamples / bufdiff;
2320 nsmps = (int)sc_floor(sweep * sweepdiff + 0.5f) - donesmps;
2321 nsmps = sc_clip(nsmps, 1, remain);
2324 float slope = sweepdiff / (float)nsmps;
2326 int32 bufnum = (int32)sc_floor(cur);
2328 VOSC_GET_BUF
2330 const float *table0 = bufs[0].data;
2331 const float *table2 = bufs[1].data;
2332 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2333 ClearUnitOutputs(unit, inNumSamples);
2334 return;
2337 const float *table1 = table0 + 1;
2338 const float *table3 = table2 + 1;
2340 LOOP(nsmps,
2341 float pfrac = PhaseFrac1(phase);
2342 uint32 index = ((phase >> xlobits1) & lomask);
2343 float val0 = *(float*)((char*)table0 + index);
2344 float val1 = *(float*)((char*)table1 + index);
2345 float val2 = *(float*)((char*)table2 + index);
2346 float val3 = *(float*)((char*)table3 + index);
2347 float a = val0 + val1 * pfrac;
2348 float b = val2 + val3 * pfrac;
2349 ZXP(out) = a + level * (b - a);
2350 phase += phaseinc;
2351 level += slope;
2353 donesmps += nsmps;
2354 remain -= nsmps;
2355 cur = cut;
2358 unit->m_bufpos = nextbufpos;
2359 unit->m_phase = phase;
2362 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2364 void VOsc3_Ctor(VOsc3 *unit)
2366 SETCALC(VOsc3_next_ik);
2368 float nextbufpos = ZIN0(0);
2369 unit->m_bufpos = nextbufpos;
2370 int32 bufnum = (int32)sc_floor(nextbufpos);
2371 World *world = unit->mWorld;
2373 VOSC_GET_BUF
2374 int tableSize = bufs[0].samples;
2376 unit->mTableSize = tableSize;
2377 int tableSize2 = tableSize >> 1;
2378 unit->m_lomask = (tableSize2 - 1) << 3;
2379 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2381 unit->m_phase1 = 0;
2382 unit->m_phase2 = 0;
2383 unit->m_phase3 = 0;
2385 VOsc3_next_ik(unit, 1);
2388 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples)
2390 float *out = ZOUT(0);
2391 float nextbufpos = ZIN0(0);
2392 float freq1in = ZIN0(1);
2393 float freq2in = ZIN0(2);
2394 float freq3in = ZIN0(3);
2396 float prevbufpos = unit->m_bufpos;
2397 float bufdiff = nextbufpos - prevbufpos;
2399 int32 phase1 = unit->m_phase1;
2400 int32 phase2 = unit->m_phase2;
2401 int32 phase3 = unit->m_phase3;
2403 int32 freq1 = (int32)(unit->m_cpstoinc * freq1in);
2404 int32 freq2 = (int32)(unit->m_cpstoinc * freq2in);
2405 int32 freq3 = (int32)(unit->m_cpstoinc * freq3in);
2407 int32 lomask = unit->m_lomask;
2408 int tableSize = unit->mTableSize;
2409 float cur = prevbufpos;
2410 World *world = unit->mWorld;
2412 if (bufdiff == 0.f) {
2413 float level = cur - (int)cur;
2415 int bufnum = (int)cur;
2417 VOSC_GET_BUF
2419 const float *table0 = bufs[0].data;
2420 const float *table2 = bufs[1].data;
2421 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2422 ClearUnitOutputs(unit, inNumSamples);
2423 return;
2426 const float *table1 = table0 + 1;
2427 const float *table3 = table2 + 1;
2429 LOOP1(inNumSamples,
2431 float pfrac1 = PhaseFrac1(phase1);
2432 float pfrac2 = PhaseFrac1(phase2);
2433 float pfrac3 = PhaseFrac1(phase3);
2435 int index1 = ((phase1 >> xlobits1) & lomask);
2436 int index2 = ((phase2 >> xlobits1) & lomask);
2437 int index3 = ((phase3 >> xlobits1) & lomask);
2439 phase1 += freq1;
2440 phase2 += freq2;
2441 phase3 += freq3;
2443 float val10 = *(float*)((char*)table0 + index1);
2444 float val11 = *(float*)((char*)table1 + index1);
2445 float val12 = *(float*)((char*)table2 + index1);
2446 float val13 = *(float*)((char*)table3 + index1);
2447 float a = val10 + val11 * pfrac1;
2448 float b = val12 + val13 * pfrac1;
2450 float val20 = *(float*)((char*)table0 + index2);
2451 float val21 = *(float*)((char*)table1 + index2);
2452 float val22 = *(float*)((char*)table2 + index2);
2453 float val23 = *(float*)((char*)table3 + index2);
2454 a += val20 + val21 * pfrac2;
2455 b += val22 + val23 * pfrac2;
2457 float val30 = *(float*)((char*)table0 + index3);
2458 float val31 = *(float*)((char*)table1 + index3);
2459 float val32 = *(float*)((char*)table2 + index3);
2460 float val33 = *(float*)((char*)table3 + index3);
2461 a += val30 + val31 * pfrac3;
2462 b += val32 + val33 * pfrac3;
2464 ZXP(out) = a + level * (b - a);
2466 } else {
2467 int nsmps;
2468 int donesmps = 0;
2469 int remain = inNumSamples;
2470 do {
2471 float level = cur - sc_trunc(cur);
2473 float cut;
2474 if (bufdiff >= 0.)
2475 cut = sc_min(nextbufpos, sc_trunc(cur+1.f));
2476 else
2477 cut = sc_max(nextbufpos, sc_ceil(cur-1.f));
2479 float sweepdiff = cut - cur;
2480 if (cut == nextbufpos) nsmps = remain;
2481 else {
2482 float sweep = (float)inNumSamples / bufdiff;
2483 nsmps = sc_floor(sweep * sweepdiff + 0.5f) - donesmps;
2484 nsmps = sc_clip(nsmps, 1, remain);
2487 float slope = sweepdiff / (float)nsmps;
2489 int bufnum = (int)cur;
2491 VOSC_GET_BUF
2493 const float *table0 = bufs[0].data;
2494 const float *table2 = bufs[1].data;
2495 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2496 ClearUnitOutputs(unit, inNumSamples);
2497 return;
2500 const float *table1 = table0 + 1;
2501 const float *table3 = table2 + 1;
2503 LOOP(nsmps,
2505 float pfrac1 = PhaseFrac1(phase1);
2506 float pfrac2 = PhaseFrac1(phase2);
2507 float pfrac3 = PhaseFrac1(phase3);
2509 int index1 = ((phase1 >> xlobits1) & lomask);
2510 int index2 = ((phase2 >> xlobits1) & lomask);
2511 int index3 = ((phase3 >> xlobits1) & lomask);
2513 phase1 += freq1;
2514 phase2 += freq2;
2515 phase3 += freq3;
2517 float val10 = *(float*)((char*)table0 + index1);
2518 float val11 = *(float*)((char*)table1 + index1);
2519 float val12 = *(float*)((char*)table2 + index1);
2520 float val13 = *(float*)((char*)table3 + index1);
2521 float a = val10 + val11 * pfrac1;
2522 float b = val12 + val13 * pfrac1;
2524 float val20 = *(float*)((char*)table0 + index2);
2525 float val21 = *(float*)((char*)table1 + index2);
2526 float val22 = *(float*)((char*)table2 + index2);
2527 float val23 = *(float*)((char*)table3 + index2);
2528 a += val20 + val21 * pfrac2;
2529 b += val22 + val23 * pfrac2;
2531 float val30 = *(float*)((char*)table0 + index3);
2532 float val31 = *(float*)((char*)table1 + index3);
2533 float val32 = *(float*)((char*)table2 + index3);
2534 float val33 = *(float*)((char*)table3 + index3);
2535 a += val30 + val31 * pfrac3;
2536 b += val32 + val33 * pfrac3;
2538 ZXP(out) = a + level * (b - a);
2539 level += slope;
2541 donesmps += nsmps;
2542 remain -= nsmps;
2543 cur = cut;
2544 } while (remain);
2546 unit->m_bufpos = nextbufpos;
2547 unit->m_phase1 = phase1;
2548 unit->m_phase2 = phase2;
2549 unit->m_phase3 = phase3;
2552 //////////////////////////////////////////////////////////////////////////////////////////
2554 void Formant_Ctor(Formant *unit)
2556 SETCALC(Formant_next);
2557 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536.;
2558 unit->m_phase1 = 0;
2559 unit->m_phase2 = 0;
2560 unit->m_phase3 = 0;
2561 Formant_next(unit, 1);
2564 #define tqcyc13 0x18000000
2566 void Formant_next(Formant *unit, int inNumSamples)
2568 float *out = ZOUT(0);
2569 float freq1in = ZIN0(0);
2570 float freq2in = ZIN0(1);
2571 float freq3in = ZIN0(2);
2573 int32 phase1 = unit->m_phase1;
2574 int32 phase2 = unit->m_phase2;
2575 int32 phase3 = unit->m_phase3;
2576 float cpstoinc = unit->m_cpstoinc;
2577 int32 freq1 = (int32)(cpstoinc * freq1in);
2578 int32 freq2 = (int32)(cpstoinc * freq2in);
2579 int32 freq3 = (int32)(cpstoinc * freq3in);
2580 float* sine = ft->mSine;
2581 int32 formfreq = sc_max(freq1, freq3);
2582 LOOP1(inNumSamples,
2583 if (phase3 < onecyc13) {
2584 ZXP(out) = (*(float*)((char*)sine + (((phase3 + tqcyc13) >> xlobits) & xlomask13)) + 1.f)
2585 * *(float*)((char*)sine + ((phase2 >> xlobits) & xlomask13));
2586 phase3 += formfreq;
2587 } else {
2588 ZXP(out) = 0.f;
2590 phase1 += freq1;
2591 phase2 += freq2;
2592 if (phase1 > onecyc13) {
2593 phase1 -= onecyc13;
2594 phase2 = phase1 * freq2 / freq1;
2595 phase3 = phase1 * freq3 / freq1;
2599 unit->m_phase1 = phase1;
2600 unit->m_phase2 = phase2;
2601 unit->m_phase3 = phase3;
2604 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2606 inline float lookup13(float* table, int32 pphase)
2608 float pfrac = PhaseFrac(pphase);
2609 float* tbl = (float*)((char*)table + ((pphase >> xlobits) & xlomask13));
2610 return lininterp(pfrac, tbl[0], tbl[1]);
2613 void Blip_Ctor(Blip *unit)
2615 SETCALC(Blip_next);
2616 unit->m_freqin = ZIN0(0);
2617 unit->m_numharm = (int32)ZIN0(1);
2619 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2620 int32 N = unit->m_numharm;
2621 int32 maxN = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2622 if (N > maxN) N = maxN;
2623 if (N < 1) N = 1;
2624 unit->m_N = N;
2625 unit->m_scale = 0.5/N;
2626 unit->m_phase = 0;
2628 Blip_next(unit, 1);
2631 void Blip_next(Blip *unit, int inNumSamples)
2633 float *out = ZOUT(0);
2634 float freqin = ZIN0(0);
2635 int numharm = (int32)ZIN0(1);
2637 int32 phase = unit->m_phase;
2639 float* numtbl = ft->mSine;
2640 float* dentbl = ft->mCosecant;
2642 int32 freq, N, prevN;
2643 float scale, prevscale;
2644 bool crossfade;
2645 if (numharm != unit->m_numharm || freqin != unit->m_freqin) {
2646 N = numharm;
2647 int32 maxN = (int32)((SAMPLERATE * 0.5) / freqin);
2648 if (N > maxN) {
2649 float maxfreqin;
2650 N = maxN;
2651 maxfreqin = sc_max(unit->m_freqin, freqin);
2652 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2653 } else {
2654 if (N < 1) { N = 1; }
2655 freq = (int32)(unit->m_cpstoinc * freqin);
2657 crossfade = N != unit->m_N;
2658 prevN = unit->m_N;
2659 prevscale = unit->m_scale;
2660 unit->m_N = N;
2661 unit->m_scale = scale = 0.5/N;
2662 } else {
2663 N = unit->m_N;
2664 freq = (int32)(unit->m_cpstoinc * freqin);
2665 scale = unit->m_scale;
2666 crossfade = false;
2668 int32 N2 = 2*N+1;
2670 if (crossfade) {
2671 int32 prevN2 = 2 * prevN + 1;
2672 float xfade_slope = unit->mRate->mSlopeFactor;
2673 float xfade = 0.f;
2674 LOOP1(inNumSamples,
2675 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2676 float t0 = tbl[0];
2677 float t1 = tbl[1];
2678 if (t0 == kBadValue || t1 == kBadValue) {
2679 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2680 t0 = tbl[0];
2681 t1 = tbl[1];
2682 float pfrac = PhaseFrac(phase);
2683 float denom = t0 + (t1 - t0) * pfrac;
2684 if (std::abs(denom) < 0.0005f) {
2685 ZXP(out) = 1.f;
2686 } else {
2687 int32 rphase = phase * prevN2;
2688 pfrac = PhaseFrac(rphase);
2689 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2690 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2691 float n1 = (numer / denom - 1.f) * prevscale;
2693 rphase = phase * N2;
2694 pfrac = PhaseFrac(rphase);
2695 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2696 numer = lininterp(pfrac, tbl[0], tbl[1]);
2697 float n2 = (numer / denom - 1.f) * scale;
2699 ZXP(out) = lininterp(xfade, n1, n2);
2701 } else {
2702 float pfrac = PhaseFrac(phase);
2703 float denom = t0 + (t1 - t0) * pfrac;
2705 int32 rphase = phase * prevN2;
2706 pfrac = PhaseFrac(rphase);
2707 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2708 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2709 float n1 = (numer * denom - 1.f) * prevscale;
2711 rphase = phase * N2;
2712 pfrac = PhaseFrac(rphase);
2713 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2714 numer = lininterp(pfrac, tbl[0], tbl[1]);
2715 float n2 = (numer * denom - 1.f) * scale;
2717 ZXP(out) = lininterp(xfade, n1, n2);
2719 phase += freq;
2720 xfade += xfade_slope;
2722 } else {
2723 // hmm, if freq is above sr/4 then revert to sine table osc w/ no interpolation ?
2724 // why bother, it isn't a common choice for a fundamental.
2725 LOOP1(inNumSamples,
2726 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2727 float t0 = tbl[0];
2728 float t1 = tbl[1];
2729 if (t0 == kBadValue || t1 == kBadValue) {
2730 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2731 t0 = tbl[0];
2732 t1 = tbl[1];
2733 float pfrac = PhaseFrac(phase);
2734 float denom = t0 + (t1 - t0) * pfrac;
2735 if (std::abs(denom) < 0.0005f) {
2736 ZXP(out) = 1.f;
2737 } else {
2738 int32 rphase = phase * N2;
2739 pfrac = PhaseFrac(rphase);
2740 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2741 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2742 ZXP(out) = (numer / denom - 1.f) * scale;
2744 } else {
2745 float pfrac = PhaseFrac(phase);
2746 float denom = t0 + (t1 - t0) * pfrac;
2747 int32 rphase = phase * N2;
2748 pfrac = PhaseFrac(rphase);
2749 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2750 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2751 ZXP(out) = (numer * denom - 1.f) * scale;
2753 phase += freq;
2757 unit->m_phase = phase;
2758 unit->m_freqin = freqin;
2759 unit->m_numharm = numharm;
2763 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2766 void Saw_Ctor(Saw *unit)
2768 SETCALC(Saw_next);
2769 unit->m_freqin = ZIN0(0);
2771 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2772 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2773 unit->m_scale = 0.5/unit->m_N;
2774 unit->m_phase = 0;
2775 unit->m_y1 = -0.46f;
2777 ZOUT0(0) = 0.f;
2780 void Saw_next(Saw *unit, int inNumSamples)
2782 float *out = ZOUT(0);
2783 float freqin = ZIN0(0);
2785 int32 phase = unit->m_phase;
2786 float y1 = unit->m_y1;
2788 float* numtbl = ft->mSine;
2789 float* dentbl = ft->mCosecant;
2791 int32 freq, N, prevN;
2792 float scale, prevscale;
2793 bool crossfade;
2794 if (freqin != unit->m_freqin) {
2795 N = (int32)((SAMPLERATE * 0.5) / freqin);
2796 if (N != unit->m_N) {
2797 float maxfreqin;
2798 maxfreqin = sc_max(unit->m_freqin, freqin);
2799 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2800 crossfade = true;
2801 } else {
2802 freq = (int32)(unit->m_cpstoinc * freqin);
2803 crossfade = false;
2805 prevN = unit->m_N;
2806 prevscale = unit->m_scale;
2807 unit->m_N = N;
2808 unit->m_scale = scale = 0.5/N;
2809 } else {
2810 N = unit->m_N;
2811 freq = (int32)(unit->m_cpstoinc * freqin);
2812 scale = unit->m_scale;
2813 crossfade = false;
2815 int32 N2 = 2*N+1;
2817 if (crossfade) {
2818 int32 prevN2 = 2 * prevN + 1;
2819 float xfade_slope = unit->mRate->mSlopeFactor;
2820 float xfade = 0.f;
2821 LOOP1(inNumSamples,
2822 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2823 float t0 = tbl[0];
2824 float t1 = tbl[1];
2825 if (t0 == kBadValue || t1 == kBadValue) {
2826 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2827 t0 = tbl[0];
2828 t1 = tbl[1];
2829 float pfrac = PhaseFrac(phase);
2830 float denom = t0 + (t1 - t0) * pfrac;
2831 if (std::abs(denom) < 0.0005f) {
2832 ZXP(out) = y1 = 1.f + 0.999f * y1;
2833 } else {
2834 int32 rphase = phase * prevN2;
2835 pfrac = PhaseFrac(rphase);
2836 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2837 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2838 float n1 = (numer / denom - 1.f) * prevscale;
2840 rphase = phase * N2;
2841 pfrac = PhaseFrac(rphase);
2842 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2843 numer = lininterp(pfrac, tbl[0], tbl[1]);
2844 float n2 = (numer / denom - 1.f) * scale;
2846 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2849 } else {
2850 float pfrac = PhaseFrac(phase);
2851 float denom = t0 + (t1 - t0) * pfrac;
2853 int32 rphase = phase * prevN2;
2854 pfrac = PhaseFrac(rphase);
2855 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2856 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2857 float n1 = (numer * denom - 1.f) * prevscale;
2859 rphase = phase * N2;
2860 pfrac = PhaseFrac(rphase);
2861 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2862 numer = lininterp(pfrac, tbl[0], tbl[1]);
2863 float n2 = (numer * denom - 1.f) * scale;
2865 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2867 phase += freq;
2868 xfade += xfade_slope;
2870 } else {
2871 // hmm, if freq is above sr/4 then revert to sine table osc ?
2872 // why bother, it isn't a common choice for a fundamental.
2873 LOOP1(inNumSamples,
2874 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2875 float t0 = tbl[0];
2876 float t1 = tbl[1];
2877 if (t0 == kBadValue || t1 == kBadValue) {
2878 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2879 t0 = tbl[0];
2880 t1 = tbl[1];
2881 float pfrac = PhaseFrac(phase);
2882 float denom = t0 + (t1 - t0) * pfrac;
2883 if (std::abs(denom) < 0.0005f) {
2884 ZXP(out) = y1 = 1.f + 0.999f * y1;
2885 } else {
2886 int32 rphase = phase * N2;
2887 pfrac = PhaseFrac(rphase);
2888 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2889 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2890 ZXP(out) = y1 = (numer / denom - 1.f) * scale + 0.999f * y1;
2892 } else {
2893 float pfrac = PhaseFrac(phase);
2894 float denom = t0 + (t1 - t0) * pfrac;
2895 int32 rphase = phase * N2;
2896 pfrac = PhaseFrac(rphase);
2897 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2898 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2899 ZXP(out) = y1 = (numer * denom - 1.f) * scale + 0.999f * y1;
2901 phase += freq;
2905 unit->m_y1 = y1;
2906 unit->m_phase = phase;
2907 unit->m_freqin = freqin;
2911 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2914 void Pulse_Ctor(Pulse *unit)
2916 SETCALC(Pulse_next);
2917 unit->m_freqin = ZIN0(0);
2919 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2920 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2921 unit->m_scale = 0.5/unit->m_N;
2922 unit->m_phase = 0;
2923 unit->m_phaseoff = 0;
2924 unit->m_y1 = 0.f;
2925 ZOUT0(0) = 0.f;
2928 void Pulse_next(Pulse *unit, int inNumSamples)
2930 float *out = ZOUT(0);
2931 float freqin = ZIN0(0);
2932 float duty = ZIN0(1);
2934 int32 phase = unit->m_phase;
2935 float y1 = unit->m_y1;
2937 float* numtbl = ft->mSine;
2938 float* dentbl = ft->mCosecant;
2940 int32 freq, N, prevN;
2941 float scale, prevscale;
2942 bool crossfade;
2944 if (freqin != unit->m_freqin) {
2945 N = (int32)((SAMPLERATE * 0.5) / freqin);
2946 if (N != unit->m_N) {
2947 float maxfreqin;
2948 maxfreqin = sc_max(unit->m_freqin, freqin);
2949 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2950 crossfade = true;
2951 } else {
2952 freq = (int32)(unit->m_cpstoinc * freqin);
2953 crossfade = false;
2955 prevN = unit->m_N;
2956 prevscale = unit->m_scale;
2957 unit->m_N = N;
2958 unit->m_scale = scale = 0.5/N;
2959 } else {
2960 N = unit->m_N;
2961 freq = (int32)(unit->m_cpstoinc * freqin);
2962 scale = unit->m_scale;
2963 crossfade = false;
2965 int32 N2 = 2*N+1;
2967 int32 phaseoff = unit->m_phaseoff;
2968 int32 next_phaseoff = (int32)(duty * (1L << 28));
2969 int32 phaseoff_slope = (int32)((next_phaseoff - phaseoff) * unit->mRate->mSlopeFactor);
2970 unit->m_phaseoff = next_phaseoff;
2971 float rscale = 1.f / scale + 1.f;
2972 float pul1, pul2;
2974 if (crossfade) {
2975 int32 prevN2 = 2 * prevN + 1;
2976 float xfade_slope = unit->mRate->mSlopeFactor;
2977 float xfade = 0.f;
2978 LOOP1(inNumSamples,
2979 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2980 float t0 = tbl[0];
2981 float t1 = tbl[1];
2982 if (t0 == kBadValue || t1 == kBadValue) {
2983 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2984 t0 = tbl[0];
2985 t1 = tbl[1];
2986 float pfrac = PhaseFrac(phase);
2987 float denom = t0 + (t1 - t0) * pfrac;
2988 if (std::abs(denom) < 0.0005f) {
2989 pul1 = 1.f;
2990 } else {
2991 int32 rphase = phase * 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 = phase * 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 pul1 = lininterp(xfade, n1, n2);
3005 } else {
3006 float pfrac = PhaseFrac(phase);
3007 float denom = lininterp(pfrac, t0, t1);
3009 int32 rphase = phase * prevN2;
3010 pfrac = PhaseFrac(rphase);
3011 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3012 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3013 float n1 = (numer * denom - 1.f) * prevscale;
3015 rphase = phase * N2;
3016 pfrac = PhaseFrac(rphase);
3017 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3018 numer = lininterp(pfrac, tbl[0], tbl[1]);
3019 float n2 = (numer * denom - 1.f) * scale;
3021 pul1 = lininterp(xfade, n1, n2);
3024 int32 phase2 = phase + phaseoff;
3025 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
3026 t0 = tbl[0];
3027 t1 = tbl[1];
3028 if (t0 == kBadValue || t1 == kBadValue) {
3029 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
3030 t0 = tbl[0];
3031 t1 = tbl[1];
3032 float pfrac = PhaseFrac(phase2);
3033 float denom = t0 + (t1 - t0) * pfrac;
3034 if (std::abs(denom) < 0.0005f) {
3035 pul2 = 1.f;
3036 } else {
3037 int32 rphase = phase2 * prevN2;
3038 pfrac = PhaseFrac(rphase);
3039 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3040 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3041 float n1 = (numer / denom - 1.f) * prevscale;
3043 rphase = phase2 * N2;
3044 pfrac = PhaseFrac(rphase);
3045 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3046 numer = lininterp(pfrac, tbl[0], tbl[1]);
3047 float n2 = (numer / denom - 1.f) * scale;
3049 pul2 = lininterp(xfade, n1, n2);
3051 } else {
3052 float pfrac = PhaseFrac(phase2);
3053 float denom = t0 + (t1 - t0) * pfrac;
3055 int32 rphase = phase2 * prevN2;
3056 pfrac = PhaseFrac(rphase);
3057 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3058 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3059 float n1 = (numer * denom - 1.f) * prevscale;
3061 rphase = phase2 * N2;
3062 pfrac = PhaseFrac(rphase);
3063 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3064 numer = lininterp(pfrac, tbl[0], tbl[1]);
3065 float n2 = (numer * denom - 1.f) * scale;
3067 pul2 = lininterp(xfade, n1, n2);
3070 ZXP(out) = y1 = pul1 - pul2 + 0.999f * y1;
3071 phase += freq;
3072 phaseoff += phaseoff_slope;
3073 xfade += xfade_slope;
3075 } else {
3076 LOOP1(inNumSamples,
3077 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
3078 float t0 = tbl[0];
3079 float t1 = tbl[1];
3080 if (t0 == kBadValue || t1 == kBadValue) {
3081 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
3082 t0 = tbl[0];
3083 t1 = tbl[1];
3084 float pfrac = PhaseFrac(phase);
3085 float denom = t0 + (t1 - t0) * pfrac;
3086 if (std::abs(denom) < 0.0005f) {
3087 pul1 = rscale;
3088 } else {
3089 int32 rphase = phase * N2;
3090 pfrac = PhaseFrac(rphase);
3091 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3092 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3093 pul1 = numer / denom;
3095 } else {
3096 float pfrac = PhaseFrac(phase);
3097 float denom = t0 + (t1 - t0) * pfrac;
3098 int32 rphase = phase * N2;
3099 pfrac = PhaseFrac(rphase);
3100 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3101 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3102 pul1 = (numer * denom);
3105 int32 phase2 = phase + phaseoff;
3106 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
3107 t0 = tbl[0];
3108 t1 = tbl[1];
3109 if (t0 == kBadValue || t1 == kBadValue) {
3110 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
3111 t0 = tbl[0];
3112 t1 = tbl[1];
3113 float pfrac = PhaseFrac(phase2);
3114 float denom = t0 + (t1 - t0) * pfrac;
3115 if (std::abs(denom) < 0.0005f) {
3116 pul2 = rscale;
3117 } else {
3118 int32 rphase = phase2 * N2;
3119 pfrac = PhaseFrac(rphase);
3120 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3121 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3122 pul2 = numer / denom;
3124 } else {
3125 float pfrac = PhaseFrac(phase2);
3126 float denom = t0 + (t1 - t0) * pfrac;
3127 int32 rphase = phase2 * N2;
3128 pfrac = PhaseFrac(rphase);
3129 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3130 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3132 pul2 = (numer * denom);
3135 ZXP(out) = y1 = (pul1 - pul2) * scale + 0.999f * y1;
3136 phase += freq;
3137 phaseoff += phaseoff_slope;
3141 unit->m_y1 = y1;
3142 unit->m_phase = phase;
3143 unit->m_freqin = freqin;
3146 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3148 void Klang_Dtor(Klang *unit)
3150 RTFree(unit->mWorld, unit->m_coefs);
3153 static float Klang_SetCoefs(Klang *unit);
3155 void Klang_Ctor(Klang *unit)
3157 SETCALC(Klang_next);
3158 ZOUT0(0) = Klang_SetCoefs(unit);
3161 float Klang_SetCoefs(Klang *unit)
3163 unit->m_numpartials = (unit->mNumInputs - 2)/3;
3165 int numcoefs = unit->m_numpartials * 3;
3166 unit->m_coefs = (float*)RTAlloc(unit->mWorld, numcoefs * sizeof(float));
3168 float freqscale = ZIN0(0) * unit->mRate->mRadiansPerSample;
3169 float freqoffset = ZIN0(1) * unit->mRate->mRadiansPerSample;
3171 float outf = 0.;
3172 float* coefs = unit->m_coefs - 1;
3174 for (int i=0,j=2; i<unit->m_numpartials; ++i,j+=3) {
3175 float w = ZIN0(j) * freqscale + freqoffset;
3176 float level = ZIN0(j+1);
3177 float phase = ZIN0(j+2);
3179 if (phase != 0.f) {
3180 outf += *++coefs = level * sin(phase); // y1
3181 *++coefs = level * sin(phase - w); // y2
3182 } else {
3183 outf += *++coefs = 0.f; // y1
3184 *++coefs = level * -sin(w); // y2
3186 *++coefs = 2. * cos(w); // b1
3188 return outf;
3191 void Klang_next(Klang *unit, int inNumSamples)
3193 float *out0 = ZOUT(0);
3195 float* out;
3196 float y0_0, y1_0, y2_0, b1_0;
3197 float y0_1, y1_1, y2_1, b1_1;
3198 float y0_2, y1_2, y2_2, b1_2;
3199 float y0_3, y1_3, y2_3, b1_3;
3200 float outf;
3202 float* coefs = unit->m_coefs - 1;
3203 int32 numpartials = unit->m_numpartials;
3205 switch (numpartials & 3) {
3206 case 3 :
3207 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3208 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3209 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3211 out = out0;
3212 LOOP(unit->mRate->mFilterLoops,
3213 outf = y0_0 = b1_0 * y1_0 - y2_0;
3214 outf += y0_1 = b1_1 * y1_1 - y2_1;
3215 outf += y0_2 = b1_2 * y1_2 - y2_2;
3216 ZXP(out) = outf;
3218 outf = y2_0 = b1_0 * y0_0 - y1_0;
3219 outf += y2_1 = b1_1 * y0_1 - y1_1;
3220 outf += y2_2 = b1_2 * y0_2 - y1_2;
3221 ZXP(out) = outf;
3223 outf = y1_0 = b1_0 * y2_0 - y0_0;
3224 outf += y1_1 = b1_1 * y2_1 - y0_1;
3225 outf += y1_2 = b1_2 * y2_2 - y0_2;
3226 ZXP(out) = outf;
3228 LOOP(unit->mRate->mFilterRemain,
3229 outf = y0_0 = b1_0 * y1_0 - y2_0;
3230 outf += y0_1 = b1_1 * y1_1 - y2_1;
3231 outf += y0_2 = b1_2 * y1_2 - y2_2;
3232 y2_0 = y1_0; y1_0 = y0_0;
3233 y2_1 = y1_1; y1_1 = y0_1;
3234 y2_2 = y1_2; y1_2 = y0_2;
3235 ZXP(out) = outf;
3237 coefs -= 9;
3238 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3239 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3240 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3241 break;
3242 case 2 :
3243 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3244 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3246 out = out0;
3247 LOOP(unit->mRate->mFilterLoops,
3248 outf = y0_0 = b1_0 * y1_0 - y2_0;
3249 outf += y0_1 = b1_1 * y1_1 - y2_1;
3250 ZXP(out) = outf;
3252 outf = y2_0 = b1_0 * y0_0 - y1_0;
3253 outf += y2_1 = b1_1 * y0_1 - y1_1;
3254 ZXP(out) = outf;
3256 outf = y1_0 = b1_0 * y2_0 - y0_0;
3257 outf += y1_1 = b1_1 * y2_1 - y0_1;
3258 ZXP(out) = outf;
3260 LOOP(unit->mRate->mFilterRemain,
3261 outf = y0_0 = b1_0 * y1_0 - y2_0;
3262 outf += y0_1 = b1_1 * y1_1 - y2_1;
3263 y2_0 = y1_0; y1_0 = y0_0;
3264 y2_1 = y1_1; y1_1 = y0_1;
3265 ZXP(out) = outf;
3268 coefs -= 6;
3269 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3270 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3271 break;
3272 case 1 :
3273 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3275 out = out0;
3276 LOOP(unit->mRate->mFilterLoops,
3277 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3279 ZXP(out) = y2_0 = b1_0 * y0_0 - y1_0;
3281 ZXP(out) = y1_0 = b1_0 * y2_0 - y0_0;
3283 LOOP(unit->mRate->mFilterRemain,
3284 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3285 y2_0 = y1_0; y1_0 = y0_0;
3288 coefs -= 3;
3289 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3290 break;
3291 case 0 :
3292 out = out0;
3293 ZClear(inNumSamples, out);
3294 break;
3297 int32 imax = numpartials >> 2;
3299 for (int i=0; i<imax; ++i) {
3300 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3301 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3302 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3303 y1_3 = *++coefs; y2_3 = *++coefs; b1_3 = *++coefs;
3305 out = out0;
3306 LOOP(unit->mRate->mFilterLoops,
3307 outf = y0_0 = b1_0 * y1_0 - y2_0;
3308 outf += y0_1 = b1_1 * y1_1 - y2_1;
3309 outf += y0_2 = b1_2 * y1_2 - y2_2;
3310 outf += y0_3 = b1_3 * y1_3 - y2_3;
3311 ZXP(out) += outf;
3313 outf = y2_0 = b1_0 * y0_0 - y1_0;
3314 outf += y2_1 = b1_1 * y0_1 - y1_1;
3315 outf += y2_2 = b1_2 * y0_2 - y1_2;
3316 outf += y2_3 = b1_3 * y0_3 - y1_3;
3317 ZXP(out) += outf;
3319 outf = y1_0 = b1_0 * y2_0 - y0_0;
3320 outf += y1_1 = b1_1 * y2_1 - y0_1;
3321 outf += y1_2 = b1_2 * y2_2 - y0_2;
3322 outf += y1_3 = b1_3 * y2_3 - y0_3;
3323 ZXP(out) += outf;
3325 LOOP(unit->mRate->mFilterRemain,
3326 outf = y0_0 = b1_0 * y1_0 - y2_0;
3327 outf += y0_1 = b1_1 * y1_1 - y2_1;
3328 outf += y0_2 = b1_2 * y1_2 - y2_2;
3329 outf += y0_3 = b1_3 * y1_3 - y2_3;
3330 y2_0 = y1_0; y1_0 = y0_0;
3331 y2_1 = y1_1; y1_1 = y0_1;
3332 y2_2 = y1_2; y1_2 = y0_2;
3333 y2_3 = y1_3; y1_3 = y0_3;
3334 ZXP(out) += outf;
3336 coefs -= 12;
3337 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3338 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3339 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3340 *++coefs = y1_3; *++coefs = y2_3; ++coefs;
3344 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3348 void Klank_Dtor(Klank *unit)
3350 RTFree(unit->mWorld, unit->m_coefs);
3353 static void Klank_SetCoefs(Klank *unit);
3355 void Klank_Ctor(Klank *unit)
3357 SETCALC(Klank_next);
3358 unit->m_x1 = unit->m_x2 = 0.f;
3359 Klank_SetCoefs(unit);
3360 ZOUT0(0) = 0.f;
3363 void Klank_SetCoefs(Klank *unit)
3365 int numpartials = (unit->mNumInputs - 4) / 3;
3366 unit->m_numpartials = numpartials;
3368 int numcoefs = ((unit->m_numpartials + 3) & ~3) * 5;
3369 unit->m_coefs = (float*)RTAlloc(unit->mWorld, (numcoefs + unit->mWorld->mBufLength) * sizeof(float));
3370 unit->m_buf = unit->m_coefs + numcoefs;
3372 float freqscale = ZIN0(1) * unit->mRate->mRadiansPerSample;
3373 float freqoffset = ZIN0(2) * unit->mRate->mRadiansPerSample;
3374 float decayscale = ZIN0(3);
3376 float* coefs = unit->m_coefs;
3378 float sampleRate = SAMPLERATE;
3380 for (int i=0,j=4; i<numpartials; ++i,j+=3) {
3381 float w = ZIN0(j) * freqscale + freqoffset;
3382 float level = ZIN0(j+1);
3383 float time = ZIN0(j+2) * decayscale;
3385 float R = time == 0.f ? 0.f : exp(log001/(time * sampleRate));
3386 float twoR = 2.f * R;
3387 float R2 = R * R;
3388 float cost = (twoR * cos(w)) / (1.f + R2);
3390 int k = 20 * (i>>2) + (i & 3);
3391 coefs[k+0] = 0.f; // y1
3392 coefs[k+4] = 0.f; // y2
3393 coefs[k+8] = twoR * cost; // b1
3394 coefs[k+12] = -R2; // b2
3395 coefs[k+16] = level * 0.25; // a0
3396 //Print("coefs %d %g %g %g\n", i, twoR * cost, -R2, ampf * 0.25);
3401 void Klank_next(Klank *unit, int inNumSamples)
3403 float *out0 = ZOUT(0);
3404 float *in0 = ZIN(0);
3406 float *in, *out;
3407 float inf;
3408 float y0_0, y1_0, y2_0, a0_0, b1_0, b2_0;
3409 float y0_1, y1_1, y2_1, a0_1, b1_1, b2_1;
3410 float y0_2, y1_2, y2_2, a0_2, b1_2, b2_2;
3411 float y0_3, y1_3, y2_3, a0_3, b1_3, b2_3;
3413 int32 numpartials = unit->m_numpartials;
3414 int32 imax = numpartials >> 2;
3416 float* coefs = unit->m_coefs + imax * 20;
3418 switch (numpartials & 3) {
3419 case 3 :
3420 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3421 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3422 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3424 in = in0;
3425 out = unit->m_buf - 1;
3426 LooP(unit->mRate->mFilterLoops) {
3427 inf = *++in;
3428 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3429 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3430 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3431 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3433 inf = *++in;
3434 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3435 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3436 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3437 *++out = a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2;
3439 inf = *++in;
3440 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3441 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3442 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3443 *++out = a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2;
3445 LooP(unit->mRate->mFilterRemain) {
3446 inf = *++in;
3447 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3448 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3449 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3450 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3451 y2_0 = y1_0; y1_0 = y0_0;
3452 y2_1 = y1_1; y1_1 = y0_1;
3453 y2_2 = y1_2; y1_2 = y0_2;
3455 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3456 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3457 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3458 break;
3459 case 2 :
3460 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3461 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3463 in = in0;
3464 out = unit->m_buf - 1;
3465 LooP(unit->mRate->mFilterLoops) {
3466 inf = *++in;
3467 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3468 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3469 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3471 inf = *++in;
3472 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3473 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3474 *++out = a0_0 * y2_0 + a0_1 * y2_1;
3476 inf = *++in;
3477 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3478 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3479 *++out = a0_0 * y1_0 + a0_1 * y1_1;
3481 LooP(unit->mRate->mFilterRemain) {
3482 inf = *++in;
3483 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3484 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3485 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3486 y2_0 = y1_0; y1_0 = y0_0;
3487 y2_1 = y1_1; y1_1 = y0_1;
3489 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3490 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3491 break;
3492 case 1 :
3493 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3495 //Print("rcoefs %g %g %g %g %g\n", y1_0, y2_0, b1_0, b2_0, a0_0);
3496 in = in0;
3497 out = unit->m_buf - 1;
3498 LooP(unit->mRate->mFilterLoops) {
3499 inf = *++in;
3500 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3501 *++out = a0_0 * y0_0;
3503 inf = *++in;
3504 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3505 *++out = a0_0 * y2_0;
3507 inf = *++in;
3508 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3509 *++out = a0_0 * y1_0;
3510 //Print("out %g %g %g\n", y0_0, y2_0, y1_0);
3512 LooP(unit->mRate->mFilterRemain) {
3513 inf = *++in;
3514 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3515 *++out = a0_0 * y0_0;
3516 y2_0 = y1_0; y1_0 = y0_0;
3517 //Print("out %g\n", y0_0);
3520 coefs[0] = y1_0; coefs[4] = y2_0;
3522 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3523 break;
3524 case 0 :
3525 out = unit->m_buf - 1;
3526 LooP(inNumSamples) { *++out = 0.f; }
3527 break;
3530 coefs = unit->m_coefs;
3532 for (int i=0; i<imax; ++i) {
3533 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3534 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3535 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3536 y1_3 = coefs[3]; y2_3 = coefs[7]; b1_3 = coefs[11]; b2_3 = coefs[15]; a0_3 = coefs[19];
3538 in = in0;
3539 out = unit->m_buf - 1;
3540 LooP(unit->mRate->mFilterLoops) {
3541 inf = *++in;
3542 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3543 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3544 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3545 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3546 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3548 inf = *++in;
3549 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3550 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3551 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3552 y2_3 = inf + b1_3 * y0_3 + b2_3 * y1_3;
3553 *++out += a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2 + a0_3 * y2_3;
3555 inf = *++in;
3556 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3557 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3558 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3559 y1_3 = inf + b1_3 * y2_3 + b2_3 * y0_3;
3560 *++out += a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2 + a0_3 * y1_3;
3562 LooP(unit->mRate->mFilterRemain) {
3563 inf = *++in;
3564 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3565 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3566 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3567 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3568 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3569 y2_0 = y1_0; y1_0 = y0_0;
3570 y2_1 = y1_1; y1_1 = y0_1;
3571 y2_2 = y1_2; y1_2 = y0_2;
3572 y2_3 = y1_3; y1_3 = y0_3;
3574 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3575 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3576 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3577 coefs[3] = zapgremlins(y1_3); coefs[7] = zapgremlins(y2_3);
3578 coefs += 20;
3581 float x0;
3582 float x1 = unit->m_x1;
3583 float x2 = unit->m_x2;
3585 in = unit->m_buf - 1;
3586 out = out0;
3587 LooP(unit->mRate->mFilterLoops) {
3588 x0 = *++in;
3589 *++out = x0 - x2;
3590 x2 = *++in;
3591 *++out = x2 - x1;
3592 x1 = *++in;
3593 *++out = x1 - x0;
3595 LooP(unit->mRate->mFilterRemain) {
3596 x0 = *++in;
3597 *++out = x0 - x2;
3598 x2 = x1;
3599 x1 = x0;
3602 unit->m_x1 = x1;
3603 unit->m_x2 = x2;
3606 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3608 static void normalize_samples(int size, float* data, float peak)
3610 float maxamp = 0.f;
3611 for (int i=0; i<size; ++i) {
3612 float absamp = std::abs(data[i]);
3613 if (absamp > maxamp) maxamp = absamp;
3615 if (maxamp != 0.f && maxamp != peak) {
3616 float ampfac = peak / maxamp;
3617 for (int i=0; i<size; ++i) {
3618 data[i] *= ampfac;
3623 static void normalize_wsamples(int size, float* data, float peak)
3625 float maxamp = 0.f;
3626 for (int i=0; i<size; i+=2) {
3627 float absamp = std::abs(data[i] + data[i+1]);
3628 if (absamp > maxamp) maxamp = absamp;
3630 if (maxamp != 0.f && maxamp != peak) {
3631 float ampfac = peak / maxamp;
3632 for (int i=0; i<size; ++i) {
3633 data[i] *= ampfac;
3638 static void add_partial(int size, float *data, double partial, double amp, double phase)
3640 if (amp == 0.0) return;
3641 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size;
3642 for (int i=0; i<size; ++i) {
3643 data[i] += amp * sin(phase);
3644 phase += w;
3648 static void add_wpartial(int size, float *data, double partial, double amp, double phase)
3650 if (amp == 0.0) return;
3651 int size2 = size >> 1;
3652 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size2;
3653 double cur = amp * sin(phase);
3654 phase += w;
3655 for (int i=0; i<size; i+=2) {
3656 double next = amp * sin(phase);
3657 data[i] += 2 * cur - next;
3658 data[i+1] += next - cur;
3659 cur = next;
3660 phase += w;
3664 static void add_chebyshev(int size, float *data, double partial, double amp)
3666 if (amp == 0.0) return;
3667 double w = 2.0 / (double)size;
3668 double phase = -1.0;
3669 double offset = -amp * cos (partial * pi2);
3670 for (int i=0; i<size; ++i) {
3671 data[i] += amp * cos (partial * acos (phase)) - offset;
3672 phase += w;
3676 static void add_wchebyshev(int size, float *data, double partial, double amp)
3678 if (amp == 0.0) return;
3679 int size2 = size >> 1;
3680 double w = 2.0 / (double)size2;
3681 double phase = -1.0;
3682 double offset = -amp * cos (partial * pi2);
3683 double cur = amp * cos (partial * acos (phase))-offset;
3684 phase += w;
3685 for (int i=0; i<size; i+=2) {
3686 double next = amp * cos (partial * acos (phase)) - offset;
3687 data[i] += 2 * cur - next;
3688 data[i+1] += next - cur;
3689 cur = next;
3690 phase += w;
3694 static void cantorFill(int size, float *data) // long offset, double amp)
3696 // if (amp == 0.0) return;
3697 for (int i=0; i<(size); ++i) {
3698 int j = i;
3699 float flag = 1.f;
3700 while ((j > 0) && (flag == 1.f) ) {
3701 if (j % 3 == 1) { flag = 0.f; }
3702 j = j / 3;
3704 if(flag) { data[i] += 1.f; }
3709 enum {
3710 flag_Normalize = 1,
3711 flag_Wavetable = 2,
3712 flag_Clear = 4
3715 void ChebyFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3717 if (buf->channels != 1) return;
3719 int flags = msg->geti();
3721 int size = buf->samples;
3722 int byteSize = size * sizeof(float);
3723 float *data = (float*)malloc(byteSize);
3725 if (flags & flag_Clear) Fill(size, data, 0.);
3726 else memcpy(data, buf->data, byteSize);
3728 for (int partial=1; msg->remain(); partial++) {
3729 double amp = msg->getf();
3730 if (flags & flag_Wavetable) add_wchebyshev(size, data, partial, amp);
3731 else add_chebyshev(size, data, partial, amp);
3734 if (flags & flag_Normalize) {
3735 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3736 else normalize_samples(size, data, 1.);
3739 memcpy(buf->data, data, byteSize);
3740 free(data);
3744 void SineFill1(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3746 if (buf->channels != 1) return;
3748 int flags = msg->geti();
3750 int size = buf->samples;
3751 int byteSize = size * sizeof(float);
3752 float *data = (float*)malloc(byteSize);
3754 if (flags & flag_Clear) Fill(size, data, 0.);
3755 else memcpy(data, buf->data, byteSize);
3757 for (int partial=1; msg->remain(); partial++) {
3758 double amp = msg->getf();
3759 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3760 else add_partial(size, data, partial, amp, 0.);
3763 if (flags & flag_Normalize) {
3764 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3765 else normalize_samples(size, data, 1.);
3768 memcpy(buf->data, data, byteSize);
3769 free(data);
3772 void SineFill2(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3774 if (buf->channels != 1) return;
3776 int flags = msg->geti();
3778 int size = buf->samples;
3779 int byteSize = size * sizeof(float);
3780 float *data = (float*)malloc(byteSize);
3782 if (flags & flag_Clear) Fill(size, data, 0.);
3783 else memcpy(data, buf->data, byteSize);
3785 while (msg->remain()) {
3786 double partial = msg->getf();
3787 double amp = msg->getf();
3788 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3789 else add_partial(size, data, partial, amp, 0.);
3791 if (flags & flag_Normalize) {
3792 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3793 else normalize_samples(size, data, 1.);
3796 memcpy(buf->data, data, byteSize);
3797 free(data);
3800 void SineFill3(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3802 if (buf->channels != 1) return;
3804 int flags = msg->geti();
3806 int size = buf->samples;
3807 int byteSize = size * sizeof(float);
3808 float *data = (float*)malloc(byteSize);
3810 if (flags & flag_Clear) Fill(size, data, 0.);
3811 else memcpy(data, buf->data, byteSize);
3813 while (msg->remain()) {
3814 double partial = msg->getf();
3815 double amp = msg->getf();
3816 double phase = msg->getf();
3817 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, phase);
3818 else add_partial(size, data, partial, amp, phase);
3820 if (flags & flag_Normalize) {
3821 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3822 else normalize_samples(size, data, 1.);
3825 memcpy(buf->data, data, byteSize);
3826 free(data);
3829 void NormalizeBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3831 float newmax;
3832 if(msg->remain() != 0){
3833 newmax = msg->getf();
3834 }else{
3835 newmax = 1.f;
3837 float *data = buf->data;
3838 int size = buf->samples;
3839 normalize_samples(size, data, newmax);
3842 void NormalizeWaveBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3844 float newmax;
3845 if(msg->remain() != 0){
3846 newmax = msg->getf();
3847 }else{
3848 newmax = 1.f;
3850 float *data = buf->data;
3851 int size = buf->samples;
3852 normalize_wsamples(size, data, newmax);
3855 void CopyBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3857 int frames1 = buf->frames;
3858 int channels1 = buf->channels;
3860 int toPos = msg->geti();
3861 uint32 bufnum2 = msg->geti();
3862 int fromPos = msg->geti();
3863 int length = msg->geti();
3865 if (bufnum2 >= world->mNumSndBufs) bufnum2 = 0;
3866 SndBuf* buf2 = world->mSndBufs + bufnum2;
3867 int frames2 = buf2->frames;
3868 int channels2 = buf2->channels;
3870 if (channels1 != channels2) return;
3872 fromPos = sc_clip(fromPos, 0, frames2-1);
3873 toPos = sc_clip(toPos, 0, frames1-1);
3875 int maxLength = sc_min(frames2 - fromPos, frames1 - toPos);
3876 if (length < 0) {
3877 length = maxLength;
3878 } else {
3879 length = sc_min(length, maxLength);
3882 if (length <= 0) return;
3884 int numbytes = length * sizeof(float) * channels1;
3885 float *data1 = buf->data + toPos * channels1;
3886 float *data2 = buf2->data + fromPos * channels2;
3888 if ((((char*)data1 + numbytes) > (char*)data2) || (((char*)data2 + numbytes) > (char*)data1)) {
3889 memmove(data1, data2, numbytes);
3890 } else {
3891 memcpy(data1, data2, numbytes);
3895 void CantorFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3897 float *data = buf->data;
3898 int size = buf->samples;
3899 // double offset = msg->getf();
3900 // double amp = msg->getf();
3901 // long offs = (long) offset;
3902 cantorFill(size, data);
3907 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3909 PluginLoad(Osc)
3911 ft = inTable;
3913 DefineSimpleUnit(DegreeToKey);
3914 DefineSimpleUnit(Select);
3915 DefineSimpleUnit(TWindex);
3916 DefineSimpleUnit(Index);
3917 DefineSimpleUnit(IndexL);
3918 DefineSimpleUnit(FoldIndex);
3919 DefineSimpleUnit(WrapIndex);
3920 DefineSimpleUnit(IndexInBetween);
3921 DefineSimpleUnit(DetectIndex);
3922 DefineSimpleUnit(Shaper);
3923 DefineSimpleUnit(SigOsc);
3924 DefineSimpleUnit(FSinOsc);
3925 DefineSimpleUnit(SinOsc);
3926 DefineSimpleUnit(SinOscFB);
3927 DefineSimpleUnit(VOsc);
3928 DefineSimpleUnit(VOsc3);
3929 DefineSimpleUnit(Osc);
3930 DefineSimpleUnit(OscN);
3931 DefineSimpleUnit(COsc);
3932 DefineSimpleUnit(Formant);
3933 DefineSimpleUnit(Blip);
3934 DefineSimpleUnit(Saw);
3935 DefineSimpleUnit(Pulse);
3936 DefineDtorUnit(Klang);
3937 DefineDtorUnit(Klank);
3939 DefineBufGen("cheby", ChebyFill);
3940 DefineBufGen("sine1", SineFill1);
3941 DefineBufGen("sine2", SineFill2);
3942 DefineBufGen("sine3", SineFill3);
3943 DefineBufGen("normalize", NormalizeBuf);
3944 DefineBufGen("wnormalize", NormalizeWaveBuf);
3945 DefineBufGen("copy", CopyBuf);
3946 DefineBufGen("cantorFill", CantorFill);
3949 //////////////////////////////////////////////////////////////////////////////////////////////////