Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
midiFileParser.h
Go to the documentation of this file.
1#pragma once
2
3//==============================================================================
4// Includes
5//==============================================================================
6
7#include <stdint.h>
8#include <stdbool.h>
9
10//==============================================================================
11// Enums
12//==============================================================================
13
26
30typedef enum
31{
33 TEXT = 0x01,
34 COPYRIGHT = 0x02,
37 LYRIC = 0x05,
38 MARKER = 0x06,
39 CUE_POINT = 0x07,
40 // I'm not sure these are real???
41 // PROGRAM_NAME,
42 // DEVICE_NAME,
46 TEMPO = 0x51,
52
63
64//==============================================================================
65// Structs
66//==============================================================================
67
71typedef struct
72{
74 uint32_t length;
75
77 uint8_t* data;
79
83typedef struct
84{
86 uint8_t* data;
87
89 uint32_t length;
90
93
95 uint16_t timeDivision;
96
98 uint16_t trackCount;
99
102} midiFile_t;
103
105
106typedef struct
107{
110
113
115 uint16_t division;
116
118 uint8_t stateCount;
119
123
127typedef struct
128{
130 uint8_t status;
131
134 uint8_t data[2];
136
151
155typedef struct
156{
159
161 uint32_t length;
162
163 union
164 {
167 const char* text;
168
170 const uint8_t* data;
171
173 uint32_t tempo;
174
176 uint16_t sequenceNumber;
177
179 uint8_t prefix;
180
182 struct
183 {
184 uint8_t hour;
185 uint8_t min;
186 uint8_t sec;
187 uint8_t frame;
188 uint8_t frameHundredths;
189 } startTime;
190
192 midiTimeSignature_t timeSignature;
193
196 struct
197 {
199 uint8_t flats;
200
202 uint8_t sharps;
203
205 bool minor;
206 } keySignature;
207 };
209
213typedef struct
214{
218
220 uint32_t length;
221
223 uint8_t prefix;
224
226 const uint8_t* data;
228
232typedef struct
233{
235 uint32_t deltaTime;
236
238 uint32_t absTime;
239
242
244 uint8_t track;
245
246 union
247 {
251 midiMetaEvent_t meta;
253 midiSysexEvent_t sysex;
254 };
256
257//==============================================================================
258// Function Declarations
259//==============================================================================
260
270bool loadMidiFile(const char* name, midiFile_t* file, bool spiRam);
271
277void unloadMidiFile(midiFile_t* file);
278
287bool initMidiParser(midiFileReader_t* reader, const midiFile_t* file);
288
295void midiParserSetFile(midiFileReader_t* reader, const midiFile_t* file);
296
305
312
320
329bool midiNextEvent(midiFileReader_t* reader, midiEvent_t* event);
330
339int midiWriteEvent(uint8_t* out, int max, const midiEvent_t* event);
Contains all track-specific parsing state.
Definition midiFileParser.c:25
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.
Definition midiFileParser.c:888
midiTrackState_t * states
An array containing the internal parser state for each track.
Definition midiFileParser.h:121
uint32_t deltaTime
The time between this event and the previous event.
Definition midiFileParser.h:235
uint8_t midiClocksPerMetronomeTick
The number of MIDI clocks per metronome tick.
Definition midiFileParser.h:147
midiTrack_t * tracks
An array of MIDI tracks.
Definition midiFileParser.h:101
midiFileFormat_t format
The MIDI file format which defines how this file's tracks are interpreted.
Definition midiFileParser.h:92
metaEventType_t type
The type of this MIDI meta-event.
Definition midiFileParser.h:158
midiFileFormat_t
The MIDI file format, which determines how to interpret the track or tracks it contains.
Definition midiFileParser.h:55
@ MIDI_FORMAT_0
One track only containing all MIDI data for any number of channels.
Definition midiFileParser.h:57
@ MIDI_FORMAT_2
Multiple MIDI tracks, played sequentially.
Definition midiFileParser.h:61
@ MIDI_FORMAT_1
Multiple MIDI tracks, played simultaneously.
Definition midiFileParser.h:59
uint8_t num32ndNotesPerBeat
Number of 32nd notes per 24 MIDI clocks (= 1 beat)
Definition midiFileParser.h:149
int midiWriteEvent(uint8_t *out, int max, const midiEvent_t *event)
Writes a MIDI event to a byte buffer.
Definition midiFileParser.c:1073
const uint8_t * data
A pointer to the data for this SysEx event.
Definition midiFileParser.h:226
midiEventType_t
The possible sub-types of MIDI events.
Definition midiFileParser.h:18
@ META_EVENT
A non-MIDI meta-event from a MIDI file, such as tempo or lyrics.
Definition midiFileParser.h:22
@ MIDI_EVENT
A normal MIDI status event, such as note on or off.
Definition midiFileParser.h:20
@ SYSEX_EVENT
A system-exclusive MIDI event.
Definition midiFileParser.h:24
uint8_t stateCount
The number of track states allocated.
Definition midiFileParser.h:118
void resetMidiParser(midiFileReader_t *reader)
Reset the state of the MIDI parser without deinitializing it or changing the file.
Definition midiFileParser.c:912
uint8_t denominator
The power of two of the time signature denominator (e.g. 2 for 4/4, 3 for 4/8)
Definition midiFileParser.h:145
bool midiNextEvent(midiFileReader_t *reader, midiEvent_t *event)
Process and retrieve the next MIDI event in the file.
Definition midiFileParser.c:953
uint16_t timeDivision
The time division of MIDI frames, either ticks per frame or ticks per quarter note.
Definition midiFileParser.h:95
metaEventType_t
The possible types of meta-events.
Definition midiFileParser.h:31
@ TIME_SIGNATURE
Definition midiFileParser.h:48
@ SEQUENCE_NUMBER
Definition midiFileParser.h:32
@ KEY_SIGNATURE
Definition midiFileParser.h:49
@ LYRIC
Definition midiFileParser.h:37
@ MARKER
Definition midiFileParser.h:38
@ PORT_PREFIX
Definition midiFileParser.h:44
@ CUE_POINT
Definition midiFileParser.h:39
@ SEQUENCE_OR_TRACK_NAME
Definition midiFileParser.h:35
@ TEXT
Definition midiFileParser.h:33
@ COPYRIGHT
Definition midiFileParser.h:34
@ CHANNEL_PREFIX
Definition midiFileParser.h:43
@ INSTRUMENT_NAME
Definition midiFileParser.h:36
@ PROPRIETARY
Definition midiFileParser.h:50
@ SMPTE_OFFSET
Definition midiFileParser.h:47
@ TEMPO
Definition midiFileParser.h:46
@ END_OF_TRACK
Definition midiFileParser.h:45
uint16_t trackCount
The number of tracks in this file.
Definition midiFileParser.h:98
const midiFile_t * file
A pointer to the MIDI file currently loaded into the reader, if any.
Definition midiFileParser.h:109
void deinitMidiParser(midiFileReader_t *reader)
Deinitialize the MIDI file reader and free any memory it has allocated.
Definition midiFileParser.c:939
uint8_t track
The index of the track which contains this event.
Definition midiFileParser.h:244
uint8_t numerator
The numerator of the time signature.
Definition midiFileParser.h:143
uint8_t * data
Pointer to the start of this chunk's data.
Definition midiFileParser.h:77
void unloadMidiFile(midiFile_t *file)
Free the data associated with the given MIDI file.
Definition midiFileParser.c:865
uint32_t absTime
The absolute timestamp of this event in ticks.
Definition midiFileParser.h:238
uint8_t prefix
A byte to prefix to the actual data, if non-zero.
Definition midiFileParser.h:223
bool loadMidiFile(const char *name, midiFile_t *file, bool spiRam)
Load a MIDI file from the filesystem.
Definition midiFileParser.c:794
uint8_t status
The MIDI status byte.
Definition midiFileParser.h:130
midiEventType_t type
The overall event type – MIDI, Meta, or SysEx.
Definition midiFileParser.h:241
uint32_t length
Total chunk length.
Definition midiFileParser.h:74
uint16_t manufacturerId
The manufacturer ID embedded in the SysEx event.
Definition midiFileParser.h:217
bool handleMetaEvents
If true, text meta-events will be handled and sent to the MIDI player.
Definition midiFileParser.h:112
bool initMidiParser(midiFileReader_t *reader, const midiFile_t *file)
Initialize or reset the MIDI file reader with a particular file.
Definition midiFileParser.c:872
uint32_t midiNextEventTime(midiFileReader_t *reader)
Return the start time of the next event in the MIDI file being read.
uint16_t division
The number of divisions per midi tick.
Definition midiFileParser.h:115
Contains information for an entire MIDI event or non-MIDI meta-event.
Definition midiFileParser.h:233
Definition midiFileParser.h:107
Contains information which applies to the entire MIDI file.
Definition midiFileParser.h:84
Contains information for a non-MIDI meta-event from a MIDI file.
Definition midiFileParser.h:156
Data for a normal MIDI status event.
Definition midiFileParser.h:128
Contains information for a MIDI System Exclusive event.
Definition midiFileParser.h:214
Data for a MIDI time signature definition.
Definition midiFileParser.h:141
Contains basic information pointing to a MIDI track within its file data.
Definition midiFileParser.h:72