-
Notifications
You must be signed in to change notification settings - Fork 3
/
mml.c
471 lines (437 loc) · 12.7 KB
/
mml.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
467
468
469
470
471
/*!
* Polyphonic synthesizer for microcontrollers. MML compiler.
* (C) 2021 Luciano Martorella
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "mml.h"
#include "synth.h"
#include "sequencer.h"
#include "debug.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*!
* Not optimized for microcontroller usage.
* Requires dynamic memory allocation support (heap), especially `malloc` and `realloc`.
*/
/*! Manage parser errors */
static void (*error_handler)(const char* err, int line, int column);
static int line = 1;
static int pos = 1;
#define ARTICULATION_STACCATO (3.0 / 4.0)
#define ARTICULATION_NORMAL (7.0 / 8.0)
#define ARTICULATION_LEGATO (1.0)
/*! Temporary list of sequencer stream frames, per channel */
static struct seq_frame_map_t frame_map;
static void init_stream_channel(int channel) {
// Init new channels
frame_map.channels[channel].count = 0;
frame_map.channels[channel].frames = malloc(sizeof(struct seq_frame_t) * 16);
}
static void add_channel_frame(int channel, int frequency, int duration, int volume, double articulation, int waveform) {
if (channel >= frame_map.channel_count) {
int old_count = frame_map.channel_count;
frame_map.channel_count = channel + 1;
frame_map.channels = realloc(frame_map.channels, sizeof(struct seq_frame_list_t) * frame_map.channel_count);
for (int i = old_count; i < frame_map.channel_count; i++) {
// Init new channels
init_stream_channel(i);
}
}
if (frame_map.channels[channel].count > 0 && (frame_map.channels[channel].count % 16) == 0) {
frame_map.channels[channel].frames = realloc(frame_map.channels[channel].frames, sizeof(struct seq_frame_t) * (frame_map.channels[channel].count + 16));
}
struct seq_frame_t* p = &frame_map.channels[channel].frames[frame_map.channels[channel].count++];
if (!frequency) {
p->waveform_def.mode = VOICE_MODE_DC;
} else {
p->waveform_def.mode = VOICE_MODE_SQUARE;
p->waveform_def.period = voice_wf_freq_to_period(frequency);
p->waveform_def.amplitude = volume;
p->waveform_def.mode = waveform;
}
// Init voice, simple square without envelope
p->adsr_def.delay_time = 0;
p->adsr_def.attack_time = 12;
p->adsr_def.decay_time = 12;
p->adsr_def.peak_amp = 63;
p->adsr_def.sustain_amp = 40;
// Calc duration and scale: TODO better scale algo
int scale = duration / 128;
p->adsr_def.time_scale = scale;
p->adsr_def.release_time = 128 * (1.0 - articulation);
p->adsr_def.sustain_time = 128 - (p->adsr_def.delay_time + p->adsr_def.attack_time + p->adsr_def.decay_time + p->adsr_def.release_time);
}
void mml_set_error_handler(void (*handler)(const char* err, int line, int column)) {
error_handler = handler;
}
/*! Read a single digit from the stream and advance */
static uint8_t read_digit(const char** str, int* pos) {
const char code = **str;
*str += 1;
*pos += 1;
if (code < '0' || code > '9') {
return 255;
} else {
return code - '0';
}
}
/*! Read a number from the stream and advance */
static int read_number(const char** str, int* pos) {
char* end;
int ret = strtol(*str, &end, 10);
if (!ret || end == *str) {
return -1;
}
*pos += (end - *str);
*str = end;
return ret;
}
/*! Convert a node 0-84 to frequency. 0 is "C" at octave 0, so octave 2 (fourth-octave in scientific pitch) c2 = note 24, and a2 (Helmholtz 440Hz) = note 33 */
static int get_freq_from_code(int noteCode) {
return (int)(440.0 * pow(2, ((noteCode - 33) / 12.0)));
}
/*! Convert a a-g code chromatic scale to frequency. Octave 2 is the fourth-octave in scientific pitch */
static int get_freq_from_note(char note, int sharp, int octave) {
int semitone = ((note - 'a' + 5) % 7) * 2;
if (semitone > 4) {
semitone--;
}
if (sharp) {
semitone++;
}
// semitone is 0 for c
return get_freq_from_code(semitone + octave * 12);
}
/*! Get duration in samples. Tempo is in numbers of quartes per minute. Length is fraction of whole note. Dots are number of dots (1 dot = 3/2, 2 dots = 9/4, etc..) */
static int get_duration(int tempo, int length, int dots) {
double l = length;
for (; dots > 0; dots--) {
l /= 1.5;
}
return (int)(synth_freq * 60.0 * 4 / tempo / l);
}
/*! Parser state, per channel */
struct mml_channel_state_t {
uint8_t octave;
int defaultLength;
int defaultLengthDot;
int tempo;
int volume;
double articulation;
int waveform;
// Active in current MML parsing line
int isActive;
};
static struct mml_channel_state_t* mml_channel_states;
static int mml_channel_count;
static void enable_channel(int channel) {
if (channel >= mml_channel_count) {
mml_channel_count = channel + 1;
mml_channel_states = realloc(mml_channel_states, sizeof(struct mml_channel_state_t) * mml_channel_count);
// Init new channel
mml_channel_states[channel].octave = 4;
mml_channel_states[channel].defaultLength = 4;
mml_channel_states[channel].defaultLengthDot = 0;
mml_channel_states[channel].tempo = 120;
mml_channel_states[channel].volume = 63;
mml_channel_states[channel].articulation = ARTICULATION_NORMAL;
mml_channel_states[channel].waveform = VOICE_MODE_SQUARE;
}
mml_channel_states[channel].isActive = 1;
}
// By default, if no channel identifier at the beginning of a MML line, it is referring to A channel only
static void reset_active_state() {
for (int i = 1; i < mml_channel_count; i++) {
mml_channel_states[i].isActive = 0;
}
enable_channel(0);
}
/*!
* Parse the MML file and produce sequencer stream of frames in `stream_channel` array.
*/
static int mml_parse(const char* content) {
line = 1;
pos = 0;
// Starts with 1 voice
mml_channel_states = malloc(0);
frame_map.channels = malloc(0);
frame_map.channel_count = 0;
// Read the string until end
reset_active_state();
while(1) {
pos++;
char code = content[0];
content++;
if (!code) {
break;
}
if (code <= 32 || code == '|') {
// Skip blanks and partitures
if (code == '\n') {
line++;
reset_active_state();
pos = 0;
}
if (code == '\r') {
pos--;
}
continue;
}
if (code == '#' || code == ';') {
// Skip line comment
while (*content != '\n') {
content++;
}
content++;
line++;
reset_active_state();
pos = 0;
continue;
}
if (code >= 'A' && code <= 'Z') {
if (pos == 1) {
// Decode active channels
mml_channel_states[0].isActive = 0;
enable_channel(code - 'A');
while (*content >= 'A' && *content <= 'Z') {
enable_channel(*content - 'A');
content++;
pos++;
}
continue;
} else {
error_handler("Misplaced channel selector", line, pos);
}
}
int isPause;
int isNoteCode;
if (code == 'o') {
int octave = read_digit(&content, &pos);
if (octave == 255 || octave > 6) {
error_handler("Invalid octave", line, pos);
return 1;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].octave = octave;
}
}
} else if (code == 'l') {
int length = read_number(&content, &pos);
if (length < 0) {
error_handler("Invalid length", line, pos);
return 1;
}
int dot = 0;
while (*content == '.') {
dot++;
content++;
pos++;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].defaultLength = length;
mml_channel_states[i].defaultLengthDot = dot;
}
}
} else if (code == 't') {
int tempo = read_number(&content, &pos);
if (tempo < 0) {
error_handler("Invalid tempo", line, pos);
return 1;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].tempo = tempo;
}
}
} else if (code == 'v') {
int volume = read_number(&content, &pos);
if (volume < 0 || volume > 128) {
error_handler("Invalid volume", line, pos);
return 1;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].volume = volume;
}
}
} else if (code == '<') {
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
if (mml_channel_states[i].octave == 0) {
error_handler("Invalid octave step down", line, pos);
return 1;
}
mml_channel_states[i].octave--;
}
}
} else if (code == '>') {
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
if (mml_channel_states[i].octave == 9) {
error_handler("Invalid octave step up", line, pos);
return 1;
}
mml_channel_states[i].octave++;
}
}
} else if (code == 'm') {
// Music articulation
double articulation;
switch (*content) {
case 'l':
articulation = ARTICULATION_LEGATO;
break;
case 'n':
articulation = ARTICULATION_NORMAL;
break;
case 's':
articulation = ARTICULATION_STACCATO;
break;
default:
error_handler("Invalid music articulation", line, pos);
return 1;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].articulation = articulation;
}
}
pos++;
content++;
} else if (code == 'w') {
// Waveform
int waveform;
switch (*content) {
case 's':
waveform = VOICE_MODE_SQUARE;
break;
case 'w':
waveform = VOICE_MODE_SAWTOOTH;
break;
case 't':
waveform = VOICE_MODE_TRIANGLE;
break;
default:
error_handler("Invalid waveform", line, pos);
return 1;
}
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
mml_channel_states[i].waveform = waveform;
}
}
pos++;
content++;
} else if ((isPause = (code == 'p' || code == 'r')) || (isNoteCode = code == 'n') || (code >= 'a' && code <= 'g')) {
// Note or pause
int length = -1;
int dot = 0;
int sharp = 0;
int customLength = 0;
int noteCode = -1;
while (1) {
char next = content[0];
if (!isPause && !isNoteCode) {
// Sharp/flat?
if (next == '-' || next == '+' || next == '#') {
// variation
if (next == '-') {
code--;
}
if (code == 'e' || code == 'b') {
error_handler("Invalid sharp", line, pos);
return 1;
}
sharp = 1;
content++;
pos++;
continue;
}
}
if (next >= '0' && next <= '9') {
if (isNoteCode) {
if (noteCode != -1) {
error_handler("Invalid note code", line, pos);
return 1;
}
noteCode = read_number(&content, &pos);
if (noteCode < 0 || noteCode > 84) {
error_handler("Invalid note code", line, pos);
return 1;
}
} else {
if (customLength) {
error_handler("Invalid length", line, pos);
return 1;
}
// Length
length = read_number(&content, &pos);
if (length < 0) {
error_handler("Invalid length", line, pos);
return 1;
}
customLength = 1;
}
continue;
}
if (next == '.') {
// Half length
dot++;
content++;
pos++;
continue;
}
break;
}
// Set note
for (int i = 0; i < mml_channel_count; i++) {
if (mml_channel_states[i].isActive) {
if (isNoteCode && noteCode == 0) {
isPause = 1;
}
int frequency = isPause ? 0 : (isNoteCode ? get_freq_from_code(noteCode) : get_freq_from_note(code, sharp, mml_channel_states[i].octave));
int duration = get_duration(mml_channel_states[i].tempo, length < 0 ? mml_channel_states[i].defaultLength : length, (length < 0 && !dot) ? mml_channel_states[i].defaultLengthDot : dot);
add_channel_frame(i, frequency, duration, mml_channel_states[i].volume, mml_channel_states[i].articulation, mml_channel_states[i].waveform);
}
}
} else {
error_handler("Unknown command", line, pos);
return 1;
}
}
free(mml_channel_states);
}
/*!
* Parse the MML file and produce sequencer frames map.
*/
int mml_compile(const char* content, struct seq_frame_map_t* map) {
int ret = mml_parse(content);
if (ret) {
return ret;
}
*map = frame_map;
return 0;
}
void mml_free(struct seq_frame_map_t* map) {
for (int i = 0; i < map->channel_count; i++) {
free(map->channels[i].frames);
}
free(map->channels);
}