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.

4

1 回答 1

3

TarsosDSP 已经有一个针对 android 的实现。他们有一个AudioDispatcherFactory类和 fromDefaultMicrophone(...) 方法。

因此,您可以使用此方法来初始化音频调度程序实例并向其添加任何可用的处理器。在你的情况下 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();
于 2016-05-01T21:46:07.570 回答