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.