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

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:

So yeah. Like I said, hairy math.

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.

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.