-
Notifications
You must be signed in to change notification settings - Fork 14
/
xmlHelpers.ts
486 lines (480 loc) · 18.5 KB
/
xmlHelpers.ts
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
// Copyright (c) Aaron David Newman 2021.
import { smoSerialize } from '../../common/serializationHelpers';
import { SmoArticulation, SmoNoteModifierBase, SmoOrnament } from '../data/noteModifiers';
import { SmoMusic } from '../data/music';
import { SmoNote } from '../data/note';
import { Pitch, PitchLetter } from '../data/common';
import { SmoSelector } from '../xform/selections';
import { SmoBarline } from '../data/measureModifiers';
const VF = eval('Vex.Flow');
export interface XmlOrnamentData {
ctor: string,
params: Record<string, string>
}
export interface XmlSmoMap {
xml: string, smo: string
}
export interface XmlDurationAlteration {
noteCount: number, noteDuration: number
}
export interface XmlDuration {
tickCount: number, duration: number, alteration: XmlDurationAlteration
}
/**
* Store slur information when parsing xml
*/
export interface XmlSlurType {
number: number, type: string, orientation: string, placement: string, controlX: number, controlY: number, selector: SmoSelector, yOffset: number
}
/**
* Store tie information when parsing xml
*/
export interface XmlTieType {
number: number, type: string, orientation: string, selector: SmoSelector, pitchIndex: number
}
/**
* Store tuplet information when parsing xml
*/
export interface XmlTupletData {
number: number, type: string
}
export interface XmlEndingData {
numbers: number[], type: string
}
export type LyricSyllabic = 'begin' | 'end' | 'middle' | 'single';
/**
* Store lyric information when parsing xml
*/
export interface XmlLyricData {
_text: string, verse: number | string, syllabic: LyricSyllabic
}
/**
* Utilities for parsing and serialzing musicXML.
* @category SmoToXml
* */
export class XmlHelpers {
/**
* mxml note 'types', really s/b stem types.
* For grace notes, we use the note type and not duration
* to get the flag
*/
static get noteTypesToSmoMap(): Record<string, number> {
return {
'breve': 8192 * 4,
'whole': 8192 * 2,
'half': 8192,
'quarter': 4096,
'eighth': 2048,
'16th': 1024,
'32nd': 512,
'64th': 256,
'128th': 128
};
}
static readonly _ticksToNoteTypeMap: Record<number, string> = smoSerialize.reverseMap(XmlHelpers.noteTypesToSmoMap) as Record<number, string>;
static get ticksToNoteTypeMap(): Record<number, string> {
return XmlHelpers._ticksToNoteTypeMap;
}
// ### closestStemType
// smo infers the stem type from the duration, but other applications don't
static closestStemType(ticks: number) {
const nticks = VF.durationToTicks(SmoMusic.vexStemType(ticks));
return XmlHelpers.ticksToNoteTypeMap[nticks];
}
static get beamStates(): Record<string, number> {
return {
BEGIN: 1,
END: 2,
AUTO: 3
};
}
static get ornamentXmlToSmoMap(): Record<string, XmlOrnamentData> {
return {
staccato: { ctor: 'SmoArticulation', params: { articulation: SmoArticulation.articulations.staccato } },
tenuto: { ctor: 'SmoArticulation', params: { articulation: SmoArticulation.articulations.tenuto } },
marcato: { ctor: 'SmoArticulation', params: { articulation: SmoArticulation.articulations.marcato } },
accent: { ctor: 'SmoArticulation', params: { articulation: SmoArticulation.articulations.accent } },
doit: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.doitLong } },
falloff: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.fall } },
scoop: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.scoop } },
'delayed-turn': { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.turn, offset: SmoOrnament.offsets.after } },
turn: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.turn, offset: SmoOrnament.offsets.on } },
'inverted-turn': { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.turnInverted } },
mordent: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.mordent } },
'inverted-mordent': { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.mordentInverted } },
shake: { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.mordentInverted } },
'trill-mark': { ctor: 'SmoOrnament', params: { ornament: SmoOrnament.ornaments.trill } },
};
}
// ### createRootElement
// Create score-partwise document with prelude
// https://bugzilla.mozilla.org/show_bug.cgi?id=318086
static createRootElement() {
const doc = document.implementation.createDocument('', '', null);
const rootElem = doc.createElement('score-partwise');
const piElement = doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"');
rootElem.setAttribute('version', '3.1');
doc.appendChild(rootElem);
doc.insertBefore(piElement, rootElem);
return doc;
}
// Parse an element whose child has a number in the textContent
static getNumberFromElement(parent: Element, path: string, defaults: number): number {
let rv = (typeof (defaults) === 'undefined' || defaults === null)
? 0 : defaults;
const tval = XmlHelpers.getTextFromElement(parent, path, defaults);
if (!tval) {
return rv;
}
if (typeof (tval) === 'number') {
return tval;
}
if (tval.indexOf('.')) {
const tf = parseFloat(tval);
rv = isNaN(tf) ? rv : tf;
} else {
const ff = parseInt(tval, 10);
rv = isNaN(ff) ? rv : ff;
}
return rv;
}
// Parse an element whose child has a textContent
static getTextFromElement(parent: Element, path: string, defaults: number | string | null): string {
const rv = (typeof (defaults) === 'undefined' || defaults === null)
? 0 : defaults;
const el = [...parent.getElementsByTagName(path)];
if (!el.length) {
return rv.toString();
}
return el[0].textContent as string;
}
static getNumberFromAttribute(node: Element, attribute: string, defaults: number) {
const str = XmlHelpers.getTextFromAttribute(node, attribute, defaults.toString());
const rv = parseInt(str, 10);
if (isNaN(rv)) {
return defaults;
}
return rv;
}
static getTextFromAttribute(node: Element, attribute: string, defaults: string): string {
const rv = node.getAttribute(attribute);
if (rv) {
return rv;
}
return defaults;
}
// ### getChildrenFromPath
// Like xpath, given ['foo', 'bar'] and parent element
// 'moo' return any element /moo/foo/bar as an array of elements
static getChildrenFromPath(parent: Element, pathAr: string[]): Element[] {
let i = 0;
let node = parent;
const rv: Element[] = [];
for (i = 0; i < pathAr.length; ++i) {
const tag = pathAr[i];
const nodes: Element[] = [...node.getElementsByTagName(tag)];
if (nodes.length === 0) {
return [];
}
if (i < pathAr.length - 1) {
node = nodes[0];
} else {
nodes.forEach((nn: Element) => {
rv.push(nn);
});
}
}
return rv;
}
static getStemType(noteElement: Element) {
const tt = XmlHelpers.getTextFromElement(noteElement, 'stem', '');
if (tt === 'up') {
return SmoNote.flagStates.up;
} else if (tt === 'down') {
return SmoNote.flagStates.down;
}
return SmoNote.flagStates.auto;
}
static getEnding(barlineNode: Element): XmlEndingData | null {
const endingNodes = [...barlineNode.getElementsByTagName('ending')];
if (!endingNodes.length) {
return null;
}
const attrs = XmlHelpers.nodeAttributes(endingNodes[0]);
if (attrs.number && attrs.type) {
return {
numbers: attrs.number.split(',').map((x) => parseInt(x, 10)),
type: attrs.type
};
}
return null;
}
static getBarline(barlineNode: Element): number {
const rptNode = [...barlineNode.getElementsByTagName('repeat')];
if (rptNode.length) {
const repeatattr = XmlHelpers.nodeAttributes(rptNode[0]);
return repeatattr.direction === 'forward' ? SmoBarline.barlines.startRepeat : SmoBarline.barlines.endRepeat;
}
const styleText = XmlHelpers.getTextFromElement(barlineNode, 'bar-style', '');
if (styleText.length) {
const double = styleText.indexOf('-') >= 0;
const heavy = styleText.indexOf('heavy') >= 0;
const light = styleText.indexOf('light') >= 0;
if (double && heavy && light) {
return SmoBarline.barlines.endBar;
}
if (double) {
return SmoBarline.barlines.doubleBar;
}
}
return SmoBarline.barlines.singleBar;
}
// ### assignDefaults
// Map SMO layout data from xml layout data (default node)
static assignDefaults(node: Element, defObj: any, parameters: XmlSmoMap[]) {
parameters.forEach((param) => {
if (!isNaN(parseInt(defObj[param.smo], 10))) {
const smoParam = param.smo;
const xmlParam = param.xml;
defObj[smoParam] = XmlHelpers.getNumberFromElement(node, xmlParam, defObj[smoParam]);
}
});
}
// ### nodeAttributes
// turn the attributes of an element into a JS hash
static nodeAttributes(node: Element): Record<string, string> {
const rv: Record<string, string> = {};
node.getAttributeNames().forEach((attr) => {
const aval: string | null = node.getAttribute(attr);
if (aval) {
rv[attr] = aval;
}
});
return rv;
}
// Some measures have staff ID, some don't.
// convert xml 1 index to array 0 index
static getStaffId(node: Element) {
const staff = [...node.getElementsByTagName('staff')];
if (staff.length && staff[0].textContent) {
return parseInt(staff[0].textContent, 10) - 1;
}
return 0;
}
static noteBeamState(noteNode: Element) {
const beamNodes = [...noteNode.getElementsByTagName('beam')];
if (!beamNodes.length) {
return XmlHelpers.beamStates.AUTO;
}
const beamText = beamNodes[0].textContent;
if (beamText === 'begin') {
return XmlHelpers.beamStates.BEGIN;
} else if (beamText === 'end') {
return XmlHelpers.beamStates.END;
}
return XmlHelpers.beamStates.AUTO;
}
// same with notes and voices. same convert
static getVoiceId(node: Element) {
const voice = [...node.getElementsByTagName('voice')];
if (voice.length && voice[0].textContent) {
return parseInt(voice[0].textContent, 10) - 1;
}
return 0;
}
static smoPitchFromNote(noteNode: Element, defaultPitch: Pitch): Pitch {
const accidentals = ['bb', 'b', 'n', '#', '##'];
const letter: PitchLetter = XmlHelpers.getTextFromElement(noteNode, 'step', defaultPitch.letter).toLowerCase() as PitchLetter;
const octave = XmlHelpers.getNumberFromElement(noteNode, 'octave', defaultPitch.octave);
const xaccidental = XmlHelpers.getNumberFromElement(noteNode, 'alter', 0);
return { letter, accidental: accidentals[xaccidental + 2], octave };
}
static isGrace(noteNode: Element) {
const path = XmlHelpers.getChildrenFromPath(noteNode, ['grace']);
return path?.length > 0;
}
static isSystemBreak(measureNode: Element) {
const printNodes = measureNode.getElementsByTagName('print');
if (printNodes.length) {
const attrs = XmlHelpers.nodeAttributes(printNodes[0]);
if (typeof (attrs['new-system']) !== 'undefined') {
return attrs['new-system'] === 'yes';
}
}
return false;
}
// ### durationFromType
// Get the SMO tick duration of a note, based on the XML type element (quarter, etc)
static durationFromType(noteNode: Element, def: number): number {
const typeNodes = [...noteNode.getElementsByTagName('type')];
if (typeNodes.length) {
const txt = typeNodes[0].textContent;
if (txt && XmlHelpers.noteTypesToSmoMap[txt]) {
return XmlHelpers.noteTypesToSmoMap[txt];
}
}
return def;
}
// ### durationFromNode
// the true duration value, used to handle forward/backward
static durationFromNode(noteNode: Element, def: number) {
const durationNodes = [...noteNode.getElementsByTagName('duration')];
if (durationNodes.length && durationNodes[0].textContent) {
const duration = parseInt(durationNodes[0].textContent, 10);
return duration;
}
return def;
}
static ticksFromDuration(noteNode: Element, divisions: number, def: number): XmlDuration {
const rv: XmlDuration = { tickCount: def, duration: def / divisions, alteration: { noteCount: 1, noteDuration: 1 } };
const durationNodes = [...noteNode.getElementsByTagName('duration')];
const timeAlteration = XmlHelpers.getTimeAlteration(noteNode);
// different ways to declare note duration - from type is the graphical
// type, SMO uses ticks for everything
if (durationNodes.length && durationNodes[0].textContent) {
rv.duration = parseInt(durationNodes[0].textContent, 10);
rv.tickCount = 4096 * (rv.duration / divisions);
} else {
rv.tickCount = XmlHelpers.durationFromType(noteNode, def);
rv.duration = (divisions / 4096) * rv.tickCount;
}
// If this is a tuplet, we adjust the note duration back to the graphical type
// and SMO will create the tuplet after. We keep track of tuplet data though for beaming
if (timeAlteration) {
rv.tickCount = (rv.tickCount * timeAlteration.noteCount) / timeAlteration.noteDuration;
rv.alteration = timeAlteration;
}
return rv;
}
static getTieData(noteNode: Element, selector: SmoSelector, pitchIndex: number): XmlTieType[] {
const rv: XmlTieType[] = [];
let number = 0;
const nNodes = [...noteNode.getElementsByTagName('notations')];
nNodes.forEach((nNode) => {
const slurNodes = [...nNode.getElementsByTagName('tied')];
slurNodes.forEach((slurNode) => {
const orientation = XmlHelpers.getTextFromAttribute(slurNode, 'orientation', 'auto');
const type = slurNode.getAttribute('type') as string;
number = XmlHelpers.getNumberFromAttribute(slurNode, 'number', 1);
rv.push({ number, type, orientation, selector, pitchIndex });
});
});
return rv;
}
static getSlurData(noteNode: Element, selector: SmoSelector): XmlSlurType[] {
const rv: XmlSlurType[] = [];
const nNodes = [...noteNode.getElementsByTagName('notations')];
nNodes.forEach((nNode) => {
const slurNodes = [...nNode.getElementsByTagName('slur')];
slurNodes.forEach((slurNode) => {
const number = parseInt(slurNode.getAttribute('number') as string, 10);
const type = slurNode.getAttribute('type') as string;
const orientation = XmlHelpers.getTextFromAttribute(slurNode, 'orienation', 'auto');
const placement = XmlHelpers.getTextFromAttribute(slurNode, 'placement', 'auto');
const controlX = XmlHelpers.getNumberFromAttribute(slurNode, 'bezier-x', 0);
// Y coordinates are reversed from music XML to SVG, hence the -1
const controlY = XmlHelpers.getNumberFromAttribute(slurNode, 'bezier-y', 15) * -1;
const slurInfo = { number, type, orientation, placement, controlX, controlY, selector, invert: false, yOffset: 0 };
rv.push(slurInfo);
});
});
return rv;
}
static getCrescendoData(directionElement: Element) {
let rv = {};
const nNodes = XmlHelpers.getChildrenFromPath(directionElement,
['direction-type', 'wedge']);
nNodes.forEach((nNode) => {
rv = { type: nNode.getAttribute('type') };
});
return rv;
}
static getTupletData(noteNode: Element): XmlTupletData[] {
const rv: XmlTupletData[] = [];
const nNodes = [...noteNode.getElementsByTagName('notations')];
nNodes.forEach((nNode) => {
const slurNodes = [...nNode.getElementsByTagName('tuplet')];
slurNodes.forEach((slurNode) => {
const number = parseInt(slurNode.getAttribute('number') as string, 10);
const type = slurNode.getAttribute('type') as string;
rv.push({ number, type });
});
});
return rv;
}
static articulationsAndOrnaments(noteNode: Element): SmoNoteModifierBase[] {
const rv: SmoNoteModifierBase[] = [];
const nNodes = [...noteNode.getElementsByTagName('notations')];
nNodes.forEach((nNode) => {
['articulations', 'ornaments'].forEach((typ) => {
const articulations = [...nNode.getElementsByTagName(typ)];
articulations.forEach((articulation) => {
Object.keys(XmlHelpers.ornamentXmlToSmoMap).forEach((key) => {
if (articulation.getElementsByTagName(key).length) {
const ctor = eval('globalThis.Smo.' + XmlHelpers.ornamentXmlToSmoMap[key].ctor);
rv.push(new ctor(XmlHelpers.ornamentXmlToSmoMap[key].params));
}
});
});
});
});
return rv;
}
static lyrics(noteNode: Element): XmlLyricData[] {
const rv: XmlLyricData[] = [];
const nNodes = [...noteNode.getElementsByTagName('lyric')];
nNodes.forEach((nNode) => {
let verse = nNode.getAttribute('number');
const text = XmlHelpers.getTextFromElement(nNode, 'text', '_');
const name = nNode.getAttribute('name') as string;
const syllabic = XmlHelpers.getTextFromElement(nNode, 'syllabic', 'end') as LyricSyllabic;
// Per xml spec, verse can be specified by a string (name), as in 'chorus'
if (!verse) {
verse = name;
}
const obj: XmlLyricData = { _text: text, verse, syllabic };
rv.push(obj);
});
return rv;
}
static getTimeAlteration(noteNode: Element): XmlDurationAlteration | null {
const timeNodes = XmlHelpers.getChildrenFromPath(noteNode, ['time-modification']);
if (timeNodes.length) {
return {
noteCount: XmlHelpers.getNumberFromElement(timeNodes[0], 'actual-notes', 1),
noteDuration: XmlHelpers.getNumberFromElement(timeNodes[0], 'normal-notes', 1)
};
}
return null;
}
// ### createTextElementChild
// In: ../parent
// Out: ../parent/elementName/obj[field]
// returns elementName element. If obj is null, just creates and returns child
// if obj is a string, it uses it as the text value
static createTextElementChild(parentElement: Element, elementName: string, obj: any, field: string): Element {
const el = parentElement.ownerDocument.createElement(elementName);
if (obj) {
if (typeof (obj) === 'string') {
el.textContent = obj;
} else {
el.textContent = obj[field];
}
}
parentElement.appendChild(el);
return el;
}
static createAttributes(element: Element, obj: any) {
Object.keys(obj).forEach((key) => {
const attr = element.ownerDocument.createAttribute(key);
attr.value = obj[key];
element.setAttributeNode(attr);
});
}
static createAttribute(element: Element, name: string, value: any) {
const obj: any = {};
obj[name] = value;
XmlHelpers.createAttributes(element, obj);
}
}