-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart.cpp
More file actions
309 lines (275 loc) · 8.08 KB
/
Copy pathpart.cpp
File metadata and controls
309 lines (275 loc) · 8.08 KB
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
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cmath>
#include "part.h"
#include "codegen.h"
char mml_ctx::getchar()
{
char c;
while(true) {
if(pos < line.size() && (c = line[pos++]) != '\0')
return c;
if(macro_stack.empty())
return 0;
pop_macro();
}
}
int mml_ctx::read_number(int& out, int& is_relative)
{
int sign = 1;
is_relative = 0;
if(peek() == '+' || peek() == '-') {
if(peek() == '-')
sign = -1;
is_relative = 1;
getchar();
}
if(!isdigit(peek())) {
return 0; // no number found
}
int value = 0;
while(isdigit(peek())) {
value = value * 10 + (getchar() - '0');
}
out = value * sign;
while(isspace(peek()))
getchar(); // skip whitespace
return 1;
}
int mml_ctx::read_numbers(int* out, int count)
{
int rel = 0, nums_read = 0;
for(int i = 0; i < count; i++) {
if(!read_number(out[i], rel)) {
return nums_read; // failed to read number
}
nums_read++;
if(peek() == ',' && i < count - 1)
getchar(); // skip comma
while(isspace(peek()))
getchar(); // skip whitespace
}
return nums_read;
}
int convert_length_to_clock(int len)
{
// assuming 4/4 time signature and 24 clocks per quarter note
if(len <= 0)
{
printf("warning: length too short (%d), setting to 1\n", len);
return 1;
}
if(len > TIMEBASE) {
printf("warning: length too long (%d), limiting to 96\n", len);
len = TIMEBASE;
}
return TIMEBASE / len; // 96 = 24 * 4
}
int mml_ctx::read_length(int& out, int& is_relative)
{
if(peek() == '%') {
// direct clock number
getchar(); // consume '%'
return read_number(out, is_relative);
} else {
int total = 0;
while(true) {
int value = 0;
if(isdigit(peek())) {
while(isdigit(peek())) {
value = value * 10 + (getchar() - '0');
}
value = convert_length_to_clock(value);
} else {
value = out;
}
if(peek() == '.') {
// dot length
int x = value;
do {
value += (x >>= 1);
getchar();
}while(peek() == '.');
}
total += value;
if(peek() == '^') {
getchar();
continue;
}
break;
}
out = total;
is_relative = 0;
while(isspace(peek()))
getchar(); // skip whitespace
return 1;
}
}
void mml_ctx::gen_note(mml_part &mp, int code, part_buffer &pb)
{
int len = mp.len + mp.len1, rel;
if(int ret = read_length(len, rel); ret == 0)
len = mp.len + mp.len1;
if(peek() == '&') {
getchar();
if(mp.xxleg < 2) mp.xxleg = 2; // tie
}
if(mp.porsw == 2) {
int x = static_cast<int>(log(fabs(mp.pornote2 - mp.pornote1) / len) / log(2.0));
x <<= 4;
x += 128;
pb.write(CCD_PPA);
pb.write(mp.pornote2 - mp.pornote1);
pb.write(x);
mp.porsw = 0;
pb.write(CCD_PON);
}
if(mp.xxleg) mp.xxleg--;
if(mp.xxleg) {
if(!(mp.leg & 1)) {
pb.write(CCD_LEG);
mp.leg |= 1;
}
} else {
if(mp.leg & 1) {
pb.write(CCD_LOF);
mp.leg &= ~1;
}
}
if ( !mp.porsw ) {
if ( mp.xlen != len ) {
mp.xlen = len;
pb.write_length(len);
}
mp.current_tick() += len;
pb.write((code >= 0 && code < 96) ? 128 + code : CCD_RRR);
} else {
mp.pornote2 = mp.pornote1;
mp.pornote1 = code;
}
}
int mml_ctx::call_macro(mml_part &mp, part_buffer &pb)
{
char c;
char macro_name[MAXMACRONAME];
int i = 0;
while((c = peek()) != '\0' && isalpha(c)) {
macro_name[i++] = c;
getchar();
}
macro_name[i] = '\0';
if(auto it = macro.find(std::string(macro_name)); it != macro.end()) {
if(macro_stack.size() >= MACRONEST) {
printf("Macro stack overflow.\n");
return -1;
}
push_macro();
set_p(const_cast<char *>(it->second.definition.c_str()), it->second.lineno);
} else if(mp.tr_attr & 1) {
if(auto dit = drummacro.find(std::string(macro_name)); dit != drummacro.end()) {
gen_note(mp, dit->second.index, pb);
mp.xnote = dit->second.index;
} else {
printf("Drum macro not found: [%s]\n", macro_name);
}
} else {
printf("Macro isn't found: [%s], note: cannot call drum macro in tracks other than =1'ed\n", macro_name);
}
return 0;
}
void mml_ctx::register_macro(int lineno, const std::string& name, const std::string& definition)
{
if(auto [iter, success] = macro.insert_or_assign(name, macro_item{lineno, definition});
!success) {
printf("Warning: macro redefined! [%s] in line %d\n", name.c_str(), lineno);
}
}
void mml_ctx::register_drummacro(int lineno, const std::string& name, const std::string& definition)
{
if(drummacro_count >= MAXDRUMMACRO) {
printf("Too many drum macros (max %d).\n", MAXDRUMMACRO);
return;
}
if(auto [iter, success] = drummacro.insert_or_assign(
name, drummacro_item{drummacro_count, lineno, definition});
!success) {
printf("Warning: drum macro redefined! [%s] in line %d\n", name.c_str(), lineno);
}
drummacro_count++;
}
int mml_ctx::parse_partline(std::string_view part_token, int lineno, std::string_view line, codegen& cg)
{
char part_name[2];
for(char pt : part_token) {
if(0 && strchr(part_token.data(), pt) != &pt) {
printf("duplicate part definition: [%c][%s]\n", pt, part_token.data());
return -1;
}
char part = pt;
partflags |= 1 << (part - 'A');
part_name[0] = part;
part_name[1] = '\0';
// printf("\tParsing for part [%c][%s]\n", part, line.data());
// set part context to p
mml_part& mp = parts[part - 'A'];
part_buffer& pb = cg.get_part(part - 'A');
set_p(line, lineno);
parse_partdef(part_name, mp, pb);
pb.length_written = mp.current_tick();
}
return 0;
}
int mml_ctx::parse_wildcardline(int lineno, std::string_view line, codegen& cg)
{
char part_name[32 + 1];
uint32_t pf = partflags;
char* pt = part_name;
for (int part = 0; part < MAXPART; part++) {
if (pf & 1) {
*pt++ = 'A' + part;
}
pf >>= 1;
}
*pt = '\0';
// printf("\tWILDCARD: Parsing for part [%s][%s]\n", part_name, line.data());
return parse_partline(part_name, lineno, line, cg);
}
int mml_ctx::parse_drumline(codegen& cg)
{
char drum_token[MAXMACRONAME];
std::vector<part_buffer> drum_buffers;
drum_buffers.resize(drummacro_count);
for(auto &[name, item] : drummacro) {
const std::string& definition = item.definition;
mml_part dummy_part; // dummy part context for drum pattern
part_buffer& pb = drum_buffers[item.index];
set_p(const_cast<char *>(definition.c_str()), item.lineno);
snprintf(drum_token, sizeof drum_token, "!!%s", name.c_str());
parse_partdef(drum_token, dummy_part, pb);
pb.write(CCD_END);
pb.size = pb.pos;
pb.length_written = dummy_part.current_tick();
}
cg.set_drum_patterns(std::move(drum_buffers));
return 0;
}
int mml_ctx::end_mml(codegen& cg)
{
parse_drumline(cg);
return 0;
}
void mml_ctx::report_macro(FILE *fp)
{
fprintf(fp, "MACRO definitions:\n");
for(auto& [key, value]: macro) {
fprintf(fp, "\tline %4d: !%s = [%s]\n", value.lineno, key.c_str(), value.definition.c_str());
}
}
void mml_ctx::report_drummacro(FILE *fp)
{
fprintf(fp, "DRUMMACRO definitions:\n");
for(auto& [key, value]: drummacro) {
fprintf(fp, "\tline %4d: !!%s = (%2d)[%s]\n", value.lineno, key.c_str(), value.index, value.definition.c_str());
}
}