UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

910 lines (909 loc) 28.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.grace = exports.accidental = exports.octave = exports.alter = exports.step = exports.pitch = exports.displayOctave = exports.displayStep = exports.rest = exports.dot = exports.stem = exports.ending = exports.repeat = exports.barStyle = exports.staves = exports.barline = exports.perMinute = exports.beatUnitDot = exports.beatUnit = exports.metronome = exports.symbolic = exports.words = exports.directionType = exports.direction = exports.topSystemDistance = exports.systemDistance = exports.rightMargin = exports.leftMargin = exports.systemMargins = exports.systemLayout = exports.staffDistance = exports.staffLayout = exports.defaults = exports.print = exports.divisions = exports.attributes = exports.type = exports.forward = exports.backup = exports.note = exports.measure = exports.part = exports.partName = exports.scorePart = exports.partList = exports.scorePartwise = exports.musicXML = exports.createNamedElement = exports.createElement = exports.createDocument = void 0; exports.staccato = exports.strongAccent = exports.accent = exports.articulations = exports.fermata = exports.rootfile = exports.rootfiles = exports.container = exports.pedal = exports.octaveShift = exports.wavyLine = exports.trillMark = exports.ornaments = exports.wedge = exports.tied = exports.slur = exports.normalNotes = exports.actualNotes = exports.timeModification = exports.tuplet = exports.staffType = exports.staffLines = exports.multipleRest = exports.measureStyle = exports.staffDetails = exports.beam = exports.chord = exports.notehead = exports.staff = exports.voice = exports.arpeggiate = exports.notations = exports.elision = exports.text = exports.syllabic = exports.lyric = exports.beatType = exports.beats = exports.clefOctaveChange = exports.line = exports.sign = exports.segno = exports.coda = exports.clef = exports.senzaMisura = exports.time = exports.mode = exports.fifths = exports.key = exports.duration = void 0; exports.accidentalMark = exports.breathMark = exports.falloff = exports.doit = exports.plop = exports.scoop = exports.staccatissimo = exports.detachedLegato = exports.tenuto = void 0; /* eslint-disable @typescript-eslint/no-unused-vars */ const namedelement_1 = require("./namedelement"); /** * A wrapper around NamedElement that performs mutative actions. * * This allows callers to exclusively work with NamedElements, as opposed to casting NamedElements to their native form. * It also prevents the need for NamedElement to have an mutative actions. */ class NamedElementEditor { element; constructor(element) { this.element = element; } /** Appends the elements to the wrapped element as direct children. */ append(...elements) { this.element.native().append(...elements.map((element) => element.native())); } /** Sets the text content of the wrapped element. */ setTextContent(textContent) { this.element.native().textContent = textContent; } /** Sets the attribute of the wrapped element. */ setAttribute(name, value) { this.element.native().setAttribute(name, value); } } /** Creates a document node. */ const createDocument = () => document.implementation.createDocument(null, null); exports.createDocument = createDocument; /** Creates an XML element with the specified tag name. */ const createElement = (tagName) => { return (0, exports.createDocument)().createElement(tagName); }; exports.createElement = createElement; /** Creates a NamedElement with the specified tag name. */ const createNamedElement = (tagName) => { return namedelement_1.NamedElement.of((0, exports.createElement)(tagName)); }; exports.createNamedElement = createNamedElement; /** * Creates an element factory. * * Factories are invoked by invoking the returned function with the arguments. * @example * const fooFactory = createNamedElementFactory<'foo', { bar: string }>('foo', (e, { bar }) => { * if (typeof bar === 'string') { * e.setTextContent(bar); * } * }); * const fooNamedElement = fooFactory('bar'); */ const createNamedElementFactory = (tagName, builder) => { return (args) => { const element = (0, exports.createNamedElement)(tagName); builder(new NamedElementEditor(element), args ?? {}); return element; }; }; const musicXML = (scorePartwise) => { const root = (0, exports.createDocument)(); root.appendChild(scorePartwise.native()); return root; }; exports.musicXML = musicXML; exports.scorePartwise = createNamedElementFactory('score-partwise', (e, { parts, partList, defaults }) => { if (parts) { e.append(...parts); } if (partList) { e.append(partList); } if (defaults) { e.append(defaults); } }); exports.partList = createNamedElementFactory('part-list', (e, { scoreParts }) => { if (scoreParts) { e.append(...scoreParts); } }); exports.scorePart = createNamedElementFactory('score-part', (e, { id, partName }) => { if (id) { e.setAttribute('id', id); } if (partName) { e.append(partName); } }); exports.partName = createNamedElementFactory('part-name', (e, { textContent }) => { if (textContent) { e.setTextContent(textContent); } }); exports.part = createNamedElementFactory('part', (e, { id, measures }) => { if (id) { e.setAttribute('id', id); } if (measures) { e.append(...measures); } }); exports.measure = createNamedElementFactory('measure', (e, { width, entries, implicit, number, notes, attributes, barlines, prints }) => { if (entries) { e.append(...entries); } if (notes) { e.append(...notes); } if (attributes) { e.append(...attributes); } if (barlines) { e.append(...barlines); } if (prints) { e.append(...prints); } if (implicit) { e.setAttribute('implicit', implicit); } if (typeof width === 'number') { e.setAttribute('width', width.toString()); } if (typeof number === 'string') { e.setAttribute('number', number); } }); exports.note = createNamedElementFactory('note', (e, { printObject, type, grace, stem, dots, rest, pitch, accidental, timeModification, notehead, duration, notations, voice, staff, beams, chord, lyrics, }) => { if (typeof printObject === 'string') { e.setAttribute('print-object', printObject); } if (grace) { e.append(grace); } if (chord) { e.append(chord); } if (pitch) { e.append(pitch); } if (rest) { e.append(rest); } if (duration) { e.append(duration); } if (voice) { e.append(voice); } if (type) { e.append(type); } if (dots) { e.append(...dots); } if (accidental) { e.append(accidental); } if (timeModification) { e.append(timeModification); } if (stem) { e.append(stem); } if (notehead) { e.append(notehead); } if (staff) { e.append(staff); } if (beams) { e.append(...beams); } if (notations) { e.append(...notations); } if (lyrics) { e.append(...lyrics); } }); exports.backup = createNamedElementFactory('backup', (e, { duration }) => { if (duration) { e.append(duration); } }); exports.forward = createNamedElementFactory('forward', (e, { duration }) => { if (duration) { e.append(duration); } }); exports.type = createNamedElementFactory('type', (e, { textContent }) => { if (textContent) { e.setTextContent(textContent); } }); exports.attributes = createNamedElementFactory('attributes', (e, { divisions, staves, clefs, times, keys, staffDetails, measureStyles }) => { if (divisions) { e.append(divisions); } if (keys) { e.append(...keys); } if (times) { e.append(...times); } if (clefs) { e.append(...clefs); } if (staves) { e.append(staves); } if (staffDetails) { e.append(...staffDetails); } if (measureStyles) { e.append(...measureStyles); } }); exports.divisions = createNamedElementFactory('divisions', (e, { positiveDivisions }) => { if (typeof positiveDivisions === 'number') { e.setTextContent(positiveDivisions.toString()); } }); exports.print = createNamedElementFactory('print', (e, { newSystem, newPage, staffLayouts, systemLayout }) => { if (typeof newSystem === 'boolean') { e.setAttribute('new-system', newSystem ? 'yes' : 'no'); } if (typeof newPage === 'boolean') { e.setAttribute('new-page', newPage ? 'yes' : 'no'); } if (staffLayouts) { e.append(...staffLayouts); } if (systemLayout) { e.append(systemLayout); } }); exports.defaults = createNamedElementFactory('defaults', (e, { staffLayouts, systemLayout }) => { if (staffLayouts) { e.append(...staffLayouts); } if (systemLayout) { e.append(systemLayout); } }); exports.staffLayout = createNamedElementFactory('staff-layout', (e, { number, staffDistance }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (staffDistance) { e.append(staffDistance); } }); exports.staffDistance = createNamedElementFactory('staff-distance', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.systemLayout = createNamedElementFactory('system-layout', (e, { systemMargins, systemDistance, topSystemDistance, systemDividers }) => { if (systemMargins) { e.append(systemMargins); } if (systemDistance) { e.append(systemDistance); } if (topSystemDistance) { e.append(topSystemDistance); } if (systemDividers) { e.append(systemDividers); } }); exports.systemMargins = createNamedElementFactory('system-margins', (e, { leftMargin, rightMargin }) => { if (leftMargin) { e.append(leftMargin); } if (rightMargin) { e.append(rightMargin); } }); exports.leftMargin = createNamedElementFactory('left-margin', (e, { tenths }) => { if (typeof tenths === 'number') { e.setTextContent(tenths.toString()); } }); exports.rightMargin = createNamedElementFactory('right-margin', (e, { tenths }) => { if (typeof tenths === 'number') { e.setTextContent(tenths.toString()); } }); exports.systemDistance = createNamedElementFactory('system-distance', (e, { tenths }) => { if (typeof tenths === 'number') { e.setTextContent(tenths.toString()); } }); exports.topSystemDistance = createNamedElementFactory('top-system-distance', (e, { tenths }) => { if (typeof tenths === 'number') { e.setTextContent(tenths.toString()); } }); exports.direction = createNamedElementFactory('direction', (e, { placement, types }) => { if (placement) { e.setAttribute('placement', placement); } if (types) { e.append(...types); } }); exports.directionType = createNamedElementFactory('direction-type', (e, { segnos, codas, wedge, pedal, metronome, octaveShift, tokens }) => { if (segnos) { e.append(...segnos); } if (codas) { e.append(...codas); } if (wedge) { e.append(wedge); } if (pedal) { e.append(pedal); } if (metronome) { e.append(metronome); } if (octaveShift) { e.append(octaveShift); } if (tokens) { e.append(...tokens); } }); exports.words = createNamedElementFactory('words', (e, { content }) => { if (content) { e.setTextContent(content); } }); exports.symbolic = createNamedElementFactory('symbol', (e, { smuflGlyphName }) => { if (smuflGlyphName) { e.setTextContent(smuflGlyphName); } }); exports.metronome = createNamedElementFactory('metronome', (e, { parentheses, content }) => { if (typeof parentheses === 'string') { e.setAttribute('parentheses', parentheses); } if (content) { e.append(...content); } }); exports.beatUnit = createNamedElementFactory('beat-unit', (e, { noteTypeValue }) => { if (noteTypeValue) { e.setTextContent(noteTypeValue); } }); exports.beatUnitDot = createNamedElementFactory('beat-unit-dot', () => { }); exports.perMinute = createNamedElementFactory('per-minute', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.barline = createNamedElementFactory('barline', (e, { location, barStyle, repeat, ending }) => { if (location) { e.setAttribute('location', location); } if (barStyle) { e.append(barStyle); } if (repeat) { e.append(repeat); } if (ending) { e.append(ending); } }); exports.staves = createNamedElementFactory('staves', (e, { staveCount }) => { if (typeof staveCount === 'number') { e.setTextContent(staveCount.toString()); } }); exports.barStyle = createNamedElementFactory('bar-style', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.repeat = createNamedElementFactory('repeat', (e, { direction }) => { if (direction) { e.setAttribute('direction', direction); } }); exports.ending = createNamedElementFactory('ending', (e, { number, type, textContent }) => { if (number) { e.setAttribute('number', number); } if (type) { e.setAttribute('type', type); } if (textContent) { e.setTextContent(textContent); } }); exports.stem = createNamedElementFactory('stem', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.dot = createNamedElementFactory('dot', (_, __) => { // noop }); exports.rest = createNamedElementFactory('rest', (e, { displayStep, displayOctave }) => { if (displayStep) { e.append(displayStep); } if (displayOctave) { e.append(displayOctave); } }); exports.displayStep = createNamedElementFactory('display-step', (e, { step }) => { if (step) { e.setTextContent(step); } }); exports.displayOctave = createNamedElementFactory('display-octave', (e, { octave }) => { if (octave) { e.setTextContent(octave); } }); exports.pitch = createNamedElementFactory('pitch', (e, { step, alter, octave }) => { if (step) { e.append(step); } if (alter) { e.append(alter); } if (octave) { e.append(octave); } }); exports.step = createNamedElementFactory('step', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.alter = createNamedElementFactory('alter', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.octave = createNamedElementFactory('octave', (e, { value }) => { if (value) { e.setTextContent(value.toString()); } }); exports.accidental = createNamedElementFactory('accidental', (e, { value, cautionary }) => { if (value) { e.setTextContent(value); } if (cautionary) { e.setAttribute('cautionary', cautionary); } }); exports.grace = createNamedElementFactory('grace', (e, { slash }) => { if (slash) { e.setAttribute('slash', slash); } }); exports.duration = createNamedElementFactory('duration', (e, { positiveDivisions }) => { if (typeof positiveDivisions === 'number') { e.setTextContent(positiveDivisions.toString()); } }); exports.key = createNamedElementFactory('key', (e, { fifths, mode }) => { if (fifths) { e.append(fifths); } if (mode) { e.append(mode); } }); exports.fifths = createNamedElementFactory('fifths', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.mode = createNamedElementFactory('mode', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.time = createNamedElementFactory('time', (e, { staveNumber, symbol, times, senzaMisura }) => { if (typeof staveNumber === 'number') { e.setAttribute('number', staveNumber.toString()); } if (symbol) { e.setAttribute('symbol', symbol); } if (times) { for (const { beats, beatType } of times) { if (beats) { e.append(beats); } if (beatType) { e.append(beatType); } } } if (senzaMisura) { e.append(senzaMisura); } }); exports.senzaMisura = createNamedElementFactory('senza-misura', (e, { content }) => { if (content) { e.setTextContent(content); } }); exports.clef = createNamedElementFactory('clef', (e, { number, sign, line, clefOctaveChange }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (sign) { e.append(sign); } if (line) { e.append(line); } if (clefOctaveChange) { e.append(clefOctaveChange); } }); exports.coda = createNamedElementFactory('coda', (_, __) => { // noop }); exports.segno = createNamedElementFactory('segno', (_, __) => { // noop }); exports.sign = createNamedElementFactory('sign', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.line = createNamedElementFactory('line', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.clefOctaveChange = createNamedElementFactory('clef-octave-change', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.beats = createNamedElementFactory('beats', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.beatType = createNamedElementFactory('beat-type', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.lyric = createNamedElementFactory('lyric', (e, { number, components }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (components) { e.append(...components); } }); exports.syllabic = createNamedElementFactory('syllabic', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.text = createNamedElementFactory('text', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.elision = createNamedElementFactory('elision', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.notations = createNamedElementFactory('notations', (e, { tieds, slurs, tuplets, arpeggiate, ornaments, fermatas, articulations, accidentalMarks }) => { if (tieds) { e.append(...tieds); } if (slurs) { e.append(...slurs); } if (tuplets) { e.append(...tuplets); } if (arpeggiate) { e.append(arpeggiate); } if (ornaments) { e.append(...ornaments); } if (articulations) { e.append(...articulations); } if (fermatas) { e.append(...fermatas); } if (accidentalMarks) { e.append(...accidentalMarks); } }); exports.arpeggiate = createNamedElementFactory('arpeggiate', (e, { direction }) => { if (direction) { e.setAttribute('direction', direction); } }); exports.voice = createNamedElementFactory('voice', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.staff = createNamedElementFactory('staff', (e, { number }) => { if (typeof number === 'number') { e.setTextContent(number.toString()); } }); exports.notehead = createNamedElementFactory('notehead', (e, { value }) => { if (value) { e.setTextContent(value); } }); exports.chord = createNamedElementFactory('chord', (_, __) => { // noop }); exports.beam = createNamedElementFactory('beam', (e, { number, beamValue }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (typeof beamValue === 'string') { e.setTextContent(beamValue); } }); exports.staffDetails = createNamedElementFactory('staff-details', (e, { number, staffType, staffLines }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (staffType) { e.append(staffType); } if (staffLines) { e.append(staffLines); } }); exports.measureStyle = createNamedElementFactory('measure-style', (e, { staffNumber, multipleRest }) => { if (typeof staffNumber === 'number') { e.setAttribute('number', staffNumber.toString()); } if (multipleRest) { e.append(multipleRest); } }); exports.multipleRest = createNamedElementFactory('multiple-rest', (e, { multipleRestCount }) => { if (typeof multipleRestCount === 'number') { e.setTextContent(multipleRestCount.toString()); } }); exports.staffLines = createNamedElementFactory('staff-lines', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.staffType = createNamedElementFactory('staff-type', (e, { value }) => { if (typeof value === 'string') { e.setTextContent(value); } }); exports.tuplet = createNamedElementFactory('tuplet', (e, { type, placement, showNumber }) => { if (type) { e.setAttribute('type', type); } if (placement) { e.setAttribute('placement', placement); } if (showNumber) { e.setAttribute('show-number', showNumber); } }); exports.timeModification = createNamedElementFactory('time-modification', (e, { actualNotes, normalNotes }) => { if (actualNotes) { e.append(actualNotes); } if (normalNotes) { e.append(normalNotes); } }); exports.actualNotes = createNamedElementFactory('actual-notes', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.normalNotes = createNamedElementFactory('normal-notes', (e, { value }) => { if (typeof value === 'number') { e.setTextContent(value.toString()); } }); exports.slur = createNamedElementFactory('slur', (e, { type, placement, number, lineType }) => { if (type) { e.setAttribute('type', type); } if (placement) { e.setAttribute('placement', placement); } if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.tied = createNamedElementFactory('tied', (e, { type, placement, number, lineType }) => { if (type) { e.setAttribute('type', type); } if (placement) { e.setAttribute('placement', placement); } if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.wedge = createNamedElementFactory('wedge', (e, { type, spread }) => { if (type) { e.setAttribute('type', type); } if (typeof spread === 'number') { e.setAttribute('spread', spread.toString()); } }); exports.ornaments = createNamedElementFactory('ornaments', (e, { contents }) => { if (contents) { e.append(...contents); } }); exports.trillMark = createNamedElementFactory('trill-mark', (e) => { }); exports.wavyLine = createNamedElementFactory('wavy-line', (e, { number, type }) => { if (typeof number === 'number') { e.setAttribute('number', number.toString()); } if (type) { e.setAttribute('type', type); } }); exports.octaveShift = createNamedElementFactory('octave-shift', (e, { type, size }) => { if (type) { e.setAttribute('type', type); } if (typeof size === 'number') { e.setAttribute('size', size.toString()); } }); exports.pedal = createNamedElementFactory('pedal', (e, { type, line, sign }) => { if (type) { e.setAttribute('type', type); } if (line) { e.setAttribute('line', line); } if (sign) { e.setAttribute('sign', sign); } }); exports.container = createNamedElementFactory('container', (e, { rootfiles }) => { if (rootfiles) { e.append(rootfiles); } }); exports.rootfiles = createNamedElementFactory('rootfiles', (e, { rootfiles }) => { if (rootfiles) { e.append(...rootfiles); } }); exports.rootfile = createNamedElementFactory('rootfile', (e, { fullPath, mediaType }) => { if (fullPath) { e.setAttribute('full-path', fullPath); } if (mediaType) { e.setAttribute('media-type', mediaType); } }); exports.fermata = createNamedElementFactory('fermata', (e, { shape, type }) => { if (shape) { e.setTextContent(shape); } if (type) { e.setAttribute('type', type); } }); exports.articulations = createNamedElementFactory('articulations', (e, { accents, strongAccents, staccatos, tenutos, detachedLegatos, staccatissimos, scoops, plops, doits, falloffs, breathMarks, }) => { if (accents) { e.append(...accents); } if (strongAccents) { e.append(...strongAccents); } if (staccatos) { e.append(...staccatos); } if (tenutos) { e.append(...tenutos); } if (detachedLegatos) { e.append(...detachedLegatos); } if (staccatissimos) { e.append(...staccatissimos); } if (scoops) { e.append(...scoops); } if (plops) { e.append(...plops); } if (doits) { e.append(...doits); } if (falloffs) { e.append(...falloffs); } if (breathMarks) { e.append(...breathMarks); } }); exports.accent = createNamedElementFactory('accent', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.strongAccent = createNamedElementFactory('strong-accent', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.staccato = createNamedElementFactory('staccato', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.tenuto = createNamedElementFactory('tenuto', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.detachedLegato = createNamedElementFactory('detached-legato', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.staccatissimo = createNamedElementFactory('staccatissimo', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.scoop = createNamedElementFactory('scoop', (e, { placement, lineType }) => { if (placement) { e.setAttribute('placement', placement); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.plop = createNamedElementFactory('plop', (e, { placement, lineType }) => { if (placement) { e.setAttribute('placement', placement); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.doit = createNamedElementFactory('doit', (e, { placement, lineType }) => { if (placement) { e.setAttribute('placement', placement); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.falloff = createNamedElementFactory('falloff', (e, { placement, lineType }) => { if (placement) { e.setAttribute('placement', placement); } if (lineType) { e.setAttribute('line-type', lineType); } }); exports.breathMark = createNamedElementFactory('breath-mark', (e, { placement }) => { if (placement) { e.setAttribute('placement', placement); } }); exports.accidentalMark = createNamedElementFactory('accidental-mark', (e, { type }) => { if (type) { e.setTextContent(type); } });