/ simulated_instruments / aerophone
Sign in
simulated_instruments / aerophone
aerophone / src / audio.ts
136 lines · 4.5 KBBlameHistoryRaw
1// Turning the listener's recording (one sample per lattice step) into audio:
2// band-limited resampling to 48 kHz, a 20 Hz high-pass, a WAV encoder, and a magnitude spectrum.
4export const AUDIO_RATE = 48000;
6// Windowed-sinc resampling from fsIn to fsOut. The low-pass cutoff is 0.45 * min(fsIn, fsOut),
7// so this works for both down- and upsampling. Kernel: 16 zero crossings each side, Blackman window.
8export function resample(src: Float32Array, fsIn: number, fsOut: number): Float32Array {
9 const fc = 0.45 * Math.min(fsIn, fsOut);
10 const zc = fsIn / (2 * fc); // input samples per zero crossing
11 const half = 16 * zc;
12 const nOut = Math.floor(((src.length - 1) * fsOut) / fsIn) + 1;
13 const out = new Float32Array(nOut);
14 const gain = (2 * fc) / fsIn;
15 for (let n = 0; n < nOut; n++) {
16 const t = (n * fsIn) / fsOut;
17 const k0 = Math.max(0, Math.ceil(t - half));
18 const k1 = Math.min(src.length - 1, Math.floor(t + half));
19 let acc = 0;
20 for (let k = k0; k <= k1; k++) {
21 const tau = k - t;
22 const x = tau / zc; // in zero crossings
23 const sinc = x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x);
24 const w = 0.42 + 0.5 * Math.cos((Math.PI * tau) / half) + 0.08 * Math.cos((2 * Math.PI * tau) / half);
25 acc += src[k] * sinc * w;
26 }
27 out[n] = acc * gain;
28 }
29 return out;
32// One-pole high-pass (after removing the mean), to drop the slow mean-pressure drift.
33export function highPass(x: Float32Array, fs: number, fc: number): Float32Array {
34 const out = new Float32Array(x.length);
35 if (x.length === 0) return out;
36 let mean = 0;
37 for (const v of x) mean += v;
38 mean /= x.length;
39 const rc = 1 / (2 * Math.PI * fc);
40 const a = rc / (rc + 1 / fs);
41 let yPrev = 0;
42 let xPrev = x[0] - mean;
43 for (let n = 0; n < x.length; n++) {
44 const xn = x[n] - mean;
45 const y = n === 0 ? 0 : a * (yPrev + xn - xPrev);
46 out[n] = y;
47 yPrev = y;
48 xPrev = xn;
49 }
50 return out;
53export function rms(x: Float32Array): number {
54 let s = 0;
55 for (const v of x) s += v * v;
56 return Math.sqrt(s / Math.max(1, x.length));
59export function peakAbs(x: Float32Array): number {
60 let m = 0;
61 for (const v of x) m = Math.max(m, Math.abs(v));
62 return m;
65// 16-bit mono WAV.
66export function encodeWav(x: Float32Array, fs: number): Blob {
67 const n = x.length;
68 const buf = new ArrayBuffer(44 + 2 * n);
69 const v = new DataView(buf);
70 const str = (o: number, s: string) => [...s].forEach((c, k) => v.setUint8(o + k, c.charCodeAt(0)));
71 str(0, "RIFF");
72 v.setUint32(4, 36 + 2 * n, true);
73 str(8, "WAVE");
74 str(12, "fmt ");
75 v.setUint32(16, 16, true);
76 v.setUint16(20, 1, true);
77 v.setUint16(22, 1, true);
78 v.setUint32(24, fs, true);
79 v.setUint32(28, 2 * fs, true);
80 v.setUint16(32, 2, true);
81 v.setUint16(34, 16, true);
82 str(36, "data");
83 v.setUint32(40, 2 * n, true);
84 for (let k = 0; k < n; k++) v.setInt16(44 + 2 * k, Math.round(Math.max(-1, Math.min(1, x[k])) * 32767), true);
85 return new Blob([buf], { type: "audio/wav" });
88// In-place radix-2 FFT.
89function fft(re: Float64Array, im: Float64Array) {
90 const n = re.length;
91 for (let i = 1, j = 0; i < n; i++) {
92 let bit = n >> 1;
93 for (; j & bit; bit >>= 1) j ^= bit;
94 j ^= bit;
95 if (i < j) {
96 [re[i], re[j]] = [re[j], re[i]];
97 [im[i], im[j]] = [im[j], im[i]];
98 }
99 }
100 for (let len = 2; len <= n; len <<= 1) {
101 const ang = (-2 * Math.PI) / len;
102 for (let i = 0; i < n; i += len) {
103 for (let k = 0; k < len / 2; k++) {
104 const wr = Math.cos(ang * k);
105 const wi = Math.sin(ang * k);
106 const ar = re[i + k + len / 2];
107 const ai = im[i + k + len / 2];
108 const tr = ar * wr - ai * wi;
109 const ti = ar * wi + ai * wr;
110 re[i + k + len / 2] = re[i + k] - tr;
111 im[i + k + len / 2] = im[i + k] - ti;
112 re[i + k] += tr;
113 im[i + k] += ti;
114 }
115 }
116 }
119// Magnitude spectrum (dB, arbitrary reference) of the last up-to-2^16 samples, Hann windowed.
120export function spectrum(x: Float32Array, fs: number): { freq: Float64Array; db: Float64Array } {
121 let n = 1;
122 while (n * 2 <= Math.min(x.length, 1 << 16)) n *= 2;
123 const re = new Float64Array(n);
124 const im = new Float64Array(n);
125 const off = x.length - n;
126 for (let k = 0; k < n; k++) re[k] = x[off + k] * (0.5 - 0.5 * Math.cos((2 * Math.PI * k) / (n - 1)));
127 fft(re, im);
128 const m = n / 2;
129 const freq = new Float64Array(m);
130 const db = new Float64Array(m);
131 for (let k = 0; k < m; k++) {
132 freq[k] = (k * fs) / n;
133 db[k] = 10 * Math.log10(re[k] * re[k] + im[k] * im[k] + 1e-30);
134 }
135 return { freq, db };
↑↓ move↵openescclose