Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
|
This utility code is provided to run and mix oscillators for DAC output (hdw-dac.h). Each oscillator has a shape, frequency, and amplitude. The oscillators assume the output sample rate is DAC_SAMPLE_RATE_HZ.
When an oscillator's amplitude is changed by swSynthSetVolume(), it will linearly adjust the output volume over time. This avoids clicks and pops caused by discontinuities in the output. Changing the oscillator's frequency will not cause any discontinuities. Changing the oscillator's shape will have a discontinuity and likely have an audible click or pop.
Initialize an oscillator with swSynthInitOscillator(). Change the oscillator's properties with swSynthSetShape(), swSynthSetFreq(), or swSynthSetVolume().
Call swSynthMixOscillators() to step a set of oscillators, mix their output, and return it for a DAC buffer.
Go to the source code of this file.
Data Structures | |
union | oscAccum_t |
An accumulator used to increment through a wave. A value is accumulated in a 32-bit integer and bits 16->23 are used as an index into a 256 point wave. More... | |
struct | synthOscillator_t |
A software oscillator with controllable frequency, amplitude, and shape. More... | |
Macros | |
#define | SPK_MAX_VOLUME 255 |
Typedefs | |
typedef int8_t(* | waveFunc_t) (uint16_t idx, void *data) |
Function typedef to return a sample from a wave. | |
Enumerations | |
enum | oscillatorShape_t { SHAPE_SINE , SHAPE_SAWTOOTH , SHAPE_TRIANGLE , SHAPE_SQUARE , SHAPE_NOISE } |
The different wave shapes that can be generated. More... | |
Functions | |
void | swSynthInitOscillator (synthOscillator_t *osc, oscillatorShape_t shape, uint32_t freq, uint8_t volume) |
Initialize a software synthesizer oscillator. | |
void | swSynthInitOscillatorWave (synthOscillator_t *osc, waveFunc_t waveFunc, void *waveData, uint32_t freq, uint8_t volume) |
Initialize a software synthesizer oscillator. | |
void | swSynthSetShape (synthOscillator_t *osc, oscillatorShape_t shape) |
Set a software synthesizer oscillator's shape. | |
void | swSynthSetWaveFunc (synthOscillator_t *osc, waveFunc_t waveFunc, void *waveFuncData) |
Set the wave function of an oscillator. | |
void | swSynthSetFreq (synthOscillator_t *osc, uint32_t freq) |
Set the frequency of an oscillator. | |
void | swSynthSetFreqPrecise (synthOscillator_t *osc, uq16_16 freq) |
Set the frequency of an oscillator with 16 bits of decimal precision. | |
void | swSynthSetVolume (synthOscillator_t *osc, uint8_t volume) |
Set the volume (amplitude) of an oscillator. | |
uint8_t | swSynthMixOscillators (synthOscillator_t *oscillators[], uint16_t numOscillators) |
Increment a set of oscillators by one step each, mix together the resulting samples, and return the single mixed sample. | |
int32_t | swSynthSumOscillators (synthOscillator_t *oscillators[], uint16_t numOscillators) |
Increment and mix together a set of oscillators like swSynthMixOscillators(), but returns the intermediate sample sum rather than the average, to allow mixing in other sources without losing precision. | |
int8_t | swSynthSampleWave (oscillatorShape_t shape, uint8_t idx) |
union oscAccum_t |
struct synthOscillator_t |
Data Fields | ||
---|---|---|
waveFunc_t | waveFunc | A pointer to the function which generates samples. |
void * | waveFuncData | A pointer to pass to the wave function. |
oscAccum_t | accumulator | An accumulator to increment the wave sample. |
int32_t | stepSize | The step that should be added to the accumulator each sample, dependent on frequency. |
uint32_t | tVol | The target volume (amplitude) |
uint32_t | cVol | The current volume which smoothly transitions to the target volume. |
uint8_t | chorus | The number of offset samples to return. |
#define SPK_MAX_VOLUME 255 |
The maximum speaker volume
typedef int8_t(* waveFunc_t) (uint16_t idx, void *data) |
Function typedef to return a sample from a wave.
idx | The index of the wave to get a sample from |
enum oscillatorShape_t |
void swSynthInitOscillator | ( | synthOscillator_t * | osc, |
oscillatorShape_t | shape, | ||
uint32_t | freq, | ||
uint8_t | volume ) |
Initialize a software synthesizer oscillator.
osc | The oscillator to initialize |
shape | The shape of the wave to generate |
freq | The frequency of the wave to generate, in hertz |
volume | The volume (amplitude) of the wave to generate |
void swSynthInitOscillatorWave | ( | synthOscillator_t * | osc, |
waveFunc_t | waveFunc, | ||
void * | waveData, | ||
uint32_t | freq, | ||
uint8_t | volume ) |
Initialize a software synthesizer oscillator.
osc | The oscillator to initialize |
waveFunc | The wave function to use |
waveData | Custom data to pass into the wave function |
freq | The frequency of the wave to generate, in hertz |
volume | The volume (amplitude) of the wave to generate |
void swSynthSetShape | ( | synthOscillator_t * | osc, |
oscillatorShape_t | shape ) |
Set a software synthesizer oscillator's shape.
osc | The oscillator to set the shape for |
shape | The shape to set (sine, square, sawtooth, triangle, or noise) |
void swSynthSetWaveFunc | ( | synthOscillator_t * | osc, |
waveFunc_t | waveFunc, | ||
void * | waveFuncData ) |
Set the wave function of an oscillator.
osc | The oscillator to set the wave function of |
waveFunc | The wave function to use |
waveFuncData | Data to be passed to the wave function |
void swSynthSetFreq | ( | synthOscillator_t * | osc, |
uint32_t | freq ) |
Set the frequency of an oscillator.
osc | The oscillator to set the frequency for |
freq | The frequency to set |
void swSynthSetFreqPrecise | ( | synthOscillator_t * | osc, |
uq16_16 | freq ) |
Set the frequency of an oscillator with 16 bits of decimal precision.
osc | The oscillator to set the frequency for |
freq | The frequency to set, as a fixed-point value with 16 bits of decimal precision |
void swSynthSetVolume | ( | synthOscillator_t * | osc, |
uint8_t | volume ) |
Set the volume (amplitude) of an oscillator.
osc | The oscillator to set the volume for |
volume | The volume, 255 is loudest, 0 is off |
uint8_t swSynthMixOscillators | ( | synthOscillator_t * | oscillators[], |
uint16_t | numOscillators ) |
Increment a set of oscillators by one step each, mix together the resulting samples, and return the single mixed sample.
oscillators | An array of pointers to oscillators to step and mix together |
numOscillators | The number of oscillators to step and mix |
int32_t swSynthSumOscillators | ( | synthOscillator_t * | oscillators[], |
uint16_t | numOscillators ) |
Increment and mix together a set of oscillators like swSynthMixOscillators(), but returns the intermediate sample sum rather than the average, to allow mixing in other sources without losing precision.
The caller must divide this value by the number of oscillators (plus the number of other sources) then add 128 to the result to convert it to an unsigned 8-bit value.
oscillators | An array of oscillator pointers |
numOscillators | The number of members in oscillators |
int8_t swSynthSampleWave | ( | oscillatorShape_t | shape, |
uint8_t | idx ) |