1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| function encodeWAV(samples) { var buffer = new ArrayBuffer(44 + samples.length * 2); var view = new DataView(buffer);
numChannels = 2;
let bitPerSample = 16, blockAlign = numChannels * (bitPerSample / 8);
writeString(view, 0, 'RIFF'); view.setUint32(4, 36 + samples.length * 2, true); writeString(view, 8, 'WAVE'); writeString(view, 12, 'fmt '); view.setUint32(16, 16, true); view.setUint16(20, 1, true); view.setUint16(22, numChannels, true); view.setUint32(24, newSampleRate, true); view.setUint32(28, newSampleRate * blockAlign, true); view.setUint16(32, blockAlign, true); view.setUint16(34, bitPerSample, true); writeString(view, 36, 'data'); view.setUint32(40, samples.length * 2, true);
floatTo16BitPCM(view, 44, samples);
return view; } }, self);
|