javascriptaudiohtml5-audiogetusermediaaudiobuffer

JS Audio - audioBuffer getChannelData to frequency


bsd

I am trying achieve pitch detection, and moreover learn some basic audio physics on the way, I am actually really new to this and just trying to understand how this whole thing works...

My question is, What is exactly the audioBuffer and how is the data coming from getChannelData related to frequencies. and how can I extract frequency data from the audioBuffer...

Also, if someone can explain just a bit about sample rates etc. also this would be great.

Thanks!


Solution

  • An AudioBuffer simply represents an audio resource, namely audio samples and additional information about the audio.

    For instance, you can access the sampleRate property of an AudioBuffer object to know about the sampling frequency of the audio contained in the AudioBuffer.

    Using getChannelData will return you an array of audio samples. At every interval given by the sampling rate, you have a number (comprised between -1.0 and +1.0 for IEEE 32 float audio samples) corresponding to the audio amplitude of the sample. Thus, this array of samples contain time-domain audio information.

    To do pitch detection, you need to work in the frequency domain and to go from the time domain to the frequency domain, you need to use the Fourier transform. If you want to understand underlying DSP (digital signal processing) principles, you can use a pure FFT library (for instance, node-fft). If you just want to achieve a pitch detection, using a turnkey solution such as pitch.js will be easier.