8

In my Android application I am recording the user's voice which I save as a .3gp encoded audio file.

What I want to do is open it up, i.e. the sequence x[n] representing the audio sample, in order to perform some audio signal analysis.

Does anyone know how I could go about doing this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
JDS
  • 16,388
  • 47
  • 161
  • 224

1 Answers1

7

You can use the Android MediaCodec class to decode 3gp or other media files. The decoder output is standard PCM byte array. You can directly send this output to the Android AudioTrack class to play or continue with this output byte array for further processing such as DSP. To apply DSP algorithm the byte array must be transform into float/double array. There are several steps to get the byte array output. In summary it looks like as follows:

  1. Instantiate MediaCodec

    String mMime = "audio/3gpp"
    MediaCodec  mMediaCodec = MediaCodec.createDecoderByType(mMime);
    
  2. Create Media format and configure media codec

    MediaFormat mMediaFormat = new MediaFormat();
    mMediaFormat = MediaFormat.createAudioFormat(mMime,
        mMediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE),
        mMediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT));
    
    mMediaCodec.configure(mMediaFormat, null, null, 0);
    mMediaCodec.start();
    
  3. Capture output from MediaCodec ( Should process inside a thread)

    MediaCodec.BufferInfo buf_info = new MediaCodec.BufferInfo();
    int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(buf_info, 0);
    byte[] pcm = new byte[buf_info.size];
    mOutputBuffers[outputBufferIndex].get(pcm, 0, buf_info.size);
    

This Google IO talk might be relevant here.

jaybers
  • 1,991
  • 13
  • 18
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
  • Thank you very much for getting me started. I will test and implement some of this code later this week and likely accept your answer. – JDS Jan 21 '13 at 21:34
  • Can you please tell how to add AudioTrack that will play the whole inputStream of the 3gp file ? – android developer Apr 11 '16 at 14:49
  • Once you read the data into byte array from MediaCodec, it could be played using AudioTrack class write method. Please see write method signature here, http://developer.android.com/reference/android/media/AudioTrack.html – Shamim Ahmmed Apr 14 '16 at 17:02
  • @shamimz How do we pass a file as input to this MediaCodec? I currently only have a filePath that points to a 3gp file. – Zhen Liu Dec 03 '16 at 05:10