Convert aws polly synthesizeSpeech response to twilio mulaw format in NodeJs

180 Views Asked by At

I have a Twilio bi-directional stream running, so when I get the Twilio payload I process them and respond back, while responding back I am using aws-sdk Polly service to get the text-to-speech data, which is of type audio/PCM in a signed 16-bit, mono little-endian format. Now I want to send this response to the Twilio stream Challenges: How do I make this Polly response to Twilio compatible? Unable to convert Pooly response to audio/mu-law format

with the posted snippet all I am hearing is some random noise.

NodeJs is my scripting platform.

Below is the piece of code that converts the Polly service response to mu-law format

try {
      const data = await this.polly.synthesizeSpeech(params).promise();
      const audioData = Buffer.from(data.AudioStream as Uint8Array);
      const wav = new WaveFile();
      wav.fromScratch(1, 8000, '8', audioData);
      wav.toMuLaw();
      wav.toBitDepth('8');
      // @ts-ignore
      const payload = Buffer.from(wav.data.samples).toString('base64');
      return payload;
}

Can you explain to me the things that I have to do here? (new to this audio formats stuff).

I want to know Do I have to strip off the headers? if Yes how many bytes is it?

How do I get the caller to hear the real voice?

0

There are 0 best solutions below