Question

I am trying to do a school project where I am trying to build an application in Java that I have to analise a song, and as the song is playing I need to output certain frequencies when they are hit ie: whenever it hit 1hz it would print to the console "1Hz".

I have all the files for the audio-analysis ie: com.badlogic.audio package.

This is a very important project, and I am relatively new to Java (I am in high school and this is my major project for the year).

Please can someone talk me through how to implement the FFT algorithm into a song? ie: how do I display on screen: "select a song" then when they select the song, play it out loud over a media player or something and while it is playing, whenever it hits certain points, output something (like the 1hz example above).

Is there another library that can return the current frequency in the song to me?

Thanks in advance for any help or advice!

Was it helpful?

Solution

The problem is that there is almost never a single "current frequency" in a song. Unless it's a pure sine tone, there are multiple overtones; when you have multiple instruments that gets progressively more complicated.

Yes, fourier transform could tell you which frequencies are present. You'd have to look at the documentation for the FFT package you're using (the badlogic code) to get information about how to invoke it. Their sample programs, such as the FFTTest, are likely to be particularly informative.

Not a trivial project, but should be a fun one. Good luck!

OTHER TIPS

Why not start by studying the java sound api? You will need to import your audio file into java before analyzing it. Get different segments of code to work. Then map out all the things your code needs to do, and google to find code for the missing elements. Finally, put all the working elements together.

You can use the Commons Math library for doing a DFT (FFT), as it contains numerous other useful methods. The FFT can simply be invoked from a FastFourierTransformer object and the transform(double[] input) and transform(Complex[] input) methods. So you'll need to get your audio file's data into an array format like that. Pretty much all DFT/FFT methods will expect an array input and will return to you a (complex) array output containing the transformed input.

However, keep in mind that the FFT contains no time information whatsoever, so if you want to look at when a frequency (1Hz) appears in order to do something then, you'll need to either do multiple FFTs every so often (and decide whether a note was hit in that time window) or use a wavelet transform instead. The reason the FFT gives you no time resolution is exactly because you are converting a time-domain signal into a frequency-domain signal. This is an important point to understand if you want to do this project.

If you want to do this the first thing would be to find an FFT package and familiarize yourself with it. (You do not want to write your own.) Figure out some way to plot the output (if only via "print plot") so you can get a feel for what the spectrum looks like.

Once you get the hang of it, detecting specific peaks is not that hard (if you have a mind for the math and the programming).

Also, you might want to get a stand-alone audio package that can do FFT and plot, since those plots will be cleaner with better resolution. (I think Audacity can do this.)

Also note that there is a Signal Processing SO site that can give you more specialized assistance. (Be sure to explain that you're a HS student, not a pro.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top