Get audio frequency


I want to program a tuner. But I don’t know if it’s even possible. Is there any way in Defold to get the frequency of sound?
Thank you veru much for every answer.



The current audio capabilities in Defold are fairly limited. You can achieve what you want if you use a native extension. I’d start by looking at for instance the OpenAL extension:

1 Like


I am very grateful for your answer.
I’ve researched OpenAl before, but I still can’t imagine how I could use it to get individual frequencies. Could you please explain it to me?



I have no idea actually! :slight_smile: I’m just thinking that OpenAL might have something in the API that could be used.



You definitely need a native extension. What you’re looking for is the following:
a) A way to get sound input. I think OpenAL has some functions to capture sound from the user’s microphone.
b) Say you got a buffer full of sound samples. They’re in the time domain (each sample represents the value of the amplitude at a certain time). In order to obtain the frequency domain from a short time window of amplitude samples, you’ll need to apply a Fourier transform (look for libraries that do FFT).
c) The FFT will return an array of 2D vectors in the frequency domain (each vector represents the amplitude and phase of an oscillator running at a given frequency). You’ll want to take each vector and calculate the amplitude (the length of the 2D vector (sqrt(x^2 + y^2)).
d) Now it’s just a matter of selecting the frequency with the highest amplitude from your array. One optimisation would be to get rid of the sqrt(), since you’re just looking for the max value.



Sounds like someone should create Defftw :slight_smile:



Thanks, I’ll take a look at it. I’m currently trying this: