Search code examples
audioaudio-processingpyo

Combining multiple input channels to one output channel audio live


I am trying to make my own basic mixer and wanted to know how I could take multiple channels of input audio and outputting all of the channels as one mixed audio source with controllable levels for each input channel. Right now I am trying to use pyo but I am unable to mix the channels in real-time.


Solution

  • here is some pseudo code to combine multiple input channels into a single output channel where each input channel has its own volume control in array mix_volume

    max_index = length(all_chan[0])  // identify audio buffer size
    
    all_chan   // assume all channels live in a two dimensional array where 
               // dimension 0 is which channel and dim 1 is index into each audio sample
    
    mix_volume  // array holding multiplication factor to control volume per channel
                // each element a floating point value between 0.0 and 1.0
    
    output_chan  //  define and/or allocate your output channel buffer
    
    for index := 0; index < max_index; index++ {
    
        curr_sample := 0  // output audio curve height for current audio sample
    
        for curr_chan := 0; curr_chan < num_channels; curr_chan++ {
    
            curr_sample += (all_chan[curr_chan][index] * mix_volume[curr_chan])
        }
    
        output_chan[index] = curr_sample / num_channels  // output audio buffer
    }
    

    the trick to perform above on a live stream is to populate the above all_chan audio buffers inside an event loop where you copy into these buffers the audio sample values for each channel then execute above code from inside that event loop ... typically you will want your audio buffers to have about 2^12 ( 4096 ) audio samples ... experiment using larger or smaller buffer size ... too small and this event loop will become very cpu intensive yet too large and you will incur an audible delay ... have fun

    you may want to use a compiled language like golang YMMV