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
)
667 float zreset
= ZIN0(d_env_reset
);
670 float *out
= ZOUT(0);
671 double level
= unit
->m_level
;
672 float phase
= unit
->m_phase
;
673 double curve
= unit
->m_curve
;
674 bool release
= unit
->m_release
;
675 bool running
= unit
->m_running
;
676 int shape
= unit
->m_shape
;
678 // printf("phase %f level %f \n", phase, level);
680 for (int i
=0; i
<inNumSamples
; ++i
) {
684 if (zreset
> 0.f
&& unit
->m_prevreset
<= 0.f
) {
685 //printf("reset: %f %f \n", zreset, unit->m_prevreset);
686 RESETINPUT(d_env_level
);
687 RESETINPUT(d_env_dur
);
688 RESETINPUT(d_env_shape
);
689 RESETINPUT(d_env_curve
);
692 DEMANDINPUT(d_env_level
); // remove first level
694 level
= DEMANDINPUT(d_env_level
); // jump to first level
704 if (phase
<= 0.f
&& running
) {
706 // was a release during last segment?
711 // printf("release: %f %f \n", phase, level);
712 int doneAction
= (int)ZIN0(d_env_doneAction
);
713 DoneAction(doneAction
, unit
);
721 float dur
= DEMANDINPUT(d_env_dur
);
722 // printf("dur: %f \n", dur);
726 phase
= numeric_limits
<float>::max();
728 phase
= dur
* ZIN0(d_env_timeScale
) * SAMPLERATE
+ phase
;
735 shape
= (int)DEMANDINPUT(d_env_shape
);
736 curve
= DEMANDINPUT(d_env_curve
);
739 if (sc_isnan(curve
)) curve
= unit
->m_shape
;
741 shape
= 1; // shape_Linear
746 if(dur
* 0.5f
< SAMPLEDUR
) shape
= 1;
748 //printf("shape: %i, curve: %f, dur: %f \n", shape, curve, dur);
753 double endLevel
= DEMANDINPUT(d_env_level
);
754 // printf("levels: %f %f\n", level, endLevel);
755 if (sc_isnan(endLevel
)) {
756 endLevel
= unit
->m_endLevel
;
761 endLevel
= endLevel
* ZIN0(d_env_levelScale
) + ZIN0(d_env_levelBias
);
762 unit
->m_endLevel
= endLevel
;
766 // calculate shape parameters
772 case shape_Linear
: {
773 unit
->m_grow
= (endLevel
- level
) / count
;
775 case shape_Exponential
: {
776 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / count
);
779 double w
= pi
/ count
;
781 unit
->m_a2
= (endLevel
+ level
) * 0.5;
782 unit
->m_b1
= 2. * cos(w
);
783 unit
->m_y1
= (endLevel
- level
) * 0.5;
784 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
785 level
= unit
->m_a2
- unit
->m_y1
;
788 double w
= (pi
* 0.5) / count
;
790 unit
->m_b1
= 2. * cos(w
);
792 if (endLevel
>= level
) {
795 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
797 unit
->m_a2
= endLevel
;
798 unit
->m_y1
= level
- endLevel
;
799 unit
->m_y2
= cos(w
) * (level
- endLevel
);
801 level
= unit
->m_a2
+ unit
->m_y1
;
804 if (fabs(curve
) < 0.001) {
805 unit
->m_shape
= 1; // shape_Linear
806 unit
->m_grow
= (endLevel
- level
) / count
;
808 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
809 unit
->m_a2
= level
+ a1
;
811 unit
->m_grow
= exp(curve
/ count
);
814 case shape_Squared
: {
815 unit
->m_y1
= sqrt(level
);
816 unit
->m_y2
= sqrt(endLevel
);
817 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / count
;
820 unit
->m_y1
= pow(level
, 0.33333333);
821 unit
->m_y2
= pow(endLevel
, 0.33333333);
822 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / count
;
835 case shape_Linear
: {
836 double grow
= unit
->m_grow
;
837 //Print("level %g\n", level);
840 case shape_Exponential
: {
841 double grow
= unit
->m_grow
;
845 double a2
= unit
->m_a2
;
846 double b1
= unit
->m_b1
;
847 double y2
= unit
->m_y2
;
848 double y1
= unit
->m_y1
;
849 double y0
= b1
* y1
- y2
;
857 double a2
= unit
->m_a2
;
858 double b1
= unit
->m_b1
;
859 double y2
= unit
->m_y2
;
860 double y1
= unit
->m_y1
;
861 double y0
= b1
* y1
- y2
;
869 double a2
= unit
->m_a2
;
870 double b1
= unit
->m_b1
;
871 double grow
= unit
->m_grow
;
876 case shape_Squared
: {
877 double grow
= unit
->m_grow
;
878 double y1
= unit
->m_y1
;
884 double grow
= unit
->m_grow
;
885 double y1
= unit
->m_y1
;
890 case shape_Sustain
: {
902 float zgate
= ZIN0(d_env_gate
);
904 unit
->m_running
= true;
905 } else if (zgate
> 0.f
) {
906 unit
->m_running
= true;
907 release
= true; // release next time.
909 unit
->m_running
= false; // sample and hold
912 unit
->m_level
= level
;
913 unit
->m_curve
= curve
;
914 unit
->m_shape
= shape
;
915 unit
->m_prevreset
= zreset
;
916 unit
->m_release
= release
;
918 unit
->m_phase
= phase
;
925 void DemandEnvGen_next_a(DemandEnvGen
*unit
, int inNumSamples
)
928 float *reset
= ZIN(d_env_reset
);
929 float *gate
= ZIN(d_env_gate
);
931 float *out
= ZOUT(0);
933 float prevreset
= unit
->m_prevreset
;
934 double level
= unit
->m_level
;
935 float phase
= unit
->m_phase
;
936 double curve
= unit
->m_curve
;
937 bool release
= unit
->m_release
;
938 bool running
= unit
->m_running
;
941 int shape
= unit
->m_shape
;
942 // printf("phase %f \n", phase);
944 for (int i
=0; i
<inNumSamples
; ++i
) {
946 float zreset
= ZXP(reset
);
947 if (zreset
> 0.f
&& prevreset
<= 0.f
) {
948 // printf("reset: %f %f \n", zreset, unit->m_prevreset);
949 RESETINPUT(d_env_level
);
951 DEMANDINPUT_A(d_env_level
, i
+ 1); // remove first level
953 level
= DEMANDINPUT_A(d_env_level
, i
+ 1); // jump to first level
956 RESETINPUT(d_env_dur
);
957 RESETINPUT(d_env_shape
);
968 if (phase
<= 0.f
&& running
) {
975 // printf("release: %f %f \n", phase, level);
976 int doneAction
= (int)ZIN0(d_env_doneAction
);
977 DoneAction(doneAction
, unit
);
985 float dur
= DEMANDINPUT_A(d_env_dur
, i
+ 1);
986 // printf("dur: %f \n", dur);
990 phase
= numeric_limits
<float>::max();
992 phase
= dur
* ZIN0(d_env_timeScale
) * SAMPLERATE
+ phase
;
997 curve
= DEMANDINPUT_A(d_env_shape
, i
+ 1);
998 shape
= (int)DEMANDINPUT_A(d_env_shape
, i
+ 1);
1000 // printf("shapes: %i \n", shape);
1001 if (sc_isnan(curve
)) curve
= unit
->m_shape
;
1002 if (sc_isnan(shape
)) shape
= unit
->m_shape
;
1005 shape
= 1; // shape_Linear
1010 if(dur
* 0.5f
< SAMPLEDUR
) shape
= 1;
1015 double endLevel
= DEMANDINPUT_A(d_env_level
, i
+ 1);
1016 // printf("levels: %f %f\n", level, endLevel);
1017 if (sc_isnan(endLevel
)) {
1018 endLevel
= unit
->m_endLevel
;
1023 endLevel
= endLevel
* ZIN0(d_env_levelScale
) + ZIN0(d_env_levelBias
);
1024 unit
->m_endLevel
= endLevel
;
1028 // calculate shape parameters
1034 case shape_Linear
: {
1035 unit
->m_grow
= (endLevel
- level
) / count
;
1037 case shape_Exponential
: {
1038 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / count
);
1041 double w
= pi
/ count
;
1043 unit
->m_a2
= (endLevel
+ level
) * 0.5;
1044 unit
->m_b1
= 2. * cos(w
);
1045 unit
->m_y1
= (endLevel
- level
) * 0.5;
1046 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
1047 level
= unit
->m_a2
- unit
->m_y1
;
1049 case shape_Welch
: {
1050 double w
= (pi
* 0.5) / count
;
1052 unit
->m_b1
= 2. * cos(w
);
1054 if (endLevel
>= level
) {
1057 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
1059 unit
->m_a2
= endLevel
;
1060 unit
->m_y1
= level
- endLevel
;
1061 unit
->m_y2
= cos(w
) * (level
- endLevel
);
1063 level
= unit
->m_a2
+ unit
->m_y1
;
1065 case shape_Curve
: {
1066 if (fabs(curve
) < 0.001) {
1067 unit
->m_shape
= 1; // shape_Linear
1068 unit
->m_grow
= (endLevel
- level
) / count
;
1070 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
1071 unit
->m_a2
= level
+ a1
;
1073 unit
->m_grow
= exp(curve
/ count
);
1076 case shape_Squared
: {
1077 unit
->m_y1
= sqrt(level
);
1078 unit
->m_y2
= sqrt(endLevel
);
1079 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / count
;
1081 case shape_Cubed
: {
1082 unit
->m_y1
= pow(level
, 0.33333333);
1083 unit
->m_y2
= pow(endLevel
, 0.33333333);
1084 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / count
;
1096 case shape_Linear
: {
1097 double grow
= unit
->m_grow
;
1098 //Print("level %g\n", level);
1101 case shape_Exponential
: {
1102 double grow
= unit
->m_grow
;
1106 double a2
= unit
->m_a2
;
1107 double b1
= unit
->m_b1
;
1108 double y2
= unit
->m_y2
;
1109 double y1
= unit
->m_y1
;
1110 double y0
= b1
* y1
- y2
;
1117 case shape_Welch
: {
1118 double a2
= unit
->m_a2
;
1119 double b1
= unit
->m_b1
;
1120 double y2
= unit
->m_y2
;
1121 double y1
= unit
->m_y1
;
1122 double y0
= b1
* y1
- y2
;
1129 case shape_Curve
: {
1130 double a2
= unit
->m_a2
;
1131 double b1
= unit
->m_b1
;
1132 double grow
= unit
->m_grow
;
1137 case shape_Squared
: {
1138 double grow
= unit
->m_grow
;
1139 double y1
= unit
->m_y1
;
1144 case shape_Cubed
: {
1145 double grow
= unit
->m_grow
;
1146 double y1
= unit
->m_y1
;
1151 case shape_Sustain
: {
1160 float zgate
= ZXP(gate
);
1163 unit
->m_running
= true;
1164 } else if (zgate
> 0.f
) {
1165 unit
->m_running
= true;
1166 release
= true; // release next time.
1168 unit
->m_running
= false; // sample and hold
1172 unit
->m_level
= level
;
1173 unit
->m_curve
= curve
;
1174 unit
->m_shape
= shape
;
1175 unit
->m_prevreset
= prevreset
;
1176 unit
->m_release
= release
;
1177 unit
->m_phase
= phase
;
1182 void DemandEnvGen_Ctor(DemandEnvGen
*unit
)
1184 // derive the first level.
1186 unit
->m_level
= DEMANDINPUT(d_env_level
);
1187 if(sc_isnan(unit
->m_level
)) { unit
->m_level
= 0.f
; }
1188 unit
->m_endLevel
= unit
->m_level
;
1189 unit
->m_release
= false;
1190 unit
->m_prevreset
= 0.f
;
1191 unit
->m_phase
= 0.f
;
1192 unit
->m_running
= ZIN0(d_env_gate
) > 0.f
;
1194 if(INRATE(d_env_gate
) == calc_FullRate
) {
1195 SETCALC(DemandEnvGen_next_a
);
1197 SETCALC(DemandEnvGen_next_k
);
1200 DemandEnvGen_next_k(unit
, 1);
1205 /////////////////////////////////////////////////////////////////////////////
1207 void TDuty_next_da(TDuty
*unit
, int inNumSamples
)
1210 float *reset
= ZIN(duty_reset
);
1211 float *out
= OUT(0);
1213 float count
= unit
->m_count
;
1214 float prevreset
= unit
->m_prevreset
;
1215 float sr
= (float) SAMPLERATE
;
1217 for (int i
=0; i
<inNumSamples
; ++i
) {
1219 float zreset
= ZXP(reset
);
1220 if (zreset
> 0.f
&& prevreset
<= 0.f
) {
1222 RESETINPUT(duty_level
);
1223 RESETINPUT(duty_dur
);
1227 count
= DEMANDINPUT_A(duty_dur
, i
+ 1) * sr
+ count
;
1228 if(sc_isnan(count
)) {
1229 int doneAction
= (int)ZIN0(2);
1230 DoneAction(doneAction
, unit
);
1232 float x
= DEMANDINPUT_A(duty_level
, i
+ 1);
1233 //printf("in %d %g\n", k, x);
1234 if (sc_isnan(x
)) x
= 0.f
;
1243 unit
->m_count
= count
;
1244 unit
->m_prevreset
= prevreset
;
1248 void TDuty_next_dk(TDuty
*unit
, int inNumSamples
)
1251 float zreset
= ZIN0(duty_reset
);
1253 float *out
= OUT(0);
1254 float count
= unit
->m_count
;
1255 float prevreset
= unit
->m_prevreset
;
1256 float sr
= (float) SAMPLERATE
;
1258 for (int i
=0; i
<inNumSamples
; ++i
) {
1260 if (zreset
> 0.f
&& prevreset
<= 0.f
) {
1262 RESETINPUT(duty_level
);
1263 RESETINPUT(duty_dur
);
1267 count
= DEMANDINPUT_A(duty_dur
, i
+ 1) * sr
+ count
;
1268 if(sc_isnan(count
)) {
1269 int doneAction
= (int)ZIN0(2);
1270 DoneAction(doneAction
, unit
);
1272 float x
= DEMANDINPUT_A(duty_level
, i
+ 1);
1273 //printf("in %d %g\n", k, x);
1274 if (sc_isnan(x
)) x
= 0.f
;
1283 unit
->m_count
= count
;
1284 unit
->m_prevreset
= prevreset
;
1288 void TDuty_next_dd(TDuty
*unit
, int inNumSamples
)
1291 float *out
= OUT(0);
1292 float count
= unit
->m_count
;
1293 float reset
= unit
->m_prevreset
;
1294 float sr
= (float) SAMPLERATE
;
1296 for (int i
=0; i
<inNumSamples
; ++i
) {
1299 RESETINPUT(duty_level
);
1300 RESETINPUT(duty_dur
);
1302 reset
= DEMANDINPUT_A(duty_reset
, i
+ 1) * sr
+ reset
;
1307 count
= DEMANDINPUT_A(duty_dur
, i
+ 1) * sr
+ count
;
1308 if(sc_isnan(count
)) {
1309 int doneAction
= (int)ZIN0(2);
1310 DoneAction(doneAction
, unit
);
1312 float x
= DEMANDINPUT_A(duty_level
, i
+ 1);
1313 //printf("in %d %g\n", k, x);
1314 if (sc_isnan(x
)) x
= 0.f
;
1323 unit
->m_count
= count
;
1324 unit
->m_prevreset
= reset
;
1329 void TDuty_Ctor(TDuty
*unit
)
1331 if (INRATE(1) == calc_FullRate
) {
1333 SETCALC(TDuty_next_da
);
1334 unit
->m_prevreset
= 0.f
;
1337 if(INRATE(1) == calc_DemandRate
) {
1338 SETCALC(TDuty_next_dd
);
1339 unit
->m_prevreset
= DEMANDINPUT(1) * SAMPLERATE
;
1341 SETCALC(TDuty_next_dk
);
1342 unit
->m_prevreset
= 0.f
;
1345 // support for gap-first.
1347 unit
->m_count
= DEMANDINPUT(duty_dur
) * SAMPLERATE
;
1349 unit
->m_count
= 0.f
;
1355 /////////////////////////////////////////////////////////////////////////////
1359 void Dseries_next(Dseries
*unit
, int inNumSamples
)
1362 float step
= DEMANDINPUT_A(2, inNumSamples
);
1363 if(!sc_isnan(step
)) { unit
->m_step
= step
; }
1364 if (unit
->m_repeats
< 0.) {
1365 float x
= DEMANDINPUT_A(0, inNumSamples
);
1366 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1367 unit
->m_value
= DEMANDINPUT_A(1, inNumSamples
);
1369 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1373 OUT0(0) = unit
->m_value
;
1374 unit
->m_value
+= unit
->m_step
;
1375 unit
->m_repeatCount
++;
1377 unit
->m_repeats
= -1.f
;
1378 unit
->m_repeatCount
= 0;
1382 void Dseries_Ctor(Dseries
*unit
)
1384 SETCALC(Dseries_next
);
1385 Dseries_next(unit
, 0);
1390 void Dgeom_next(Dgeom
*unit
, int inNumSamples
)
1393 float grow
= DEMANDINPUT_A(2, inNumSamples
);
1394 if(!sc_isnan(grow
)) { unit
->m_grow
= grow
; }
1395 if (unit
->m_repeats
< 0.) {
1396 float x
= DEMANDINPUT_A(0, inNumSamples
);
1397 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1398 unit
->m_value
= DEMANDINPUT_A(1, inNumSamples
);
1400 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1404 OUT0(0) = unit
->m_value
;
1405 unit
->m_value
*= unit
->m_grow
;
1406 unit
->m_repeatCount
++;
1408 unit
->m_repeats
= -1.f
;
1409 unit
->m_repeatCount
= 0;
1413 void Dgeom_Ctor(Dgeom
*unit
)
1415 SETCALC(Dgeom_next
);
1416 Dgeom_next(unit
, 0);
1421 void Dwhite_next(Dwhite
*unit
, int inNumSamples
)
1424 if (unit
->m_repeats
< 0.) {
1425 float x
= DEMANDINPUT_A(0, inNumSamples
);
1426 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1428 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1432 unit
->m_repeatCount
++;
1433 float lo
= DEMANDINPUT_A(1, inNumSamples
);
1434 float hi
= DEMANDINPUT_A(2, inNumSamples
);
1436 if(!sc_isnan(lo
)) { unit
->m_lo
= lo
;}
1437 if(!sc_isnan(hi
)) { unit
->m_range
= hi
- lo
; }
1438 float x
= unit
->mParent
->mRGen
->frand() * unit
->m_range
+ unit
->m_lo
;
1441 unit
->m_repeats
= -1.f
;
1442 unit
->m_repeatCount
= 0;
1446 void Dwhite_Ctor(Dwhite
*unit
)
1448 SETCALC(Dwhite_next
);
1449 Dwhite_next(unit
, 0);
1454 void Diwhite_next(Diwhite
*unit
, int inNumSamples
)
1457 float lo
= DEMANDINPUT_A(1, inNumSamples
);
1458 float hi
= DEMANDINPUT_A(2, inNumSamples
);
1460 if(!sc_isnan(lo
)) { unit
->m_lo
= (int32
)floor(DEMANDINPUT_A(1, inNumSamples
) + 0.5f
); }
1462 int32 hi
= (int32
)floor(DEMANDINPUT_A(2, inNumSamples
) + 0.5f
);
1463 unit
->m_range
= hi
- unit
->m_lo
+ 1;
1466 if (unit
->m_repeats
< 0.) {
1467 float x
= DEMANDINPUT_A(0, inNumSamples
);
1468 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1470 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1474 unit
->m_repeatCount
++;
1475 float x
= unit
->mParent
->mRGen
->irand(unit
->m_range
) + unit
->m_lo
;
1478 unit
->m_repeats
= -1.f
;
1479 unit
->m_repeatCount
= 0;
1483 void Diwhite_Ctor(Diwhite
*unit
)
1485 SETCALC(Diwhite_next
);
1486 Diwhite_next(unit
, 0);
1491 void Dbrown_next(Dbrown
*unit
, int inNumSamples
)
1494 float lo
= DEMANDINPUT_A(1, inNumSamples
); if(!sc_isnan(lo
)) { unit
->m_lo
= lo
; }
1495 float hi
= DEMANDINPUT_A(2, inNumSamples
); if(!sc_isnan(hi
)) { unit
->m_hi
= hi
; }
1496 float step
= DEMANDINPUT_A(3, inNumSamples
); if(!sc_isnan(step
)) { unit
->m_step
= step
; }
1498 if (unit
->m_repeats
< 0.) {
1499 float x
= DEMANDINPUT_A(0, inNumSamples
);
1500 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1501 unit
->m_val
= unit
->mParent
->mRGen
->frand() * (unit
->m_hi
- unit
->m_lo
) + unit
->m_lo
;
1503 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1507 unit
->m_repeatCount
++;
1508 OUT0(0) = unit
->m_val
;
1509 float x
= unit
->m_val
+ unit
->mParent
->mRGen
->frand2() * unit
->m_step
;
1510 unit
->m_val
= sc_fold(x
, unit
->m_lo
, unit
->m_hi
);
1512 unit
->m_repeats
= -1.f
;
1513 unit
->m_repeatCount
= 0;
1517 void Dbrown_Ctor(Dbrown
*unit
)
1519 SETCALC(Dbrown_next
);
1520 Dbrown_next(unit
, 0);
1525 void Dibrown_next(Dibrown
*unit
, int inNumSamples
)
1529 float lo
= DEMANDINPUT_A(1, inNumSamples
); if(!sc_isnan(lo
)) { unit
->m_lo
= (int32
)lo
; }
1530 float hi
= DEMANDINPUT_A(2, inNumSamples
); if(!sc_isnan(hi
)) { unit
->m_hi
= (int32
)hi
; }
1531 float step
= DEMANDINPUT_A(3, inNumSamples
); if(!sc_isnan(step
)) { unit
->m_step
= (int32
)step
; }
1533 if (unit
->m_repeats
< 0.) {
1534 float x
= DEMANDINPUT_A(0, inNumSamples
);
1535 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1536 unit
->m_val
= unit
->mParent
->mRGen
->irand(unit
->m_hi
- unit
->m_lo
+ 1) + unit
->m_lo
;
1538 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1542 OUT0(0) = unit
->m_val
;
1543 int32 z
= unit
->m_val
+ unit
->mParent
->mRGen
->irand2(unit
->m_step
);
1544 unit
->m_val
= sc_fold(z
, unit
->m_lo
, unit
->m_hi
);
1546 unit
->m_repeats
= -1.f
;
1547 unit
->m_repeatCount
= 0;
1551 void Dibrown_Ctor(Dibrown
*unit
)
1553 SETCALC(Dibrown_next
);
1554 Dibrown_next(unit
, 0);
1560 void Dseq_next(Dseq
*unit
, int inNumSamples
)
1562 //Print("->Dseq_next %d\n", inNumSamples);
1564 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1565 if (unit
->m_repeats
< 0.) {
1566 float x
= DEMANDINPUT_A(0, inNumSamples
);
1567 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1571 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1572 if (unit
->m_index
>= unit
->mNumInputs
) {
1574 unit
->m_repeatCount
++;
1576 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1582 if (ISDEMANDINPUT(unit
->m_index
)) {
1583 if (unit
->m_needToResetChild
) {
1584 unit
->m_needToResetChild
= false;
1585 RESETINPUT(unit
->m_index
);
1587 float x
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1590 unit
->m_needToResetChild
= true;
1596 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1597 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1599 unit
->m_needToResetChild
= true;
1603 if (attempts
++ > unit
->mNumInputs
) {
1604 Print("Warning: empty sequence in Dseq\n");
1609 unit
->m_repeats
= -1.f
;
1610 unit
->m_repeatCount
= 0;
1611 unit
->m_needToResetChild
= true;
1617 void Dseq_Ctor(Dseq
*unit
)
1625 void Dser_next(Dser
*unit
, int inNumSamples
)
1628 if (unit
->m_repeats
< 0.) {
1629 float x
= DEMANDINPUT_A(0, inNumSamples
);
1630 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1633 if (unit
->m_index
>= unit
->mNumInputs
) {
1636 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1640 if (ISDEMANDINPUT(unit
->m_index
)) {
1641 if (unit
->m_needToResetChild
) {
1642 unit
->m_needToResetChild
= false;
1643 RESETINPUT(unit
->m_index
);
1645 float x
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1648 unit
->m_repeatCount
++;
1649 unit
->m_needToResetChild
= true;
1655 OUT0(0) = IN0(unit
->m_index
);
1657 unit
->m_repeatCount
++;
1658 unit
->m_needToResetChild
= true;
1663 unit
->m_repeats
= -1.f
;
1664 unit
->m_repeatCount
= 0;
1665 unit
->m_needToResetChild
= true;
1670 void Dser_Ctor(Dser
*unit
)
1679 void Drand_next(Drand
*unit
, int inNumSamples
)
1683 if (unit
->m_repeats
< 0.) {
1684 float x
= DEMANDINPUT_A(0, inNumSamples
);
1685 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1688 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1692 if (ISDEMANDINPUT(unit
->m_index
)) {
1693 if (unit
->m_needToResetChild
) {
1694 unit
->m_needToResetChild
= false;
1695 RESETINPUT(unit
->m_index
);
1697 float x
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1699 unit
->m_index
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 1) + 1;
1700 unit
->m_repeatCount
++;
1701 unit
->m_needToResetChild
= true;
1707 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1708 unit
->m_index
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 1) + 1;
1709 unit
->m_repeatCount
++;
1710 unit
->m_needToResetChild
= true;
1715 unit
->m_repeats
= -1.f
;
1716 unit
->m_repeatCount
= 0;
1717 unit
->m_needToResetChild
= true;
1718 unit
->m_index
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 1) + 1;
1722 void Drand_Ctor(Drand
*unit
)
1724 SETCALC(Drand_next
);
1725 Drand_next(unit
, 0);
1731 void Dxrand_next(Dxrand
*unit
, int 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
);
1739 if (unit
->m_index
>= unit
->mNumInputs
) {
1742 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1746 if (ISDEMANDINPUT(unit
->m_index
)) {
1747 if (unit
->m_needToResetChild
) {
1748 unit
->m_needToResetChild
= false;
1749 RESETINPUT(unit
->m_index
);
1751 float x
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1753 int newindex
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 2) + 1;
1754 unit
->m_index
= newindex
< unit
->m_index
? newindex
: newindex
+ 1;
1755 unit
->m_repeatCount
++;
1756 unit
->m_needToResetChild
= true;
1762 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1763 int newindex
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 2) + 1;
1764 unit
->m_index
= newindex
< unit
->m_index
? newindex
: newindex
+ 1;
1765 unit
->m_repeatCount
++;
1766 unit
->m_needToResetChild
= true;
1771 unit
->m_repeats
= -1.f
;
1772 unit
->m_repeatCount
= 0;
1773 unit
->m_needToResetChild
= true;
1774 int newindex
= unit
->mParent
->mRGen
->irand(unit
->mNumInputs
- 2) + 1;
1775 unit
->m_index
= newindex
< unit
->m_index
? newindex
: newindex
+ 1;
1779 void Dxrand_Ctor(Dxrand
*unit
)
1781 SETCALC(Dxrand_next
);
1782 Dxrand_next(unit
, 0);
1787 float w, sum = 0.0; \
1788 float r = unit->mParent->mRGen->frand(); \
1789 for (int i=0; i<weights_size; ++i) { \
1793 unit->m_index = i + offset; \
1799 void Dwrand_next(Dwrand *unit, int inNumSamples)
1801 int offset
= unit
->m_weights_size
+ 2;
1802 int weights_size
= unit
->mNumInputs
- offset
;
1805 if (unit
->m_repeats
< 0.) {
1806 float x
= DEMANDINPUT_A(0, inNumSamples
);
1807 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1811 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1816 if (ISDEMANDINPUT(unit
->m_index
)) {
1817 if (unit
->m_needToResetChild
) {
1818 unit
->m_needToResetChild
= false;
1819 RESETINPUT(unit
->m_index
);
1821 float x
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1825 unit
->m_repeatCount
++;
1826 unit
->m_needToResetChild
= true;
1832 OUT0(0) = DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1834 unit
->m_repeatCount
++;
1835 unit
->m_needToResetChild
= true;
1840 unit
->m_repeats
= -1.f
;
1841 unit
->m_repeatCount
= 0;
1842 unit
->m_needToResetChild
= true;
1847 void Dwrand_Ctor(Dwrand
*unit
)
1849 SETCALC(Dwrand_next
);
1850 unit
->m_weights_size
= IN0(1);
1851 Dwrand_next(unit
, 0);
1857 static void Dshuf_scramble(Dshuf
*unit
);
1859 void Dshuf_next(Dshuf
*unit
, int inNumSamples
)
1861 // Print("->Dshuf_next %d\n", inNumSamples);
1863 //Print(" unit->m_repeats %d\n", unit->m_repeats);
1864 if (unit
->m_repeats
< 0.) {
1865 float x
= DEMANDINPUT_A(0, inNumSamples
);
1866 unit
->m_repeats
= sc_isnan(x
) ? 0.f
: floor(x
+ 0.5f
);
1869 //Print(" unit->m_index %d unit->m_repeatCount %d\n", unit->m_index, unit->m_repeatCount);
1870 if (unit
->m_index
>= (unit
->mNumInputs
- 1)) {
1872 unit
->m_repeatCount
++;
1874 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
1880 if (ISDEMANDINPUT(unit
->m_indices
[unit
->m_index
])) {
1881 if (unit
->m_needToResetChild
) {
1882 unit
->m_needToResetChild
= false;
1883 RESETINPUT(unit
->m_indices
[unit
->m_index
]);
1885 float x
= DEMANDINPUT_A(unit
->m_indices
[unit
->m_index
], inNumSamples
);
1889 unit
->m_needToResetChild
= true;
1895 OUT0(0) = DEMANDINPUT_A(unit
->m_indices
[unit
->m_index
], inNumSamples
);
1896 //Print(" unit->m_index %d OUT0(0) %g\n", unit->m_index, OUT0(0));
1898 unit
->m_needToResetChild
= true;
1903 unit
->m_repeats
= -1.f
;
1904 unit
->m_repeatCount
= 0;
1905 unit
->m_needToResetChild
= true;
1907 Dshuf_scramble(unit
);
1911 void Dshuf_scramble(Dshuf
*unit
) {
1912 int32 i
, j
, m
, k
, size
;
1915 size
= (int32
)(unit
->mNumInputs
) - 1;
1919 for (i
=0, m
=k
; i
<k
-1; ++i
, --m
) {
1920 j
= i
+ unit
->mParent
->mRGen
->irand(m
);
1921 temp
= unit
->m_indices
[i
];
1922 unit
->m_indices
[i
] = unit
->m_indices
[j
];
1923 unit
->m_indices
[j
] = temp
;
1929 void Dshuf_Ctor(Dshuf
*unit
)
1933 size
= (int32
)(unit
->mNumInputs
) - 1;
1935 unit
->m_indices
= (int32
*)RTAlloc(unit
->mWorld
, size
* sizeof(int32
));
1937 for(i
=0; i
< size
; ++i
) {
1938 unit
->m_indices
[i
] = i
+ 1;
1941 SETCALC(Dshuf_next
);
1942 Dshuf_next(unit
, 0);
1946 void Dshuf_Dtor(Dshuf
*unit
)
1948 RTFree(unit
->mWorld
, unit
->m_indices
);
1952 void Dswitch1_next(Dswitch1
*unit
, int inNumSamples
)
1955 float x
= DEMANDINPUT_A(0, inNumSamples
);
1960 int index
= (int32
)floor(x
+ 0.5f
);
1961 index
= sc_wrap(index
, 0, unit
->mNumInputs
- 2) + 1;
1962 OUT0(0) = DEMANDINPUT_A(index
, inNumSamples
);
1964 for (int i
=0; i
<unit
->mNumInputs
; ++i
) {
1970 void Dswitch1_Ctor(Dswitch1
*unit
)
1972 SETCALC(Dswitch1_next
);
1976 /////////////////////////////
1978 void Dswitch_next(Dswitch
*unit
, int inNumSamples
)
1981 float val
= DEMANDINPUT_A(unit
->m_index
, inNumSamples
);
1982 //printf("index: %i\n", (int) val);
1984 float ival
= DEMANDINPUT_A(0, inNumSamples
);
1989 int index
= (int32
)floor(ival
+ 0.5f
);
1990 index
= sc_wrap(index
, 0, unit
->mNumInputs
- 2) + 1;
1991 val
= DEMANDINPUT_A(index
, inNumSamples
);
1993 RESETINPUT(unit
->m_index
);
1994 // printf("resetting index: %i\n", unit->m_index);
1995 unit
->m_index
= index
;
2002 for (int i
=0; i
<unit
->mNumInputs
; ++i
) {
2005 int index
= (int32
)floor(DEMANDINPUT(0) + 0.5f
);
2006 index
= sc_wrap(index
, 0, unit
->mNumInputs
- 1) + 1;
2007 unit
->m_index
= index
;
2011 void Dswitch_Ctor(Dswitch
*unit
)
2013 SETCALC(Dswitch_next
);
2014 int index
= (int32
)floor(DEMANDINPUT(0) + 0.5f
);
2015 index
= sc_wrap(index
, 0, unit
->mNumInputs
- 1) + 1;
2016 unit
->m_index
= index
;
2020 //////////////////////////////
2023 void Dstutter_next(Dstutter
*unit
, int inNumSamples
)
2027 if (unit
->m_repeatCount
>= unit
->m_repeats
) {
2029 float val
= DEMANDINPUT_A(1, inNumSamples
);
2030 float repeats
= DEMANDINPUT_A(0, inNumSamples
);
2032 if(sc_isnan(repeats
) || sc_isnan(val
)) {
2036 unit
->m_value
= val
;
2037 unit
->m_repeats
= floor(repeats
+ 0.5f
);
2038 unit
->m_repeatCount
= 0.f
;
2042 OUT0(0) = unit
->m_value
;
2043 unit
->m_repeatCount
++;
2047 unit
->m_repeats
= -1.f
;
2048 unit
->m_repeatCount
= 0.f
;
2055 void Dstutter_Ctor(Dstutter
*unit
)
2057 SETCALC(Dstutter_next
);
2058 Dstutter_next(unit
, 0);
2062 //////////////////////////////
2066 void Dpoll_next(Dpoll
*unit
, int inNumSamples
)
2069 float x
= DEMANDINPUT_A(0, inNumSamples
);
2070 float run
= DEMANDINPUT_A(2, inNumSamples
) > 0.f
;
2071 if(unit
->m_mayprint
&& run
) {
2072 Print("%s: %g block offset: %d\n", unit
->m_id_string
, x
, inNumSamples
- 1);
2074 if(IN0(1) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(1), x
);
2081 void Dpoll_Ctor(Dpoll
*unit
)
2083 SETCALC(Dpoll_next
);
2084 unit
->m_id
= IN0(3); // number of chars in the id string
2085 unit
->m_id_string
= (char*)RTAlloc(unit
->mWorld
, ((int)unit
->m_id
+ 1) * sizeof(char));
2086 for(int i
= 0; i
< (int)unit
->m_id
; i
++) {
2087 unit
->m_id_string
[i
] = (char)IN0(4+i
);
2089 unit
->m_id_string
[(int)unit
->m_id
] = '\0';
2090 unit
->m_mayprint
= unit
->mWorld
->mVerbosity
>= 0;
2094 void Dpoll_Dtor(Dpoll
* unit
)
2096 RTFree(unit
->mWorld
, unit
->m_id_string
);
2099 //////////////////////////////
2103 void Dreset_next(Dreset
*unit
, int inNumSamples
)
2106 float x
= DEMANDINPUT_A(0, inNumSamples
);
2107 float reset
= DEMANDINPUT_A(1, inNumSamples
);
2112 if(reset
> 0.0 && (unit
->prev_reset
<= 0.0)) { RESETINPUT(0); }
2113 unit
->prev_reset
= reset
;
2120 void Dreset_Ctor(Dreset
*unit
)
2122 SETCALC(Dreset_next
);
2123 unit
->prev_reset
= 0.0;
2124 Dreset_next(unit
, 0);
2129 //////////////////////////////
2133 void Donce_next(Donce
*unit
, int inNumSamples
)
2136 if (unit
->m_bufcounter
== unit
->mWorld
->mBufCounter
) {
2137 OUT0(0) = unit
->m_prev
;
2139 float x
= DEMANDINPUT_A(0, inNumSamples
);
2148 void Donce_Ctor(Donce
*unit
)
2150 SETCALC(Donce_next
);
2155 inline double sc_loop(Unit
*unit
, double in
, double hi
, int loop
)
2157 // avoid the divide if possible
2164 if (in
< hi
) return in
;
2165 } else if (in
< 0.) {
2171 if (in
>= 0.) return in
;
2174 return in
- hi
* floor(in
/hi
);
2177 #define D_CHECK_BUF \
2179 unit->mDone = true; \
2180 ClearUnitOutputs(unit, 1); \
2185 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2186 if (fbufnum != unit->m_fbufnum) { \
2187 uint32 bufnum = (int)fbufnum; \
2188 World *world = unit->mWorld; \
2189 if (bufnum < 0) { bufnum = 0; } \
2190 if (bufnum >= world->mNumSndBufs) { \
2191 int localBufNum = bufnum - world->mNumSndBufs; \
2192 Graph *parent = unit->mParent; \
2193 if(localBufNum <= parent->localBufNum) { \
2194 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2197 unit->m_buf = world->mSndBufs + bufnum; \
2200 unit->m_buf = world->mSndBufs + bufnum; \
2202 unit->m_fbufnum = fbufnum; \
2204 SndBuf *buf = unit->m_buf; \
2206 float *bufData __attribute__((__unused__)) = buf->data; \
2207 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2208 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2209 uint32 bufFrames = buf->frames; \
2210 int mask __attribute__((__unused__)) = buf->mask; \
2211 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2213 #define D_GET_BUF_SHARED \
2214 float fbufnum = DEMANDINPUT_A(0, inNumSamples);; \
2215 if (fbufnum != unit->m_fbufnum) { \
2216 uint32 bufnum = (int)fbufnum; \
2217 World *world = unit->mWorld; \
2218 if (bufnum < 0) { bufnum = 0; } \
2219 if (bufnum >= world->mNumSndBufs) { \
2220 int localBufNum = bufnum - world->mNumSndBufs; \
2221 Graph *parent = unit->mParent; \
2222 if(localBufNum <= parent->localBufNum) { \
2223 unit->m_buf = parent->mLocalSndBufs + localBufNum; \
2226 unit->m_buf = world->mSndBufs + bufnum; \
2229 unit->m_buf = world->mSndBufs + bufnum; \
2231 unit->m_fbufnum = fbufnum; \
2233 const SndBuf *buf = unit->m_buf; \
2234 LOCK_SNDBUF_SHARED(buf); \
2235 const float *bufData __attribute__((__unused__)) = buf->data; \
2236 uint32 bufChannels __attribute__((__unused__)) = buf->channels; \
2237 uint32 bufSamples __attribute__((__unused__)) = buf->samples; \
2238 uint32 bufFrames = buf->frames; \
2239 int mask __attribute__((__unused__)) = buf->mask; \
2240 int guardFrame __attribute__((__unused__)) = bufFrames - 2;
2243 void Dbufrd_next(Dbufrd
*unit
, int inNumSamples
)
2245 int32 loop
= (int32
)DEMANDINPUT_A(2, inNumSamples
);
2250 double loopMax
= (double)(loop
? bufFrames
: bufFrames
- 1);
2255 float x
= DEMANDINPUT_A(1, inNumSamples
);
2262 phase
= sc_loop((Unit
*)unit
, phase
, loopMax
, loop
);
2263 int32 iphase
= (int32
)phase
;
2264 const float* table1
= bufData
+ iphase
* bufChannels
;
2265 OUT0(0) = table1
[0];
2273 void Dbufrd_Ctor(Dbufrd
*unit
)
2275 SETCALC(Dbufrd_next
);
2277 unit
->m_fbufnum
= -1e9f
;
2279 Dbufrd_next(unit
, 0);
2283 ////////////////////////////////////
2285 void Dbufwr_next(Dbufwr
*unit
, int inNumSamples
)
2287 int32 loop
= (int32
)DEMANDINPUT_A(3, inNumSamples
);
2292 double loopMax
= (double)(loop
? bufFrames
: bufFrames
- 1);
2298 float x
= DEMANDINPUT_A(1, inNumSamples
);
2304 val
= DEMANDINPUT_A(2, inNumSamples
);
2305 if (sc_isnan(val
)) {
2310 phase
= sc_loop((Unit
*)unit
, phase
, loopMax
, loop
);
2311 int32 iphase
= (int32
)phase
;
2312 float* table0
= bufData
+ iphase
* bufChannels
;
2323 void Dbufwr_Ctor(Dbufwr
*unit
)
2325 SETCALC(Dbufwr_next
);
2327 unit
->m_fbufnum
= -1e9f
;
2329 Dbufwr_next(unit
, 0);
2334 //////////////////////////////////////////////////////
2342 DefineDtorCantAliasUnit(Demand
);
2343 DefineSimpleCantAliasUnit(Duty
);
2344 DefineSimpleCantAliasUnit(DemandEnvGen
);
2345 DefineSimpleCantAliasUnit(TDuty
);
2346 DefineSimpleUnit(Dseries
);
2347 DefineSimpleUnit(Dgeom
);
2348 DefineSimpleUnit(Dwhite
);
2349 DefineSimpleUnit(Dbrown
);
2350 DefineSimpleUnit(Diwhite
);
2351 DefineSimpleUnit(Dibrown
);
2352 DefineSimpleUnit(Dseq
);
2353 DefineSimpleUnit(Dser
);
2354 DefineSimpleUnit(Dbufrd
);
2355 DefineSimpleUnit(Dbufwr
);
2356 DefineSimpleUnit(Drand
);
2357 DefineSimpleUnit(Dwrand
);
2358 DefineSimpleUnit(Dxrand
);
2359 DefineDtorUnit(Dshuf
);
2360 DefineSimpleUnit(Dswitch1
);
2361 DefineSimpleUnit(Dswitch
);
2362 DefineSimpleUnit(Dstutter
);
2363 DefineSimpleUnit(Donce
);
2364 DefineSimpleUnit(Dreset
);
2365 DefineDtorUnit(Dpoll
);