0

I am currently using getID3() to read mp3 tag data such as artist name, filesize, duration etc. All of this happens on the fly when a user uploads a file to my site.

However I would like to automatically detect the tempo in bpm of each song in order to save it to my database.

So in a nutshell I am looking for either a command line utility that can be run from a centOS server or a php based script that will take an mp3 or wav file, analyze it and return the tempo as bpm.

I have found soundstretch but which can apparently do just that but for some reasons seem unable to get it installed.

Does anyone have any ideas?

EDIT: I have finally managed to successfully install soundtouch/soundstretch.

I would like to call them dynamically from within my php upload script so that the return bpm value can be added to the database.

I have tried to following with no success...

$bpm = exec("soundstretch $filename -bpm");

assuming that the variable $bpm would now contain the bpm. I must be misunderstanding how soundtouch works. unfortunately the documentation is minimal.

How would I go about collecting the returned bpm and storing it as a variable in order to save it into my database.

gordyr
  • 6,078
  • 14
  • 65
  • 123
  • 1
    "for some reasons" , why don't you post that error instead? – J-16 SDiZ Jan 06 '12 at 02:09
  • From reading it appears that the bpm detection in soundtouch/soundstretch is flakey at best. I was hoping someone had some other suggestions. – gordyr Jan 06 '12 at 02:12
  • 1
    All bpm detection is flakey, all you can do is find one suck less. – J-16 SDiZ Jan 06 '12 at 02:21
  • possible duplicate of [Bpm audio detection Library](http://stackoverflow.com/questions/477944/bpm-audio-detection-library) – J-16 SDiZ Jan 06 '12 at 02:22
  • Point taken... I have updated my original question now that I have managed to install soundtouch/soundstretch – gordyr Jan 06 '12 at 02:49

3 Answers3

2

Old thread, but maybe it helps someone.

At first convert the mp3 to wav. I have noticed that it works with it the best. Seems that soundstretch doesn't return the result into the resutl of shell_exec so i'm using a additional file. There could be done some improvement if you are knowing linux better than me ;-). If you need the bpm of a track one after one it works.

// create new files, because we don't want to override the old files
$wavFile = $filename . ".wav";
$bpmFile = $filename . ".bpm";

//convert to wav file with ffmpeg
$exec = "ffmpeg -loglevel quiet -i \"" . $filename . "\" -ar 32000 -ac 1 \"" . $wavFile . "\"";
$output = shell_exec($exec);

// now execute soundstretch with the newly generated wav file, write the result into a file
$exec = "soundstretch \"" . $wavFile . "\" -bpm  2> " . $bpmFile;
shell_exec($exec);

// read and parse the file 
$output = file_get_contents($bpmFile);
preg_match_all("!(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)!is", $output, $match);

// don't forget to delete the new generated files
unlink($wavFile);
unlink($bpmFile);

// here we have the bpm
echo $match[0][2];
simon
  • 3,378
  • 2
  • 22
  • 32
0

Check out php-bpm-detect by deseven, it is very simple php wrapper and works great. You just need to install ffmpeg and soundstrech dependecies:

Basic Usage:

$bpm_detect = new bpm_detect("file.mp3");  
echo $bpm_detect->detectBPM(); 
Jeff
  • 1,018
  • 1
  • 15
  • 33
0

Here is the bash command line to get the BPM as a text with SoundStretch.

soundstretch audio.wav -bpm 2>&1 | grep "Detected BPM" | awk '{print $4}'

SoundStretch is using stderr instead of stdout as the command line output. You can check the details in their source code (SoundStretch/main.cpp)

QuestionDriven
  • 360
  • 4
  • 10