Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / server / plugins / DynNoiseUGens.cpp
blob8451054653e4bfb7091aa3827ff064a88d3179e9
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
21 * DynNoiseUGens.cpp
22 * xSC3plugins
24 * Created by Alberto de Campo, Sekhar Ramacrishnan, Julian Rohrhuber on Sun May 30 2004.
25 * Copyright (c) 2004 HfbK. All rights reserved.
30 #include "SC_PlugIn.h"
32 static InterfaceTable *ft;
34 struct LFDNoise0 : public Unit
36 float mLevel;
37 float mPhase;
39 struct LFDNoise1 : public Unit
41 float mPhase;
42 float mPrevLevel;
43 float mNextLevel;
47 struct LFDNoise3 : public Unit
49 float mPhase;
50 float mLevelA, mLevelB, mLevelC, mLevelD;
53 struct LFDClipNoise : public Unit
55 float mLevel;
56 float mPhase;
59 //////////////////////////////////////////////////////////////////////////////////////////////////
62 extern "C"
65 void LFDNoise0_next(LFDNoise0 *unit, int inNumSamples);
66 void LFDNoise0_next_k(LFDNoise0 *unit, int inNumSamples);
67 void LFDNoise0_Ctor(LFDNoise0 *unit);
69 void LFDNoise1_next(LFDNoise1 *unit, int inNumSamples);
70 void LFDNoise1_next_k(LFDNoise1 *unit, int inNumSamples);
71 void LFDNoise1_Ctor(LFDNoise1 *unit);
74 void LFDNoise3_next(LFDNoise3 *unit, int inNumSamples);
75 void LFDNoise3_next_k(LFDNoise3 *unit, int inNumSamples);
76 void LFDNoise3_Ctor(LFDNoise3 *unit);
78 void LFDClipNoise_next(LFDClipNoise *unit, int inNumSamples);
79 void LFDClipNoise_Ctor(LFDClipNoise *unit);
82 //////////////////////////////////////////////////////////////////////////////////////////////////
84 void LFDNoise0_next(LFDNoise0 *unit, int inNumSamples)
86 float *out = ZOUT(0);
87 float *freq = ZIN(0);
88 float level = unit->mLevel;
89 float phase = unit->mPhase;
90 float smpdur = SAMPLEDUR;
91 RGET
93 LOOP1(inNumSamples,
94 phase -= ZXP(freq) * smpdur;
95 if (phase < 0) {
96 phase = sc_wrap(phase, 0.f, 1.f);
97 level = frand2(s1,s2,s3);
99 ZXP(out) = level;
101 unit->mLevel = level;
102 unit->mPhase = phase;
103 RPUT
107 void LFDNoise0_next_k(LFDNoise0 *unit, int inNumSamples)
109 float *out = ZOUT(0);
110 float freq = ZIN0(0);
111 float level = unit->mLevel;
112 float phase = unit->mPhase;
113 float smpdur = SAMPLEDUR;
114 float dphase = smpdur * freq;
116 RGET
118 LOOP1(inNumSamples,
119 phase -= dphase;
120 if (phase < 0) {
121 phase = sc_wrap(phase, 0.f, 1.f);
122 level = frand2(s1,s2,s3);
124 ZXP(out) = level;
126 unit->mLevel = level;
127 unit->mPhase = phase;
128 RPUT
132 void LFDNoise0_Ctor(LFDNoise0* unit)
134 if (INRATE(0) == calc_FullRate) {
135 SETCALC(LFDNoise0_next);
136 } else {
137 SETCALC(LFDNoise0_next_k);
140 unit->mPhase = 0.f;
141 unit->mLevel = 0.f;
143 LFDNoise0_next(unit, 1);
146 //////////////////////////////////////////////////////////////////////////////////////////////////
148 void LFDNoise1_next(LFDNoise1 *unit, int inNumSamples)
150 float *out = ZOUT(0);
151 float *freq = ZIN(0);
152 float prevLevel = unit->mPrevLevel;
153 float nextLevel = unit->mNextLevel;
154 float phase = unit->mPhase;
155 float smpdur = SAMPLEDUR;
157 RGET
159 LOOP1(inNumSamples,
160 phase -= ZXP(freq) * smpdur;
161 if (phase < 0) {
162 phase = sc_wrap(phase, 0.f, 1.f);
163 prevLevel = nextLevel;
164 nextLevel = frand2(s1,s2,s3);
166 ZXP(out) = nextLevel + ( phase * (prevLevel - nextLevel) );
168 unit->mPrevLevel = prevLevel;
169 unit->mNextLevel = nextLevel;
170 unit->mPhase = phase;
171 RPUT
174 void LFDNoise1_next_k(LFDNoise1 *unit, int inNumSamples)
176 float *out = ZOUT(0);
177 float freq = ZIN0(0);
178 float prevLevel = unit->mPrevLevel;
179 float nextLevel = unit->mNextLevel;
180 float phase = unit->mPhase;
181 float smpdur = SAMPLEDUR;
182 float dphase = freq * smpdur;
184 RGET
186 LOOP1(inNumSamples,
187 phase -= dphase;
188 if (phase < 0) {
189 phase = sc_wrap(phase, 0.f, 1.f);
190 prevLevel = nextLevel;
191 nextLevel = frand2(s1,s2,s3);
193 ZXP(out)= nextLevel + ( phase * (prevLevel - nextLevel) );
195 unit->mPrevLevel = prevLevel;
196 unit->mNextLevel = nextLevel;
197 unit->mPhase = phase;
198 RPUT
201 void LFDNoise1_Ctor(LFDNoise1* unit)
203 if (INRATE(0) == calc_FullRate) {
204 SETCALC(LFDNoise1_next);
205 } else {
206 SETCALC(LFDNoise1_next_k);
209 unit->mPhase = 0.f;
210 unit->mPrevLevel = 0.f;
211 unit->mNextLevel = unit->mParent->mRGen->frand2();
213 LFDNoise1_next(unit, 1);
216 ////////////////////////////////////////////////////////////////////////////////////////////////////////
218 void LFDNoise3_next(LFDNoise3 *unit, int inNumSamples)
220 float *out = ZOUT(0);
221 float *freq = ZIN(0);
222 float a = unit->mLevelA;
223 float b = unit->mLevelB;
224 float c = unit->mLevelC;
225 float d = unit->mLevelD;
226 float phase = unit->mPhase;
227 float smpdur = SAMPLEDUR;
229 RGET
231 LOOP1(inNumSamples,
232 phase -= ZXP(freq) * smpdur;
233 if (phase < 0) {
234 phase = sc_wrap(phase, 0.f, 1.f);
235 a = b;
236 b = c;
237 c = d;
238 d = frand2(s1,s2,s3) * 0.8f; // limits max interpol. overshoot to 1.
240 ZXP(out) = cubicinterp(1.f - phase, a, b, c, d);
242 unit->mLevelA = a;
243 unit->mLevelB = b;
244 unit->mLevelC = c;
245 unit->mLevelD = d;
246 unit->mPhase = phase;
247 RPUT
250 void LFDNoise3_next_k(LFDNoise3 *unit, int inNumSamples)
252 float *out = ZOUT(0);
253 float freq = ZIN0(0);
254 float a = unit->mLevelA;
255 float b = unit->mLevelB;
256 float c = unit->mLevelC;
257 float d = unit->mLevelD;
258 float phase = unit->mPhase;
259 float dphase = freq * SAMPLEDUR;
261 RGET
263 LOOP1(inNumSamples,
264 phase -= dphase;
265 if (phase < 0) {
266 phase = sc_wrap(phase, 0.f, 1.f);
267 a = b;
268 b = c;
269 c = d;
270 d = frand2(s1,s2,s3) * 0.8f; // limits max interpol. overshoot to 1.
272 ZXP(out) = cubicinterp(1.f - phase, a, b, c, d);
274 unit->mLevelA = a;
275 unit->mLevelB = b;
276 unit->mLevelC = c;
277 unit->mLevelD = d;
278 unit->mPhase = phase;
279 RPUT
282 void LFDNoise3_Ctor(LFDNoise3* unit)
284 if (INRATE(0) == calc_FullRate) {
285 SETCALC(LFDNoise3_next);
286 } else {
287 SETCALC(LFDNoise3_next_k);
290 RGET
291 unit->mPhase = 0.f;
292 unit->mLevelA = frand2(s1, s2, s3) * 0.8f; // limits max interpol. overshoot to 1.
293 unit->mLevelB = frand2(s1, s2, s3) * 0.8f;
294 unit->mLevelC = frand2(s1, s2, s3) * 0.8f;
295 unit->mLevelD = frand2(s1, s2, s3) * 0.8f;
296 RPUT
298 LFDNoise3_next(unit, 1);
301 ////////////////////////////////////////////////////////////////////////////////////////////////////////
302 void LFDClipNoise_next(LFDClipNoise *unit, int inNumSamples)
304 float *out = ZOUT(0);
305 float *freq = ZIN(0);
306 float level = unit->mLevel;
307 float phase = unit->mPhase;
308 float smpdur = SAMPLEDUR;
309 RGET
311 LOOP1(inNumSamples,
312 phase -= ZXP(freq) * smpdur;
313 if (phase < 0) {
314 phase = sc_wrap(phase, 0.f, 1.f);
315 level = fcoin(s1,s2,s3);
317 ZXP(out) = level;
320 unit->mLevel = level;
321 unit->mPhase = phase;
322 RPUT
326 void LFDClipNoise_next_k(LFDClipNoise *unit, int inNumSamples)
328 float *out = ZOUT(0);
329 float freq = ZIN0(0);
330 float level = unit->mLevel;
331 float phase = unit->mPhase;
332 float smpdur = SAMPLEDUR;
333 float dphase = smpdur * freq;
335 RGET
337 LOOP1(inNumSamples,
338 phase -= dphase;
339 if (phase < 0) {
340 phase = sc_wrap(phase, 0.f, 1.f);
341 level = fcoin(s1,s2,s3);
343 ZXP(out) = level;
345 unit->mLevel = level;
346 unit->mPhase = phase;
347 RPUT
351 void LFDClipNoise_Ctor(LFDClipNoise* unit)
353 if (INRATE(0) == calc_FullRate) {
354 SETCALC(LFDClipNoise_next);
355 } else {
356 SETCALC(LFDClipNoise_next_k);
359 unit->mPhase = 0.f;
360 unit->mLevel = 0.f;
362 LFDClipNoise_next(unit, 1);
369 ////////////////////////////////////////////////////////////////////////////////////////////////////////
372 PluginLoad(DynNoise)
374 ft = inTable;
376 DefineSimpleUnit(LFDNoise0);
377 DefineSimpleUnit(LFDNoise1);
378 DefineSimpleUnit(LFDNoise3);
379 DefineSimpleUnit(LFDClipNoise);
383 ////////////////////////////////////////////////////////////////////////////////////////////////////////