Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono to Stereo conversion

Tags:

c

algorithm

audio

I have the following issue here: I get a block of bytes (uint16_t*) representing audio data, and the device generating them is capturing mono sound, so obviously I have mono audio data, on 1 channel. I need to pass this data to another device, which is expecting interleaved stereo data (so, 2 channels). What I want to do is basically duplicate the 1 channel in data so that both channels of the stereo data will contain the same bytes. Can you point me to an efficient algorithm doing this?

Thanks, f.

like image 919
Ferenc Deak Avatar asked Jun 09 '11 12:06

Ferenc Deak


People also ask

Can mono be converted to stereo?

You can convert audio files from mono to stereo and from stereo to mono. Converting a mono file into a stereo file produces an audio file that contains the same material in both channels, for example for further processing into real stereo.


1 Answers

If you just want interleaved stereo samples then you could use a function like this:

void interleave(const uint16_t * in_L,     // mono input buffer (left channel)
                const uint16_t * in_R,     // mono input buffer (right channel)
                uint16_t * out,            // stereo output buffer
                const size_t num_samples)  // number of samples
{
    for (size_t i = 0; i < num_samples; ++i)
    {
        out[i * 2] = in_L[i];
        out[i * 2 + 1] = in_R[i];
    }
}

To generate stereo from a single mono buffer then you would just pass the same pointer for in_L and in_R, e.g.

interleave(mono_buffer, mono_buffer, stereo_buffer, num_samples);
like image 178
Paul R Avatar answered Oct 12 '22 06:10

Paul R