admin管理员组文章数量:1302403
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:
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:
Share edited Dec 15, 2015 at 20:32 thanksd 55.7k23 gold badges165 silver badges154 bronze badges asked Nov 16, 2015 at 15:27 carduhcarduh 5291 gold badge4 silver badges15 bronze badges 3- You need to show more of what you're doing. Also, WebAudio expects the data to be in the range -1 to 1, so I don't understand why you want to convert your float samples to int16's. – Raymond Toy Commented Nov 16, 2015 at 16:58
- I suppose it might have something to do with the fact that PCMs require Int16s. – icedwater Commented Nov 27, 2015 at 2:12
- 16-bit PCM (LINEAR16) is often used as input for audio processing pipelines (speech-to-text, voice activity detection, speaker labelling, etc). – bfla Commented Apr 29, 2022 at 19:31
2 Answers
Reset to default 5Replacing 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.
This is working for me:
const int16Array = Int16Array.from(buffer, x => x * 32767);
本文标签: Float32 to Int16Javascript (Web Audio API)Stack Overflow
版权声明:本文标题:Float32 to Int16 - Javascript (Web Audio API) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741666694a2391352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论