1

My question is simple. How do I get PCM data from the mp3 file?

Now I found only ne way to get PCM bytes from the song, I loaded and played mp3 by MediaPlayer and tried listen to the change of waves by Visualizer.OnDataCaptureListener . This interface's method passes a byte[] bytes parameter. I tried to save all these bytes into the one byte array and then I passed this array to AudioTrack.write method and played it by AudioTrack.play method. Something like this:

    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
                public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                        int samplingRate) {
                       // allBytes += bytes
                }
        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
               }
, Visualizer.getMaxCaptureRate() / 2, true, false);
     }

and then

AudioTrack tr = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, 
                              AudioFormat.CHANNEL_OUT_STEREO,   
                              AudioFormat.ENCODING_PCM_16BIT,
                              allBytes.length, AudioTrack.MODE_STREAM);
tr.play();
tr.write(barr, 0, allBytes.length);

But unfortunately I didn't get expected results. The sound is playing but there are a lot of noise in the sound, quality is so bad, you can't actually identify what is singing there.

But it was some kinda of "hack method" to get PCM data. Can you say if there is a normal method to get PCM data from my mp3?

pleerock
  • 18,322
  • 16
  • 103
  • 128

1 Answers1

3

I am using the mpg123 native library for decoding my MP3. It decoded into PCM data in shorts not bytes. I am sure you can do the conversions. The decoder is very fast since it uses the NDK.

http://www.badlogicgames.com/wordpress/?p=446

Also,

I am not sure how big your files are but I would stream the bytes into the AudioTrack instead of putting the whole song in one big array.

nawlrus
  • 777
  • 1
  • 13
  • 27
  • yes, I would stream it, I used byte variable just for test. And I already found solution, I put my answer. Thanks for replay! – pleerock Apr 04 '12 at 13:00
  • 2
    @pleerock: Would you mind sharing your solution? I've been trying for weeks to get the PCM stream from Android apps without success... – t.mikael.d Oct 21 '14 at 12:51