2
\$\begingroup\$

I'm trying to load a level from a music file (mp3, midi, etc, I'm not picky) for a rhythm-based game. I must be searching in all the wrong places because I can't seem to find any help on this topic.

(I am using Eclipse Luna, AndEngine, Android SDK Tools 20.0.5 and ADT 23.0.4)

My plan was to stream the music file at run-time and create objects when certain pitches, etc are reached, but I have no idea how to do this in Java? Are there any libraries, etc I should be using or is this just not possible?

\$\endgroup\$
1

2 Answers 2

3
\$\begingroup\$

It seems to me that you will need to perform some sort of Frequency analysis on the audio file in question. You could either pre-process the file on level load (using the data to queue up your level actions), or perform the analysis in real-time.

Either way, you'll want to divide your audio waveform into sections, each comprised of N samples, and then perform a Fourier Transform on those N samples. This will give you frequency data for that section of time in your audio, from which you can decide what to do in your game (e.g. drop a bomb on a bass kick).

If you instead perform a transform on the whole song, you will get the frequency data for the whole song. Which you probably don't want, as you will no longer know at what points in time the frequencies occur. However, you could use this information to theme your level (e.g. lots of bass = some sort of underground level or somesuch).

In order to do that in a reasonable amount of time (pre-processing or real-time), you'll want to implement a Fast Fourier Transform algorithm. The Cooley-Tukey algorithm is apparently pretty commonly used. If you don't want to try to implement such an algorithm yourself, JTransforms is a multi-threaded FFT library for Java, and Kiss FFT, while in C, has a BSD license (and you may be able to find a java wrapper for it).

Addendum: You will also probably want to take a look into whether or not applying a Windowing Function affects your output in a significant way. In the above, taking N samples, a Rectangular Window Function is applied automatically.

From Information I have found, it appears that most people don't worry too much about dealing with a Window Function for the FFT.

\$\endgroup\$
1
  • \$\begingroup\$ Also, I don't know if Kiss FFT or JTransforms applies a windowing function, or if the effects of not applying a windowing function would be noticeable enough to matter. Either way, it's something additional to look into. \$\endgroup\$ Commented Dec 1, 2014 at 14:23
0
\$\begingroup\$

This is definitely possible. I would look into this book or ebook online and it teaches you how to write effective sound classes so that you can use them in the future, with situations like you are speaking of.

You can iterate through samples in any sound file and throw flags on high/low pitches, amplitudes, frequencies, etc..

Here is the book:

http://www.amazon.com/Introduction-Computing-Programming-Java-Multimedia/dp/0131496980

Once you have the sound class down, you can store the samples into an obj, then use that obj on the fly. Instead of trying to iterate through the sound file as it is playing.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .