forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxml.js
316 lines (286 loc) · 10.7 KB
/
mxml.js
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
// Copyright (c) 2019-20 Marcus Chong
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/* global saveMxmlOutput:writable,voiceNum:writable */
/* exported saveMxmlOutput */
saveMxmlOutput = function (logo) {
// temporary until I get more things sorted out
const ignore = ["voice two", "voice one", "one voice"];
let res = "";
let indent = 0;
const add = function (str) {
for (let i = 0; i < indent; i++) {
res += " ";
}
res += str + "\n";
};
add("<?xml version='1.0' encoding='UTF-8'?>");
add(
'<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">'
);
add('<score-partwise version="3.1">');
indent++;
add("<part-list>");
indent++;
// Why is logo.notation.notationStaging an object and not an array?
Object.keys(logo.notation.notationStaging).forEach((voice) => {
if (logo.notation.notationStaging[voice].length === 0) {
return;
}
voiceNum = parseInt(voice) + 1;
add('<score-part id="P' + voiceNum + '">');
indent++;
add("<part-name> Voice #" + voiceNum + " </part-name>");
indent--;
add("</score-part>");
});
indent--;
add("</part-list>");
indent--;
Object.keys(logo.notation.notationStaging).forEach((voice) => {
if (logo.notation.notationStaging[voice].length === 0) {
return;
}
voiceNum = parseInt(voice) + 1;
indent++;
add('<part id="P' + voiceNum + '">');
indent++;
// assume 4/4 time, 32 divisions bc smallest note is 1/32
// key is C by default
let currMeasure = 1;
let divisions = 32;
let beats = 4;
let beatType = 4;
let beatsChanged = false;
let newDivisions = -1;
let newBeats = -1;
let newBeatType = -1;
let openedMeasureTag = false;
let queuedTempo = null;
let firstMeasure = true;
indent++;
let divisionsLeft = divisions;
const notes = logo.notation.notationStaging[voice];
for (let i = 0; i < notes.length; i += 1) {
// obj = [note, duration, dotCount, tupletValue, roundDown, insideChord, staccato]
const obj = notes[i];
if (["tie", "begin slur", "end slur"].includes(obj)) {
continue;
}
if (ignore.includes(obj)) {
continue;
}
// ignore key
if (obj === "key") {
i += 2;
continue;
}
if (obj === "begin crescendo") {
add('<direction placement="above">');
indent++;
add("<direction-type>");
indent++;
add('<wedge type="crescendo"/>');
indent--;
add("</direction-type>");
indent--;
add("</direction>");
continue;
}
if (obj === "begin decrescendo") {
add('<direction placement="above">');
indent++;
add("<direction-type>");
indent++;
add('<wedge type="diminuendo"/>');
indent--;
add("</direction-type>");
indent--;
add("</direction>");
continue;
}
if (obj === "end crescendo" || obj === "end decrescendo") {
add("<direction>");
indent++;
add("<direction-type>");
indent++;
add('<wedge type="stop"/>');
indent--;
add("</direction-type>");
indent--;
add("</direction>");
continue;
}
if (obj === "tempo") {
const bpm = notes[i + 1];
const beatMeasure = notes[i + 2];
const bpmAdjusted = Math.floor(bpm * (4 / beatMeasure));
if (openedMeasureTag) {
add('<sound tempo="' + bpmAdjusted + '"/>');
} else {
queuedTempo = '<sound tempo="' + bpmAdjusted + '"/>';
}
i += 2;
continue;
}
if (obj === "meter") {
newBeats = notes[i + 1];
newBeatType = notes[i + 2];
// divisions per beat == how many 1/32 notes fit in one beat?
newDivisions = newBeats * (1 / newBeatType / (1 / 32));
i += 2;
beatsChanged = true;
continue;
}
// We only add </chord> tag to the non-first elements in a chord
let isChordNote = false;
for (const p of obj[0]) {
let dur = 32 / obj[1];
for (let j = 0; j < obj[2]; j++) dur += dur / 2;
if (divisionsLeft < dur && !isChordNote) {
if (openedMeasureTag) {
// throw "big chungus";
add("</measure>");
currMeasure++;
divisionsLeft = divisions;
openedMeasureTag = false;
}
}
if (!isChordNote) {
if (divisionsLeft === divisions) {
if (firstMeasure) {
add(
'<measure number="' +
currMeasure +
'"> <attributes> <divisions>' +
divisions +
"</divisions> <key> <fifths>0</fifths> </key> <time> <beats>" +
beats +
"</beats> <beat-type>" +
beatType +
"</beat-type> </time> <clef> <sign>G</sign> <line>2</line> </clef> </attributes>"
);
firstMeasure = false;
} else if (beatsChanged) {
beats = newBeats;
beatType = newBeatType;
divisions = newDivisions;
divisionsLeft = divisions;
add(
'<measure number="' +
currMeasure +
'"> <attributes> <divisions>' +
newDivisions +
"</divisions> <key> <fifths>0</fifths> </key> <time> <beats>" +
newBeats +
"</beats> <beat-type>" +
newBeatType +
"</beat-type> </time> <clef> <sign>G</sign> <line>2</line> </clef> </attributes>"
);
beatsChanged = false;
} else {
add('<measure number="' + currMeasure + '">');
}
openedMeasureTag = true;
if (queuedTempo !== null) {
add(queuedTempo);
queuedTempo = null;
}
}
divisionsLeft -= dur;
}
let alter;
if (p[1] === "\u266d") {
alter = -1; // flat
} else if (p[1] === "\u266F") {
alter = 1; // sharp
} else {
alter = 0; // no accidental
}
add("<note>");
indent++;
if (isChordNote) add("<chord/>");
if (p[0] === "R") {
add("<rest/>");
} else {
add("<pitch>");
indent++;
add("<step>" + p[0] + "</step>");
if (alter != 0) {
add("<alter>" + alter + "</alter>");
}
add("<octave>" + p[p.length - 1] + "</octave>");
indent--;
add("</pitch>");
}
add("<duration>" + dur + "</duration>");
if (notes[i + 1] === "tie") {
add('<tie type="start"/>');
} else if (notes[i - 1] === "tie") {
add('<tie type="stop"/>');
}
indent--;
add("<notations>");
indent++;
add("<articulations>");
indent++;
if (obj[6]) {
add('<staccato placement="below"/>');
}
indent--;
add("</articulations>");
indent--;
if (notes[i - 1] === "begin slur") {
indent++;
add('<slur type="start"/>');
indent--;
}
if (notes[i + 1] === "end slur") {
indent++;
add('<slur type="stop"/>');
indent--;
}
add("</notations>");
add("</note>");
isChordNote = true;
}
}
indent--;
if (openedMeasureTag) {
indent++;
add("<barline>");
indent++;
add("<bar-style>light-heavy</bar-style>");
indent--;
add("</barline>");
indent--;
add("</measure>");
}
indent--;
add("</part>");
indent--;
});
add("</score-partwise>");
// Filter voices
let mi = 1e5;
for (let i = 0; i < res.length - 1; i++) {
if ((res[i] === "P" || res[i] === "#") && "123456789".includes(res[i + 1])) {
mi = Math.min(mi, parseInt(res[i + 1]));
}
}
res = res.split("");
for (let i = 0; i < res.length - 1; i++) {
if ((res[i] === "P" || res[i] === "#") && "123456789".includes(res[i + 1])) {
res[i + 1] = parseInt(res[i + 1]) - mi + 1;
}
}
res = res.join("");
return res;
};