Reputation: 57
I'm coding a Twilio auto answer bot. I can receive the stream from twilio and get the content using msg.media.payload in the "media" event of the socket. But I cannot convert the base64 mulaw to 16 bit pcm array using NodeJS.
function basetoPCM(data){
console.log(data)
var blob = atob(data); // Base64 string converted to a char array
console.log("use base64 decoder to decode the Payload received.");
console.log(blob);
//use this payload buffer and decode using the G.711 decoder (mulaw to pcm)
var pcm = alawmulaw.mulaw.decode(blob);
console.log("getting decode pcm");
console.log(pcm);
return pcm;
}
But the response it get it just an array with same number:
getting decode pcm
Int16Array(160) [
-32124, -32124, -32124, -32124, -32124, -32124, -32124, ...
The function to send audio to the Kaldi transcribe server that I use is:
const buffer = e;
const int16ArrayData = convertFloat32ToInt16(buffer);
console.log("int16data");
console.log(int16ArrayData)
this.ws.send(int16ArrayData.buffer);
So my question is how to convert Twilio base64 mulaw to a 16 bit PCM audio buffer. And is there anyway to make Twilio encode in PCM? Thank you!
Upvotes: 0
Views: 2217
Reputation: 21
console.log("Receiving audio...");
const WaveFile = require('wavefile').WaveFile;
const payload = msg.media.payload;
const wav = new WaveFile();
wav.fromScratch(1, 8000, "8m", Buffer.from(payload, "base64"));
wav.fromMuLaw();
wav.toSampleRate(16000);
const results = wav.toBuffer();
const samples = wav.data.samples;
fs.writeFileSync('./stream.wav', results);
Upvotes: 1
Reputation: 73057
Twilio developer evangelist here.
I've not used the alawmulaw library you seem to be trying here, but looking at the docs it appears you might be running the wrong function here. As the live web socket data arrives, I would have thought it was a "sample" rather than a set of samples and that you should try:
var pcm = alawmulaw.mulaw.decodeSample(blob);
Alternatively, in this answer my colleague Craig recommended the WaveFile lib. Take a look at the answer for some sample code.
Upvotes: 1