<< Chapter < Page Chapter >> Page >

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.

Beat detection on metric's "gimme sympathy"

pic
Beats outlined by blue bars

Beat detection on flogging molly's "don't shut 'em down"

pic
Beats outlined by blue bars

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.

< E local > = ( Length of Instant Signal in Time ) * 0 F max |FFT( S local ) | 2
E instant = 0 F max |FFT( S instant ) | 2

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.

σ 2 = ( Length of Instant Signal in Time ) * ( E instant - < E local > ) 2
C = (−0.0025714 σ 2 ) + 1.5142857
< E local > C * E instant

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Tetris sound: using music to affect gameplay. OpenStax CNX. Dec 20, 2013 Download for free at http://cnx.org/content/col11609/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Tetris sound: using music to affect gameplay' conversation and receive update notifications?

Ask