FFT: Prevent user from attempting hops smaller than SC's block size
[supercollider.git] / server / plugins / OscUGens.cpp
blobde76f678fcbdeecff63074f80b3238c40825c33e
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "SC_PlugIn.h"
22 #include "SC_Altivec.h"
23 #include <limits>
24 #include <string.h>
26 static InterfaceTable *ft;
28 struct BufUnit : public Unit
30 SndBuf *m_buf;
31 float m_fbufnum;
34 struct TableLookup : public BufUnit
36 double m_cpstoinc, m_radtoinc;
37 int32 mTableSize;
38 int32 m_lomask;
41 struct DegreeToKey : public BufUnit
43 SndBuf *m_buf;
44 float m_fbufnum;
45 int32 mPrevIndex;
46 float mPrevKey;
47 int32 mOctave;
50 struct Select : public Unit
54 struct TWindex : public Unit
56 int32 m_prevIndex;
57 float m_trig;
60 struct Index : public BufUnit
64 struct IndexL : public BufUnit
68 struct WrapIndex : public BufUnit
72 struct FoldIndex : public BufUnit
76 struct IndexInBetween : public BufUnit
80 struct DetectIndex : public BufUnit
82 float mPrev;
83 float mPrevIn;
86 struct Shaper : public BufUnit
88 float mOffset;
89 float mPrevIn;
92 struct SigOsc : public BufUnit
94 int32 mTableSize;
95 double m_cpstoinc;
96 float mPhase;
99 struct FSinOsc : public Unit
101 double m_b1, m_y1, m_y2, m_freq;
104 struct PSinGrain : public Unit
106 double m_b1, m_y1, m_y2;
107 double m_level, m_slope, m_curve;
108 int32 mCounter;
111 struct Osc : public TableLookup
113 int32 m_phase;
114 float m_phasein;
117 struct SinOsc : public TableLookup
119 int32 m_phase;
120 float m_phasein;
123 struct SinOscFB : public TableLookup
125 int32 m_phase;
126 float m_prevout, m_feedback;
129 struct OscN : public TableLookup
131 int32 m_phase;
132 float m_phasein;
135 struct COsc : public TableLookup
137 int32 m_phase1, m_phase2;
140 struct VOsc : public Unit
142 double m_cpstoinc, m_radtoinc;
143 int32 mTableSize;
144 int32 m_lomask;
145 int32 m_phase, m_phaseoffset;
146 float m_phasein, m_bufpos;
149 struct VOsc3 : public Unit
151 double m_cpstoinc;
152 int32 mTableSize;
153 int32 m_lomask;
154 int32 m_phase1, m_phase2, m_phase3;
155 float m_bufpos;
158 struct Formant : public Unit
160 int32 m_phase1, m_phase2, m_phase3;
161 double m_cpstoinc;
166 struct Blip : public Unit
168 int32 m_phase, m_numharm, m_N;
169 float m_freqin, m_scale;
170 double m_cpstoinc;
173 struct Saw : public Unit
175 int32 m_phase, m_N;
176 float m_freqin, m_scale, m_y1;
177 double m_cpstoinc;
180 struct Pulse : public Unit
182 int32 m_phase, m_phaseoff, m_N;
183 float m_freqin, m_scale, m_y1;
184 double m_cpstoinc;
187 struct Klang : public Unit
189 float *m_coefs;
190 int32 m_numpartials;
193 struct Klank : public Unit
195 float *m_coefs;
196 float *m_buf;
197 float m_x1, m_x2;
198 int32 m_numpartials;
203 #define xlomask8 0x000003FC
204 #define xlomask9 0x000007FC
205 #define xlomask10 0x00000FFC
206 #define xlomask11 0x00001FFC
207 #define xlomask12 0x00003FFC
208 #define xlomask13 0x00007FFC
210 #define xlomask8i 0x000007F8
211 #define xlomask9i 0x00000FF8
212 #define xlomask10i 0x00001FF8
213 #define xlomask11i 0x00003FF8
214 #define xlomask12i 0x00007FF8
215 #define xlomask13i 0x0000FFF8
217 #define onecyc13 0x20000000
221 //////////////////////////////////////////////////////////////////////////////////////////////////
223 extern "C"
226 void DegreeToKey_Ctor(DegreeToKey *unit);
227 void DegreeToKey_next_1(DegreeToKey *unit, int inNumSamples);
228 void DegreeToKey_next_k(DegreeToKey *unit, int inNumSamples);
229 void DegreeToKey_next_a(DegreeToKey *unit, int inNumSamples);
231 void Select_Ctor(Select *unit);
232 void Select_next_1(Select *unit, int inNumSamples);
233 void Select_next_k(Select *unit, int inNumSamples);
234 void Select_next_a(Select *unit, int inNumSamples);
236 void TWindex_Ctor(TWindex *unit);
237 void TWindex_next_k(TWindex *unit, int inNumSamples);
238 void TWindex_next_ak(TWindex *unit, int inNumSamples);
240 void Index_Ctor(Index *unit);
241 void Index_next_1(Index *unit, int inNumSamples);
242 void Index_next_k(Index *unit, int inNumSamples);
243 void Index_next_a(Index *unit, int inNumSamples);
245 void IndexL_Ctor(IndexL *unit);
246 void IndexL_next_1(IndexL *unit, int inNumSamples);
247 void IndexL_next_k(IndexL *unit, int inNumSamples);
248 void IndexL_next_a(IndexL *unit, int inNumSamples);
250 void FoldIndex_Ctor(FoldIndex *unit);
251 void FoldIndex_next_1(FoldIndex *unit, int inNumSamples);
252 void FoldIndex_next_k(FoldIndex *unit, int inNumSamples);
253 void FoldIndex_next_a(FoldIndex *unit, int inNumSamples);
255 void WrapIndex_Ctor(WrapIndex *unit);
256 void WrapIndex_next_1(WrapIndex *unit, int inNumSamples);
257 void WrapIndex_next_k(WrapIndex *unit, int inNumSamples);
258 void WrapIndex_next_a(WrapIndex *unit, int inNumSamples);
260 void Shaper_Ctor(Shaper *unit);
261 void Shaper_next_1(Shaper *unit, int inNumSamples);
262 void Shaper_next_k(Shaper *unit, int inNumSamples);
263 void Shaper_next_a(Shaper *unit, int inNumSamples);
266 void DetectIndex_Ctor(DetectIndex *unit);
267 void DetectIndex_next_1(DetectIndex *unit, int inNumSamples);
268 void DetectIndex_next_k(DetectIndex *unit, int inNumSamples);
269 void DetectIndex_next_a(DetectIndex *unit, int inNumSamples);
271 void IndexInBetween_Ctor(IndexInBetween *unit);
272 void IndexInBetween_next_1(IndexInBetween *unit, int inNumSamples);
273 void IndexInBetween_next_k(IndexInBetween *unit, int inNumSamples);
274 void IndexInBetween_next_a(IndexInBetween *unit, int inNumSamples);
277 void SigOsc_Ctor(SigOsc *unit);
278 void SigOsc_next_1(SigOsc *unit, int inNumSamples);
279 void SigOsc_next_k(SigOsc *unit, int inNumSamples);
280 void SigOsc_next_a(SigOsc *unit, int inNumSamples);
282 void FSinOsc_Ctor(FSinOsc *unit);
283 void FSinOsc_next(FSinOsc *unit, int inNumSamples);
285 void PSinGrain_Ctor(PSinGrain *unit);
286 void PSinGrain_next(PSinGrain *unit, int inNumSamples);
288 void SinOsc_Ctor(SinOsc *unit);
289 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples);
290 void SinOsc_next_ika(SinOsc *unit, int inNumSamples);
291 void SinOsc_next_iak(SinOsc *unit, int inNumSamples);
292 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples);
294 void Osc_Ctor(Osc *unit);
295 void Osc_next_ikk(Osc *unit, int inNumSamples);
296 void Osc_next_ika(Osc *unit, int inNumSamples);
297 void Osc_next_iak(Osc *unit, int inNumSamples);
298 void Osc_next_iaa(Osc *unit, int inNumSamples);
300 void OscN_Ctor(OscN *unit);
301 void OscN_next_nkk(OscN *unit, int inNumSamples);
302 void OscN_next_nka(OscN *unit, int inNumSamples);
303 void OscN_next_nak(OscN *unit, int inNumSamples);
304 void OscN_next_naa(OscN *unit, int inNumSamples);
306 void COsc_Ctor(COsc *unit);
307 void COsc_next(COsc *unit, int inNumSamples);
309 void VOsc_Ctor(VOsc *unit);
310 void VOsc_next_ik(VOsc *unit, int inNumSamples);
312 void VOsc3_Ctor(VOsc3 *unit);
313 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples);
315 void Formant_Ctor(Formant *unit);
316 void Formant_next(Formant *unit, int inNumSamples);
318 void Blip_Ctor(Blip *unit);
319 void Blip_next(Blip *unit, int inNumSamples);
321 void Saw_Ctor(Saw *unit);
322 void Saw_next(Saw *unit, int inNumSamples);
324 void Pulse_Ctor(Pulse *unit);
325 void Pulse_next(Pulse *unit, int inNumSamples);
327 void Klang_Dtor(Klang *unit);
328 void Klang_Ctor(Klang *unit);
329 void Klang_next(Klang *unit, int inNumSamples);
331 void Klank_Dtor(Klank *unit);
332 void Klank_Ctor(Klank *unit);
333 void Klank_next(Klank *unit, int inNumSamples);
337 //////////////////////////////////////////////////////////////////////////////////////////////////
339 #define GET_TABLE \
340 float fbufnum = ZIN0(0); \
341 if (fbufnum != unit->m_fbufnum) { \
342 uint32 bufnum = (uint32)fbufnum; \
343 World *world = unit->mWorld; \
344 if (bufnum >= world->mNumSndBufs) { \
345 int localBufNum = bufnum - world->mNumSndBufs; \
346 Graph *parent = unit->mParent; \
347 if(localBufNum <= parent->localBufNum) { \
348 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
349 } else { \
350 bufnum = 0; \
351 unit->m_buf = world->mSndBufs + bufnum; \
353 } else { \
354 unit->m_buf = world->mSndBufs + bufnum; \
356 unit->m_fbufnum = fbufnum; \
358 const SndBuf *buf = unit->m_buf; \
359 if(!buf) { \
360 ClearUnitOutputs(unit, inNumSamples); \
361 return; \
363 LOCK_SNDBUF_SHARED(buf); \
364 const float *bufData __attribute__((__unused__)) = buf->data; \
365 if (!bufData) { \
366 ClearUnitOutputs(unit, inNumSamples); \
367 return; \
369 int tableSize = buf->samples;
372 ////////////////////////////////////////////////////////////////////////////////////////////////////////
375 void TableLookup_SetTable(TableLookup* unit, int32 inSize, float* inTable)
377 unit->mTable0 = inTable;
378 unit->mTable1 = inTable + 1;
379 unit->mTableSize = inSize;
380 unit->mMaxIndex = unit->mTableSize - 1;
381 unit->mFMaxIndex = unit->mMaxIndex;
382 unit->m_radtoinc = unit->mTableSize * (rtwopi * 65536.);
383 unit->m_cpstoinc = unit->mTableSize * SAMPLEDUR * 65536.;
384 //Print("TableLookup_SetTable unit->m_radtoinc %g %d %g\n", m_radtoinc, unit->mTableSize, rtwopi);
388 ////////////////////////////////////////////////////////////////////////////////////////////////////////
390 void DegreeToKey_Ctor(DegreeToKey *unit)
392 unit->m_fbufnum = -1e9f;
393 if (BUFLENGTH == 1) {
394 SETCALC(DegreeToKey_next_1);
395 } else if (INRATE(0) == calc_FullRate) {
396 SETCALC(DegreeToKey_next_a);
397 } else {
398 SETCALC(DegreeToKey_next_k);
400 unit->mOctave = (int32)ZIN0(2);
401 unit->mPrevIndex = std::numeric_limits<int32>::max();
402 unit->mPrevKey = 0.;
403 DegreeToKey_next_1(unit, 1);
406 void DegreeToKey_next_1(DegreeToKey *unit, int inNumSamples)
408 // get table
409 GET_TABLE
410 const float *table = bufData;
411 int32 maxindex = tableSize - 1;
413 int32 key, oct;
415 int32 octave = unit->mOctave;
416 float val;
418 int32 index = (int32)floor(ZIN0(1));
419 if (index == unit->mPrevIndex) {
420 val = unit->mPrevKey;
421 } else if (index < 0) {
422 unit->mPrevIndex = index;
423 key = tableSize + index % tableSize;
424 oct = (index + 1) / tableSize - 1;
425 val = unit->mPrevKey = table[key] + octave * oct;
426 } else if (index > maxindex) {
427 unit->mPrevIndex = index;
428 key = index % tableSize;
429 oct = index / tableSize;
430 val = unit->mPrevKey = table[key] + octave * oct;
431 } else {
432 unit->mPrevIndex = index;
433 val = unit->mPrevKey = table[index];
435 ZOUT0(0) = val;
438 void DegreeToKey_next_k(DegreeToKey *unit, int inNumSamples)
440 // get table
441 GET_TABLE
442 const float *table = bufData;
443 int32 maxindex = tableSize - 1;
445 float *out = ZOUT(0);
447 int32 key, oct;
448 float octave = unit->mOctave;
449 float val;
451 int32 index = (int32)floor(ZIN0(1));
452 if (index == unit->mPrevIndex) {
453 val = unit->mPrevKey;
454 } else if (index < 0) {
455 unit->mPrevIndex = index;
456 key = tableSize + index % tableSize;
457 oct = (index + 1) / tableSize - 1;
458 val = unit->mPrevKey = table[key] + octave * oct;
459 } else if (index > maxindex) {
460 unit->mPrevIndex = index;
461 key = index % tableSize;
462 oct = index / tableSize;
463 val = unit->mPrevKey = table[key] + octave * oct;
464 } else {
465 unit->mPrevIndex = index;
466 val = unit->mPrevKey = table[index];
468 LOOP1(inNumSamples,
469 ZXP(out) = val;
474 void DegreeToKey_next_a(DegreeToKey *unit, int inNumSamples)
476 // get table
477 GET_TABLE
478 const float *table = bufData;
479 int32 maxindex = tableSize - 1;
481 float *out = ZOUT(0);
482 float *in = ZIN(1);
483 int32 previndex = unit->mPrevIndex;
484 float prevkey = unit->mPrevKey;
485 int32 key, oct;
487 float octave = unit->mOctave;
489 LOOP1(inNumSamples,
490 int32 index = (int32)floor(ZXP(in));
491 if (index == previndex) {
492 ZXP(out) = prevkey;
493 } else if (index < 0) {
494 previndex = index;
495 key = tableSize + index % tableSize;
496 oct = (index + 1) / tableSize - 1;
497 ZXP(out) = prevkey = table[key] + octave * oct;
498 } else if (index > maxindex) {
499 previndex = index;
500 key = index % tableSize;
501 oct = index / tableSize;
502 ZXP(out) = prevkey = table[key] + octave * oct;
503 } else {
504 previndex = index;
505 ZXP(out) = prevkey = table[index];
508 unit->mPrevIndex = previndex;
509 unit->mPrevKey = prevkey;
512 ////////////////////////////////////////////////////////////////////////////////////
514 void Select_Ctor(Select *unit)
516 if (BUFLENGTH == 1) {
517 SETCALC(Select_next_1);
518 } else if (INRATE(0) == calc_FullRate) {
519 SETCALC(Select_next_a);
520 } else {
521 SETCALC(Select_next_k);
523 Select_next_1(unit, 1);
526 void Select_next_1(Select *unit, int inNumSamples)
528 int32 maxindex = unit->mNumInputs - 1;
529 int32 index = (int32)ZIN0(0) + 1;
530 index = sc_clip(index, 1, maxindex);
531 ZOUT0(0) = ZIN0(index);
534 void Select_next_k(Select *unit, int inNumSamples)
536 int32 maxindex = unit->mNumInputs - 1;
537 int32 index = (int32)ZIN0(0) + 1;
538 index = sc_clip(index, 1, maxindex);
540 float *out = OUT(0);
541 float *in = IN(index);
543 Copy(inNumSamples, out, in);
546 void Select_next_a(Select *unit, int inNumSamples)
548 int32 maxindex = unit->mNumInputs - 1;
550 float *out = ZOUT(0);
551 float *in0 = ZIN(0);
552 float **in = unit->mInBuf;
554 for (int i=0; i<inNumSamples; ++i) {
555 int32 index = (int32)ZXP(in0) + 1;
556 index = sc_clip(index, 1, maxindex);
557 ZXP(out) = in[index][i];
560 ////////////////////////////////////////////////////////////////////////////////////
562 void TWindex_Ctor(TWindex *unit)
564 if (INRATE(0) == calc_FullRate) {
565 SETCALC(TWindex_next_ak); // todo : ar
566 } else {
567 SETCALC(TWindex_next_k);
569 unit->m_prevIndex = 0;
570 unit->m_trig = -1.f; // make it trigger the first time
571 TWindex_next_k(unit, 1);
575 void TWindex_next_k(TWindex *unit, int inNumSamples)
578 int maxindex = unit->mNumInputs;
579 int32 index = maxindex;
580 float sum = 0.f;
581 float maxSum = 0.f;
582 float normalize = ZIN0(1); // switch normalisation on or off
583 float trig = ZIN0(0);
584 float *out = ZOUT(0);
585 if (trig > 0.f && unit->m_trig <= 0.f) {
586 if(normalize == 1) {
587 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
588 } else {
589 maxSum = 1.f;
591 RGen& rgen = *unit->mParent->mRGen;
592 float max = maxSum * rgen.frand();
594 for (int32 k=2; k<maxindex; ++k) {
595 sum += ZIN0(k);
596 if(sum >= max) {
597 index = k - 2;
598 break;
602 unit->m_prevIndex = index;
603 } else {
604 index = unit->m_prevIndex;
607 LOOP1(inNumSamples,
608 ZXP(out) = index;
610 unit->m_trig = trig;
613 void TWindex_next_ak(TWindex *unit, int inNumSamples)
615 int maxindex = unit->mNumInputs;
616 int32 index = maxindex;
618 float sum = 0.f;
619 float maxSum = 0.f;
620 float normalize = ZIN0(1);//switch normalisation on or off
621 float *trig = ZIN(0);
622 float *out = ZOUT(0);
623 float curtrig;
624 if(normalize == 1) {
625 for (int32 k=2; k<maxindex; ++k) { maxSum += ZIN0(k); }
626 } else
627 maxSum = 1.f;
628 RGen& rgen = *unit->mParent->mRGen;
630 LOOP1(inNumSamples,
631 curtrig = ZXP(trig);
632 if (curtrig > 0.f && unit->m_trig <= 0.f) {
633 float max = maxSum * rgen.frand();
634 for (int32 k=2; k<maxindex; ++k) {
635 sum += ZIN0(k);
636 if(sum >= max) {
637 index = k - 2;
638 break;
642 unit->m_prevIndex = index;
643 } else
644 index = unit->m_prevIndex;
646 ZXP(out) = index;
647 unit->m_trig = curtrig;
651 ////////////////////////////////////////////////////////////////////////////////////
653 void Index_Ctor(Index *unit)
655 unit->m_fbufnum = -1e9f;
656 if (BUFLENGTH == 1) {
657 SETCALC(Index_next_1);
658 } else if (INRATE(0) == calc_FullRate) {
659 SETCALC(Index_next_a);
660 } else {
661 SETCALC(Index_next_k);
663 Index_next_1(unit, 1);
666 void Index_next_1(Index *unit, int inNumSamples)
668 // get table
669 GET_TABLE
670 const float *table = bufData;
671 int32 maxindex = tableSize - 1;
673 int32 index = (int32)ZIN0(1);
674 index = sc_clip(index, 0, maxindex);
675 ZOUT0(0) = table[index];
678 void Index_next_k(Index *unit, int inNumSamples)
680 // get table
681 GET_TABLE
682 const float *table = bufData;
683 int32 maxindex = tableSize - 1;
685 float *out = ZOUT(0);
687 int32 index = (int32)ZIN0(1);
689 index = sc_clip(index, 0, maxindex);
690 float val = table[index];
691 LOOP1(inNumSamples,
692 ZXP(out) = val;
697 void Index_next_a(Index *unit, int inNumSamples)
699 // get table
700 GET_TABLE
701 const float *table = bufData;
702 int32 maxindex = tableSize - 1;
704 float *out = ZOUT(0);
705 float *in = ZIN(1);
707 LOOP1(inNumSamples,
708 int32 index = (int32)ZXP(in);
709 index = sc_clip(index, 0, maxindex);
710 ZXP(out) = table[index];
715 ////////////////////////////////////////////////////////////////////////////////////
717 void IndexL_Ctor(IndexL *unit)
719 unit->m_fbufnum = -1e9f;
720 if (BUFLENGTH == 1) {
721 SETCALC(IndexL_next_1);
722 } else if (INRATE(0) == calc_FullRate) {
723 SETCALC(IndexL_next_a);
724 } else {
725 SETCALC(IndexL_next_k);
727 IndexL_next_1(unit, 1);
730 void IndexL_next_1(IndexL *unit, int inNumSamples)
732 // get table
733 GET_TABLE
734 const float *table = bufData;
735 int32 maxindex = tableSize - 1;
737 float findex = ZIN0(1);
738 float frac = sc_frac(findex);
740 int32 index = (int32)findex;
741 index = sc_clip(index, 0, maxindex);
743 float a = table[index];
744 float b = table[sc_clip(index + 1, 0, maxindex)];
745 ZOUT0(0) = lininterp(frac, a, b);
748 void IndexL_next_k(IndexL *unit, int inNumSamples)
750 // get table
751 GET_TABLE
752 const float *table = bufData;
753 int32 maxindex = tableSize - 1;
755 float *out = ZOUT(0);
757 float findex = ZIN0(1);
758 float frac = sc_frac(findex);
760 int32 index = (int32)findex;
761 index = sc_clip(index, 0, maxindex);
764 float a = table[index];
765 float b = table[sc_clip(index + 1, 0, maxindex)];
766 float val = lininterp(frac, a, b);
768 LOOP1(inNumSamples,
769 ZXP(out) = val;
774 void IndexL_next_a(IndexL *unit, int inNumSamples)
776 // get table
777 GET_TABLE
778 const float *table = bufData;
779 int32 maxindex = tableSize - 1;
781 float *out = ZOUT(0);
782 float *in = ZIN(1);
784 LOOP1(inNumSamples,
785 float findex = ZXP(in);
786 float frac = sc_frac(findex);
787 int32 i1 = sc_clip((int32)findex, 0, maxindex);
788 int32 i2 = sc_clip(i1 + 1, 0, maxindex);
789 float a = table[i1];
790 float b = table[i2];
791 ZXP(out) = lininterp(frac, a, b);
797 ////////////////////////////////////////////////////////////////////////////////////
800 void FoldIndex_Ctor(FoldIndex *unit)
802 unit->m_fbufnum = -1e9f;
803 if (BUFLENGTH == 1) {
804 SETCALC(FoldIndex_next_1);
805 } else if (INRATE(0) == calc_FullRate) {
806 SETCALC(FoldIndex_next_a);
807 } else {
808 SETCALC(FoldIndex_next_k);
810 FoldIndex_next_1(unit, 1);
813 void FoldIndex_next_1(FoldIndex *unit, int inNumSamples)
815 // get table
816 GET_TABLE
817 const float *table = bufData;
818 int32 maxindex = tableSize - 1;
820 int32 index = (int32)ZIN0(1);
821 index = sc_fold(index, 0, maxindex);
822 ZOUT0(0) = table[index];
825 void FoldIndex_next_k(FoldIndex *unit, int inNumSamples)
827 // get table
828 GET_TABLE
829 const float *table = bufData;
830 int32 maxindex = tableSize - 1;
832 int32 index = (int32)ZIN0(1);
833 float *out = ZOUT(0);
835 index = sc_fold(index, 0, maxindex);
836 float val = table[index];
837 LOOP1(inNumSamples,
838 ZXP(out) = val;
843 void FoldIndex_next_a(FoldIndex *unit, int inNumSamples)
845 // get table
846 GET_TABLE
847 const float *table = bufData;
848 int32 maxindex = tableSize - 1;
850 float *out = ZOUT(0);
851 float *in = ZIN(1);
853 LOOP1(inNumSamples,
854 int32 index = (int32)ZXP(in);
855 index = sc_fold(index, 0, maxindex);
856 ZXP(out) = table[index];
861 ////////////////////////////////////////////////////////////////////////////////////
863 void WrapIndex_Ctor(WrapIndex *unit)
865 unit->m_fbufnum = -1e9f;
866 if (BUFLENGTH == 1) {
867 SETCALC(WrapIndex_next_1);
868 } else if (INRATE(0) == calc_FullRate) {
869 SETCALC(WrapIndex_next_a);
870 } else {
871 SETCALC(WrapIndex_next_k);
873 WrapIndex_next_1(unit, 1);
876 void WrapIndex_next_1(WrapIndex *unit, int inNumSamples)
878 // get table
879 GET_TABLE
880 const float *table = bufData;
881 int32 maxindex = tableSize - 1;
883 int32 index = (int32)floor(ZIN0(1));
884 index = sc_wrap(index, 0, maxindex);
885 ZOUT0(0) = table[index];
888 void WrapIndex_next_k(WrapIndex *unit, int inNumSamples)
890 // get table
891 GET_TABLE
892 const float *table = bufData;
893 int32 maxindex = tableSize - 1;
895 float *out = ZOUT(0);
897 int32 index = (int32)ZIN0(1);
898 index = sc_wrap(index, 0, maxindex);
899 float val = table[index];
900 LOOP1(inNumSamples,
901 ZXP(out) = val;
906 void WrapIndex_next_a(WrapIndex *unit, int inNumSamples)
908 // get table
909 GET_TABLE
910 const float *table = bufData;
911 int32 maxindex = tableSize - 1;
913 float *out = ZOUT(0);
914 float *in = ZIN(1);
916 LOOP1(inNumSamples,
917 int32 index = (int32)ZXP(in);
918 index = sc_wrap(index, 0, maxindex);
919 ZXP(out) = table[index];
923 ////////////////////////////////////////////////////////////////////////////////////
925 static float IndexInBetween_FindIndex(const float* table, float in, int32 maxindex)
927 for(int32 i = 0; i <= maxindex; i++) {
928 if(table[i] > in) {
929 if(i == 0) {
930 return 0.f;
931 } else {
932 return ((in - table[i - 1]) / (table[i] - table[i - 1]) + i - 1);
936 return (float)maxindex;
939 void IndexInBetween_Ctor(IndexInBetween *unit)
941 unit->m_fbufnum = -1e9f;
942 if (BUFLENGTH == 1) {
943 SETCALC(IndexInBetween_next_1);
944 } else if (INRATE(0) == calc_FullRate) {
945 SETCALC(IndexInBetween_next_a);
946 } else {
947 SETCALC(IndexInBetween_next_k);
949 IndexInBetween_next_1(unit, 1);
952 void IndexInBetween_next_1(IndexInBetween *unit, int inNumSamples)
954 // get table
955 GET_TABLE
956 const float *table = bufData;
957 int32 maxindex = tableSize - 1;
959 float in = ZIN0(1);
960 ZOUT0(0) = IndexInBetween_FindIndex(table, in, maxindex);
963 void IndexInBetween_next_k(IndexInBetween *unit, int inNumSamples)
965 // get table
966 GET_TABLE
967 const float *table = bufData;
968 int32 maxindex = tableSize - 1;
970 float *out = ZOUT(0);
971 float in = ZIN0(1);
973 float val = IndexInBetween_FindIndex(table, in, maxindex);
974 LOOP1(inNumSamples,
975 ZXP(out) = val;
980 void IndexInBetween_next_a(IndexInBetween *unit, int inNumSamples)
982 // get table
983 GET_TABLE
984 const float *table = bufData;
985 int32 maxindex = tableSize - 1;
987 float *out = ZOUT(0);
988 float *in = ZIN(1);
990 LOOP1(inNumSamples,
991 ZXP(out) = IndexInBetween_FindIndex(table, ZXP(in), maxindex);
996 ////////////////////////////////////////////////////////////////////////////////////
998 static int32 DetectIndex_FindIndex(const float* table, float in, int32 maxindex)
1000 int32 index;
1001 for(index = 0; index <= maxindex; index+=1) {
1002 if(table[index] == in) {
1003 return index;
1006 return -1;
1009 void DetectIndex_Ctor(DetectIndex *unit)
1011 unit->m_fbufnum = -1e9f;
1012 if (BUFLENGTH == 1) {
1013 SETCALC(DetectIndex_next_1);
1014 } else if (INRATE(0) == calc_FullRate) {
1015 SETCALC(DetectIndex_next_a);
1016 } else {
1017 SETCALC(DetectIndex_next_k);
1019 unit->mPrev = -1.f;
1020 DetectIndex_next_1(unit, 1);
1023 void DetectIndex_next_1(DetectIndex *unit, int inNumSamples)
1025 // get table
1026 GET_TABLE
1027 const float *table = bufData;
1028 int32 maxindex = tableSize - 1;
1030 float in = ZIN0(1);
1031 int32 index;
1032 if(in == unit->mPrevIn) {
1033 index = (int32)unit->mPrev;
1034 } else {
1035 index = DetectIndex_FindIndex(table, in, maxindex);
1036 unit->mPrev = index;
1037 unit->mPrevIn = in;
1039 ZOUT0(0) = (float)index;
1042 void DetectIndex_next_k(DetectIndex *unit, int inNumSamples)
1044 // get table
1045 GET_TABLE
1046 const float *table = bufData;
1047 int32 maxindex = tableSize - 1;
1049 float *out = ZOUT(0);
1050 float in = ZIN0(1);
1051 int32 index;
1052 float val;
1053 if(in == unit->mPrevIn) {
1054 index = (int32)unit->mPrev;
1055 } else {
1056 index = DetectIndex_FindIndex(table, in, maxindex);
1057 unit->mPrev = index;
1058 unit->mPrevIn = in;
1060 val = (float)index;
1061 LOOP1(inNumSamples,
1062 ZXP(out) = val;
1067 void DetectIndex_next_a(DetectIndex *unit, int inNumSamples)
1069 // get table
1070 GET_TABLE
1071 const float *table = bufData;
1072 int32 maxindex = tableSize - 1;
1074 float *out = ZOUT(0);
1075 float *in = ZIN(1);
1076 float prev = unit->mPrevIn;
1077 int32 prevIndex = (int32)unit->mPrev;
1078 float inval;
1080 LOOP1(inNumSamples,
1081 inval = ZXP(in);
1082 if(inval != prev) {
1083 prevIndex = DetectIndex_FindIndex(table, inval, maxindex);
1085 prev = inval;
1086 ZXP(out) = (float)prevIndex;
1089 unit->mPrev = prevIndex;
1090 unit->mPrevIn = inval;
1094 ////////////////////////////////////////////////////////////////////////////////////
1096 void Shaper_Ctor(Shaper *unit)
1098 unit->m_fbufnum = -1e9f;
1099 if (BUFLENGTH == 1) {
1100 SETCALC(Shaper_next_1);
1101 } else if (INRATE(1) == calc_FullRate) {
1102 SETCALC(Shaper_next_a);
1103 } else {
1104 SETCALC(Shaper_next_k);
1106 unit->mPrevIn = ZIN0(0);
1107 Shaper_next_1(unit, 1);
1110 void Shaper_next_1(Shaper *unit, int inNumSamples)
1112 // get table
1113 GET_TABLE
1114 const float *table0 = bufData;
1115 const float *table1 = table0 + 1;
1116 float fmaxindex = (float)(tableSize>>1) - 0.001;
1117 float offset = tableSize * 0.25;
1119 float val;
1121 float fin = ZIN0(1);
1122 float findex = offset + fin * offset;
1123 findex = sc_clip(findex, 0.f, fmaxindex);
1124 int32 index = (int32)findex;
1125 float pfrac = findex - (index - 1);
1126 index <<= 3;
1127 float val1 = *(float*)((char*)table0 + index);
1128 float val2 = *(float*)((char*)table1 + index);
1129 val = val1 + val2 * pfrac;
1130 ZOUT0(0) = val;
1133 void Shaper_next_k(Shaper *unit, int inNumSamples)
1135 // get table
1136 GET_TABLE
1137 const float *table0 = bufData;
1138 const float *table1 = table0 + 1;
1139 float fmaxindex = (float)(tableSize>>1) - 0.001;
1140 float offset = tableSize * 0.25;
1142 float *out = ZOUT(0);
1144 float val;
1146 float fin = ZIN0(1);
1147 float phaseinc = (fin - unit->mPrevIn) * offset;
1148 unit->mPrevIn = fin;
1150 LOOP1(inNumSamples,
1151 float findex = offset + fin * offset;
1152 findex = sc_clip(findex, 0.f, fmaxindex);
1153 int32 index = (int32)findex;
1154 float pfrac = findex - (index - 1);
1155 index <<= 3;
1156 float val1 = *(float*)((char*)table0 + index);
1157 float val2 = *(float*)((char*)table1 + index);
1158 val = val1 + val2 * pfrac;
1159 ZXP(out) = val;
1160 fin += phaseinc;
1164 void Shaper_next_a(Shaper *unit, int inNumSamples)
1166 // get table
1167 GET_TABLE
1168 const float *table0 = bufData;
1169 const float *table1 = table0 + 1;
1170 float fmaxindex = (float)(tableSize>>1) - 0.001;
1171 float offset = tableSize * 0.25;
1173 float *out = ZOUT(0);
1174 float *in = ZIN(1);
1175 float val;
1177 LOOP1(inNumSamples,
1178 float fin = ZXP(in);
1179 float findex = offset + fin * offset;
1180 findex = sc_clip(findex, 0.f, fmaxindex);
1181 int32 index = (int32)findex;
1182 float pfrac = findex - (index - 1);
1183 index <<= 3;
1184 float val1 = *(float*)((char*)table0 + index);
1185 float val2 = *(float*)((char*)table1 + index);
1186 val = val1 + val2 * pfrac;
1187 ZXP(out) = val;
1193 ////////////////////////////////////////////////////////////////////////////////////
1195 void SigOsc_Ctor(SigOsc *unit)
1197 unit->m_fbufnum = -1e9f;
1198 if (BUFLENGTH == 1) {
1199 SETCALC(SigOsc_next_1);
1200 } else if (INRATE(0) == calc_FullRate) {
1201 SETCALC(SigOsc_next_a);
1202 } else {
1203 SETCALC(SigOsc_next_k);
1205 unit->mPhase = 0.f;
1206 SigOsc_next_1(unit, 1);
1210 void SigOsc_next_1(SigOsc *unit, int inNumSamples)
1212 // get table
1213 GET_TABLE
1214 const float *table0 = bufData;
1215 const float *table1 = table0 + 1;
1216 int32 maxindex = tableSize - 1;
1217 float maxphase = (float)maxindex;
1218 if (tableSize != unit->mTableSize) {
1219 unit->mTableSize = tableSize;
1220 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1223 float phase = unit->mPhase;
1225 while (phase < 0.f) phase += maxphase;
1226 while (phase >= maxphase) phase -= maxphase;
1227 int32 iphase = (int32)phase;
1228 float pfrac = phase - iphase;
1229 float val1 = *(float*)((char*)table0 + iphase);
1230 float val2 = *(float*)((char*)table1 + iphase);
1231 float val = lininterp(pfrac, val1, val2);
1232 phase += ZIN0(1) * unit->m_cpstoinc;
1234 ZOUT0(0) = val;
1236 unit->mPhase = phase;
1239 void SigOsc_next_k(SigOsc *unit, int inNumSamples)
1241 // get table
1242 GET_TABLE
1243 const float *table0 = bufData;
1244 const float *table1 = table0 + 1;
1245 int32 maxindex = tableSize - 1;
1246 float maxphase = (float)maxindex;
1247 if (tableSize != unit->mTableSize) {
1248 unit->mTableSize = tableSize;
1249 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1252 float *out = ZOUT(0);
1254 float phase = unit->mPhase;
1256 float freq = ZIN0(1) * unit->m_cpstoinc;
1258 LOOP1(inNumSamples,
1259 while (phase < 0.f) phase += maxphase;
1260 while (phase >= maxphase) phase -= maxphase;
1261 int32 iphase = (int32)phase;
1262 float pfrac = phase - iphase;
1263 float val1 = *(float*)((char*)table0 + iphase);
1264 float val2 = *(float*)((char*)table1 + iphase);
1265 float val = lininterp(pfrac, val1, val2);
1266 phase += freq;
1268 ZXP(out) = val;
1271 unit->mPhase = phase;
1274 void SigOsc_next_a(SigOsc *unit, int inNumSamples)
1276 // get table
1277 GET_TABLE
1278 const float *table0 = bufData;
1279 const float *table1 = table0 + 1;
1280 int32 maxindex = tableSize - 1;
1281 float maxphase = (float)maxindex;
1282 if (tableSize != unit->mTableSize) {
1283 unit->mTableSize = tableSize;
1284 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
1287 float *out = ZOUT(0);
1288 float *freqin = ZIN(1);
1289 float phase = unit->mPhase;
1290 float cpstoinc = unit->m_cpstoinc;
1292 LOOP1(inNumSamples,
1293 while (phase < 0.f) phase += maxphase;
1294 while (phase >= maxphase) phase -= maxphase;
1295 int32 iphase = (int32)phase;
1296 float pfrac = phase - iphase;
1297 float val1 = *(float*)((char*)table0 + iphase);
1298 float val2 = *(float*)((char*)table1 + iphase);
1299 float val = lininterp(pfrac, val1, val2);
1300 phase += ZXP(freqin) * cpstoinc;
1302 ZXP(out) = val;
1305 unit->mPhase = phase;
1309 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1311 void FSinOsc_Ctor(FSinOsc *unit)
1313 SETCALC(FSinOsc_next);
1314 unit->m_freq = ZIN0(0);
1315 float iphase = ZIN0(1);
1316 float w = unit->m_freq * unit->mRate->mRadiansPerSample;
1317 unit->m_b1 = 2. * cos(w);
1318 unit->m_y1 = sin(iphase);
1319 unit->m_y2 = sin(iphase - w);
1321 ZOUT0(0) = unit->m_y1;
1324 void FSinOsc_next(FSinOsc *unit, int inNumSamples)
1326 float *out = ZOUT(0);
1327 double freq = ZIN0(0);
1328 double b1;
1329 if (freq != unit->m_freq) {
1330 unit->m_freq = freq;
1331 double w = freq * unit->mRate->mRadiansPerSample;
1332 unit->m_b1 = b1 = 2.f * cos(w);
1333 } else {
1334 b1 = unit->m_b1;
1336 double y0;
1337 double y1 = unit->m_y1;
1338 double y2 = unit->m_y2;
1339 //Print("y %g %g b1 %g\n", y1, y2, b1);
1340 //Print("%d %d\n", unit->mRate->mFilterLoops, unit->mRate->mFilterRemain);
1341 LOOP(unit->mRate->mFilterLoops,
1342 ZXP(out) = y0 = b1 * y1 - y2;
1343 ZXP(out) = y2 = b1 * y0 - y1;
1344 ZXP(out) = y1 = b1 * y2 - y0;
1346 LOOP(unit->mRate->mFilterRemain,
1347 ZXP(out) = y0 = b1 * y1 - y2;
1348 y2 = y1;
1349 y1 = y0;
1351 //Print("y %g %g b1 %g\n", y1, y2, b1);
1352 unit->m_y1 = y1;
1353 unit->m_y2 = y2;
1354 unit->m_b1 = b1;
1357 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1359 void PSinGrain_Ctor(PSinGrain *unit)
1361 SETCALC(PSinGrain_next);
1362 float freq = ZIN0(0);
1363 float dur = ZIN0(1);
1364 float amp = ZIN0(2);
1366 float w = freq * unit->mRate->mRadiansPerSample;
1367 float sdur = SAMPLERATE * dur;
1369 float rdur = 1.f / sdur;
1370 float rdur2 = rdur * rdur;
1372 unit->m_level = 0.f;
1373 unit->m_slope = 4.0 * (rdur - rdur2); // ampslope
1374 unit->m_curve = -8.0 * rdur2; // ampcurve
1375 unit->mCounter = (int32)(sdur + .5);
1377 /* calc feedback param and initial conditions */
1378 unit->m_b1 = 2. * cos(w);
1379 unit->m_y1 = 0.f;
1380 unit->m_y2 = -sin(w) * amp;
1381 ZOUT0(0) = 0.f;
1385 void PSinGrain_next(PSinGrain *unit, int inNumSamples)
1387 float *out = ZOUT(0);
1388 float y0;
1389 float y1 = unit->m_y1;
1390 float y2 = unit->m_y2;
1391 float b1 = unit->m_b1;
1392 float level = unit->m_level;
1393 float slope = unit->m_slope;
1394 float curve = unit->m_curve;
1395 int32 counter = unit->mCounter;
1396 int32 remain = inNumSamples;
1397 int32 nsmps;
1398 do {
1399 if (counter<=0) {
1400 nsmps = remain;
1401 remain = 0;
1402 LOOP(nsmps, ZXP(out) = 0.f;); // can't use Clear bcs might not be aligned
1403 } else {
1404 nsmps = sc_min(remain, counter);
1405 remain -= nsmps;
1406 counter -= nsmps;
1407 if (nsmps == inNumSamples) {
1408 nsmps = unit->mRate->mFilterLoops;
1409 LOOP(nsmps,
1410 y0 = b1 * y1 - y2;
1411 ZXP(out) = y0 * level;
1412 level += slope;
1413 slope += curve;
1414 y2 = b1 * y0 - y1;
1415 ZXP(out) = y2 * level;
1416 level += slope;
1417 slope += curve;
1418 y1 = b1 * y2 - y0;
1419 ZXP(out) = y1 * level;
1420 level += slope;
1421 slope += curve;
1423 nsmps = unit->mRate->mFilterRemain;
1424 LOOP(nsmps,
1425 y0 = b1 * y1 - y2;
1426 y2 = y1;
1427 y1 = y0;
1428 ZXP(out) = y0 * level;
1429 level += slope;
1430 slope += curve;
1432 } else {
1433 LOOP(nsmps,
1434 y0 = b1 * y1 - y2;
1435 y2 = y1;
1436 y1 = y0;
1437 ZXP(out) = y0 * level;
1438 level += slope;
1439 slope += curve;
1442 if (counter == 0) {
1443 NodeEnd(&unit->mParent->mNode);
1446 } while (remain>0);
1447 unit->m_level = level;
1448 unit->m_slope = slope;
1449 unit->m_y1 = y1;
1450 unit->m_y2 = y2;
1453 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1455 //////////////!!!
1457 void SinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1459 float *out = ZOUT(0);
1460 float freqin = ZIN0(0);
1461 float phasein = ZIN0(1);
1463 float *table0 = ft->mSineWavetable;
1464 float *table1 = table0 + 1;
1466 int32 phase = unit->m_phase;
1467 int32 lomask = unit->m_lomask;
1469 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1470 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1471 unit->m_phasein = phasein;
1473 LOOP1(inNumSamples,
1474 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1475 phase += phaseinc;
1477 unit->m_phase = phase;
1480 #if __VEC__
1482 void vSinOsc_next_ikk(SinOsc *unit, int inNumSamples)
1484 vfloat32 *vout = (vfloat32*)OUT(0);
1485 float freqin = ZIN0(0);
1486 float phasein = ZIN0(1);
1488 float *table0 = ft->mSineWavetable;
1489 float *table1 = table0 + 1;
1491 int32 phase = unit->m_phase;
1492 int32 lomask = unit->m_lomask;
1494 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1495 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1496 unit->m_phasein = phasein;
1498 vint32 vphase = vload(phase, phase+phaseinc, phase+2*phaseinc, phase+3*phaseinc);
1499 vint32 vphaseinc = vload(phaseinc << 2);
1500 vint32 v3F800000 = (vint32)vinit(0x3F800000);
1501 vint32 v007FFF80 = (vint32)vinit(0x007FFF80);
1502 vint32 vlomask = vload(lomask);
1503 vuint32 vxlobits1 = (vuint32)vinit(xlobits1);
1504 vuint32 v7 = (vuint32)vinit(7);
1506 vint32 vtable0 = vload((int32)table0); // assuming 32 bit pointers
1507 vint32 vtable1 = vload((int32)table1); // assuming 32 bit pointers
1509 int len = inNumSamples << 2;
1510 for (int i=0; i<len; i+=16) {
1512 vfloat32 vfrac = (vfloat32)(vec_or(v3F800000, vec_and(v007FFF80, vec_sl(vphase, v7))));
1513 vint32 vindex = vec_and(vec_sr(vphase, vxlobits1), vlomask);
1514 vec_union vaddr0, vaddr1;
1515 vaddr0.vi = vec_add(vindex, vtable0);
1516 vaddr1.vi = vec_add(vindex, vtable1);
1518 vec_union vval1, vval2;
1519 vval1.f[0] = *(float*)(vaddr0.i[0]);
1520 vval2.f[0] = *(float*)(vaddr1.i[0]);
1521 vval1.f[1] = *(float*)(vaddr0.i[1]);
1522 vval2.f[1] = *(float*)(vaddr1.i[1]);
1523 vval1.f[2] = *(float*)(vaddr0.i[2]);
1524 vval2.f[2] = *(float*)(vaddr1.i[2]);
1525 vval1.f[3] = *(float*)(vaddr0.i[3]);
1526 vval2.f[3] = *(float*)(vaddr1.i[3]);
1528 vec_st(vec_madd(vval2.vf, vfrac, vval1.vf), i, vout);
1529 vphase = vec_add(vphase, vphaseinc);
1531 unit->m_phase = phase + inNumSamples * phaseinc;
1535 #endif
1537 void SinOsc_next_ika(SinOsc *unit, int inNumSamples)
1539 float *out = ZOUT(0);
1540 float freqin = ZIN0(0);
1541 float *phasein = ZIN(1);
1543 float *table0 = ft->mSineWavetable;
1544 float *table1 = table0 + 1;
1546 int32 phase = unit->m_phase;
1547 int32 lomask = unit->m_lomask;
1549 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1550 float radtoinc = unit->m_radtoinc;
1551 //Print("SinOsc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1552 LOOP1(inNumSamples,
1553 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1554 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1555 phase += freq;
1557 unit->m_phase = phase;
1558 //unit->m_phasein = phasein;
1562 void SinOsc_next_iaa(SinOsc *unit, int inNumSamples)
1564 float *out = ZOUT(0);
1565 float *freqin = ZIN(0);
1566 float *phasein = ZIN(1);
1568 float *table0 = ft->mSineWavetable;
1569 float *table1 = table0 + 1;
1571 int32 phase = unit->m_phase;
1572 int32 lomask = unit->m_lomask;
1574 float cpstoinc = unit->m_cpstoinc;
1575 float radtoinc = unit->m_radtoinc;
1576 //Print("SinOsc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1577 LOOP1(inNumSamples,
1578 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1579 float z = lookupi1(table0, table1, phaseoffset, lomask);
1580 phase += (int32)(cpstoinc * ZXP(freqin));
1581 ZXP(out) = z;
1583 unit->m_phase = phase;
1584 //unit->m_phasein = ZX(phasein);
1588 void SinOsc_next_iak(SinOsc *unit, int inNumSamples)
1590 float *out = ZOUT(0);
1591 float *freqin = ZIN(0);
1592 float phasein = ZIN0(1);
1594 float *table0 = ft->mSineWavetable;
1595 float *table1 = table0 + 1;
1597 int32 phase = unit->m_phase;
1598 int32 lomask = unit->m_lomask;
1600 float cpstoinc = unit->m_cpstoinc;
1601 float radtoinc = unit->m_radtoinc;
1602 float phasemod = unit->m_phasein;
1603 float phaseslope = CALCSLOPE(phasein, phasemod);
1605 LOOP1(inNumSamples,
1606 int32 pphase = phase + (int32)(radtoinc * phasemod);
1607 phasemod += phaseslope;
1608 float z = lookupi1(table0, table1, pphase, lomask);
1609 phase += (int32)(cpstoinc * ZXP(freqin));
1610 ZXP(out) = z;
1612 unit->m_phase = phase;
1613 unit->m_phasein = phasein;
1616 void SinOsc_next_iai(SinOsc *unit, int inNumSamples)
1618 float *out = ZOUT(0);
1619 float *freqin = ZIN(0);
1621 float *table0 = ft->mSineWavetable;
1622 float *table1 = table0 + 1;
1624 int32 phase = unit->m_phase;
1625 int32 lomask = unit->m_lomask;
1627 float cpstoinc = unit->m_cpstoinc;
1628 float radtoinc = unit->m_radtoinc;
1629 float phasemod = unit->m_phasein;
1631 LOOP1(inNumSamples,
1632 int32 pphase = phase + (int32)(radtoinc * phasemod);
1633 float z = lookupi1(table0, table1, pphase, lomask);
1634 phase += (int32)(cpstoinc * ZXP(freqin));
1635 ZXP(out) = z;
1637 unit->m_phase = phase;
1642 void SinOsc_Ctor(SinOsc *unit)
1644 int tableSize2 = ft->mSineSize;
1645 unit->m_phasein = ZIN0(1);
1646 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1647 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1648 unit->m_lomask = (tableSize2 - 1) << 3;
1650 if (INRATE(0) == calc_FullRate) {
1651 if (INRATE(1) == calc_FullRate)
1652 SETCALC(SinOsc_next_iaa);
1653 else if (INRATE(1) == calc_BufRate)
1654 SETCALC(SinOsc_next_iak);
1655 else
1656 SETCALC(SinOsc_next_iai);
1658 unit->m_phase = 0;
1659 } else {
1660 if (INRATE(1) == calc_FullRate) {
1661 //Print("next_ika\n");
1662 SETCALC(SinOsc_next_ika);
1663 unit->m_phase = 0;
1664 } else {
1665 #if __VEC__
1666 if (USEVEC) {
1667 //Print("vSinOsc_next_ikk\n");
1668 SETCALC(vSinOsc_next_ikk);
1669 } else {
1670 //Print("SinOsc_next_ikk\n");
1671 SETCALC(SinOsc_next_ikk);
1673 #else
1674 //Print("next_ikk\n");
1675 SETCALC(SinOsc_next_ikk);
1676 #endif
1677 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1681 SinOsc_next_ikk(unit, 1);
1685 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1686 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1687 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1689 //////////////!!!
1691 void SinOscFB_next_kk(SinOscFB *unit, int inNumSamples)
1693 float *out = ZOUT(0);
1694 float freqin = ZIN0(0);
1696 float feedback = unit->m_feedback;
1697 float nextFeedback = ZIN0(1) * unit->m_radtoinc;
1699 float *table0 = ft->mSineWavetable;
1700 float *table1 = table0 + 1;
1702 int32 phase = unit->m_phase;
1703 int32 lomask = unit->m_lomask;
1704 float prevout = unit->m_prevout;
1705 float feedback_slope = CALCSLOPE(nextFeedback, feedback);
1706 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1708 LooP(inNumSamples) {
1709 prevout = lookupi1(table0, table1, phase + (int32)(feedback * prevout), lomask);
1710 ZXP(out) = prevout;
1711 phase += freq;
1712 feedback += feedback_slope;
1714 unit->m_phase = phase;
1715 unit->m_prevout = prevout;
1716 unit->m_feedback = feedback;
1719 void SinOscFB_Ctor(SinOscFB *unit)
1721 //Print("next_ik\n");
1722 SETCALC(SinOscFB_next_kk);
1724 int tableSize2 = ft->mSineSize;
1725 unit->m_lomask = (tableSize2 - 1) << 3;
1726 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
1727 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1728 unit->m_prevout = 0.;
1729 unit->m_feedback = ZIN0(1) * unit->m_radtoinc;
1731 unit->m_phase = 0;
1733 SinOscFB_next_kk(unit, 1);
1737 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1738 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1740 void Osc_Ctor(Osc *unit)
1742 unit->mTableSize = -1;
1744 float fbufnum = ZIN0(0);
1745 uint32 bufnum = (uint32)fbufnum;
1746 World *world = unit->mWorld;
1748 SndBuf *buf;
1749 if (bufnum >= world->mNumSndBufs) {
1750 int localBufNum = bufnum - world->mNumSndBufs;
1751 Graph *parent = unit->mParent;
1752 if(localBufNum <= parent->localBufNum) {
1753 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1754 } else {
1755 buf = unit->m_buf = world->mSndBufs;
1757 } else {
1758 buf = unit->m_buf = world->mSndBufs + bufnum;
1761 int tableSize = buf->samples;
1762 int tableSize2 = tableSize >> 1;
1763 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1765 unit->m_phasein = ZIN0(2);
1767 if (INRATE(1) == calc_FullRate) {
1768 if (INRATE(2) == calc_FullRate) {
1769 //Print("next_iaa\n");
1770 SETCALC(Osc_next_iaa);
1771 unit->m_phase = 0;
1772 } else {
1773 //Print("next_iak\n");
1774 SETCALC(Osc_next_iak);
1775 unit->m_phase = 0;
1777 } else {
1778 if (INRATE(2) == calc_FullRate) {
1779 //Print("next_ika\n");
1780 SETCALC(Osc_next_ika);
1781 unit->m_phase = 0;
1782 } else {
1783 //Print("next_ikk\n");
1784 SETCALC(Osc_next_ikk);
1785 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1789 Osc_next_ikk(unit, 1);
1792 //////////////!!!
1794 void Osc_next_ikk(Osc *unit, int inNumSamples)
1796 // get table
1797 GET_TABLE
1798 const float *table0 = bufData;
1799 const float *table1 = table0 + 1;
1800 if (tableSize != unit->mTableSize) {
1801 unit->mTableSize = tableSize;
1802 int tableSize2 = tableSize >> 1;
1803 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1804 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1805 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1806 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1809 float *out = ZOUT(0);
1810 float freqin = ZIN0(1);
1811 float phasein = ZIN0(2);
1813 int32 phase = unit->m_phase;
1814 int32 lomask = unit->m_lomask;
1816 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1817 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
1818 unit->m_phasein = phasein;
1820 LOOP1(inNumSamples,
1821 ZXP(out) = lookupi1(table0, table1, phase, lomask);
1822 phase += phaseinc;
1824 unit->m_phase = phase;
1828 void Osc_next_ika(Osc *unit, int inNumSamples)
1830 // get table
1831 GET_TABLE
1832 const float *table0 = bufData;
1833 const float *table1 = table0 + 1;
1834 if (tableSize != unit->mTableSize) {
1835 unit->mTableSize = tableSize;
1836 int tableSize2 = tableSize >> 1;
1837 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1838 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1839 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1840 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1843 float *out = ZOUT(0);
1844 float freqin = ZIN0(1);
1845 float *phasein = ZIN(2);
1847 int32 phase = unit->m_phase;
1848 int32 lomask = unit->m_lomask;
1850 int32 freq = (int32)(unit->m_cpstoinc * freqin);
1851 float radtoinc = unit->m_radtoinc;
1852 //Print("Osc_next_ika %d %g %d\n", inNumSamples, radtoinc, phase);
1853 LOOP1(inNumSamples,
1854 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1855 ZXP(out) = lookupi1(table0, table1, phaseoffset, lomask);
1856 phase += freq;
1858 unit->m_phase = phase;
1859 //unit->m_phasein = phasein;
1862 void Osc_next_iaa(Osc *unit, int inNumSamples)
1864 // get table
1865 GET_TABLE
1866 const float *table0 = bufData;
1867 const float *table1 = table0 + 1;
1868 if (tableSize != unit->mTableSize) {
1869 unit->mTableSize = tableSize;
1870 int tableSize2 = tableSize >> 1;
1871 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1872 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1873 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1874 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1877 float *out = ZOUT(0);
1878 float *freqin = ZIN(1);
1879 float *phasein = ZIN(2);
1881 int32 phase = unit->m_phase;
1882 int32 lomask = unit->m_lomask;
1884 float cpstoinc = unit->m_cpstoinc;
1885 float radtoinc = unit->m_radtoinc;
1886 //Print("Osc_next_iaa %d %g %g %d\n", inNumSamples, cpstoinc, radtoinc, phase);
1887 LOOP1(inNumSamples,
1888 int32 phaseoffset = phase + (int32)(radtoinc * ZXP(phasein));
1889 float z = lookupi1(table0, table1, phaseoffset, lomask);
1890 phase += (int32)(cpstoinc * ZXP(freqin));
1891 ZXP(out) = z;
1893 unit->m_phase = phase;
1894 //unit->m_phasein = ZX(phasein);
1898 void Osc_next_iak(Osc *unit, int inNumSamples)
1900 // get table
1901 GET_TABLE
1902 const float *table0 = bufData;
1903 const float *table1 = table0 + 1;
1904 if (tableSize != unit->mTableSize) {
1905 unit->mTableSize = tableSize;
1906 int tableSize2 = tableSize >> 1;
1907 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
1908 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.); // Osc, OscN, PMOsc
1909 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
1910 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
1913 float *out = ZOUT(0);
1914 float *freqin = ZIN(1);
1915 float phasein = ZIN0(2);
1917 int32 phase = unit->m_phase;
1918 int32 lomask = unit->m_lomask;
1920 float cpstoinc = unit->m_cpstoinc;
1921 float radtoinc = unit->m_radtoinc;
1922 float phasemod = unit->m_phasein;
1923 float phaseslope = CALCSLOPE(phasein, phasemod);
1925 LOOP1(inNumSamples,
1926 int32 pphase = phase + (int32)(radtoinc * phasemod);
1927 phasemod += phaseslope;
1928 float z = lookupi1(table0, table1, pphase, lomask);
1929 phase += (int32)(cpstoinc * ZXP(freqin));
1930 ZXP(out) = z;
1932 unit->m_phase = phase;
1933 unit->m_phasein = phasein;
1936 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1938 void OscN_Ctor(OscN *unit)
1940 unit->mTableSize = -1;
1942 float fbufnum = ZIN0(0);
1943 uint32 bufnum = (uint32)fbufnum;
1944 World *world = unit->mWorld;
1945 SndBuf *buf;
1946 if (bufnum >= world->mNumSndBufs) {
1947 int localBufNum = bufnum - world->mNumSndBufs;
1948 Graph *parent = unit->mParent;
1949 if(localBufNum <= parent->localBufNum) {
1950 buf = unit->m_buf = parent->mLocalSndBufs + localBufNum;
1951 } else {
1952 buf = unit->m_buf = world->mSndBufs;
1954 } else {
1955 buf = unit->m_buf = world->mSndBufs + bufnum;
1958 int tableSize = buf->samples;
1959 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
1961 unit->m_phasein = ZIN0(2);
1962 //Print("OscN_Ctor\n");
1963 if (INRATE(1) == calc_FullRate) {
1964 if (INRATE(2) == calc_FullRate) {
1965 //Print("next_naa\n");
1966 SETCALC(OscN_next_naa);
1967 unit->m_phase = 0;
1968 } else {
1969 //Print("next_nak\n");
1970 SETCALC(OscN_next_nak);
1971 unit->m_phase = 0;
1973 } else {
1974 if (INRATE(2) == calc_FullRate) {
1975 //Print("next_nka\n");
1976 SETCALC(OscN_next_nka);
1977 unit->m_phase = 0;
1978 } else {
1979 //Print("next_nkk\n");
1980 SETCALC(OscN_next_nkk);
1981 unit->m_phase = (int32)(unit->m_phasein * unit->m_radtoinc);
1985 OscN_next_nkk(unit, 1);
1989 void OscN_next_nkk(OscN *unit, int inNumSamples)
1991 // get table
1992 GET_TABLE
1993 const float *table = bufData;
1994 if (tableSize != unit->mTableSize) {
1995 unit->mTableSize = tableSize;
1996 unit->m_lomask = (tableSize - 1) << 2;
1997 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
1998 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2001 float *out = ZOUT(0);
2002 float freqin = ZIN0(1);
2003 float phasein = ZIN0(2);
2005 int32 phase = unit->m_phase;
2006 int32 lomask = unit->m_lomask;
2008 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2009 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
2010 unit->m_phasein = phasein;
2012 LOOP1(inNumSamples,
2013 ZXP(out) = *(float*)((char*)table + ((phase >> xlobits) & lomask));
2014 phase += phaseinc;
2016 unit->m_phase = phase;
2021 void OscN_next_nka(OscN *unit, int inNumSamples)
2023 // get table
2024 GET_TABLE
2025 const float *table = bufData;
2026 if (tableSize != unit->mTableSize) {
2027 unit->mTableSize = tableSize;
2028 unit->m_lomask = (tableSize - 1) << 2;
2029 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2030 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2033 float *out = ZOUT(0);
2034 float freqin = ZIN0(1);
2035 float *phasein = ZIN(2);
2037 int32 phase = unit->m_phase;
2038 int32 lomask = unit->m_lomask;
2040 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2041 float radtoinc = unit->m_radtoinc;
2042 LOOP1(inNumSamples,
2043 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2044 ZXP(out) = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2045 phase += freq;
2047 unit->m_phase = phase;
2050 void OscN_next_naa(OscN *unit, int inNumSamples)
2052 // get table
2053 GET_TABLE
2054 const float *table = bufData;
2055 if (tableSize != unit->mTableSize) {
2056 unit->mTableSize = tableSize;
2057 unit->m_lomask = (tableSize - 1) << 2;
2058 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2059 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2062 float *out = ZOUT(0);
2063 float *freqin = ZIN(1);
2064 float *phasein = ZIN(2);
2066 int32 phase = unit->m_phase;
2067 int32 lomask = unit->m_lomask;
2069 float cpstoinc = unit->m_cpstoinc;
2070 float radtoinc = unit->m_radtoinc;
2071 LOOP1(inNumSamples,
2072 int32 pphase = phase + (int32)(radtoinc * ZXP(phasein));
2073 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2074 phase += (int32)(cpstoinc * ZXP(freqin));
2075 ZXP(out) = z;
2077 unit->m_phase = phase;
2081 void OscN_next_nak(OscN *unit, int inNumSamples)
2083 // get table
2084 GET_TABLE
2085 const float *table = bufData;
2086 if (tableSize != unit->mTableSize) {
2087 unit->mTableSize = tableSize;
2088 unit->m_lomask = (tableSize - 1) << 2;
2089 unit->m_radtoinc = tableSize * (rtwopi * 65536.);
2090 unit->m_cpstoinc = tableSize * SAMPLEDUR * 65536.;
2093 float *out = ZOUT(0);
2094 float *freqin = ZIN(1);
2095 float phasein = ZIN0(2);
2097 int32 phase = unit->m_phase;
2098 int32 lomask = unit->m_lomask;
2100 float cpstoinc = unit->m_cpstoinc;
2101 float radtoinc = unit->m_radtoinc;
2102 float phasemod = unit->m_phasein;
2103 float phaseslope = CALCSLOPE(phasein, phasemod);
2105 LOOP1(inNumSamples,
2106 int32 pphase = phase + (int32)(radtoinc * phasemod);
2107 phasemod += phaseslope;
2108 float z = *(float*)((char*)table + ((pphase >> xlobits) & lomask));
2109 phase += (int32)(cpstoinc * ZXP(freqin));
2110 ZXP(out) = z;
2112 unit->m_phase = phase;
2113 unit->m_phasein = phasein;
2116 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2118 void COsc_Ctor(COsc *unit)
2120 unit->m_fbufnum = -1e9f;
2121 SETCALC(COsc_next);
2122 unit->m_phase1 = 0;
2123 unit->m_phase2 = 0;
2124 unit->mTableSize = -1;
2125 COsc_next(unit, 1);
2129 void COsc_next(COsc *unit, int inNumSamples)
2131 // get table
2132 GET_TABLE
2133 const float *table0 = bufData;
2134 const float *table1 = table0 + 1;
2135 if (tableSize != unit->mTableSize) {
2136 unit->mTableSize = tableSize;
2137 int tableSize2 = tableSize >> 1;
2138 unit->m_lomask = (tableSize2 - 1) << 3; // Osc, OscN, COsc, COsc, COsc2, OscX4, OscX2
2139 // SigOsc, Osc, OscN, PMOsc, COsc, COsc2, OscX4, OscX2
2140 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2143 float *out = ZOUT(0);
2144 float freqin = ZIN0(1);
2145 float beats = ZIN0(2) * 0.5f;
2147 int32 phase1 = unit->m_phase1;
2148 int32 phase2 = unit->m_phase2;
2149 int32 lomask = unit->m_lomask;
2151 int32 cfreq = (int32)(unit->m_cpstoinc * freqin);
2152 int32 beatf = (int32)(unit->m_cpstoinc * beats);
2153 int32 freq1 = cfreq + beatf;
2154 int32 freq2 = cfreq - beatf;
2155 LOOP1(inNumSamples,
2156 float a = lookupi1(table0, table1, phase1, lomask);
2157 float b = lookupi1(table0, table1, phase2, lomask);
2158 ZXP(out) = a + b;
2159 phase1 += freq1;
2160 phase2 += freq2;
2162 unit->m_phase1 = phase1;
2163 unit->m_phase2 = phase2;
2166 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2167 #define VOSC_GET_BUF_UNLOCKED \
2168 const SndBuf *bufs; \
2169 if (bufnum < 0) \
2170 bufnum = 0; \
2172 if (bufnum+1 >= world->mNumSndBufs) { \
2173 int localBufNum = bufnum - world->mNumSndBufs; \
2174 Graph *parent = unit->mParent; \
2175 if(localBufNum <= parent->localBufNum) { \
2176 bufs = parent->mLocalSndBufs + localBufNum; \
2177 } else { \
2178 bufnum = 0; \
2179 bufs = world->mSndBufs + bufnum; \
2181 } else { \
2182 if (bufnum >= world->mNumSndBufs) \
2183 bufnum = 0; \
2184 bufs = world->mSndBufs + sc_max(0, bufnum); \
2187 #define VOSC_GET_BUF \
2188 VOSC_GET_BUF_UNLOCKED \
2189 LOCK_SNDBUF_SHARED(bufs);
2191 void VOsc_Ctor(VOsc *unit)
2193 SETCALC(VOsc_next_ik);
2195 float nextbufpos = ZIN0(0);
2196 unit->m_bufpos = nextbufpos;
2197 int bufnum = sc_floor(nextbufpos);
2198 World *world = unit->mWorld;
2200 VOSC_GET_BUF_UNLOCKED
2202 int tableSize = bufs[0].samples;
2204 unit->mTableSize = tableSize;
2205 int tableSize2 = tableSize >> 1;
2206 unit->m_lomask = (tableSize2 - 1) << 3;
2207 unit->m_radtoinc = tableSize2 * (rtwopi * 65536.);
2208 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2210 unit->m_phasein = ZIN0(2);
2211 unit->m_phaseoffset = (int32)(unit->m_phasein * unit->m_radtoinc);
2212 unit->m_phase = unit->m_phaseoffset;
2214 VOsc_next_ik(unit, 1);
2217 void VOsc_next_ik(VOsc *unit, int inNumSamples)
2219 float *out = ZOUT(0);
2220 float nextbufpos = ZIN0(0);
2221 float freqin = ZIN0(1);
2222 float phasein = ZIN0(2);
2224 float prevbufpos = unit->m_bufpos;
2225 float bufdiff = nextbufpos - prevbufpos;
2227 int32 phase = unit->m_phase;
2228 int32 lomask = unit->m_lomask;
2230 int32 freq = (int32)(unit->m_cpstoinc * freqin);
2231 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
2232 unit->m_phasein = phasein;
2233 int tableSize = unit->mTableSize;
2234 float cur = prevbufpos;
2235 World *world = unit->mWorld;
2237 if (bufdiff == 0.f) {
2238 float level = cur - sc_floor(cur);
2239 uint32 bufnum = (int)sc_floor(cur);
2241 VOSC_GET_BUF
2243 const float *table0 = bufs[0].data;
2244 const float *table2 = bufs[1].data;
2245 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2246 ClearUnitOutputs(unit, inNumSamples);
2247 return;
2250 const float *table1 = table0 + 1;
2251 const float *table3 = table2 + 1;
2253 LOOP1(inNumSamples,
2254 float pfrac = PhaseFrac1(phase);
2255 uint32 index = ((phase >> xlobits1) & lomask);
2256 float val0 = *(float*)((char*)table0 + index);
2257 float val1 = *(float*)((char*)table1 + index);
2258 float val2 = *(float*)((char*)table2 + index);
2259 float val3 = *(float*)((char*)table3 + index);
2260 float a = val0 + val1 * pfrac;
2261 float b = val2 + val3 * pfrac;
2262 ZXP(out) = a + level * (b - a);
2263 phase += phaseinc;
2265 } else {
2266 int nsmps;
2267 int donesmps = 0;
2268 int remain = inNumSamples;
2269 while (remain) {
2270 float level = cur - sc_floor(cur);
2272 float cut;
2273 if (bufdiff > 0.) {
2274 cut = sc_min(nextbufpos, sc_floor(cur+1.f));
2275 } else {
2276 cut = sc_max(nextbufpos, sc_ceil(cur-1.f));
2279 float sweepdiff = cut - cur;
2280 if (cut == nextbufpos) nsmps = remain;
2281 else {
2282 float sweep = (float)inNumSamples / bufdiff;
2283 nsmps = (int)sc_floor(sweep * sweepdiff + 0.5f) - donesmps;
2284 nsmps = sc_clip(nsmps, 1, remain);
2287 float slope = sweepdiff / (float)nsmps;
2289 int32 bufnum = (int32)sc_floor(cur);
2291 VOSC_GET_BUF
2293 const float *table0 = bufs[0].data;
2294 const float *table2 = bufs[1].data;
2295 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2296 ClearUnitOutputs(unit, inNumSamples);
2297 return;
2300 const float *table1 = table0 + 1;
2301 const float *table3 = table2 + 1;
2303 LOOP(nsmps,
2304 float pfrac = PhaseFrac1(phase);
2305 uint32 index = ((phase >> xlobits1) & lomask);
2306 float val0 = *(float*)((char*)table0 + index);
2307 float val1 = *(float*)((char*)table1 + index);
2308 float val2 = *(float*)((char*)table2 + index);
2309 float val3 = *(float*)((char*)table3 + index);
2310 float a = val0 + val1 * pfrac;
2311 float b = val2 + val3 * pfrac;
2312 ZXP(out) = a + level * (b - a);
2313 phase += phaseinc;
2314 level += slope;
2316 donesmps += nsmps;
2317 remain -= nsmps;
2318 cur = cut;
2321 unit->m_bufpos = nextbufpos;
2322 unit->m_phase = phase;
2325 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2327 void VOsc3_Ctor(VOsc3 *unit)
2329 SETCALC(VOsc3_next_ik);
2331 float nextbufpos = ZIN0(0);
2332 unit->m_bufpos = nextbufpos;
2333 int32 bufnum = (int32)sc_floor(nextbufpos);
2334 World *world = unit->mWorld;
2336 VOSC_GET_BUF
2337 int tableSize = bufs[0].samples;
2339 unit->mTableSize = tableSize;
2340 int tableSize2 = tableSize >> 1;
2341 unit->m_lomask = (tableSize2 - 1) << 3;
2342 unit->m_cpstoinc = tableSize2 * SAMPLEDUR * 65536.;
2344 unit->m_phase1 = 0;
2345 unit->m_phase2 = 0;
2346 unit->m_phase3 = 0;
2348 VOsc3_next_ik(unit, 1);
2351 void VOsc3_next_ik(VOsc3 *unit, int inNumSamples)
2353 float *out = ZOUT(0);
2354 float nextbufpos = ZIN0(0);
2355 float freq1in = ZIN0(1);
2356 float freq2in = ZIN0(2);
2357 float freq3in = ZIN0(3);
2359 float prevbufpos = unit->m_bufpos;
2360 float bufdiff = nextbufpos - prevbufpos;
2362 int32 phase1 = unit->m_phase1;
2363 int32 phase2 = unit->m_phase2;
2364 int32 phase3 = unit->m_phase3;
2366 int32 freq1 = (int32)(unit->m_cpstoinc * freq1in);
2367 int32 freq2 = (int32)(unit->m_cpstoinc * freq2in);
2368 int32 freq3 = (int32)(unit->m_cpstoinc * freq3in);
2370 int32 lomask = unit->m_lomask;
2371 int tableSize = unit->mTableSize;
2372 float cur = prevbufpos;
2373 World *world = unit->mWorld;
2375 if (bufdiff == 0.f) {
2376 float level = cur - (int)cur;
2378 int bufnum = (int)cur;
2380 VOSC_GET_BUF
2382 const float *table0 = bufs[0].data;
2383 const float *table2 = bufs[1].data;
2384 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2385 ClearUnitOutputs(unit, inNumSamples);
2386 return;
2389 const float *table1 = table0 + 1;
2390 const float *table3 = table2 + 1;
2392 LOOP1(inNumSamples,
2394 float pfrac1 = PhaseFrac1(phase1);
2395 float pfrac2 = PhaseFrac1(phase2);
2396 float pfrac3 = PhaseFrac1(phase3);
2398 int index1 = ((phase1 >> xlobits1) & lomask);
2399 int index2 = ((phase2 >> xlobits1) & lomask);
2400 int index3 = ((phase3 >> xlobits1) & lomask);
2402 phase1 += freq1;
2403 phase2 += freq2;
2404 phase3 += freq3;
2406 float val10 = *(float*)((char*)table0 + index1);
2407 float val11 = *(float*)((char*)table1 + index1);
2408 float val12 = *(float*)((char*)table2 + index1);
2409 float val13 = *(float*)((char*)table3 + index1);
2410 float a = val10 + val11 * pfrac1;
2411 float b = val12 + val13 * pfrac1;
2413 float val20 = *(float*)((char*)table0 + index2);
2414 float val21 = *(float*)((char*)table1 + index2);
2415 float val22 = *(float*)((char*)table2 + index2);
2416 float val23 = *(float*)((char*)table3 + index2);
2417 a += val20 + val21 * pfrac2;
2418 b += val22 + val23 * pfrac2;
2420 float val30 = *(float*)((char*)table0 + index3);
2421 float val31 = *(float*)((char*)table1 + index3);
2422 float val32 = *(float*)((char*)table2 + index3);
2423 float val33 = *(float*)((char*)table3 + index3);
2424 a += val30 + val31 * pfrac3;
2425 b += val32 + val33 * pfrac3;
2427 ZXP(out) = a + level * (b - a);
2429 } else {
2430 int nsmps;
2431 int donesmps = 0;
2432 int remain = inNumSamples;
2433 do {
2434 float level = cur - sc_trunc(cur);
2436 float cut;
2437 if (bufdiff >= 0.)
2438 cut = sc_min(nextbufpos, sc_trunc(cur+1.f));
2439 else
2440 cut = sc_max(nextbufpos, sc_ceil(cur-1.f));
2442 float sweepdiff = cut - cur;
2443 if (cut == nextbufpos) nsmps = remain;
2444 else {
2445 float sweep = (float)inNumSamples / bufdiff;
2446 nsmps = sc_floor(sweep * sweepdiff + 0.5f) - donesmps;
2447 nsmps = sc_clip(nsmps, 1, remain);
2450 float slope = sweepdiff / (float)nsmps;
2452 int bufnum = (int)cur;
2454 VOSC_GET_BUF
2456 const float *table0 = bufs[0].data;
2457 const float *table2 = bufs[1].data;
2458 if (!table0 || !table2 || tableSize != bufs[0].samples|| tableSize != bufs[1].samples) {
2459 ClearUnitOutputs(unit, inNumSamples);
2460 return;
2463 const float *table1 = table0 + 1;
2464 const float *table3 = table2 + 1;
2466 LOOP(nsmps,
2468 float pfrac1 = PhaseFrac1(phase1);
2469 float pfrac2 = PhaseFrac1(phase2);
2470 float pfrac3 = PhaseFrac1(phase3);
2472 int index1 = ((phase1 >> xlobits1) & lomask);
2473 int index2 = ((phase2 >> xlobits1) & lomask);
2474 int index3 = ((phase3 >> xlobits1) & lomask);
2476 phase1 += freq1;
2477 phase2 += freq2;
2478 phase3 += freq3;
2480 float val10 = *(float*)((char*)table0 + index1);
2481 float val11 = *(float*)((char*)table1 + index1);
2482 float val12 = *(float*)((char*)table2 + index1);
2483 float val13 = *(float*)((char*)table3 + index1);
2484 float a = val10 + val11 * pfrac1;
2485 float b = val12 + val13 * pfrac1;
2487 float val20 = *(float*)((char*)table0 + index2);
2488 float val21 = *(float*)((char*)table1 + index2);
2489 float val22 = *(float*)((char*)table2 + index2);
2490 float val23 = *(float*)((char*)table3 + index2);
2491 a += val20 + val21 * pfrac2;
2492 b += val22 + val23 * pfrac2;
2494 float val30 = *(float*)((char*)table0 + index3);
2495 float val31 = *(float*)((char*)table1 + index3);
2496 float val32 = *(float*)((char*)table2 + index3);
2497 float val33 = *(float*)((char*)table3 + index3);
2498 a += val30 + val31 * pfrac3;
2499 b += val32 + val33 * pfrac3;
2501 ZXP(out) = a + level * (b - a);
2502 level += slope;
2504 donesmps += nsmps;
2505 remain -= nsmps;
2506 cur = cut;
2507 } while (remain);
2509 unit->m_bufpos = nextbufpos;
2510 unit->m_phase1 = phase1;
2511 unit->m_phase2 = phase2;
2512 unit->m_phase3 = phase3;
2515 //////////////////////////////////////////////////////////////////////////////////////////
2517 void Formant_Ctor(Formant *unit)
2519 SETCALC(Formant_next);
2520 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536.;
2521 unit->m_phase1 = 0;
2522 unit->m_phase2 = 0;
2523 unit->m_phase3 = 0;
2524 Formant_next(unit, 1);
2527 #define tqcyc13 0x18000000
2529 void Formant_next(Formant *unit, int inNumSamples)
2531 float *out = ZOUT(0);
2532 float freq1in = ZIN0(0);
2533 float freq2in = ZIN0(1);
2534 float freq3in = ZIN0(2);
2536 int32 phase1 = unit->m_phase1;
2537 int32 phase2 = unit->m_phase2;
2538 int32 phase3 = unit->m_phase3;
2539 float cpstoinc = unit->m_cpstoinc;
2540 int32 freq1 = (int32)(cpstoinc * freq1in);
2541 int32 freq2 = (int32)(cpstoinc * freq2in);
2542 int32 freq3 = (int32)(cpstoinc * freq3in);
2543 float* sine = ft->mSine;
2544 int32 formfreq = sc_max(freq1, freq3);
2545 LOOP1(inNumSamples,
2546 if (phase3 < onecyc13) {
2547 ZXP(out) = (*(float*)((char*)sine + (((phase3 + tqcyc13) >> xlobits) & xlomask13)) + 1.f)
2548 * *(float*)((char*)sine + ((phase2 >> xlobits) & xlomask13));
2549 phase3 += formfreq;
2550 } else {
2551 ZXP(out) = 0.f;
2553 phase1 += freq1;
2554 phase2 += freq2;
2555 if (phase1 > onecyc13) {
2556 phase1 -= onecyc13;
2557 phase2 = phase1 * freq2 / freq1;
2558 phase3 = phase1 * freq3 / freq1;
2562 unit->m_phase1 = phase1;
2563 unit->m_phase2 = phase2;
2564 unit->m_phase3 = phase3;
2567 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2569 inline float lookup13(float* table, int32 pphase)
2571 float pfrac = PhaseFrac(pphase);
2572 float* tbl = (float*)((char*)table + ((pphase >> xlobits) & xlomask13));
2573 return lininterp(pfrac, tbl[0], tbl[1]);
2576 void Blip_Ctor(Blip *unit)
2578 SETCALC(Blip_next);
2579 unit->m_freqin = ZIN0(0);
2580 unit->m_numharm = (int32)ZIN0(1);
2582 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2583 int32 N = unit->m_numharm;
2584 int32 maxN = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2585 if (N > maxN) N = maxN;
2586 if (N < 1) N = 1;
2587 unit->m_N = N;
2588 unit->m_scale = 0.5/N;
2589 unit->m_phase = 0;
2591 Blip_next(unit, 1);
2594 void Blip_next(Blip *unit, int inNumSamples)
2596 float *out = ZOUT(0);
2597 float freqin = ZIN0(0);
2598 int numharm = (int32)ZIN0(1);
2600 int32 phase = unit->m_phase;
2602 float* numtbl = ft->mSine;
2603 float* dentbl = ft->mCosecant;
2605 int32 freq, N, prevN;
2606 float scale, prevscale;
2607 bool crossfade;
2608 if (numharm != unit->m_numharm || freqin != unit->m_freqin) {
2609 N = numharm;
2610 int32 maxN = (int32)((SAMPLERATE * 0.5) / freqin);
2611 if (N > maxN) {
2612 float maxfreqin;
2613 N = maxN;
2614 maxfreqin = sc_max(unit->m_freqin, freqin);
2615 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2616 } else {
2617 if (N < 1) { N = 1; }
2618 freq = (int32)(unit->m_cpstoinc * freqin);
2620 crossfade = N != unit->m_N;
2621 prevN = unit->m_N;
2622 prevscale = unit->m_scale;
2623 unit->m_N = N;
2624 unit->m_scale = scale = 0.5/N;
2625 } else {
2626 N = unit->m_N;
2627 freq = (int32)(unit->m_cpstoinc * freqin);
2628 scale = unit->m_scale;
2629 crossfade = false;
2631 int32 N2 = 2*N+1;
2633 if (crossfade) {
2634 int32 prevN2 = 2 * prevN + 1;
2635 float xfade_slope = unit->mRate->mSlopeFactor;
2636 float xfade = 0.f;
2637 LOOP1(inNumSamples,
2638 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2639 float t0 = tbl[0];
2640 float t1 = tbl[1];
2641 if (t0 == kBadValue || t1 == kBadValue) {
2642 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2643 t0 = tbl[0];
2644 t1 = tbl[1];
2645 float pfrac = PhaseFrac(phase);
2646 float denom = t0 + (t1 - t0) * pfrac;
2647 if (std::abs(denom) < 0.0005f) {
2648 ZXP(out) = 1.f;
2649 } else {
2650 int32 rphase = phase * prevN2;
2651 pfrac = PhaseFrac(rphase);
2652 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2653 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2654 float n1 = (numer / denom - 1.f) * prevscale;
2656 rphase = phase * N2;
2657 pfrac = PhaseFrac(rphase);
2658 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2659 numer = lininterp(pfrac, tbl[0], tbl[1]);
2660 float n2 = (numer / denom - 1.f) * scale;
2662 ZXP(out) = lininterp(xfade, n1, n2);
2664 } else {
2665 float pfrac = PhaseFrac(phase);
2666 float denom = t0 + (t1 - t0) * pfrac;
2668 int32 rphase = phase * prevN2;
2669 pfrac = PhaseFrac(rphase);
2670 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2671 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2672 float n1 = (numer * denom - 1.f) * prevscale;
2674 rphase = phase * N2;
2675 pfrac = PhaseFrac(rphase);
2676 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2677 numer = lininterp(pfrac, tbl[0], tbl[1]);
2678 float n2 = (numer * denom - 1.f) * scale;
2680 ZXP(out) = lininterp(xfade, n1, n2);
2682 phase += freq;
2683 xfade += xfade_slope;
2685 } else {
2686 // hmm, if freq is above sr/4 then revert to sine table osc w/ no interpolation ?
2687 // why bother, it isn't a common choice for a fundamental.
2688 LOOP1(inNumSamples,
2689 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2690 float t0 = tbl[0];
2691 float t1 = tbl[1];
2692 if (t0 == kBadValue || t1 == kBadValue) {
2693 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2694 t0 = tbl[0];
2695 t1 = tbl[1];
2696 float pfrac = PhaseFrac(phase);
2697 float denom = t0 + (t1 - t0) * pfrac;
2698 if (std::abs(denom) < 0.0005f) {
2699 ZXP(out) = 1.f;
2700 } else {
2701 int32 rphase = phase * N2;
2702 pfrac = PhaseFrac(rphase);
2703 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2704 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2705 ZXP(out) = (numer / denom - 1.f) * scale;
2707 } else {
2708 float pfrac = PhaseFrac(phase);
2709 float denom = t0 + (t1 - t0) * pfrac;
2710 int32 rphase = phase * N2;
2711 pfrac = PhaseFrac(rphase);
2712 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2713 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2714 ZXP(out) = (numer * denom - 1.f) * scale;
2716 phase += freq;
2720 unit->m_phase = phase;
2721 unit->m_freqin = freqin;
2722 unit->m_numharm = numharm;
2726 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2729 void Saw_Ctor(Saw *unit)
2731 SETCALC(Saw_next);
2732 unit->m_freqin = ZIN0(0);
2734 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2735 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2736 unit->m_scale = 0.5/unit->m_N;
2737 unit->m_phase = 0;
2738 unit->m_y1 = -0.46f;
2740 ZOUT0(0) = 0.f;
2743 void Saw_next(Saw *unit, int inNumSamples)
2745 float *out = ZOUT(0);
2746 float freqin = ZIN0(0);
2748 int32 phase = unit->m_phase;
2749 float y1 = unit->m_y1;
2751 float* numtbl = ft->mSine;
2752 float* dentbl = ft->mCosecant;
2754 int32 freq, N, prevN;
2755 float scale, prevscale;
2756 bool crossfade;
2757 if (freqin != unit->m_freqin) {
2758 N = (int32)((SAMPLERATE * 0.5) / freqin);
2759 if (N != unit->m_N) {
2760 float maxfreqin;
2761 maxfreqin = sc_max(unit->m_freqin, freqin);
2762 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2763 crossfade = true;
2764 } else {
2765 freq = (int32)(unit->m_cpstoinc * freqin);
2766 crossfade = false;
2768 prevN = unit->m_N;
2769 prevscale = unit->m_scale;
2770 unit->m_N = N;
2771 unit->m_scale = scale = 0.5/N;
2772 } else {
2773 N = unit->m_N;
2774 freq = (int32)(unit->m_cpstoinc * freqin);
2775 scale = unit->m_scale;
2776 crossfade = false;
2778 int32 N2 = 2*N+1;
2780 if (crossfade) {
2781 int32 prevN2 = 2 * prevN + 1;
2782 float xfade_slope = unit->mRate->mSlopeFactor;
2783 float xfade = 0.f;
2784 LOOP1(inNumSamples,
2785 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2786 float t0 = tbl[0];
2787 float t1 = tbl[1];
2788 if (t0 == kBadValue || t1 == kBadValue) {
2789 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2790 t0 = tbl[0];
2791 t1 = tbl[1];
2792 float pfrac = PhaseFrac(phase);
2793 float denom = t0 + (t1 - t0) * pfrac;
2794 if (std::abs(denom) < 0.0005f) {
2795 ZXP(out) = y1 = 1.f + 0.999f * y1;
2796 } else {
2797 int32 rphase = phase * prevN2;
2798 pfrac = PhaseFrac(rphase);
2799 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2800 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2801 float n1 = (numer / denom - 1.f) * prevscale;
2803 rphase = phase * N2;
2804 pfrac = PhaseFrac(rphase);
2805 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2806 numer = lininterp(pfrac, tbl[0], tbl[1]);
2807 float n2 = (numer / denom - 1.f) * scale;
2809 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2812 } else {
2813 float pfrac = PhaseFrac(phase);
2814 float denom = t0 + (t1 - t0) * pfrac;
2816 int32 rphase = phase * prevN2;
2817 pfrac = PhaseFrac(rphase);
2818 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2819 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2820 float n1 = (numer * denom - 1.f) * prevscale;
2822 rphase = phase * N2;
2823 pfrac = PhaseFrac(rphase);
2824 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2825 numer = lininterp(pfrac, tbl[0], tbl[1]);
2826 float n2 = (numer * denom - 1.f) * scale;
2828 ZXP(out) = y1 = n1 + xfade * (n2 - n1) + 0.999f * y1;
2830 phase += freq;
2831 xfade += xfade_slope;
2833 } else {
2834 // hmm, if freq is above sr/4 then revert to sine table osc ?
2835 // why bother, it isn't a common choice for a fundamental.
2836 LOOP1(inNumSamples,
2837 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2838 float t0 = tbl[0];
2839 float t1 = tbl[1];
2840 if (t0 == kBadValue || t1 == kBadValue) {
2841 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2842 t0 = tbl[0];
2843 t1 = tbl[1];
2844 float pfrac = PhaseFrac(phase);
2845 float denom = t0 + (t1 - t0) * pfrac;
2846 if (std::abs(denom) < 0.0005f) {
2847 ZXP(out) = y1 = 1.f + 0.999f * y1;
2848 } else {
2849 int32 rphase = phase * N2;
2850 pfrac = PhaseFrac(rphase);
2851 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2852 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2853 ZXP(out) = y1 = (numer / denom - 1.f) * scale + 0.999f * y1;
2855 } else {
2856 float pfrac = PhaseFrac(phase);
2857 float denom = t0 + (t1 - t0) * pfrac;
2858 int32 rphase = phase * N2;
2859 pfrac = PhaseFrac(rphase);
2860 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2861 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2862 ZXP(out) = y1 = (numer * denom - 1.f) * scale + 0.999f * y1;
2864 phase += freq;
2868 unit->m_y1 = y1;
2869 unit->m_phase = phase;
2870 unit->m_freqin = freqin;
2874 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2877 void Pulse_Ctor(Pulse *unit)
2879 SETCALC(Pulse_next);
2880 unit->m_freqin = ZIN0(0);
2882 unit->m_cpstoinc = ft->mSineSize * SAMPLEDUR * 65536. * 0.5;
2883 unit->m_N = (int32)((SAMPLERATE * 0.5) / unit->m_freqin);
2884 unit->m_scale = 0.5/unit->m_N;
2885 unit->m_phase = 0;
2886 unit->m_phaseoff = 0;
2887 unit->m_y1 = 0.f;
2888 ZOUT0(0) = 0.f;
2891 void Pulse_next(Pulse *unit, int inNumSamples)
2893 float *out = ZOUT(0);
2894 float freqin = ZIN0(0);
2895 float duty = ZIN0(1);
2897 int32 phase = unit->m_phase;
2898 float y1 = unit->m_y1;
2900 float* numtbl = ft->mSine;
2901 float* dentbl = ft->mCosecant;
2903 int32 freq, N, prevN;
2904 float scale, prevscale;
2905 bool crossfade;
2907 if (freqin != unit->m_freqin) {
2908 N = (int32)((SAMPLERATE * 0.5) / freqin);
2909 if (N != unit->m_N) {
2910 float maxfreqin;
2911 maxfreqin = sc_max(unit->m_freqin, freqin);
2912 freq = (int32)(unit->m_cpstoinc * maxfreqin);
2913 crossfade = true;
2914 } else {
2915 freq = (int32)(unit->m_cpstoinc * freqin);
2916 crossfade = false;
2918 prevN = unit->m_N;
2919 prevscale = unit->m_scale;
2920 unit->m_N = N;
2921 unit->m_scale = scale = 0.5/N;
2922 } else {
2923 N = unit->m_N;
2924 freq = (int32)(unit->m_cpstoinc * freqin);
2925 scale = unit->m_scale;
2926 crossfade = false;
2928 int32 N2 = 2*N+1;
2930 int32 phaseoff = unit->m_phaseoff;
2931 int32 next_phaseoff = (int32)(duty * (1L << 28));
2932 int32 phaseoff_slope = (int32)((next_phaseoff - phaseoff) * unit->mRate->mSlopeFactor);
2933 unit->m_phaseoff = next_phaseoff;
2934 float rscale = 1.f / scale + 1.f;
2935 float pul1, pul2;
2937 if (crossfade) {
2938 int32 prevN2 = 2 * prevN + 1;
2939 float xfade_slope = unit->mRate->mSlopeFactor;
2940 float xfade = 0.f;
2941 LOOP1(inNumSamples,
2942 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
2943 float t0 = tbl[0];
2944 float t1 = tbl[1];
2945 if (t0 == kBadValue || t1 == kBadValue) {
2946 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
2947 t0 = tbl[0];
2948 t1 = tbl[1];
2949 float pfrac = PhaseFrac(phase);
2950 float denom = t0 + (t1 - t0) * pfrac;
2951 if (std::abs(denom) < 0.0005f) {
2952 pul1 = 1.f;
2953 } else {
2954 int32 rphase = phase * prevN2;
2955 pfrac = PhaseFrac(rphase);
2956 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2957 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2958 float n1 = (numer / denom - 1.f) * prevscale;
2960 rphase = phase * N2;
2961 pfrac = PhaseFrac(rphase);
2962 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2963 numer = lininterp(pfrac, tbl[0], tbl[1]);
2964 float n2 = (numer / denom - 1.f) * scale;
2966 pul1 = lininterp(xfade, n1, n2);
2968 } else {
2969 float pfrac = PhaseFrac(phase);
2970 float denom = lininterp(pfrac, t0, t1);
2972 int32 rphase = phase * prevN2;
2973 pfrac = PhaseFrac(rphase);
2974 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2975 float numer = lininterp(pfrac, tbl[0], tbl[1]);
2976 float n1 = (numer * denom - 1.f) * prevscale;
2978 rphase = phase * N2;
2979 pfrac = PhaseFrac(rphase);
2980 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
2981 numer = lininterp(pfrac, tbl[0], tbl[1]);
2982 float n2 = (numer * denom - 1.f) * scale;
2984 pul1 = lininterp(xfade, n1, n2);
2987 int32 phase2 = phase + phaseoff;
2988 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
2989 t0 = tbl[0];
2990 t1 = tbl[1];
2991 if (t0 == kBadValue || t1 == kBadValue) {
2992 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
2993 t0 = tbl[0];
2994 t1 = tbl[1];
2995 float pfrac = PhaseFrac(phase2);
2996 float denom = t0 + (t1 - t0) * pfrac;
2997 if (std::abs(denom) < 0.0005f) {
2998 pul2 = 1.f;
2999 } else {
3000 int32 rphase = phase2 * prevN2;
3001 pfrac = PhaseFrac(rphase);
3002 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3003 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3004 float n1 = (numer / denom - 1.f) * prevscale;
3006 rphase = phase2 * N2;
3007 pfrac = PhaseFrac(rphase);
3008 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3009 numer = lininterp(pfrac, tbl[0], tbl[1]);
3010 float n2 = (numer / denom - 1.f) * scale;
3012 pul2 = lininterp(xfade, n1, n2);
3014 } else {
3015 float pfrac = PhaseFrac(phase2);
3016 float denom = t0 + (t1 - t0) * pfrac;
3018 int32 rphase = phase2 * prevN2;
3019 pfrac = PhaseFrac(rphase);
3020 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3021 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3022 float n1 = (numer * denom - 1.f) * prevscale;
3024 rphase = phase2 * N2;
3025 pfrac = PhaseFrac(rphase);
3026 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3027 numer = lininterp(pfrac, tbl[0], tbl[1]);
3028 float n2 = (numer * denom - 1.f) * scale;
3030 pul2 = lininterp(xfade, n1, n2);
3033 ZXP(out) = y1 = pul1 - pul2 + 0.999f * y1;
3034 phase += freq;
3035 phaseoff += phaseoff_slope;
3036 xfade += xfade_slope;
3038 } else {
3039 LOOP1(inNumSamples,
3040 float* tbl = (float*)((char*)dentbl + ((phase >> xlobits) & xlomask13));
3041 float t0 = tbl[0];
3042 float t1 = tbl[1];
3043 if (t0 == kBadValue || t1 == kBadValue) {
3044 tbl = (float*)((char*)numtbl + ((phase >> xlobits) & xlomask13));
3045 t0 = tbl[0];
3046 t1 = tbl[1];
3047 float pfrac = PhaseFrac(phase);
3048 float denom = t0 + (t1 - t0) * pfrac;
3049 if (std::abs(denom) < 0.0005f) {
3050 pul1 = rscale;
3051 } else {
3052 int32 rphase = phase * N2;
3053 pfrac = PhaseFrac(rphase);
3054 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3055 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3056 pul1 = numer / denom;
3058 } else {
3059 float pfrac = PhaseFrac(phase);
3060 float denom = t0 + (t1 - t0) * pfrac;
3061 int32 rphase = phase * N2;
3062 pfrac = PhaseFrac(rphase);
3063 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3064 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3065 pul1 = (numer * denom);
3068 int32 phase2 = phase + phaseoff;
3069 tbl = (float*)((char*)dentbl + ((phase2 >> xlobits) & xlomask13));
3070 t0 = tbl[0];
3071 t1 = tbl[1];
3072 if (t0 == kBadValue || t1 == kBadValue) {
3073 tbl = (float*)((char*)numtbl + ((phase2 >> xlobits) & xlomask13));
3074 t0 = tbl[0];
3075 t1 = tbl[1];
3076 float pfrac = PhaseFrac(phase2);
3077 float denom = t0 + (t1 - t0) * pfrac;
3078 if (std::abs(denom) < 0.0005f) {
3079 pul2 = rscale;
3080 } else {
3081 int32 rphase = phase2 * N2;
3082 pfrac = PhaseFrac(rphase);
3083 tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3084 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3085 pul2 = numer / denom;
3087 } else {
3088 float pfrac = PhaseFrac(phase2);
3089 float denom = t0 + (t1 - t0) * pfrac;
3090 int32 rphase = phase2 * N2;
3091 pfrac = PhaseFrac(rphase);
3092 float* tbl = (float*)((char*)numtbl + ((rphase >> xlobits) & xlomask13));
3093 float numer = lininterp(pfrac, tbl[0], tbl[1]);
3095 pul2 = (numer * denom);
3098 ZXP(out) = y1 = (pul1 - pul2) * scale + 0.999f * y1;
3099 phase += freq;
3100 phaseoff += phaseoff_slope;
3104 unit->m_y1 = y1;
3105 unit->m_phase = phase;
3106 unit->m_freqin = freqin;
3109 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3111 void Klang_Dtor(Klang *unit)
3113 RTFree(unit->mWorld, unit->m_coefs);
3116 static float Klang_SetCoefs(Klang *unit);
3118 void Klang_Ctor(Klang *unit)
3120 SETCALC(Klang_next);
3121 ZOUT0(0) = Klang_SetCoefs(unit);
3124 float Klang_SetCoefs(Klang *unit)
3126 unit->m_numpartials = (unit->mNumInputs - 2)/3;
3128 int numcoefs = unit->m_numpartials * 3;
3129 unit->m_coefs = (float*)RTAlloc(unit->mWorld, numcoefs * sizeof(float));
3131 float freqscale = ZIN0(0) * unit->mRate->mRadiansPerSample;
3132 float freqoffset = ZIN0(1) * unit->mRate->mRadiansPerSample;
3134 float outf = 0.;
3135 float* coefs = unit->m_coefs - 1;
3137 for (int i=0,j=2; i<unit->m_numpartials; ++i,j+=3) {
3138 float w = ZIN0(j) * freqscale + freqoffset;
3139 float level = ZIN0(j+1);
3140 float phase = ZIN0(j+2);
3142 if (phase != 0.f) {
3143 outf += *++coefs = level * sin(phase); // y1
3144 *++coefs = level * sin(phase - w); // y2
3145 } else {
3146 outf += *++coefs = 0.f; // y1
3147 *++coefs = level * -sin(w); // y2
3149 *++coefs = 2. * cos(w); // b1
3151 return outf;
3154 void Klang_next(Klang *unit, int inNumSamples)
3156 float *out0 = ZOUT(0);
3158 float* out;
3159 float y0_0, y1_0, y2_0, b1_0;
3160 float y0_1, y1_1, y2_1, b1_1;
3161 float y0_2, y1_2, y2_2, b1_2;
3162 float y0_3, y1_3, y2_3, b1_3;
3163 float outf;
3165 float* coefs = unit->m_coefs - 1;
3166 int32 numpartials = unit->m_numpartials;
3168 switch (numpartials & 3) {
3169 case 3 :
3170 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3171 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3172 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3174 out = out0;
3175 LOOP(unit->mRate->mFilterLoops,
3176 outf = y0_0 = b1_0 * y1_0 - y2_0;
3177 outf += y0_1 = b1_1 * y1_1 - y2_1;
3178 outf += y0_2 = b1_2 * y1_2 - y2_2;
3179 ZXP(out) = outf;
3181 outf = y2_0 = b1_0 * y0_0 - y1_0;
3182 outf += y2_1 = b1_1 * y0_1 - y1_1;
3183 outf += y2_2 = b1_2 * y0_2 - y1_2;
3184 ZXP(out) = outf;
3186 outf = y1_0 = b1_0 * y2_0 - y0_0;
3187 outf += y1_1 = b1_1 * y2_1 - y0_1;
3188 outf += y1_2 = b1_2 * y2_2 - y0_2;
3189 ZXP(out) = outf;
3191 LOOP(unit->mRate->mFilterRemain,
3192 outf = y0_0 = b1_0 * y1_0 - y2_0;
3193 outf += y0_1 = b1_1 * y1_1 - y2_1;
3194 outf += y0_2 = b1_2 * y1_2 - y2_2;
3195 y2_0 = y1_0; y1_0 = y0_0;
3196 y2_1 = y1_1; y1_1 = y0_1;
3197 y2_2 = y1_2; y1_2 = y0_2;
3198 ZXP(out) = outf;
3200 coefs -= 9;
3201 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3202 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3203 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3204 break;
3205 case 2 :
3206 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3207 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3209 out = out0;
3210 LOOP(unit->mRate->mFilterLoops,
3211 outf = y0_0 = b1_0 * y1_0 - y2_0;
3212 outf += y0_1 = b1_1 * y1_1 - y2_1;
3213 ZXP(out) = outf;
3215 outf = y2_0 = b1_0 * y0_0 - y1_0;
3216 outf += y2_1 = b1_1 * y0_1 - y1_1;
3217 ZXP(out) = outf;
3219 outf = y1_0 = b1_0 * y2_0 - y0_0;
3220 outf += y1_1 = b1_1 * y2_1 - y0_1;
3221 ZXP(out) = outf;
3223 LOOP(unit->mRate->mFilterRemain,
3224 outf = y0_0 = b1_0 * y1_0 - y2_0;
3225 outf += y0_1 = b1_1 * y1_1 - y2_1;
3226 y2_0 = y1_0; y1_0 = y0_0;
3227 y2_1 = y1_1; y1_1 = y0_1;
3228 ZXP(out) = outf;
3231 coefs -= 6;
3232 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3233 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3234 break;
3235 case 1 :
3236 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3238 out = out0;
3239 LOOP(unit->mRate->mFilterLoops,
3240 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3242 ZXP(out) = y2_0 = b1_0 * y0_0 - y1_0;
3244 ZXP(out) = y1_0 = b1_0 * y2_0 - y0_0;
3246 LOOP(unit->mRate->mFilterRemain,
3247 ZXP(out) = y0_0 = b1_0 * y1_0 - y2_0;
3248 y2_0 = y1_0; y1_0 = y0_0;
3251 coefs -= 3;
3252 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3253 break;
3254 case 0 :
3255 out = out0;
3256 ZClear(inNumSamples, out);
3257 break;
3260 int32 imax = numpartials >> 2;
3262 for (int i=0; i<imax; ++i) {
3263 y1_0 = *++coefs; y2_0 = *++coefs; b1_0 = *++coefs;
3264 y1_1 = *++coefs; y2_1 = *++coefs; b1_1 = *++coefs;
3265 y1_2 = *++coefs; y2_2 = *++coefs; b1_2 = *++coefs;
3266 y1_3 = *++coefs; y2_3 = *++coefs; b1_3 = *++coefs;
3268 out = out0;
3269 LOOP(unit->mRate->mFilterLoops,
3270 outf = y0_0 = b1_0 * y1_0 - y2_0;
3271 outf += y0_1 = b1_1 * y1_1 - y2_1;
3272 outf += y0_2 = b1_2 * y1_2 - y2_2;
3273 outf += y0_3 = b1_3 * y1_3 - y2_3;
3274 ZXP(out) += outf;
3276 outf = y2_0 = b1_0 * y0_0 - y1_0;
3277 outf += y2_1 = b1_1 * y0_1 - y1_1;
3278 outf += y2_2 = b1_2 * y0_2 - y1_2;
3279 outf += y2_3 = b1_3 * y0_3 - y1_3;
3280 ZXP(out) += outf;
3282 outf = y1_0 = b1_0 * y2_0 - y0_0;
3283 outf += y1_1 = b1_1 * y2_1 - y0_1;
3284 outf += y1_2 = b1_2 * y2_2 - y0_2;
3285 outf += y1_3 = b1_3 * y2_3 - y0_3;
3286 ZXP(out) += outf;
3288 LOOP(unit->mRate->mFilterRemain,
3289 outf = y0_0 = b1_0 * y1_0 - y2_0;
3290 outf += y0_1 = b1_1 * y1_1 - y2_1;
3291 outf += y0_2 = b1_2 * y1_2 - y2_2;
3292 outf += y0_3 = b1_3 * y1_3 - y2_3;
3293 y2_0 = y1_0; y1_0 = y0_0;
3294 y2_1 = y1_1; y1_1 = y0_1;
3295 y2_2 = y1_2; y1_2 = y0_2;
3296 y2_3 = y1_3; y1_3 = y0_3;
3297 ZXP(out) += outf;
3299 coefs -= 12;
3300 *++coefs = y1_0; *++coefs = y2_0; ++coefs;
3301 *++coefs = y1_1; *++coefs = y2_1; ++coefs;
3302 *++coefs = y1_2; *++coefs = y2_2; ++coefs;
3303 *++coefs = y1_3; *++coefs = y2_3; ++coefs;
3307 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3311 void Klank_Dtor(Klank *unit)
3313 RTFree(unit->mWorld, unit->m_coefs);
3316 static void Klank_SetCoefs(Klank *unit);
3318 void Klank_Ctor(Klank *unit)
3320 SETCALC(Klank_next);
3321 unit->m_x1 = unit->m_x2 = 0.f;
3322 Klank_SetCoefs(unit);
3323 ZOUT0(0) = 0.f;
3326 void Klank_SetCoefs(Klank *unit)
3328 int numpartials = (unit->mNumInputs - 4) / 3;
3329 unit->m_numpartials = numpartials;
3331 int numcoefs = ((unit->m_numpartials + 3) & ~3) * 5;
3332 unit->m_coefs = (float*)RTAlloc(unit->mWorld, (numcoefs + unit->mWorld->mBufLength) * sizeof(float));
3333 unit->m_buf = unit->m_coefs + numcoefs;
3335 float freqscale = ZIN0(1) * unit->mRate->mRadiansPerSample;
3336 float freqoffset = ZIN0(2) * unit->mRate->mRadiansPerSample;
3337 float decayscale = ZIN0(3);
3339 float* coefs = unit->m_coefs;
3341 float sampleRate = SAMPLERATE;
3343 for (int i=0,j=4; i<numpartials; ++i,j+=3) {
3344 float w = ZIN0(j) * freqscale + freqoffset;
3345 float level = ZIN0(j+1);
3346 float time = ZIN0(j+2) * decayscale;
3348 float R = time == 0.f ? 0.f : exp(log001/(time * sampleRate));
3349 float twoR = 2.f * R;
3350 float R2 = R * R;
3351 float cost = (twoR * cos(w)) / (1.f + R2);
3353 int k = 20 * (i>>2) + (i & 3);
3354 coefs[k+0] = 0.f; // y1
3355 coefs[k+4] = 0.f; // y2
3356 coefs[k+8] = twoR * cost; // b1
3357 coefs[k+12] = -R2; // b2
3358 coefs[k+16] = level * 0.25; // a0
3359 //Print("coefs %d %g %g %g\n", i, twoR * cost, -R2, ampf * 0.25);
3364 void Klank_next(Klank *unit, int inNumSamples)
3366 float *out0 = ZOUT(0);
3367 float *in0 = ZIN(0);
3369 float *in, *out;
3370 float inf;
3371 float y0_0, y1_0, y2_0, a0_0, b1_0, b2_0;
3372 float y0_1, y1_1, y2_1, a0_1, b1_1, b2_1;
3373 float y0_2, y1_2, y2_2, a0_2, b1_2, b2_2;
3374 float y0_3, y1_3, y2_3, a0_3, b1_3, b2_3;
3376 int32 numpartials = unit->m_numpartials;
3377 int32 imax = numpartials >> 2;
3379 float* coefs = unit->m_coefs + imax * 20;
3381 switch (numpartials & 3) {
3382 case 3 :
3383 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3384 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3385 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3387 in = in0;
3388 out = unit->m_buf - 1;
3389 LooP(unit->mRate->mFilterLoops) {
3390 inf = *++in;
3391 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3392 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3393 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3394 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3396 inf = *++in;
3397 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3398 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3399 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3400 *++out = a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2;
3402 inf = *++in;
3403 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3404 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3405 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3406 *++out = a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2;
3408 LooP(unit->mRate->mFilterRemain) {
3409 inf = *++in;
3410 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3411 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3412 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3413 *++out = a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2;
3414 y2_0 = y1_0; y1_0 = y0_0;
3415 y2_1 = y1_1; y1_1 = y0_1;
3416 y2_2 = y1_2; y1_2 = y0_2;
3418 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3419 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3420 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3421 break;
3422 case 2 :
3423 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3424 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3426 in = in0;
3427 out = unit->m_buf - 1;
3428 LooP(unit->mRate->mFilterLoops) {
3429 inf = *++in;
3430 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3431 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3432 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3434 inf = *++in;
3435 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3436 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3437 *++out = a0_0 * y2_0 + a0_1 * y2_1;
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 *++out = a0_0 * y1_0 + a0_1 * y1_1;
3444 LooP(unit->mRate->mFilterRemain) {
3445 inf = *++in;
3446 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3447 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3448 *++out = a0_0 * y0_0 + a0_1 * y0_1;
3449 y2_0 = y1_0; y1_0 = y0_0;
3450 y2_1 = y1_1; y1_1 = y0_1;
3452 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3453 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3454 break;
3455 case 1 :
3456 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3458 //Print("rcoefs %g %g %g %g %g\n", y1_0, y2_0, b1_0, b2_0, a0_0);
3459 in = in0;
3460 out = unit->m_buf - 1;
3461 LooP(unit->mRate->mFilterLoops) {
3462 inf = *++in;
3463 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3464 *++out = a0_0 * y0_0;
3466 inf = *++in;
3467 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3468 *++out = a0_0 * y2_0;
3470 inf = *++in;
3471 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3472 *++out = a0_0 * y1_0;
3473 //Print("out %g %g %g\n", y0_0, y2_0, y1_0);
3475 LooP(unit->mRate->mFilterRemain) {
3476 inf = *++in;
3477 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3478 *++out = a0_0 * y0_0;
3479 y2_0 = y1_0; y1_0 = y0_0;
3480 //Print("out %g\n", y0_0);
3483 coefs[0] = y1_0; coefs[4] = y2_0;
3485 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3486 break;
3487 case 0 :
3488 out = unit->m_buf - 1;
3489 LooP(inNumSamples) { *++out = 0.f; }
3490 break;
3493 coefs = unit->m_coefs;
3495 for (int i=0; i<imax; ++i) {
3496 y1_0 = coefs[0]; y2_0 = coefs[4]; b1_0 = coefs[8]; b2_0 = coefs[12]; a0_0 = coefs[16];
3497 y1_1 = coefs[1]; y2_1 = coefs[5]; b1_1 = coefs[9]; b2_1 = coefs[13]; a0_1 = coefs[17];
3498 y1_2 = coefs[2]; y2_2 = coefs[6]; b1_2 = coefs[10]; b2_2 = coefs[14]; a0_2 = coefs[18];
3499 y1_3 = coefs[3]; y2_3 = coefs[7]; b1_3 = coefs[11]; b2_3 = coefs[15]; a0_3 = coefs[19];
3501 in = in0;
3502 out = unit->m_buf - 1;
3503 LooP(unit->mRate->mFilterLoops) {
3504 inf = *++in;
3505 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3506 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3507 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3508 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3509 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3511 inf = *++in;
3512 y2_0 = inf + b1_0 * y0_0 + b2_0 * y1_0;
3513 y2_1 = inf + b1_1 * y0_1 + b2_1 * y1_1;
3514 y2_2 = inf + b1_2 * y0_2 + b2_2 * y1_2;
3515 y2_3 = inf + b1_3 * y0_3 + b2_3 * y1_3;
3516 *++out += a0_0 * y2_0 + a0_1 * y2_1 + a0_2 * y2_2 + a0_3 * y2_3;
3518 inf = *++in;
3519 y1_0 = inf + b1_0 * y2_0 + b2_0 * y0_0;
3520 y1_1 = inf + b1_1 * y2_1 + b2_1 * y0_1;
3521 y1_2 = inf + b1_2 * y2_2 + b2_2 * y0_2;
3522 y1_3 = inf + b1_3 * y2_3 + b2_3 * y0_3;
3523 *++out += a0_0 * y1_0 + a0_1 * y1_1 + a0_2 * y1_2 + a0_3 * y1_3;
3525 LooP(unit->mRate->mFilterRemain) {
3526 inf = *++in;
3527 y0_0 = inf + b1_0 * y1_0 + b2_0 * y2_0;
3528 y0_1 = inf + b1_1 * y1_1 + b2_1 * y2_1;
3529 y0_2 = inf + b1_2 * y1_2 + b2_2 * y2_2;
3530 y0_3 = inf + b1_3 * y1_3 + b2_3 * y2_3;
3531 *++out += a0_0 * y0_0 + a0_1 * y0_1 + a0_2 * y0_2 + a0_3 * y0_3;
3532 y2_0 = y1_0; y1_0 = y0_0;
3533 y2_1 = y1_1; y1_1 = y0_1;
3534 y2_2 = y1_2; y1_2 = y0_2;
3535 y2_3 = y1_3; y1_3 = y0_3;
3537 coefs[0] = zapgremlins(y1_0); coefs[4] = zapgremlins(y2_0);
3538 coefs[1] = zapgremlins(y1_1); coefs[5] = zapgremlins(y2_1);
3539 coefs[2] = zapgremlins(y1_2); coefs[6] = zapgremlins(y2_2);
3540 coefs[3] = zapgremlins(y1_3); coefs[7] = zapgremlins(y2_3);
3541 coefs += 20;
3544 float x0;
3545 float x1 = unit->m_x1;
3546 float x2 = unit->m_x2;
3548 in = unit->m_buf - 1;
3549 out = out0;
3550 LooP(unit->mRate->mFilterLoops) {
3551 x0 = *++in;
3552 *++out = x0 - x2;
3553 x2 = *++in;
3554 *++out = x2 - x1;
3555 x1 = *++in;
3556 *++out = x1 - x0;
3558 LooP(unit->mRate->mFilterRemain) {
3559 x0 = *++in;
3560 *++out = x0 - x2;
3561 x2 = x1;
3562 x1 = x0;
3565 unit->m_x1 = x1;
3566 unit->m_x2 = x2;
3569 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3571 static void normalize_samples(int size, float* data, float peak)
3573 float maxamp = 0.f;
3574 for (int i=0; i<size; ++i) {
3575 float absamp = std::abs(data[i]);
3576 if (absamp > maxamp) maxamp = absamp;
3578 if (maxamp != 0.f && maxamp != peak) {
3579 float ampfac = peak / maxamp;
3580 for (int i=0; i<size; ++i) {
3581 data[i] *= ampfac;
3586 static void normalize_wsamples(int size, float* data, float peak)
3588 float maxamp = 0.f;
3589 for (int i=0; i<size; i+=2) {
3590 float absamp = std::abs(data[i] + data[i+1]);
3591 if (absamp > maxamp) maxamp = absamp;
3593 if (maxamp != 0.f && maxamp != peak) {
3594 float ampfac = peak / maxamp;
3595 for (int i=0; i<size; ++i) {
3596 data[i] *= ampfac;
3601 static void add_partial(int size, float *data, double partial, double amp, double phase)
3603 if (amp == 0.0) return;
3604 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size;
3605 for (int i=0; i<size; ++i) {
3606 data[i] += amp * sin(phase);
3607 phase += w;
3611 static void add_wpartial(int size, float *data, double partial, double amp, double phase)
3613 if (amp == 0.0) return;
3614 int size2 = size >> 1;
3615 double w = (partial * 2.0 * 3.1415926535897932384626433832795) / (double)size2;
3616 double cur = amp * sin(phase);
3617 phase += w;
3618 for (int i=0; i<size; i+=2) {
3619 double next = amp * sin(phase);
3620 data[i] += 2 * cur - next;
3621 data[i+1] += next - cur;
3622 cur = next;
3623 phase += w;
3627 static void add_chebyshev(int size, float *data, double partial, double amp)
3629 if (amp == 0.0) return;
3630 double w = 2.0 / (double)size;
3631 double phase = -1.0;
3632 double offset = -amp * cos (partial * pi2);
3633 for (int i=0; i<size; ++i) {
3634 data[i] += amp * cos (partial * acos (phase)) - offset;
3635 phase += w;
3639 static void add_wchebyshev(int size, float *data, double partial, double amp)
3641 if (amp == 0.0) return;
3642 int size2 = size >> 1;
3643 double w = 2.0 / (double)size2;
3644 double phase = -1.0;
3645 double offset = -amp * cos (partial * pi2);
3646 double cur = amp * cos (partial * acos (phase))-offset;
3647 phase += w;
3648 for (int i=0; i<size; i+=2) {
3649 double next = amp * cos (partial * acos (phase)) - offset;
3650 data[i] += 2 * cur - next;
3651 data[i+1] += next - cur;
3652 cur = next;
3653 phase += w;
3657 static void cantorFill(int size, float *data) // long offset, double amp)
3659 // if (amp == 0.0) return;
3660 for (int i=0; i<(size); ++i) {
3661 int j = i;
3662 float flag = 1.f;
3663 while ((j > 0) && (flag == 1.f) ) {
3664 if (j % 3 == 1) { flag = 0.f; }
3665 j = j / 3;
3667 if(flag) { data[i] += 1.f; }
3672 enum {
3673 flag_Normalize = 1,
3674 flag_Wavetable = 2,
3675 flag_Clear = 4
3678 void ChebyFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3680 if (buf->channels != 1) return;
3682 int flags = msg->geti();
3684 int size = buf->samples;
3685 int byteSize = size * sizeof(float);
3686 float *data = (float*)malloc(byteSize);
3688 if (flags & flag_Clear) Fill(size, data, 0.);
3689 else memcpy(data, buf->data, byteSize);
3691 for (int partial=1; msg->remain(); partial++) {
3692 double amp = msg->getf();
3693 if (flags & flag_Wavetable) add_wchebyshev(size, data, partial, amp);
3694 else add_chebyshev(size, data, partial, amp);
3697 if (flags & flag_Normalize) {
3698 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3699 else normalize_samples(size, data, 1.);
3702 memcpy(buf->data, data, byteSize);
3703 free(data);
3707 void SineFill1(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3709 if (buf->channels != 1) return;
3711 int flags = msg->geti();
3713 int size = buf->samples;
3714 int byteSize = size * sizeof(float);
3715 float *data = (float*)malloc(byteSize);
3717 if (flags & flag_Clear) Fill(size, data, 0.);
3718 else memcpy(data, buf->data, byteSize);
3720 for (int partial=1; msg->remain(); partial++) {
3721 double amp = msg->getf();
3722 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3723 else add_partial(size, data, partial, amp, 0.);
3726 if (flags & flag_Normalize) {
3727 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3728 else normalize_samples(size, data, 1.);
3731 memcpy(buf->data, data, byteSize);
3732 free(data);
3735 void SineFill2(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3737 if (buf->channels != 1) return;
3739 int flags = msg->geti();
3741 int size = buf->samples;
3742 int byteSize = size * sizeof(float);
3743 float *data = (float*)malloc(byteSize);
3745 if (flags & flag_Clear) Fill(size, data, 0.);
3746 else memcpy(data, buf->data, byteSize);
3748 while (msg->remain()) {
3749 double partial = msg->getf();
3750 double amp = msg->getf();
3751 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, 0.);
3752 else add_partial(size, data, partial, amp, 0.);
3754 if (flags & flag_Normalize) {
3755 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3756 else normalize_samples(size, data, 1.);
3759 memcpy(buf->data, data, byteSize);
3760 free(data);
3763 void SineFill3(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3765 if (buf->channels != 1) return;
3767 int flags = msg->geti();
3769 int size = buf->samples;
3770 int byteSize = size * sizeof(float);
3771 float *data = (float*)malloc(byteSize);
3773 if (flags & flag_Clear) Fill(size, data, 0.);
3774 else memcpy(data, buf->data, byteSize);
3776 while (msg->remain()) {
3777 double partial = msg->getf();
3778 double amp = msg->getf();
3779 double phase = msg->getf();
3780 if (flags & flag_Wavetable) add_wpartial(size, data, partial, amp, phase);
3781 else add_partial(size, data, partial, amp, phase);
3783 if (flags & flag_Normalize) {
3784 if (flags & flag_Wavetable) normalize_wsamples(size, data, 1.);
3785 else normalize_samples(size, data, 1.);
3788 memcpy(buf->data, data, byteSize);
3789 free(data);
3792 void NormalizeBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3794 float newmax;
3795 if(msg->remain() != 0){
3796 newmax = msg->getf();
3797 }else{
3798 newmax = 1.f;
3800 float *data = buf->data;
3801 int size = buf->samples;
3802 normalize_samples(size, data, newmax);
3805 void NormalizeWaveBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3807 float newmax;
3808 if(msg->remain() != 0){
3809 newmax = msg->getf();
3810 }else{
3811 newmax = 1.f;
3813 float *data = buf->data;
3814 int size = buf->samples;
3815 normalize_wsamples(size, data, newmax);
3818 void CopyBuf(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3820 int frames1 = buf->frames;
3821 int channels1 = buf->channels;
3823 int toPos = msg->geti();
3824 uint32 bufnum2 = msg->geti();
3825 int fromPos = msg->geti();
3826 int length = msg->geti();
3828 if (bufnum2 >= world->mNumSndBufs) bufnum2 = 0;
3829 SndBuf* buf2 = world->mSndBufs + bufnum2;
3830 int frames2 = buf2->frames;
3831 int channels2 = buf2->channels;
3833 if (channels1 != channels2) return;
3835 fromPos = sc_clip(fromPos, 0, frames2-1);
3836 toPos = sc_clip(toPos, 0, frames1-1);
3838 int maxLength = sc_min(frames2 - fromPos, frames1 - toPos);
3839 if (length < 0) {
3840 length = maxLength;
3841 } else {
3842 length = sc_min(length, maxLength);
3845 if (length <= 0) return;
3847 int numbytes = length * sizeof(float) * channels1;
3848 float *data1 = buf->data + toPos * channels1;
3849 float *data2 = buf2->data + fromPos * channels2;
3851 if ((((char*)data1 + numbytes) > (char*)data2) || (((char*)data2 + numbytes) > (char*)data1)) {
3852 memmove(data1, data2, numbytes);
3853 } else {
3854 memcpy(data1, data2, numbytes);
3858 void CantorFill(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
3860 float *data = buf->data;
3861 int size = buf->samples;
3862 // double offset = msg->getf();
3863 // double amp = msg->getf();
3864 // long offs = (long) offset;
3865 cantorFill(size, data);
3870 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3872 PluginLoad(Osc)
3874 ft = inTable;
3876 DefineSimpleUnit(DegreeToKey);
3877 DefineSimpleUnit(Select);
3878 DefineSimpleUnit(TWindex);
3879 DefineSimpleUnit(Index);
3880 DefineSimpleUnit(IndexL);
3881 DefineSimpleUnit(FoldIndex);
3882 DefineSimpleUnit(WrapIndex);
3883 DefineSimpleUnit(IndexInBetween);
3884 DefineSimpleUnit(DetectIndex);
3885 DefineSimpleUnit(Shaper);
3886 DefineSimpleUnit(SigOsc);
3887 DefineSimpleUnit(FSinOsc);
3888 DefineSimpleUnit(SinOsc);
3889 DefineSimpleUnit(SinOscFB);
3890 DefineSimpleUnit(VOsc);
3891 DefineSimpleUnit(VOsc3);
3892 DefineSimpleUnit(Osc);
3893 DefineSimpleUnit(OscN);
3894 DefineSimpleUnit(COsc);
3895 DefineSimpleUnit(Formant);
3896 DefineSimpleUnit(Blip);
3897 DefineSimpleUnit(Saw);
3898 DefineSimpleUnit(Pulse);
3899 DefineDtorUnit(Klang);
3900 DefineDtorUnit(Klank);
3902 DefineBufGen("cheby", ChebyFill);
3903 DefineBufGen("sine1", SineFill1);
3904 DefineBufGen("sine2", SineFill2);
3905 DefineBufGen("sine3", SineFill3);
3906 DefineBufGen("normalize", NormalizeBuf);
3907 DefineBufGen("wnormalize", NormalizeWaveBuf);
3908 DefineBufGen("copy", CopyBuf);
3909 DefineBufGen("cantorFill", CantorFill);
3912 //////////////////////////////////////////////////////////////////////////////////////////////////