-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.jsfx-inc
311 lines (246 loc) · 7.37 KB
/
noise.jsfx-inc
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
desc:Noise generator
// Copyright (C) 2013-2022 Theo Niessink <[email protected]>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
/* Example
desc:Pink noise generator
import Tale/noise.jsfx-inc
@sample
spl0 = spl1 = 0.021 * rng.lcg_pink();
Setting Functions
* lcg_init(seed)
Example: rng.lcg_init(12345);
Initialises the random number generator with the specified seed value
(an integer in the range 1 through 2^31-2), and returns this value.
* lcg_density(density)
Example: black.lcg_density(0.25);
Sets the black noise density (0..1), and returns this value.
* lcg_leaky_set(rc)
Example: rng.lcg_leaky_set(0.0082);
Sets the feedback coefficient of the leaky integrator used for
generating Brownian noise to the specified RC time constant (in
seconds), and returns the filter coefficient.
The leaky integrator behaves like a high-pass filter with a
cutoff frequency (in Hz) of 1/(2*$pi*rc).
Note: lcg_brown() will automatically set the default RC time constant
if none has been set.
Noise Functions
* lcg_white() -- White
* lcg_pink() -- Pink (1/f)
* lcg_brown() -- Brownian
* lcg_blue() -- Blue
* lcg_violet() -- Violet
* lcg_grey() -- Grey
* lcg_black() -- Black
Example: sample = rng.lcg_white();
Returns a sample of noise.
Miscellaneous Functions
* lcg_rand(x)
* lcg_rand2(x)
Example: y = lcg_rand(x);
Example: y = rng.lcg_rand2(x);
Returns a psuedorandom number between 0 and the parameter.
Instance Variables
* seed
Example: rng2.lcg_init(rng1.seed);
The current seed value.
* density
Example: rng2.lcg_density(rng1.density);
The black noise density.
* b0
* b1
* b2
Example: rng2.b0 = rng1.b0; rng2.b1 = rng1.b1; rng2.b2 = rng1.b2;
Pink noise filter coefficients.
* leak.a
* leak.lp
Example: rng2.leak.a = rng1.leak.a;
Brownian noise leaky integrator.
* pink
* white
Example: pink_noise = blue_noise.pink;
Example: white_noise = violet_noise.white;
Blue/violet noise generator pink/white noise output.
* low_shelf*
* peak*
Grey noise generator filters.
*/
import rbj_filter.jsfx-inc
@init
// Recommended range is 1..2^31-2, but for legacy's sake supports at least
// 0..2^31-1.
function lcg_init(seed)
(
seed >= 1 ? (
// seed = (seed - 1) % (2^31 - 2) + 1
seed -= 1;
seed -= ((seed / 2147483646)|0) * 2147483646;
seed += 1;
) : (
seed = 2147483646;
);
this.seed = seed;
);
function lcg_density(density)
(
this.density = density;
);
function lcg_leaky_set(rc)
instance(leak.a)
(
rc <= 0 ? rc = 0.0082;
leak.a = 1 - 1 / (rc * srate + 1);
);
// Lehmer pseudorandom number generator, parameter values from Wikipedia.
// http://en.wikipedia.org/wiki/Lehmer_random_number_generator
function lcg_rand(x)
static(seed)
(
seed < 1 ? seed = 1;
// seed = (seed * 48271) % (2^31 - 1)
seed = seed * 48271 + ((seed * 0.000022477936010098986)|0) * -2147483647;
// (seed - 1) / (2^31 - 3) * x
(seed * x - x) * 0.00000000046566128795826056;
);
function _lcg_rand2()
instance(seed)
(
seed < 1 ? seed = 1;
// seed = (seed * 48271) % (2^31 - 1)
seed = seed * 48271 + ((seed * 0.000022477936010098986)|0) * -2147483647;
);
function lcg_rand2(x)
instance(seed)
(
seed < 1 ? seed = 1;
// seed = (seed * 48271) % (2^31 - 1)
seed = seed * 48271 + ((seed * 0.000022477936010098986)|0) * -2147483647;
// (seed - 1) / (2^31 - 3) * x
(seed * x - x) * 0.00000000046566128795826056;
);
function lcg_white()
instance(seed)
(
seed < 1 ? seed = 1;
// seed = (seed * 48271) % (2^31 - 1)
seed = seed * 48271 + ((seed * 0.000022477936010098986)|0) * -2147483647;
// (seed - 1) / (2^31 - 3) * 2 - 1
seed * 0.00000000093132257591652112 + -1.0000000009313226;
);
// "Pinking" filter (Paul Kellet's economy method) from "DSP generation of
// Pink (1/f) Noise".
// http://www.firstpr.com.au/dsp/pink-noise/
function lcg_pink()
instance(b0, b1, b2)
local(white)
(
white = this.lcg_white();
b0 = 0.99765 * b0 + white * 0.0990460;
b1 = 0.96300 * b1 + white * 0.2965164;
b2 = 0.57000 * b2 + white * 1.0526913;
(b0 + b1 + b2 + white * 0.1848);
);
function lcg_brown()
instance(leak.a, leak.lp)
(
!leak.a ? this.lcg_leaky_set(0);
leak.lp = leak.a * (this.lcg_white() + leak.lp);
);
function lcg_blue()
instance(pink)
local(tmp)
(
tmp = pink;
pink = this.lcg_pink();
(pink - tmp);
);
function lcg_violet()
instance(white)
local(tmp)
(
tmp = white;
white = this.lcg_white();
(white - tmp);
);
function lcg_grey()
instance(low_shelf, peak)
(
// Inverted A-weighting curve appoximation.
!low_shelf.a1 ? (
low_shelf.rbj_low_shelf(70, 0.707, 37.5);
peak.rbj_peak(2150, 0.19, -11.0);
);
low_shelf.rbj_df2(peak.rbj_df2(this.lcg_white())) * 3.1;
);
function lcg_black()
instance(density)
local(white)
(
white = this.lcg_white();
abs(white) >= (1 - density) ? white : 0;
);
/* @gfx
// _lcg_test(1000000);
function _lcg_test(count)
instance(seed)
static(ok1, ok2, n, i, j, first, prev)
local(m, k, x, y, tmp)
(
!i ? i += 1;
n = 2;
loop(i <= n ? count,
i == 1 ? (
this.lcg_init(j);
j == 0 ? ok1 = 1;
ok1 &= j == 0 ? seed == 2^31 - 2 :
j < 2^31 - 1 ? seed == j :
// j < 2^32 - 3 ? seed == j - (2^31 - 2) :
// j < 2^32 ? seed == j - (2^32 - 4);
j < 2^31 ? seed == j - (2^31 - 2);
j += 1;
// m = 2^32;
m = 2^31;
j >= m || !ok1 ? ( i += 1; j = 0; );
) :
i == 2 ? (
j == 0 ? (
this.lcg_init(1);
first = prev = seed;
);
tmp = seed; x = this.lcg_rand2(1);
seed = tmp; y = this.lcg_white();
j == 0 ? ok2 = 1;
ok2 &= seed >= 1 && seed <= 2^31 - 2;
ok2 &= seed != prev;
ok2 &= j < 2^31 - 3 ? seed != first : seed == first;
ok2 &= x >= 0.0 && x <= 1.0;
ok2 &= y >= -1.0 && y <= 1.0;
prev = seed;
j += 1;
m = 2^31 - 2;
j >= m || !ok2 ? ( i += 1; j = 0; );
);
);
gfx_g = gfx_a = 1;
gfx_r = gfx_b = 0;
gfx_x = gfx_y = gfx_texth;
gfx_drawchar($'1'); gfx_drawchar($'.'); gfx_drawchar($'.');
gfx_drawnumber(n, 0);
k = 1;
loop(n,
gfx_x = gfx_texth;
gfx_y += gfx_texth * 5/4;
k == i ? (
gfx_drawnumber(min(j / m * 100, 99), 0); gfx_drawchar($'%');
) : k < i ? (
tmp = k == 1 ? ok1 : k == 2 ? ok2;
!tmp ? ( gfx_drawchar($'n'); gfx_drawchar($'o'); gfx_drawchar($'t'); gfx_drawchar($' '); );
gfx_drawchar($'o'); gfx_drawchar($'k'); gfx_drawchar($' ');
gfx_drawnumber(k, 0); gfx_drawchar($' ');
k == 1 ? ( gfx_drawchar($'S'); gfx_drawchar($'e'); gfx_drawchar($'e'); gfx_drawchar($'d'); ) :
k == 2 ? ( gfx_drawchar($'R'); gfx_drawchar($'a'); gfx_drawchar($'n'); gfx_drawchar($'d'); gfx_drawchar($'o'); gfx_drawchar($'m'); );
);
k += 1;
);
); */