-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamples.ts
More file actions
642 lines (545 loc) · 17.6 KB
/
samples.ts
File metadata and controls
642 lines (545 loc) · 17.6 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import type { Sample } from '../types';
// Generate synthesized sounds using Web Audio API
// Covers drums, bass, synths, and FX - all procedurally generated
export async function createSynthesizedSamples(
audioContext: AudioContext
): Promise<Map<string, Sample>> {
const samples = new Map<string, Sample>();
// === DRUMS ===
samples.set('kick', {
id: 'kick',
name: 'Kick',
buffer: await createKick(audioContext),
url: '',
});
samples.set('snare', {
id: 'snare',
name: 'Snare',
buffer: await createSnare(audioContext),
url: '',
});
samples.set('hihat', {
id: 'hihat',
name: 'Hi-Hat',
buffer: await createHiHat(audioContext),
url: '',
});
samples.set('clap', {
id: 'clap',
name: 'Clap',
buffer: await createClap(audioContext),
url: '',
});
samples.set('tom', {
id: 'tom',
name: 'Tom',
buffer: await createTom(audioContext),
url: '',
});
samples.set('rim', {
id: 'rim',
name: 'Rim',
buffer: await createRim(audioContext),
url: '',
});
samples.set('cowbell', {
id: 'cowbell',
name: 'Cowbell',
buffer: await createCowbell(audioContext),
url: '',
});
samples.set('openhat', {
id: 'openhat',
name: 'Open Hat',
buffer: await createOpenHat(audioContext),
url: '',
});
// === WORLD/LATIN PERCUSSION ===
samples.set('shaker', {
id: 'shaker',
name: 'Shaker',
buffer: await createShaker(audioContext),
url: '',
});
samples.set('conga', {
id: 'conga',
name: 'Conga',
buffer: await createConga(audioContext),
url: '',
});
samples.set('tambourine', {
id: 'tambourine',
name: 'Tambourine',
buffer: await createTambourine(audioContext),
url: '',
});
samples.set('clave', {
id: 'clave',
name: 'Clave',
buffer: await createClave(audioContext),
url: '',
});
samples.set('cabasa', {
id: 'cabasa',
name: 'Cabasa',
buffer: await createCabasa(audioContext),
url: '',
});
samples.set('woodblock', {
id: 'woodblock',
name: 'Woodblock',
buffer: await createWoodblock(audioContext),
url: '',
});
// === BASS ===
samples.set('bass', {
id: 'bass',
name: 'Bass',
buffer: await createBass(audioContext),
url: '',
});
samples.set('subbass', {
id: 'subbass',
name: 'Sub Bass',
buffer: await createSubBass(audioContext),
url: '',
});
// === SYNTHS ===
samples.set('lead', {
id: 'lead',
name: 'Lead',
buffer: await createLead(audioContext),
url: '',
});
samples.set('pluck', {
id: 'pluck',
name: 'Pluck',
buffer: await createPluck(audioContext),
url: '',
});
samples.set('chord', {
id: 'chord',
name: 'Chord',
buffer: await createChord(audioContext),
url: '',
});
samples.set('pad', {
id: 'pad',
name: 'Pad',
buffer: await createPad(audioContext),
url: '',
});
// === FX ===
samples.set('zap', {
id: 'zap',
name: 'Zap',
buffer: await createZap(audioContext),
url: '',
});
samples.set('noise', {
id: 'noise',
name: 'Noise',
buffer: await createNoiseHit(audioContext),
url: '',
});
return samples;
}
async function createKick(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.5;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Frequency drops from 150Hz to 40Hz
const freq = 150 * Math.exp(-t * 10) + 40;
// Amplitude envelope
const amp = Math.exp(-t * 8);
data[i] = Math.sin(2 * Math.PI * freq * t) * amp;
}
return buffer;
}
async function createSnare(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.3;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Mix of noise and tone
const noise = (Math.random() * 2 - 1) * Math.exp(-t * 15);
const tone = Math.sin(2 * Math.PI * 180 * t) * Math.exp(-t * 20);
data[i] = noise * 0.7 + tone * 0.3;
}
return buffer;
}
async function createHiHat(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.1;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// High-frequency noise with fast decay
const noise = (Math.random() * 2 - 1) * Math.exp(-t * 40);
data[i] = noise * 0.85;
}
return buffer;
}
async function createClap(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.3;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Multiple noise bursts
let amp = 0;
// Initial burst
if (t < 0.02) amp = Math.exp(-t * 100);
// Second burst
else if (t < 0.04) amp = Math.exp(-(t - 0.02) * 100) * 0.8;
// Third burst
else if (t < 0.06) amp = Math.exp(-(t - 0.04) * 100) * 0.6;
// Tail
else amp = Math.exp(-(t - 0.06) * 20) * 0.4;
data[i] = (Math.random() * 2 - 1) * amp;
}
return buffer;
}
// === Additional Drums ===
async function createTom(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.4;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Frequency drops from 200Hz to 80Hz
const freq = 200 * Math.exp(-t * 8) + 80;
const amp = Math.exp(-t * 6);
data[i] = Math.sin(2 * Math.PI * freq * t) * amp * 0.95;
}
return buffer;
}
async function createRim(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.1;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// High pitched click with fast decay
const tone1 = Math.sin(2 * Math.PI * 1200 * t);
const tone2 = Math.sin(2 * Math.PI * 800 * t);
const amp = Math.exp(-t * 80);
data[i] = (tone1 * 0.5 + tone2 * 0.5) * amp;
}
return buffer;
}
async function createCowbell(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.3;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Two inharmonic frequencies (classic cowbell recipe)
const tone1 = Math.sin(2 * Math.PI * 562 * t);
const tone2 = Math.sin(2 * Math.PI * 845 * t);
const amp = Math.exp(-t * 12);
data[i] = (tone1 * 0.6 + tone2 * 0.4) * amp * 0.9;
}
return buffer;
}
async function createOpenHat(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.4;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Longer noise with slower decay than closed hat
const noise = (Math.random() * 2 - 1) * Math.exp(-t * 8);
// Add some metallic tones
const metallic = Math.sin(2 * Math.PI * 4000 * t) * 0.15 * Math.exp(-t * 15);
data[i] = (noise * 0.7 + metallic);
}
return buffer;
}
// === World/Latin Percussion ===
async function createShaker(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.15;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// High-frequency noise with fast attack/decay
const noise = Math.random() * 2 - 1;
const envelope = Math.exp(-t * 25) * (1 - Math.exp(-t * 500));
// Simple highpass approximation
const filtered = noise * 0.7 + (Math.random() * 0.6 - 0.3);
data[i] = filtered * envelope * 0.6;
}
return buffer;
}
async function createConga(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.4;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Pitched membrane sound with slight pitch drop
const freq = 200 * Math.exp(-t * 3);
const fundamental = Math.sin(2 * Math.PI * freq * t);
// Add harmonics for wood/skin character
const harmonic2 = Math.sin(2 * Math.PI * freq * 2.3 * t) * 0.3;
const harmonic3 = Math.sin(2 * Math.PI * freq * 3.1 * t) * 0.15;
// Attack transient (slap)
const slap = (Math.random() * 2 - 1) * Math.exp(-t * 100) * 0.4;
// Envelope
const envelope = Math.exp(-t * 6);
data[i] = (fundamental + harmonic2 + harmonic3 + slap) * envelope * 0.7;
}
return buffer;
}
async function createTambourine(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.25;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Metallic jingles (multiple inharmonic frequencies)
const jingle1 = Math.sin(2 * Math.PI * 2100 * t);
const jingle2 = Math.sin(2 * Math.PI * 3400 * t);
const jingle3 = Math.sin(2 * Math.PI * 4800 * t);
const jingle4 = Math.sin(2 * Math.PI * 6200 * t);
// Noise component for stick hit
const noise = (Math.random() * 2 - 1) * Math.exp(-t * 50);
// Envelope with sustain for jingles
const envelope = Math.exp(-t * 8);
const jingles = (jingle1 + jingle2 * 0.7 + jingle3 * 0.5 + jingle4 * 0.3) * 0.15;
data[i] = (jingles + noise * 0.3) * envelope;
}
return buffer;
}
async function createClave(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.12;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Two-tone wooden click (like two sticks hitting)
const freq1 = 2500;
const freq2 = 3200;
const tone1 = Math.sin(2 * Math.PI * freq1 * t);
const tone2 = Math.sin(2 * Math.PI * freq2 * t) * 0.6;
// Very fast decay
const envelope = Math.exp(-t * 40);
data[i] = (tone1 + tone2) * envelope * 0.6;
}
return buffer;
}
async function createCabasa(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.08;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Very high frequency noise burst
const noise = Math.random() * 2 - 1;
// Very fast attack and decay
const envelope = Math.exp(-t * 60) * (1 - Math.exp(-t * 2000));
data[i] = noise * envelope * 0.5;
}
return buffer;
}
async function createWoodblock(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.15;
const sampleRate = ctx.sampleRate;
const length = Math.floor(duration * sampleRate);
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Resonant filtered click
const freq = 800;
const fundamental = Math.sin(2 * Math.PI * freq * t);
const harmonic = Math.sin(2 * Math.PI * freq * 2.7 * t) * 0.4;
// Sharp attack, medium decay with resonance
const envelope = Math.exp(-t * 20);
const attack = Math.exp(-t * 200);
data[i] = (fundamental + harmonic) * envelope * (0.7 + attack * 0.3);
}
return buffer;
}
// === Bass ===
async function createBass(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.5;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
const freq = 55; // A1
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Sawtooth-ish bass with harmonics
let sample = 0;
for (let h = 1; h <= 8; h++) {
sample += Math.sin(2 * Math.PI * freq * h * t) / h;
}
// Plucky envelope
const amp = Math.exp(-t * 4) * 0.9;
data[i] = sample * amp * 0.8;
}
return buffer;
}
async function createSubBass(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.6;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
const freq = 40; // Low E
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Pure sine sub with slight attack
const attack = Math.min(t * 50, 1);
const decay = Math.exp(-t * 2);
data[i] = Math.sin(2 * Math.PI * freq * t) * attack * decay * 0.9;
}
return buffer;
}
// === Synths ===
async function createLead(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.6;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
const freq = 440; // A4
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Square-ish wave (odd harmonics)
let sample = 0;
for (let h = 1; h <= 7; h += 2) {
sample += Math.sin(2 * Math.PI * freq * h * t) / h;
}
// Synthy envelope with sustain
const attack = Math.min(t * 100, 1);
const release = t > 0.4 ? Math.exp(-(t - 0.4) * 10) : 1;
data[i] = sample * attack * release * 0.75;
}
return buffer;
}
async function createPluck(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.4;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
const freq = 330; // E4
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Karplus-Strong-ish pluck (harmonics that decay at different rates)
let sample = 0;
for (let h = 1; h <= 12; h++) {
const harmonicDecay = Math.exp(-t * (5 + h * 3));
sample += Math.sin(2 * Math.PI * freq * h * t) * harmonicDecay / h;
}
data[i] = sample * 0.8;
}
return buffer;
}
async function createChord(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.8;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
// Minor chord: root, minor third, fifth
const freqs = [220, 261.63, 330]; // A3, C4, E4
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
let sample = 0;
for (const freq of freqs) {
// Soft saw per voice
for (let h = 1; h <= 4; h++) {
sample += Math.sin(2 * Math.PI * freq * h * t) / (h * 3);
}
}
// Soft envelope
const attack = Math.min(t * 20, 1);
const release = t > 0.5 ? Math.exp(-(t - 0.5) * 5) : 1;
data[i] = sample * attack * release * 0.65;
}
return buffer;
}
async function createPad(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 1.5;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
const freq = 220; // A3
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Soft detuned oscillators
const osc1 = Math.sin(2 * Math.PI * freq * t);
const osc2 = Math.sin(2 * Math.PI * freq * 1.005 * t); // Slight detune
const osc3 = Math.sin(2 * Math.PI * freq * 0.995 * t);
// Slow attack, long release
const attack = Math.min(t * 3, 1);
const release = t > 1.0 ? Math.exp(-(t - 1.0) * 3) : 1;
data[i] = (osc1 + osc2 + osc3) / 3 * attack * release * 0.8;
}
return buffer;
}
// === FX ===
async function createZap(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.2;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// Frequency sweeps down rapidly
const freq = 2000 * Math.exp(-t * 30) + 100;
const amp = Math.exp(-t * 15);
data[i] = Math.sin(2 * Math.PI * freq * t) * amp * 0.85;
}
return buffer;
}
async function createNoiseHit(ctx: AudioContext): Promise<AudioBuffer> {
const duration = 0.3;
const sampleRate = ctx.sampleRate;
const length = duration * sampleRate;
const buffer = ctx.createBuffer(1, length, sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < length; i++) {
const t = i / sampleRate;
// White noise with envelope
const amp = Math.exp(-t * 10);
data[i] = (Math.random() * 2 - 1) * amp * 0.8;
}
return buffer;
}