- Home /
Get the frequency from an AudioSource
Hey!
I am trying to make a game where I use my guitar as controller. (I know unity isn't the best for this) I figured out some stuff and now I can use my guitar as Input. But is there a way to get the input frequency from a mic or from a Audiosource? (for the people that ask why I would want the frequency, I want to use it to check if I hit a certain note).
Thanks :)
http://answers.unity3d.com/questions/1113690/microphone-input-in-unity-5x.html
This seems to show how to get realtime input from the microphone. Figuring out the frequency through FFT will be your job though :)
Well, as I wrote in my question, I already have something to get realtime input. I just can't figure out how to find out the frequency
I just found this in the docu. $$anonymous$$aybe it does what you need? https://docs.unity3d.com/ScriptReference/AudioSource.GetSpectrumData.html
Here are some posts I have bookmarked. Not sure how much editing you'll have to do to be compatible with Unity5, but at least its a starting point.
Frequency :
http://answers.unity3d.com/questions/175173/audio-analysis-pass-filter.html
http://forum.unity3d.com/threads/unity-3-audio-spectrum-analysis.58769/#post-409103
https://www.youtube.com/watch?v=V_8JSWVT36k
Amplitude :
http://answers.unity3d.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html
http://answers.unity3d.com/questions/165729/editing-height-relative-to-audio-levels.html
Answer by jkeogh1413 · Mar 08, 2018 at 03:06 AM
@danivdwerf I've had some luck with this by using AudioSource.GetSpectrumData. It transforms the most recently played audio samples from the time domain to the frequency domain, so what you get is an array populated with bins of frequency values from 0Hz - the sampling rate (which can be retrieved from AudioSettings.outputSampleRate) / 2, split by the length of the array you provide. So if you provided an array with a length of 1024, and your sample rate was 48000Hz, your maximum returned frequency would be 24000Hz and each index of the array would represent a frequency chunk of ~23Hz.
If you're running your live microphone input into an AudioSource like so: https://docs.unity3d.com/ScriptReference/Microphone.Start.html
This method should work well for you.
I have a more in-depth article where I go through more examples and provide a bit more clarity, but this short description should get you on the right track. If you want to check out the article it's here: https://medium.com/giant-scam/algorithmic-beat-mapping-in-unity-intro-d4c2c25d2f27