Skip to content

Converting the Number of Input and Output Channels

Phil Schatzmann edited this page Mar 27, 2023 · 15 revisions

We can use the ChannelFormatConverterStream or the ChannelFormatConverterStreamT class to convert the number of input and output channels: we just need to wrap the final input or output class in the constructor. The class ending with a T expects a type specification. Alternatively you can also use the more generic FormatConverterStream which also supports the change of the bits_per_sample (see here). These classes are supporting both: the conversion on the input and on the output side. Here are examples for the different scenarios:

Increasing Output Channels

The input is providing 1 channel. We convert the data to 2 channels so that we can output to a stream with 2 channels.

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 1;                                           
uint8_t to_channels = 2;                                            
SineWaveGenerator<int16_t> sine_wave(32000);                         // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(sine_wave);                  // Stream generated from sine wave
CsvStream<int16_t> out(Serial, to_channels);                         // Output to Serial
ChannelFormatConverterStreamT<int16_t> conv(out);
StreamCopy copier(conv, in_stream);                                  // copies sound to out

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  sine_wave.begin(from_channels, sample_rate, N_B4);
  conv.begin(from_channels, to_channels);
  in_stream.begin();

  out.begin();
}

void loop(){
    copier.copy();
}

Decreasing Output Channels

The input is providing 2 channels. We convert the data to 1 channel so that we can output to a stream with 1 channels.

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 2;                                           
uint8_t to_channels = 1;                                             
SineWaveGenerator<int16_t> sine_wave(32000);                         // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(sine_wave);                  // Stream generated from sine wave
CsvStream<int16_t> out(Serial, to_channels);                         // Output to Serial
ChannelFormatConverterStreamT<int16_t> conv(out);
StreamCopy copier(conv, in_stream);                                  // copies sound to out

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  sine_wave.begin(from_channels, sample_rate, N_B4);
  conv.begin(from_channels, to_channels);
  in_stream.begin();

  out.begin();
}

void loop(){
    copier.copy();
}

Increasing Input Channels

The input is providing 1 channel. We convert the data to 2 channels so that we can copy to a stream with 2 channels.

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 1;                                           
uint8_t to_channels = 2;                                             
SineWaveGenerator<int16_t> sine_wave(32000);                         // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(sine_wave);                  // Stream generated from sine wave
CsvStream<int16_t> out(Serial, to_channels);                         // Output to Serial
ChannelFormatConverterStreamT<int16_t> conv(in_stream);
StreamCopy copier(out, conv);                                       // copies converted sound to out

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  sine_wave.begin(from_channels, sample_rate, N_B4);
  conv.begin(from_channels, to_channels);
  in_stream.begin();

  out.begin();
}

void loop(){
    copier.copy();
}

Decreasing Input Channels

The input is providing 2 channels. We convert the data to 1 channel so that we can copy to a stream with 1 channels.

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 2;                                          
uint8_t to_channels = 1;                                             
SineWaveGenerator<int16_t> sine_wave(32000);                         // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(sine_wave);                  // Stream generated from sine wave
CsvStream<int16_t> out(Serial, to_channels);                         // Output to Serial
ChannelFormatConverterStreamT<int16_t> conv(in_stream);
StreamCopy copier(out, conv);                                       // copies converted sound to out

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  sine_wave.begin(from_channels, sample_rate, N_B4);
  conv.begin(from_channels, to_channels);
  in_stream.begin();

  out.begin();
}

void loop(){
    copier.copy();
}

Alternative Logic: Using Converters

We can also reduce the number of channels by using a ChannelReducer Converters:

#include "AudioTools.h"
#include "AudioLibs/PortAudioStream.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 2;                                          
uint8_t to_channels = 1;                                             
SineWaveGenerator<int16_t> sine_wave(32000);                      // subclass of SoundGenerator with max amplitude of
GeneratedSoundStream<int16_t> in_stream(sine_wave);               // Stream generated from sine wave
CsvStream<int16_t> out(Serial, to_channels);                         // Output to Serial
StreamCopy copier(out, in_stream);                                // copies sound to out
ChannelReducer<int16_t> reducer(from_channels,to_channels);

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  sine_wave.begin(from_channels, sample_rate, N_B4);
  in_stream.begin();
  
  out.begin();
}

void loop(){
    copier.copy(reducer);
}
Clone this wiki locally