FFT: Prevent user from attempting hops smaller than SC's block size
[supercollider.git] / server / plugins / DemandUGens.cpp
blobd6da29c3e3efbbb71df71237868f42920ece1c33
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 <cstdio>
23 #include <cmath>
24 #include <limits>
26 using std::floor;
27 using std::numeric_limits;
29 static InterfaceTable *ft;
31 struct Demand : public Unit
33 float m_prevtrig;
34 float m_prevreset;
35 float *m_prevout;
36 float **m_out;
39 struct Duty : public Unit
41 float m_count;
42 float m_prevreset;
43 float m_prevout;
46 struct DemandEnvGen : public Unit
48 float m_phase;
49 float m_prevreset;
51 double m_a1, m_a2, m_b1, m_y1, m_y2, m_grow, m_level, m_endLevel, m_curve;
52 int m_shape;
53 bool m_release, m_running;
56 struct TDuty : public Unit
58 float m_count;
59 float m_prevreset;
63 struct Dseries : public Unit
65 double m_repeats;
66 int32 m_repeatCount;
67 double m_value;
68 double m_step;
71 struct Dgeom : public Unit
73 double m_repeats;
74 int32 m_repeatCount;
75 double m_value;
76 double m_grow;
79 struct Dwhite : public Unit
81 double m_repeats;
82 int32 m_repeatCount;
83 float m_lo;
84 float m_range;
87 struct Dbrown : public Unit
89 double m_repeats;
90 int32 m_repeatCount;
91 float m_lo;
92 float m_hi;
93 float m_step;
94 float m_val;
97 struct Diwhite : public Unit
99 double m_repeats;
100 int32 m_repeatCount;
101 int32 m_lo;
102 int32 m_range;
105 struct Dibrown : public Unit
107 double m_repeats;
108 int32 m_repeatCount;
109 int32 m_lo;
110 int32 m_hi;
111 int32 m_step;
112 int32 m_val;
115 struct Dseq : public Unit
117 double m_repeats;
118 int32 m_repeatCount;
119 int32 m_index;
120 bool m_needToResetChild;
123 struct Dshuf : public Unit
125 double m_repeats;
126 int32 m_repeatCount;
127 int32 m_index;
128 bool m_needToResetChild;
129 int32 *m_indices;
132 struct Dbufrd : public Unit
134 float m_fbufnum;
135 SndBuf *m_buf;
138 struct Dbufwr : public Unit
140 float m_fbufnum;
141 SndBuf *m_buf;
144 struct Dser : public Dseq
148 struct Drand : public Dseq
152 struct Dxrand : public Dseq
156 struct Dwrand : public Dseq
158 int32 m_weights_size;
161 struct Dswitch1 : public Unit
165 struct Dswitch : public Unit
167 int m_index;
170 struct Dstutter : public Unit
172 double m_repeats;
173 double m_repeatCount;
174 float m_value;
177 struct Donce : public Unit
179 int m_bufcounter;
180 float m_prev;
183 struct Dpoll : public Unit
185 char *m_id_string;
186 bool m_mayprint;
187 float m_id;
190 struct Dreset : public Unit
192 float prev_reset;
195 extern "C"
199 void Demand_Ctor(Demand *unit);
200 void Demand_Dtor(Demand *unit);
201 void Demand_next_aa(Demand *unit, int inNumSamples);
202 void Demand_next_ak(Demand *unit, int inNumSamples);
203 void Demand_next_ka(Demand *unit, int inNumSamples);
205 void Duty_Ctor(Duty *unit);
206 void Duty_next_da(Duty *unit, int inNumSamples);
207 void Duty_next_dk(Duty *unit, int inNumSamples);
208 void Duty_next_dd(Duty *unit, int inNumSamples);
210 void TDuty_Ctor(TDuty *unit);
211 void TDuty_next_da(TDuty *unit, int inNumSamples);
212 void TDuty_next_dk(TDuty *unit, int inNumSamples);
213 void TDuty_next_dd(TDuty *unit, int inNumSamples);
215 void DemandEnvGen_Ctor(DemandEnvGen *unit);
216 void DemandEnvGen_next_k(DemandEnvGen *unit, int inNumSamples);
217 void DemandEnvGen_next_a(DemandEnvGen *unit, int inNumSamples);
219 void Dseries_Ctor(Dseries *unit);
220 void Dseries_next(Dseries *unit, int inNumSamples);
222 void Dgeom_Ctor(Dgeom *unit);
223 void Dgeom_next(Dgeom *unit, int inNumSamples);
225 void Dwhite_Ctor(Dwhite *unit);
226 void Dwhite_next(Dwhite *unit, int inNumSamples);
228 void Dbrown_Ctor(Dbrown *unit);
229 void Dbrown_next(Dbrown *unit, int inNumSamples);
231 void Diwhite_Ctor(Diwhite *unit);
232 void Diwhite_next(Diwhite *unit, int inNumSamples);
234 void Dibrown_Ctor(Dibrown *unit);
235 void Dibrown_next(Dibrown *unit, int inNumSamples);
237 void Dseq_Ctor(Dseq *unit);
238 void Dseq_next(Dseq *unit, int inNumSamples);
240 void Dshuf_Ctor(Dshuf *unit);
241 void Dshuf_Dtor(Dshuf *unit);
242 void Dshuf_next(Dshuf *unit, int inNumSamples);
245 void Dbufrd_Ctor(Dbufrd *unit);
246 void Dbufrd_next(Dbufrd *unit, int inNumSamples);
248 void Dbufwr_Ctor(Dbufwr *unit);
249 void Dbufwr_next(Dbufwr *unit, int inNumSamples);
251 void Dser_Ctor(Dser *unit);
252 void Dser_next(Dser *unit, int inNumSamples);
254 void Drand_Ctor(Drand *unit);
255 void Drand_next(Drand *unit, int inNumSamples);
257 void Dxrand_Ctor(Dxrand *unit);
258 void Dxrand_next(Dxrand *unit, int inNumSamples);
260 void Dwrand_Ctor(Dwrand *unit);
261 void Dwrand_next(Dwrand *unit, int inNumSamples);
263 void Dswitch1_Ctor(Dswitch1 *unit);
264 void Dswitch1_next(Dswitch1 *unit, int inNumSamples);
266 void Dswitch_Ctor(Dswitch *unit);
267 void Dswitch_next(Dswitch *unit, int inNumSamples);
269 void Dstutter_Ctor(Dstutter *unit);
270 void Dstutter_next(Dstutter *unit, int inNumSamples);
272 void Dpoll_Ctor(Dpoll *unit);
273 void Dpoll_Ctor(Dpoll *unit);
274 void Dpoll_next(Dpoll *unit, int inNumSamples);
276 void Dreset_Ctor(Dreset *unit);
277 void Dreset_next(Dreset *unit, int inNumSamples);
279 void Donce_Ctor(Donce *unit);
280 void Donce_next(Donce *unit, int inNumSamples);
286 void Demand_next_aa(Demand *unit, int inNumSamples)
288 float *trig = ZIN(0);
289 float *reset = ZIN(1);
291 float** out = unit->m_out;
292 float* prevout = unit->m_prevout;
294 for (int i=0; i<unit->mNumOutputs; ++i) {
295 out[i] = OUT(i);
298 float prevtrig = unit->m_prevtrig;
299 float prevreset = unit->m_prevreset;
301 //Print("Demand_next_aa %d %g\n", inNumSamples, prevtrig);
302 for (int i=0; i<inNumSamples; ++i) {
303 float ztrig = ZXP(trig);
304 float zreset = ZXP(reset);
305 if (zreset > 0.f && prevreset <= 0.f) {
306 for (int j=2; j<unit->mNumInputs; ++j) {
307 RESETINPUT(j);
310 if (ztrig > 0.f && prevtrig <= 0.f) {
311 //Print("triggered\n");
312 for (int j=2, k=0; j<unit->mNumInputs; ++j, ++k) {
313 float x = DEMANDINPUT_A(j, i + 1);
314 //printf("in %d %g\n", k, x);
315 if (sc_isnan(x)) { x = prevout[k]; unit->mDone = true; }
316 else prevout[k] = x;
317 out[k][i] = x;
319 } else {
320 for (int j=2, k=0; j<unit->mNumInputs; ++j, ++k) {
321 out[k][i] = prevout[k];
324 prevtrig = ztrig;
325 prevreset = zreset;
328 unit->m_prevtrig = prevtrig;
329 unit->m_prevreset = prevreset;
333 void Demand_next_ak(Demand *unit, int inNumSamples)
335 float *trig = ZIN(0);
336 float zreset = IN0(1);
338 float** out = unit->m_out;
339 float *prevout = unit->m_prevout;
341 for (int i=0; i<unit->mNumOutputs; ++i) {
342 out[i] = OUT(i);
345 float prevtrig = unit->m_prevtrig;
346 float prevreset = unit->m_prevreset;
348 for (int i=0; i<inNumSamples; ++i) {
349 float ztrig = ZXP(trig);
350 if (zreset > 0.f && prevreset <= 0.f) {
351 for (int j=2; j<unit->mNumInputs; ++j) {
352 RESETINPUT(j);
356 if (ztrig > 0.f && prevtrig <= 0.f) {
357 for (int j=2, k=0; j<unit->mNumInputs; ++j, ++k) {
358 float x = DEMANDINPUT_A(j, i + 1);
359 if (sc_isnan(x)) { x = prevout[k]; unit->mDone = true; }
360 else prevout[k] = x;
361 out[k][i] = x;
364 } else {
365 for (int j=2, k=0; j<unit->mNumInputs; ++j, ++k) {
366 out[k][i] = prevout[k];
370 prevtrig = ztrig;
371 prevreset = zreset;
374 unit->m_prevtrig = prevtrig;
375 unit->m_prevreset = prevreset;
379 void Demand_next_ka(Demand *unit, int inNumSamples)
381 float ztrig = IN0(0);
382 float *reset = ZIN(1);
384 float** out = unit->m_out;
385 float *prevout = unit->m_prevout;
387 for (int i=0; i<unit->mNumOutputs; ++i) {
388 out[i] = OUT(i);
391 float prevtrig = unit->m_prevtrig;
392 float prevreset = unit->m_prevreset;
394 for (int i=0; i<inNumSamples; ++i) {
395 float zreset = ZXP(reset);
396 if (zreset > 0.f && prevreset <= 0.f) {
397 for (int j=2; j<unit->mNumInputs; ++j) {
398 RESETINPUT(j);
401 if (ztrig > 0.f && prevtrig <= 0.f) {
402 for (int j=2, k=0; j<unit->mNumInputs; ++j, ++k) {
403 float x = DEMANDINPUT_A(j, i + 1);
404 if (sc_isnan(x)) { x = prevout[k]; unit->mDone = true; }
405 else prevout[k] = x;
406 out[k][i] = x;
409 prevtrig = ztrig;
410 prevreset = zreset;
413 unit->m_prevtrig = prevtrig;
414 unit->m_prevreset = prevreset;
419 void Demand_Ctor(Demand *unit)
421 //Print("Demand_Ctor\n");
422 if (INRATE(0) == calc_FullRate) {
423 if (INRATE(1) == calc_FullRate) {
424 SETCALC(Demand_next_aa);
425 } else {
426 SETCALC(Demand_next_ak);
428 } else {
429 if (INRATE(1) == calc_FullRate) {
430 SETCALC(Demand_next_ka);
431 } else {
432 SETCALC(Demand_next_aa);
436 unit->m_prevout = (float*) RTAlloc(unit->mWorld, unit->mNumOutputs * sizeof(float));
437 unit->m_out = (float**) RTAlloc(unit->mWorld, unit->mNumOutputs * sizeof(float*));
439 //Print("Demand_Ctor calc %p\n", unit->mCalcFunc);
440 unit->m_prevtrig = 0.f;
441 unit->m_prevreset = 0.f;
442 for (int i=0; i<unit->mNumOutputs; ++i) {
443 unit->m_prevout[i] = 0.f;
444 OUT0(i) = 0.f;
448 void Demand_Dtor(Demand* unit)
450 if(unit->m_prevout) RTFree(unit->mWorld, unit->m_prevout);
451 if(unit->m_out) RTFree(unit->mWorld, unit->m_out);
455 /////////////////////////////////////////////////////////////////////////////
457 enum {
458 duty_dur,
459 duty_reset,
460 duty_doneAction,
461 duty_level
464 void Duty_next_da(Duty *unit, int inNumSamples)
467 float *reset = ZIN(duty_reset);
469 float *out = OUT(0);
470 float prevout = unit->m_prevout;
471 float count = unit->m_count;
472 float prevreset = unit->m_prevreset;
473 float sr = (float) SAMPLERATE;
475 for (int i=0; i<inNumSamples; ++i) {
477 float zreset = ZXP(reset);
478 if (zreset > 0.f && prevreset <= 0.f) {
480 RESETINPUT(duty_level);
481 RESETINPUT(duty_dur);
482 count = 0.f;
484 if (count <= 0.f) {
485 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
486 if(sc_isnan(count)) {
487 int doneAction = (int)ZIN0(duty_doneAction);
488 DoneAction(doneAction, unit);
490 float x = DEMANDINPUT_A(duty_level, i + 1);
491 //printf("in %d\n", count);
492 if(sc_isnan(x)) {
493 x = prevout;
494 int doneAction = (int)ZIN0(duty_doneAction);
495 DoneAction(doneAction, unit);
496 } else {
497 prevout = x;
499 out[i] = x;
501 } else {
502 count--;
503 out[i] = prevout;
506 prevreset = zreset;
509 unit->m_count = count;
510 unit->m_prevreset = prevreset;
511 unit->m_prevout = prevout;
514 void Duty_next_dk(Duty *unit, int inNumSamples)
516 float zreset = ZIN0(duty_reset);
518 float *out = OUT(0);
519 float prevout = unit->m_prevout;
520 float count = unit->m_count;
521 float prevreset = unit->m_prevreset;
522 float sr = (float) SAMPLERATE;
524 for (int i=0; i<inNumSamples; ++i) {
526 if (zreset > 0.f && prevreset <= 0.f) {
528 RESETINPUT(duty_level);
529 RESETINPUT(duty_dur);
530 count = 0.f;
532 if (count <= 0.f) {
533 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
534 if(sc_isnan(count)) {
535 int doneAction = (int)ZIN0(duty_doneAction);
536 DoneAction(doneAction, unit);
539 float x = DEMANDINPUT_A(duty_level, i + 1);
540 if(sc_isnan(x)) {
541 x = prevout;
542 int doneAction = (int)ZIN0(duty_doneAction);
543 DoneAction(doneAction, unit);
544 } else {
545 prevout = x;
548 out[i] = x;
550 } else {
552 out[i] = prevout;
554 count--;
555 prevreset = zreset;
558 unit->m_count = count;
559 unit->m_prevreset = prevreset;
560 unit->m_prevout = prevout;
565 void Duty_next_dd(Duty *unit, int inNumSamples)
567 float *out = OUT(0);
568 float prevout = unit->m_prevout;
569 float count = unit->m_count;
570 float reset = unit->m_prevreset;
571 float sr = (float) SAMPLERATE;
573 for (int i=0; i<inNumSamples; ++i) {
575 if (reset <= 0.f) {
576 RESETINPUT(duty_level);
577 RESETINPUT(duty_dur);
578 count = 0.f;
579 reset = DEMANDINPUT_A(duty_reset, i + 1) * sr + reset;
580 } else {
581 reset--;
583 if (count <= 0.f) {
584 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
585 if(sc_isnan(count)) {
586 int doneAction = (int)ZIN0(duty_doneAction);
587 DoneAction(doneAction, unit);
589 float x = DEMANDINPUT_A(duty_level, i + 1);
590 //printf("in %d %g\n", k, x);
591 if(sc_isnan(x)) {
592 x = prevout;
593 int doneAction = (int)ZIN0(duty_doneAction);
594 DoneAction(doneAction, unit);
595 } else {
596 prevout = x;
600 out[i] = prevout;
601 count--;
604 unit->m_count = count;
605 unit->m_prevreset = reset;
606 unit->m_prevout = prevout;
610 void Duty_Ctor(Duty *unit)
612 if (INRATE(duty_reset) == calc_FullRate) {
614 SETCALC(Duty_next_da);
615 unit->m_prevreset = 0.f;
617 } else {
618 if(INRATE(duty_reset) == calc_DemandRate) {
619 SETCALC(Duty_next_dd);
620 unit->m_prevreset = DEMANDINPUT(duty_reset) * SAMPLERATE;
621 } else {
622 SETCALC(Duty_next_dk);
623 unit->m_prevreset = 0.f;
627 unit->m_count = DEMANDINPUT(duty_dur) * SAMPLERATE - 1;
628 unit->m_prevout = DEMANDINPUT(duty_level);
629 OUT0(0) = unit->m_prevout;
634 /////////////////////////////////////////////////////////////////////////////
636 enum {
637 d_env_level,
638 d_env_dur,
639 d_env_shape,
640 d_env_curve,
641 d_env_gate,
642 d_env_reset,
643 d_env_levelScale,
644 d_env_levelBias,
645 d_env_timeScale,
646 d_env_doneAction
650 enum {
651 shape_Step,
652 shape_Linear,
653 shape_Exponential,
654 shape_Sine,
655 shape_Welch,
656 shape_Curve,
657 shape_Squared,
658 shape_Cubed,
659 shape_Sustain = 9999
664 void DemandEnvGen_next_k(DemandEnvGen *unit, int inNumSamples)
667 float zreset = ZIN0(d_env_reset);
670 float *out = ZOUT(0);
671 double level = unit->m_level;
672 float phase = unit->m_phase;
673 double curve = unit->m_curve;
674 bool release = unit->m_release;
675 bool running = unit->m_running;
676 int shape = unit->m_shape;
678 // printf("phase %f level %f \n", phase, level);
680 for (int i=0; i<inNumSamples; ++i) {
684 if (zreset > 0.f && unit->m_prevreset <= 0.f) {
685 //printf("reset: %f %f \n", zreset, unit->m_prevreset);
686 RESETINPUT(d_env_level);
687 RESETINPUT(d_env_dur);
688 RESETINPUT(d_env_shape);
689 RESETINPUT(d_env_curve);
691 if(zreset <= 1.f) {
692 DEMANDINPUT(d_env_level); // remove first level
693 } else {
694 level = DEMANDINPUT(d_env_level); // jump to first level
697 release = false;
698 running = true;
699 phase = 0.f;
704 if (phase <= 0.f && running) {
706 // was a release during last segment?
707 if(release) {
709 running = false;
710 release = false;
711 // printf("release: %f %f \n", phase, level);
712 int doneAction = (int)ZIN0(d_env_doneAction);
713 DoneAction(doneAction, unit);
715 } else {
719 // new time
721 float dur = DEMANDINPUT(d_env_dur);
722 // printf("dur: %f \n", dur);
723 if(sc_isnan(dur)) {
724 release = true;
725 running = false;
726 phase = numeric_limits<float>::max();
727 } else {
728 phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
732 // new shape
734 float count;
735 shape = (int)DEMANDINPUT(d_env_shape);
736 curve = DEMANDINPUT(d_env_curve);
739 if (sc_isnan(curve)) curve = unit->m_shape;
740 if (phase <= 1.f) {
741 shape = 1; // shape_Linear
742 count = 1.f;
743 } else {
744 count = phase;
746 if(dur * 0.5f < SAMPLEDUR) shape = 1;
748 //printf("shape: %i, curve: %f, dur: %f \n", shape, curve, dur);
751 // new end level
753 double endLevel = DEMANDINPUT(d_env_level);
754 // printf("levels: %f %f\n", level, endLevel);
755 if (sc_isnan(endLevel)) {
756 endLevel = unit->m_endLevel;
757 release = true;
758 phase = 0.f;
759 shape = 0;
760 } else {
761 endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
762 unit->m_endLevel = endLevel;
766 // calculate shape parameters
768 switch (shape) {
769 case shape_Step : {
770 level = endLevel;
771 } break;
772 case shape_Linear : {
773 unit->m_grow = (endLevel - level) / count;
774 } break;
775 case shape_Exponential : {
776 unit->m_grow = pow(endLevel / level, 1.0 / count);
777 } break;
778 case shape_Sine : {
779 double w = pi / count;
781 unit->m_a2 = (endLevel + level) * 0.5;
782 unit->m_b1 = 2. * cos(w);
783 unit->m_y1 = (endLevel - level) * 0.5;
784 unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
785 level = unit->m_a2 - unit->m_y1;
786 } break;
787 case shape_Welch : {
788 double w = (pi * 0.5) / count;
790 unit->m_b1 = 2. * cos(w);
792 if (endLevel >= level) {
793 unit->m_a2 = level;
794 unit->m_y1 = 0.;
795 unit->m_y2 = -sin(w) * (endLevel - level);
796 } else {
797 unit->m_a2 = endLevel;
798 unit->m_y1 = level - endLevel;
799 unit->m_y2 = cos(w) * (level - endLevel);
801 level = unit->m_a2 + unit->m_y1;
802 } break;
803 case shape_Curve : {
804 if (fabs(curve) < 0.001) {
805 unit->m_shape = 1; // shape_Linear
806 unit->m_grow = (endLevel - level) / count;
807 } else {
808 double a1 = (endLevel - level) / (1.0 - exp(curve));
809 unit->m_a2 = level + a1;
810 unit->m_b1 = a1;
811 unit->m_grow = exp(curve / count);
813 } break;
814 case shape_Squared : {
815 unit->m_y1 = sqrt(level);
816 unit->m_y2 = sqrt(endLevel);
817 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
818 } break;
819 case shape_Cubed : {
820 unit->m_y1 = pow(level, 0.33333333);
821 unit->m_y2 = pow(endLevel, 0.33333333);
822 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
823 } break;
830 if(running) {
832 switch (shape) {
833 case shape_Step : {
834 } break;
835 case shape_Linear : {
836 double grow = unit->m_grow;
837 //Print("level %g\n", level);
838 level += grow;
839 } break;
840 case shape_Exponential : {
841 double grow = unit->m_grow;
842 level *= grow;
843 } break;
844 case shape_Sine : {
845 double a2 = unit->m_a2;
846 double b1 = unit->m_b1;
847 double y2 = unit->m_y2;
848 double y1 = unit->m_y1;
849 double y0 = b1 * y1 - y2;
850 level = a2 - y0;
851 y2 = y1;
852 y1 = y0;
853 unit->m_y1 = y1;
854 unit->m_y2 = y2;
855 } break;
856 case shape_Welch : {
857 double a2 = unit->m_a2;
858 double b1 = unit->m_b1;
859 double y2 = unit->m_y2;
860 double y1 = unit->m_y1;
861 double y0 = b1 * y1 - y2;
862 level = a2 + y0;
863 y2 = y1;
864 y1 = y0;
865 unit->m_y1 = y1;
866 unit->m_y2 = y2;
867 } break;
868 case shape_Curve : {
869 double a2 = unit->m_a2;
870 double b1 = unit->m_b1;
871 double grow = unit->m_grow;
872 b1 *= grow;
873 level = a2 - b1;
874 unit->m_b1 = b1;
875 } break;
876 case shape_Squared : {
877 double grow = unit->m_grow;
878 double y1 = unit->m_y1;
879 y1 += grow;
880 level = y1*y1;
881 unit->m_y1 = y1;
882 } break;
883 case shape_Cubed : {
884 double grow = unit->m_grow;
885 double y1 = unit->m_y1;
886 y1 += grow;
887 level = y1*y1*y1;
888 unit->m_y1 = y1;
889 } break;
890 case shape_Sustain : {
891 } break;
894 phase --;
899 ZXP(out) = level;
902 float zgate = ZIN0(d_env_gate);
903 if(zgate >= 1.f) {
904 unit->m_running = true;
905 } else if (zgate > 0.f) {
906 unit->m_running = true;
907 release = true; // release next time.
908 } else {
909 unit->m_running = false; // sample and hold
912 unit->m_level = level;
913 unit->m_curve = curve;
914 unit->m_shape = shape;
915 unit->m_prevreset = zreset;
916 unit->m_release = release;
918 unit->m_phase = phase;
925 void DemandEnvGen_next_a(DemandEnvGen *unit, int inNumSamples)
928 float *reset = ZIN(d_env_reset);
929 float *gate = ZIN(d_env_gate);
931 float *out = ZOUT(0);
933 float prevreset = unit->m_prevreset;
934 double level = unit->m_level;
935 float phase = unit->m_phase;
936 double curve = unit->m_curve;
937 bool release = unit->m_release;
938 bool running = unit->m_running;
941 int shape = unit->m_shape;
942 // printf("phase %f \n", phase);
944 for (int i=0; i<inNumSamples; ++i) {
946 float zreset = ZXP(reset);
947 if (zreset > 0.f && prevreset <= 0.f) {
948 // printf("reset: %f %f \n", zreset, unit->m_prevreset);
949 RESETINPUT(d_env_level);
950 if(zreset <= 1.f) {
951 DEMANDINPUT_A(d_env_level, i + 1); // remove first level
952 } else {
953 level = DEMANDINPUT_A(d_env_level, i + 1); // jump to first level
956 RESETINPUT(d_env_dur);
957 RESETINPUT(d_env_shape);
958 release = false;
959 running = true;
961 phase = 0.f;
965 prevreset = zreset;
968 if (phase <= 0.f && running) {
970 // was a release?
971 if(release) {
973 running = false;
974 release = false;
975 // printf("release: %f %f \n", phase, level);
976 int doneAction = (int)ZIN0(d_env_doneAction);
977 DoneAction(doneAction, unit);
979 } else {
983 // new time
985 float dur = DEMANDINPUT_A(d_env_dur, i + 1);
986 // printf("dur: %f \n", dur);
987 if(sc_isnan(dur)) {
988 release = true;
989 running = false;
990 phase = numeric_limits<float>::max();
991 } else {
992 phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
995 // new shape
996 float count;
997 curve = DEMANDINPUT_A(d_env_shape, i + 1);
998 shape = (int)DEMANDINPUT_A(d_env_shape, i + 1);
1000 // printf("shapes: %i \n", shape);
1001 if (sc_isnan(curve)) curve = unit->m_shape;
1002 if (sc_isnan(shape)) shape = unit->m_shape;
1004 if (phase <= 1.f) {
1005 shape = 1; // shape_Linear
1006 count = 1.f;
1007 } else {
1008 count = phase;
1010 if(dur * 0.5f < SAMPLEDUR) shape = 1;
1013 // new end level
1015 double endLevel = DEMANDINPUT_A(d_env_level, i + 1);
1016 // printf("levels: %f %f\n", level, endLevel);
1017 if (sc_isnan(endLevel)) {
1018 endLevel = unit->m_endLevel;
1019 release = true;
1020 phase = 0.f;
1021 shape = 0;
1022 } else {
1023 endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
1024 unit->m_endLevel = endLevel;
1028 // calculate shape parameters
1030 switch (shape) {
1031 case shape_Step : {
1032 level = endLevel;
1033 } break;
1034 case shape_Linear : {
1035 unit->m_grow = (endLevel - level) / count;
1036 } break;
1037 case shape_Exponential : {
1038 unit->m_grow = pow(endLevel / level, 1.0 / count);
1039 } break;
1040 case shape_Sine : {
1041 double w = pi / count;
1043 unit->m_a2 = (endLevel + level) * 0.5;
1044 unit->m_b1 = 2. * cos(w);
1045 unit->m_y1 = (endLevel - level) * 0.5;
1046 unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
1047 level = unit->m_a2 - unit->m_y1;
1048 } break;
1049 case shape_Welch : {
1050 double w = (pi * 0.5) / count;
1052 unit->m_b1 = 2. * cos(w);
1054 if (endLevel >= level) {
1055 unit->m_a2 = level;
1056 unit->m_y1 = 0.;
1057 unit->m_y2 = -sin(w) * (endLevel - level);
1058 } else {
1059 unit->m_a2 = endLevel;
1060 unit->m_y1 = level - endLevel;
1061 unit->m_y2 = cos(w) * (level - endLevel);
1063 level = unit->m_a2 + unit->m_y1;
1064 } break;
1065 case shape_Curve : {
1066 if (fabs(curve) < 0.001) {
1067 unit->m_shape = 1; // shape_Linear
1068 unit->m_grow = (endLevel - level) / count;
1069 } else {
1070 double a1 = (endLevel - level) / (1.0 - exp(curve));
1071 unit->m_a2 = level + a1;
1072 unit->m_b1 = a1;
1073 unit->m_grow = exp(curve / count);
1075 } break;
1076 case shape_Squared : {
1077 unit->m_y1 = sqrt(level);
1078 unit->m_y2 = sqrt(endLevel);
1079 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1080 } break;
1081 case shape_Cubed : {
1082 unit->m_y1 = pow(level, 0.33333333);
1083 unit->m_y2 = pow(endLevel, 0.33333333);
1084 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1085 } break;
1092 if(running) {
1093 switch (shape) {
1094 case shape_Step : {
1095 } break;
1096 case shape_Linear : {
1097 double grow = unit->m_grow;
1098 //Print("level %g\n", level);
1099 level += grow;
1100 } break;
1101 case shape_Exponential : {
1102 double grow = unit->m_grow;
1103 level *= grow;
1104 } break;
1105 case shape_Sine : {
1106 double a2 = unit->m_a2;
1107 double b1 = unit->m_b1;
1108 double y2 = unit->m_y2;
1109 double y1 = unit->m_y1;
1110 double y0 = b1 * y1 - y2;
1111 level = a2 - y0;
1112 y2 = y1;
1113 y1 = y0;
1114 unit->m_y1 = y1;
1115 unit->m_y2 = y2;
1116 } break;
1117 case shape_Welch : {
1118 double a2 = unit->m_a2;
1119 double b1 = unit->m_b1;
1120 double y2 = unit->m_y2;
1121 double y1 = unit->m_y1;
1122 double y0 = b1 * y1 - y2;
1123 level = a2 + y0;
1124 y2 = y1;
1125 y1 = y0;
1126 unit->m_y1 = y1;
1127 unit->m_y2 = y2;
1128 } break;
1129 case shape_Curve : {
1130 double a2 = unit->m_a2;
1131 double b1 = unit->m_b1;
1132 double grow = unit->m_grow;
1133 b1 *= grow;
1134 level = a2 - b1;
1135 unit->m_b1 = b1;
1136 } break;
1137 case shape_Squared : {
1138 double grow = unit->m_grow;
1139 double y1 = unit->m_y1;
1140 y1 += grow;
1141 level = y1*y1;
1142 unit->m_y1 = y1;
1143 } break;
1144 case shape_Cubed : {
1145 double grow = unit->m_grow;
1146 double y1 = unit->m_y1;
1147 y1 += grow;
1148 level = y1*y1*y1;
1149 unit->m_y1 = y1;
1150 } break;
1151 case shape_Sustain : {
1152 } break;
1155 phase--;
1159 ZXP(out) = level;
1160 float zgate = ZXP(gate);
1162 if(zgate >= 1.f) {
1163 unit->m_running = true;
1164 } else if (zgate > 0.f) {
1165 unit->m_running = true;
1166 release = true; // release next time.
1167 } else {
1168 unit->m_running = false; // sample and hold
1172 unit->m_level = level;
1173 unit->m_curve = curve;
1174 unit->m_shape = shape;
1175 unit->m_prevreset = prevreset;
1176 unit->m_release = release;
1177 unit->m_phase = phase;
1182 void DemandEnvGen_Ctor(DemandEnvGen *unit)
1184 // derive the first level.
1186 unit->m_level = DEMANDINPUT(d_env_level);
1187 if(sc_isnan(unit->m_level)) { unit->m_level = 0.f; }
1188 unit->m_endLevel = unit->m_level;
1189 unit->m_release = false;
1190 unit->m_prevreset = 0.f;
1191 unit->m_phase = 0.f;
1192 unit->m_running = ZIN0(d_env_gate) > 0.f;
1194 if(INRATE(d_env_gate) == calc_FullRate) {
1195 SETCALC(DemandEnvGen_next_a);
1196 } else {
1197 SETCALC(DemandEnvGen_next_k);
1200 DemandEnvGen_next_k(unit, 1);
1205 /////////////////////////////////////////////////////////////////////////////
1207 void TDuty_next_da(TDuty *unit, int inNumSamples)
1210 float *reset = ZIN(duty_reset);
1211 float *out = OUT(0);
1213 float count = unit->m_count;
1214 float prevreset = unit->m_prevreset;
1215 float sr = (float) SAMPLERATE;
1217 for (int i=0; i<inNumSamples; ++i) {
1219 float zreset = ZXP(reset);
1220 if (zreset > 0.f && prevreset <= 0.f) {
1222 RESETINPUT(duty_level);
1223 RESETINPUT(duty_dur);
1224 count = 0.f;
1226 if (count <= 0.f) {
1227 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1228 if(sc_isnan(count)) {
1229 int doneAction = (int)ZIN0(2);
1230 DoneAction(doneAction, unit);
1232 float x = DEMANDINPUT_A(duty_level, i + 1);
1233 //printf("in %d %g\n", k, x);
1234 if (sc_isnan(x)) x = 0.f;
1235 out[i] = x;
1236 } else {
1237 out[i] = 0.f;
1239 count--;
1240 prevreset = zreset;
1243 unit->m_count = count;
1244 unit->m_prevreset = prevreset;
1248 void TDuty_next_dk(TDuty *unit, int inNumSamples)
1251 float zreset = ZIN0(duty_reset);
1253 float *out = OUT(0);
1254 float count = unit->m_count;
1255 float prevreset = unit->m_prevreset;
1256 float sr = (float) SAMPLERATE;
1258 for (int i=0; i<inNumSamples; ++i) {
1260 if (zreset > 0.f && prevreset <= 0.f) {
1262 RESETINPUT(duty_level);
1263 RESETINPUT(duty_dur);
1264 count = 0.f;
1266 if (count <= 0.f) {
1267 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1268 if(sc_isnan(count)) {
1269 int doneAction = (int)ZIN0(2);
1270 DoneAction(doneAction, unit);
1272 float x = DEMANDINPUT_A(duty_level, i + 1);
1273 //printf("in %d %g\n", k, x);
1274 if (sc_isnan(x)) x = 0.f;
1275 out[i] = x;
1276 } else {
1277 out[i] = 0.f;
1279 count--;
1280 prevreset = zreset;
1283 unit->m_count = count;
1284 unit->m_prevreset = prevreset;
1288 void TDuty_next_dd(TDuty *unit, int inNumSamples)
1291 float *out = OUT(0);
1292 float count = unit->m_count;
1293 float reset = unit->m_prevreset;
1294 float sr = (float) SAMPLERATE;
1296 for (int i=0; i<inNumSamples; ++i) {
1298 if (reset <= 0.f) {
1299 RESETINPUT(duty_level);
1300 RESETINPUT(duty_dur);
1301 count = 0.f;
1302 reset = DEMANDINPUT_A(duty_reset, i + 1) * sr + reset;
1303 } else {
1304 reset--;
1306 if (count <= 0.f) {
1307 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1308 if(sc_isnan(count)) {
1309 int doneAction = (int)ZIN0(2);
1310 DoneAction(doneAction, unit);
1312 float x = DEMANDINPUT_A(duty_level, i + 1);
1313 //printf("in %d %g\n", k, x);
1314 if (sc_isnan(x)) x = 0.f;
1315 out[i] = x;
1316 } else {
1318 out[i] = 0.f;
1320 count--;
1323 unit->m_count = count;
1324 unit->m_prevreset = reset;
1329 void TDuty_Ctor(TDuty *unit)
1331 if (INRATE(1) == calc_FullRate) {
1333 SETCALC(TDuty_next_da);
1334 unit->m_prevreset = 0.f;
1336 } else {
1337 if(INRATE(1) == calc_DemandRate) {
1338 SETCALC(TDuty_next_dd);
1339 unit->m_prevreset = DEMANDINPUT(1) * SAMPLERATE;
1340 } else {
1341 SETCALC(TDuty_next_dk);
1342 unit->m_prevreset = 0.f;
1345 // support for gap-first.
1346 if(IN0(4)) {
1347 unit->m_count = DEMANDINPUT(duty_dur) * SAMPLERATE;
1348 } else {
1349 unit->m_count = 0.f;
1351 OUT0(0) = 0.f;
1355 /////////////////////////////////////////////////////////////////////////////
1359 void Dseries_next(Dseries *unit, int inNumSamples)
1361 if (inNumSamples) {
1362 float step = DEMANDINPUT_A(2, inNumSamples);
1363 if(!sc_isnan(step)) { unit->m_step = step; }
1364 if (unit->m_repeats < 0.) {
1365 float x = DEMANDINPUT_A(0, inNumSamples);
1366 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1367 unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1369 if (unit->m_repeatCount >= unit->m_repeats) {
1370 OUT0(0) = NAN;
1371 return;
1373 OUT0(0) = unit->m_value;
1374 unit->m_value += unit->m_step;
1375 unit->m_repeatCount++;
1376 } else {
1377 unit->m_repeats = -1.f;
1378 unit->m_repeatCount = 0;
1382 void Dseries_Ctor(Dseries *unit)
1384 SETCALC(Dseries_next);
1385 Dseries_next(unit, 0);
1386 OUT0(0) = 0.f;
1390 void Dgeom_next(Dgeom *unit, int inNumSamples)
1392 if (inNumSamples) {
1393 float grow = DEMANDINPUT_A(2, inNumSamples);
1394 if(!sc_isnan(grow)) { unit->m_grow = grow; }
1395 if (unit->m_repeats < 0.) {
1396 float x = DEMANDINPUT_A(0, inNumSamples);
1397 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1398 unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1400 if (unit->m_repeatCount >= unit->m_repeats) {
1401 OUT0(0) = NAN;
1402 return;
1404 OUT0(0) = unit->m_value;
1405 unit->m_value *= unit->m_grow;
1406 unit->m_repeatCount++;
1407 } else {
1408 unit->m_repeats = -1.f;
1409 unit->m_repeatCount = 0;
1413 void Dgeom_Ctor(Dgeom *unit)
1415 SETCALC(Dgeom_next);
1416 Dgeom_next(unit, 0);
1417 OUT0(0) = 0.f;
1421 void Dwhite_next(Dwhite *unit, int inNumSamples)
1423 if (inNumSamples) {
1424 if (unit->m_repeats < 0.) {
1425 float x = DEMANDINPUT_A(0, inNumSamples);
1426 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1428 if (unit->m_repeatCount >= unit->m_repeats) {
1429 OUT0(0) = NAN;
1430 return;
1432 unit->m_repeatCount++;
1433 float lo = DEMANDINPUT_A(1, inNumSamples);
1434 float hi = DEMANDINPUT_A(2, inNumSamples);
1436 if(!sc_isnan(lo)) { unit->m_lo = lo;}
1437 if(!sc_isnan(hi)) { unit->m_range = hi - lo; }
1438 float x = unit->mParent->mRGen->frand() * unit->m_range + unit->m_lo;
1439 OUT0(0) = x;
1440 } else {
1441 unit->m_repeats = -1.f;
1442 unit->m_repeatCount = 0;
1446 void Dwhite_Ctor(Dwhite *unit)
1448 SETCALC(Dwhite_next);
1449 Dwhite_next(unit, 0);
1450 OUT0(0) = 0.f;
1454 void Diwhite_next(Diwhite *unit, int inNumSamples)
1456 if (inNumSamples) {
1457 float lo = DEMANDINPUT_A(1, inNumSamples);
1458 float hi = DEMANDINPUT_A(2, inNumSamples);
1460 if(!sc_isnan(lo)) { unit->m_lo = (int32)floor(DEMANDINPUT_A(1, inNumSamples) + 0.5f); }
1461 if(!sc_isnan(hi)) {
1462 int32 hi = (int32)floor(DEMANDINPUT_A(2, inNumSamples) + 0.5f);
1463 unit->m_range = hi - unit->m_lo + 1;
1466 if (unit->m_repeats < 0.) {
1467 float x = DEMANDINPUT_A(0, inNumSamples);
1468 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1470 if (unit->m_repeatCount >= unit->m_repeats) {
1471 OUT0(0) = NAN;
1472 return;
1474 unit->m_repeatCount++;
1475 float x = unit->mParent->mRGen->irand(unit->m_range) + unit->m_lo;
1476 OUT0(0) = x;
1477 } else {
1478 unit->m_repeats = -1.f;
1479 unit->m_repeatCount = 0;
1483 void Diwhite_Ctor(Diwhite *unit)
1485 SETCALC(Diwhite_next);
1486 Diwhite_next(unit, 0);
1487 OUT0(0) = 0.f;
1491 void Dbrown_next(Dbrown *unit, int inNumSamples)
1493 if (inNumSamples) {
1494 float lo = DEMANDINPUT_A(1, inNumSamples); if(!sc_isnan(lo)) { unit->m_lo = lo; }
1495 float hi = DEMANDINPUT_A(2, inNumSamples); if(!sc_isnan(hi)) { unit->m_hi = hi; }
1496 float step = DEMANDINPUT_A(3, inNumSamples); if(!sc_isnan(step)) { unit->m_step = step; }
1498 if (unit->m_repeats < 0.) {
1499 float x = DEMANDINPUT_A(0, inNumSamples);
1500 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1501 unit->m_val = unit->mParent->mRGen->frand() * (unit->m_hi - unit->m_lo) + unit->m_lo;
1503 if (unit->m_repeatCount >= unit->m_repeats) {
1504 OUT0(0) = NAN;
1505 return;
1507 unit->m_repeatCount++;
1508 OUT0(0) = unit->m_val;
1509 float x = unit->m_val + unit->mParent->mRGen->frand2() * unit->m_step;
1510 unit->m_val = sc_fold(x, unit->m_lo, unit->m_hi);
1511 } else {
1512 unit->m_repeats = -1.f;
1513 unit->m_repeatCount = 0;
1517 void Dbrown_Ctor(Dbrown *unit)
1519 SETCALC(Dbrown_next);
1520 Dbrown_next(unit, 0);
1521 OUT0(0) = 0.f;
1525 void Dibrown_next(Dibrown *unit, int inNumSamples)
1527 if (inNumSamples) {
1529 float lo = DEMANDINPUT_A(1, inNumSamples); if(!sc_isnan(lo)) { unit->m_lo = (int32)lo; }
1530 float hi = DEMANDINPUT_A(2, inNumSamples); if(!sc_isnan(hi)) { unit->m_hi = (int32)hi; }
1531 float step = DEMANDINPUT_A(3, inNumSamples); if(!sc_isnan(step)) { unit->m_step = (int32)step; }
1533 if (unit->m_repeats < 0.) {
1534 float x = DEMANDINPUT_A(0, inNumSamples);
1535 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1536 unit->m_val = unit->mParent->mRGen->irand(unit->m_hi - unit->m_lo + 1) + unit->m_lo;
1538 if (unit->m_repeatCount >= unit->m_repeats) {
1539 OUT0(0) = NAN;
1540 return;
1542 OUT0(0) = unit->m_val;
1543 int32 z = unit->m_val + unit->mParent->mRGen->irand2(unit->m_step);
1544 unit->m_val = sc_fold(z, unit->m_lo, unit->m_hi);
1545 } else {
1546 unit->m_repeats = -1.f;
1547 unit->m_repeatCount = 0;
1551 void Dibrown_Ctor(Dibrown *unit)
1553 SETCALC(Dibrown_next);
1554 Dibrown_next(unit, 0);
1555 OUT0(0) = 0.f;
1560 void Dseq_next(Dseq *unit, int inNumSamples)
1562 //Print("->Dseq_next %d\n", inNumSamples);
1563 if (inNumSamples) {
1564 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1565 if (unit->m_repeats < 0.) {
1566 float x = DEMANDINPUT_A(0, inNumSamples);
1567 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1569 int attempts = 0;
1570 while (true) {
1571 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1572 if (unit->m_index >= unit->mNumInputs) {
1573 unit->m_index = 1;
1574 unit->m_repeatCount++;
1576 if (unit->m_repeatCount >= unit->m_repeats) {
1577 //Print("done\n");
1578 OUT0(0) = NAN;
1579 unit->m_index = 1;
1580 return;
1582 if (ISDEMANDINPUT(unit->m_index)) {
1583 if (unit->m_needToResetChild) {
1584 unit->m_needToResetChild = false;
1585 RESETINPUT(unit->m_index);
1587 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1588 if (sc_isnan(x)) {
1589 unit->m_index++;
1590 unit->m_needToResetChild = true;
1591 } else {
1592 OUT0(0) = x;
1593 return;
1595 } else {
1596 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1597 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1598 unit->m_index++;
1599 unit->m_needToResetChild = true;
1600 return;
1603 if (attempts++ > unit->mNumInputs) {
1604 Print("Warning: empty sequence in Dseq\n");
1605 return;
1608 } else {
1609 unit->m_repeats = -1.f;
1610 unit->m_repeatCount = 0;
1611 unit->m_needToResetChild = true;
1612 unit->m_index = 1;
1617 void Dseq_Ctor(Dseq *unit)
1619 SETCALC(Dseq_next);
1620 Dseq_next(unit, 0);
1621 OUT0(0) = 0.f;
1625 void Dser_next(Dser *unit, int inNumSamples)
1627 if (inNumSamples) {
1628 if (unit->m_repeats < 0.) {
1629 float x = DEMANDINPUT_A(0, inNumSamples);
1630 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1632 while (true) {
1633 if (unit->m_index >= unit->mNumInputs) {
1634 unit->m_index = 1;
1636 if (unit->m_repeatCount >= unit->m_repeats) {
1637 OUT0(0) = NAN;
1638 return;
1640 if (ISDEMANDINPUT(unit->m_index)) {
1641 if (unit->m_needToResetChild) {
1642 unit->m_needToResetChild = false;
1643 RESETINPUT(unit->m_index);
1645 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1646 if (sc_isnan(x)) {
1647 unit->m_index++;
1648 unit->m_repeatCount++;
1649 unit->m_needToResetChild = true;
1650 } else {
1651 OUT0(0) = x;
1652 return;
1654 } else {
1655 OUT0(0) = IN0(unit->m_index);
1656 unit->m_index++;
1657 unit->m_repeatCount++;
1658 unit->m_needToResetChild = true;
1659 return;
1662 } else {
1663 unit->m_repeats = -1.f;
1664 unit->m_repeatCount = 0;
1665 unit->m_needToResetChild = true;
1666 unit->m_index = 1;
1670 void Dser_Ctor(Dser *unit)
1672 SETCALC(Dser_next);
1673 Dser_next(unit, 0);
1674 OUT0(0) = 0.f;
1679 void Drand_next(Drand *unit, int inNumSamples)
1681 if (inNumSamples) {
1683 if (unit->m_repeats < 0.) {
1684 float x = DEMANDINPUT_A(0, inNumSamples);
1685 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1687 while (true) {
1688 if (unit->m_repeatCount >= unit->m_repeats) {
1689 OUT0(0) = NAN;
1690 return;
1692 if (ISDEMANDINPUT(unit->m_index)) {
1693 if (unit->m_needToResetChild) {
1694 unit->m_needToResetChild = false;
1695 RESETINPUT(unit->m_index);
1697 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1698 if (sc_isnan(x)) {
1699 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1700 unit->m_repeatCount++;
1701 unit->m_needToResetChild = true;
1702 } else {
1703 OUT0(0) = x;
1704 return;
1706 } else {
1707 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1708 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1709 unit->m_repeatCount++;
1710 unit->m_needToResetChild = true;
1711 return;
1714 } else {
1715 unit->m_repeats = -1.f;
1716 unit->m_repeatCount = 0;
1717 unit->m_needToResetChild = true;
1718 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1722 void Drand_Ctor(Drand *unit)
1724 SETCALC(Drand_next);
1725 Drand_next(unit, 0);
1726 OUT0(0) = 0.f;
1731 void Dxrand_next(Dxrand *unit, int inNumSamples)
1733 if (inNumSamples) {
1734 if (unit->m_repeats < 0.) {
1735 float x = DEMANDINPUT_A(0, inNumSamples);
1736 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1738 while (true) {
1739 if (unit->m_index >= unit->mNumInputs) {
1740 unit->m_index = 1;
1742 if (unit->m_repeatCount >= unit->m_repeats) {
1743 OUT0(0) = NAN;
1744 return;
1746 if (ISDEMANDINPUT(unit->m_index)) {
1747 if (unit->m_needToResetChild) {
1748 unit->m_needToResetChild = false;
1749 RESETINPUT(unit->m_index);
1751 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1752 if (sc_isnan(x)) {
1753 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1754 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1755 unit->m_repeatCount++;
1756 unit->m_needToResetChild = true;
1757 } else {
1758 OUT0(0) = x;
1759 return;
1761 } else {
1762 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1763 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1764 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1765 unit->m_repeatCount++;
1766 unit->m_needToResetChild = true;
1767 return;
1770 } else {
1771 unit->m_repeats = -1.f;
1772 unit->m_repeatCount = 0;
1773 unit->m_needToResetChild = true;
1774 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1775 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1779 void Dxrand_Ctor(Dxrand *unit)
1781 SETCALC(Dxrand_next);
1782 Dxrand_next(unit, 0);
1783 OUT0(0) = 0.f;
1786 #define WINDEX \
1787 float w, sum = 0.0; \
1788 float r = unit->mParent->mRGen->frand(); \
1789 for (int i=0; i<weights_size; ++i) { \
1790 w = IN0(2 + i); \
1791 sum += w; \
1792 if (sum >= r) { \
1793 unit->m_index = i + offset; \
1794 break; \
1799 void Dwrand_next(Dwrand *unit, int inNumSamples)
1801 int offset = unit->m_weights_size + 2;
1802 int weights_size = unit->mNumInputs - offset;
1803 if (inNumSamples) {
1805 if (unit->m_repeats < 0.) {
1806 float x = DEMANDINPUT_A(0, inNumSamples);
1807 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1809 while (true) {
1811 if (unit->m_repeatCount >= unit->m_repeats) {
1812 OUT0(0) = NAN;
1813 return;
1816 if (ISDEMANDINPUT(unit->m_index)) {
1817 if (unit->m_needToResetChild) {
1818 unit->m_needToResetChild = false;
1819 RESETINPUT(unit->m_index);
1821 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1822 if (sc_isnan(x)) {
1824 WINDEX;
1825 unit->m_repeatCount++;
1826 unit->m_needToResetChild = true;
1827 } else {
1828 OUT0(0) = x;
1829 return;
1831 } else {
1832 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1833 WINDEX;
1834 unit->m_repeatCount++;
1835 unit->m_needToResetChild = true;
1836 return;
1839 } else {
1840 unit->m_repeats = -1.f;
1841 unit->m_repeatCount = 0;
1842 unit->m_needToResetChild = true;
1843 WINDEX;
1847 void Dwrand_Ctor(Dwrand *unit)
1849 SETCALC(Dwrand_next);
1850 unit->m_weights_size = IN0(1);
1851 Dwrand_next(unit, 0);
1852 OUT0(0) = 0.f;
1857 static void Dshuf_scramble(Dshuf *unit);
1859 void Dshuf_next(Dshuf *unit, int inNumSamples)
1861 // Print("->Dshuf_next %d\n", inNumSamples);
1862 if (inNumSamples) {
1863 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1864 if (unit->m_repeats < 0.) {
1865 float x = DEMANDINPUT_A(0, inNumSamples);
1866 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1868 while (true) {
1869 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1870 if (unit->m_index >= (unit->mNumInputs - 1)) {
1871 unit->m_index = 0;
1872 unit->m_repeatCount++;
1874 if (unit->m_repeatCount >= unit->m_repeats) {
1875 //Print("done\n");
1876 OUT0(0) = NAN;
1877 unit->m_index = 0;
1878 return;
1880 if (ISDEMANDINPUT(unit->m_indices[unit->m_index])) {
1881 if (unit->m_needToResetChild) {
1882 unit->m_needToResetChild = false;
1883 RESETINPUT(unit->m_indices[unit->m_index]);
1885 float x = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1887 if (sc_isnan(x)) {
1888 unit->m_index++;
1889 unit->m_needToResetChild = true;
1890 } else {
1891 OUT0(0) = x;
1892 return;
1894 } else {
1895 OUT0(0) = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1896 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1897 unit->m_index++;
1898 unit->m_needToResetChild = true;
1899 return;
1902 } else {
1903 unit->m_repeats = -1.f;
1904 unit->m_repeatCount = 0;
1905 unit->m_needToResetChild = true;
1906 unit->m_index = 0;
1907 Dshuf_scramble(unit);
1911 void Dshuf_scramble(Dshuf *unit) {
1912 int32 i, j, m, k, size;
1913 int32 temp;
1915 size = (int32)(unit->mNumInputs) - 1;
1917 if (size > 1) {
1918 k = size;
1919 for (i=0, m=k; i<k-1; ++i, --m) {
1920 j = i + unit->mParent->mRGen->irand(m);
1921 temp = unit->m_indices[i];
1922 unit->m_indices[i] = unit->m_indices[j];
1923 unit->m_indices[j] = temp;
1929 void Dshuf_Ctor(Dshuf *unit)
1931 int32 i, size;
1933 size = (int32)(unit->mNumInputs) - 1;
1935 unit->m_indices = (int32*)RTAlloc(unit->mWorld, size * sizeof(int32));
1937 for(i=0; i < size; ++i) {
1938 unit->m_indices[i] = i + 1;
1941 SETCALC(Dshuf_next);
1942 Dshuf_next(unit, 0);
1943 OUT0(0) = 0.f;
1946 void Dshuf_Dtor(Dshuf *unit)
1948 RTFree(unit->mWorld, unit->m_indices);
1952 void Dswitch1_next(Dswitch1 *unit, int inNumSamples)
1954 if (inNumSamples) {
1955 float x = DEMANDINPUT_A(0, inNumSamples);
1956 if (sc_isnan(x)) {
1957 OUT0(0) = x;
1958 return;
1960 int index = (int32)floor(x + 0.5f);
1961 index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1962 OUT0(0) = DEMANDINPUT_A(index, inNumSamples);
1963 } else {
1964 for (int i=0; i<unit->mNumInputs; ++i) {
1965 RESETINPUT(i);
1970 void Dswitch1_Ctor(Dswitch1 *unit)
1972 SETCALC(Dswitch1_next);
1973 OUT0(0) = 0.f;
1976 /////////////////////////////
1978 void Dswitch_next(Dswitch *unit, int inNumSamples)
1980 if (inNumSamples) {
1981 float val = DEMANDINPUT_A(unit->m_index, inNumSamples);
1982 //printf("index: %i\n", (int) val);
1983 if(sc_isnan(val)) {
1984 float ival = DEMANDINPUT_A(0, inNumSamples);
1986 if(sc_isnan(ival))
1987 val = ival;
1988 else {
1989 int index = (int32)floor(ival + 0.5f);
1990 index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1991 val = DEMANDINPUT_A(index, inNumSamples);
1993 RESETINPUT(unit->m_index);
1994 // printf("resetting index: %i\n", unit->m_index);
1995 unit->m_index = index;
1998 OUT0(0) = val;
2000 } else {
2001 printf("...\n");
2002 for (int i=0; i<unit->mNumInputs; ++i) {
2003 RESETINPUT(i);
2005 int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
2006 index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
2007 unit->m_index = index;
2011 void Dswitch_Ctor(Dswitch *unit)
2013 SETCALC(Dswitch_next);
2014 int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
2015 index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
2016 unit->m_index = index;
2017 OUT0(0) = 0.f;
2020 //////////////////////////////
2023 void Dstutter_next(Dstutter *unit, int inNumSamples)
2025 if (inNumSamples) {
2027 if (unit->m_repeatCount >= unit->m_repeats) {
2029 float val = DEMANDINPUT_A(1, inNumSamples);
2030 float repeats = DEMANDINPUT_A(0, inNumSamples);
2032 if(sc_isnan(repeats) || sc_isnan(val)) {
2033 OUT0(0) = NAN;
2034 return;
2035 } else {
2036 unit->m_value = val;
2037 unit->m_repeats = floor(repeats + 0.5f);
2038 unit->m_repeatCount = 0.f;
2042 OUT0(0) = unit->m_value;
2043 unit->m_repeatCount++;
2045 } else {
2047 unit->m_repeats = -1.f;
2048 unit->m_repeatCount = 0.f;
2049 RESETINPUT(0);
2050 RESETINPUT(1);
2055 void Dstutter_Ctor(Dstutter *unit)
2057 SETCALC(Dstutter_next);
2058 Dstutter_next(unit, 0);
2059 OUT0(0) = 0.f;
2062 //////////////////////////////
2066 void Dpoll_next(Dpoll *unit, int inNumSamples)
2068 if (inNumSamples) {
2069 float x = DEMANDINPUT_A(0, inNumSamples);
2070 float run = DEMANDINPUT_A(2, inNumSamples) > 0.f;
2071 if(unit->m_mayprint && run) {
2072 Print("%s: %g block offset: %d\n", unit->m_id_string, x, inNumSamples - 1);
2074 if(IN0(1) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(1), x);
2075 OUT0(0) = x;
2076 } else {
2077 RESETINPUT(0);
2081 void Dpoll_Ctor(Dpoll *unit)
2083 SETCALC(Dpoll_next);
2084 unit->m_id = IN0(3); // number of chars in the id string
2085 unit->m_id_string = (char*)RTAlloc(unit->mWorld, ((int)unit->m_id + 1) * sizeof(char));
2086 for(int i = 0; i < (int)unit->m_id; i++) {
2087 unit->m_id_string[i] = (char)IN0(4+i);
2089 unit->m_id_string[(int)unit->m_id] = '\0';
2090 unit->m_mayprint = unit->mWorld->mVerbosity >= 0;
2091 OUT0(0) = 0.f;
2094 void Dpoll_Dtor(Dpoll* unit)
2096 RTFree(unit->mWorld, unit->m_id_string);
2099 //////////////////////////////
2103 void Dreset_next(Dreset *unit, int inNumSamples)
2105 if (inNumSamples) {
2106 float x = DEMANDINPUT_A(0, inNumSamples);
2107 float reset = DEMANDINPUT_A(1, inNumSamples);
2108 if(sc_isnan(x)) {
2109 OUT0(0) = NAN;
2110 return;
2112 if(reset > 0.0 && (unit->prev_reset <= 0.0)) { RESETINPUT(0); }
2113 unit->prev_reset = reset;
2114 OUT0(0) = x;
2115 } else {
2116 RESETINPUT(0);
2120 void Dreset_Ctor(Dreset *unit)
2122 SETCALC(Dreset_next);
2123 unit->prev_reset = 0.0;
2124 Dreset_next(unit, 0);
2129 //////////////////////////////
2133 void Donce_next(Donce *unit, int inNumSamples)
2135 if (inNumSamples) {
2136 if (unit->m_bufcounter == unit->mWorld->mBufCounter) {
2137 OUT0(0) = unit->m_prev;
2138 } else {
2139 float x = DEMANDINPUT_A(0, inNumSamples);
2140 unit->m_prev = x;
2141 OUT0(0) = x;
2143 } else {
2144 RESETINPUT(0);
2148 void Donce_Ctor(Donce *unit)
2150 SETCALC(Donce_next);
2151 OUT0(0) = 0.f;
2155 inline double sc_loop(Unit *unit, double in, double hi, int loop)
2157 // avoid the divide if possible
2158 if (in >= hi) {
2159 if (!loop) {
2160 unit->mDone = true;
2161 return hi;
2163 in -= hi;
2164 if (in < hi) return in;
2165 } else if (in < 0.) {
2166 if (!loop) {
2167 unit->mDone = true;
2168 return 0.;
2170 in += hi;
2171 if (in >= 0.) return in;
2172 } else return in;
2174 return in - hi * floor(in/hi);
2177 #define D_CHECK_BUF \
2178 if (!bufData) { \
2179 unit->mDone = true; \
2180 ClearUnitOutputs(unit, 1); \
2181 return; \
2184 #define D_GET_BUF \
2185 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2186 if (fbufnum != unit->m_fbufnum) { \
2187 uint32 bufnum = (int)fbufnum; \
2188 World *world = unit->mWorld; \
2189 if (bufnum < 0) { bufnum = 0; } \
2190 if (bufnum >= world->mNumSndBufs) { \
2191 int localBufNum = bufnum - world->mNumSndBufs; \
2192 Graph *parent = unit->mParent; \
2193 if(localBufNum <= parent->localBufNum) { \
2194 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2195 } else { \
2196 bufnum = 0; \
2197 unit->m_buf = world->mSndBufs + bufnum; \
2199 } else { \
2200 unit->m_buf = world->mSndBufs + bufnum; \
2202 unit->m_fbufnum = fbufnum; \
2204 SndBuf *buf = unit->m_buf; \
2205 LOCK_SNDBUF(buf); \
2206 float *bufData __attribute__((__unused__)) = buf->data; \
2207 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2208 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2209 uint32 bufFrames = buf->frames; \
2210 int mask __attribute__((__unused__)) = buf->mask; \
2211 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2213 #define D_GET_BUF_SHARED \
2214 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2215 if (fbufnum != unit->m_fbufnum) { \
2216 uint32 bufnum = (int)fbufnum; \
2217 World *world = unit->mWorld; \
2218 if (bufnum < 0) { bufnum = 0; } \
2219 if (bufnum >= world->mNumSndBufs) { \
2220 int localBufNum = bufnum - world->mNumSndBufs; \
2221 Graph *parent = unit->mParent; \
2222 if(localBufNum <= parent->localBufNum) { \
2223 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2224 } else { \
2225 bufnum = 0; \
2226 unit->m_buf = world->mSndBufs + bufnum; \
2228 } else { \
2229 unit->m_buf = world->mSndBufs + bufnum; \
2231 unit->m_fbufnum = fbufnum; \
2233 const SndBuf *buf = unit->m_buf; \
2234 LOCK_SNDBUF_SHARED(buf); \
2235 const float *bufData __attribute__((__unused__)) = buf->data; \
2236 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2237 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2238 uint32 bufFrames = buf->frames; \
2239 int mask __attribute__((__unused__)) = buf->mask; \
2240 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2243 void Dbufrd_next(Dbufrd *unit, int inNumSamples)
2245 int32 loop = (int32)DEMANDINPUT_A(2, inNumSamples);
2247 D_GET_BUF_SHARED
2248 D_CHECK_BUF
2250 double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2252 double phase;
2253 if (inNumSamples)
2255 float x = DEMANDINPUT_A(1, inNumSamples);
2256 if (sc_isnan(x)) {
2257 OUT0(0) = NAN;
2258 return;
2260 phase = x;
2262 phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2263 int32 iphase = (int32)phase;
2264 const float* table1 = bufData + iphase * bufChannels;
2265 OUT0(0) = table1[0];
2267 else
2269 RESETINPUT(1);
2273 void Dbufrd_Ctor(Dbufrd *unit)
2275 SETCALC(Dbufrd_next);
2277 unit->m_fbufnum = -1e9f;
2279 Dbufrd_next(unit, 0);
2280 OUT0(0) = 0.f;
2283 ////////////////////////////////////
2285 void Dbufwr_next(Dbufwr *unit, int inNumSamples)
2287 int32 loop = (int32)DEMANDINPUT_A(3, inNumSamples);
2289 D_GET_BUF
2290 D_CHECK_BUF
2292 double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2294 double phase;
2295 float val;
2296 if (inNumSamples)
2298 float x = DEMANDINPUT_A(1, inNumSamples);
2299 if (sc_isnan(x)) {
2300 OUT0(0) = NAN;
2301 return;
2303 phase = x;
2304 val = DEMANDINPUT_A(2, inNumSamples);
2305 if (sc_isnan(val)) {
2306 OUT0(0) = NAN;
2307 return;
2310 phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2311 int32 iphase = (int32)phase;
2312 float* table0 = bufData + iphase * bufChannels;
2313 table0[0] = val;
2314 OUT0(0) = val;
2316 else
2318 RESETINPUT(1);
2319 RESETINPUT(2);
2323 void Dbufwr_Ctor(Dbufwr *unit)
2325 SETCALC(Dbufwr_next);
2327 unit->m_fbufnum = -1e9f;
2329 Dbufwr_next(unit, 0);
2330 OUT0(0) = 0.f;
2334 //////////////////////////////////////////////////////
2338 PluginLoad(Demand)
2340 ft = inTable;
2342 DefineDtorCantAliasUnit(Demand);
2343 DefineSimpleCantAliasUnit(Duty);
2344 DefineSimpleCantAliasUnit(DemandEnvGen);
2345 DefineSimpleCantAliasUnit(TDuty);
2346 DefineSimpleUnit(Dseries);
2347 DefineSimpleUnit(Dgeom);
2348 DefineSimpleUnit(Dwhite);
2349 DefineSimpleUnit(Dbrown);
2350 DefineSimpleUnit(Diwhite);
2351 DefineSimpleUnit(Dibrown);
2352 DefineSimpleUnit(Dseq);
2353 DefineSimpleUnit(Dser);
2354 DefineSimpleUnit(Dbufrd);
2355 DefineSimpleUnit(Dbufwr);
2356 DefineSimpleUnit(Drand);
2357 DefineSimpleUnit(Dwrand);
2358 DefineSimpleUnit(Dxrand);
2359 DefineDtorUnit(Dshuf);
2360 DefineSimpleUnit(Dswitch1);
2361 DefineSimpleUnit(Dswitch);
2362 DefineSimpleUnit(Dstutter);
2363 DefineSimpleUnit(Donce);
2364 DefineSimpleUnit(Dreset);
2365 DefineDtorUnit(Dpoll);