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 *values
= unit
->m_values
;
842 int valueSize
= unit
->m_valueSize
;
843 int valueOffset
= unit
->m_valueOffset
;
844 for(int j
= 0; j
< inNumSamples
; j
++) {
845 float curtrig
= trig
[j
];
846 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
847 for(int i
=0; i
<valueSize
; i
++) {
848 int offset
= INRATE( i
+ valueOffset
) != calc_FullRate
? 0 : j
;
849 values
[i
] = IN(i
+ valueOffset
)[offset
];
851 SendNodeReply(&unit
->mParent
->mNode
, (int)ZIN0(1), unit
->m_cmdName
, unit
->m_valueSize
, values
);
855 unit
->m_prevtrig
= prevtrig
;
859 ////////////////////////////////////////////////////////////////////////////////////////////////////////
861 void Poll_Ctor(Poll
* unit
)
863 if (INRATE(0) == calc_FullRate
){
864 if (INRATE(1) == calc_FullRate
){
865 SETCALC(Poll_next_aa
);
867 SETCALC(Poll_next_ak
);
870 SETCALC(Poll_next_kk
);
873 unit
->m_trig
= IN0(0);
874 unit
->m_id
= IN0(3); // number of chars in the id string
875 unit
->m_id_string
= (char*)RTAlloc(unit
->mWorld
, ((int)unit
->m_id
+ 1) * sizeof(char));
876 for(int i
= 0; i
< (int)unit
->m_id
; i
++){
877 unit
->m_id_string
[i
] = (char)IN0(4+i
);
879 unit
->m_id_string
[(int)unit
->m_id
] = '\0';
881 unit
->m_mayprint
= unit
->mWorld
->mVerbosity
>= -1;
883 Poll_next_kk(unit
, 1);
886 void Poll_Dtor(Poll
* unit
)
888 RTFree(unit
->mWorld
, unit
->m_id_string
);
891 void Poll_next_aa(Poll
*unit
, int inNumSamples
){
894 float lasttrig
= unit
->m_trig
;
895 for(int i
= 0; i
< inNumSamples
; i
++){
896 if((lasttrig
<= 0.0) && (trig
[i
] > 0.0)){
897 if(unit
->m_mayprint
){
898 Print("%s: %g\n", unit
->m_id_string
, in
[i
]);
900 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
[i
]);
904 unit
->m_trig
= lasttrig
;
907 void Poll_next_kk(Poll
*unit
, int inNumSamples
){
910 if((unit
->m_trig
<= 0.0) && (trig
> 0.0)){
911 if(unit
->m_mayprint
){
912 Print("%s: %g\n", unit
->m_id_string
, in
);
914 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
);
920 void Poll_next_ak(Poll
*unit
, int inNumSamples
){
923 float lasttrig
= unit
->m_trig
;
924 for(int i
= 0; i
< inNumSamples
; i
++){
925 if((lasttrig
<= 0.0) && (trig
[i
] > 0.0)){
926 if(unit
->m_mayprint
){
927 Print("%s: %g\n", unit
->m_id_string
, in
);
929 if(IN0(2) >= 0.0) SendTrigger(&unit
->mParent
->mNode
, (int)IN0(2), in
);
933 unit
->m_trig
= lasttrig
;
938 ////////////////////////////////////////////////////////////////////////////////////////////////////////
940 void SetResetFF_Ctor(SetResetFF
*unit
)
942 if (INRATE(1) == calc_FullRate
) {
943 SETCALC(SetResetFF_next_a
);
945 SETCALC(SetResetFF_next_k
);
948 unit
->m_prevtrig
= 0.f
;
949 unit
->m_prevreset
= 0.f
;
956 void SetResetFF_next_a(SetResetFF
*unit
, int inNumSamples
)
958 float *out
= ZOUT(0);
959 float *trig
= ZIN(0);
960 float *reset
= ZIN(1);
961 float prevtrig
= unit
->m_prevtrig
;
962 float prevreset
= unit
->m_prevreset
;
963 float level
= unit
->mLevel
;
966 float curtrig
= ZXP(trig
);
967 float curreset
= ZXP(reset
);
968 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
969 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
972 prevreset
= curreset
;
974 unit
->m_prevtrig
= prevtrig
;
975 unit
->m_prevreset
= prevreset
;
976 unit
->mLevel
= level
;
979 void SetResetFF_next_k(SetResetFF
*unit
, int inNumSamples
)
981 float *out
= ZOUT(0);
982 float *trig
= ZIN(0);
983 float curreset
= ZIN0(1);
984 float prevtrig
= unit
->m_prevtrig
;
985 float prevreset
= unit
->m_prevreset
;
986 float level
= unit
->mLevel
;
988 float curtrig
= ZXP(trig
);
989 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
990 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
994 LOOP(inNumSamples
- 1,
996 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
;
1000 unit
->m_prevtrig
= prevtrig
;
1001 unit
->m_prevreset
= curreset
;
1002 unit
->mLevel
= level
;
1005 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1008 void ToggleFF_Ctor(ToggleFF
*unit
)
1010 SETCALC(ToggleFF_next
);
1012 unit
->m_prevtrig
= 0.f
;
1019 void ToggleFF_next(ToggleFF
*unit
, int inNumSamples
)
1021 float *out
= ZOUT(0);
1022 float *trig
= ZIN(0);
1023 float prevtrig
= unit
->m_prevtrig
;
1024 float level
= unit
->mLevel
;
1027 float curtrig
= ZXP(trig
);
1028 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= 1.f
- level
;
1032 unit
->m_prevtrig
= prevtrig
;
1033 unit
->mLevel
= level
;
1036 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1040 void Latch_next_ak_nova(Latch
*unit
, int inNumSamples
)
1042 float level
= unit
->mLevel
;
1044 float curtrig
= ZIN0(1);
1045 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1047 nova::setvec_simd(OUT(0), level
, inNumSamples
);
1049 unit
->m_prevtrig
= curtrig
;
1050 unit
->mLevel
= level
;
1053 void Latch_next_ak_nova_64(Latch
*unit
, int inNumSamples
)
1055 float level
= unit
->mLevel
;
1057 float curtrig
= ZIN0(1);
1058 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1060 nova::setvec_simd
<64>(OUT(0), level
);
1062 unit
->m_prevtrig
= curtrig
;
1063 unit
->mLevel
= level
;
1067 void Latch_Ctor(Latch
*unit
)
1069 if (INRATE(1) == calc_FullRate
) {
1070 SETCALC(Latch_next_aa
);
1073 if (BUFLENGTH
== 64)
1074 SETCALC(Latch_next_ak_nova_64
);
1075 if (!(BUFLENGTH
& 15))
1076 SETCALC(Latch_next_ak_nova
);
1079 SETCALC(Latch_next_ak
);
1082 unit
->m_prevtrig
= 0.f
;
1085 ZOUT0(0) = ZIN0(1) > 0.f
? ZIN0(0) : 0.f
;
1089 void Latch_next_ak(Latch
*unit
, int inNumSamples
)
1091 float *out
= ZOUT(0);
1092 float level
= unit
->mLevel
;
1094 float curtrig
= ZIN0(1);
1095 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZIN0(0);
1097 LOOP1(inNumSamples
, ZXP(out
) = level
; );
1099 unit
->m_prevtrig
= curtrig
;
1100 unit
->mLevel
= level
;
1104 void Latch_next_aa(Latch
*unit
, int inNumSamples
)
1106 float *out
= ZOUT(0);
1108 float *trig
= ZIN(1);
1109 float prevtrig
= unit
->m_prevtrig
;
1110 float level
= unit
->mLevel
;
1113 float curtrig
= ZXP(trig
);
1114 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= ZXP(in
);
1119 unit
->m_prevtrig
= prevtrig
;
1120 unit
->mLevel
= level
;
1125 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1128 FLATTEN
void Gate_next_ak_nova(Gate
*unit
, int inNumSamples
)
1130 float *trig
= ZIN(1);
1131 float level
= unit
->mLevel
;
1133 float curtrig
= ZXP(trig
);
1134 if (curtrig
> 0.f
) {
1135 nova::copyvec_simd(OUT(0), IN(0), inNumSamples
);
1136 unit
->mLevel
= IN(0)[inNumSamples
-1];
1138 nova::setvec_simd(OUT(0), level
, inNumSamples
);
1141 FLATTEN
void Gate_next_ak_nova_64(Gate
*unit
, int inNumSamples
)
1143 float *trig
= ZIN(1);
1144 float level
= unit
->mLevel
;
1146 float curtrig
= ZXP(trig
);
1147 if (curtrig
> 0.f
) {
1148 nova::copyvec_simd
<64>(OUT(0), IN(0));
1149 unit
->mLevel
= IN(0)[inNumSamples
-1];
1151 nova::setvec_simd
<64>(OUT(0), level
);
1155 void Gate_Ctor(Gate
*unit
)
1157 if (INRATE(1) == calc_FullRate
) {
1158 SETCALC(Gate_next_aa
);
1161 if (BUFLENGTH
== 64)
1162 SETCALC(Gate_next_ak_nova_64
);
1163 if (!(BUFLENGTH
& 15))
1164 SETCALC(Gate_next_ak_nova
);
1167 SETCALC(Gate_next_ak
);
1177 void Gate_next_ak(Gate
*unit
, int inNumSamples
)
1179 float *out
= ZOUT(0);
1181 float *trig
= ZIN(1);
1182 float level
= unit
->mLevel
;
1184 float curtrig
= ZXP(trig
);
1185 if (curtrig
> 0.f
) {
1190 unit
->mLevel
= level
;
1198 void Gate_next_aa(Gate
*unit
, int inNumSamples
)
1200 float *out
= ZOUT(0);
1202 float *trig
= ZIN(1);
1203 float level
= unit
->mLevel
;
1206 float curtrig
= ZXP(trig
);
1207 if (curtrig
> 0.f
) level
= ZXP(in
);
1211 unit
->mLevel
= level
;
1214 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1217 void Schmidt_Ctor(Schmidt
*unit
)
1219 SETCALC(Schmidt_next
);
1226 void Schmidt_next(Schmidt
*unit
, int inNumSamples
)
1228 float *out
= ZOUT(0);
1232 float level
= unit
->mLevel
;
1234 float zin
= ZXP(in
);
1236 if (zin
< lo
) level
= 0.f
;
1238 if (zin
> hi
) level
= 1.f
;
1242 unit
->mLevel
= level
;
1245 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1247 void PulseDivider_Ctor(PulseDivider
*unit
)
1249 SETCALC(PulseDivider_next
);
1251 unit
->m_prevtrig
= 0.f
;
1253 unit
->mCounter
= (long)floor(ZIN0(2) + 0.5);
1259 void PulseDivider_next(PulseDivider
*unit
, int inNumSamples
)
1261 float *out
= ZOUT(0);
1262 float *trig
= ZIN(0);
1263 long div
= (long)ZIN0(1);
1264 float prevtrig
= unit
->m_prevtrig
;
1265 long counter
= unit
->mCounter
;
1269 float curtrig
= ZXP(trig
);
1270 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1272 if (counter
>= div
) {
1284 unit
->mCounter
= counter
;
1285 unit
->m_prevtrig
= prevtrig
;
1288 //////////////////////////////////////////////////////////////////////////////////////////
1291 void PulseCount_Ctor(PulseCount
*unit
)
1293 if (INRATE(1) == calc_FullRate
) {
1294 SETCALC(PulseCount_next_a
);
1295 } else if (INRATE(1) == calc_BufRate
) {
1296 SETCALC(PulseCount_next_k
);
1298 SETCALC(PulseCount_next_0
);
1300 unit
->m_prevtrig
= 0.f
;
1301 unit
->m_prevreset
= 0.f
;
1308 void PulseCount_next_a(PulseCount
*unit
, int inNumSamples
)
1310 float *out
= ZOUT(0);
1311 float *trig
= ZIN(0);
1312 float *reset
= ZIN(1);
1313 float prevtrig
= unit
->m_prevtrig
;
1314 float prevreset
= unit
->m_prevreset
;
1315 float level
= unit
->mLevel
;
1318 float curtrig
= ZXP(trig
);
1319 float curreset
= ZXP(reset
);
1320 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
1321 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1326 prevreset
= curreset
;
1328 unit
->mLevel
= level
;
1329 unit
->m_prevtrig
= prevtrig
;
1330 unit
->m_prevreset
= prevreset
;
1333 void PulseCount_next_k(PulseCount
*unit
, int inNumSamples
)
1335 float *out
= ZOUT(0);
1336 float *trig
= ZIN(0);
1337 float curreset
= ZIN0(1);
1338 float prevtrig
= unit
->m_prevtrig
;
1339 float prevreset
= unit
->m_prevreset
;
1340 float level
= unit
->mLevel
;
1343 float curtrig
= ZXP(trig
);
1344 if (prevreset
<= 0.f
&& curreset
> 0.f
) level
= 0.f
;
1345 else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1350 prevreset
= curreset
;
1352 unit
->mLevel
= level
;
1353 unit
->m_prevtrig
= prevtrig
;
1354 unit
->m_prevreset
= prevreset
;
1357 void PulseCount_next_0(PulseCount
*unit
, int inNumSamples
)
1359 float *out
= ZOUT(0);
1360 float *trig
= ZIN(0);
1361 float prevtrig
= unit
->m_prevtrig
;
1362 float level
= unit
->mLevel
;
1365 float curtrig
= ZXP(trig
);
1366 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1372 unit
->mLevel
= level
;
1373 unit
->m_prevtrig
= prevtrig
;
1376 //////////////////////////////////////////////////////////////////////////////////////////
1379 void Stepper_Ctor(Stepper
*unit
)
1381 if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) == calc_FullRate
&& INRATE(1) == calc_ScalarRate
) {
1382 SETCALC(Stepper_next_a0
);
1383 } else if (unit
->mCalcRate
== calc_FullRate
&& INRATE(0) == calc_FullRate
&& INRATE(1) != calc_FullRate
) {
1384 SETCALC(Stepper_next_ak
);
1386 SETCALC(Stepper_next_aa
);
1389 int32 resetval
= (int32
)ZIN0(5);
1391 unit
->m_prevtrig
= 0.f
;
1392 unit
->m_prevreset
= 0.f
;
1393 unit
->mLevel
= (float)resetval
;
1399 void Stepper_next_aa(Stepper
*unit
, int inNumSamples
)
1401 float *out
= ZOUT(0);
1402 float *trig
= ZIN(0);
1403 float *reset
= ZIN(1);
1404 int32 zmin
= (int32
)ZIN0(2);
1405 int32 zmax
= (int32
)ZIN0(3);
1406 int32 step
= (int32
)ZIN0(4);
1407 int32 resetval
= (int32
)ZIN0(5);
1408 float prevtrig
= unit
->m_prevtrig
;
1409 float prevreset
= unit
->m_prevreset
;
1410 float level
= unit
->mLevel
;
1413 float curtrig
= ZXP(trig
);
1414 float curreset
= ZXP(reset
);
1415 if (prevreset
<= 0.f
&& curreset
> 0.f
) {
1416 level
= (float)sc_wrap(resetval
, zmin
, zmax
);
1417 } else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1418 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1422 prevreset
= curreset
;
1424 unit
->mLevel
= level
;
1425 unit
->m_prevtrig
= prevtrig
;
1426 unit
->m_prevreset
= prevreset
;
1429 void Stepper_next_ak(Stepper
*unit
, int inNumSamples
)
1431 float *out
= ZOUT(0);
1432 float *trig
= ZIN(0);
1433 float curreset
= ZIN0(1);
1434 int32 zmin
= (int32
)ZIN0(2);
1435 int32 zmax
= (int32
)ZIN0(3);
1436 int32 step
= (int32
)ZIN0(4);
1437 int32 resetval
= (int32
)ZIN0(5);
1438 float prevtrig
= unit
->m_prevtrig
;
1439 float prevreset
= unit
->m_prevreset
;
1440 float level
= unit
->mLevel
;
1443 float curtrig
= ZXP(trig
);
1444 if (prevreset
<= 0.f
&& curreset
> 0.f
) {
1445 level
= (float)sc_wrap(resetval
, zmin
, zmax
);
1446 } else if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1447 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1451 prevreset
= curreset
;
1453 unit
->mLevel
= level
;
1454 unit
->m_prevtrig
= prevtrig
;
1455 unit
->m_prevreset
= prevreset
;
1458 void Stepper_next_a0(Stepper
*unit
, int inNumSamples
)
1460 float *out
= ZOUT(0);
1461 float *trig
= ZIN(0);
1462 int32 zmin
= (int32
)ZIN0(2);
1463 int32 zmax
= (int32
)ZIN0(3);
1464 int32 step
= (int32
)ZIN0(4);
1465 float prevtrig
= unit
->m_prevtrig
;
1466 float level
= unit
->mLevel
;
1469 float curtrig
= ZXP(trig
);
1470 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1471 level
= (float)sc_wrap((int32
)level
+ step
, zmin
, zmax
);
1476 unit
->mLevel
= level
;
1477 unit
->m_prevtrig
= prevtrig
;
1480 //////////////////////////////////////////////////////////////////////////////////////////
1482 void TDelay_Ctor(TDelay
*unit
)
1484 SETCALC(TDelay_next
);
1486 unit
->m_prevtrig
= 0.f
;
1494 void TDelay_next(TDelay
*unit
, int inNumSamples
)
1496 float *out
= ZOUT(0);
1497 float *trig
= ZIN(0);
1498 float dur
= ZIN0(1);
1499 float prevtrig
= unit
->m_prevtrig
;
1500 long counter
= unit
->mCounter
;
1503 float curtrig
= ZXP(trig
);
1508 } else if (counter
<=0) {
1509 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
1510 counter
= (long)(dur
* unit
->mRate
->mSampleRate
+ .5f
);
1511 if (counter
< 1) counter
= 1;
1521 unit
->m_prevtrig
= prevtrig
;
1522 unit
->mCounter
= counter
;
1526 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1529 void ZeroCrossing_Ctor(ZeroCrossing
*unit
)
1531 SETCALC(ZeroCrossing_next_a
);
1533 unit
->m_prevfrac
= 0.f
;
1534 unit
->m_previn
= ZIN0(0);
1535 ZOUT0(0) = unit
->mLevel
= 0.f
;
1539 void ZeroCrossing_next_a(ZeroCrossing
*unit
, int inNumSamples
)
1541 float *out
= ZOUT(0);
1543 float previn
= unit
->m_previn
;
1544 float prevfrac
= unit
->m_prevfrac
;
1545 float level
= unit
->mLevel
;
1546 long counter
= unit
->mCounter
;
1550 float curin
= ZXP(in
);
1551 if (counter
> 4 && previn
<= 0.f
&& curin
> 0.f
) {
1552 float frac
= -previn
/(curin
-previn
);
1553 level
= unit
->mRate
->mSampleRate
/ (frac
+ counter
- prevfrac
);
1561 unit
->m_previn
= previn
;
1562 unit
->m_prevfrac
= prevfrac
;
1563 unit
->mLevel
= level
;
1564 unit
->mCounter
= counter
;
1567 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1569 void Timer_Ctor(Timer
*unit
)
1571 SETCALC(Timer_next_a
);
1573 unit
->m_prevfrac
= 0.f
;
1574 unit
->m_previn
= ZIN0(0);
1575 ZOUT0(0) = unit
->mLevel
= 0.f
;
1579 void Timer_next_a(Timer
*unit
, int inNumSamples
)
1581 float *out
= ZOUT(0);
1583 float previn
= unit
->m_previn
;
1584 float prevfrac
= unit
->m_prevfrac
;
1585 float level
= unit
->mLevel
;
1586 long counter
= unit
->mCounter
;
1590 float curin
= ZXP(in
);
1591 if (previn
<= 0.f
&& curin
> 0.f
) {
1592 float frac
= -previn
/(curin
-previn
);
1593 level
= unit
->mRate
->mSampleDur
* (frac
+ counter
- prevfrac
);
1601 unit
->m_previn
= previn
;
1602 unit
->m_prevfrac
= prevfrac
;
1603 unit
->mLevel
= level
;
1604 unit
->mCounter
= counter
;
1608 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1610 void Sweep_Ctor(Sweep
*unit
)
1612 if (INRATE(0) == calc_ScalarRate
) {
1613 if (INRATE(1) == calc_FullRate
) {
1614 SETCALC(Sweep_next_0a
);
1616 SETCALC(Sweep_next_0k
);
1618 } else if (INRATE(0) == calc_BufRate
) {
1619 if (INRATE(1) == calc_FullRate
) {
1620 SETCALC(Sweep_next_ka
);
1622 SETCALC(Sweep_next_kk
);
1625 if (INRATE(1) == calc_FullRate
) {
1626 SETCALC(Sweep_next_aa
);
1628 SETCALC(Sweep_next_ak
);
1632 unit
->m_previn
= ZIN0(0);
1633 ZOUT0(0) = unit
->mLevel
= 0.f
;
1636 void Sweep_next_0k(Sweep
*unit
, int inNumSamples
)
1638 float *out
= ZOUT(0);
1639 double rate
= ZIN0(1) * SAMPLEDUR
;
1640 double level
= unit
->mLevel
;
1647 unit
->mLevel
= level
;
1650 void Sweep_next_0a(Sweep
*unit
, int inNumSamples
)
1652 float *out
= ZOUT(0);
1653 float *rate
= ZIN(1);
1654 double level
= unit
->mLevel
;
1655 float sampledur
= SAMPLEDUR
;
1658 float zrate
= ZXP(rate
) * sampledur
;
1663 unit
->mLevel
= level
;
1666 void Sweep_next_kk(Sweep
*unit
, int inNumSamples
)
1668 float *out
= ZOUT(0);
1669 float curin
= ZIN0(0);
1670 double rate
= ZIN0(1) * SAMPLEDUR
;
1671 float previn
= unit
->m_previn
;
1672 double level
= unit
->mLevel
;
1674 if (previn
<= 0.f
&& curin
> 0.f
) {
1675 float frac
= -previn
/(curin
-previn
);
1676 level
= frac
* rate
;
1684 unit
->m_previn
= curin
;
1685 unit
->mLevel
= level
;
1688 void Sweep_next_ka(Sweep
*unit
, int inNumSamples
)
1690 float *out
= ZOUT(0);
1691 float curin
= ZIN0(0);
1692 float *rate
= ZIN(1);
1693 float previn
= unit
->m_previn
;
1694 double level
= unit
->mLevel
;
1695 float sampledur
= SAMPLEDUR
;
1697 if (previn
<= 0.f
&& curin
> 0.f
) {
1698 float frac
= -previn
/(curin
-previn
);
1699 level
= frac
* rate
[ZOFF
] * sampledur
;
1703 float zrate
= ZXP(rate
) * sampledur
;
1708 unit
->m_previn
= curin
;
1709 unit
->mLevel
= level
;
1712 void Sweep_next_ak(Sweep
*unit
, int inNumSamples
)
1714 float *out
= ZOUT(0);
1716 double rate
= ZIN0(1) * SAMPLEDUR
;
1717 float previn
= unit
->m_previn
;
1718 double level
= unit
->mLevel
;
1721 float curin
= ZXP(in
);
1722 if (previn
<= 0.f
&& curin
> 0.f
) {
1723 float frac
= -previn
/(curin
-previn
);
1724 level
= frac
* rate
;
1732 unit
->m_previn
= previn
;
1733 unit
->mLevel
= level
;
1736 void Sweep_next_aa(Sweep
*unit
, int inNumSamples
)
1738 float *out
= ZOUT(0);
1740 float *rate
= ZIN(1);
1741 float previn
= unit
->m_previn
;
1742 double level
= unit
->mLevel
;
1743 float sampledur
= SAMPLEDUR
;
1746 float curin
= ZXP(in
);
1747 float zrate
= ZXP(rate
) * sampledur
;
1748 if (previn
<= 0.f
&& curin
> 0.f
) {
1749 float frac
= -previn
/(curin
-previn
);
1750 level
= frac
* zrate
;
1758 unit
->m_previn
= previn
;
1759 unit
->mLevel
= level
;
1762 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1764 void Phasor_Ctor(Phasor
*unit
)
1766 if (unit
->mCalcRate
== calc_FullRate
) {
1767 if (INRATE(0) == calc_FullRate
) {
1768 if (INRATE(1) == calc_FullRate
) {
1769 SETCALC(Phasor_next_aa
);
1771 SETCALC(Phasor_next_ak
);
1774 SETCALC(Phasor_next_kk
);
1777 SETCALC(Phasor_next_ak
);
1780 unit
->m_previn
= ZIN0(0);
1781 ZOUT0(0) = unit
->mLevel
= ZIN0(2);
1784 void Phasor_next_kk(Phasor
*unit
, int inNumSamples
)
1786 float *out
= ZOUT(0);
1789 double rate
= ZIN0(1);
1790 double start
= ZIN0(2);
1791 double end
= ZIN0(3);
1792 float resetPos
= ZIN0(4);
1794 float previn
= unit
->m_previn
;
1795 double level
= unit
->mLevel
;
1797 if (previn
<= 0.f
&& in
> 0.f
) {
1801 level
= sc_wrap(level
, start
, end
);
1806 unit
->m_previn
= in
;
1807 unit
->mLevel
= level
;
1810 void Phasor_next_ak(Phasor
*unit
, int inNumSamples
)
1812 float *out
= ZOUT(0);
1815 double rate
= ZIN0(1);
1816 double start
= ZIN0(2);
1817 double end
= ZIN0(3);
1818 float resetPos
= ZIN0(4);
1820 float previn
= unit
->m_previn
;
1821 double level
= unit
->mLevel
;
1824 float curin
= ZXP(in
);
1825 if (previn
<= 0.f
&& curin
> 0.f
) {
1826 float frac
= 1.f
- previn
/(curin
-previn
);
1827 level
= resetPos
+ frac
* rate
;
1831 level
= sc_wrap(level
, start
, end
);
1836 unit
->m_previn
= previn
;
1837 unit
->mLevel
= level
;
1840 void Phasor_next_aa(Phasor
*unit
, int inNumSamples
)
1842 float *out
= ZOUT(0);
1844 float *rate
= ZIN(1);
1845 double start
= ZIN0(2);
1846 double end
= ZIN0(3);
1847 float resetPos
= ZIN0(4);
1849 float previn
= unit
->m_previn
;
1850 double level
= unit
->mLevel
;
1853 float curin
= ZXP(in
);
1854 double zrate
= ZXP(rate
);
1855 if (previn
<= 0.f
&& curin
> 0.f
) {
1856 float frac
= 1.f
- previn
/(curin
-previn
);
1857 level
= resetPos
+ frac
* zrate
;
1861 level
= sc_wrap(level
, start
, end
);
1865 unit
->m_previn
= previn
;
1866 unit
->mLevel
= level
;
1869 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1872 void Peak_next_ak_unroll(Peak
*unit
, int inNumSamples
);
1873 void Peak_next_ai_unroll(Peak
*unit
, int inNumSamples
);
1876 FLATTEN
void Peak_next_ak_k_nova(Peak
*unit
, int inNumSamples
);
1877 FLATTEN
void Peak_next_ai_k_nova(Peak
*unit
, int inNumSamples
);
1880 void Peak_Ctor(Peak
*unit
)
1882 if (BUFLENGTH
== 1 &&
1883 INRATE(0) == calc_FullRate
) {
1884 /* audio-rate input with control-rate output */
1885 if (INRATE(1) == calc_FullRate
) {
1886 SETCALC(Peak_next_aa_k
);
1887 } else if (INRATE(1) == calc_ScalarRate
) {
1889 if (INBUFLENGTH(0) & 7)
1890 SETCALC(Peak_next_ai_k
);
1892 SETCALC(Peak_next_ai_k_nova
);
1894 SETCALC(Peak_next_ai_k
);
1898 if (INBUFLENGTH(0) & 7)
1899 SETCALC(Peak_next_ak_k
);
1901 SETCALC(Peak_next_ak_k_nova
);
1903 SETCALC(Peak_next_ak_k
);
1907 if (INRATE(1) == calc_FullRate
) {
1908 SETCALC(Peak_next_aa
);
1909 } else if (INRATE(1) == calc_ScalarRate
) {
1911 SETCALC(Peak_next_ai
);
1913 SETCALC(Peak_next_ai_unroll
);
1916 SETCALC(Peak_next_ak
);
1918 SETCALC(Peak_next_ak_unroll
);
1921 unit
->m_prevtrig
= 0.f
;
1922 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
1925 void Peak_next_ak(Peak
*unit
, int inNumSamples
)
1927 float *out
= ZOUT(0);
1929 float curtrig
= ZIN0(1);
1930 float level
= unit
->mLevel
;
1933 inlevel
= std::abs(ZXP(in
));
1934 level
= std::max(inlevel
, level
);
1937 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
1938 unit
->m_prevtrig
= curtrig
;
1939 unit
->mLevel
= level
;
1942 void Peak_next_ai(Peak
*unit
, int inNumSamples
)
1944 float *out
= ZOUT(0);
1946 float level
= unit
->mLevel
;
1949 inlevel
= std::abs(ZXP(in
));
1950 level
= std::max(inlevel
, level
);
1953 unit
->mLevel
= level
;
1956 void Peak_next_aa(Peak
*unit
, int inNumSamples
)
1958 float *out
= ZOUT(0);
1960 float *trig
= ZIN(1);
1961 float prevtrig
= unit
->m_prevtrig
;
1962 float level
= unit
->mLevel
;
1965 float curtrig
= ZXP(trig
);
1966 float inlevel
= std::abs(ZXP(in
));
1967 level
= std::max(inlevel
, level
);
1969 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
1972 unit
->m_prevtrig
= prevtrig
;
1973 unit
->mLevel
= level
;
1976 static inline float Peak_unroll_body(Peak
*unit
, int inNumSamples
, float & level
)
1978 float *out
= OUT(0);
1982 using namespace std
;
1983 for (int i
= 0; i
!= inNumSamples
; i
+= 8, out
+= 8, in
+= 8)
1985 float level0
= max(abs(in
[0]), level
);
1986 float level1
= max(abs(in
[1]), level0
);
1987 float level2
= max(abs(in
[2]), level1
);
1988 float level3
= max(abs(in
[3]), level2
);
1989 float level4
= max(abs(in
[4]), level3
);
1990 float level5
= max(abs(in
[5]), level4
);
1991 float level6
= max(abs(in
[6]), level5
);
1992 inlevel
= abs(in
[7]);
1993 float level7
= max(inlevel
, level6
);
2004 return inlevel
; /* input level of the last sample */
2007 void Peak_next_ak_unroll(Peak
*unit
, int inNumSamples
)
2009 float curtrig
= ZIN0(1);
2010 float level
= unit
->mLevel
;
2011 float inlevel
= Peak_unroll_body(unit
, inNumSamples
, level
);
2013 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2014 unit
->m_prevtrig
= curtrig
;
2015 unit
->mLevel
= level
;
2018 void Peak_next_ai_unroll(Peak
*unit
, int inNumSamples
)
2020 float level
= unit
->mLevel
;
2021 Peak_unroll_body(unit
, inNumSamples
, level
);
2022 unit
->mLevel
= level
;
2025 void Peak_next_ak_k(Peak
*unit
, int inNumSamples
)
2027 float *out
= ZOUT(0);
2029 float curtrig
= ZIN0(1);
2031 inNumSamples
= INBUFLENGTH(0);
2032 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) {
2033 level
= std::abs(ZXP(in
));
2037 level
= unit
->mLevel
;
2039 level
= std::max(std::abs(ZXP(in
)), level
);
2042 unit
->m_prevtrig
= curtrig
;
2043 unit
->mLevel
= level
;
2046 void Peak_next_ai_k(Peak
*unit
, int inNumSamples
)
2048 float *out
= ZOUT(0);
2050 float level
= unit
->mLevel
;
2051 inNumSamples
= INBUFLENGTH(0);
2053 level
= std::max(std::abs(ZXP(in
)), level
);
2056 unit
->mLevel
= level
;
2059 void Peak_next_aa_k(Peak
*unit
, int inNumSamples
)
2061 float *out
= ZOUT(0);
2063 float *trig
= ZIN(1);
2064 float prevtrig
= unit
->m_prevtrig
;
2065 float level
= unit
->mLevel
;
2067 bool triggered
= false;
2069 inNumSamples
= INBUFLENGTH(0);
2071 float curtrig
= ZXP(trig
);
2072 float inlevel
= std::abs(ZXP(in
));
2073 level
= std::max(inlevel
, level
);
2074 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
2082 ZXP(out
) = out_level
;
2085 unit
->m_prevtrig
= prevtrig
;
2086 unit
->mLevel
= level
;
2090 void Peak_next_ak_k_nova(Peak
*unit
, int inNumSamples
)
2092 float *out
= ZOUT(0);
2094 float curtrig
= ZIN0(1);
2095 float level
= unit
->mLevel
;
2097 inNumSamples
= INBUFLENGTH(0);
2098 inlevel
= nova::peak_vec_simd(in
, &level
, inNumSamples
);
2100 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2101 unit
->m_prevtrig
= curtrig
;
2102 unit
->mLevel
= level
;
2105 void Peak_next_ai_k_nova(Peak
*unit
, int inNumSamples
)
2107 float *out
= ZOUT(0);
2109 float level
= unit
->mLevel
;
2111 inNumSamples
= INBUFLENGTH(0);
2112 inlevel
= nova::peak_vec_simd(in
, &level
, inNumSamples
);
2114 unit
->mLevel
= level
;
2118 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2120 void RunningMin_Ctor(RunningMin
*unit
)
2122 if (INRATE(1) == calc_FullRate
) {
2123 SETCALC(RunningMin_next_aa
);
2124 } else if (INRATE(1) == calc_ScalarRate
) {
2125 SETCALC(RunningMin_next_ai
);
2127 SETCALC(RunningMin_next_ak
);
2129 unit
->m_prevtrig
= 0.f
;
2130 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2133 void RunningMin_next_ak(RunningMin
*unit
, int inNumSamples
)
2135 float *out
= ZOUT(0);
2137 float curtrig
= ZIN0(1);
2138 float level
= unit
->mLevel
;
2142 if (inlevel
< level
) level
= inlevel
;
2145 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2146 unit
->m_prevtrig
= curtrig
;
2147 unit
->mLevel
= level
;
2150 void RunningMin_next_ai(RunningMin
*unit
, int inNumSamples
)
2152 float *out
= ZOUT(0);
2154 float level
= unit
->mLevel
;
2158 if (inlevel
< level
) level
= inlevel
;
2161 unit
->mLevel
= level
;
2164 void RunningMin_next_aa(RunningMin
*unit
, int inNumSamples
)
2166 float *out
= ZOUT(0);
2168 float *trig
= ZIN(1);
2169 float prevtrig
= unit
->m_prevtrig
;
2170 float level
= unit
->mLevel
;
2173 float curtrig
= ZXP(trig
);
2174 float inlevel
= ZXP(in
);
2175 if (inlevel
< level
) level
= inlevel
;
2177 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2180 unit
->m_prevtrig
= prevtrig
;
2181 unit
->mLevel
= level
;
2185 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2186 void RunningMax_Ctor(RunningMax
*unit
)
2188 if (INRATE(1) == calc_FullRate
) {
2189 SETCALC(RunningMax_next_aa
);
2190 } else if (INRATE(1) == calc_ScalarRate
) {
2191 SETCALC(RunningMax_next_ai
);
2193 SETCALC(RunningMax_next_ak
);
2195 unit
->m_prevtrig
= 0.f
;
2196 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2199 void RunningMax_next_ak(RunningMax
*unit
, int inNumSamples
)
2201 float *out
= ZOUT(0);
2203 float curtrig
= ZIN0(1);
2204 float level
= unit
->mLevel
;
2208 if (inlevel
> level
) level
= inlevel
;
2211 if (unit
->m_prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2212 unit
->m_prevtrig
= curtrig
;
2213 unit
->mLevel
= level
;
2216 void RunningMax_next_ai(RunningMax
*unit
, int inNumSamples
)
2218 float *out
= ZOUT(0);
2220 float level
= unit
->mLevel
;
2224 if (inlevel
> level
) level
= inlevel
;
2227 unit
->mLevel
= level
;
2230 void RunningMax_next_aa(RunningMax
*unit
, int inNumSamples
)
2232 float *out
= ZOUT(0);
2234 float *trig
= ZIN(1);
2235 float prevtrig
= unit
->m_prevtrig
;
2236 float level
= unit
->mLevel
;
2239 float curtrig
= ZXP(trig
);
2240 float inlevel
= ZXP(in
);
2241 if (inlevel
> level
) level
= inlevel
;
2243 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) level
= inlevel
;
2246 unit
->m_prevtrig
= prevtrig
;
2247 unit
->mLevel
= level
;
2251 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2254 void PeakFollower_Ctor(PeakFollower
*unit
)
2257 if (INRATE(1) == calc_ScalarRate
) {
2258 SETCALC(PeakFollower_next_ai
);
2260 SETCALC(PeakFollower_next
);
2263 unit
->mDecay
= ZIN0(1);
2264 ZOUT0(0) = unit
->mLevel
= ZIN0(0);
2267 void PeakFollower_next(PeakFollower
*unit
, int inNumSamples
)
2269 float *out
= ZOUT(0);
2271 float decay
= ZIN0(1);
2272 float level
= unit
->mLevel
;
2275 if(decay
== unit
->mDecay
) {
2277 float inlevel
= std::abs(ZXP(in
));
2278 if (inlevel
>= level
) {
2281 level
= inlevel
+ decay
* (level
- inlevel
);
2288 float decay_slope
= CALCSLOPE(decay
, unit
->mDecay
);
2289 if (decay
>= 0.f
&& unit
->mDecay
>= 0.f
) {
2291 float inlevel
= std::abs(ZXP(in
));
2292 if (inlevel
>= level
) {
2295 level
= inlevel
+ decay
* (level
- inlevel
);
2296 decay
+= decay_slope
;
2300 } else if (decay
<= 0.f
&& unit
->mDecay
<= 0.f
) {
2302 float inlevel
= std::abs(ZXP(in
));
2303 if (inlevel
>= level
) {
2306 level
= inlevel
+ decay
* (level
+ inlevel
);
2307 decay
+= decay_slope
;
2313 float inlevel
= std::abs(ZXP(in
));
2314 if (inlevel
>= level
) {
2317 level
= (1.f
- std::abs(decay
)) * inlevel
+ decay
* level
;
2318 decay
+= decay_slope
;
2325 unit
->mLevel
= level
;
2326 unit
->mDecay
= decay
;
2330 void PeakFollower_next_ai(PeakFollower
*unit
, int inNumSamples
)
2332 float *out
= ZOUT(0);
2334 float decay
= ZIN0(1);
2335 float level
= unit
->mLevel
;
2338 float inlevel
= std::abs(ZXP(in
));
2339 if (inlevel
>= level
) {
2342 level
= inlevel
+ decay
* (level
- inlevel
);
2347 unit
->mLevel
= level
;
2351 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2354 void MostChange_Ctor(MostChange
*unit
)
2356 if (INRATE(0) == calc_FullRate
) {
2357 if (INRATE(1) == calc_FullRate
) {
2358 SETCALC(MostChange_next_aa
);
2360 SETCALC(MostChange_next_ak
);
2363 if (INRATE(1) == calc_FullRate
) {
2364 SETCALC(MostChange_next_ka
);
2366 SETCALC(MostChange_next_aa
);
2372 MostChange_next_aa(unit
, 1);
2375 void MostChange_next_ak(MostChange
*unit
, int inNumSamples
)
2377 float *out
= ZOUT(0);
2380 float prevA
= unit
->mPrevA
;
2381 float prevB
= unit
->mPrevB
;
2382 int recent
= unit
->mRecent
;
2385 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2389 } else if (diff
< 0.f
) {
2393 ZXP(out
) = recent
? xb
: xa
;
2398 unit
->mPrevA
= prevA
;
2399 unit
->mPrevB
= prevB
;
2400 unit
->mRecent
= recent
;
2403 void MostChange_next_aa(MostChange
*unit
, int inNumSamples
)
2405 float *out
= ZOUT(0);
2408 float prevA
= unit
->mPrevA
;
2409 float prevB
= unit
->mPrevB
;
2410 int recent
= unit
->mRecent
;
2414 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2418 } else if (diff
< 0.f
) {
2422 ZXP(out
) = recent
? xb
: xa
;
2427 unit
->mPrevA
= prevA
;
2428 unit
->mPrevB
= prevB
;
2429 unit
->mRecent
= recent
;
2432 void MostChange_next_ka(MostChange
*unit
, int inNumSamples
)
2434 float *out
= ZOUT(0);
2437 float prevA
= unit
->mPrevA
;
2438 float prevB
= unit
->mPrevB
;
2439 int recent
= unit
->mRecent
;
2442 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2446 } else if (diff
< 0.f
) {
2450 ZXP(out
) = recent
? xb
: xa
;
2455 unit
->mPrevA
= prevA
;
2456 unit
->mPrevB
= prevB
;
2457 unit
->mRecent
= recent
;
2461 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2463 void LeastChange_Ctor(LeastChange
*unit
)
2465 if (INRATE(0) == calc_FullRate
) {
2466 if (INRATE(1) == calc_FullRate
) {
2467 SETCALC(LeastChange_next_aa
);
2469 SETCALC(LeastChange_next_ak
);
2472 if (INRATE(1) == calc_FullRate
) {
2473 SETCALC(LeastChange_next_ka
);
2475 SETCALC(LeastChange_next_aa
);
2481 LeastChange_next_aa(unit
, 1);
2484 void LeastChange_next_ak(LeastChange
*unit
, int inNumSamples
)
2486 float *out
= ZOUT(0);
2489 float prevA
= unit
->mPrevA
;
2490 float prevB
= unit
->mPrevB
;
2491 int recent
= unit
->mRecent
;
2494 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2498 } else if (diff
> 0.f
) {
2502 ZXP(out
) = recent
? xb
: xa
;
2507 unit
->mPrevA
= prevA
;
2508 unit
->mPrevB
= prevB
;
2509 unit
->mRecent
= recent
;
2512 void LeastChange_next_aa(LeastChange
*unit
, int inNumSamples
)
2514 float *out
= ZOUT(0);
2517 float prevA
= unit
->mPrevA
;
2518 float prevB
= unit
->mPrevB
;
2519 int recent
= unit
->mRecent
;
2523 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2527 } else if (diff
> 0.f
) {
2531 ZXP(out
) = recent
? xb
: xa
;
2536 unit
->mPrevA
= prevA
;
2537 unit
->mPrevB
= prevB
;
2538 unit
->mRecent
= recent
;
2541 void LeastChange_next_ka(LeastChange
*unit
, int inNumSamples
)
2543 float *out
= ZOUT(0);
2546 float prevA
= unit
->mPrevA
;
2547 float prevB
= unit
->mPrevB
;
2548 int recent
= unit
->mRecent
;
2551 float diff
= std::abs(xa
- prevA
) - std::abs(xb
- prevB
);
2555 } else if (diff
> 0.f
) {
2559 ZXP(out
) = recent
? xb
: xa
;
2564 unit
->mPrevA
= prevA
;
2565 unit
->mPrevB
= prevB
;
2566 unit
->mRecent
= recent
;
2570 ////////////////////
2572 void LastValue_Ctor(LastValue
*unit
)
2574 if (INRATE(0) == calc_FullRate
) {
2575 SETCALC(LastValue_next_ak
);
2577 SETCALC(LastValue_next_kk
);
2580 unit
->mPrev
= ZIN0(0);
2581 unit
->mCurr
= ZIN0(0);
2582 LastValue_next_kk(unit
, 1);
2585 void LastValue_next_kk(LastValue
*unit
, int inNumSamples
)
2587 float *out
= ZOUT(0);
2588 float inval
= ZIN0(0);
2589 float delta
= ZIN0(1);
2590 float diff
= std::abs(inval
- unit
->mCurr
);
2592 unit
->mPrev
= unit
->mCurr
;
2593 unit
->mCurr
= inval
;
2595 float level
= unit
->mPrev
;
2596 LOOP1(inNumSamples
, ZXP(out
) = level
; );
2600 void LastValue_next_ak(LastValue
*unit
, int inNumSamples
)
2602 float *out
= ZOUT(0);
2604 float delta
= ZIN0(1);
2605 float prev
= unit
->mPrev
;
2606 float curr
= unit
->mCurr
;
2609 float inval
= ZXP(in
);
2610 float diff
= std::abs(inval
- curr
);
2624 //////////////////////////////////////////////////////////////////////////////////////////
2626 void Done_Ctor(Done
*unit
)
2630 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2637 void Done_next(Done
*unit
, int inNumSamples
)
2639 float *out
= OUT(0);
2640 Unit
*src
= unit
->m_src
;
2641 if (src
) *out
= src
->mDone
? 1.f
: 0.f
;
2645 //////////////////////////////////////////////////////////////////////////////////////////
2647 void FreeSelf_Ctor(FreeSelf
*unit
)
2649 SETCALC(FreeSelf_next
);
2650 unit
->m_prevtrig
= 0.f
;
2651 FreeSelf_next(unit
, 1);
2655 void FreeSelf_next(FreeSelf
*unit
, int inNumSamples
)
2658 if (in
> 0.f
&& unit
->m_prevtrig
<= 0.f
) {
2659 NodeEnd(&unit
->mParent
->mNode
);
2661 unit
->m_prevtrig
= in
;
2664 //////////////////////////////////////////////////////////////////////////////////////////
2666 void PauseSelf_Ctor(PauseSelf
*unit
)
2668 SETCALC(PauseSelf_next
);
2669 unit
->m_prevtrig
= 0.f
;
2670 PauseSelf_next(unit
, 1);
2674 void PauseSelf_next(PauseSelf
*unit
, int inNumSamples
)
2677 if (in
> 0.f
&& unit
->m_prevtrig
<= 0.f
) {
2678 NodeRun(&unit
->mParent
->mNode
, 0);
2680 unit
->m_prevtrig
= in
;
2683 //////////////////////////////////////////////////////////////////////////////////////////
2685 void Pause_Ctor(Pause
*unit
)
2687 SETCALC(Pause_next
);
2695 void Pause_next(Pause
*unit
, int inNumSamples
)
2698 int state
= in
== 0.f
? 0 : 1;
2699 if (state
!= unit
->m_state
) {
2700 unit
->m_state
= state
;
2701 int id
= (int)ZIN0(1);
2702 Node
*node
= SC_GetNode(unit
->mWorld
, id
);
2704 NodeRun(node
, state
);
2710 //////////////////////////////////////////////////////////////////////////////////////////
2712 void Free_Ctor(Free
*unit
)
2716 unit
->m_prevtrig
= 0.f
;
2722 void Free_next(Free
*unit
, int inNumSamples
)
2724 float trig
= ZIN0(0);
2725 if (trig
> 0.f
&& unit
->m_prevtrig
<= 0) {
2726 int id
= (int)ZIN0(1);
2727 Node
*node
= SC_GetNode(unit
->mWorld
, id
);
2732 unit
->m_prevtrig
= trig
;
2736 //////////////////////////////////////////////////////////////////////////////////////////
2738 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone
*unit
)
2740 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2743 SETCALC(FreeSelfWhenDone_next
);
2744 FreeSelfWhenDone_next(unit
, 1);
2746 SETCALC(ClearUnitOutputs
);
2747 ClearUnitOutputs(unit
, 1);
2752 void FreeSelfWhenDone_next(FreeSelfWhenDone
*unit
, int inNumSamples
)
2754 float *out
= OUT(0);
2756 Unit
*src
= unit
->m_src
;
2758 NodeEnd(&unit
->mParent
->mNode
);
2759 SETCALC(ClearUnitOutputs
);
2764 //////////////////////////////////////////////////////////////////////////////////////////
2766 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone
*unit
)
2768 unit
->m_src
= unit
->mInput
[0]->mFromUnit
;
2771 SETCALC(PauseSelfWhenDone_next
);
2772 PauseSelfWhenDone_next(unit
, 1);
2774 SETCALC(ClearUnitOutputs
);
2775 ClearUnitOutputs(unit
, 1);
2780 void PauseSelfWhenDone_next(PauseSelfWhenDone
*unit
, int inNumSamples
)
2782 float *out
= OUT(0);
2784 Unit
*src
= unit
->m_src
;
2786 NodeRun(&unit
->mParent
->mNode
, 0);
2787 SETCALC(ClearUnitOutputs
);
2792 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2797 // rate, level lag, replyid, channel count, [channels, ], cmd name size, [cmdname, ]
2798 static const int rateIndex
= 0;
2799 static const int levelLagIndex
= 1;
2800 static const int replyIdIndex
= 2;
2801 static const int channelCountIndex
= 3;
2802 static const int signalStartIndex
= 4;
2806 SendPeakRMS
* unit
= this;
2808 mChannelCount
= (unsigned int)IN0(channelCountIndex
);
2809 size_t channelDataAllocSize
= mChannelCount
* 3 * sizeof(float);
2811 int cmdSizeIndex
= signalStartIndex
+ mChannelCount
;
2812 size_t cmdNameSize
= IN0(cmdSizeIndex
);
2813 size_t cmdNameAllocSize
= (cmdNameSize
+ 1) * sizeof(char);
2815 void * allocData
= RTAlloc(unit
->mWorld
, channelDataAllocSize
+ cmdNameAllocSize
);
2817 memset(allocData
, 0, channelDataAllocSize
);
2818 mChannelData
= (float*)allocData
;
2820 char * cmdName
= (char*)(allocData
) + channelDataAllocSize
;
2822 size_t cmdNameIndex
= cmdSizeIndex
+ 1;
2823 for(int i
= 0; i
< cmdNameSize
; i
++)
2824 cmdName
[i
] = (char)IN0(cmdNameIndex
+ i
);
2825 cmdName
[cmdNameSize
] = 0;
2827 if ((FULLBUFLENGTH
& 15) == 0) {
2828 if (mCalcRate
== calc_FullRate
)
2829 SETCALC(SendPeakRMS::perform_a
<true>);
2831 SETCALC(SendPeakRMS::perform_k
<true>);
2833 if (mCalcRate
== calc_FullRate
)
2834 SETCALC(SendPeakRMS::perform_a
<false>);
2836 SETCALC(SendPeakRMS::perform_k
<false>);
2839 float replyRate
= IN0(rateIndex
);
2841 mAudioSamplesPerTick
= FULLRATE
/ replyRate
;
2842 mControlSamplesPerTick
= BUFRATE
/ replyRate
;
2844 mPhaseRemain
= (mCalcRate
== calc_FullRate
) ? mAudioSamplesPerTick
2845 : mControlSamplesPerTick
;
2847 float32 lag
= ZIN0(levelLagIndex
);
2848 mB1
= (lag
!= 0.f
) ? exp(log001
/ (lag
* replyRate
))
2854 SendPeakRMS
* unit
= this;
2855 RTFree(unit
->mWorld
, mChannelData
);
2858 unsigned int mChannelCount
;
2859 float * mChannelData
;
2862 int mAudioSamplesPerTick
;
2863 int mControlSamplesPerTick
;
2866 void performLevelLag(float & out
, float y0
, float & y1
)
2871 out
= y1
= y0
+ mB1
* (y1
- y0
);
2874 char * getCmdName (void)
2876 void * buffer
= mChannelData
;
2877 return (char*)(buffer
) + mChannelCount
* 3 * sizeof(float);
2880 void sendReply(void)
2882 SendPeakRMS
* unit
= this;
2883 float * reply
= (float*)alloca(mChannelCount
* 2 * sizeof(float));
2886 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2887 float & maxLevel
= reply
[2*i
];
2888 float & rms
= reply
[2*i
+ 1];
2890 performLevelLag(maxLevel
, mChannelData
[2*i
], mChannelData
[2*mChannelCount
+ i
]);
2892 if (INRATE(signalStartIndex
+ i
) == calc_FullRate
)
2893 rms
= std::sqrt(mChannelData
[2*i
+ 1] / (float)mAudioSamplesPerTick
);
2895 rms
= std::sqrt(mChannelData
[2*i
+ 1] / (float)mControlSamplesPerTick
);
2898 SendNodeReply(&unit
->mParent
->mNode
, (int)ZIN0(replyIdIndex
), getCmdName(), mChannelCount
*2, reply
);
2899 memset(mChannelData
, 0, mChannelCount
* 2 * sizeof(float));
2902 template <bool simd
>
2903 void analyzeFullBlock(void)
2905 SendPeakRMS
* unit
= this;
2906 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2907 float * in
= IN(signalStartIndex
+ i
);
2908 int numSamples
= INBUFLENGTH(signalStartIndex
+ i
);
2910 float & level
= mChannelData
[2*i
];
2911 float & sqrsum
= mChannelData
[2*i
+ 1];
2912 if (numSamples
== 1)
2913 nova::peak_rms_vec(in
, &level
, &sqrsum
, 1);
2916 nova::peak_rms_vec_simd(in
, &level
, &sqrsum
, numSamples
);
2918 nova::peak_rms_vec(in
, &level
, &sqrsum
, numSamples
);
2923 void analyzePartialBlock(int firstSample
, int samplesToAnalyze
)
2925 SendPeakRMS
* unit
= this;
2926 for (int i
= 0; i
!= mChannelCount
; ++i
) {
2927 float * in
= IN(signalStartIndex
+ i
) + firstSample
;
2928 int numSamples
= INBUFLENGTH(signalStartIndex
+ i
);
2930 float & level
= mChannelData
[2*i
];
2931 float & sqrsum
= mChannelData
[2*i
+ 1];
2932 if (numSamples
== 1) {
2933 if (firstSample
== 0)
2934 nova::peak_rms_vec(in
, &level
, &sqrsum
, 1);
2936 if (!(samplesToAnalyze
& 15) && !(firstSample
& 3)) // check for unrolling and alignment
2937 nova::peak_rms_vec_simd(in
, &level
, &sqrsum
, samplesToAnalyze
);
2939 nova::peak_rms_vec(in
, &level
, &sqrsum
, samplesToAnalyze
);
2944 template <bool simd
>
2945 inline void next_k(int inNumSamples
)
2949 if (mPhaseRemain
<= 0) {
2950 mPhaseRemain
+= mControlSamplesPerTick
;
2954 analyzeFullBlock
<simd
>();
2957 template <bool simd
>
2958 inline void next_a(int inNumSamples
)
2960 if (mPhaseRemain
>= inNumSamples
) {
2961 mPhaseRemain
-= inNumSamples
;
2962 analyzeFullBlock
<simd
>();
2964 if (mPhaseRemain
== 0) {
2966 mPhaseRemain
= mAudioSamplesPerTick
;
2969 int startSample
= 0;
2970 int samplesToAnalyze
= std::min(mPhaseRemain
, inNumSamples
);
2971 int remain
= inNumSamples
;
2974 analyzePartialBlock(startSample
, samplesToAnalyze
);
2976 startSample
+= samplesToAnalyze
;
2977 mPhaseRemain
-= samplesToAnalyze
;
2978 if (mPhaseRemain
== 0) {
2980 mPhaseRemain
= mAudioSamplesPerTick
;
2983 remain
-= samplesToAnalyze
;
2984 samplesToAnalyze
= std::min(remain
, mPhaseRemain
);
2989 template <bool simd
>
2990 static void perform_k(Unit
* unit
, int inNumSamples
)
2992 static_cast<SendPeakRMS
*>(unit
)->next_k
<simd
>(inNumSamples
);
2995 template <bool simd
>
2996 static void perform_a(Unit
* unit
, int inNumSamples
)
2998 static_cast<SendPeakRMS
*>(unit
)->next_a
<simd
>(inNumSamples
);
3002 static void SendPeakRMS_Ctor(SendPeakRMS
* unit
) { new(unit
) SendPeakRMS (); }
3003 static void SendPeakRMS_Dtor(SendPeakRMS
* unit
) { unit
->~SendPeakRMS (); }
3005 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3011 DefineSimpleUnit(Trig1
);
3012 DefineSimpleUnit(Trig
);
3013 DefineSimpleUnit(SendTrig
);
3014 DefineDtorUnit(SendReply
);
3015 DefineDtorUnit(Poll
);
3016 DefineSimpleUnit(ToggleFF
);
3017 DefineSimpleUnit(SetResetFF
);
3018 DefineSimpleUnit(Latch
);
3019 DefineSimpleUnit(Gate
);
3020 DefineSimpleUnit(Schmidt
);
3021 DefineSimpleUnit(PulseDivider
);
3022 DefineSimpleUnit(PulseCount
);
3023 DefineSimpleUnit(Stepper
);
3024 DefineSimpleUnit(TDelay
);
3025 DefineSimpleUnit(ZeroCrossing
);
3026 DefineSimpleUnit(Timer
);
3027 DefineSimpleUnit(Sweep
);
3028 DefineSimpleUnit(Phasor
);
3029 DefineSimpleUnit(Peak
);
3030 DefineSimpleUnit(RunningMin
);
3031 DefineSimpleUnit(RunningMax
);
3032 DefineSimpleUnit(PeakFollower
);
3033 DefineSimpleUnit(MostChange
);
3034 DefineSimpleUnit(LeastChange
);
3035 DefineSimpleUnit(LastValue
);
3036 DefineSimpleUnit(Done
);
3037 DefineSimpleUnit(Pause
);
3038 DefineSimpleUnit(FreeSelf
);
3039 DefineSimpleUnit(PauseSelf
);
3040 DefineSimpleUnit(Free
);
3041 DefineSimpleUnit(FreeSelfWhenDone
);
3042 DefineSimpleUnit(PauseSelfWhenDone
);
3044 DefineDtorUnit(SendPeakRMS
);