I'm reading the docs of https://www.twilio.com/docs/voice/twiml/stream#websocket-messages-to-twilio.
I'm currently using a TTS API that returns back raw audio/mpeg binary data. But in order to play this in a twilio media stream it has to be converted into the audio/x-mulaw with a sample rate of 8000 and base64 encoded format.
Here's the function I've written so far but anytime I try it, it just produces a strange scrambled hissing sound, that suggests the encoding is not correctly being done:
async function convertToMuLaw(rawAudioData) {
let wav = new WaveFile();
wav.fromScratch(1, 8000, "16", rawAudioData);
wav.toMuLaw();
const mulawEncoded = wav.toDataURI().split("base64,")[1];
return Buffer.from(mulawEncoded, "base64").toString("base64");
}
I've been stuck on this for days.