title: Float32 to Int16 - Javascript (Web Audio API)
body: <p>I am trying to convert Float32 to Int16. But so far, is not effective. Because the output audio will generate lots of clippings (so, very poor audio output). I am using this function:</p>
  
  <pre><code>function convertoFloat32ToInt16(buffer) {
    var l = buffer.length;  //Buffer
    var buf = new Int16Array(l/3);
  
    while (l--) {
      if (l==-1) break;
  
      if (buffer[l]*0xFFFF &gt; 32767)
        buf[l] = 32767;
      elseif (buffer[l]*0xFFFF &lt; -32768)
        buf[l] = -32768;
      else 
        buf[l] = buffer[l]*0xFFFF;
    }
    return buf.buffer;
  }
  </code></pre>
  
  <p>If I implement the <a href="https://developer.mozilla.org/pt-PT/docs/Web/API/GainNode" rel="noreferrer">gainNode()</a> previously, the clipping effect is less perceptible. But is not a desirable way, because the purpose is to be effective in every microphones.  The clipping effect is visible in this Matlab plot:</p>
  
  <p><a href="https://i.stack.imgur.com/mn2aV.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/mn2aV.jpg" alt="clipping effect"></a></p>