Search code examples
caudiopcm

Remove channels from PCM


I have extended "libtinyalsa" (a very small version of ALSA) with a method to resample in- & outgoing PCM-audio (with libresample, e.g. from 48 kHz to 44.1 kHz).

Now I would like to extend it with a "downMixing" (is that the right word?) method.

I have 5.1 channel PCM and just need 2 channel stereo audio (left & right):

  • is "downMixing" the right word?
  • how can I do this in C?

I have no real knowledge about C and PCM - simple answers would be really welcome.

With kind regards & thanks in advance!


Solution

  • 5.1 PCM may be already encoded in such a way that it is effectively mixed down to 2 channels. If that is the case, you should be able to just ignore the 5.1 channels and treat it as if it were two channels. If this is the case but you need to get rid of all trace of 5.1 PCM, rather than just having it play correctly on a 2-channel output, you'd need to first use a 5.1 channel decoder library.

    If you actually have a genuine 5.1 channel encoded output, then you would get at it like this:

    for(i = 0; i < buffer_end; i += 6) {
        front_left = buffer[i + 0];
        front_right = buffer[i + 1];
        center = buffer[i + 2];
        lfe = buffer[i + 3]; // (sub-woofer)
        back_left = buffer[i + 4];
        back_right = buffer[i + 5];
    }
    

    The mixdown would then be:

    left = (front_left + back_left)/2 + (lfe + front_center)/4;
    right = (front_right + back_right)/2 + (lfe + front_center)/4;
    

    which would be interleaved in the same way, i.e.

    output_buffer[i] = left;
    output_buffer[i + 1] = right;
    

    Note that there are different ways of doing the mixdown, depending on your desired results and your constraints. But that is a whole can of worms.

    You also should ask yourself if you are really using the right tool for the job. Libraries exist that do all of these things, and ALSA is itself already capable of fairly sophisticated mixing. See here for some other solutions: http://www.halfgaar.net/surround-sound-in-linux