I am working on a Node.js application where I need to convert audio data, received in chunks, into a WAV format. The audio data consists of an array of strings, with each string representing a chunk of audio data. The conversion works fine for most chunks, but I encounter distortion specifically for chunks that start with “data:audio/webm;codecs=opus;base64”. I am using a function that handles the conversion, but the output for these chunks is not as expected, resulting in distorted audio.
I have tried using the provided function to convert the audio data to WAV format. For chunks that do not start with “data:audio/webm;codecs=opus;base64”, the conversion works correctly, producing clear audio output. However, for chunks that start with “data:audio/webm;codecs=opus;base64”, the resulting audio is distorted. I have attempted to handle these chunks differently in the conversion function by decoding the base64 data and writing it to a WAV file using the ‘wav’ module in Node.js. However, this approach has not resolved the distortion issue.
async sendAudioDataToWAVConverter(audioData, streamSid){
return new Promise((resolve, reject) => {
const outputPath = `./audio_files/${streamSid}/output_${Date.now()}.wav`
const writer = new wav.FileWriter(outputPath, {
channels: 1,
sampleRate: 8000,
bitDepth: 16,
});
writer.on('error', (err) => {
console.error('WAV FileWriter Error: ', err);
reject(err);
})
audioData.forEach((item) => {
if (item.event === 'media' && item.media && item.media.payload) {
const audioDataa = Buffer.from(item.media.payload, 'base64');
writer.write(audioDataa);
} else if(item.startsWith('data:audio/webm;codecs=opus;base64,')){
const base64Data = item.split(';base64,')[1];
const audioDataa = Buffer.from(base64Data, 'base64');
fs.writeFileSync('./audio.wav', audioDataa)
writer.write(audioDataa);
} else{
const audioDataa = Buffer.from(item, 'base64');
writer.write(audioDataa);
}
})
writer.end(() => {
resolve(outputPath);
});
});
},