Im my last Blog I have demonstrated how we can use some simple Sound Generator with my Arduino Audio Tools library. I spent a small effort to integrate Mozzi and the result looks quite promising.

Mozzi brings your Arduino to life by allowing it to produce much more complex and interesting growls, sweeps and chorusing atmospherics. These sounds can be quickly and easily constructed from familiar synthesis units like oscillators, delays, filters and envelopes.

The Sketch

Here is a minimal Example Sketch that uses both libraries and sends the generated sound to a Bluetooth Speaker.

#include "AudioTools.h"
#include "AudioA2DP.h"
#include "AudioMozzi.h"
// -> Mozzi
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable

using namespace audio_tools;  

MozziGenerator mozzi(CONTROL_RATE);         // subclass of SoundGenerator 
GeneratedSoundStream<int16_t> in(mozzi, 2); // Stream generated with mozzi
A2DPStream out = A2DPStream::instance();    // A2DP output - A2DPStream is a singleton!
StreamCopy copier(out, in);                 // copy in to out

// -> Mozzi
// use: Oscil <table_size, update_rate> oscilName (wavetable)
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);


void setup(){
  Serial.begin(115200);

  // A2DP works with 44100
  if (mozzi.config().sample_rate!=44100){
    Serial.println("Please set the AUDIO_RATE in the mozzi_config.h to 44100");
    stop();
  }

  // We send the test signal via A2DP - so we connect to the MyMusic Bluetooth Speaker
  out.begin(TX_MODE, "MyMusic");
  Serial.println("A2DP is connected now...");

  // -> Mozzi setup
  aSin.setFreq(440); // set the frequency
  in.begin();
}

void updateControl(){
}

AudioOutput_t updateAudio(){
  return MonoOutput::from8Bit(aSin.next()); // return an int signal centred around 0
}

// Arduino loop  
void loop() {
  if (out)
    copier.copy();
}

As you can see above you can add the MozziGenerator like any other Generator to the GeneratedSoundStream class.

In the setup you need to combine the Audio-Tools setup and then call the Mozzi specific setup functions. Replace the startMozzi() with our GeneratedSoundStream.begin().

Like in any other Mozzi Sketch you need to define the updateControl() and updateAudio()!
In the loop however you need to replace the audioHook() with your copy logic:

Mozzi Configuration

A2DP requires an audio rate of 44100. Therefore you need to make sure that you have the following settings in your mozzi_config.h:

#define EXTERNAL_AUDIO_OUTPUT true
#define AUDIO_RATE 44100
#define MICROS_PER_AUDIO_TICK 1000000 / AUDIO_RATE 

Summary

I am also providing a more complex example in my project. I think both libraries are complementing each other perfectly.

Both projects can be found on Github:


0 Comments

Leave a Reply

Your email address will not be published.