TarsosDSP Clap Detection

3k Views Asked by At

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.

2

There are 2 best solutions below

2
On BEST ANSWER

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();
0
On

The accepted answer is the right one, however it does not detect clap with

threshold = 8 

for me. The following code detects the clap well:

 final AudioDispatcher fromDefaultMicrophone = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
        double threshold = 6; // lower it a bit
        double sensitivity = 20;

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

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

        fromDefaultMicrophone.addAudioProcessor(mPercussionDetector);
        new Thread(fromDefaultMicrophone,"Audio Dispatcher").start();