本文介绍了JavaScript:UInt8Array到Float32Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些未签名的8位PCM格式的音频缓冲区,需要通过网络音频播放,该音频仅接受签名的32位PCM.现在我有了用于pcm_u8数据片段的ArrayBuffer(来自Uint8array).如何将其转换为Float32Array?
I have some audio buffer in usigned 8bit PCM format need to play via web audio which only accept signed 32bit PCM. And now I have ArrayBuffer for pieces of pcm_u8 data(come from Uint8array). How can I convert it to Float32Array?
推荐答案
此函数将ArrayBuffer转换为Float32Array
This function converts an ArrayBuffer to Float32Array
var convertBlock(buffer) { // incoming data is an ArrayBuffer
var incomingData = new Uint8Array(buffer); // create a uint8 view on the ArrayBuffer
var i, l = incomingData.length; // length, we need this for the loop
var outputData = new Float32Array(incomingData.length); // create the Float32Array for output
for (i = 0; i < l; i++) {
outputData[i] = (incomingData[i] - 128) / 128.0; // convert audio to float
}
return outputData; // return the Float32Array
}
这将Uint8Array转换为Float32Array
This one converts a Uint8Array to Float32Array
var convertBlock(incomingData) { // incoming data is a UInt8Array
var i, l = incomingData.length;
var outputData = new Float32Array(incomingData.length);
for (i = 0; i < l; i++) {
outputData[i] = (incomingData[i] - 128) / 128.0;
}
return outputData;
}
这篇关于JavaScript:UInt8Array到Float32Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!