1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
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.
10 
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.
15 
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
19 */
20 
21 #include "SC_PlugIn.h"
22 #include <cstdio>
23 #include <cmath>
24 #include <limits>
25 
26 using std::floor;
27 using std::numeric_limits;
28 
29 static InterfaceTable* ft;
30 
31 struct Demand : public Unit {
32     float m_prevtrig;
33     float m_prevreset;
34     float* m_prevout;
35     float** m_out;
36 };
37 
38 struct Duty : public Unit {
39     float m_count;
40     float m_prevreset;
41     float m_prevout;
42 };
43 
44 struct DemandEnvGen : public Unit {
45     float m_phase;
46     float m_prevreset;
47 
48     double m_a1, m_a2, m_b1, m_y1, m_y2, m_grow, m_level, m_endLevel, m_curve;
49     int m_shape;
50     bool m_release, m_running;
51 };
52 
53 struct TDuty : public Unit {
54     float m_count;
55     float m_prevreset;
56 };
57 
58 
59 struct Dseries : public Unit {
60     double m_repeats;
61     int32 m_repeatCount;
62     double m_value;
63     double m_step;
64 };
65 
66 struct Dgeom : public Unit {
67     double m_repeats;
68     int32 m_repeatCount;
69     double m_value;
70     double m_grow;
71 };
72 
73 struct Dwhite : public Unit {
74     double m_repeats;
75     int32 m_repeatCount;
76     float m_lo;
77     float m_range;
78 };
79 
80 struct Dbrown : public Unit {
81     double m_repeats;
82     int32 m_repeatCount;
83     float m_lo;
84     float m_hi;
85     float m_step;
86     float m_val;
87 };
88 
89 struct Diwhite : public Unit {
90     double m_repeats;
91     int32 m_repeatCount;
92     int32 m_lo;
93     int32 m_range;
94 };
95 
96 struct Dibrown : public Unit {
97     double m_repeats;
98     int32 m_repeatCount;
99     int32 m_lo;
100     int32 m_hi;
101     int32 m_step;
102     int32 m_val;
103 };
104 
105 struct Dseq : public Unit {
106     double m_repeats;
107     int32 m_repeatCount;
108     int32 m_index;
109     bool m_needToResetChild;
110 };
111 
112 struct Dshuf : public Unit {
113     double m_repeats;
114     int32 m_repeatCount;
115     int32 m_index;
116     bool m_needToResetChild;
117     int32* m_indices;
118 };
119 
120 struct Dbufrd : public Unit {
121     float m_fbufnum;
122     SndBuf* m_buf;
123 };
124 
125 struct Dbufwr : public Unit {
126     float m_fbufnum;
127     SndBuf* m_buf;
128 };
129 
130 struct Dser : public Dseq {};
131 
132 struct Drand : public Dseq {};
133 
134 struct Dxrand : public Dseq {};
135 
136 struct Dwrand : public Dseq {
137     int32 m_weights_size;
138 };
139 
140 struct Dswitch1 : public Unit {};
141 
142 struct Dswitch : public Unit {
143     int m_index;
144 };
145 
146 struct Dstutter : public Unit {
147     double m_repeats;
148     double m_repeatCount;
149     float m_value;
150 };
151 
152 struct Dpoll : public Unit {
153     char* m_id_string;
154     bool m_mayprint;
155     float m_id;
156 };
157 
158 struct Dreset : public Unit {
159     float prev_reset;
160 };
161 
162 struct Dconst : public Unit {
163     float m_total;
164     float m_runningsum;
165     float m_tolerance;
166 };
167 
168 extern "C" {
169 
170 
171 void Demand_Ctor(Demand* unit);
172 void Demand_Dtor(Demand* unit);
173 void Demand_next_aa(Demand* unit, int inNumSamples);
174 void Demand_next_ak(Demand* unit, int inNumSamples);
175 void Demand_next_ka(Demand* unit, int inNumSamples);
176 
177 void Duty_Ctor(Duty* unit);
178 void Duty_next_da(Duty* unit, int inNumSamples);
179 void Duty_next_dk(Duty* unit, int inNumSamples);
180 void Duty_next_dd(Duty* unit, int inNumSamples);
181 
182 void TDuty_Ctor(TDuty* unit);
183 void TDuty_next_da(TDuty* unit, int inNumSamples);
184 void TDuty_next_dk(TDuty* unit, int inNumSamples);
185 void TDuty_next_dd(TDuty* unit, int inNumSamples);
186 
187 void DemandEnvGen_Ctor(DemandEnvGen* unit);
188 void DemandEnvGen_next_k(DemandEnvGen* unit, int inNumSamples);
189 void DemandEnvGen_next_a(DemandEnvGen* unit, int inNumSamples);
190 
191 void Dseries_Ctor(Dseries* unit);
192 void Dseries_next(Dseries* unit, int inNumSamples);
193 
194 void Dgeom_Ctor(Dgeom* unit);
195 void Dgeom_next(Dgeom* unit, int inNumSamples);
196 
197 void Dwhite_Ctor(Dwhite* unit);
198 void Dwhite_next(Dwhite* unit, int inNumSamples);
199 
200 void Dbrown_Ctor(Dbrown* unit);
201 void Dbrown_next(Dbrown* unit, int inNumSamples);
202 
203 void Diwhite_Ctor(Diwhite* unit);
204 void Diwhite_next(Diwhite* unit, int inNumSamples);
205 
206 void Dibrown_Ctor(Dibrown* unit);
207 void Dibrown_next(Dibrown* unit, int inNumSamples);
208 
209 void Dseq_Ctor(Dseq* unit);
210 void Dseq_next(Dseq* unit, int inNumSamples);
211 
212 void Dshuf_Ctor(Dshuf* unit);
213 void Dshuf_Dtor(Dshuf* unit);
214 void Dshuf_next(Dshuf* unit, int inNumSamples);
215 
216 
217 void Dbufrd_Ctor(Dbufrd* unit);
218 void Dbufrd_next(Dbufrd* unit, int inNumSamples);
219 
220 void Dbufwr_Ctor(Dbufwr* unit);
221 void Dbufwr_next(Dbufwr* unit, int inNumSamples);
222 
223 void Dser_Ctor(Dser* unit);
224 void Dser_next(Dser* unit, int inNumSamples);
225 
226 void Drand_Ctor(Drand* unit);
227 void Drand_next(Drand* unit, int inNumSamples);
228 
229 void Dxrand_Ctor(Dxrand* unit);
230 void Dxrand_next(Dxrand* unit, int inNumSamples);
231 
232 void Dwrand_Ctor(Dwrand* unit);
233 void Dwrand_next(Dwrand* unit, int inNumSamples);
234 
235 void Dswitch1_Ctor(Dswitch1* unit);
236 void Dswitch1_next(Dswitch1* unit, int inNumSamples);
237 
238 void Dswitch_Ctor(Dswitch* unit);
239 void Dswitch_next(Dswitch* unit, int inNumSamples);
240 
241 void Dstutter_Ctor(Dstutter* unit);
242 void Dstutter_next(Dstutter* unit, int inNumSamples);
243 
244 void Dpoll_Ctor(Dpoll* unit);
245 void Dpoll_Ctor(Dpoll* unit);
246 void Dpoll_next(Dpoll* unit, int inNumSamples);
247 
248 void Dreset_Ctor(Dreset* unit);
249 void Dreset_next(Dreset* unit, int inNumSamples);
250 
251 void Dconst_Ctor(Dconst* unit);
252 void Dconst_next(Dconst* unit, int inNumSamples);
253 };
254 
255 
Demand_next_aa(Demand * unit,int inNumSamples)256 void Demand_next_aa(Demand* unit, int inNumSamples) {
257     float* trig = ZIN(0);
258     float* reset = ZIN(1);
259 
260     float** out = unit->m_out;
261     float* prevout = unit->m_prevout;
262 
263     for (int i = 0; i < unit->mNumOutputs; ++i) {
264         out[i] = OUT(i);
265     }
266 
267     float prevtrig = unit->m_prevtrig;
268     float prevreset = unit->m_prevreset;
269 
270     // Print("Demand_next_aa %d  %g\n", inNumSamples, prevtrig);
271     for (int i = 0; i < inNumSamples; ++i) {
272         float ztrig = ZXP(trig);
273         float zreset = ZXP(reset);
274         if (zreset > 0.f && prevreset <= 0.f) {
275             for (int j = 2; j < unit->mNumInputs; ++j) {
276                 RESETINPUT(j);
277             }
278         }
279         if (ztrig > 0.f && prevtrig <= 0.f) {
280             // Print("triggered\n");
281             for (int j = 2, k = 0; j < unit->mNumInputs; ++j, ++k) {
282                 float x = DEMANDINPUT_A(j, i + 1);
283                 // printf("in  %d %g\n", k, x);
284                 if (sc_isnan(x)) {
285                     x = prevout[k];
286                     unit->mDone = true;
287                 } else
288                     prevout[k] = x;
289                 out[k][i] = x;
290             }
291         } else {
292             for (int j = 2, k = 0; j < unit->mNumInputs; ++j, ++k) {
293                 out[k][i] = prevout[k];
294             }
295         }
296         prevtrig = ztrig;
297         prevreset = zreset;
298     }
299 
300     unit->m_prevtrig = prevtrig;
301     unit->m_prevreset = prevreset;
302 }
303 
304 
Demand_next_ak(Demand * unit,int inNumSamples)305 void Demand_next_ak(Demand* unit, int inNumSamples) {
306     float* trig = ZIN(0);
307     float zreset = IN0(1);
308 
309     float** out = unit->m_out;
310     float* prevout = unit->m_prevout;
311 
312     for (int i = 0; i < unit->mNumOutputs; ++i) {
313         out[i] = OUT(i);
314     }
315 
316     float prevtrig = unit->m_prevtrig;
317     float prevreset = unit->m_prevreset;
318 
319     for (int i = 0; i < inNumSamples; ++i) {
320         float ztrig = ZXP(trig);
321         if (zreset > 0.f && prevreset <= 0.f) {
322             for (int j = 2; j < unit->mNumInputs; ++j) {
323                 RESETINPUT(j);
324             }
325         }
326 
327         if (ztrig > 0.f && prevtrig <= 0.f) {
328             for (int j = 2, k = 0; j < unit->mNumInputs; ++j, ++k) {
329                 float x = DEMANDINPUT_A(j, i + 1);
330                 if (sc_isnan(x)) {
331                     x = prevout[k];
332                     unit->mDone = true;
333                 } else
334                     prevout[k] = x;
335                 out[k][i] = x;
336             }
337 
338         } else {
339             for (int j = 2, k = 0; j < unit->mNumInputs; ++j, ++k) {
340                 out[k][i] = prevout[k];
341             }
342         }
343         prevtrig = ztrig;
344         prevreset = zreset;
345     }
346 
347     unit->m_prevtrig = prevtrig;
348     unit->m_prevreset = prevreset;
349 }
350 
351 
Demand_next_ka(Demand * unit,int inNumSamples)352 void Demand_next_ka(Demand* unit, int inNumSamples) {
353     float ztrig = IN0(0);
354     float* reset = ZIN(1);
355 
356     float** out = unit->m_out;
357     float* prevout = unit->m_prevout;
358 
359     for (int i = 0; i < unit->mNumOutputs; ++i) {
360         out[i] = OUT(i);
361     }
362 
363     float prevtrig = unit->m_prevtrig;
364     float prevreset = unit->m_prevreset;
365 
366     for (int i = 0; i < inNumSamples; ++i) {
367         float zreset = ZXP(reset);
368         if (zreset > 0.f && prevreset <= 0.f) {
369             for (int j = 2; j < unit->mNumInputs; ++j) {
370                 RESETINPUT(j);
371             }
372         }
373         if (ztrig > 0.f && prevtrig <= 0.f) {
374             for (int j = 2, k = 0; j < unit->mNumInputs; ++j, ++k) {
375                 float x = DEMANDINPUT_A(j, i + 1);
376                 if (sc_isnan(x)) {
377                     x = prevout[k];
378                     unit->mDone = true;
379                 } else
380                     prevout[k] = x;
381                 out[k][i] = x;
382             }
383         }
384         prevtrig = ztrig;
385         prevreset = zreset;
386     }
387 
388     unit->m_prevtrig = prevtrig;
389     unit->m_prevreset = prevreset;
390 }
391 
392 
Demand_Ctor(Demand * unit)393 void Demand_Ctor(Demand* unit) {
394     // Print("Demand_Ctor\n");
395     if (INRATE(0) == calc_FullRate) {
396         if (INRATE(1) == calc_FullRate) {
397             SETCALC(Demand_next_aa);
398         } else {
399             SETCALC(Demand_next_ak);
400         }
401     } else {
402         if (INRATE(1) == calc_FullRate) {
403             SETCALC(Demand_next_ka);
404         } else {
405             SETCALC(Demand_next_aa);
406         }
407     }
408 
409     for (int i = 0; i < unit->mNumOutputs; ++i)
410         OUT0(i) = 0.f;
411 
412     char* memoryChunk = (char*)RTAlloc(unit->mWorld, unit->mNumOutputs * (sizeof(float) + sizeof(float*)));
413 
414     if (!memoryChunk) {
415         Print("Demand: RT memory allocation failed\n");
416         SETCALC(ClearUnitOutputs);
417         return;
418     }
419 
420     unit->m_prevout = (float*)memoryChunk;
421     unit->m_out = (float**)(memoryChunk + unit->mNumOutputs * sizeof(float));
422 
423     unit->m_prevtrig = 0.f;
424     unit->m_prevreset = 0.f;
425     std::fill_n(unit->m_prevout, unit->mNumOutputs, 0.f);
426 }
427 
Demand_Dtor(Demand * unit)428 void Demand_Dtor(Demand* unit) {
429     if (unit->m_prevout)
430         RTFree(unit->mWorld, unit->m_prevout);
431 }
432 
433 
434 /////////////////////////////////////////////////////////////////////////////
435 
436 enum { duty_dur, duty_reset, duty_doneAction, duty_level };
437 
Duty_next_da(Duty * unit,int inNumSamples)438 void Duty_next_da(Duty* unit, int inNumSamples) {
439     float* reset = ZIN(duty_reset);
440 
441     float* out = OUT(0);
442     float prevout = unit->m_prevout;
443     float count = unit->m_count;
444     float prevreset = unit->m_prevreset;
445     float sr = (float)SAMPLERATE;
446 
447     for (int i = 0; i < inNumSamples; ++i) {
448         float zreset = ZXP(reset);
449         if (zreset > 0.f && prevreset <= 0.f) {
450             RESETINPUT(duty_level);
451             RESETINPUT(duty_dur);
452             count = 0.f;
453         }
454         if (count <= 0.f) {
455             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
456             if (sc_isnan(count)) {
457                 int doneAction = (int)ZIN0(duty_doneAction);
458                 DoneAction(doneAction, unit);
459             }
460             float x = DEMANDINPUT_A(duty_level, i + 1);
461             // printf("in  %d\n", count);
462             if (sc_isnan(x)) {
463                 x = prevout;
464                 int doneAction = (int)ZIN0(duty_doneAction);
465                 DoneAction(doneAction, unit);
466             } else {
467                 prevout = x;
468             }
469             out[i] = x;
470 
471         } else {
472             out[i] = prevout;
473         }
474         count--;
475         prevreset = zreset;
476     }
477 
478     unit->m_count = count;
479     unit->m_prevreset = prevreset;
480     unit->m_prevout = prevout;
481 }
482 
Duty_next_dk(Duty * unit,int inNumSamples)483 void Duty_next_dk(Duty* unit, int inNumSamples) {
484     float zreset = ZIN0(duty_reset);
485 
486     float* out = OUT(0);
487     float prevout = unit->m_prevout;
488     float count = unit->m_count;
489     float prevreset = unit->m_prevreset;
490     float sr = (float)SAMPLERATE;
491 
492     for (int i = 0; i < inNumSamples; ++i) {
493         if (zreset > 0.f && prevreset <= 0.f) {
494             RESETINPUT(duty_level);
495             RESETINPUT(duty_dur);
496             count = 0.f;
497         }
498         if (count <= 0.f) {
499             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
500             if (sc_isnan(count)) {
501                 int doneAction = (int)ZIN0(duty_doneAction);
502                 DoneAction(doneAction, unit);
503             }
504 
505             float x = DEMANDINPUT_A(duty_level, i + 1);
506             if (sc_isnan(x)) {
507                 x = prevout;
508                 int doneAction = (int)ZIN0(duty_doneAction);
509                 DoneAction(doneAction, unit);
510             } else {
511                 prevout = x;
512             }
513 
514             out[i] = x;
515 
516         } else {
517             out[i] = prevout;
518         }
519         count--;
520         prevreset = zreset;
521     }
522 
523     unit->m_count = count;
524     unit->m_prevreset = prevreset;
525     unit->m_prevout = prevout;
526 }
527 
528 
Duty_next_dd(Duty * unit,int inNumSamples)529 void Duty_next_dd(Duty* unit, int inNumSamples) {
530     float* out = OUT(0);
531     float prevout = unit->m_prevout;
532     float count = unit->m_count;
533     float reset = unit->m_prevreset;
534     float sr = (float)SAMPLERATE;
535 
536     for (int i = 0; i < inNumSamples; ++i) {
537         if (reset <= 0.f) {
538             RESETINPUT(duty_level);
539             RESETINPUT(duty_dur);
540             count = 0.f;
541             reset = DEMANDINPUT_A(duty_reset, i + 1) * sr + reset;
542         } else {
543             reset--;
544         }
545         if (count <= 0.f) {
546             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
547             if (sc_isnan(count)) {
548                 int doneAction = (int)ZIN0(duty_doneAction);
549                 DoneAction(doneAction, unit);
550             }
551             float x = DEMANDINPUT_A(duty_level, i + 1);
552             // printf("in  %d %g\n", k, x);
553             if (sc_isnan(x)) {
554                 x = prevout;
555                 int doneAction = (int)ZIN0(duty_doneAction);
556                 DoneAction(doneAction, unit);
557             } else {
558                 prevout = x;
559             }
560         }
561 
562         out[i] = prevout;
563         count--;
564     }
565 
566     unit->m_count = count;
567     unit->m_prevreset = reset;
568     unit->m_prevout = prevout;
569 }
570 
571 
Duty_Ctor(Duty * unit)572 void Duty_Ctor(Duty* unit) {
573     // DEMANDINPUT_A is not needed here, because we are at init time.
574 
575     if (INRATE(duty_reset) == calc_FullRate) {
576         SETCALC(Duty_next_da);
577         unit->m_prevreset = 0.f;
578 
579     } else {
580         if (INRATE(duty_reset) == calc_DemandRate) {
581             SETCALC(Duty_next_dd);
582             unit->m_prevreset = DEMANDINPUT(duty_reset) * SAMPLERATE;
583         } else {
584             SETCALC(Duty_next_dk);
585             unit->m_prevreset = 0.f;
586         }
587     }
588 
589     unit->m_count = DEMANDINPUT(duty_dur) * SAMPLERATE;
590     unit->m_prevout = DEMANDINPUT(duty_level);
591     OUT0(0) = unit->m_prevout;
592 }
593 
594 
595 /////////////////////////////////////////////////////////////////////////////
596 
597 enum {
598     d_env_level,
599     d_env_dur,
600     d_env_shape,
601     d_env_curve,
602     d_env_gate,
603     d_env_reset,
604     d_env_levelScale,
605     d_env_levelBias,
606     d_env_timeScale,
607     d_env_doneAction
608 
609 };
610 
611 enum {
612     shape_Step,
613     shape_Linear,
614     shape_Exponential,
615     shape_Sine,
616     shape_Welch,
617     shape_Curve,
618     shape_Squared,
619     shape_Cubed,
620     shape_Sustain = 9999
621 };
622 
623 
DemandEnvGen_next_k(DemandEnvGen * unit,int inNumSamples)624 void DemandEnvGen_next_k(DemandEnvGen* unit, int inNumSamples) {
625     float zreset = ZIN0(d_env_reset);
626 
627     float* out = ZOUT(0);
628     double level = unit->m_level;
629     float phase = unit->m_phase;
630     double curve = unit->m_curve;
631     bool release = unit->m_release;
632     bool running = unit->m_running;
633     int shape = unit->m_shape;
634 
635     // printf("phase %f level %f \n", phase, level);
636 
637     for (int i = 0; i < inNumSamples; ++i) {
638         if (zreset > 0.f && unit->m_prevreset <= 0.f) {
639             // printf("reset: %f %f \n", zreset, unit->m_prevreset);
640             RESETINPUT(d_env_level);
641             RESETINPUT(d_env_dur);
642             RESETINPUT(d_env_shape);
643             RESETINPUT(d_env_curve);
644 
645             if (zreset <= 1.f) {
646                 DEMANDINPUT(d_env_level); // remove first level
647             } else {
648                 level = DEMANDINPUT(d_env_level); // jump to first level
649             }
650 
651             release = false;
652             running = true;
653             phase = 0.f;
654         }
655 
656 
657         if (phase <= 0.f && running) {
658             // was a release during last segment?
659             if (release) {
660                 running = false;
661                 release = false;
662                 // printf("release: %f %f \n", phase, level);
663                 int doneAction = (int)ZIN0(d_env_doneAction);
664                 DoneAction(doneAction, unit);
665 
666             } else {
667                 // new time
668 
669                 float dur = DEMANDINPUT(d_env_dur);
670                 // printf("dur: %f \n", dur);
671                 if (sc_isnan(dur)) {
672                     release = true;
673                     running = false;
674                     phase = numeric_limits<float>::max();
675                 } else {
676                     phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
677                 }
678 
679 
680                 // new shape
681                 float fshape = DEMANDINPUT(d_env_shape);
682                 if (sc_isnan(fshape))
683                     shape = unit->m_shape;
684                 else
685                     shape = (int)fshape;
686 
687                 curve = DEMANDINPUT(d_env_curve);
688                 if (sc_isnan(curve))
689                     curve = unit->m_curve;
690 
691                 float count;
692                 if (phase <= 1.f) {
693                     shape = 1; // shape_Linear
694                     count = 1.f;
695                 } else {
696                     count = phase;
697                 }
698                 if (dur * 0.5f < SAMPLEDUR)
699                     shape = 1;
700 
701                 // printf("shape: %i, curve: %f, dur: %f \n", shape, curve, dur);
702 
703 
704                 // new end level
705 
706                 double endLevel = DEMANDINPUT(d_env_level);
707                 // printf("levels: %f %f\n", level, endLevel);
708                 if (sc_isnan(endLevel)) {
709                     endLevel = unit->m_endLevel;
710                     release = true;
711                     phase = 0.f;
712                     shape = 0;
713                 } else {
714                     endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
715                     unit->m_endLevel = endLevel;
716                 }
717 
718 
719                 // calculate shape parameters
720 
721                 switch (shape) {
722                 case shape_Step: {
723                     level = endLevel;
724                 } break;
725                 case shape_Linear: {
726                     unit->m_grow = (endLevel - level) / count;
727                 } break;
728                 case shape_Exponential: {
729                     unit->m_grow = pow(endLevel / level, 1.0 / count);
730                 } break;
731                 case shape_Sine: {
732                     double w = pi / count;
733 
734                     unit->m_a2 = (endLevel + level) * 0.5;
735                     unit->m_b1 = 2. * cos(w);
736                     unit->m_y1 = (endLevel - level) * 0.5;
737                     unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
738                     level = unit->m_a2 - unit->m_y1;
739                 } break;
740                 case shape_Welch: {
741                     double w = (pi * 0.5) / count;
742 
743                     unit->m_b1 = 2. * cos(w);
744 
745                     if (endLevel >= level) {
746                         unit->m_a2 = level;
747                         unit->m_y1 = 0.;
748                         unit->m_y2 = -sin(w) * (endLevel - level);
749                     } else {
750                         unit->m_a2 = endLevel;
751                         unit->m_y1 = level - endLevel;
752                         unit->m_y2 = cos(w) * (level - endLevel);
753                     }
754                     level = unit->m_a2 + unit->m_y1;
755                 } break;
756                 case shape_Curve: {
757                     if (fabs(curve) < 0.001) {
758                         unit->m_shape = shape = 1; // shape_Linear
759                         unit->m_grow = (endLevel - level) / count;
760                     } else {
761                         double a1 = (endLevel - level) / (1.0 - exp(curve));
762                         unit->m_a2 = level + a1;
763                         unit->m_b1 = a1;
764                         unit->m_grow = exp(curve / ceil(count));
765                     }
766                 } break;
767                 case shape_Squared: {
768                     unit->m_y1 = sqrt(level);
769                     unit->m_y2 = sqrt(endLevel);
770                     unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
771                 } break;
772                 case shape_Cubed: {
773                     unit->m_y1 = pow(level, 0.33333333);
774                     unit->m_y2 = pow(endLevel, 0.33333333);
775                     unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
776                 } break;
777                 }
778             }
779         }
780 
781 
782         if (running) {
783             switch (shape) {
784             case shape_Step: {
785             } break;
786             case shape_Linear: {
787                 double grow = unit->m_grow;
788                 // Print("level %g\n", level);
789                 level += grow;
790             } break;
791             case shape_Exponential: {
792                 double grow = unit->m_grow;
793                 level *= grow;
794             } break;
795             case shape_Sine: {
796                 double a2 = unit->m_a2;
797                 double b1 = unit->m_b1;
798                 double y2 = unit->m_y2;
799                 double y1 = unit->m_y1;
800                 double y0 = b1 * y1 - y2;
801                 level = a2 - y0;
802                 y2 = y1;
803                 y1 = y0;
804                 unit->m_y1 = y1;
805                 unit->m_y2 = y2;
806             } break;
807             case shape_Welch: {
808                 double a2 = unit->m_a2;
809                 double b1 = unit->m_b1;
810                 double y2 = unit->m_y2;
811                 double y1 = unit->m_y1;
812                 double y0 = b1 * y1 - y2;
813                 level = a2 + y0;
814                 y2 = y1;
815                 y1 = y0;
816                 unit->m_y1 = y1;
817                 unit->m_y2 = y2;
818             } break;
819             case shape_Curve: {
820                 double a2 = unit->m_a2;
821                 double b1 = unit->m_b1;
822                 double grow = unit->m_grow;
823                 b1 *= grow;
824                 level = a2 - b1;
825                 unit->m_b1 = b1;
826             } break;
827             case shape_Squared: {
828                 double grow = unit->m_grow;
829                 double y1 = unit->m_y1;
830                 y1 += grow;
831                 level = y1 * y1;
832                 unit->m_y1 = y1;
833             } break;
834             case shape_Cubed: {
835                 double grow = unit->m_grow;
836                 double y1 = unit->m_y1;
837                 y1 += grow;
838                 level = y1 * y1 * y1;
839                 unit->m_y1 = y1;
840             } break;
841             case shape_Sustain: {
842             } break;
843             }
844 
845             phase--;
846         }
847 
848 
849         ZXP(out) = level;
850     }
851     float zgate = ZIN0(d_env_gate);
852     if (zgate >= 1.f) {
853         unit->m_running = true;
854     } else if (zgate > 0.f) {
855         unit->m_running = true;
856         release = true; // release next time.
857     } else {
858         unit->m_running = false; // sample and hold
859     }
860 
861     unit->m_level = level;
862     unit->m_curve = curve;
863     unit->m_shape = shape;
864     unit->m_prevreset = zreset;
865     unit->m_release = release;
866 
867     unit->m_phase = phase;
868 }
869 
870 
DemandEnvGen_next_a(DemandEnvGen * unit,int inNumSamples)871 void DemandEnvGen_next_a(DemandEnvGen* unit, int inNumSamples) {
872     float* reset = ZIN(d_env_reset);
873     float* gate = ZIN(d_env_gate);
874 
875     float* out = ZOUT(0);
876 
877     float prevreset = unit->m_prevreset;
878     double level = unit->m_level;
879     float phase = unit->m_phase;
880     double curve = unit->m_curve;
881     bool release = unit->m_release;
882     bool running = unit->m_running;
883 
884 
885     int shape = unit->m_shape;
886     // printf("phase %f \n", phase);
887 
888     for (int i = 0; i < inNumSamples; ++i) {
889         float zreset = ZXP(reset);
890         if (zreset > 0.f && prevreset <= 0.f) {
891             // printf("reset: %f %f \n", zreset, unit->m_prevreset);
892             RESETINPUT(d_env_level);
893             if (zreset <= 1.f) {
894                 DEMANDINPUT_A(d_env_level, i + 1); // remove first level
895             } else {
896                 level = DEMANDINPUT_A(d_env_level, i + 1); // jump to first level
897             }
898 
899             RESETINPUT(d_env_dur);
900             RESETINPUT(d_env_shape);
901             release = false;
902             running = true;
903 
904             phase = 0.f;
905         }
906 
907         prevreset = zreset;
908 
909 
910         if (phase <= 0.f && running) {
911             // was a release?
912             if (release) {
913                 running = false;
914                 release = false;
915                 // printf("release: %f %f \n", phase, level);
916                 int doneAction = (int)ZIN0(d_env_doneAction);
917                 DoneAction(doneAction, unit);
918 
919             } else {
920                 // new time
921 
922                 float dur = DEMANDINPUT_A(d_env_dur, i + 1);
923                 // printf("dur: %f \n", dur);
924                 if (sc_isnan(dur)) {
925                     release = true;
926                     running = false;
927                     phase = numeric_limits<float>::max();
928                 } else {
929                     phase = dur * ZIN0(d_env_timeScale) * SAMPLERATE + phase;
930                 }
931 
932                 // new shape
933                 float count;
934                 curve = DEMANDINPUT_A(d_env_curve, i + 1);
935 
936                 // printf("shapes: %i \n", shape);
937                 if (sc_isnan(curve))
938                     curve = unit->m_curve;
939 
940                 float fshape = DEMANDINPUT_A(d_env_shape, i + 1);
941                 if (sc_isnan(fshape))
942                     shape = unit->m_shape;
943                 else
944                     shape = (int)fshape;
945 
946                 if (phase <= 1.f) {
947                     shape = 1; // shape_Linear
948                     count = 1.f;
949                 } else {
950                     count = phase;
951                 }
952                 if (dur * 0.5f < SAMPLEDUR)
953                     shape = 1;
954 
955 
956                 // new end level
957 
958                 double endLevel = DEMANDINPUT_A(d_env_level, i + 1);
959                 // printf("levels: %f %f\n", level, endLevel);
960                 if (sc_isnan(endLevel)) {
961                     endLevel = unit->m_endLevel;
962                     release = true;
963                     phase = 0.f;
964                     shape = 0;
965                 } else {
966                     endLevel = endLevel * ZIN0(d_env_levelScale) + ZIN0(d_env_levelBias);
967                     unit->m_endLevel = endLevel;
968                 }
969 
970 
971                 // calculate shape parameters
972 
973                 switch (shape) {
974                 case shape_Step: {
975                     level = endLevel;
976                 } break;
977                 case shape_Linear: {
978                     unit->m_grow = (endLevel - level) / count;
979                 } break;
980                 case shape_Exponential: {
981                     unit->m_grow = pow(endLevel / level, 1.0 / count);
982                 } break;
983                 case shape_Sine: {
984                     double w = pi / count;
985 
986                     unit->m_a2 = (endLevel + level) * 0.5;
987                     unit->m_b1 = 2. * cos(w);
988                     unit->m_y1 = (endLevel - level) * 0.5;
989                     unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
990                     level = unit->m_a2 - unit->m_y1;
991                 } break;
992                 case shape_Welch: {
993                     double w = (pi * 0.5) / count;
994 
995                     unit->m_b1 = 2. * cos(w);
996 
997                     if (endLevel >= level) {
998                         unit->m_a2 = level;
999                         unit->m_y1 = 0.;
1000                         unit->m_y2 = -sin(w) * (endLevel - level);
1001                     } else {
1002                         unit->m_a2 = endLevel;
1003                         unit->m_y1 = level - endLevel;
1004                         unit->m_y2 = cos(w) * (level - endLevel);
1005                     }
1006                     level = unit->m_a2 + unit->m_y1;
1007                 } break;
1008                 case shape_Curve: {
1009                     if (fabs(curve) < 0.001) {
1010                         unit->m_shape = shape = 1; // shape_Linear
1011                         unit->m_grow = (endLevel - level) / count;
1012                     } else {
1013                         double a1 = (endLevel - level) / (1.0 - exp(curve));
1014                         unit->m_a2 = level + a1;
1015                         unit->m_b1 = a1;
1016                         unit->m_grow = exp(curve / ceil(count));
1017                     }
1018                 } break;
1019                 case shape_Squared: {
1020                     unit->m_y1 = sqrt(level);
1021                     unit->m_y2 = sqrt(endLevel);
1022                     unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1023                 } break;
1024                 case shape_Cubed: {
1025                     unit->m_y1 = pow(level, 0.33333333);
1026                     unit->m_y2 = pow(endLevel, 0.33333333);
1027                     unit->m_grow = (unit->m_y2 - unit->m_y1) / count;
1028                 } break;
1029                 }
1030             }
1031         }
1032 
1033 
1034         if (running) {
1035             switch (shape) {
1036             case shape_Step: {
1037             } break;
1038             case shape_Linear: {
1039                 double grow = unit->m_grow;
1040                 // Print("level %g\n", level);
1041                 level += grow;
1042             } break;
1043             case shape_Exponential: {
1044                 double grow = unit->m_grow;
1045                 level *= grow;
1046             } break;
1047             case shape_Sine: {
1048                 double a2 = unit->m_a2;
1049                 double b1 = unit->m_b1;
1050                 double y2 = unit->m_y2;
1051                 double y1 = unit->m_y1;
1052                 double y0 = b1 * y1 - y2;
1053                 level = a2 - y0;
1054                 y2 = y1;
1055                 y1 = y0;
1056                 unit->m_y1 = y1;
1057                 unit->m_y2 = y2;
1058             } break;
1059             case shape_Welch: {
1060                 double a2 = unit->m_a2;
1061                 double b1 = unit->m_b1;
1062                 double y2 = unit->m_y2;
1063                 double y1 = unit->m_y1;
1064                 double y0 = b1 * y1 - y2;
1065                 level = a2 + y0;
1066                 y2 = y1;
1067                 y1 = y0;
1068                 unit->m_y1 = y1;
1069                 unit->m_y2 = y2;
1070             } break;
1071             case shape_Curve: {
1072                 double a2 = unit->m_a2;
1073                 double b1 = unit->m_b1;
1074                 double grow = unit->m_grow;
1075                 b1 *= grow;
1076                 level = a2 - b1;
1077                 unit->m_b1 = b1;
1078             } break;
1079             case shape_Squared: {
1080                 double grow = unit->m_grow;
1081                 double y1 = unit->m_y1;
1082                 y1 += grow;
1083                 level = y1 * y1;
1084                 unit->m_y1 = y1;
1085             } break;
1086             case shape_Cubed: {
1087                 double grow = unit->m_grow;
1088                 double y1 = unit->m_y1;
1089                 y1 += grow;
1090                 level = y1 * y1 * y1;
1091                 unit->m_y1 = y1;
1092             } break;
1093             case shape_Sustain: {
1094             } break;
1095             }
1096 
1097             phase--;
1098         }
1099 
1100         ZXP(out) = level;
1101         float zgate = ZXP(gate);
1102 
1103         if (zgate >= 1.f) {
1104             unit->m_running = true;
1105         } else if (zgate > 0.f) {
1106             unit->m_running = true;
1107             release = true; // release next time.
1108         } else {
1109             unit->m_running = false; // sample and hold
1110         }
1111     }
1112 
1113     unit->m_level = level;
1114     unit->m_curve = curve;
1115     unit->m_shape = shape;
1116     unit->m_prevreset = prevreset;
1117     unit->m_release = release;
1118     unit->m_phase = phase;
1119 }
1120 
1121 
DemandEnvGen_Ctor(DemandEnvGen * unit)1122 void DemandEnvGen_Ctor(DemandEnvGen* unit) {
1123     // derive the first level.
1124 
1125     unit->m_level = DEMANDINPUT(d_env_level);
1126     if (sc_isnan(unit->m_level)) {
1127         unit->m_level = 0.f;
1128     }
1129     unit->m_endLevel = unit->m_level;
1130     unit->m_release = false;
1131     unit->m_prevreset = 0.f;
1132     unit->m_phase = 0.f;
1133     unit->m_running = ZIN0(d_env_gate) > 0.f;
1134 
1135     if (INRATE(d_env_gate) == calc_FullRate) {
1136         SETCALC(DemandEnvGen_next_a);
1137     } else {
1138         SETCALC(DemandEnvGen_next_k);
1139     }
1140 
1141     DemandEnvGen_next_k(unit, 1);
1142 }
1143 
1144 
1145 /////////////////////////////////////////////////////////////////////////////
1146 
TDuty_next_da(TDuty * unit,int inNumSamples)1147 void TDuty_next_da(TDuty* unit, int inNumSamples) {
1148     float* reset = ZIN(duty_reset);
1149     float* out = OUT(0);
1150 
1151     float count = unit->m_count;
1152     float prevreset = unit->m_prevreset;
1153     float sr = (float)SAMPLERATE;
1154 
1155     for (int i = 0; i < inNumSamples; ++i) {
1156         float zreset = ZXP(reset);
1157         if (zreset > 0.f && prevreset <= 0.f) {
1158             RESETINPUT(duty_level);
1159             RESETINPUT(duty_dur);
1160             count = 0.f;
1161         }
1162         if (count <= 0.f) {
1163             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1164             if (sc_isnan(count)) {
1165                 int doneAction = (int)ZIN0(2);
1166                 DoneAction(doneAction, unit);
1167             }
1168             float x = DEMANDINPUT_A(duty_level, i + 1);
1169             // printf("in  %d %g\n", k, x);
1170             if (sc_isnan(x))
1171                 x = 0.f;
1172             out[i] = x;
1173         } else {
1174             out[i] = 0.f;
1175         }
1176         count--;
1177         prevreset = zreset;
1178     }
1179 
1180     unit->m_count = count;
1181     unit->m_prevreset = prevreset;
1182 }
1183 
TDuty_next_dk(TDuty * unit,int inNumSamples)1184 void TDuty_next_dk(TDuty* unit, int inNumSamples) {
1185     float zreset = ZIN0(duty_reset);
1186 
1187     float* out = OUT(0);
1188     float count = unit->m_count;
1189     float prevreset = unit->m_prevreset;
1190     float sr = (float)SAMPLERATE;
1191 
1192     for (int i = 0; i < inNumSamples; ++i) {
1193         if (zreset > 0.f && prevreset <= 0.f) {
1194             RESETINPUT(duty_level);
1195             RESETINPUT(duty_dur);
1196             count = 0.f;
1197         }
1198         if (count <= 0.f) {
1199             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1200             if (sc_isnan(count)) {
1201                 int doneAction = (int)ZIN0(2);
1202                 DoneAction(doneAction, unit);
1203             }
1204             float x = DEMANDINPUT_A(duty_level, i + 1);
1205             // printf("in  %d %g\n", k, x);
1206             if (sc_isnan(x))
1207                 x = 0.f;
1208             out[i] = x;
1209         } else {
1210             out[i] = 0.f;
1211         }
1212         count--;
1213         prevreset = zreset;
1214     }
1215 
1216     unit->m_count = count;
1217     unit->m_prevreset = prevreset;
1218 }
1219 
1220 
TDuty_next_dd(TDuty * unit,int inNumSamples)1221 void TDuty_next_dd(TDuty* unit, int inNumSamples) {
1222     float* out = OUT(0);
1223     float count = unit->m_count;
1224     float reset = unit->m_prevreset;
1225     float sr = (float)SAMPLERATE;
1226 
1227     for (int i = 0; i < inNumSamples; ++i) {
1228         if (reset <= 0.f) {
1229             RESETINPUT(duty_level);
1230             RESETINPUT(duty_dur);
1231             count = 0.f;
1232             reset = DEMANDINPUT_A(duty_reset, i + 1) * sr + reset;
1233         } else {
1234             reset--;
1235         }
1236         if (count <= 0.f) {
1237             count = DEMANDINPUT_A(duty_dur, i + 1) * sr + count;
1238             if (sc_isnan(count)) {
1239                 int doneAction = (int)ZIN0(2);
1240                 DoneAction(doneAction, unit);
1241             }
1242             float x = DEMANDINPUT_A(duty_level, i + 1);
1243             // printf("in  %d %g\n", k, x);
1244             if (sc_isnan(x))
1245                 x = 0.f;
1246             out[i] = x;
1247         } else {
1248             out[i] = 0.f;
1249         }
1250         count--;
1251     }
1252 
1253     unit->m_count = count;
1254     unit->m_prevreset = reset;
1255 }
1256 
1257 
TDuty_Ctor(TDuty * unit)1258 void TDuty_Ctor(TDuty* unit) {
1259     // DEMANDINPUT_A is not needed here, because we are at init time.
1260     if (INRATE(1) == calc_FullRate) {
1261         SETCALC(TDuty_next_da);
1262         unit->m_prevreset = 0.f;
1263 
1264     } else {
1265         if (INRATE(1) == calc_DemandRate) {
1266             SETCALC(TDuty_next_dd);
1267             unit->m_prevreset = DEMANDINPUT(duty_reset) * SAMPLERATE;
1268         } else {
1269             SETCALC(TDuty_next_dk);
1270             unit->m_prevreset = 0.f;
1271         }
1272     }
1273     // support for gap-first.
1274     if (IN0(4)) {
1275         unit->m_count = DEMANDINPUT(duty_dur) * SAMPLERATE;
1276     } else {
1277         unit->m_count = 0.f;
1278     }
1279     OUT0(0) = 0.f;
1280 }
1281 
1282 
1283 /////////////////////////////////////////////////////////////////////////////
1284 
1285 
Dseries_next(Dseries * unit,int inNumSamples)1286 void Dseries_next(Dseries* unit, int inNumSamples) {
1287     if (inNumSamples) {
1288         float step = DEMANDINPUT_A(2, inNumSamples);
1289         if (!sc_isnan(step)) {
1290             unit->m_step = step;
1291         }
1292         if (unit->m_repeats < 0.) {
1293             float x = DEMANDINPUT_A(0, inNumSamples);
1294             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1295             unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1296         }
1297         if (unit->m_repeatCount >= unit->m_repeats) {
1298             OUT0(0) = NAN;
1299             return;
1300         }
1301         OUT0(0) = unit->m_value;
1302         unit->m_value += unit->m_step;
1303         unit->m_repeatCount++;
1304     } else {
1305         unit->m_repeats = -1.f;
1306         unit->m_repeatCount = 0;
1307     }
1308 }
1309 
Dseries_Ctor(Dseries * unit)1310 void Dseries_Ctor(Dseries* unit) {
1311     SETCALC(Dseries_next);
1312     Dseries_next(unit, 0);
1313     OUT0(0) = 0.f;
1314 }
1315 
1316 
Dgeom_next(Dgeom * unit,int inNumSamples)1317 void Dgeom_next(Dgeom* unit, int inNumSamples) {
1318     if (inNumSamples) {
1319         float grow = DEMANDINPUT_A(2, inNumSamples);
1320         if (!sc_isnan(grow)) {
1321             unit->m_grow = grow;
1322         }
1323         if (unit->m_repeats < 0.) {
1324             float x = DEMANDINPUT_A(0, inNumSamples);
1325             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1326             unit->m_value = DEMANDINPUT_A(1, inNumSamples);
1327         }
1328         if (unit->m_repeatCount >= unit->m_repeats) {
1329             OUT0(0) = NAN;
1330             return;
1331         }
1332         OUT0(0) = unit->m_value;
1333         unit->m_value *= unit->m_grow;
1334         unit->m_repeatCount++;
1335     } else {
1336         unit->m_repeats = -1.f;
1337         unit->m_repeatCount = 0;
1338     }
1339 }
1340 
Dgeom_Ctor(Dgeom * unit)1341 void Dgeom_Ctor(Dgeom* unit) {
1342     SETCALC(Dgeom_next);
1343     Dgeom_next(unit, 0);
1344     OUT0(0) = 0.f;
1345 }
1346 
1347 
Dwhite_next(Dwhite * unit,int inNumSamples)1348 void Dwhite_next(Dwhite* unit, int inNumSamples) {
1349     if (inNumSamples) {
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         }
1354         if (unit->m_repeatCount >= unit->m_repeats) {
1355             OUT0(0) = NAN;
1356             return;
1357         }
1358         unit->m_repeatCount++;
1359         float lo = DEMANDINPUT_A(1, inNumSamples);
1360         float hi = DEMANDINPUT_A(2, inNumSamples);
1361 
1362         if (!sc_isnan(lo)) {
1363             unit->m_lo = lo;
1364         }
1365         if (!sc_isnan(hi)) {
1366             unit->m_range = hi - lo;
1367         }
1368         float x = unit->mParent->mRGen->frand() * unit->m_range + unit->m_lo;
1369         OUT0(0) = x;
1370     } else {
1371         unit->m_repeats = -1.f;
1372         unit->m_repeatCount = 0;
1373     }
1374 }
1375 
Dwhite_Ctor(Dwhite * unit)1376 void Dwhite_Ctor(Dwhite* unit) {
1377     SETCALC(Dwhite_next);
1378     Dwhite_next(unit, 0);
1379     OUT0(0) = 0.f;
1380 }
1381 
1382 
Diwhite_next(Diwhite * unit,int inNumSamples)1383 void Diwhite_next(Diwhite* unit, int inNumSamples) {
1384     if (inNumSamples) {
1385         float lo = DEMANDINPUT_A(1, inNumSamples);
1386         float hi = DEMANDINPUT_A(2, inNumSamples);
1387 
1388         if (!sc_isnan(lo)) {
1389             unit->m_lo = (int32)floor(DEMANDINPUT_A(1, inNumSamples) + 0.5f);
1390         }
1391         if (!sc_isnan(hi)) {
1392             int32 hi = (int32)floor(DEMANDINPUT_A(2, inNumSamples) + 0.5f);
1393             unit->m_range = hi - unit->m_lo + 1;
1394         }
1395 
1396         if (unit->m_repeats < 0.) {
1397             float x = DEMANDINPUT_A(0, inNumSamples);
1398             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1399         }
1400         if (unit->m_repeatCount >= unit->m_repeats) {
1401             OUT0(0) = NAN;
1402             return;
1403         }
1404         unit->m_repeatCount++;
1405         float x = unit->mParent->mRGen->irand(unit->m_range) + unit->m_lo;
1406         OUT0(0) = x;
1407     } else {
1408         unit->m_repeats = -1.f;
1409         unit->m_repeatCount = 0;
1410     }
1411 }
1412 
Diwhite_Ctor(Diwhite * unit)1413 void Diwhite_Ctor(Diwhite* unit) {
1414     SETCALC(Diwhite_next);
1415     Diwhite_next(unit, 0);
1416     OUT0(0) = 0.f;
1417 }
1418 
1419 
Dbrown_next(Dbrown * unit,int inNumSamples)1420 void Dbrown_next(Dbrown* unit, int inNumSamples) {
1421     if (inNumSamples) {
1422         float lo = DEMANDINPUT_A(1, inNumSamples);
1423         if (!sc_isnan(lo)) {
1424             unit->m_lo = lo;
1425         }
1426         float hi = DEMANDINPUT_A(2, inNumSamples);
1427         if (!sc_isnan(hi)) {
1428             unit->m_hi = hi;
1429         }
1430         float step = DEMANDINPUT_A(3, inNumSamples);
1431         if (!sc_isnan(step)) {
1432             unit->m_step = step;
1433         }
1434 
1435         if (unit->m_repeats < 0.) {
1436             float x = DEMANDINPUT_A(0, inNumSamples);
1437             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1438             unit->m_val = unit->mParent->mRGen->frand() * (unit->m_hi - unit->m_lo) + unit->m_lo;
1439         }
1440         if (unit->m_repeatCount >= unit->m_repeats) {
1441             OUT0(0) = NAN;
1442             return;
1443         }
1444         unit->m_repeatCount++;
1445         OUT0(0) = unit->m_val;
1446         float x = unit->m_val + unit->mParent->mRGen->frand2() * unit->m_step;
1447         unit->m_val = sc_fold(x, unit->m_lo, unit->m_hi);
1448     } else {
1449         unit->m_repeats = -1.f;
1450         unit->m_repeatCount = 0;
1451     }
1452 }
1453 
Dbrown_Ctor(Dbrown * unit)1454 void Dbrown_Ctor(Dbrown* unit) {
1455     SETCALC(Dbrown_next);
1456     Dbrown_next(unit, 0);
1457     OUT0(0) = 0.f;
1458 }
1459 
1460 
Dibrown_next(Dibrown * unit,int inNumSamples)1461 void Dibrown_next(Dibrown* unit, int inNumSamples) {
1462     if (inNumSamples) {
1463         float lo = DEMANDINPUT_A(1, inNumSamples);
1464         if (!sc_isnan(lo)) {
1465             unit->m_lo = (int32)lo;
1466         }
1467         float hi = DEMANDINPUT_A(2, inNumSamples);
1468         if (!sc_isnan(hi)) {
1469             unit->m_hi = (int32)hi;
1470         }
1471         float step = DEMANDINPUT_A(3, inNumSamples);
1472         if (!sc_isnan(step)) {
1473             unit->m_step = (int32)step;
1474         }
1475 
1476         if (unit->m_repeats < 0.) {
1477             float x = DEMANDINPUT_A(0, inNumSamples);
1478             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1479             unit->m_val = unit->mParent->mRGen->irand(unit->m_hi - unit->m_lo + 1) + unit->m_lo;
1480         }
1481         if (unit->m_repeatCount >= unit->m_repeats) {
1482             OUT0(0) = NAN;
1483             return;
1484         }
1485         unit->m_repeatCount++;
1486         OUT0(0) = unit->m_val;
1487         int32 z = unit->m_val + unit->mParent->mRGen->irand2(unit->m_step);
1488         unit->m_val = sc_fold(z, unit->m_lo, unit->m_hi);
1489     } else {
1490         unit->m_repeats = -1.f;
1491         unit->m_repeatCount = 0;
1492     }
1493 }
1494 
Dibrown_Ctor(Dibrown * unit)1495 void Dibrown_Ctor(Dibrown* unit) {
1496     SETCALC(Dibrown_next);
1497     Dibrown_next(unit, 0);
1498     OUT0(0) = 0.f;
1499 }
1500 
1501 
Dseq_next(Dseq * unit,int inNumSamples)1502 void Dseq_next(Dseq* unit, int inNumSamples) {
1503     // Print("->Dseq_next %d\n", inNumSamples);
1504     if (inNumSamples) {
1505         // Print("   unit->m_repeats %d\n", unit->m_repeats);
1506         if (unit->m_repeats < 0.) {
1507             float x = DEMANDINPUT_A(0, inNumSamples);
1508             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1509         }
1510         int attempts = 0;
1511         while (true) {
1512             // Print("   unit->m_index %d   unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1513             if (unit->m_index >= unit->mNumInputs) {
1514                 unit->m_index = 1;
1515                 unit->m_repeatCount++;
1516             }
1517             if (unit->m_repeatCount >= unit->m_repeats) {
1518                 // Print("done\n");
1519                 OUT0(0) = NAN;
1520                 unit->m_index = 1;
1521                 return;
1522             }
1523             if (ISDEMANDINPUT(unit->m_index)) {
1524                 if (unit->m_needToResetChild) {
1525                     unit->m_needToResetChild = false;
1526                     RESETINPUT(unit->m_index);
1527                 }
1528                 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1529                 if (sc_isnan(x)) {
1530                     unit->m_index++;
1531                     unit->m_needToResetChild = true;
1532                 } else {
1533                     OUT0(0) = x;
1534                     return;
1535                 }
1536             } else {
1537                 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1538                 // Print("   unit->m_index %d   OUT0(0) %g\n", unit->m_index, OUT0(0));
1539                 unit->m_index++;
1540                 unit->m_needToResetChild = true;
1541                 return;
1542             }
1543 
1544             if (attempts++ > unit->mNumInputs) {
1545                 Print("Warning: empty sequence in Dseq\n");
1546                 return;
1547             }
1548         }
1549     } else {
1550         unit->m_repeats = -1.f;
1551         unit->m_repeatCount = 0;
1552         unit->m_needToResetChild = true;
1553         unit->m_index = 1;
1554     }
1555 }
1556 
1557 
Dseq_Ctor(Dseq * unit)1558 void Dseq_Ctor(Dseq* unit) {
1559     SETCALC(Dseq_next);
1560     Dseq_next(unit, 0);
1561     OUT0(0) = 0.f;
1562 }
1563 
1564 
Dser_next(Dser * unit,int inNumSamples)1565 void Dser_next(Dser* unit, int inNumSamples) {
1566     if (inNumSamples) {
1567         if (unit->m_repeats < 0.) {
1568             float x = DEMANDINPUT_A(0, inNumSamples);
1569             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1570         }
1571         while (true) {
1572             if (unit->m_index >= unit->mNumInputs) {
1573                 unit->m_index = 1;
1574             }
1575             if (unit->m_repeatCount >= unit->m_repeats) {
1576                 OUT0(0) = NAN;
1577                 return;
1578             }
1579             if (ISDEMANDINPUT(unit->m_index)) {
1580                 if (unit->m_needToResetChild) {
1581                     unit->m_needToResetChild = false;
1582                     RESETINPUT(unit->m_index);
1583                 }
1584                 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1585                 if (sc_isnan(x)) {
1586                     unit->m_index++;
1587                     unit->m_repeatCount++;
1588                     unit->m_needToResetChild = true;
1589                 } else {
1590                     OUT0(0) = x;
1591                     return;
1592                 }
1593             } else {
1594                 OUT0(0) = IN0(unit->m_index);
1595                 unit->m_index++;
1596                 unit->m_repeatCount++;
1597                 unit->m_needToResetChild = true;
1598                 return;
1599             }
1600         }
1601     } else {
1602         unit->m_repeats = -1.f;
1603         unit->m_repeatCount = 0;
1604         unit->m_needToResetChild = true;
1605         unit->m_index = 1;
1606     }
1607 }
1608 
Dser_Ctor(Dser * unit)1609 void Dser_Ctor(Dser* unit) {
1610     SETCALC(Dser_next);
1611     Dser_next(unit, 0);
1612     OUT0(0) = 0.f;
1613 }
1614 
1615 
Drand_next(Drand * unit,int inNumSamples)1616 void Drand_next(Drand* unit, int inNumSamples) {
1617     if (inNumSamples) {
1618         if (unit->m_repeats < 0.) {
1619             float x = DEMANDINPUT_A(0, inNumSamples);
1620             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1621         }
1622         while (true) {
1623             if (unit->m_repeatCount >= unit->m_repeats) {
1624                 OUT0(0) = NAN;
1625                 return;
1626             }
1627             if (ISDEMANDINPUT(unit->m_index)) {
1628                 if (unit->m_needToResetChild) {
1629                     unit->m_needToResetChild = false;
1630                     RESETINPUT(unit->m_index);
1631                 }
1632                 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1633                 if (sc_isnan(x)) {
1634                     unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1635                     unit->m_repeatCount++;
1636                     unit->m_needToResetChild = true;
1637                 } else {
1638                     OUT0(0) = x;
1639                     return;
1640                 }
1641             } else {
1642                 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1643                 unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1644                 unit->m_repeatCount++;
1645                 unit->m_needToResetChild = true;
1646                 return;
1647             }
1648         }
1649     } else {
1650         unit->m_repeats = -1.f;
1651         unit->m_repeatCount = 0;
1652         unit->m_needToResetChild = true;
1653         unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
1654     }
1655 }
1656 
Drand_Ctor(Drand * unit)1657 void Drand_Ctor(Drand* unit) {
1658     SETCALC(Drand_next);
1659     Drand_next(unit, 0);
1660     OUT0(0) = 0.f;
1661 }
1662 
1663 
Dxrand_next(Dxrand * unit,int inNumSamples)1664 void Dxrand_next(Dxrand* unit, int inNumSamples) {
1665     if (inNumSamples) {
1666         if (unit->m_repeats < 0.) {
1667             float x = DEMANDINPUT_A(0, inNumSamples);
1668             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1669         }
1670         while (true) {
1671             if (unit->m_index >= unit->mNumInputs) {
1672                 unit->m_index = 1;
1673             }
1674             if (unit->m_repeatCount >= unit->m_repeats) {
1675                 OUT0(0) = NAN;
1676                 return;
1677             }
1678             if (ISDEMANDINPUT(unit->m_index)) {
1679                 if (unit->m_needToResetChild) {
1680                     unit->m_needToResetChild = false;
1681                     RESETINPUT(unit->m_index);
1682                 }
1683                 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1684                 if (sc_isnan(x)) {
1685                     int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1686                     unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1687                     unit->m_repeatCount++;
1688                     unit->m_needToResetChild = true;
1689                 } else {
1690                     OUT0(0) = x;
1691                     return;
1692                 }
1693             } else {
1694                 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1695                 int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1696                 unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1697                 unit->m_repeatCount++;
1698                 unit->m_needToResetChild = true;
1699                 return;
1700             }
1701         }
1702     } else {
1703         unit->m_repeats = -1.f;
1704         unit->m_repeatCount = 0;
1705         unit->m_needToResetChild = true;
1706         int newindex = unit->mParent->mRGen->irand(unit->mNumInputs - 2) + 1;
1707         unit->m_index = newindex < unit->m_index ? newindex : newindex + 1;
1708     }
1709 }
1710 
Dxrand_Ctor(Dxrand * unit)1711 void Dxrand_Ctor(Dxrand* unit) {
1712     SETCALC(Dxrand_next);
1713     Dxrand_next(unit, 0);
1714     OUT0(0) = 0.f;
1715 }
1716 
1717 #define WINDEX                                                                                                         \
1718     float w, sum = 0.0;                                                                                                \
1719     float r = unit->mParent->mRGen->frand();                                                                           \
1720     for (int i = 0; i < weights_size; ++i) {                                                                           \
1721         w = IN0(2 + i);                                                                                                \
1722         sum += w;                                                                                                      \
1723         if (sum >= r) {                                                                                                \
1724             unit->m_index = i + offset;                                                                                \
1725             break;                                                                                                     \
1726         }                                                                                                              \
1727     }
1728 
1729 
Dwrand_next(Dwrand * unit,int inNumSamples)1730 void Dwrand_next(Dwrand* unit, int inNumSamples) {
1731     int offset = unit->m_weights_size + 2;
1732     int weights_size = unit->mNumInputs - offset;
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);
1737         }
1738         while (true) {
1739             if (unit->m_repeatCount >= unit->m_repeats) {
1740                 OUT0(0) = NAN;
1741                 return;
1742             }
1743 
1744             if (ISDEMANDINPUT(unit->m_index)) {
1745                 if (unit->m_needToResetChild) {
1746                     unit->m_needToResetChild = false;
1747                     RESETINPUT(unit->m_index);
1748                 }
1749                 float x = DEMANDINPUT_A(unit->m_index, inNumSamples);
1750                 if (sc_isnan(x)) {
1751                     WINDEX;
1752                     unit->m_repeatCount++;
1753                     unit->m_needToResetChild = true;
1754                 } else {
1755                     OUT0(0) = x;
1756                     return;
1757                 }
1758             } else {
1759                 OUT0(0) = DEMANDINPUT_A(unit->m_index, inNumSamples);
1760                 WINDEX;
1761                 unit->m_repeatCount++;
1762                 unit->m_needToResetChild = true;
1763                 return;
1764             }
1765         }
1766     } else {
1767         unit->m_repeats = -1.f;
1768         unit->m_repeatCount = 0;
1769         unit->m_needToResetChild = true;
1770         WINDEX;
1771     }
1772 }
1773 
Dwrand_Ctor(Dwrand * unit)1774 void Dwrand_Ctor(Dwrand* unit) {
1775     SETCALC(Dwrand_next);
1776     unit->m_weights_size = IN0(1);
1777     Dwrand_next(unit, 0);
1778     OUT0(0) = 0.f;
1779 }
1780 
1781 
1782 static void Dshuf_scramble(Dshuf* unit);
1783 
Dshuf_next(Dshuf * unit,int inNumSamples)1784 void Dshuf_next(Dshuf* unit, int inNumSamples) {
1785     // Print("->Dshuf_next %d\n", inNumSamples);
1786     if (inNumSamples) {
1787         // Print("   unit->m_repeats %d\n", unit->m_repeats);
1788         if (unit->m_repeats < 0.) {
1789             float x = DEMANDINPUT_A(0, inNumSamples);
1790             unit->m_repeats = sc_isnan(x) ? 0.f : floor(x + 0.5f);
1791         }
1792         while (true) {
1793             // Print("   unit->m_index %d   unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1794             if (unit->m_index >= (unit->mNumInputs - 1)) {
1795                 unit->m_index = 0;
1796                 unit->m_repeatCount++;
1797             }
1798             if (unit->m_repeatCount >= unit->m_repeats) {
1799                 // Print("done\n");
1800                 OUT0(0) = NAN;
1801                 unit->m_index = 0;
1802                 return;
1803             }
1804             if (ISDEMANDINPUT(unit->m_indices[unit->m_index])) {
1805                 if (unit->m_needToResetChild) {
1806                     unit->m_needToResetChild = false;
1807                     RESETINPUT(unit->m_indices[unit->m_index]);
1808                 }
1809                 float x = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1810 
1811                 if (sc_isnan(x)) {
1812                     unit->m_index++;
1813                     unit->m_needToResetChild = true;
1814                 } else {
1815                     OUT0(0) = x;
1816                     return;
1817                 }
1818             } else {
1819                 OUT0(0) = DEMANDINPUT_A(unit->m_indices[unit->m_index], inNumSamples);
1820                 // Print("   unit->m_index %d   OUT0(0) %g\n", unit->m_index, OUT0(0));
1821                 unit->m_index++;
1822                 unit->m_needToResetChild = true;
1823                 return;
1824             }
1825         }
1826     } else {
1827         unit->m_repeats = -1.f;
1828         unit->m_repeatCount = 0;
1829         unit->m_needToResetChild = true;
1830         unit->m_index = 0;
1831         Dshuf_scramble(unit);
1832     }
1833 }
1834 
Dshuf_scramble(Dshuf * unit)1835 void Dshuf_scramble(Dshuf* unit) {
1836     int32 i, j, m, k, size;
1837     int32 temp;
1838 
1839     size = (int32)(unit->mNumInputs) - 1;
1840 
1841     if (size > 1) {
1842         k = size;
1843         for (i = 0, m = k; i < k - 1; ++i, --m) {
1844             j = i + unit->mParent->mRGen->irand(m);
1845             temp = unit->m_indices[i];
1846             unit->m_indices[i] = unit->m_indices[j];
1847             unit->m_indices[j] = temp;
1848         }
1849     }
1850 }
1851 
Dshuf_Ctor(Dshuf * unit)1852 void Dshuf_Ctor(Dshuf* unit) {
1853     OUT0(0) = 0.f;
1854 
1855     uint32 size = (unit->mNumInputs) - 1;
1856     unit->m_indices = (int32*)RTAlloc(unit->mWorld, size * sizeof(int32));
1857 
1858     if (!unit->m_indices) {
1859         Print("Dshuf: RT memory allocation failed\n");
1860         SETCALC(ClearUnitOutputs);
1861         return;
1862     }
1863 
1864     for (uint32 i = 0; i < size; ++i)
1865         unit->m_indices[i] = i + 1;
1866 
1867     SETCALC(Dshuf_next);
1868     Dshuf_next(unit, 0);
1869 }
1870 
Dshuf_Dtor(Dshuf * unit)1871 void Dshuf_Dtor(Dshuf* unit) { RTFree(unit->mWorld, unit->m_indices); }
1872 
1873 
Dswitch1_next(Dswitch1 * unit,int inNumSamples)1874 void Dswitch1_next(Dswitch1* unit, int inNumSamples) {
1875     if (inNumSamples) {
1876         float x = DEMANDINPUT_A(0, inNumSamples);
1877         if (sc_isnan(x)) {
1878             OUT0(0) = x;
1879             return;
1880         }
1881         int index = (int32)floor(x + 0.5f);
1882         index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1883         OUT0(0) = DEMANDINPUT_A(index, inNumSamples);
1884     } else {
1885         for (int i = 0; i < unit->mNumInputs; ++i) {
1886             RESETINPUT(i);
1887         }
1888     }
1889 }
1890 
Dswitch1_Ctor(Dswitch1 * unit)1891 void Dswitch1_Ctor(Dswitch1* unit) {
1892     SETCALC(Dswitch1_next);
1893     OUT0(0) = 0.f;
1894 }
1895 
1896 /////////////////////////////
1897 
Dswitch_next(Dswitch * unit,int inNumSamples)1898 void Dswitch_next(Dswitch* unit, int inNumSamples) {
1899     if (inNumSamples) {
1900         float val = DEMANDINPUT_A(unit->m_index, inNumSamples);
1901         // printf("index: %i\n", (int) val);
1902         if (sc_isnan(val)) {
1903             float ival = DEMANDINPUT_A(0, inNumSamples);
1904 
1905             if (sc_isnan(ival))
1906                 val = ival;
1907             else {
1908                 int index = (int32)floor(ival + 0.5f);
1909                 index = sc_wrap(index, 0, unit->mNumInputs - 2) + 1;
1910                 val = DEMANDINPUT_A(index, inNumSamples);
1911 
1912                 RESETINPUT(unit->m_index);
1913                 // printf("resetting index: %i\n", unit->m_index);
1914                 unit->m_index = index;
1915             }
1916         }
1917         OUT0(0) = val;
1918 
1919     } else {
1920         printf("...\n");
1921         for (int i = 0; i < unit->mNumInputs; ++i) {
1922             RESETINPUT(i);
1923         }
1924         int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
1925         index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
1926         unit->m_index = index;
1927     }
1928 }
1929 
Dswitch_Ctor(Dswitch * unit)1930 void Dswitch_Ctor(Dswitch* unit) {
1931     SETCALC(Dswitch_next);
1932     int index = (int32)floor(DEMANDINPUT(0) + 0.5f);
1933     index = sc_wrap(index, 0, unit->mNumInputs - 1) + 1;
1934     unit->m_index = index;
1935     OUT0(0) = 0.f;
1936 }
1937 
1938 //////////////////////////////
1939 
1940 
Dstutter_next(Dstutter * unit,int inNumSamples)1941 void Dstutter_next(Dstutter* unit, int inNumSamples) {
1942     if (inNumSamples) {
1943         if (unit->m_repeatCount >= unit->m_repeats) {
1944             float val = DEMANDINPUT_A(1, inNumSamples);
1945             float repeats = DEMANDINPUT_A(0, inNumSamples);
1946 
1947             if (sc_isnan(repeats) || sc_isnan(val)) {
1948                 OUT0(0) = NAN;
1949                 return;
1950             } else {
1951                 unit->m_value = val;
1952                 unit->m_repeats = floor(repeats + 0.5f);
1953                 unit->m_repeatCount = 0.f;
1954             }
1955         }
1956 
1957         OUT0(0) = unit->m_value;
1958         unit->m_repeatCount++;
1959 
1960     } else {
1961         unit->m_repeats = -1.f;
1962         unit->m_repeatCount = 0.f;
1963         RESETINPUT(0);
1964         RESETINPUT(1);
1965     }
1966 }
1967 
Dstutter_Ctor(Dstutter * unit)1968 void Dstutter_Ctor(Dstutter* unit) {
1969     SETCALC(Dstutter_next);
1970     Dstutter_next(unit, 0);
1971     OUT0(0) = 0.f;
1972 }
1973 
1974 //////////////////////////////
1975 
Dconst_next(Dconst * unit,int inNumSamples)1976 void Dconst_next(Dconst* unit, int inNumSamples) {
1977     float total, tolerance;
1978 
1979     if (inNumSamples) {
1980         if (sc_isnan(unit->m_runningsum)) {
1981             OUT0(0) = NAN;
1982             return;
1983         }
1984 
1985         if (unit->m_total < 0.f) {
1986             total = DEMANDINPUT_A(0, inNumSamples);
1987             tolerance = DEMANDINPUT_A(2, inNumSamples);
1988             if (sc_isnan(total) || sc_isnan(tolerance)) {
1989                 OUT0(0) = NAN;
1990                 return;
1991             }
1992             unit->m_total = total;
1993             unit->m_tolerance = tolerance;
1994         }
1995         {
1996             total = unit->m_total;
1997             tolerance = unit->m_tolerance;
1998         }
1999 
2000         float val = DEMANDINPUT_A(1, inNumSamples);
2001 
2002         if (sc_isnan(val)) {
2003             OUT0(0) = NAN;
2004             return;
2005         } else {
2006             float runningsum = unit->m_runningsum + val;
2007             if ((runningsum > total) || (fabs(total - runningsum) <= tolerance)) {
2008                 OUT0(0) = total - unit->m_runningsum;
2009                 unit->m_runningsum = NAN; // stop next time
2010             } else {
2011                 unit->m_runningsum = runningsum;
2012                 OUT0(0) = val;
2013             }
2014         }
2015 
2016     } else {
2017         unit->m_total = -1.f;
2018         unit->m_runningsum = 0.f;
2019         RESETINPUT(0);
2020         RESETINPUT(1);
2021         RESETINPUT(2);
2022     }
2023 }
2024 
Dconst_Ctor(Dconst * unit)2025 void Dconst_Ctor(Dconst* unit) {
2026     SETCALC(Dconst_next);
2027     Dconst_next(unit, 0);
2028     OUT0(0) = 0.f;
2029 }
2030 
2031 //////////////////////////////
2032 
2033 
Dpoll_next(Dpoll * unit,int inNumSamples)2034 void Dpoll_next(Dpoll* unit, int inNumSamples) {
2035     if (inNumSamples) {
2036         float x = DEMANDINPUT_A(0, inNumSamples);
2037         float run = DEMANDINPUT_A(2, inNumSamples) > 0.f;
2038         if (unit->m_mayprint && run) {
2039             Print("%s: %g block offset: %d\n", unit->m_id_string, x, inNumSamples - 1);
2040         }
2041         if (IN0(1) >= 0.0)
2042             SendTrigger(&unit->mParent->mNode, (int)IN0(1), x);
2043         OUT0(0) = x;
2044     } else {
2045         RESETINPUT(0);
2046     }
2047 }
2048 
Dpoll_Ctor(Dpoll * unit)2049 void Dpoll_Ctor(Dpoll* unit) {
2050     OUT0(0) = 0.f;
2051 
2052     const int idStringSize = (int)IN0(3);
2053 
2054     unit->m_id_string = (char*)RTAlloc(unit->mWorld, (idStringSize + 1) * sizeof(char));
2055 
2056     if (!unit->m_id_string) {
2057         Print("Dpoll: RT memory allocation failed\n");
2058         SETCALC(ClearUnitOutputs);
2059         return;
2060     }
2061 
2062     for (int i = 0; i < idStringSize; i++)
2063         unit->m_id_string[i] = (char)IN0(4 + i);
2064 
2065     SETCALC(Dpoll_next);
2066     unit->m_id_string[idStringSize] = '\0';
2067     unit->m_mayprint = unit->mWorld->mVerbosity >= -1;
2068     OUT0(0) = 0.f;
2069 }
2070 
Dpoll_Dtor(Dpoll * unit)2071 void Dpoll_Dtor(Dpoll* unit) { RTFree(unit->mWorld, unit->m_id_string); }
2072 
2073 //////////////////////////////
2074 
2075 
Dreset_next(Dreset * unit,int inNumSamples)2076 void Dreset_next(Dreset* unit, int inNumSamples) {
2077     if (inNumSamples) {
2078         float x = DEMANDINPUT_A(0, inNumSamples);
2079         float reset = DEMANDINPUT_A(1, inNumSamples);
2080         if (sc_isnan(x)) {
2081             OUT0(0) = NAN;
2082             return;
2083         }
2084         if (reset > 0.0 && (unit->prev_reset <= 0.0)) {
2085             RESETINPUT(0);
2086         }
2087         unit->prev_reset = reset;
2088         OUT0(0) = x;
2089     } else {
2090         RESETINPUT(0);
2091     }
2092 }
2093 
Dreset_Ctor(Dreset * unit)2094 void Dreset_Ctor(Dreset* unit) {
2095     SETCALC(Dreset_next);
2096     unit->prev_reset = 0.0;
2097     Dreset_next(unit, 0);
2098 }
2099 
2100 
2101 //////////////////////////////
2102 
sc_loop(Unit * unit,double in,double hi,int loop)2103 inline double sc_loop(Unit* unit, double in, double hi, int loop) {
2104     // avoid the divide if possible
2105     if (in >= hi) {
2106         if (!loop) {
2107             unit->mDone = true;
2108             return hi;
2109         }
2110         in -= hi;
2111         if (in < hi)
2112             return in;
2113     } else if (in < 0.) {
2114         if (!loop) {
2115             unit->mDone = true;
2116             return 0.;
2117         }
2118         in += hi;
2119         if (in >= 0.)
2120             return in;
2121     } else
2122         return in;
2123 
2124     return in - hi * floor(in / hi);
2125 }
2126 
2127 #define D_CHECK_BUF                                                                                                    \
2128     if (!bufData) {                                                                                                    \
2129         unit->mDone = true;                                                                                            \
2130         ClearUnitOutputs(unit, 1);                                                                                     \
2131         return;                                                                                                        \
2132     }
2133 
2134 #define D_GET_BUF                                                                                                      \
2135     float fbufnum = DEMANDINPUT_A(0, inNumSamples);                                                                    \
2136     ;                                                                                                                  \
2137     if (sc_isnan(fbufnum)) {                                                                                           \
2138         OUT0(0) = NAN;                                                                                                 \
2139         return;                                                                                                        \
2140     }                                                                                                                  \
2141     fbufnum = sc_max(0.f, fbufnum);                                                                                    \
2142     if (fbufnum != unit->m_fbufnum) {                                                                                  \
2143         int bufnum = (int)fbufnum;                                                                                     \
2144         World* world = unit->mWorld;                                                                                   \
2145         if (bufnum < 0) {                                                                                              \
2146             bufnum = 0;                                                                                                \
2147         }                                                                                                              \
2148         if (bufnum >= world->mNumSndBufs) {                                                                            \
2149             int localBufNum = bufnum - world->mNumSndBufs;                                                             \
2150             Graph* parent = unit->mParent;                                                                             \
2151             if (localBufNum <= parent->localBufNum) {                                                                  \
2152                 unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
2153             } else {                                                                                                   \
2154                 bufnum = 0;                                                                                            \
2155                 unit->m_buf = world->mSndBufs + bufnum;                                                                \
2156             }                                                                                                          \
2157         } else {                                                                                                       \
2158             unit->m_buf = world->mSndBufs + bufnum;                                                                    \
2159         }                                                                                                              \
2160         unit->m_fbufnum = fbufnum;                                                                                     \
2161     }                                                                                                                  \
2162     SndBuf* buf = unit->m_buf;                                                                                         \
2163     LOCK_SNDBUF(buf);                                                                                                  \
2164     float* bufData __attribute__((__unused__)) = buf->data;                                                            \
2165     uint32 bufChannels __attribute__((__unused__)) = buf->channels;                                                    \
2166     uint32 bufSamples __attribute__((__unused__)) = buf->samples;                                                      \
2167     uint32 bufFrames = buf->frames;                                                                                    \
2168     int mask __attribute__((__unused__)) = buf->mask;                                                                  \
2169     int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2170 
2171 #define D_GET_BUF_SHARED                                                                                               \
2172     float fbufnum = DEMANDINPUT_A(0, inNumSamples);                                                                    \
2173     ;                                                                                                                  \
2174     if (sc_isnan(fbufnum)) {                                                                                           \
2175         OUT0(0) = NAN;                                                                                                 \
2176         return;                                                                                                        \
2177     }                                                                                                                  \
2178     fbufnum = sc_max(0.f, fbufnum);                                                                                    \
2179     if (fbufnum != unit->m_fbufnum) {                                                                                  \
2180         int bufnum = (int)fbufnum;                                                                                     \
2181         World* world = unit->mWorld;                                                                                   \
2182         if (bufnum < 0) {                                                                                              \
2183             bufnum = 0;                                                                                                \
2184         }                                                                                                              \
2185         if (bufnum >= world->mNumSndBufs) {                                                                            \
2186             int localBufNum = bufnum - world->mNumSndBufs;                                                             \
2187             Graph* parent = unit->mParent;                                                                             \
2188             if (localBufNum <= parent->localBufNum) {                                                                  \
2189                 unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
2190             } else {                                                                                                   \
2191                 bufnum = 0;                                                                                            \
2192                 unit->m_buf = world->mSndBufs + bufnum;                                                                \
2193             }                                                                                                          \
2194         } else {                                                                                                       \
2195             unit->m_buf = world->mSndBufs + bufnum;                                                                    \
2196         }                                                                                                              \
2197         unit->m_fbufnum = fbufnum;                                                                                     \
2198     }                                                                                                                  \
2199     const SndBuf* buf = unit->m_buf;                                                                                   \
2200     LOCK_SNDBUF_SHARED(buf);                                                                                           \
2201     const float* bufData __attribute__((__unused__)) = buf->data;                                                      \
2202     uint32 bufChannels __attribute__((__unused__)) = buf->channels;                                                    \
2203     uint32 bufSamples __attribute__((__unused__)) = buf->samples;                                                      \
2204     uint32 bufFrames = buf->frames;                                                                                    \
2205     int mask __attribute__((__unused__)) = buf->mask;                                                                  \
2206     int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2207 
2208 
Dbufrd_next(Dbufrd * unit,int inNumSamples)2209 void Dbufrd_next(Dbufrd* unit, int inNumSamples) {
2210     int32 loop = (int32)DEMANDINPUT_A(2, inNumSamples);
2211 
2212     D_GET_BUF_SHARED
2213     D_CHECK_BUF
2214 
2215     double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2216 
2217     double phase;
2218     if (inNumSamples) {
2219         float x = DEMANDINPUT_A(1, inNumSamples);
2220         if (sc_isnan(x)) {
2221             OUT0(0) = NAN;
2222             return;
2223         }
2224         phase = x;
2225 
2226         phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2227         int32 iphase = (int32)phase;
2228         const float* table1 = bufData + iphase * bufChannels;
2229         OUT0(0) = table1[0];
2230     } else {
2231         RESETINPUT(1);
2232     }
2233 }
2234 
Dbufrd_Ctor(Dbufrd * unit)2235 void Dbufrd_Ctor(Dbufrd* unit) {
2236     SETCALC(Dbufrd_next);
2237 
2238     unit->m_fbufnum = -1e9f;
2239 
2240     Dbufrd_next(unit, 0);
2241     OUT0(0) = 0.f;
2242 }
2243 
2244 ////////////////////////////////////
2245 
Dbufwr_next(Dbufwr * unit,int inNumSamples)2246 void Dbufwr_next(Dbufwr* unit, int inNumSamples) {
2247     int32 loop = (int32)DEMANDINPUT_A(3, inNumSamples);
2248 
2249     D_GET_BUF
2250     D_CHECK_BUF
2251 
2252     double loopMax = (double)(loop ? bufFrames : bufFrames - 1);
2253 
2254     double phase;
2255     float val;
2256     if (inNumSamples) {
2257         float x = DEMANDINPUT_A(1, inNumSamples);
2258         if (sc_isnan(x)) {
2259             OUT0(0) = NAN;
2260             return;
2261         }
2262         phase = x;
2263         val = DEMANDINPUT_A(2, inNumSamples);
2264         if (sc_isnan(val)) {
2265             OUT0(0) = NAN;
2266             return;
2267         }
2268 
2269         phase = sc_loop((Unit*)unit, phase, loopMax, loop);
2270         int32 iphase = (int32)phase;
2271         float* table0 = bufData + iphase * bufChannels;
2272         table0[0] = val;
2273         OUT0(0) = val;
2274     } else {
2275         RESETINPUT(1);
2276         RESETINPUT(2);
2277     }
2278 }
2279 
Dbufwr_Ctor(Dbufwr * unit)2280 void Dbufwr_Ctor(Dbufwr* unit) {
2281     SETCALC(Dbufwr_next);
2282 
2283     unit->m_fbufnum = -1e9f;
2284 
2285     Dbufwr_next(unit, 0);
2286     OUT0(0) = 0.f;
2287 }
2288 
2289 
2290 //////////////////////////////////////////////////////
2291 
2292 
PluginLoad(Demand)2293 PluginLoad(Demand) {
2294     ft = inTable;
2295 
2296     DefineDtorCantAliasUnit(Demand);
2297     DefineSimpleCantAliasUnit(Duty);
2298     DefineSimpleCantAliasUnit(DemandEnvGen);
2299     DefineSimpleCantAliasUnit(TDuty);
2300     DefineSimpleUnit(Dseries);
2301     DefineSimpleUnit(Dgeom);
2302     DefineSimpleUnit(Dwhite);
2303     DefineSimpleUnit(Dbrown);
2304     DefineSimpleUnit(Diwhite);
2305     DefineSimpleUnit(Dibrown);
2306     DefineSimpleUnit(Dseq);
2307     DefineSimpleUnit(Dser);
2308     DefineSimpleUnit(Dbufrd);
2309     DefineSimpleUnit(Dbufwr);
2310     DefineSimpleUnit(Drand);
2311     DefineSimpleUnit(Dwrand);
2312     DefineSimpleUnit(Dxrand);
2313     DefineDtorUnit(Dshuf);
2314     DefineSimpleUnit(Dswitch1);
2315     DefineSimpleUnit(Dswitch);
2316     DefineSimpleUnit(Dstutter);
2317     DefineSimpleUnit(Dconst);
2318     DefineSimpleUnit(Dreset);
2319     DefineDtorUnit(Dpoll);
2320 }
2321