Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / server / plugins / DemandUGens.cpp
blob2a5f030479b9a9520c57ca63d46f6e0a14060cbb
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)
666 float zreset = ZIN0(d_env_reset);
668 float *out = ZOUT(0);
669 double level = unit->m_level;
670 float phase = unit->m_phase;
671 double curve = unit->m_curve;
672 bool release = unit->m_release;
673 bool running = unit->m_running;
674 int shape = unit->m_shape;
676 // printf("phase %f level %f \n", phase, level);
678 for (int i=0; i<inNumSamples; ++i) {
680 if (zreset > 0.f && unit->m_prevreset <= 0.f) {
681 //printf("reset: %f %f \n", zreset, unit->m_prevreset);
682 RESETINPUT(d_env_level);
683 RESETINPUT(d_env_dur);
684 RESETINPUT(d_env_shape);
685 RESETINPUT(d_env_curve);
687 if(zreset <= 1.f) {
688 DEMANDINPUT(d_env_level); // remove first level
689 } else {
690 level = DEMANDINPUT(d_env_level); // jump to first level
693 release = false;
694 running = true;
695 phase = 0.f;
700 if (phase <= 0.f && running) {
702 // was a release during last segment?
703 if(release) {
705 running = false;
706 release = false;
707 // printf("release: %f %f \n", phase, level);
708 int doneAction = (int)ZIN0(d_env_doneAction);
709 DoneAction(doneAction, unit);
711 } else {
712 // new time
714 float dur = DEMANDINPUT(d_env_dur);
715 // printf("dur: %f \n", dur);
716 if(sc_isnan(dur)) {
717 release = true;
718 running = false;
719 phase = numeric_limits<float>::max();
720 } else {
721 phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
725 // new shape
727 float count;
728 shape = (int)DEMANDINPUT(d_env_shape);
729 curve = DEMANDINPUT(d_env_curve);
731 if (sc_isnan(shape)) shape = unit->m_shape;
732 if (sc_isnan(curve)) curve = unit->m_curve;
733 if (phase <= 1.f) {
734 shape = 1; // shape_Linear
735 count = 1.f;
736 } else {
737 count = phase;
739 if(dur * 0.5f < SAMPLEDUR) shape = 1;
741 //printf("shape: %i, curve: %f, dur: %f \n", shape, curve, dur);
744 // new end level
746 double endLevel = DEMANDINPUT(d_env_level);
747 // printf("levels: %f %f\n", level, endLevel);
748 if (sc_isnan(endLevel)) {
749 endLevel = unit->m_endLevel;
750 release = true;
751 phase = 0.f;
752 shape = 0;
753 } else {
754 endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
755 unit->m_endLevel = endLevel;
759 // calculate shape parameters
761 switch (shape) {
762 case shape_Step : {
763 level = endLevel;
764 } break;
765 case shape_Linear : {
766 unit->m_grow = (endLevel - level) / count;
767 } break;
768 case shape_Exponential : {
769 unit->m_grow = pow(endLevel / level, 1.0 / count);
770 } break;
771 case shape_Sine : {
772 double w = pi / count;
774 unit->m_a2 = (endLevel + level) * 0.5;
775 unit->m_b1 = 2. * cos(w);
776 unit->m_y1 = (endLevel - level) * 0.5;
777 unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
778 level = unit->m_a2 - unit->m_y1;
779 } break;
780 case shape_Welch : {
781 double w = (pi * 0.5) / count;
783 unit->m_b1 = 2. * cos(w);
785 if (endLevel >= level) {
786 unit->m_a2 = level;
787 unit->m_y1 = 0.;
788 unit->m_y2 = -sin(w) * (endLevel - level);
789 } else {
790 unit->m_a2 = endLevel;
791 unit->m_y1 = level - endLevel;
792 unit->m_y2 = cos(w) * (level - endLevel);
794 level = unit->m_a2 + unit->m_y1;
795 } break;
796 case shape_Curve : {
797 if (fabs(curve) < 0.001) {
798 unit->m_shape = shape = 1; // shape_Linear
799 unit->m_grow = (endLevel - level) / count;
800 } else {
801 double a1 = (endLevel - level) / (1.0 - exp(curve));
802 unit->m_a2 = level + a1;
803 unit->m_b1 = a1;
804 unit->m_grow = exp(curve / count);
806 } break;
807 case shape_Squared : {
808 unit->m_y1 = sqrt(level);
809 unit->m_y2 = sqrt(endLevel);
810 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
811 } break;
812 case shape_Cubed : {
813 unit->m_y1 = pow(level, 0.33333333);
814 unit->m_y2 = pow(endLevel, 0.33333333);
815 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
816 } break;
823 if(running) {
825 switch (shape) {
826 case shape_Step : {
827 } break;
828 case shape_Linear : {
829 double grow = unit->m_grow;
830 //Print("level %g\n", level);
831 level += grow;
832 } break;
833 case shape_Exponential : {
834 double grow = unit->m_grow;
835 level *= grow;
836 } break;
837 case shape_Sine : {
838 double a2 = unit->m_a2;
839 double b1 = unit->m_b1;
840 double y2 = unit->m_y2;
841 double y1 = unit->m_y1;
842 double y0 = b1 * y1 - y2;
843 level = a2 - y0;
844 y2 = y1;
845 y1 = y0;
846 unit->m_y1 = y1;
847 unit->m_y2 = y2;
848 } break;
849 case shape_Welch : {
850 double a2 = unit->m_a2;
851 double b1 = unit->m_b1;
852 double y2 = unit->m_y2;
853 double y1 = unit->m_y1;
854 double y0 = b1 * y1 - y2;
855 level = a2 + y0;
856 y2 = y1;
857 y1 = y0;
858 unit->m_y1 = y1;
859 unit->m_y2 = y2;
860 } break;
861 case shape_Curve : {
862 double a2 = unit->m_a2;
863 double b1 = unit->m_b1;
864 double grow = unit->m_grow;
865 b1 *= grow;
866 level = a2 - b1;
867 unit->m_b1 = b1;
868 } break;
869 case shape_Squared : {
870 double grow = unit->m_grow;
871 double y1 = unit->m_y1;
872 y1 += grow;
873 level = y1*y1;
874 unit->m_y1 = y1;
875 } break;
876 case shape_Cubed : {
877 double grow = unit->m_grow;
878 double y1 = unit->m_y1;
879 y1 += grow;
880 level = y1*y1*y1;
881 unit->m_y1 = y1;
882 } break;
883 case shape_Sustain : {
884 } break;
887 phase --;
892 ZXP(out) = level;
895 float zgate = ZIN0(d_env_gate);
896 if(zgate >= 1.f) {
897 unit->m_running = true;
898 } else if (zgate > 0.f) {
899 unit->m_running = true;
900 release = true; // release next time.
901 } else {
902 unit->m_running = false; // sample and hold
905 unit->m_level = level;
906 unit->m_curve = curve;
907 unit->m_shape = shape;
908 unit->m_prevreset = zreset;
909 unit->m_release = release;
911 unit->m_phase = phase;
916 void DemandEnvGen_next_a(DemandEnvGen *unit, int inNumSamples)
919 float *reset = ZIN(d_env_reset);
920 float *gate = ZIN(d_env_gate);
922 float *out = ZOUT(0);
924 float prevreset = unit->m_prevreset;
925 double level = unit->m_level;
926 float phase = unit->m_phase;
927 double curve = unit->m_curve;
928 bool release = unit->m_release;
929 bool running = unit->m_running;
932 int shape = unit->m_shape;
933 // printf("phase %f \n", phase);
935 for (int i=0; i<inNumSamples; ++i) {
937 float zreset = ZXP(reset);
938 if (zreset > 0.f && prevreset <= 0.f) {
939 // printf("reset: %f %f \n", zreset, unit->m_prevreset);
940 RESETINPUT(d_env_level);
941 if(zreset <= 1.f) {
942 DEMANDINPUT_A(d_env_level, i + 1); // remove first level
943 } else {
944 level = DEMANDINPUT_A(d_env_level, i + 1); // jump to first level
947 RESETINPUT(d_env_dur);
948 RESETINPUT(d_env_shape);
949 release = false;
950 running = true;
952 phase = 0.f;
955 prevreset = zreset;
958 if (phase <= 0.f && running) {
960 // was a release?
961 if(release) {
963 running = false;
964 release = false;
965 // printf("release: %f %f \n", phase, level);
966 int doneAction = (int)ZIN0(d_env_doneAction);
967 DoneAction(doneAction, unit);
969 } else {
970 // new time
972 float dur = DEMANDINPUT_A(d_env_dur, i + 1);
973 // printf("dur: %f \n", dur);
974 if(sc_isnan(dur)) {
975 release = true;
976 running = false;
977 phase = numeric_limits<float>::max();
978 } else {
979 phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
982 // new shape
983 float count;
984 curve = DEMANDINPUT_A(d_env_curve, i + 1);
985 shape = (int)DEMANDINPUT_A(d_env_shape, i + 1);
987 // printf("shapes: %i \n", shape);
988 if (sc_isnan(curve)) curve = unit->m_curve;
989 if (sc_isnan(shape)) shape = unit->m_shape;
991 if (phase <= 1.f) {
992 shape = 1; // shape_Linear
993 count = 1.f;
994 } else {
995 count = phase;
997 if(dur * 0.5f < SAMPLEDUR) shape = 1;
1000 // new end level
1002 double endLevel = DEMANDINPUT_A(d_env_level, i + 1);
1003 // printf("levels: %f %f\n", level, endLevel);
1004 if (sc_isnan(endLevel)) {
1005 endLevel = unit->m_endLevel;
1006 release = true;
1007 phase = 0.f;
1008 shape = 0;
1009 } else {
1010 endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
1011 unit->m_endLevel = endLevel;
1015 // calculate shape parameters
1017 switch (shape) {
1018 case shape_Step : {
1019 level = endLevel;
1020 } break;
1021 case shape_Linear : {
1022 unit->m_grow = (endLevel - level) / count;
1023 } break;
1024 case shape_Exponential : {
1025 unit->m_grow = pow(endLevel / level, 1.0 / count);
1026 } break;
1027 case shape_Sine : {
1028 double w = pi / count;
1030 unit->m_a2 = (endLevel + level) * 0.5;
1031 unit->m_b1 = 2. * cos(w);
1032 unit->m_y1 = (endLevel - level) * 0.5;
1033 unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
1034 level = unit->m_a2 - unit->m_y1;
1035 } break;
1036 case shape_Welch : {
1037 double w = (pi * 0.5) / count;
1039 unit->m_b1 = 2. * cos(w);
1041 if (endLevel >= level) {
1042 unit->m_a2 = level;
1043 unit->m_y1 = 0.;
1044 unit->m_y2 = -sin(w) * (endLevel - level);
1045 } else {
1046 unit->m_a2 = endLevel;
1047 unit->m_y1 = level - endLevel;
1048 unit->m_y2 = cos(w) * (level - endLevel);
1050 level = unit->m_a2 + unit->m_y1;
1051 } break;
1052 case shape_Curve : {
1053 if (fabs(curve) < 0.001) {
1054 unit->m_shape = shape = 1; // shape_Linear
1055 unit->m_grow = (endLevel - level) / count;
1056 } else {
1057 double a1 = (endLevel - level) / (1.0 - exp(curve));
1058 unit->m_a2 = level + a1;
1059 unit->m_b1 = a1;
1060 unit->m_grow = exp(curve / count);
1062 } break;
1063 case shape_Squared : {
1064 unit->m_y1 = sqrt(level);
1065 unit->m_y2 = sqrt(endLevel);
1066 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1067 } break;
1068 case shape_Cubed : {
1069 unit->m_y1 = pow(level, 0.33333333);
1070 unit->m_y2 = pow(endLevel, 0.33333333);
1071 unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1072 } break;
1079 if(running) {
1080 switch (shape) {
1081 case shape_Step : {
1082 } break;
1083 case shape_Linear : {
1084 double grow = unit->m_grow;
1085 //Print("level %g\n", level);
1086 level += grow;
1087 } break;
1088 case shape_Exponential : {
1089 double grow = unit->m_grow;
1090 level *= grow;
1091 } break;
1092 case shape_Sine : {
1093 double a2 = unit->m_a2;
1094 double b1 = unit->m_b1;
1095 double y2 = unit->m_y2;
1096 double y1 = unit->m_y1;
1097 double y0 = b1 * y1 - y2;
1098 level = a2 - y0;
1099 y2 = y1;
1100 y1 = y0;
1101 unit->m_y1 = y1;
1102 unit->m_y2 = y2;
1103 } break;
1104 case shape_Welch : {
1105 double a2 = unit->m_a2;
1106 double b1 = unit->m_b1;
1107 double y2 = unit->m_y2;
1108 double y1 = unit->m_y1;
1109 double y0 = b1 * y1 - y2;
1110 level = a2 + y0;
1111 y2 = y1;
1112 y1 = y0;
1113 unit->m_y1 = y1;
1114 unit->m_y2 = y2;
1115 } break;
1116 case shape_Curve : {
1117 double a2 = unit->m_a2;
1118 double b1 = unit->m_b1;
1119 double grow = unit->m_grow;
1120 b1 *= grow;
1121 level = a2 - b1;
1122 unit->m_b1 = b1;
1123 } break;
1124 case shape_Squared : {
1125 double grow = unit->m_grow;
1126 double y1 = unit->m_y1;
1127 y1 += grow;
1128 level = y1*y1;
1129 unit->m_y1 = y1;
1130 } break;
1131 case shape_Cubed : {
1132 double grow = unit->m_grow;
1133 double y1 = unit->m_y1;
1134 y1 += grow;
1135 level = y1*y1*y1;
1136 unit->m_y1 = y1;
1137 } break;
1138 case shape_Sustain : {
1139 } break;
1142 phase--;
1146 ZXP(out) = level;
1147 float zgate = ZXP(gate);
1149 if(zgate >= 1.f) {
1150 unit->m_running = true;
1151 } else if (zgate > 0.f) {
1152 unit->m_running = true;
1153 release = true; // release next time.
1154 } else {
1155 unit->m_running = false; // sample and hold
1159 unit->m_level = level;
1160 unit->m_curve = curve;
1161 unit->m_shape = shape;
1162 unit->m_prevreset = prevreset;
1163 unit->m_release = release;
1164 unit->m_phase = phase;
1168 void DemandEnvGen_Ctor(DemandEnvGen *unit)
1170 // derive the first level.
1172 unit->m_level = DEMANDINPUT(d_env_level);
1173 if(sc_isnan(unit->m_level)) { unit->m_level = 0.f; }
1174 unit->m_endLevel = unit->m_level;
1175 unit->m_release = false;
1176 unit->m_prevreset = 0.f;
1177 unit->m_phase = 0.f;
1178 unit->m_running = ZIN0(d_env_gate) > 0.f;
1180 if(INRATE(d_env_gate) == calc_FullRate) {
1181 SETCALC(DemandEnvGen_next_a);
1182 } else {
1183 SETCALC(DemandEnvGen_next_k);
1186 DemandEnvGen_next_k(unit, 1);
1191 /////////////////////////////////////////////////////////////////////////////
1193 void TDuty_next_da(TDuty *unit, int inNumSamples)
1196 float *reset = ZIN(duty_reset);
1197 float *out = OUT(0);
1199 float count = unit->m_count;
1200 float prevreset = unit->m_prevreset;
1201 float sr = (float) SAMPLERATE;
1203 for (int i=0; i<inNumSamples; ++i) {
1205 float zreset = ZXP(reset);
1206 if (zreset > 0.f && prevreset <= 0.f) {
1208 RESETINPUT(duty_level);
1209 RESETINPUT(duty_dur);
1210 count = 0.f;
1212 if (count <= 0.f) {
1213 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1214 if(sc_isnan(count)) {
1215 int doneAction = (int)ZIN0(2);
1216 DoneAction(doneAction, unit);
1218 float x = DEMANDINPUT_A(duty_level, i + 1);
1219 //printf("in %d %g\n", k, x);
1220 if (sc_isnan(x)) x = 0.f;
1221 out[i] = x;
1222 } else {
1223 out[i] = 0.f;
1225 count--;
1226 prevreset = zreset;
1229 unit->m_count = count;
1230 unit->m_prevreset = prevreset;
1234 void TDuty_next_dk(TDuty *unit, int inNumSamples)
1237 float zreset = ZIN0(duty_reset);
1239 float *out = OUT(0);
1240 float count = unit->m_count;
1241 float prevreset = unit->m_prevreset;
1242 float sr = (float) SAMPLERATE;
1244 for (int i=0; i<inNumSamples; ++i) {
1246 if (zreset > 0.f && prevreset <= 0.f) {
1248 RESETINPUT(duty_level);
1249 RESETINPUT(duty_dur);
1250 count = 0.f;
1252 if (count <= 0.f) {
1253 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1254 if(sc_isnan(count)) {
1255 int doneAction = (int)ZIN0(2);
1256 DoneAction(doneAction, unit);
1258 float x = DEMANDINPUT_A(duty_level, i + 1);
1259 //printf("in %d %g\n", k, x);
1260 if (sc_isnan(x)) x = 0.f;
1261 out[i] = x;
1262 } else {
1263 out[i] = 0.f;
1265 count--;
1266 prevreset = zreset;
1269 unit->m_count = count;
1270 unit->m_prevreset = prevreset;
1274 void TDuty_next_dd(TDuty *unit, int inNumSamples)
1277 float *out = OUT(0);
1278 float count = unit->m_count;
1279 float reset = unit->m_prevreset;
1280 float sr = (float) SAMPLERATE;
1282 for (int i=0; i<inNumSamples; ++i) {
1284 if (reset <= 0.f) {
1285 RESETINPUT(duty_level);
1286 RESETINPUT(duty_dur);
1287 count = 0.f;
1288 reset = DEMANDINPUT_A(duty_reset, i + 1) * sr + reset;
1289 } else {
1290 reset--;
1292 if (count <= 0.f) {
1293 count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1294 if(sc_isnan(count)) {
1295 int doneAction = (int)ZIN0(2);
1296 DoneAction(doneAction, unit);
1298 float x = DEMANDINPUT_A(duty_level, i + 1);
1299 //printf("in %d %g\n", k, x);
1300 if (sc_isnan(x)) x = 0.f;
1301 out[i] = x;
1302 } else {
1304 out[i] = 0.f;
1306 count--;
1309 unit->m_count = count;
1310 unit->m_prevreset = reset;
1315 void TDuty_Ctor(TDuty *unit)
1317 if (INRATE(1) == calc_FullRate) {
1319 SETCALC(TDuty_next_da);
1320 unit->m_prevreset = 0.f;
1322 } else {
1323 if(INRATE(1) == calc_DemandRate) {
1324 SETCALC(TDuty_next_dd);
1325 unit->m_prevreset = DEMANDINPUT(1) * SAMPLERATE;
1326 } else {
1327 SETCALC(TDuty_next_dk);
1328 unit->m_prevreset = 0.f;
1331 // support for gap-first.
1332 if(IN0(4)) {
1333 unit->m_count = DEMANDINPUT(duty_dur) * SAMPLERATE;
1334 } else {
1335 unit->m_count = 0.f;
1337 OUT0(0) = 0.f;
1341 /////////////////////////////////////////////////////////////////////////////
1345 void Dseries_next(Dseries *unit, int inNumSamples)
1347 if (inNumSamples) {
1348 float step = DEMANDINPUT_A(2, inNumSamples);
1349 if(!sc_isnan(step)) { unit->m_step = step; }
1350 if (unit->m_repeats < 0.) {
1351 float x = DEMANDINPUT_A(0, inNumSamples);
1352 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1353 unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1355 if (unit->m_repeatCount >= unit->m_repeats) {
1356 OUT0(0) = NAN;
1357 return;
1359 OUT0(0) = unit->m_value;
1360 unit->m_value += unit->m_step;
1361 unit->m_repeatCount++;
1362 } else {
1363 unit->m_repeats = -1.f;
1364 unit->m_repeatCount = 0;
1368 void Dseries_Ctor(Dseries *unit)
1370 SETCALC(Dseries_next);
1371 Dseries_next(unit, 0);
1372 OUT0(0) = 0.f;
1376 void Dgeom_next(Dgeom *unit, int inNumSamples)
1378 if (inNumSamples) {
1379 float grow = DEMANDINPUT_A(2, inNumSamples);
1380 if(!sc_isnan(grow)) { unit->m_grow = grow; }
1381 if (unit->m_repeats < 0.) {
1382 float x = DEMANDINPUT_A(0, inNumSamples);
1383 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1384 unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1386 if (unit->m_repeatCount >= unit->m_repeats) {
1387 OUT0(0) = NAN;
1388 return;
1390 OUT0(0) = unit->m_value;
1391 unit->m_value *= unit->m_grow;
1392 unit->m_repeatCount++;
1393 } else {
1394 unit->m_repeats = -1.f;
1395 unit->m_repeatCount = 0;
1399 void Dgeom_Ctor(Dgeom *unit)
1401 SETCALC(Dgeom_next);
1402 Dgeom_next(unit, 0);
1403 OUT0(0) = 0.f;
1407 void Dwhite_next(Dwhite *unit, int inNumSamples)
1409 if (inNumSamples) {
1410 if (unit->m_repeats < 0.) {
1411 float x = DEMANDINPUT_A(0, inNumSamples);
1412 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1414 if (unit->m_repeatCount >= unit->m_repeats) {
1415 OUT0(0) = NAN;
1416 return;
1418 unit->m_repeatCount++;
1419 float lo = DEMANDINPUT_A(1, inNumSamples);
1420 float hi = DEMANDINPUT_A(2, inNumSamples);
1422 if(!sc_isnan(lo)) { unit->m_lo = lo;}
1423 if(!sc_isnan(hi)) { unit->m_range = hi - lo; }
1424 float x = unit->mParent->mRGen->frand() * unit->m_range + unit->m_lo;
1425 OUT0(0) = x;
1426 } else {
1427 unit->m_repeats = -1.f;
1428 unit->m_repeatCount = 0;
1432 void Dwhite_Ctor(Dwhite *unit)
1434 SETCALC(Dwhite_next);
1435 Dwhite_next(unit, 0);
1436 OUT0(0) = 0.f;
1440 void Diwhite_next(Diwhite *unit, int inNumSamples)
1442 if (inNumSamples) {
1443 float lo = DEMANDINPUT_A(1, inNumSamples);
1444 float hi = DEMANDINPUT_A(2, inNumSamples);
1446 if(!sc_isnan(lo)) { unit->m_lo = (int32)floor(DEMANDINPUT_A(1, inNumSamples) + 0.5f); }
1447 if(!sc_isnan(hi)) {
1448 int32 hi = (int32)floor(DEMANDINPUT_A(2, inNumSamples) + 0.5f);
1449 unit->m_range = hi - unit->m_lo + 1;
1452 if (unit->m_repeats < 0.) {
1453 float x = DEMANDINPUT_A(0, inNumSamples);
1454 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1456 if (unit->m_repeatCount >= unit->m_repeats) {
1457 OUT0(0) = NAN;
1458 return;
1460 unit->m_repeatCount++;
1461 float x = unit->mParent->mRGen->irand(unit->m_range) + unit->m_lo;
1462 OUT0(0) = x;
1463 } else {
1464 unit->m_repeats = -1.f;
1465 unit->m_repeatCount = 0;
1469 void Diwhite_Ctor(Diwhite *unit)
1471 SETCALC(Diwhite_next);
1472 Diwhite_next(unit, 0);
1473 OUT0(0) = 0.f;
1477 void Dbrown_next(Dbrown *unit, int inNumSamples)
1479 if (inNumSamples) {
1480 float lo = DEMANDINPUT_A(1, inNumSamples); if(!sc_isnan(lo)) { unit->m_lo = lo; }
1481 float hi = DEMANDINPUT_A(2, inNumSamples); if(!sc_isnan(hi)) { unit->m_hi = hi; }
1482 float step = DEMANDINPUT_A(3, inNumSamples); if(!sc_isnan(step)) { unit->m_step = step; }
1484 if (unit->m_repeats < 0.) {
1485 float x = DEMANDINPUT_A(0, inNumSamples);
1486 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1487 unit->m_val = unit->mParent->mRGen->frand() * (unit->m_hi - unit->m_lo) + unit->m_lo;
1489 if (unit->m_repeatCount >= unit->m_repeats) {
1490 OUT0(0) = NAN;
1491 return;
1493 unit->m_repeatCount++;
1494 OUT0(0) = unit->m_val;
1495 float x = unit->m_val + unit->mParent->mRGen->frand2() * unit->m_step;
1496 unit->m_val = sc_fold(x, unit->m_lo, unit->m_hi);
1497 } else {
1498 unit->m_repeats = -1.f;
1499 unit->m_repeatCount = 0;
1503 void Dbrown_Ctor(Dbrown *unit)
1505 SETCALC(Dbrown_next);
1506 Dbrown_next(unit, 0);
1507 OUT0(0) = 0.f;
1511 void Dibrown_next(Dibrown *unit, int inNumSamples)
1513 if (inNumSamples) {
1515 float lo = DEMANDINPUT_A(1, inNumSamples); if(!sc_isnan(lo)) { unit->m_lo = (int32)lo; }
1516 float hi = DEMANDINPUT_A(2, inNumSamples); if(!sc_isnan(hi)) { unit->m_hi = (int32)hi; }
1517 float step = DEMANDINPUT_A(3, inNumSamples); if(!sc_isnan(step)) { unit->m_step = (int32)step; }
1519 if (unit->m_repeats < 0.) {
1520 float x = DEMANDINPUT_A(0, inNumSamples);
1521 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1522 unit->m_val = unit->mParent->mRGen->irand(unit->m_hi - unit->m_lo + 1) + unit->m_lo;
1524 if (unit->m_repeatCount >= unit->m_repeats) {
1525 OUT0(0) = NAN;
1526 return;
1528 OUT0(0) = unit->m_val;
1529 int32 z = unit->m_val + unit->mParent->mRGen->irand2(unit->m_step);
1530 unit->m_val = sc_fold(z, unit->m_lo, unit->m_hi);
1531 } else {
1532 unit->m_repeats = -1.f;
1533 unit->m_repeatCount = 0;
1537 void Dibrown_Ctor(Dibrown *unit)
1539 SETCALC(Dibrown_next);
1540 Dibrown_next(unit, 0);
1541 OUT0(0) = 0.f;
1546 void Dseq_next(Dseq *unit, int inNumSamples)
1548 //Print("->Dseq_next %d\n", inNumSamples);
1549 if (inNumSamples) {
1550 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1551 if (unit->m_repeats < 0.) {
1552 float x = DEMANDINPUT_A(0, inNumSamples);
1553 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1555 int attempts = 0;
1556 while (true) {
1557 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1558 if (unit->m_index >= unit->mNumInputs) {
1559 unit->m_index = 1;
1560 unit->m_repeatCount++;
1562 if (unit->m_repeatCount >= unit->m_repeats) {
1563 //Print("done\n");
1564 OUT0(0) = NAN;
1565 unit->m_index = 1;
1566 return;
1568 if (ISDEMANDINPUT(unit->m_index)) {
1569 if (unit->m_needToResetChild) {
1570 unit->m_needToResetChild = false;
1571 RESETINPUT(unit->m_index);
1573 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1574 if (sc_isnan(x)) {
1575 unit->m_index++;
1576 unit->m_needToResetChild = true;
1577 } else {
1578 OUT0(0) = x;
1579 return;
1581 } else {
1582 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1583 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1584 unit->m_index++;
1585 unit->m_needToResetChild = true;
1586 return;
1589 if (attempts++ > unit->mNumInputs) {
1590 Print("Warning: empty sequence in Dseq\n");
1591 return;
1594 } else {
1595 unit->m_repeats = -1.f;
1596 unit->m_repeatCount = 0;
1597 unit->m_needToResetChild = true;
1598 unit->m_index = 1;
1603 void Dseq_Ctor(Dseq *unit)
1605 SETCALC(Dseq_next);
1606 Dseq_next(unit, 0);
1607 OUT0(0) = 0.f;
1611 void Dser_next(Dser *unit, int inNumSamples)
1613 if (inNumSamples) {
1614 if (unit->m_repeats < 0.) {
1615 float x = DEMANDINPUT_A(0, inNumSamples);
1616 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1618 while (true) {
1619 if (unit->m_index >= unit->mNumInputs) {
1620 unit->m_index = 1;
1622 if (unit->m_repeatCount >= unit->m_repeats) {
1623 OUT0(0) = NAN;
1624 return;
1626 if (ISDEMANDINPUT(unit->m_index)) {
1627 if (unit->m_needToResetChild) {
1628 unit->m_needToResetChild = false;
1629 RESETINPUT(unit->m_index);
1631 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1632 if (sc_isnan(x)) {
1633 unit->m_index++;
1634 unit->m_repeatCount++;
1635 unit->m_needToResetChild = true;
1636 } else {
1637 OUT0(0) = x;
1638 return;
1640 } else {
1641 OUT0(0) = IN0(unit->m_index);
1642 unit->m_index++;
1643 unit->m_repeatCount++;
1644 unit->m_needToResetChild = true;
1645 return;
1648 } else {
1649 unit->m_repeats = -1.f;
1650 unit->m_repeatCount = 0;
1651 unit->m_needToResetChild = true;
1652 unit->m_index = 1;
1656 void Dser_Ctor(Dser *unit)
1658 SETCALC(Dser_next);
1659 Dser_next(unit, 0);
1660 OUT0(0) = 0.f;
1665 void Drand_next(Drand *unit, int inNumSamples)
1667 if (inNumSamples) {
1669 if (unit->m_repeats < 0.) {
1670 float x = DEMANDINPUT_A(0, inNumSamples);
1671 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1673 while (true) {
1674 if (unit->m_repeatCount >= unit->m_repeats) {
1675 OUT0(0) = NAN;
1676 return;
1678 if (ISDEMANDINPUT(unit->m_index)) {
1679 if (unit->m_needToResetChild) {
1680 unit->m_needToResetChild = false;
1681 RESETINPUT(unit->m_index);
1683 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1684 if (sc_isnan(x)) {
1685 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1686 unit->m_repeatCount++;
1687 unit->m_needToResetChild = true;
1688 } else {
1689 OUT0(0) = x;
1690 return;
1692 } else {
1693 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1694 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1695 unit->m_repeatCount++;
1696 unit->m_needToResetChild = true;
1697 return;
1700 } else {
1701 unit->m_repeats = -1.f;
1702 unit->m_repeatCount = 0;
1703 unit->m_needToResetChild = true;
1704 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1708 void Drand_Ctor(Drand *unit)
1710 SETCALC(Drand_next);
1711 Drand_next(unit, 0);
1712 OUT0(0) = 0.f;
1717 void Dxrand_next(Dxrand *unit, int inNumSamples)
1719 if (inNumSamples) {
1720 if (unit->m_repeats < 0.) {
1721 float x = DEMANDINPUT_A(0, inNumSamples);
1722 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1724 while (true) {
1725 if (unit->m_index >= unit->mNumInputs) {
1726 unit->m_index = 1;
1728 if (unit->m_repeatCount >= unit->m_repeats) {
1729 OUT0(0) = NAN;
1730 return;
1732 if (ISDEMANDINPUT(unit->m_index)) {
1733 if (unit->m_needToResetChild) {
1734 unit->m_needToResetChild = false;
1735 RESETINPUT(unit->m_index);
1737 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1738 if (sc_isnan(x)) {
1739 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1740 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1741 unit->m_repeatCount++;
1742 unit->m_needToResetChild = true;
1743 } else {
1744 OUT0(0) = x;
1745 return;
1747 } else {
1748 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1749 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1750 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1751 unit->m_repeatCount++;
1752 unit->m_needToResetChild = true;
1753 return;
1756 } else {
1757 unit->m_repeats = -1.f;
1758 unit->m_repeatCount = 0;
1759 unit->m_needToResetChild = true;
1760 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1761 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1765 void Dxrand_Ctor(Dxrand *unit)
1767 SETCALC(Dxrand_next);
1768 Dxrand_next(unit, 0);
1769 OUT0(0) = 0.f;
1772 #define WINDEX \
1773 float w, sum = 0.0; \
1774 float r = unit->mParent->mRGen->frand(); \
1775 for (int i=0; i<weights_size; ++i) { \
1776 w = IN0(2 + i); \
1777 sum += w; \
1778 if (sum >= r) { \
1779 unit->m_index = i + offset; \
1780 break; \
1785 void Dwrand_next(Dwrand *unit, int inNumSamples)
1787 int offset = unit->m_weights_size + 2;
1788 int weights_size = unit->mNumInputs - offset;
1789 if (inNumSamples) {
1791 if (unit->m_repeats < 0.) {
1792 float x = DEMANDINPUT_A(0, inNumSamples);
1793 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1795 while (true) {
1797 if (unit->m_repeatCount >= unit->m_repeats) {
1798 OUT0(0) = NAN;
1799 return;
1802 if (ISDEMANDINPUT(unit->m_index)) {
1803 if (unit->m_needToResetChild) {
1804 unit->m_needToResetChild = false;
1805 RESETINPUT(unit->m_index);
1807 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1808 if (sc_isnan(x)) {
1810 WINDEX;
1811 unit->m_repeatCount++;
1812 unit->m_needToResetChild = true;
1813 } else {
1814 OUT0(0) = x;
1815 return;
1817 } else {
1818 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1819 WINDEX;
1820 unit->m_repeatCount++;
1821 unit->m_needToResetChild = true;
1822 return;
1825 } else {
1826 unit->m_repeats = -1.f;
1827 unit->m_repeatCount = 0;
1828 unit->m_needToResetChild = true;
1829 WINDEX;
1833 void Dwrand_Ctor(Dwrand *unit)
1835 SETCALC(Dwrand_next);
1836 unit->m_weights_size = IN0(1);
1837 Dwrand_next(unit, 0);
1838 OUT0(0) = 0.f;
1843 static void Dshuf_scramble(Dshuf *unit);
1845 void Dshuf_next(Dshuf *unit, int inNumSamples)
1847 // Print("->Dshuf_next %d\n", inNumSamples);
1848 if (inNumSamples) {
1849 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1850 if (unit->m_repeats < 0.) {
1851 float x = DEMANDINPUT_A(0, inNumSamples);
1852 unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1854 while (true) {
1855 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1856 if (unit->m_index >= (unit->mNumInputs - 1)) {
1857 unit->m_index = 0;
1858 unit->m_repeatCount++;
1860 if (unit->m_repeatCount >= unit->m_repeats) {
1861 //Print("done\n");
1862 OUT0(0) = NAN;
1863 unit->m_index = 0;
1864 return;
1866 if (ISDEMANDINPUT(unit->m_indices[unit->m_index])) {
1867 if (unit->m_needToResetChild) {
1868 unit->m_needToResetChild = false;
1869 RESETINPUT(unit->m_indices[unit->m_index]);
1871 float x = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1873 if (sc_isnan(x)) {
1874 unit->m_index++;
1875 unit->m_needToResetChild = true;
1876 } else {
1877 OUT0(0) = x;
1878 return;
1880 } else {
1881 OUT0(0) = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1882 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1883 unit->m_index++;
1884 unit->m_needToResetChild = true;
1885 return;
1888 } else {
1889 unit->m_repeats = -1.f;
1890 unit->m_repeatCount = 0;
1891 unit->m_needToResetChild = true;
1892 unit->m_index = 0;
1893 Dshuf_scramble(unit);
1897 void Dshuf_scramble(Dshuf *unit) {
1898 int32 i, j, m, k, size;
1899 int32 temp;
1901 size = (int32)(unit->mNumInputs) - 1;
1903 if (size > 1) {
1904 k = size;
1905 for (i=0, m=k; i<k-1; ++i, --m) {
1906 j = i + unit->mParent->mRGen->irand(m);
1907 temp = unit->m_indices[i];
1908 unit->m_indices[i] = unit->m_indices[j];
1909 unit->m_indices[j] = temp;
1915 void Dshuf_Ctor(Dshuf *unit)
1917 int32 i, size;
1919 size = (int32)(unit->mNumInputs) - 1;
1921 unit->m_indices = (int32*)RTAlloc(unit->mWorld, size * sizeof(int32));
1923 for(i=0; i < size; ++i) {
1924 unit->m_indices[i] = i + 1;
1927 SETCALC(Dshuf_next);
1928 Dshuf_next(unit, 0);
1929 OUT0(0) = 0.f;
1932 void Dshuf_Dtor(Dshuf *unit)
1934 RTFree(unit->mWorld, unit->m_indices);
1938 void Dswitch1_next(Dswitch1 *unit, int inNumSamples)
1940 if (inNumSamples) {
1941 float x = DEMANDINPUT_A(0, inNumSamples);
1942 if (sc_isnan(x)) {
1943 OUT0(0) = x;
1944 return;
1946 int index = (int32)floor(x + 0.5f);
1947 index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1948 OUT0(0) = DEMANDINPUT_A(index, inNumSamples);
1949 } else {
1950 for (int i=0; i<unit->mNumInputs; ++i) {
1951 RESETINPUT(i);
1956 void Dswitch1_Ctor(Dswitch1 *unit)
1958 SETCALC(Dswitch1_next);
1959 OUT0(0) = 0.f;
1962 /////////////////////////////
1964 void Dswitch_next(Dswitch *unit, int inNumSamples)
1966 if (inNumSamples) {
1967 float val = DEMANDINPUT_A(unit->m_index, inNumSamples);
1968 //printf("index: %i\n", (int) val);
1969 if(sc_isnan(val)) {
1970 float ival = DEMANDINPUT_A(0, inNumSamples);
1972 if(sc_isnan(ival))
1973 val = ival;
1974 else {
1975 int index = (int32)floor(ival + 0.5f);
1976 index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1977 val = DEMANDINPUT_A(index, inNumSamples);
1979 RESETINPUT(unit->m_index);
1980 // printf("resetting index: %i\n", unit->m_index);
1981 unit->m_index = index;
1984 OUT0(0) = val;
1986 } else {
1987 printf("...\n");
1988 for (int i=0; i<unit->mNumInputs; ++i) {
1989 RESETINPUT(i);
1991 int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
1992 index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
1993 unit->m_index = index;
1997 void Dswitch_Ctor(Dswitch *unit)
1999 SETCALC(Dswitch_next);
2000 int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
2001 index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
2002 unit->m_index = index;
2003 OUT0(0) = 0.f;
2006 //////////////////////////////
2009 void Dstutter_next(Dstutter *unit, int inNumSamples)
2011 if (inNumSamples) {
2013 if (unit->m_repeatCount >= unit->m_repeats) {
2015 float val = DEMANDINPUT_A(1, inNumSamples);
2016 float repeats = DEMANDINPUT_A(0, inNumSamples);
2018 if(sc_isnan(repeats) || sc_isnan(val)) {
2019 OUT0(0) = NAN;
2020 return;
2021 } else {
2022 unit->m_value = val;
2023 unit->m_repeats = floor(repeats + 0.5f);
2024 unit->m_repeatCount = 0.f;
2028 OUT0(0) = unit->m_value;
2029 unit->m_repeatCount++;
2031 } else {
2033 unit->m_repeats = -1.f;
2034 unit->m_repeatCount = 0.f;
2035 RESETINPUT(0);
2036 RESETINPUT(1);
2041 void Dstutter_Ctor(Dstutter *unit)
2043 SETCALC(Dstutter_next);
2044 Dstutter_next(unit, 0);
2045 OUT0(0) = 0.f;
2048 //////////////////////////////
2052 void Dpoll_next(Dpoll *unit, int inNumSamples)
2054 if (inNumSamples) {
2055 float x = DEMANDINPUT_A(0, inNumSamples);
2056 float run = DEMANDINPUT_A(2, inNumSamples) > 0.f;
2057 if(unit->m_mayprint && run) {
2058 Print("%s: %g block offset: %d\n", unit->m_id_string, x, inNumSamples - 1);
2060 if(IN0(1) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(1), x);
2061 OUT0(0) = x;
2062 } else {
2063 RESETINPUT(0);
2067 void Dpoll_Ctor(Dpoll *unit)
2069 SETCALC(Dpoll_next);
2070 unit->m_id = IN0(3); // number of chars in the id string
2071 unit->m_id_string = (char*)RTAlloc(unit->mWorld, ((int)unit->m_id + 1) * sizeof(char));
2072 for(int i = 0; i < (int)unit->m_id; i++) {
2073 unit->m_id_string[i] = (char)IN0(4+i);
2075 unit->m_id_string[(int)unit->m_id] = '\0';
2076 unit->m_mayprint = unit->mWorld->mVerbosity >= -1;
2077 OUT0(0) = 0.f;
2080 void Dpoll_Dtor(Dpoll* unit)
2082 RTFree(unit->mWorld, unit->m_id_string);
2085 //////////////////////////////
2089 void Dreset_next(Dreset *unit, int inNumSamples)
2091 if (inNumSamples) {
2092 float x = DEMANDINPUT_A(0, inNumSamples);
2093 float reset = DEMANDINPUT_A(1, inNumSamples);
2094 if(sc_isnan(x)) {
2095 OUT0(0) = NAN;
2096 return;
2098 if(reset > 0.0 && (unit->prev_reset <= 0.0)) { RESETINPUT(0); }
2099 unit->prev_reset = reset;
2100 OUT0(0) = x;
2101 } else {
2102 RESETINPUT(0);
2106 void Dreset_Ctor(Dreset *unit)
2108 SETCALC(Dreset_next);
2109 unit->prev_reset = 0.0;
2110 Dreset_next(unit, 0);
2115 //////////////////////////////
2119 void Donce_next(Donce *unit, int inNumSamples)
2121 if (inNumSamples) {
2122 if (unit->m_bufcounter == unit->mWorld->mBufCounter) {
2123 OUT0(0) = unit->m_prev;
2124 } else {
2125 float x = DEMANDINPUT_A(0, inNumSamples);
2126 unit->m_prev = x;
2127 OUT0(0) = x;
2129 } else {
2130 RESETINPUT(0);
2134 void Donce_Ctor(Donce *unit)
2136 SETCALC(Donce_next);
2137 OUT0(0) = 0.f;
2141 inline double sc_loop(Unit *unit, double in, double hi, int loop)
2143 // avoid the divide if possible
2144 if (in >= hi) {
2145 if (!loop) {
2146 unit->mDone = true;
2147 return hi;
2149 in -= hi;
2150 if (in < hi) return in;
2151 } else if (in < 0.) {
2152 if (!loop) {
2153 unit->mDone = true;
2154 return 0.;
2156 in += hi;
2157 if (in >= 0.) return in;
2158 } else return in;
2160 return in - hi * floor(in/hi);
2163 #define D_CHECK_BUF \
2164 if (!bufData) { \
2165 unit->mDone = true; \
2166 ClearUnitOutputs(unit, 1); \
2167 return; \
2170 #define D_GET_BUF \
2171 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2172 if (fbufnum != unit->m_fbufnum) { \
2173 uint32 bufnum = (int)fbufnum; \
2174 World *world = unit->mWorld; \
2175 if (bufnum < 0) { bufnum = 0; } \
2176 if (bufnum >= world->mNumSndBufs) { \
2177 int localBufNum = bufnum - world->mNumSndBufs; \
2178 Graph *parent = unit->mParent; \
2179 if(localBufNum <= parent->localBufNum) { \
2180 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2181 } else { \
2182 bufnum = 0; \
2183 unit->m_buf = world->mSndBufs + bufnum; \
2185 } else { \
2186 unit->m_buf = world->mSndBufs + bufnum; \
2188 unit->m_fbufnum = fbufnum; \
2190 SndBuf *buf = unit->m_buf; \
2191 LOCK_SNDBUF(buf); \
2192 float *bufData __attribute__((__unused__)) = buf->data; \
2193 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2194 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2195 uint32 bufFrames = buf->frames; \
2196 int mask __attribute__((__unused__)) = buf->mask; \
2197 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2199 #define D_GET_BUF_SHARED \
2200 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2201 if (fbufnum != unit->m_fbufnum) { \
2202 uint32 bufnum = (int)fbufnum; \
2203 World *world = unit->mWorld; \
2204 if (bufnum < 0) { bufnum = 0; } \
2205 if (bufnum >= world->mNumSndBufs) { \
2206 int localBufNum = bufnum - world->mNumSndBufs; \
2207 Graph *parent = unit->mParent; \
2208 if(localBufNum <= parent->localBufNum) { \
2209 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2210 } else { \
2211 bufnum = 0; \
2212 unit->m_buf = world->mSndBufs + bufnum; \
2214 } else { \
2215 unit->m_buf = world->mSndBufs + bufnum; \
2217 unit->m_fbufnum = fbufnum; \
2219 const SndBuf *buf = unit->m_buf; \
2220 LOCK_SNDBUF_SHARED(buf); \
2221 const float *bufData __attribute__((__unused__)) = buf->data; \
2222 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2223 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2224 uint32 bufFrames = buf->frames; \
2225 int mask __attribute__((__unused__)) = buf->mask; \
2226 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2229 void Dbufrd_next(Dbufrd *unit, int inNumSamples)
2231 int32 loop = (int32)DEMANDINPUT_A(2, inNumSamples);
2233 D_GET_BUF_SHARED
2234 D_CHECK_BUF
2236 double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2238 double phase;
2239 if (inNumSamples)
2241 float x = DEMANDINPUT_A(1, inNumSamples);
2242 if (sc_isnan(x)) {
2243 OUT0(0) = NAN;
2244 return;
2246 phase = x;
2248 phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2249 int32 iphase = (int32)phase;
2250 const float* table1 = bufData + iphase * bufChannels;
2251 OUT0(0) = table1[0];
2253 else
2255 RESETINPUT(1);
2259 void Dbufrd_Ctor(Dbufrd *unit)
2261 SETCALC(Dbufrd_next);
2263 unit->m_fbufnum = -1e9f;
2265 Dbufrd_next(unit, 0);
2266 OUT0(0) = 0.f;
2269 ////////////////////////////////////
2271 void Dbufwr_next(Dbufwr *unit, int inNumSamples)
2273 int32 loop = (int32)DEMANDINPUT_A(3, inNumSamples);
2275 D_GET_BUF
2276 D_CHECK_BUF
2278 double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2280 double phase;
2281 float val;
2282 if (inNumSamples)
2284 float x = DEMANDINPUT_A(1, inNumSamples);
2285 if (sc_isnan(x)) {
2286 OUT0(0) = NAN;
2287 return;
2289 phase = x;
2290 val = DEMANDINPUT_A(2, inNumSamples);
2291 if (sc_isnan(val)) {
2292 OUT0(0) = NAN;
2293 return;
2296 phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2297 int32 iphase = (int32)phase;
2298 float* table0 = bufData + iphase * bufChannels;
2299 table0[0] = val;
2300 OUT0(0) = val;
2302 else
2304 RESETINPUT(1);
2305 RESETINPUT(2);
2309 void Dbufwr_Ctor(Dbufwr *unit)
2311 SETCALC(Dbufwr_next);
2313 unit->m_fbufnum = -1e9f;
2315 Dbufwr_next(unit, 0);
2316 OUT0(0) = 0.f;
2320 //////////////////////////////////////////////////////
2324 PluginLoad(Demand)
2326 ft = inTable;
2328 DefineDtorCantAliasUnit(Demand);
2329 DefineSimpleCantAliasUnit(Duty);
2330 DefineSimpleCantAliasUnit(DemandEnvGen);
2331 DefineSimpleCantAliasUnit(TDuty);
2332 DefineSimpleUnit(Dseries);
2333 DefineSimpleUnit(Dgeom);
2334 DefineSimpleUnit(Dwhite);
2335 DefineSimpleUnit(Dbrown);
2336 DefineSimpleUnit(Diwhite);
2337 DefineSimpleUnit(Dibrown);
2338 DefineSimpleUnit(Dseq);
2339 DefineSimpleUnit(Dser);
2340 DefineSimpleUnit(Dbufrd);
2341 DefineSimpleUnit(Dbufwr);
2342 DefineSimpleUnit(Drand);
2343 DefineSimpleUnit(Dwrand);
2344 DefineSimpleUnit(Dxrand);
2345 DefineDtorUnit(Dshuf);
2346 DefineSimpleUnit(Dswitch1);
2347 DefineSimpleUnit(Dswitch);
2348 DefineSimpleUnit(Dstutter);
2349 DefineSimpleUnit(Donce);
2350 DefineSimpleUnit(Dreset);
2351 DefineDtorUnit(Dpoll);