forked from grame-cncm/faustlibraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noises.lib
296 lines (267 loc) · 10 KB
/
noises.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//##################################### noises.lib ########################################
// Faust Noise Generator Library. Its official prefix is `no`.
//########################################################################################
ma = library("maths.lib");
ba = library("basics.lib");
fi = library("filters.lib");
os = library("oscillators.lib");
no = library("noises.lib"); // for compatible copy/paste out of this file
declare name "Faust Noise Generator Library";
declare version "0.0";
//=============================Functions Reference========================================
//========================================================================================
/************************************************************************
************************************************************************
FAUST library file, GRAME section
Except where noted otherwise, Copyright (C) 2003-2017 by GRAME,
Centre National de Creation Musicale.
----------------------------------------------------------------------
GRAME LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
EXCEPTION TO THE LGPL LICENSE : As a special exception, you may create a
larger FAUST program which directly or indirectly imports this library
file and still distribute the compiled code generated by the FAUST
compiler, or a modified version of this compiled code, under your own
copyright and license. This EXCEPTION TO THE LGPL LICENSE explicitly
grants you the right to freely choose the license for the resulting
compiled code. In particular the resulting compiled code has no obligation
to be LGPL or GPL. For example you are free to choose a commercial or
closed source license or any other license if you decide so.
************************************************************************
************************************************************************/
//-------`(no.)noise`----------
// White noise generator (outputs random number between -1 and 1).
// `Noise` is a standard Faust function.
//
// #### Usage
//
// ```
// noise : _
// ```
//------------------------
noise = random / RANDMAX
with{
mask = 4294967295; // 2^32-1
random = +(12345) ~ *(1103515245) & mask; // "linear congruential"
RANDMAX = 2147483647.0; // = 2^31-1 = MAX_SIGNED_INT in 32 bits
};
//---------------------`(no.)multirandom`--------------------------
// Generates multiple decorrelated random numbers in parallel.
//
// #### Usage
// ```
// multirandom(n) : si.bus(n)
// ```
//
// Where:
//
// * `n`: the number of decorrelated random numbers in parallel
//-------------------------------------------------------------
multirandom(n) = randomize(n) ~_
with {
randomize(1) = +(12345) : *(1103515245);
randomize(n) = randomize(1) <: randomize(n-1),_;
};
//-----------------------`(no.)multinoise`------------------------
// Generates multiple decorrelated noises in parallel.
//
// #### Usage
//
// ```
// multinoise(n) : si.bus(n)
// ```
//
// Where:
//
// * `n`: the number of decorrelated random numbers in parallel
//------------------------------------------------------------
multinoise(n) = multirandom(n) : par(i,n,/(RANDMAX)) : par(i,n,float)
with {
RANDMAX = 2147483647.0;
};
//-----------------------`(no.)noises`------------------------
// TODO.
//----------------------------------------------------------
noises(N,i) = multinoise(N) : ba.selector(i,N);
//########################################################################################
/************************************************************************
FAUST library file, jos section
Except where noted otherwise, The Faust functions below in this
section are Copyright (C) 2003-2017 by Julius O. Smith III <[email protected]>
([jos](http://ccrma.stanford.edu/~jos/)), and released under the
(MIT-style) [STK-4.3](#stk-4.3-license) license.
All MarkDown comments in this section are Copyright 2016-2017 by Romain
Michon and/or Julius O. Smith III, and are released under the
[CCA4I](https://creativecommons.org/licenses/by/4.0/) license (TODO: if/when Romain agrees)
************************************************************************/
//---------------------------`(no.)pink_noise`--------------------------
// Pink noise (1/f noise) generator (third-order approximation)
// `pink_noise` is a standard Faust function.
//
// #### Usage
//
// ```
// pink_noise : _;
// ```
//
// #### Reference:
//
// <https://ccrma.stanford.edu/~jos/sasp/Example_Synthesis_1_F_Noise.html>
//------------------------------------------------------------
pink_filter = fi.iir((0.049922035, -0.095993537, 0.050612699, -0.004408786),
(-2.494956002, 2.017265875, -0.522189400));
pink_noise = noise : pink_filter;
pink_noise_m = pink_noise * 12.5; // Equalizes loudness to that of no.noise (thanks Mykle Hansen) - beware of clipping
//-------------------------`(no.)pink_noise_vm`-------------------
// Multi pink noise generator.
//
// #### Usage
//
// ```
// pink_noise_vm(N) : _;
// ```
//
// Where:
//
// * `N`: number of latched white-noise processes to sum,
// not to exceed sizeof(int) in C++ (typically 32).
//
// #### References
//
// * <http://www.dsprelated.com/showarticle/908.php>
// * <http://www.firstpr.com.au/dsp/pink-noise/#Voss-McCartney>
//------------------------------------------------------------
pink_noise_vm(N) = noise <: _,par(i,N,ba.latch(clock(i))) :> _
with {
clock(i) = (ba.time>>i)&1; // i'th latch clock signal
};
//--------------------`(no.)lfnoise`, `(no.)lfnoise0` and `(no.)lfnoiseN`-----------------
// Low-frequency noise generators (Butterworth-filtered downsampled white noise).
//
// #### Usage
//
// ```
// lfnoise0(rate) : _; // new random number every int(SR/rate) samples or so
// lfnoiseN(N,rate) : _; // same as "lfnoise0(rate) : lowpass(N,rate)" [see filters.lib]
// lfnoise(rate) : _; // same as "lfnoise0(rate) : seq(i,5,lowpass(N,rate))" (no overshoot)
// ```
//
// #### Example
//
// (view waveforms in faust2octave):
//
// ```
// rate = SR/100.0; // new random value every 100 samples (SR from music.lib)
// process = lfnoise0(rate), // sampled/held noise (piecewise constant)
// lfnoiseN(3,rate), // lfnoise0 smoothed by 3rd order Butterworth LPF
// lfnoise(rate); // lfnoise0 smoothed with no overshoot
// ```
//------------------------------------------------------------
lfnoise0(freq) = noise : ba.latch(os.oscrs(freq));
lfnoiseN(N,freq) = lfnoise0(freq) : fi.lowpass(N,freq); // Nth-order Butterworth lowpass
lfnoise(freq) = lfnoise0(freq) : seq(i,5,fi.lowpass(1,freq)); // non-overshooting lowpass
//-------------------------`(no.)sparse_noise_vm`-------------------
// sparse noise generator.
//
// #### Usage
//
// ```
// sparse_noise(f0) : _;
// ```
//
// Where:
//
// * ` f0`: average frequency of noise impulses per second
//
// Random impulses in the amplitude range -1 to 1 are generated
// at an average rate of f0 impulses per second.
//
// #### Reference
//
// * See velvet_noise
//------------------------------------------------------------
sparse_noise(f0) = sn
with {
saw = os.lf_sawpos(f0);
sawdiff = saw - saw';
e = float(no.noise); // float() keeps 4.656613e-10f scaling here instead of later
eHeld = e : ba.latch(sawdiff);
eHeldPos = 0.5 + 0.5 * eHeld;
crossed = (saw >= eHeldPos) * (saw' < eHeldPos);
sn = e' * float(crossed);
};
//-------------------------`(no.)velvet_noise_vm`-------------------
// velvet noise generator.
//
// #### Usage
//
// ```
// velvet_noise(amp,f0) : _;
// ```
//
// Where:
//
// * `amp`: amplitude of noise impulses (positive and negative)
// * ` f0`: average frequency of noise impulses per second
//
// #### Reference
//
// * Matti Karjalainen and Hanna Jarvelainen,
// "Reverberation Modeling Using Velvet Noise",
// in Proc. 30th Int. Conf. Intelligent Audio Environments (AES07),
// March 2007.
//
//------------------------------------------------------------
velvet_noise(amp,f0) = vn
with {
sn = no.sparse_noise(f0);
vn = amp * ma.signum(sn);
};
//----------------------------`(no.)gnoise`------------------------
// approximate zero-mean, unit-variance Gaussian white noise generator.
//
// #### Usage
//
// ```
// gnoise(N) : _;
// ```
//
// Where:
//
// * `N`: number of uniform random numbers added to approximate Gaussian white noise
//
// #### Reference
//
// * See Central Limit Theorem
//
//------------------------------------------------------------
gnoise(N) = uvgwn
with {
uwn = no.multinoise(N); // uniform white noise in [-1,1] on N channels
gwn = uwn :> _; // sum of uniform approaches Gaussian by centeral limit thm
sigma = sqrt(N/3.0); // rms of each uwn channel
uvgwn = gwn / sigma; // approaches zero-mean, unit-variance Gaussian white noise, for large N
};
gnoisem(N) = gnoise(N) * 0.625; // Equalizes loudness to that of no.noise (thanks Mykle Hansen)
/*** END jos section ***/
//########################################################################################
/************************************************************************
FAUST library file, further contributions section
All contributions below should indicate both the contributor and terms
of license. If no such indication is found, "git blame" will say who
last edited each line, and that person can be emailed to inquire about
license disposition, if their license choice is not already indicated
elsewhere among the libraries. It is expected that all software will be
released under LGPL, STK-4.3, MIT, BSD, or a similar FOSS license.
************************************************************************/