質問

I am creating an app that detects frequencies of sound playing through the mic. The analysis is done in an AsyncTask. It works when I try one note.

However, when I try to analyse 6 notes, say from different guitar strings, one after the other (with a delay of a second or so in between) it only captures maybe one more.

This is some of the LogCat output I get:

11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=-1
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [SKW]do_route_audio_rpc(0, 1, 0)
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): msm72xx_enable_audpp: 0x0000
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=0
11-22 19:50:30.523: ERROR/AudioHardwareMSM72XX(1296): Cannot open /dev/msm_pcm_in errno: 16
11-22 19:50:30.533: ERROR/AudioRecord(23241): Could not get audio input for record source 1
11-22 19:50:30.533: ERROR/AudioRecord-JNI(23241): Error creating AudioRecord instance: initialization check failed.
11-22 19:50:30.533: ERROR/AudioRecord-Java(23241): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.

I deleted any repetitions for you all.

Here is the jist of my class:

static AudioRecord recorder;
static short[] audioData;
static int bufferSize;
static int samplerate = 8000;         
static boolean recorded = false;

@Override
protected Integer doInBackground(params) {

bufferSize= AudioRecord.getMinBufferSize(samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT)*2; 

recorder = new AudioRecord (AudioSource.MIC,samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT,bufferSize); 

    audioData = new short [bufferSize]; //short array that pcm data is put into.

    int recordingLoops = 0;
    while (recordingLoops < 4 || recorded) {  //loop until recording is running

    if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED)
        if(recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
              recorder.startRecording();                     
        else {         
           recorder.read(audioData,0,bufferSize); 

           getFrequencyFromAudiodata(audioData);

           recorded = true;
             }        
    recordingLoops++;  
}

    if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) 
    {
        killRecorder();
    }

    return 1;
}

Have you guys got any idea what is happening? I have done some looking around but I seem to just be running into people saying Android is 'full of bugs'.

Any help would be massively appreciated.

Thanks, Ben

役に立ちましたか?

解決

The answer lay in this:

while (recordingLoops < 4 || recorded) 

I changed it to:

while (!recorded) 

And only set recorder to true when the array of audio data was sufficiently populated. This meant it would loop until perfect. thanks for your help Dave!

他のヒント

You might look at this answer, it looks like some thought went into handling the threading associated with AudioRecord:

Android AudioRecord class - process live mic audio quickly, set up callback function

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top