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

Go to the source code of this file.

Data Structures

struct  midiTrack_t
 Contains basic information pointing to a MIDI track within its file data. More...
 
struct  midiFile_t
 Contains information which applies to the entire MIDI file. More...
 
struct  midiFileReader_t
 
struct  midiStatusEvent_t
 Data for a normal MIDI status event. More...
 
struct  midiTimeSignature_t
 Data for a MIDI time signature definition. More...
 
struct  midiMetaEvent_t
 Contains information for a non-MIDI meta-event from a MIDI file. More...
 
struct  midiSysexEvent_t
 Contains information for a MIDI System Exclusive event. More...
 
struct  midiEvent_t
 Contains information for an entire MIDI event or non-MIDI meta-event. More...
 
union  midiMetaEvent_t.__unnamed1__
 
struct  midiMetaEvent_t.__unnamed1__.startTime
 Contains the start time of this track, when type is SMPTE_OFFSET. More...
 
struct  midiMetaEvent_t.__unnamed1__.keySignature
 Contains key signature data, when type is KEY_SIGNATURE. More...
 
union  midiEvent_t.__unnamed5__
 

Typedefs

typedef struct midiTrackState midiTrackState_t
 

Enumerations

enum  midiEventType_t { MIDI_EVENT , META_EVENT , SYSEX_EVENT }
 The possible sub-types of MIDI events. More...
 
enum  metaEventType_t {
  SEQUENCE_NUMBER = 0x00 , TEXT = 0x01 , COPYRIGHT = 0x02 , SEQUENCE_OR_TRACK_NAME = 0x03 ,
  INSTRUMENT_NAME = 0x04 , LYRIC = 0x05 , MARKER = 0x06 , CUE_POINT = 0x07 ,
  CHANNEL_PREFIX = 0x20 , PORT_PREFIX = 0x21 , END_OF_TRACK = 0x2F , TEMPO = 0x51 ,
  SMPTE_OFFSET = 0x54 , TIME_SIGNATURE = 0x58 , KEY_SIGNATURE = 0x59 , PROPRIETARY = 0x7F
}
 The possible types of meta-events. More...
 
enum  midiFileFormat_t { MIDI_FORMAT_0 = 0 , MIDI_FORMAT_1 = 1 , MIDI_FORMAT_2 = 2 }
 The MIDI file format, which determines how to interpret the track or tracks it contains. More...
 

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.
 
uint32_t midiNextEventTime (midiFileReader_t *reader)
 Return the start time of the next event in the MIDI file being read.
 
bool midiNextEvent (midiFileReader_t *reader, midiEvent_t *event)
 Process and retrieve the next MIDI event in the file.
 
int midiWriteEvent (uint8_t *out, int max, const midiEvent_t *event)
 Writes a MIDI event to a byte buffer.
 

Data Structure Documentation

◆ midiTrack_t

struct midiTrack_t
Data Fields
uint32_t length Total chunk length.
uint8_t * data Pointer to the start of this chunk's data.

◆ midiFile_t

struct midiFile_t
Data Fields
uint8_t * data A pointer to the start of the MIDI file.
uint32_t length The total length of the MIDI file.
midiFileFormat_t format The MIDI file format which defines how this file's tracks are interpreted.
uint16_t timeDivision The time division of MIDI frames, either ticks per frame or ticks per quarter note.
uint16_t trackCount The number of tracks in this file.
midiTrack_t * tracks An array of MIDI tracks.

◆ midiFileReader_t

struct midiFileReader_t
Data Fields
const midiFile_t * file A pointer to the MIDI file currently loaded into the reader, if any.
bool handleMetaEvents If true, text meta-events will be handled and sent to the MIDI player.
uint16_t division The number of divisions per midi tick.
uint8_t stateCount The number of track states allocated.
midiTrackState_t * states An array containing the internal parser state for each track.

◆ midiStatusEvent_t

struct midiStatusEvent_t
Data Fields
uint8_t status The MIDI status byte.
uint8_t data[2] The data bytes of the MIDI status event. The meaning of these bytes depends on the status value.

◆ midiTimeSignature_t

struct midiTimeSignature_t
Data Fields
uint8_t numerator The numerator of the time signature.
uint8_t denominator The power of two of the time signature denominator (e.g. 2 for 4/4, 3 for 4/8)
uint8_t midiClocksPerMetronomeTick The number of MIDI clocks per metronome tick.
uint8_t num32ndNotesPerBeat Number of 32nd notes per 24 MIDI clocks (= 1 beat)

◆ midiMetaEvent_t

struct midiMetaEvent_t
Data Fields
metaEventType_t type The type of this MIDI meta-event.
uint32_t length The number of data bytes this meta event contains.
union midiMetaEvent_t.__unnamed1__ __unnamed__

◆ midiSysexEvent_t

struct midiSysexEvent_t
Data Fields
uint16_t manufacturerId The manufacturer ID embedded in the SysEx event.
Note
When this is a single-byte manufacturer ID, the most-significant bit (15) will be set
uint32_t length The length of the data contained in this SysEx event.
uint8_t prefix A byte to prefix to the actual data, if non-zero.
const uint8_t * data A pointer to the data for this SysEx event.

◆ midiEvent_t

struct midiEvent_t
Data Fields
uint32_t deltaTime The time between this event and the previous event.
uint32_t absTime The absolute timestamp of this event in ticks.
midiEventType_t type The overall event type – MIDI, Meta, or SysEx.
uint8_t track The index of the track which contains this event.
union midiEvent_t.__unnamed5__ __unnamed__

◆ midiMetaEvent_t.__unnamed1__

union midiMetaEvent_t.__unnamed1__
Data Fields
const char * text Contains text data, when {type <= 0x0F}.
Warning
This data is NOT NUL-terminated
const uint8_t * data Contains binary data, when type is PROPRIETARY.
uint32_t tempo Contains a tempo, in microseconds per quarter note, when type is TEMPO.
uint16_t sequenceNumber Contains a sequence number for this track, when type is SEQUENCE_NUMBER.
uint8_t prefix Contains a channel or port prefix, when type is CHANNEL_PREFIX or PORT_PREFIX respectively.
struct midiMetaEvent_t.__unnamed1__.startTime startTime Contains the start time of this track, when type is SMPTE_OFFSET.
midiTimeSignature_t timeSignature Contains time signature data, when type is TIME_SIGNATURE.
struct midiMetaEvent_t.__unnamed1__.keySignature keySignature Contains key signature data, when type is KEY_SIGNATURE.
Note
At most one of flats or sharps will contain a nonzero value.

◆ midiMetaEvent_t.__unnamed1__.startTime

struct midiMetaEvent_t.__unnamed1__.startTime
Data Fields
uint8_t hour
uint8_t min
uint8_t sec
uint8_t frame
uint8_t frameHundredths

◆ midiMetaEvent_t.__unnamed1__.keySignature

struct midiMetaEvent_t.__unnamed1__.keySignature
Data Fields
uint8_t flats The number of flats in the key.
uint8_t sharps The number of sharps in the key.
bool minor True for a minor key, false for a major key.

◆ midiEvent_t.__unnamed5__

union midiEvent_t.__unnamed5__
Data Fields
midiStatusEvent_t midi The MIDI status event data, when type is MIDI_EVENT.
midiMetaEvent_t meta The non-MIDI meta-event data, when type is META_EVENT.
midiSysexEvent_t sysex The MIDI System Exclusive event data, when type is SYSEX_EVENT.

Typedef Documentation

◆ midiTrackState_t

Enumeration Type Documentation

◆ midiEventType_t

The possible sub-types of MIDI events.

Enumerator
MIDI_EVENT 

A normal MIDI status event, such as note on or off.

META_EVENT 

A non-MIDI meta-event from a MIDI file, such as tempo or lyrics.

SYSEX_EVENT 

A system-exclusive MIDI event.

◆ metaEventType_t

The possible types of meta-events.

Enumerator
SEQUENCE_NUMBER 
TEXT 
COPYRIGHT 
SEQUENCE_OR_TRACK_NAME 
INSTRUMENT_NAME 
LYRIC 
MARKER 
CUE_POINT 
CHANNEL_PREFIX 
PORT_PREFIX 
END_OF_TRACK 
TEMPO 
SMPTE_OFFSET 
TIME_SIGNATURE 
KEY_SIGNATURE 
PROPRIETARY 

◆ midiFileFormat_t

The MIDI file format, which determines how to interpret the track or tracks it contains.

Enumerator
MIDI_FORMAT_0 

One track only containing all MIDI data for any number of channels.

MIDI_FORMAT_1 

Multiple MIDI tracks, played simultaneously.

MIDI_FORMAT_2 

Multiple MIDI tracks, played sequentially.

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

◆ midiNextEventTime()

uint32_t midiNextEventTime ( midiFileReader_t * reader)

Return the start time of the next event in the MIDI file being read.

Parameters
readerThe reader to check
Returns
uint32_t The timestamp of the next event, or UINT32_MAX if there is none

◆ 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

◆ 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