/* eslint-disable no-unused-expressions */ import { TheoryCheck } from "../../util/TheoryCheck"; /** * @class YYMParse musicxml 解析 */ export class TheoryParse { // 默认初始化 unit64, 代表一个 64 分音符所在的 占用的 时长 private _xmlDOM: Document; private _time: HTMLCollectionOf; private _octaveShifts: HTMLCollectionOf; private _measures: HTMLCollectionOf; private _notes: HTMLCollectionOf; private _print: HTMLCollectionOf; private _attribute: HTMLCollectionOf; private _part: HTMLCollectionOf; private _measureBackupDuration: number[]; // 4 分音符的持续时间 private _divisions: number; public constructor(xmlDOM_: Document) { this._xmlDOM = xmlDOM_; // remove words on music sheet (such as " Moderato " , "Allegretto") this.removeWords(); } private removeWords() { let directionWords = this._xmlDOM.getElementsByTagName("words"); while (!TheoryCheck.isEmptyArray(directionWords)) { directionWords[0]?.parentNode?.removeChild(directionWords[0]); } } public getMusicxmlDOM() { return this._xmlDOM; } public setMusicxmlDOM(value: Document) { this._xmlDOM = value; } public getXMLNoteList(): HTMLCollectionOf { return this._xmlDOM.getElementsByTagName("note"); } public getClefBeats() { if (TheoryCheck.isNullOrUndefined(this._time)) { this._time = this._xmlDOM.getElementsByTagName("time"); } return parseInt(this._time[0].getElementsByTagName("beats")[0].innerHTML, 10); } public getBeatsType() { if (TheoryCheck.isNullOrUndefined(this._time)) { this._time = this._xmlDOM.getElementsByTagName("time"); } return parseInt(this._time[0].getElementsByTagName("beat-type")[0].innerHTML, 10); } public getOctaveShift() { if (TheoryCheck.isNullOrUndefined(this._octaveShifts)) { this._octaveShifts = this._xmlDOM.getElementsByTagName("octave-shift"); } return this._octaveShifts; } public getMeasures() { if (TheoryCheck.isNullOrUndefined(this._measures)) { this._measures = this._xmlDOM.getElementsByTagName("measure"); } return this._measures; } public getNotes() { if (TheoryCheck.isNullOrUndefined(this._notes)) { this._notes = this._xmlDOM.getElementsByTagName("note"); } return this._notes; } public getPrint() { if (TheoryCheck.isNullOrUndefined(this._print)) { this._print = this.getMeasures()[0].getElementsByTagName("print"); } return this._print; } public getAttributes() { if (TheoryCheck.isNullOrUndefined(this._attribute)) { this._attribute = this.getMeasures()[0].getElementsByTagName("attributes"); } return this._attribute; } public getPart() { if (TheoryCheck.isNullOrUndefined(this._part)) { this._part = this._xmlDOM.getElementsByTagName("part"); } return this._part; } public getSpecificMeasureClef(index: number) { return this.getMeasures()[index].getElementsByTagName("clef"); } public getMeasureBackupDuration(): number[] { if (TheoryCheck.isNullOrUndefined(this._measures)) { this._measures = this._xmlDOM.getElementsByTagName("measure"); } if (TheoryCheck.isNullOrUndefined(this._measureBackupDuration)) { this._measureBackupDuration = []; // 获取每个小节的 backup 然后存起来 for (let i = 0; i < this._measures.length; i++) { let backupDuration = parseInt( this._measures[i].getElementsByTagName("backup")[0].getElementsByTagName("duration")[0].innerHTML, 10 ); this._measureBackupDuration.push(backupDuration); } } return this._measureBackupDuration; } public getDivisions(): number { if (TheoryCheck.isNullOrUndefined(this._measures)) { this._measures = this._xmlDOM.getElementsByTagName("measure"); } if (this._divisions === undefined) { this._divisions = parseInt(this._measures[0].getElementsByTagName("divisions")[0].innerHTML, 10); } return this._divisions; } }