Skip to content

Commit 5226278

Browse files
committed
upload save wav file logic
1 parent cf46d5a commit 5226278

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

app/lib/audio.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ export class AudioHandler {
131131
_saveData(data: Int16Array, bytesPerSample = 16): Blob {
132132
const headerLength = 44;
133133
const numberOfChannels = 1;
134-
const dataLength = data.length;
135-
const wav = new Uint8Array(headerLength + dataLength * 2);
136-
const view = new DataView(wav.buffer);
134+
const byteLength = data.buffer.byteLength;
135+
const header = new Uint8Array(headerLength);
136+
const view = new DataView(header.buffer);
137137
view.setUint32(0, 1380533830, false); // RIFF identifier 'RIFF'
138-
view.setUint32(4, 36 + dataLength * 2, true); // file length minus RIFF identifier length and file description length
138+
view.setUint32(4, 36 + byteLength, true); // file length minus RIFF identifier length and file description length
139139
view.setUint32(8, 1463899717, false); // RIFF type 'WAVE'
140140
view.setUint32(12, 1718449184, false); // format chunk identifier 'fmt '
141141
view.setUint32(16, 16, true); // format chunk length
@@ -146,14 +146,13 @@ export class AudioHandler {
146146
view.setUint16(32, numberOfChannels * 2, true); // block align (channel count * bytes per sample)
147147
view.setUint16(34, bytesPerSample, true); // bits per sample
148148
view.setUint32(36, 1684108385, false); // data chunk identifier 'data'
149-
view.setUint32(40, dataLength * 2, true); // data chunk length
150-
for (let i = 0; i < dataLength; i++) {
151-
view.setInt16(44 + i * 2, data[i], true);
152-
}
153-
return new Blob([view], { type: "audio/mpeg" });
149+
view.setUint32(40, byteLength, true); // data chunk length
150+
151+
// using data.buffer, so no need to setUint16 to view.
152+
return new Blob([view, data.buffer], { type: "audio/mpeg" });
154153
}
155154
savePlayFile() {
156-
return this._saveData(this.playBuffer);
155+
return this._saveData(new Int16Array(this.playBuffer));
157156
}
158157
async close() {
159158
this.workletNode?.disconnect();

0 commit comments

Comments
 (0)