1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
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.
10 
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.
15 
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
19 */
20 /*
21  *  DynNoiseUGens.cpp
22  *  xSC3plugins
23  *
24  *  Created by Alberto de Campo, Sekhar Ramacrishnan, Julian Rohrhuber on Sun May 30 2004.
25  *  Copyright (c) 2004 HfbK. All rights reserved.
26  *
27  */
28 
29 
30 #include "SC_PlugIn.h"
31 
32 static InterfaceTable* ft;
33 
34 struct LFDNoise0 : public Unit {
35     float mLevel;
36     float mPhase;
37 };
38 struct LFDNoise1 : public Unit {
39     float mPhase;
40     float mPrevLevel;
41     float mNextLevel;
42 };
43 
44 
45 struct LFDNoise3 : public Unit {
46     float mPhase;
47     float mLevelA, mLevelB, mLevelC, mLevelD;
48 };
49 
50 struct LFDClipNoise : public Unit {
51     float mLevel;
52     float mPhase;
53 };
54 
55 //////////////////////////////////////////////////////////////////////////////////////////////////
56 
57 
58 extern "C" {
59 
60 void LFDNoise0_next(LFDNoise0* unit, int inNumSamples);
61 void LFDNoise0_next_k(LFDNoise0* unit, int inNumSamples);
62 void LFDNoise0_Ctor(LFDNoise0* unit);
63 
64 void LFDNoise1_next(LFDNoise1* unit, int inNumSamples);
65 void LFDNoise1_next_k(LFDNoise1* unit, int inNumSamples);
66 void LFDNoise1_Ctor(LFDNoise1* unit);
67 
68 
69 void LFDNoise3_next(LFDNoise3* unit, int inNumSamples);
70 void LFDNoise3_next_k(LFDNoise3* unit, int inNumSamples);
71 void LFDNoise3_Ctor(LFDNoise3* unit);
72 
73 void LFDClipNoise_next(LFDClipNoise* unit, int inNumSamples);
74 void LFDClipNoise_Ctor(LFDClipNoise* unit);
75 }
76 
77 //////////////////////////////////////////////////////////////////////////////////////////////////
78 
LFDNoise0_next(LFDNoise0 * unit,int inNumSamples)79 void LFDNoise0_next(LFDNoise0* unit, int inNumSamples) {
80     float* out = ZOUT(0);
81     float* freq = ZIN(0);
82     float level = unit->mLevel;
83     float phase = unit->mPhase;
84     float smpdur = SAMPLEDUR;
85     RGET
86 
87         LOOP1(
88             inNumSamples, phase -= ZXP(freq) * smpdur; if (phase < 0) {
89                 phase = sc_wrap(phase, 0.f, 1.f);
90                 level = frand2(s1, s2, s3);
91             } ZXP(out) = level;) unit->mLevel = level;
92     unit->mPhase = phase;
93     RPUT
94 }
95 
LFDNoise0_next_k(LFDNoise0 * unit,int inNumSamples)96 void LFDNoise0_next_k(LFDNoise0* unit, int inNumSamples) {
97     float* out = ZOUT(0);
98     float freq = ZIN0(0);
99     float level = unit->mLevel;
100     float phase = unit->mPhase;
101     float smpdur = SAMPLEDUR;
102     float dphase = smpdur * freq;
103 
104     RGET
105 
106         LOOP1(
107             inNumSamples, phase -= dphase; if (phase < 0) {
108                 phase = sc_wrap(phase, 0.f, 1.f);
109                 level = frand2(s1, s2, s3);
110             } ZXP(out) = level;) unit->mLevel = level;
111     unit->mPhase = phase;
112     RPUT
113 }
114 
LFDNoise0_Ctor(LFDNoise0 * unit)115 void LFDNoise0_Ctor(LFDNoise0* unit) {
116     if (INRATE(0) == calc_FullRate) {
117         SETCALC(LFDNoise0_next);
118     } else {
119         SETCALC(LFDNoise0_next_k);
120     }
121 
122     unit->mPhase = 0.f;
123     unit->mLevel = 0.f;
124 
125     LFDNoise0_next(unit, 1);
126 }
127 
128 //////////////////////////////////////////////////////////////////////////////////////////////////
129 
LFDNoise1_next(LFDNoise1 * unit,int inNumSamples)130 void LFDNoise1_next(LFDNoise1* unit, int inNumSamples) {
131     float* out = ZOUT(0);
132     float* freq = ZIN(0);
133     float prevLevel = unit->mPrevLevel;
134     float nextLevel = unit->mNextLevel;
135     float phase = unit->mPhase;
136     float smpdur = SAMPLEDUR;
137 
138     RGET
139 
140         LOOP1(
141             inNumSamples, phase -= ZXP(freq) * smpdur; if (phase < 0) {
142                 phase = sc_wrap(phase, 0.f, 1.f);
143                 prevLevel = nextLevel;
144                 nextLevel = frand2(s1, s2, s3);
145             } ZXP(out) = nextLevel + (phase * (prevLevel - nextLevel));) unit->mPrevLevel = prevLevel;
146     unit->mNextLevel = nextLevel;
147     unit->mPhase = phase;
148     RPUT
149 }
150 
LFDNoise1_next_k(LFDNoise1 * unit,int inNumSamples)151 void LFDNoise1_next_k(LFDNoise1* unit, int inNumSamples) {
152     float* out = ZOUT(0);
153     float freq = ZIN0(0);
154     float prevLevel = unit->mPrevLevel;
155     float nextLevel = unit->mNextLevel;
156     float phase = unit->mPhase;
157     float smpdur = SAMPLEDUR;
158     float dphase = freq * smpdur;
159 
160     RGET
161 
162         LOOP1(
163             inNumSamples, phase -= dphase; if (phase < 0) {
164                 phase = sc_wrap(phase, 0.f, 1.f);
165                 prevLevel = nextLevel;
166                 nextLevel = frand2(s1, s2, s3);
167             } ZXP(out) = nextLevel + (phase * (prevLevel - nextLevel));) unit->mPrevLevel = prevLevel;
168     unit->mNextLevel = nextLevel;
169     unit->mPhase = phase;
170     RPUT
171 }
172 
LFDNoise1_Ctor(LFDNoise1 * unit)173 void LFDNoise1_Ctor(LFDNoise1* unit) {
174     if (INRATE(0) == calc_FullRate) {
175         SETCALC(LFDNoise1_next);
176     } else {
177         SETCALC(LFDNoise1_next_k);
178     }
179 
180     unit->mPhase = 0.f;
181     unit->mPrevLevel = 0.f;
182     unit->mNextLevel = unit->mParent->mRGen->frand2();
183 
184     LFDNoise1_next(unit, 1);
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////////////////////////////
188 
LFDNoise3_next(LFDNoise3 * unit,int inNumSamples)189 void LFDNoise3_next(LFDNoise3* unit, int inNumSamples) {
190     float* out = ZOUT(0);
191     float* freq = ZIN(0);
192     float a = unit->mLevelA;
193     float b = unit->mLevelB;
194     float c = unit->mLevelC;
195     float d = unit->mLevelD;
196     float phase = unit->mPhase;
197     float smpdur = SAMPLEDUR;
198 
199     RGET
200 
201         LOOP1(
202             inNumSamples, phase -= ZXP(freq) * smpdur; if (phase < 0) {
203                 phase = sc_wrap(phase, 0.f, 1.f);
204                 a = b;
205                 b = c;
206                 c = d;
207                 d = frand2(s1, s2, s3) * 0.8f; // limits max interpol. overshoot to 1.
208             } ZXP(out) = cubicinterp(1.f - phase, a, b, c, d);) unit->mLevelA = a;
209     unit->mLevelB = b;
210     unit->mLevelC = c;
211     unit->mLevelD = d;
212     unit->mPhase = phase;
213     RPUT
214 }
215 
LFDNoise3_next_k(LFDNoise3 * unit,int inNumSamples)216 void LFDNoise3_next_k(LFDNoise3* unit, int inNumSamples) {
217     float* out = ZOUT(0);
218     float freq = ZIN0(0);
219     float a = unit->mLevelA;
220     float b = unit->mLevelB;
221     float c = unit->mLevelC;
222     float d = unit->mLevelD;
223     float phase = unit->mPhase;
224     float dphase = freq * SAMPLEDUR;
225 
226     RGET
227 
228         LOOP1(
229             inNumSamples, phase -= dphase; if (phase < 0) {
230                 phase = sc_wrap(phase, 0.f, 1.f);
231                 a = b;
232                 b = c;
233                 c = d;
234                 d = frand2(s1, s2, s3) * 0.8f; // limits max interpol. overshoot to 1.
235             } ZXP(out) = cubicinterp(1.f - phase, a, b, c, d);) unit->mLevelA = a;
236     unit->mLevelB = b;
237     unit->mLevelC = c;
238     unit->mLevelD = d;
239     unit->mPhase = phase;
240     RPUT
241 }
242 
LFDNoise3_Ctor(LFDNoise3 * unit)243 void LFDNoise3_Ctor(LFDNoise3* unit) {
244     if (INRATE(0) == calc_FullRate) {
245         SETCALC(LFDNoise3_next);
246     } else {
247         SETCALC(LFDNoise3_next_k);
248     }
249 
250     RGET unit->mPhase = 0.f;
251     unit->mLevelA = frand2(s1, s2, s3) * 0.8f; // limits max interpol. overshoot to 1.
252     unit->mLevelB = frand2(s1, s2, s3) * 0.8f;
253     unit->mLevelC = frand2(s1, s2, s3) * 0.8f;
254     unit->mLevelD = frand2(s1, s2, s3) * 0.8f;
255     RPUT
256 
257         LFDNoise3_next(unit, 1);
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////////////////////////////
LFDClipNoise_next(LFDClipNoise * unit,int inNumSamples)261 void LFDClipNoise_next(LFDClipNoise* unit, int inNumSamples) {
262     float* out = ZOUT(0);
263     float* freq = ZIN(0);
264     float level = unit->mLevel;
265     float phase = unit->mPhase;
266     float smpdur = SAMPLEDUR;
267     RGET
268 
269         LOOP1(
270             inNumSamples, phase -= ZXP(freq) * smpdur; if (phase < 0) {
271                 phase = sc_wrap(phase, 0.f, 1.f);
272                 level = fcoin(s1, s2, s3);
273             } ZXP(out) = level;)
274 
275             unit->mLevel = level;
276     unit->mPhase = phase;
277     RPUT
278 }
279 
LFDClipNoise_next_k(LFDClipNoise * unit,int inNumSamples)280 void LFDClipNoise_next_k(LFDClipNoise* unit, int inNumSamples) {
281     float* out = ZOUT(0);
282     float freq = ZIN0(0);
283     float level = unit->mLevel;
284     float phase = unit->mPhase;
285     float smpdur = SAMPLEDUR;
286     float dphase = smpdur * freq;
287 
288     RGET
289 
290         LOOP1(
291             inNumSamples, phase -= dphase; if (phase < 0) {
292                 phase = sc_wrap(phase, 0.f, 1.f);
293                 level = fcoin(s1, s2, s3);
294             } ZXP(out) = level;) unit->mLevel = level;
295     unit->mPhase = phase;
296     RPUT
297 }
298 
LFDClipNoise_Ctor(LFDClipNoise * unit)299 void LFDClipNoise_Ctor(LFDClipNoise* unit) {
300     if (INRATE(0) == calc_FullRate) {
301         SETCALC(LFDClipNoise_next);
302     } else {
303         SETCALC(LFDClipNoise_next_k);
304     }
305 
306     unit->mPhase = 0.f;
307     unit->mLevel = 0.f;
308 
309     LFDClipNoise_next(unit, 1);
310 }
311 
312 
313 ////////////////////////////////////////////////////////////////////////////////////////////////////////
314 
315 
PluginLoad(DynNoise)316 PluginLoad(DynNoise) {
317     ft = inTable;
318 
319     DefineSimpleUnit(LFDNoise0);
320     DefineSimpleUnit(LFDNoise1);
321     DefineSimpleUnit(LFDNoise3);
322     DefineSimpleUnit(LFDClipNoise);
323 }
324 
325 ////////////////////////////////////////////////////////////////////////////////////////////////////////
326