Mozzi  version v2.0
sound synthesis library for Arduino
Oscil< NUM_TABLE_CELLS, UPDATE_RATE > Class Template Reference

Oscil plays a wavetable, cycling through the table to generate an audio or control signal. More...

#include <Oscil.h>

Detailed Description

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
class Oscil< NUM_TABLE_CELLS, UPDATE_RATE >

Oscil plays a wavetable, cycling through the table to generate an audio or control signal.

The frequency of the signal can be set or changed with setFreq(), and the output of an Oscil can be produced with next() for a simple cycling oscillator, or atIndex() for a particular sample in the table.

Template Parameters
NUM_TABLE_CELLSThis is defined in the table ".h" file the Oscil will be using. It's important that it's a power of 2, and either a literal number (eg. "8192") or a defined macro, rather than a const or int, for the Oscil to run fast enough.
UPDATE_RATEThis will be MOZZI_AUDIO_RATE if the Oscil is updated in updateAudio(), or MOZZI_CONTROL_RATE if it's updated each time updateControl() is called. It could also be a fraction of MOZZI_CONTROL_RATE if you are doing some kind of cyclic updating in updateControl(), for example, to spread out the processor load.
Note
If you #define OSCIL_DITHER_PHASE before you #include <Oscil.h>, the phase increments will be dithered, which reduces spurious frequency spurs in the audio output, at the cost of some extra processing and memory.

int8_t2mozzi

Converting soundfiles for Mozzi There is a python script called char2mozzi.py in the Mozzi/python folder. The usage is: char2mozzi.py infilename outfilename tablename samplerate

Definition at line 61 of file Oscil.h.

Public Member Functions

 Oscil (const int8_t *TABLE_NAME)
 Constructor. More...
 
 Oscil ()
 Constructor. More...
 
int8_t next ()
 Updates the phase according to the current frequency and returns the sample at the new phase position. More...
 
void setTable (const int8_t *TABLE_NAME)
 Change the sound table which will be played by the Oscil. More...
 
void setPhase (unsigned int phase)
 Set the phase of the Oscil. More...
 
void setPhaseFractional (uint32_t phase)
 Set the phase of the Oscil. More...
 
uint32_t getPhaseFractional ()
 Get the phase of the Oscil in fractional format. More...
 
int8_t phMod (Q15n16 phmod_proportion)
 Returns the next sample given a phase modulation value. More...
 
template<int8_t NI, int8_t NF, uint8_t RANGE>
int8_t phMod (SFix< NI, NF, RANGE > phmod_proportion)
 Returns the next sample given a phase modulation value. More...
 
int8_t phMod (SFix< 15, 16 > phmod_proportion)
 Returns the next sample given a phase modulation value. More...
 
void setFreq (int frequency)
 Set the oscillator frequency with an unsigned int. More...
 
void setFreq (float frequency)
 Set the oscillator frequency with a float. More...
 
template<int8_t NI, int8_t NF, uint64_t RANGE>
void setFreq (UFix< NI, NF, RANGE > frequency)
 Set the frequency using UFix<NI,NF> fixed-point number format. More...
 
void setFreq_Q24n8 (Q24n8 frequency)
 Set the frequency using Q24n8 fixed-point number format. More...
 
template<uint64_t RANGE>
void setFreq (UFix< 24, 8, RANGE > frequency)
 Set the frequency using UFix<24,8> fixed-point number format. More...
 
void setFreq_Q16n16 (Q16n16 frequency)
 Set the frequency using Q16n16 fixed-point number format. More...
 
template<uint64_t RANGE>
void setFreq (UFix< 16, 16, RANGE > frequency)
 Set the frequency using UFix<16,16> fixed-point number format. More...
 
template<int8_t NI, int8_t NF, uint64_t RANGE>
void setFreq (SFix< NI, NF, RANGE > frequency)
 Set the frequency using SFix<NI,NF> fixed-point number format. More...
 
int8_t atIndex (unsigned int index)
 Returns the sample at the given table index. More...
 
uint32_t phaseIncFromFreq (int frequency)
 phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies. More...
 
void setPhaseInc (uint32_t phaseinc_fractional)
 Set a specific phase increment. More...
 

Constructor & Destructor Documentation

◆ Oscil() [1/2]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::Oscil ( const int8_t *  TABLE_NAME)
inline

Constructor.

Parameters
TABLE_NAMEthe name of the array the Oscil will be using. This can be found in the table ".h" file if you are using a table made for Mozzi by the int8_t2mozzi.py python script in Mozzi's python folder.

Definition at line 71 of file Oscil.h.

◆ Oscil() [2/2]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::Oscil ( )
inline

Constructor.

Declare an Oscil with template TABLE_NUM_CELLS and UPDATE_RATE parameters, without specifying a particular wave table for it to play. The table can be set or changed on the fly with setTable(). Any tables used by the Oscil must be the same size.

Definition at line 81 of file Oscil.h.

Member Function Documentation

◆ atIndex()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
int8_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::atIndex ( unsigned int  index)
inline

Returns the sample at the given table index.

Parameters
indexbetween 0 and the table size.The index rolls back around to 0 if it's larger than the table size.
Returns
the sample at the given table index.

Definition at line 333 of file Oscil.h.

◆ getPhaseFractional()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
uint32_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::getPhaseFractional ( )
inline

Get the phase of the Oscil in fractional format.

Returns
position in the wavetable, shifted left by OSCIL_F_BITS (which is 16 when this was written).

Definition at line 131 of file Oscil.h.

◆ next()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
int8_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::next ( )
inline

Updates the phase according to the current frequency and returns the sample at the new phase position.

Returns
the next sample.

Definition at line 89 of file Oscil.h.

◆ phaseIncFromFreq()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
uint32_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::phaseIncFromFreq ( int  frequency)
inline

phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.

Instead of recalculating the phase increment for each frequency in between, you can just calculate the phase increment for each end frequency with phaseIncFromFreq(), then use a Line to interpolate on the fly and use setPhaseInc() to set the phase increment at each step. (Note: I should really profile this with the oscilloscope to see if it's worth the extra confusion!)

Parameters
frequencyfor which you want to calculate a phase increment value.
Returns
the phase increment value which will produce a given frequency.

Definition at line 350 of file Oscil.h.

◆ phMod() [1/3]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
int8_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::phMod ( Q15n16  phmod_proportion)
inline

Returns the next sample given a phase modulation value.

Parameters
phmod_proportiona phase modulation value given as a proportion of the wave. The phmod_proportion parameter is a Q15n16 fixed-point number where the fractional n16 part represents almost -1 to almost 1, modulating the phase by one whole table length in each direction.
Returns
a sample from the table.

Definition at line 149 of file Oscil.h.

◆ phMod() [2/3]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
int8_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::phMod ( SFix< 15, 16 >  phmod_proportion)
inline

Returns the next sample given a phase modulation value.

Parameters
phmod_proportiona phase modulation value given as a proportion of the wave. The phmod_proportion parameter is a SFix<15,16> fixed-point number where the fractional part represents almost -1 to almost 1, modulating the phase by one whole table length in each direction.
Returns
a sample from the table.

Definition at line 178 of file Oscil.h.

◆ phMod() [3/3]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
template<int8_t NI, int8_t NF, uint8_t RANGE>
int8_t Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::phMod ( SFix< NI, NF, RANGE >  phmod_proportion)
inline

Returns the next sample given a phase modulation value.

Parameters
phmod_proportiona phase modulation value given as a proportion of the wave. The phmod_proportion parameter is a SFix<NI,NF> fixed-point number where the fractional part represents almost -1 to almost 1, modulating the phase by one whole table length in each direction. This fixed point math number is interpreted as a SFix<15,16> internally.
Returns
a sample from the table.

Definition at line 164 of file Oscil.h.

◆ setFreq() [1/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( float  frequency)
inline

Set the oscillator frequency with a float.

Using a float is the most reliable way to set frequencies, -Might- be slower than using an int but you need either this, setFreq_Q24n8() or setFreq_Q16n16() for fractional frequencies.

Parameters
frequencyto play the wave table.

Definition at line 207 of file Oscil.h.

◆ setFreq() [2/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( int  frequency)
inline

Set the oscillator frequency with an unsigned int.

This is faster than using a float, so it's useful when processor time is tight, but it can be tricky with low and high frequencies, depending on the size of the wavetable being used. If you're not getting the results you expect, try explicitly using a float, or try setFreq_Q24n8() or or setFreq_Q16n16().

Parameters
frequencyto play the wave table.

Definition at line 192 of file Oscil.h.

◆ setFreq() [3/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
template<int8_t NI, int8_t NF, uint64_t RANGE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( SFix< NI, NF, RANGE >  frequency)
inline

Set the frequency using SFix<NI,NF> fixed-point number format.

This falls back to using UFix<16,16> internally and is provided as a fallout for other UFix types. If possible try to use directly UFix<16,16> or UFix<24,8> for well defined (and well tested) behaviors.

Note
This should work OK with tables 2048 cells or smaller and frequencies up to 4096 Hz. Can't be used with UPDATE_RATE less than 64 Hz.
This didn't run faster than float last time it was tested, after 2014 code changes. Need to see if 2014 changes improved or worsened performance.
Parameters
frequencyin SFix<16,16> fixed-point number format.

Definition at line 316 of file Oscil.h.

◆ setFreq() [4/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
template<uint64_t RANGE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( UFix< 16, 16, RANGE >  frequency)
inline

Set the frequency using UFix<16,16> fixed-point number format.

This is useful in combination with Q16n16_mtof(), a fast alternative to mtof(), using UFix<16,16> fixed-point format instead of fractional numbers.

Note
This should work OK with tables 2048 cells or smaller and frequencies up to 4096 Hz. Can't be used with UPDATE_RATE less than 64 Hz.
This didn't run faster than float last time it was tested, after 2014 code changes. Need to see if 2014 changes improved or worsened performance.
Parameters
frequencyin UFix<16,16> fixed-point number format.

Definition at line 301 of file Oscil.h.

◆ setFreq() [5/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
template<uint64_t RANGE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( UFix< 24, 8, RANGE >  frequency)
inline

Set the frequency using UFix<24,8> fixed-point number format.

This might be faster than the float version for setting low frequencies such as 1.5 Hz, or other values which may not work well with your table size. A UFix<24,8> representation of 1.5 is 384 (ie. 1.5 * 256). Can't be used with UPDATE_RATE less than 64 Hz.

Parameters
frequencyin UFix<24,8> fixed-point number format.

Definition at line 261 of file Oscil.h.

◆ setFreq() [6/6]

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
template<int8_t NI, int8_t NF, uint64_t RANGE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq ( UFix< NI, NF, RANGE >  frequency)
inline

Set the frequency using UFix<NI,NF> fixed-point number format.

This falls back to using UFix<16,16> internally and is provided as a fallout for other UFix types. If possible try to use directly UFix<16,16> or UFix<24,8> for well defined (and well tested) behaviors.

Note
This should work OK with tables 2048 cells or smaller and frequencies up to 4096 Hz. Can't be used with UPDATE_RATE less than 64 Hz.
This didn't run faster than float last time it was tested, after 2014 code changes. Need to see if 2014 changes improved or worsened performance.
Parameters
frequencyin UFix<NI,NF> fixed-point number format.

Definition at line 221 of file Oscil.h.

◆ setFreq_Q16n16()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq_Q16n16 ( Q16n16  frequency)
inline

Set the frequency using Q16n16 fixed-point number format.

This is useful in combination with Q16n16_mtof(), a fast alternative to mtof(), using Q16n16 fixed-point format instead of floats.

Note
This should work OK with tables 2048 cells or smaller and frequencies up to 4096 Hz. Can't be used with UPDATE_RATE less than 64 Hz.
This didn't run faster than float last time it was tested, after 2014 code changes. Need to see if 2014 changes improved or worsened performance.
Parameters
frequencyin Q16n16 fixed-point number format.

Definition at line 276 of file Oscil.h.

◆ setFreq_Q24n8()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setFreq_Q24n8 ( Q24n8  frequency)
inline

Set the frequency using Q24n8 fixed-point number format.

This might be faster than the float version for setting low frequencies such as 1.5 Hz, or other values which may not work well with your table size. A Q24n8 representation of 1.5 is 384 (ie. 1.5 * 256). Can't be used with UPDATE_RATE less than 64 Hz.

Parameters
frequencyin Q24n8 fixed-point number format.

Definition at line 236 of file Oscil.h.

◆ setPhase()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setPhase ( unsigned int  phase)
inline

Set the phase of the Oscil.

This does the same thing as Sample::start(offset). Just different ways of thinking about oscillators and samples.

Parameters
phasea position in the wavetable.

Definition at line 111 of file Oscil.h.

◆ setPhaseFractional()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setPhaseFractional ( uint32_t  phase)
inline

Set the phase of the Oscil.

Might be useful with getPhaseFractional().

Parameters
phasea position in the wavetable.

Definition at line 122 of file Oscil.h.

◆ setPhaseInc()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setPhaseInc ( uint32_t  phaseinc_fractional)
inline

Set a specific phase increment.

See phaseIncFromFreq().

Parameters
phaseinc_fractionala phase increment value as calculated by phaseIncFromFreq().

Definition at line 363 of file Oscil.h.

◆ setTable()

template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE>
void Oscil< NUM_TABLE_CELLS, UPDATE_RATE >::setTable ( const int8_t *  TABLE_NAME)
inline

Change the sound table which will be played by the Oscil.

Parameters
TABLE_NAMEis the name of the array in the table ".h" file you're using.

Definition at line 99 of file Oscil.h.