我一直试图通过webaudio api从麦克风输入中获取实时音量。在阅读了一些文章后,我认为我有一些工作,但似乎分析器和脚本处理器返回的卷总是为0。
更新,以便"麦克风"用作数据源
// detect support for different browsers
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
var Mosquito = {
setup : function(){
// ask permission to grab the mic
var stream = navigator.getUserMedia({ audio: true }, Mosquito.successCallback, Mosquito.errorCallback);
},
successCallback : function(localMediaStream){
var audioContext = new AudioContext();
// hook up the mic to the mediaStreamSource in audioContext
var microphone = audioContext.createMediaStreamSource( localMediaStream );
Mosquito.processAudio(microphone, audioContext);
},
processAudio : function(microphone, audioContext){
// analyze a piece of the signal
var analyser = audioContext.createAnalyser();
analyser.smoothingTimeConstant = 0.3; // make it less jizzy
analyser.fftSize = 512;
// create a scriptProcessor in order to read out the analyser data
var javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
// connect all the things
microphone.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioContext.destination);
javascriptNode.onaudioprocess = function(e){
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
console.log(array);
var average = Mosquito.getAverageVolume(array);
console.log(average);
Mosquito.drawMeters(average);
};
},
drawMeters : function(average){
document.getElementById("meter").innerHTML = average;
},
getAverageVolume : function(array){
var values = 0;
var average;
var length = array.length;
// get all the frequency amplitudes
for (var i = 0; i < length; i++) {
values += array[i];
}
average = values / length;
return average;
},
errorCallback : function(){
console.log('nope');
}
}
Mosquito.setup();
感谢您花时间看看我做错了什么,网络音频对我来说仍然是一个新鲜事物:)
答案 0 :(得分:0)
为什么要创建缓冲源?您应该只连接mediastreamsource(当前未连接,这就是您没有获得任何数据的原因)。