user2145312
user2145312

Reputation: 928

TarsosDSP for Android AudioRecord Object not Initialising from within LibGDX project

I am attempting to (re)create an acoustic tuner I coded some time ago using a fork of the Original TarsosDSP project making it usable with Android. This time around I am using the updated TarsosDSP for Android project by JorenSix and as far as I can tell I have not changed anything structurally important in my code from previous, but I am receiving the following error when I try to run the application:

E/AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
D/libEGL: eglTerminate EGLDisplay = 0x7f90cf2e28
E/AndroidRuntime: FATAL EXCEPTION: GLThread 2218
              Process: com.fraserjohnstone.tuner, PID: 19923
              java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
                  at android.media.AudioRecord.startRecording(AudioRecord.java:943)
                  at be.tarsos.dsp.io.android.AudioDispatcherFactory.fromDefaultMicrophone(Unknown Source)
                  at com.fraserjohnstone.tuner.screens.TunerScreen.initTuner(TunerScreen.java:238)
                  at com.fraserjohnstone.tuner.screens.TunerScreen.show(TunerScreen.java:123)
                  at com.badlogic.gdx.Game.setScreen(Game.java:61)
                  at com.fraserjohnstone.tuner.screens.SplashScreen.goToTunerScreen(SplashScreen.java:148)
                  at com.fraserjohnstone.tuner.screens.SplashScreen.render(SplashScreen.java:131)
                  at com.badlogic.gdx.Game.render(Game.java:46)
                  at com.fraserjohnstone.tuner.Tuner.render(Tuner.java:73)
                  at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
                  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1649)
                  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1354)
E/AndroidGraphics: waiting for pause synchronization took too long; assuming deadlock and killing

I am initialising the tuner in the method initTuner() which is called from the constructor:

private void initTuner(){
    if(dispatcher == null){
                //set initial tuner values
                algo = PitchEstimationAlgorithm.FFT_YIN;
                currentHertz = 440.00;              
                sampleRate = 44100;                 
                bufferSize = 2048;             
                sampleOverlap = 1536;             
                offsetInCents = 0;                  
                sharpOrFlat = "";                   
                targetNote = "A";                   
                targetRotation = 0;                 

                dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(sampleRate, bufferSize, sampleOverlap);

                PitchDetectionHandler printPitch = new PitchDetectionHandler() {
                    @Override
                    public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
                        double pitch = pitchDetectionResult.getPitch();
                        if(pitch != -1 && !audioEvent.isSilence(silenceThreshold)){
                            audioDetected = true;
                            currentHertz = pitch;
                        }
                        else{
                            audioDetected = false;
                            offsetInCents = 0.00;
                        }
                    }
                };
                AudioProcessor audioProcessor = new PitchProcessor(algo, sampleRate, bufferSize, printPitch);
                dispatcher.addAudioProcessor(audioProcessor);

        new Thread(dispatcher,"Audio Dispatcher").start();
    }
}

And my android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fraserjohnstone.tuner"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="24" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/GdxTheme" >
    <activity
        android:name="com.fraserjohnstone.tuner.AndroidLauncher"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Any help would be appreciated.

Upvotes: 0

Views: 717

Answers (1)

user2145312
user2145312

Reputation: 928

Solved.

Even though I was declaring the permission RECORD_AUDIO in my android manifest file, I did not realise that it was a dangerous permission and thus permission had to be obtained at run-time as I was targeting Android 6.0 and higher.

Upvotes: 1

Related Questions