javascript - Web Audio API - How do I save the audio buffer to a file including all changes? - Stack Overflow

I made changes to an audio buffer like gain and panning, connected them to an audio context.Now I want

I made changes to an audio buffer like gain and panning, connected them to an audio context. Now I want to save to a file with all the implemented changes. Saving the buffer as is would give me the original audio without the changes.

Any idea of a method or a procedure existed to do that?

I made changes to an audio buffer like gain and panning, connected them to an audio context. Now I want to save to a file with all the implemented changes. Saving the buffer as is would give me the original audio without the changes.

Any idea of a method or a procedure existed to do that?

Share Improve this question asked Mar 22, 2021 at 1:46 ShemShem 4184 silver badges8 bronze badges 1
  • making changes to buffer as you say is very similar to connecting the audio context to a file sink after running it thru an encoder, see the example saving encoded web audio to a file : github./GersonRosales/… – Robert Rowntree Commented Mar 22, 2021 at 15:22
Add a ment  | 

2 Answers 2

Reset to default 3

On way is to use a MediaRecorder to save the modified audio.

So, in addition to connecting to the destination, connect to a MediaStreamDestinationNode. This node has a stream object that you can use to initialize a MediaRecorder. Set up the recorder to save the data when data is available. When you're down recording, you have a blob that you can then download.

Many details are missing here, but you can find out how to use a MediaRecorder using the MDN example.

I found a solution, with OfflineAudioContext. Here is an example with adding a gain change to my audio and saving it. On the last line of the code I get the array buffer with the changes I made. From there, I can go on saving the file.

let offlineCtx = new OfflineAudioContext(this.bufferNode.buffer.numberOfChannels, this.bufferNode.buffer.length, this.bufferNode.buffer.sampleRate);
let obs = offlineCtx.createBufferSource();
obs.buffer = this.buffer;
let gain = offlineCtx.createGain();
gain.gain.value = this.gain.gain.value;
obs.connect(gain).connect(offlineCtx.destination);
obs.start();
let obsRES = this.ctx.createBufferSource();
await offlineCtx.startRendering().then(r => {
  obsRES.buffer = r;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745394660a4625822.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信