Skip to content

Converting the Number of Input and Output Channels

Phil Schatzmann edited this page Jun 17, 2022 · 16 revisions

We can use the ChannelFormatConverterStream class to convert the number of input and output channels: we just need to wrap the final input or output class in the constructor. This class is supporting both: the conversion on the input and on the output side. Here are examples for the different scenarios:

Increasing Output 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
ChannelFormatConverterStream<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

#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
ChannelFormatConverterStream<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

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 1;                                           // The stream will have 2 channels 
uint8_t to_channels = 2;                                             // The stream will have 2 channels 
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
ChannelFormatConverterStream<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

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint8_t from_channels = 2;                                           // The stream will have 2 channels 
uint8_t to_channels = 1;                                             // The stream will have 2 channels 
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
ChannelFormatConverterStream<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();
}

Clone this wiki locally