Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 654
Karl Avatar asked Sep 17 '08 02:09

Karl


People also ask

How do you calculate beats per measure?

You'll spot the time signature in the beginning of the music – it's two numbers stacked vertically. The top number tells you how many beats there are in one measure. The bottom number tells you what kind of note is considered one beat.

What is audio BPM?

In the language of music, the phrase “beats per minute” (BPM) is relatively self-explanatory: It indicates the number of beats in one minute. For instance, a tempo notated as 60 BPM would mean that a beat sounds exactly once per second.

What is the BPM of 1 second?

A rate of 60 bpm means that one beat will occur every second.


2 Answers

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!

like image 69
Hallgrim Avatar answered Sep 22 '22 23:09

Hallgrim


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

like image 28
Nick Johnson Avatar answered Sep 23 '22 23:09

Nick Johnson