Manually insert pcm data into AudioBuffer

So, I pulled the channel data from AudioBufferand sent it through the portable object to the web worker to do some processing on it, and now I want to get it back. Do I really need to copy it 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];
}

I really hope that there is some way to simply change ArrayBuffereach of the feed links AudioBuffer. Sort of...

audioBuf.channel[0].buffer = myData.buffer;

... would be surprisingly simple and effective, but doesn't seem to exist. Is there any way to change the link and avoid copying the data?

The EDIT: . With a little further research, I see that the problem of using the Web Audio API with portable objects is even more annoying. When you transfer the buffers of the array to the working one, the buffers of the base array AudioBufferare cleared, I find it impossible to make the copy operation through the Float32Arrayreturned getChannelDataimpossible. The only way I can accomplish what I want now is to abandon the original AudioBuffer, create a completely new one AudioBuffer, and then copy my data into it. Really??

+3
source share
4 answers

AudioBuffer, copyFromChannel. , ( ArrayBuffer AudioBuffer), , ( , ).

, Firefox , .

+2

, , .

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];
}
+2

:

1) , , .

var copy = new Float32Array(orig.length);
copy.set(orig);

2) AudioBuffer - - (https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers#Passing_data_by_transferring_ownership_(transferable_objects)). . , , .

, AudioBuffer . . 3 . , , , -.

In any case, if you could give more detailed information about your application and what it does, I would be happy to make some recommendations.

+1
source

You were so close! You want to AudioBuffer.copyToChannel().

This will copy your nodeFloat32Array sound buffer as follows:

var myData = new Float32Array(audioBuf.length);

//copy mydata to first channel

audioBuf.copyToChannel( myData , 0 , 0 );

Deep in the docs:

https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer/copyToChannel

Hope this helps

0
source

All Articles