javaandroidtarsosdsp

TarsosDSP Clap Detection


I decided to try out developing for android studio and designed an app that listens for a clap then performs an action of some sort. My problem lies in using TarsosDSP.

I want to run the Listener class as an IntentService, so I can lock my phone and it still listens. However I'm having trouble setting up the AudioDispatcher and TarsosDSPAudioInputStream.

Here's the onHandleIntent so far:

protected void onHandleIntent(Intent Intent) {
        AudioDispatcher mDispatcher = new AudioDispatcher(TarsosDSPAudioInputStream, SAMPLE_RATE, BUFFER_OVERLAP);
        double threshold = 8;
        double sensitivity = 20;

        PercussionOnsetDetector mPercussionDetector = new PercussionOnsetDetector(22050, 1024,
                new OnsetHandler() {

                    @Override
                    public void handleOnset(double time, double salience) {
                        Log.d(TAG, "Clap detected!");
                    }
                }, sensitivity, threshold);

        mDispatcher.addAudioProcessor(mPercussionDetector);
        new Thread(mDispatcher).start();
    }

I guess more specifically, I'm not sure how I should define the TarsosDSPAudioInputStream object. The documentation says it's an interface, but I don't know how that works. I'm super new to Android Studio and java but have a year of experience with C++ as it's part of my major.


Solution

  • TarsosDSP already has an implementation for android. They have an AudioDispatcherFactory class and fromDefaultMicrophone(...) method.

    So you can use this method to initialize audio dispatcher instance and add any available processors to it. In your case PercussionOnsetDetector

        AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
    
        double threshold = 8;
        double sensitivity = 20;
    
        PercussionOnsetDetector mPercussionDetector = new PercussionOnsetDetector(22050, 1024,
                new OnsetHandler() {
    
                    @Override
                    public void handleOnset(double time, double salience) {
                        Log.d(TAG, "Clap detected!");
                    }
                }, sensitivity, threshold);
    
        mDispatcher.addAudioProcessor(mPercussionDetector);
        new Thread(mDispatcher,"Audio Dispatcher").start();