class library: SynthDef - replaceUGen fixes
[supercollider.git] / server / plugins / NoiseUGens.cpp
blob83275d39987d0b4d18616a76a74726108b967d21
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 static InterfaceTable *ft;
26 struct WhiteNoise : public Unit
30 struct ClipNoise : public Unit
34 struct BrownNoise : public Unit
36 float mLevel;
39 struct PinkNoise : public Unit
41 uint32 mDice[16];
42 int32 mTotal;
45 struct Dust : public Unit
47 float m_density, m_thresh, m_scale;
50 struct Dust2 : public Unit
52 float m_density, m_thresh, m_scale;
55 struct GrayNoise : public Unit
57 int32 mCounter;
60 struct Crackle : public Unit
62 double m_y1, m_y2;
65 struct Logistic : public Unit
67 double m_y1;
68 int mCounter;
71 struct Hasher : public Unit
75 struct MantissaMask : public Unit
79 struct IRand : public Unit
83 struct Rand : public Unit
87 struct TRand : public Unit
89 float m_trig, m_value;
92 struct TIRand : public Unit
94 float m_trig, m_value;
97 struct TExpRand : public Unit
99 float m_trig, m_value;
102 struct NRand : public Unit
106 struct LinRand : public Unit
110 struct ExpRand : public Unit
114 struct CoinGate : public Unit
116 float m_trig;
119 struct LFClipNoise : public Unit
121 float mLevel;
122 int mCounter;
125 struct LFNoise0 : public Unit
127 float mLevel;
128 int mCounter;
131 struct LFNoise1 : public Unit
133 float mLevel, mSlope;
134 int mCounter;
137 struct LFNoise2 : public Unit
139 float mLevel, mSlope, mCurve;
140 float m_nextvalue, m_nextmidpt;
141 int mCounter;
144 struct RandSeed : public Unit
146 float m_trig;
149 struct RandID : public Unit
151 float m_id;
154 //////////////////////////////////////////////////////////////////////////////////////////////////
157 extern "C"
159 void WhiteNoise_next(WhiteNoise *unit, int inNumSamples);
160 void WhiteNoise_Ctor(WhiteNoise* unit);
162 void GrayNoise_next(GrayNoise *unit, int inNumSamples);
163 void GrayNoise_Ctor(GrayNoise* unit);
165 void ClipNoise_next(ClipNoise *unit, int inNumSamples);
166 void ClipNoise_Ctor(ClipNoise* unit);
168 void PinkNoise_next(PinkNoise *unit, int inNumSamples);
169 void PinkNoise_Ctor(PinkNoise* unit);
171 void BrownNoise_next(BrownNoise *unit, int inNumSamples);
172 void BrownNoise_Ctor(BrownNoise* unit);
174 void Dust_next(Dust *unit, int inNumSamples);
175 void Dust_Ctor(Dust *unit);
177 void Dust2_next(Dust2 *unit, int inNumSamples);
178 void Dust2_Ctor(Dust2 *unit);
180 void Crackle_next(Crackle *unit, int inNumSamples);
181 void Crackle_Ctor(Crackle *unit);
183 void Hasher_next(Hasher *unit, int inNumSamples);
184 void Hasher_Ctor(Hasher *unit);
186 void MantissaMask_next(MantissaMask *unit, int inNumSamples);
187 void MantissaMask_Ctor(MantissaMask *unit);
189 void IRand_Ctor(IRand *unit);
190 void Rand_Ctor(Rand *unit);
191 void LinRand_Ctor(LinRand* unit);
192 void NRand_Ctor(NRand* unit);
193 void ExpRand_Ctor(ExpRand *unit);
195 void CoinGate_Ctor(CoinGate *unit);
196 void CoinGate_next_k(CoinGate *unit, int inNumSamples);
197 void CoinGate_next(CoinGate *unit, int inNumSamples);
199 void TIRand_next_a(TIRand *unit, int inNumSamples);
200 void TIRand_next_k(TIRand *unit, int inNumSamples);
201 void TIRand_Ctor(TIRand *unit);
203 void TRand_next_a(TRand *unit, int inNumSamples);
204 void TRand_next_k(TRand *unit, int inNumSamples);
205 void TRand_Ctor(TRand *unit);
207 void TExpRand_next_a(TExpRand *unit, int inNumSamples);
208 void TExpRand_next_k(TExpRand *unit, int inNumSamples);
209 void TExpRand_Ctor(TExpRand *unit);
211 void Logistic_next_1(Logistic *unit, int inNumSamples);
212 void Logistic_next_k(Logistic *unit, int inNumSamples);
213 void Logistic_Ctor(Logistic *unit);
215 void LFClipNoise_next(LFClipNoise *unit, int inNumSamples);
216 void LFClipNoise_Ctor(LFClipNoise *unit);
218 void LFNoise0_next(LFNoise0 *unit, int inNumSamples);
219 void LFNoise0_Ctor(LFNoise0 *unit);
221 void LFNoise1_next(LFNoise1 *unit, int inNumSamples);
222 void LFNoise1_Ctor(LFNoise1 *unit);
224 void LFNoise2_next(LFNoise2 *unit, int inNumSamples);
225 void LFNoise2_Ctor(LFNoise2 *unit);
227 void RandSeed_next(RandSeed *unit, int inNumSamples);
228 void RandSeed_next_k(RandSeed *unit, int inNumSamples);
229 void RandSeed_Ctor(RandSeed *unit);
231 void RandID_next(RandID *unit, int inNumSamples);
232 void RandID_Ctor(RandID *unit);
235 //////////////////////////////////////////////////////////////////////////////////////////////////
237 void ClipNoise_next(ClipNoise *unit, int inNumSamples)
239 float *out = ZOUT(0);
241 RGET
242 LOOP1(inNumSamples,
243 ZXP(out) = fcoin(s1, s2, s3);
245 RPUT
248 void ClipNoise_Ctor(ClipNoise* unit)
250 SETCALC(ClipNoise_next);
252 ClipNoise_next(unit, 1);
255 //////////////////////////////////////////////////////////////////////////////////////////////////
257 void GrayNoise_next(GrayNoise *unit, int inNumSamples)
259 float *out = ZOUT(0);
261 RGET
262 int counter = unit->mCounter;
264 LOOP1(inNumSamples,
265 counter ^= 1L << (trand(s1,s2,s3) & 31);
266 ZXP(out) = counter * 4.65661287308e-10f;
268 unit->mCounter = counter;
269 RPUT
272 void GrayNoise_Ctor(GrayNoise* unit)
274 SETCALC(GrayNoise_next);
275 unit->mCounter = 0;
277 GrayNoise_next(unit, 1);
280 //////////////////////////////////////////////////////////////////////////////////////////////////
282 void WhiteNoise_next(WhiteNoise *unit, int inNumSamples)
284 float *out = ZOUT(0);
286 RGET
287 LOOP1(inNumSamples,
288 ZXP(out) = frand2(s1, s2, s3);
290 RPUT
293 void WhiteNoise_Ctor(WhiteNoise* unit)
295 SETCALC(WhiteNoise_next);
297 WhiteNoise_next(unit, 1);
301 //////////////////////////////////////////////////////////////////////////////////////////////////
303 void PinkNoise_next(PinkNoise *unit, int inNumSamples)
305 float *out = ZOUT(0);
307 RGET
309 uint32 total = unit->mTotal;
310 uint32 *dice = unit->mDice;
311 LOOP1(inNumSamples,
312 uint32 counter = trand(s1,s2,s3); // Magnus Jonsson's suggestion.
313 uint32 newrand = counter >> 13;
314 int k = (CTZ(counter)) & 15;
315 uint32 prevrand = dice[k];
316 dice[k] = newrand;
317 total += (newrand - prevrand);
318 newrand = trand(s1,s2,s3) >> 13;
319 elem32 val; // ensure write before read <sk>
320 val.u = (total + newrand) | 0x40000000;
321 ZXP(out) = val.f - 3.0f;
322 counter ++;
324 unit->mTotal = total;
325 RPUT
328 void PinkNoise_Ctor(PinkNoise* unit)
330 SETCALC(PinkNoise_next);
332 RGET
333 uint32 *dice = unit->mDice;
334 int32 total = 0;
335 for (int i=0; i<16; ++i) {
336 uint32 newrand = trand(s1,s2,s3) >> 13;
337 total += newrand;
338 dice[i] = newrand;
340 unit->mTotal = total;
342 RPUT
344 PinkNoise_next(unit, 1);
347 ////////////////////////////////////////////////////////////////////////////////////////////////////////
349 void BrownNoise_next(BrownNoise *unit, int inNumSamples)
351 float *out = ZOUT(0);
352 RGET
354 float z = unit->mLevel;
355 LOOP1(inNumSamples,
356 z += frand8(s1, s2, s3);
357 if (z > 1.f) z = 2.f - z;
358 else if (z < -1.f) z = -2.f - z;
359 ZXP(out) = z;
361 unit->mLevel = z;
362 RPUT
365 void BrownNoise_Ctor(BrownNoise* unit)
367 SETCALC(BrownNoise_next);
369 unit->mLevel = unit->mParent->mRGen->frand2();
371 ZOUT0(0) = unit->mLevel;
374 ////////////////////////////////////////////////////////////////////////////////////////////////////////
376 void Dust_Ctor(Dust *unit)
378 SETCALC(Dust_next);
380 unit->m_density = 0.f;
381 unit->m_scale = 0.f;
382 unit->m_thresh = 0.f;
383 Dust_next(unit, 1);
386 void Dust_next(Dust *unit, int inNumSamples)
388 float *out = ZOUT(0);
389 float density = ZIN0(0);
390 float thresh, scale;
392 RGET
394 if (density != unit->m_density) {
395 thresh = unit->m_thresh = density * unit->mRate->mSampleDur;
396 scale = unit->m_scale = thresh > 0.f ? 1.f / thresh : 0.f;
397 unit->m_density = density;
398 } else {
399 thresh = unit->m_thresh;
400 scale = unit->m_scale;
403 LOOP1(inNumSamples,
404 float z = frand(s1,s2,s3);
405 if (z < thresh) ZXP(out) = z * scale;
406 else ZXP(out) = 0.f;
409 RPUT
412 ////////////////////////////////////////////////////////////////////////////////////////////////////////
414 void Dust2_Ctor(Dust2 *unit)
416 SETCALC(Dust2_next);
418 unit->m_density = 0.f;
419 unit->m_scale = 0.f;
420 unit->m_thresh = 0.f;
421 Dust2_next(unit, 1);
424 void Dust2_next(Dust2 *unit, int inNumSamples)
426 float *out = ZOUT(0);
427 float density = ZIN0(0);
428 float thresh, scale;
430 RGET
432 if (density != unit->m_density) {
433 thresh = unit->m_thresh = density * unit->mRate->mSampleDur;
434 scale = unit->m_scale = thresh > 0.f ? 2.f / thresh : 0.f;
435 unit->m_density = density;
436 } else {
437 thresh = unit->m_thresh;
438 scale = unit->m_scale;
441 LOOP1(inNumSamples,
442 float z = frand(s1,s2,s3);
443 if (z < thresh) ZXP(out) = z * scale - 1.f;
444 else ZXP(out) = 0.f;
447 RPUT
450 ////////////////////////////////////////////////////////////////////////////////////////////////////////
452 void Crackle_next(Crackle *unit, int inNumSamples)
454 float *out = ZOUT(0);
455 float paramf = ZIN0(0);
456 float y1 = unit->m_y1;
457 float y2 = unit->m_y2;
458 float y0;
460 LOOP1(inNumSamples,
461 ZXP(out) = y0 = fabs(y1 * paramf - y2 - 0.05f);
462 y2 = y1; y1 = y0;
464 unit->m_y1 = y1;
465 unit->m_y2 = y2;
468 void Crackle_Ctor(Crackle* unit)
470 SETCALC(Crackle_next);
472 unit->m_y1 = unit->mParent->mRGen->drand();
473 unit->m_y2 = 0.f;
475 Crackle_next(unit, 1);
478 ////////////////////////////////////////////////////////////////////////////////////////////////////////
480 void Logistic_next_1(Logistic *unit, int inNumSamples)
482 float *out = ZOUT(0);
483 double paramf = ZIN0(0);
484 double y1 = unit->m_y1;
486 LOOP1(inNumSamples,
487 ZXP(out) = y1 = paramf * y1 * (1.0 - y1); // chaotic equation
489 unit->m_y1 = y1;
492 void Logistic_next_k(Logistic *unit, int inNumSamples)
494 float *out = ZOUT(0);
495 double paramf = ZIN0(0);
496 float freq = ZIN0(1);
497 double y1 = unit->m_y1;
498 int32 counter = unit->mCounter;
500 long remain = inNumSamples;
501 do {
502 if (counter<=0) {
503 counter = (int32)(unit->mRate->mSampleRate / sc_max(freq, .001f));
504 counter = sc_max(1, counter);
505 y1 = paramf * y1 * (1.0 - y1); // chaotic equation
507 long nsmps = sc_min(counter, remain);
508 counter -= nsmps;
509 remain -= nsmps;
510 LOOP(nsmps, ZXP(out) = y1;);
511 } while (remain);
512 unit->m_y1 = y1;
513 unit->mCounter = counter;
516 void Logistic_Ctor(Logistic* unit)
518 if (INRATE(0) == calc_ScalarRate && ZIN0(1) >= unit->mRate->mSampleRate)
519 SETCALC(Logistic_next_1);
520 else
521 SETCALC(Logistic_next_k);
523 unit->m_y1 = ZIN0(2);
524 unit->mCounter = 0;
526 Logistic_next_1(unit, 1);
529 ////////////////////////////////////////////////////////////////////////////////////////////////////////
531 void Rand_Ctor(Rand* unit)
533 float lo = ZIN0(0);
534 float hi = ZIN0(1);
535 float range = hi - lo;
536 RGen& rgen = *unit->mParent->mRGen;
537 ZOUT0(0) = rgen.frand() * range + lo;
540 ////////////////////////////////////////////////////////////////////////////////////////////////////////
542 void TRand_next_k(TRand* unit, int inNumSamples)
544 float trig = ZIN0(2);
545 if (trig > 0.f && unit->m_trig <= 0.f) {
546 float lo = ZIN0(0);
547 float hi = ZIN0(1);
548 float range = hi - lo;
549 RGen& rgen = *unit->mParent->mRGen;
550 ZOUT0(0) = unit->m_value = rgen.frand() * range + lo;
551 } else {
552 ZOUT0(0) = unit->m_value;
554 unit->m_trig = trig;
557 void TRand_next_a(TRand* unit, int inNumSamples)
559 float *trig = ZIN(2);
560 float prev = unit->m_trig;
561 float *out = ZOUT(0);
562 float outval = unit->m_value;
563 float next;
565 LOOP1(inNumSamples,
566 next = ZXP(trig);
567 if (next > 0.f && prev <= 0.f) {
568 float lo = ZIN0(0);
569 float hi = ZIN0(1);
570 float range = hi - lo;
571 RGen& rgen = *unit->mParent->mRGen;
572 ZXP(out) = outval = rgen.frand() * range + lo;
573 } else {
574 ZXP(out) = outval;
576 prev = next;
579 unit->m_trig = next;
580 unit->m_value = outval;
583 void TRand_Ctor(TRand* unit)
585 float lo = ZIN0(0);
586 float hi = ZIN0(1);
587 float range = hi - lo;
588 RGen& rgen = *unit->mParent->mRGen;
589 ZOUT0(0) = unit->m_value = rgen.frand() * range + lo;
590 if(unit->mCalcRate == calc_FullRate){ SETCALC(TRand_next_a); } else { SETCALC(TRand_next_k); }
591 unit->m_trig = ZIN0(2);
595 ////////////////////////////////////////////////////////////////////////////////////////////////////////
597 void TExpRand_next_k(TExpRand* unit, int inNumSamples)
599 float trig = ZIN0(2);
600 if (trig > 0.f && unit->m_trig <= 0.f) {
601 float lo = ZIN0(0);
602 float hi = ZIN0(1);
603 float ratio = hi / lo;
604 RGen& rgen = *unit->mParent->mRGen;
605 ZOUT0(0) = unit->m_value = pow(ratio, rgen.frand()) * lo;
606 } else {
607 ZOUT0(0) = unit->m_value;
609 unit->m_trig = trig;
612 void TExpRand_next_a(TExpRand* unit, int inNumSamples)
614 float *trig = ZIN(2);
615 float prev = unit->m_trig;
616 float *out = ZOUT(0);
617 float outval = unit->m_value;
618 float next;
620 LOOP1(inNumSamples,
621 next = ZXP(trig);
622 if (next > 0.f && prev <= 0.f) {
623 float lo = ZIN0(0);
624 float hi = ZIN0(1);
625 float ratio = hi / lo;
626 RGen& rgen = *unit->mParent->mRGen;
627 ZXP(out) = outval = pow(ratio, rgen.frand()) * lo;
628 } else {
629 ZXP(out) = outval;
633 unit->m_trig = next;
634 unit->m_value = outval;
637 void TExpRand_Ctor(TExpRand* unit)
639 float lo = ZIN0(0);
640 float hi = ZIN0(1);
641 float ratio = hi / lo;
642 RGen& rgen = *unit->mParent->mRGen;
644 ZOUT0(0) = unit->m_value = pow(ratio, rgen.frand()) * lo;
645 if(unit->mCalcRate == calc_FullRate){ SETCALC(TExpRand_next_a); } else { SETCALC(TExpRand_next_k); }
646 unit->m_trig = ZIN0(2);
650 ////////////////////////////////////////////////////////////////////////////////////////////////////////
652 void IRand_Ctor(IRand* unit)
654 int lo = (int)ZIN0(0);
655 int hi = (int)ZIN0(1);
656 int range = hi - lo + 1;
657 RGen& rgen = *unit->mParent->mRGen;
658 ZOUT0(0) = (float)(rgen.irand(range) + lo);
661 ////////////////////////////////////////////////////////////////////////////////////////////////////////
663 void TIRand_next_k(TIRand* unit, int inNumSamples)
665 float trig = ZIN0(2);
666 if (trig > 0.f && unit->m_trig <= 0.f) {
667 int lo = (int)ZIN0(0);
668 int hi = (int)ZIN0(1);
669 int range = hi - lo + 1;
670 RGen& rgen = *unit->mParent->mRGen;
671 ZOUT0(0) = unit->m_value = (float)(rgen.irand(range) + lo);
672 } else {
673 ZOUT0(0) = unit->m_value;
675 unit->m_trig = trig;
678 void TIRand_next_a(TIRand* unit, int inNumSamples)
680 float *trig = ZIN(2);
681 float prev = unit->m_trig;
682 float *out = ZOUT(0);
683 float outval = unit->m_value;
684 float next;
686 LOOP1(inNumSamples,
687 next = ZXP(trig);
688 if (next > 0.f && prev <= 0.f) {
689 int lo = (int)ZIN0(0);
690 int hi = (int)ZIN0(1);
691 int range = hi - lo + 1;
692 RGen& rgen = *unit->mParent->mRGen;
693 ZXP(out) = outval = (float)(rgen.irand(range) + lo);
694 } else {
695 ZXP(out) = outval;
699 unit->m_trig = next;
700 unit->m_value = outval;
704 void TIRand_Ctor(TIRand* unit)
706 int lo = (int)ZIN0(0);
707 int hi = (int)ZIN0(1);
708 int range = hi - lo + 1;
709 RGen& rgen = *unit->mParent->mRGen;
710 ZOUT0(0) = unit->m_value = (float)(rgen.irand(range) + lo);
711 if(unit->mCalcRate == calc_FullRate){ SETCALC(TIRand_next_a); } else { SETCALC(TIRand_next_k); }
712 unit->m_trig = ZIN0(2);
716 ////////////////////////////////////////////////////////////////////////////////////////////////////////
718 void CoinGate_Ctor(CoinGate* unit)
720 if (unit->mCalcRate == calc_FullRate) {
721 SETCALC(CoinGate_next);
722 } else {
723 SETCALC(CoinGate_next_k);
725 unit->m_trig = ZIN0(1);
728 void CoinGate_next_k(CoinGate* unit, int inNumSamples)
730 float trig = ZIN0(1);
731 float level = 0.f;
732 RGen& rgen = *unit->mParent->mRGen;
733 if (trig > 0.f && unit->m_trig <= 0.f) {
735 if(rgen.frand() < ZIN0(0)) {
736 level = trig;
740 ZOUT0(0) = level;
741 unit->m_trig = trig;
744 void CoinGate_next(CoinGate* unit, int inNumSamples)
746 float *trig = ZIN(1);
747 float *out = ZOUT(0);
748 float prevtrig = unit->m_trig;
749 float probability = ZIN0(0);
750 RGen& rgen = *unit->mParent->mRGen;
751 LOOP1(inNumSamples,
752 float curtrig = ZXP(trig);
753 float level = 0.f;
754 if (prevtrig <= 0.f && curtrig > 0.f) {
755 if(rgen.frand() < probability) {
756 level = curtrig;
757 } else {
758 level = 0.f;
761 prevtrig = curtrig;
762 ZXP(out) = level;
765 unit->m_trig = prevtrig;
768 ////////////////////////////////////////////////////////////////////////////////////////////////////////
770 void RandSeed_Ctor(RandSeed* unit)
772 unit->m_trig = 0.;
773 if(unit->mCalcRate == calc_FullRate){ SETCALC(RandSeed_next); } else { SETCALC(RandSeed_next_k); }
774 RandSeed_next(unit, 1);
777 void RandSeed_next_k(RandSeed* unit, int inNumSamples)
779 float trig = ZIN0(0);
781 if (trig > 0.f && unit->m_trig <= 0.f) {
782 RGen& rgen = *unit->mParent->mRGen;
783 int seed = (int)DEMANDINPUT_A(1, inNumSamples);
784 rgen.init(seed);
786 unit->m_trig = trig;
787 ZOUT0(0) = 0.f;
790 void RandSeed_next(RandSeed* unit, int inNumSamples)
792 float *trig = ZIN(0);
793 float *out = ZOUT(0);
795 float prevtrig = unit->m_trig;
796 float curtrig;
798 LOOP1(inNumSamples,
800 curtrig = ZXP(trig);
801 if (curtrig > 0.f && prevtrig <= 0.f) {
802 RGen& rgen = *unit->mParent->mRGen;
803 int seed = (int)DEMANDINPUT_A(1, inNumSamples);
804 rgen.init(seed);
806 prevtrig = curtrig;
807 ZXP(out) = 0.f;
811 unit->m_trig = curtrig;
814 ////////////////////////////////////////////////////////////////////////////////////////////////////////
816 void RandID_Ctor(RandID* unit)
818 unit->m_id = -1.;
819 SETCALC(RandID_next);
820 RandID_next(unit, 1);
823 void RandID_next(RandID* unit, int inNumSamples)
825 float id = ZIN0(0);
827 if (id != unit->m_id) {
828 unit->m_id = id;
829 uint32 iid = (uint32)id;
830 if (iid < unit->mWorld->mNumRGens) {
831 unit->mParent->mRGen = unit->mWorld->mRGen + iid;
834 ZOUT0(0) = 0.f;
837 ////////////////////////////////////////////////////////////////////////////////////////////////////////
839 void LinRand_Ctor(LinRand* unit)
841 float lo = ZIN0(0);
842 float hi = ZIN0(1);
843 int n = (int)ZIN0(2);
845 float range = hi - lo;
846 RGen& rgen = *unit->mParent->mRGen;
847 float a, b;
848 a = rgen.frand();
849 b = rgen.frand();
850 if (n <= 0) {
851 ZOUT0(0) = sc_min(a, b) * range + lo;
852 } else {
853 ZOUT0(0) = sc_max(a, b) * range + lo;
857 ////////////////////////////////////////////////////////////////////////////////////////////////////////
859 void NRand_Ctor(NRand* unit)
861 float lo = ZIN0(0);
862 float hi = ZIN0(1);
863 int n = (int)ZIN0(2);
865 float range = hi - lo;
866 RGen& rgen = *unit->mParent->mRGen;
867 float sum = 0;
868 for (int i=0; i<n; ++i) {
869 sum += rgen.frand();
871 ZOUT0(0) = (sum/n) * range + lo;
874 ////////////////////////////////////////////////////////////////////////////////////////////////////////
876 void ExpRand_Ctor(ExpRand* unit)
878 float lo = ZIN0(0);
879 float hi = ZIN0(1);
880 float ratio = hi / lo;
882 ZOUT0(0) = pow(ratio, unit->mParent->mRGen->frand()) * lo;
885 ////////////////////////////////////////////////////////////////////////////////////////////////////////
887 void Hasher_next(Hasher *unit, int inNumSamples)
889 int32 *in = (int32*)ZIN(0);
890 float *out = ZOUT(0);
892 LOOP1(inNumSamples,
893 union { float f; int i; } u;
894 int z = ZXP(in);
895 u.i = 0x40000000 | ((uint32)Hash(z) >> 9);
896 ZXP(out) = u.f - 3.f;
900 void Hasher_Ctor(Hasher* unit)
902 SETCALC(Hasher_next);
904 Hasher_next(unit, 1);
907 ////////////////////////////////////////////////////////////////////////////////////////////////////////
909 void MantissaMask_next(MantissaMask *unit, int inNumSamples)
911 int32 *in = (int32*)ZIN(0);
912 int32 bits = (int32)ZIN0(1);
913 int32 *out = (int32*)ZOUT(0);
914 int32 mask = -1 << (23 - bits);
915 LOOP1(inNumSamples,
916 ZXP(out) = mask & ZXP(in);
920 void MantissaMask_Ctor(MantissaMask* unit)
922 SETCALC(MantissaMask_next);
924 MantissaMask_next(unit, 1);
927 ////////////////////////////////////////////////////////////////////////////////////////////////////////
929 void LFClipNoise_next(LFClipNoise *unit, int inNumSamples)
931 float *out = ZOUT(0);
932 float freq = ZIN0(0);
933 float level = unit->mLevel;
934 int32 counter = unit->mCounter;
935 RGET
937 int remain = inNumSamples;
938 do {
939 if (counter<=0) {
940 counter = (int)(unit->mRate->mSampleRate / sc_max(freq, .001f));
941 counter = sc_max(1, counter);
942 level = fcoin(s1,s2,s3);
944 int nsmps = sc_min(remain, counter);
945 remain -= nsmps;
946 counter -= nsmps;
947 LOOP(nsmps, ZXP(out) = level;);
948 } while (remain);
949 unit->mLevel = level;
950 unit->mCounter = counter;
951 RPUT
954 void LFClipNoise_Ctor(LFClipNoise* unit)
956 SETCALC(LFClipNoise_next);
958 unit->mCounter = 0;
959 unit->mLevel = 0.f;
961 LFClipNoise_next(unit, 1);
964 ////////////////////////////////////////////////////////////////////////////////////////////////////////
966 void LFNoise0_next(LFNoise0 *unit, int inNumSamples)
968 float *out = ZOUT(0);
969 float freq = ZIN0(0);
970 float level = unit->mLevel;
971 int32 counter = unit->mCounter;
972 RGET
974 int remain = inNumSamples;
975 do {
976 if (counter<=0) {
977 counter = (int32)(unit->mRate->mSampleRate / sc_max(freq, .001f));
978 counter = sc_max(1, counter);
979 level = frand2(s1,s2,s3);
981 int nsmps = sc_min(remain, counter);
982 remain -= nsmps;
983 counter -= nsmps;
984 LOOP(nsmps, ZXP(out) = level;);
985 } while (remain);
986 unit->mLevel = level;
987 unit->mCounter = counter;
988 RPUT
991 void LFNoise0_Ctor(LFNoise0* unit)
993 SETCALC(LFNoise0_next);
995 unit->mCounter = 0;
996 unit->mLevel = 0.f;
998 LFNoise0_next(unit, 1);
1001 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1003 void LFNoise1_next(LFNoise1 *unit, int inNumSamples)
1005 float *out = ZOUT(0);
1006 float freq = ZIN0(0);
1007 float level = unit->mLevel;
1008 float slope = unit->mSlope;
1009 int32 counter = unit->mCounter;
1010 RGET
1012 int remain = inNumSamples;
1013 do {
1014 if (counter<=0) {
1015 counter = (int32)(unit->mRate->mSampleRate / sc_max(freq, .001f));
1016 counter = sc_max(1, counter);
1017 float nextlevel = frand2(s1,s2,s3);
1018 slope = (nextlevel - level) / counter;
1020 int nsmps = sc_min(remain, counter);
1021 remain -= nsmps;
1022 counter -= nsmps;
1023 LOOP(nsmps, ZXP(out) = level; level += slope;);
1024 } while (remain);
1025 unit->mLevel = level;
1026 unit->mSlope = slope;
1027 unit->mCounter = counter;
1028 RPUT
1031 void LFNoise1_Ctor(LFNoise1* unit)
1033 SETCALC(LFNoise1_next);
1035 unit->mCounter = 0;
1036 unit->mLevel = unit->mParent->mRGen->frand2();
1037 unit->mSlope = 0.f;
1039 LFNoise1_next(unit, 1);
1042 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1044 void LFNoise2_next(LFNoise2 *unit, int inNumSamples)
1046 float *out = ZOUT(0);
1047 float freq = ZIN0(0);
1048 float level = unit->mLevel;
1049 float slope = unit->mSlope;
1050 float curve = unit->mCurve;
1051 int counter = unit->mCounter;
1052 RGET
1054 int remain = inNumSamples;
1055 do {
1056 if (counter<=0) {
1057 float value = unit->m_nextvalue;
1058 unit->m_nextvalue = frand2(s1,s2,s3);
1059 level = unit->m_nextmidpt;
1060 unit->m_nextmidpt = (unit->m_nextvalue + value) * .5;
1062 counter = (int32)(unit->mRate->mSampleRate / sc_max(freq, .001f));
1063 counter = sc_max(2, counter);
1064 float fseglen = (float)counter;
1065 curve = 2.f * (unit->m_nextmidpt - level - fseglen * slope) / (fseglen * fseglen + fseglen);
1067 int nsmps = sc_min(remain, counter);
1068 remain -= nsmps;
1069 counter -= nsmps;
1070 LOOP(nsmps,
1071 ZXP(out) = level;
1072 slope += curve;
1073 level += slope;
1075 } while (remain);
1076 unit->mLevel = level;
1077 unit->mSlope = slope;
1078 unit->mCurve = curve;
1079 unit->mCounter = counter;
1080 RPUT
1083 void LFNoise2_Ctor(LFNoise2* unit)
1085 SETCALC(LFNoise2_next);
1087 unit->mCounter = 0;
1088 unit->mSlope = 0.f;
1089 unit->mLevel = 0.f;
1090 unit->m_nextvalue = unit->mParent->mRGen->frand2();
1091 unit->m_nextmidpt = unit->m_nextvalue * .5f;
1093 LFNoise2_next(unit, 1);
1096 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1098 void WrapBufRd_next0(Unit *unit, int inNumSamples);
1099 void WrapBufRd_next0(Unit *unit, int inNumSamples)
1101 int bufnum = (int)ZIN0(0);
1102 float *pos = ZIN(1);
1104 const SndBuf *buf = unit->mWorld->mSndBufs + bufnum;
1105 int numchan = buf->channels;
1107 LOCK_SNDBUF_SHARED(buf);
1108 if (numchan != unit->mNumOutputs) {
1109 ClearUnitOutputs(unit, inNumSamples);
1110 return;
1113 const float *data = buf->data;
1114 int numframes = buf->frames;
1116 float *out[16];
1117 for (int i=0; i<numchan; ++i) out[i] = OUT(i);
1119 LOOP1(inNumSamples,
1120 float fpos = ZXP(pos);
1121 int ipos = (int)fpos * numchan;
1122 ipos = sc_mod(ipos, numframes);
1123 int index = numchan * ipos;
1124 for (int i=0; i<numchan; ++i) {
1125 *++(out[i]) = data[index];
1126 index++;
1131 void ClipBufRd_next0(Unit *unit, int inNumSamples);
1132 void ClipBufRd_next0(Unit *unit, int inNumSamples)
1134 int bufnum = (int)ZIN0(0);
1135 float *pos = ZIN(1);
1137 const SndBuf *buf = unit->mWorld->mSndBufs + bufnum;
1138 LOCK_SNDBUF_SHARED(buf);
1139 int numchan = buf->channels;
1141 if (numchan != unit->mNumOutputs) {
1142 ClearUnitOutputs(unit, inNumSamples);
1143 return;
1146 const float *data = buf->data;
1147 int numframes = buf->frames;
1148 int maxframe = numframes - 2;
1150 float *out[16];
1151 for (int i=0; i<numchan; ++i) out[i] = OUT(i);
1153 LOOP1(inNumSamples,
1154 float fpos = ZXP(pos);
1155 int ipos = (int)fpos * numchan;
1156 ipos = sc_clip(ipos, 0, maxframe);
1157 int index = numchan * ipos;
1158 for (int i=0; i<numchan; ++i) {
1159 *++(out[i]) = data[index];
1160 index++;
1165 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1167 PluginLoad(Noise)
1169 ft = inTable;
1171 DefineSimpleUnit(WhiteNoise);
1172 DefineSimpleUnit(GrayNoise);
1173 DefineSimpleUnit(ClipNoise);
1174 DefineSimpleUnit(PinkNoise);
1175 DefineSimpleUnit(BrownNoise);
1176 DefineSimpleUnit(Dust);
1177 DefineSimpleUnit(Dust2);
1178 DefineSimpleUnit(Crackle);
1179 DefineSimpleUnit(Logistic);
1180 DefineSimpleUnit(Hasher);
1181 DefineSimpleUnit(MantissaMask);
1182 DefineSimpleUnit(LFClipNoise);
1183 DefineSimpleUnit(LFNoise0);
1184 DefineSimpleUnit(LFNoise1);
1185 DefineSimpleUnit(LFNoise2);
1186 DefineSimpleUnit(Rand);
1187 DefineSimpleUnit(IRand);
1188 DefineSimpleUnit(TRand);
1189 DefineSimpleUnit(TExpRand);
1190 DefineSimpleUnit(TIRand);
1191 DefineSimpleUnit(NRand);
1192 DefineSimpleUnit(LinRand);
1193 DefineSimpleUnit(ExpRand);
1194 DefineSimpleUnit(CoinGate);
1195 DefineSimpleUnit(RandSeed);
1196 DefineSimpleUnit(RandID);
1199 ////////////////////////////////////////////////////////////////////////////////////////////////////////