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

≯℡__Kan透↙ 提交于 2020-01-01 18:58:14

问题


I would like to know is there a way we can detect audio frequency from microphone in html 5 web audio. I wish to make an online guitar tuner, and I need to have the audio frequency in hertz, from the sound input. I've seen some EQ and filters effects, but I didn't see anything about frequency recognition.

EDIT: I found this: http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound The 2nd point (analyser node) is really interesting. I seen his source code, but I can't figure how to connect the analyser to the microphone input. He calls a playSound() function when the mp3 file starts to play, and there he draws his canvas. But I do not have a playSound() like function...


回答1:


I wrote a web audio library which, among other things, can detect frequency from mic input. Check it out at 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.



回答2:


You should be able to use BiquadFilterNode.

Example code from the link:

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



回答3:


You can use the following code to get the frequencies from the mic.

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
});


来源:https://stackoverflow.com/questions/28329389/is-there-a-way-to-detect-audio-frequency-in-html-5-web-audio-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!