FFT: Prevent user from attempting hops smaller than SC's block size
[supercollider.git] / server / plugins / TriggerUGens.cpp
blob1d4aeff4e5501f5b285235445272652c787fbf8d
1 /*
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"
28 #ifdef NOVA_SIMD
29 #include "simd_memory.hpp"
31 #include "function_attributes.h"
33 #endif
35 static InterfaceTable *ft;
37 //////////////////////////////////////////////////////////////////////////////////////////////////
39 struct Trig1 : public Unit
41 float m_prevtrig;
42 long mCounter;
45 struct Trig : public Unit
47 float mLevel;
48 float m_prevtrig;
49 long mCounter;
52 struct SendTrig : public Unit
54 float m_prevtrig;
57 struct SendReply : public Unit
59 float m_prevtrig;
60 int m_valueSize;
61 int m_valueOffset;
62 float *m_values;
63 int m_cmdNameSize;
64 char *m_cmdName;
68 struct Poll : public Unit
70 int m_samplesRemain, m_intervalSamples;
71 float m_trig;
72 float m_lastPoll, m_id;
73 char *m_id_string;
74 bool m_mayprint;
78 struct ToggleFF : public Unit
80 float mLevel;
81 float m_prevtrig;
84 struct SetResetFF : public Unit
86 float mLevel;
87 float m_prevtrig, m_prevreset;
90 struct Latch : public Unit
92 float mLevel;
93 float m_prevtrig;
96 struct Gate : public Unit
98 float mLevel;
101 struct Schmidt : public Unit
103 float mLevel;
106 struct PulseDivider : public Unit
108 float mLevel;
109 float m_prevtrig;
110 long mCounter;
113 struct PulseCount : public Unit
115 float mLevel;
116 float m_prevtrig, m_prevreset;
119 struct Stepper : public Unit
121 float mLevel;
122 float m_prevtrig, m_prevreset;
125 struct TDelay : public Unit
127 long mCounter;
128 float m_prevtrig;
131 struct ZeroCrossing : public Unit
133 float mLevel, m_prevfrac, m_previn;
134 int32 mCounter;
137 struct Timer : public Unit
139 float mLevel, m_prevfrac, m_previn;
140 int32 mCounter;
143 struct Sweep : public Unit
145 double mLevel;
146 float m_previn;
149 struct Phasor : public Unit
151 double mLevel;
152 float m_previn;
155 struct Peak : public Unit
157 float mLevel;
158 float m_prevtrig;
161 struct RunningMin : public Unit
163 float mLevel;
164 float m_prevtrig;
167 struct RunningMax : public Unit
169 float mLevel;
170 float m_prevtrig;
173 struct PeakFollower : public Unit
175 float mLevel;
176 float mDecay;
179 struct MostChange : public Unit
181 float mPrevA, mPrevB;
182 int mRecent;
185 struct LeastChange : public Unit
187 float mPrevA, mPrevB;
188 int mRecent;
191 struct LastValue : public Unit
193 float mPrev;
194 float mCurr;
199 struct Done : public Unit
201 Unit *m_src;
204 struct FreeSelf : public Unit
206 float m_prevtrig;
209 struct PauseSelf : public Unit
211 float m_prevtrig;
214 struct Pause : public Unit
216 int m_state;
219 struct Free : public Unit
221 float m_prevtrig;
224 struct FreeSelfWhenDone : public Unit
226 Unit *m_src;
229 struct PauseSelfWhenDone : public Unit
231 Unit *m_src;
234 extern "C"
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 ////////////////////////////////////////////////////////////////////////////////////////////////////////
372 #ifdef NOVA_SIMD
373 FLATTEN void Trig1_next_nova(Trig1 *unit, int inNumSamples);
374 FLATTEN void Trig1_next_k_nova(Trig1 *unit, int inNumSamples);
375 #endif
377 void Trig1_Ctor(Trig1 *unit)
379 if (unit->mCalcRate == calc_FullRate && INRATE(0) != calc_FullRate) {
380 #ifdef NOVA_SIMD
381 if (!(BUFLENGTH & 15))
382 SETCALC(Trig1_next_k_nova);
383 else
384 #endif
385 SETCALC(Trig1_next_k);
386 } else {
387 #ifdef NOVA_SIMD
388 if (!(BUFLENGTH & 15))
389 SETCALC(Trig1_next_nova);
390 else
391 #endif
392 SETCALC(Trig1_next);
394 unit->mCounter = 0;
395 unit->m_prevtrig = 0.f;
397 ZOUT0(0) = 0.f;
400 void Trig1_next(Trig1 *unit, int inNumSamples)
402 float *out = ZOUT(0);
403 float *trig = ZIN(0);
404 float dur = ZIN0(1);
405 float sr = unit->mRate->mSampleRate;
406 float prevtrig = unit->m_prevtrig;
407 unsigned long counter = unit->mCounter;
409 LOOP1(inNumSamples,
410 float curtrig = ZXP(trig);
411 float zout;
412 if (counter > 0) {
413 zout = --counter ? 1.f : 0.f;
414 } else {
415 if (curtrig > 0.f && prevtrig <= 0.f) {
416 counter = (long)(dur * sr + .5f);
417 if (counter < 1) counter = 1;
418 zout = 1.f;
419 } else {
420 zout = 0.f;
423 prevtrig = curtrig;
424 ZXP(out) = zout;
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);
434 float dur = ZIN0(1);
435 float sr = unit->mRate->mSampleRate;
436 float prevtrig = unit->m_prevtrig;
437 unsigned long counter = unit->mCounter;
439 LOOP1(inNumSamples,
440 float zout;
441 if (counter > 0) {
442 zout = --counter ? 1.f : 0.f;
443 } else {
444 if (curtrig > 0.f && prevtrig <= 0.f) {
445 counter = (long)(dur * sr + .5f);
446 if (counter < 1) counter = 1;
447 zout = 1.f;
448 } else {
449 zout = 0.f;
452 prevtrig = curtrig;
453 ZXP(out) = zout;
455 unit->m_prevtrig = prevtrig;
456 unit->mCounter = counter;
459 #ifdef NOVA_SIMD
460 void Trig1_next_nova(Trig1 *unit, int inNumSamples)
462 float *out = ZOUT(0);
463 float *trig = ZIN(0);
464 float dur = ZIN0(1);
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;
473 assert(counter > 0);
474 prevtrig = IN(0)[inNumSamples-1];
476 else
478 LOOP1(inNumSamples,
479 float curtrig = ZXP(trig);
480 float zout;
481 if (counter > 0) {
482 zout = --counter ? 1.f : 0.f;
483 } else {
484 if (curtrig > 0.f && prevtrig <= 0.f) {
485 counter = (long)(dur * sr + .5f);
486 if (counter < 1) counter = 1;
487 zout = 1.f;
488 } else {
489 zout = 0.f;
492 prevtrig = curtrig;
493 ZXP(out) = zout;
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);
505 float dur = ZIN0(1);
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;
514 assert(counter > 0);
515 prevtrig = curtrig;
517 else if (counter == 0 && (curtrig <= 0.f || prevtrig > 0.f))
519 nova::zerovec_simd(OUT(0), inNumSamples);
520 prevtrig = curtrig;
522 else
524 LOOP1(inNumSamples,
525 float zout;
526 if (counter > 0) {
527 zout = --counter ? 1.f : 0.f;
528 } else {
529 if (curtrig > 0.f && prevtrig <= 0.f) {
530 counter = (long)(dur * sr + .5f);
531 if (counter < 1) counter = 1;
532 zout = 1.f;
533 } else {
534 zout = 0.f;
537 prevtrig = curtrig;
538 ZXP(out) = zout;
542 unit->m_prevtrig = prevtrig;
543 unit->mCounter = counter;
545 #endif
548 ////////////////////////////////////////////////////////////////////////////////////////////////////////
550 #ifdef NOVA_SIMD
551 void Trig_next_nova(Trig *unit, int inNumSamples);
552 void Trig_next_k_nova(Trig *unit, int inNumSamples);
553 #endif
555 void Trig_Ctor(Trig *unit)
557 if (unit->mCalcRate == calc_FullRate && INRATE(0) != calc_FullRate) {
558 #ifdef NOVA_SIMD
559 if (!(BUFLENGTH & 15))
560 SETCALC(Trig_next_k_nova);
561 else
562 #endif
563 SETCALC(Trig_next_k);
564 } else {
565 #ifdef NOVA_SIMD
566 if (!(BUFLENGTH & 15))
567 SETCALC(Trig_next_nova);
568 else
569 #endif
570 SETCALC(Trig_next);
573 unit->mCounter = 0;
574 unit->m_prevtrig = 0.f;
575 unit->mLevel = 0.f;
577 ZOUT0(0) = 0.f;
580 void Trig_next(Trig *unit, int inNumSamples)
582 float *out = ZOUT(0);
583 float *trig = ZIN(0);
584 float dur = ZIN0(1);
585 float sr = unit->mRate->mSampleRate;
586 float prevtrig = unit->m_prevtrig;
587 float level = unit->mLevel;
588 unsigned long counter = unit->mCounter;
590 LOOP1(inNumSamples,
591 float curtrig = ZXP(trig);
592 float zout;
593 if (counter > 0) {
594 zout = --counter ? level : 0.f;
595 } else {
596 if (curtrig > 0.f && prevtrig <= 0.f) {
597 counter = (long)(dur * sr + .5f);
598 if (counter < 1) counter = 1;
599 level = curtrig;
600 zout = level;
601 } else {
602 zout = 0.f;
605 prevtrig = curtrig;
606 ZXP(out) = zout;
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);
617 float dur = ZIN0(1);
618 float sr = unit->mRate->mSampleRate;
619 float prevtrig = unit->m_prevtrig;
620 float level = unit->mLevel;
621 unsigned long counter = unit->mCounter;
623 LOOP1(inNumSamples,
624 float zout;
625 if (counter > 0) {
626 zout = --counter ? level : 0.f;
627 } else {
628 if (curtrig > 0.f && prevtrig <= 0.f) {
629 counter = (long)(dur * sr + .5f);
630 if (counter < 1) counter = 1;
631 level = curtrig;
632 zout = level;
633 } else {
634 zout = 0.f;
637 prevtrig = curtrig;
638 ZXP(out) = zout;
640 unit->m_prevtrig = prevtrig;
641 unit->mCounter = counter;
642 unit->mLevel = level;
645 #ifdef NOVA_SIMD
646 void Trig_next_nova(Trig *unit, int inNumSamples)
648 float *out = ZOUT(0);
649 float *trig = ZIN(0);
650 float dur = ZIN0(1);
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;
660 assert(counter > 0);
661 prevtrig = IN(0)[inNumSamples-1];
663 else
665 LOOP1(inNumSamples,
666 float curtrig = ZXP(trig);
667 float zout;
668 if (counter > 0) {
669 zout = --counter ? level : 0.f;
670 } else {
671 if (curtrig > 0.f && prevtrig <= 0.f) {
672 counter = (long)(dur * sr + .5f);
673 if (counter < 1) counter = 1;
674 level = curtrig;
675 zout = level;
676 } else {
677 zout = 0.f;
680 prevtrig = curtrig;
681 ZXP(out) = zout;
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);
693 float dur = ZIN0(1);
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;
703 assert(counter > 0);
704 prevtrig = curtrig;
706 else if (counter == 0 && (curtrig <= 0.f || prevtrig > 0.f))
708 nova::zerovec_simd(OUT(0), inNumSamples);
709 prevtrig = curtrig;
711 else
713 LOOP1(inNumSamples,
714 float zout;
715 if (counter > 0) {
716 zout = --counter ? level : 0.f;
717 } else {
718 if (curtrig > 0.f && prevtrig <= 0.f) {
719 counter = (long)(dur * sr + .5f);
720 if (counter < 1) counter = 1;
721 level = curtrig;
722 zout = level;
723 } else {
724 zout = 0.f;
727 prevtrig = curtrig;
728 ZXP(out) = zout;
731 unit->m_prevtrig = prevtrig;
732 unit->mCounter = counter;
733 unit->mLevel = level;
735 #endif
737 ////////////////////////////////////////////////////////////////////////////////////////////////////////
739 void SendTrig_Ctor(SendTrig *unit)
741 if (INRATE(2) == calc_FullRate) {
742 SETCALC(SendTrig_next_aka);
743 } else {
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;
754 LOOP1(inNumSamples,
755 float curtrig = ZXP(trig);
756 if (curtrig > 0.f && prevtrig <= 0.f) {
757 SendTrigger(&unit->mParent->mNode, (int)ZIN0(1), ZIN0(2));
759 prevtrig = curtrig;
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;
770 LOOP1(inNumSamples,
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);
776 prevtrig = curtrig;
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;
792 // allocations
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);
797 // terminate string
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);
805 } else {
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)
819 float *trig = IN(0);
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);
832 prevtrig = curtrig;
834 unit->m_prevtrig = prevtrig;
837 void SendReply_next_aka(SendReply *unit, int inNumSamples)
839 float *trig = IN(0);
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);
854 prevtrig = curtrig;
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);
867 } else {
868 SETCALC(Poll_next_ak);
870 } else {
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){
893 float* in = IN(1);
894 float* trig = IN(0);
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]);
903 lasttrig = trig[i];
905 unit->m_trig = lasttrig;
908 void Poll_next_kk(Poll *unit, int inNumSamples){
909 float in = IN0(1);
910 float trig = IN0(0);
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);
917 unit->m_trig = trig;
921 void Poll_next_ak(Poll *unit, int inNumSamples){
922 float in = IN0(1);
923 float* trig = IN(0);
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);
932 lasttrig = trig[i];
934 unit->m_trig = lasttrig;
939 ////////////////////////////////////////////////////////////////////////////////////////////////////////
941 void SetResetFF_Ctor(SetResetFF *unit)
943 if (INRATE(1) == calc_FullRate) {
944 SETCALC(SetResetFF_next_a);
945 } else {
946 SETCALC(SetResetFF_next_k);
949 unit->m_prevtrig = 0.f;
950 unit->m_prevreset = 0.f;
951 unit->mLevel = 0.f;
953 ZOUT0(0) = 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;
966 LOOP1(inNumSamples,
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;
971 ZXP(out) = level;
972 prevtrig = curtrig;
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;
992 ZXP(out) = level;
993 prevtrig = curtrig;
995 LOOP(inNumSamples - 1,
996 curtrig = ZXP(trig);
997 if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f;
998 ZXP(out) = level;
999 prevtrig = curtrig;
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;
1014 unit->mLevel = 0.f;
1016 ZOUT0(0) = 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;
1027 LOOP1(inNumSamples,
1028 float curtrig = ZXP(trig);
1029 if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f - level;
1030 ZXP(out) = level;
1031 prevtrig = curtrig;
1033 unit->m_prevtrig = prevtrig;
1034 unit->mLevel = level;
1037 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1040 #ifdef NOVA_SIMD
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;
1066 #endif
1068 void Latch_Ctor(Latch *unit)
1070 if (INRATE(1) == calc_FullRate) {
1071 SETCALC(Latch_next_aa);
1072 } else {
1073 #ifdef NOVA_SIMD
1074 if (BUFLENGTH == 64)
1075 SETCALC(Latch_next_ak_nova_64);
1076 if (!(BUFLENGTH & 15))
1077 SETCALC(Latch_next_ak_nova);
1078 else
1079 #endif
1080 SETCALC(Latch_next_ak);
1083 unit->m_prevtrig = 0.f;
1084 unit->mLevel = 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);
1108 float *in = ZIN(0);
1109 float *trig = ZIN(1);
1110 float prevtrig = unit->m_prevtrig;
1111 float level = unit->mLevel;
1113 LOOP1(inNumSamples,
1114 float curtrig = ZXP(trig);
1115 if (prevtrig <= 0.f && curtrig > 0.f) level = ZXP(in);
1116 else { PZ(in); }
1117 ZXP(out) = level;
1118 prevtrig = curtrig;
1120 unit->m_prevtrig = prevtrig;
1121 unit->mLevel = level;
1126 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1128 #ifdef NOVA_SIMD
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];
1138 } else
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];
1151 } else
1152 nova::setvec_simd<64>(OUT(0), level);
1154 #endif
1156 void Gate_Ctor(Gate *unit)
1158 if (INRATE(1) == calc_FullRate) {
1159 SETCALC(Gate_next_aa);
1160 } else {
1161 #ifdef NOVA_SIMD
1162 if (BUFLENGTH == 64)
1163 SETCALC(Gate_next_ak_nova_64);
1164 if (!(BUFLENGTH & 15))
1165 SETCALC(Gate_next_ak_nova);
1166 else
1167 #endif
1168 SETCALC(Gate_next_ak);
1171 unit->mLevel = 0.f;
1173 ZOUT0(0) = 0.f;
1178 void Gate_next_ak(Gate *unit, int inNumSamples)
1180 float *out = ZOUT(0);
1181 float *in = ZIN(0);
1182 float *trig = ZIN(1);
1183 float level = unit->mLevel;
1185 float curtrig = ZXP(trig);
1186 if (curtrig > 0.f) {
1187 LOOP1(inNumSamples,
1188 level = ZXP(in);
1189 ZXP(out) = level;
1191 unit->mLevel = level;
1192 } else {
1193 LOOP1(inNumSamples,
1194 ZXP(out) = level;
1199 void Gate_next_aa(Gate *unit, int inNumSamples)
1201 float *out = ZOUT(0);
1202 float *in = ZIN(0);
1203 float *trig = ZIN(1);
1204 float level = unit->mLevel;
1206 LOOP1(inNumSamples,
1207 float curtrig = ZXP(trig);
1208 if (curtrig > 0.f) level = ZXP(in);
1209 else { PZ(in); }
1210 ZXP(out) = level;
1212 unit->mLevel = level;
1215 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1218 void Schmidt_Ctor(Schmidt *unit)
1220 SETCALC(Schmidt_next);
1222 unit->mLevel = 0.f;
1224 ZOUT0(0) = 0.f;
1227 void Schmidt_next(Schmidt *unit, int inNumSamples)
1229 float *out = ZOUT(0);
1230 float *in = ZIN(0);
1231 float lo = ZIN0(1);
1232 float hi = ZIN0(2);
1233 float level = unit->mLevel;
1234 LOOP1(inNumSamples,
1235 float zin = ZXP(in);
1236 if (level == 1.) {
1237 if (zin < lo) level = 0.f;
1238 } else {
1239 if (zin > hi) level = 1.f;
1241 ZXP(out) = level;
1243 unit->mLevel = level;
1246 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1248 void PulseDivider_Ctor(PulseDivider *unit)
1250 SETCALC(PulseDivider_next);
1252 unit->m_prevtrig = 0.f;
1253 unit->mLevel = 0.f;
1254 unit->mCounter = (long)floor(ZIN0(2) + 0.5);
1256 ZOUT0(0) = 0.f;
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;
1268 LOOP1(inNumSamples,
1269 float z;
1270 float curtrig = ZXP(trig);
1271 if (prevtrig <= 0.f && curtrig > 0.f) {
1272 counter++;
1273 if (counter >= div) {
1274 counter = 0;
1275 z = 1.f;
1276 } else {
1277 z = 0.f;
1279 } else {
1280 z = 0.f;
1282 ZXP(out) = z;
1283 prevtrig = curtrig;
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);
1298 } else {
1299 SETCALC(PulseCount_next_0);
1301 unit->m_prevtrig = 0.f;
1302 unit->m_prevreset = 0.f;
1303 unit->mLevel = 0.f;
1305 ZOUT0(0) = 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;
1318 LOOP1(inNumSamples,
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) {
1323 level += 1.f;
1325 ZXP(out) = level;
1326 prevtrig = curtrig;
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;
1343 LOOP1(inNumSamples,
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) {
1347 level += 1.f;
1349 ZXP(out) = level;
1350 prevtrig = curtrig;
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;
1365 LOOP1(inNumSamples,
1366 float curtrig = ZXP(trig);
1367 if (prevtrig <= 0.f && curtrig > 0.f) {
1368 level += 1.f;
1370 ZXP(out) = level;
1371 prevtrig = curtrig;
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);
1386 } else {
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;
1396 ZOUT0(0) = 0.f;
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;
1413 LOOP1(inNumSamples,
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);
1421 ZXP(out) = level;
1422 prevtrig = curtrig;
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;
1443 LOOP1(inNumSamples,
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);
1450 ZXP(out) = level;
1451 prevtrig = curtrig;
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;
1469 LOOP1(inNumSamples,
1470 float curtrig = ZXP(trig);
1471 if (prevtrig <= 0.f && curtrig > 0.f) {
1472 level = (float)sc_wrap((int32)level + step, zmin, zmax);
1474 ZXP(out) = level;
1475 prevtrig = curtrig;
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;
1488 unit->mCounter = 0;
1490 ZOUT0(0) = 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;
1503 LOOP1(inNumSamples,
1504 float curtrig = ZXP(trig);
1505 float zout;
1506 if (counter > 1) {
1507 counter--;
1508 zout = 0.f;
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;
1514 zout = 0.f;
1515 } else {
1516 counter = 0;
1517 zout = 1.f;
1519 ZXP(out) = zout;
1520 prevtrig = curtrig;
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;
1537 unit->mCounter = 0;
1540 void ZeroCrossing_next_a(ZeroCrossing *unit, int inNumSamples)
1542 float *out = ZOUT(0);
1543 float *in = ZIN(0);
1544 float previn = unit->m_previn;
1545 float prevfrac = unit->m_prevfrac;
1546 float level = unit->mLevel;
1547 long counter = unit->mCounter;
1549 LOOP1(inNumSamples,
1550 counter++;
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);
1555 prevfrac = frac;
1556 counter = 0;
1558 ZXP(out) = level;
1559 previn = curin;
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;
1577 unit->mCounter = 0;
1580 void Timer_next_a(Timer *unit, int inNumSamples)
1582 float *out = ZOUT(0);
1583 float *in = ZIN(0);
1584 float previn = unit->m_previn;
1585 float prevfrac = unit->m_prevfrac;
1586 float level = unit->mLevel;
1587 long counter = unit->mCounter;
1589 LOOP1(inNumSamples,
1590 counter++;
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);
1595 prevfrac = frac;
1596 counter = 0;
1598 ZXP(out) = level;
1599 previn = curin;
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);
1616 } else {
1617 SETCALC(Sweep_next_0k);
1619 } else if (INRATE(0) == calc_BufRate) {
1620 if (INRATE(1) == calc_FullRate) {
1621 SETCALC(Sweep_next_ka);
1622 } else {
1623 SETCALC(Sweep_next_kk);
1625 } else {
1626 if (INRATE(1) == calc_FullRate) {
1627 SETCALC(Sweep_next_aa);
1628 } else {
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;
1643 LOOP1(inNumSamples,
1644 level += rate;
1645 ZXP(out) = level;
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;
1658 LOOP1(inNumSamples,
1659 float zrate = ZXP(rate) * sampledur;
1660 level += zrate;
1661 ZXP(out) = level;
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;
1680 LOOP1(inNumSamples,
1681 level += rate;
1682 ZXP(out) = level;
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;
1703 LOOP1(inNumSamples,
1704 float zrate = ZXP(rate) * sampledur;
1705 level += zrate;
1706 ZXP(out) = level;
1709 unit->m_previn = curin;
1710 unit->mLevel = level;
1713 void Sweep_next_ak(Sweep *unit, int inNumSamples)
1715 float *out = ZOUT(0);
1716 float *in = ZIN(0);
1717 double rate = ZIN0(1) * SAMPLEDUR;
1718 float previn = unit->m_previn;
1719 double level = unit->mLevel;
1721 LOOP1(inNumSamples,
1722 float curin = ZXP(in);
1723 if (previn <= 0.f && curin > 0.f) {
1724 float frac = -previn/(curin-previn);
1725 level = frac * rate;
1726 } else {
1727 level += rate;
1729 ZXP(out) = level;
1730 previn = curin;
1733 unit->m_previn = previn;
1734 unit->mLevel = level;
1737 void Sweep_next_aa(Sweep *unit, int inNumSamples)
1739 float *out = ZOUT(0);
1740 float *in = ZIN(0);
1741 float *rate = ZIN(1);
1742 float previn = unit->m_previn;
1743 double level = unit->mLevel;
1744 float sampledur = SAMPLEDUR;
1746 LOOP1(inNumSamples,
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;
1752 } else {
1753 level += zrate;
1755 ZXP(out) = level;
1756 previn = curin;
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);
1771 } else {
1772 SETCALC(Phasor_next_ak);
1774 } else {
1775 SETCALC(Phasor_next_kk);
1777 } else {
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);
1789 float in = ZIN0(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) {
1799 level = resetPos;
1801 LOOP1(inNumSamples,
1802 level = sc_wrap(level, start, end);
1803 ZXP(out) = level;
1804 level += rate;
1807 unit->m_previn = in;
1808 unit->mLevel = level;
1811 void Phasor_next_ak(Phasor *unit, int inNumSamples)
1813 float *out = ZOUT(0);
1815 float *in = ZIN(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;
1824 LOOP1(inNumSamples,
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;
1830 ZXP(out) = level;
1831 level += rate;
1832 level = sc_wrap(level, start, end);
1834 previn = curin;
1837 unit->m_previn = previn;
1838 unit->mLevel = level;
1841 void Phasor_next_aa(Phasor *unit, int inNumSamples)
1843 float *out = ZOUT(0);
1844 float *in = ZIN(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;
1853 LOOP1(inNumSamples,
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;
1860 ZXP(out) = level;
1861 level += zrate;
1862 level = sc_wrap(level, start, end);
1863 previn = curin;
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);
1876 #ifdef NOVA_SIMD
1877 FLATTEN void Peak_next_ak_k_nova(Peak *unit, int inNumSamples);
1878 FLATTEN void Peak_next_ai_k_nova(Peak *unit, int inNumSamples);
1879 #endif
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) {
1889 #ifdef NOVA_SIMD
1890 if (INBUFLENGTH(0) & 7)
1891 SETCALC(Peak_next_ai_k);
1892 else
1893 SETCALC(Peak_next_ai_k_nova);
1894 #else
1895 SETCALC(Peak_next_ai_k);
1896 #endif
1897 } else {
1898 #ifdef NOVA_SIMD
1899 if (INBUFLENGTH(0) & 7)
1900 SETCALC(Peak_next_ak_k);
1901 else
1902 SETCALC(Peak_next_ak_k_nova);
1903 #else
1904 SETCALC(Peak_next_ak_k);
1905 #endif
1907 } else {
1908 if (INRATE(1) == calc_FullRate) {
1909 SETCALC(Peak_next_aa);
1910 } else if (INRATE(1) == calc_ScalarRate) {
1911 if (BUFLENGTH & 15)
1912 SETCALC(Peak_next_ai);
1913 else
1914 SETCALC(Peak_next_ai_unroll);
1915 } else {
1916 if (BUFLENGTH & 15)
1917 SETCALC(Peak_next_ak);
1918 else
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);
1929 float *in = ZIN(0);
1930 float curtrig = ZIN0(1);
1931 float level = unit->mLevel;
1932 float inlevel;
1933 LOOP1(inNumSamples,
1934 inlevel = std::abs(ZXP(in));
1935 level = std::max(inlevel, level);
1936 ZXP(out) = 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);
1946 float *in = ZIN(0);
1947 float level = unit->mLevel;
1948 float inlevel;
1949 LOOP1(inNumSamples,
1950 inlevel = std::abs(ZXP(in));
1951 level = std::max(inlevel, level);
1952 ZXP(out) = level;
1954 unit->mLevel = level;
1957 void Peak_next_aa(Peak *unit, int inNumSamples)
1959 float *out = ZOUT(0);
1960 float *in = ZIN(0);
1961 float *trig = ZIN(1);
1962 float prevtrig = unit->m_prevtrig;
1963 float level = unit->mLevel;
1965 LOOP1(inNumSamples,
1966 float curtrig = ZXP(trig);
1967 float inlevel = std::abs(ZXP(in));
1968 level = std::max(inlevel, level);
1969 ZXP(out) = level;
1970 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
1971 prevtrig = curtrig;
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);
1980 float *in = IN(0);
1981 float inlevel;
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);
1995 out[0] = level0;
1996 out[1] = level1;
1997 out[2] = level2;
1998 out[3] = level3;
1999 out[4] = level4;
2000 out[5] = level5;
2001 out[6] = level6;
2002 out[7] = level7;
2003 level = level7;
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);
2029 float *in = ZIN(0);
2030 float curtrig = ZIN0(1);
2031 float level;
2032 inNumSamples = INBUFLENGTH(0);
2033 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) {
2034 level = std::abs(ZXP(in));
2035 inNumSamples -= 1;
2037 else
2038 level = unit->mLevel;
2039 LOOP1(inNumSamples,
2040 level = std::max(std::abs(ZXP(in)), level);
2042 ZXP(out) = 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);
2050 float *in = ZIN(0);
2051 float level = unit->mLevel;
2052 inNumSamples = INBUFLENGTH(0);
2053 LOOP1(inNumSamples,
2054 level = std::max(std::abs(ZXP(in)), level);
2056 ZXP(out) = level;
2057 unit->mLevel = level;
2060 void Peak_next_aa_k(Peak *unit, int inNumSamples)
2062 float *out = ZOUT(0);
2063 float *in = ZIN(0);
2064 float *trig = ZIN(1);
2065 float prevtrig = unit->m_prevtrig;
2066 float level = unit->mLevel;
2067 float out_level;
2068 bool triggered = false;
2070 inNumSamples = INBUFLENGTH(0);
2071 LOOP1(inNumSamples,
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) {
2076 triggered = true;
2077 out_level = level;
2078 level = inlevel;
2080 prevtrig = curtrig;
2082 if (triggered)
2083 ZXP(out) = out_level;
2084 else
2085 ZXP(out) = level;
2086 unit->m_prevtrig = prevtrig;
2087 unit->mLevel = level;
2090 #ifdef NOVA_SIMD
2091 void Peak_next_ak_k_nova(Peak *unit, int inNumSamples)
2093 float *out = ZOUT(0);
2094 float *in = IN(0);
2095 float curtrig = ZIN0(1);
2096 float level = unit->mLevel;
2097 float inlevel;
2098 inNumSamples = INBUFLENGTH(0);
2099 inlevel = nova::peak_vec_simd(in, &level, inNumSamples);
2100 ZXP(out) = level;
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);
2109 float *in = ZIN(0);
2110 float level = unit->mLevel;
2111 float inlevel;
2112 inNumSamples = INBUFLENGTH(0);
2113 inlevel = nova::peak_vec_simd(in, &level, inNumSamples);
2114 ZXP(out) = level;
2115 unit->mLevel = level;
2117 #endif
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);
2127 } else {
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);
2137 float *in = ZIN(0);
2138 float curtrig = ZIN0(1);
2139 float level = unit->mLevel;
2140 float inlevel;
2141 LOOP1(inNumSamples,
2142 inlevel = ZXP(in);
2143 if (inlevel < level) level = inlevel;
2144 ZXP(out) = level;
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);
2154 float *in = ZIN(0);
2155 float level = unit->mLevel;
2156 float inlevel;
2157 LOOP1(inNumSamples,
2158 inlevel = ZXP(in);
2159 if (inlevel < level) level = inlevel;
2160 ZXP(out) = level;
2162 unit->mLevel = level;
2165 void RunningMin_next_aa(RunningMin *unit, int inNumSamples)
2167 float *out = ZOUT(0);
2168 float *in = ZIN(0);
2169 float *trig = ZIN(1);
2170 float prevtrig = unit->m_prevtrig;
2171 float level = unit->mLevel;
2173 LOOP1(inNumSamples,
2174 float curtrig = ZXP(trig);
2175 float inlevel = ZXP(in);
2176 if (inlevel < level) level = inlevel;
2177 ZXP(out) = level;
2178 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2179 prevtrig = curtrig;
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);
2193 } else {
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);
2203 float *in = ZIN(0);
2204 float curtrig = ZIN0(1);
2205 float level = unit->mLevel;
2206 float inlevel;
2207 LOOP1(inNumSamples,
2208 inlevel = ZXP(in);
2209 if (inlevel > level) level = inlevel;
2210 ZXP(out) = level;
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);
2220 float *in = ZIN(0);
2221 float level = unit->mLevel;
2222 float inlevel;
2223 LOOP1(inNumSamples,
2224 inlevel = ZXP(in);
2225 if (inlevel > level) level = inlevel;
2226 ZXP(out) = level;
2228 unit->mLevel = level;
2231 void RunningMax_next_aa(RunningMax *unit, int inNumSamples)
2233 float *out = ZOUT(0);
2234 float *in = ZIN(0);
2235 float *trig = ZIN(1);
2236 float prevtrig = unit->m_prevtrig;
2237 float level = unit->mLevel;
2239 LOOP1(inNumSamples,
2240 float curtrig = ZXP(trig);
2241 float inlevel = ZXP(in);
2242 if (inlevel > level) level = inlevel;
2243 ZXP(out) = level;
2244 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2245 prevtrig = curtrig;
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);
2260 } else {
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);
2271 float *in = ZIN(0);
2272 float decay = ZIN0(1);
2273 float level = unit->mLevel;
2276 if(decay == unit->mDecay) {
2277 LOOP1(inNumSamples,
2278 float inlevel = std::abs(ZXP(in));
2279 if (inlevel >= level) {
2280 level = inlevel;
2281 } else {
2282 level = inlevel + decay * (level - inlevel);
2284 ZXP(out) = level;
2287 } else {
2289 float decay_slope = CALCSLOPE(decay, unit->mDecay);
2290 if (decay >= 0.f && unit->mDecay >= 0.f) {
2291 LOOP1(inNumSamples,
2292 float inlevel = std::abs(ZXP(in));
2293 if (inlevel >= level) {
2294 level = inlevel;
2295 } else {
2296 level = inlevel + decay * (level - inlevel);
2297 decay += decay_slope;
2299 ZXP(out) = level;
2301 } else if (decay <= 0.f && unit->mDecay <= 0.f) {
2302 LOOP1(inNumSamples,
2303 float inlevel = std::abs(ZXP(in));
2304 if (inlevel >= level) {
2305 level = inlevel;
2306 } else {
2307 level = inlevel + decay * (level + inlevel);
2308 decay += decay_slope;
2310 ZXP(out) = level;
2312 } else {
2313 LOOP1(inNumSamples,
2314 float inlevel = std::abs(ZXP(in));
2315 if (inlevel >= level) {
2316 level = inlevel;
2317 } else {
2318 level = (1.f - std::abs(decay)) * inlevel + decay * level;
2319 decay += decay_slope;
2321 ZXP(out) = level;
2326 unit->mLevel = level;
2327 unit->mDecay = decay;
2331 void PeakFollower_next_ai(PeakFollower *unit, int inNumSamples)
2333 float *out = ZOUT(0);
2334 float *in = ZIN(0);
2335 float decay = ZIN0(1);
2336 float level = unit->mLevel;
2338 LOOP1(inNumSamples,
2339 float inlevel = std::abs(ZXP(in));
2340 if (inlevel >= level) {
2341 level = inlevel;
2342 } else {
2343 level = inlevel + decay * (level - inlevel);
2345 ZXP(out) = level;
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);
2360 } else {
2361 SETCALC(MostChange_next_ak);
2363 } else {
2364 if (INRATE(1) == calc_FullRate) {
2365 SETCALC(MostChange_next_ka);
2366 } else {
2367 SETCALC(MostChange_next_aa);
2370 unit->mPrevA = 0.f;
2371 unit->mPrevB = 0.f;
2372 unit->mRecent = 1;
2373 MostChange_next_aa(unit, 1);
2376 void MostChange_next_ak(MostChange *unit, int inNumSamples)
2378 float *out = ZOUT(0);
2379 float *a = ZIN(0);
2380 float xb = ZIN0(1);
2381 float prevA = unit->mPrevA;
2382 float prevB = unit->mPrevB;
2383 int recent = unit->mRecent;
2384 LOOP1(inNumSamples,
2385 float xa = ZXP(a);
2386 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2387 if (diff > 0.f) {
2388 recent = 0;
2389 ZXP(out) = xa;
2390 } else if (diff < 0.f) {
2391 recent = 1;
2392 ZXP(out) = xb;
2393 } else {
2394 ZXP(out) = recent ? xb : xa;
2396 prevA = xa;
2397 prevB = xb;
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);
2407 float *a = ZIN(0);
2408 float *b = ZIN(1);
2409 float prevA = unit->mPrevA;
2410 float prevB = unit->mPrevB;
2411 int recent = unit->mRecent;
2412 LOOP1(inNumSamples,
2413 float xa = ZXP(a);
2414 float xb = ZXP(b);
2415 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2416 if (diff > 0.f) {
2417 recent = 0;
2418 ZXP(out) = xa;
2419 } else if (diff < 0.f) {
2420 recent = 1;
2421 ZXP(out) = xb;
2422 } else {
2423 ZXP(out) = recent ? xb : xa;
2425 prevA = xa;
2426 prevB = xb;
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);
2436 float xa = ZIN0(0);
2437 float *b = ZIN(1);
2438 float prevA = unit->mPrevA;
2439 float prevB = unit->mPrevB;
2440 int recent = unit->mRecent;
2441 LOOP1(inNumSamples,
2442 float xb = ZXP(b);
2443 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2444 if (diff > 0.f) {
2445 recent = 0;
2446 ZXP(out) = xa;
2447 } else if (diff < 0.f) {
2448 recent = 1;
2449 ZXP(out) = xb;
2450 } else {
2451 ZXP(out) = recent ? xb : xa;
2453 prevA = xa;
2454 prevB = xb;
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);
2469 } else {
2470 SETCALC(LeastChange_next_ak);
2472 } else {
2473 if (INRATE(1) == calc_FullRate) {
2474 SETCALC(LeastChange_next_ka);
2475 } else {
2476 SETCALC(LeastChange_next_aa);
2479 unit->mPrevA = 0.f;
2480 unit->mPrevB = 0.f;
2481 unit->mRecent = 0;
2482 LeastChange_next_aa(unit, 1);
2485 void LeastChange_next_ak(LeastChange *unit, int inNumSamples)
2487 float *out = ZOUT(0);
2488 float *a = ZIN(0);
2489 float xb = ZIN0(1);
2490 float prevA = unit->mPrevA;
2491 float prevB = unit->mPrevB;
2492 int recent = unit->mRecent;
2493 LOOP1(inNumSamples,
2494 float xa = ZXP(a);
2495 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2496 if (diff < 0.f) {
2497 recent = 0;
2498 ZXP(out) = xa;
2499 } else if (diff > 0.f) {
2500 recent = 1;
2501 ZXP(out) = xb;
2502 } else {
2503 ZXP(out) = recent ? xb : xa;
2505 prevA = xa;
2506 prevB = xb;
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);
2516 float *a = ZIN(0);
2517 float *b = ZIN(1);
2518 float prevA = unit->mPrevA;
2519 float prevB = unit->mPrevB;
2520 int recent = unit->mRecent;
2521 LOOP1(inNumSamples,
2522 float xa = ZXP(a);
2523 float xb = ZXP(b);
2524 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2525 if (diff < 0.f) {
2526 recent = 0;
2527 ZXP(out) = xa;
2528 } else if (diff > 0.f) {
2529 recent = 1;
2530 ZXP(out) = xb;
2531 } else {
2532 ZXP(out) = recent ? xb : xa;
2534 prevA = xa;
2535 prevB = xb;
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);
2545 float xa = ZIN0(0);
2546 float *b = ZIN(1);
2547 float prevA = unit->mPrevA;
2548 float prevB = unit->mPrevB;
2549 int recent = unit->mRecent;
2550 LOOP1(inNumSamples,
2551 float xb = ZXP(b);
2552 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2553 if (diff < 0.f) {
2554 recent = 0;
2555 ZXP(out) = xa;
2556 } else if (diff > 0.f) {
2557 recent = 1;
2558 ZXP(out) = xb;
2559 } else {
2560 ZXP(out) = recent ? xb : xa;
2562 prevA = xa;
2563 prevB = xb;
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);
2577 } else {
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);
2592 if(diff >= delta) {
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);
2604 float *in = ZIN(0);
2605 float delta = ZIN0(1);
2606 float prev = unit->mPrev;
2607 float curr = unit->mCurr;
2609 LOOP1(inNumSamples,
2610 float inval = ZXP(in);
2611 float diff = std::abs(inval - curr);
2612 if(diff >= delta) {
2613 prev = curr;
2614 curr = inval;
2616 ZXP(out) = prev
2618 unit->mPrev = prev;
2619 unit->mCurr = curr;
2625 //////////////////////////////////////////////////////////////////////////////////////////
2627 void Done_Ctor(Done *unit)
2629 SETCALC(Done_next);
2631 unit->m_src = unit->mInput[0]->mFromUnit;
2633 Done_next(unit, 1);
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;
2643 else *out = 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)
2658 float in = ZIN0(0);
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)
2677 float in = ZIN0(0);
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);
2690 unit->m_state = 1;
2692 ZOUT0(0) = ZIN0(0);
2696 void Pause_next(Pause *unit, int inNumSamples)
2698 float in = ZIN0(0);
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);
2704 if (node) {
2705 NodeRun(node, state);
2708 ZOUT0(0) = in;
2711 //////////////////////////////////////////////////////////////////////////////////////////
2713 void Free_Ctor(Free *unit)
2715 SETCALC(Free_next);
2717 unit->m_prevtrig = 0.f;
2719 ZOUT0(0) = ZIN0(0);
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);
2729 if (node) {
2730 NodeEnd(node);
2733 unit->m_prevtrig = trig;
2734 ZOUT0(0) = trig;
2737 //////////////////////////////////////////////////////////////////////////////////////////
2739 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone *unit)
2741 unit->m_src = unit->mInput[0]->mFromUnit;
2743 if (unit->m_src) {
2744 SETCALC(FreeSelfWhenDone_next);
2745 FreeSelfWhenDone_next(unit, 1);
2746 } else {
2747 SETCALC(ClearUnitOutputs);
2748 ClearUnitOutputs(unit, 1);
2753 void FreeSelfWhenDone_next(FreeSelfWhenDone *unit, int inNumSamples)
2755 float *out = OUT(0);
2756 float *in = IN(0);
2757 Unit *src = unit->m_src;
2758 if (src->mDone) {
2759 NodeEnd(&unit->mParent->mNode);
2760 SETCALC(ClearUnitOutputs);
2762 *out = *in;
2765 //////////////////////////////////////////////////////////////////////////////////////////
2767 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone *unit)
2769 unit->m_src = unit->mInput[0]->mFromUnit;
2771 if (unit->m_src) {
2772 SETCALC(PauseSelfWhenDone_next);
2773 PauseSelfWhenDone_next(unit, 1);
2774 } else {
2775 SETCALC(ClearUnitOutputs);
2776 ClearUnitOutputs(unit, 1);
2781 void PauseSelfWhenDone_next(PauseSelfWhenDone *unit, int inNumSamples)
2783 float *out = OUT(0);
2784 float *in = IN(0);
2785 Unit *src = unit->m_src;
2786 if (src->mDone) {
2787 NodeRun(&unit->mParent->mNode, 0);
2788 SETCALC(ClearUnitOutputs);
2790 *out = *in;
2793 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2795 struct SendPeakRMS:
2796 public Unit
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;
2805 SendPeakRMS(void)
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>);
2831 else
2832 SETCALC(SendPeakRMS::perform_k<true>);
2833 } else {
2834 if (mCalcRate == calc_FullRate)
2835 SETCALC(SendPeakRMS::perform_a<false>);
2836 else
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))
2850 : 0.f;
2853 ~SendPeakRMS (void)
2855 SendPeakRMS * unit = this;
2856 RTFree(unit->mWorld, mChannelData);
2859 unsigned int mChannelCount;
2860 float * mChannelData;
2862 float mB1;
2863 int mAudioSamplesPerTick;
2864 int mControlSamplesPerTick;
2865 int mPhaseRemain;
2867 void performLevelLag(float & out, float y0, float & y1)
2869 if (y0 >= y1)
2870 out = y1 = y0;
2871 else
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);
2895 else
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);
2915 else {
2916 if (simd)
2917 nova::peak_rms_vec_simd(in, &level, &sqrsum, numSamples);
2918 else
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);
2936 } else {
2937 if (!(samplesToAnalyze & 15) && !(firstSample & 3)) // check for unrolling and alignment
2938 nova::peak_rms_vec_simd(in, &level, &sqrsum, samplesToAnalyze);
2939 else
2940 nova::peak_rms_vec(in, &level, &sqrsum, samplesToAnalyze);
2945 template <bool simd>
2946 inline void next_k(int inNumSamples)
2948 mPhaseRemain -= 1;
2950 if (mPhaseRemain <= 0) {
2951 mPhaseRemain += mControlSamplesPerTick;
2952 sendReply();
2955 analyzeFullBlock<simd>();
2958 template <bool simd>
2959 inline void next_a(int inNumSamples)
2961 if (mPhaseRemain >= inNumSamples) {
2962 mPhaseRemain -= inNumSamples;
2963 analyzeFullBlock<simd>();
2964 } else {
2965 if (mPhaseRemain == 0) {
2966 sendReply();
2967 mPhaseRemain = mAudioSamplesPerTick;
2970 int startSample = 0;
2971 int samplesToAnalyze = std::min(mPhaseRemain, inNumSamples);
2972 int remain = inNumSamples;
2974 do {
2975 analyzePartialBlock(startSample, samplesToAnalyze);
2977 startSample += samplesToAnalyze;
2978 mPhaseRemain -= samplesToAnalyze;
2979 if (mPhaseRemain == 0) {
2980 sendReply();
2981 mPhaseRemain = mAudioSamplesPerTick;
2984 remain -= samplesToAnalyze;
2985 samplesToAnalyze = std::min(remain, mPhaseRemain);
2986 } while(remain);
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 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3008 PluginLoad(Trigger)
3010 ft = inTable;
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);