A problem with performance using AudioRecord class

edited November 2015 in Android Mode

Greetings! In fact it is not a real problem, but i need some explanations about the reason causing that issue. So, i was making some simple dictaphone for my android device with a realtime waveform drawing. When i initialize AudioRecord object using stereo channels it's working fine, i got ~30 fps. But when i'm trying to use mono regime my fps falls to 12-15. Here is some code:

constants i'm using:

final int RECORDER_SAMPLERATE = 44100;                                
final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

recorder initializing:

bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

drawing waveform:

bufferReadResult = audioRecord.read(buffer, 0, inShorts);
x = 0;
    for(int i = 0; i < buffer.length; i += inc){
        newY = height/2 + (int)map(buffer[i], -40000,40000, -300,300); 
        newX = x+1;
        line(x,y,newX, newY);  
        x = newX;
        y = newY;
    }

inShorts it's just bufferSize/2 cause i'm using short arrays. In fact, i should divide mybuffer array in two arrays for each channel, but here where the problem is. When i'm trying to draw a single channel or use AudioFormat.CHANNEL_IN_MONO instead of stereo, i've got very low performance. The question is: why it is so? I checked that my microphone is a mono device.

Answers

  • @aiz7103::: there is (as for me!!!) not any "reason" why changing from stereo to mono makes the fps falls but as your code (which is a standard android one) is only for stereo i cannot guess what happens. (what is "inc"???, how do you you change from buffer to shorts[] and so on; why are you talking about buffer.size as you are using [inShorts]???) give a code which does not work...

  • @akenaton ok, here is the code that anyone can launch, it also has some answers on your questions:

    import android.media.AudioRecord;
    import android.media.AudioFormat;
    import android.media.MediaRecorder;
    
    final int RECORDER_SAMPLERATE = 44100;                                
    final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
    final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    
    AudioRecord audioRecord  = null;
    short[]     buffer       = null;     
    float       inc;                     
    int         bufferSize;              //buffer[] size in bytes(8bit)
    int         inShorts;                //buffer[] size in shorts -> bufferSize/2
    int         bufferReadResult;   
    int         x, y, newX, newY;
    
    void setup(){
      bufferSize =         AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);              
      audioRecord.startRecording();
      inShorts = bufferSize/2;
      buffer  = new short[inShorts];
      inc = (buffer.length/width); 
    }
    
    void draw(){
       background(255);
       bufferReadResult = audioRecord.read(buffer, 0, inShorts);
       x = 0;
       for(int i = 0; i < buffer.length; i += inc){
           newY = height/2 + (int)map(buffer[i], -40000,40000, -300,300); 
           newX = x+1;
           line(x,y,newX, newY);  
           x = newX;
           y = newY;
       }   
    }
    
    void stop() {
      audioRecord.stop();
      audioRecord.release();
    }
    

    it works great. All i need to make my fps to fall down it's just change: final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO; with:

    final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;

  • @MarcoReverse=== i tested your code with P3 and a real device; it works fine stereo or mono....in the two cases i get 26 or 27 fps. So i cannot see what to do...

  • @akenaton intresting. Maybe it's all about my device.

  • Answer ✓

    @aiz7103=== yes, probably, or with your os; android docs says that only stereo mode is "garanteed" to work, which means that some device with some os fail with mono.

  • @akenaton thanks for your time.

Sign In or Register to comment.