<p>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.</p>

<p>I've seen <a href="http://www.gamedev.net/page/resources/_/technical/math-and-physics/beat-detection-algorithms-r1952" rel="noreferrer">this gamedev article</a>, and that was absolutely no help. I went through and tried to implement what he was doing but it just wasn't working.</p>

<p>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.</p>

[{'answer_id': 79480, 'author': 'Thomas', 'author_id': 14637, 'author_profile': 'https://Stackoverflow.com/users/14637', 'pm_score': 3, 'selected': False, 'text': '<p>This is by no means an easy problem. I\'ll try to give you an overview only.</p>\n\n<p>What you could do is something like the following:</p>\n\n<ol>\n<li>Compute the average (root-mean-square) loudness of the signal over blocks of, say, 5 milliseconds. (Having never done this before, I don\'t know what a good block size would be.)</li>\n<li>Take the Fourier transform of the "blocked" signal, using the FFT algorithm.</li>\n<li>Find the component in the transformed signal that has the largest magnitude.</li>\n</ol>\n\n<p>A Fourier transform is basically a way of computing the strength of all frequencies present in the signal. If you do that over the "blocked" signal, the frequency of the beat will hopefully be the strongest one.</p>\n\n<p>Maybe you need to apply a filter first, to focus on specific frequencies (like the bass) that usually contain the most information about the BPM.</p>\n'}, {'answer_id': 79532, 'author': 'Dan Harper', 'author_id': 14530, 'author_profile': 'https://Stackoverflow.com/users/14530', 'pm_score': 3, 'selected': False, 'text': '<p>Not that I have a clue how to implement this, but from an audio engineering perspective you\'d need to filter first.  Bass drum hits would be the first to check.  A low pass filter that gives you anything under about 200Hz should give you a pretty clear picture of the bass drum.  A gate might also be necessary to cleanup any clutter from other instruments with harmonics that low.</p>\n\n<p>The next to check would be snare hits.  You\'d have to EQ this one.  The "crack" from a snare is around 1.5kHz from memory, but you\'d need to definitely gate this one.</p>\n\n<p>The next challenge would be to work out an algorithm for funky beats.  How would you programatically find beat 1?  I guess you\'d keep track of previous beats and use a pattern matching something-or-other.  So, you\'d probably need a few bars to accurately find the beat.  Then there\'s timing issues like 4/4, 3/4, 6/8, wow, I can\'t imagine what would be required to do this accurately!  I\'m sure it\'d be worth some serious money to audio hardware/software companies.</p>\n'}, {'answer_id': 81462, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<p>It\'s all available here: <a href="http://monket.net/dancing-monkeys-v2/Main_Page" rel="noreferrer">http://monket.net/dancing-monkeys-v2/Main_Page</a></p>\n'}, {'answer_id': 81666, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 5, 'selected': False, 'text': '<p>Calculate a powerspectrum with a sliding window FFT:\nTake 1024 samples: </p>\n\n<pre><code>double[] signal = stream.Take(1024);\n</code></pre>\n\n<p>Feed it to an FFT algorithm: </p>\n\n<pre><code>double[] real = new double[signal.Length];\ndouble[] imag = new double[signal.Length);\nFFT(signal, out real, out imag);\n</code></pre>\n\n<p>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.</p>\n\n<p>EDIT:</p>\n\n<p>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:</p>\n\n<pre><code>for (i=0; i &lt; real.Length; i++) real[i] = real[i] * real[i];\n</code></pre>\n\n<p>Similarly for the imaginary part.</p>\n\n<pre><code>for (i=0; i &lt; imag.Length; i++) imag[i] = imag[i] * imag[i];\n</code></pre>\n\n<p>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 \nfrequencies.</p>\n\n<p>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:</p>\n\n<pre><code>double bassIntensity = 0;\nfor (i=8; i &lt; 96; i++) bassIntensity += real[i];\n</code></pre>\n\n<p>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. </p>\n\n<p>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. </p>\n\n<p>Good luck!</p>\n'}, {'answer_id': 668127, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>The easy way to do it is to have the user tap a button in rhythm with the beat, and count the number of taps divided by the time.</p>\n'}, {'answer_id': 4219423, 'author': 'pete', 'author_id': 512711, 'author_profile': 'https://Stackoverflow.com/users/512711', 'pm_score': 1, 'selected': False, 'text': '<p>First of all, what Hallgrim is producing is not the power spectral density function. Statistical periodicities in any signal can be brought out through an autocorrelation function. The fourier transform of the autocorrelation signal is the power spectral density. Dominant peaks in the PSD other than at 0 Hz will correspond to the effective periodicity in the signal (in Hz)... </p>\n'}, {'answer_id': 10342753, 'author': 'eandersson', 'author_id': 408182, 'author_profile': 'https://Stackoverflow.com/users/408182', 'pm_score': 3, 'selected': False, 'text': '<p>I found this library which seem to have a pretty solid implementation for detecting <strong>Beats per Minute</strong>.\n<a href="https://github.com/owoudenberg/soundtouch.net" rel="nofollow noreferrer">https://github.com/owoudenberg/soundtouch.net</a></p>\n<p>It\'s based on <a href="http://www.surina.net/soundtouch/index.html" rel="nofollow noreferrer">http://www.surina.net/soundtouch/index.html</a> which is used in quite a few DJ projects <a href="http://www.surina.net/soundtouch/applications.html" rel="nofollow noreferrer">http://www.surina.net/soundtouch/applications.html</a></p>\n'}, {'answer_id': 28925534, 'author': 'Matt Williams', 'author_id': 3905343, 'author_profile': 'https://Stackoverflow.com/users/3905343', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d recommend checking out the BASS audio library and the BASS.NET wrapper. It has a built in BPMCounter class. </p>\n\n<p>Details for this specific function can be found at\n<a href="http://bass.radio42.com/help/html/0833aa5a-3be9-037c-66f2-9adfd42a8512.htm" rel="nofollow">http://bass.radio42.com/help/html/0833aa5a-3be9-037c-66f2-9adfd42a8512.htm</a>.</p>\n'}]
['https://Stackoverflow.com/questions/79445', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14758/']