Skip to content
Source
Sign inSign up
fuko/rive-preview-player
Open History to see the last commit

gen-fixture-sounds.mjs

26 lines1.4 KB

1// Writes the fixture's synthetic sound effects (16-bit mono WAV). Run: node scripts/gen-fixture-sounds.mjs
2import { writeFileSync } from 'node:fs';
3const RATE = 22050;
4function wav(name, seconds, fn) {
5 const n = Math.round(seconds * RATE);
6 const buf = Buffer.alloc(44 + n * 2);
7 buf.write('RIFF', 0); buf.writeUInt32LE(36 + n * 2, 4); buf.write('WAVE', 8);
8 buf.write('fmt ', 12); buf.writeUInt32LE(16, 16); buf.writeUInt16LE(1, 20); buf.writeUInt16LE(1, 22);
9 buf.writeUInt32LE(RATE, 24); buf.writeUInt32LE(RATE * 2, 28); buf.writeUInt16LE(2, 32); buf.writeUInt16LE(16, 34);
10 buf.write('data', 36); buf.writeUInt32LE(n * 2, 40);
11 let seed = 1;
12 const noise = () => ((seed = (seed * 1103515245 + 12345) & 0x7fffffff) / 0x3fffffff) - 1;
13 for (let i = 0; i < n; i++) {
14 const t = i / RATE;
15 const v = Math.max(-1, Math.min(1, fn(t, seconds, noise)));
16 buf.writeInt16LE(Math.round(v * 32000), 44 + i * 2);
17 }
18 writeFileSync(new URL(`../test-fixture-audio/${name}`, import.meta.url), buf);
20// whoosh: band-ish noise swelling and fading
21let lp = 0;
22wav('whoosh.wav', 0.9, (t, d, noise) => { lp += (noise() - lp) * 0.08; return lp * 2.2 * Math.sin(Math.PI * t / d); });
23// pop: short pitched blip
24wav('pop.wav', 0.25, (t) => Math.sin(2 * Math.PI * (600 - 900 * t) * t) * Math.exp(-t * 18) * 0.9);
25// ding: bell
26wav('ding.wav', 1.4, (t) => (Math.sin(2 * Math.PI * 880 * t) * 0.6 + Math.sin(2 * Math.PI * 1760 * t) * 0.25) * Math.exp(-t * 3));