Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / server / plugins / TriggerUGens.cpp
blobc0142aa7db51002e562b8da1160a1b34c9ad0068
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 *values = unit->m_values;
842 int valueSize = unit->m_valueSize;
843 int valueOffset = unit->m_valueOffset;
844 for(int j = 0; j < inNumSamples; j++) {
845 float curtrig = trig[j];
846 if (curtrig > 0.f && prevtrig <= 0.f) {
847 for(int i=0; i<valueSize; i++) {
848 int offset = INRATE( i + valueOffset ) != calc_FullRate ? 0 : j;
849 values[i] = IN(i + valueOffset)[offset];
851 SendNodeReply(&unit->mParent->mNode, (int)ZIN0(1), unit->m_cmdName, unit->m_valueSize, values);
853 prevtrig = curtrig;
855 unit->m_prevtrig = prevtrig;
859 ////////////////////////////////////////////////////////////////////////////////////////////////////////
861 void Poll_Ctor(Poll* unit)
863 if (INRATE(0) == calc_FullRate){
864 if (INRATE(1) == calc_FullRate){
865 SETCALC(Poll_next_aa);
866 } else {
867 SETCALC(Poll_next_ak);
869 } else {
870 SETCALC(Poll_next_kk);
873 unit->m_trig = IN0(0);
874 unit->m_id = IN0(3); // number of chars in the id string
875 unit->m_id_string = (char*)RTAlloc(unit->mWorld, ((int)unit->m_id + 1) * sizeof(char));
876 for(int i = 0; i < (int)unit->m_id; i++){
877 unit->m_id_string[i] = (char)IN0(4+i);
879 unit->m_id_string[(int)unit->m_id] = '\0';
881 unit->m_mayprint = unit->mWorld->mVerbosity >= -1;
883 Poll_next_kk(unit, 1);
886 void Poll_Dtor(Poll* unit)
888 RTFree(unit->mWorld, unit->m_id_string);
891 void Poll_next_aa(Poll *unit, int inNumSamples){
892 float* in = IN(1);
893 float* trig = IN(0);
894 float lasttrig = unit->m_trig;
895 for(int i = 0; i < inNumSamples; i++){
896 if((lasttrig <= 0.0) && (trig[i] > 0.0)){
897 if(unit->m_mayprint){
898 Print("%s: %g\n", unit->m_id_string, in[i]);
900 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in[i]);
902 lasttrig = trig[i];
904 unit->m_trig = lasttrig;
907 void Poll_next_kk(Poll *unit, int inNumSamples){
908 float in = IN0(1);
909 float trig = IN0(0);
910 if((unit->m_trig <= 0.0) && (trig > 0.0)){
911 if(unit->m_mayprint){
912 Print("%s: %g\n", unit->m_id_string, in);
914 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in);
916 unit->m_trig = trig;
920 void Poll_next_ak(Poll *unit, int inNumSamples){
921 float in = IN0(1);
922 float* trig = IN(0);
923 float lasttrig = unit->m_trig;
924 for(int i = 0; i < inNumSamples; i++){
925 if((lasttrig <= 0.0) && (trig[i] > 0.0)){
926 if(unit->m_mayprint){
927 Print("%s: %g\n", unit->m_id_string, in);
929 if(IN0(2) >= 0.0) SendTrigger(&unit->mParent->mNode, (int)IN0(2), in);
931 lasttrig = trig[i];
933 unit->m_trig = lasttrig;
938 ////////////////////////////////////////////////////////////////////////////////////////////////////////
940 void SetResetFF_Ctor(SetResetFF *unit)
942 if (INRATE(1) == calc_FullRate) {
943 SETCALC(SetResetFF_next_a);
944 } else {
945 SETCALC(SetResetFF_next_k);
948 unit->m_prevtrig = 0.f;
949 unit->m_prevreset = 0.f;
950 unit->mLevel = 0.f;
952 ZOUT0(0) = 0.f;
956 void SetResetFF_next_a(SetResetFF *unit, int inNumSamples)
958 float *out = ZOUT(0);
959 float *trig = ZIN(0);
960 float *reset = ZIN(1);
961 float prevtrig = unit->m_prevtrig;
962 float prevreset = unit->m_prevreset;
963 float level = unit->mLevel;
965 LOOP1(inNumSamples,
966 float curtrig = ZXP(trig);
967 float curreset = ZXP(reset);
968 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
969 else if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f;
970 ZXP(out) = level;
971 prevtrig = curtrig;
972 prevreset = curreset;
974 unit->m_prevtrig = prevtrig;
975 unit->m_prevreset = prevreset;
976 unit->mLevel = level;
979 void SetResetFF_next_k(SetResetFF *unit, int inNumSamples)
981 float *out = ZOUT(0);
982 float *trig = ZIN(0);
983 float curreset = ZIN0(1);
984 float prevtrig = unit->m_prevtrig;
985 float prevreset = unit->m_prevreset;
986 float level = unit->mLevel;
988 float curtrig = ZXP(trig);
989 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
990 else if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f;
991 ZXP(out) = level;
992 prevtrig = curtrig;
994 LOOP(inNumSamples - 1,
995 curtrig = ZXP(trig);
996 if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f;
997 ZXP(out) = level;
998 prevtrig = curtrig;
1000 unit->m_prevtrig = prevtrig;
1001 unit->m_prevreset = curreset;
1002 unit->mLevel = level;
1005 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1008 void ToggleFF_Ctor(ToggleFF *unit)
1010 SETCALC(ToggleFF_next);
1012 unit->m_prevtrig = 0.f;
1013 unit->mLevel = 0.f;
1015 ZOUT0(0) = 0.f;
1019 void ToggleFF_next(ToggleFF *unit, int inNumSamples)
1021 float *out = ZOUT(0);
1022 float *trig = ZIN(0);
1023 float prevtrig = unit->m_prevtrig;
1024 float level = unit->mLevel;
1026 LOOP1(inNumSamples,
1027 float curtrig = ZXP(trig);
1028 if (prevtrig <= 0.f && curtrig > 0.f) level = 1.f - level;
1029 ZXP(out) = level;
1030 prevtrig = curtrig;
1032 unit->m_prevtrig = prevtrig;
1033 unit->mLevel = level;
1036 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1039 #ifdef NOVA_SIMD
1040 void Latch_next_ak_nova(Latch *unit, int inNumSamples)
1042 float level = unit->mLevel;
1044 float curtrig = ZIN0(1);
1045 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = ZIN0(0);
1047 nova::setvec_simd(OUT(0), level, inNumSamples);
1049 unit->m_prevtrig = curtrig;
1050 unit->mLevel = level;
1053 void Latch_next_ak_nova_64(Latch *unit, int inNumSamples)
1055 float level = unit->mLevel;
1057 float curtrig = ZIN0(1);
1058 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = ZIN0(0);
1060 nova::setvec_simd<64>(OUT(0), level);
1062 unit->m_prevtrig = curtrig;
1063 unit->mLevel = level;
1065 #endif
1067 void Latch_Ctor(Latch *unit)
1069 if (INRATE(1) == calc_FullRate) {
1070 SETCALC(Latch_next_aa);
1071 } else {
1072 #ifdef NOVA_SIMD
1073 if (BUFLENGTH == 64)
1074 SETCALC(Latch_next_ak_nova_64);
1075 if (!(BUFLENGTH & 15))
1076 SETCALC(Latch_next_ak_nova);
1077 else
1078 #endif
1079 SETCALC(Latch_next_ak);
1082 unit->m_prevtrig = 0.f;
1083 unit->mLevel = 0.f;
1085 ZOUT0(0) = ZIN0(1) > 0.f ? ZIN0(0) : 0.f;
1089 void Latch_next_ak(Latch *unit, int inNumSamples)
1091 float *out = ZOUT(0);
1092 float level = unit->mLevel;
1094 float curtrig = ZIN0(1);
1095 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = ZIN0(0);
1097 LOOP1(inNumSamples, ZXP(out) = level; );
1099 unit->m_prevtrig = curtrig;
1100 unit->mLevel = level;
1104 void Latch_next_aa(Latch *unit, int inNumSamples)
1106 float *out = ZOUT(0);
1107 float *in = ZIN(0);
1108 float *trig = ZIN(1);
1109 float prevtrig = unit->m_prevtrig;
1110 float level = unit->mLevel;
1112 LOOP1(inNumSamples,
1113 float curtrig = ZXP(trig);
1114 if (prevtrig <= 0.f && curtrig > 0.f) level = ZXP(in);
1115 else { PZ(in); }
1116 ZXP(out) = level;
1117 prevtrig = curtrig;
1119 unit->m_prevtrig = prevtrig;
1120 unit->mLevel = level;
1125 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1127 #ifdef NOVA_SIMD
1128 FLATTEN void Gate_next_ak_nova(Gate *unit, int inNumSamples)
1130 float *trig = ZIN(1);
1131 float level = unit->mLevel;
1133 float curtrig = ZXP(trig);
1134 if (curtrig > 0.f) {
1135 nova::copyvec_simd(OUT(0), IN(0), inNumSamples);
1136 unit->mLevel = IN(0)[inNumSamples-1];
1137 } else
1138 nova::setvec_simd(OUT(0), level, inNumSamples);
1141 FLATTEN void Gate_next_ak_nova_64(Gate *unit, int inNumSamples)
1143 float *trig = ZIN(1);
1144 float level = unit->mLevel;
1146 float curtrig = ZXP(trig);
1147 if (curtrig > 0.f) {
1148 nova::copyvec_simd<64>(OUT(0), IN(0));
1149 unit->mLevel = IN(0)[inNumSamples-1];
1150 } else
1151 nova::setvec_simd<64>(OUT(0), level);
1153 #endif
1155 void Gate_Ctor(Gate *unit)
1157 if (INRATE(1) == calc_FullRate) {
1158 SETCALC(Gate_next_aa);
1159 } else {
1160 #ifdef NOVA_SIMD
1161 if (BUFLENGTH == 64)
1162 SETCALC(Gate_next_ak_nova_64);
1163 if (!(BUFLENGTH & 15))
1164 SETCALC(Gate_next_ak_nova);
1165 else
1166 #endif
1167 SETCALC(Gate_next_ak);
1170 unit->mLevel = 0.f;
1172 ZOUT0(0) = 0.f;
1177 void Gate_next_ak(Gate *unit, int inNumSamples)
1179 float *out = ZOUT(0);
1180 float *in = ZIN(0);
1181 float *trig = ZIN(1);
1182 float level = unit->mLevel;
1184 float curtrig = ZXP(trig);
1185 if (curtrig > 0.f) {
1186 LOOP1(inNumSamples,
1187 level = ZXP(in);
1188 ZXP(out) = level;
1190 unit->mLevel = level;
1191 } else {
1192 LOOP1(inNumSamples,
1193 ZXP(out) = level;
1198 void Gate_next_aa(Gate *unit, int inNumSamples)
1200 float *out = ZOUT(0);
1201 float *in = ZIN(0);
1202 float *trig = ZIN(1);
1203 float level = unit->mLevel;
1205 LOOP1(inNumSamples,
1206 float curtrig = ZXP(trig);
1207 if (curtrig > 0.f) level = ZXP(in);
1208 else { PZ(in); }
1209 ZXP(out) = level;
1211 unit->mLevel = level;
1214 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1217 void Schmidt_Ctor(Schmidt *unit)
1219 SETCALC(Schmidt_next);
1221 unit->mLevel = 0.f;
1223 ZOUT0(0) = 0.f;
1226 void Schmidt_next(Schmidt *unit, int inNumSamples)
1228 float *out = ZOUT(0);
1229 float *in = ZIN(0);
1230 float lo = ZIN0(1);
1231 float hi = ZIN0(2);
1232 float level = unit->mLevel;
1233 LOOP1(inNumSamples,
1234 float zin = ZXP(in);
1235 if (level == 1.) {
1236 if (zin < lo) level = 0.f;
1237 } else {
1238 if (zin > hi) level = 1.f;
1240 ZXP(out) = level;
1242 unit->mLevel = level;
1245 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1247 void PulseDivider_Ctor(PulseDivider *unit)
1249 SETCALC(PulseDivider_next);
1251 unit->m_prevtrig = 0.f;
1252 unit->mLevel = 0.f;
1253 unit->mCounter = (long)floor(ZIN0(2) + 0.5);
1255 ZOUT0(0) = 0.f;
1259 void PulseDivider_next(PulseDivider *unit, int inNumSamples)
1261 float *out = ZOUT(0);
1262 float *trig = ZIN(0);
1263 long div = (long)ZIN0(1);
1264 float prevtrig = unit->m_prevtrig;
1265 long counter = unit->mCounter;
1267 LOOP1(inNumSamples,
1268 float z;
1269 float curtrig = ZXP(trig);
1270 if (prevtrig <= 0.f && curtrig > 0.f) {
1271 counter++;
1272 if (counter >= div) {
1273 counter = 0;
1274 z = 1.f;
1275 } else {
1276 z = 0.f;
1278 } else {
1279 z = 0.f;
1281 ZXP(out) = z;
1282 prevtrig = curtrig;
1284 unit->mCounter = counter;
1285 unit->m_prevtrig = prevtrig;
1288 //////////////////////////////////////////////////////////////////////////////////////////
1291 void PulseCount_Ctor(PulseCount *unit)
1293 if (INRATE(1) == calc_FullRate) {
1294 SETCALC(PulseCount_next_a);
1295 } else if (INRATE(1) == calc_BufRate) {
1296 SETCALC(PulseCount_next_k);
1297 } else {
1298 SETCALC(PulseCount_next_0);
1300 unit->m_prevtrig = 0.f;
1301 unit->m_prevreset = 0.f;
1302 unit->mLevel = 0.f;
1304 ZOUT0(0) = 0.f;
1308 void PulseCount_next_a(PulseCount *unit, int inNumSamples)
1310 float *out = ZOUT(0);
1311 float *trig = ZIN(0);
1312 float *reset = ZIN(1);
1313 float prevtrig = unit->m_prevtrig;
1314 float prevreset = unit->m_prevreset;
1315 float level = unit->mLevel;
1317 LOOP1(inNumSamples,
1318 float curtrig = ZXP(trig);
1319 float curreset = ZXP(reset);
1320 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
1321 else if (prevtrig <= 0.f && curtrig > 0.f) {
1322 level += 1.f;
1324 ZXP(out) = level;
1325 prevtrig = curtrig;
1326 prevreset = curreset;
1328 unit->mLevel = level;
1329 unit->m_prevtrig = prevtrig;
1330 unit->m_prevreset = prevreset;
1333 void PulseCount_next_k(PulseCount *unit, int inNumSamples)
1335 float *out = ZOUT(0);
1336 float *trig = ZIN(0);
1337 float curreset = ZIN0(1);
1338 float prevtrig = unit->m_prevtrig;
1339 float prevreset = unit->m_prevreset;
1340 float level = unit->mLevel;
1342 LOOP1(inNumSamples,
1343 float curtrig = ZXP(trig);
1344 if (prevreset <= 0.f && curreset > 0.f) level = 0.f;
1345 else if (prevtrig <= 0.f && curtrig > 0.f) {
1346 level += 1.f;
1348 ZXP(out) = level;
1349 prevtrig = curtrig;
1350 prevreset = curreset;
1352 unit->mLevel = level;
1353 unit->m_prevtrig = prevtrig;
1354 unit->m_prevreset = prevreset;
1357 void PulseCount_next_0(PulseCount *unit, int inNumSamples)
1359 float *out = ZOUT(0);
1360 float *trig = ZIN(0);
1361 float prevtrig = unit->m_prevtrig;
1362 float level = unit->mLevel;
1364 LOOP1(inNumSamples,
1365 float curtrig = ZXP(trig);
1366 if (prevtrig <= 0.f && curtrig > 0.f) {
1367 level += 1.f;
1369 ZXP(out) = level;
1370 prevtrig = curtrig;
1372 unit->mLevel = level;
1373 unit->m_prevtrig = prevtrig;
1376 //////////////////////////////////////////////////////////////////////////////////////////
1379 void Stepper_Ctor(Stepper *unit)
1381 if (unit->mCalcRate == calc_FullRate && INRATE(0) == calc_FullRate && INRATE(1) == calc_ScalarRate) {
1382 SETCALC(Stepper_next_a0);
1383 } else if (unit->mCalcRate == calc_FullRate && INRATE(0) == calc_FullRate && INRATE(1) != calc_FullRate) {
1384 SETCALC(Stepper_next_ak);
1385 } else {
1386 SETCALC(Stepper_next_aa);
1389 int32 resetval = (int32)ZIN0(5);
1391 unit->m_prevtrig = 0.f;
1392 unit->m_prevreset = 0.f;
1393 unit->mLevel = (float)resetval;
1395 ZOUT0(0) = 0.f;
1399 void Stepper_next_aa(Stepper *unit, int inNumSamples)
1401 float *out = ZOUT(0);
1402 float *trig = ZIN(0);
1403 float *reset = ZIN(1);
1404 int32 zmin = (int32)ZIN0(2);
1405 int32 zmax = (int32)ZIN0(3);
1406 int32 step = (int32)ZIN0(4);
1407 int32 resetval = (int32)ZIN0(5);
1408 float prevtrig = unit->m_prevtrig;
1409 float prevreset = unit->m_prevreset;
1410 float level = unit->mLevel;
1412 LOOP1(inNumSamples,
1413 float curtrig = ZXP(trig);
1414 float curreset = ZXP(reset);
1415 if (prevreset <= 0.f && curreset > 0.f) {
1416 level = (float)sc_wrap(resetval, zmin, zmax);
1417 } else if (prevtrig <= 0.f && curtrig > 0.f) {
1418 level = (float)sc_wrap((int32)level + step, zmin, zmax);
1420 ZXP(out) = level;
1421 prevtrig = curtrig;
1422 prevreset = curreset;
1424 unit->mLevel = level;
1425 unit->m_prevtrig = prevtrig;
1426 unit->m_prevreset = prevreset;
1429 void Stepper_next_ak(Stepper *unit, int inNumSamples)
1431 float *out = ZOUT(0);
1432 float *trig = ZIN(0);
1433 float curreset = ZIN0(1);
1434 int32 zmin = (int32)ZIN0(2);
1435 int32 zmax = (int32)ZIN0(3);
1436 int32 step = (int32)ZIN0(4);
1437 int32 resetval = (int32)ZIN0(5);
1438 float prevtrig = unit->m_prevtrig;
1439 float prevreset = unit->m_prevreset;
1440 float level = unit->mLevel;
1442 LOOP1(inNumSamples,
1443 float curtrig = ZXP(trig);
1444 if (prevreset <= 0.f && curreset > 0.f) {
1445 level = (float)sc_wrap(resetval, zmin, zmax);
1446 } else if (prevtrig <= 0.f && curtrig > 0.f) {
1447 level = (float)sc_wrap((int32)level + step, zmin, zmax);
1449 ZXP(out) = level;
1450 prevtrig = curtrig;
1451 prevreset = curreset;
1453 unit->mLevel = level;
1454 unit->m_prevtrig = prevtrig;
1455 unit->m_prevreset = prevreset;
1458 void Stepper_next_a0(Stepper *unit, int inNumSamples)
1460 float *out = ZOUT(0);
1461 float *trig = ZIN(0);
1462 int32 zmin = (int32)ZIN0(2);
1463 int32 zmax = (int32)ZIN0(3);
1464 int32 step = (int32)ZIN0(4);
1465 float prevtrig = unit->m_prevtrig;
1466 float level = unit->mLevel;
1468 LOOP1(inNumSamples,
1469 float curtrig = ZXP(trig);
1470 if (prevtrig <= 0.f && curtrig > 0.f) {
1471 level = (float)sc_wrap((int32)level + step, zmin, zmax);
1473 ZXP(out) = level;
1474 prevtrig = curtrig;
1476 unit->mLevel = level;
1477 unit->m_prevtrig = prevtrig;
1480 //////////////////////////////////////////////////////////////////////////////////////////
1482 void TDelay_Ctor(TDelay *unit)
1484 SETCALC(TDelay_next);
1486 unit->m_prevtrig = 0.f;
1487 unit->mCounter = 0;
1489 ZOUT0(0) = 0.f;
1494 void TDelay_next(TDelay *unit, int inNumSamples)
1496 float *out = ZOUT(0);
1497 float *trig = ZIN(0);
1498 float dur = ZIN0(1);
1499 float prevtrig = unit->m_prevtrig;
1500 long counter = unit->mCounter;
1502 LOOP1(inNumSamples,
1503 float curtrig = ZXP(trig);
1504 float zout;
1505 if (counter > 1) {
1506 counter--;
1507 zout = 0.f;
1508 } else if (counter<=0) {
1509 if (prevtrig <= 0.f && curtrig > 0.f) {
1510 counter = (long)(dur * unit->mRate->mSampleRate + .5f);
1511 if (counter < 1) counter = 1;
1513 zout = 0.f;
1514 } else {
1515 counter = 0;
1516 zout = 1.f;
1518 ZXP(out) = zout;
1519 prevtrig = curtrig;
1521 unit->m_prevtrig = prevtrig;
1522 unit->mCounter = counter;
1526 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1529 void ZeroCrossing_Ctor(ZeroCrossing *unit)
1531 SETCALC(ZeroCrossing_next_a);
1533 unit->m_prevfrac = 0.f;
1534 unit->m_previn = ZIN0(0);
1535 ZOUT0(0) = unit->mLevel = 0.f;
1536 unit->mCounter = 0;
1539 void ZeroCrossing_next_a(ZeroCrossing *unit, int inNumSamples)
1541 float *out = ZOUT(0);
1542 float *in = ZIN(0);
1543 float previn = unit->m_previn;
1544 float prevfrac = unit->m_prevfrac;
1545 float level = unit->mLevel;
1546 long counter = unit->mCounter;
1548 LOOP1(inNumSamples,
1549 counter++;
1550 float curin = ZXP(in);
1551 if (counter > 4 && previn <= 0.f && curin > 0.f) {
1552 float frac = -previn/(curin-previn);
1553 level = unit->mRate->mSampleRate / (frac + counter - prevfrac);
1554 prevfrac = frac;
1555 counter = 0;
1557 ZXP(out) = level;
1558 previn = curin;
1561 unit->m_previn = previn;
1562 unit->m_prevfrac = prevfrac;
1563 unit->mLevel = level;
1564 unit->mCounter = counter;
1567 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1569 void Timer_Ctor(Timer *unit)
1571 SETCALC(Timer_next_a);
1573 unit->m_prevfrac = 0.f;
1574 unit->m_previn = ZIN0(0);
1575 ZOUT0(0) = unit->mLevel = 0.f;
1576 unit->mCounter = 0;
1579 void Timer_next_a(Timer *unit, int inNumSamples)
1581 float *out = ZOUT(0);
1582 float *in = ZIN(0);
1583 float previn = unit->m_previn;
1584 float prevfrac = unit->m_prevfrac;
1585 float level = unit->mLevel;
1586 long counter = unit->mCounter;
1588 LOOP1(inNumSamples,
1589 counter++;
1590 float curin = ZXP(in);
1591 if (previn <= 0.f && curin > 0.f) {
1592 float frac = -previn/(curin-previn);
1593 level = unit->mRate->mSampleDur * (frac + counter - prevfrac);
1594 prevfrac = frac;
1595 counter = 0;
1597 ZXP(out) = level;
1598 previn = curin;
1601 unit->m_previn = previn;
1602 unit->m_prevfrac = prevfrac;
1603 unit->mLevel = level;
1604 unit->mCounter = counter;
1608 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1610 void Sweep_Ctor(Sweep *unit)
1612 if (INRATE(0) == calc_ScalarRate) {
1613 if (INRATE(1) == calc_FullRate) {
1614 SETCALC(Sweep_next_0a);
1615 } else {
1616 SETCALC(Sweep_next_0k);
1618 } else if (INRATE(0) == calc_BufRate) {
1619 if (INRATE(1) == calc_FullRate) {
1620 SETCALC(Sweep_next_ka);
1621 } else {
1622 SETCALC(Sweep_next_kk);
1624 } else {
1625 if (INRATE(1) == calc_FullRate) {
1626 SETCALC(Sweep_next_aa);
1627 } else {
1628 SETCALC(Sweep_next_ak);
1632 unit->m_previn = ZIN0(0);
1633 ZOUT0(0) = unit->mLevel = 0.f;
1636 void Sweep_next_0k(Sweep *unit, int inNumSamples)
1638 float *out = ZOUT(0);
1639 double rate = ZIN0(1) * SAMPLEDUR;
1640 double level = unit->mLevel;
1642 LOOP1(inNumSamples,
1643 level += rate;
1644 ZXP(out) = level;
1647 unit->mLevel = level;
1650 void Sweep_next_0a(Sweep *unit, int inNumSamples)
1652 float *out = ZOUT(0);
1653 float *rate = ZIN(1);
1654 double level = unit->mLevel;
1655 float sampledur = SAMPLEDUR;
1657 LOOP1(inNumSamples,
1658 float zrate = ZXP(rate) * sampledur;
1659 level += zrate;
1660 ZXP(out) = level;
1663 unit->mLevel = level;
1666 void Sweep_next_kk(Sweep *unit, int inNumSamples)
1668 float *out = ZOUT(0);
1669 float curin = ZIN0(0);
1670 double rate = ZIN0(1) * SAMPLEDUR;
1671 float previn = unit->m_previn;
1672 double level = unit->mLevel;
1674 if (previn <= 0.f && curin > 0.f) {
1675 float frac = -previn/(curin-previn);
1676 level = frac * rate;
1679 LOOP1(inNumSamples,
1680 level += rate;
1681 ZXP(out) = level;
1684 unit->m_previn = curin;
1685 unit->mLevel = level;
1688 void Sweep_next_ka(Sweep *unit, int inNumSamples)
1690 float *out = ZOUT(0);
1691 float curin = ZIN0(0);
1692 float *rate = ZIN(1);
1693 float previn = unit->m_previn;
1694 double level = unit->mLevel;
1695 float sampledur = SAMPLEDUR;
1697 if (previn <= 0.f && curin > 0.f) {
1698 float frac = -previn/(curin-previn);
1699 level = frac * rate[ZOFF] * sampledur;
1702 LOOP1(inNumSamples,
1703 float zrate = ZXP(rate) * sampledur;
1704 level += zrate;
1705 ZXP(out) = level;
1708 unit->m_previn = curin;
1709 unit->mLevel = level;
1712 void Sweep_next_ak(Sweep *unit, int inNumSamples)
1714 float *out = ZOUT(0);
1715 float *in = ZIN(0);
1716 double rate = ZIN0(1) * SAMPLEDUR;
1717 float previn = unit->m_previn;
1718 double level = unit->mLevel;
1720 LOOP1(inNumSamples,
1721 float curin = ZXP(in);
1722 if (previn <= 0.f && curin > 0.f) {
1723 float frac = -previn/(curin-previn);
1724 level = frac * rate;
1725 } else {
1726 level += rate;
1728 ZXP(out) = level;
1729 previn = curin;
1732 unit->m_previn = previn;
1733 unit->mLevel = level;
1736 void Sweep_next_aa(Sweep *unit, int inNumSamples)
1738 float *out = ZOUT(0);
1739 float *in = ZIN(0);
1740 float *rate = ZIN(1);
1741 float previn = unit->m_previn;
1742 double level = unit->mLevel;
1743 float sampledur = SAMPLEDUR;
1745 LOOP1(inNumSamples,
1746 float curin = ZXP(in);
1747 float zrate = ZXP(rate) * sampledur;
1748 if (previn <= 0.f && curin > 0.f) {
1749 float frac = -previn/(curin-previn);
1750 level = frac * zrate;
1751 } else {
1752 level += zrate;
1754 ZXP(out) = level;
1755 previn = curin;
1758 unit->m_previn = previn;
1759 unit->mLevel = level;
1762 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1764 void Phasor_Ctor(Phasor *unit)
1766 if (unit->mCalcRate == calc_FullRate) {
1767 if (INRATE(0) == calc_FullRate) {
1768 if (INRATE(1) == calc_FullRate) {
1769 SETCALC(Phasor_next_aa);
1770 } else {
1771 SETCALC(Phasor_next_ak);
1773 } else {
1774 SETCALC(Phasor_next_kk);
1776 } else {
1777 SETCALC(Phasor_next_ak);
1780 unit->m_previn = ZIN0(0);
1781 ZOUT0(0) = unit->mLevel = ZIN0(2);
1784 void Phasor_next_kk(Phasor *unit, int inNumSamples)
1786 float *out = ZOUT(0);
1788 float in = ZIN0(0);
1789 double rate = ZIN0(1);
1790 double start = ZIN0(2);
1791 double end = ZIN0(3);
1792 float resetPos = ZIN0(4);
1794 float previn = unit->m_previn;
1795 double level = unit->mLevel;
1797 if (previn <= 0.f && in > 0.f) {
1798 level = resetPos;
1800 LOOP1(inNumSamples,
1801 level = sc_wrap(level, start, end);
1802 ZXP(out) = level;
1803 level += rate;
1806 unit->m_previn = in;
1807 unit->mLevel = level;
1810 void Phasor_next_ak(Phasor *unit, int inNumSamples)
1812 float *out = ZOUT(0);
1814 float *in = ZIN(0);
1815 double rate = ZIN0(1);
1816 double start = ZIN0(2);
1817 double end = ZIN0(3);
1818 float resetPos = ZIN0(4);
1820 float previn = unit->m_previn;
1821 double level = unit->mLevel;
1823 LOOP1(inNumSamples,
1824 float curin = ZXP(in);
1825 if (previn <= 0.f && curin > 0.f) {
1826 float frac = 1.f - previn/(curin-previn);
1827 level = resetPos + frac * rate;
1829 ZXP(out) = level;
1830 level += rate;
1831 level = sc_wrap(level, start, end);
1833 previn = curin;
1836 unit->m_previn = previn;
1837 unit->mLevel = level;
1840 void Phasor_next_aa(Phasor *unit, int inNumSamples)
1842 float *out = ZOUT(0);
1843 float *in = ZIN(0);
1844 float *rate = ZIN(1);
1845 double start = ZIN0(2);
1846 double end = ZIN0(3);
1847 float resetPos = ZIN0(4);
1849 float previn = unit->m_previn;
1850 double level = unit->mLevel;
1852 LOOP1(inNumSamples,
1853 float curin = ZXP(in);
1854 double zrate = ZXP(rate);
1855 if (previn <= 0.f && curin > 0.f) {
1856 float frac = 1.f - previn/(curin-previn);
1857 level = resetPos + frac * zrate;
1859 ZXP(out) = level;
1860 level += zrate;
1861 level = sc_wrap(level, start, end);
1862 previn = curin;
1865 unit->m_previn = previn;
1866 unit->mLevel = level;
1869 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1872 void Peak_next_ak_unroll(Peak *unit, int inNumSamples);
1873 void Peak_next_ai_unroll(Peak *unit, int inNumSamples);
1875 #ifdef NOVA_SIMD
1876 FLATTEN void Peak_next_ak_k_nova(Peak *unit, int inNumSamples);
1877 FLATTEN void Peak_next_ai_k_nova(Peak *unit, int inNumSamples);
1878 #endif
1880 void Peak_Ctor(Peak *unit)
1882 if (BUFLENGTH == 1 &&
1883 INRATE(0) == calc_FullRate) {
1884 /* audio-rate input with control-rate output */
1885 if (INRATE(1) == calc_FullRate) {
1886 SETCALC(Peak_next_aa_k);
1887 } else if (INRATE(1) == calc_ScalarRate) {
1888 #ifdef NOVA_SIMD
1889 if (INBUFLENGTH(0) & 7)
1890 SETCALC(Peak_next_ai_k);
1891 else
1892 SETCALC(Peak_next_ai_k_nova);
1893 #else
1894 SETCALC(Peak_next_ai_k);
1895 #endif
1896 } else {
1897 #ifdef NOVA_SIMD
1898 if (INBUFLENGTH(0) & 7)
1899 SETCALC(Peak_next_ak_k);
1900 else
1901 SETCALC(Peak_next_ak_k_nova);
1902 #else
1903 SETCALC(Peak_next_ak_k);
1904 #endif
1906 } else {
1907 if (INRATE(1) == calc_FullRate) {
1908 SETCALC(Peak_next_aa);
1909 } else if (INRATE(1) == calc_ScalarRate) {
1910 if (BUFLENGTH & 15)
1911 SETCALC(Peak_next_ai);
1912 else
1913 SETCALC(Peak_next_ai_unroll);
1914 } else {
1915 if (BUFLENGTH & 15)
1916 SETCALC(Peak_next_ak);
1917 else
1918 SETCALC(Peak_next_ak_unroll);
1921 unit->m_prevtrig = 0.f;
1922 ZOUT0(0) = unit->mLevel = ZIN0(0);
1925 void Peak_next_ak(Peak *unit, int inNumSamples)
1927 float *out = ZOUT(0);
1928 float *in = ZIN(0);
1929 float curtrig = ZIN0(1);
1930 float level = unit->mLevel;
1931 float inlevel;
1932 LOOP1(inNumSamples,
1933 inlevel = std::abs(ZXP(in));
1934 level = std::max(inlevel, level);
1935 ZXP(out) = level;
1937 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
1938 unit->m_prevtrig = curtrig;
1939 unit->mLevel = level;
1942 void Peak_next_ai(Peak *unit, int inNumSamples)
1944 float *out = ZOUT(0);
1945 float *in = ZIN(0);
1946 float level = unit->mLevel;
1947 float inlevel;
1948 LOOP1(inNumSamples,
1949 inlevel = std::abs(ZXP(in));
1950 level = std::max(inlevel, level);
1951 ZXP(out) = level;
1953 unit->mLevel = level;
1956 void Peak_next_aa(Peak *unit, int inNumSamples)
1958 float *out = ZOUT(0);
1959 float *in = ZIN(0);
1960 float *trig = ZIN(1);
1961 float prevtrig = unit->m_prevtrig;
1962 float level = unit->mLevel;
1964 LOOP1(inNumSamples,
1965 float curtrig = ZXP(trig);
1966 float inlevel = std::abs(ZXP(in));
1967 level = std::max(inlevel, level);
1968 ZXP(out) = level;
1969 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
1970 prevtrig = curtrig;
1972 unit->m_prevtrig = prevtrig;
1973 unit->mLevel = level;
1976 static inline float Peak_unroll_body(Peak *unit, int inNumSamples, float & level)
1978 float *out = OUT(0);
1979 float *in = IN(0);
1980 float inlevel;
1982 using namespace std;
1983 for (int i = 0; i != inNumSamples; i += 8, out += 8, in += 8)
1985 float level0 = max(abs(in[0]), level);
1986 float level1 = max(abs(in[1]), level0);
1987 float level2 = max(abs(in[2]), level1);
1988 float level3 = max(abs(in[3]), level2);
1989 float level4 = max(abs(in[4]), level3);
1990 float level5 = max(abs(in[5]), level4);
1991 float level6 = max(abs(in[6]), level5);
1992 inlevel = abs(in[7]);
1993 float level7 = max(inlevel, level6);
1994 out[0] = level0;
1995 out[1] = level1;
1996 out[2] = level2;
1997 out[3] = level3;
1998 out[4] = level4;
1999 out[5] = level5;
2000 out[6] = level6;
2001 out[7] = level7;
2002 level = level7;
2004 return inlevel; /* input level of the last sample */
2007 void Peak_next_ak_unroll(Peak *unit, int inNumSamples)
2009 float curtrig = ZIN0(1);
2010 float level = unit->mLevel;
2011 float inlevel = Peak_unroll_body(unit, inNumSamples, level);
2013 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2014 unit->m_prevtrig = curtrig;
2015 unit->mLevel = level;
2018 void Peak_next_ai_unroll(Peak *unit, int inNumSamples)
2020 float level = unit->mLevel;
2021 Peak_unroll_body(unit, inNumSamples, level);
2022 unit->mLevel = level;
2025 void Peak_next_ak_k(Peak *unit, int inNumSamples)
2027 float *out = ZOUT(0);
2028 float *in = ZIN(0);
2029 float curtrig = ZIN0(1);
2030 float level;
2031 inNumSamples = INBUFLENGTH(0);
2032 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) {
2033 level = std::abs(ZXP(in));
2034 inNumSamples -= 1;
2036 else
2037 level = unit->mLevel;
2038 LOOP1(inNumSamples,
2039 level = std::max(std::abs(ZXP(in)), level);
2041 ZXP(out) = level;
2042 unit->m_prevtrig = curtrig;
2043 unit->mLevel = level;
2046 void Peak_next_ai_k(Peak *unit, int inNumSamples)
2048 float *out = ZOUT(0);
2049 float *in = ZIN(0);
2050 float level = unit->mLevel;
2051 inNumSamples = INBUFLENGTH(0);
2052 LOOP1(inNumSamples,
2053 level = std::max(std::abs(ZXP(in)), level);
2055 ZXP(out) = level;
2056 unit->mLevel = level;
2059 void Peak_next_aa_k(Peak *unit, int inNumSamples)
2061 float *out = ZOUT(0);
2062 float *in = ZIN(0);
2063 float *trig = ZIN(1);
2064 float prevtrig = unit->m_prevtrig;
2065 float level = unit->mLevel;
2066 float out_level;
2067 bool triggered = false;
2069 inNumSamples = INBUFLENGTH(0);
2070 LOOP1(inNumSamples,
2071 float curtrig = ZXP(trig);
2072 float inlevel = std::abs(ZXP(in));
2073 level = std::max(inlevel, level);
2074 if (prevtrig <= 0.f && curtrig > 0.f) {
2075 triggered = true;
2076 out_level = level;
2077 level = inlevel;
2079 prevtrig = curtrig;
2081 if (triggered)
2082 ZXP(out) = out_level;
2083 else
2084 ZXP(out) = level;
2085 unit->m_prevtrig = prevtrig;
2086 unit->mLevel = level;
2089 #ifdef NOVA_SIMD
2090 void Peak_next_ak_k_nova(Peak *unit, int inNumSamples)
2092 float *out = ZOUT(0);
2093 float *in = IN(0);
2094 float curtrig = ZIN0(1);
2095 float level = unit->mLevel;
2096 float inlevel;
2097 inNumSamples = INBUFLENGTH(0);
2098 inlevel = nova::peak_vec_simd(in, &level, inNumSamples);
2099 ZXP(out) = level;
2100 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2101 unit->m_prevtrig = curtrig;
2102 unit->mLevel = level;
2105 void Peak_next_ai_k_nova(Peak *unit, int inNumSamples)
2107 float *out = ZOUT(0);
2108 float *in = ZIN(0);
2109 float level = unit->mLevel;
2110 float inlevel;
2111 inNumSamples = INBUFLENGTH(0);
2112 inlevel = nova::peak_vec_simd(in, &level, inNumSamples);
2113 ZXP(out) = level;
2114 unit->mLevel = level;
2116 #endif
2118 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2120 void RunningMin_Ctor(RunningMin *unit)
2122 if (INRATE(1) == calc_FullRate) {
2123 SETCALC(RunningMin_next_aa);
2124 } else if (INRATE(1) == calc_ScalarRate) {
2125 SETCALC(RunningMin_next_ai);
2126 } else {
2127 SETCALC(RunningMin_next_ak);
2129 unit->m_prevtrig = 0.f;
2130 ZOUT0(0) = unit->mLevel = ZIN0(0);
2133 void RunningMin_next_ak(RunningMin *unit, int inNumSamples)
2135 float *out = ZOUT(0);
2136 float *in = ZIN(0);
2137 float curtrig = ZIN0(1);
2138 float level = unit->mLevel;
2139 float inlevel;
2140 LOOP1(inNumSamples,
2141 inlevel = ZXP(in);
2142 if (inlevel < level) level = inlevel;
2143 ZXP(out) = level;
2145 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2146 unit->m_prevtrig = curtrig;
2147 unit->mLevel = level;
2150 void RunningMin_next_ai(RunningMin *unit, int inNumSamples)
2152 float *out = ZOUT(0);
2153 float *in = ZIN(0);
2154 float level = unit->mLevel;
2155 float inlevel;
2156 LOOP1(inNumSamples,
2157 inlevel = ZXP(in);
2158 if (inlevel < level) level = inlevel;
2159 ZXP(out) = level;
2161 unit->mLevel = level;
2164 void RunningMin_next_aa(RunningMin *unit, int inNumSamples)
2166 float *out = ZOUT(0);
2167 float *in = ZIN(0);
2168 float *trig = ZIN(1);
2169 float prevtrig = unit->m_prevtrig;
2170 float level = unit->mLevel;
2172 LOOP1(inNumSamples,
2173 float curtrig = ZXP(trig);
2174 float inlevel = ZXP(in);
2175 if (inlevel < level) level = inlevel;
2176 ZXP(out) = level;
2177 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2178 prevtrig = curtrig;
2180 unit->m_prevtrig = prevtrig;
2181 unit->mLevel = level;
2185 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2186 void RunningMax_Ctor(RunningMax *unit)
2188 if (INRATE(1) == calc_FullRate) {
2189 SETCALC(RunningMax_next_aa);
2190 } else if (INRATE(1) == calc_ScalarRate) {
2191 SETCALC(RunningMax_next_ai);
2192 } else {
2193 SETCALC(RunningMax_next_ak);
2195 unit->m_prevtrig = 0.f;
2196 ZOUT0(0) = unit->mLevel = ZIN0(0);
2199 void RunningMax_next_ak(RunningMax *unit, int inNumSamples)
2201 float *out = ZOUT(0);
2202 float *in = ZIN(0);
2203 float curtrig = ZIN0(1);
2204 float level = unit->mLevel;
2205 float inlevel;
2206 LOOP1(inNumSamples,
2207 inlevel = ZXP(in);
2208 if (inlevel > level) level = inlevel;
2209 ZXP(out) = level;
2211 if (unit->m_prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2212 unit->m_prevtrig = curtrig;
2213 unit->mLevel = level;
2216 void RunningMax_next_ai(RunningMax *unit, int inNumSamples)
2218 float *out = ZOUT(0);
2219 float *in = ZIN(0);
2220 float level = unit->mLevel;
2221 float inlevel;
2222 LOOP1(inNumSamples,
2223 inlevel = ZXP(in);
2224 if (inlevel > level) level = inlevel;
2225 ZXP(out) = level;
2227 unit->mLevel = level;
2230 void RunningMax_next_aa(RunningMax *unit, int inNumSamples)
2232 float *out = ZOUT(0);
2233 float *in = ZIN(0);
2234 float *trig = ZIN(1);
2235 float prevtrig = unit->m_prevtrig;
2236 float level = unit->mLevel;
2238 LOOP1(inNumSamples,
2239 float curtrig = ZXP(trig);
2240 float inlevel = ZXP(in);
2241 if (inlevel > level) level = inlevel;
2242 ZXP(out) = level;
2243 if (prevtrig <= 0.f && curtrig > 0.f) level = inlevel;
2244 prevtrig = curtrig;
2246 unit->m_prevtrig = prevtrig;
2247 unit->mLevel = level;
2251 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2254 void PeakFollower_Ctor(PeakFollower *unit)
2257 if (INRATE(1) == calc_ScalarRate) {
2258 SETCALC(PeakFollower_next_ai);
2259 } else {
2260 SETCALC(PeakFollower_next);
2263 unit->mDecay = ZIN0(1);
2264 ZOUT0(0) = unit->mLevel = ZIN0(0);
2267 void PeakFollower_next(PeakFollower *unit, int inNumSamples)
2269 float *out = ZOUT(0);
2270 float *in = ZIN(0);
2271 float decay = ZIN0(1);
2272 float level = unit->mLevel;
2275 if(decay == unit->mDecay) {
2276 LOOP1(inNumSamples,
2277 float inlevel = std::abs(ZXP(in));
2278 if (inlevel >= level) {
2279 level = inlevel;
2280 } else {
2281 level = inlevel + decay * (level - inlevel);
2283 ZXP(out) = level;
2286 } else {
2288 float decay_slope = CALCSLOPE(decay, unit->mDecay);
2289 if (decay >= 0.f && unit->mDecay >= 0.f) {
2290 LOOP1(inNumSamples,
2291 float inlevel = std::abs(ZXP(in));
2292 if (inlevel >= level) {
2293 level = inlevel;
2294 } else {
2295 level = inlevel + decay * (level - inlevel);
2296 decay += decay_slope;
2298 ZXP(out) = level;
2300 } else if (decay <= 0.f && unit->mDecay <= 0.f) {
2301 LOOP1(inNumSamples,
2302 float inlevel = std::abs(ZXP(in));
2303 if (inlevel >= level) {
2304 level = inlevel;
2305 } else {
2306 level = inlevel + decay * (level + inlevel);
2307 decay += decay_slope;
2309 ZXP(out) = level;
2311 } else {
2312 LOOP1(inNumSamples,
2313 float inlevel = std::abs(ZXP(in));
2314 if (inlevel >= level) {
2315 level = inlevel;
2316 } else {
2317 level = (1.f - std::abs(decay)) * inlevel + decay * level;
2318 decay += decay_slope;
2320 ZXP(out) = level;
2325 unit->mLevel = level;
2326 unit->mDecay = decay;
2330 void PeakFollower_next_ai(PeakFollower *unit, int inNumSamples)
2332 float *out = ZOUT(0);
2333 float *in = ZIN(0);
2334 float decay = ZIN0(1);
2335 float level = unit->mLevel;
2337 LOOP1(inNumSamples,
2338 float inlevel = std::abs(ZXP(in));
2339 if (inlevel >= level) {
2340 level = inlevel;
2341 } else {
2342 level = inlevel + decay * (level - inlevel);
2344 ZXP(out) = level;
2347 unit->mLevel = level;
2351 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2354 void MostChange_Ctor(MostChange *unit)
2356 if (INRATE(0) == calc_FullRate) {
2357 if (INRATE(1) == calc_FullRate) {
2358 SETCALC(MostChange_next_aa);
2359 } else {
2360 SETCALC(MostChange_next_ak);
2362 } else {
2363 if (INRATE(1) == calc_FullRate) {
2364 SETCALC(MostChange_next_ka);
2365 } else {
2366 SETCALC(MostChange_next_aa);
2369 unit->mPrevA = 0.f;
2370 unit->mPrevB = 0.f;
2371 unit->mRecent = 1;
2372 MostChange_next_aa(unit, 1);
2375 void MostChange_next_ak(MostChange *unit, int inNumSamples)
2377 float *out = ZOUT(0);
2378 float *a = ZIN(0);
2379 float xb = ZIN0(1);
2380 float prevA = unit->mPrevA;
2381 float prevB = unit->mPrevB;
2382 int recent = unit->mRecent;
2383 LOOP1(inNumSamples,
2384 float xa = ZXP(a);
2385 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2386 if (diff > 0.f) {
2387 recent = 0;
2388 ZXP(out) = xa;
2389 } else if (diff < 0.f) {
2390 recent = 1;
2391 ZXP(out) = xb;
2392 } else {
2393 ZXP(out) = recent ? xb : xa;
2395 prevA = xa;
2396 prevB = xb;
2398 unit->mPrevA = prevA;
2399 unit->mPrevB = prevB;
2400 unit->mRecent = recent;
2403 void MostChange_next_aa(MostChange *unit, int inNumSamples)
2405 float *out = ZOUT(0);
2406 float *a = ZIN(0);
2407 float *b = ZIN(1);
2408 float prevA = unit->mPrevA;
2409 float prevB = unit->mPrevB;
2410 int recent = unit->mRecent;
2411 LOOP1(inNumSamples,
2412 float xa = ZXP(a);
2413 float xb = ZXP(b);
2414 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2415 if (diff > 0.f) {
2416 recent = 0;
2417 ZXP(out) = xa;
2418 } else if (diff < 0.f) {
2419 recent = 1;
2420 ZXP(out) = xb;
2421 } else {
2422 ZXP(out) = recent ? xb : xa;
2424 prevA = xa;
2425 prevB = xb;
2427 unit->mPrevA = prevA;
2428 unit->mPrevB = prevB;
2429 unit->mRecent = recent;
2432 void MostChange_next_ka(MostChange *unit, int inNumSamples)
2434 float *out = ZOUT(0);
2435 float xa = ZIN0(0);
2436 float *b = ZIN(1);
2437 float prevA = unit->mPrevA;
2438 float prevB = unit->mPrevB;
2439 int recent = unit->mRecent;
2440 LOOP1(inNumSamples,
2441 float xb = ZXP(b);
2442 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2443 if (diff > 0.f) {
2444 recent = 0;
2445 ZXP(out) = xa;
2446 } else if (diff < 0.f) {
2447 recent = 1;
2448 ZXP(out) = xb;
2449 } else {
2450 ZXP(out) = recent ? xb : xa;
2452 prevA = xa;
2453 prevB = xb;
2455 unit->mPrevA = prevA;
2456 unit->mPrevB = prevB;
2457 unit->mRecent = recent;
2461 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2463 void LeastChange_Ctor(LeastChange *unit)
2465 if (INRATE(0) == calc_FullRate) {
2466 if (INRATE(1) == calc_FullRate) {
2467 SETCALC(LeastChange_next_aa);
2468 } else {
2469 SETCALC(LeastChange_next_ak);
2471 } else {
2472 if (INRATE(1) == calc_FullRate) {
2473 SETCALC(LeastChange_next_ka);
2474 } else {
2475 SETCALC(LeastChange_next_aa);
2478 unit->mPrevA = 0.f;
2479 unit->mPrevB = 0.f;
2480 unit->mRecent = 0;
2481 LeastChange_next_aa(unit, 1);
2484 void LeastChange_next_ak(LeastChange *unit, int inNumSamples)
2486 float *out = ZOUT(0);
2487 float *a = ZIN(0);
2488 float xb = ZIN0(1);
2489 float prevA = unit->mPrevA;
2490 float prevB = unit->mPrevB;
2491 int recent = unit->mRecent;
2492 LOOP1(inNumSamples,
2493 float xa = ZXP(a);
2494 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2495 if (diff < 0.f) {
2496 recent = 0;
2497 ZXP(out) = xa;
2498 } else if (diff > 0.f) {
2499 recent = 1;
2500 ZXP(out) = xb;
2501 } else {
2502 ZXP(out) = recent ? xb : xa;
2504 prevA = xa;
2505 prevB = xb;
2507 unit->mPrevA = prevA;
2508 unit->mPrevB = prevB;
2509 unit->mRecent = recent;
2512 void LeastChange_next_aa(LeastChange *unit, int inNumSamples)
2514 float *out = ZOUT(0);
2515 float *a = ZIN(0);
2516 float *b = ZIN(1);
2517 float prevA = unit->mPrevA;
2518 float prevB = unit->mPrevB;
2519 int recent = unit->mRecent;
2520 LOOP1(inNumSamples,
2521 float xa = ZXP(a);
2522 float xb = ZXP(b);
2523 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2524 if (diff < 0.f) {
2525 recent = 0;
2526 ZXP(out) = xa;
2527 } else if (diff > 0.f) {
2528 recent = 1;
2529 ZXP(out) = xb;
2530 } else {
2531 ZXP(out) = recent ? xb : xa;
2533 prevA = xa;
2534 prevB = xb;
2536 unit->mPrevA = prevA;
2537 unit->mPrevB = prevB;
2538 unit->mRecent = recent;
2541 void LeastChange_next_ka(LeastChange *unit, int inNumSamples)
2543 float *out = ZOUT(0);
2544 float xa = ZIN0(0);
2545 float *b = ZIN(1);
2546 float prevA = unit->mPrevA;
2547 float prevB = unit->mPrevB;
2548 int recent = unit->mRecent;
2549 LOOP1(inNumSamples,
2550 float xb = ZXP(b);
2551 float diff = std::abs(xa - prevA) - std::abs(xb - prevB);
2552 if (diff < 0.f) {
2553 recent = 0;
2554 ZXP(out) = xa;
2555 } else if (diff > 0.f) {
2556 recent = 1;
2557 ZXP(out) = xb;
2558 } else {
2559 ZXP(out) = recent ? xb : xa;
2561 prevA = xa;
2562 prevB = xb;
2564 unit->mPrevA = prevA;
2565 unit->mPrevB = prevB;
2566 unit->mRecent = recent;
2570 ////////////////////
2572 void LastValue_Ctor(LastValue *unit)
2574 if (INRATE(0) == calc_FullRate) {
2575 SETCALC(LastValue_next_ak);
2576 } else {
2577 SETCALC(LastValue_next_kk);
2580 unit->mPrev = ZIN0(0);
2581 unit->mCurr = ZIN0(0);
2582 LastValue_next_kk(unit, 1);
2585 void LastValue_next_kk(LastValue *unit, int inNumSamples)
2587 float *out = ZOUT(0);
2588 float inval = ZIN0(0);
2589 float delta = ZIN0(1);
2590 float diff = std::abs(inval - unit->mCurr);
2591 if(diff >= delta) {
2592 unit->mPrev = unit->mCurr;
2593 unit->mCurr = inval;
2595 float level = unit->mPrev;
2596 LOOP1(inNumSamples, ZXP(out) = level; );
2600 void LastValue_next_ak(LastValue *unit, int inNumSamples)
2602 float *out = ZOUT(0);
2603 float *in = ZIN(0);
2604 float delta = ZIN0(1);
2605 float prev = unit->mPrev;
2606 float curr = unit->mCurr;
2608 LOOP1(inNumSamples,
2609 float inval = ZXP(in);
2610 float diff = std::abs(inval - curr);
2611 if(diff >= delta) {
2612 prev = curr;
2613 curr = inval;
2615 ZXP(out) = prev
2617 unit->mPrev = prev;
2618 unit->mCurr = curr;
2624 //////////////////////////////////////////////////////////////////////////////////////////
2626 void Done_Ctor(Done *unit)
2628 SETCALC(Done_next);
2630 unit->m_src = unit->mInput[0]->mFromUnit;
2632 Done_next(unit, 1);
2637 void Done_next(Done *unit, int inNumSamples)
2639 float *out = OUT(0);
2640 Unit *src = unit->m_src;
2641 if (src) *out = src->mDone ? 1.f : 0.f;
2642 else *out = 0.f;
2645 //////////////////////////////////////////////////////////////////////////////////////////
2647 void FreeSelf_Ctor(FreeSelf *unit)
2649 SETCALC(FreeSelf_next);
2650 unit->m_prevtrig = 0.f;
2651 FreeSelf_next(unit, 1);
2655 void FreeSelf_next(FreeSelf *unit, int inNumSamples)
2657 float in = ZIN0(0);
2658 if (in > 0.f && unit->m_prevtrig <= 0.f) {
2659 NodeEnd(&unit->mParent->mNode);
2661 unit->m_prevtrig = in;
2664 //////////////////////////////////////////////////////////////////////////////////////////
2666 void PauseSelf_Ctor(PauseSelf *unit)
2668 SETCALC(PauseSelf_next);
2669 unit->m_prevtrig = 0.f;
2670 PauseSelf_next(unit, 1);
2674 void PauseSelf_next(PauseSelf *unit, int inNumSamples)
2676 float in = ZIN0(0);
2677 if (in > 0.f && unit->m_prevtrig <= 0.f) {
2678 NodeRun(&unit->mParent->mNode, 0);
2680 unit->m_prevtrig = in;
2683 //////////////////////////////////////////////////////////////////////////////////////////
2685 void Pause_Ctor(Pause *unit)
2687 SETCALC(Pause_next);
2689 unit->m_state = 1;
2691 ZOUT0(0) = ZIN0(0);
2695 void Pause_next(Pause *unit, int inNumSamples)
2697 float in = ZIN0(0);
2698 int state = in == 0.f ? 0 : 1;
2699 if (state != unit->m_state) {
2700 unit->m_state = state;
2701 int id = (int)ZIN0(1);
2702 Node *node = SC_GetNode(unit->mWorld, id);
2703 if (node) {
2704 NodeRun(node, state);
2707 ZOUT0(0) = in;
2710 //////////////////////////////////////////////////////////////////////////////////////////
2712 void Free_Ctor(Free *unit)
2714 SETCALC(Free_next);
2716 unit->m_prevtrig = 0.f;
2718 ZOUT0(0) = ZIN0(0);
2722 void Free_next(Free *unit, int inNumSamples)
2724 float trig = ZIN0(0);
2725 if (trig > 0.f && unit->m_prevtrig <= 0) {
2726 int id = (int)ZIN0(1);
2727 Node *node = SC_GetNode(unit->mWorld, id);
2728 if (node) {
2729 NodeEnd(node);
2732 unit->m_prevtrig = trig;
2733 ZOUT0(0) = trig;
2736 //////////////////////////////////////////////////////////////////////////////////////////
2738 void FreeSelfWhenDone_Ctor(FreeSelfWhenDone *unit)
2740 unit->m_src = unit->mInput[0]->mFromUnit;
2742 if (unit->m_src) {
2743 SETCALC(FreeSelfWhenDone_next);
2744 FreeSelfWhenDone_next(unit, 1);
2745 } else {
2746 SETCALC(ClearUnitOutputs);
2747 ClearUnitOutputs(unit, 1);
2752 void FreeSelfWhenDone_next(FreeSelfWhenDone *unit, int inNumSamples)
2754 float *out = OUT(0);
2755 float *in = IN(0);
2756 Unit *src = unit->m_src;
2757 if (src->mDone) {
2758 NodeEnd(&unit->mParent->mNode);
2759 SETCALC(ClearUnitOutputs);
2761 *out = *in;
2764 //////////////////////////////////////////////////////////////////////////////////////////
2766 void PauseSelfWhenDone_Ctor(PauseSelfWhenDone *unit)
2768 unit->m_src = unit->mInput[0]->mFromUnit;
2770 if (unit->m_src) {
2771 SETCALC(PauseSelfWhenDone_next);
2772 PauseSelfWhenDone_next(unit, 1);
2773 } else {
2774 SETCALC(ClearUnitOutputs);
2775 ClearUnitOutputs(unit, 1);
2780 void PauseSelfWhenDone_next(PauseSelfWhenDone *unit, int inNumSamples)
2782 float *out = OUT(0);
2783 float *in = IN(0);
2784 Unit *src = unit->m_src;
2785 if (src->mDone) {
2786 NodeRun(&unit->mParent->mNode, 0);
2787 SETCALC(ClearUnitOutputs);
2789 *out = *in;
2792 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2794 struct SendPeakRMS:
2795 public Unit
2797 // rate, level lag, replyid, channel count, [channels, ], cmd name size, [cmdname, ]
2798 static const int rateIndex = 0;
2799 static const int levelLagIndex = 1;
2800 static const int replyIdIndex = 2;
2801 static const int channelCountIndex = 3;
2802 static const int signalStartIndex = 4;
2804 SendPeakRMS(void)
2806 SendPeakRMS * unit = this;
2808 mChannelCount = (unsigned int)IN0(channelCountIndex);
2809 size_t channelDataAllocSize = mChannelCount * 3 * sizeof(float);
2811 int cmdSizeIndex = signalStartIndex + mChannelCount;
2812 size_t cmdNameSize = IN0(cmdSizeIndex);
2813 size_t cmdNameAllocSize = (cmdNameSize + 1) * sizeof(char);
2815 void * allocData = RTAlloc(unit->mWorld, channelDataAllocSize + cmdNameAllocSize);
2817 memset(allocData, 0, channelDataAllocSize);
2818 mChannelData = (float*)allocData;
2820 char * cmdName = (char*)(allocData) + channelDataAllocSize;
2822 size_t cmdNameIndex = cmdSizeIndex + 1;
2823 for(int i = 0; i < cmdNameSize; i++)
2824 cmdName[i] = (char)IN0(cmdNameIndex + i);
2825 cmdName[cmdNameSize] = 0;
2827 if ((FULLBUFLENGTH & 15) == 0) {
2828 if (mCalcRate == calc_FullRate)
2829 SETCALC(SendPeakRMS::perform_a<true>);
2830 else
2831 SETCALC(SendPeakRMS::perform_k<true>);
2832 } else {
2833 if (mCalcRate == calc_FullRate)
2834 SETCALC(SendPeakRMS::perform_a<false>);
2835 else
2836 SETCALC(SendPeakRMS::perform_k<false>);
2839 float replyRate = IN0(rateIndex);
2841 mAudioSamplesPerTick = FULLRATE / replyRate;
2842 mControlSamplesPerTick = BUFRATE / replyRate;
2844 mPhaseRemain = (mCalcRate == calc_FullRate) ? mAudioSamplesPerTick
2845 : mControlSamplesPerTick;
2847 float32 lag = ZIN0(levelLagIndex);
2848 mB1 = (lag != 0.f) ? exp(log001 / (lag * replyRate))
2849 : 0.f;
2852 ~SendPeakRMS (void)
2854 SendPeakRMS * unit = this;
2855 RTFree(unit->mWorld, mChannelData);
2858 unsigned int mChannelCount;
2859 float * mChannelData;
2861 float mB1;
2862 int mAudioSamplesPerTick;
2863 int mControlSamplesPerTick;
2864 int mPhaseRemain;
2866 void performLevelLag(float & out, float y0, float & y1)
2868 if (y0 >= y1)
2869 out = y1 = y0;
2870 else
2871 out = y1 = y0 + mB1 * (y1 - y0);
2874 char * getCmdName (void)
2876 void * buffer = mChannelData;
2877 return (char*)(buffer) + mChannelCount * 3 * sizeof(float);
2880 void sendReply(void)
2882 SendPeakRMS * unit = this;
2883 float * reply = (float*)alloca(mChannelCount * 2 * sizeof(float));
2886 for (int i = 0; i != mChannelCount; ++i) {
2887 float & maxLevel = reply[2*i];
2888 float & rms = reply[2*i + 1];
2890 performLevelLag(maxLevel, mChannelData[2*i], mChannelData[2*mChannelCount + i]);
2892 if (INRATE(signalStartIndex + i) == calc_FullRate)
2893 rms = std::sqrt(mChannelData[2*i + 1] / (float)mAudioSamplesPerTick);
2894 else
2895 rms = std::sqrt(mChannelData[2*i + 1] / (float)mControlSamplesPerTick);
2898 SendNodeReply(&unit->mParent->mNode, (int)ZIN0(replyIdIndex), getCmdName(), mChannelCount*2, reply);
2899 memset(mChannelData, 0, mChannelCount * 2 * sizeof(float));
2902 template <bool simd>
2903 void analyzeFullBlock(void)
2905 SendPeakRMS * unit = this;
2906 for (int i = 0; i != mChannelCount; ++i) {
2907 float * in = IN(signalStartIndex + i);
2908 int numSamples = INBUFLENGTH(signalStartIndex + i);
2910 float & level = mChannelData[2*i];
2911 float & sqrsum = mChannelData[2*i + 1];
2912 if (numSamples == 1)
2913 nova::peak_rms_vec(in, &level, &sqrsum, 1);
2914 else {
2915 if (simd)
2916 nova::peak_rms_vec_simd(in, &level, &sqrsum, numSamples);
2917 else
2918 nova::peak_rms_vec(in, &level, &sqrsum, numSamples);
2923 void analyzePartialBlock(int firstSample, int samplesToAnalyze)
2925 SendPeakRMS * unit = this;
2926 for (int i = 0; i != mChannelCount; ++i) {
2927 float * in = IN(signalStartIndex + i) + firstSample;
2928 int numSamples = INBUFLENGTH(signalStartIndex + i);
2930 float & level = mChannelData[2*i];
2931 float & sqrsum = mChannelData[2*i + 1];
2932 if (numSamples == 1) {
2933 if (firstSample == 0)
2934 nova::peak_rms_vec(in, &level, &sqrsum, 1);
2935 } else {
2936 if (!(samplesToAnalyze & 15) && !(firstSample & 3)) // check for unrolling and alignment
2937 nova::peak_rms_vec_simd(in, &level, &sqrsum, samplesToAnalyze);
2938 else
2939 nova::peak_rms_vec(in, &level, &sqrsum, samplesToAnalyze);
2944 template <bool simd>
2945 inline void next_k(int inNumSamples)
2947 mPhaseRemain -= 1;
2949 if (mPhaseRemain <= 0) {
2950 mPhaseRemain += mControlSamplesPerTick;
2951 sendReply();
2954 analyzeFullBlock<simd>();
2957 template <bool simd>
2958 inline void next_a(int inNumSamples)
2960 if (mPhaseRemain >= inNumSamples) {
2961 mPhaseRemain -= inNumSamples;
2962 analyzeFullBlock<simd>();
2963 } else {
2964 if (mPhaseRemain == 0) {
2965 sendReply();
2966 mPhaseRemain = mAudioSamplesPerTick;
2969 int startSample = 0;
2970 int samplesToAnalyze = std::min(mPhaseRemain, inNumSamples);
2971 int remain = inNumSamples;
2973 do {
2974 analyzePartialBlock(startSample, samplesToAnalyze);
2976 startSample += samplesToAnalyze;
2977 mPhaseRemain -= samplesToAnalyze;
2978 if (mPhaseRemain == 0) {
2979 sendReply();
2980 mPhaseRemain = mAudioSamplesPerTick;
2983 remain -= samplesToAnalyze;
2984 samplesToAnalyze = std::min(remain, mPhaseRemain);
2985 } while(remain);
2989 template <bool simd>
2990 static void perform_k(Unit * unit, int inNumSamples)
2992 static_cast<SendPeakRMS*>(unit)->next_k<simd>(inNumSamples);
2995 template <bool simd>
2996 static void perform_a(Unit * unit, int inNumSamples)
2998 static_cast<SendPeakRMS*>(unit)->next_a<simd>(inNumSamples);
3002 static void SendPeakRMS_Ctor(SendPeakRMS * unit) { new(unit) SendPeakRMS (); }
3003 static void SendPeakRMS_Dtor(SendPeakRMS * unit) { unit->~SendPeakRMS (); }
3005 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3007 PluginLoad(Trigger)
3009 ft = inTable;
3011 DefineSimpleUnit(Trig1);
3012 DefineSimpleUnit(Trig);
3013 DefineSimpleUnit(SendTrig);
3014 DefineDtorUnit(SendReply);
3015 DefineDtorUnit(Poll);
3016 DefineSimpleUnit(ToggleFF);
3017 DefineSimpleUnit(SetResetFF);
3018 DefineSimpleUnit(Latch);
3019 DefineSimpleUnit(Gate);
3020 DefineSimpleUnit(Schmidt);
3021 DefineSimpleUnit(PulseDivider);
3022 DefineSimpleUnit(PulseCount);
3023 DefineSimpleUnit(Stepper);
3024 DefineSimpleUnit(TDelay);
3025 DefineSimpleUnit(ZeroCrossing);
3026 DefineSimpleUnit(Timer);
3027 DefineSimpleUnit(Sweep);
3028 DefineSimpleUnit(Phasor);
3029 DefineSimpleUnit(Peak);
3030 DefineSimpleUnit(RunningMin);
3031 DefineSimpleUnit(RunningMax);
3032 DefineSimpleUnit(PeakFollower);
3033 DefineSimpleUnit(MostChange);
3034 DefineSimpleUnit(LeastChange);
3035 DefineSimpleUnit(LastValue);
3036 DefineSimpleUnit(Done);
3037 DefineSimpleUnit(Pause);
3038 DefineSimpleUnit(FreeSelf);
3039 DefineSimpleUnit(PauseSelf);
3040 DefineSimpleUnit(Free);
3041 DefineSimpleUnit(FreeSelfWhenDone);
3042 DefineSimpleUnit(PauseSelfWhenDone);
3044 DefineDtorUnit(SendPeakRMS);