@stringsync/vexml
Version:
MusicXML to Vexflow
52 lines (51 loc) • 1.67 kB
JavaScript
import { SYLLABIC_TYPES } from './enums';
/**
* The <lyric> element represents text underlays for lyrics.
*
* https://www.w3.org/2021/06/musicxml40/musicxml-reference/elements/lyric/
*/
export class Lyric {
element;
constructor(element) {
this.element = element;
}
/** Returns the verse number the lyric belongs to. Defaults to 1. */
getVerseNumber() {
return this.element.attr('number').withDefault(1).int();
}
/**
* Returns the components of the lyric.
*
* This method assumes that the lyric children adhere to the MusicXML spec — it does not coerc values.
*/
getComponents() {
const components = new Array();
for (const element of this.element.children('syllabic', 'text', 'elision')) {
if (element.isNamed('syllabic')) {
components.push(this.createSyllabic(element));
}
else if (element.isNamed('text')) {
components.push(this.createText(element));
}
else if (element.isNamed('elision')) {
components.push(this.createElision(element));
}
}
return components;
}
createSyllabic(syllabic) {
const value = syllabic
.content()
.withDefault('single')
.enum(SYLLABIC_TYPES);
return { type: 'syllabic', value };
}
createText(text) {
const value = text.content().withDefault('').str();
return { type: 'text', value };
}
createElision(elision) {
const value = elision.content().withDefault('').str();
return { type: 'elision', value };
}
}