One of the algorithms we chose to implement was a beat detections algorithm. In the game this maps to a pulsing visualization.
Background
When a drum is hit, a spike in energy occurs across all frequencies. When a bass guitar is plucked, there is a spike in energy in the lower frequires. As these two instruments are commonly used in modern music to give a song rhythm or a beat, we decided to look at energy in the lower end of the spectrum. Theoretically, this should automatically filter out other spikes of energy created by a vocalist or electric guitar.
Using energy comparisons to detect beats
Energy calculation
Because energy spikes when a beat occurs, it is fairly straight forward to implement an algorithm which compares an average local energy to an instantaneous energy. Because we only want to look at the low frequencies, we need to compute the FFT of the signal. Then, we compute the average local energy by taking the integral of the square of the FFT of a second in time over the number of samples. The instantaneous energy is calculated by taking the integral of the square of the FFT of a single sample.
Constant calculation
Probabilistically speaking, if the instantaneous energy is greater than the local energy by a large enough quantity, we can say with a high likelihood that a beat has occured. The quantity is realized through the variance of the instantaneous energy to the average local energy. A constant is then calculated from the variance and we can see that the instantaneous energy needs to be a factor of the constant larger than the average local energy.
Filtering extra beats
If a beat is detected, its sample number is recorded in a vector. If a beat spikes for multiple samples, multiple beats would be recorded which leads to an overestimation of the current tempo. To account for this, we merely look at the samples, detect a leading edge where the first beat is recorded, and ignore all subsequent sequential beats.