有没有办法检测 HTML 5 网络音频 API 中的音频?

Is there a way to detect audio frequency in HTML 5 web audio API?

我想知道有没有一种方法可以检测 html 5 网络音频中麦克风的音频。我想制作一个在线吉他调谐器,我需要从声音输入中获得以赫兹为单位的音频频率。我看过一些EQ和滤波效果,但是我没有看到任何关于频率识别的东西。

编辑: 我发现了这个:http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound 第二点(分析器节点)非常有趣。我看过他的源代码,但我不知道如何将分析器连接到麦克风输入。 当 mp3 文件开始播放时,他调用了 playSound() 函数,并在那里绘制了他的 canvas。但是我没有类似 playSound() 的函数...

您应该可以使用 BiquadFilterNode

来自 link 的示例代码:

var audioCtx = new AudioContext();
var biquadFilter = audioCtx.createBiquadFilter();
biquadfilter.getFrequencyResponse(myFrequencyArray,magResponseOutput,phaseResponseOutput);

我写了一个网络音频库,除其他外,它可以检测麦克风输入的频率。在 https://github.com/rserota/wad#pitch-detection

查看
var voice = new Wad({source : 'mic' });
var tuner = new Wad.Poly();
tuner.add(voice);
voice.play();

tuner.updatePitch() // The tuner is now calculating the pitch and note name of its input 60 times per second. These values are stored in tuner.pitch and tuner.noteName.

var logPitch = function(){
    console.log(tuner.pitch, tuner.noteName)
    requestAnimationFrame(logPitch)
};
logPitch();
// If you sing into your microphone, your pitch will be logged to the console in real time.

tuner.stopUpdatingPitch(); // Stop calculating the pitch if you don't need to know it anymore.

您可以使用以下代码从麦克风获取频率。

navigator.mediaDevices.getUserMedia({audio:true}).then(function(localStream){
  var audioContext = new(window.AudioContext || window.webkitAudioContext)();
  var input = audioContext.createMediaStreamSource(localStream);
  var analyser = audioContext.createAnalyser();
  var scriptProcessor = audioContext.createScriptProcessor();
  // Some analyser setup
  analyser.smoothingTimeConstant = 0;
  analyser.fftSize = 64;

  input.connect(analyser);
  analyser.connect(scriptProcessor);
  scriptProcessor.connect(audioContext.destination);
  var getAverageVolume  =  function( array){
      var length = array.length;
      var values = 0;
      var i = 0;
     for (; i < length; i++) {
        values += array[i];
     }
    return values / length;
  };
  var onAudio = function(){
    var tempArray = new window.Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(tempArray);
    var latestFrequency = (getAverageVolume(tempArray));
    //use latestFrequency
  };
  scriptProcessor.onaudioprocess = onAudio;
})
.catch(function(){
  //Handle error
});