keiusui has asked for the wisdom of the Perl Monks concerning the following question:

The MP3::Tag module is wonderful for reading and writing ID3 tags on mp3 files.

Does anyone know of a module that can calculate the beats-per-minute (aka tempo or BPM) of a song?

There are numerous free programs for the Windows XP operating system that will calculate the BPM of a song, but I'm trying to calculate the BPM of a song using a Perl script.

Any solutions or ideas are gratefully appreciated. Thank you.

  • Comment on calculating the bpm of a song in mp3 format

Replies are listed 'Best First'.
Re: calculating the bpm of a song in mp3 format
by kvale (Monsignor) on Apr 16, 2006 at 00:32 UTC
    The algorithm I would use to calculate BPM is to
    1. calculate the modulation envelope of the waveform using either a Hilbert transform or rectification and smoothing
    2. compute the Fast Fourier Transform (FFT) of the modulation envelope
    3. look for the strongest peak in the 0.2-5Hz range (depending on the music)
    4. Assign the center of the peak to be the BPM.
    Note that most music is approximately scale-free (or fractal, to use a more popular term) so that there will be energy at all time scales. But for some kinds of music there will be a prominent peak in the above range that people usually associate with BPM.

    How to do all of this in perl? I would use PDL. It is very efficient in manipulating vector quantities like sound waveforms and has a built in FFT function.

    -Mark

Re: calculating the bpm of a song in mp3 format
by supersan (Initiate) on Apr 16, 2006 at 22:48 UTC
    Hi,

    I did this in a project once and after a lot of searching, this is the best paper I have found on the subject: http://www.owlnet.rice.edu/~elec301/Projects01/beat_sync/beatalgo.html (its Matlab Code)

    Here is a small outline of the steps involved in doing this Perl.
    1. first convert your mp3 file into a .wav file with the lame encoder (-d option).
    2. Using the Win32::Sound module load this .wav file and read the samples.
    3. Take a FFT to convert your time signal into frequency domain
    4. Optionally, you can use a comb filter (it involves convolving a series of sample signals to determine which signal yields the highest energy) as described in the above paper. Personally, I skipped it
    Cheers!
    san.

    print length "The answer to life, universe & everything!"
Re: calculating the bpm of a song in mp3 format
by zentara (Cardinal) on Apr 16, 2006 at 11:03 UTC
    Dosn't the BPM change quite often in modern music, with synchopated rythems, etc? How would you know that the point you are sampling at is indicative of the whole song? Would you average the whole thing?

    I'm not really a human, but I play one on earth. flash japh

      The BPM really doesn't necessarily change that much in modern music, zentara. Sure, there are songs that speed up and slow down, but these days most music is recorded to a click track or uses programmed drums. And even though music such as jazz is highly syncopated and the rhythms are highly complex, the drummer tends to keep a very steady beat with the hi hat. There are, of course, exceptions to the rule, so to speak -- but i think that you can get an accurate BPM measurement programatically with most songs. For those songs that do change time signature and tempos, you'd have to break them down into parts. For those that feature a drummer that can't keep good time, you'd probably have to take an average and round that number up or down to the nearest commonly used BPM.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        The BPM really doesn't necessarily change that much in modern music
        Thus speaks someone with very limited musical tastes :-)
Re: calculating the bpm of a song in mp3 format
by aquarium (Curate) on Apr 16, 2006 at 12:08 UTC
    you fail to mention "why" you're wanting this bpm information...as further guidance may be given. 2 + 2 = 4....but not if you're a farmer looking for subsidies on a farm going broke. If you want the BPM info to auto-classify music genera, then it may be only 50% or 75% effective. give us more clues.
    the hardest line to type correctly is: stty erase ^H