当 audioContext.destination 不在 Web Audio API 中播放声音时如何收听
How to listen when audioContext.destination do not play sound in Web Audio API
我正在使用 $timeout
angular 函数每 512 毫秒调用一次 tick() 以播放我的音频队列中的数据。我正在使用它来执行实时音频流。有时声音会有所中断,我真的需要在发出和接收声音之间保持一秒的增量。所以我想删除队列中与每次剪辑的持续时间相对应的一些音频数据。
你知道是否有办法在 audioContext.destination 上收听那些剪辑,例如:
audioContext.destination.oncuts = function(duration) {
audioQueue.read(duration);
});
这是我的 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;
}
};
您不需要依赖 Javascript 计时器(出于音频目的,它们非常不准确)并提前安排您的滴答声。查看 http://www.html5rocks.com/en/tutorials/audio/scheduling/,这是我不久前写的关于调度计时器的文章。
我正在使用 $timeout
angular 函数每 512 毫秒调用一次 tick() 以播放我的音频队列中的数据。我正在使用它来执行实时音频流。有时声音会有所中断,我真的需要在发出和接收声音之间保持一秒的增量。所以我想删除队列中与每次剪辑的持续时间相对应的一些音频数据。
你知道是否有办法在 audioContext.destination 上收听那些剪辑,例如:
audioContext.destination.oncuts = function(duration) {
audioQueue.read(duration);
});
这是我的 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;
}
};
您不需要依赖 Javascript 计时器(出于音频目的,它们非常不准确)并提前安排您的滴答声。查看 http://www.html5rocks.com/en/tutorials/audio/scheduling/,这是我不久前写的关于调度计时器的文章。