- Home /
Read the pitch frequency of a audio clip
i need to be able to read the pitch of a song at runtime in unity, and to avoid confusion i dont mean audioClip.pitch or audioClip.frequency, because frequency is the same through out and pitch just changes the pitch of the clip not tell me the actual frequency, any help would be greatly appreciated
Answer by maturasek · Jun 26, 2012 at 01:11 PM
Pitch detection is not a trivial problem, since every polyphonic audio data has a whole spectrum of frequencies. The actual "pitch" is just the most significant frequency in it for the human mind.
This spectrum can be extracted with the FFT algoritms, which is a widely discussed and reasearched topic. Unity also has FFT solution, which I found fast enough for some real time work. You can simply use AudioSource.GetSpectrumData, but, as FadeToBlack mentioned, the spectral data will only give you the source material for actual pich detection algorithms (findign the fundamental frequency, etc) If you need some more info on the topic, please refer to the article on this page
Answer by FadeToBlack · Jun 26, 2012 at 11:25 AM
to get actual pitch of sound playing at current time, you need to get sound data currently playing (using something like AudioSource.GetOutputData), then you need to learn what is Fast Fourier Transform, find & download some ready solutions around this. using FFT you can detect pitch of your sound (or even detect a note), but it also need to do some hard work. i think simply you need to find some ready solutions for sound analyzing for realtime. if you will not found ready solutions, then you can do some experiments with FFT. it gives you spectrum of your sound, you need to found maximum amplitude, it tells you approximated frequency.
AudioSource.GetSpectrumData - wow ) even so.. a long time ago i have try to make guitar tuner, but also stuck on frequency detection, but i was needed in best precision. detect which note playing or approx frequency is quite simple.
Yeah, you are right, in a guitar tuner, pitch detection is quite simple and precision is a bigger importance. When I wrote my answer I had my music information retrieval project (kind of a genre detection problem) in my $$anonymous$$d, and in my case it was quite hard to extract pitch data from a complex music piece. Well, I guess I have to relax a bit :)
Your answer
Follow this Question
Related Questions
getting pitch 1 Answer
How to use Mathf.SmoothDamp for audio ? 1 Answer
constant audio sounds? 1 Answer
Audio signal plug-in? 1 Answer
Code to measure sound waves 1 Answer