android - 使用 TarsosDSP 进行音调检测

标签 android detection pitch

我正在尝试使用 android studio 制作一个应用程序。我要制作的应用程序是一个使用我们的声音的游戏。它是这样工作的。如果我唱任何 Do(或靠近 Do,例如 C#)我的车向右移动,如果我唱 Re(或可能靠近 Re)则向左移动。重点是我的应用程序首先需要识别我发出的声音。之后,如果我的声音在[Do to 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 我还在 list 下面添加了这个词

<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

最佳答案

在虚拟设备中检查应用权限。无论出于何种原因,放置“uses-permission android:name="android.permission.RECORD_AUDIO""并不总是有效。

关于android - 使用 TarsosDSP 进行音调检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47635687/

相关文章:

圆形检测

java - 检测复制或相似的文本 block

audio - 如何使用 ffmpeg 可靠地同时改变音调和节奏

java - 如何在不丢弃中间字符串的情况下验证在 EditText 中键入的字符串?

Android Studio 2.3.2 不打开 Android *.xml 布局文件

android - 如何使 ftrace function_graph 跟踪器可以使用 linux 内核函数?

iphone - 如何检测 iPhone 上的声音频率/音调?

java - 我需要做什么才能让我的 Web 应用程序在手机上运行?

java - 禁用Java应用程序中运行的屏幕捕获软件

iphone - CoreAudio 节奏变化 (iOS)