Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
|
This code is provided to play MIDI songs via the DAC speaker. MIDI songs are loaded with midiFileParser.h.
Two system-wide MIDI players are provided by default and can be used to playback BGM and SFX independently. They will automatically be initialized if swadgeMode_t.fnDacCb is NULL
. Each MIDI player supports 16 channels and can simultaneously play up to 24 melodic notes and 8 percussion notes, shared across all channels. All 128 General MIDI instruments are supported, as well as the full General MIDI percussion range.
Go to the source code of this file.
Data Structures | |
struct | envelope_t |
Describes the characteristics of a particular timbre while. More... | |
struct | timbreEffects_t |
struct | midiTimbre_t |
Defines the sound characteristics of a particular instrument. More... | |
struct | midiVoice_t |
Tracks the state of a single voice, playing a single note. More... | |
struct | voiceStates_t |
Holds several bitfields that track the state of each voice for fast access. This may be used for dynamic voice allocation, and to minimize the impact of note stealing if we run out of voices. More... | |
struct | midiChannel_t |
Tracks the state of a single MIDI channel. More... | |
struct | midiPlayer_t |
Tracks the state of the entire MIDI apparatus. More... | |
union | midiTimbre_t.__unnamed7__ |
struct | midiTimbre_t.__unnamed7__.__unnamed9__ |
struct | midiTimbre_t.__unnamed7__.sample |
union | midiTimbre_t.__unnamed7__.sample.__unnamed13__ |
The frequency of the base sample to be used when pitch shifting. More... | |
struct | midiTimbre_t.__unnamed7__.sample.__unnamed13__.__unnamed15__ |
struct | midiTimbre_t.__unnamed7__.sample.__unnamed13__.config |
struct | midiTimbre_t.__unnamed7__.percussion |
union | midiVoice_t.__unnamed18__ |
struct | midiVoice_t.__unnamed18__.__unnamed20__ |
Macros | |
#define | MIDI_CHANNEL_COUNT 16 |
#define | POOL_VOICE_COUNT 24 |
#define | PERCUSSION_VOICES 8 |
#define | OSC_PER_VOICE 1 |
#define | NUM_GLOBAL_PLAYERS 2 |
#define | MIDI_SFX 0 |
#define | MIDI_BGM 1 |
#define | MAX_VOLUME 13 |
#define | MIDI_TRUE 0x7F |
#define | MIDI_FALSE 0x00 |
#define | MIDI_TO_BOOL(val) (val > 63) |
#define | BOOL_TO_MIDI(val) (val ? MIDI_TRUE : MIDI_FALSE) |
#define | MIDI_DEF_HEADROOM 0x2666 |
#define | PITCH_BEND_CENTER 0x2000 |
#define | SAMPLES_TO_MIDI_TICKS(n, tempo, div) ((n) * 1000000 * (div) / DAC_SAMPLE_RATE_HZ / (tempo)) |
Convert the sample count to MIDI ticks. | |
#define | SAMPLES_TO_MS(samp) (((samp) * 1000) / DAC_SAMPLE_RATE_HZ) |
Convert samples to milliseconds. | |
#define | SAMPLES_TO_US(samp) (((samp) * 1000000) / DAC_SAMPLE_RATE_HZ) |
Convert samples to microseconds. | |
#define | MS_TO_SAMPLES(ms) ((ms) * DAC_SAMPLE_RATE_HZ / 1000) |
Calculate the number of DAC samples in the given number of milliseconds. | |
#define | MIDI_TICKS_TO_US(ticks, tempo, div) (int64_t)((int64_t)((int64_t)(ticks) * (int64_t)(tempo)) / ((int64_t)(div))) |
Convert MIDI ticks to microseconds. | |
Typedefs | |
typedef void(* | songFinishedCbFn) (void) |
Callback function used to provide feedback when a song finishes playing. | |
typedef int8_t(* | percussionFunc_t) (percussionNote_t drum, uint32_t idx, bool *done, uint32_t scratch[4], void *data) |
A function that returns samples for a percussion timbre rather than a melodic one. | |
typedef void(* | midiTextCallback_t) (metaEventType_t type, const char *text, uint32_t length) |
A function to handle text meta-messages from playing MIDI files. | |
typedef bool(* | midiStreamingCallback_t) (midiEvent_t *event) |
A function to return MIDI events in streaming mode. | |
Functions | |
void | midiPlayerInit (midiPlayer_t *player) |
Initialize the MIDI player. | |
void | midiPlayerReset (midiPlayer_t *player) |
Reset the MIDI player state. | |
void | midiPlayerResetNewSong (midiPlayer_t *player) |
Reset the MIDI player state by only doing the bare minimum. This is useful when playing multiple sound effects in quick succession. | |
int32_t | midiPlayerStep (midiPlayer_t *player) |
Calculate and return the next MIDI sample, stepping the player state forward by one sample. | |
void | midiPlayerFillBuffer (midiPlayer_t *player, uint8_t *samples, int16_t len) |
Fill a buffer with the next set of samples from the MIDI player. This should be called by the callback passed into initDac(). Samples are generated at sampling rate of DAC_SAMPLE_RATE_HZ. | |
void | midiPlayerFillBufferMulti (midiPlayer_t *players, uint8_t playerCount, uint8_t *samples, int16_t len) |
Fill a buffer with the next set of samples from an array of MIDI players. | |
void | midiAllSoundOff (midiPlayer_t *player) |
Stop all sound immediately. This is not affected by the sustain pedal. | |
void | midiResetChannelControllers (midiPlayer_t *player, uint8_t channel) |
Reset all controllers on a MIDI channel. | |
void | midiGmOn (midiPlayer_t *player) |
Activate General MIDI mode on a MIDI player. | |
void | midiGmOff (midiPlayer_t *player) |
Deactivate General MIDI mode on a MIDI player. | |
void | midiAllNotesOff (midiPlayer_t *player, uint8_t channel) |
Tun off all notes which are currently on, as though midiNoteOff() were called for each note. This respects the sustain pedal. | |
void | midiNoteOn (midiPlayer_t *player, uint8_t channel, uint8_t note, uint8_t velocity) |
Begin playing a note on a given MIDI channel. | |
void | midiAfterTouch (midiPlayer_t *player, uint8_t channel, uint8_t note, uint8_t velocity) |
Change the velocity of a note on a given MIDI channel, after the note starts playing. | |
void | midiNoteOff (midiPlayer_t *player, uint8_t channel, uint8_t note, uint8_t velocity) |
Stop playing a particular note on a given MIDI channel. | |
void | midiSetProgram (midiPlayer_t *player, uint8_t channel, uint8_t program) |
Change the program (instrument) on a given MIDI channel. | |
void | midiSustain (midiPlayer_t *player, uint8_t channel, uint8_t val) |
Set the hold pedal status. | |
void | midiSustenuto (midiPlayer_t *player, uint8_t channel, uint8_t val) |
Set the sustenuto pedal status. | |
void | midiControlChange (midiPlayer_t *player, uint8_t channel, midiControl_t control, uint8_t val) |
Set a MIDI control value. | |
uint8_t | midiGetControlValue (midiPlayer_t *player, uint8_t channel, midiControl_t control) |
Get the value of a MIDI control. | |
uint16_t | midiGetControlValue14bit (midiPlayer_t *player, uint8_t channel, midiControl_t control) |
Get the combined value of two MIDI control registers. | |
void | midiSetParameter (midiPlayer_t *player, uint8_t channel, bool registered, uint16_t param, uint16_t value) |
Set a registered or non-registered parameter value. | |
uint16_t | midiGetParameterValue (midiPlayer_t *player, uint8_t channel, bool registered, uint16_t param) |
Get the value of a registered or non-registered parameter. | |
void | midiPitchWheel (midiPlayer_t *player, uint8_t channel, uint16_t value) |
Set the pitch wheel value on a given MIDI channel. | |
void | midiSetTempo (midiPlayer_t *player, uint32_t tempo) |
Change the MIDI playback tempo. | |
void | midiSetFile (midiPlayer_t *player, const midiFile_t *file) |
Configure this MIDI player to read from a MIDI file. | |
void | midiPause (midiPlayer_t *player, bool pause) |
Set the paused state of a MIDI song. | |
void | midiSeek (midiPlayer_t *player, uint32_t ticks) |
Seek to a given time offset within a file. | |
void | initGlobalMidiPlayer (void) |
Initialize the system-wide MIDI players for both BGM and SFX. | |
void | deinitGlobalMidiPlayer (void) |
Deinitialize and free memory associated with the system-wide MIDI players. | |
void | globalMidiPlayerFillBuffer (uint8_t *samples, int16_t len) |
Fill the given sample buffer with samples from both the BGM and SFX system-wide MIDI players. | |
void | globalMidiPlayerPlaySong (midiFile_t *song, uint8_t trackType) |
Play a song on one of the system-wide MIDI players, either BGM or SFX. | |
void | globalMidiPlayerPlaySongCb (midiFile_t *song, uint8_t trackType, songFinishedCbFn cb) |
Play a song on noe of the system-wide MIDI players, with a callback once the song finishes. | |
void | globalMidiPlayerSetVolume (uint8_t trackType, int32_t volumeSetting) |
Set the volume using a value from 0 to 13. | |
void | globalMidiPlayerPauseAll (void) |
Pause all songs currently being played by the system-wide MIDI players. | |
void | globalMidiPlayerResumeAll (void) |
Resume all songs currently being played by the system-wide MIDI players. | |
void | globalMidiPlayerStop (bool reset) |
Stop all songs currently being played by the system-wide MIDI players, optionally resetting their state to the beginning of the song. | |
void * | globalMidiSave (void) |
Stop all MIDI playback and return a pointer containing the full playback state. This state must be passed to globalMidiRestore() later to restore the previous state. | |
void | globalMidiRestore (void *data) |
Resume MIDI playback from the state stored in the given pointer. The data will be freed after this call and cannot be reused. | |
midiPlayer_t * | globalMidiPlayerGet (uint8_t trackType) |
Return a pointer to the system-wide MIDI player for the given track type, either MIDI_SFX or MIDI_BGM. | |
struct envelope_t |
Data Fields | ||
---|---|---|
int32_t | attackTime | Base time taken to ramp up to full volume. |
q24_8 | attackTimeVel | This value will be multiplied by the note velocity and added to the attack time,. |
int32_t | decayTime | Base time taken for the volume to fade to the sustain volume. |
q24_8 | decayTimeVel | This value will be multiplied by the note velocity and added to the decay time. |
int32_t | releaseTime | Base time it takes to silence the note after release, in DAC samples. |
q24_8 | releaseTimeVel | This value will be multiplied by the note velocity and added to the attack time. |
uint8_t | sustainVol | The base volume of the sustained note, proportional to the sample volume. |
q24_8 | sustainVolVel | This value will be multiplied by the note velocity and added to the attack time. |
struct midiTimbre_t |
Data Fields | ||
---|---|---|
timbreType_t | type | The source of samples for this instrument. |
timbreFlags_t | flags | Flags bitfield for this timbre. |
union midiTimbre_t.__unnamed7__ | __unnamed__ | |
envelope_t | envelope | The ASDR characteristics of this timbre. |
timbreEffects_t | effects | Various effects applied to this timbre. May be ignored by percussion timbres. |
const char * | name | The name of this timbre, if any. |
struct midiVoice_t |
Data Fields | ||
---|---|---|
uint32_t | transitionTicks | The number of samples remaining before transitioning to the next state. |
uint32_t | transitionTicksTotal | The number of samples remaining before the next volume adjustment. |
uint8_t | transitionStartVol | The volume at the start of the transition time. |
uint8_t | targetVol | The target volume of this tick. |
uint32_t | sampleTick | The monotonic tick counter for playback of sampled timbres. |
uint8_t | note | The MIDI note number for the sound being played. |
uint8_t | velocity | The MIDI note velocity of the playing note. |
uint8_t | channel | The index of the MIDI channel that owns the currently playing note. |
synthOscillator_t | oscillators[OSC_PER_VOICE] | The synthesizer oscillators used to generate the sounds. |
union midiVoice_t.__unnamed18__ | __unnamed__ | |
const midiTimbre_t * | timbre | A pointer to the timbre of this voice, which defines its musical characteristics. |
struct voiceStates_t |
struct midiChannel_t |
Data Fields | ||
---|---|---|
uint16_t | volume | The 14-bit volume level for this channel only. |
uint16_t | bank | The bank to use for program changes on this channel. |
uint8_t | program | The ID of the program (timbre) set for this channel. |
bool | registeredParameter | Whether selectedParameter represents a registered or non-registered parameter. |
uint16_t | selectedParameter | The ID of the currently selected registered or non-registered parameter. |
midiTimbre_t | timbre | The actual current timbre definition which the program ID corresponds to. |
uint32_t | allocedVoices | A bitmap of which voices have been allocated to this channel. |
uint16_t | pitchBend | The 14-bit pitch wheel value. |
bool | percussion | Whether this channel is reserved for percussion. |
bool | held | Whether notes will be held after release. |
bool | sustenuto | Whether certain notes will be held after release. |
bool | ignore | If set, events on this channel will be completely ignored. |
struct midiPlayer_t |
Data Fields | ||
---|---|---|
uint16_t | volume | The global 14-bit volume level. |
midiChannel_t | channels[MIDI_CHANNEL_COUNT] | The state of all MIDI channels. |
midiVoice_t | percVoices[PERCUSSION_VOICES] | The voices reserved for percussion. |
voiceStates_t | percVoiceStates | The percussion voice state bitmaps. |
uint32_t | percSpecialStates | A bitmap to track which percussion voices have special notes playing This includes all 3 hi-hats (open, closed, and pedal), short and long whistle and guiro, and mute and open cuica and triangle. This value contains 5 separate 6-bit fields, each of which contains either the index of the voice used by that instrument group (0 to 31) or has all 6 bits set to indicate that the instrument group has no voice allocated. |
midiVoice_t | poolVoices[POOL_VOICE_COUNT] | The global voice pool for non-percussion channels. |
voiceStates_t | poolVoiceStates | The global voice pool state bitmaps. |
synthOscillator_t * | allOscillators[(POOL_VOICE_COUNT+PERCUSSION_VOICES) *OSC_PER_VOICE] | An array holding a pointer to every oscillator. |
uint16_t | oscillatorCount |
The total number of oscillators in the allOscillators array. |
midiPlayerMode_t | mode | Whether this player is playing a song or a MIDI stream. |
midiFileReader_t | reader | A MIDI reader to use for file playback, when in MIDI_FILE mode. |
midiStreamingCallback_t | streamingCallback | A callback function to retrieve the next event, when in MIDI_STREAMING mode. |
midiTextCallback_t | textMessageCallback | A callback to call when a text meta-message is received. |
songFinishedCbFn | songFinishedCallback | A callback to call when the playing song is finished. |
int32_t | headroom | The constant value to multiply each frame's samples by, before being shifted right 16 bits This value must be between 0x0000 (muted) and 0x7FFF (100% volume). The default value is 0x2666 (30%), and something around this value should be considered "full volume" with values higher than that being much more likely to clip for louder sounds. |
uint32_t | clipped |
Number of samples that were clipped Note: This is not set when using midiPlayerFillBufferMulti() |
uint64_t | sampleCount | The number of samples elapsed in the playing song. |
midiEvent_t | pendingEvent | The next event in the MIDI file, which occurs after the current time. |
bool | eventAvailable | True if pendingEvent is valid, false if it must be updated. |
uint32_t | tempo | The number of microseconds per quarter note. |
bool | paused | True when playback of the current file is paused. |
bool | seeking | True when the MIDI player is seeking, and will not produce sound. |
bool | loop | If true, the playing file will automatically repeat when complete. |
union midiTimbre_t.__unnamed7__ |
Data Fields | ||
---|---|---|
struct midiTimbre_t.__unnamed7__.__unnamed9__ | __unnamed__ | |
struct midiTimbre_t.__unnamed7__.sample | sample | |
struct midiTimbre_t.__unnamed7__.percussion | percussion | |
oscillatorShape_t | shape | The shape of this wave, when type is RAW_WAVE. |
struct midiTimbre_t.__unnamed7__.__unnamed9__ |
Data Fields | ||
---|---|---|
uint16_t | waveIndex | The index of this timbre's wave in the table, when type is WAVETABLE. |
waveFunc_t | waveFunc | The function to use to retrieve wavetable samples. |
struct midiTimbre_t.__unnamed7__.sample |
Data Fields | ||
---|---|---|
union midiTimbre_t.__unnamed7__.sample.__unnamed13__ | __unnamed__ | The frequency of the base sample to be used when pitch shifting. |
uint32_t | rate | The sample rate. |
uq8_24 | baseNote | The base frequency, at which the sample plays at normal speed. |
uint32_t | loop | 0 to loop forever, or the number of loops to play |
union midiTimbre_t.__unnamed7__.sample.__unnamed13__ |
Data Fields | ||
---|---|---|
struct midiTimbre_t.__unnamed7__.sample.__unnamed13__.__unnamed15__ | __unnamed__ | |
struct midiTimbre_t.__unnamed7__.sample.__unnamed13__.config | config |
struct midiTimbre_t.__unnamed7__.sample.__unnamed13__.__unnamed15__ |
struct midiTimbre_t.__unnamed7__.sample.__unnamed13__.config |
struct midiTimbre_t.__unnamed7__.percussion |
Data Fields | ||
---|---|---|
percussionFunc_t | playFunc | A callback to call for drum data. |
void * | data | User data to pass to the drumkit. |
union midiVoice_t.__unnamed18__ |
Data Fields | ||
---|---|---|
uint32_t | percScratch[4] | An array of scratch data for percussion functions to use. |
struct midiVoice_t.__unnamed18__.__unnamed20__ | __unnamed__ |
struct midiVoice_t.__unnamed18__.__unnamed20__ |
#define MIDI_CHANNEL_COUNT 16 |
#define POOL_VOICE_COUNT 24 |
#define PERCUSSION_VOICES 8 |
#define OSC_PER_VOICE 1 |
#define NUM_GLOBAL_PLAYERS 2 |
#define MIDI_SFX 0 |
#define MIDI_BGM 1 |
#define MAX_VOLUME 13 |
#define MIDI_TRUE 0x7F |
#define MIDI_FALSE 0x00 |
#define MIDI_TO_BOOL | ( | val | ) | (val > 63) |
#define BOOL_TO_MIDI | ( | val | ) | (val ? MIDI_TRUE : MIDI_FALSE) |
#define MIDI_DEF_HEADROOM 0x2666 |
#define PITCH_BEND_CENTER 0x2000 |
#define SAMPLES_TO_MIDI_TICKS | ( | n, | |
tempo, | |||
div ) ((n) * 1000000 * (div) / DAC_SAMPLE_RATE_HZ / (tempo)) |
Convert the sample count to MIDI ticks.
#define SAMPLES_TO_MS | ( | samp | ) | (((samp) * 1000) / DAC_SAMPLE_RATE_HZ) |
Convert samples to milliseconds.
#define SAMPLES_TO_US | ( | samp | ) | (((samp) * 1000000) / DAC_SAMPLE_RATE_HZ) |
Convert samples to microseconds.
#define MS_TO_SAMPLES | ( | ms | ) | ((ms) * DAC_SAMPLE_RATE_HZ / 1000) |
Calculate the number of DAC samples in the given number of milliseconds.
#define MIDI_TICKS_TO_US | ( | ticks, | |
tempo, | |||
div ) (int64_t)((int64_t)((int64_t)(ticks) * (int64_t)(tempo)) / ((int64_t)(div))) |
Convert MIDI ticks to microseconds.
typedef void(* songFinishedCbFn) (void) |
Callback function used to provide feedback when a song finishes playing.
typedef int8_t(* percussionFunc_t) (percussionNote_t drum, uint32_t idx, bool *done, uint32_t scratch[4], void *data) |
A function that returns samples for a percussion timbre rather than a melodic one.
drum | The percussion instrument to generate sound for | |
idx | The monotonic sample index within this note. Will not repeat for any particular note. | |
[out] | done | A pointer to a boolean to be set to 1 when the drum sample is finished playing |
[in,out] | scratch | A pointer to an array of 4 uint32_t that will persist for the duration of the note |
data | A pointer to user-defined data which may be used in sample generation |
typedef void(* midiTextCallback_t) (metaEventType_t type, const char *text, uint32_t length) |
A function to handle text meta-messages from playing MIDI files.
type | The type of meta-message |
text | The message text - NOT NUL-terminated |
length | The length of the text |
typedef bool(* midiStreamingCallback_t) (midiEvent_t *event) |
A function to return MIDI events in streaming mode.
enum midiPlayerMode_t |
Represents the source of MIDI data.
Enumerator | |
---|---|
MIDI_STREAMING | Streaming over USB. |
MIDI_FILE | Reading from a |
enum timbreType_t |
enum timbreFlags_t |
enum percussionNote_t |
Defines the MIDI note numbers mapped to by the General MIDI percussion note names.
Enumerator | |
---|---|
ACOUSTIC_BASS_DRUM_OR_LOW_BASS_DRUM | |
ELECTRIC_BASS_DRUM_OR_HIGH_BASS_DRUM | |
SIDE_STICK | |
ACOUSTIC_SNARE | |
HAND_CLAP | |
ELECTRIC_SNARE_OR_RIMSHOT | |
LOW_FLOOR_TOM | |
CLOSED_HI_HAT | This note supersedes any CLOSED_HI_HAT, PEDAL_HI_HAT, or OPEN_HI_HAT notes playing. |
HIGH_FLOOR_TOM | |
PEDAL_HI_HAT | This note supersedes any CLOSED_HI_HAT, PEDAL_HI_HAT, or OPEN_HI_HAT notes playing. |
LOW_TOM | |
OPEN_HI_HAT | This note supersedes any CLOSED_HI_HAT, PEDAL_HI_HAT, or OPEN_HI_HAT notes playing. |
LOW_MID_TOM | |
HIGH_MID_TOM | |
CRASH_CYMBAL_1 | |
HIGH_TOM | |
RIDE_CYMBAL_1 | |
CHINESE_CYMBAL | |
RIDE_BELL | |
TAMBOURINE | |
SPLASH_CYMBAL | |
COWBELL | |
CRASH_CYMBAL_2 | |
VIBRASLAP | |
RIDE_CYMBAL_2 | |
HIGH_BONGO | |
LOW_BONGO | |
MUTE_HIGH_CONGA | |
OPEN_HIGH_CONGA | |
LOW_CONGA | |
HIGH_TIMBALE | |
LOW_TIMBALE | |
HIGH_AGOGO | |
LOW_AGOGO | |
CABASA | |
MARACAS | |
SHORT_WHISTLE | This note supersedes any SHORT_WHISTLE or LONG_WHISTLE notes playing. |
LONG_WHISTLE | This note supersedes any SHORT_WHISTLE or LONG_WHISTLE notes playing. |
SHORT_GUIRO | This note supersedes any SHORT_GUIRO or LONG_GUIRO notes playing. |
LONG_GUIRO | This note supersedes any SHORT_GUIRO or LONG_GUIRO notes playing. |
CLAVES | |
HIGH_WOODBLOCK | |
LOW_WOODBLOCK | |
MUTE_CUICA | This note supersedes any SHORT_GUIRO or LONG_GUIRO notes playing. |
OPEN_CUICA | This note supersedes any SHORT_GUIRO or LONG_GUIRO notes playing. |
MUTE_TRIANGLE | This note supersedes any MUTE_TRIANGLE or OPEN_TRIANGLE notes playing. |
OPEN_TRIANGLE | This note supersedes any MUTE_TRIANGLE or OPEN_TRIANGLE notes playing. |
enum midiControl_t |
Defines the MIDI continuous controller registers.
Values suffixed with _MSB
and _LSB
represent the upper and lower 7 bits of two separate MIDI controller values. Some values are designated as switches, which will be considered off when set between 0 and 63, and on when set from 64 to 127. All other values represent a single 7-bit value with a range of 0 to 127 inclusive unless otherwise specified.
enum midiManufacturerId_t |
Values that can be directly compared against midiSysexEvent_t::manufacturerId.
Enumerator | |
---|---|
MMFR_EDUCATIONAL_USE | |
MMFR_UNIVERSAL_NON_REAL_TIME | |
MMFR_UNIVERSAL_REAL_TIME |
void midiPlayerInit | ( | midiPlayer_t * | player | ) |
Initialize the MIDI player.
player | The MIDI player to initialize |
void midiPlayerReset | ( | midiPlayer_t * | player | ) |
Reset the MIDI player state.
player | The MIDI player to reset |
void midiPlayerResetNewSong | ( | midiPlayer_t * | player | ) |
Reset the MIDI player state by only doing the bare minimum. This is useful when playing multiple sound effects in quick succession.
player | The MIDI player to reset |
int32_t midiPlayerStep | ( | midiPlayer_t * | player | ) |
Calculate and return the next MIDI sample, stepping the player state forward by one sample.
player | The player to step forward |
void midiPlayerFillBuffer | ( | midiPlayer_t * | player, |
uint8_t * | samples, | ||
int16_t | len ) |
Fill a buffer with the next set of samples from the MIDI player. This should be called by the callback passed into initDac(). Samples are generated at sampling rate of DAC_SAMPLE_RATE_HZ.
player | The MIDI player to sample from |
samples | An array of unsigned 8-bit samples to fill |
len | The length of the array to fill |
void midiPlayerFillBufferMulti | ( | midiPlayer_t * | players, |
uint8_t | playerCount, | ||
uint8_t * | samples, | ||
int16_t | len ) |
Fill a buffer with the next set of samples from an array of MIDI players.
players | A pointer to an array of MIDI players |
playerCount | The number of MIDI players in the array |
samples | An array of unsigned 8-bit samples to fill |
len | The length of the array to fill |
void midiAllSoundOff | ( | midiPlayer_t * | player | ) |
Stop all sound immediately. This is not affected by the sustain pedal.
player | The player to stop |
void midiResetChannelControllers | ( | midiPlayer_t * | player, |
uint8_t | channel ) |
Reset all controllers on a MIDI channel.
This includes:
player | |
channel |
void midiGmOn | ( | midiPlayer_t * | player | ) |
Activate General MIDI mode on a MIDI player.
player | The MIDI player to set to General MIDI mode |
void midiGmOff | ( | midiPlayer_t * | player | ) |
Deactivate General MIDI mode on a MIDI player.
player | The MIDI player to take out of General MIDI mode |
void midiAllNotesOff | ( | midiPlayer_t * | player, |
uint8_t | channel ) |
Tun off all notes which are currently on, as though midiNoteOff() were called for each note. This respects the sustain pedal.
player | The MIDI player |
channel | The MIDI channel on which to stop notes |
void midiNoteOn | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | note, | ||
uint8_t | velocity ) |
Begin playing a note on a given MIDI channel.
Using a velocity of 0
is equivalent to calling midiNoteOff()
player | The MIDI player |
channel | The MIDI channel on which to start the note |
note | The note number to play |
velocity | The note velocity which affects its volume and effects |
void midiAfterTouch | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | note, | ||
uint8_t | velocity ) |
Change the velocity of a note on a given MIDI channel, after the note starts playing.
player | The MIDI player |
channel | The MIDI channel on which to change the note velocity |
note | The currently-playing note number to modify |
velocity | The note velocity which affects its volume and effects |
void midiNoteOff | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | note, | ||
uint8_t | velocity ) |
Stop playing a particular note on a given MIDI channel.
player | The MIDI player |
channel | The MIDI channel on which to stop the note |
note | The note number to stop |
velocity | [NYI] The release velocity which affects the note's release time |
void midiSetProgram | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | program ) |
Change the program (instrument) on a given MIDI channel.
player | The MIDI player |
channel | The MIDI channel whose program will be changed |
program | The program ID, from 0-127, to set for this channel |
void midiSustain | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | val ) |
Set the hold pedal status.
When set, all notes that are currently on will be sustained until the pedal is unset, as well as all notes that are played after the pedal is set.
This is a convenience method for midiControlChange(player, channel, MCC_HOLD_PEDAL (64), val ? 127:0)
player | The MIDI player |
channel | The MIDI channel to set the hold status for |
val | The sustain pedal value. Values 0-63 are OFF, and 64-127 are ON. |
void midiSustenuto | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint8_t | val ) |
Set the sustenuto pedal status.
When set, only the notes that are currently on will be sustained until the pedal is unset. Notes that are played after the pedal is set will not be sustained.
player | The MIDI player |
channel | The MIDI channel to set the sustenuto status for |
val | The sustenuto pedal value. Values 0-63 are OFF, and 64-127 are ON. |
void midiControlChange | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
midiControl_t | control, | ||
uint8_t | val ) |
Set a MIDI control value.
player | The MIDI player |
channel | The channel to set the control on |
control | The control number to set |
val | The control value, from 0-127 whose meaning depends on the control number |
uint8_t midiGetControlValue | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
midiControl_t | control ) |
Get the value of a MIDI control.
player | The MIDI player |
channel | The channel to retrieve the control from |
control | The MIDI control number of the control |
uint16_t midiGetControlValue14bit | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
midiControl_t | control ) |
Get the combined value of two MIDI control registers.
player | The MIDI player |
channel | The channel to retrieve the control from |
control | The MIDI control number of either of the two controls that make up the value |
void midiSetParameter | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
bool | registered, | ||
uint16_t | param, | ||
uint16_t | value ) |
Set a registered or non-registered parameter value.
player | The MIDI player |
channel | The channel to set the parameter on |
registered | true if param refers to a registered parameter number and false if it refers to a non-registered |
param | The registered or non-registered MIDI parameter to set the value of |
value | The 14-bit value to set the parameter to |
uint16_t midiGetParameterValue | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
bool | registered, | ||
uint16_t | param ) |
Get the value of a registered or non-registered parameter.
player | The MIDI player |
channel | The channel to retrieve the parameter from |
registered | true if param refers to a registered parameter number and false if it refers to a non-registered |
param | The registered or non-registered MIDI parameter number to retrieve the value for |
void midiPitchWheel | ( | midiPlayer_t * | player, |
uint8_t | channel, | ||
uint16_t | value ) |
Set the pitch wheel value on a given MIDI channel.
By default, the center of the pitch wheel is 0x2000
. A value of 0x0000
transposes one step down, while a value of 0x3FFF
transposes one step down.
[NYI] The range of the pitch wheel can be changed using the registered parameters, with MSB being the range in (+/-)semitones and LSB being the range in (+/-) cents
player | The MIDI player |
channel | The MIDI channel to change the pitch wheel for |
value | The pitch wheel value, from 0 to 0x3FFF (14-bits) |
void midiSetTempo | ( | midiPlayer_t * | player, |
uint32_t | tempo ) |
Change the MIDI playback tempo.
player | The player to change the tempo of |
tempo | The new tempo to set, in microseconds per quarter note |
void midiSetFile | ( | midiPlayer_t * | player, |
const midiFile_t * | file ) |
Configure this MIDI player to read from a MIDI file.
player | The MIDI player |
file | A pointer to the MIDI file to be played |
void midiPause | ( | midiPlayer_t * | player, |
bool | pause ) |
Set the paused state of a MIDI song.
player | The player |
pause | True to pause, false to play |
void midiSeek | ( | midiPlayer_t * | player, |
uint32_t | ticks ) |
Seek to a given time offset within a file.
Note that in the current implementation, seeking backwards by any amount requires re-reading the file from the beginning, and so may be very slow, particularly for large MIDI files.
player | The MIDI player to seek on |
ticks | The absolute number of MIDI ticks to seek to. If this is -1, it will seek to the end of the song |
void initGlobalMidiPlayer | ( | void | ) |
Initialize the system-wide MIDI players for both BGM and SFX.
void deinitGlobalMidiPlayer | ( | void | ) |
Deinitialize and free memory associated with the system-wide MIDI players.
void globalMidiPlayerFillBuffer | ( | uint8_t * | samples, |
int16_t | len ) |
Fill the given sample buffer with samples from both the BGM and SFX system-wide MIDI players.
[out] | samples | A pointer to the array of unsigned 8-bit samples to be filled |
len | The number of samples |
void globalMidiPlayerPlaySong | ( | midiFile_t * | song, |
uint8_t | trackType ) |
Play a song on one of the system-wide MIDI players, either BGM or SFX.
song | A pointer to the song to play |
trackType | The player to use, either MIDI_SFX or MIDI_BGM |
void globalMidiPlayerPlaySongCb | ( | midiFile_t * | song, |
uint8_t | trackType, | ||
songFinishedCbFn | cb ) |
Play a song on noe of the system-wide MIDI players, with a callback once the song finishes.
song | A pointer to the song to play |
trackType | The player to use, either MIDI_SFX or MIDI_BGM |
cb | The function to be called once the song completes |
void globalMidiPlayerSetVolume | ( | uint8_t | trackType, |
int32_t | volumeSetting ) |
Set the volume using a value from 0 to 13.
trackType | The player to set the volume for, either MIDI_SFX or MIDI_BGM |
volumeSetting | The volume value |
void globalMidiPlayerPauseAll | ( | void | ) |
Pause all songs currently being played by the system-wide MIDI players.
void globalMidiPlayerResumeAll | ( | void | ) |
Resume all songs currently being played by the system-wide MIDI players.
void globalMidiPlayerStop | ( | bool | reset | ) |
Stop all songs currently being played by the system-wide MIDI players, optionally resetting their state to the beginning of the song.
reset | if true, the players will be reset to the beginning of the song |
void * globalMidiSave | ( | void | ) |
Stop all MIDI playback and return a pointer containing the full playback state. This state must be passed to globalMidiRestore() later to restore the previous state.
void globalMidiRestore | ( | void * | data | ) |
Resume MIDI playback from the state stored in the given pointer. The data will be freed after this call and cannot be reused.
data | The playback state data returned by globalMidiSave() |
midiPlayer_t * globalMidiPlayerGet | ( | uint8_t | trackType | ) |
Return a pointer to the system-wide MIDI player for the given track type, either MIDI_SFX or MIDI_BGM.
trackType | Which MIDI player to return, either MIDI_SFX or MIDI_BGM |