I added some basic simple audio mixing functionality to my Arduino Audio Tools Library where we can mix multiple input audio streams together.

Example Sketch

We just need to define a Mixer and then add the different input streams that need to be mixed. Optionally we could define a weight parameter to the add method, which we did not do in the example below:

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

uint16_t sample_rate=32000;
uint8_t channels = 2;                                       
SineWaveGenerator<int16_t> sineWave1(32000);                
SineWaveGenerator<int16_t> sineWave2(32000);                
GeneratedSoundStream<int16_t> sound1(sineWave1); 
GeneratedSoundStream<int16_t> sound2(sineWave2);            
Mixer<int16_t> mixer;
AudioKitStream out; 
StreamCopy copier(out, mixer);  // copies sound to i2s

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

  // Define streams to be mixed together
  mixer.add(sound1);
  mixer.add(sound2);

  // start I2S
  Serial.println("starting I2S...");
  auto config = out.defaultConfig(TX_MODE);
  config.sample_rate = sample_rate; 
  config.channels = channels;
  config.bits_per_sample = 16;
  out.begin(config);

  // Setup sine wave
  sineWave1.begin(channels, sample_rate, N_B4);
  sineWave2.begin(channels, sample_rate, N_E4);
  Serial.println("started...");
}

// Arduino loop - copy sound to out 
void loop() {
  copier.copy();
}

The implementation of the Mixer class was pretty easy. We just need to sum all samples*weight from all streams and divide by the total weight. Of cause this only works if all streams have consistent audio information (sample rate, channels, bits_per_sample) !
The (potentially updated) example can also be found on Github.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.