我需要了解音频缓冲区的工作原理,并要执行以下顺序:Microphone-> Auto-> Processor-> Manual-> Buffer-> Auto-> Speakers
。自动表示自动数据传输,手动表示我通过processor.onaudioprocess
中的代码进行操作。所以我有以下代码:
navigator.getUserMedia = navigator.getUserMedia ||navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var audioContext;
var myAudioBuffer;
var microphone;
var speakers;
if (navigator.getUserMedia) {
navigator.getUserMedia(
{audio: true},
function(stream) {
audioContext = new AudioContext();
//STEP 1 - we create buffer and its node
speakers = audioContext.destination;
myAudioBuffer = audioContext.createBuffer(1, 22050, 44100);
var bufferNode = audioContext.createBufferSource();
bufferNode.buffer = myAudioBuffer;
bufferNode.connect(speakers);
bufferNode.start();
//STEP 2- we create microphone and processor
microphone = audioContext.createMediaStreamSource(stream);
var processor = (microphone.context.createScriptProcessor ||
microphone.context.createJavaScriptNode).call(microphone.context,4096, 1, 1);
processor.onaudioprocess = function(audioProcessingEvent) {
var inputBuffer = audioProcessingEvent.inputBuffer;
var inputData = inputBuffer.getChannelData(0); // we have only one channel
var nowBuffering = myAudioBuffer.getChannelData(0);
for (var sample = 0; sample < inputBuffer.length; sample++) {
nowBuffering[sample] = inputData[sample];
}
}
microphone.connect(processor);
},
function() {
console.log("Error 003.")
});
}
但是,此代码不起作用。没有错误,只有沉默。我的错误在哪里?
最佳答案
您是否变得沉默(即您的进程正在被调用,但缓冲区为空)或什么都没有(即您的进程从未被调用)?
如果是后者,请尝试将脚本处理器连接到context.destination。即使您不使用输出,当前某些实现也需要该连接才能拉入数据。
关于javascript - HTML 5 : AudioContext AudioBuffer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38282611/