- Home /
Audiosurf style audio analysis (unity 3.5)
I want to be able to analyze music before actually playing it similarly to what the game Audiosurf does. This is necessary in order to build levels and such based on the music data. To my knowledge this makes AudioScource.GetOutputData() and AudioSource.GetSpectrumData() useless as it provides up to 1024 audio samples as the music is playing.
Now, it seems as of Unity3.5(correct me if I'm wrong), we have AudioClip.GetData() which gives 44100(or whatever the sample rate is) samples per second; the entire song's sample data. Now to my understanding these samples provided by GetData() coincide with the ones provided by GetOutputData(), as in they are the songs amplitudes.
But it seems like GetSpectrumData() does not have a sister function to provide the entire song's samples (which in GetSpectrumData's case appear to be the frequencies?). Won't I need the frequencies for Fast Fourier Transform analysis (which I believe Audiosurf does)?
I realize I'm a bit in over my head here but I know I'm not the only one looking for this info in Unity and any information that could be provided here would be most appreciated.
I'm currently trying to achieve the same, could you please tell me if you have succeeded in calling AudioClip.GetData() on the clip you retrieve from WWW? I can't get it working :( The call is successfull but the float[] returned is filled only with 0 :(
Answer by luizgpa · Jan 04, 2012 at 06:19 PM
I don't have Unity 3.5, but if there's a way to get all the samples from an audio clip, you could use some FFT implementation to transform the data to frequency domain and analyze it.
Answer by jkeogh1413 · Mar 05, 2018 at 10:58 PM
I'm late to the game here, but I had the exact same question about a month ago. It took a lot of searching around and a lot of testing, but I landed on a pretty solid solution.
Using GetData
, I retrieved the interleaved stereo samples of a clip and then averaged each pair of stereo samples together to get something similar to GetOutputData
's Channel 0.
For the FFT it took some minor conversion but I used DSPLib (https://www.codeproject.com/Articles/1107480/DSPLib-FFT-DFT-Fourier-Transform-Library-for-NET).
I explain in detail in this blog series: https://medium.com/giant-scam/algorithmic-beat-mapping-in-unity-intro-d4c2c25d2f27
After you get the initial context, you're probably most interested in the "Preprocessed Audio Analysis" post. I also include a link in the Outro to a public Github repo with the working source.
Hope this helps you (6 years later) and anyone else that stumbles upon this post looking for a way to do preprocessed audio analysis in Unity!
Answer by d_vizigin · Jun 07, 2014 at 09:45 AM
Hi, Maybe this plugin can help: http://u3d.as/content/live-typing/3d-audio-road-generator