[英]How to listen when audioContext.destination do not play sound in Web Audio API
I'm using the $timeout
angular function to call tick() each 512 ms in order to play datas which are in my audio queue. 我正在使用$timeout
角度函数每512毫秒调用一次tick(),以播放音频队列中的数据。 I'm using this to perform a live audio stream. 我正在使用它来执行实时音频流。 Sometimes there are some cuts in the sounds and I really need to maintain a delta of one second between emitting and receiving sound. 有时声音会有所减少,我确实需要在发出和接收声音之间保持一秒钟的增量。 So I want to delete some audio datas in my queue corresponding to the duration of each cuts. 因此,我想删除队列中与每个剪辑时长相对应的一些音频数据。
Do you know if there is a way to listen to those cuts on the audioContext.destination like : 您是否知道是否可以通过以下方式收听audioContext.destination上的内容:
audioContext.destination.oncuts = function(duration) {
audioQueue.read(duration);
});
Here is my tick and audioQueue functions : 这是我的tick和audioQueue函数:
var tick = function() {
$scope.soundclock = Date.now();
$timeout(tick, $scope.tickInterval);
if(startStream && isFocused) {
if(isChrome === true || isOpera === true || isIE === true || isFirefox === true) {
if(audioQueue.length()>=size) {
float32 = audioQueue.read(size);
source = audioContext.createBufferSource();
audioBuffer = audioContext.createBuffer(1, size, sampleRate);
data = audioBuffer.getChannelData(0);
for(var i=0; i<size;i++) {
data[i] = float32[i];
}
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start(0);
}
}
if(isSafari === true) {
if(audioQueue.length()>=size) {
float32 = audioQueue.read(size);
source = audioContext.createBufferSource();
audioBuffer = audioContext.createBuffer(1, size, sampleRate);
data = audioBuffer.getChannelData(0);
for(var j=0; j<size;j++) {
data[j] = float32[j];
}
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.noteOn(0);
}
}
}
};
var audioQueue = {
buffer: new Float32Array(0),
write: function(newAudio){
currentQLength = this.buffer.length;
newBuffer = new Float32Array(currentQLength+newAudio.length);
d = Date.now() - date;
console.log('Queued '+newBuffer.length+' samples. ');
date = Date.now();
newBuffer.set(this.buffer, 0);
newBuffer.set(newAudio, currentQLength);
this.buffer = newBuffer;
},
read: function(nSamples){
samplesToPlay = this.buffer.subarray(0, nSamples);
this.buffer = this.buffer.subarray(nSamples, this.buffer.length);
console.log('Queue at '+this.buffer.length+' samples. ');
return samplesToPlay;
},
length: function(){
return this.buffer.length;
}
};
You need to not rely on Javascript timers (which are, for audio purposes, horribly inaccurate) and schedule your ticks ahead of time. 您无需依赖Javascript计时器(出于音频目的,这是非常不准确的),并且可以提前安排滴答声。 Check out http://www.html5rocks.com/en/tutorials/audio/scheduling/ , which I wrote a while ago about scheduling timers. 请查看http://www.html5rocks.com/zh-CN/tutorials/audio/scheduling/ ,我前一段时间写过有关调度计时器的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.