Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
midiFileParser.c File Reference

Data Structures

struct  midiTrackState
 Contains all track-specific parsing state. More...
 
struct  midiSaveState_t
 

Macros

#define TRK_REMAIN()   (track->track->length - (track->cur - track->track->data))
 
#define ERR()
 

Functions

bool loadMidiFile (const char *name, midiFile_t *file, bool spiRam)
 Load a MIDI file from the filesystem.
 
void unloadMidiFile (midiFile_t *file)
 Free the data associated with the given MIDI file.
 
bool initMidiParser (midiFileReader_t *reader, const midiFile_t *file)
 Initialize or reset the MIDI file reader with a particular file.
 
void midiParserSetFile (midiFileReader_t *reader, const midiFile_t *file)
 Set a new file for the MIDI file reader. The reader's state will be reset.
 
void resetMidiParser (midiFileReader_t *reader)
 Reset the state of the MIDI parser without deinitializing it or changing the file.
 
void deinitMidiParser (midiFileReader_t *reader)
 Deinitialize the MIDI file reader and free any memory it has allocated.
 
bool midiNextEvent (midiFileReader_t *reader, midiEvent_t *event)
 Process and retrieve the next MIDI event in the file.
 
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.
 
int midiWriteEvent (uint8_t *out, int max, const midiEvent_t *event)
 Writes a MIDI event to a byte buffer.
 

Data Structure Documentation

◆ midiTrackState

struct midiTrackState
Data Fields
const midiTrack_t * track A pointer to the MIDI track itself.
const uint8_t * cur The pointer to the current offset within this chunk.
uint32_t time The accumulated delta times for this track, which nextEvent is relative to.
bool eventParsed True if the next event in this channel has already been parsed.
midiEvent_t nextEvent The next event, when eventParsed is true.
uint8_t runningStatus The last applicable running status command, or 0 if none.
bool done Whether or not the END OF TRACK event has been read.

◆ midiSaveState_t

struct midiSaveState_t
Data Fields
midiPlayer_t player
uint8_t trackCount
midiTrackState_t * trackStates

Macro Definition Documentation

◆ TRK_REMAIN

#define TRK_REMAIN ( )    (track->track->length - (track->cur - track->track->data))

◆ ERR

#define ERR ( )
Value:
do \
{ \
track->eventParsed = false; \
track->nextEvent.deltaTime = UINT32_MAX; \
ESP_LOGE("MIDIParser", "Error parsing at line %d and offset %" PRIuPTR " (total offset %" PRIuPTR ")", \
__LINE__, (track->cur - track->track->data), (track->cur - reader->file->data)); \
return false; \
} while (0)

Function Documentation

◆ loadMidiFile()

bool loadMidiFile ( const char * name,
midiFile_t * file,
bool spiRam )

Load a MIDI file from the filesystem.

Parameters
fileA pointer to a midiFile_t struct to load the file into
nameThe name of the MIDI file to load
spiRamWhether to load the MIDI file into SPIRAM
Returns
true If the load succeeded
false If the load failed

◆ unloadMidiFile()

void unloadMidiFile ( midiFile_t * file)

Free the data associated with the given MIDI file.

Parameters
fileA pointer to the MIDI file to be unloaded

◆ initMidiParser()

bool initMidiParser ( midiFileReader_t * reader,
const midiFile_t * file )

Initialize or reset the MIDI file reader with a particular file.

Parameters
readerA pointer to the MIDI file reader to initialize
fileA pointer to the MIDI file to load
Returns
true if the MIDI file reader was initialized
false if an error occurred while allocating data for the MIDI file reader

◆ midiParserSetFile()

void midiParserSetFile ( midiFileReader_t * reader,
const midiFile_t * file )

Set a new file for the MIDI file reader. The reader's state will be reset.

Parameters
readerA pointer to the MIDI file reader to set the file of
fileA pointer to the MIDI file to load

◆ resetMidiParser()

void resetMidiParser ( midiFileReader_t * reader)

Reset the state of the MIDI parser without deinitializing it or changing the file.

The next event returned from this MIDI parser will be the first event in the file again.

Parameters
readerA pointer to the MIDI file reader to reset

◆ deinitMidiParser()

void deinitMidiParser ( midiFileReader_t * reader)

Deinitialize the MIDI file reader and free any memory it has allocated.

Parameters
readerThe MIDI file reader to deinitialize

◆ midiNextEvent()

bool midiNextEvent ( midiFileReader_t * reader,
midiEvent_t * event )

Process and retrieve the next MIDI event in the file.

Parameters
readerThe reader to read the event from
eventA pointer to a MIDI event to be updated with the next event
Returns
true If event data was written to event
false If there are no more events in this file or there was a fatal parse error

◆ globalMidiSave()

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.

Returns
void* A void-pointer containing the full MIDI playback state

◆ globalMidiRestore()

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.

Parameters
dataThe playback state data returned by globalMidiSave()

◆ midiWriteEvent()

int midiWriteEvent ( uint8_t * out,
int max,
const midiEvent_t * event )

Writes a MIDI event to a byte buffer.

Parameters
outThe byte array
maxThe maximum number of bytes to write
eventThe event to write
Returns
int The number of bytes written