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"
24 #include <algorithm> /* for std::min and std::max */
26 #include "simd_peakmeter.hpp"
29 #include "simd_memory.hpp"
31 #include "function_attributes.h"
35 static InterfaceTable
*ft
;
37 //////////////////////////////////////////////////////////////////////////////////////////////////
39 struct Trig1
: public Unit
45 struct Trig
: public Unit
52 struct SendTrig
: public Unit
57 struct SendReply
: public Unit
68 struct Poll
: public Unit
70 int m_samplesRemain
, m_intervalSamples
;
72 float m_lastPoll
, m_id
;
78 struct ToggleFF
: public Unit
84 struct SetResetFF
: public Unit
87 float m_prevtrig
, m_prevreset
;
90 struct Latch
: public Unit
96 struct Gate
: public Unit
101 struct Schmidt
: public Unit
106 struct PulseDivider
: public Unit
113 struct PulseCount
: public Unit
116 float m_prevtrig
, m_prevreset
;
119 struct Stepper
: public Unit
122 float m_prevtrig
, m_prevreset
;
125 struct TDelay
: public Unit
131 struct ZeroCrossing
: public Unit
133 float mLevel
, m_prevfrac
, m_previn
;
137 struct Timer
: public Unit
139 float mLevel
, m_prevfrac
, m_previn
;
143 struct Sweep
: public Unit
149 struct Phasor
: public Unit
155 struct Peak
: public Unit
161 struct RunningMin
: public Unit
167 struct RunningMax
: public Unit
173 struct PeakFollower
: public Unit
179 struct MostChange
: public Unit
181 float mPrevA
, mPrevB
;
185 struct LeastChange
: public Unit
187 float mPrevA
, mPrevB
;
191 struct LastValue
: public Unit
199 struct Done
: public Unit
204 struct FreeSelf
: public Unit
209 struct PauseSelf
: public Unit
214 struct Pause
: public Unit
219 struct Free
: public Unit
224 struct FreeSelfWhenDone
: public Unit
229 struct PauseSelfWhenDone
: public Unit
236 void Trig1_Ctor(Trig1
*unit
);
237 void Trig1_next(Trig1
*unit
, int inNumSamples
);
238 void Trig1_next_k(Trig1
*unit
, int inNumSamples
);
240 void Trig_Ctor(Trig
*unit
);
241 void Trig_next(Trig
*unit
, int inNumSamples
);
242 void Trig_next_k(Trig
*unit
, int inNumSamples
);
244 void SendTrig_Ctor(SendTrig
*unit
);
245 void SendTrig_next(SendTrig
*unit
, int inNumSamples
);
246 void SendTrig_next_aka(SendTrig
*unit
, int inNumSamples
);
248 void SendReply_Ctor(SendReply
*unit
);
249 void SendReply_next(SendReply
*unit
, int inNumSamples
);
250 void SendReply_next_aka(SendReply
*unit
, int inNumSamples
);
252 void Poll_Ctor(Poll
* unit
);
253 void Poll_next_aa(Poll
*unit
, int inNumSamples
);
254 void Poll_next_ak(Poll
*unit
, int inNumSamples
);
255 void Poll_next_kk(Poll
*unit
, int inNumSamples
);
257 void SetResetFF_Ctor(SetResetFF
*unit
);
258 void SetResetFF_next_a(SetResetFF
*unit
, int inNumSamples
);
259 void SetResetFF_next_k(SetResetFF
*unit
, int inNumSamples
);
261 void ToggleFF_Ctor(ToggleFF
*unit
);
262 void ToggleFF_next(ToggleFF
*unit
, int inNumSamples
);
264 void Latch_Ctor(Latch
*unit
);
265 void Latch_next_ak(Latch
*unit
, int inNumSamples
);
266 void Latch_next_aa(Latch
*unit
, int inNumSamples
);
268 void Gate_Ctor(Gate
*unit
);
269 void Gate_next_ak(Gate
*unit
, int inNumSamples
);
270 void Gate_next_aa(Gate
*unit
, int inNumSamples
);
272 void Schmidt_Ctor(Schmidt
*unit
);
273 void Schmidt_next(Schmidt
*unit
, int inNumSamples
);
275 void PulseDivider_Ctor(PulseDivider
*unit
);
276 void PulseDivider_next(PulseDivider
*unit
, int inNumSamples
);
278 void PulseCount_Ctor(PulseCount
*unit
);
279 void PulseCount_next_a(PulseCount
*unit
, int inNumSamples
);
280 void PulseCount_next_k(PulseCount
*unit
, int inNumSamples
);
281 void PulseCount_next_0(PulseCount
*unit
, int inNumSamples
);
283 void Stepper_Ctor(Stepper
*unit
);
284 void Stepper_next_aa(Stepper
*unit
, int inNumSamples
);
285 void Stepper_next_ak(Stepper
*unit
, int inNumSamples
);
286 void Stepper_next_a0(Stepper
*unit
, int inNumSamples
);
288 void TDelay_Ctor(TDelay
*unit
);
289 void TDelay_next(TDelay
*unit
, int inNumSamples
);
291 void ZeroCrossing_Ctor(ZeroCrossing
*unit
);
292 void ZeroCrossing_next_a(ZeroCrossing
*unit
, int inNumSamples
);
294 void Timer_Ctor(Timer
*unit
);
295 void Timer_next_a(Timer
*unit
, int inNumSamples
);
297 void Sweep_Ctor(Sweep
*unit
);
298 void Sweep_next_0k(Sweep
*unit
, int inNumSamples
);
299 void Sweep_next_0a(Sweep
*unit
, int inNumSamples
);
300 void Sweep_next_kk(Sweep
*unit
, int inNumSamples
);
301 void Sweep_next_ka(Sweep
*unit
, int inNumSamples
);
302 void Sweep_next_ak(Sweep
*unit
, int inNumSamples
);
303 void Sweep_next_aa(Sweep
*unit
, int inNumSamples
);
305 void Phasor_Ctor(Phasor
*unit
);
306 void Phasor_next_kk(Phasor
*unit
, int inNumSamples
);
307 void Phasor_next_ak(Phasor
*unit
, int inNumSamples
);
308 void Phasor_next_aa(Phasor
*unit
, int inNumSamples
);
310 void Peak_Ctor(Peak
*unit
);
311 void Peak_next_ak(Peak
*unit
, int inNumSamples
);
312 void Peak_next_ai(Peak
*unit
, int inNumSamples
);
313 void Peak_next_aa(Peak
*unit
, int inNumSamples
);
314 void Peak_next_ak_k(Peak
*unit
, int inNumSamples
);
315 void Peak_next_ai_k(Peak
*unit
, int inNumSamples
);
316 void Peak_next_aa_k(Peak
*unit
, int inNumSamples
);
318 void RunningMin_Ctor(RunningMin
*unit
);
319 void RunningMin_next_ak(RunningMin
*unit
, int inNumSamples
);
320 void RunningMin_next_ai(RunningMin
*unit
, int inNumSamples
);
321 void RunningMin_next_aa(RunningMin
*unit
, int inNumSamples
);
323 void RunningMax_Ctor(RunningMax
*unit
);
324 void RunningMax_next_ak(RunningMax
*unit
, int inNumSamples
);
325 void RunningMax_next_ai(RunningMax
*unit
, int inNumSamples
);
326 void RunningMax_next_aa(RunningMax
*unit
, int inNumSamples
);
329 void PeakFollower_Ctor(PeakFollower
*unit
);
330 void PeakFollower_next(PeakFollower
*unit
, int inNumSamples
);
331 void PeakFollower_next_ai(PeakFollower
*unit
, int inNumSamples
);
333 void MostChange_Ctor(MostChange
*unit
);
334 void MostChange_next_ak(MostChange
*unit
, int inNumSamples
);
335 void MostChange_next_ka(MostChange
*unit
, int inNumSamples
);
336 void MostChange_next_aa(MostChange
*unit
, int inNumSamples
);
338 void LeastChange_Ctor(LeastChange
*unit
);
339 void LeastChange_next_ak(LeastChange
*unit
, int inNumSamples
);
340 void LeastChange_next_ka(LeastChange
*unit
, int inNumSamples
);
341 void LeastChange_next_aa(LeastChange
*unit
, int inNumSamples
);
343 void LastValue_Ctor(LastValue
*unit
);
344 void LastValue_next_ak(LastValue
*unit
, int inNumSamples
);
345 void LastValue_next_kk(LastValue
*unit
, int inNumSamples
);
347 void Done_Ctor(Done
*unit
);
348 void Done_next(Done
*unit
, int inNumSamples
);
350 void FreeSelf_Ctor(FreeSelf
*unit
);
351 void FreeSelf_next(FreeSelf
*unit
, int inNumSamples
);
353 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone
*unit
);
354 void FreeSelfWhenDone_next(FreeSelfWhenDone
*unit
, int inNumSamples
);
356 void PauseSelf_Ctor(PauseSelf
*unit
);
357 void PauseSelf_next(PauseSelf
*unit
, int inNumSamples
);
359 void Pause_Ctor(Pause
*unit
);
360 void Pause_next(Pause
*unit
, int inNumSamples
);
362 void Free_Ctor(Free
*unit
);
363 void Free_next(Free
*unit
, int inNumSamples
);
365 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone
*unit
);
366 void PauseSelfWhenDone_next(PauseSelfWhenDone
*unit
, int inNumSamples
);
370 ////////////////////////////////////////////////////////////////////////////////////////////////////////
373 FLATTEN
void Trig1_next_nova(Trig1
*unit
, int inNumSamples
);
374 FLATTEN
void Trig1_next_k_nova(Trig1
*unit
, int inNumSamples
);
377 void Trig1_Ctor(Trig1
*unit
)
379 if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) != calc_FullRate
) {
381 if (!(BUFLENGTH
& 15))
382 SETCALC(Trig1_next_k_nova
);
385 SETCALC(Trig1_next_k
);
388 if (!(BUFLENGTH
& 15))
389 SETCALC(Trig1_next_nova
);
395 unit
->m_prevtrig
= 0.f
;
400 void Trig1_next(Trig1
*unit
, int inNumSamples
)
402 float *out
= ZOUT(0);
403 float *trig
= ZIN(0);
405 float sr
= unit
->mRate
->mSampleRate
;
406 float prevtrig
= unit
->m_prevtrig
;
407 unsigned long counter
= unit
->mCounter
;
410 float curtrig
= ZXP(trig
);
413 zout
= --counter
? 1.f
: 0.f
;
415 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
416 counter
= (long)(dur
* sr
+ .5f
);
417 if (counter
< 1) counter
= 1;
426 unit
->m_prevtrig
= prevtrig
;
427 unit
->mCounter
= counter
;
430 void Trig1_next_k(Trig1
*unit
, int inNumSamples
)
432 float *out
= ZOUT(0);
433 float curtrig
= ZIN0(0);
435 float sr
= unit
->mRate
->mSampleRate
;
436 float prevtrig
= unit
->m_prevtrig
;
437 unsigned long counter
= unit
->mCounter
;
442 zout
= --counter
? 1.f
: 0.f
;
444 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
445 counter
= (long)(dur
* sr
+ .5f
);
446 if (counter
< 1) counter
= 1;
455 unit
->m_prevtrig
= prevtrig
;
456 unit
->mCounter
= counter
;
460 void Trig1_next_nova(Trig1
*unit
, int inNumSamples
)
462 float *out
= ZOUT(0);
463 float *trig
= ZIN(0);
465 float sr
= unit
->mRate
->mSampleRate
;
466 float prevtrig
= unit
->m_prevtrig
;
467 unsigned long counter
= unit
->mCounter
;
469 if (counter
> inNumSamples
)
471 nova::setvec_simd(OUT(0), 1.f
, inNumSamples
);
472 counter
-= inNumSamples
;
474 prevtrig
= IN(0)[inNumSamples
-1];
479 float curtrig
= ZXP(trig
);
482 zout
= --counter
? 1.f
: 0.f
;
484 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
485 counter
= (long)(dur
* sr
+ .5f
);
486 if (counter
< 1) counter
= 1;
496 unit
->m_prevtrig
= prevtrig
;
497 unit
->mCounter
= counter
;
501 void Trig1_next_k_nova(Trig1
*unit
, int inNumSamples
)
503 float *out
= ZOUT(0);
504 float curtrig
= ZIN0(0);
506 float sr
= unit
->mRate
->mSampleRate
;
507 float prevtrig
= unit
->m_prevtrig
;
508 unsigned long counter
= unit
->mCounter
;
510 if (counter
> inNumSamples
)
512 nova::setvec_simd(OUT(0), 1.f
, inNumSamples
);
513 counter
-= inNumSamples
;
517 else if (counter
== 0 && (curtrig
<= 0.f
|| prevtrig
> 0.f
))
519 nova::zerovec_simd(OUT(0), inNumSamples
);
527 zout
= --counter
? 1.f
: 0.f
;
529 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
530 counter
= (long)(dur
* sr
+ .5f
);
531 if (counter
< 1) counter
= 1;
542 unit
->m_prevtrig
= prevtrig
;
543 unit
->mCounter
= counter
;
548 ////////////////////////////////////////////////////////////////////////////////////////////////////////
551 void Trig_next_nova(Trig
*unit
, int inNumSamples
);
552 void Trig_next_k_nova(Trig
*unit
, int inNumSamples
);
555 void Trig_Ctor(Trig
*unit
)
557 if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) != calc_FullRate
) {
559 if (!(BUFLENGTH
& 15))
560 SETCALC(Trig_next_k_nova
);
563 SETCALC(Trig_next_k
);
566 if (!(BUFLENGTH
& 15))
567 SETCALC(Trig_next_nova
);
574 unit
->m_prevtrig
= 0.f
;
580 void Trig_next(Trig
*unit
, int inNumSamples
)
582 float *out
= ZOUT(0);
583 float *trig
= ZIN(0);
585 float sr
= unit
->mRate
->mSampleRate
;
586 float prevtrig
= unit
->m_prevtrig
;
587 float level
= unit
->mLevel
;
588 unsigned long counter
= unit
->mCounter
;
591 float curtrig
= ZXP(trig
);
594 zout
= --counter
? level
: 0.f
;
596 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
597 counter
= (long)(dur
* sr
+ .5f
);
598 if (counter
< 1) counter
= 1;
608 unit
->m_prevtrig
= prevtrig
;
609 unit
->mCounter
= counter
;
610 unit
->mLevel
= level
;
613 void Trig_next_k(Trig
*unit
, int inNumSamples
)
615 float *out
= ZOUT(0);
616 float curtrig
= ZIN0(0);
618 float sr
= unit
->mRate
->mSampleRate
;
619 float prevtrig
= unit
->m_prevtrig
;
620 float level
= unit
->mLevel
;
621 unsigned long counter
= unit
->mCounter
;
626 zout
= --counter
? level
: 0.f
;
628 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
629 counter
= (long)(dur
* sr
+ .5f
);
630 if (counter
< 1) counter
= 1;
640 unit
->m_prevtrig
= prevtrig
;
641 unit
->mCounter
= counter
;
642 unit
->mLevel
= level
;
646 void Trig_next_nova(Trig
*unit
, int inNumSamples
)
648 float *out
= ZOUT(0);
649 float *trig
= ZIN(0);
651 float sr
= unit
->mRate
->mSampleRate
;
652 float prevtrig
= unit
->m_prevtrig
;
653 float level
= unit
->mLevel
;
654 unsigned long counter
= unit
->mCounter
;
656 if (counter
> inNumSamples
)
658 nova::setvec_simd(OUT(0), level
, inNumSamples
);
659 counter
-= inNumSamples
;
661 prevtrig
= IN(0)[inNumSamples
-1];
666 float curtrig
= ZXP(trig
);
669 zout
= --counter
? level
: 0.f
;
671 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
672 counter
= (long)(dur
* sr
+ .5f
);
673 if (counter
< 1) counter
= 1;
684 unit
->m_prevtrig
= prevtrig
;
685 unit
->mCounter
= counter
;
686 unit
->mLevel
= level
;
689 void Trig_next_k_nova(Trig
*unit
, int inNumSamples
)
691 float *out
= ZOUT(0);
692 float curtrig
= ZIN0(0);
694 float sr
= unit
->mRate
->mSampleRate
;
695 float prevtrig
= unit
->m_prevtrig
;
696 float level
= unit
->mLevel
;
697 unsigned long counter
= unit
->mCounter
;
699 if (counter
> inNumSamples
)
701 nova::setvec_simd(OUT(0), level
, inNumSamples
);
702 counter
-= inNumSamples
;
706 else if (counter
== 0 && (curtrig
<= 0.f
|| prevtrig
> 0.f
))
708 nova::zerovec_simd(OUT(0), inNumSamples
);
716 zout
= --counter
? level
: 0.f
;
718 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
719 counter
= (long)(dur
* sr
+ .5f
);
720 if (counter
< 1) counter
= 1;
731 unit
->m_prevtrig
= prevtrig
;
732 unit
->mCounter
= counter
;
733 unit
->mLevel
= level
;
737 ////////////////////////////////////////////////////////////////////////////////////////////////////////
739 void SendTrig_Ctor(SendTrig
*unit
)
741 if (INRATE(2) == calc_FullRate
) {
742 SETCALC(SendTrig_next_aka
);
744 SETCALC(SendTrig_next
);
746 unit
->m_prevtrig
= 0.f
;
749 void SendTrig_next(SendTrig
*unit
, int inNumSamples
)
751 float *trig
= ZIN(0);
752 float prevtrig
= unit
->m_prevtrig
;
755 float curtrig
= ZXP(trig
);
756 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
757 SendTrigger(&unit
->mParent
->mNode
, (int)ZIN0(1), ZIN0(2));
761 unit
->m_prevtrig
= prevtrig
;
764 void SendTrig_next_aka(SendTrig
*unit
, int inNumSamples
)
766 float *trig
= ZIN(0);
767 float *value
= ZIN(2);
768 float prevtrig
= unit
->m_prevtrig
;
771 float curtrig
= ZXP(trig
);
772 float curval
= ZXP(value
);
773 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
774 SendTrigger(&unit
->mParent
->mNode
, (int)ZIN0(1), curval
);
778 unit
->m_prevtrig
= prevtrig
;
782 ////////////////////////////////////////////////////////////////////////////////////////////////////////
784 void SendReply_Ctor(SendReply
*unit
)
786 const int kVarOffset
= 3;
788 unit
->m_prevtrig
= 0.f
;
789 unit
->m_cmdNameSize
= IN0(2);
790 unit
->m_valueSize
= unit
->mNumInputs
- unit
->m_cmdNameSize
- kVarOffset
;
793 unit
->m_cmdName
= (char*)RTAlloc(unit
->mWorld
, (unit
->m_cmdNameSize
+ 1) * sizeof(char));
794 for(int i
= 0; i
< (int)unit
->m_cmdNameSize
; i
++){
795 unit
->m_cmdName
[i
] = (char)IN0(kVarOffset
+i
);
798 unit
->m_cmdName
[unit
->m_cmdNameSize
] = 0;
800 unit
->m_valueOffset
= kVarOffset
+ unit
->m_cmdNameSize
;
801 unit
->m_values
= (float*)RTAlloc(unit
->mWorld
, unit
->m_valueSize
* sizeof(float));
803 if (INRATE(0) == calc_FullRate
) {
804 SETCALC(SendReply_next_aka
);
806 SETCALC(SendReply_next
);
810 void SendReply_Dtor(SendReply
* unit
)
812 RTFree(unit
->mWorld
, unit
->m_values
);
813 RTFree(unit
->mWorld
, unit
->m_cmdName
);
817 void SendReply_next(SendReply
*unit
, int inNumSamples
)
820 float prevtrig
= unit
->m_prevtrig
;
821 float *values
= unit
->m_values
;
822 int valueSize
= unit
->m_valueSize
;
823 int valueOffset
= unit
->m_valueOffset
;
824 for(int j
= 0; j
< inNumSamples
; j
++) {
825 float curtrig
= trig
[j
];
826 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
827 for(int i
=0; i
<valueSize
; i
++) {
828 values
[i
] = IN(i
+ valueOffset
)[0];
830 SendNodeReply(&unit
->mParent
->mNode
, (int)ZIN0(1), unit
->m_cmdName
, unit
->m_valueSize
, values
);
834 unit
->m_prevtrig
= prevtrig
;
837 void SendReply_next_aka(SendReply
*unit
, int inNumSamples
)
840 float prevtrig
= unit
->m_prevtrig
;
841 float *invalues
= IN(unit
->m_valueOffset
);
842 float *values
= unit
->m_values
;
843 int valueSize
= unit
->m_valueSize
;
844 int valueOffset
= unit
->m_valueOffset
;
845 for(int j
= 0; j
< inNumSamples
; j
++) {
846 float curtrig
= trig
[j
];
847 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
848 for(int i
=0; i
<valueSize
; i
++) {
849 int offset
= INRATE( i
+ valueOffset
) != calc_FullRate
? 0 : j
;
850 values
[i
] = IN(i
+ valueOffset
)[offset
];
852 SendNodeReply(&unit
->mParent
->mNode
, (int)ZIN0(1), unit
->m_cmdName
, unit
->m_valueSize
, values
);
856 unit
->m_prevtrig
= prevtrig
;
860 ////////////////////////////////////////////////////////////////////////////////////////////////////////
862 void Poll_Ctor(Poll
* unit
)
864 if (INRATE(0) == calc_FullRate
){
865 if (INRATE(1) == calc_FullRate
){
866 SETCALC(Poll_next_aa
);
868 SETCALC(Poll_next_ak
);
871 SETCALC(Poll_next_kk
);
874 unit
->m_trig
= IN0(0);
875 unit
->m_id
= IN0(3); // number of chars in the id string
876 unit
->m_id_string
= (char*)RTAlloc(unit
->mWorld
, ((int)unit
->m_id
+ 1) * sizeof(char));
877 for(int i
= 0; i
< (int)unit
->m_id
; i
++){
878 unit
->m_id_string
[i
] = (char)IN0(4+i
);
880 unit
->m_id_string
[(int)unit
->m_id
] = '\0';
882 unit
->m_mayprint
= unit
->mWorld
->mVerbosity
>= 0;
884 Poll_next_kk(unit
, 1);
887 void Poll_Dtor(Poll
* unit
)
889 RTFree(unit
->mWorld
, unit
->m_id_string
);
892 void Poll_next_aa(Poll
*unit
, int inNumSamples
){
895 float lasttrig
= unit
->m_trig
;
896 for(int i
= 0; i
< inNumSamples
; i
++){
897 if((lasttrig
<= 0.0) && (trig
[i
] > 0.0)){
898 if(unit
->m_mayprint
){
899 Print("%s: %g\n", unit
->m_id_string
, in
[i
]);
901 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
[i
]);
905 unit
->m_trig
= lasttrig
;
908 void Poll_next_kk(Poll
*unit
, int inNumSamples
){
911 if((unit
->m_trig
<= 0.0) && (trig
> 0.0)){
912 if(unit
->m_mayprint
){
913 Print("%s: %g\n", unit
->m_id_string
, in
);
915 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
);
921 void Poll_next_ak(Poll
*unit
, int inNumSamples
){
924 float lasttrig
= unit
->m_trig
;
925 for(int i
= 0; i
< inNumSamples
; i
++){
926 if((lasttrig
<= 0.0) && (trig
[i
] > 0.0)){
927 if(unit
->m_mayprint
){
928 Print("%s: %g\n", unit
->m_id_string
, in
);
930 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
);
934 unit
->m_trig
= lasttrig
;
939 ////////////////////////////////////////////////////////////////////////////////////////////////////////
941 void SetResetFF_Ctor(SetResetFF
*unit
)
943 if (INRATE(1) == calc_FullRate
) {
944 SETCALC(SetResetFF_next_a
);
946 SETCALC(SetResetFF_next_k
);
949 unit
->m_prevtrig
= 0.f
;
950 unit
->m_prevreset
= 0.f
;
957 void SetResetFF_next_a(SetResetFF
*unit
, int inNumSamples
)
959 float *out
= ZOUT(0);
960 float *trig
= ZIN(0);
961 float *reset
= ZIN(1);
962 float prevtrig
= unit
->m_prevtrig
;
963 float prevreset
= unit
->m_prevreset
;
964 float level
= unit
->mLevel
;
967 float curtrig
= ZXP(trig
);
968 float curreset
= ZXP(reset
);
969 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
970 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
973 prevreset
= curreset
;
975 unit
->m_prevtrig
= prevtrig
;
976 unit
->m_prevreset
= prevreset
;
977 unit
->mLevel
= level
;
980 void SetResetFF_next_k(SetResetFF
*unit
, int inNumSamples
)
982 float *out
= ZOUT(0);
983 float *trig
= ZIN(0);
984 float curreset
= ZIN0(1);
985 float prevtrig
= unit
->m_prevtrig
;
986 float prevreset
= unit
->m_prevreset
;
987 float level
= unit
->mLevel
;
989 float curtrig
= ZXP(trig
);
990 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
991 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
995 LOOP(inNumSamples
- 1,
997 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
1001 unit
->m_prevtrig
= prevtrig
;
1002 unit
->m_prevreset
= curreset
;
1003 unit
->mLevel
= level
;
1006 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1009 void ToggleFF_Ctor(ToggleFF
*unit
)
1011 SETCALC(ToggleFF_next
);
1013 unit
->m_prevtrig
= 0.f
;
1020 void ToggleFF_next(ToggleFF
*unit
, int inNumSamples
)
1022 float *out
= ZOUT(0);
1023 float *trig
= ZIN(0);
1024 float prevtrig
= unit
->m_prevtrig
;
1025 float level
= unit
->mLevel
;
1028 float curtrig
= ZXP(trig
);
1029 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
- level
;
1033 unit
->m_prevtrig
= prevtrig
;
1034 unit
->mLevel
= level
;
1037 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1041 void Latch_next_ak_nova(Latch
*unit
, int inNumSamples
)
1043 float level
= unit
->mLevel
;
1045 float curtrig
= ZIN0(1);
1046 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1048 nova::setvec_simd(OUT(0), level
, inNumSamples
);
1050 unit
->m_prevtrig
= curtrig
;
1051 unit
->mLevel
= level
;
1054 void Latch_next_ak_nova_64(Latch
*unit
, int inNumSamples
)
1056 float level
= unit
->mLevel
;
1058 float curtrig
= ZIN0(1);
1059 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1061 nova::setvec_simd
<64>(OUT(0), level
);
1063 unit
->m_prevtrig
= curtrig
;
1064 unit
->mLevel
= level
;
1068 void Latch_Ctor(Latch
*unit
)
1070 if (INRATE(1) == calc_FullRate
) {
1071 SETCALC(Latch_next_aa
);
1074 if (BUFLENGTH
== 64)
1075 SETCALC(Latch_next_ak_nova_64
);
1076 if (!(BUFLENGTH
& 15))
1077 SETCALC(Latch_next_ak_nova
);
1080 SETCALC(Latch_next_ak
);
1083 unit
->m_prevtrig
= 0.f
;
1086 ZOUT0(0) = ZIN0(1) > 0.f
? ZIN0(0) : 0.f
;
1090 void Latch_next_ak(Latch
*unit
, int inNumSamples
)
1092 float *out
= ZOUT(0);
1093 float level
= unit
->mLevel
;
1095 float curtrig
= ZIN0(1);
1096 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1098 LOOP1(inNumSamples
, ZXP(out
) = level
; );
1100 unit
->m_prevtrig
= curtrig
;
1101 unit
->mLevel
= level
;
1105 void Latch_next_aa(Latch
*unit
, int inNumSamples
)
1107 float *out
= ZOUT(0);
1109 float *trig
= ZIN(1);
1110 float prevtrig
= unit
->m_prevtrig
;
1111 float level
= unit
->mLevel
;
1114 float curtrig
= ZXP(trig
);
1115 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZXP(in
);
1120 unit
->m_prevtrig
= prevtrig
;
1121 unit
->mLevel
= level
;
1126 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1129 FLATTEN
void Gate_next_ak_nova(Gate
*unit
, int inNumSamples
)
1131 float *trig
= ZIN(1);
1132 float level
= unit
->mLevel
;
1134 float curtrig
= ZXP(trig
);
1135 if (curtrig
> 0.f
) {
1136 nova::copyvec_simd(OUT(0), IN(0), inNumSamples
);
1137 unit
->mLevel
= IN(0)[inNumSamples
-1];
1139 nova::setvec_simd(OUT(0), level
, inNumSamples
);
1142 FLATTEN
void Gate_next_ak_nova_64(Gate
*unit
, int inNumSamples
)
1144 float *trig
= ZIN(1);
1145 float level
= unit
->mLevel
;
1147 float curtrig
= ZXP(trig
);
1148 if (curtrig
> 0.f
) {
1149 nova::copyvec_simd
<64>(OUT(0), IN(0));
1150 unit
->mLevel
= IN(0)[inNumSamples
-1];
1152 nova::setvec_simd
<64>(OUT(0), level
);
1156 void Gate_Ctor(Gate
*unit
)
1158 if (INRATE(1) == calc_FullRate
) {
1159 SETCALC(Gate_next_aa
);
1162 if (BUFLENGTH
== 64)
1163 SETCALC(Gate_next_ak_nova_64
);
1164 if (!(BUFLENGTH
& 15))
1165 SETCALC(Gate_next_ak_nova
);
1168 SETCALC(Gate_next_ak
);
1178 void Gate_next_ak(Gate
*unit
, int inNumSamples
)
1180 float *out
= ZOUT(0);
1182 float *trig
= ZIN(1);
1183 float level
= unit
->mLevel
;
1185 float curtrig
= ZXP(trig
);
1186 if (curtrig
> 0.f
) {
1191 unit
->mLevel
= level
;
1199 void Gate_next_aa(Gate
*unit
, int inNumSamples
)
1201 float *out
= ZOUT(0);
1203 float *trig
= ZIN(1);
1204 float level
= unit
->mLevel
;
1207 float curtrig
= ZXP(trig
);
1208 if (curtrig
> 0.f
) level
= ZXP(in
);
1212 unit
->mLevel
= level
;
1215 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1218 void Schmidt_Ctor(Schmidt
*unit
)
1220 SETCALC(Schmidt_next
);
1227 void Schmidt_next(Schmidt
*unit
, int inNumSamples
)
1229 float *out
= ZOUT(0);
1233 float level
= unit
->mLevel
;
1235 float zin
= ZXP(in
);
1237 if (zin
< lo
) level
= 0.f
;
1239 if (zin
> hi
) level
= 1.f
;
1243 unit
->mLevel
= level
;
1246 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1248 void PulseDivider_Ctor(PulseDivider
*unit
)
1250 SETCALC(PulseDivider_next
);
1252 unit
->m_prevtrig
= 0.f
;
1254 unit
->mCounter
= (long)floor(ZIN0(2) + 0.5);
1260 void PulseDivider_next(PulseDivider
*unit
, int inNumSamples
)
1262 float *out
= ZOUT(0);
1263 float *trig
= ZIN(0);
1264 long div
= (long)ZIN0(1);
1265 float prevtrig
= unit
->m_prevtrig
;
1266 long counter
= unit
->mCounter
;
1270 float curtrig
= ZXP(trig
);
1271 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1273 if (counter
>= div
) {
1285 unit
->mCounter
= counter
;
1286 unit
->m_prevtrig
= prevtrig
;
1289 //////////////////////////////////////////////////////////////////////////////////////////
1292 void PulseCount_Ctor(PulseCount
*unit
)
1294 if (INRATE(1) == calc_FullRate
) {
1295 SETCALC(PulseCount_next_a
);
1296 } else if (INRATE(1) == calc_BufRate
) {
1297 SETCALC(PulseCount_next_k
);
1299 SETCALC(PulseCount_next_0
);
1301 unit
->m_prevtrig
= 0.f
;
1302 unit
->m_prevreset
= 0.f
;
1309 void PulseCount_next_a(PulseCount
*unit
, int inNumSamples
)
1311 float *out
= ZOUT(0);
1312 float *trig
= ZIN(0);
1313 float *reset
= ZIN(1);
1314 float prevtrig
= unit
->m_prevtrig
;
1315 float prevreset
= unit
->m_prevreset
;
1316 float level
= unit
->mLevel
;
1319 float curtrig
= ZXP(trig
);
1320 float curreset
= ZXP(reset
);
1321 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
1322 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1327 prevreset
= curreset
;
1329 unit
->mLevel
= level
;
1330 unit
->m_prevtrig
= prevtrig
;
1331 unit
->m_prevreset
= prevreset
;
1334 void PulseCount_next_k(PulseCount
*unit
, int inNumSamples
)
1336 float *out
= ZOUT(0);
1337 float *trig
= ZIN(0);
1338 float curreset
= ZIN0(1);
1339 float prevtrig
= unit
->m_prevtrig
;
1340 float prevreset
= unit
->m_prevreset
;
1341 float level
= unit
->mLevel
;
1344 float curtrig
= ZXP(trig
);
1345 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
1346 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1351 prevreset
= curreset
;
1353 unit
->mLevel
= level
;
1354 unit
->m_prevtrig
= prevtrig
;
1355 unit
->m_prevreset
= prevreset
;
1358 void PulseCount_next_0(PulseCount
*unit
, int inNumSamples
)
1360 float *out
= ZOUT(0);
1361 float *trig
= ZIN(0);
1362 float prevtrig
= unit
->m_prevtrig
;
1363 float level
= unit
->mLevel
;
1366 float curtrig
= ZXP(trig
);
1367 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1373 unit
->mLevel
= level
;
1374 unit
->m_prevtrig
= prevtrig
;
1377 //////////////////////////////////////////////////////////////////////////////////////////
1380 void Stepper_Ctor(Stepper
*unit
)
1382 if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) == calc_FullRate
&& INRATE(1) == calc_ScalarRate
) {
1383 SETCALC(Stepper_next_a0
);
1384 } else if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) == calc_FullRate
&& INRATE(1) != calc_FullRate
) {
1385 SETCALC(Stepper_next_ak
);
1387 SETCALC(Stepper_next_aa
);
1390 int32 resetval
= (int32
)ZIN0(5);
1392 unit
->m_prevtrig
= 0.f
;
1393 unit
->m_prevreset
= 0.f
;
1394 unit
->mLevel
= (float)resetval
;
1400 void Stepper_next_aa(Stepper
*unit
, int inNumSamples
)
1402 float *out
= ZOUT(0);
1403 float *trig
= ZIN(0);
1404 float *reset
= ZIN(1);
1405 int32 zmin
= (int32
)ZIN0(2);
1406 int32 zmax
= (int32
)ZIN0(3);
1407 int32 step
= (int32
)ZIN0(4);
1408 int32 resetval
= (int32
)ZIN0(5);
1409 float prevtrig
= unit
->m_prevtrig
;
1410 float prevreset
= unit
->m_prevreset
;
1411 float level
= unit
->mLevel
;
1414 float curtrig
= ZXP(trig
);
1415 float curreset
= ZXP(reset
);
1416 if (prevreset
<= 0.f
&& curreset
> 0.f
) {
1417 level
= (float)sc_wrap(resetval
, zmin
, zmax
);
1418 } else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1419 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1423 prevreset
= curreset
;
1425 unit
->mLevel
= level
;
1426 unit
->m_prevtrig
= prevtrig
;
1427 unit
->m_prevreset
= prevreset
;
1430 void Stepper_next_ak(Stepper
*unit
, int inNumSamples
)
1432 float *out
= ZOUT(0);
1433 float *trig
= ZIN(0);
1434 float curreset
= ZIN0(1);
1435 int32 zmin
= (int32
)ZIN0(2);
1436 int32 zmax
= (int32
)ZIN0(3);
1437 int32 step
= (int32
)ZIN0(4);
1438 int32 resetval
= (int32
)ZIN0(5);
1439 float prevtrig
= unit
->m_prevtrig
;
1440 float prevreset
= unit
->m_prevreset
;
1441 float level
= unit
->mLevel
;
1444 float curtrig
= ZXP(trig
);
1445 if (prevreset
<= 0.f
&& curreset
> 0.f
) {
1446 level
= (float)sc_wrap(resetval
, zmin
, zmax
);
1447 } else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1448 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1452 prevreset
= curreset
;
1454 unit
->mLevel
= level
;
1455 unit
->m_prevtrig
= prevtrig
;
1456 unit
->m_prevreset
= prevreset
;
1459 void Stepper_next_a0(Stepper
*unit
, int inNumSamples
)
1461 float *out
= ZOUT(0);
1462 float *trig
= ZIN(0);
1463 int32 zmin
= (int32
)ZIN0(2);
1464 int32 zmax
= (int32
)ZIN0(3);
1465 int32 step
= (int32
)ZIN0(4);
1466 float prevtrig
= unit
->m_prevtrig
;
1467 float level
= unit
->mLevel
;
1470 float curtrig
= ZXP(trig
);
1471 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1472 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1477 unit
->mLevel
= level
;
1478 unit
->m_prevtrig
= prevtrig
;
1481 //////////////////////////////////////////////////////////////////////////////////////////
1483 void TDelay_Ctor(TDelay
*unit
)
1485 SETCALC(TDelay_next
);
1487 unit
->m_prevtrig
= 0.f
;
1495 void TDelay_next(TDelay
*unit
, int inNumSamples
)
1497 float *out
= ZOUT(0);
1498 float *trig
= ZIN(0);
1499 float dur
= ZIN0(1);
1500 float prevtrig
= unit
->m_prevtrig
;
1501 long counter
= unit
->mCounter
;
1504 float curtrig
= ZXP(trig
);
1509 } else if (counter
<=0) {
1510 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1511 counter
= (long)(dur
* unit
->mRate
->mSampleRate
+ .5f
);
1512 if (counter
< 1) counter
= 1;
1522 unit
->m_prevtrig
= prevtrig
;
1523 unit
->mCounter
= counter
;
1527 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1530 void ZeroCrossing_Ctor(ZeroCrossing
*unit
)
1532 SETCALC(ZeroCrossing_next_a
);
1534 unit
->m_prevfrac
= 0.f
;
1535 unit
->m_previn
= ZIN0(0);
1536 ZOUT0(0) = unit
->mLevel
= 0.f
;
1540 void ZeroCrossing_next_a(ZeroCrossing
*unit
, int inNumSamples
)
1542 float *out
= ZOUT(0);
1544 float previn
= unit
->m_previn
;
1545 float prevfrac
= unit
->m_prevfrac
;
1546 float level
= unit
->mLevel
;
1547 long counter
= unit
->mCounter
;
1551 float curin
= ZXP(in
);
1552 if (counter
> 4 && previn
<= 0.f
&& curin
> 0.f
) {
1553 float frac
= -previn
/(curin
-previn
);
1554 level
= unit
->mRate
->mSampleRate
/ (frac
+ counter
- prevfrac
);
1562 unit
->m_previn
= previn
;
1563 unit
->m_prevfrac
= prevfrac
;
1564 unit
->mLevel
= level
;
1565 unit
->mCounter
= counter
;
1568 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1570 void Timer_Ctor(Timer
*unit
)
1572 SETCALC(Timer_next_a
);
1574 unit
->m_prevfrac
= 0.f
;
1575 unit
->m_previn
= ZIN0(0);
1576 ZOUT0(0) = unit
->mLevel
= 0.f
;
1580 void Timer_next_a(Timer
*unit
, int inNumSamples
)
1582 float *out
= ZOUT(0);
1584 float previn
= unit
->m_previn
;
1585 float prevfrac
= unit
->m_prevfrac
;
1586 float level
= unit
->mLevel
;
1587 long counter
= unit
->mCounter
;
1591 float curin
= ZXP(in
);
1592 if (previn
<= 0.f
&& curin
> 0.f
) {
1593 float frac
= -previn
/(curin
-previn
);
1594 level
= unit
->mRate
->mSampleDur
* (frac
+ counter
- prevfrac
);
1602 unit
->m_previn
= previn
;
1603 unit
->m_prevfrac
= prevfrac
;
1604 unit
->mLevel
= level
;
1605 unit
->mCounter
= counter
;
1609 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1611 void Sweep_Ctor(Sweep
*unit
)
1613 if (INRATE(0) == calc_ScalarRate
) {
1614 if (INRATE(1) == calc_FullRate
) {
1615 SETCALC(Sweep_next_0a
);
1617 SETCALC(Sweep_next_0k
);
1619 } else if (INRATE(0) == calc_BufRate
) {
1620 if (INRATE(1) == calc_FullRate
) {
1621 SETCALC(Sweep_next_ka
);
1623 SETCALC(Sweep_next_kk
);
1626 if (INRATE(1) == calc_FullRate
) {
1627 SETCALC(Sweep_next_aa
);
1629 SETCALC(Sweep_next_ak
);
1633 unit
->m_previn
= ZIN0(0);
1634 ZOUT0(0) = unit
->mLevel
= 0.f
;
1637 void Sweep_next_0k(Sweep
*unit
, int inNumSamples
)
1639 float *out
= ZOUT(0);
1640 double rate
= ZIN0(1) * SAMPLEDUR
;
1641 double level
= unit
->mLevel
;
1648 unit
->mLevel
= level
;
1651 void Sweep_next_0a(Sweep
*unit
, int inNumSamples
)
1653 float *out
= ZOUT(0);
1654 float *rate
= ZIN(1);
1655 double level
= unit
->mLevel
;
1656 float sampledur
= SAMPLEDUR
;
1659 float zrate
= ZXP(rate
) * sampledur
;
1664 unit
->mLevel
= level
;
1667 void Sweep_next_kk(Sweep
*unit
, int inNumSamples
)
1669 float *out
= ZOUT(0);
1670 float curin
= ZIN0(0);
1671 double rate
= ZIN0(1) * SAMPLEDUR
;
1672 float previn
= unit
->m_previn
;
1673 double level
= unit
->mLevel
;
1675 if (previn
<= 0.f
&& curin
> 0.f
) {
1676 float frac
= -previn
/(curin
-previn
);
1677 level
= frac
* rate
;
1685 unit
->m_previn
= curin
;
1686 unit
->mLevel
= level
;
1689 void Sweep_next_ka(Sweep
*unit
, int inNumSamples
)
1691 float *out
= ZOUT(0);
1692 float curin
= ZIN0(0);
1693 float *rate
= ZIN(1);
1694 float previn
= unit
->m_previn
;
1695 double level
= unit
->mLevel
;
1696 float sampledur
= SAMPLEDUR
;
1698 if (previn
<= 0.f
&& curin
> 0.f
) {
1699 float frac
= -previn
/(curin
-previn
);
1700 level
= frac
* rate
[ZOFF
] * sampledur
;
1704 float zrate
= ZXP(rate
) * sampledur
;
1709 unit
->m_previn
= curin
;
1710 unit
->mLevel
= level
;
1713 void Sweep_next_ak(Sweep
*unit
, int inNumSamples
)
1715 float *out
= ZOUT(0);
1717 double rate
= ZIN0(1) * SAMPLEDUR
;
1718 float previn
= unit
->m_previn
;
1719 double level
= unit
->mLevel
;
1722 float curin
= ZXP(in
);
1723 if (previn
<= 0.f
&& curin
> 0.f
) {
1724 float frac
= -previn
/(curin
-previn
);
1725 level
= frac
* rate
;
1733 unit
->m_previn
= previn
;
1734 unit
->mLevel
= level
;
1737 void Sweep_next_aa(Sweep
*unit
, int inNumSamples
)
1739 float *out
= ZOUT(0);
1741 float *rate
= ZIN(1);
1742 float previn
= unit
->m_previn
;
1743 double level
= unit
->mLevel
;
1744 float sampledur
= SAMPLEDUR
;
1747 float curin
= ZXP(in
);
1748 float zrate
= ZXP(rate
) * sampledur
;
1749 if (previn
<= 0.f
&& curin
> 0.f
) {
1750 float frac
= -previn
/(curin
-previn
);
1751 level
= frac
* zrate
;
1759 unit
->m_previn
= previn
;
1760 unit
->mLevel
= level
;
1763 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1765 void Phasor_Ctor(Phasor
*unit
)
1767 if (unit
->mCalcRate
== calc_FullRate
) {
1768 if (INRATE(0) == calc_FullRate
) {
1769 if (INRATE(1) == calc_FullRate
) {
1770 SETCALC(Phasor_next_aa
);
1772 SETCALC(Phasor_next_ak
);
1775 SETCALC(Phasor_next_kk
);
1778 SETCALC(Phasor_next_ak
);
1781 unit
->m_previn
= ZIN0(0);
1782 ZOUT0(0) = unit
->mLevel
= ZIN0(2);
1785 void Phasor_next_kk(Phasor
*unit
, int inNumSamples
)
1787 float *out
= ZOUT(0);
1790 double rate
= ZIN0(1);
1791 double start
= ZIN0(2);
1792 double end
= ZIN0(3);
1793 float resetPos
= ZIN0(4);
1795 float previn
= unit
->m_previn
;
1796 double level
= unit
->mLevel
;
1798 if (previn
<= 0.f
&& in
> 0.f
) {
1802 level
= sc_wrap(level
, start
, end
);
1807 unit
->m_previn
= in
;
1808 unit
->mLevel
= level
;
1811 void Phasor_next_ak(Phasor
*unit
, int inNumSamples
)
1813 float *out
= ZOUT(0);
1816 double rate
= ZIN0(1);
1817 double start
= ZIN0(2);
1818 double end
= ZIN0(3);
1819 float resetPos
= ZIN0(4);
1821 float previn
= unit
->m_previn
;
1822 double level
= unit
->mLevel
;
1825 float curin
= ZXP(in
);
1826 if (previn
<= 0.f
&& curin
> 0.f
) {
1827 float frac
= 1.f
- previn
/(curin
-previn
);
1828 level
= resetPos
+ frac
* rate
;
1832 level
= sc_wrap(level
, start
, end
);
1837 unit
->m_previn
= previn
;
1838 unit
->mLevel
= level
;
1841 void Phasor_next_aa(Phasor
*unit
, int inNumSamples
)
1843 float *out
= ZOUT(0);
1845 float *rate
= ZIN(1);
1846 double start
= ZIN0(2);
1847 double end
= ZIN0(3);
1848 float resetPos
= ZIN0(4);
1850 float previn
= unit
->m_previn
;
1851 double level
= unit
->mLevel
;
1854 float curin
= ZXP(in
);
1855 double zrate
= ZXP(rate
);
1856 if (previn
<= 0.f
&& curin
> 0.f
) {
1857 float frac
= 1.f
- previn
/(curin
-previn
);
1858 level
= resetPos
+ frac
* zrate
;
1862 level
= sc_wrap(level
, start
, end
);
1866 unit
->m_previn
= previn
;
1867 unit
->mLevel
= level
;
1870 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1873 void Peak_next_ak_unroll(Peak
*unit
, int inNumSamples
);
1874 void Peak_next_ai_unroll(Peak
*unit
, int inNumSamples
);
1877 FLATTEN
void Peak_next_ak_k_nova(Peak
*unit
, int inNumSamples
);
1878 FLATTEN
void Peak_next_ai_k_nova(Peak
*unit
, int inNumSamples
);
1881 void Peak_Ctor(Peak
*unit
)
1883 if (BUFLENGTH
== 1 &&
1884 INRATE(0) == calc_FullRate
) {
1885 /* audio-rate input with control-rate output */
1886 if (INRATE(1) == calc_FullRate
) {
1887 SETCALC(Peak_next_aa_k
);
1888 } else if (INRATE(1) == calc_ScalarRate
) {
1890 if (INBUFLENGTH(0) & 7)
1891 SETCALC(Peak_next_ai_k
);
1893 SETCALC(Peak_next_ai_k_nova
);
1895 SETCALC(Peak_next_ai_k
);
1899 if (INBUFLENGTH(0) & 7)
1900 SETCALC(Peak_next_ak_k
);
1902 SETCALC(Peak_next_ak_k_nova
);
1904 SETCALC(Peak_next_ak_k
);
1908 if (INRATE(1) == calc_FullRate
) {
1909 SETCALC(Peak_next_aa
);
1910 } else if (INRATE(1) == calc_ScalarRate
) {
1912 SETCALC(Peak_next_ai
);
1914 SETCALC(Peak_next_ai_unroll
);
1917 SETCALC(Peak_next_ak
);
1919 SETCALC(Peak_next_ak_unroll
);
1922 unit
->m_prevtrig
= 0.f
;
1923 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
1926 void Peak_next_ak(Peak
*unit
, int inNumSamples
)
1928 float *out
= ZOUT(0);
1930 float curtrig
= ZIN0(1);
1931 float level
= unit
->mLevel
;
1934 inlevel
= std::abs(ZXP(in
));
1935 level
= std::max(inlevel
, level
);
1938 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
1939 unit
->m_prevtrig
= curtrig
;
1940 unit
->mLevel
= level
;
1943 void Peak_next_ai(Peak
*unit
, int inNumSamples
)
1945 float *out
= ZOUT(0);
1947 float level
= unit
->mLevel
;
1950 inlevel
= std::abs(ZXP(in
));
1951 level
= std::max(inlevel
, level
);
1954 unit
->mLevel
= level
;
1957 void Peak_next_aa(Peak
*unit
, int inNumSamples
)
1959 float *out
= ZOUT(0);
1961 float *trig
= ZIN(1);
1962 float prevtrig
= unit
->m_prevtrig
;
1963 float level
= unit
->mLevel
;
1966 float curtrig
= ZXP(trig
);
1967 float inlevel
= std::abs(ZXP(in
));
1968 level
= std::max(inlevel
, level
);
1970 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
1973 unit
->m_prevtrig
= prevtrig
;
1974 unit
->mLevel
= level
;
1977 static inline float Peak_unroll_body(Peak
*unit
, int inNumSamples
, float & level
)
1979 float *out
= OUT(0);
1983 using namespace std
;
1984 for (int i
= 0; i
!= inNumSamples
; i
+= 8, out
+= 8, in
+= 8)
1986 float level0
= max(abs(in
[0]), level
);
1987 float level1
= max(abs(in
[1]), level0
);
1988 float level2
= max(abs(in
[2]), level1
);
1989 float level3
= max(abs(in
[3]), level2
);
1990 float level4
= max(abs(in
[4]), level3
);
1991 float level5
= max(abs(in
[5]), level4
);
1992 float level6
= max(abs(in
[6]), level5
);
1993 inlevel
= abs(in
[7]);
1994 float level7
= max(inlevel
, level6
);
2005 return inlevel
; /* input level of the last sample */
2008 void Peak_next_ak_unroll(Peak
*unit
, int inNumSamples
)
2010 float curtrig
= ZIN0(1);
2011 float level
= unit
->mLevel
;
2012 float inlevel
= Peak_unroll_body(unit
, inNumSamples
, level
);
2014 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2015 unit
->m_prevtrig
= curtrig
;
2016 unit
->mLevel
= level
;
2019 void Peak_next_ai_unroll(Peak
*unit
, int inNumSamples
)
2021 float level
= unit
->mLevel
;
2022 Peak_unroll_body(unit
, inNumSamples
, level
);
2023 unit
->mLevel
= level
;
2026 void Peak_next_ak_k(Peak
*unit
, int inNumSamples
)
2028 float *out
= ZOUT(0);
2030 float curtrig
= ZIN0(1);
2032 inNumSamples
= INBUFLENGTH(0);
2033 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) {
2034 level
= std::abs(ZXP(in
));
2038 level
= unit
->mLevel
;
2040 level
= std::max(std::abs(ZXP(in
)), level
);
2043 unit
->m_prevtrig
= curtrig
;
2044 unit
->mLevel
= level
;
2047 void Peak_next_ai_k(Peak
*unit
, int inNumSamples
)
2049 float *out
= ZOUT(0);
2051 float level
= unit
->mLevel
;
2052 inNumSamples
= INBUFLENGTH(0);
2054 level
= std::max(std::abs(ZXP(in
)), level
);
2057 unit
->mLevel
= level
;
2060 void Peak_next_aa_k(Peak
*unit
, int inNumSamples
)
2062 float *out
= ZOUT(0);
2064 float *trig
= ZIN(1);
2065 float prevtrig
= unit
->m_prevtrig
;
2066 float level
= unit
->mLevel
;
2068 bool triggered
= false;
2070 inNumSamples
= INBUFLENGTH(0);
2072 float curtrig
= ZXP(trig
);
2073 float inlevel
= std::abs(ZXP(in
));
2074 level
= std::max(inlevel
, level
);
2075 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
2083 ZXP(out
) = out_level
;
2086 unit
->m_prevtrig
= prevtrig
;
2087 unit
->mLevel
= level
;
2091 void Peak_next_ak_k_nova(Peak
*unit
, int inNumSamples
)
2093 float *out
= ZOUT(0);
2095 float curtrig
= ZIN0(1);
2096 float level
= unit
->mLevel
;
2098 inNumSamples
= INBUFLENGTH(0);
2099 inlevel
= nova::peak_vec_simd(in
, &level
, inNumSamples
);
2101 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2102 unit
->m_prevtrig
= curtrig
;
2103 unit
->mLevel
= level
;
2106 void Peak_next_ai_k_nova(Peak
*unit
, int inNumSamples
)
2108 float *out
= ZOUT(0);
2110 float level
= unit
->mLevel
;
2112 inNumSamples
= INBUFLENGTH(0);
2113 inlevel
= nova::peak_vec_simd(in
, &level
, inNumSamples
);
2115 unit
->mLevel
= level
;
2119 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2121 void RunningMin_Ctor(RunningMin
*unit
)
2123 if (INRATE(1) == calc_FullRate
) {
2124 SETCALC(RunningMin_next_aa
);
2125 } else if (INRATE(1) == calc_ScalarRate
) {
2126 SETCALC(RunningMin_next_ai
);
2128 SETCALC(RunningMin_next_ak
);
2130 unit
->m_prevtrig
= 0.f
;
2131 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2134 void RunningMin_next_ak(RunningMin
*unit
, int inNumSamples
)
2136 float *out
= ZOUT(0);
2138 float curtrig
= ZIN0(1);
2139 float level
= unit
->mLevel
;
2143 if (inlevel
< level
) level
= inlevel
;
2146 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2147 unit
->m_prevtrig
= curtrig
;
2148 unit
->mLevel
= level
;
2151 void RunningMin_next_ai(RunningMin
*unit
, int inNumSamples
)
2153 float *out
= ZOUT(0);
2155 float level
= unit
->mLevel
;
2159 if (inlevel
< level
) level
= inlevel
;
2162 unit
->mLevel
= level
;
2165 void RunningMin_next_aa(RunningMin
*unit
, int inNumSamples
)
2167 float *out
= ZOUT(0);
2169 float *trig
= ZIN(1);
2170 float prevtrig
= unit
->m_prevtrig
;
2171 float level
= unit
->mLevel
;
2174 float curtrig
= ZXP(trig
);
2175 float inlevel
= ZXP(in
);
2176 if (inlevel
< level
) level
= inlevel
;
2178 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2181 unit
->m_prevtrig
= prevtrig
;
2182 unit
->mLevel
= level
;
2186 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2187 void RunningMax_Ctor(RunningMax
*unit
)
2189 if (INRATE(1) == calc_FullRate
) {
2190 SETCALC(RunningMax_next_aa
);
2191 } else if (INRATE(1) == calc_ScalarRate
) {
2192 SETCALC(RunningMax_next_ai
);
2194 SETCALC(RunningMax_next_ak
);
2196 unit
->m_prevtrig
= 0.f
;
2197 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2200 void RunningMax_next_ak(RunningMax
*unit
, int inNumSamples
)
2202 float *out
= ZOUT(0);
2204 float curtrig
= ZIN0(1);
2205 float level
= unit
->mLevel
;
2209 if (inlevel
> level
) level
= inlevel
;
2212 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2213 unit
->m_prevtrig
= curtrig
;
2214 unit
->mLevel
= level
;
2217 void RunningMax_next_ai(RunningMax
*unit
, int inNumSamples
)
2219 float *out
= ZOUT(0);
2221 float level
= unit
->mLevel
;
2225 if (inlevel
> level
) level
= inlevel
;
2228 unit
->mLevel
= level
;
2231 void RunningMax_next_aa(RunningMax
*unit
, int inNumSamples
)
2233 float *out
= ZOUT(0);
2235 float *trig
= ZIN(1);
2236 float prevtrig
= unit
->m_prevtrig
;
2237 float level
= unit
->mLevel
;
2240 float curtrig
= ZXP(trig
);
2241 float inlevel
= ZXP(in
);
2242 if (inlevel
> level
) level
= inlevel
;
2244 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2247 unit
->m_prevtrig
= prevtrig
;
2248 unit
->mLevel
= level
;
2252 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2255 void PeakFollower_Ctor(PeakFollower
*unit
)
2258 if (INRATE(1) == calc_ScalarRate
) {
2259 SETCALC(PeakFollower_next_ai
);
2261 SETCALC(PeakFollower_next
);
2264 unit
->mDecay
= ZIN0(1);
2265 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2268 void PeakFollower_next(PeakFollower
*unit
, int inNumSamples
)
2270 float *out
= ZOUT(0);
2272 float decay
= ZIN0(1);
2273 float level
= unit
->mLevel
;
2276 if(decay
== unit
->mDecay
) {
2278 float inlevel
= std::abs(ZXP(in
));
2279 if (inlevel
>= level
) {
2282 level
= inlevel
+ decay
* (level
- inlevel
);
2289 float decay_slope
= CALCSLOPE(decay
, unit
->mDecay
);
2290 if (decay
>= 0.f
&& unit
->mDecay
>= 0.f
) {
2292 float inlevel
= std::abs(ZXP(in
));
2293 if (inlevel
>= level
) {
2296 level
= inlevel
+ decay
* (level
- inlevel
);
2297 decay
+= decay_slope
;
2301 } else if (decay
<= 0.f
&& unit
->mDecay
<= 0.f
) {
2303 float inlevel
= std::abs(ZXP(in
));
2304 if (inlevel
>= level
) {
2307 level
= inlevel
+ decay
* (level
+ inlevel
);
2308 decay
+= decay_slope
;
2314 float inlevel
= std::abs(ZXP(in
));
2315 if (inlevel
>= level
) {
2318 level
= (1.f
- std::abs(decay
)) * inlevel
+ decay
* level
;
2319 decay
+= decay_slope
;
2326 unit
->mLevel
= level
;
2327 unit
->mDecay
= decay
;
2331 void PeakFollower_next_ai(PeakFollower
*unit
, int inNumSamples
)
2333 float *out
= ZOUT(0);
2335 float decay
= ZIN0(1);
2336 float level
= unit
->mLevel
;
2339 float inlevel
= std::abs(ZXP(in
));
2340 if (inlevel
>= level
) {
2343 level
= inlevel
+ decay
* (level
- inlevel
);
2348 unit
->mLevel
= level
;
2352 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2355 void MostChange_Ctor(MostChange
*unit
)
2357 if (INRATE(0) == calc_FullRate
) {
2358 if (INRATE(1) == calc_FullRate
) {
2359 SETCALC(MostChange_next_aa
);
2361 SETCALC(MostChange_next_ak
);
2364 if (INRATE(1) == calc_FullRate
) {
2365 SETCALC(MostChange_next_ka
);
2367 SETCALC(MostChange_next_aa
);
2373 MostChange_next_aa(unit
, 1);
2376 void MostChange_next_ak(MostChange
*unit
, int inNumSamples
)
2378 float *out
= ZOUT(0);
2381 float prevA
= unit
->mPrevA
;
2382 float prevB
= unit
->mPrevB
;
2383 int recent
= unit
->mRecent
;
2386 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2390 } else if (diff
< 0.f
) {
2394 ZXP(out
) = recent
? xb
: xa
;
2399 unit
->mPrevA
= prevA
;
2400 unit
->mPrevB
= prevB
;
2401 unit
->mRecent
= recent
;
2404 void MostChange_next_aa(MostChange
*unit
, int inNumSamples
)
2406 float *out
= ZOUT(0);
2409 float prevA
= unit
->mPrevA
;
2410 float prevB
= unit
->mPrevB
;
2411 int recent
= unit
->mRecent
;
2415 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2419 } else if (diff
< 0.f
) {
2423 ZXP(out
) = recent
? xb
: xa
;
2428 unit
->mPrevA
= prevA
;
2429 unit
->mPrevB
= prevB
;
2430 unit
->mRecent
= recent
;
2433 void MostChange_next_ka(MostChange
*unit
, int inNumSamples
)
2435 float *out
= ZOUT(0);
2438 float prevA
= unit
->mPrevA
;
2439 float prevB
= unit
->mPrevB
;
2440 int recent
= unit
->mRecent
;
2443 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2447 } else if (diff
< 0.f
) {
2451 ZXP(out
) = recent
? xb
: xa
;
2456 unit
->mPrevA
= prevA
;
2457 unit
->mPrevB
= prevB
;
2458 unit
->mRecent
= recent
;
2462 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2464 void LeastChange_Ctor(LeastChange
*unit
)
2466 if (INRATE(0) == calc_FullRate
) {
2467 if (INRATE(1) == calc_FullRate
) {
2468 SETCALC(LeastChange_next_aa
);
2470 SETCALC(LeastChange_next_ak
);
2473 if (INRATE(1) == calc_FullRate
) {
2474 SETCALC(LeastChange_next_ka
);
2476 SETCALC(LeastChange_next_aa
);
2482 LeastChange_next_aa(unit
, 1);
2485 void LeastChange_next_ak(LeastChange
*unit
, int inNumSamples
)
2487 float *out
= ZOUT(0);
2490 float prevA
= unit
->mPrevA
;
2491 float prevB
= unit
->mPrevB
;
2492 int recent
= unit
->mRecent
;
2495 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2499 } else if (diff
> 0.f
) {
2503 ZXP(out
) = recent
? xb
: xa
;
2508 unit
->mPrevA
= prevA
;
2509 unit
->mPrevB
= prevB
;
2510 unit
->mRecent
= recent
;
2513 void LeastChange_next_aa(LeastChange
*unit
, int inNumSamples
)
2515 float *out
= ZOUT(0);
2518 float prevA
= unit
->mPrevA
;
2519 float prevB
= unit
->mPrevB
;
2520 int recent
= unit
->mRecent
;
2524 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2528 } else if (diff
> 0.f
) {
2532 ZXP(out
) = recent
? xb
: xa
;
2537 unit
->mPrevA
= prevA
;
2538 unit
->mPrevB
= prevB
;
2539 unit
->mRecent
= recent
;
2542 void LeastChange_next_ka(LeastChange
*unit
, int inNumSamples
)
2544 float *out
= ZOUT(0);
2547 float prevA
= unit
->mPrevA
;
2548 float prevB
= unit
->mPrevB
;
2549 int recent
= unit
->mRecent
;
2552 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2556 } else if (diff
> 0.f
) {
2560 ZXP(out
) = recent
? xb
: xa
;
2565 unit
->mPrevA
= prevA
;
2566 unit
->mPrevB
= prevB
;
2567 unit
->mRecent
= recent
;
2571 ////////////////////
2573 void LastValue_Ctor(LastValue
*unit
)
2575 if (INRATE(0) == calc_FullRate
) {
2576 SETCALC(LastValue_next_ak
);
2578 SETCALC(LastValue_next_kk
);
2581 unit
->mPrev
= ZIN0(0);
2582 unit
->mCurr
= ZIN0(0);
2583 LastValue_next_kk(unit
, 1);
2586 void LastValue_next_kk(LastValue
*unit
, int inNumSamples
)
2588 float *out
= ZOUT(0);
2589 float inval
= ZIN0(0);
2590 float delta
= ZIN0(1);
2591 float diff
= std::abs(inval
- unit
->mCurr
);
2593 unit
->mPrev
= unit
->mCurr
;
2594 unit
->mCurr
= inval
;
2596 float level
= unit
->mPrev
;
2597 LOOP1(inNumSamples
, ZXP(out
) = level
; );
2601 void LastValue_next_ak(LastValue
*unit
, int inNumSamples
)
2603 float *out
= ZOUT(0);
2605 float delta
= ZIN0(1);
2606 float prev
= unit
->mPrev
;
2607 float curr
= unit
->mCurr
;
2610 float inval
= ZXP(in
);
2611 float diff
= std::abs(inval
- curr
);
2625 //////////////////////////////////////////////////////////////////////////////////////////
2627 void Done_Ctor(Done
*unit
)
2631 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2638 void Done_next(Done
*unit
, int inNumSamples
)
2640 float *out
= OUT(0);
2641 Unit
*src
= unit
->m_src
;
2642 if (src
) *out
= src
->mDone
? 1.f
: 0.f
;
2646 //////////////////////////////////////////////////////////////////////////////////////////
2648 void FreeSelf_Ctor(FreeSelf
*unit
)
2650 SETCALC(FreeSelf_next
);
2651 unit
->m_prevtrig
= 0.f
;
2652 FreeSelf_next(unit
, 1);
2656 void FreeSelf_next(FreeSelf
*unit
, int inNumSamples
)
2659 if (in
> 0.f
&& unit
->m_prevtrig
<= 0.f
) {
2660 NodeEnd(&unit
->mParent
->mNode
);
2662 unit
->m_prevtrig
= in
;
2665 //////////////////////////////////////////////////////////////////////////////////////////
2667 void PauseSelf_Ctor(PauseSelf
*unit
)
2669 SETCALC(PauseSelf_next
);
2670 unit
->m_prevtrig
= 0.f
;
2671 PauseSelf_next(unit
, 1);
2675 void PauseSelf_next(PauseSelf
*unit
, int inNumSamples
)
2678 if (in
> 0.f
&& unit
->m_prevtrig
<= 0.f
) {
2679 NodeRun(&unit
->mParent
->mNode
, 0);
2681 unit
->m_prevtrig
= in
;
2684 //////////////////////////////////////////////////////////////////////////////////////////
2686 void Pause_Ctor(Pause
*unit
)
2688 SETCALC(Pause_next
);
2696 void Pause_next(Pause
*unit
, int inNumSamples
)
2699 int state
= in
== 0.f
? 0 : 1;
2700 if (state
!= unit
->m_state
) {
2701 unit
->m_state
= state
;
2702 int id
= (int)ZIN0(1);
2703 Node
*node
= SC_GetNode(unit
->mWorld
, id
);
2705 NodeRun(node
, state
);
2711 //////////////////////////////////////////////////////////////////////////////////////////
2713 void Free_Ctor(Free
*unit
)
2717 unit
->m_prevtrig
= 0.f
;
2723 void Free_next(Free
*unit
, int inNumSamples
)
2725 float trig
= ZIN0(0);
2726 if (trig
> 0.f
&& unit
->m_prevtrig
<= 0) {
2727 int id
= (int)ZIN0(1);
2728 Node
*node
= SC_GetNode(unit
->mWorld
, id
);
2733 unit
->m_prevtrig
= trig
;
2737 //////////////////////////////////////////////////////////////////////////////////////////
2739 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone
*unit
)
2741 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2744 SETCALC(FreeSelfWhenDone_next
);
2745 FreeSelfWhenDone_next(unit
, 1);
2747 SETCALC(ClearUnitOutputs
);
2748 ClearUnitOutputs(unit
, 1);
2753 void FreeSelfWhenDone_next(FreeSelfWhenDone
*unit
, int inNumSamples
)
2755 float *out
= OUT(0);
2757 Unit
*src
= unit
->m_src
;
2759 NodeEnd(&unit
->mParent
->mNode
);
2760 SETCALC(ClearUnitOutputs
);
2765 //////////////////////////////////////////////////////////////////////////////////////////
2767 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone
*unit
)
2769 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2772 SETCALC(PauseSelfWhenDone_next
);
2773 PauseSelfWhenDone_next(unit
, 1);
2775 SETCALC(ClearUnitOutputs
);
2776 ClearUnitOutputs(unit
, 1);
2781 void PauseSelfWhenDone_next(PauseSelfWhenDone
*unit
, int inNumSamples
)
2783 float *out
= OUT(0);
2785 Unit
*src
= unit
->m_src
;
2787 NodeRun(&unit
->mParent
->mNode
, 0);
2788 SETCALC(ClearUnitOutputs
);
2793 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2798 // rate, level lag, replyid, channel count, [channels, ], cmd name size, [cmdname, ]
2799 static const int rateIndex
= 0;
2800 static const int levelLagIndex
= 1;
2801 static const int replyIdIndex
= 2;
2802 static const int channelCountIndex
= 3;
2803 static const int signalStartIndex
= 4;
2807 SendPeakRMS
* unit
= this;
2809 mChannelCount
= (unsigned int)IN0(channelCountIndex
);
2810 size_t channelDataAllocSize
= mChannelCount
* 3 * sizeof(float);
2812 int cmdSizeIndex
= signalStartIndex
+ mChannelCount
;
2813 size_t cmdNameSize
= IN0(cmdSizeIndex
);
2814 size_t cmdNameAllocSize
= (cmdNameSize
+ 1) * sizeof(char);
2816 void * allocData
= RTAlloc(unit
->mWorld
, channelDataAllocSize
+ cmdNameAllocSize
);
2818 memset(allocData
, 0, channelDataAllocSize
);
2819 mChannelData
= (float*)allocData
;
2821 char * cmdName
= (char*)(allocData
) + channelDataAllocSize
;
2823 size_t cmdNameIndex
= cmdSizeIndex
+ 1;
2824 for(int i
= 0; i
< cmdNameSize
; i
++)
2825 cmdName
[i
] = (char)IN0(cmdNameIndex
+ i
);
2826 cmdName
[cmdNameSize
] = 0;
2828 if ((FULLBUFLENGTH
& 15) == 0) {
2829 if (mCalcRate
== calc_FullRate
)
2830 SETCALC(SendPeakRMS::perform_a
<true>);
2832 SETCALC(SendPeakRMS::perform_k
<true>);
2834 if (mCalcRate
== calc_FullRate
)
2835 SETCALC(SendPeakRMS::perform_a
<false>);
2837 SETCALC(SendPeakRMS::perform_k
<false>);
2840 float replyRate
= IN0(rateIndex
);
2842 mAudioSamplesPerTick
= FULLRATE
/ replyRate
;
2843 mControlSamplesPerTick
= BUFRATE
/ replyRate
;
2845 mPhaseRemain
= (mCalcRate
== calc_FullRate
) ? mAudioSamplesPerTick
2846 : mControlSamplesPerTick
;
2848 float32 lag
= ZIN0(levelLagIndex
);
2849 mB1
= (lag
!= 0.f
) ? exp(log001
/ (lag
* replyRate
))
2855 SendPeakRMS
* unit
= this;
2856 RTFree(unit
->mWorld
, mChannelData
);
2859 unsigned int mChannelCount
;
2860 float * mChannelData
;
2863 int mAudioSamplesPerTick
;
2864 int mControlSamplesPerTick
;
2867 void performLevelLag(float & out
, float y0
, float & y1
)
2872 out
= y1
= y0
+ mB1
* (y1
- y0
);
2875 char * getCmdName (void)
2877 void * buffer
= mChannelData
;
2878 return (char*)(buffer
) + mChannelCount
* 3 * sizeof(float);
2881 void sendReply(void)
2883 SendPeakRMS
* unit
= this;
2884 float * reply
= (float*)alloca(mChannelCount
* 2 * sizeof(float));
2887 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2888 float & maxLevel
= reply
[2*i
];
2889 float & rms
= reply
[2*i
+ 1];
2891 performLevelLag(maxLevel
, mChannelData
[2*i
], mChannelData
[2*mChannelCount
+ i
]);
2893 if (INRATE(signalStartIndex
+ i
) == calc_FullRate
)
2894 rms
= std::sqrt(mChannelData
[2*i
+ 1] / (float)mAudioSamplesPerTick
);
2896 rms
= std::sqrt(mChannelData
[2*i
+ 1] / (float)mControlSamplesPerTick
);
2899 SendNodeReply(&unit
->mParent
->mNode
, (int)ZIN0(replyIdIndex
), getCmdName(), mChannelCount
*2, reply
);
2900 memset(mChannelData
, 0, mChannelCount
* 2 * sizeof(float));
2903 template <bool simd
>
2904 void analyzeFullBlock(void)
2906 SendPeakRMS
* unit
= this;
2907 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2908 float * in
= IN(signalStartIndex
+ i
);
2909 int numSamples
= INBUFLENGTH(signalStartIndex
+ i
);
2911 float & level
= mChannelData
[2*i
];
2912 float & sqrsum
= mChannelData
[2*i
+ 1];
2913 if (numSamples
== 1)
2914 nova::peak_rms_vec(in
, &level
, &sqrsum
, 1);
2917 nova::peak_rms_vec_simd(in
, &level
, &sqrsum
, numSamples
);
2919 nova::peak_rms_vec(in
, &level
, &sqrsum
, numSamples
);
2924 void analyzePartialBlock(int firstSample
, int samplesToAnalyze
)
2926 SendPeakRMS
* unit
= this;
2927 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2928 float * in
= IN(signalStartIndex
+ i
) + firstSample
;
2929 int numSamples
= INBUFLENGTH(signalStartIndex
+ i
);
2931 float & level
= mChannelData
[2*i
];
2932 float & sqrsum
= mChannelData
[2*i
+ 1];
2933 if (numSamples
== 1) {
2934 if (firstSample
== 0)
2935 nova::peak_rms_vec(in
, &level
, &sqrsum
, 1);
2937 if (!(samplesToAnalyze
& 15) && !(firstSample
& 3)) // check for unrolling and alignment
2938 nova::peak_rms_vec_simd(in
, &level
, &sqrsum
, samplesToAnalyze
);
2940 nova::peak_rms_vec(in
, &level
, &sqrsum
, samplesToAnalyze
);
2945 template <bool simd
>
2946 inline void next_k(int inNumSamples
)
2950 if (mPhaseRemain
<= 0) {
2951 mPhaseRemain
+= mControlSamplesPerTick
;
2955 analyzeFullBlock
<simd
>();
2958 template <bool simd
>
2959 inline void next_a(int inNumSamples
)
2961 if (mPhaseRemain
>= inNumSamples
) {
2962 mPhaseRemain
-= inNumSamples
;
2963 analyzeFullBlock
<simd
>();
2965 if (mPhaseRemain
== 0) {
2967 mPhaseRemain
= mAudioSamplesPerTick
;
2970 int startSample
= 0;
2971 int samplesToAnalyze
= std::min(mPhaseRemain
, inNumSamples
);
2972 int remain
= inNumSamples
;
2975 analyzePartialBlock(startSample
, samplesToAnalyze
);
2977 startSample
+= samplesToAnalyze
;
2978 mPhaseRemain
-= samplesToAnalyze
;
2979 if (mPhaseRemain
== 0) {
2981 mPhaseRemain
= mAudioSamplesPerTick
;
2984 remain
-= samplesToAnalyze
;
2985 samplesToAnalyze
= std::min(remain
, mPhaseRemain
);
2990 template <bool simd
>
2991 static void perform_k(Unit
* unit
, int inNumSamples
)
2993 static_cast<SendPeakRMS
*>(unit
)->next_k
<simd
>(inNumSamples
);
2996 template <bool simd
>
2997 static void perform_a(Unit
* unit
, int inNumSamples
)
2999 static_cast<SendPeakRMS
*>(unit
)->next_a
<simd
>(inNumSamples
);
3003 static void SendPeakRMS_Ctor(SendPeakRMS
* unit
) { new(unit
) SendPeakRMS (); }
3004 static void SendPeakRMS_Dtor(SendPeakRMS
* unit
) { unit
->~SendPeakRMS (); }
3006 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3012 DefineSimpleUnit(Trig1
);
3013 DefineSimpleUnit(Trig
);
3014 DefineSimpleUnit(SendTrig
);
3015 DefineDtorUnit(SendReply
);
3016 DefineDtorUnit(Poll
);
3017 DefineSimpleUnit(ToggleFF
);
3018 DefineSimpleUnit(SetResetFF
);
3019 DefineSimpleUnit(Latch
);
3020 DefineSimpleUnit(Gate
);
3021 DefineSimpleUnit(Schmidt
);
3022 DefineSimpleUnit(PulseDivider
);
3023 DefineSimpleUnit(PulseCount
);
3024 DefineSimpleUnit(Stepper
);
3025 DefineSimpleUnit(TDelay
);
3026 DefineSimpleUnit(ZeroCrossing
);
3027 DefineSimpleUnit(Timer
);
3028 DefineSimpleUnit(Sweep
);
3029 DefineSimpleUnit(Phasor
);
3030 DefineSimpleUnit(Peak
);
3031 DefineSimpleUnit(RunningMin
);
3032 DefineSimpleUnit(RunningMax
);
3033 DefineSimpleUnit(PeakFollower
);
3034 DefineSimpleUnit(MostChange
);
3035 DefineSimpleUnit(LeastChange
);
3036 DefineSimpleUnit(LastValue
);
3037 DefineSimpleUnit(Done
);
3038 DefineSimpleUnit(Pause
);
3039 DefineSimpleUnit(FreeSelf
);
3040 DefineSimpleUnit(PauseSelf
);
3041 DefineSimpleUnit(Free
);
3042 DefineSimpleUnit(FreeSelfWhenDone
);
3043 DefineSimpleUnit(PauseSelfWhenDone
);
3045 DefineDtorUnit(SendPeakRMS
);