Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
swSynth.h
Go to the documentation of this file.
1
43#pragma once
44
45#include <stdint.h>
46#include <stdbool.h>
47
48#include "fp_math.h"
49
50//==============================================================================
51// Defines
52//==============================================================================
53
55#define SPK_MAX_VOLUME 255
56
57//==============================================================================
58// Enums
59//==============================================================================
60
72
73//==============================================================================
74// Unions
75//==============================================================================
76
81typedef union
82{
83 uint32_t accum32;
84 uint8_t bytes[4];
86
87//==============================================================================
88// Typedefs
89//==============================================================================
90
97typedef int8_t (*waveFunc_t)(uint16_t idx, void* data);
98
99//==============================================================================
100// Structs
101//==============================================================================
102
106typedef struct
107{
111 int32_t stepSize;
112 uint32_t tVol;
113 uint32_t cVol;
114 uint8_t chorus;
116
117//==============================================================================
118// Function prototypes
119//==============================================================================
120
121void swSynthInitOscillator(synthOscillator_t* osc, oscillatorShape_t shape, uint32_t freq, uint8_t volume);
122void swSynthInitOscillatorWave(synthOscillator_t* osc, waveFunc_t waveFunc, void* waveData, uint32_t freq,
123 uint8_t volume);
125void swSynthSetWaveFunc(synthOscillator_t* osc, waveFunc_t waveFunc, void* waveFuncData);
126void swSynthSetFreq(synthOscillator_t* osc, uint32_t freq);
128void swSynthSetVolume(synthOscillator_t* osc, uint8_t volume);
129uint8_t swSynthMixOscillators(synthOscillator_t* oscillators[], uint16_t numOscillators);
130int32_t swSynthSumOscillators(synthOscillator_t* oscillators[], uint16_t numOscillators);
131int8_t swSynthSampleWave(oscillatorShape_t shape, uint8_t idx);
uint32_t uq16_16
unsigned 16 bits integer, 16 bits fraction
Definition fp_math.h:42
oscAccum_t accumulator
An accumulator to increment the wave sample.
Definition swSynth.h:110
oscillatorShape_t
The different wave shapes that can be generated.
Definition swSynth.h:65
@ SHAPE_SAWTOOTH
A sawtooth wave.
Definition swSynth.h:67
@ SHAPE_SQUARE
A square wave.
Definition swSynth.h:69
@ SHAPE_SINE
A sine wave.
Definition swSynth.h:66
@ SHAPE_TRIANGLE
A triangle wave.
Definition swSynth.h:68
@ SHAPE_NOISE
Random noise from a linear feedback shift register.
Definition swSynth.h:70
void swSynthSetShape(synthOscillator_t *osc, oscillatorShape_t shape)
Set a software synthesizer oscillator's shape.
Definition swSynth.c:210
void swSynthSetFreqPrecise(synthOscillator_t *osc, uq16_16 freq)
Set the frequency of an oscillator with 16 bits of decimal precision.
Definition swSynth.c:273
int32_t swSynthSumOscillators(synthOscillator_t *oscillators[], uint16_t numOscillators)
Increment and mix together a set of oscillators like swSynthMixOscillators(), but returns the interme...
Definition swSynth.c:315
void swSynthSetWaveFunc(synthOscillator_t *osc, waveFunc_t waveFunc, void *waveFuncData)
Set the wave function of an oscillator.
Definition swSynth.c:250
uint32_t tVol
The target volume (amplitude)
Definition swSynth.h:112
void swSynthSetFreq(synthOscillator_t *osc, uint32_t freq)
Set the frequency of an oscillator.
Definition swSynth.c:262
uint8_t swSynthMixOscillators(synthOscillator_t *oscillators[], uint16_t numOscillators)
Increment a set of oscillators by one step each, mix together the resulting samples,...
Definition swSynth.c:298
uint32_t accum32
The oscillator value is accumulated in a 32 bit integer.
Definition swSynth.h:83
int8_t(* waveFunc_t)(uint16_t idx, void *data)
Function typedef to return a sample from a wave.
Definition swSynth.h:97
void swSynthSetVolume(synthOscillator_t *osc, uint8_t volume)
Set the volume (amplitude) of an oscillator.
Definition swSynth.c:285
uint32_t cVol
The current volume which smoothly transitions to the target volume.
Definition swSynth.h:113
int32_t stepSize
The step that should be added to the accumulator each sample, dependent on frequency.
Definition swSynth.h:111
uint8_t chorus
The number of offset samples to return.
Definition swSynth.h:114
int8_t swSynthSampleWave(oscillatorShape_t shape, uint8_t idx)
Definition swSynth.c:353
waveFunc_t waveFunc
A pointer to the function which generates samples.
Definition swSynth.h:108
void * waveFuncData
A pointer to pass to the wave function.
Definition swSynth.h:109
void swSynthInitOscillatorWave(synthOscillator_t *osc, waveFunc_t waveFunc, void *waveData, uint32_t freq, uint8_t volume)
Initialize a software synthesizer oscillator.
Definition swSynth.c:192
void swSynthInitOscillator(synthOscillator_t *osc, oscillatorShape_t shape, uint32_t freq, uint8_t volume)
Initialize a software synthesizer oscillator.
Definition swSynth.c:171
A software oscillator with controllable frequency, amplitude, and shape.
Definition swSynth.h:107
An accumulator used to increment through a wave. A value is accumulated in a 32-bit integer and bits ...
Definition swSynth.h:82