您真的不能只这样调用source.start(audioContext.currentTime)。
setTimeout()的延迟时间长且不精确-
其他主线程可能会继续运行,因此setTimeout()调用可能会延迟几毫秒,甚至数十毫秒(通过垃圾回收,JS执行,布局…)。代码正试图在具有数十毫秒不精确度的计时器上立即播放音频-
该音频必须在大约0.02ms的精度内启动而不出现毛刺-。
Web音频系统的全部要点是音频调度程序在单独的高优先级线程中工作,并且您可以以非常高的精度预先调度音频(开始,停止和audioparam更改)。您应该将系统重写为:
1)跟踪在音频上下文时间安排第一个块的时间-不要立即安排第一个块,给它们一些延迟,以便您的网络有望跟上进度。
2)基于其“下一块”定时安排将来接收的每个连续块。
例如(请注意,我尚未测试此代码,这超出了我的头脑):
window.AudioContext = window.AudioContext || window.webkitAudioContext;var context = new AudioContext();var delayTime = 0;var init = 0;var audioStack = [];var nextTime = 0;client.on('stream', function(stream, meta){ stream.on('data', function(data) { context.depreAudioData(data, function(buffer) { audioStack.push(buffer); if ((init!=0) || (audioStack.length > 10)) { // make sure we put at least 10 chunks in the buffer before starting init++; scheduleBuffers(); } }, function(err) { console.log("err(depreAudioData): "+err); }); });});function scheduleBuffers() { while ( audioStack.length) { var buffer = audioStack.shift(); var source = context.createBufferSource(); source.buffer = buffer; source.connect(context.destination); if (nextTime == 0) nextTime = context.currentTime + 0.05; /// add 50ms latency to work well across systems - tune this if you like source.start(nextTime); nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played };}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)