Pitch detection using TarsosDSP

pitch detection in android using tarsosdsp
tarsosdsp documentation
tarsosdsp android pitch detection
audio dispatcher android
audio analysis java
librosa java
java audio filter
edit audio android github

I am trying to make a application using android studio. The application I'm going to make is a game using our voice. It works like this. If, I sing any Do( or near Do, C# for example) my car moves right and moves left if I sing Re(or maybe near Re). The point is my app first needs to recognize the sound I am making. After that, if my sound is in the range of [Do to Do#], it recognizes it as a Do and moves right. I recently found an library "TarsosDSP" that can help my work. I used an example that is on link, to figure out how it works. My code is right below

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");
    }
}

I am using AP 21: Android 5.0 (Lollipop) and my Emulator is NEXUS 6 API 24 I also added this word below in the manifest

<uses-permission android:name="android.permission.RECORD_AUDIO">

But I have errors like below

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)

How can I fix this problem?? I also have to use at least android 6.0

AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);

Should not be called statically. So try calling it in onCreate or after onCreate method.

Pitch detection using TarsosDSP, AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);. Should not be called statically. So try calling it in onCreate or after onCreate  Pitch detection using TarsosDSP. I am trying to make a application using android studio. The application I'm going to make is a game using our voice. It works like this. If, I sing any Do( or near Do, C# for example) my car moves right and moves left if I sing Re(or maybe near Re).

In the virtual device check the app permissions. For whatever reason putting "uses-permission android:name="android.permission.RECORD_AUDIO"" doesn't always work.

JorenSix/TarsosDSP: A Real-Time Audio Processing , Contribute to JorenSix/TarsosDSP development by creating an account on GitHub. PitchDetector this demo application shows real-time pitch detection. When pitch is Audio Time Stretching – Implementation in Pure Java Using WSOLA an  To use TarsosDSP within an Android application you don't really need the javax.sound.* packages. What is needed however, is a bridge between incoming audio and the TarsosDSP API. The incoming audio can be from a mirophone or an audio file. In Android this bridge is provided by the android.media packages.

This problem happens because on API v23+ you need to grant permission at the runtime not only by adding the permissions in the manifest file. So to fix this error "startRecording() called on an uninitialized AudioRecord." You need to check Audio Record permission is granted at app start-up before firing any code relating to the Audio Record permission

Pitch Detection in android · Issue #23 · JorenSix/TarsosDSP · GitHub, been trying to incorporate this library (specifically pitch detection) into achieving this result (I am using the pitch detection function and want  TarsosDSP PitchDetection Issue. I am using the audiodispatcher from the TarsosDSPlibrary. The pitchdetection is used to detect sounds from the mic. Once detected, it switches to the next activity (which is a Maths quiz). After completing the quiz on the next activity, it returns to this activity and starts the process all over again.

TarsosDSP on Android, The following example connects an AudioDispatcher to the microphone of an Android device. Subsequently, a real-time pitch detection algorithm  The following are top voted examples for showing how to use be.tarsos.dsp.pitch.PitchDetectionResult. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

TarsosDSP, TarsosDSP-Android-x.x.jar Jar library for inclusion in Android projects, with source files included. PitchDetector this demo application shows real-time pitch detection. Audio Time Stretching – Implementation in Pure Java Using WSOLA an  Alternatively, we also have a pitch-shifting algorithm that allows the user to change the pitch without changing the speed. A resampling algorithm with related software package is also included. TarsosDSP consists of two onset detectors and several infinite impulse response (IIR) filters including low-pass, high-pass and band-pass filters.

Using TarsosDSP in Android for Pitch Detection, TarsosDSP는 타악기 히트 감지와 YIN, Mcleod Pitch Method 및 Dynamic Wavelet Algorithm Pitch Tracking 등과 같은 다양한 Pitch Detection  TarsosDSP is a collection of classes to do simple audio processing. It features an implementation of a percussion onset detector and two pitch detection algorithms: Yin and the Mcleod Pitch method. Also included is a Goertzel DTMF decoding algorithm and a time stretch algorithm (WSOLA).