title: Convert Twilio Mulaw to 16Khz PCM stream in Node JS
body: <p>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 &quot;media&quot; event of the socket. But I cannot convert the base64 mulaw to 16 bit pcm array using NodeJS.</p>
  <pre><code>function basetoPCM(data){
    console.log(data)
    var blob  = atob(data);   // Base64 string converted to a char array
    console.log(&quot;use base64 decoder to decode the Payload received.&quot;);
      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(&quot;getting decode pcm&quot;);
    console.log(pcm);
    return pcm;
  }
  </code></pre>
  <p>But the response it get it just an array with same number:</p>
  <pre><code>getting decode pcm
  Int16Array(160) [
    -32124, -32124, -32124, -32124, -32124, -32124, -32124, ...
  </code></pre>
  <p>The function to send audio to the Kaldi transcribe server that I use is:</p>
  <pre><code>const buffer = e;
        const int16ArrayData = convertFloat32ToInt16(buffer);
        console.log(&quot;int16data&quot;);
        console.log(int16ArrayData)
        this.ws.send(int16ArrayData.buffer);
  </code></pre>
  <p>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!</p>