TarsosDSP拍手检测

3
我决定尝试开发Android Studio,并设计了一个应用程序,它可以监听拍手声并执行某些操作。我的问题在于使用TarsosDSP。
我想将Listener类作为IntentService运行,这样我就可以锁定手机而它仍在监听。然而,我在设置AudioDispatcher和TarsosDSPAudioInputStream方面遇到了麻烦。
以下是目前的onHandleIntent内容:
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();
    }

我想更具体地说,我不确定如何定义TarsosDSPAudioInputStream对象。 文档中说它是一个接口,但我不知道它是如何工作的。我对Android Studio和Java非常新手,但我在C++方面有一年的经验,因为这是我的专业课程之一。

2个回答

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

好的,我现在明白了。所以你覆盖了当打击检测器检测到拍手时发生的事情。然后这个实现被放入mDispatcher中,并且在最后一行启动了Thread。这很有道理,谢谢。尝试理解新库总是很痛苦的。 - Ausche
没有付出就没有收获 :) 幸运的是,tarsosdsp 是一个文档齐全的库。感谢作者。 - ugur

0

被接受的答案是正确的,但它无法检测到拍手声。

threshold = 8 

对我来说。 以下代码可以很好地检测到拍手声:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接