// Turning the listener's recording (one sample per lattice step) into audio: // band-limited resampling to 48 kHz, a 20 Hz high-pass, a WAV encoder, and a magnitude spectrum. export const AUDIO_RATE = 48000; // Windowed-sinc resampling from fsIn to fsOut. The low-pass cutoff is 0.45 * min(fsIn, fsOut), // so this works for both down- and upsampling. Kernel: 16 zero crossings each side, Blackman window. export function resample(src: Float32Array, fsIn: number, fsOut: number): Float32Array { const fc = 0.45 * Math.min(fsIn, fsOut); const zc = fsIn / (2 * fc); // input samples per zero crossing const half = 16 * zc; const nOut = Math.floor(((src.length - 1) * fsOut) / fsIn) + 1; const out = new Float32Array(nOut); const gain = (2 * fc) / fsIn; for (let n = 0; n < nOut; n++) { const t = (n * fsIn) / fsOut; const k0 = Math.max(0, Math.ceil(t - half)); const k1 = Math.min(src.length - 1, Math.floor(t + half)); let acc = 0; for (let k = k0; k <= k1; k++) { const tau = k - t; const x = tau / zc; // in zero crossings const sinc = x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x); const w = 0.42 + 0.5 * Math.cos((Math.PI * tau) / half) + 0.08 * Math.cos((2 * Math.PI * tau) / half); acc += src[k] * sinc * w; } out[n] = acc * gain; } return out; } // One-pole high-pass (after removing the mean), to drop the slow mean-pressure drift. export function highPass(x: Float32Array, fs: number, fc: number): Float32Array { const out = new Float32Array(x.length); if (x.length === 0) return out; let mean = 0; for (const v of x) mean += v; mean /= x.length; const rc = 1 / (2 * Math.PI * fc); const a = rc / (rc + 1 / fs); let yPrev = 0; let xPrev = x[0] - mean; for (let n = 0; n < x.length; n++) { const xn = x[n] - mean; const y = n === 0 ? 0 : a * (yPrev + xn - xPrev); out[n] = y; yPrev = y; xPrev = xn; } return out; } export function rms(x: Float32Array): number { let s = 0; for (const v of x) s += v * v; return Math.sqrt(s / Math.max(1, x.length)); } export function peakAbs(x: Float32Array): number { let m = 0; for (const v of x) m = Math.max(m, Math.abs(v)); return m; } // 16-bit mono WAV. export function encodeWav(x: Float32Array, fs: number): Blob { const n = x.length; const buf = new ArrayBuffer(44 + 2 * n); const v = new DataView(buf); const str = (o: number, s: string) => [...s].forEach((c, k) => v.setUint8(o + k, c.charCodeAt(0))); str(0, "RIFF"); v.setUint32(4, 36 + 2 * n, true); str(8, "WAVE"); str(12, "fmt "); v.setUint32(16, 16, true); v.setUint16(20, 1, true); v.setUint16(22, 1, true); v.setUint32(24, fs, true); v.setUint32(28, 2 * fs, true); v.setUint16(32, 2, true); v.setUint16(34, 16, true); str(36, "data"); v.setUint32(40, 2 * n, true); for (let k = 0; k < n; k++) v.setInt16(44 + 2 * k, Math.round(Math.max(-1, Math.min(1, x[k])) * 32767), true); return new Blob([buf], { type: "audio/wav" }); } // In-place radix-2 FFT. function fft(re: Float64Array, im: Float64Array) { const n = re.length; for (let i = 1, j = 0; i < n; i++) { let bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) { [re[i], re[j]] = [re[j], re[i]]; [im[i], im[j]] = [im[j], im[i]]; } } for (let len = 2; len <= n; len <<= 1) { const ang = (-2 * Math.PI) / len; for (let i = 0; i < n; i += len) { for (let k = 0; k < len / 2; k++) { const wr = Math.cos(ang * k); const wi = Math.sin(ang * k); const ar = re[i + k + len / 2]; const ai = im[i + k + len / 2]; const tr = ar * wr - ai * wi; const ti = ar * wi + ai * wr; re[i + k + len / 2] = re[i + k] - tr; im[i + k + len / 2] = im[i + k] - ti; re[i + k] += tr; im[i + k] += ti; } } } } // Magnitude spectrum (dB, arbitrary reference) of the last up-to-2^16 samples, Hann windowed. export function spectrum(x: Float32Array, fs: number): { freq: Float64Array; db: Float64Array } { let n = 1; while (n * 2 <= Math.min(x.length, 1 << 16)) n *= 2; const re = new Float64Array(n); const im = new Float64Array(n); const off = x.length - n; for (let k = 0; k < n; k++) re[k] = x[off + k] * (0.5 - 0.5 * Math.cos((2 * Math.PI * k) / (n - 1))); fft(re, im); const m = n / 2; const freq = new Float64Array(m); const db = new Float64Array(m); for (let k = 0; k < m; k++) { freq[k] = (k * fs) / n; db[k] = 10 * Math.log10(re[k] * re[k] + im[k] * im[k] + 1e-30); } return { freq, db }; }