d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 1// Minimal WAV encode/decode. Export is 32-bit float so samples round-trip exactly.
3export function encodeWavFloat32(samples: Float32Array, sampleRate: number): Blob {
4 const dataBytes = samples.length * 4;
5 const buf = new ArrayBuffer(44 + dataBytes);
6 const v = new DataView(buf);
7 const str = (o: number, s: string) => {
8 for (let i = 0; i < s.length; i++) v.setUint8(o + i, s.charCodeAt(i));
9 };
10 str(0, "RIFF");
11 v.setUint32(4, 36 + dataBytes, true);
12 str(8, "WAVE");
13 str(12, "fmt ");
14 v.setUint32(16, 16, true);
15 v.setUint16(20, 3, true); // IEEE float
16 v.setUint16(22, 1, true); // mono
17 v.setUint32(24, sampleRate, true);
18 v.setUint32(28, sampleRate * 4, true);
19 v.setUint16(32, 4, true);
20 v.setUint16(34, 32, true);
21 str(36, "data");
22 v.setUint32(40, dataBytes, true);
23 new Float32Array(buf, 44).set(samples);
24 return new Blob([buf], { type: "audio/wav" });
25}
27export interface DecodedWav {
28 sampleRate: number;
29 channels: number;
30 samples: Float32Array; // first channel only
31}
33// Parses PCM (16/24/32-bit int) and IEEE float (32/64-bit) WAV files without resampling.
34// Returns null for anything else.
35export function decodeWav(buffer: ArrayBuffer): DecodedWav | null {
36 const v = new DataView(buffer);
37 const tag = (o: number) =>
38 String.fromCharCode(v.getUint8(o), v.getUint8(o + 1), v.getUint8(o + 2), v.getUint8(o + 3));
39 if (buffer.byteLength < 12 || tag(0) !== "RIFF" || tag(8) !== "WAVE") return null;
40 let off = 12;
41 let format = 0;
42 let channels = 0;
43 let sampleRate = 0;
44 let bits = 0;
45 let dataOff = -1;
46 let dataLen = 0;
47 while (off + 8 <= buffer.byteLength) {
48 const id = tag(off);
49 const len = v.getUint32(off + 4, true);
50 const body = off + 8;
51 if (id === "fmt ") {
52 format = v.getUint16(body, true);
53 channels = v.getUint16(body + 2, true);
54 sampleRate = v.getUint32(body + 4, true);
55 bits = v.getUint16(body + 14, true);
56 if (format === 0xfffe && len >= 26) format = v.getUint16(body + 24, true); // extensible
57 } else if (id === "data") {
58 dataOff = body;
59 dataLen = Math.min(len, buffer.byteLength - body);
60 break;
61 }
62 off = body + len + (len % 2);
63 }
64 if (dataOff < 0 || channels < 1 || !sampleRate) return null;
65 const bytes = bits / 8;
66 const frameBytes = bytes * channels;
67 const n = Math.floor(dataLen / frameBytes);
68 const out = new Float32Array(n);
69 for (let i = 0; i < n; i++) {
70 const p = dataOff + i * frameBytes;
71 let x: number;
72 if (format === 3 && bits === 32) x = v.getFloat32(p, true);
73 else if (format === 3 && bits === 64) x = v.getFloat64(p, true);
74 else if (format === 1 && bits === 16) x = v.getInt16(p, true) / 32768;
75 else if (format === 1 && bits === 24) {
76 let s = v.getUint8(p) | (v.getUint8(p + 1) << 8) | (v.getUint8(p + 2) << 16);
77 if (s & 0x800000) s -= 0x1000000;
78 x = s / 8388608;
79 } else if (format === 1 && bits === 32) x = v.getInt32(p, true) / 2147483648;
80 else return null;
81 out[i] = x;
82 }
83 return { sampleRate, channels, samples: out };
84}