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

Sample is like Oscil, it plays a wavetable. More...

#include <Sample.h>

Detailed Description

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
class Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >

Sample is like Oscil, it plays a wavetable.

However, Sample can be set to play once through only, with variable start and end points, or can loop, also with variable start and end points. It defaults to playing once through the whole sound table, from start to finish.

Template Parameters
NUM_TABLE_CELLSThis is defined in the table ".h" file the Sample will be using. The sound table can be arbitrary length for Sample. It's important that NUM_TABLE_CELLS is either a literal number (eg. "8192") or a defined macro, rather than a const or int, for the Sample to run fast enough.
UPDATE_RATEThis will be MOZZI_AUDIO_RATE if the Sample 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.

int8_t2mozzi

Converting soundfiles for Mozzi. There is a python script called int8_t2mozzi.py in the Mozzi/python folder. The script converts raw sound data saved from a program like Audacity. Instructions are in the int8_t2mozzi.py file.

Definition at line 48 of file Sample.h.

Public Member Functions

 Sample (const int8_t *TABLE_NAME)
 Constructor. More...
 
 Sample ()
 Constructor. More...
 
void setTable (const int8_t *TABLE_NAME)
 Change the sound table which will be played by the Sample. More...
 
void setStart (unsigned int startpos)
 Sets the starting position in samples. More...
 
void start ()
 Resets the phase (the playhead) to the start position, which will be 0 unless set to another value with setStart();.
 
void start (unsigned int startpos)
 Sets a new start position plays the sample from that position. More...
 
void setEnd (unsigned int end)
 Sets the end position in samples from the beginning of the sound. More...
 
void rangeWholeSample ()
 Sets the start and end points to include the range of the whole sound table.
 
void setLoopingOn ()
 Turns looping on.
 
void setLoopingOff ()
 Turns looping off.
 
int8_t next ()
 Returns the sample at the current phase position, or 0 if looping is off and the phase overshoots the end of the sample. More...
 
boolean isPlaying ()
 Checks if the sample is playing by seeing if the phase is within the limits of its end position. More...
 
void setFreq (int frequency)
 Set the oscillator frequency with an unsigned int. More...
 
void setFreq (float frequency)
 Set the sample frequency with a float. More...
 
void setFreq_Q24n8 (Q24n8 frequency)
 Set the frequency using Q24n8 fixed-point number format. More...
 
int8_t atIndex (unsigned int index)
 Returns the sample at the given table index. More...
 
unsigned long phaseIncFromFreq (unsigned int frequency)
 phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies. More...
 
void setPhaseInc (unsigned long phaseinc_fractional)
 Set a specific phase increment. More...
 

Constructor & Destructor Documentation

◆ Sample() [1/2]

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::Sample ( const int8_t *  TABLE_NAME)
inline

Constructor.

Parameters
TABLE_NAMEthe name of the array the Sample 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. Sound tables can be of arbitrary lengths for Sample().

Definition at line 59 of file Sample.h.

◆ Sample() [2/2]

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::Sample ( )
inline

Constructor.

Declare a Sample 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().

Definition at line 71 of file Sample.h.

Member Function Documentation

◆ atIndex()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
int8_t Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::atIndex ( unsigned int  index)
inline

Returns the sample at the given table index.

Parameters
indexbetween 0 and the table size.
Returns
the sample at the given table index.

Definition at line 264 of file Sample.h.

◆ isPlaying()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
boolean Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::isPlaying ( )
inline

Checks if the sample is playing by seeing if the phase is within the limits of its end position.

Returns
true if the sample is playing

Definition at line 194 of file Sample.h.

◆ next()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
int8_t Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::next ( )
inline

Returns the sample at the current phase position, or 0 if looping is off and the phase overshoots the end of the sample.

Updates the phase according to the current frequency.

Returns
the next sample value from the table, or 0 if it's finished playing.

Definition at line 165 of file Sample.h.

◆ phaseIncFromFreq()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
unsigned long Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::phaseIncFromFreq ( unsigned 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 281 of file Sample.h.

◆ setEnd()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::setEnd ( unsigned int  end)
inline

Sets the end position in samples from the beginning of the sound.

Parameters
endposition in samples.

Definition at line 122 of file Sample.h.

◆ setFreq() [1/2]

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::setFreq ( float  frequency)
inline

Set the sample 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 or setFreq_Q24n8 for fractional frequencies.

Parameters
frequencyto play the wave table.

Definition at line 236 of file Sample.h.

◆ setFreq() [2/2]

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::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.

Parameters
frequencyto play the wave table.

Definition at line 225 of file Sample.h.

◆ setFreq_Q24n8()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::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. Note: use with caution because it's prone to overflow with higher frequencies and larger table sizes. An Q24n8 representation of 1.5 is 384 (ie. 1.5 * 256).

Parameters
frequencyin Q24n8 fixed-point number format.

Definition at line 251 of file Sample.h.

◆ setPhaseInc()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::setPhaseInc ( unsigned long  phaseinc_fractional)
inline

Set a specific phase increment.

See phaseIncFromFreq().

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

Definition at line 291 of file Sample.h.

◆ setStart()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::setStart ( unsigned int  startpos)
inline

Sets the starting position in samples.

Parameters
startposoffset position in samples.

Definition at line 92 of file Sample.h.

◆ setTable()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::setTable ( const int8_t *  TABLE_NAME)
inline

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

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

Definition at line 82 of file Sample.h.

◆ start()

template<unsigned int NUM_TABLE_CELLS, unsigned int UPDATE_RATE, uint8_t INTERP = INTERP_NONE>
void Sample< NUM_TABLE_CELLS, UPDATE_RATE, INTERP >::start ( unsigned int  startpos)
inline

Sets a new start position plays the sample from that position.

Parameters
startposposition in samples from the beginning of the sound.

Definition at line 111 of file Sample.h.