class library: SynthDef - replaceUGen fixes
[supercollider.git] / server / plugins / TriggerUGens.cpp
blobc4b23b79ff348121fdd7d666bf7141d1cf88fde2
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 #if defined(__GNUC__) && !defined(__clang__)
32 #define inline_functions __attribute__ ((flatten))
33 #else
34 #define inline_functions
35 #endif
37 #endif
39 static InterfaceTable *ft;
41 //////////////////////////////////////////////////////////////////////////////////////////////////
43 struct Trig1 : public Unit
45 float m_prevtrig;
46 long mCounter;
49 struct Trig : public Unit
51 float mLevel;
52 float m_prevtrig;
53 long mCounter;
56 struct SendTrig : public Unit
58 float m_prevtrig;
61 struct SendReply : public Unit
63 float m_prevtrig;
64 int m_valueSize;
65 int m_valueOffset;
66 float *m_values;
67 int m_cmdNameSize;
68 char *m_cmdName;
72 struct Poll : public Unit
74 int m_samplesRemain, m_intervalSamples;
75 float m_trig;
76 float m_lastPoll, m_id;
77 char *m_id_string;
78 bool m_mayprint;
82 struct ToggleFF : public Unit
84 float mLevel;
85 float m_prevtrig;
88 struct SetResetFF : public Unit
90 float mLevel;
91 float m_prevtrig, m_prevreset;
94 struct Latch : public Unit
96 float mLevel;
97 float m_prevtrig;
100 struct Gate : public Unit
102 float mLevel;
105 struct Schmidt : public Unit
107 float mLevel;
110 struct PulseDivider : public Unit
112 float mLevel;
113 float m_prevtrig;
114 long mCounter;
117 struct PulseCount : public Unit
119 float mLevel;
120 float m_prevtrig, m_prevreset;
123 struct Stepper : public Unit
125 float mLevel;
126 float m_prevtrig, m_prevreset;
129 struct TDelay : public Unit
131 long mCounter;
132 float m_prevtrig;
135 struct ZeroCrossing : public Unit
137 float mLevel, m_prevfrac, m_previn;
138 int32 mCounter;
141 struct Timer : public Unit
143 float mLevel, m_prevfrac, m_previn;
144 int32 mCounter;
147 struct Sweep : public Unit
149 double mLevel;
150 float m_previn;
153 struct Phasor : public Unit
155 double mLevel;
156 float m_previn;
159 struct Peak : public Unit
161 float mLevel;
162 float m_prevtrig;
165 struct RunningMin : public Unit
167 float mLevel;
168 float m_prevtrig;
171 struct RunningMax : public Unit
173 float mLevel;
174 float m_prevtrig;
177 struct PeakFollower : public Unit
179 float mLevel;
180 float mDecay;
183 struct MostChange : public Unit
185 float mPrevA, mPrevB;
186 int mRecent;
189 struct LeastChange : public Unit
191 float mPrevA, mPrevB;
192 int mRecent;
195 struct LastValue : public Unit
197 float mPrev;
198 float mCurr;
203 struct Done : public Unit
205 Unit *m_src;
208 struct FreeSelf : public Unit
210 float m_prevtrig;
213 struct PauseSelf : public Unit
215 float m_prevtrig;
218 struct Pause : public Unit
220 int m_state;
223 struct Free : public Unit
225 float m_prevtrig;
228 struct FreeSelfWhenDone : public Unit
230 Unit *m_src;
233 struct PauseSelfWhenDone : public Unit
235 Unit *m_src;
238 extern "C"
240 void Trig1_Ctor(Trig1 *unit);
241 void Trig1_next(Trig1 *unit, int inNumSamples);
242 void Trig1_next_k(Trig1 *unit, int inNumSamples);
244 void Trig_Ctor(Trig *unit);
245 void Trig_next(Trig *unit, int inNumSamples);
246 void Trig_next_k(Trig *unit, int inNumSamples);
248 void SendTrig_Ctor(SendTrig *unit);
249 void SendTrig_next(SendTrig *unit, int inNumSamples);
250 void SendTrig_next_aka(SendTrig *unit, int inNumSamples);
252 void SendReply_Ctor(SendReply *unit);
253 void SendReply_next(SendReply *unit, int inNumSamples);
254 void SendReply_next_aka(SendReply *unit, int inNumSamples);
256 void Poll_Ctor(Poll* unit);
257 void Poll_next_aa(Poll *unit, int inNumSamples);
258 void Poll_next_ak(Poll *unit, int inNumSamples);
259 void Poll_next_kk(Poll *unit, int inNumSamples);
261 void SetResetFF_Ctor(SetResetFF *unit);
262 void SetResetFF_next_a(SetResetFF *unit, int inNumSamples);
263 void SetResetFF_next_k(SetResetFF *unit, int inNumSamples);
265 void ToggleFF_Ctor(ToggleFF *unit);
266 void ToggleFF_next(ToggleFF *unit, int inNumSamples);
268 void Latch_Ctor(Latch *unit);
269 void Latch_next_ak(Latch *unit, int inNumSamples);
270 void Latch_next_aa(Latch *unit, int inNumSamples);
272 void Gate_Ctor(Gate *unit);
273 void Gate_next_ak(Gate *unit, int inNumSamples);
274 void Gate_next_aa(Gate *unit, int inNumSamples);
276 void Schmidt_Ctor(Schmidt *unit);
277 void Schmidt_next(Schmidt *unit, int inNumSamples);
279 void PulseDivider_Ctor(PulseDivider *unit);
280 void PulseDivider_next(PulseDivider *unit, int inNumSamples);
282 void PulseCount_Ctor(PulseCount *unit);
283 void PulseCount_next_a(PulseCount *unit, int inNumSamples);
284 void PulseCount_next_k(PulseCount *unit, int inNumSamples);
285 void PulseCount_next_0(PulseCount *unit, int inNumSamples);
287 void Stepper_Ctor(Stepper *unit);
288 void Stepper_next_aa(Stepper *unit, int inNumSamples);
289 void Stepper_next_ak(Stepper *unit, int inNumSamples);
290 void Stepper_next_a0(Stepper *unit, int inNumSamples);
292 void TDelay_Ctor(TDelay *unit);
293 void TDelay_next(TDelay *unit, int inNumSamples);
295 void ZeroCrossing_Ctor(ZeroCrossing *unit);
296 void ZeroCrossing_next_a(ZeroCrossing *unit, int inNumSamples);
298 void Timer_Ctor(Timer *unit);
299 void Timer_next_a(Timer *unit, int inNumSamples);
301 void Sweep_Ctor(Sweep *unit);
302 void Sweep_next_0k(Sweep *unit, int inNumSamples);
303 void Sweep_next_0a(Sweep *unit, int inNumSamples);
304 void Sweep_next_kk(Sweep *unit, int inNumSamples);
305 void Sweep_next_ka(Sweep *unit, int inNumSamples);
306 void Sweep_next_ak(Sweep *unit, int inNumSamples);
307 void Sweep_next_aa(Sweep *unit, int inNumSamples);
309 void Phasor_Ctor(Phasor *unit);
310 void Phasor_next_kk(Phasor *unit, int inNumSamples);
311 void Phasor_next_ak(Phasor *unit, int inNumSamples);
312 void Phasor_next_aa(Phasor *unit, int inNumSamples);
314 void Peak_Ctor(Peak *unit);
315 void Peak_next_ak(Peak *unit, int inNumSamples);
316 void Peak_next_ai(Peak *unit, int inNumSamples);
317 void Peak_next_aa(Peak *unit, int inNumSamples);
318 void Peak_next_ak_k(Peak *unit, int inNumSamples);
319 void Peak_next_ai_k(Peak *unit, int inNumSamples);
320 void Peak_next_aa_k(Peak *unit, int inNumSamples);
322 void RunningMin_Ctor(RunningMin *unit);
323 void RunningMin_next_ak(RunningMin *unit, int inNumSamples);
324 void RunningMin_next_ai(RunningMin *unit, int inNumSamples);
325 void RunningMin_next_aa(RunningMin *unit, int inNumSamples);
327 void RunningMax_Ctor(RunningMax *unit);
328 void RunningMax_next_ak(RunningMax *unit, int inNumSamples);
329 void RunningMax_next_ai(RunningMax *unit, int inNumSamples);
330 void RunningMax_next_aa(RunningMax *unit, int inNumSamples);
333 void PeakFollower_Ctor(PeakFollower *unit);
334 void PeakFollower_next(PeakFollower *unit, int inNumSamples);
335 void PeakFollower_next_ai(PeakFollower *unit, int inNumSamples);
337 void MostChange_Ctor(MostChange *unit);
338 void MostChange_next_ak(MostChange *unit, int inNumSamples);
339 void MostChange_next_ka(MostChange *unit, int inNumSamples);
340 void MostChange_next_aa(MostChange *unit, int inNumSamples);
342 void LeastChange_Ctor(LeastChange *unit);
343 void LeastChange_next_ak(LeastChange *unit, int inNumSamples);
344 void LeastChange_next_ka(LeastChange *unit, int inNumSamples);
345 void LeastChange_next_aa(LeastChange *unit, int inNumSamples);
347 void LastValue_Ctor(LastValue *unit);
348 void LastValue_next_ak(LastValue *unit, int inNumSamples);
349 void LastValue_next_kk(LastValue *unit, int inNumSamples);
351 void Done_Ctor(Done *unit);
352 void Done_next(Done *unit, int inNumSamples);
354 void FreeSelf_Ctor(FreeSelf *unit);
355 void FreeSelf_next(FreeSelf *unit, int inNumSamples);
357 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone *unit);
358 void FreeSelfWhenDone_next(FreeSelfWhenDone *unit, int inNumSamples);
360 void PauseSelf_Ctor(PauseSelf *unit);
361 void PauseSelf_next(PauseSelf *unit, int inNumSamples);
363 void Pause_Ctor(Pause *unit);
364 void Pause_next(Pause *unit, int inNumSamples);
366 void Free_Ctor(Free *unit);
367 void Free_next(Free *unit, int inNumSamples);
369 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone *unit);
370 void PauseSelfWhenDone_next(PauseSelfWhenDone *unit, int inNumSamples);
374 ////////////////////////////////////////////////////////////////////////////////////////////////////////
376 #ifdef NOVA_SIMD
377 inline_functions void Trig1_next_nova(Trig1 *unit, int inNumSamples);
378 inline_functions void Trig1_next_k_nova(Trig1 *unit, int inNumSamples);
379 #endif
381 void Trig1_Ctor(Trig1 *unit)
383 if (unit->mCalcRate == calc_FullRate && INRATE(0) != calc_FullRate) {
384 #ifdef NOVA_SIMD
385 if (!(BUFLENGTH & 15))
386 SETCALC(Trig1_next_k_nova);
387 else
388 #endif
389 SETCALC(Trig1_next_k);
390 } else {
391 #ifdef NOVA_SIMD
392 if (!(BUFLENGTH & 15))
393 SETCALC(Trig1_next_nova);
394 else
395 #endif
396 SETCALC(Trig1_next);
398 unit->mCounter = 0;
399 unit->m_prevtrig = 0.f;
401 ZOUT0(0) = 0.f;
404 void Trig1_next(Trig1 *unit, int inNumSamples)
406 float *out = ZOUT(0);
407 float *trig = ZIN(0);
408 float dur = ZIN0(1);
409 float sr = unit->mRate->mSampleRate;
410 float prevtrig = unit->m_prevtrig;
411 unsigned long counter = unit->mCounter;
413 LOOP1(inNumSamples,
414 float curtrig = ZXP(trig);
415 float zout;
416 if (counter > 0) {
417 zout = --counter ? 1.f : 0.f;
418 } else {
419 if (curtrig > 0.f && prevtrig <= 0.f) {
420 counter = (long)(dur * sr + .5f);
421 if (counter < 1) counter = 1;
422 zout = 1.f;
423 } else {
424 zout = 0.f;
427 prevtrig = curtrig;
428 ZXP(out) = zout;
430 unit->m_prevtrig = prevtrig;
431 unit->mCounter = counter;
434 void Trig1_next_k(Trig1 *unit, int inNumSamples)
436 float *out = ZOUT(0);
437 float curtrig = ZIN0(0);
438 float dur = ZIN0(1);
439 float sr = unit->mRate->mSampleRate;
440 float prevtrig = unit->m_prevtrig;
441 unsigned long counter = unit->mCounter;
443 LOOP1(inNumSamples,
444 float zout;
445 if (counter > 0) {
446 zout = --counter ? 1.f : 0.f;
447 } else {
448 if (curtrig > 0.f && prevtrig <= 0.f) {
449 counter = (long)(dur * sr + .5f);
450 if (counter < 1) counter = 1;
451 zout = 1.f;
452 } else {
453 zout = 0.f;
456 prevtrig = curtrig;
457 ZXP(out) = zout;
459 unit->m_prevtrig = prevtrig;
460 unit->mCounter = counter;
463 #ifdef NOVA_SIMD
464 void Trig1_next_nova(Trig1 *unit, int inNumSamples)
466 float *out = ZOUT(0);
467 float *trig = ZIN(0);
468 float dur = ZIN0(1);
469 float sr = unit->mRate->mSampleRate;
470 float prevtrig = unit->m_prevtrig;
471 unsigned long counter = unit->mCounter;
473 if (counter > inNumSamples)
475 nova::setvec_simd(OUT(0), 1.f, inNumSamples);
476 counter -= inNumSamples;
477 assert(counter > 0);
478 prevtrig = IN(0)[inNumSamples-1];
480 else
482 LOOP1(inNumSamples,
483 float curtrig = ZXP(trig);
484 float zout;
485 if (counter > 0) {
486 zout = --counter ? 1.f : 0.f;
487 } else {
488 if (curtrig > 0.f && prevtrig <= 0.f) {
489 counter = (long)(dur * sr + .5f);
490 if (counter < 1) counter = 1;
491 zout = 1.f;
492 } else {
493 zout = 0.f;
496 prevtrig = curtrig;
497 ZXP(out) = zout;
500 unit->m_prevtrig = prevtrig;
501 unit->mCounter = counter;
505 void Trig1_next_k_nova(Trig1 *unit, int inNumSamples)
507 float *out = ZOUT(0);
508 float curtrig = ZIN0(0);
509 float dur = ZIN0(1);
510 float sr = unit->mRate->mSampleRate;
511 float prevtrig = unit->m_prevtrig;
512 unsigned long counter = unit->mCounter;
514 if (counter > inNumSamples)
516 nova::setvec_simd(OUT(0), 1.f, inNumSamples);
517 counter -= inNumSamples;
518 assert(counter > 0);
519 prevtrig = curtrig;
521 else if (counter == 0 && (curtrig <= 0.f || prevtrig > 0.f))
523 nova::zerovec_simd(OUT(0), inNumSamples);
524 prevtrig = curtrig;
526 else
528 LOOP1(inNumSamples,
529 float zout;
530 if (counter > 0) {
531 zout = --counter ? 1.f : 0.f;
532 } else {
533 if (curtrig > 0.f && prevtrig <= 0.f) {
534 counter = (long)(dur * sr + .5f);
535 if (counter < 1) counter = 1;
536 zout = 1.f;
537 } else {
538 zout = 0.f;
541 prevtrig = curtrig;
542 ZXP(out) = zout;
546 unit->m_prevtrig = prevtrig;
547 unit->mCounter = counter;
549 #endif
552 ////////////////////////////////////////////////////////////////////////////////////////////////////////
554 #ifdef NOVA_SIMD
555 void Trig_next_nova(Trig *unit, int inNumSamples);
556 void Trig_next_k_nova(Trig *unit, int inNumSamples);
557 #endif
559 void Trig_Ctor(Trig *unit)
561 if (unit->mCalcRate == calc_FullRate && INRATE(0) != calc_FullRate) {
562 #ifdef NOVA_SIMD
563 if (!(BUFLENGTH & 15))
564 SETCALC(Trig_next_k_nova);
565 else
566 #endif
567 SETCALC(Trig_next_k);
568 } else {
569 #ifdef NOVA_SIMD
570 if (!(BUFLENGTH & 15))
571 SETCALC(Trig_next_nova);
572 else
573 #endif
574 SETCALC(Trig_next);
577 unit->mCounter = 0;
578 unit->m_prevtrig = 0.f;
579 unit->mLevel = 0.f;
581 ZOUT0(0) = 0.f;
584 void Trig_next(Trig *unit, int inNumSamples)
586 float *out = ZOUT(0);
587 float *trig = ZIN(0);
588 float dur = ZIN0(1);
589 float sr = unit->mRate->mSampleRate;
590 float prevtrig = unit->m_prevtrig;
591 float level = unit->mLevel;
592 unsigned long counter = unit->mCounter;
594 LOOP1(inNumSamples,
595 float curtrig = ZXP(trig);
596 float zout;
597 if (counter > 0) {
598 zout = --counter ? level : 0.f;
599 } else {
600 if (curtrig > 0.f && prevtrig <= 0.f) {
601 counter = (long)(dur * sr + .5f);
602 if (counter < 1) counter = 1;
603 level = curtrig;
604 zout = level;
605 } else {
606 zout = 0.f;
609 prevtrig = curtrig;
610 ZXP(out) = zout;
612 unit->m_prevtrig = prevtrig;
613 unit->mCounter = counter;
614 unit->mLevel = level;
617 void Trig_next_k(Trig *unit, int inNumSamples)
619 float *out = ZOUT(0);
620 float curtrig = ZIN0(0);
621 float dur = ZIN0(1);
622 float sr = unit->mRate->mSampleRate;
623 float prevtrig = unit->m_prevtrig;
624 float level = unit->mLevel;
625 unsigned long counter = unit->mCounter;
627 LOOP1(inNumSamples,
628 float zout;
629 if (counter > 0) {
630 zout = --counter ? level : 0.f;
631 } else {
632 if (curtrig > 0.f && prevtrig <= 0.f) {
633 counter = (long)(dur * sr + .5f);
634 if (counter < 1) counter = 1;
635 level = curtrig;
636 zout = level;
637 } else {
638 zout = 0.f;
641 prevtrig = curtrig;
642 ZXP(out) = zout;
644 unit->m_prevtrig = prevtrig;
645 unit->mCounter = counter;
646 unit->mLevel = level;
649 #ifdef NOVA_SIMD
650 void Trig_next_nova(Trig *unit, int inNumSamples)
652 float *out = ZOUT(0);
653 float *trig = ZIN(0);
654 float dur = ZIN0(1);
655 float sr = unit->mRate->mSampleRate;
656 float prevtrig = unit->m_prevtrig;
657 float level = unit->mLevel;
658 unsigned long counter = unit->mCounter;
660 if (counter > inNumSamples)
662 nova::setvec_simd(OUT(0), level, inNumSamples);
663 counter -= inNumSamples;
664 assert(counter > 0);
665 prevtrig = IN(0)[inNumSamples-1];
667 else
669 LOOP1(inNumSamples,
670 float curtrig = ZXP(trig);
671 float zout;
672 if (counter > 0) {
673 zout = --counter ? level : 0.f;
674 } else {
675 if (curtrig > 0.f && prevtrig <= 0.f) {
676 counter = (long)(dur * sr + .5f);
677 if (counter < 1) counter = 1;
678 level = curtrig;
679 zout = level;
680 } else {
681 zout = 0.f;
684 prevtrig = curtrig;
685 ZXP(out) = zout;
688 unit->m_prevtrig = prevtrig;
689 unit->mCounter = counter;
690 unit->mLevel = level;
693 void Trig_next_k_nova(Trig *unit, int inNumSamples)
695 float *out = ZOUT(0);
696 float curtrig = ZIN0(0);
697 float dur = ZIN0(1);
698 float sr = unit->mRate->mSampleRate;
699 float prevtrig = unit->m_prevtrig;
700 float level = unit->mLevel;
701 unsigned long counter = unit->mCounter;
703 if (counter > inNumSamples)
705 nova::setvec_simd(OUT(0), level, inNumSamples);
706 counter -= inNumSamples;
707 assert(counter > 0);
708 prevtrig = curtrig;
710 else if (counter == 0 && (curtrig <= 0.f || prevtrig > 0.f))
712 nova::zerovec_simd(OUT(0), inNumSamples);
713 prevtrig = curtrig;
715 else
717 LOOP1(inNumSamples,
718 float zout;
719 if (counter > 0) {
720 zout = --counter ? level : 0.f;
721 } else {
722 if (curtrig > 0.f && prevtrig <= 0.f) {
723 counter = (long)(dur * sr + .5f);
724 if (counter < 1) counter = 1;
725 level = curtrig;
726 zout = level;
727 } else {
728 zout = 0.f;
731 prevtrig = curtrig;
732 ZXP(out) = zout;
735 unit->m_prevtrig = prevtrig;
736 unit->mCounter = counter;
737 unit->mLevel = level;
739 #endif
741 ////////////////////////////////////////////////////////////////////////////////////////////////////////
743 void SendTrig_Ctor(SendTrig *unit)
745 if (INRATE(2) == calc_FullRate) {
746 SETCALC(SendTrig_next_aka);
747 } else {
748 SETCALC(SendTrig_next);
750 unit->m_prevtrig = 0.f;
753 void SendTrig_next(SendTrig *unit, int inNumSamples)
755 float *trig = ZIN(0);
756 float prevtrig = unit->m_prevtrig;
758 LOOP1(inNumSamples,
759 float curtrig = ZXP(trig);
760 if (curtrig > 0.f && prevtrig <= 0.f) {
761 SendTrigger(&unit->mParent->mNode, (int)ZIN0(1), ZIN0(2));
763 prevtrig = curtrig;
765 unit->m_prevtrig = prevtrig;
768 void SendTrig_next_aka(SendTrig *unit, int inNumSamples)
770 float *trig = ZIN(0);
771 float *value = ZIN(2);
772 float prevtrig = unit->m_prevtrig;
774 LOOP1(inNumSamples,
775 float curtrig = ZXP(trig);
776 float curval = ZXP(value);
777 if (curtrig > 0.f && prevtrig <= 0.f) {
778 SendTrigger(&unit->mParent->mNode, (int)ZIN0(1), curval);
780 prevtrig = curtrig;
782 unit->m_prevtrig = prevtrig;
786 ////////////////////////////////////////////////////////////////////////////////////////////////////////
788 void SendReply_Ctor(SendReply *unit)
790 const int kVarOffset = 3;
792 unit->m_prevtrig = 0.f;
793 unit->m_cmdNameSize = IN0(2);
794 unit->m_valueSize = unit->mNumInputs - unit->m_cmdNameSize - kVarOffset;
796 // allocations
797 unit->m_cmdName = (char*)RTAlloc(unit->mWorld, (unit->m_cmdNameSize + 1) * sizeof(char));
798 for(int i = 0; i < (int)unit->m_cmdNameSize; i++){
799 unit->m_cmdName[i] = (char)IN0(kVarOffset+i);
801 // terminate string
802 unit->m_cmdName[unit->m_cmdNameSize] = 0;
804 unit->m_valueOffset = kVarOffset + unit->m_cmdNameSize;
805 unit->m_values = (float*)RTAlloc(unit->mWorld, unit->m_valueSize * sizeof(float));
807 if (INRATE(0) == calc_FullRate) {
808 SETCALC(SendReply_next_aka);
809 } else {
810 SETCALC(SendReply_next);
814 void SendReply_Dtor(SendReply* unit)
816 RTFree(unit->mWorld, unit->m_values);
817 RTFree(unit->mWorld, unit->m_cmdName);
821 void SendReply_next(SendReply *unit, int inNumSamples)
823 float *trig = IN(0);
824 float prevtrig = unit->m_prevtrig;
825 float *values = unit->m_values;
826 int valueSize = unit->m_valueSize;
827 int valueOffset = unit->m_valueOffset;
828 for(int j = 0; j < inNumSamples; j++) {
829 float curtrig = trig[j];
830 if (curtrig > 0.f && prevtrig <= 0.f) {
831 for(int i=0; i<valueSize; i++) {
832 values[i] = IN(i + valueOffset)[0];
834 SendNodeReply(&unit->mParent->mNode, (int)ZIN0(1), unit->m_cmdName, unit->m_valueSize, values);
836 prevtrig = curtrig;
838 unit->m_prevtrig = prevtrig;
841 void SendReply_next_aka(SendReply *unit, int inNumSamples)
843 float *trig = IN(0);
844 float prevtrig = unit->m_prevtrig;
845 float *invalues = IN(unit->m_valueOffset);
846 float *values = unit->m_values;
847 int valueSize = unit->m_valueSize;
848 int valueOffset = unit->m_valueOffset;
849 for(int j = 0; j < inNumSamples; j++) {
850 float curtrig = trig[j];
851 if (curtrig > 0.f && prevtrig <= 0.f) {
852 for(int i=0; i<valueSize; i++) {
853 int offset = INRATE( i + valueOffset ) != calc_FullRate ? 0 : j;
854 values[i] = IN(i + valueOffset)[offset];
856 SendNodeReply(&unit->mParent->mNode, (int)ZIN0(1), unit->m_cmdName, unit->m_valueSize, values);
858 prevtrig = curtrig;
860 unit->m_prevtrig = prevtrig;
864 ////////////////////////////////////////////////////////////////////////////////////////////////////////
866 void Poll_Ctor(Poll* unit)
868 if (INRATE(0) == calc_FullRate){
869 if (INRATE(1) == calc_FullRate){
870 SETCALC(Poll_next_aa);
871 } else {
872 SETCALC(Poll_next_ak);
874 } else {
875 SETCALC(Poll_next_kk);
878 unit->m_trig = IN0(0);
879 unit->m_id = IN0(3); // number of chars in the id string
880 unit->m_id_string = (char*)RTAlloc(unit->mWorld, ((int)unit->m_id + 1) * sizeof(char));
881 for(int i = 0; i < (int)unit->m_id; i++){
882 unit->m_id_string[i] = (char)IN0(4+i);
884 unit->m_id_string[(int)unit->m_id] = '\0';
886 unit->m_mayprint = unit->mWorld->mVerbosity >= 0;
888 Poll_next_kk(unit, 1);
891 void Poll_Dtor(Poll* unit)
893 RTFree(unit->mWorld, unit->m_id_string);
896 void Poll_next_aa(Poll *unit, int inNumSamples){
897 float* in = IN(1);
898 float* trig = IN(0);
899 float lasttrig = unit->m_trig;
900 for(int i = 0; i < inNumSamples; i++){
901 if((lasttrig <= 0.0) && (trig[i] > 0.0)){
902 if(unit->m_mayprint){
903 Print("%s: %g\n", unit->m_id_string, in[i]);
905 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in[i]);
907 lasttrig = trig[i];
909 unit->m_trig = lasttrig;
912 void Poll_next_kk(Poll *unit, int inNumSamples){
913 float in = IN0(1);
914 float trig = IN0(0);
915 if((unit->m_trig <= 0.0) && (trig > 0.0)){
916 if(unit->m_mayprint){
917 Print("%s: %g\n", unit->m_id_string, in);
919 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in);
921 unit->m_trig = trig;
925 void Poll_next_ak(Poll *unit, int inNumSamples){
926 float in = IN0(1);
927 float* trig = IN(0);
928 float lasttrig = unit->m_trig;
929 for(int i = 0; i < inNumSamples; i++){
930 if((lasttrig <= 0.0) && (trig[i] > 0.0)){
931 if(unit->m_mayprint){
932 Print("%s: %g\n", unit->m_id_string, in);
934 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in);
936 lasttrig = trig[i];
938 unit->m_trig = lasttrig;
943 ////////////////////////////////////////////////////////////////////////////////////////////////////////
945 void SetResetFF_Ctor(SetResetFF *unit)
947 if (INRATE(1) == calc_FullRate) {
948 SETCALC(SetResetFF_next_a);
949 } else {
950 SETCALC(SetResetFF_next_k);
953 unit->m_prevtrig = 0.f;
954 unit->m_prevreset = 0.f;
955 unit->mLevel = 0.f;
957 ZOUT0(0) = 0.f;
961 void SetResetFF_next_a(SetResetFF *unit, int inNumSamples)
963 float *out = ZOUT(0);
964 float *trig = ZIN(0);
965 float *reset = ZIN(1);
966 float prevtrig = unit->m_prevtrig;
967 float prevreset = unit->m_prevreset;
968 float level = unit->mLevel;
970 LOOP1(inNumSamples,
971 float curtrig = ZXP(trig);
972 float curreset = ZXP(reset);
973 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
974 else if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f;
975 ZXP(out) = level;
976 prevtrig = curtrig;
977 prevreset = curreset;
979 unit->m_prevtrig = prevtrig;
980 unit->m_prevreset = prevreset;
981 unit->mLevel = level;
984 void SetResetFF_next_k(SetResetFF *unit, int inNumSamples)
986 float *out = ZOUT(0);
987 float *trig = ZIN(0);
988 float curreset = ZIN0(1);
989 float prevtrig = unit->m_prevtrig;
990 float prevreset = unit->m_prevreset;
991 float level = unit->mLevel;
993 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
995 LOOP1(inNumSamples,
996 float 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 inline_functions 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 inline_functions 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 inline_functions void Peak_next_ak_k_nova(Peak *unit, int inNumSamples);
1878 inline_functions 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);