Decoder: streaming APT.Decoder (coherent PLL or envelope demod, sync A+B tracking loop with coasting and timing-jump re-lock), telemetry analysis, Q-gated impulse (FM click) repair. GUI with fast / realtime decode, Listen, live input, telemetry table, markers. Encoder: APT.Encoder with test impairments (noise, clicks, clock offset, fading, line-timing jump); encoder dialog that feeds the decoder. Tools: offline decode/encode, quality survey, stub-DOM GUI smoke test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
3.3 KiB
JavaScript
104 lines
3.3 KiB
JavaScript
/*
|
|
* wav.js - minimal RIFF/WAVE reader and writer (PCM 8/16/24/32, float 32/64,
|
|
* WAVE_FORMAT_EXTENSIBLE). Parse() returns { rate, channels, data } with data
|
|
* the first channel as Float32Array in -1..1, so the decoder sees the file's
|
|
* native rate instead of a browser-resampled copy.
|
|
*/
|
|
(function (root) {
|
|
'use strict';
|
|
|
|
function Parse(buffer)
|
|
{
|
|
const u8 = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
const tag = (o) => String.fromCharCode(u8[o], u8[o + 1], u8[o + 2], u8[o + 3]);
|
|
if (u8.length < 12 || tag(0) !== 'RIFF' || tag(8) !== 'WAVE')
|
|
throw new Error('not a RIFF/WAVE file');
|
|
|
|
let fmt = null, dataOff = -1, dataLen = 0;
|
|
let o = 12;
|
|
while (o + 8 <= u8.length) {
|
|
const id = tag(o);
|
|
let len = dv.getUint32(o + 4, true);
|
|
const body = o + 8;
|
|
if (id === 'fmt ') {
|
|
fmt = {
|
|
format: dv.getUint16(body, true),
|
|
channels: dv.getUint16(body + 2, true),
|
|
rate: dv.getUint32(body + 4, true),
|
|
align: dv.getUint16(body + 12, true),
|
|
bits: dv.getUint16(body + 14, true),
|
|
};
|
|
if (fmt.format === 0xfffe && len >= 26)
|
|
fmt.format = dv.getUint16(body + 24, true);
|
|
} else if (id === 'data') {
|
|
dataOff = body;
|
|
/* streamed / truncated recordings: clamp to the file */
|
|
if (len === 0 || len === 0xffffffff || body + len > u8.length)
|
|
len = u8.length - body;
|
|
dataLen = len;
|
|
break;
|
|
}
|
|
o = body + len + (len & 1);
|
|
}
|
|
if (!fmt || dataOff < 0)
|
|
throw new Error('WAVE file without fmt/data chunk');
|
|
|
|
const bps = fmt.bits >> 3;
|
|
const align = fmt.align || bps * fmt.channels;
|
|
const n = Math.floor(dataLen / align);
|
|
const data = new Float32Array(n);
|
|
for (let i = 0; i < n; i++) {
|
|
const p = dataOff + i * align;
|
|
let v;
|
|
if (fmt.format === 3)
|
|
v = bps === 8 ? dv.getFloat64(p, true) : dv.getFloat32(p, true);
|
|
else if (bps === 1)
|
|
v = (u8[p] - 128) / 128;
|
|
else if (bps === 2)
|
|
v = dv.getInt16(p, true) / 32768;
|
|
else if (bps === 3)
|
|
v = ((u8[p] | (u8[p + 1] << 8) | (u8[p + 2] << 16)) << 8 >> 8) / 8388608;
|
|
else if (bps === 4)
|
|
v = dv.getInt32(p, true) / 2147483648;
|
|
else
|
|
throw new Error('unsupported sample size ' + fmt.bits);
|
|
data[i] = v;
|
|
}
|
|
return { rate: fmt.rate, channels: fmt.channels, data: data };
|
|
}
|
|
|
|
/* Mono 16-bit PCM WAV from Float32Array samples in -1..1 */
|
|
function Build(samples, rate)
|
|
{
|
|
const n = samples.length;
|
|
const out = new Uint8Array(44 + n * 2);
|
|
const dv = new DataView(out.buffer);
|
|
const put = (o, s) => { for (let i = 0; i < 4; i++) out[o + i] = s.charCodeAt(i); };
|
|
put(0, 'RIFF');
|
|
dv.setUint32(4, 36 + n * 2, true);
|
|
put(8, 'WAVE');
|
|
put(12, 'fmt ');
|
|
dv.setUint32(16, 16, true);
|
|
dv.setUint16(20, 1, true);
|
|
dv.setUint16(22, 1, true);
|
|
dv.setUint32(24, rate, true);
|
|
dv.setUint32(28, rate * 2, true);
|
|
dv.setUint16(32, 2, true);
|
|
dv.setUint16(34, 16, true);
|
|
put(36, 'data');
|
|
dv.setUint32(40, n * 2, true);
|
|
for (let i = 0; i < n; i++) {
|
|
const v = Math.max(-1, Math.min(1, samples[i]));
|
|
dv.setInt16(44 + i * 2, Math.round(v * 32767), true);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const Wav = { Parse, Build };
|
|
if (typeof module !== 'undefined' && module.exports)
|
|
module.exports = Wav;
|
|
else
|
|
root.Wav = Wav;
|
|
})(typeof window !== 'undefined' ? window : globalThis);
|