-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMIDITemplate.bt
executable file
·241 lines (232 loc) · 5.55 KB
/
MIDITemplate.bt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* General-MIDI Parser
* By Jack Andersen
*/
BigEndian();
struct MidiHeader
{
char m_magic[4] <format = hex>;
uint m_seclen;
enum <short>
{
MIDI_SINGLE = 0,
MIDI_MULTIPLE = 1,
MIDI_PATTERN = 2
} m_format;
short m_ntracks;
short m_tickdiv;
};
struct DeltaTime
{
local uint total = 0;
char t0;
total += t0 & 0x7f;
if (!(t0 & 0x80))
break;
total <<= 7;
char t1;
total += t1 & 0x7f;
if (!(t1 & 0x80))
break;
total <<= 7;
char t2;
total += t2 & 0x7f;
if (!(t2 & 0x80))
break;
total <<= 7;
char t3;
total += t3 & 0x7f;
if (!(t3 & 0x80))
break;
};
struct MidiMessage
{
DeltaTime m_dtime;
char m_status;
local char m_channel = m_status & 0xf;
if ((m_status & 0xf0) == 0x80)
{
struct
{
char m_note;
char m_velocity;
} note_off_event;
}
else if ((m_status & 0xf0) == 0x90)
{
struct
{
char m_note;
char m_velocity;
} note_on_event;
}
else if ((m_status & 0xf0) == 0xA0)
{
struct
{
char m_note;
char m_pressure;
} note_pressure_event;
}
else if ((m_status & 0xf0) == 0xB0)
{
struct
{
char m_controller;
char m_value;
} controller_event;
}
else if ((m_status & 0xf0) == 0xC0)
{
struct
{
char m_program;
} program_event;
}
else if ((m_status & 0xf0) == 0xD0)
{
struct
{
char m_pressure;
} channel_pressure_event;
}
else if ((m_status & 0xf0) == 0xE0)
{
struct
{
char m_lsb;
char m_msb;
} pitch_bend_event;
}
else if (m_status == -1)
{
struct
{
enum <char>
{
META_SEQUENCE_NUM = 0,
META_TEXT = 1,
META_COPYRIGHT = 2,
META_SEQUENCE_NAME = 3,
META_INSTRUMENT_NAME = 4,
META_LYRIC = 5,
META_MARKER = 6,
META_CUE_POINT = 7,
META_PROGRAM_NAME = 8,
META_DEVICE_NAME = 9,
META_MIDI_CHANNEL_PREFIX = 0x20,
META_MIDI_PORT = 0x21,
META_END_OF_TRACK = 0x2f,
META_TEMPO = 0x51,
META_SMPTE_OFFSET = 0x54,
META_TIME_SIGNATURE = 0x58,
META_KEY_SIGNATURE = 0x59,
META_SEQUENCER_EVENT = 0x7f
} m_type;
DeltaTime m_length;
if (m_type == META_SEQUENCE_NUM)
{
short m_seqNum;
}
else if (m_type == META_TEXT)
{
char m_text[m_length.total];
}
else if (m_type == META_COPYRIGHT)
{
char m_copyright[m_length.total];
}
else if (m_type == META_SEQUENCE_NAME)
{
char m_name[m_length.total];
}
else if (m_type == META_INSTRUMENT_NAME)
{
char m_name[m_length.total];
}
else if (m_type == META_LYRIC)
{
char m_lyric[m_length.total];
}
else if (m_type == META_MARKER)
{
char m_marker[m_length.total];
}
else if (m_type == META_CUE_POINT)
{
char m_cuePoint[m_length.total];
}
else if (m_type == META_PROGRAM_NAME)
{
char m_programName[m_length.total];
}
else if (m_type == META_DEVICE_NAME)
{
char m_deviceName[m_length.total];
}
else if (m_type == META_MIDI_CHANNEL_PREFIX)
{
char m_channelPrefix;
}
else if (m_type == META_MIDI_PORT)
{
char m_port;
}
else if (m_type == META_END_OF_TRACK)
{
}
else if (m_type == META_TEMPO)
{
uint m_usecPerQuarterNote : 24;
local uint m_bpm = 60000000 / m_usecPerQuarterNote;
FSeek(FTell() - 1);
}
else if (m_type == META_SMPTE_OFFSET)
{
char m_hours;
char m_mins;
char m_secs;
char m_fps;
char m_fracFrames;
}
else if (m_type == META_TIME_SIGNATURE)
{
char m_numerator;
char m_denominator;
char m_clocksPerClick;
char m_32ndPer4th;
}
else if (m_type == META_KEY_SIGNATURE)
{
char m_flatsSharps;
char m_majorMinor;
}
else
{
char m_data[m_length.total];
}
} meta_event;
}
else if ((m_status & 0xf0) == 0xF0)
{
struct
{
DeltaTime m_length;
char m_message[m_length.total];
} sysex_event;
}
};
struct MidiTrack
{
char m_magic[4] <format = hex>;
uint m_seclen;
local uint remaining = m_seclen;
while (remaining) {
MidiMessage message;
remaining -= sizeof(message);
}
};
struct
{
MidiHeader header;
MidiTrack tracks[header.m_ntracks] <optimize=false>;
} file;