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
39 struct PinkNoise
: public Unit
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
60 struct Crackle
: public Unit
65 struct Logistic
: public Unit
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
119 struct LFClipNoise
: public Unit
125 struct LFNoise0
: public Unit
131 struct LFNoise1
: public Unit
133 float mLevel
, mSlope
;
137 struct LFNoise2
: public Unit
139 float mLevel
, mSlope
, mCurve
;
140 float m_nextvalue
, m_nextmidpt
;
144 struct RandSeed
: public Unit
149 struct RandID
: public Unit
154 //////////////////////////////////////////////////////////////////////////////////////////////////
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);
243 ZXP(out
) = fcoin(s1
, s2
, s3
);
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);
262 int counter
= unit
->mCounter
;
265 counter
^= 1L << (trand(s1
,s2
,s3
) & 31);
266 ZXP(out
) = counter
* 4.65661287308e-10f
;
268 unit
->mCounter
= counter
;
272 void GrayNoise_Ctor(GrayNoise
* unit
)
274 SETCALC(GrayNoise_next
);
277 GrayNoise_next(unit
, 1);
280 //////////////////////////////////////////////////////////////////////////////////////////////////
282 void WhiteNoise_next(WhiteNoise
*unit
, int inNumSamples
)
284 float *out
= ZOUT(0);
288 ZXP(out
) = frand2(s1
, s2
, s3
);
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);
309 uint32 total
= unit
->mTotal
;
310 uint32
*dice
= unit
->mDice
;
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
];
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
;
324 unit
->mTotal
= total
;
328 void PinkNoise_Ctor(PinkNoise
* unit
)
330 SETCALC(PinkNoise_next
);
333 uint32
*dice
= unit
->mDice
;
335 for (int i
=0; i
<16; ++i
) {
336 uint32 newrand
= trand(s1
,s2
,s3
) >> 13;
340 unit
->mTotal
= total
;
344 PinkNoise_next(unit
, 1);
347 ////////////////////////////////////////////////////////////////////////////////////////////////////////
349 void BrownNoise_next(BrownNoise
*unit
, int inNumSamples
)
351 float *out
= ZOUT(0);
354 float z
= unit
->mLevel
;
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
;
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
)
380 unit
->m_density
= 0.f
;
382 unit
->m_thresh
= 0.f
;
386 void Dust_next(Dust
*unit
, int inNumSamples
)
388 float *out
= ZOUT(0);
389 float density
= ZIN0(0);
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
;
399 thresh
= unit
->m_thresh
;
400 scale
= unit
->m_scale
;
404 float z
= frand(s1
,s2
,s3
);
405 if (z
< thresh
) ZXP(out
) = z
* scale
;
412 ////////////////////////////////////////////////////////////////////////////////////////////////////////
414 void Dust2_Ctor(Dust2
*unit
)
418 unit
->m_density
= 0.f
;
420 unit
->m_thresh
= 0.f
;
424 void Dust2_next(Dust2
*unit
, int inNumSamples
)
426 float *out
= ZOUT(0);
427 float density
= ZIN0(0);
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
;
437 thresh
= unit
->m_thresh
;
438 scale
= unit
->m_scale
;
442 float z
= frand(s1
,s2
,s3
);
443 if (z
< thresh
) ZXP(out
) = z
* scale
- 1.f
;
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
;
461 ZXP(out
) = y0
= fabs(y1
* paramf
- y2
- 0.05f
);
468 void Crackle_Ctor(Crackle
* unit
)
470 SETCALC(Crackle_next
);
472 unit
->m_y1
= unit
->mParent
->mRGen
->drand();
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
;
487 ZXP(out
) = y1
= paramf
* y1
* (1.0 - y1
); // chaotic equation
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
;
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
);
510 LOOP(nsmps
, ZXP(out
) = 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
);
521 SETCALC(Logistic_next_k
);
523 unit
->m_y1
= ZIN0(2);
526 Logistic_next_1(unit
, 1);
529 ////////////////////////////////////////////////////////////////////////////////////////////////////////
531 void Rand_Ctor(Rand
* unit
)
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
) {
548 float range
= hi
- lo
;
549 RGen
& rgen
= *unit
->mParent
->mRGen
;
550 ZOUT0(0) = unit
->m_value
= rgen
.frand() * range
+ lo
;
552 ZOUT0(0) = unit
->m_value
;
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
;
567 if (next
> 0.f
&& prev
<= 0.f
) {
570 float range
= hi
- lo
;
571 RGen
& rgen
= *unit
->mParent
->mRGen
;
572 ZXP(out
) = outval
= rgen
.frand() * range
+ lo
;
580 unit
->m_value
= outval
;
583 void TRand_Ctor(TRand
* unit
)
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
) {
603 float ratio
= hi
/ lo
;
604 RGen
& rgen
= *unit
->mParent
->mRGen
;
605 ZOUT0(0) = unit
->m_value
= pow(ratio
, rgen
.frand()) * lo
;
607 ZOUT0(0) = unit
->m_value
;
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
;
622 if (next
> 0.f
&& prev
<= 0.f
) {
625 float ratio
= hi
/ lo
;
626 RGen
& rgen
= *unit
->mParent
->mRGen
;
627 ZXP(out
) = outval
= pow(ratio
, rgen
.frand()) * lo
;
634 unit
->m_value
= outval
;
637 void TExpRand_Ctor(TExpRand
* unit
)
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
);
673 ZOUT0(0) = unit
->m_value
;
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
;
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
);
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
);
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);
732 RGen
& rgen
= *unit
->mParent
->mRGen
;
733 if (trig
> 0.f
&& unit
->m_trig
<= 0.f
) {
735 if(rgen
.frand() < ZIN0(0)) {
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
;
752 float curtrig
= ZXP(trig
);
754 if (prevtrig
<= 0.f
&& curtrig
> 0.f
) {
755 if(rgen
.frand() < probability
) {
765 unit
->m_trig
= prevtrig
;
768 ////////////////////////////////////////////////////////////////////////////////////////////////////////
770 void RandSeed_Ctor(RandSeed
* unit
)
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
);
790 void RandSeed_next(RandSeed
* unit
, int inNumSamples
)
792 float *trig
= ZIN(0);
793 float *out
= ZOUT(0);
795 float prevtrig
= unit
->m_trig
;
801 if (curtrig
> 0.f
&& prevtrig
<= 0.f
) {
802 RGen
& rgen
= *unit
->mParent
->mRGen
;
803 int seed
= (int)DEMANDINPUT_A(1, inNumSamples
);
811 unit
->m_trig
= curtrig
;
814 ////////////////////////////////////////////////////////////////////////////////////////////////////////
816 void RandID_Ctor(RandID
* unit
)
819 SETCALC(RandID_next
);
820 RandID_next(unit
, 1);
823 void RandID_next(RandID
* unit
, int inNumSamples
)
827 if (id
!= unit
->m_id
) {
829 uint32 iid
= (uint32
)id
;
830 if (iid
< unit
->mWorld
->mNumRGens
) {
831 unit
->mParent
->mRGen
= unit
->mWorld
->mRGen
+ iid
;
837 ////////////////////////////////////////////////////////////////////////////////////////////////////////
839 void LinRand_Ctor(LinRand
* unit
)
843 int n
= (int)ZIN0(2);
845 float range
= hi
- lo
;
846 RGen
& rgen
= *unit
->mParent
->mRGen
;
851 ZOUT0(0) = sc_min(a
, b
) * range
+ lo
;
853 ZOUT0(0) = sc_max(a
, b
) * range
+ lo
;
857 ////////////////////////////////////////////////////////////////////////////////////////////////////////
859 void NRand_Ctor(NRand
* unit
)
863 int n
= (int)ZIN0(2);
865 float range
= hi
- lo
;
866 RGen
& rgen
= *unit
->mParent
->mRGen
;
868 for (int i
=0; i
<n
; ++i
) {
871 ZOUT0(0) = (sum
/n
) * range
+ lo
;
874 ////////////////////////////////////////////////////////////////////////////////////////////////////////
876 void ExpRand_Ctor(ExpRand
* unit
)
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);
893 union { float f
; int i
; } u
;
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
);
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
;
937 int remain
= inNumSamples
;
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
);
947 LOOP(nsmps
, ZXP(out
) = level
;);
949 unit
->mLevel
= level
;
950 unit
->mCounter
= counter
;
954 void LFClipNoise_Ctor(LFClipNoise
* unit
)
956 SETCALC(LFClipNoise_next
);
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
;
974 int remain
= inNumSamples
;
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
);
984 LOOP(nsmps
, ZXP(out
) = level
;);
986 unit
->mLevel
= level
;
987 unit
->mCounter
= counter
;
991 void LFNoise0_next_1(LFNoise0
*unit
, int inNumSamples
)
993 assert(inNumSamples
== 1);
994 float freq
= ZIN0(0);
995 float level
= unit
->mLevel
;
996 int32 counter
= unit
->mCounter
;
999 counter
= (int32
)(unit
->mRate
->mSampleRate
/ sc_max(freq
, .001f
));
1000 counter
= sc_max(1, counter
);
1002 level
= frand2(s1
,s2
,s3
);
1003 unit
->mLevel
= level
;
1008 unit
->mCounter
= counter
;
1012 void LFNoise0_Ctor(LFNoise0
* unit
)
1015 SETCALC(LFNoise0_next_1
);
1017 SETCALC(LFNoise0_next
);
1022 LFNoise0_next_1(unit
, 1);
1025 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1027 void LFNoise1_next(LFNoise1
*unit
, int inNumSamples
)
1029 float *out
= ZOUT(0);
1030 float freq
= ZIN0(0);
1031 float level
= unit
->mLevel
;
1032 float slope
= unit
->mSlope
;
1033 int32 counter
= unit
->mCounter
;
1036 int remain
= inNumSamples
;
1039 counter
= (int32
)(unit
->mRate
->mSampleRate
/ sc_max(freq
, .001f
));
1040 counter
= sc_max(1, counter
);
1041 float nextlevel
= frand2(s1
,s2
,s3
);
1042 slope
= (nextlevel
- level
) / counter
;
1044 int nsmps
= sc_min(remain
, counter
);
1047 LOOP(nsmps
, ZXP(out
) = level
; level
+= slope
;);
1049 unit
->mLevel
= level
;
1050 unit
->mSlope
= slope
;
1051 unit
->mCounter
= counter
;
1055 void LFNoise1_Ctor(LFNoise1
* unit
)
1057 SETCALC(LFNoise1_next
);
1060 unit
->mLevel
= unit
->mParent
->mRGen
->frand2();
1063 LFNoise1_next(unit
, 1);
1066 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1068 void LFNoise2_next(LFNoise2
*unit
, int inNumSamples
)
1070 float *out
= ZOUT(0);
1071 float freq
= ZIN0(0);
1072 float level
= unit
->mLevel
;
1073 float slope
= unit
->mSlope
;
1074 float curve
= unit
->mCurve
;
1075 int counter
= unit
->mCounter
;
1078 int remain
= inNumSamples
;
1081 float value
= unit
->m_nextvalue
;
1082 unit
->m_nextvalue
= frand2(s1
,s2
,s3
);
1083 level
= unit
->m_nextmidpt
;
1084 unit
->m_nextmidpt
= (unit
->m_nextvalue
+ value
) * .5;
1086 counter
= (int32
)(unit
->mRate
->mSampleRate
/ sc_max(freq
, .001f
));
1087 counter
= sc_max(2, counter
);
1088 float fseglen
= (float)counter
;
1089 curve
= 2.f
* (unit
->m_nextmidpt
- level
- fseglen
* slope
) / (fseglen
* fseglen
+ fseglen
);
1091 int nsmps
= sc_min(remain
, counter
);
1100 unit
->mLevel
= level
;
1101 unit
->mSlope
= slope
;
1102 unit
->mCurve
= curve
;
1103 unit
->mCounter
= counter
;
1107 void LFNoise2_Ctor(LFNoise2
* unit
)
1109 SETCALC(LFNoise2_next
);
1114 unit
->m_nextvalue
= unit
->mParent
->mRGen
->frand2();
1115 unit
->m_nextmidpt
= unit
->m_nextvalue
* .5f
;
1117 LFNoise2_next(unit
, 1);
1120 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1122 void WrapBufRd_next0(Unit
*unit
, int inNumSamples
);
1123 void WrapBufRd_next0(Unit
*unit
, int inNumSamples
)
1125 int bufnum
= (int)ZIN0(0);
1126 float *pos
= ZIN(1);
1128 const SndBuf
*buf
= unit
->mWorld
->mSndBufs
+ bufnum
;
1129 int numchan
= buf
->channels
;
1131 LOCK_SNDBUF_SHARED(buf
);
1132 if (numchan
!= unit
->mNumOutputs
) {
1133 ClearUnitOutputs(unit
, inNumSamples
);
1137 const float *data
= buf
->data
;
1138 int numframes
= buf
->frames
;
1141 for (int i
=0; i
<numchan
; ++i
) out
[i
] = OUT(i
);
1144 float fpos
= ZXP(pos
);
1145 int ipos
= (int)fpos
* numchan
;
1146 ipos
= sc_mod(ipos
, numframes
);
1147 int index
= numchan
* ipos
;
1148 for (int i
=0; i
<numchan
; ++i
) {
1149 *++(out
[i
]) = data
[index
];
1155 void ClipBufRd_next0(Unit
*unit
, int inNumSamples
);
1156 void ClipBufRd_next0(Unit
*unit
, int inNumSamples
)
1158 int bufnum
= (int)ZIN0(0);
1159 float *pos
= ZIN(1);
1161 const SndBuf
*buf
= unit
->mWorld
->mSndBufs
+ bufnum
;
1162 LOCK_SNDBUF_SHARED(buf
);
1163 int numchan
= buf
->channels
;
1165 if (numchan
!= unit
->mNumOutputs
) {
1166 ClearUnitOutputs(unit
, inNumSamples
);
1170 const float *data
= buf
->data
;
1171 int numframes
= buf
->frames
;
1172 int maxframe
= numframes
- 2;
1175 for (int i
=0; i
<numchan
; ++i
) out
[i
] = OUT(i
);
1178 float fpos
= ZXP(pos
);
1179 int ipos
= (int)fpos
* numchan
;
1180 ipos
= sc_clip(ipos
, 0, maxframe
);
1181 int index
= numchan
* ipos
;
1182 for (int i
=0; i
<numchan
; ++i
) {
1183 *++(out
[i
]) = data
[index
];
1189 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1195 DefineSimpleUnit(WhiteNoise
);
1196 DefineSimpleUnit(GrayNoise
);
1197 DefineSimpleUnit(ClipNoise
);
1198 DefineSimpleUnit(PinkNoise
);
1199 DefineSimpleUnit(BrownNoise
);
1200 DefineSimpleUnit(Dust
);
1201 DefineSimpleUnit(Dust2
);
1202 DefineSimpleUnit(Crackle
);
1203 DefineSimpleUnit(Logistic
);
1204 DefineSimpleUnit(Hasher
);
1205 DefineSimpleUnit(MantissaMask
);
1206 DefineSimpleUnit(LFClipNoise
);
1207 DefineSimpleUnit(LFNoise0
);
1208 DefineSimpleUnit(LFNoise1
);
1209 DefineSimpleUnit(LFNoise2
);
1210 DefineSimpleUnit(Rand
);
1211 DefineSimpleUnit(IRand
);
1212 DefineSimpleUnit(TRand
);
1213 DefineSimpleUnit(TExpRand
);
1214 DefineSimpleUnit(TIRand
);
1215 DefineSimpleUnit(NRand
);
1216 DefineSimpleUnit(LinRand
);
1217 DefineSimpleUnit(ExpRand
);
1218 DefineSimpleUnit(CoinGate
);
1219 DefineSimpleUnit(RandSeed
);
1220 DefineSimpleUnit(RandID
);
1223 ////////////////////////////////////////////////////////////////////////////////////////////////////////