Search code examples
caudiofftpcm

Extract left channel PCM data from MPG123 output


I'm using the MPG123 library to decode MP3s to PCM data. I want to apply a FFT to the PCM data, specifically the left channel's. The mpg123_read function populates a char array with the PCM data. How can I extract the left channel's PCM data from this char array?


Solution

  • Assuming the audio is in stereo, once you have the raw PCM in the two first bytes correspond to the left channel audio, and two that follow to the right channel audio, so you can treat them as an shorts then the first short represents left channel and second right channel. So if you want to get the left channel data you will have to make a loop with the input raw PCM and save every one short. like this:

    void demux(short in[], short left[], short right[], int len) {
        for (int i=0; i<len/2; i+2) {
           left[i] = in[i];
           right[i+1] = in[i+1];
        }
    }
    

    where in has the raw input pointer, left the left buffer pointer, right the right buffer pointer, and len the length of the input stream in bytes.