This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
brainfiller.snd.glsl
381 lines (296 loc) · 9.71 KB
/
brainfiller.snd.glsl
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#version 430
#define saturate(i) clamp(i,0.,1.)
#define linearstep(a,b,x) saturate(((x)-(a))/((b)-(a)))
#define p2f(i) (exp2(((i)-69.)/12.)*440.)
#define repeat(i, n) for(int i=0; i<(n); i++)
#define inRange(t,a,b) (step(a,t)*(1.-step(b,t)))
#define inRangeB(t,a,b) ((a<=t)&&(t<b))
const float PI = acos( -1.0 );
const float TAU = PI * 2.0;
const float GOLD = PI * (3.0 - sqrt(5.0));// 2.39996...
const float BPS = 2.25;
const float B2T = 1.0 / BPS;
const float S2T = 0.25 * B2T;
const float SAMPLES_PER_SEC = 48000.0;
const float SWING = 1.15;
int SAMPLES_PER_STEP = int( SAMPLES_PER_SEC / BPS / 4.0 );
int SAMPLES_PER_BEAT = 4 * SAMPLES_PER_STEP;
uvec3 hash3u(uvec3 v) {
v = v * 1664525u + 1013904223u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
v ^= v >> 16u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return v;
}
vec3 hash3f(vec3 v) {
uvec3 u = hash3u(floatBitsToUint(v));
return vec3(u) / float(-1u);
}
vec2 cis(float t) {
return vec2(cos(t), sin(t));
}
mat2 rotate2D( float x ) {
vec2 v = cis(x);
return mat2(v.x, v.y, -v.y, v.x);
}
mat3 orthBas(vec3 z) {
z = normalize(z);
vec3 x = normalize(cross(vec3(0, 1, 0), z));
vec3 y = cross(z, x);
return mat3(x, y, z);
}
vec3 cyclicNoise(vec3 p, float pers) {
vec4 sum = vec4(0.0);
for (int i = 0; i ++ < 4;) {
p *= orthBas(vec3(-1.0, 2.0, -3.0));
p += sin(p.yzx);
sum = (sum + vec4(cross(sin(p.zxy), cos(p)), 1.0)) / pers;
p *= 2.0;
}
return sum.xyz / sum.w;
}
layout(location = 0) uniform int waveOutPosition;
#if defined(EXPORT_EXECUTABLE)
#pragma work_around_begin:layout(std430,binding=0)buffer ssbo{vec2 %s[];};layout(local_size_x=1)in;
vec2 waveOutSamples[];
#pragma work_around_end
#else
layout(std430, binding = 0) buffer SoundOutput{ vec2 waveOutSamples[]; };
layout(local_size_x = 1) in;
#endif
vec2 shotgun( float t, float spread ) {
vec2 sum = vec2( 0.0 );
for ( int i = 0; i < 64; i ++ ) {
vec3 dice = hash3f( vec3( i ) );
sum += vec2( sin( TAU * t * exp2( spread * dice.x ) ) ) * rotate2D( TAU * dice.y );
}
return sum / 64.0;
}
vec2 cheapnoise(float t) {
uvec3 s=uvec3(t * 256.0);
float p=fract(t * 256.0);
vec3 dice;
vec2 v = vec2(0.0);
dice=vec3(hash3u(s + 0u)) / float(-1u) - vec3(0.5, 0.5, 0.0);
v += dice.xy * smoothstep(1.0, 0.0, abs(p + dice.z));
dice=vec3(hash3u(s + 1u)) / float(-1u) - vec3(0.5, 0.5, 1.0);
v += dice.xy * smoothstep(1.0, 0.0, abs(p + dice.z));
dice=vec3(hash3u(s + 2u)) / float(-1u) - vec3(0.5, 0.5, 2.0);
v += dice.xy * smoothstep(1.0, 0.0, abs(p + dice.z));
return 2.0 * v;
}
vec2 ladderLPF(float freq, float cutoff, float reso) {
float omega = freq / cutoff;
float omegaSq = omega * omega;
float a = 4.0 * reso + omegaSq * omegaSq - 6.0 * omegaSq + 1.0;
float b = 4.0 * omega * (omegaSq - 1.0);
return vec2(
1.0 / sqrt(a * a + b * b),
atan(a, b)
);
}
vec2 twoPoleHPF(float freq, float cutoff, float reso) {
float omega = freq / cutoff;
float omegaSq = omega * omega;
float a = 2.0 * (1.0 - reso) * omega;
float b = omegaSq - 1.0;
return vec2(
omegaSq / sqrt(a * a + b * b),
atan(a, b)
);
}
vec4 quant(float x, float ks, float kt, out float i) {
i = floor(floor(x / ks + 1E-4) * ks / kt + 1E-4);
float s = kt <= ks
? ks * floor(x / ks + 1E-4)
: ks * ceil(i * kt / ks - 1E-4);
float l = kt <= ks
? ks
: ks * ceil((i + 1.0) * kt / ks - 1E-4) - s;
float t = x - s;
float q = l - t;
return vec4(s, t, s + l, q);
}
vec4 quant(float x, float ks, float kt) {
float i;
return quant(x, ks, kt, i);
}
float swing(float x, float k) {
float xm = mod(x, 2.0);
return x + (1.0 - k) * linearstep(0.0, k, xm) * linearstep(2.0, k, xm);
}
float unswing(float x0, float x, float y, float k) {
return (
x0
- 2.0 * floor((x - y) / 2.0)
- k * linearstep(0.0, 1.0, mod(x - y, 2.0))
- (2.0 - k) * linearstep(1.0, 2.0, mod(x - y, 2.0))
);
}
float cheapFilterSaw( float phase, float k ) {
float i_wave = fract( phase );
float i_c = smoothstep( 1.0, 0.0, i_wave / k );
return ( i_wave + i_c ) * 2.0 - 1.0 - k;
}
float CHORDS[] = float[](
0.0, 2.0, 3.0, 7.0, 14.0, 15.0, 19.0, 22.0,
-5.0, 2.0, 3.0, 5.0, 7.0, 10.0, 14.0, 15.0,
-4.0, 0.0, 3.0, 7.0, 14.0, 15.0, 19.0, 24.0,
-7.0, 0.0, 7.0, 8.0, 10.0, 12.0, 15.0, 19.0
);
void main() {
int frame = int(gl_GlobalInvocationID.x) + waveOutPosition;
vec4 time = vec4(frame % (SAMPLES_PER_BEAT * ivec4(1, 4, 64, 65536))) / SAMPLES_PER_SEC;
vec4 beats = time * BPS;
// return float( max( 0, frame + SAMPLES_PER_STEP * offset ) % ( SAMPLES_PER_STEP * every ) ) / SAMPLES_PER_SEC;
vec2 dest = vec2(0);
float sidechain;
{ // kick
float t = time.x;
float q = B2T - t;
sidechain = 0.2 + 0.8 * smoothstep(0.0, 0.4, t) * smoothstep(0.0, 0.001, q);
float env = smoothstep(0.0, 0.001, q) * smoothstep(2.0 * B2T, 0.1 * B2T, t);
if (128.0 < beats.w && beats.w < 160.0) {
env *= exp(-70.0 * t);
}
if (32.0 < beats.w && beats.w < 287.9) {
float wave = sin(
270.0 * t
- 40.0 * exp(-t * 20.0)
- 20.0 * exp(-t * 60.0)
- 10.0 * exp(-t * 300.0)
- 0.4 * sin(120.0 * t)
);
dest += 0.6 * tanh(2.0 * env * wave);
}
}
if (64.0 - 0.75 < beats.w && beats.w < 256.0) { // hihat
float t = mod(time.x, S2T);
float st = mod(floor(time.y / S2T), 16.0);
float vel = fract(st * 0.2 + 0.42);
float env = exp(-exp2(7.0 - 3.0 * vel) * t);
vec2 wave = shotgun(6000.0 * t, 2.0);
dest += 0.2 * env * sidechain * tanh(8.0 * wave);
}
if (96.0 < beats.w && beats.w < 290.0) { // clap
float t = mod(time.y + S2T, 4.0 * B2T);
float env = mix(
exp(-26.0 * t),
exp(-200.0 * mod(t, 0.013)),
exp(-80.0 * max(0.0, t - 0.02))
);
vec2 wave = cyclicNoise(vec3(4.0 * cis(800.0 * t), 1940.0 * t), 1.5).xy;
dest += 0.1 * tanh(20.0 * env * wave);
}
if (32.0 < beats.w && beats.w < 256.0) { // shaker
float t = mod(time.x, S2T);
float st = mod(floor(time.y / S2T), 16.0);
float vel = fract(st * 0.41 + 0.63);
float env = smoothstep(0.0, 0.02, t) * exp(-exp2(6.0 - 3.0 * vel) * t);
vec2 wave = cyclicNoise(vec3(cis(2800.0 * t), exp2(8.0 + 3.0 * vel) * t), 0.8).xy;
dest += 0.15 * env * sidechain * tanh(2.0 * wave);
}
{ // perc 1
float t = mod(time.z - S2T, 6.0 * S2T);
float env = mix(
exp(-t),
exp(-30.0 * t),
0.95
);
vec2 wave = sin(7100.0 * t + vec2(0, PI / 2.0) + 10.0 * cheapnoise(t));
dest += 0.3 * env * tanh(wave) * smoothstep(128.0, 160.0, beats.w);
}
{ // perc 2
float t = mod(time.z - 3.0 * S2T, 6.0 * S2T);
float env = mix(
exp(-t),
exp(-30.0 * t),
0.95
);
vec2 wave = 2.0 * fract(1200.0 * t + sin(1000.0 * t) + vec2(0.0, 0.25)) - 1.0;
dest += 0.3 * env * tanh(wave) * smoothstep(128.0, 160.0, beats.w);
}
{ // beep
float t = mod(time.y - 5.0 * S2T, 16.0 * S2T);
float env = smoothstep(0.0, 0.001, t) * mix(
exp(-2.0 * t),
smoothstep(0.0, 0.001, 0.07 - t),
0.98
);
vec2 wave = sin(50000.0 * t + vec2(PI / 2.0, 0));
dest += 0.2 * env * wave * smoothstep(16.0, 32.0, beats.w);
}
if (96.0 < beats.w) { // crash
float t = mod(time.z - 32.0 * B2T, 64.0 * B2T);
float env = mix(exp(-t), exp(-10.0 * t), 0.7);
vec2 wave = shotgun(3800.0 * t, 2.0);
dest += 0.3 * env * sidechain * tanh(8.0 * wave);
}
if (160.0 < beats.w && beats.w < 224.0) { // ride
float t = mod(time.x, 2.0 * S2T);
float q = 2.0 * S2T - t;
float env = exp(-5.0 * t);
vec2 sum = vec2(0.0);
repeat(i, 8) {
vec3 dice = hash3f(vec3(i));
vec3 dice2 = hash3f(dice);
vec2 wave = vec2(0.0);
wave = 4.5 * env * sin(wave + exp2(12.10 + 0.1 * dice.x) * t + dice2.xy);
wave = 2.2 * env * sin(wave + exp2(14.67 + 0.5 * dice.y) * t + dice2.yz);
wave = 1.0 * env * sin(wave + exp2(13.89 + 1.0 * dice.z) * t + dice2.zx);
sum += wave;
}
dest += 0.08 * env * sidechain * tanh(sum);
}
{ // additive riff
vec2 sum = vec2(0.0);
float t = mod(time.x, S2T);
float q = S2T - t;
float st = floor(time.z / S2T);
float env = smoothstep(0.0, 0.01, t) * smoothstep(0.0, 0.01, q);
float basefreq = 80.0;
float stmod = fract(0.615 * st);
float cutenv = smoothstep(0.0, 0.01, t) * exp(-14.0 * t);
float cutoff = exp2(
5.0
- 2.0 * smoothstep(32.0, 16.0, beats.w)
+ 2.5 * smoothstep(128.0, 160.0, beats.w)
- step(256.0, beats.w)
- 2.0 * smoothstep(288.0, 320.0, beats.w)
+ 3.0 * stmod
+ 4.0 * cutenv
);
repeat(i, 128) {
float fi = float(i);
float p = 1.0 + fi;
p = pow(p, 1.1); // metal
p = mix(p, 1.0, 0.1); // centroid
float freq = basefreq * p;
vec2 phase = vec2(t * freq);
vec2 lpf = ladderLPF(freq, cutoff, 0.3);
vec2 hpf = twoPoleHPF(freq, 200.0, 0.0);
sum += sin(TAU * phase + lpf.y + hpf.y) / p * lpf.x * hpf.x * env * rotate2D(2.4 * fi);
}
dest += 0.2 * mix(0.2, 1.0, sidechain) * tanh(5.0 * sum) * smoothstep(8.0, 32.0, beats.w);
}
if (beats.w < 224.0) { // oidos drone
vec2 sum=vec2(0.0);
repeat(i, 2500) {
vec3 diceA = hash3f(vec3(i / 50));
vec3 diceB = hash3f(vec3(i));
float t = mod(time.z - diceA.x * (64.0 * B2T), 64.0 * B2T);
float env = sin(PI * t / (64.0 * B2T));
float tone = 8.0 + 9.0 * diceA.y + 0.06 * diceB.y;
float freq = exp2(tone);
vec2 phase = t * freq + fract(diceB.xy * 999.0);
phase += 0.1 * fract(32.0 * phase); // add high freq
sum += sin(TAU * phase) * env / 1000.0;
}
dest += 1.0 * mix(0.2, 1.0, sidechain) * sum * smoothstep(0.0, 32.0, beats.w);
}
waveOutSamples[frame] = clamp(dest, -1.0, 1.0) * smoothstep(320.0, 288.0, beats.w);
}