- Home /
Calculating rhythm of any music?
I am making a music based game and I want the level being generated by the rhythm of any sound track the player puts in the game. How do I find/calculate the rhythm of any song? Any ideas? Thanks
Take a look at this: AudioClip.
I've never really worked with the audio clip variables, so I don't know if it will help you.
Answer by CHPedersen · Aug 06, 2013 at 07:31 AM
This has the potential to be somewhat complex. :-/ There is no out-of-the-box feature in Unity that calculates the rhythm of arbitrary music, probably because doing so is a non-trivial signal processing task uncommon in the field of computer graphics. It can be done, of course, but if you were hoping for that one magical method call, you're in for some tough luck, I'm afraid; it's going to be involved, and it's going to require a lot of hairy math and hard work.
The defining characteristic of rhythm is repetition over time. Some feature of the music that creates a distinguishable pattern which is periodic and has a clear frequency. The most obvious place to look would be the bass drum. You mention you want the player to select a song from his library. Have you considered what happens if the player selects a song that doesn't have a bass drum? What if he passes your algorithm a classical piece that has no distinguishable beat? Or worse yet, a file that contains white noise?
Even if the music has a periodic beat, it is not enough to just examine a sample in the frequency domain (which is what DaveA's reply suggests. GetSpectrumData takes the Fast Fourier Transform of a time domain sample to convert it to its frequency domain representation). Articles I've seen which reliably evaluate the beat of music instead use the Continuous Wavelet Transform. See links:
http://www.ams.org/notices/200903/rtx090300356p.pdf
http://marcs.uws.edu.au/links/ICoMusic/Full_Paper_PDF/Smith_Honing.pdf
So yeah. Like I said, hairy math.
Answer by DaveA · Aug 06, 2013 at 06:54 AM
Use GetSpectrumData. You'll probably want to just use that visualizer code in the scripting reference so you can find the array index or indices that will indicate the beat.
I don't need a visualizer. I want the player to select a song from his library and the game will generate a level based on the songs beat. How can I get the rhythm of it?
I have been doing some research on GetSpectrumData and I have a few questions. How do I use this information? What kind of information does it give me? I need to know this. I have to generate platforms based on the tempo of the music. The players speed is also affected by the tempo.
Answer by Vetpetmon · Nov 29, 2014 at 12:00 PM
You would have to listen to the song a few times. Peaceful beats (few or none)means that it can not be used for combat zones. chaotic music (many or fast)is for battle zones and dangerous areas. Also, find the most used drum in the music and judge if it goes good for you. Note: I'm not a musician, I don't make music but I do listen to music.
Your answer
Follow this Question
Related Questions
Audio controls disabled??! 0 Answers
How To Turn Off Sound For Certain Objects? 1 Answer
Audio Scripting 1 Answer