-
Notifications
You must be signed in to change notification settings - Fork 9
/
mididump.c
466 lines (394 loc) · 10 KB
/
mididump.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Audio Overload SDK
* MIDI dumping
*
* Author: Nmlgc
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ao.h"
#include "corlett.h"
#include "mididump.h"
#include "utils.h"
ao_bool nomidi = false;
#define MSB(ctl) ctl
#define LSB(ctl) ctl+0x20
/// MIDI events
/// -----------
typedef enum {
NOTE_OFF = 0x8,
NOTE_ON = 0x9,
NOTE_AFTERTOUCH = 0xA,
CONTROLLER = 0xB,
PROGRAM_CHANGE = 0xC,
CHANNEL_AFTERTOUCH = 0xD,
PITCH_BEND = 0xE,
} event_type_t;
typedef enum {
META_TRACK_NAME = 0x3,
META_INSTRUMENT_NAME = 0x4,
META_TRACK_END = 0x2F,
META_TEMPO = 0x51,
} meta_event_type_t;
typedef union {
int8 byte[2];
int16 word;
} event_param_t;
typedef struct event {
unsigned long time;
event_type_t type;
event_param_t param;
struct event *next;
} event_t;
/// -----------
/// Virtual channels
/// ----------------
typedef struct {
int id;
int8 cur_ctl_vals[128];
event_t *first;
event_t *last;
} vchan_t;
int64 first_note_distance = -1;
static void vchan_event_push(
vchan_t *vchan, event_type_t type, event_param_t param
)
{
event_t *event_out = malloc(sizeof(event_t));
assert(vchan);
if(vchan->last) {
vchan->last->next = event_out;
}
if(!vchan->first) {
vchan->first = event_out;
}
vchan->last = event_out;
if(first_note_distance == -1) {
event_out->time = 0;
if(type == NOTE_ON) {
first_note_distance = corlett_sample_count() / 2;
}
} else {
event_out->time = (corlett_sample_count() / 2) - first_note_distance;
}
event_out->type = type;
event_out->param.word = param.word;
event_out->next = NULL;
}
static void vchan_ctl7_push(vchan_t *vchan, int8 ctl7, int8 val)
{
assert(vchan);
assert(ctl7 >= 0 && ctl7 <= 127);
assert(val >= 0);
if(vchan->cur_ctl_vals[ctl7] != val) {
event_param_t param = {ctl7, val};
vchan_event_push(vchan, CONTROLLER, param);
vchan->cur_ctl_vals[ctl7] = val;
}
}
static int vchan_compare(const vchan_t **a, const vchan_t **b)
{
return (*a)->id - (*b)->id;
}
static void vchan_free(vchan_t *vchan)
{
event_t *e;
assert(vchan);
e = vchan->first;
while(e) {
event_t *e_cur = e;
e = e->next;
free(e_cur);
};
vchan->first = NULL;
vchan->last = NULL;
}
/// ----------------
/// Virtual channel hash table
/// --------------------------
hashtable_t vchans;
vchan_t* vchans_get(int id)
{
vchan_t *ret;
blob_t id_blob = {&id, sizeof(int)};
hashtable_init(&vchans, sizeof(vchan_t));
ret = hashtable_get(&vchans, &id_blob, HT_CREATE);
if(ret->id == 0) {
memset(ret->cur_ctl_vals, 0xFF, sizeof(ret->cur_ctl_vals));
ret->id = id;
}
return (vchan_t*)ret;
}
vchan_t* vchans_iterate(hashtable_iterator_t *iter)
{
return (vchan_t*)hashtable_iterate(NULL, &vchans, iter);
}
static void vchans_free(void)
{
hashtable_iterator_t iter = {0};
vchan_t *vchan = NULL;
while((vchan = vchans_iterate(&iter))) {
vchan_free(vchan);
}
hashtable_free(&vchans);
}
/// --------------------------
/// BPM recognition
/// ---------------
static double samples_to_bpm(double samples)
{
return (60.0 / samples) * 22050.0;
}
#define bpm_to_samples(bpm) ((uint32)samples_to_bpm(bpm))
// Analyzes all virtual channels and returns the most common note distance in
// samples that would imply a tempo between 40.37 and 300 BPM.
static uint16 mididump_beat_distance_find(void)
{
const uint16 DISTANCE_MIN = bpm_to_samples(300) + 2; // = 4412
const uint16 DISTANCE_MAX = 0x7FFF; // MIDI limitation
uint32 occurrences[(0x7FFF - 4412)] = {0}; // I hate C
double bpm = 0;
uint32 max_occurrences = 0;
uint16 beat_distance = 0;
uint32 i = 0;
hashtable_iterator_t iter = {0};
vchan_t *vchan = vchans_iterate(&iter);
if(!vchan) {
return false;
}
do {
uint32 time_prev;
const event_t *event = vchan->first;
while(event && event->type != NOTE_ON) {
event = event->next;
}
if(!event) {
continue;
}
time_prev = event->time;
// Intentionally skip the first element
while((event = event->next)) {
uint32 dist;
if(event->type != NOTE_ON) {
continue;
}
dist = event->time - time_prev;
if(dist > DISTANCE_MIN && dist < DISTANCE_MAX) {
occurrences[dist - DISTANCE_MIN]++;
}
time_prev = event->time;
}
} while((vchan = vchans_iterate(&iter)));
memset(&iter, 0, sizeof(iter));
for(i = 0; i < DISTANCE_MAX - DISTANCE_MIN; i++) {
uint32 dist = i + DISTANCE_MIN;
if(occurrences[i] > max_occurrences) {
max_occurrences = occurrences[i];
beat_distance = dist;
}
}
return beat_distance;
}
/// ---------------
/// MIDI output
/// -----------
static FILE* midi_open(const char *fn)
{
return fopen_derivative(fn, ".mid");
}
static void midi_varlen_write(FILE *midi, uint32 val)
{
long buffer = val & 0x7f;
while((val >>= 7) > 0) {
buffer <<= 8;
buffer |= 0x80;
buffer += (val & 0x7f);
}
while(1) {
putc(buffer, midi);
if(buffer & 0x80) {
buffer >>= 8;
} else {
break;
}
}
};
// Meta events
// -----------
const unsigned char MIDI_META_EVENT = 0xFF;
static void midi_meta_track_end_write(FILE *midi)
{
const char type = META_TRACK_END;
const char val = 0;
midi_varlen_write(midi, 0); // delta time
fwrite(&MIDI_META_EVENT, 1, 1, midi);
fwrite(&type, 1, 1, midi);
fwrite(&val, 1, 1, midi);
}
// -----------
// Track macros
// ------------
static long midi_track_begin(FILE *midi)
{
const char *MTrk = "MTrk";
long track_start = 0;
fwrite(MTrk, 4, 1, midi);
fwrite(&track_start, 4, 1, midi);
return ftell(midi);
}
static void midi_track_end(FILE* midi, long track_start)
{
long track_length;
long track_end;
midi_meta_track_end_write(midi);
// Fix up length
track_end = ftell(midi);
fseek(midi, track_start - 4, SEEK_SET);
track_length = BE32(track_end - track_start);
fwrite(&track_length, 4, 1, midi);
fseek(midi, track_end, SEEK_SET);
}
// ------------
static void midi_header_write(FILE *midi, uint16 beat_distance)
{
const char *MThd = "MThd";
const uint32 chunk_size = BE32(6);
const uint16 format = BE16(1);
const uint16 track_count = BE16(hashtable_length(&vchans) + 1);
const uint16 time_division = BE16(beat_distance);
// Sequence name
// TODO: Should write all original song tags, somehow
const char sequence_name_type = META_TRACK_NAME;
const char *sequence_name = "Generated by aosdk";
size_t sequence_name_len = strlen(sequence_name);
// Tempo
const char tempo_type = META_TEMPO;
const char tempo_len = 3;
const uint32 tempo = SWAP24(60000000.0 / samples_to_bpm(beat_distance));
long track_start;
fwrite(MThd, 4, 1, midi);
fwrite(&chunk_size, 4, 1, midi);
fwrite(&format, 2, 1, midi);
fwrite(&track_count, 2, 1, midi);
fwrite(&time_division, 2, 1, midi);
track_start = midi_track_begin(midi);
midi_varlen_write(midi, 0); // delta time
fwrite(&MIDI_META_EVENT, 1, 1, midi);
fwrite(&sequence_name_type, 1, 1, midi);
fwrite(&sequence_name_len, 1, 1, midi);
fwrite(sequence_name, sequence_name_len, 1, midi);
midi_varlen_write(midi, 0); // delta time
fwrite(&MIDI_META_EVENT, 1, 1, midi);
fwrite(&tempo_type, 1, 1, midi);
fwrite(&tempo_len, 1, 1, midi);
fwrite(&tempo, tempo_len, 1, midi);
midi_track_end(midi, track_start);
}
static void midi_track_write(FILE *midi, const vchan_t *vchan, char midi_channel)
{
const event_t *event;
uint32 time_prev = 0;
long track_start = midi_track_begin(midi);
// Instrument name
const char type = META_TRACK_NAME;
char track_name[2 + 8 + 1];
char track_name_len = sprintf(track_name, "0x%x", vchan->id);
midi_varlen_write(midi, 0); // delta time
fwrite(&MIDI_META_EVENT, 1, 1, midi);
fwrite(&type, 1, 1, midi);
fwrite(&track_name_len, 1, 1, midi);
fwrite(&track_name, track_name_len, 1, midi);
// Event data
event = vchan->first;
while(event) {
uint32 time_delta = event->time - time_prev;
unsigned char type_and_channel =
((event->type & 0xF) << 4) | (midi_channel & 0xF);
midi_varlen_write(midi, time_delta);
fwrite(&type_and_channel, 1, 1, midi);
fwrite(&event->param.word, 2, 1, midi);
time_prev = event->time;
event = event->next;
}
midi_track_end(midi, track_start);
}
/// -----------
/// Public API
/// ----------
void mididump_vchan_note_on(int vchan_id, char note, char velocity)
{
event_param_t param = {note, velocity};
vchan_event_push(vchans_get(vchan_id), NOTE_ON, param);
}
void mididump_vchan_note_off(int vchan_id, char note, char velocity)
{
event_param_t param = {note, velocity};
vchan_event_push(vchans_get(vchan_id), NOTE_OFF, param);
}
void mididump_vchan_ctl7_set(int vchan_id, int8 ctl7, int8 val)
{
vchan_ctl7_push(vchans_get(vchan_id), ctl7, val);
}
void mididump_vchan_ctl14_set(int vchan_id, int8 ctl14, int16 val)
{
vchan_t *vchan = vchans_get(vchan_id);
assert(ctl14 >= 0 && ctl14 <= 0x1F);
assert(val >= 0 && val <= 0x3FFF);
vchan_ctl7_push(vchan, MSB(ctl14), (val >> 7) & 0x7F);
vchan_ctl7_push(vchan, LSB(ctl14), (val >> 0) & 0x7F);
}
ao_bool mididump_write(const char *fn)
{
uint16 beat_distance;
hashtable_iterator_t iter = {0};
vchan_t *vchan = vchans_iterate(&iter);
FILE *midi;
char midi_channel = 0;
int i = 0;
int vchans_len = hashtable_length(&vchans);
vchan_t **vchans_sorted = NULL;
if(!vchan) {
return false;
}
printf("Writing MIDI data... ");
vchans_sorted = malloc(vchans_len * sizeof(*vchans_sorted));
do {
vchans_sorted[i++] = vchan;
} while( (vchan = vchans_iterate(&iter)) );
qsort(
vchans_sorted, i, sizeof(*vchans_sorted),
(int (*)(const void *, const void *))vchan_compare
);
printf("Analyzing BPM... ");
beat_distance = mididump_beat_distance_find();
if(beat_distance == 0) {
beat_distance = 22050;
printf(
"(piece too slow, falling back to %.2f BPM)\n",
samples_to_bpm(beat_distance)
);
} else {
printf("%.2f\n", samples_to_bpm(beat_distance));
}
midi = midi_open(fn);
midi_header_write(midi, beat_distance);
for(i = 0; i < vchans_len; i++) {
midi_track_write(midi, vchans_sorted[i], midi_channel++);
// Mistakenly allocating a melody track to the drum channel is
// way more more annoying than mistakely playing back percussion
// with the Grand Piano, so...
if((midi_channel & 0xF) == 9) {
midi_channel++;
}
}
fclose(midi);
free(vchans_sorted);
return true;
}
void mididump_free(void)
{
vchans_free();
}
/// ----------