I am currently providing quite a few audio processing classes which can be used to change the audio format.

So far, the FormatConverterStream could be used to change the bits_per_sample and the number of channels only.

I thought it might be cool, if we could use this class for changing the sample rate as well, so I finally managed to commit this change!

Here is an example which changes

  • The sample rate from 32000 to 16000
  • The channels from 2 to 1
  • The bits_per_sample from 32 to 16

The following audio chain is implemented GeneratedSoundStream -> FormatConverterStream – copy -> I2SStream. The data is read with the audio information defined in the from variable and writes it with a different format (specified with the to variable) to I2S. In this example the conversion is happening on the reading/input side!


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

AudioInfo from(32000,2,32);
AudioInfo to(16000,1,16);
SineWaveGenerator<int32_t> sineWave;                
GeneratedSoundStream<int32_t> sound(sineWave); // Stream generated from sine wave
I2SStream out;   // or any other e.g. AudioKitStream, CsvStream<int16_t> out(Serial); 
FormatConverterStream converter(sound);  // or use converter(out)
StreamCopy copier(out, converter);       //        copier(converter, sound);     

// Arduino Setup
void setup(void) {  
  // Open Serial 
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  // Setup Input
  sineWave.begin(from, N_B4);

  // Define Converter
  converter.begin(from, to);

  // Start Output
  Serial.println("starting I2S...");
  auto config = out.defaultConfig(TX_MODE);


// Arduino loop - copy sound to out 
void loop() {

Of cause this is also working on the output side: In fact it is even better to do the conversion this way, because we will use less memory. This only requires a small change for the example above:

FormatConverterStream converter(out);  
StreamCopy copier(converter, sound);       


I have updated the documentation on Github
You can also find the demo example there.

Final Comments

This class is very flexible and convenient to use. However if you need to save some milliseconds in your processing, it is a bit more efficient to use the dedicated class if you need to change only a single parameter: e.g. the sample_rate:

Categories: ArduinoMachine Sound


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *