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"
27 using std::numeric_limits
;
29 static InterfaceTable
*ft
;
31 struct Demand
: public Unit
39 struct Duty
: public Unit
46 struct DemandEnvGen
: public Unit
51 double m_a1
, m_a2
, m_b1
, m_y1
, m_y2
, m_grow
, m_level
, m_endLevel
, m_curve
;
53 bool m_release
, m_running
;
56 struct TDuty
: public Unit
63 struct Dseries
: public Unit
71 struct Dgeom
: public Unit
79 struct Dwhite
: public Unit
87 struct Dbrown
: public Unit
97 struct Diwhite
: public Unit
105 struct Dibrown
: public Unit
115 struct Dseq
: public Unit
120 bool m_needToResetChild
;
123 struct Dshuf
: public Unit
128 bool m_needToResetChild
;
132 struct Dbufrd
: public Unit
138 struct Dbufwr
: public Unit
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
170 struct Dstutter
: public Unit
173 double m_repeatCount
;
177 struct Donce
: public Unit
183 struct Dpoll
: public Unit
190 struct Dreset
: public Unit
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
) {
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
) {
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; }
320 for (int j
=2, k
=0; j
<unit
->mNumInputs
; ++j
, ++k
) {
321 out
[k
][i
] = prevout
[k
];
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
) {
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
) {
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; }
365 for (int j
=2, k
=0; j
<unit
->mNumInputs
; ++j
, ++k
) {
366 out
[k
][i
] = prevout
[k
];
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
) {
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
) {
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; }
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
);
426 SETCALC(Demand_next_ak
);
429 if (INRATE(1) == calc_FullRate
) {
430 SETCALC(Demand_next_ka
);
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
;
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 /////////////////////////////////////////////////////////////////////////////
464 void Duty_next_da(Duty
*unit
, int inNumSamples
)
467 float *reset
= ZIN(duty_reset
);
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
);
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);
494 int doneAction
= (int)ZIN0(duty_doneAction
);
495 DoneAction(doneAction
, unit
);
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
);
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
);
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);
542 int doneAction
= (int)ZIN0(duty_doneAction
);
543 DoneAction(doneAction
, unit
);
558 unit
->m_count
= count
;
559 unit
->m_prevreset
= prevreset
;
560 unit
->m_prevout
= prevout
;
565 void Duty_next_dd(Duty
*unit
, int inNumSamples
)
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
) {
576 RESETINPUT(duty_level
);
577 RESETINPUT(duty_dur
);
579 reset
= DEMANDINPUT_A(duty_reset
, i
+ 1) * sr
+ reset
;
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);
593 int doneAction
= (int)ZIN0(duty_doneAction
);
594 DoneAction(doneAction
, unit
);
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
;
618 if(INRATE(duty_reset
) == calc_DemandRate
) {
619 SETCALC(Duty_next_dd
);
620 unit
->m_prevreset
= DEMANDINPUT(duty_reset
) * SAMPLERATE
;
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 /////////////////////////////////////////////////////////////////////////////
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
);
688 DEMANDINPUT(d_env_level
); // remove first level
690 level
= DEMANDINPUT(d_env_level
); // jump to first level
700 if (phase
<= 0.f
&& running
) {
702 // was a release during last segment?
707 // printf("release: %f %f \n", phase, level);
708 int doneAction
= (int)ZIN0(d_env_doneAction
);
709 DoneAction(doneAction
, unit
);
714 float dur
= DEMANDINPUT(d_env_dur
);
715 // printf("dur: %f \n", dur);
719 phase
= numeric_limits
<float>::max();
721 phase
= dur
* ZIN0(d_env_timeScale
) * SAMPLERATE
+ phase
;
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
;
734 shape
= 1; // shape_Linear
739 if(dur
* 0.5f
< SAMPLEDUR
) shape
= 1;
741 //printf("shape: %i, curve: %f, dur: %f \n", shape, curve, dur);
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
;
754 endLevel
= endLevel
* ZIN0(d_env_levelScale
) + ZIN0(d_env_levelBias
);
755 unit
->m_endLevel
= endLevel
;
759 // calculate shape parameters
765 case shape_Linear
: {
766 unit
->m_grow
= (endLevel
- level
) / count
;
768 case shape_Exponential
: {
769 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / count
);
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
;
781 double w
= (pi
* 0.5) / count
;
783 unit
->m_b1
= 2. * cos(w
);
785 if (endLevel
>= level
) {
788 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
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
;
797 if (fabs(curve
) < 0.001) {
798 unit
->m_shape
= shape
= 1; // shape_Linear
799 unit
->m_grow
= (endLevel
- level
) / count
;
801 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
802 unit
->m_a2
= level
+ a1
;
804 unit
->m_grow
= exp(curve
/ count
);
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
;
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
;
828 case shape_Linear
: {
829 double grow
= unit
->m_grow
;
830 //Print("level %g\n", level);
833 case shape_Exponential
: {
834 double grow
= unit
->m_grow
;
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
;
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
;
862 double a2
= unit
->m_a2
;
863 double b1
= unit
->m_b1
;
864 double grow
= unit
->m_grow
;
869 case shape_Squared
: {
870 double grow
= unit
->m_grow
;
871 double y1
= unit
->m_y1
;
877 double grow
= unit
->m_grow
;
878 double y1
= unit
->m_y1
;
883 case shape_Sustain
: {
895 float zgate
= ZIN0(d_env_gate
);
897 unit
->m_running
= true;
898 } else if (zgate
> 0.f
) {
899 unit
->m_running
= true;
900 release
= true; // release next time.
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
);
942 DEMANDINPUT_A(d_env_level
, i
+ 1); // remove first level
944 level
= DEMANDINPUT_A(d_env_level
, i
+ 1); // jump to first level
947 RESETINPUT(d_env_dur
);
948 RESETINPUT(d_env_shape
);
958 if (phase
<= 0.f
&& running
) {
965 // printf("release: %f %f \n", phase, level);
966 int doneAction
= (int)ZIN0(d_env_doneAction
);
967 DoneAction(doneAction
, unit
);
972 float dur
= DEMANDINPUT_A(d_env_dur
, i
+ 1);
973 // printf("dur: %f \n", dur);
977 phase
= numeric_limits
<float>::max();
979 phase
= dur
* ZIN0(d_env_timeScale
) * SAMPLERATE
+ phase
;
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
;
992 shape
= 1; // shape_Linear
997 if(dur
* 0.5f
< SAMPLEDUR
) shape
= 1;
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
;
1010 endLevel
= endLevel
* ZIN0(d_env_levelScale
) + ZIN0(d_env_levelBias
);
1011 unit
->m_endLevel
= endLevel
;
1015 // calculate shape parameters
1021 case shape_Linear
: {
1022 unit
->m_grow
= (endLevel
- level
) / count
;
1024 case shape_Exponential
: {
1025 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / count
);
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
;
1036 case shape_Welch
: {
1037 double w
= (pi
* 0.5) / count
;
1039 unit
->m_b1
= 2. * cos(w
);
1041 if (endLevel
>= level
) {
1044 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
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
;
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
;
1057 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
1058 unit
->m_a2
= level
+ a1
;
1060 unit
->m_grow
= exp(curve
/ count
);
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
;
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
;
1083 case shape_Linear
: {
1084 double grow
= unit
->m_grow
;
1085 //Print("level %g\n", level);
1088 case shape_Exponential
: {
1089 double grow
= unit
->m_grow
;
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
;
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
;
1116 case shape_Curve
: {
1117 double a2
= unit
->m_a2
;
1118 double b1
= unit
->m_b1
;
1119 double grow
= unit
->m_grow
;
1124 case shape_Squared
: {
1125 double grow
= unit
->m_grow
;
1126 double y1
= unit
->m_y1
;
1131 case shape_Cubed
: {
1132 double grow
= unit
->m_grow
;
1133 double y1
= unit
->m_y1
;
1138 case shape_Sustain
: {
1147 float zgate
= ZXP(gate
);
1150 unit
->m_running
= true;
1151 } else if (zgate
> 0.f
) {
1152 unit
->m_running
= true;
1153 release
= true; // release next time.
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
);
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
);
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
;
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
);
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
;
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
) {
1285 RESETINPUT(duty_level
);
1286 RESETINPUT(duty_dur
);
1288 reset
= DEMANDINPUT_A(duty_reset
, i
+ 1) * sr
+ reset
;
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
;
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
;
1323 if(INRATE(1) == calc_DemandRate
) {
1324 SETCALC(TDuty_next_dd
);
1325 unit
->m_prevreset
= DEMANDINPUT(1) * SAMPLERATE
;
1327 SETCALC(TDuty_next_dk
);
1328 unit
->m_prevreset
= 0.f
;
1331 // support for gap-first.
1333 unit
->m_count
= DEMANDINPUT(duty_dur
) * SAMPLERATE
;
1335 unit
->m_count
= 0.f
;
1341 /////////////////////////////////////////////////////////////////////////////
1345 void Dseries_next(Dseries
*unit
, int 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
) {
1359 OUT0(0) = unit
->m_value
;
1360 unit
->m_value
+= unit
->m_step
;
1361 unit
->m_repeatCount
++;
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);
1376 void Dgeom_next(Dgeom
*unit
, int 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
) {
1390 OUT0(0) = unit
->m_value
;
1391 unit
->m_value
*= unit
->m_grow
;
1392 unit
->m_repeatCount
++;
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);
1407 void Dwhite_next(Dwhite
*unit
, int 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
) {
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
;
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);
1440 void Diwhite_next(Diwhite
*unit
, int 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
); }
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
) {
1460 unit
->m_repeatCount
++;
1461 float x
= unit
->mParent
->mRGen
->irand(unit
->m_range
) + unit
->m_lo
;
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);
1477 void Dbrown_next(Dbrown
*unit
, int 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
) {
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
);
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);
1511 void Dibrown_next(Dibrown
*unit
, int 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
) {
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
);
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);
1546 void Dseq_next(Dseq
*unit
, int inNumSamples
)
1548 //Print("->Dseq_next %d\n", 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
);
1557 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1558 if (unit
->m_index
>= unit
->mNumInputs
) {
1560 unit
->m_repeatCount
++;
1562 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
1576 unit
->m_needToResetChild
= true;
1582 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1583 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1585 unit
->m_needToResetChild
= true;
1589 if (attempts
++ > unit
->mNumInputs
) {
1590 Print("Warning: empty sequence in Dseq\n");
1595 unit
->m_repeats
= -1.f
;
1596 unit
->m_repeatCount
= 0;
1597 unit
->m_needToResetChild
= true;
1603 void Dseq_Ctor(Dseq
*unit
)
1611 void Dser_next(Dser
*unit
, int 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
);
1619 if (unit
->m_index
>= unit
->mNumInputs
) {
1622 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
1634 unit
->m_repeatCount
++;
1635 unit
->m_needToResetChild
= true;
1641 OUT0(0) = IN0(unit
->m_index
);
1643 unit
->m_repeatCount
++;
1644 unit
->m_needToResetChild
= true;
1649 unit
->m_repeats
= -1.f
;
1650 unit
->m_repeatCount
= 0;
1651 unit
->m_needToResetChild
= true;
1656 void Dser_Ctor(Dser
*unit
)
1665 void Drand_next(Drand
*unit
, int 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
);
1674 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
1685 unit
->m_index
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 1) + 1;
1686 unit
->m_repeatCount
++;
1687 unit
->m_needToResetChild
= true;
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;
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);
1717 void Dxrand_next(Dxrand
*unit
, int 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
);
1725 if (unit
->m_index
>= unit
->mNumInputs
) {
1728 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
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;
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;
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);
1773 float w, sum = 0.0; \
1774 float r = unit->mParent->mRGen->frand(); \
1775 for (int i=0; i<weights_size; ++i) { \
1779 unit->m_index = i + offset; \
1785 void Dwrand_next(Dwrand *unit, int inNumSamples)
1787 int offset
= unit
->m_weights_size
+ 2;
1788 int weights_size
= unit
->mNumInputs
- offset
;
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
);
1797 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
1811 unit
->m_repeatCount
++;
1812 unit
->m_needToResetChild
= true;
1818 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1820 unit
->m_repeatCount
++;
1821 unit
->m_needToResetChild
= true;
1826 unit
->m_repeats
= -1.f
;
1827 unit
->m_repeatCount
= 0;
1828 unit
->m_needToResetChild
= true;
1833 void Dwrand_Ctor(Dwrand
*unit
)
1835 SETCALC(Dwrand_next
);
1836 unit
->m_weights_size
= IN0(1);
1837 Dwrand_next(unit
, 0);
1843 static void Dshuf_scramble(Dshuf
*unit
);
1845 void Dshuf_next(Dshuf
*unit
, int inNumSamples
)
1847 // Print("->Dshuf_next %d\n", 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
);
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)) {
1858 unit
->m_repeatCount
++;
1860 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
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
);
1875 unit
->m_needToResetChild
= true;
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));
1884 unit
->m_needToResetChild
= true;
1889 unit
->m_repeats
= -1.f
;
1890 unit
->m_repeatCount
= 0;
1891 unit
->m_needToResetChild
= true;
1893 Dshuf_scramble(unit
);
1897 void Dshuf_scramble(Dshuf
*unit
) {
1898 int32 i
, j
, m
, k
, size
;
1901 size
= (int32
)(unit
->mNumInputs
) - 1;
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
)
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);
1932 void Dshuf_Dtor(Dshuf
*unit
)
1934 RTFree(unit
->mWorld
, unit
->m_indices
);
1938 void Dswitch1_next(Dswitch1
*unit
, int inNumSamples
)
1941 float x
= DEMANDINPUT_A(0, inNumSamples
);
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
);
1950 for (int i
=0; i
<unit
->mNumInputs
; ++i
) {
1956 void Dswitch1_Ctor(Dswitch1
*unit
)
1958 SETCALC(Dswitch1_next
);
1962 /////////////////////////////
1964 void Dswitch_next(Dswitch
*unit
, int inNumSamples
)
1967 float val
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1968 //printf("index: %i\n", (int) val);
1970 float ival
= DEMANDINPUT_A(0, inNumSamples
);
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
;
1988 for (int i
=0; i
<unit
->mNumInputs
; ++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
;
2006 //////////////////////////////
2009 void Dstutter_next(Dstutter
*unit
, int 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
)) {
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
++;
2033 unit
->m_repeats
= -1.f
;
2034 unit
->m_repeatCount
= 0.f
;
2041 void Dstutter_Ctor(Dstutter
*unit
)
2043 SETCALC(Dstutter_next
);
2044 Dstutter_next(unit
, 0);
2048 //////////////////////////////
2052 void Dpoll_next(Dpoll
*unit
, int 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
);
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;
2080 void Dpoll_Dtor(Dpoll
* unit
)
2082 RTFree(unit
->mWorld
, unit
->m_id_string
);
2085 //////////////////////////////
2089 void Dreset_next(Dreset
*unit
, int inNumSamples
)
2092 float x
= DEMANDINPUT_A(0, inNumSamples
);
2093 float reset
= DEMANDINPUT_A(1, inNumSamples
);
2098 if(reset
> 0.0 && (unit
->prev_reset
<= 0.0)) { RESETINPUT(0); }
2099 unit
->prev_reset
= reset
;
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
)
2122 if (unit
->m_bufcounter
== unit
->mWorld
->mBufCounter
) {
2123 OUT0(0) = unit
->m_prev
;
2125 float x
= DEMANDINPUT_A(0, inNumSamples
);
2134 void Donce_Ctor(Donce
*unit
)
2136 SETCALC(Donce_next
);
2141 inline double sc_loop(Unit
*unit
, double in
, double hi
, int loop
)
2143 // avoid the divide if possible
2150 if (in
< hi
) return in
;
2151 } else if (in
< 0.) {
2157 if (in
>= 0.) return in
;
2160 return in
- hi
* floor(in
/hi
);
2163 #define D_CHECK_BUF \
2165 unit->mDone = true; \
2166 ClearUnitOutputs(unit, 1); \
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; \
2183 unit->m_buf = world->mSndBufs + bufnum; \
2186 unit->m_buf = world->mSndBufs + bufnum; \
2188 unit->m_fbufnum = fbufnum; \
2190 SndBuf *buf = unit->m_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; \
2212 unit->m_buf = world->mSndBufs + bufnum; \
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
);
2236 double loopMax
= (double)(loop
? bufFrames
: bufFrames
- 1);
2241 float x
= DEMANDINPUT_A(1, inNumSamples
);
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];
2259 void Dbufrd_Ctor(Dbufrd
*unit
)
2261 SETCALC(Dbufrd_next
);
2263 unit
->m_fbufnum
= -1e9f
;
2265 Dbufrd_next(unit
, 0);
2269 ////////////////////////////////////
2271 void Dbufwr_next(Dbufwr
*unit
, int inNumSamples
)
2273 int32 loop
= (int32
)DEMANDINPUT_A(3, inNumSamples
);
2278 double loopMax
= (double)(loop
? bufFrames
: bufFrames
- 1);
2284 float x
= DEMANDINPUT_A(1, inNumSamples
);
2290 val
= DEMANDINPUT_A(2, inNumSamples
);
2291 if (sc_isnan(val
)) {
2296 phase
= sc_loop((Unit
*)unit
, phase
, loopMax
, loop
);
2297 int32 iphase
= (int32
)phase
;
2298 float* table0
= bufData
+ iphase
* bufChannels
;
2309 void Dbufwr_Ctor(Dbufwr
*unit
)
2311 SETCALC(Dbufwr_next
);
2313 unit
->m_fbufnum
= -1e9f
;
2315 Dbufwr_next(unit
, 0);
2320 //////////////////////////////////////////////////////
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
);