Question

Beats per minute from real-time audio input

I'd like to write a simple C# application to monitor the line-in audio and give me the current (well, the rolling average) beats per minute.

I've seen this gamedev article, and that was absolutely no help. I went through and tried to implement what he was doing but it just wasn't working.

I know there have to be tons of solutions for this, because lots of DJ software does it, but I'm not having any luck in finding any open-source library or instructions on doing it myself.

 45  46947  45
1 Jan 1970

Solution

 28

Calculate a powerspectrum with a sliding window FFT: Take 1024 samples:

double[] signal = stream.Take(1024);

Feed it to an FFT algorithm:

double[] real = new double[signal.Length];
double[] imag = new double[signal.Length);
FFT(signal, out real, out imag);

You will get a real part and an imaginary part. Do NOT throw away the imaginary part. Do the same to the real part as the imaginary. While it is true that the imaginary part is pi / 2 out of phase with the real, it still contains 50% of the spectrum information.

EDIT:

Calculate the power as opposed to the amplitude so that you have a high number when it is loud and close to zero when it is quiet:

for (i=0; i < real.Length; i++) real[i] = real[i] * real[i];

Similarly for the imaginary part.

for (i=0; i < imag.Length; i++) imag[i] = imag[i] * imag[i];

Now you have a power spectrum for the last 1024 samples. Where the first part of the spectrum is the low frequencies and the last part of the spectrum is the high frequencies.

If you want to find BPM in popular music you should probably focus on the bass. You can pick up the bass intensity by summing the lower part of the power spectrum. Which numbers to use depends on the sampling frequency:

double bassIntensity = 0;
for (i=8; i < 96; i++) bassIntensity += real[i];

Now do the same again but move the window 256 samples before you calculate a new spectrum. Now you end up with calculating the bassIntensity for every 256 samples.

This is a good input for your BPM analysis. When the bass is quiet you do not have a beat and when it is loud you have a beat.

Good luck!

2008-09-17

Solution

 16

There's an excellent project called Dancing Monkeys, which procedurally generates DDR dance steps from music. A large part of what it does is based on (necessarily very accurate) beat analysis, and their project paper goes into much detail describing the various beat detection algorithms and their suitability to the task. They include references to the original papers for each of the algorithms. They've also published the matlab code for their solution. I'm sure that between those you can find what you need.

It's all available here: http://monket.net/dancing-monkeys-v2/Main_Page

2008-09-17