-
Notifications
You must be signed in to change notification settings - Fork 15
/
mdx_compiler.c
452 lines (399 loc) · 14.8 KB
/
mdx_compiler.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "mdx_compiler.h"
#include "mmlc.yy.h"
#include "midilib/buffer.h"
void mdx_compiler_init(struct mdx_compiler *compiler) {
for(int i = 0; i < 16; i++) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
buffer_init(&chan->buf);
chan->total_ticks = 0;
chan->default_note_length = 48;
chan->octave = 4;
}
memset(compiler->voices, 0, sizeof(compiler->voices));
compiler->title = compiler->pcmfile = 0;
compiler->ex_pcm = 0;
}
void mdx_compiler_destroy(struct mdx_compiler *compiler) {
for(int i = 0; i < 16; i++) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
buffer_destroy(&chan->buf);
}
}
// https://stackoverflow.com/questions/48850242/thread-safe-reentrant-bison-flex
int mdx_compiler_parse(struct mdx_compiler *compiler, FILE *f) {
yylex_init_extra(compiler, &compiler->scanner);
yyset_in(f, compiler->scanner);
yyparse(compiler->scanner, compiler);
yylex_destroy(compiler->scanner);
return 0;
}
void mdx_compiler_dump(struct mdx_compiler *compiler) {
for(int i = 0; i < 16; i++) {
printf("%2d: %5d: ", i, compiler->channels[i].total_ticks);
buffer_dump(&compiler->channels[i].buf);
}
}
void mdx_compiler_save(struct mdx_compiler *compiler, const char *filename) {
FILE *f = fopen(filename, "wb");
char buf[4] = { 0x0d, 0x0a, 0x1a, 0x00 };
if(compiler->title)
fwrite(compiler->title, 1, strlen(compiler->title), f);
fwrite(buf, 1, 3, f);
if(compiler->pcmfile)
fwrite(compiler->pcmfile, 1, strlen(compiler->pcmfile), f);
buf[0] = 0;
fwrite(buf, 1, 1, f);
int total_voices = 0;
for(int i = 0; i < 256; i++) {
if(compiler->voices[i].used) {
total_voices++;
}
}
int total_data_len = 0;
int total_channels = compiler->ex_pcm ? 16 : 9;
for(int i = 0; i < total_channels; i++) {
buffer_write_uint8(&compiler->channels[i].buf, 0xf1);
buffer_write_uint8(&compiler->channels[i].buf, 0x00);
total_data_len += compiler->channels[i].buf.data_len;
}
total_data_len += total_channels * 2 + 2;
buf[0] = total_data_len >> 8;
buf[1] = total_data_len;
fwrite(buf, 1, 2, f);
int o = 2 + total_channels * 2 ;
for(int i = 0; i < total_channels; i++) {
buf[0] = o >> 8;
buf[1] = o;
fwrite(buf, 1, 2, f);
o += compiler->channels[i].buf.data_len;
}
for(int i = 0; i < total_channels; i++) {
fwrite(compiler->channels[i].buf.data, 1, compiler->channels[i].buf.data_len, f);
}
for(int i = 0; i < 256; i++) {
struct mdx_compiler_opm_voice *voice = &compiler->voices[i];
if(!voice->used) continue;
uint8_t buf[3 + 4 * 6];
buf[0] = i;
buf[1] = voice->fl_con;
buf[2] = voice->slot_mask;
uint8_t *b = buf + 3;
int ops[4] = { 0, 2, 1, 3 };
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].dt1_mul;
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].tl;
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].ks_ar;
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].ame_d1r;
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].dt2_d2r;
for(int j = 0; j < 4; j++) *b++ = voice->op[ops[j]].d1l_rr;
fwrite(buf, 1, 3 + 4 * 6, f);
}
fclose(f);
}
void mdx_compiler_directive(struct mdx_compiler *compiler, char *directive_str, char *value) {
if(!strcasecmp(directive_str, "title")) {
compiler->title = value;
} else if(!strcasecmp(directive_str, "pcmfile")) {
compiler->pcmfile = value;
} else if(!strcasecmp(directive_str, "ex-pcm")) {
compiler->ex_pcm = 1;
} else fprintf(stderr, "Unknown directive: %s\n", directive_str);
}
void mdx_compiler_opmdef(struct mdx_compiler *compiler, int voice_num, uint8_t *data) {
if(voice_num < 0) {
fprintf(stderr, "Voice definition number %d cannot be less than 0\n", voice_num);
return;
} else if(voice_num > 255) {
fprintf(stderr, "Voice definition number %d cannot be more than 255\n", voice_num);
return;
}
struct mdx_compiler_opm_voice *voice = &compiler->voices[voice_num];
voice->used = 1;
voice->fl_con = ((data[45] & 0x07) << 3) | (data[44] & 0x07);
voice->slot_mask = data[46] & 0x0f;
for(int i = 0; i < 4; i++) {
struct mdx_compiler_opm_voice_op *op = &voice->op[i];
op->dt1_mul = (data[i * 11 + 7] & 0x0f) | ((data[i * 11 + 8] & 0x07) << 4);
op->tl = data[i * 11 + 5] & 0x7f;
op->ks_ar = (data[i * 11] & 0x1f) | ((data[i * 11 + 6] & 0x03) << 6);
op->ame_d1r = ((data[i * 11 + 10] & 0x01) << 7) | (data[i * 11 + 1] & 0x1f);
op->dt2_d2r = ((data[i * 11 + 9] & 0x03) << 6) | (data[i * 11 + 2] & 0x1f);
op->d1l_rr = ((data[i * 11 + 4] & 0x0f) << 4) | (data[i * 11 + 3] & 0x0f);
}
}
void mdx_compiler_write(struct mdx_compiler *compiler, int chan_mask, ...) {
va_list ap;
va_start(ap, chan_mask);
while(1) {
int c = va_arg(ap, int);
if(c < 0) break;
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
buffer_write_uint8(&chan->buf, c);
}
}
}
va_end(ap);
}
void mdx_compiler_write16(struct mdx_compiler *compiler, int chan_mask, ...) {
va_list ap;
va_start(ap, chan_mask);
while(1) {
int c = va_arg(ap, int);
if(c < 0) break;
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
buffer_write_big_uint16(&chan->buf, c);
}
}
}
va_end(ap);
}
static int mdx_compiler_calc_notelength(struct mml_notelength *l, int def) {
if(!l) return def;
switch(l->type) {
case NoteLenTicks:
return l->val;
case NoteLenDot:
return mdx_compiler_calc_notelength(l->n1, def) * 3 / 2;
case NoteLenInt:
return 192 / l->val;
case NoteLenDefault:
return def;
case NoteLenJoin:
return mdx_compiler_calc_notelength(l->n1, def) + mdx_compiler_calc_notelength(l->n2, def);
}
free(l);
return 48;
}
static uint8_t mdx_compiler_note_value(int octave, int note) {
// d+ is 0 a b c d e f g
int note_table[] = { 6, 8, -3, -1, 1, 2, 4 };
int n = note & 0xff;
if(n < 0) n = 0;
if(n > 6) n = 6;
int val = octave * 12 + note_table[n] + ((note & MML_NOTE_SHARP) ? 1 : ((note & MML_NOTE_FLAT) ? -1 : 0));
if(val < 0) return 0xff;
if(val > 95) return 0xff;
return val;
}
static int mdx_compiler_tempo_to_opm(int tempo) {
return 256 - (78125 / (16 * tempo));
}
void mdx_compiler_tempo(struct mdx_compiler *compiler, int chan_mask, int tempo, int at) {
if(!at && tempo < 19) {
fprintf(stderr, "Tempo %d cannot be less than 19\n", tempo);
return;
} else if(!at && tempo > 4882) {
fprintf(stderr, "Tempo %d cannot be more than 4882\n", tempo);
return;
}
mdx_compiler_write(compiler, chan_mask, 0xff, at ? tempo : mdx_compiler_tempo_to_opm(tempo), -1);
}
void mdx_compiler_note(struct mdx_compiler *compiler, int chan_mask, int note, struct mml_notelength *l) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
int note_value = mdx_compiler_note_value(chan->octave, note);
if(note_value == 0xff) {
fprintf(stderr, "Invalid note value o%d%c%c\n", chan->octave, 'a' + (note & 0xff), ((note & MML_NOTE_SHARP) ? '+' : (note & MML_NOTE_FLAT) ? '-' : ' '));
continue;
}
buffer_write_uint8(&chan->buf, 0x80 + note_value);
int ticks = mdx_compiler_calc_notelength(l, chan->default_note_length);
buffer_write_uint8(&chan->buf, ticks - 1);
chan->total_ticks += ticks;
}
}
}
void mdx_compiler_rest(struct mdx_compiler *compiler, int chan_mask, struct mml_notelength *l) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
int ticks = mdx_compiler_calc_notelength(l, chan->default_note_length);
buffer_write_uint8(&chan->buf, ticks-1);
chan->total_ticks += ticks;
}
}
}
void mdx_compiler_set_default_note_length(struct mdx_compiler *compiler, int chan_mask, struct mml_notelength *l) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
int ticks = mdx_compiler_calc_notelength(l, chan->default_note_length);
chan->default_note_length = ticks;
}
}
}
void mdx_compiler_octave(struct mdx_compiler *compiler, int chan_mask, int octave) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
chan->octave = octave;
}
}
}
void mdx_compiler_octave_down(struct mdx_compiler *compiler, int chan_mask) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
chan->octave--;
if(chan->octave < 0) chan->octave = 0;
}
}
}
void mdx_compiler_octave_up(struct mdx_compiler *compiler, int chan_mask) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
chan->octave++;
if(chan->octave > 8) chan->octave = 8;
}
}
}
void mdx_compiler_note_num(struct mdx_compiler *compiler, int chan_mask, int note, struct mml_notelength *l) {
if(note < 0) {
fprintf(stderr, "Invalid negative note %d\n", note);
return;
} else if(note > 95) {
fprintf(stderr, "Invalid note %d > 95\n", note);
return;
}
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
buffer_write_uint8(&chan->buf, 0x80 + note);
int ticks = mdx_compiler_calc_notelength(l, chan->default_note_length);
buffer_write_uint8(&chan->buf, ticks-1);
chan->total_ticks += ticks;
}
}
}
void mdx_compiler_staccato(struct mdx_compiler *compiler, int chan_mask, int q, int at) {
mdx_compiler_write(compiler, chan_mask, 0xf8, at ? 256 - q : q, -1);
}
void mdx_compiler_portamento(struct mdx_compiler *compiler, int chan_mask, int note, struct mml_notelength *l, int note2) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
int ticks = mdx_compiler_calc_notelength(l, chan->default_note_length);
int note_value_1 = mdx_compiler_note_value(chan->octave, note);
if(note_value_1 == 0xff) {
fprintf(stderr, "Invalid note value o%d%c%c\n", chan->octave, 'a' + (note & 0xff), ((note & MML_NOTE_SHARP) ? '+' : (note & MML_NOTE_FLAT) ? '-' : ' '));
continue;
}
int note_value_2 = mdx_compiler_note_value(chan->octave, note2);
if(note_value_2 == 0xff) {
fprintf(stderr, "Invalid note value o%d%c%c\n", chan->octave, 'a' + (note2 & 0xff), ((note2 & MML_NOTE_SHARP) ? '+' : (note2 & MML_NOTE_FLAT) ? '-' : ' '));
continue;
}
int portamento = 16384 * (note_value_2 - note_value_1) / (ticks);
buffer_write_uint8(&chan->buf, 0xf2);
buffer_write_big_int16(&chan->buf, portamento);
buffer_write_uint8(&chan->buf, 0x80 + note_value_1);
buffer_write_uint8(&chan->buf, ticks-1);
}
}
}
void mdx_compiler_legato(struct mdx_compiler *compiler, int chan_mask) {
for(int i = 0, m = 1; i < 16; i++, m <<= 1) {
if(chan_mask & m) {
struct mdx_compiler_channel *chan = &compiler->channels[i];
if(chan->buf.data_len > 2) {
buffer_write_uint8(&chan->buf, chan->buf.data[chan->buf.data_len - 1]);
chan->buf.data[chan->buf.data_len - 2] = chan->buf.data[chan->buf.data_len - 3];
chan->buf.data[chan->buf.data_len - 3] = 0xf7;
}
}
}
}
void mdx_compiler_volume(struct mdx_compiler *compiler, int chan_mask, int v, int at) {
mdx_compiler_write(compiler, chan_mask, 0xfb, at ? 128 + v : v, -1);
}
void mdx_compiler_set_voice(struct mdx_compiler *compiler, int chan_mask, int voice) {
if(voice > 255)
fprintf(stderr, "Voice %d cannot be greater than 255\n", voice);
mdx_compiler_write(compiler, chan_mask, 0xfd, voice, -1);
}
void mdx_compiler_end(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xf1, 0x00, -1);
}
void mdx_compiler_volume_down(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xfa, -1);
}
void mdx_compiler_volume_up(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xf9, -1);
}
void mdx_compiler_pan(struct mdx_compiler *compiler, int chan_mask, int p) {
mdx_compiler_write(compiler, chan_mask, 0xfc, p & 3, -1);
}
void mdx_compiler_key_on_delay(struct mdx_compiler *compiler, int chan_mask, int k) {
mdx_compiler_write(compiler, chan_mask, 0xf0, k & 0xff, -1);
}
void mdx_compiler_loop_start(struct mdx_compiler *compiler, int chan_mask) {
}
void mdx_compiler_detune(struct mdx_compiler *compiler, int chan_mask, int d) {
mdx_compiler_write(compiler, chan_mask, 0xf3, d, -1);
}
void mdx_compiler_opm_noise_freq(struct mdx_compiler *compiler, int chan_mask, int w) {
mdx_compiler_write(compiler, chan_mask, 0xed, 0x80 + w, -1);
}
void mdx_compiler_opm_write(struct mdx_compiler *compiler, int chan_mask, int r, int d) {
mdx_compiler_write(compiler, chan_mask, 0xfe, r & 0xff, d & 0xff, -1);
}
void mdx_compiler_sync_send(struct mdx_compiler *compiler, int chan_mask, int c) {
mdx_compiler_write(compiler, chan_mask, 0xef, c, -1);
}
void mdx_compiler_sync_wait(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xee, -1);
}
void mdx_compiler_adpcm_freq(struct mdx_compiler *compiler, int chan_mask, int f) {
mdx_compiler_write(compiler, chan_mask, 0xed, f & 0x07, -1);
}
void mdx_compiler_mh(struct mdx_compiler *compiler, int chan_mask, int lfo, int lfrq, int pmd, int amd, int pms, int ams, int sync) {
mdx_compiler_write(
compiler,
chan_mask,
0xea,
(lfo & 0x03) | ((sync & 0x01) << 6),
lfrq,
(pmd & 0x7f) | 0x80,
amd,
((pms & 0x0f) << 4) | (ams & 0x0f),
-1
);
}
void mdx_compiler_mhon(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xea, 0x81, -1);
}
void mdx_compiler_mhof(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xea, 0x80, -1);
}
void mdx_compiler_ma(struct mdx_compiler *compiler, int chan_mask, int waveform, int freq, int amplitude) {
mdx_compiler_write(compiler, chan_mask, 0xeb, waveform & 0x03, -1);
mdx_compiler_write16(compiler, chan_mask, freq * 2, amplitude * 32, -1);
}
void mdx_compiler_maon(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xeb, 0x81, -1);
}
void mdx_compiler_maof(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xeb, 0x80, -1);
}
void mdx_compiler_mp(struct mdx_compiler *compiler, int chan_mask, int waveform, int freq, int amplitude) {
mdx_compiler_write(compiler, chan_mask, 0xec, waveform & 0x03, -1);
mdx_compiler_write16(compiler, chan_mask, freq * 2, amplitude * 128, -1);
}
void mdx_compiler_mpon(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xec, 0x81, -1);
}
void mdx_compiler_mpof(struct mdx_compiler *compiler, int chan_mask) {
mdx_compiler_write(compiler, chan_mask, 0xec, 0x80, -1);
}
void mdx_compiler_md(struct mdx_compiler *compiler, int chan_mask, int ticks) {
mdx_compiler_write(compiler, chan_mask, 0xe9, ticks, -1);
}