-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_midi.c
320 lines (274 loc) · 7.8 KB
/
parse_midi.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Functions responsible for taking a MIDI file as an input and producing the
corresponding PIANOREADER specific format that encodes lights with timing
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "midi.h"
////////////////////////////////////////////////////////////////////////////
/////////////// Error Defines //////////////////////////
////////////////////////////////////////////////////////////////////////////
#define FILE_NOT_FOUND -1
#define BAD_HEADER -2
#define BAD_READ -3
////////////////////////////////////////////////////////////////////////////
/////////////// Standard Defines /////////////////////////
////////////////////////////////////////////////////////////////////////////
#define MAX_MIDI_SIZE 322146
#define MAX_SONG_NAME 256
#define HEADER_ASCII 0x4D546864
#define TRACK_ASCII 0x4D54726B
////////////////////////////////////////////////////////////////////////////
/////////////// Misc. Defines /////////////////////////
////////////////////////////////////////////////////////////////////////////
#define FILE_NAME "/home/pandaman/Downloads/for_elise_by_beethoven.mid"
////////////////////////////////////////////////////////////////////////////
/////////////// Struct Defs /////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t file_len;
float ticks_per_qn = 0;
char song_name[MAX_SONG_NAME];
char midi_buffered[MAX_MIDI_SIZE];
uint64_t run_time=0;
note hands[4096];
uint32_t cnt_on = 0;
uint32_t cnt_off = 0;
int main(){
FILE* midi_handle;
// Open file in read mode
midi_handle=fopen(FILE_NAME, "rb");
if(midi_handle){
uint32_t header_word, len;
uint32_t time_scale;
// Read entire file into a buffer for parsing and null terminate it
file_len = fread(midi_buffered, sizeof(char), MAX_MIDI_SIZE, midi_handle);
if (ferror(midi_handle) != 0){
fprintf(stderr, "Error reading %s", FILE_NAME);
return BAD_READ;
}
else{
midi_buffered[file_len++] = '\0';
}
// Parse through the buffered file and then do cleanup
parse_midi_buffer();
fclose(midi_handle);
//print_buffer_string(midi_buffered, file_len);
}
else{
fprintf(stderr, "Error opening midi file!\n");
return FILE_NOT_FOUND;
}
int i;
printf("Cnt on %d\n", cnt_on);
printf("Cnt off %d\n", cnt_off);
for(i=0; i < (cnt_on + cnt_off - 2); i++){
printf("N: %s, T: %f, D: %d\n", key_decode[(hands[i]).key], hands[i].time, hands[i].dir);
}
}
void print_buffer_string(char* str, uint32_t len){
for(int i=0; i<len; i++){
fprintf(stderr, "%c", str[i]);
}
}
// Function for getting 32 bits of data from a buffered midi file
// the offset into the buffer is updated and the word is returned
uint32_t get_word(uint32_t* offset){
uint32_t word = ((uint8_t) midi_buffered[(*offset)] << 24) | ((uint8_t) midi_buffered[(*offset)+1] << 16) | ((uint8_t) midi_buffered[(*offset)+2] << 8) | ((uint8_t) midi_buffered[(*offset)+3]);
(*offset) += 4;
return word;
}
uint16_t get_half_word(uint32_t* offset){
uint16_t half = ((uint8_t) midi_buffered[(*offset)] << 8) | ((uint8_t) midi_buffered[(*offset)+1]);
(*offset) += 2;
return half;
}
// Reads the variable length format designated by the midi specification
// returns the corresponding value and then updates the offset
uint64_t get_event_length(uint32_t* file_offset){
bool length_parsing = true;
uint64_t event_length = 0;
while(length_parsing){
uint32_t variable_len = midi_buffered[*file_offset];
if (variable_len <= 0x80){
length_parsing = false;
}
event_length = (event_length << 7) | (variable_len & 0x7f);
(*file_offset)++;
}
return event_length;
}
uint8_t parse_midi_buffer(){
int offset = 0;
header hdr;
// Get Midi File Header
uint32_t first_word = get_word(&offset);
if (first_word != HEADER_ASCII){
return BAD_HEADER;
}
uint32_t header_len = get_word(&offset);
if (header_len != 6){
return BAD_HEADER;
}
hdr.format = get_half_word(&offset);
hdr.tracks = get_half_word(&offset);
hdr.div = get_half_word(&offset);
if (!(hdr.div & 0x8000)){
ticks_per_qn = hdr.div & 0x7FFF;
}
track tracks[16] = {0};
uint8_t find_pos = 1;
uint32_t tmp_offset = offset;
uint32_t data = 0;
int index = 0;
printf("file len %d\n", file_len);
while (find_pos && (tmp_offset < file_len)){
uint8_t ind = midi_buffered[tmp_offset];
if (ind == 0x4d){
data = get_word(&tmp_offset);
if (data == TRACK_ASCII){
tracks[index].pos = tmp_offset;
tracks[index].len = get_word(&tmp_offset);
index++;
if (index == hdr.tracks){
find_pos = 0;
}
}
}
else{
tmp_offset++;
}
}
int event_length = 0;
for (int i=0; i < hdr.tracks; i++){
printf("Parsing Track %d, len %d\n", i, tracks[i].len);
tmp_offset = tracks[i].pos + 4;
while(tmp_offset < (tracks[i].len + tracks[i].pos)){
run_time += get_event_length(&tmp_offset);
uint8_t dat = midi_buffered[tmp_offset];
tmp_offset++;
if (dat == 0xFF){
parse_meta_event(&tmp_offset);
}
else if (dat == 0xF0 || dat == 0xF7){
parse_sys_event(&tmp_offset);
}
else{
parse_midi_event(&tmp_offset, dat);
}
}
}
}
void parse_meta_event(uint32_t* offset){
uint8_t type = midi_buffered[*offset];
int len = 0;
(*offset)++;
switch (type) {
case 0x00:
(*offset)++;
len = 2;
break;
case 0x01:
len = get_event_length(offset);
break;
case 0x02:
len = get_event_length(offset);
break;
case 0x03:
len = get_event_length(offset);
memcpy(song_name, (midi_buffered + (*offset)), len);
printf("Title %s\n", song_name);
break;
case 0x04:
len = get_event_length(offset);
break;
case 0x05:
len = get_event_length(offset);
break;
case 0x06:
len = get_event_length(offset);
break;
case 0x07:
len = get_event_length(offset);
break;
case 0x20:
(*offset)++;
len = 0x01;
break;
case 0x2F:
(*offset)++;
len = 0x00;
break;
case 0x51:
(*offset)++;
len = 0x03;
break;
case 0x54:
(*offset)++;
len = 0x05;
break;
case 0x58:
(*offset)++;
len = 0x04;
break;
case 0x59:
(*offset)++;
len = 0x02;
break;
case 0x7F:
len = get_event_length(offset);
break;
}
(*offset) += len;
}
void parse_sys_event(uint32_t* offset){
int len = get_event_length(offset);
(*offset) += len;
}
void parse_midi_event(uint32_t* offset, uint8_t type){
int len = 0;
static int temp_time = 0;
if (!temp_time) {
temp_time = run_time;
}
int dif = temp_time - run_time;
temp_time = run_time;
switch (type & 0xF0){
case 0x80:
len = 2;
int key_off = midi_buffered[*offset];
hands[cnt_on + cnt_off].time = dif/ticks_per_qn;
hands[cnt_on + cnt_off].dir = 0;
hands[cnt_on + cnt_off].key = key_off;
hands[cnt_on + cnt_off].hand = key_off > 60 ? 1 : 2;
cnt_off++;
break;
case 0x90:
len = 2;
int key = midi_buffered[*offset];
hands[cnt_on + cnt_off].time = dif/ticks_per_qn;
hands[cnt_on + cnt_off].dir = 1;
hands[cnt_on + cnt_off].key = key;
hands[cnt_on + cnt_off].hand = key > 60 ? 1 : 2;
cnt_on++;
break;
case 0xA0:
len = 2;
break;
case 0xB0:
len = 2;
break;
case 0xC0:
len = 1;
break;
case 0xD0:
len = 1;
break;
case 0xE0:
len = 2;
break;
}
(*offset) += len;
}