Float32 to Int16 - Javascript (Web Audio API)

穿精又带淫゛_ 提交于 2019-12-07 03:50:25

问题


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:

function convertoFloat32ToInt16(buffer) {
  var l = buffer.length;  //Buffer
  var buf = new Int16Array(l/3);

  while (l--) {
    if (l==-1) break;

    if (buffer[l]*0xFFFF > 32767)
      buf[l] = 32767;
    elseif (buffer[l]*0xFFFF < -32768)
      buf[l] = -32768;
    else 
      buf[l] = buffer[l]*0xFFFF;
  }
  return buf.buffer;
}

If I implement the gainNode() 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:


回答1:


Replacing the while by this, is the solution:

while (l--) {
    s = Math.max(-1, Math.min(1, samples[l]));
    buf[l] = s < 0 ? s * 0x8000 : s * 0x7FFF;
    //buf[l] = buffer[l]*0xFFFF; //old   //convert to 16 bit
  }
}

Now, the records sounds perfect and the Matlab plots to.



来源:https://stackoverflow.com/questions/33738873/float32-to-int16-javascript-web-audio-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!