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.


9 Comments

Luis Miguel · 21. March 2024 at 8:42

Good morning:
Is there a way to mix with ESP32 without the audiokit board? When I compile everything goes well, but I don’t get sound and I guess it’s because I’m just using the ESP32.
Thank you so much.

Carsten · 16. January 2024 at 20:45

Nice idea, so I have create simple questions in my mind.

I have a lot of ideas to create a simple full digital mixer with arduino nanos and rotary encoders. Is it possible ? Currently I have programmed a small VU-meter as first idea.

    pschatzmann · 25. January 2024 at 15:35

    No, the nano is too slow and has not enough RAM!

      Lawal · 23. April 2024 at 21:38

      pls I need the circuit diagram that goes with the code. where to connect the input and output

        pschatzmann · 23. April 2024 at 21:47

        I am afraid your question does not make any sense to me: I suggest that you read the Wiki of the AudioTools project and if you still have any questions please open a discussion on Github: https://github.com/pschatzmann/arduino-audio-tools/discussions

        In this example you can see that the input comes from a sine generator and the output goes to an ESP32 AudioKit Board!

MyHand · 10. December 2023 at 3:10

Can I use this when I need to make something like a Wav trigger, so the sound could be polyphonic?
And output them to an I2S DAC

Ian · 27. December 2022 at 20:14

I would like to use the library to mix sound data in memory and send the output to a Bluetooth speaker. I have tried many combinations but i have failed, How do I get the memory streams into the mixer and the mixer output to the speaker sink.
Thanks

    pschatzmann · 28. December 2022 at 9:23

    Output to bluetooth is challenging, so make sure that you test your logic first before you do the step to use bluetooth
    – the provided data format must be right (number of channels, sample rate, bits per sample)
    – you need to be able to provide the data fast enough
    Make sure that you have a proper testing strategy, otherwise there is no chance to find out what you are doing wrong!

Leave a Reply

Avatar placeholder

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