@holtjs/stage 0.0.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/audio.js ADDED
@@ -0,0 +1,781 @@
1
+ // src/audio.ts
2
+ const TAU = Math.PI * 2;
3
+ /** Fast deterministic PRNG (mulberry32), returning floats in [0, 1). */
4
+ function mulberry32(seed) {
5
+ let a = seed >>> 0 || 1;
6
+ return () => {
7
+ a = a + 0x6d2b79f5 >>> 0;
8
+ let x = Math.imul(a ^ a >>> 15, 1 | a);
9
+ x = x + Math.imul(x ^ x >>> 7, 61 | x) ^ x;
10
+ return ((x ^ x >>> 14) >>> 0) / 4294967296;
11
+ };
12
+ }
13
+ /** One-pole filter alpha coefficient for cutoff `hz` at sample rate `sr`. */
14
+ function onePoleAlpha(hz, sr) {
15
+ if (!(hz > 0) || !(sr > 0)) return 1;
16
+ const clamped = Math.min(hz, sr * .45);
17
+ return 1 - Math.exp(-TAU * clamped / sr);
18
+ }
19
+ /** Smooth soft-saturation limiter preventing harsh digital clipping while preserving warmth. */
20
+ function softClipSample(x, knee = .85) {
21
+ const ax = Math.abs(x);
22
+ if (ax <= knee) return x;
23
+ const headroom = Math.max(.05, 1 - knee);
24
+ return Math.sign(x) * (knee + headroom * Math.tanh((ax - knee) / headroom));
25
+ }
26
+ function foleyDuration(layers) {
27
+ let end = 0;
28
+ for (const l of layers) {
29
+ const t0 = l.t ?? 0;
30
+ const len = l.kind === "noise" ? (l.a ?? .006) + (l.h ?? .01) + (l.d ?? .08) : l.kind === "bubbles" ? l.span + (l.durMax ?? .024) + .01 : (l.a ?? .002) + (l.d ?? .05);
31
+ if (t0 + len > end) end = t0 + len;
32
+ }
33
+ return end;
34
+ }
35
+ function renderNoiseLayer(out, sr, l) {
36
+ const a = Math.max(5e-4, l.a ?? .006);
37
+ const h = Math.max(0, l.h ?? .01);
38
+ const d = Math.max(.002, l.d ?? .08);
39
+ const len = a + h + d;
40
+ const i0 = Math.max(0, Math.round((l.t ?? 0) * sr));
41
+ const n = Math.min(Math.ceil(len * sr), out.length - i0);
42
+ if (n <= 0) return;
43
+ const aN = Math.max(1, a * sr);
44
+ const hN = h * sr;
45
+ const dN = Math.max(1, d * sr);
46
+ const vol = l.vol ?? .5;
47
+ const color = l.color ?? "pink";
48
+ const curve = l.curve ?? "exp";
49
+ const rand = mulberry32(l.seed ?? 0x9e3779b9);
50
+ let p0 = 0, p1 = 0, p2 = 0, brown = 0;
51
+ const useBp = typeof l.bp === "number" && Number.isFinite(l.bp) && l.bp > 0;
52
+ const bpMax = Math.max(20, sr * .32);
53
+ const bp0 = useBp ? Math.max(20, Math.min(l.bp, bpMax)) : 0;
54
+ const bp1 = useBp ? Math.max(20, Math.min(typeof l.bp1 === "number" && Number.isFinite(l.bp1) ? l.bp1 : bp0, bpMax)) : 0;
55
+ const damp = 1 / Math.max(.35, Math.min(8, l.q ?? .85));
56
+ let svfLow = 0, svfBand = 0, svfF = 0;
57
+ const useLp = typeof l.lp === "number" && Number.isFinite(l.lp) && l.lp > 0;
58
+ const lpMax = Math.max(20, sr * .45);
59
+ const lp0 = useLp ? Math.max(20, Math.min(l.lp, lpMax)) : 0;
60
+ const lp1 = useLp ? Math.max(20, Math.min(typeof l.lp1 === "number" && Number.isFinite(l.lp1) ? l.lp1 : lp0, lpMax)) : 0;
61
+ let lpY = 0, lpK = 0;
62
+ const hpA = typeof l.hp === "number" && l.hp > 0 ? Math.exp(-TAU * Math.min(l.hp, sr * .4) / sr) : 0;
63
+ let hpX = 0, hpY = 0;
64
+ for (let i = 0; i < n; i++) {
65
+ const frac = i / n;
66
+ const w = rand() * 2 - 1;
67
+ let s;
68
+ if (color === "pink") {
69
+ p0 = .99765 * p0 + w * .099046;
70
+ p1 = .963 * p1 + w * .2965164;
71
+ p2 = .57 * p2 + w * 1.0526913;
72
+ s = (p0 + p1 + p2 + w * .1848) * .22;
73
+ } else if (color === "brown") {
74
+ brown = .985 * brown + w * .095;
75
+ s = brown * 1.9;
76
+ } else s = w;
77
+ if (useBp) {
78
+ if ((i & 15) === 0) {
79
+ const fc = bp0 * Math.pow(bp1 / bp0, frac);
80
+ svfF = Math.min(.92, 2 * Math.sin(Math.PI * fc / sr));
81
+ }
82
+ svfLow += svfF * svfBand;
83
+ const svfHigh = s - svfLow - damp * svfBand;
84
+ svfBand += svfF * svfHigh;
85
+ s = svfBand;
86
+ }
87
+ if (useLp) {
88
+ if ((i & 15) === 0) lpK = onePoleAlpha(lp0 * Math.pow(lp1 / lp0, frac), sr);
89
+ lpY += (s - lpY) * lpK;
90
+ s = lpY;
91
+ }
92
+ if (hpA) {
93
+ const y = hpA * (hpY + s - hpX);
94
+ hpX = s;
95
+ hpY = y;
96
+ s = y;
97
+ }
98
+ let env;
99
+ if (i < aN) {
100
+ const u = i / aN;
101
+ env = curve === "cosine" ? .5 - .5 * Math.cos(Math.PI * u) : u;
102
+ } else if (i < aN + hN) env = 1;
103
+ else {
104
+ const u = Math.min(1, (i - aN - hN) / dN);
105
+ if (curve === "cosine") {
106
+ const c = .5 + .5 * Math.cos(Math.PI * u);
107
+ env = c * c;
108
+ } else {
109
+ const r = 1 - u;
110
+ env = r * r * r;
111
+ }
112
+ }
113
+ out[i0 + i] += s * env * vol;
114
+ }
115
+ }
116
+ function renderModalLayer(out, sr, l) {
117
+ const a = Math.max(5e-4, l.a ?? .002);
118
+ const d = Math.max(.004, l.d ?? .05);
119
+ const len = a + d;
120
+ const i0 = Math.max(0, Math.round((l.t ?? 0) * sr));
121
+ const n = Math.min(Math.ceil(len * sr), out.length - i0);
122
+ if (n <= 0) return;
123
+ const aN = Math.max(1, a * sr);
124
+ const dN = Math.max(1, d * sr);
125
+ const f0 = Math.max(20, l.f);
126
+ const f1 = Math.max(20, l.f1 ?? f0);
127
+ const slideN = Math.max(1, (l.slide ?? len) * sr);
128
+ const slideK = f0 === f1 ? 1 : Math.pow(f1 / f0, 1 / slideN);
129
+ const vol = l.vol ?? .4;
130
+ const harm2 = Math.max(0, Math.min(.5, l.harm2 ?? 0));
131
+ let freq = f0;
132
+ let ph = 0;
133
+ for (let i = 0; i < n; i++) {
134
+ if (i < slideN) freq *= slideK;
135
+ ph += TAU * freq / sr;
136
+ let s = Math.sin(ph);
137
+ if (harm2 > 0) s += harm2 * Math.sin(ph * 2);
138
+ let env;
139
+ if (i < aN) env = .5 - .5 * Math.cos(Math.PI * (i / aN));
140
+ else {
141
+ const u = (i - aN) / dN;
142
+ env = Math.exp(-4.6 * u) * (1 - u);
143
+ }
144
+ out[i0 + i] += s * env * vol;
145
+ }
146
+ }
147
+ function renderBubbleLayer(out, sr, l) {
148
+ const count = Math.max(1, Math.min(48, Math.floor(l.count)));
149
+ const t0 = l.t ?? 0;
150
+ const span = Math.max(.01, l.span);
151
+ const fMin = Math.max(120, l.fMin);
152
+ const fMax = Math.max(fMin, l.fMax);
153
+ const rise = Math.max(1, Math.min(1.6, l.rise ?? 1.24));
154
+ const dMin = Math.max(.004, l.durMin ?? .008);
155
+ const dMax = Math.max(dMin, l.durMax ?? .022);
156
+ const vol = l.vol ?? .22;
157
+ const rand = mulberry32(l.seed ?? 726343225);
158
+ for (let b = 0; b < count; b++) {
159
+ const u = (b + .15 + rand() * .7) / count;
160
+ const startSec = t0 + u * span;
161
+ const bell = Math.sin(Math.PI * Math.min(1, Math.max(.05, u)));
162
+ const f0 = fMin * Math.pow(fMax / fMin, rand());
163
+ const f1 = f0 * (1 + (rise - 1) * (.65 + rand() * .7));
164
+ const dur = dMin + rand() * (dMax - dMin);
165
+ renderModalLayer(out, sr, {
166
+ kind: "modal",
167
+ t: startSec,
168
+ f: f0,
169
+ f1,
170
+ slide: dur,
171
+ a: .0018,
172
+ d: dur,
173
+ vol: vol * (.45 + .55 * rand()) * (.45 + .55 * bell)
174
+ });
175
+ }
176
+ }
177
+ /**
178
+ * Render a layered procedural foley sound into a mono Float32Array at `sr` Hz.
179
+ * Pure & deterministic: runs identically in browsers and Node.
180
+ */
181
+ function renderFoley(layers, sr = 44100, opts = {}) {
182
+ const dur = foleyDuration(layers);
183
+ const total = Math.max(1, Math.ceil((dur + .015) * sr));
184
+ const out = new Float32Array(total);
185
+ for (const layer of layers) if (layer.kind === "noise") renderNoiseLayer(out, sr, layer);
186
+ else if (layer.kind === "bubbles") renderBubbleLayer(out, sr, layer);
187
+ else renderModalLayer(out, sr, layer);
188
+ let dcX = 0, dcY = 0, maxAbs = 0;
189
+ for (let i = 0; i < out.length; i++) {
190
+ const x = out[i];
191
+ const y = x - dcX + .995 * dcY;
192
+ dcX = x;
193
+ dcY = y;
194
+ out[i] = y;
195
+ const ay = Math.abs(y);
196
+ if (ay > maxAbs) maxAbs = ay;
197
+ }
198
+ const targetPeak = opts.peak ?? .82;
199
+ const clipKnee = opts.clip ?? .85;
200
+ const scale = targetPeak > 0 && maxAbs > 1e-5 ? Math.min(2.2, targetPeak / maxAbs) : 1;
201
+ for (let i = 0; i < out.length; i++) out[i] = softClipSample(out[i] * scale, clipKnee);
202
+ return out;
203
+ }
204
+ /** Deterministically pre-render `count` acoustic variations of a foley builder (eliminates robotic repetition). */
205
+ function renderFoleyVariants(builder, count, sr = 44100, opts = {}) {
206
+ const n = Math.max(1, Math.floor(count));
207
+ const out = [];
208
+ for (let i = 0; i < n; i++) out.push(renderFoley(builder(i), sr, opts));
209
+ return out;
210
+ }
211
+ /** Non-repeating deterministic variant selector so consecutive steps/strokes always sound organic. */
212
+ function createVariantPicker(count, seed = 20903) {
213
+ const n = Math.max(1, Math.floor(count));
214
+ let rand = mulberry32(seed);
215
+ let prev = -1;
216
+ return {
217
+ count: n,
218
+ next() {
219
+ if (n <= 1) return 0;
220
+ let idx = Math.floor(rand() * n);
221
+ if (idx === prev) idx = (idx + 1 + Math.floor(rand() * (n - 1))) % n;
222
+ prev = idx;
223
+ return idx;
224
+ },
225
+ reset() {
226
+ rand = mulberry32(seed);
227
+ prev = -1;
228
+ }
229
+ };
230
+ }
231
+ /** Compute distance attenuation & stereo pan from listener delta `(dx, dy)` in world tiles, or `null` if out of earshot. */
232
+ function spatialMix(dx, dy, near = 4, far = 13, farVol = .3, panSpan = 8) {
233
+ const d = Math.hypot(dx, dy);
234
+ if (d > far + 1) return null;
235
+ return {
236
+ vol: d <= near ? 1 : Math.max(farVol, 1 - (d - near) / Math.max(.001, far - near) * (1 - farVol)),
237
+ pan: Math.max(-.5, Math.min(.5, dx / Math.max(.1, panSpan)))
238
+ };
239
+ }
240
+ /** Encode mono Float32 samples (-1..1) as a standard 16-bit PCM WAV byte array (for Node preview / asset export). */
241
+ function encodeWavPcm16(samples, sr = 44100) {
242
+ const n = samples.length;
243
+ const buf = new ArrayBuffer(44 + n * 2);
244
+ const v = new DataView(buf);
245
+ const writeStr = (off, s) => {
246
+ for (let i = 0; i < s.length; i++) v.setUint8(off + i, s.charCodeAt(i));
247
+ };
248
+ writeStr(0, "RIFF");
249
+ v.setUint32(4, 36 + n * 2, true);
250
+ writeStr(8, "WAVE");
251
+ writeStr(12, "fmt ");
252
+ v.setUint32(16, 16, true);
253
+ v.setUint16(20, 1, true);
254
+ v.setUint16(22, 1, true);
255
+ v.setUint32(24, sr, true);
256
+ v.setUint32(28, sr * 2, true);
257
+ v.setUint16(32, 2, true);
258
+ v.setUint16(34, 16, true);
259
+ writeStr(36, "data");
260
+ v.setUint32(40, n * 2, true);
261
+ for (let i = 0; i < n; i++) {
262
+ const raw = samples[i];
263
+ const c = Number.isFinite(raw) ? Math.max(-1, Math.min(1, raw)) : 0;
264
+ v.setInt16(44 + i * 2, Math.round(c * 32767), true);
265
+ }
266
+ return new Uint8Array(buf);
267
+ }
268
+ /** Gentle hydrodynamic water paddle stroke: soft water wash + delicate Minnaert micro-bubbles (never wind-like or laser-like). */
269
+ function buildSwimStrokeFoley(variant = 0) {
270
+ const v = (variant % 4 + 4) % 4;
271
+ const fShift = [
272
+ 1,
273
+ .92,
274
+ 1.07,
275
+ .96
276
+ ][v];
277
+ const dShift = [
278
+ 1,
279
+ 1.06,
280
+ .95,
281
+ 1.02
282
+ ][v];
283
+ const seedBase = 701 + v * 97;
284
+ return [
285
+ {
286
+ kind: "noise",
287
+ color: "brown",
288
+ t: 0,
289
+ a: .045 * dShift,
290
+ h: .025 * dShift,
291
+ d: .175 * dShift,
292
+ curve: "cosine",
293
+ vol: .36,
294
+ bp: 460 * fShift,
295
+ bp1: 230 * fShift,
296
+ q: .9,
297
+ lp: 950 * fShift,
298
+ lp1: 480 * fShift,
299
+ hp: 130,
300
+ seed: seedBase
301
+ },
302
+ {
303
+ kind: "noise",
304
+ color: "pink",
305
+ t: .022 * dShift,
306
+ a: .042 * dShift,
307
+ h: .02 * dShift,
308
+ d: .175 * dShift,
309
+ curve: "cosine",
310
+ vol: .32,
311
+ bp: 1050 * fShift,
312
+ bp1: 440 * fShift,
313
+ q: .85,
314
+ lp: 1800 * fShift,
315
+ lp1: 760 * fShift,
316
+ hp: 300,
317
+ seed: seedBase + 19
318
+ },
319
+ {
320
+ kind: "bubbles",
321
+ t: .024 * dShift,
322
+ span: .19 * dShift,
323
+ count: 9,
324
+ fMin: 400 * fShift,
325
+ fMax: 920 * fShift,
326
+ rise: 1.18,
327
+ durMin: .008,
328
+ durMax: .018,
329
+ vol: .08,
330
+ seed: seedBase + 41
331
+ }
332
+ ];
333
+ }
334
+ /** Soft character plunge into water: cozy liquid "plop" + frothy splash + shimmering water droplets. */
335
+ function buildDiveInFoley(variant = 0) {
336
+ const v = (variant % 3 + 3) % 3;
337
+ const fShift = [
338
+ 1,
339
+ .94,
340
+ 1.06
341
+ ][v];
342
+ const seedBase = 1103 + v * 131;
343
+ return [
344
+ {
345
+ kind: "modal",
346
+ t: 0,
347
+ f: 250 * fShift,
348
+ f1: 115 * fShift,
349
+ slide: .052,
350
+ a: .004,
351
+ d: .068,
352
+ vol: .24
353
+ },
354
+ {
355
+ kind: "modal",
356
+ t: .032,
357
+ f: 330 * fShift,
358
+ f1: 455 * fShift,
359
+ slide: .026,
360
+ a: .003,
361
+ d: .032,
362
+ vol: .16
363
+ },
364
+ {
365
+ kind: "noise",
366
+ color: "pink",
367
+ t: .01,
368
+ a: .014,
369
+ h: .024,
370
+ d: .23,
371
+ curve: "exp",
372
+ vol: .42,
373
+ bp: 1280 * fShift,
374
+ bp1: 460 * fShift,
375
+ q: .85,
376
+ lp: 2300 * fShift,
377
+ lp1: 760 * fShift,
378
+ hp: 260,
379
+ seed: seedBase + 29
380
+ },
381
+ {
382
+ kind: "bubbles",
383
+ t: .035,
384
+ span: .24,
385
+ count: 11,
386
+ fMin: 460 * fShift,
387
+ fMax: 1380 * fShift,
388
+ rise: 1.28,
389
+ durMin: .007,
390
+ durMax: .021,
391
+ vol: .15,
392
+ seed: seedBase + 53
393
+ }
394
+ ];
395
+ }
396
+ /** Climbing out of water onto a wooden dock/deck: light wet wooden step + brief trickling droplets. */
397
+ function buildClimbOutFoley(variant = 0) {
398
+ const v = (variant % 3 + 3) % 3;
399
+ const fShift = [
400
+ 1,
401
+ .95,
402
+ 1.05
403
+ ][v];
404
+ const seedBase = 1409 + v * 113;
405
+ return [
406
+ {
407
+ kind: "modal",
408
+ t: 0,
409
+ f: 265 * fShift,
410
+ f1: 165 * fShift,
411
+ slide: .024,
412
+ a: .002,
413
+ d: .034,
414
+ vol: .26,
415
+ harm2: .1
416
+ },
417
+ {
418
+ kind: "noise",
419
+ color: "pink",
420
+ t: .012,
421
+ a: .018,
422
+ h: .012,
423
+ d: .135,
424
+ curve: "cosine",
425
+ vol: .3,
426
+ bp: 1220 * fShift,
427
+ bp1: 520 * fShift,
428
+ q: .95,
429
+ lp: 1950 * fShift,
430
+ lp1: 780 * fShift,
431
+ hp: 280,
432
+ seed: seedBase
433
+ },
434
+ {
435
+ kind: "bubbles",
436
+ t: .03,
437
+ span: .14,
438
+ count: 5,
439
+ fMin: 560 * fShift,
440
+ fMax: 1280 * fShift,
441
+ rise: 1.24,
442
+ durMin: .006,
443
+ durMax: .015,
444
+ vol: .11,
445
+ seed: seedBase + 31
446
+ }
447
+ ];
448
+ }
449
+ /** Warm wooden plank footstep (light wooden knock + soft step — never booming). */
450
+ function buildStepWoodFoley(variant = 0) {
451
+ const v = (variant % 4 + 4) % 4;
452
+ const fShift = [
453
+ 1,
454
+ .92,
455
+ 1.08,
456
+ .96
457
+ ][v];
458
+ const seed = 2111 + v * 89;
459
+ return [
460
+ {
461
+ kind: "modal",
462
+ t: 0,
463
+ f: 285 * fShift,
464
+ f1: 175 * fShift,
465
+ slide: .018,
466
+ a: .0015,
467
+ d: .026,
468
+ vol: .34,
469
+ harm2: .14
470
+ },
471
+ {
472
+ kind: "modal",
473
+ t: .001,
474
+ f: 520 * fShift,
475
+ f1: 360 * fShift,
476
+ slide: .012,
477
+ a: .001,
478
+ d: .016,
479
+ vol: .13
480
+ },
481
+ {
482
+ kind: "noise",
483
+ color: "pink",
484
+ t: 0,
485
+ a: .002,
486
+ h: .003,
487
+ d: .024,
488
+ curve: "exp",
489
+ vol: .28,
490
+ bp: 1050 * fShift,
491
+ bp1: 580 * fShift,
492
+ q: .85,
493
+ lp: 1650,
494
+ hp: 220,
495
+ seed
496
+ }
497
+ ];
498
+ }
499
+ /** Soft grass / cherry meadow footstep (plush leafy brush + faint muted step). */
500
+ function buildStepGrassFoley(variant = 0) {
501
+ const v = (variant % 4 + 4) % 4;
502
+ const fShift = [
503
+ 1,
504
+ .93,
505
+ 1.09,
506
+ .97
507
+ ][v];
508
+ const seed = 2503 + v * 97;
509
+ return [{
510
+ kind: "modal",
511
+ t: 0,
512
+ f: 165 * fShift,
513
+ f1: 105 * fShift,
514
+ slide: .02,
515
+ a: .003,
516
+ d: .024,
517
+ vol: .14
518
+ }, {
519
+ kind: "noise",
520
+ color: "pink",
521
+ t: .002,
522
+ a: .014,
523
+ h: .008,
524
+ d: .064,
525
+ curve: "cosine",
526
+ vol: .44,
527
+ bp: 1320 * fShift,
528
+ bp1: 740 * fShift,
529
+ q: .78,
530
+ lp: 2050,
531
+ hp: 320,
532
+ seed
533
+ }];
534
+ }
535
+ /** Warm beach sand footstep (two overlapping soft fine-grain puffs). */
536
+ function buildStepSandFoley(variant = 0) {
537
+ const v = (variant % 4 + 4) % 4;
538
+ const fShift = [
539
+ 1,
540
+ .92,
541
+ 1.07,
542
+ .96
543
+ ][v];
544
+ const seed = 2909 + v * 101;
545
+ return [
546
+ {
547
+ kind: "modal",
548
+ t: 0,
549
+ f: 145 * fShift,
550
+ f1: 92 * fShift,
551
+ slide: .018,
552
+ a: .003,
553
+ d: .024,
554
+ vol: .12
555
+ },
556
+ {
557
+ kind: "noise",
558
+ color: "pink",
559
+ t: 0,
560
+ a: .01,
561
+ h: .006,
562
+ d: .056,
563
+ curve: "cosine",
564
+ vol: .38,
565
+ bp: 1580 * fShift,
566
+ bp1: 880 * fShift,
567
+ q: .88,
568
+ lp: 2550,
569
+ hp: 420,
570
+ seed
571
+ },
572
+ {
573
+ kind: "noise",
574
+ color: "pink",
575
+ t: .022,
576
+ a: .01,
577
+ h: .004,
578
+ d: .058,
579
+ curve: "exp",
580
+ vol: .26,
581
+ bp: 1850 * fShift,
582
+ bp1: 960 * fShift,
583
+ q: .92,
584
+ lp: 2650,
585
+ hp: 500,
586
+ seed: seed + 17
587
+ }
588
+ ];
589
+ }
590
+ /** Clean flagstone / cobble footstep (warm rounded pebble tap). */
591
+ function buildStepStoneFoley(variant = 0) {
592
+ const v = (variant % 4 + 4) % 4;
593
+ const fShift = [
594
+ 1,
595
+ .93,
596
+ 1.08,
597
+ .97
598
+ ][v];
599
+ const seed = 3301 + v * 83;
600
+ return [
601
+ {
602
+ kind: "modal",
603
+ t: 0,
604
+ f: 470 * fShift,
605
+ f1: 290 * fShift,
606
+ slide: .014,
607
+ a: .001,
608
+ d: .02,
609
+ vol: .26
610
+ },
611
+ {
612
+ kind: "modal",
613
+ t: 0,
614
+ f: 235 * fShift,
615
+ f1: 145 * fShift,
616
+ slide: .018,
617
+ a: .001,
618
+ d: .024,
619
+ vol: .24
620
+ },
621
+ {
622
+ kind: "noise",
623
+ color: "pink",
624
+ t: 0,
625
+ a: .0015,
626
+ h: .003,
627
+ d: .022,
628
+ curve: "exp",
629
+ vol: .24,
630
+ bp: 1520 * fShift,
631
+ bp1: 920 * fShift,
632
+ q: 1.05,
633
+ lp: 2400,
634
+ hp: 400,
635
+ seed
636
+ }
637
+ ];
638
+ }
639
+ /** Muffled carpet / rug / tatami footstep (soft cloth pad). */
640
+ function buildStepSoftFoley(variant = 0) {
641
+ const v = (variant % 4 + 4) % 4;
642
+ const fShift = [
643
+ 1,
644
+ .94,
645
+ 1.06,
646
+ .97
647
+ ][v];
648
+ const seed = 3701 + v * 73;
649
+ return [{
650
+ kind: "modal",
651
+ t: 0,
652
+ f: 145 * fShift,
653
+ f1: 92 * fShift,
654
+ slide: .02,
655
+ a: .004,
656
+ d: .028,
657
+ vol: .18
658
+ }, {
659
+ kind: "noise",
660
+ color: "pink",
661
+ t: 0,
662
+ a: .01,
663
+ h: .006,
664
+ d: .048,
665
+ curve: "cosine",
666
+ vol: .34,
667
+ lp: 760 * fShift,
668
+ hp: 110,
669
+ seed
670
+ }];
671
+ }
672
+ /** MIDI note number -> frequency in Hz. */
673
+ const midi = (n) => 440 * Math.pow(2, (n - 69) / 12);
674
+ const SYNTH_NOTE = {
675
+ c: 0,
676
+ d: 2,
677
+ e: 4,
678
+ f: 5,
679
+ g: 7,
680
+ a: 9,
681
+ b: 11
682
+ };
683
+ /** Parse note name ('c5', 'g#4', 'bb3') -> MIDI number. */
684
+ function noteNum(s) {
685
+ const m = /^([a-g])([#b]?)(-?\d)$/.exec(s);
686
+ if (!m) throw new Error("note " + s);
687
+ return SYNTH_NOTE[m[1]] + (m[2] === "#" ? 1 : m[2] === "b" ? -1 : 0) + (+m[3] + 1) * 12;
688
+ }
689
+ /** Parse note name -> frequency in Hz. */
690
+ const hz = (s) => midi(noteNum(s));
691
+ function synthRng(seed) {
692
+ let h = seed >>> 0 || 1;
693
+ return () => {
694
+ h ^= h << 13;
695
+ h ^= h >>> 17;
696
+ h ^= h << 5;
697
+ return (h >>> 0) / 4294967296 * 2 - 1;
698
+ };
699
+ }
700
+ /** Render one chiptune/chime voice additively into `out` (Float32Array at `sr`). */
701
+ function synthVoice(out, sr, v) {
702
+ const a = v.a ?? .003, h = v.h ?? .05, d = v.d ?? .1, len = a + h + d, i0 = Math.round((v.t ?? 0) * sr);
703
+ const n = Math.min(Math.ceil(len * sr), out.length - i0), f0 = v.f, f1 = v.f1 ?? v.f, slideN = (v.slide ?? len) * sr, vol = v.vol ?? .5;
704
+ const W = {
705
+ sq: 0,
706
+ tri: 1,
707
+ saw: 2,
708
+ sin: 3
709
+ }[v.w ?? "sq"] ?? 4;
710
+ const duty0 = v.duty ?? .5, dDuty = ((v.duty1 ?? duty0) - duty0) / (len * sr), vib = v.vib, arp = v.arp, punch = v.punch ?? 0;
711
+ const lp0 = v.lp ?? 0, dLp = ((v.lp1 ?? lp0) - lp0) / (len * sr), hpA = v.hp ? Math.exp(-2 * Math.PI * v.hp / sr) : 0, q = v.bits ? Math.pow(2, v.bits - 1) : 0;
712
+ const slideK = f0 === f1 ? 1 : Math.pow(f1 / f0, 1 / slideN), aN = a * sr, hN = h * sr, dN = d * sr;
713
+ const vibW = vib ? TAU * vib[1] / sr : 0, vibD = vib ? vib[0] / 12 : 0, vib0 = vib ? (vib[2] ?? 0) * sr : 0;
714
+ const rnd = synthRng(v.seed ?? 7);
715
+ let ph = 0, nz = rnd(), lpY = 0, lpK = 0, hpX = 0, hpY = 0, fs = f0, mult = 1, ai = 0;
716
+ for (let i = 0; i < n; i++) {
717
+ if (i < slideN) fs *= slideK;
718
+ if (arp && ai < arp.length && i >= arp[ai][0] * sr) mult = arp[ai++][1];
719
+ let f = fs * mult;
720
+ if (vib && i > vib0) f *= 1 + vibD * .693 * Math.sin(vibW * i);
721
+ ph += f / sr;
722
+ let s;
723
+ if (ph >= 1) {
724
+ ph -= Math.floor(ph);
725
+ if (W === 4) nz = rnd();
726
+ }
727
+ if (W === 0) s = ph < duty0 + dDuty * i ? 1 : -1;
728
+ else if (W === 1) s = Math.round((ph < .5 ? 4 * ph - 1 : 3 - 4 * ph) * 7.5) / 7.5;
729
+ else if (W === 2) s = 2 * ph - 1;
730
+ else if (W === 3) s = Math.sin(TAU * ph);
731
+ else {
732
+ if (f > sr / 2) nz = rnd();
733
+ s = nz;
734
+ }
735
+ let e;
736
+ if (i < aN) e = i / aN;
737
+ else if (i < aN + hN) e = 1 + punch * (1 - (i - aN) / hN);
738
+ else {
739
+ const r = 1 - (i - aN - hN) / dN;
740
+ e = r * r;
741
+ }
742
+ s *= e * vol;
743
+ if (lp0) {
744
+ if ((i & 31) === 0) lpK = 1 - Math.exp(-TAU * (lp0 + dLp * i) / sr);
745
+ lpY += (s - lpY) * lpK;
746
+ s = lpY;
747
+ }
748
+ if (hpA) {
749
+ const y = hpA * (hpY + s - hpX);
750
+ hpX = s;
751
+ hpY = y;
752
+ s = y;
753
+ }
754
+ if (q) s = Math.round(s * q) / q;
755
+ out[i0 + i] += s;
756
+ }
757
+ }
758
+ const voicesDuration = (vs) => Math.max(...vs.map((v) => (v.t ?? 0) + (v.a ?? .003) + (v.h ?? .05) + (v.d ?? .1)));
759
+ /** Sequence of notes (`[[note, beats], ...]`) at `step` seconds per beat, using `base` voice settings. */
760
+ const tune = (notes, step, base, t0 = 0) => {
761
+ let t = t0;
762
+ const out = [];
763
+ for (const [n, b] of notes) {
764
+ if (n) out.push({
765
+ ...base,
766
+ t,
767
+ f: hz(n),
768
+ h: base.h ?? step * b * .6
769
+ });
770
+ t += step * b;
771
+ }
772
+ return out;
773
+ };
774
+ /** Render a list of synth voices to a soft-clipped mono Float32Array at `sr`. Pure. */
775
+ function renderVoices(vs, sr = 44100) {
776
+ const out = new Float32Array(Math.ceil((voicesDuration(vs) + .01) * sr));
777
+ for (const v of vs) synthVoice(out, sr, v);
778
+ for (let i = 0; i < out.length; i++) out[i] = Math.tanh(out[i]);
779
+ return out;
780
+ }
781
+ export { buildClimbOutFoley, buildDiveInFoley, buildStepGrassFoley, buildStepSandFoley, buildStepSoftFoley, buildStepStoneFoley, buildStepWoodFoley, buildSwimStrokeFoley, createVariantPicker, encodeWavPcm16, hz, midi, mulberry32, noteNum, onePoleAlpha, renderFoley, renderFoleyVariants, renderVoices, softClipSample, spatialMix, synthVoice, tune };