Soundtouch bpm iPhone

1.4k Views Asked by At

I'm trying to integrate a mechanism to calculate the BPM of the song in the iPod library(also on iphone). Searching on the web I found that the most used and reliable libraries to do this things is soundtouch.Anyone has experience with this library? It is computationally possible to make it run on the iPhone hardware?

1

There are 1 best solutions below

3
On

I have recently been using the code from the BPMDetect class of the soundtouch library succesfully. Initially compiled it on C++, later on translated the code to C# and lately I've been using the C++ code on an Android app through JNI. I'm not really familiar with development in iOS but I'm almost certain that it is possible what you're trying to do.

The only files you should use from the soundtouch source code are the following:

C++ files

  • BPMDetect.cpp
  • FIFOSampleBuffer.cpp
  • PeakFinder.cpp

Header files

  • BPMDetect.h
  • FIFOSampleBuffer.h
  • FIFOSamplePipe.h
  • PeakFinder.h
  • soundtouch_config.h
  • STTypes.h

At least these are the only ones I had to use to make it work.

The BPMDetect class recieves raw samples through its inputSamples() method, it's capable of calculating a bpm value even when the whole file is not yet loaded into its buffer. I have found that these intermediate values differ from the one obtained once the whole file is loaded, which is more accurate, in my experience.

Hope this helps.

EDIT:

It's a kind of complex process to explain in a comment so I'm going to edit the answer.

The gist of it is that you need your android app to consume native code. In order to do that, you need to compile the files listed above from the soundtouch library with the Android NDK toolset.

That will leave you with native code that will be able to process raw sound data, but you still need to get the data from the sound file, which you can do several ways, I think. The way I was doing it was using the FMOD library for Android, here's a nice example for that: FMOD for Android.

Supposing you declared a method like this in your C code:

void Java_your_package_YourClassName_cPlay(JNIEnv *env, jobject thiz)
{
    sound->play();
}

On the Android app you use your native methods in the following way:

public class Sound {
    // Native method declaration
    private native void cPlay();

    public void play()
    {
        cPlay();
    }
}

In order to have a friendlier API to work with you can create wrappers around these function calls.

I put the native C code I was using in a gist here.

Hope this helps.