问题描述
所以我从 AudioBuffer
中提取了通道数据,并通过可传输对象将其发送给网络工作者对其进行一些处理,现在我想把它放回去.做我真的必须像这样复制它吗?
So I've pulled the channel data out of an AudioBuffer
and sent it via transferable object to a web worker to do some processing on it, and now I want to put it back in. Do I really have to copy it back in like this?
var myData = new Float32Array(audioBuf.length);
var chanData = audioBuf.getChannelData(0);
for ( var n = 0; n < chanData.length; n++ ) {
chanData[n] = myData[n];
}
我真的希望有一些方法可以改变每个 AudioBuffer
通道引用的 ArrayBuffer
.像……
I'm really hoping there is some way to just change out the ArrayBuffer
each of the AudioBuffer
channels reference. Something like...
audioBuf.channel[0].buffer = myData.buffer;
...会非常简单有效,但似乎不存在.有没有办法像这样更改引用并避免复制数据?
...would be wonderfully simple and effective, but seemingly does not exist. Is there any way at all like this to change the reference and avoid copying the data?
通过进一步调查,我发现将网络音频 API 与可传输对象一起使用的问题更加烦人.当您将数组缓冲区传输到工作程序时,AudioBuffer
的底层数组缓冲区被清除,我相信通过 getChannelData 返回的
不可能.我现在能看到的完成我想要的唯一方法是放弃原来的 Float32Array
甚至可以进行复制操作AudioBuffer
,创建一个全新的 AudioBuffer
,然后将我的数据复制到其中.真的吗??
With a little further investigation, I see the problem of using the web audio API with transferable objects is even more annoying. When you transfer the array buffers to the worker, the underlying array buffers of the AudioBuffer
are cleared, I believe making even a copy operation through the Float32Array
returned by getChannelData
impossible. The only way I can see to accomplish what I want right now is to abandon the original AudioBuffer
, create an entirely new AudioBuffer
and then copy my data into it. Really??
推荐答案
我一整天都在为此苦苦挣扎,所以我想我应该分享我的解决方案.
I struggled with this all day, so I figured I should share my solution.
var PCM = [0,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1];
var room = new window.AudioContext();
var sampleRate = 44100;
var channels = 1;
var audioBuffer = room.createBuffer(channels, 0, sampleRate);
var channelObject = audioBuffer.getChannelData(0);
var audioBufferDataArray = channelObject.data;
// actual code to set the data
audioBuffer.length = PCM.length; // optional
for(var i=0; i<PCM.length; i++){
audioBufferDataArray[i] = PCM[i];
}
这篇关于手动将 pcm 数据放入 AudioBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!