Skip to content

Commit

Permalink
Correct convert implement
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Dec 24, 2023
1 parent f153bd1 commit 3436f12
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions utils/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ export interface AudioData {

export const encodeBuffer = (audio: AudioData): Buffer => {
const { data, sampleRate, channels } = audio;
const lenPerChannel = data.length / channels;
const samples = [];
for (let i = 0; i < data.length; i += channels) {
samples.push(data.slice(i, i + channels));
if (channels === 1) {
samples.push(data);
} else {
for (let c = 0; c < channels; c++) {
const sample = new Float32Array(lenPerChannel);
for (let i = 0; i < lenPerChannel; i++) {
sample[i] = data[i * channels + c];
}
samples.push(sample);
}
}
return wav.encode(samples, { sampleRate });
}
Expand Down

0 comments on commit 3436f12

Please sign in to comment.