How to play multiple AudioBufferSourceNode synchronized?

被刻印的时光 ゝ 提交于 2021-01-28 03:56:03

问题


I have multiple audio files that must be played in sync. I have read that Web Audio API is the best solution for this. But, I can't find any document that shows how to achieve this.

Almost all articles I have read do this to start playback.

//Let's say I have AudioBufferSourceNode connected to two buffers
var source1, source2;

source1.start(0);
source2.start(0);

Shouldn't this cause source2 to start playing slightly later than source1?

Also, what makes the sources stay in sync? I can not find any mention in any documentation that assures that sources are played in sync.

Thanks.


回答1:


You can schedule them slightly in the future.

var source1, source2;
var when = context.currentTime + 0.01;

source1.start(when);
source2.start(when);

That'll schedule both sounds to play exactly 10ms from the moment you define when. It's quick enough that it'll be perceived as immediate, but gives a bit of breathing room for the overhead of actually calling start on the first source node.

There are better ways to do this if you have a ton of nodes, but for simple situations, this should be fine.




回答2:


There is a single clock for the audio context, and the buffer playback is on that clock - so yes, they will stay in sync.

Even calling start(0); start(0); as above will be perfectly synchronized, because start() is setting up a scheduling request on the audio thread, and the actual scheduling of both of those will happen together. "now" is actually slightly in the future (the audio system latency).



来源:https://stackoverflow.com/questions/19846107/how-to-play-multiple-audiobuffersourcenode-synchronized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!