我正在创建一个复杂的节拍器应用程序使用离子和网络音频API。
在某些情况下,节拍器可能正在播放10+的“节拍”。
这实际上是每秒调用该函数10 times+。
function playSound(e, name) {
var buffer = audioBuffers[name];
var source = audioContext.createBufferSource();
var gain = audioContext.createGain();
source.connect(gain);
gain.connect(audioContext.destination);
gain.gain.value = 1;
source.buffer = buffer;
source.connect(audioContext.destination);
sched.nextTick(e.playbackTime, () => {
source.start(0);
});
}
用户可以选择多个样本,所以我首先获取所有样本,并将缓冲区存储在一个数组中,以提高性能,而不是每次都执行XMLHttpRequest()。
问题是,当以这些较高的速率播放时,播放会变得奇怪,有时会不同步。我使用的是https://github.com/mohayonao/web-audio-scheduler,它工作起来很可爱,所以我知道这不是一个时间问题。
如果我将样本回放替换为基本振荡器:
function oscillator(e) {
const t0 = e.playbackTime;
const t1 = t0 + 0.4;
const osc = audioContext.createOscillator();
const amp = audioContext.createGain();
osc.frequency.value = 1000;
osc.start(t0);
osc.stop(t1);
osc.connect(amp);
amp.gain.setValueAtTime(1, t0);
amp.gain.exponentialRampToValueAtTime(1e-6, t1);
amp.connect(masterGain);
sched.nextTick(t1, () => {
osc.disconnect();
amp.disconnect();
});
}
无论是什么节拍,性能都很好。是否可以对示例播放进行任何改进以帮助提高性能?
发布于 2017-04-27 00:43:51
您的第一个函数仅使用source.start(0);这使我认为您依赖setTimeout或setInterval来“调度”音频。第二个正确地使用了网络音频调度程序("start(t0)")。有关更多信息,请参阅“两个时钟的故事”:https://www.html5rocks.com/en/tutorials/audio/scheduling/。
发布于 2017-05-31 20:16:05
cwilso所说的是对的。使用AudioContext.CurrentTime和+/-手动确定setTimeout的下一次时间,而不是使用该调度器库。那么一切都会好起来的。
https://stackoverflow.com/questions/43639641
复制相似问题