Could someone give me a clue how to get the frequency of a sound in real time with javascript? Thanks.
Could someone give me a clue how to get the frequency of a sound in real time with javascript? Thanks.
The API available to do this is AudioContext , you need to do 4 things:
With the frequency samples (obtained with fast Fourier transform or FFT) you can search the frequencies with greater intensity and determine the dominant frequency. There are many methods to calculate the pitch, here I used a very simple and not very precise one that is based on the signal with more energy, but the human ear does not work in this way, so it may take some work to adapt the calculation of the "audible" frequency ..
The example is in this JSFIDDLE , because the StackSnippets do not allow capturing the microphone. Notice that the green line is the frequency shown above. It works pretty well with a tone generator like this .
Keep in mind that these are quite new APIs and although there is good support it is an option that you should use as Progressive Enhancement, so you should only include it in browsers that support it. It is also very useful in applications made with Electron or Apache Cordoba since they are based on Chromium and it supports these APIs very well.