|
|
|
|
|
export function encodeWAV(samples) { |
|
let offset = 44; |
|
const buffer = new ArrayBuffer(offset + samples.length * 4); |
|
const view = new DataView(buffer); |
|
const sampleRate = 16000; |
|
|
|
|
|
writeString(view, 0, 'RIFF') |
|
|
|
view.setUint32(4, 36 + samples.length * 4, true) |
|
|
|
writeString(view, 8, 'WAVE') |
|
|
|
writeString(view, 12, 'fmt ') |
|
|
|
view.setUint32(16, 16, true) |
|
|
|
view.setUint16(20, 3, true) |
|
|
|
view.setUint16(22, 1, true) |
|
|
|
view.setUint32(24, sampleRate, true) |
|
|
|
view.setUint32(28, sampleRate * 4, true) |
|
|
|
view.setUint16(32, 4, true) |
|
|
|
view.setUint16(34, 32, true) |
|
|
|
writeString(view, 36, 'data') |
|
|
|
view.setUint32(40, samples.length * 4, true) |
|
|
|
for (let i = 0; i < samples.length; ++i, offset += 4) { |
|
view.setFloat32(offset, samples[i], true) |
|
} |
|
|
|
return buffer |
|
} |
|
|
|
function writeString(view, offset, string) { |
|
for (let i = 0; i < string.length; ++i) { |
|
view.setUint8(offset + i, string.charCodeAt(i)) |
|
} |
|
} |