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
22 #include "SC_PlugIn.h"
23 #include "SIMD_Unit.hpp"
26 #include "function_attributes.h"
28 static InterfaceTable
*ft
;
30 struct Vibrato
: public Unit
32 double mPhase
, m_attackSlope
, m_attackLevel
;
33 float mFreqMul
, m_scaleA
, m_scaleB
, mFreq
;
34 int m_delay
, m_attack
;
37 struct LFPulse
: public Unit
40 float mFreqMul
, mDuty
;
43 struct LFSaw
: public Unit
49 struct LFPar
: public Unit
55 struct LFCub
: public Unit
61 struct LFTri
: public Unit
67 struct LFGauss
: public Unit
73 struct Impulse
: public Unit
75 double mPhase
, mPhaseOffset
;
79 struct VarSaw
: public Unit
82 float mFreqMul
, mDuty
, mInvDuty
, mInv1Duty
;
85 struct SyncSaw
: public Unit
87 double mPhase1
, mPhase2
;
91 struct Line
: public Unit
93 double mLevel
, mSlope
;
98 struct XLine
: public Unit
100 double mLevel
, mGrowth
;
105 struct Cutoff
: public Unit
107 double mLevel
, mSlope
;
111 struct LinExp
: public Unit
113 float m_dstratio
, m_rsrcrange
, m_rrminuslo
, m_dstlo
;
116 struct Clip
: public Unit
121 struct Wrap
: public Unit
126 struct Fold
: public Unit
128 float m_lo
, m_hi
, m_range
;
131 struct Unwrap
: public Unit
133 float m_range
, m_half
, m_offset
, m_prev
;
136 struct AmpComp
: public Unit
138 float m_rootmul
, m_exponent
;
141 struct AmpCompA
: public Unit
143 double m_scale
, m_offset
;
146 struct InRange
: public Unit
151 struct InRect
: public Unit
156 //struct Trapezoid : public Unit
158 // float m_leftScale, m_rightScale, m_a, m_b, m_c, m_d;
161 struct A2K
: public Unit
166 struct T2K
: public Unit
171 struct T2A
: public Unit
176 struct EnvGen
: public Unit
178 double m_a1
, m_a2
, m_b1
, m_y1
, m_y2
, m_grow
, m_level
, m_endLevel
;
179 int m_counter
, m_stage
, m_shape
, m_releaseNode
;
184 struct Linen
: public Unit
187 double m_slope
, m_level
;
188 int m_counter
, m_stage
;
193 //////////////////////////////////////////////////////////////////////////////////////////////////
198 void Vibrato_next(Vibrato
*unit
, int inNumSamples
);
199 void Vibrato_Ctor(Vibrato
* unit
);
201 void LFPulse_next_a(LFPulse
*unit
, int inNumSamples
);
202 void LFPulse_next_k(LFPulse
*unit
, int inNumSamples
);
203 void LFPulse_Ctor(LFPulse
* unit
);
205 void LFSaw_next_a(LFSaw
*unit
, int inNumSamples
);
206 void LFSaw_next_k(LFSaw
*unit
, int inNumSamples
);
207 void LFSaw_Ctor(LFSaw
* unit
);
209 void LFTri_next_a(LFTri
*unit
, int inNumSamples
);
210 void LFTri_next_k(LFTri
*unit
, int inNumSamples
);
211 void LFTri_Ctor(LFTri
* unit
);
213 void LFPar_next_a(LFPar
*unit
, int inNumSamples
);
214 void LFPar_next_k(LFPar
*unit
, int inNumSamples
);
215 void LFPar_Ctor(LFPar
* unit
);
217 void LFCub_next_a(LFCub
*unit
, int inNumSamples
);
218 void LFCub_next_k(LFCub
*unit
, int inNumSamples
);
219 void LFCub_Ctor(LFCub
* unit
);
221 void LFGauss_next_a(LFGauss
*unit
, int inNumSamples
);
222 void LFGauss_next_k(LFGauss
*unit
, int inNumSamples
);
223 void LFGauss_next_aa(LFGauss
*unit
, int inNumSamples
);
224 void LFGauss_Ctor(LFGauss
* unit
);
226 void VarSaw_next_a(VarSaw
*unit
, int inNumSamples
);
227 void VarSaw_next_k(VarSaw
*unit
, int inNumSamples
);
228 void VarSaw_Ctor(VarSaw
* unit
);
230 void Impulse_next_a(Impulse
*unit
, int inNumSamples
);
231 void Impulse_next_kk(Impulse
*unit
, int inNumSamples
);
232 void Impulse_next_k(Impulse
*unit
, int inNumSamples
);
233 void Impulse_Ctor(Impulse
* unit
);
235 void SyncSaw_next_aa(SyncSaw
*unit
, int inNumSamples
);
236 void SyncSaw_next_ak(SyncSaw
*unit
, int inNumSamples
);
237 void SyncSaw_next_ka(SyncSaw
*unit
, int inNumSamples
);
238 void SyncSaw_next_kk(SyncSaw
*unit
, int inNumSamples
);
239 void SyncSaw_Ctor(SyncSaw
* unit
);
241 void A2K_next(A2K
*unit
, int inNumSamples
);
242 void A2K_Ctor(A2K
* unit
);
244 void T2K_next(T2K
*unit
, int inNumSamples
);
245 void T2K_Ctor(T2K
* unit
);
247 void T2A_next(T2A
*unit
, int inNumSamples
);
248 void T2A_Ctor(T2A
* unit
);
250 void Line_next(Line
*unit
, int inNumSamples
);
251 void Line_Ctor(Line
* unit
);
253 void XLine_next(XLine
*unit
, int inNumSamples
);
254 void XLine_Ctor(XLine
* unit
);
256 void Wrap_next_kk(Wrap
*unit
, int inNumSamples
);
257 void Wrap_next_ak(Wrap
*unit
, int inNumSamples
);
258 void Wrap_next_ka(Wrap
*unit
, int inNumSamples
);
259 void Wrap_next_aa(Wrap
*unit
, int inNumSamples
);
260 void Wrap_Ctor(Wrap
* unit
);
262 void Fold_next_kk(Fold
*unit
, int inNumSamples
);
263 void Fold_next_ak(Fold
*unit
, int inNumSamples
);
264 void Fold_next_ka(Fold
*unit
, int inNumSamples
);
265 void Fold_next_aa(Fold
*unit
, int inNumSamples
);
266 void Fold_Ctor(Fold
* unit
);
268 void Clip_next_kk(Clip
*unit
, int inNumSamples
);
269 void Clip_next_ka(Clip
*unit
, int inNumSamples
);
270 void Clip_next_ak(Clip
*unit
, int inNumSamples
);
271 void Clip_next_aa(Clip
*unit
, int inNumSamples
);
272 void Clip_Ctor(Clip
* unit
);
274 void Unwrap_next(Unwrap
* unit
, int inNumSamples
);
275 void Unwrap_Ctor(Unwrap
* unit
);
277 void AmpComp_next(AmpComp
*unit
, int inNumSamples
);
278 void AmpComp_Ctor(AmpComp
* unit
);
280 void AmpCompA_next(AmpCompA
*unit
, int inNumSamples
);
281 void AmpCompA_Ctor(AmpCompA
* unit
);
283 void InRange_next(InRange
*unit
, int inNumSamples
);
284 void InRange_Ctor(InRange
* unit
);
286 void InRect_next(InRect
*unit
, int inNumSamples
);
287 void InRect_Ctor(InRect
* unit
);
289 void LinExp_next(LinExp
*unit
, int inNumSamples
);
290 void LinExp_next_kk(LinExp
*unit
, int inNumSamples
);
291 void LinExp_next_ak(LinExp
*unit
, int inNumSamples
);
292 void LinExp_next_ka(LinExp
*unit
, int inNumSamples
);
293 void LinExp_Ctor(LinExp
* unit
);
295 void EnvGen_next_k(EnvGen
*unit
, int inNumSamples
);
296 void EnvGen_next_aa(EnvGen
*unit
, int inNumSamples
);
297 void EnvGen_next_ak(EnvGen
*unit
, int inNumSamples
);
298 void EnvGen_Ctor(EnvGen
*unit
);
300 void Linen_next_k(Linen
*unit
, int inNumSamples
);
301 void Linen_Ctor(Linen
*unit
);
305 //////////////////////////////////////////////////////////////////////////////////////////////////
307 // in, rate, depth, rateVariation, depthVariation
310 void Vibrato_next(Vibrato
*unit
, int inNumSamples
)
312 float *out
= ZOUT(0);
315 double ffreq
= unit
->mFreq
;
316 double phase
= unit
->mPhase
;
317 float scaleA
= unit
->m_scaleA
;
318 float scaleB
= unit
->m_scaleB
;
319 if (unit
->m_delay
> 0)
321 int remain
= sc_min(inNumSamples
, unit
->m_delay
);
322 unit
->m_delay
-= remain
;
323 inNumSamples
-= remain
;
327 if (unit
->m_delay
<= 0 && inNumSamples
> 0) {
328 if (unit
->m_attack
> 0) goto doAttack
;
332 else if (unit
->m_attack
)
335 int remain
= sc_min(inNumSamples
, unit
->m_attack
);
336 unit
->m_attack
-= remain
;
337 inNumSamples
-= remain
;
338 double attackSlope
= unit
->m_attackSlope
;
339 double attackLevel
= unit
->m_attackLevel
;
344 ZXP(out
) = ZXP(in
) * (1.f
+ (float)attackLevel
* scaleA
* (1.f
- z
* z
)) ;
346 else if (phase
< 3.f
)
348 float z
= phase
- 2.f
;
349 ZXP(out
) = ZXP(in
) * (1.f
+ (float)attackLevel
* scaleB
* (z
* z
- 1.f
)) ;
356 float depth
= ZIN0(2);
357 float rateVariation
= ZIN0(5);
358 float depthVariation
= ZIN0(6);
360 float rate
= ZIN0(1) * unit
->mFreqMul
;
361 RGen
& rgen
= *unit
->mParent
->mRGen
;
362 ffreq
= rate
* (1.f
+ rateVariation
* rgen
.frand2());
363 scaleA
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
364 scaleB
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
366 ZXP(out
) = ZXP(in
) * (1.f
+ (float)attackLevel
* scaleA
* (1.f
- z
* z
)) ;
369 attackLevel
+= attackSlope
;
371 unit
->m_attackLevel
= attackLevel
;
372 if (unit
->m_attack
<= 0 && inNumSamples
> 0) goto doNormal
;
381 ZXP(out
) = ZXP(in
) * (1.f
+ scaleA
* (1.f
- z
* z
)) ;
383 else if (phase
< 3.f
)
385 float z
= phase
- 2.f
;
386 ZXP(out
) = ZXP(in
) * (1.f
+ scaleB
* (z
* z
- 1.f
)) ;
393 float depth
= ZIN0(2);
394 float rateVariation
= ZIN0(5);
395 float depthVariation
= ZIN0(6);
397 float rate
= ZIN0(1) * unit
->mFreqMul
;
398 RGen
& rgen
= *unit
->mParent
->mRGen
;
399 ffreq
= rate
* (1.f
+ rateVariation
* rgen
.frand2());
400 scaleA
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
401 scaleB
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
403 ZXP(out
) = ZXP(in
) * (1.f
+ scaleA
* (1.f
- z
* z
)) ;
408 unit
->mPhase
= phase
;
410 unit
->m_scaleA
= scaleA
;
411 unit
->m_scaleB
= scaleB
;
415 void Vibrato_Ctor(Vibrato
* unit
)
417 unit
->mFreqMul
= 4.0 * SAMPLEDUR
;
418 unit
->mPhase
= 4.0 * sc_wrap(ZIN0(7), 0.f
, 1.f
) - 1.0;
420 RGen
& rgen
= *unit
->mParent
->mRGen
;
421 float rate
= ZIN0(1) * unit
->mFreqMul
;
422 float depth
= ZIN0(2);
423 float rateVariation
= ZIN0(5);
424 float depthVariation
= ZIN0(6);
425 unit
->mFreq
= rate
* (1.f
+ rateVariation
* rgen
.frand2());
426 unit
->m_scaleA
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
427 unit
->m_scaleB
= depth
* (1.f
+ depthVariation
* rgen
.frand2());
428 unit
->m_delay
= (int)(ZIN0(3) * SAMPLERATE
);
429 unit
->m_attack
= (int)(ZIN0(4) * SAMPLERATE
);
430 unit
->m_attackSlope
= 1. / (double)(1 + unit
->m_attack
);
431 unit
->m_attackLevel
= unit
->m_attackSlope
;
433 SETCALC(Vibrato_next
);
434 Vibrato_next(unit
, 1);
437 //////////////////////////////////////////////////////////////////////////////////////////////////
440 void LFPulse_next_a(LFPulse
*unit
, int inNumSamples
)
442 float *out
= ZOUT(0);
443 float *freq
= ZIN(0);
444 float nextDuty
= ZIN0(2);
445 float duty
= unit
->mDuty
;
447 float freqmul
= unit
->mFreqMul
;
448 double phase
= unit
->mPhase
;
453 duty
= unit
->mDuty
= nextDuty
;
454 // output at least one sample from the opposite polarity
455 z
= duty
< 0.5f
? 1.f
: 0.f
;
457 z
= phase
< duty
? 1.f
: 0.f
;
459 phase
+= ZXP(freq
) * freqmul
;
463 unit
->mPhase
= phase
;
466 void LFPulse_next_k(LFPulse
*unit
, int inNumSamples
)
468 float *out
= ZOUT(0);
469 float freq
= ZIN0(0) * unit
->mFreqMul
;
470 float nextDuty
= ZIN0(2);
471 float duty
= unit
->mDuty
;
473 double phase
= unit
->mPhase
;
478 duty
= unit
->mDuty
= nextDuty
;
479 // output at least one sample from the opposite polarity
480 z
= duty
< 0.5f
? 1.f
: 0.f
;
482 z
= phase
< duty
? 1.f
: 0.f
;
488 unit
->mPhase
= phase
;
491 void LFPulse_Ctor(LFPulse
* unit
)
493 if (INRATE(0) == calc_FullRate
) {
494 SETCALC(LFPulse_next_a
);
496 SETCALC(LFPulse_next_k
);
499 unit
->mFreqMul
= unit
->mRate
->mSampleDur
;
500 unit
->mPhase
= ZIN0(1);
501 unit
->mDuty
= ZIN0(2);
503 LFPulse_next_k(unit
, 1);
507 //////////////////////////////////////////////////////////////////////////////////////////////////
509 void LFSaw_next_a(LFSaw
*unit
, int inNumSamples
)
511 float *out
= ZOUT(0);
512 float *freq
= ZIN(0);
514 float freqmul
= unit
->mFreqMul
;
515 double phase
= unit
->mPhase
;
517 float z
= phase
; // out must be written last for in place operation
518 phase
+= ZXP(freq
) * freqmul
;
519 if (phase
>= 1.f
) phase
-= 2.f
;
520 else if (phase
<= -1.f
) phase
+= 2.f
;
524 unit
->mPhase
= phase
;
527 void LFSaw_next_k(LFSaw
*unit
, int inNumSamples
)
529 float *out
= ZOUT(0);
530 float freq
= ZIN0(0) * unit
->mFreqMul
;
532 double phase
= unit
->mPhase
;
537 if (phase
>= 1.f
) phase
-= 2.f
;
543 if (phase
<= -1.f
) phase
+= 2.f
;
547 unit
->mPhase
= phase
;
550 void LFSaw_Ctor(LFSaw
* unit
)
552 if (INRATE(0) == calc_FullRate
)
553 SETCALC(LFSaw_next_a
);
555 SETCALC(LFSaw_next_k
);
557 unit
->mFreqMul
= 2.0 * unit
->mRate
->mSampleDur
;
558 unit
->mPhase
= ZIN0(1);
560 LFSaw_next_k(unit
, 1);
564 //////////////////////////////////////////////////////////////////////////////////////////////////
566 void LFPar_next_a(LFPar
*unit
, int inNumSamples
)
568 float *out
= ZOUT(0);
569 float *freq
= ZIN(0);
571 float freqmul
= unit
->mFreqMul
;
572 double phase
= unit
->mPhase
;
578 } else if (phase
< 3.f
) {
586 // Note: the following two lines were originally one, but seems to compile wrong on mac
587 float phaseadd
= ZXP(freq
);
588 phase
+= phaseadd
* freqmul
;
592 unit
->mPhase
= phase
;
595 void LFPar_next_k(LFPar
*unit
, int inNumSamples
)
597 float *out
= ZOUT(0);
598 float freq
= ZIN0(0) * unit
->mFreqMul
;
600 double phase
= unit
->mPhase
;
604 ZXP(out
) = 1.f
- z
*z
;
605 } else if (phase
< 3.f
) {
606 float z
= phase
- 2.f
;
607 ZXP(out
) = z
*z
- 1.f
;
611 ZXP(out
) = 1.f
- z
*z
;
616 unit
->mPhase
= phase
;
619 void LFPar_Ctor(LFPar
* unit
)
621 if (INRATE(0) == calc_FullRate
)
622 SETCALC(LFPar_next_a
);
624 SETCALC(LFPar_next_k
);
626 unit
->mFreqMul
= 4.0 * unit
->mRate
->mSampleDur
;
627 unit
->mPhase
= ZIN0(1);
629 LFPar_next_k(unit
, 1);
634 //////////////////////////////////////////////////////////////////////////////////////////////////
636 void LFCub_next_a(LFCub
*unit
, int inNumSamples
)
638 float *out
= ZOUT(0);
639 float *freq
= ZIN(0);
641 float freqmul
= unit
->mFreqMul
;
642 double phase
= unit
->mPhase
;
647 } else if (phase
< 2.f
) {
653 float phaseadd
= ZXP(freq
);
654 phase
+= phaseadd
* freqmul
;
655 ZXP(out
) = z
* z
* (6.f
- 4.f
* z
) - 1.f
;
658 unit
->mPhase
= phase
;
661 void LFCub_next_k(LFCub
*unit
, int inNumSamples
)
663 float *out
= ZOUT(0);
664 float freq
= ZIN0(0) * unit
->mFreqMul
;
666 double phase
= unit
->mPhase
;
671 } else if (phase
< 2.f
) {
677 ZXP(out
) = z
* z
* (6.f
- 4.f
* z
) - 1.f
;
681 unit
->mPhase
= phase
;
684 void LFCub_Ctor(LFCub
* unit
)
686 if (INRATE(0) == calc_FullRate
)
687 SETCALC(LFCub_next_a
);
689 SETCALC(LFCub_next_k
);
691 unit
->mFreqMul
= 2.0 * unit
->mRate
->mSampleDur
;
692 unit
->mPhase
= ZIN0(1) + 0.5;
694 LFCub_next_k(unit
, 1);
699 //////////////////////////////////////////////////////////////////////////////////////////////////
701 void LFTri_next_a(LFTri
*unit
, int inNumSamples
)
703 float *out
= ZOUT(0);
704 float *freq
= ZIN(0);
706 float freqmul
= unit
->mFreqMul
;
707 double phase
= unit
->mPhase
;
709 float z
= phase
> 1.f
? 2.f
- phase
: phase
;
710 phase
+= ZXP(freq
) * freqmul
;
711 if (phase
>= 3.f
) phase
-= 4.f
;
715 unit
->mPhase
= phase
;
718 void LFTri_next_k(LFTri
*unit
, int inNumSamples
)
720 float *out
= ZOUT(0);
721 float freq
= ZIN0(0) * unit
->mFreqMul
;
723 double phase
= unit
->mPhase
;
725 float z
= phase
> 1.f
? 2.f
- phase
: phase
;
727 if (phase
>= 3.f
) phase
-= 4.f
;
731 unit
->mPhase
= phase
;
734 void LFTri_Ctor(LFTri
* unit
)
736 if (INRATE(0) == calc_FullRate
) {
737 SETCALC(LFTri_next_a
);
739 SETCALC(LFTri_next_k
);
742 unit
->mFreqMul
= 4.0 * unit
->mRate
->mSampleDur
;
743 unit
->mPhase
= ZIN0(1);
745 LFTri_next_k(unit
, 1);
749 //////////////////////////////////////////////////////////////////////////////////////////////////
751 void LFGauss_next_k(LFGauss
*unit
, int inNumSamples
)
753 float *out
= ZOUT(0);
758 float loop
= ZIN0(3);
761 double x
= unit
->mPhase
- b
;
763 // for a full cycle from -1 to 1 in duration, double the step.
764 float step
= 2.f
/ (dur
* unit
->mRate
->mSampleRate
);
766 // calculate exponent only once per loop
767 float factor
= -1.f
/ (2.f
* c
* c
);
772 if(loop
) { x
-= 2.f
; } else { DoneAction(ZIN0(4), unit
); }
774 ZXP(out
) = exp(x
* x
* factor
);
778 unit
->mPhase
= x
+ b
;
781 void LFGauss_next_a(LFGauss
*unit
, int inNumSamples
)
783 float *out
= ZOUT(0);
789 float loop
= ZIN0(3);
790 float sr
= unit
->mRate
->mSampleRate
;
793 double x
= unit
->mPhase
- b
;
794 float factor
= -1.f
/ (2.f
* c
* c
);
799 if(loop
) { x
-= 2.f
; } else { DoneAction(ZIN0(4), unit
); }
802 // for a full cycle from -1 to 1 in duration, double the step.
803 float step
= 2.f
/ (ZXP(dur
) * sr
);
805 ZXP(out
) = exp(x
* x
* factor
);
810 unit
->mPhase
= x
+ b
;
815 void LFGauss_next_aa(LFGauss
*unit
, int inNumSamples
)
817 float *out
= ZOUT(0);
823 float loop
= ZIN0(3);
824 float sr
= unit
->mRate
->mSampleRate
;
827 double x
= unit
->mPhase
- b
;
832 if(loop
) { x
-= 2.f
; } else { DoneAction(ZIN0(4), unit
); }
835 // for a full cycle from -1 to 1 in duration, double the step.
836 float step
= 2.f
/ (ZXP(dur
) * sr
);
840 float factor
= -1.f
/ (2.f
* cval
* cval
);
841 ZXP(out
) = exp(x
* x
* factor
);
846 unit
->mPhase
= x
+ b
;
850 void LFGauss_Ctor(LFGauss
* unit
)
853 if (INRATE(0) == calc_FullRate
) {
854 if (INRATE(1) == calc_FullRate
) {
855 SETCALC(LFGauss_next_aa
);
857 SETCALC(LFGauss_next_a
);
858 printf("LFGauss_next_a\n");
861 SETCALC(LFGauss_next_k
);
865 //LFGauss_next_k(unit, 1);
869 //////////////////////////////////////////////////////////////////////////////////////////////////
871 void Impulse_next_a(Impulse
*unit
, int inNumSamples
)
873 float *out
= ZOUT(0);
874 float *freq
= ZIN(0);
876 float freqmul
= unit
->mFreqMul
;
877 double phase
= unit
->mPhase
;
886 phase
+= ZXP(freq
) * freqmul
;
890 unit
->mPhase
= phase
;
893 /* phase mod - jrh 03 */
895 void Impulse_next_ak(Impulse
*unit
, int inNumSamples
)
897 float *out
= ZOUT(0);
898 float *freq
= ZIN(0);
899 double phaseOffset
= ZIN0(1);
901 float freqmul
= unit
->mFreqMul
;
902 double phase
= unit
->mPhase
;
903 double prev_phaseOffset
= unit
->mPhaseOffset
;
904 double phaseSlope
= CALCSLOPE(phaseOffset
, prev_phaseOffset
);
905 phase
+= prev_phaseOffset
;
916 phase
+= ZXP(freq
) * freqmul
;
920 unit
->mPhase
= phase
- phaseOffset
;
921 unit
->mPhaseOffset
= phaseOffset
;
924 void Impulse_next_kk(Impulse
*unit
, int inNumSamples
)
926 float *out
= ZOUT(0);
927 float freq
= ZIN0(0) * unit
->mFreqMul
;
928 double phaseOffset
= ZIN0(1);
930 double phase
= unit
->mPhase
;
931 double prev_phaseOffset
= unit
->mPhaseOffset
;
932 double phaseSlope
= CALCSLOPE(phaseOffset
, prev_phaseOffset
);
933 phase
+= prev_phaseOffset
;
948 unit
->mPhase
= phase
- phaseOffset
;
949 unit
->mPhaseOffset
= phaseOffset
;
953 void Impulse_next_k(Impulse
*unit
, int inNumSamples
)
955 float *out
= ZOUT(0);
956 float freq
= ZIN0(0) * unit
->mFreqMul
;
958 double phase
= unit
->mPhase
;
971 unit
->mPhase
= phase
;
974 void Impulse_Ctor(Impulse
* unit
)
977 unit
->mPhase
= ZIN0(1);
979 if (INRATE(0) == calc_FullRate
) {
980 if(INRATE(1) != calc_ScalarRate
) {
981 SETCALC(Impulse_next_ak
);
984 SETCALC(Impulse_next_a
);
987 if(INRATE(1) != calc_ScalarRate
) {
988 SETCALC(Impulse_next_kk
);
991 SETCALC(Impulse_next_k
);
996 unit
->mPhaseOffset
= 0.f
;
997 unit
->mFreqMul
= unit
->mRate
->mSampleDur
;
998 if (unit
->mPhase
== 0.f
) unit
->mPhase
= 1.f
;
1003 //////////////////////////////////////////////////////////////////////////////////////////////////
1005 void VarSaw_next_a(VarSaw
*unit
, int inNumSamples
)
1007 float *out
= ZOUT(0);
1008 float *freq
= ZIN(0);
1009 float nextDuty
= ZIN0(2);
1010 float duty
= unit
->mDuty
;
1011 float invduty
= unit
->mInvDuty
;
1012 float inv1duty
= unit
->mInv1Duty
;
1014 float freqmul
= unit
->mFreqMul
;
1015 double phase
= unit
->mPhase
;
1020 duty
= unit
->mDuty
= sc_clip(nextDuty
, 0.001, 0.999);
1021 invduty
= unit
->mInvDuty
= 2.f
/ duty
;
1022 inv1duty
= unit
->mInv1Duty
= 2.f
/ (1.f
- duty
);
1024 float z
= phase
< duty
? phase
* invduty
: (1.f
- phase
) * inv1duty
;
1025 phase
+= ZXP(freq
) * freqmul
;
1029 unit
->mPhase
= phase
;
1032 void VarSaw_next_k(VarSaw
*unit
, int inNumSamples
)
1034 float *out
= ZOUT(0);
1035 float freq
= ZIN0(0) * unit
->mFreqMul
;
1036 float nextDuty
= ZIN0(2);
1037 float duty
= unit
->mDuty
;
1038 float invduty
= unit
->mInvDuty
;
1039 float inv1duty
= unit
->mInv1Duty
;
1041 double phase
= unit
->mPhase
;
1046 duty
= unit
->mDuty
= sc_clip(nextDuty
, 0.001, 0.999);
1047 invduty
= unit
->mInvDuty
= 2.f
/ duty
;
1048 inv1duty
= unit
->mInv1Duty
= 2.f
/ (1.f
- duty
);
1050 float z
= phase
< duty
? phase
* invduty
: (1.f
- phase
) * inv1duty
;
1055 unit
->mPhase
= phase
;
1058 void VarSaw_Ctor(VarSaw
* unit
)
1060 if (INRATE(0) == calc_FullRate
) {
1061 SETCALC(VarSaw_next_a
);
1063 SETCALC(VarSaw_next_k
);
1066 unit
->mFreqMul
= unit
->mRate
->mSampleDur
;
1067 unit
->mPhase
= ZIN0(1);
1068 float duty
= ZIN0(2);
1069 duty
= unit
->mDuty
= sc_clip(duty
, 0.001, 0.999);
1070 unit
->mInvDuty
= 2.f
/ duty
;
1071 unit
->mInv1Duty
= 2.f
/ (1.f
- duty
);
1076 //////////////////////////////////////////////////////////////////////////////////////////////////
1078 void SyncSaw_next_aa(SyncSaw
*unit
, int inNumSamples
)
1080 float freqmul
= unit
->mFreqMul
;
1081 float *out
= ZOUT(0);
1082 float *freq1
= ZIN(0);
1083 float *freq2
= ZIN(1);
1085 double phase1
= unit
->mPhase1
;
1086 double phase2
= unit
->mPhase2
;
1089 float freq1x
= ZXP(freq1
) * freqmul
;
1090 float freq2x
= ZXP(freq2
) * freqmul
;
1093 if (phase2
>= 1.f
) phase2
-= 2.f
;
1095 if (phase1
>= 1.f
) {
1097 phase2
= (phase1
+ 1.f
) * freq2x
/ freq1x
- 1.f
;
1102 unit
->mPhase1
= phase1
;
1103 unit
->mPhase2
= phase2
;
1106 void SyncSaw_next_ak(SyncSaw
*unit
, int inNumSamples
)
1108 float freqmul
= unit
->mFreqMul
;
1109 float *out
= ZOUT(0);
1110 float *freq1
= ZIN(0);
1111 float freq2x
= ZIN0(1) * freqmul
;
1113 double phase1
= unit
->mPhase1
;
1114 double phase2
= unit
->mPhase2
;
1117 float freq1x
= ZXP(freq1
) * freqmul
;
1120 if (phase2
>= 1.f
) phase2
-= 2.f
;
1122 if (phase1
>= 1.f
) {
1124 phase2
= (phase1
+ 1.f
) * freq2x
/ freq1x
- 1.f
;
1129 unit
->mPhase1
= phase1
;
1130 unit
->mPhase2
= phase2
;
1133 void SyncSaw_next_ka(SyncSaw
*unit
, int inNumSamples
)
1135 float freqmul
= unit
->mFreqMul
;
1136 float *out
= ZOUT(0);
1137 float freq1x
= ZIN0(0) * freqmul
;
1138 float *freq2
= ZIN(1);
1140 double phase1
= unit
->mPhase1
;
1141 double phase2
= unit
->mPhase2
;
1144 float freq2x
= ZXP(freq2
) * freqmul
;
1147 if (phase2
>= 1.f
) phase2
-= 2.f
;
1149 if (phase1
>= 1.f
) {
1151 phase2
= (phase1
+ 1.f
) * freq2x
/ freq1x
- 1.f
;
1156 unit
->mPhase1
= phase1
;
1157 unit
->mPhase2
= phase2
;
1160 void SyncSaw_next_kk(SyncSaw
*unit
, int inNumSamples
)
1162 float *out
= ZOUT(0);
1163 float freq1x
= ZIN0(0) * unit
->mFreqMul
;
1164 float freq2x
= ZIN0(1) * unit
->mFreqMul
;
1165 double phase1
= unit
->mPhase1
;
1166 double phase2
= unit
->mPhase2
;
1171 if (phase2
>= 1.f
) phase2
-= 2.f
;
1173 if (phase1
>= 1.f
) {
1175 phase2
= (phase1
+ 1.f
) * freq2x
/ freq1x
- 1.f
;
1180 unit
->mPhase1
= phase1
;
1181 unit
->mPhase2
= phase2
;
1184 void SyncSaw_Ctor(SyncSaw
* unit
)
1186 if (INRATE(0) == calc_FullRate
) {
1187 if (INRATE(1) == calc_FullRate
) {
1188 SETCALC(SyncSaw_next_aa
);
1190 SETCALC(SyncSaw_next_ak
);
1193 if (INRATE(1) == calc_FullRate
) {
1194 SETCALC(SyncSaw_next_ka
);
1196 SETCALC(SyncSaw_next_kk
);
1199 unit
->mFreqMul
= 2.0 * unit
->mRate
->mSampleDur
;
1203 SyncSaw_next_kk(unit
, 1);
1206 //////////////////////////////////////////////////////////////////////////////////////////////////
1211 ControlRateInput
<0> mLevel
;
1216 if (inRate(0) == calc_ScalarRate
)
1217 set_unrolled_calc_function
<K2A
, &K2A::next_i
<unrolled_64
>, &K2A::next_i
<unrolled
>, &K2A::next_i
<scalar
> >();
1219 set_unrolled_calc_function
<K2A
, &K2A::next_k
<unrolled_64
>, &K2A::next_k
<unrolled
>, &K2A::next_k
<scalar
> >();
1223 void next_k(int inNumSamples
)
1225 if (mLevel
.changed(this))
1226 slope_vec
<type
>(out(0), mLevel
.slope(this), inNumSamples
);
1228 next_i
<type
>(inNumSamples
);
1232 void next_i(int inNumSamples
)
1234 set_vec
<type
>(out(0), mLevel
, inNumSamples
);
1240 //////////////////////////////////////////////////////////////////////////////////////////////////
1242 void A2K_next(A2K
*unit
, int inNumSamples
)
1244 ZOUT0(0) = ZIN0(0); // return first sample in block
1247 void A2K_Ctor(A2K
* unit
)
1253 //////////////////////////////////////////////////////////////////////////////////////////////////
1255 void T2K_next(T2K
*unit
, int inNumSamples
)
1257 float out
= 0.f
, val
;
1259 int n
= unit
->mWorld
->mBufLength
;
1262 if(val
>out
) out
=val
;
1267 void T2K_Ctor(T2K
* unit
)
1273 //////////////////////////////////////////////////////////////////////////////////////////////////
1275 static inline void T2A_write_trigger(T2A
* unit
, float level
)
1277 float *out
= OUT(0);
1278 int offset
= (int) IN0(1);
1279 out
[offset
] = level
;
1282 void T2A_next(T2A
*unit
, int inNumSamples
)
1284 float level
= IN0(0);
1286 ZClear(inNumSamples
, ZOUT(0));
1287 if((unit
->mLevel
<= 0.f
&& level
> 0.f
))
1288 T2A_write_trigger(unit
, level
);
1290 unit
->mLevel
= level
;
1294 FLATTEN
void T2A_next_nova(T2A
*unit
, int inNumSamples
)
1296 float level
= IN0(0);
1298 nova::zerovec_simd(OUT(0), inNumSamples
);
1299 if((unit
->mLevel
<= 0.f
&& level
> 0.f
))
1300 T2A_write_trigger(unit
, level
);
1302 unit
->mLevel
= level
;
1305 FLATTEN
void T2A_next_nova_64(T2A
*unit
, int inNumSamples
)
1307 float level
= IN0(0);
1309 nova::zerovec_simd
<64>(OUT(0));
1310 if((unit
->mLevel
<= 0.f
&& level
> 0.f
))
1311 T2A_write_trigger(unit
, level
);
1313 unit
->mLevel
= level
;
1318 void T2A_Ctor(T2A
* unit
)
1321 if (BUFLENGTH
== 64)
1322 SETCALC(T2A_next_nova_64
);
1323 else if (!(BUFLENGTH
& 15))
1324 SETCALC(T2A_next_nova
);
1332 //////////////////////////////////////////////////////////////////////////////////////////////////
1342 set_unrolled_calc_function
<DC
, &DC::next_i
<unrolled_64
, true>,
1343 &DC::next_i
<unrolled
, true>, &DC::next_i
<scalar
, true> >();
1345 set_unrolled_calc_function
<DC
, &DC::next_i
<unrolled_64
, false>,
1346 &DC::next_i
<unrolled
, false>, &DC::next_i
<scalar
, false> >();
1349 template <int type
, bool isZero
>
1350 void next_i(int inNumSamples
)
1353 zero_vec
<type
>(out(0), inNumSamples
);
1355 set_vec
<type
>(out(0), value
, inNumSamples
);
1361 //////////////////////////////////////////////////////////////////////////////////////////////////
1363 static inline void Line_next_loop(Line
* unit
, int & counter
, int remain
, double & level
)
1365 float *out
= ZOUT(0);
1366 double slope
= unit
->mSlope
;
1372 float endlevel
= unit
->mEndLevel
;
1374 ZXP(out
) = endlevel
;
1377 int nsmps
= sc_min(remain
, counter
);
1386 int doneAction
= (int)ZIN0(3);
1387 DoneAction(doneAction
, unit
);
1395 void Line_next(Line
*unit
, int inNumSamples
)
1397 double level
= unit
->mLevel
;
1398 int counter
= unit
->mCounter
;
1399 Line_next_loop(unit
, counter
, inNumSamples
, level
);
1400 unit
->mCounter
= counter
;
1401 unit
->mLevel
= level
;
1405 FLATTEN
void Line_next_nova(Line
*unit
, int inNumSamples
)
1407 double level
= unit
->mLevel
;
1408 int counter
= unit
->mCounter
;
1412 nova::setvec_simd(OUT(0), unit
->mEndLevel
, inNumSamples
);
1416 if (counter
> inNumSamples
)
1418 double slope
= unit
->mSlope
;
1419 nova::set_slope_vec_simd(OUT(0), (float)level
, (float)slope
, inNumSamples
);
1420 unit
->mLevel
= level
+ inNumSamples
* slope
;
1421 unit
->mCounter
= counter
- inNumSamples
;
1424 Line_next_loop(unit
, counter
, inNumSamples
, level
);
1425 unit
->mCounter
= counter
;
1426 unit
->mLevel
= level
;
1429 FLATTEN
void Line_next_nova_64(Line
*unit
, int inNumSamples
)
1431 double level
= unit
->mLevel
;
1432 int counter
= unit
->mCounter
;
1436 nova::setvec_simd
<64>(OUT(0), unit
->mEndLevel
);
1440 if (counter
> inNumSamples
)
1442 double slope
= unit
->mSlope
;
1443 nova::set_slope_vec_simd(OUT(0), (float)level
, (float)slope
, 64);
1444 unit
->mLevel
= level
+ inNumSamples
* slope
;
1445 unit
->mCounter
= counter
- inNumSamples
;
1449 Line_next_loop(unit
, counter
, inNumSamples
, level
);
1450 unit
->mCounter
= counter
;
1451 unit
->mLevel
= level
;
1456 void Line_Ctor(Line
* unit
)
1459 if (BUFLENGTH
== 64)
1460 SETCALC(Line_next_nova
);
1461 else if (!(BUFLENGTH
& 15))
1462 SETCALC(Line_next_nova
);
1466 double start
= ZIN0(0);
1467 double end
= ZIN0(1);
1468 double dur
= ZIN0(2);
1470 int counter
= (int)(dur
* unit
->mRate
->mSampleRate
+ .5f
);
1471 unit
->mCounter
= sc_max(1, counter
);
1476 unit
->mLevel
= start
;
1477 unit
->mSlope
= (end
- start
) / unit
->mCounter
;
1478 unit
->mLevel
+= unit
->mSlope
;
1480 unit
->mEndLevel
= end
;
1481 ZOUT0(0) = unit
->mLevel
;
1484 //////////////////////////////////////////////////////////////////////////////////////////////////
1486 static inline void Xline_next_loop(XLine
* unit
, int & counter
, int remain
, double & level
)
1488 float *out
= ZOUT(0);
1489 double grow
= unit
->mGrowth
;
1499 int nsmps
= sc_min(remain
, counter
);
1507 level
= unit
->mEndLevel
;
1509 int doneAction
= (int)ZIN0(3);
1510 DoneAction(doneAction
, unit
);
1516 void XLine_next(XLine
*unit
, int inNumSamples
)
1518 double level
= unit
->mLevel
;
1519 int counter
= unit
->mCounter
;
1521 Xline_next_loop(unit
, counter
, inNumSamples
, level
);
1522 unit
->mCounter
= counter
;
1523 unit
->mLevel
= level
;
1527 FLATTEN
void XLine_next_nova(XLine
*unit
, int inNumSamples
)
1529 double level
= unit
->mLevel
;
1530 int counter
= unit
->mCounter
;
1534 nova::setvec_simd(OUT(0), (float)level
, inNumSamples
);
1537 if (counter
> inNumSamples
)
1539 double grow
= unit
->mGrowth
;
1540 nova::set_exp_vec_simd(OUT(0), (float)level
, (float)grow
, inNumSamples
);
1541 level
*= sc_powi(grow
, inNumSamples
);
1542 counter
-= inNumSamples
;
1544 Xline_next_loop(unit
, counter
, inNumSamples
, level
);
1545 unit
->mCounter
= counter
;
1546 unit
->mLevel
= level
;
1549 FLATTEN
void XLine_next_nova_64(XLine
*unit
, int inNumSamples
)
1551 double level
= unit
->mLevel
;
1552 int counter
= unit
->mCounter
;
1556 nova::setvec_simd
<64>(OUT(0), (float)level
);
1561 double grow
= unit
->mGrowth
;
1562 nova::set_exp_vec_simd(OUT(0), (float)level
, (float)grow
, 64);
1563 level
*= sc_powi(grow
, inNumSamples
);
1564 counter
-= inNumSamples
;
1566 Xline_next_loop(unit
, counter
, inNumSamples
, level
);
1567 unit
->mCounter
= counter
;
1568 unit
->mLevel
= level
;
1573 void XLine_Ctor(XLine
* unit
)
1576 if (BUFLENGTH
== 64)
1577 SETCALC(XLine_next_nova_64
);
1578 else if (!(BUFLENGTH
& 15))
1579 SETCALC(XLine_next_nova
);
1583 SETCALC(XLine_next
);
1584 double start
= ZIN0(0);
1585 double end
= ZIN0(1);
1586 double dur
= ZIN0(2);
1588 int counter
= (int)(dur
* unit
->mRate
->mSampleRate
+ .5f
);
1590 unit
->mEndLevel
= end
;
1599 unit
->mCounter
= counter
;
1600 unit
->mGrowth
= pow(end
/ start
, 1.0 / counter
);
1601 unit
->mLevel
= start
* unit
->mGrowth
;
1605 //////////////////////////////////////////////////////////////////////////////////////////////////
1607 void Wrap_next(Wrap* unit, int inNumSamples)
1609 float *out = ZOUT(0);
1611 float lo = unit->m_lo;
1612 float hi = unit->m_hi;
1613 float range = unit->m_range;
1616 ZXP(out) = sc_wrap(ZXP(in), lo, hi, range);
1620 void Wrap_Ctor(Wrap* unit)
1624 unit->m_lo = ZIN0(1);
1625 unit->m_hi = ZIN0(2);
1627 if (unit->m_lo > unit->m_hi) {
1628 float temp = unit->m_lo;
1629 unit->m_lo = unit->m_hi;
1632 unit->m_range = unit->m_hi - unit->m_lo;
1639 void Wrap_next_kk(Wrap
* unit
, int inNumSamples
)
1641 float *out
= ZOUT(0);
1643 float next_lo
= ZIN0(1);
1644 float next_hi
= ZIN0(2);
1645 float lo
= unit
->m_lo
;
1646 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1647 float hi
= unit
->m_hi
;
1648 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1650 float range
= hi
- lo
;
1651 ZXP(out
) = sc_wrap(ZXP(in
), lo
, hi
, range
);
1659 void Wrap_next_ka(Wrap
* unit
, int inNumSamples
)
1661 float *out
= ZOUT(0);
1663 float next_lo
= ZIN0(1);
1665 float lo
= unit
->m_lo
;
1666 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1668 float curhi
= ZXP(hi
);
1669 ZXP(out
) = sc_wrap(ZXP(in
), lo
, curhi
, curhi
- lo
);
1675 void Wrap_next_ak(Wrap
* unit
, int inNumSamples
)
1677 float *out
= ZOUT(0);
1680 float next_hi
= ZIN0(2);
1681 float hi
= unit
->m_hi
;
1682 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1685 float curlo
= ZXP(lo
);
1686 ZXP(out
) = sc_wrap(ZXP(in
), curlo
, hi
, hi
- curlo
);
1692 void Wrap_next_aa(Wrap
* unit
, int inNumSamples
)
1694 float *out
= ZOUT(0);
1700 float curhi
= ZXP(hi
);
1701 float curlo
= ZXP(lo
);
1702 ZXP(out
) = sc_wrap(ZXP(in
), curlo
, curhi
, curhi
- curlo
);
1706 void Wrap_Ctor(Wrap
* unit
)
1708 if(BUFLENGTH
== 1) {
1709 // _aa? Well, yes - that calc func doesn't interpolate
1710 // and interpolation is not needed for kr (1 sample/block)
1711 SETCALC(Wrap_next_aa
);
1713 if(INRATE(1) == calc_FullRate
) {
1714 if(INRATE(2) == calc_FullRate
)
1715 SETCALC(Wrap_next_aa
);
1717 SETCALC(Wrap_next_ak
);
1719 if(INRATE(2) == calc_FullRate
)
1720 SETCALC(Wrap_next_ka
);
1722 SETCALC(Wrap_next_kk
);
1726 unit
->m_lo
= ZIN0(1);
1727 unit
->m_hi
= ZIN0(2);
1729 Wrap_next_kk(unit
, 1);
1733 //////////////////////////////////////////////////////////////////////////////////////////////////
1735 void Fold_next(Fold* unit, int inNumSamples)
1737 float *out = ZOUT(0);
1739 float lo = unit->m_lo;
1740 float hi = unit->m_hi;
1741 float range = unit->m_range;
1742 float range2 = unit->m_range2;
1745 ZXP(out) = sc_fold(ZXP(in), lo, hi, range, range2);
1749 void Fold_Ctor(Fold* unit)
1753 unit->m_lo = ZIN0(1);
1754 unit->m_hi = ZIN0(2);
1756 if (unit->m_lo > unit->m_hi) {
1757 float temp = unit->m_lo;
1758 unit->m_lo = unit->m_hi;
1761 unit->m_range = unit->m_hi - unit->m_lo;
1762 unit->m_range2 = 2.f * unit->m_range;
1767 void Fold_next_kk(Fold
* unit
, int inNumSamples
)
1769 float *out
= ZOUT(0);
1771 float next_lo
= ZIN0(1);
1772 float next_hi
= ZIN0(2);
1773 float lo
= unit
->m_lo
;
1774 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1775 float hi
= unit
->m_hi
;
1776 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1779 float range
= hi
- lo
;
1780 float range2
= range
* 2.f
;
1781 ZXP(out
) = sc_fold(ZXP(in
), lo
, hi
, range
, range2
);
1790 void Fold_next_ka(Fold
* unit
, int inNumSamples
)
1792 float *out
= ZOUT(0);
1794 float next_lo
= ZIN0(1);
1796 float lo
= unit
->m_lo
;
1797 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1800 float curhi
= ZXP(hi
);
1801 float range
= curhi
- lo
;
1802 float range2
= range
* 2.f
;
1803 ZXP(out
) = sc_fold(ZXP(in
), lo
, curhi
, range
, range2
);
1809 void Fold_next_ak(Fold
* unit
, int inNumSamples
)
1811 float *out
= ZOUT(0);
1814 float next_hi
= ZIN0(2);
1815 float hi
= unit
->m_hi
;
1816 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1819 float curlo
= ZXP(lo
);
1820 float range
= hi
- curlo
;
1821 float range2
= range
* 2.f
;
1822 ZXP(out
) = sc_fold(ZXP(in
), curlo
, hi
, range
, range2
);
1828 void Fold_next_aa(Fold
* unit
, int inNumSamples
)
1830 float *out
= ZOUT(0);
1836 float curhi
= ZXP(hi
);
1837 float curlo
= ZXP(lo
);
1838 float range
= curhi
- curlo
;
1839 float range2
= range
* 2.0;
1840 ZXP(out
) = sc_fold(ZXP(in
), curlo
, curhi
, range
, range2
);
1844 void Fold_Ctor(Fold
* unit
)
1846 if(BUFLENGTH
== 1) {
1847 // _aa? Well, yes - that calc func doesn't interpolate
1848 // and interpolation is not needed for kr (1 sample/block)
1849 SETCALC(Fold_next_aa
);
1851 if(INRATE(1) == calc_FullRate
) {
1852 if(INRATE(2) == calc_FullRate
)
1853 SETCALC(Fold_next_aa
);
1855 SETCALC(Fold_next_ak
);
1857 if(INRATE(2) == calc_FullRate
)
1858 SETCALC(Fold_next_ka
);
1860 SETCALC(Fold_next_kk
);
1864 unit
->m_lo
= ZIN0(1);
1865 unit
->m_hi
= ZIN0(2);
1867 Fold_next_kk(unit
, 1);
1870 //////////////////////////////////////////////////////////////////////////////////////////////////
1872 void Clip_next_ii(Clip
* unit
, int inNumSamples
)
1874 float *out
= ZOUT(0);
1876 float lo
= unit
->m_lo
;
1877 float hi
= unit
->m_hi
;
1880 ZXP(out
) = sc_clip(ZXP(in
), lo
, hi
);
1884 void Clip_next_kk(Clip
* unit
, int inNumSamples
)
1886 float next_lo
= ZIN0(1);
1887 float next_hi
= ZIN0(2);
1888 float lo
= unit
->m_lo
;
1889 float hi
= unit
->m_hi
;
1891 if (lo
== next_lo
&& hi
== next_hi
) {
1892 Clip_next_ii(unit
, inNumSamples
);
1896 float *out
= ZOUT(0);
1898 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1899 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1902 ZXP(out
) = sc_clip(ZXP(in
), lo
, hi
);
1910 void Clip_next_ka(Clip
* unit
, int inNumSamples
)
1912 float *out
= ZOUT(0);
1914 float next_lo
= ZIN0(1);
1916 float lo
= unit
->m_lo
;
1917 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1920 ZXP(out
) = sc_clip(ZXP(in
), lo
, ZXP(hi
));
1926 void Clip_next_ak(Clip
* unit
, int inNumSamples
)
1928 float *out
= ZOUT(0);
1931 float next_hi
= ZIN0(2);
1932 float hi
= unit
->m_hi
;
1933 float hi_slope
= CALCSLOPE(next_hi
, hi
);
1936 ZXP(out
) = sc_clip(ZXP(in
), ZXP(lo
), hi
);
1942 void Clip_next_aa(Clip
* unit
, int inNumSamples
)
1944 float *out
= ZOUT(0);
1950 ZXP(out
) = sc_clip(ZXP(in
), ZXP(lo
), ZXP(hi
));
1954 void Clip_next_k(Clip
* unit
, int inNumSamples
)
1956 float *out
= ZOUT(0);
1961 ZXP(out
) = sc_clip(ZXP(in
), lo
, hi
);
1965 void Clip_next_nova_ii(Clip
* unit
, int inNumSamples
)
1967 float lo
= unit
->m_lo
;
1968 float hi
= unit
->m_hi
;
1970 nova::clip_vec_simd(OUT(0), IN(0), lo
, hi
, inNumSamples
);
1973 void Clip_next_nova_ki(Clip
* unit
, int inNumSamples
)
1975 float next_lo
= ZIN0(1);
1976 float lo
= unit
->m_lo
;
1977 float hi
= unit
->m_hi
;
1979 if (lo
== next_lo
) {
1980 Clip_next_nova_ii(unit
, inNumSamples
);
1984 float lo_slope
= CALCSLOPE(next_lo
, lo
);
1985 nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo
, lo_slope
), hi
, inNumSamples
);
1988 void Clip_next_nova_ik(Clip
* unit
, int inNumSamples
)
1990 float next_hi
= ZIN0(2);
1991 float lo
= unit
->m_lo
;
1992 float hi
= unit
->m_hi
;
1994 if (hi
== next_hi
) {
1995 Clip_next_nova_ii(unit
, inNumSamples
);
1999 float hi_slope
= CALCSLOPE(next_hi
, hi
);
2000 nova::clip_vec_simd(OUT(0), IN(0), lo
, slope_argument(hi
, hi_slope
), inNumSamples
);
2003 void Clip_next_nova_kk(Clip
* unit
, int inNumSamples
)
2005 float next_lo
= ZIN0(1);
2006 float next_hi
= ZIN0(2);
2007 float lo
= unit
->m_lo
;
2008 float hi
= unit
->m_hi
;
2010 if (lo
== next_lo
&& hi
== next_hi
) {
2011 Clip_next_nova_ii(unit
, inNumSamples
);
2015 if (lo
== next_lo
) {
2016 Clip_next_nova_ik(unit
, inNumSamples
);
2020 if (hi
== next_hi
) {
2021 Clip_next_nova_ki(unit
, inNumSamples
);
2025 float lo_slope
= CALCSLOPE(next_lo
, lo
);
2026 float hi_slope
= CALCSLOPE(next_hi
, hi
);
2028 nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo
, lo_slope
), slope_argument(hi
, hi_slope
), inNumSamples
);
2031 void Clip_next_nova_ai(Clip
* unit
, int inNumSamples
)
2033 float hi
= unit
->m_hi
;
2034 nova::clip_vec_simd(OUT(0), IN(0), IN(1), hi
, inNumSamples
);
2037 void Clip_next_nova_ak(Clip
* unit
, int inNumSamples
)
2039 float next_hi
= ZIN0(2);
2040 float hi
= unit
->m_hi
;
2042 if (hi
== next_hi
) {
2043 Clip_next_nova_ai(unit
, inNumSamples
);
2047 float hi_slope
= CALCSLOPE(next_hi
, hi
);
2049 nova::clip_vec_simd(OUT(0), IN(0), IN(1), slope_argument(hi
, hi_slope
), inNumSamples
);
2052 void Clip_next_nova_ia(Clip
* unit
, int inNumSamples
)
2054 float lo
= unit
->m_lo
;
2055 nova::clip_vec_simd(OUT(0), IN(0), lo
, IN(2), inNumSamples
);
2058 void Clip_next_nova_ka(Clip
* unit
, int inNumSamples
)
2060 float next_lo
= ZIN0(1);
2061 float lo
= unit
->m_lo
;
2063 if (lo
== next_lo
) {
2064 Clip_next_nova_ia(unit
, inNumSamples
);
2068 float lo_slope
= CALCSLOPE(next_lo
, lo
);
2069 nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo
, lo_slope
), IN(2), inNumSamples
);
2073 void Clip_next_nova_aa(Clip
* unit
, int inNumSamples
)
2075 nova::clip_vec_simd(OUT(0), IN(0), IN(1), IN(2), inNumSamples
);
2080 typedef void (*ClipCalcFunc
)(Clip
*, int);
2082 static ClipCalcFunc
Clip_SelectCalc(Clip
* unit
)
2087 int loRate
= INRATE(1);
2088 int hiRate
= INRATE(2);
2091 if (!(BUFLENGTH
& 15)) {
2097 return Clip_next_nova_aa
;
2099 return Clip_next_nova_ak
;
2100 case calc_ScalarRate
:
2101 return Clip_next_nova_ai
;
2108 return Clip_next_nova_ka
;
2110 return Clip_next_nova_kk
;
2111 case calc_ScalarRate
:
2112 return Clip_next_nova_ki
;
2116 case calc_ScalarRate
:
2119 return Clip_next_nova_ia
;
2121 return Clip_next_nova_ik
;
2122 case calc_ScalarRate
:
2123 return Clip_next_nova_ii
;
2130 if (loRate
== calc_FullRate
&& hiRate
== calc_FullRate
)
2131 return Clip_next_aa
;
2133 if (loRate
== calc_ScalarRate
&& hiRate
== calc_ScalarRate
)
2134 return Clip_next_ii
;
2136 if (loRate
== calc_FullRate
&& hiRate
!= calc_FullRate
)
2137 return Clip_next_ak
;
2139 if (loRate
!= calc_FullRate
&& hiRate
== calc_FullRate
)
2140 return Clip_next_ak
;
2142 return Clip_next_kk
;
2145 void Clip_Ctor(Clip
* unit
)
2147 ClipCalcFunc fn
= Clip_SelectCalc(unit
);
2148 unit
->mCalcFunc
= (UnitCalcFunc
)fn
;
2150 unit
->m_lo
= ZIN0(1);
2151 unit
->m_hi
= ZIN0(2);
2153 Clip_next_ii(unit
, 1);
2156 //////////////////////////////////////////////////////////////////////////////////////////////////
2158 void Unwrap_next(Unwrap
* unit
, int inNumSamples
)
2160 float *out
= ZOUT(0);
2162 float range
= unit
->m_range
;
2163 float half
= unit
->m_half
;
2164 float prev
= unit
->m_prev
;
2165 float offset
= unit
->m_offset
;
2168 float zin
= ZXP(in
);
2169 float diff
= zin
- prev
;
2170 if (fabs(diff
) > half
) {
2176 ZXP(out
) = zin
+ offset
;
2179 unit
->m_prev
= prev
;
2180 unit
->m_offset
= offset
;
2183 void Unwrap_Ctor(Unwrap
* unit
)
2185 SETCALC(Unwrap_next
);
2195 unit
->m_range
= fabs(hi
- lo
);
2196 unit
->m_half
= unit
->m_range
* 0.5f
;
2198 if (in
< lo
|| in
>= hi
) unit
->m_offset
= floor((lo
- in
)/unit
->m_range
) * unit
->m_range
;
2199 else unit
->m_offset
= 0.f
;
2201 Unwrap_next(unit
, 1);
2204 //////////////////////////////////////////////////////////////////////////////////////////////////
2206 void AmpComp_next(AmpComp
*unit
, int inNumSamples
)
2208 float *out
= ZOUT(0);
2209 float *freq
= ZIN(0);
2210 float rootmul
= unit
->m_rootmul
;
2211 float xb
= unit
->m_exponent
;
2214 float xa
= ZXP(freq
);
2215 ZXP(out
) = xa
>= 0.f
? pow(xa
, xb
) * rootmul
: -pow(-xa
, xb
) * rootmul
;
2219 void AmpComp_next_kk(AmpComp
*unit
, int inNumSamples
)
2221 float *out
= ZOUT(0);
2222 float *freq
= ZIN(0);
2223 float root
= ZIN0(1);
2227 float xa
= root
/ ZXP(freq
);
2228 ZXP(out
) = xa
>= 0.f
? pow(xa
, xb
) : -pow(-xa
, xb
);
2232 void AmpComp_Ctor(AmpComp
* unit
)
2234 if(INRATE(1) != calc_ScalarRate
|| INRATE(2) != calc_ScalarRate
) {
2235 SETCALC(AmpComp_next_kk
);
2237 float exp
= ZIN0(2);
2238 unit
->m_rootmul
= pow(ZIN0(1), exp
);
2239 unit
->m_exponent
= -1.f
* exp
;
2240 SETCALC(AmpComp_next
);
2242 AmpComp_next(unit
, 1);
2246 //////////////////////////////////////////////////////////////////////////////////////////////////
2248 const double AMPCOMP_K
= 3.5041384 * 10e15
;
2249 const double AMPCOMP_C1
= 20.598997 * 20.598997;
2250 const double AMPCOMP_C2
= 107.65265 * 107.65265;
2251 const double AMPCOMP_C3
= 737.86223 * 737.86223;
2252 const double AMPCOMP_C4
= 12194.217 * 12194.217;
2253 const double AMPCOMP_MINLEVEL
= -0.1575371167435;
2255 double AmpCompA_calcLevel(double freq
)
2257 double r
= freq
* freq
;
2258 double level
= (AMPCOMP_K
* r
* r
* r
* r
);
2259 double n1
= AMPCOMP_C1
+ r
;
2260 double n2
= AMPCOMP_C4
+ r
;
2267 level
= 1. - sqrt(level
);
2271 void AmpCompA_next(AmpCompA
*unit
, int inNumSamples
)
2273 float *out
= ZOUT(0);
2274 float *freq
= ZIN(0);
2276 double scale
= unit
->m_scale
;
2277 double offset
= unit
->m_offset
;
2280 ZXP(out
) = AmpCompA_calcLevel(ZXP(freq
)) * scale
+ offset
;
2284 void AmpCompA_Ctor(AmpCompA
* unit
)
2286 double rootFreq
= ZIN0(1);
2287 double rootLevel
= AmpCompA_calcLevel(rootFreq
);
2288 float minLevel
= ZIN0(2);
2289 unit
->m_scale
= (ZIN0(3) - minLevel
) / (rootLevel
- AMPCOMP_MINLEVEL
);
2290 unit
->m_offset
= minLevel
- unit
->m_scale
* AMPCOMP_MINLEVEL
;
2292 SETCALC(AmpCompA_next
);
2293 AmpCompA_next(unit
, 1);
2296 //////////////////////////////////////////////////////////////////////////////////////////////////
2298 void InRange_next(InRange
*unit
, int inNumSamples
)
2300 float *out
= ZOUT(0);
2306 float zin
= ZXP(in
);
2307 ZXP(out
) = zin
>= lo
&& zin
<= hi
? 1.f
: 0.f
;
2311 void InRange_Ctor(InRange
* unit
)
2313 SETCALC(InRange_next
);
2314 InRange_next(unit
, 1);
2317 //////////////////////////////////////////////////////////////////////////////////////////////////
2319 void InRect_next(InRect
* unit
, int inNumSamples
)
2321 float *out
= ZOUT(0);
2322 float *inx
= ZIN(0);
2323 float *iny
= ZIN(1);
2324 float left
= ZIN0(2);
2325 float top
= ZIN0(3);
2326 float right
= ZIN0(4);
2327 float bottom
= ZIN0(5);
2332 ZXP(out
) = x
>= left
&& x
<= right
&& y
>= top
&& y
<= bottom
? 1.f
: 0.f
;
2336 void InRect_Ctor(InRect
* unit
)
2338 SETCALC(InRect_next
);
2339 InRect_next(unit
, 1);
2342 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2345 void LinExp_next(LinExp
*unit
, int inNumSamples
)
2347 float *out
= ZOUT(0);
2349 float dstlo
= unit
->m_dstlo
;
2351 float dstratio
= unit
->m_dstratio
;
2352 float rsrcrange
= unit
->m_rsrcrange
;
2353 float rrminuslo
= unit
->m_rrminuslo
;
2356 ZXP(out
) = dstlo
* pow(dstratio
, ZXP(in
) * rsrcrange
+ rrminuslo
);
2361 static inline void LinExp_next_nova_loop(float * out
, const float * in
, int inNumSamples
,
2362 nova::vec
<float> dstlo
, nova::vec
<float> dstratio
,
2363 nova::vec
<float> rsrcrange
, nova::vec
<float> rrminuslo
)
2365 const int vecSize
= nova::vec
<float>::size
;
2366 int unroll
= inNumSamples
/ (2*vecSize
);
2370 nova::vec
<float> val0
, val1
;
2371 val0
.load_aligned(in
);
2372 val1
.load_aligned(in
+ vecSize
);
2374 val0
= dstlo
* pow(dstratio
, val0
* rsrcrange
+ rrminuslo
);
2375 val1
= dstlo
* pow(dstratio
, val1
* rsrcrange
+ rrminuslo
);
2377 val0
.store_aligned(out
);
2378 val1
.store_aligned(out
+ vecSize
);
2385 FLATTEN
static void LinExp_next_nova(LinExp
*unit
, int inNumSamples
)
2387 float *out
= OUT(0);
2390 LinExp_next_nova_loop(out
, in
, inNumSamples
, unit
->m_dstlo
, unit
->m_dstratio
, unit
->m_rsrcrange
, unit
->m_rrminuslo
);
2393 FLATTEN
static void LinExp_next_nova_kk(LinExp
*unit
, int inNumSamples
)
2395 float *out
= OUT(0);
2398 float srclo
= ZIN0(1);
2399 float srchi
= ZIN0(2);
2400 float dstlo
= ZIN0(3);
2401 float dsthi
= ZIN0(4);
2402 float dstratio
= dsthi
/dstlo
;
2403 float rsrcrange
= sc_reciprocal(srchi
- srclo
);
2404 float rrminuslo
= rsrcrange
* -srclo
;
2406 LinExp_next_nova_loop(out
, in
, inNumSamples
, dstlo
, dstratio
, rsrcrange
, rrminuslo
);
2411 void LinExp_next_kk(LinExp
*unit
, int inNumSamples
)
2413 float *out
= ZOUT(0);
2415 float srclo
= ZIN0(1);
2416 float srchi
= ZIN0(2);
2417 float dstlo
= ZIN0(3);
2418 float dsthi
= ZIN0(4);
2419 float dstratio
= dsthi
* sc_reciprocal(dstlo
);
2420 float rsrcrange
= sc_reciprocal(srchi
- srclo
);
2421 float rrminuslo
= rsrcrange
* -srclo
;
2424 ZXP(out
) = dstlo
* pow(dstratio
, ZXP(in
) * rsrcrange
+ rrminuslo
);
2428 void LinExp_next_aa(LinExp
*unit
, int inNumSamples
)
2430 float *out
= ZOUT(0);
2432 float *srclo
= ZIN(1);
2433 float *srchi
= ZIN(2);
2434 float *dstlo
= ZIN(3);
2435 float *dsthi
= ZIN(4);
2439 float zdsthi
= ZXP(dsthi
);
2440 float zdstlo
= ZXP(dstlo
);
2441 float zsrchi
= ZXP(srchi
);
2442 float zsrclo
= ZXP(srclo
);
2443 float dstratio
= zdsthi
/zdstlo
;
2444 float rsrcrange
= sc_reciprocal(zsrchi
- zsrclo
);
2445 float rrminuslo
= rsrcrange
* -zsrclo
;
2446 ZXP(out
) = zdstlo
* pow(dstratio
, ZXP(in
) * rsrcrange
+ rrminuslo
);
2450 void LinExp_next_ak(LinExp
*unit
, int inNumSamples
)
2452 float *out
= ZOUT(0);
2454 float *srclo
= ZIN(1);
2455 float *srchi
= ZIN(2);
2456 float dstlo
= ZIN0(3);
2457 float dsthi
= ZIN0(4);
2458 float dstratio
= dsthi
/dstlo
;
2461 float zsrchi
= ZXP(srchi
);
2462 float zsrclo
= ZXP(srclo
);
2464 float rsrcrange
= sc_reciprocal(zsrchi
- zsrclo
);
2465 float rrminuslo
= rsrcrange
* -zsrclo
;
2466 ZXP(out
) = dstlo
* pow(dstratio
, ZXP(in
) * rsrcrange
+ rrminuslo
);
2470 void LinExp_next_ka(LinExp
*unit
, int inNumSamples
)
2472 float *out
= ZOUT(0);
2474 float srclo
= ZIN0(1);
2475 float srchi
= ZIN0(2);
2476 float *dstlo
= ZIN(3);
2477 float *dsthi
= ZIN(4);
2478 float rsrcrange
= sc_reciprocal(srchi
- srclo
);
2479 float rrminuslo
= rsrcrange
* -srclo
;
2482 float zdsthi
= ZXP(dsthi
);
2483 float zdstlo
= ZXP(dstlo
);
2484 float dstratio
= zdsthi
/zdstlo
;
2485 ZXP(out
) = zdstlo
* pow(dstratio
, ZXP(in
) * rsrcrange
+ rrminuslo
);
2490 static void LinExp_SetCalc(LinExp
* unit
)
2492 if(INRATE(1) == calc_FullRate
|| INRATE(2) == calc_FullRate
) {
2493 if(INRATE(3) == calc_FullRate
|| INRATE(4) == calc_FullRate
) {
2494 SETCALC(LinExp_next_aa
); return;
2496 SETCALC(LinExp_next_ak
); return;
2499 if(INRATE(3) == calc_FullRate
|| INRATE(4) == calc_FullRate
) {
2500 SETCALC(LinExp_next_ka
); return;
2504 bool allScalar
= true;
2505 for(int i
= 1; i
<5; i
++) {
2506 if(INRATE(i
) != calc_ScalarRate
) {
2513 if ((BUFLENGTH
% (2*nova::vec
<float>::size
)) == 0)
2515 SETCALC(LinExp_next_nova
);
2517 SETCALC(LinExp_next_nova_kk
);
2521 SETCALC(LinExp_next
);
2523 SETCALC(LinExp_next_kk
);
2528 float srclo
= ZIN0(1);
2529 float srchi
= ZIN0(2);
2530 float dstlo
= ZIN0(3);
2531 float dsthi
= ZIN0(4);
2532 unit
->m_dstlo
= dstlo
;
2533 unit
->m_dstratio
= dsthi
/dstlo
;
2534 unit
->m_rsrcrange
= sc_reciprocal(srchi
- srclo
);
2535 unit
->m_rrminuslo
= unit
->m_rsrcrange
* -srclo
;
2538 void LinExp_Ctor(LinExp
* unit
)
2540 LinExp_SetCalc(unit
);
2541 float srclo
= ZIN0(1);
2542 float srchi
= ZIN0(2);
2543 float dstlo
= ZIN0(3);
2544 float dsthi
= ZIN0(4);
2545 unit
->m_dstlo
= dstlo
;
2546 unit
->m_dstratio
= dsthi
/dstlo
;
2547 unit
->m_rsrcrange
= 1. / (srchi
- srclo
);
2548 unit
->m_rrminuslo
= unit
->m_rsrcrange
* -srclo
;
2549 LinExp_next(unit
, 1);
2552 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2563 kEnvGen_releaseNode
,
2565 // 'kEnvGen_nodeOffset' must always be last
2566 // if you need to add an arg, put it before this one
2571 void EnvGen_next_ak_nova(EnvGen
*unit
, int inNumSamples
);
2574 void EnvGen_Ctor(EnvGen
*unit
)
2576 //Print("EnvGen_Ctor A\n");
2577 if (unit
->mCalcRate
== calc_FullRate
) {
2578 if (INRATE(0) == calc_FullRate
) {
2579 SETCALC(EnvGen_next_aa
);
2582 if (!(BUFLENGTH
& 15))
2583 SETCALC(EnvGen_next_ak_nova
);
2586 SETCALC(EnvGen_next_ak
);
2589 SETCALC(EnvGen_next_k
);
2592 // gate = 1.0, levelScale = 1.0, levelBias = 0.0, timeScale
2593 // level0, numstages, releaseNode, loopNode,
2594 // [level, dur, shape, curve]
2596 unit
->m_endLevel
= unit
->m_level
= ZIN0(kEnvGen_initLevel
) * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
);
2597 unit
->m_counter
= 0;
2598 unit
->m_stage
= 1000000000;
2599 unit
->m_prevGate
= 0.f
;
2600 unit
->m_released
= false;
2601 unit
->m_releaseNode
= (int)ZIN0(kEnvGen_releaseNode
);
2602 EnvGen_next_k(unit
, 1);
2614 shape_Sustain
= 9999
2617 void EnvGen_next_k(EnvGen
*unit
, int inNumSamples
)
2619 float *out
= OUT(0);
2620 float gate
= ZIN0(kEnvGen_gate
);
2621 //Print("->EnvGen_next_k gate %g\n", gate);
2622 int counter
= unit
->m_counter
;
2623 double level
= unit
->m_level
;
2625 if (unit
->m_prevGate
<= 0. && gate
> 0.) {
2627 unit
->mDone
= false;
2628 unit
->m_released
= false;
2630 } else if (gate
<= -1.f
&& unit
->m_prevGate
> -1.f
) {
2632 int numstages
= (int)ZIN0(kEnvGen_numStages
);
2633 float dur
= -gate
- 1.f
;
2634 counter
= (int32
)(dur
* SAMPLERATE
);
2635 counter
= sc_max(1, counter
);
2636 unit
->m_stage
= numstages
;
2637 unit
->m_shape
= shape_Linear
;
2638 // first ZIN0 gets the last envelope node's level, then apply levelScale and levelBias
2639 unit
->m_endLevel
= ZIN0(unit
->mNumInputs
- 4) * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
);
2640 unit
->m_grow
= (unit
->m_endLevel
- level
) / counter
;
2641 } else if (unit
->m_prevGate
> 0.f
&& gate
<= 0.f
2642 && unit
->m_releaseNode
>= 0 && !unit
->m_released
) {
2644 unit
->m_stage
= unit
->m_releaseNode
- 1;
2645 unit
->m_released
= true;
2647 unit
->m_prevGate
= gate
;
2650 // gate = 1.0, levelScale = 1.0, levelBias = 0.0, timeScale
2651 // level0, numstages, releaseNode, loopNode,
2652 // [level, dur, shape, curve]
2655 //Print("stage %d rel %d\n", unit->m_stage, (int)ZIN0(kEnvGen_releaseNode));
2656 int numstages
= (int)ZIN0(kEnvGen_numStages
);
2658 //Print("stage %d numstages %d\n", unit->m_stage, numstages);
2659 if (unit
->m_stage
+1 >= numstages
) { // num stages
2660 //Print("stage+1 > num stages\n");
2663 level
= unit
->m_endLevel
;
2665 int doneAction
= (int)ZIN0(kEnvGen_doneAction
);
2666 DoneAction(doneAction
, unit
);
2667 } else if (unit
->m_stage
+1 == unit
->m_releaseNode
&& !unit
->m_released
) { // sustain stage
2668 int loopNode
= (int)ZIN0(kEnvGen_loopNode
);
2669 if (loopNode
>= 0 && loopNode
< numstages
) {
2670 unit
->m_stage
= loopNode
;
2674 unit
->m_shape
= shape_Sustain
;
2675 level
= unit
->m_endLevel
;
2677 //Print("sustain\n");
2681 //Print("stage %d\n", unit->m_stage);
2682 //Print("initSegment\n");
2683 //out = unit->m_level;
2684 int stageOffset
= (unit
->m_stage
<< 2) + kEnvGen_nodeOffset
;
2686 if (stageOffset
+ 4 > unit
->mNumInputs
) {
2688 Print("envelope went past end of inputs.\n");
2689 ClearUnitOutputs(unit
, 1);
2690 NodeEnd(&unit
->mParent
->mNode
);
2694 float** envPtr
= unit
->mInBuf
+ stageOffset
;
2695 double endLevel
= *envPtr
[0] * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
); // scale levels
2696 double dur
= *envPtr
[1] * ZIN0(kEnvGen_timeScale
);
2697 unit
->m_shape
= (int32
)*envPtr
[2];
2698 double curve
= *envPtr
[3];
2699 unit
->m_endLevel
= endLevel
;
2701 counter
= (int32
)(dur
* SAMPLERATE
);
2702 counter
= sc_max(1, counter
);
2703 //Print("stageOffset %d level %g endLevel %g dur %g shape %d curve %g\n", stageOffset, level, endLevel, dur, unit->m_shape, curve);
2704 //Print("SAMPLERATE %g\n", SAMPLERATE);
2705 if (counter
== 1) unit
->m_shape
= 1; // shape_Linear
2706 //Print("new counter = %d shape = %d\n", counter, unit->m_shape);
2707 switch (unit
->m_shape
) {
2711 case shape_Linear
: {
2712 unit
->m_grow
= (endLevel
- level
) / counter
;
2713 //Print("grow %g\n", unit->m_grow);
2715 case shape_Exponential
: {
2716 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / counter
);
2719 double w
= pi
/ counter
;
2721 unit
->m_a2
= (endLevel
+ level
) * 0.5;
2722 unit
->m_b1
= 2. * cos(w
);
2723 unit
->m_y1
= (endLevel
- level
) * 0.5;
2724 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
2725 level
= unit
->m_a2
- unit
->m_y1
;
2727 case shape_Welch
: {
2728 double w
= (pi
* 0.5) / counter
;
2730 unit
->m_b1
= 2. * cos(w
);
2732 if (endLevel
>= level
) {
2735 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
2737 unit
->m_a2
= endLevel
;
2738 unit
->m_y1
= level
- endLevel
;
2739 unit
->m_y2
= cos(w
) * (level
- endLevel
);
2741 level
= unit
->m_a2
+ unit
->m_y1
;
2743 case shape_Curve
: {
2744 if (fabs(curve
) < 0.001) {
2745 unit
->m_shape
= 1; // shape_Linear
2746 unit
->m_grow
= (endLevel
- level
) / counter
;
2748 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
2749 unit
->m_a2
= level
+ a1
;
2751 unit
->m_grow
= exp(curve
/ counter
);
2754 case shape_Squared
: {
2755 unit
->m_y1
= sqrt(level
);
2756 unit
->m_y2
= sqrt(endLevel
);
2757 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
2759 case shape_Cubed
: {
2760 unit
->m_y1
= pow(level
, 0.33333333);
2761 unit
->m_y2
= pow(endLevel
, 0.33333333);
2762 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
2769 switch (unit
->m_shape
) {
2772 case shape_Linear
: {
2773 double grow
= unit
->m_grow
;
2774 //Print("level %g\n", level);
2777 case shape_Exponential
: {
2778 double grow
= unit
->m_grow
;
2782 double a2
= unit
->m_a2
;
2783 double b1
= unit
->m_b1
;
2784 double y2
= unit
->m_y2
;
2785 double y1
= unit
->m_y1
;
2786 double y0
= b1
* y1
- y2
;
2793 case shape_Welch
: {
2794 double a2
= unit
->m_a2
;
2795 double b1
= unit
->m_b1
;
2796 double y2
= unit
->m_y2
;
2797 double y1
= unit
->m_y1
;
2798 double y0
= b1
* y1
- y2
;
2805 case shape_Curve
: {
2806 double a2
= unit
->m_a2
;
2807 double b1
= unit
->m_b1
;
2808 double grow
= unit
->m_grow
;
2813 case shape_Squared
: {
2814 double grow
= unit
->m_grow
;
2815 double y1
= unit
->m_y1
;
2820 case shape_Cubed
: {
2821 double grow
= unit
->m_grow
;
2822 double y1
= unit
->m_y1
;
2827 case shape_Sustain
: {
2831 //Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, *out);
2832 unit
->m_level
= level
;
2833 unit
->m_counter
= counter
- 1;
2837 void EnvGen_next_ak(EnvGen
*unit
, int inNumSamples
)
2839 float *out
= ZOUT(0);
2840 float gate
= ZIN0(kEnvGen_gate
);
2841 int counter
= unit
->m_counter
;
2842 double level
= unit
->m_level
;
2844 if (unit
->m_prevGate
<= 0. && gate
> 0.) {
2846 unit
->mDone
= false;
2847 unit
->m_released
= false;
2849 } else if (gate
<= -1.f
&& unit
->m_prevGate
> -1.f
) {
2851 int numstages
= (int)ZIN0(kEnvGen_numStages
);
2852 float dur
= -gate
- 1.f
;
2853 counter
= (int32
)(dur
* SAMPLERATE
);
2854 counter
= sc_max(1, counter
);
2855 unit
->m_stage
= numstages
;
2856 unit
->m_shape
= shape_Linear
;
2857 unit
->m_endLevel
= ZIN0(unit
->mNumInputs
- 4) * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
);
2858 unit
->m_grow
= (unit
->m_endLevel
- level
) / counter
;
2859 } else if (unit
->m_prevGate
> 0.f
&& gate
<= 0.f
2860 && unit
->m_releaseNode
>= 0 && !unit
->m_released
) {
2862 unit
->m_stage
= unit
->m_releaseNode
- 1;
2863 unit
->m_released
= true;
2865 unit
->m_prevGate
= gate
;
2867 int remain
= inNumSamples
;
2871 int numstages
= (int)ZIN0(kEnvGen_numStages
);
2873 if (unit
->m_stage
+1 >= numstages
) { // num stages
2876 level
= unit
->m_endLevel
;
2878 int doneAction
= (int)ZIN0(kEnvGen_doneAction
);
2879 DoneAction(doneAction
, unit
);
2880 } else if (unit
->m_stage
+1 == (int)ZIN0(kEnvGen_releaseNode
) && !unit
->m_released
) { // sustain stage
2881 int loopNode
= (int)ZIN0(kEnvGen_loopNode
);
2882 if (loopNode
>= 0 && loopNode
< numstages
) {
2883 unit
->m_stage
= loopNode
;
2887 unit
->m_shape
= shape_Sustain
;
2888 level
= unit
->m_endLevel
;
2893 int stageOffset
= (unit
->m_stage
<< 2) + kEnvGen_nodeOffset
;
2895 if (stageOffset
+ 4 > unit
->mNumInputs
) {
2897 Print("envelope went past end of inputs.\n");
2898 ClearUnitOutputs(unit
, 1);
2899 NodeEnd(&unit
->mParent
->mNode
);
2903 float** envPtr
= unit
->mInBuf
+ stageOffset
;
2904 double endLevel
= *envPtr
[0] * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
); // scale levels
2905 double dur
= *envPtr
[1] * ZIN0(kEnvGen_timeScale
);
2906 unit
->m_shape
= (int32
)*envPtr
[2];
2907 double curve
= *envPtr
[3];
2908 unit
->m_endLevel
= endLevel
;
2910 counter
= (int32
)(dur
* SAMPLERATE
);
2911 counter
= sc_max(1, counter
);
2913 if (counter
== 1) unit
->m_shape
= 1; // shape_Linear
2914 switch (unit
->m_shape
) {
2918 case shape_Linear
: {
2919 unit
->m_grow
= (endLevel
- level
) / counter
;
2921 case shape_Exponential
: {
2922 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / counter
);
2925 double w
= pi
/ counter
;
2927 unit
->m_a2
= (endLevel
+ level
) * 0.5;
2928 unit
->m_b1
= 2. * cos(w
);
2929 unit
->m_y1
= (endLevel
- level
) * 0.5;
2930 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
2931 level
= unit
->m_a2
- unit
->m_y1
;
2933 case shape_Welch
: {
2934 double w
= (pi
* 0.5) / counter
;
2936 unit
->m_b1
= 2. * cos(w
);
2938 if (endLevel
>= level
) {
2941 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
2943 unit
->m_a2
= endLevel
;
2944 unit
->m_y1
= level
- endLevel
;
2945 unit
->m_y2
= cos(w
) * (level
- endLevel
);
2947 level
= unit
->m_a2
+ unit
->m_y1
;
2949 case shape_Curve
: {
2950 if (fabs(curve
) < 0.001) {
2951 unit
->m_shape
= 1; // shape_Linear
2952 unit
->m_grow
= (endLevel
- level
) / counter
;
2954 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
2955 unit
->m_a2
= level
+ a1
;
2957 unit
->m_grow
= exp(curve
/ counter
);
2960 case shape_Squared
: {
2961 unit
->m_y1
= sqrt(level
);
2962 unit
->m_y2
= sqrt(endLevel
);
2963 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
2965 case shape_Cubed
: {
2966 unit
->m_y1
= pow(level
, 0.33333333);
2967 unit
->m_y2
= pow(endLevel
, 0.33333333);
2968 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
2974 int nsmps
= sc_min(remain
, counter
);
2975 switch (unit
->m_shape
) {
2977 for (int i
=0; i
<nsmps
; ++i
) {
2981 case shape_Linear
: {
2982 double grow
= unit
->m_grow
;
2983 for (int i
=0; i
<nsmps
; ++i
) {
2988 case shape_Exponential
: {
2989 double grow
= unit
->m_grow
;
2990 for (int i
=0; i
<nsmps
; ++i
) {
2996 double a2
= unit
->m_a2
;
2997 double b1
= unit
->m_b1
;
2998 double y2
= unit
->m_y2
;
2999 double y1
= unit
->m_y1
;
3000 for (int i
=0; i
<nsmps
; ++i
) {
3002 double y0
= b1
* y1
- y2
;
3010 case shape_Welch
: {
3011 double a2
= unit
->m_a2
;
3012 double b1
= unit
->m_b1
;
3013 double y2
= unit
->m_y2
;
3014 double y1
= unit
->m_y1
;
3015 for (int i
=0; i
<nsmps
; ++i
) {
3017 double y0
= b1
* y1
- y2
;
3025 case shape_Curve
: {
3026 double a2
= unit
->m_a2
;
3027 double b1
= unit
->m_b1
;
3028 double grow
= unit
->m_grow
;
3029 for (int i
=0; i
<nsmps
; ++i
) {
3036 case shape_Squared
: {
3037 double grow
= unit
->m_grow
;
3038 double y1
= unit
->m_y1
;
3039 for (int i
=0; i
<nsmps
; ++i
) {
3046 case shape_Cubed
: {
3047 double grow
= unit
->m_grow
;
3048 double y1
= unit
->m_y1
;
3049 for (int i
=0; i
<nsmps
; ++i
) {
3056 case shape_Sustain
: {
3057 for (int i
=0; i
<nsmps
; ++i
) {
3065 //Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, ZOUT0(0));
3066 unit
->m_level
= level
;
3067 unit
->m_counter
= counter
;
3072 FLATTEN
void EnvGen_next_ak_nova(EnvGen
*unit
, int inNumSamples
)
3074 float *out
= ZOUT(0);
3075 float gate
= ZIN0(kEnvGen_gate
);
3076 int counter
= unit
->m_counter
;
3077 double level
= unit
->m_level
;
3079 if (unit
->m_prevGate
<= 0. && gate
> 0.) {
3081 unit
->mDone
= false;
3082 unit
->m_released
= false;
3084 } else if (gate
<= -1.f
&& unit
->m_prevGate
> -1.f
) {
3086 int numstages
= (int)ZIN0(kEnvGen_numStages
);
3087 float dur
= -gate
- 1.f
;
3088 counter
= (int32
)(dur
* SAMPLERATE
);
3089 counter
= sc_max(1, counter
);
3090 unit
->m_stage
= numstages
;
3091 unit
->m_shape
= shape_Linear
;
3092 unit
->m_endLevel
= ZIN0(unit
->mNumInputs
- 4) * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
);
3093 unit
->m_grow
= (unit
->m_endLevel
- level
) / counter
;
3094 } else if (unit
->m_prevGate
> 0.f
&& gate
<= 0.f
3095 && unit
->m_releaseNode
>= 0 && !unit
->m_released
) {
3097 unit
->m_stage
= unit
->m_releaseNode
- 1;
3098 unit
->m_released
= true;
3100 unit
->m_prevGate
= gate
;
3102 int remain
= inNumSamples
;
3103 if (counter
> inNumSamples
)
3105 switch (unit
->m_shape
) {
3107 case shape_Sustain
:
3108 nova::setvec_simd(OUT(0), (float)level
, inNumSamples
);
3110 counter
-= inNumSamples
;
3112 case shape_Linear
: {
3113 double slope
= unit
->m_grow
;
3114 nova::set_slope_vec_simd(OUT(0), (float)level
, (float)slope
, inNumSamples
);
3115 level
+= inNumSamples
* slope
;
3117 counter
-= inNumSamples
;
3119 case shape_Exponential
: {
3120 double grow
= unit
->m_grow
;
3121 nova::set_exp_vec_simd(OUT(0), (float)level
, (float)grow
, inNumSamples
);
3122 level
*= sc_powi(grow
, inNumSamples
);
3124 counter
-= inNumSamples
;
3132 int numstages
= (int)ZIN0(kEnvGen_numStages
);
3134 if (unit
->m_stage
+1 >= numstages
) { // num stages
3137 level
= unit
->m_endLevel
;
3139 int doneAction
= (int)ZIN0(kEnvGen_doneAction
);
3140 DoneAction(doneAction
, unit
);
3141 } else if (unit
->m_stage
+1 == (int)ZIN0(kEnvGen_releaseNode
) && !unit
->m_released
) { // sustain stage
3142 int loopNode
= (int)ZIN0(kEnvGen_loopNode
);
3143 if (loopNode
>= 0 && loopNode
< numstages
) {
3144 unit
->m_stage
= loopNode
;
3148 unit
->m_shape
= shape_Sustain
;
3149 level
= unit
->m_endLevel
;
3154 int stageOffset
= (unit
->m_stage
<< 2) + kEnvGen_nodeOffset
;
3156 if (stageOffset
+ 4 > unit
->mNumInputs
) {
3158 Print("envelope went past end of inputs.\n");
3159 ClearUnitOutputs(unit
, 1);
3160 NodeEnd(&unit
->mParent
->mNode
);
3164 float** envPtr
= unit
->mInBuf
+ stageOffset
;
3165 double endLevel
= *envPtr
[0] * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
); // scale levels
3166 double dur
= *envPtr
[1] * ZIN0(kEnvGen_timeScale
);
3167 unit
->m_shape
= (int32
)*envPtr
[2];
3168 double curve
= *envPtr
[3];
3169 unit
->m_endLevel
= endLevel
;
3171 counter
= (int32
)(dur
* SAMPLERATE
);
3172 counter
= sc_max(1, counter
);
3174 if (counter
== 1) unit
->m_shape
= 1; // shape_Linear
3175 switch (unit
->m_shape
) {
3179 case shape_Linear
: {
3180 unit
->m_grow
= (endLevel
- level
) / counter
;
3182 case shape_Exponential
: {
3183 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / counter
);
3186 double w
= pi
/ counter
;
3188 unit
->m_a2
= (endLevel
+ level
) * 0.5;
3189 unit
->m_b1
= 2. * cos(w
);
3190 unit
->m_y1
= (endLevel
- level
) * 0.5;
3191 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
3192 level
= unit
->m_a2
- unit
->m_y1
;
3194 case shape_Welch
: {
3195 double w
= (pi
* 0.5) / counter
;
3197 unit
->m_b1
= 2. * cos(w
);
3199 if (endLevel
>= level
) {
3202 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
3204 unit
->m_a2
= endLevel
;
3205 unit
->m_y1
= level
- endLevel
;
3206 unit
->m_y2
= cos(w
) * (level
- endLevel
);
3208 level
= unit
->m_a2
+ unit
->m_y1
;
3210 case shape_Curve
: {
3211 if (fabs(curve
) < 0.001) {
3212 unit
->m_shape
= 1; // shape_Linear
3213 unit
->m_grow
= (endLevel
- level
) / counter
;
3215 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
3216 unit
->m_a2
= level
+ a1
;
3218 unit
->m_grow
= exp(curve
/ counter
);
3221 case shape_Squared
: {
3222 unit
->m_y1
= sqrt(level
);
3223 unit
->m_y2
= sqrt(endLevel
);
3224 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
3226 case shape_Cubed
: {
3227 unit
->m_y1
= pow(level
, 0.33333333);
3228 unit
->m_y2
= pow(endLevel
, 0.33333333);
3229 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
3235 int nsmps
= sc_min(remain
, counter
);
3236 switch (unit
->m_shape
) {
3238 for (int i
=0; i
<nsmps
; ++i
) {
3242 case shape_Linear
: {
3243 double grow
= unit
->m_grow
;
3244 for (int i
=0; i
<nsmps
; ++i
) {
3249 case shape_Exponential
: {
3250 double grow
= unit
->m_grow
;
3251 for (int i
=0; i
<nsmps
; ++i
) {
3257 double a2
= unit
->m_a2
;
3258 double b1
= unit
->m_b1
;
3259 double y2
= unit
->m_y2
;
3260 double y1
= unit
->m_y1
;
3261 for (int i
=0; i
<nsmps
; ++i
) {
3263 double y0
= b1
* y1
- y2
;
3271 case shape_Welch
: {
3272 double a2
= unit
->m_a2
;
3273 double b1
= unit
->m_b1
;
3274 double y2
= unit
->m_y2
;
3275 double y1
= unit
->m_y1
;
3276 for (int i
=0; i
<nsmps
; ++i
) {
3278 double y0
= b1
* y1
- y2
;
3286 case shape_Curve
: {
3287 double a2
= unit
->m_a2
;
3288 double b1
= unit
->m_b1
;
3289 double grow
= unit
->m_grow
;
3290 for (int i
=0; i
<nsmps
; ++i
) {
3297 case shape_Squared
: {
3298 double grow
= unit
->m_grow
;
3299 double y1
= unit
->m_y1
;
3300 for (int i
=0; i
<nsmps
; ++i
) {
3307 case shape_Cubed
: {
3308 double grow
= unit
->m_grow
;
3309 double y1
= unit
->m_y1
;
3310 for (int i
=0; i
<nsmps
; ++i
) {
3317 case shape_Sustain
: {
3318 for (int i
=0; i
<nsmps
; ++i
) {
3326 //Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, ZOUT0(0));
3327 unit
->m_level
= level
;
3328 unit
->m_counter
= counter
;
3333 #define CHECK_GATE \
3334 float prevGate = gate; \
3335 gate = ZXP(gatein); \
3336 if (prevGate <= 0.f && gate > 0.f) { \
3338 unit->m_stage = -1; \
3339 unit->m_released = false; \
3340 unit->mDone = false; \
3344 } else if (gate <= -1.f && unit->m_prevGate > -1.f) { \
3345 int numstages = (int)ZIN0(kEnvGen_numStages); \
3346 float dur = -gate - 1.f; \
3348 counter = (int32)(dur * SAMPLERATE); \
3349 counter = sc_max(1, counter) + i; \
3350 unit->m_stage = numstages; \
3351 unit->m_shape = shape_Linear; \
3352 unit->m_endLevel = ZIN0(unit->mNumInputs - 4) * ZIN0(kEnvGen_levelScale) + ZIN0(kEnvGen_levelBias); \
3353 unit->m_grow = (unit->m_endLevel - level) / counter; \
3356 } else if (prevGate > 0.f && gate <= 0.f \
3357 && unit->m_releaseNode >= 0 && !unit->m_released) { \
3360 unit->m_stage = unit->m_releaseNode - 1; \
3361 unit->m_released = true; \
3367 void EnvGen_next_aa(EnvGen *unit, int inNumSamples)
3369 float *out
= ZOUT(0);
3370 float *gatein
= ZIN(kEnvGen_gate
);
3371 int counter
= unit
->m_counter
;
3372 double level
= unit
->m_level
;
3373 float gate
= unit
->m_prevGate
;
3374 int remain
= inNumSamples
;
3379 int numstages
= (int)ZIN0(kEnvGen_numStages
);
3381 if (unit
->m_stage
+1 >= numstages
) { // num stages
3384 level
= unit
->m_endLevel
;
3386 int doneAction
= (int)ZIN0(kEnvGen_doneAction
);
3387 DoneAction(doneAction
, unit
);
3388 } else if (unit
->m_stage
+1 == (int)ZIN0(kEnvGen_releaseNode
) && !unit
->m_released
) { // sustain stage
3389 int loopNode
= (int)ZIN0(kEnvGen_loopNode
);
3390 if (loopNode
>= 0 && loopNode
< numstages
) {
3391 unit
->m_stage
= loopNode
;
3395 unit
->m_shape
= shape_Sustain
;
3396 level
= unit
->m_endLevel
;
3401 int stageOffset
= (unit
->m_stage
<< 2) + kEnvGen_nodeOffset
;
3403 if (stageOffset
+ 4 > unit
->mNumInputs
) {
3405 Print("envelope went past end of inputs.\n");
3406 ClearUnitOutputs(unit
, 1);
3407 NodeEnd(&unit
->mParent
->mNode
);
3411 float** envPtr
= unit
->mInBuf
+ stageOffset
;
3412 double endLevel
= *envPtr
[0] * ZIN0(kEnvGen_levelScale
) + ZIN0(kEnvGen_levelBias
); // scale levels
3413 double dur
= *envPtr
[1] * ZIN0(kEnvGen_timeScale
);
3414 unit
->m_shape
= (int32
)*envPtr
[2];
3415 double curve
= *envPtr
[3];
3416 unit
->m_endLevel
= endLevel
;
3418 counter
= (int32
)(dur
* SAMPLERATE
);
3419 counter
= sc_max(1, counter
);
3420 if (counter
== 1) unit
->m_shape
= 1; // shape_Linear
3421 switch (unit
->m_shape
) {
3425 case shape_Linear
: {
3426 unit
->m_grow
= (endLevel
- level
) / counter
;
3428 case shape_Exponential
: {
3429 unit
->m_grow
= pow(endLevel
/ level
, 1.0 / counter
);
3432 double w
= pi
/ counter
;
3434 unit
->m_a2
= (endLevel
+ level
) * 0.5;
3435 unit
->m_b1
= 2. * cos(w
);
3436 unit
->m_y1
= (endLevel
- level
) * 0.5;
3437 unit
->m_y2
= unit
->m_y1
* sin(pi
* 0.5 - w
);
3438 level
= unit
->m_a2
- unit
->m_y1
;
3440 case shape_Welch
: {
3441 double w
= (pi
* 0.5) / counter
;
3443 unit
->m_b1
= 2. * cos(w
);
3445 if (endLevel
>= level
) {
3448 unit
->m_y2
= -sin(w
) * (endLevel
- level
);
3450 unit
->m_a2
= endLevel
;
3451 unit
->m_y1
= level
- endLevel
;
3452 unit
->m_y2
= cos(w
) * (level
- endLevel
);
3454 level
= unit
->m_a2
+ unit
->m_y1
;
3456 case shape_Curve
: {
3457 if (fabs(curve
) < 0.001) {
3458 unit
->m_shape
= 1; // shape_Linear
3459 unit
->m_grow
= (endLevel
- level
) / counter
;
3461 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
3462 unit
->m_a2
= level
+ a1
;
3464 unit
->m_grow
= exp(curve
/ counter
);
3467 case shape_Squared
: {
3468 unit
->m_y1
= sqrt(level
);
3469 unit
->m_y2
= sqrt(endLevel
);
3470 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
3472 case shape_Cubed
: {
3473 unit
->m_y1
= pow(level
, 0.33333333);
3474 unit
->m_y2
= pow(endLevel
, 0.33333333);
3475 unit
->m_grow
= (unit
->m_y2
- unit
->m_y1
) / counter
;
3481 int nsmps
= sc_min(remain
, counter
);
3483 switch (unit
->m_shape
) {
3485 for (int i
=0; i
<nsmps
; ++i
) {
3490 case shape_Linear
: {
3491 double grow
= unit
->m_grow
;
3492 for (int i
=0; i
<nsmps
; ++i
) {
3498 case shape_Exponential
: {
3499 double grow
= unit
->m_grow
;
3500 for (int i
=0; i
<nsmps
; ++i
) {
3507 double a2
= unit
->m_a2
;
3508 double b1
= unit
->m_b1
;
3509 double y2
= unit
->m_y2
;
3510 double y1
= unit
->m_y1
;
3511 for (int i
=0; i
<nsmps
; ++i
) {
3514 double y0
= b1
* y1
- y2
;
3522 case shape_Welch
: {
3523 double a2
= unit
->m_a2
;
3524 double b1
= unit
->m_b1
;
3525 double y2
= unit
->m_y2
;
3526 double y1
= unit
->m_y1
;
3527 for (int i
=0; i
<nsmps
; ++i
) {
3530 double y0
= b1
* y1
- y2
;
3538 case shape_Curve
: {
3539 double a2
= unit
->m_a2
;
3540 double b1
= unit
->m_b1
;
3541 double grow
= unit
->m_grow
;
3542 for (int i
=0; i
<nsmps
; ++i
) {
3550 case shape_Squared
: {
3551 double grow
= unit
->m_grow
;
3552 double y1
= unit
->m_y1
;
3553 for (int i
=0; i
<nsmps
; ++i
) {
3561 case shape_Cubed
: {
3562 double grow
= unit
->m_grow
;
3563 double y1
= unit
->m_y1
;
3564 for (int i
=0; i
<nsmps
; ++i
) {
3572 case shape_Sustain
: {
3573 for (int i
=0; i
<nsmps
; ++i
) {
3582 unit
->m_level
= level
;
3583 unit
->m_counter
= counter
;
3584 unit
->m_prevGate
= gate
;
3587 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3589 void Linen_Ctor(Linen
*unit
)
3591 // gate attack level release
3592 SETCALC(Linen_next_k
);
3594 unit
->m_level
= 0.f
;
3596 unit
->m_prevGate
= 0.f
;
3597 Linen_next_k(unit
, 1);
3600 void Linen_next_k(Linen
*unit
, int inNumSamples
)
3602 float gate
= ZIN0(0);
3603 float *out
= OUT(0);
3605 if (unit
->m_prevGate
<= 0.f
&& gate
> 0.f
) {
3606 unit
->mDone
= false;
3608 float attackTime
= ZIN0(1);
3609 float susLevel
= ZIN0(2);
3610 int counter
= (int)(attackTime
* SAMPLERATE
);
3611 counter
= sc_max(1, counter
);
3612 unit
->m_slope
= (susLevel
- unit
->m_level
) / counter
;
3613 unit
->m_counter
= counter
;
3616 switch (unit
->m_stage
) {
3619 *out
= unit
->m_level
;
3620 unit
->m_level
+= unit
->m_slope
;
3621 if (--unit
->m_counter
== 0) unit
->m_stage
++;
3624 *out
= unit
->m_level
;
3628 float releaseTime
= -gate
- 1.f
;
3629 int counter
= (int)(releaseTime
* SAMPLERATE
);
3630 counter
= sc_max(1, counter
);
3631 unit
->m_slope
= -unit
->m_level
/ counter
;
3632 unit
->m_counter
= counter
;
3633 } else if (gate
<= 0.f
) {
3635 float releaseTime
= ZIN0(3);
3636 int counter
= (int)(releaseTime
* SAMPLERATE
);
3637 counter
= sc_max(1, counter
);
3638 unit
->m_slope
= -unit
->m_level
/ counter
;
3639 unit
->m_counter
= counter
;
3641 //Print("release %d %d\n", unit->mParent->mNode.mID, counter);
3646 //Print("done %d\n", unit->mParent->mNode.mID);
3649 int doneAction
= (int)ZIN0(4);
3650 DoneAction(doneAction
, unit
);
3656 unit
->m_prevGate
= gate
;
3659 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3661 void EnvFill(World
*world
, struct SndBuf
*buf
, struct sc_msg_iter
*msg
)
3663 if (buf
->channels
!= 1) return;
3665 int size
= buf
->samples
;
3666 int byteSize
= size
* sizeof(float);
3667 float *data
= (float*)malloc(byteSize
);
3669 double level
= msg
->getf();
3670 int numStages
= msg
->geti();
3671 /*int releaseNode =*/ msg
->geti(); // ignored
3672 /*int loopNode =*/ msg
->geti(); // ignored
3676 int32 remain
= size
;
3678 for (int j
=0; j
< numStages
; ++j
)
3680 double endLevel
= msg
->getf();
3681 double dur
= msg
->getf();
3682 int shape
= msg
->geti();
3683 double curve
= msg
->getf();
3685 int32 ipos
= (int32
)pos
;
3686 double smpdur
= dur
* size
;
3687 int32 nsmps
= (int32
)smpdur
- ipos
;
3688 nsmps
= sc_min(nsmps
, remain
);
3693 for (int i
=0; i
<nsmps
; ++i
) {
3694 data
[index
++] = level
;
3697 case shape_Linear
: {
3698 double grow
= (endLevel
- level
) / nsmps
;
3699 for (int i
=0; i
<nsmps
; ++i
) {
3700 data
[index
++] = level
;
3704 case shape_Exponential
: {
3705 double grow
= pow(endLevel
/ level
, 1.0 / nsmps
);
3706 for (int i
=0; i
<nsmps
; ++i
) {
3707 data
[index
++] = level
;
3712 double w
= pi
/ nsmps
;
3714 double a2
= (endLevel
+ level
) * 0.5;
3715 double b1
= 2. * cos(w
);
3716 double y1
= (endLevel
- level
) * 0.5;
3717 double y2
= y1
* sin(pi
* 0.5 - w
);
3719 for (int i
=0; i
<nsmps
; ++i
) {
3720 data
[index
++] = level
;
3721 double y0
= b1
* y1
- y2
;
3727 case shape_Welch
: {
3728 double w
= (pi
* 0.5) / nsmps
;
3730 double b1
= 2. * cos(w
);
3732 if (endLevel
>= level
) {
3735 y2
= -sin(w
) * (endLevel
- level
);
3738 y1
= level
- endLevel
;
3739 y2
= cos(w
) * (level
- endLevel
);
3742 for (int i
=0; i
<nsmps
; ++i
) {
3743 data
[index
++] = level
;
3744 double y0
= b1
* y1
- y2
;
3750 case shape_Curve
: {
3751 if (fabs(curve
) < 0.001) {
3752 double grow
= (endLevel
- level
) / nsmps
;
3753 for (int i
=0; i
<nsmps
; ++i
) {
3754 data
[index
++] = level
;
3758 double a1
= (endLevel
- level
) / (1.0 - exp(curve
));
3759 double a2
= level
+ a1
;
3761 double grow
= exp(curve
/ nsmps
);
3762 for (int i
=0; i
<nsmps
; ++i
) {
3763 data
[index
++] = level
;
3769 case shape_Squared
: {
3770 double y1
= sqrt(level
);
3771 double y2
= sqrt(endLevel
);
3772 double grow
= (y2
- y1
) / nsmps
;
3773 for (int i
=0; i
<nsmps
; ++i
) {
3774 data
[index
++] = level
;
3779 case shape_Cubed
: {
3780 double y1
= pow(level
, 0.33333333);
3781 double y2
= pow(endLevel
, 0.33333333);
3782 double grow
= (y2
- y1
) / nsmps
;
3783 for (int i
=0; i
<nsmps
; ++i
) {
3784 data
[index
++] = level
;
3795 memcpy(buf
->data
, data
, byteSize
);
3799 //////////////////// Add IEnvGen 06/06/2007 /////////////////////////////////
3802 struct IEnvGen
: public Unit
3804 float m_level
, m_offset
;
3805 float m_startpoint
, m_numvals
, m_pointin
;
3812 void IEnvGen_next_a(IEnvGen
*unit
, int inNumSamples
);
3813 void IEnvGen_next_k(IEnvGen
*unit
, int inNumSamples
);
3814 void IEnvGen_Ctor(IEnvGen
* unit
);
3815 void IEnvGen_Dtor(IEnvGen
* unit
);
3818 #define GET_ENV_VAL \
3822 level = unit->m_level = endLevel; \
3824 case shape_Linear : \
3826 level = unit->m_level = pos * (endLevel - begLevel) + begLevel; \
3828 case shape_Exponential : \
3829 level = unit->m_level = begLevel * pow(endLevel / begLevel, pos); \
3832 level = unit->m_level = begLevel + (endLevel - begLevel) * (-cos(pi * pos) * 0.5 + 0.5); \
3834 case shape_Welch : \
3836 if (begLevel < endLevel) \
3837 level = unit->m_level = begLevel + (endLevel - begLevel) * sin(pi2 * pos); \
3839 level = unit->m_level = endLevel - (endLevel - begLevel) * sin(pi2 - pi2 * pos); \
3842 case shape_Curve : \
3843 if (fabs((float)curve) < 0.0001) { \
3844 level = unit->m_level = pos * (endLevel - begLevel) + begLevel; \
3846 double denom = 1. - exp((float)curve); \
3847 double numer = 1. - exp((float)(pos * curve)); \
3848 level = unit->m_level = begLevel + (endLevel - begLevel) * (numer/denom); \
3851 case shape_Squared : \
3853 double sqrtBegLevel = sqrt(begLevel); \
3854 double sqrtEndLevel = sqrt(endLevel); \
3855 double sqrtLevel = pos * (sqrtEndLevel - sqrtBegLevel) + sqrtBegLevel; \
3856 level = unit->m_level = sqrtLevel * sqrtLevel; \
3859 case shape_Cubed : \
3861 double cbrtBegLevel = pow(begLevel, 0.3333333f); \
3862 double cbrtEndLevel = pow(endLevel, 0.3333333f); \
3863 double cbrtLevel = pos * (cbrtEndLevel - cbrtBegLevel) + cbrtBegLevel; \
3864 level = unit->m_level = cbrtLevel * cbrtLevel * cbrtLevel; \
3870 void IEnvGen_Ctor(IEnvGen *unit)
3873 if (INRATE(0) == calc_FullRate
) {
3874 SETCALC(IEnvGen_next_a
);
3876 SETCALC(IEnvGen_next_k
);
3880 // initlevel, numstages, totaldur,
3881 // [dur, shape, curve, level] * numvals
3882 int numStages
= (int)IN0(3);
3883 int numvals
= numStages
* 4; // initlevel + (levels, dur, shape, curves) * stages
3884 float offset
= unit
->m_offset
= IN0(1);
3885 float point
= unit
->m_pointin
= IN0(0) - offset
;
3886 unit
->m_envvals
= (float*)RTAlloc(unit
->mWorld
, (int)(numvals
+ 1.) * sizeof(float));
3888 unit
->m_envvals
[0] = IN0(2);
3889 // Print("offset of and initial values %3,3f, %3.3f\n", offset, unit->m_envvals[0]);
3890 // fill m_envvals with the values;
3891 for (int i
= 1; i
<= numvals
; i
++) {
3892 unit
->m_envvals
[i
] = IN0(4 + i
);
3893 // Print("val for: %d, %3.3f\n", i, unit->m_envvals[i]);
3896 // float out = OUT0(0);
3897 float totalDur
= IN0(4);
3899 float newtime
= 0.f
;
3902 if (point
>= totalDur
) {
3903 unit
->m_level
= level
= unit
->m_envvals
[numStages
* 4]; // grab the last value
3906 unit
->m_level
= level
= unit
->m_envvals
[0];
3908 float segpos
= point
;
3909 // determine which segment the current time pointer needs calculated
3910 for(int j
= 0; point
>= newtime
; j
++) {
3911 seglen
= unit
->m_envvals
[(j
* 4) + 1];
3917 segpos
= segpos
+ seglen
;
3918 float begLevel
= unit
->m_envvals
[(stage
* 4)];
3919 int shape
= (int)unit
->m_envvals
[(stage
* 4) + 2];
3920 int curve
= (int)unit
->m_envvals
[(stage
* 4) + 3];
3921 float endLevel
= unit
->m_envvals
[(stage
* 4) + 4];
3922 float pos
= (segpos
/ seglen
);
3930 void IEnvGen_Dtor(IEnvGen
*unit
)
3932 RTFree(unit
->mWorld
, unit
->m_envvals
);
3936 void IEnvGen_next_a(IEnvGen
*unit
, int inNumSamples
)
3938 float* out
= OUT(0);
3939 float level
= unit
->m_level
;
3940 float* pointin
= IN(0);
3941 float offset
= unit
->m_offset
;
3942 int numStages
= (int)IN0(3);
3943 float point
; // = unit->m_pointin;
3945 float totalDur
= IN0(4);
3949 // level0, numstages, totaldur,
3950 // [initval, [dur, shape, curve, level] * N ]
3952 for( int i
= 0; i
< inNumSamples
; i
++) {
3953 if (pointin
[i
] == unit
->m_pointin
){
3956 unit
->m_pointin
= point
= sc_max(pointin
[i
] - offset
, 0.0);
3957 float newtime
= 0.f
;
3960 if (point
>= totalDur
) {
3961 unit
->m_level
= level
= unit
->m_envvals
[numStages
* 4]; // grab the last value
3964 unit
->m_level
= level
= unit
->m_envvals
[0];
3966 float segpos
= point
;
3967 // determine which segment the current time pointer needs
3968 for(int j
= 0; point
>= newtime
; j
++) {
3969 seglen
= unit
->m_envvals
[(j
* 4) + 1];
3974 stagemul
= stage
* 4;
3975 segpos
= segpos
+ seglen
;
3976 float begLevel
= unit
->m_envvals
[stagemul
];
3977 int shape
= (int)unit
->m_envvals
[stagemul
+ 2];
3978 int curve
= (int)unit
->m_envvals
[stagemul
+ 3];
3979 float endLevel
= unit
->m_envvals
[stagemul
+ 4];
3980 float pos
= (segpos
/ seglen
);
3990 void IEnvGen_next_k(IEnvGen
*unit
, int inNumSamples
)
3992 float* out
= OUT(0);
3993 float level
= unit
->m_level
;
3994 float pointin
= IN0(0);
3995 float offset
= unit
->m_offset
;
3996 int numStages
= (int)IN0(3);
3997 float point
; // = unit->m_pointin;
3999 float totalDur
= IN0(4);
4003 // level0, numstages, totaldur,
4004 // [initval, [dur, shape, curve, level] * N ]
4006 for( int i
= 0; i
< inNumSamples
; i
++) {
4007 if (pointin
== unit
->m_pointin
){
4010 unit
->m_pointin
= point
= sc_max(pointin
- offset
, 0.0);
4011 float newtime
= 0.f
;
4014 if (point
>= totalDur
) {
4015 unit
->m_level
= level
= unit
->m_envvals
[numStages
* 4]; // grab the last value
4018 unit
->m_level
= level
= unit
->m_envvals
[0];
4020 float segpos
= point
;
4021 // determine which segment the current time pointer needs
4022 for(int j
= 0; point
>= newtime
; j
++) {
4023 seglen
= unit
->m_envvals
[(j
* 4) + 1];
4028 stagemul
= stage
* 4;
4029 segpos
= segpos
+ seglen
;
4030 float begLevel
= unit
->m_envvals
[stagemul
];
4031 int shape
= (int)unit
->m_envvals
[stagemul
+ 2];
4032 int curve
= (int)unit
->m_envvals
[stagemul
+ 3];
4033 float endLevel
= unit
->m_envvals
[stagemul
+ 4];
4034 float pos
= (segpos
/ seglen
);
4044 ////////////////////////////////////////////////////////////////////////////////////////////////////////
4051 DefineSimpleUnit(Vibrato
);
4052 DefineSimpleUnit(LFPulse
);
4053 DefineSimpleUnit(LFSaw
);
4054 DefineSimpleUnit(LFPar
);
4055 DefineSimpleUnit(LFCub
);
4056 DefineSimpleUnit(LFTri
);
4057 DefineSimpleUnit(LFGauss
);
4058 DefineSimpleUnit(Impulse
);
4059 DefineSimpleUnit(VarSaw
);
4060 DefineSimpleUnit(SyncSaw
);
4061 DefineSimpleUnit(K2A
);
4062 DefineSimpleUnit(A2K
);
4063 DefineSimpleUnit(T2K
);
4064 DefineSimpleUnit(T2A
);
4065 DefineSimpleUnit(DC
);
4066 DefineSimpleUnit(Line
);
4067 DefineSimpleUnit(XLine
);
4069 DefineSimpleUnit(Wrap
);
4070 DefineSimpleUnit(Fold
);
4071 DefineSimpleUnit(Clip
);
4072 DefineSimpleUnit(Unwrap
);
4073 DefineSimpleUnit(AmpComp
);
4074 DefineSimpleUnit(AmpCompA
);
4075 DefineSimpleUnit(InRange
);
4076 DefineSimpleUnit(InRect
);
4077 DefineSimpleUnit(LinExp
);
4078 DefineSimpleUnit(EnvGen
);
4079 DefineSimpleUnit(Linen
);
4081 DefineBufGen("env", EnvFill
);
4083 DefineDtorUnit(IEnvGen
);