Use Oscillators - masonandrewmann/MEAP GitHub Wiki

The first part of your code is your #include statements. This tells your sketch what libraries, wavetables or additional files you will be using for your sketch. You will always include MozziGuts.h, which contains the core of the Mozzi library, and Mux.h which controls some hardware that allows you to read your DIP switches and touch keyboard. If you want to use certain Mozzi functions you will often need to include their .h files here. For example, if you want to use Mozzi oscillators you need to include Oscil.h too.

#include <MozziGuts.h>
#include <Mux.h>
#include <Oscil.h>

In this section you can also include any wavetables you plan on using. You can find all your wavetable options in libraries > Mozzi-master > tables.

#include <tables/sin8192_int8.h> // loads sine wavetable

After your includes, your control rate gets set. This is the number of times per second the updateControl() function executes. The default value is 64, but you can set it to any number here. It is recommended to set it to a power of 2. You also need the line using namespace admux which establishes communication with the Mux library

#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable

Next we need to create an oscillator object using this template Oscil<table_size, update_rate> osc1(table_data) . Table size is the number of samples in your wavetable. In our case the wavetable sin8192_int8.h has 8192 samples. This Oscil will be operating as an audio generator, so the update rate will be AUDIO_RATE (the sampling rate of M.E.A.P.) The table_data is an array which you can find the name of in the table file included at the top of the sketch. If you look in Mozzi-master/tables/sin2048_int8.h, you’ll find SIN2048_DATA.

Oscil<SIN8192_NUM_CELLS, AUDIO_RATE> mySine(SIN8192_DATA);

Now to the program functions. In Arduino’s setup() routine goes:

startMozzi(CONTROL_RATE);

This sets up one timer to call updateControl() at the rate chosen and another timer which works behind the scenes to send audio samples to the output pin at the fixed rate of 32768 Hz.

The oscillator frequency can be set in a range of ways, but we’ll use an integer:

mySine.setFreq(440);

The next parts of the sketch are updateControl() and updateAudio(), which are both required. In this example the frequency has already been set and the oscillator just needs to be run in updateAudio(), using the Oscil’s next() method which returns a signed 8 bit value from the oscillator’s wavetable. The int return value of updateAudio() must be in the range -244 to 243 when using the MonoOutput::from8Bit() function.

void updateControl(){
	// no controls being changed
}

int updateAudio(){
  return MonoOutput::from8Bit(mySine.next());
}

Finally, audioHook() goes in Arduino’s loop().

void loop(){
	audioHook();
}

This is where the sound actually gets synthesised, running as fast as possible to fill the output buffer which gets steadily emptied at Mozzi’s audio rate. For this reason, it’s usually best to avoid placing any other code in loop().

It’s important to design a sketch with efficiency in mind in terms of what can be processed in updateAudio(), updateControl() and setup(). Keep updateAudio() lean, put slow changing values in updateControl(), and pre-calculate as much as possible in setup(). Control values which directly modify audio synthesis can be efficiently interpolated with a Line object in updateAudio() if necessary.

Here's the whole sketch:

#include <MozziGuts.h>
#include <Oscil.h>
#include <Mux.h>
#include <tables/sin8192_int8.h> // loads sine wavetable

#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable

using namespace admux;

Oscil<SIN8192_NUM_CELLS, AUDIO_RATE> mySine(SIN8192_DATA);

void setup(){
  startMozzi();
  mySine.setFreq(440); //set frequency of sine oscillator
}


void loop(){
  audioHook();
}


void updateControl(){
	// no controls being changed
}


int updateAudio(){
  return MonoOutput::from8Bit(mySine.next());
}

Some of the more useful wavetables Mozzi includes for us (and an example of creating an oscillator for each):

#include <tables/sin8192_int8.h> // loads sine wavetable
Oscil<SIN8192_NUM_CELLS, AUDIO_RATE> mySine(SIN8192_DATA);


#include <tables/saw8192_int8.h> // loads saw wavetable
Oscil<SAW8192_NUM_CELLS, AUDIO_RATE> mySaw(SAW8192_DATA);


#include <tables/triangle_warm8192_int8.h> // loads triangle wavetable
Oscil<TRIANGLE_WARM8192_NUM_CELLS, AUDIO_RATE> myTri(TRIANGLE_WARM8192_DATA);


#include <tables/smoothsquare8192_int8.h> // loads square wavetable
Oscil<SMOOTHSQUARE8192_NUM_CELLS, AUDIO_RATE> mySquare(SMOOTHSQUARE8192_DATA);



#include <tables/brownnoise8192_int8.h> // loads brown noise wavetable
Oscil<BROWNNOISE8192_NUM_CELLS, AUDIO_RATE> myBrownNoise(BROWNNOISE8192_DATA);


#include <tables/pinknoise8192_int8.h> // loads pink noise wavetable
Oscil<PINKNOISE8192_NUM_CELLS, AUDIO_RATE> myPinkNoise(PINKNOISE8192_DATA);


#include <tables/whitenoise8192_int8.h> // loads white noise wavetable
Oscil<WHITENOISE8192_NUM_CELLS, AUDIO_RATE> myWhiteNoise(WHITENOISE8192_DATA);


⚠️ **GitHub.com Fallback** ⚠️