使用TarsosDSP进行音高检测

时间:2017-12-04 14:35:29

标签: android detection pitch

我正在尝试使用android studio制作应用程序。我要做的应用是使用我们的声音的游戏。它的工作原理如下。如果,我唱任何Do(或在Do附近,例如C#),我的车向右移动,如果我唱Re(或者可能在Re附近),则向左移动。关键是我的应用程序首先需要识别我正在制作的声音。之后,如果我的声音在[Do Do Do#]的范围内,它会将其识别为Do并向右移动。 我最近找到了一个可以帮助我工作的图书馆“TarsosDSP”。我使用link上的示例来弄清楚它是如何工作的。我的代码就在下面

package sogangee.sibal3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.AudioProcessor;
import be.tarsos.dsp.io.android.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;

public class MainActivity extends AppCompatActivity {

AudioDispatcher dispatcher =
        AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
TextView pitchText;
TextView noteText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pitchText = (TextView) findViewById(R.id.pitchText);
    noteText = (TextView) findViewById(R.id.noteText);

    PitchDetectionHandler pdh = new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult res, AudioEvent e){
            final float pitchInHz = res.getPitch();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    processPitch(pitchInHz);
                }
            });
        }
    };
    AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(pitchProcessor);
}

public void processPitch(float pitchInHz) {

    pitchText.setText("" + pitchInHz);

    if(pitchInHz >= 110 && pitchInHz < 123.47) {
        //A
        noteText.setText("A");
    }
    else if(pitchInHz >= 123.47 && pitchInHz < 130.81) {
        //B
        noteText.setText("B");
    }
    else if(pitchInHz >= 130.81 && pitchInHz < 146.83) {
        //C
        noteText.setText("C");
    }
    else if(pitchInHz >= 146.83 && pitchInHz < 164.81) {
        //D
        noteText.setText("D");
    }
    else if(pitchInHz >= 164.81 && pitchInHz <= 174.61) {
        //E
        noteText.setText("E");
    }
    else if(pitchInHz >= 174.61 && pitchInHz < 185) {
        //F
        noteText.setText("F");
    }
    else if(pitchInHz >= 185 && pitchInHz < 196) {
        //G
        noteText.setText("G");
    }
}

我使用的是AP 21:Android 5.0(Lollipop),我的仿真器是NEXUS 6 API 24 我还在清单

中添加了以下这个词
<uses-permission android:name="android.permission.RECORD_AUDIO">

但我有下面的错误

  

12-04 14:28:16.598 3448-3448/sogangee.sibal3 E/AudioRecord: AudioFlinger could not create record track, status: -1 12-04 14:28:16.602 3448-3448/sogangee.sibal3 E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 12-04 14:28:16.603 3448-3448/sogangee.sibal3 E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 12-04 14:28:16.606 3448-3448/sogangee.sibal3 E/AndroidRuntime: FATAL EXCEPTION: main Process: sogangee.sibal3, PID: 3448 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{sogangee.sibal3/sogangee.sibal3.MainActivity}: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. at android.media.AudioRecord.startRecording(AudioRecord.java:976) at be.tarsos.dsp.io.android.AudioDispatcherFactory.fromDefaultMicrophone(Unknown Source) at sogangee.sibal3.MainActivity.<init>(MainActivity.java:20) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我该如何解决这个问题?我还必须至少使用android 6.0

3 个答案:

答案 0 :(得分:2)

在虚拟设备中检查应用程序权限。无论出于什么原因,使用&#34;使用权限android:name =&#34; android.permission.RECORD_AUDIO&#34;&#34;并不总是有效。

答案 1 :(得分:0)

AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);

不应该静态调用。因此,请尝试在 onCreate onCreate方法后将其称为

答案 2 :(得分:0)

发生此问题是因为在API v23 +上,您需要在运行时授予权限,而不仅仅是通过在清单文件中添加权限。 所以要解决这个错误 “startRecording()在未初始化的AudioRecord上调用。” 您需要在启动应用程序启动时检查音频记录权限,然后才能解除与“音频记录”权限相关的任何代码