@stringsync/vexml
Version:
MusicXML to Vexflow
31 lines (30 loc) • 1.14 kB
JavaScript
import * as data from '../../data';
import * as musicxml from '../../musicxml';
import * as errors from '../../errors';
import { Score } from './score';
import { DEFAULT_CONFIG } from '../../config';
import { NoopLogger } from '../../debug';
export class MusicXMLParser {
config;
log;
constructor(opts) {
this.config = { ...DEFAULT_CONFIG, ...opts?.config };
this.log = opts?.logger ?? new NoopLogger();
}
/** Parses a MusicXML source into a vexml data document. */
parse(musicXMLSrc) {
let musicXML;
if (musicXMLSrc instanceof Document) {
musicXML = new musicxml.MusicXML(musicXMLSrc);
}
else if (typeof musicXMLSrc === 'string') {
musicXML = new musicxml.MusicXML(new DOMParser().parseFromString(musicXMLSrc, 'application/xml'));
}
else {
throw new errors.VexmlError(`Invalid source type: ${musicXMLSrc}`);
}
const scorePartwise = musicXML.getScorePartwise();
const score = Score.create(this.config, this.log, { scorePartwise }).parse();
return new data.Document(score);
}
}