3

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.

Ausche
  • 139
  • 2
  • 12

2 Answers2

3

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();
ugur
  • 3,604
  • 3
  • 26
  • 57
  • Okay I understand now. So you override what happens when the percussion detector detects the clap. Then this implementation is put into the `mDispatcher` and `Thread` is started in the very last line. This makes sense, thank you. It's always a pain trying to understand new libraries. – Ausche May 01 '16 at 23:26
  • No pain no gain :) and fortunately tarsosdsp is a well documented lib. thanks to the authors. – ugur May 01 '16 at 23:43
0

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();
mahamrauf
  • 21
  • 4