| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- "use strict";
- var MusicSheet_1 = require("../MusicSheet");
- var SourceMeasure_1 = require("../VoiceData/SourceMeasure");
- var fraction_1 = require("../../Common/DataObjects/fraction");
- var InstrumentReader_1 = require("./InstrumentReader");
- var Instrument_1 = require("../Instrument");
- var ITextTranslation_1 = require("../Interfaces/ITextTranslation");
- var Exceptions_1 = require("../Exceptions");
- var Logging_1 = require("../../Common/Logging");
- var RhythmInstruction_1 = require("../VoiceData/Instructions/RhythmInstruction");
- var RhythmInstruction_2 = require("../VoiceData/Instructions/RhythmInstruction");
- var SourceStaffEntry_1 = require("../VoiceData/SourceStaffEntry");
- var InstrumentalGroup_1 = require("../InstrumentalGroup");
- var SubInstrument_1 = require("../SubInstrument");
- var ClefInstruction_1 = require("../VoiceData/Instructions/ClefInstruction");
- var Label_1 = require("../Label");
- var MusicSheetReader /*implements IMusicSheetReader*/ = (function () {
- function MusicSheetReader /*implements IMusicSheetReader*/() {
- this.completeNumberOfStaves = 0;
- }
- Object.defineProperty(MusicSheetReader /*implements IMusicSheetReader*/.prototype, "CompleteNumberOfStaves", {
- get: function () {
- return this.completeNumberOfStaves;
- },
- enumerable: true,
- configurable: true
- });
- MusicSheetReader /*implements IMusicSheetReader*/.doCalculationsAfterDurationHasBeenSet = function (instrumentReaders) {
- for (var _i = 0, instrumentReaders_1 = instrumentReaders; _i < instrumentReaders_1.length; _i++) {
- var instrumentReader = instrumentReaders_1[_i];
- instrumentReader.doCalculationsAfterDurationHasBeenSet();
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.createMusicSheet = function (root, path) {
- try {
- return this._createMusicSheet(root, path);
- }
- catch (e) {
- Logging_1.Logging.log("MusicSheetReader.CreateMusicSheet", e);
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype._removeFromArray = function (list, elem) {
- var i = list.indexOf(elem);
- if (i !== -1) {
- list.splice(i, 1);
- }
- };
- // Trim from a string also newlines
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.trimString = function (str) {
- return str.replace(/^\s+|\s+$/g, "");
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype._lastElement = function (list) {
- return list[list.length - 1];
- };
- //public SetPhonicScoreInterface(phonicScoreInterface: IPhonicScoreInterface): void {
- // this.phonicScoreInterface = phonicScoreInterface;
- //}
- //public ReadMusicSheetParameters(sheetObject: MusicSheetParameterObject, root: IXmlElement, path: string): MusicSheetParameterObject {
- // this.musicSheet = new MusicSheet();
- // if (root !== undefined) {
- // this.pushSheetLabels(root, path);
- // if (this.musicSheet.Title !== undefined) {
- // sheetObject.Title = this.musicSheet.Title.text;
- // }
- // if (this.musicSheet.Composer !== undefined) {
- // sheetObject.Composer = this.musicSheet.Composer.text;
- // }
- // if (this.musicSheet.Lyricist !== undefined) {
- // sheetObject.Lyricist = this.musicSheet.Lyricist.text;
- // }
- // let partlistNode: IXmlElement = root.element("part-list");
- // let partList: IXmlElement[] = partlistNode.elements();
- // this.createInstrumentGroups(partList);
- // for (let idx: number = 0, len: number = this.musicSheet.Instruments.length; idx < len; ++idx) {
- // let instr: Instrument = this.musicSheet.Instruments[idx];
- // sheetObject.InstrumentList.push(__init(new MusicSheetParameterObject.LibrarySheetInstrument(), { name: instr.name }));
- // }
- // }
- // return sheetObject;
- //}
- MusicSheetReader /*implements IMusicSheetReader*/.prototype._createMusicSheet = function (root, path) {
- var instrumentReaders = [];
- var sourceMeasureCounter = 0;
- this.musicSheet = new MusicSheet_1.MusicSheet();
- this.musicSheet.Path = path;
- if (root === undefined) {
- throw new Exceptions_1.MusicSheetReadingException("Undefined root element");
- }
- this.pushSheetLabels(root, path);
- var partlistNode = root.element("part-list");
- if (partlistNode === undefined) {
- throw new Exceptions_1.MusicSheetReadingException("Undefined partListNode");
- }
- var partInst = root.elements("part");
- var partList = partlistNode.elements();
- this.initializeReading(partList, partInst, instrumentReaders);
- var couldReadMeasure = true;
- this.currentFraction = new fraction_1.Fraction(0, 1);
- var guitarPro = false;
- var encoding = root.element("identification");
- if (encoding !== undefined) {
- encoding = encoding.element("encoding");
- }
- if (encoding !== undefined) {
- encoding = encoding.element("software");
- }
- if (encoding !== undefined && encoding.value === "Guitar Pro 5") {
- guitarPro = true;
- }
- while (couldReadMeasure) {
- if (this.currentMeasure !== undefined && this.currentMeasure.endsPiece) {
- sourceMeasureCounter = 0;
- }
- this.currentMeasure = new SourceMeasure_1.SourceMeasure(this.completeNumberOfStaves);
- for (var _i = 0, instrumentReaders_2 = instrumentReaders; _i < instrumentReaders_2.length; _i++) {
- var instrumentReader = instrumentReaders_2[_i];
- try {
- couldReadMeasure = couldReadMeasure && instrumentReader.readNextXmlMeasure(this.currentMeasure, this.currentFraction, guitarPro);
- }
- catch (e) {
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/InstrumentError", "Error while reading instruments.");
- throw new Exceptions_1.MusicSheetReadingException(errorMsg, e);
- }
- }
- if (couldReadMeasure) {
- this.musicSheet.addMeasure(this.currentMeasure);
- this.checkIfRhythmInstructionsAreSetAndEqual(instrumentReaders);
- this.checkSourceMeasureForundefinedEntries();
- this.setSourceMeasureDuration(instrumentReaders, sourceMeasureCounter);
- MusicSheetReader.doCalculationsAfterDurationHasBeenSet(instrumentReaders);
- this.currentMeasure.AbsoluteTimestamp = this.currentFraction.clone();
- this.musicSheet.SheetErrors.finalizeMeasure(this.currentMeasure.MeasureNumber);
- this.currentFraction.Add(this.currentMeasure.Duration);
- this.previousMeasure = this.currentMeasure;
- }
- }
- if (this.repetitionInstructionReader !== undefined) {
- this.repetitionInstructionReader.removeRedundantInstructions();
- if (this.repetitionCalculator !== undefined) {
- this.repetitionCalculator.calculateRepetitions(this.musicSheet, this.repetitionInstructionReader.RepetitionInstructions);
- }
- }
- this.musicSheet.checkForInstrumentWithNoVoice();
- this.musicSheet.fillStaffList();
- //this.musicSheet.DefaultStartTempoInBpm = this.musicSheet.SheetPlaybackSetting.BeatsPerMinute;
- //for (let idx: number = 0, len: number = this.afterSheetReadingModules.length; idx < len; ++idx) {
- // let afterSheetReadingModule: IAfterSheetReadingModule = this.afterSheetReadingModules[idx];
- // afterSheetReadingModule.calculate(this.musicSheet);
- //}
- return this.musicSheet;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.initializeReading = function (partList, partInst, instrumentReaders) {
- var instrumentDict = this.createInstrumentGroups(partList);
- this.completeNumberOfStaves = this.getCompleteNumberOfStavesFromXml(partInst);
- if (partInst.length !== 0) {
- // (*) this.repetitionInstructionReader.MusicSheet = this.musicSheet;
- this.currentFraction = new fraction_1.Fraction(0, 1);
- this.currentMeasure = undefined;
- this.previousMeasure = undefined;
- }
- var counter = 0;
- for (var _i = 0, partInst_1 = partInst; _i < partInst_1.length; _i++) {
- var node = partInst_1[_i];
- var idNode = node.attribute("id");
- if (idNode) {
- var currentInstrument = instrumentDict[idNode.value];
- var xmlMeasureList = node.elements("measure");
- var instrumentNumberOfStaves = 1;
- try {
- instrumentNumberOfStaves = this.getInstrumentNumberOfStavesFromXml(node);
- }
- catch (err) {
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/InstrumentStavesNumberError", "Invalid number of staves at instrument: ");
- this.musicSheet.SheetErrors.push(errorMsg + currentInstrument.Name);
- continue;
- }
- currentInstrument.createStaves(instrumentNumberOfStaves);
- instrumentReaders.push(new InstrumentReader_1.InstrumentReader(this.repetitionInstructionReader, xmlMeasureList, currentInstrument));
- if (this.repetitionInstructionReader !== undefined) {
- this.repetitionInstructionReader.XmlMeasureList[counter] = xmlMeasureList;
- }
- counter++;
- }
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.checkIfRhythmInstructionsAreSetAndEqual = function (instrumentReaders) {
- var rhythmInstructions = [];
- for (var i = 0; i < this.completeNumberOfStaves; i++) {
- if (this.currentMeasure.FirstInstructionsStaffEntries[i] !== undefined) {
- var last = this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions[this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions.length - 1];
- if (last instanceof RhythmInstruction_1.RhythmInstruction) {
- rhythmInstructions.push(last);
- }
- }
- }
- var maxRhythmValue = 0.0;
- var index = -1;
- for (var idx = 0, len = rhythmInstructions.length; idx < len; ++idx) {
- var rhythmInstruction = rhythmInstructions[idx];
- if (rhythmInstruction.Rhythm.RealValue > maxRhythmValue) {
- if (this.areRhythmInstructionsMixed(rhythmInstructions) && rhythmInstruction.SymbolEnum !== RhythmInstruction_2.RhythmSymbolEnum.NONE) {
- continue;
- }
- maxRhythmValue = rhythmInstruction.Rhythm.RealValue;
- index = rhythmInstructions.indexOf(rhythmInstruction);
- }
- }
- if (rhythmInstructions.length > 0 && rhythmInstructions.length < this.completeNumberOfStaves) {
- var rhythmInstruction = rhythmInstructions[index].clone();
- for (var i = 0; i < this.completeNumberOfStaves; i++) {
- if (this.currentMeasure.FirstInstructionsStaffEntries[i] !== undefined &&
- !(this._lastElement(this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions) instanceof RhythmInstruction_1.RhythmInstruction)) {
- this.currentMeasure.FirstInstructionsStaffEntries[i].removeAllInstructionsOfTypeRhythmInstruction();
- this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions.push(rhythmInstruction.clone());
- }
- if (this.currentMeasure.FirstInstructionsStaffEntries[i] === undefined) {
- this.currentMeasure.FirstInstructionsStaffEntries[i] = new SourceStaffEntry_1.SourceStaffEntry(undefined, undefined);
- this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions.push(rhythmInstruction.clone());
- }
- }
- for (var idx = 0, len = instrumentReaders.length; idx < len; ++idx) {
- var instrumentReader = instrumentReaders[idx];
- instrumentReader.ActiveRhythm = rhythmInstruction;
- }
- }
- if (rhythmInstructions.length === 0 && this.currentMeasure === this.musicSheet.SourceMeasures[0]) {
- var rhythmInstruction = new RhythmInstruction_1.RhythmInstruction(new fraction_1.Fraction(4, 4, false), 4, 4, RhythmInstruction_2.RhythmSymbolEnum.NONE);
- for (var i = 0; i < this.completeNumberOfStaves; i++) {
- if (this.currentMeasure.FirstInstructionsStaffEntries[i] === undefined) {
- this.currentMeasure.FirstInstructionsStaffEntries[i] = new SourceStaffEntry_1.SourceStaffEntry(undefined, undefined);
- }
- else {
- this.currentMeasure.FirstInstructionsStaffEntries[i].removeAllInstructionsOfTypeRhythmInstruction();
- }
- this.currentMeasure.FirstInstructionsStaffEntries[i].Instructions.push(rhythmInstruction);
- }
- for (var idx = 0, len = instrumentReaders.length; idx < len; ++idx) {
- var instrumentReader = instrumentReaders[idx];
- instrumentReader.ActiveRhythm = rhythmInstruction;
- }
- }
- for (var idx = 0, len = rhythmInstructions.length; idx < len; ++idx) {
- var rhythmInstruction = rhythmInstructions[idx];
- if (rhythmInstruction.Rhythm.RealValue < maxRhythmValue) {
- if (this._lastElement(this.currentMeasure.FirstInstructionsStaffEntries[rhythmInstructions.indexOf(rhythmInstruction)].Instructions) instanceof RhythmInstruction_1.RhythmInstruction) {
- // TODO Test correctness
- var instrs = this.currentMeasure.FirstInstructionsStaffEntries[rhythmInstructions.indexOf(rhythmInstruction)].Instructions;
- instrs[instrs.length - 1] = rhythmInstructions[index].clone();
- }
- }
- if (Math.abs(rhythmInstruction.Rhythm.RealValue - maxRhythmValue) < 0.000001 &&
- rhythmInstruction.SymbolEnum !== RhythmInstruction_2.RhythmSymbolEnum.NONE &&
- this.areRhythmInstructionsMixed(rhythmInstructions)) {
- rhythmInstruction.SymbolEnum = RhythmInstruction_2.RhythmSymbolEnum.NONE;
- }
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.areRhythmInstructionsMixed = function (rhythmInstructions) {
- for (var i = 1; i < rhythmInstructions.length; i++) {
- if (Math.abs(rhythmInstructions[i].Rhythm.RealValue - rhythmInstructions[0].Rhythm.RealValue) < 0.000001 &&
- rhythmInstructions[i].SymbolEnum !== rhythmInstructions[0].SymbolEnum) {
- return true;
- }
- }
- return false;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.setSourceMeasureDuration = function (instrumentReaders, sourceMeasureCounter) {
- var activeRhythm = new fraction_1.Fraction(0, 1);
- var instrumentsMaxTieNoteFractions = [];
- for (var _i = 0, instrumentReaders_3 = instrumentReaders; _i < instrumentReaders_3.length; _i++) {
- var instrumentReader = instrumentReaders_3[_i];
- instrumentsMaxTieNoteFractions.push(instrumentReader.MaxTieNoteFraction);
- var activeRythmMeasure = instrumentReader.ActiveRhythm.Rhythm;
- if (activeRhythm.lt(activeRythmMeasure)) {
- activeRhythm = new fraction_1.Fraction(activeRythmMeasure.Numerator, activeRythmMeasure.Denominator, false);
- }
- }
- var instrumentsDurations = this.currentMeasure.calculateInstrumentsDuration(this.musicSheet, instrumentsMaxTieNoteFractions);
- var maxInstrumentDuration = new fraction_1.Fraction(0, 1);
- for (var _a = 0, instrumentsDurations_1 = instrumentsDurations; _a < instrumentsDurations_1.length; _a++) {
- var instrumentsDuration = instrumentsDurations_1[_a];
- if (maxInstrumentDuration.lt(instrumentsDuration)) {
- maxInstrumentDuration = instrumentsDuration;
- }
- }
- if (fraction_1.Fraction.Equal(maxInstrumentDuration, activeRhythm)) {
- this.checkFractionsForEquivalence(maxInstrumentDuration, activeRhythm);
- }
- else {
- if (maxInstrumentDuration.lt(activeRhythm)) {
- maxInstrumentDuration = this.currentMeasure.reverseCheck(this.musicSheet, maxInstrumentDuration);
- this.checkFractionsForEquivalence(maxInstrumentDuration, activeRhythm);
- }
- }
- this.currentMeasure.ImplicitMeasure = this.checkIfMeasureIsImplicit(maxInstrumentDuration, activeRhythm);
- if (!this.currentMeasure.ImplicitMeasure) {
- sourceMeasureCounter++;
- }
- this.currentMeasure.Duration = maxInstrumentDuration;
- this.currentMeasure.MeasureNumber = sourceMeasureCounter;
- for (var i = 0; i < instrumentsDurations.length; i++) {
- var instrumentsDuration = instrumentsDurations[i];
- if ((this.currentMeasure.ImplicitMeasure && instrumentsDuration !== maxInstrumentDuration) ||
- !fraction_1.Fraction.Equal(instrumentsDuration, activeRhythm) &&
- !this.allInstrumentsHaveSameDuration(instrumentsDurations, maxInstrumentDuration)) {
- var firstStaffIndexOfInstrument = this.musicSheet.getGlobalStaffIndexOfFirstStaff(this.musicSheet.Instruments[i]);
- for (var staffIndex = 0; staffIndex < this.musicSheet.Instruments[i].Staves.length; staffIndex++) {
- if (!this.staffMeasureIsEmpty(firstStaffIndexOfInstrument + staffIndex)) {
- this.currentMeasure.setErrorInStaffMeasure(firstStaffIndexOfInstrument + staffIndex, true);
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/MissingNotesError", "Given Notes don't correspond to measure duration.");
- this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
- }
- }
- }
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.checkFractionsForEquivalence = function (maxInstrumentDuration, activeRhythm) {
- if (activeRhythm.Denominator > maxInstrumentDuration.Denominator) {
- var factor = activeRhythm.Denominator / maxInstrumentDuration.Denominator;
- maxInstrumentDuration.multiplyWithFactor(factor);
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.checkIfMeasureIsImplicit = function (maxInstrumentDuration, activeRhythm) {
- if (this.previousMeasure === undefined && maxInstrumentDuration < activeRhythm) {
- return true;
- }
- if (this.previousMeasure !== undefined) {
- return fraction_1.Fraction.plus(this.previousMeasure.Duration, maxInstrumentDuration).CompareTo(activeRhythm) === 0;
- }
- return false;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.allInstrumentsHaveSameDuration = function (instrumentsDurations, maxInstrumentDuration) {
- var counter = 0;
- for (var idx = 0, len = instrumentsDurations.length; idx < len; ++idx) {
- var instrumentsDuration = instrumentsDurations[idx];
- if (instrumentsDuration === maxInstrumentDuration) {
- counter++;
- }
- }
- return (counter === instrumentsDurations.length && maxInstrumentDuration !== new fraction_1.Fraction(0, 1));
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.staffMeasureIsEmpty = function (index) {
- var counter = 0;
- for (var i = 0; i < this.currentMeasure.VerticalSourceStaffEntryContainers.length; i++) {
- if (this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries[index] === undefined) {
- counter++;
- }
- }
- return (counter === this.currentMeasure.VerticalSourceStaffEntryContainers.length);
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.checkSourceMeasureForundefinedEntries = function () {
- for (var i = this.currentMeasure.VerticalSourceStaffEntryContainers.length - 1; i >= 0; i--) {
- for (var j = this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries.length - 1; j >= 0; j--) {
- var sourceStaffEntry = this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries[j];
- if (sourceStaffEntry !== undefined) {
- for (var k = sourceStaffEntry.VoiceEntries.length - 1; k >= 0; k--) {
- var voiceEntry = sourceStaffEntry.VoiceEntries[k];
- if (voiceEntry.Notes.length === 0) {
- this._removeFromArray(voiceEntry.ParentVoice.VoiceEntries, voiceEntry);
- this._removeFromArray(sourceStaffEntry.VoiceEntries, voiceEntry);
- }
- }
- }
- if (sourceStaffEntry !== undefined && sourceStaffEntry.VoiceEntries.length === 0) {
- this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries[j] = undefined;
- }
- }
- }
- for (var i = this.currentMeasure.VerticalSourceStaffEntryContainers.length - 1; i >= 0; i--) {
- var counter = 0;
- for (var idx = 0, len = this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries.length; idx < len; ++idx) {
- var sourceStaffEntry = this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries[idx];
- if (sourceStaffEntry === undefined) {
- counter++;
- }
- }
- if (counter === this.currentMeasure.VerticalSourceStaffEntryContainers[i].StaffEntries.length) {
- this._removeFromArray(this.currentMeasure.VerticalSourceStaffEntryContainers, this.currentMeasure.VerticalSourceStaffEntryContainers[i]);
- }
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.pushSheetLabels = function (root, filePath) {
- this.readComposer(root);
- this.readTitle(root);
- if (this.musicSheet.Title === undefined || this.musicSheet.Composer === undefined) {
- this.readTitleAndComposerFromCredits(root);
- }
- if (this.musicSheet.Title === undefined) {
- try {
- var barI = Math.max(0, filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
- var filename = filePath.substr(barI);
- var filenameSplits = filename.split(".", 1);
- this.musicSheet.Title = new Label_1.Label(filenameSplits[0]);
- }
- catch (ex) {
- Logging_1.Logging.log("MusicSheetReader.pushSheetLabels: ", ex);
- }
- }
- };
- // Checks whether _elem_ has an attribute with value _val_.
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.presentAttrsWithValue = function (elem, val) {
- for (var _i = 0, _a = elem.attributes(); _i < _a.length; _i++) {
- var attr = _a[_i];
- if (attr.value === val) {
- return true;
- }
- }
- return false;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.readComposer = function (root) {
- var identificationNode = root.element("identification");
- if (identificationNode !== undefined) {
- var creators = identificationNode.elements("creator");
- for (var idx = 0, len = creators.length; idx < len; ++idx) {
- var creator = creators[idx];
- if (creator.hasAttributes) {
- if (this.presentAttrsWithValue(creator, "composer")) {
- this.musicSheet.Composer = new Label_1.Label(this.trimString(creator.value));
- continue;
- }
- if (this.presentAttrsWithValue(creator, "lyricist") || this.presentAttrsWithValue(creator, "poet")) {
- this.musicSheet.Lyricist = new Label_1.Label(this.trimString(creator.value));
- }
- }
- }
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.readTitleAndComposerFromCredits = function (root) {
- var systemYCoordinates = this.computeSystemYCoordinates(root);
- if (systemYCoordinates === 0) {
- return;
- }
- var largestTitleCreditSize = 1;
- var finalTitle = undefined;
- var largestCreditYInfo = 0;
- var finalSubtitle = undefined;
- var possibleTitle = undefined;
- var creditElements = root.elements("credit");
- for (var idx = 0, len = creditElements.length; idx < len; ++idx) {
- var credit = creditElements[idx];
- if (!credit.attribute("page")) {
- return;
- }
- if (credit.attribute("page").value === "1") {
- var creditChild = undefined;
- if (credit !== undefined) {
- creditChild = credit.element("credit-words");
- if (!creditChild.attribute("justify")) {
- break;
- }
- var creditJustify = creditChild.attribute("justify").value;
- var creditY = creditChild.attribute("default-y").value;
- var creditYInfo = parseFloat(creditY);
- if (creditYInfo > systemYCoordinates) {
- if (this.musicSheet.Title === undefined) {
- var creditSize = creditChild.attribute("font-size").value;
- var titleCreditSizeInt = parseFloat(creditSize);
- if (largestTitleCreditSize < titleCreditSizeInt) {
- largestTitleCreditSize = titleCreditSizeInt;
- finalTitle = creditChild.value;
- }
- }
- if (this.musicSheet.Subtitle === undefined) {
- if (creditJustify !== "right" && creditJustify !== "left") {
- if (largestCreditYInfo < creditYInfo) {
- largestCreditYInfo = creditYInfo;
- if (possibleTitle) {
- finalSubtitle = possibleTitle;
- possibleTitle = creditChild.value;
- }
- else {
- possibleTitle = creditChild.value;
- }
- }
- }
- }
- if (!(this.musicSheet.Composer !== undefined && this.musicSheet.Lyricist !== undefined)) {
- switch (creditJustify) {
- case "right":
- this.musicSheet.Composer = new Label_1.Label(this.trimString(creditChild.value));
- break;
- case "left":
- this.musicSheet.Lyricist = new Label_1.Label(this.trimString(creditChild.value));
- break;
- default:
- break;
- }
- }
- }
- }
- }
- }
- if (this.musicSheet.Title === undefined && finalTitle) {
- this.musicSheet.Title = new Label_1.Label(this.trimString(finalTitle));
- }
- if (this.musicSheet.Subtitle === undefined && finalSubtitle) {
- this.musicSheet.Subtitle = new Label_1.Label(this.trimString(finalSubtitle));
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.computeSystemYCoordinates = function (root) {
- if (root.element("defaults") === undefined) {
- return 0;
- }
- var paperHeight = 0;
- var topSystemDistance = 0;
- var defi = root.element("defaults").element("page-layout").element("page-height").value;
- paperHeight = parseFloat(defi);
- var found = false;
- var parts = root.elements("part");
- for (var idx = 0, len = parts.length; idx < len; ++idx) {
- var measures = parts[idx].elements("measure");
- for (var idx2 = 0, len2 = measures.length; idx2 < len2; ++idx2) {
- var measure = measures[idx2];
- if (measure.element("print") !== undefined) {
- var systemLayouts = measure.element("print").elements("system-layout");
- for (var idx3 = 0, len3 = systemLayouts.length; idx3 < len3; ++idx3) {
- var syslab = systemLayouts[idx3];
- if (syslab.element("top-system-distance") !== undefined) {
- var topSystemDistanceString = syslab.element("top-system-distance").value;
- topSystemDistance = parseFloat(topSystemDistanceString);
- found = true;
- break;
- }
- }
- break;
- }
- }
- if (found) {
- break;
- }
- }
- if (root.element("defaults").element("system-layout") !== undefined) {
- var syslay = root.element("defaults").element("system-layout");
- if (syslay.element("top-system-distance") !== undefined) {
- var topSystemDistanceString = root.element("defaults").element("system-layout").element("top-system-distance").value;
- topSystemDistance = parseFloat(topSystemDistanceString);
- }
- }
- if (topSystemDistance === 0) {
- return 0;
- }
- return paperHeight - topSystemDistance;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.readTitle = function (root) {
- var titleNode = root.element("work");
- var titleNodeChild = undefined;
- if (titleNode !== undefined) {
- titleNodeChild = titleNode.element("work-title");
- if (titleNodeChild !== undefined && titleNodeChild.value) {
- this.musicSheet.Title = new Label_1.Label(this.trimString(titleNodeChild.value));
- }
- }
- var movementNode = root.element("movement-title");
- var finalSubTitle = "";
- if (movementNode !== undefined) {
- if (this.musicSheet.Title === undefined) {
- this.musicSheet.Title = new Label_1.Label(this.trimString(movementNode.value));
- }
- else {
- finalSubTitle = this.trimString(movementNode.value);
- }
- }
- if (titleNode !== undefined) {
- var subtitleNodeChild = titleNode.element("work-number");
- if (subtitleNodeChild !== undefined) {
- var workNumber = subtitleNodeChild.value;
- if (workNumber) {
- if (finalSubTitle) {
- finalSubTitle = workNumber;
- }
- else {
- finalSubTitle = finalSubTitle + ", " + workNumber;
- }
- }
- }
- }
- if (finalSubTitle) {
- this.musicSheet.Subtitle = new Label_1.Label(finalSubTitle);
- }
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.createInstrumentGroups = function (entryList) {
- var instrumentId = 0;
- var instrumentDict = {};
- var currentGroup;
- try {
- var entryArray = entryList;
- for (var idx = 0, len = entryArray.length; idx < len; ++idx) {
- var node = entryArray[idx];
- if (node.name === "score-part") {
- var instrIdString = node.attribute("id").value;
- var instrument = new Instrument_1.Instrument(instrumentId, instrIdString, this.musicSheet, currentGroup);
- instrumentId++;
- var partElements = node.elements();
- for (var idx2 = 0, len2 = partElements.length; idx2 < len2; ++idx2) {
- var partElement = partElements[idx2];
- try {
- if (partElement.name === "part-name") {
- instrument.Name = partElement.value;
- }
- else if (partElement.name === "score-instrument") {
- var subInstrument = new SubInstrument_1.SubInstrument(instrument);
- subInstrument.idString = partElement.firstAttribute.value;
- instrument.SubInstruments.push(subInstrument);
- var subElement = partElement.element("instrument-name");
- if (subElement !== undefined) {
- subInstrument.name = subElement.value;
- subInstrument.setMidiInstrument(subElement.value);
- }
- }
- else if (partElement.name === "midi-instrument") {
- var subInstrument = instrument.getSubInstrument(partElement.firstAttribute.value);
- for (var idx3 = 0, len3 = instrument.SubInstruments.length; idx3 < len3; ++idx3) {
- var subInstr = instrument.SubInstruments[idx3];
- if (subInstr.idString === partElement.value) {
- subInstrument = subInstr;
- break;
- }
- }
- var instrumentElements = partElement.elements();
- for (var idx3 = 0, len3 = instrumentElements.length; idx3 < len3; ++idx3) {
- var instrumentElement = instrumentElements[idx3];
- try {
- if (instrumentElement.name === "midi-channel") {
- if (parseInt(instrumentElement.value, 10) === 10) {
- instrument.MidiInstrumentId = ClefInstruction_1.MidiInstrument.Percussion;
- }
- }
- else if (instrumentElement.name === "midi-program") {
- if (instrument.SubInstruments.length > 0 && instrument.MidiInstrumentId !== ClefInstruction_1.MidiInstrument.Percussion) {
- subInstrument.midiInstrumentID = Math.max(0, parseInt(instrumentElement.value, 10) - 1);
- }
- }
- else if (instrumentElement.name === "midi-unpitched") {
- subInstrument.fixedKey = Math.max(0, parseInt(instrumentElement.value, 10));
- }
- else if (instrumentElement.name === "volume") {
- try {
- var result = parseFloat(instrumentElement.value);
- subInstrument.volume = result / 127.0;
- }
- catch (ex) {
- Logging_1.Logging.debug("ExpressionReader.readExpressionParameters", "read volume", ex);
- }
- }
- else if (instrumentElement.name === "pan") {
- try {
- var result = parseFloat(instrumentElement.value);
- subInstrument.pan = result / 64.0;
- }
- catch (ex) {
- Logging_1.Logging.debug("ExpressionReader.readExpressionParameters", "read pan", ex);
- }
- }
- }
- catch (ex) {
- Logging_1.Logging.log("MusicSheetReader.createInstrumentGroups midi settings: ", ex);
- }
- }
- }
- }
- catch (ex) {
- Logging_1.Logging.log("MusicSheetReader.createInstrumentGroups: ", ex);
- }
- }
- if (instrument.SubInstruments.length === 0) {
- var subInstrument = new SubInstrument_1.SubInstrument(instrument);
- instrument.SubInstruments.push(subInstrument);
- }
- instrumentDict[instrIdString] = instrument;
- if (currentGroup !== undefined) {
- currentGroup.InstrumentalGroups.push(instrument);
- this.musicSheet.Instruments.push(instrument);
- }
- else {
- this.musicSheet.InstrumentalGroups.push(instrument);
- this.musicSheet.Instruments.push(instrument);
- }
- }
- else {
- if ((node.name === "part-group") && (node.attribute("type").value === "start")) {
- var iG = new InstrumentalGroup_1.InstrumentalGroup("group", this.musicSheet, currentGroup);
- if (currentGroup !== undefined) {
- currentGroup.InstrumentalGroups.push(iG);
- }
- else {
- this.musicSheet.InstrumentalGroups.push(iG);
- }
- currentGroup = iG;
- }
- else {
- if ((node.name === "part-group") && (node.attribute("type").value === "stop")) {
- if (currentGroup !== undefined) {
- if (currentGroup.InstrumentalGroups.length === 1) {
- var instr = currentGroup.InstrumentalGroups[0];
- if (currentGroup.Parent !== undefined) {
- currentGroup.Parent.InstrumentalGroups.push(instr);
- this._removeFromArray(currentGroup.Parent.InstrumentalGroups, currentGroup);
- }
- else {
- this.musicSheet.InstrumentalGroups.push(instr);
- this._removeFromArray(this.musicSheet.InstrumentalGroups, currentGroup);
- }
- }
- currentGroup = currentGroup.Parent;
- }
- }
- }
- }
- }
- }
- catch (e) {
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/InstrumentError", "Error while reading Instruments");
- throw new Exceptions_1.MusicSheetReadingException(errorMsg, e);
- }
- for (var idx = 0, len = this.musicSheet.Instruments.length; idx < len; ++idx) {
- var instrument = this.musicSheet.Instruments[idx];
- if (!instrument.Name) {
- instrument.Name = "Instr. " + instrument.IdString;
- }
- }
- return instrumentDict;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.getCompleteNumberOfStavesFromXml = function (partInst) {
- var num = 0;
- for (var _i = 0, partInst_2 = partInst; _i < partInst_2.length; _i++) {
- var partNode = partInst_2[_i];
- var xmlMeasureList = partNode.elements("measure");
- if (xmlMeasureList.length > 0) {
- var xmlMeasure = xmlMeasureList[0];
- if (xmlMeasure !== undefined) {
- var stavesNode = xmlMeasure.element("attributes");
- if (stavesNode !== undefined) {
- stavesNode = stavesNode.element("staves");
- }
- if (stavesNode === undefined) {
- num++;
- }
- else {
- num += parseInt(stavesNode.value, 10);
- }
- }
- }
- }
- if (isNaN(num) || num <= 0) {
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/StaffError", "Invalid number of staves.");
- throw new Exceptions_1.MusicSheetReadingException(errorMsg);
- }
- return num;
- };
- MusicSheetReader /*implements IMusicSheetReader*/.prototype.getInstrumentNumberOfStavesFromXml = function (partNode) {
- var num = 0;
- var xmlMeasure = partNode.element("measure");
- if (xmlMeasure !== undefined) {
- var attributes = xmlMeasure.element("attributes");
- var staves = undefined;
- if (attributes !== undefined) {
- staves = attributes.element("staves");
- }
- if (attributes === undefined || staves === undefined) {
- num = 1;
- }
- else {
- num = parseInt(staves.value, 10);
- }
- }
- if (isNaN(num) || num <= 0) {
- var errorMsg = ITextTranslation_1.ITextTranslation.translateText("ReaderErrorMessages/StaffError", "Invalid number of Staves.");
- throw new Exceptions_1.MusicSheetReadingException(errorMsg);
- }
- return num;
- };
- return MusicSheetReader /*implements IMusicSheetReader*/;
- }());
- exports.MusicSheetReader /*implements IMusicSheetReader*/ = MusicSheetReader /*implements IMusicSheetReader*/;
|