InstrumentReader.ts 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. import {Instrument} from "../Instrument";
  2. import {MusicSheet} from "../MusicSheet";
  3. import {VoiceGenerator} from "./VoiceGenerator";
  4. import {Staff} from "../VoiceData/Staff";
  5. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  6. import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
  7. import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
  8. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  9. import {RhythmInstruction} from "../VoiceData/Instructions/RhythmInstruction";
  10. import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
  11. import {Fraction} from "../../Common/DataObjects/fraction";
  12. import {IXmlElement} from "../../Common/FileIO/Xml";
  13. import {ITextTranslation} from "../Interfaces/ITextTranslation";
  14. import {MusicSheetReadingException} from "../Exceptions";
  15. import {ClefEnum} from "../VoiceData/Instructions/ClefInstruction";
  16. import {RhythmSymbolEnum} from "../VoiceData/Instructions/RhythmInstruction";
  17. import {KeyEnum} from "../VoiceData/Instructions/KeyInstruction";
  18. import {IXmlAttribute} from "../../Common/FileIO/Xml";
  19. import {ChordSymbolContainer} from "../VoiceData/ChordSymbolContainer";
  20. import {Logging} from "../../Common/logging";
  21. import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
  22. // FIXME: The following classes are missing
  23. //type repetitionInstructionReader = any;
  24. //type ChordSymbolContainer = any;
  25. //type SlurReader = any;
  26. //type RepetitionInstructionReader = any;
  27. //type ExpressionReader = any;
  28. //declare class MusicSymbolModuleFactory {
  29. // public static createSlurReader(x: any): any;
  30. // public static createExpressionGenerator(musicSheet: MusicSheet, instrument: Instrument, n: number);
  31. //}
  32. //
  33. //class MetronomeReader {
  34. // public static addMetronomeSettings(xmlNode: IXmlElement, musicSheet: MusicSheet): void { }
  35. // public static readMetronomeInstructions(xmlNode: IXmlElement, musicSheet: MusicSheet, currentXmlMeasureIndex: number): void { }
  36. // public static readTempoInstruction(soundNode: IXmlElement, musicSheet: MusicSheet, currentXmlMeasureIndex: number): void { }
  37. //}
  38. //
  39. //class ChordSymbolReader {
  40. // public static readChordSymbol(xmlNode:IXmlElement, musicSheet:MusicSheet, activeKey:any): void {
  41. // }
  42. //}
  43. type repetitionInstructionReader = any;
  44. export class InstrumentReader {
  45. constructor(repetitionInstructionReader: repetitionInstructionReader, xmlMeasureList: IXmlElement[], instrument: Instrument) {
  46. // (*) this.repetitionInstructionReader = repetitionInstructionReader;
  47. this.xmlMeasureList = xmlMeasureList;
  48. this.musicSheet = instrument.GetMusicSheet;
  49. this.instrument = instrument;
  50. this.activeClefs = new Array(instrument.Staves.length);
  51. this.activeClefsHaveBeenInitialized = new Array(instrument.Staves.length);
  52. for (let i: number = 0; i < instrument.Staves.length; i++) {
  53. this.activeClefsHaveBeenInitialized[i] = false;
  54. }
  55. // FIXME createExpressionGenerators(instrument.Staves.length);
  56. // (*) this.slurReader = MusicSymbolModuleFactory.createSlurReader(this.musicSheet);
  57. }
  58. // (*) private repetitionInstructionReader: RepetitionInstructionReader;
  59. private xmlMeasureList: IXmlElement[];
  60. private musicSheet: MusicSheet;
  61. private slurReader: any; // (*) SlurReader;
  62. private instrument: Instrument;
  63. private voiceGeneratorsDict: { [n: number]: VoiceGenerator; } = {};
  64. private staffMainVoiceGeneratorDict: { [staffId: number]: VoiceGenerator } = {};
  65. private inSourceMeasureInstrumentIndex: number;
  66. private divisions: number = 0;
  67. private currentMeasure: SourceMeasure;
  68. private previousMeasure: SourceMeasure;
  69. private currentXmlMeasureIndex: number = 0;
  70. private currentStaff: Staff;
  71. private currentStaffEntry: SourceStaffEntry;
  72. private activeClefs: ClefInstruction[];
  73. private activeKey: KeyInstruction;
  74. private activeRhythm: RhythmInstruction;
  75. private activeClefsHaveBeenInitialized: boolean[];
  76. private activeKeyHasBeenInitialized: boolean = false;
  77. private abstractInstructions: { [n: number]: AbstractNotationInstruction; } = {};
  78. private openChordSymbolContainer: ChordSymbolContainer;
  79. // (*) private expressionReaders: ExpressionReader[];
  80. private currentVoiceGenerator: VoiceGenerator;
  81. //private openSlurDict: { [n: number]: Slur; } = {};
  82. private maxTieNoteFraction: Fraction;
  83. public get ActiveKey(): KeyInstruction {
  84. return this.activeKey;
  85. }
  86. public get MaxTieNoteFraction(): Fraction {
  87. return this.maxTieNoteFraction;
  88. }
  89. public get ActiveRhythm(): RhythmInstruction {
  90. return this.activeRhythm;
  91. }
  92. public set ActiveRhythm(value: RhythmInstruction) {
  93. this.activeRhythm = value;
  94. }
  95. public readNextXmlMeasure(currentMeasure: SourceMeasure, measureStartAbsoluteTimestamp: Fraction, guitarPro: boolean): boolean {
  96. if (this.currentXmlMeasureIndex >= this.xmlMeasureList.length) {
  97. return false;
  98. }
  99. this.currentMeasure = currentMeasure;
  100. this.inSourceMeasureInstrumentIndex = this.musicSheet.getGlobalStaffIndexOfFirstStaff(this.instrument);
  101. // (*) if (this.repetitionInstructionReader !== undefined) {
  102. // this.repetitionInstructionReader.prepareReadingMeasure(currentMeasure, this.currentXmlMeasureIndex);
  103. //}
  104. let currentFraction: Fraction = new Fraction(0, 1);
  105. let previousFraction: Fraction = new Fraction(0, 1);
  106. let divisionsException: boolean = false;
  107. this.maxTieNoteFraction = new Fraction(0, 1);
  108. let lastNoteWasGrace: boolean = false;
  109. try {
  110. let xmlMeasureListArr: IXmlElement[] = this.xmlMeasureList[this.currentXmlMeasureIndex].elements();
  111. for (let xmlNode of xmlMeasureListArr) {
  112. if (xmlNode.name === "note") {
  113. if (xmlNode.hasAttributes && xmlNode.attribute("print-object") && xmlNode.attribute("print-spacing")) {
  114. continue;
  115. }
  116. let noteStaff: number = 1;
  117. if (this.instrument.Staves.length > 1) {
  118. if (xmlNode.element("staff") !== undefined) {
  119. noteStaff = parseInt(xmlNode.element("staff").value, 10);
  120. if (isNaN(noteStaff)) {
  121. Logging.debug("InstrumentReader.readNextXmlMeasure.get staff number");
  122. noteStaff = 1;
  123. }
  124. }
  125. }
  126. this.currentStaff = this.instrument.Staves[noteStaff - 1];
  127. let isChord: boolean = xmlNode.element("chord") !== undefined;
  128. if (xmlNode.element("voice") !== undefined) {
  129. let noteVoice: number = parseInt(xmlNode.element("voice").value, 10);
  130. this.currentVoiceGenerator = this.getOrCreateVoiceGenerator(noteVoice, noteStaff - 1);
  131. } else {
  132. if (!isChord || this.currentVoiceGenerator === undefined) {
  133. this.currentVoiceGenerator = this.getOrCreateVoiceGenerator(1, noteStaff - 1);
  134. }
  135. }
  136. let noteDivisions: number = 0;
  137. let noteDuration: Fraction = new Fraction(0, 1);
  138. let isTuplet: boolean = false;
  139. if (xmlNode.element("duration") !== undefined) {
  140. noteDivisions = parseInt(xmlNode.element("duration").value, 10);
  141. if (!isNaN(noteDivisions)) {
  142. noteDuration = new Fraction(noteDivisions, 4 * this.divisions);
  143. if (noteDivisions === 0) {
  144. noteDuration = this.getNoteDurationFromTypeNode(xmlNode);
  145. }
  146. if (xmlNode.element("time-modification") !== undefined) {
  147. noteDuration = this.getNoteDurationForTuplet(xmlNode);
  148. isTuplet = true;
  149. }
  150. } else {
  151. let errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/NoteDurationError", "Invalid Note Duration.");
  152. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  153. Logging.debug("InstrumentReader.readNextXmlMeasure", errorMsg);
  154. continue;
  155. }
  156. }
  157. let restNote: boolean = xmlNode.element("rest") !== undefined;
  158. Logging.log("New note found!", noteDivisions, noteDuration.toString(), restNote);
  159. let isGraceNote: boolean = xmlNode.element("grace") !== undefined || noteDivisions === 0 || isChord && lastNoteWasGrace;
  160. let musicTimestamp: Fraction = currentFraction.clone();
  161. if (isChord) {
  162. musicTimestamp = previousFraction.clone();
  163. }
  164. this.currentStaffEntry = this.currentMeasure.findOrCreateStaffEntry(
  165. musicTimestamp,
  166. this.inSourceMeasureInstrumentIndex + noteStaff - 1,
  167. this.currentStaff
  168. ).staffEntry;
  169. Logging.log("currentStaffEntry", this.currentStaffEntry, this.currentMeasure.VerticalSourceStaffEntryContainers.length);
  170. if (!this.currentVoiceGenerator.hasVoiceEntry() || (!isChord && !isGraceNote && !lastNoteWasGrace) || (!lastNoteWasGrace && isGraceNote)) {
  171. this.currentVoiceGenerator.createVoiceEntry(musicTimestamp, this.currentStaffEntry, !restNote);
  172. }
  173. if (!isGraceNote && !isChord) {
  174. previousFraction = currentFraction.clone();
  175. currentFraction.Add(noteDuration);
  176. }
  177. if (
  178. isChord &&
  179. this.currentStaffEntry !== undefined &&
  180. this.currentStaffEntry.ParentStaff !== this.currentStaff
  181. ) {
  182. this.currentStaffEntry = this.currentVoiceGenerator.checkForStaffEntryLink(
  183. this.inSourceMeasureInstrumentIndex + noteStaff - 1, this.currentStaff, this.currentStaffEntry, this.currentMeasure
  184. );
  185. }
  186. let beginOfMeasure: boolean = (
  187. this.currentStaffEntry !== undefined &&
  188. this.currentStaffEntry.Timestamp !== undefined &&
  189. this.currentStaffEntry.Timestamp.Equals(new Fraction(0, 1)) &&
  190. !this.currentStaffEntry.hasNotes()
  191. );
  192. this.saveAbstractInstructionList(this.instrument.Staves.length, beginOfMeasure);
  193. if (this.openChordSymbolContainer !== undefined) {
  194. this.currentStaffEntry.ChordContainer = this.openChordSymbolContainer;
  195. this.openChordSymbolContainer = undefined;
  196. }
  197. if (this.activeRhythm !== undefined) {
  198. // (*) this.musicSheet.SheetPlaybackSetting.Rhythm = this.activeRhythm.Rhythm;
  199. }
  200. if (isTuplet) {
  201. this.currentVoiceGenerator.read(
  202. xmlNode, noteDuration.Numerator,
  203. noteDuration.Denominator, restNote, isGraceNote,
  204. this.currentStaffEntry, this.currentMeasure,
  205. measureStartAbsoluteTimestamp,
  206. this.maxTieNoteFraction, isChord, guitarPro
  207. );
  208. } else {
  209. this.currentVoiceGenerator.read(
  210. xmlNode, noteDivisions, 4 * this.divisions,
  211. restNote, isGraceNote, this.currentStaffEntry,
  212. this.currentMeasure, measureStartAbsoluteTimestamp,
  213. this.maxTieNoteFraction, isChord, guitarPro
  214. );
  215. }
  216. let notationsNode: IXmlElement = xmlNode.element("notations");
  217. if (notationsNode !== undefined && notationsNode.element("dynamics") !== undefined) {
  218. // (*) let expressionReader: ExpressionReader = this.expressionReaders[this.readExpressionStaffNumber(xmlNode) - 1];
  219. //if (expressionReader !== undefined) {
  220. // expressionReader.readExpressionParameters(
  221. // xmlNode, this.instrument, this.divisions, currentFraction, previousFraction, this.currentMeasure.MeasureNumber, false
  222. // );
  223. // expressionReader.read(
  224. // xmlNode, this.currentMeasure, previousFraction
  225. // );
  226. //}
  227. }
  228. lastNoteWasGrace = isGraceNote;
  229. } else if (xmlNode.name === "attributes") {
  230. let divisionsNode: IXmlElement = xmlNode.element("divisions");
  231. if (divisionsNode !== undefined) {
  232. this.divisions = parseInt(divisionsNode.value, 10);
  233. if (isNaN(this.divisions)) {
  234. let errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/DivisionError", "Invalid divisions value at Instrument: ");
  235. Logging.debug("InstrumentReader.readNextXmlMeasure", errorMsg);
  236. this.divisions = this.readDivisionsFromNotes();
  237. if (this.divisions > 0) {
  238. this.musicSheet.SheetErrors.push(errorMsg + this.instrument.Name);
  239. } else {
  240. divisionsException = true;
  241. throw new MusicSheetReadingException(errorMsg + this.instrument.Name);
  242. }
  243. }
  244. }
  245. if (
  246. xmlNode.element("divisions") === undefined &&
  247. this.divisions === 0 &&
  248. this.currentXmlMeasureIndex === 0
  249. ) {
  250. let errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/DivisionError", "Invalid divisions value at Instrument: ");
  251. this.divisions = this.readDivisionsFromNotes();
  252. if (this.divisions > 0) {
  253. this.musicSheet.SheetErrors.push(errorMsg + this.instrument.Name);
  254. } else {
  255. divisionsException = true;
  256. throw new MusicSheetReadingException(errorMsg + this.instrument.Name);
  257. }
  258. }
  259. this.addAbstractInstruction(xmlNode, guitarPro);
  260. if (currentFraction.Equals(new Fraction(0, 1)) && this.isAttributesNodeAtBeginOfMeasure(this.xmlMeasureList[this.currentXmlMeasureIndex], xmlNode)) {
  261. this.saveAbstractInstructionList(this.instrument.Staves.length, true);
  262. }
  263. if (this.isAttributesNodeAtEndOfMeasure(this.xmlMeasureList[this.currentXmlMeasureIndex], xmlNode)) {
  264. this.saveClefInstructionAtEndOfMeasure();
  265. }
  266. } else if (xmlNode.name === "forward") {
  267. let forFraction: number = parseInt(xmlNode.element("duration").value, 10);
  268. currentFraction.Add(new Fraction(forFraction, 4 * this.divisions));
  269. } else if (xmlNode.name === "backup") {
  270. let backFraction: number = parseInt(xmlNode.element("duration").value, 10);
  271. currentFraction.Sub(new Fraction(backFraction, 4 * this.divisions));
  272. if (currentFraction.Numerator < 0) {
  273. currentFraction = new Fraction(0, 1);
  274. }
  275. previousFraction.Sub(new Fraction(backFraction, 4 * this.divisions));
  276. if (previousFraction.Numerator < 0) {
  277. previousFraction = new Fraction(0, 1);
  278. }
  279. } else if (xmlNode.name === "direction") {
  280. // unused let directionTypeNode: IXmlElement = xmlNode.element("direction-type");
  281. // (*) MetronomeReader.readMetronomeInstructions(xmlNode, this.musicSheet, this.currentXmlMeasureIndex);
  282. let relativePositionInMeasure: number = Math.min(1, currentFraction.RealValue);
  283. if (this.activeRhythm !== undefined && this.activeRhythm.Rhythm !== undefined) {
  284. relativePositionInMeasure /= this.activeRhythm.Rhythm.RealValue;
  285. }
  286. // unused: let handeled: boolean = false;
  287. // (*) if (this.repetitionInstructionReader !== undefined) {
  288. // handeled = this.repetitionInstructionReader.handleRepetitionInstructionsFromWordsOrSymbols(directionTypeNode, relativePositionInMeasure);
  289. //}
  290. //if (!handeled) {
  291. // let expressionReader: ExpressionReader = this.expressionReaders[0];
  292. // let staffIndex: number = this.readExpressionStaffNumber(xmlNode) - 1;
  293. // if (staffIndex < this.expressionReaders.length) {
  294. // expressionReader = this.expressionReaders[staffIndex];
  295. // }
  296. // if (expressionReader !== undefined) {
  297. // if (directionTypeNode.element("octave-shift") !== undefined) {
  298. // expressionReader.readExpressionParameters(
  299. // xmlNode, this.instrument, this.divisions, currentFraction, previousFraction, this.currentMeasure.MeasureNumber, true
  300. // );
  301. // expressionReader.addOctaveShift(xmlNode, this.currentMeasure, previousFraction.clone());
  302. // }
  303. // expressionReader.readExpressionParameters(
  304. // xmlNode, this.instrument, this.divisions, currentFraction, previousFraction, this.currentMeasure.MeasureNumber, false
  305. // );
  306. // expressionReader.read(xmlNode, this.currentMeasure, currentFraction);
  307. // }
  308. //}
  309. } else if (xmlNode.name === "barline") {
  310. // (*)
  311. //if (this.repetitionInstructionReader !== undefined) {
  312. // let measureEndsSystem: boolean = false;
  313. // this.repetitionInstructionReader.handleLineRepetitionInstructions(xmlNode, measureEndsSystem);
  314. // if (measureEndsSystem) {
  315. // this.currentMeasure.BreakSystemAfter = true;
  316. // this.currentMeasure.endsPiece = true;
  317. // }
  318. //}
  319. } else if (xmlNode.name === "sound") {
  320. // (*) MetronomeReader.readTempoInstruction(xmlNode, this.musicSheet, this.currentXmlMeasureIndex);
  321. } else if (xmlNode.name === "harmony") {
  322. // (*) this.openChordSymbolContainer = ChordSymbolReader.readChordSymbol(xmlNode, this.musicSheet, this.activeKey);
  323. }
  324. }
  325. for (let j in this.voiceGeneratorsDict) {
  326. if (this.voiceGeneratorsDict.hasOwnProperty(j)) {
  327. let voiceGenerator: VoiceGenerator = this.voiceGeneratorsDict[j];
  328. voiceGenerator.checkForOpenBeam();
  329. voiceGenerator.checkForOpenGraceNotes();
  330. }
  331. }
  332. if (this.currentXmlMeasureIndex === this.xmlMeasureList.length - 1) {
  333. for (let i: number = 0; i < this.instrument.Staves.length; i++) {
  334. if (!this.activeClefsHaveBeenInitialized[i]) {
  335. this.createDefaultClefInstruction(this.musicSheet.getGlobalStaffIndexOfFirstStaff(this.instrument) + i);
  336. }}
  337. if (!this.activeKeyHasBeenInitialized) {
  338. this.createDefaultKeyInstruction();
  339. }
  340. // (*)
  341. //for (let i: number = 0; i < this.expressionReaders.length; i++) {
  342. // let reader: ExpressionReader = this.expressionReaders[i];
  343. // if (reader !== undefined) {
  344. // reader.checkForOpenExpressions(this.currentMeasure, currentFraction);
  345. // }
  346. //}
  347. }
  348. } catch (e) {
  349. if (divisionsException) {
  350. throw new MusicSheetReadingException(e.Message);
  351. }
  352. let errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/MeasureError", "Error while reading Measure.");
  353. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  354. Logging.debug("InstrumentReader.readNextXmlMeasure", errorMsg, e);
  355. }
  356. this.previousMeasure = this.currentMeasure;
  357. this.currentXmlMeasureIndex += 1;
  358. return true;
  359. }
  360. public doCalculationsAfterDurationHasBeenSet(): void {
  361. for (let j in this.voiceGeneratorsDict) {
  362. if (this.voiceGeneratorsDict.hasOwnProperty(j)) {
  363. this.voiceGeneratorsDict[j].checkOpenTies();
  364. }
  365. }
  366. }
  367. private getOrCreateVoiceGenerator(voiceId: number, staffId: number): VoiceGenerator {
  368. let staff: Staff = this.instrument.Staves[staffId];
  369. let voiceGenerator: VoiceGenerator = this.voiceGeneratorsDict[voiceId];
  370. if (voiceGenerator !== undefined) {
  371. if (staff.Voices.indexOf(voiceGenerator.GetVoice) === -1) {
  372. staff.Voices.push(voiceGenerator.GetVoice);
  373. }
  374. } else {
  375. let mainVoiceGenerator: VoiceGenerator = this.staffMainVoiceGeneratorDict[staffId];
  376. if (mainVoiceGenerator !== undefined) {
  377. voiceGenerator = new VoiceGenerator(this.instrument, voiceId, this.slurReader, mainVoiceGenerator.GetVoice);
  378. staff.Voices.push(voiceGenerator.GetVoice);
  379. this.voiceGeneratorsDict[voiceId] = voiceGenerator;
  380. } else {
  381. voiceGenerator = new VoiceGenerator(this.instrument, voiceId, this.slurReader);
  382. staff.Voices.push(voiceGenerator.GetVoice);
  383. this.voiceGeneratorsDict[voiceId] = voiceGenerator;
  384. this.staffMainVoiceGeneratorDict[staffId] = voiceGenerator;
  385. }
  386. }
  387. return voiceGenerator;
  388. }
  389. //private createExpressionGenerators(numberOfStaves: number): void {
  390. // // (*)
  391. // //this.expressionReaders = new Array(numberOfStaves);
  392. // //for (let i: number = 0; i < numberOfStaves; i++) {
  393. // // this.expressionReaders[i] = MusicSymbolModuleFactory.createExpressionGenerator(this.musicSheet, this.instrument, i + 1);
  394. // //}
  395. //}
  396. private createDefaultClefInstruction(staffIndex: number): void {
  397. let first: SourceMeasure;
  398. if (this.musicSheet.SourceMeasures.length > 0) {
  399. first = this.musicSheet.SourceMeasures[0];
  400. } else {
  401. first = this.currentMeasure;
  402. }
  403. let clefInstruction: ClefInstruction = new ClefInstruction(ClefEnum.G, 0, 2);
  404. let firstStaffEntry: SourceStaffEntry;
  405. if (first.FirstInstructionsStaffEntries[staffIndex] === undefined) {
  406. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  407. first.FirstInstructionsStaffEntries[staffIndex] = firstStaffEntry;
  408. } else {
  409. firstStaffEntry = first.FirstInstructionsStaffEntries[staffIndex];
  410. firstStaffEntry.removeFirstInstructionOfTypeClefInstruction();
  411. }
  412. clefInstruction.Parent = firstStaffEntry;
  413. firstStaffEntry.Instructions.splice(0, 0, clefInstruction);
  414. }
  415. private createDefaultKeyInstruction(): void {
  416. let first: SourceMeasure;
  417. if (this.musicSheet.SourceMeasures.length > 0) {
  418. first = this.musicSheet.SourceMeasures[0];
  419. } else {
  420. first = this.currentMeasure;
  421. }
  422. let keyInstruction: KeyInstruction = new KeyInstruction(undefined, 0, KeyEnum.major);
  423. for (let j: number = this.inSourceMeasureInstrumentIndex; j < this.inSourceMeasureInstrumentIndex + this.instrument.Staves.length; j++) {
  424. if (first.FirstInstructionsStaffEntries[j] === undefined) {
  425. let firstStaffEntry: SourceStaffEntry = new SourceStaffEntry(undefined, undefined);
  426. first.FirstInstructionsStaffEntries[j] = firstStaffEntry;
  427. keyInstruction.Parent = firstStaffEntry;
  428. firstStaffEntry.Instructions.push(keyInstruction);
  429. } else {
  430. let firstStaffEntry: SourceStaffEntry = first.FirstInstructionsStaffEntries[j];
  431. keyInstruction.Parent = firstStaffEntry;
  432. firstStaffEntry.removeFirstInstructionOfTypeKeyInstruction();
  433. if (firstStaffEntry.Instructions[0] instanceof ClefInstruction) {
  434. firstStaffEntry.Instructions.splice(1, 0, keyInstruction);
  435. } else {
  436. firstStaffEntry.Instructions.splice(0, 0, keyInstruction);
  437. }
  438. }
  439. }
  440. }
  441. private isAttributesNodeAtBeginOfMeasure(parentNode: IXmlElement, attributesNode: IXmlElement): boolean {
  442. let children: IXmlElement[] = parentNode.elements();
  443. let attributesNodeIndex: number = children.indexOf(attributesNode); // FIXME | 0
  444. if (attributesNodeIndex > 0 && children[attributesNodeIndex - 1].name === "backup") {
  445. return true;
  446. }
  447. let firstNoteNodeIndex: number = -1;
  448. for (let i: number = 0; i < children.length; i++) {
  449. if (children[i].name === "note") {
  450. firstNoteNodeIndex = i;
  451. break;
  452. }
  453. }
  454. return (attributesNodeIndex < firstNoteNodeIndex && firstNoteNodeIndex > 0) || (firstNoteNodeIndex < 0);
  455. }
  456. private isAttributesNodeAtEndOfMeasure(parentNode: IXmlElement, attributesNode: IXmlElement): boolean {
  457. let childs: IXmlElement[] = parentNode.elements().slice();
  458. let attributesNodeIndex: number = 0;
  459. for (let i: number = 0; i < childs.length; i++) {
  460. if (childs[i] === attributesNode) {
  461. attributesNodeIndex = i;
  462. break;
  463. }
  464. }
  465. let nextNoteNodeIndex: number = 0;
  466. for (let i: number = attributesNodeIndex; i < childs.length; i++) {
  467. if (childs[i].name === "note") {
  468. nextNoteNodeIndex = i;
  469. break;
  470. }
  471. }
  472. return attributesNodeIndex > nextNoteNodeIndex;
  473. }
  474. private getNoteDurationFromTypeNode(xmlNode: IXmlElement): Fraction {
  475. if (xmlNode.element("type") !== undefined) {
  476. let typeNode: IXmlElement = xmlNode.element("type");
  477. if (typeNode !== undefined) {
  478. let type: string = typeNode.value;
  479. return this.currentVoiceGenerator.getNoteDurationFromType(type);
  480. }
  481. }
  482. return new Fraction(0, 4 * this.divisions);
  483. }
  484. private addAbstractInstruction(node: IXmlElement, guitarPro: boolean): void {
  485. if (node.element("divisions") !== undefined) {
  486. if (node.elements().length === 1) { return; }
  487. }
  488. let transposeNode: IXmlElement = node.element("transpose");
  489. if (transposeNode !== undefined) {
  490. let chromaticNode: IXmlElement = transposeNode.element("chromatic");
  491. if (chromaticNode !== undefined) {
  492. this.instrument.PlaybackTranspose = parseInt(chromaticNode.value, 10);
  493. }
  494. }
  495. let clefList: IXmlElement[] = node.elements("clef");
  496. let errorMsg: string;
  497. if (clefList.length > 0) {
  498. for (let idx: number = 0, len: number = clefList.length; idx < len; ++idx) {
  499. let nodeList: IXmlElement = clefList[idx];
  500. let clefEnum: ClefEnum = ClefEnum.G;
  501. let line: number = 2;
  502. let staffNumber: number = 1;
  503. let clefOctaveOffset: number = 0;
  504. let lineNode: IXmlElement = nodeList.element("line");
  505. if (lineNode !== undefined) {
  506. try {
  507. line = parseInt(lineNode.value, 10);
  508. } catch (ex) {
  509. errorMsg = ITextTranslation.translateText(
  510. "ReaderErrorMessages/ClefLineError",
  511. "Invalid clef line given -> using default clef line."
  512. );
  513. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  514. line = 2;
  515. Logging.debug("InstrumentReader.addAbstractInstruction", errorMsg, ex);
  516. }
  517. }
  518. let signNode: IXmlElement = nodeList.element("sign");
  519. if (signNode !== undefined) {
  520. try {
  521. clefEnum = ClefEnum[signNode.value];
  522. if (!ClefInstruction.isSupportedClef(clefEnum)) {
  523. if (clefEnum === ClefEnum.TAB && guitarPro) {
  524. clefOctaveOffset = -1;
  525. }
  526. errorMsg = ITextTranslation.translateText(
  527. "ReaderErrorMessages/ClefError",
  528. "Unsupported clef found -> using default clef."
  529. );
  530. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  531. clefEnum = ClefEnum.G;
  532. line = 2;
  533. }
  534. } catch (e) {
  535. errorMsg = ITextTranslation.translateText(
  536. "ReaderErrorMessages/ClefError",
  537. "Invalid clef found -> using default clef."
  538. );
  539. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  540. clefEnum = ClefEnum.G;
  541. line = 2;
  542. Logging.debug("InstrumentReader.addAbstractInstruction", errorMsg, e);
  543. }
  544. }
  545. let clefOctaveNode: IXmlElement = nodeList.element("clef-octave-change");
  546. if (clefOctaveNode !== undefined) {
  547. try {
  548. clefOctaveOffset = parseInt(clefOctaveNode.value, 10);
  549. } catch (e) {
  550. errorMsg = ITextTranslation.translateText(
  551. "ReaderErrorMessages/ClefOctaveError",
  552. "Invalid clef octave found -> using default clef octave."
  553. );
  554. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  555. clefOctaveOffset = 0;
  556. }
  557. }
  558. if (nodeList.hasAttributes && nodeList.attributes()[0].name === "number") {
  559. try {
  560. staffNumber = parseInt(nodeList.attributes()[0].value, 10);
  561. } catch (err) {
  562. errorMsg = ITextTranslation.translateText(
  563. "ReaderErrorMessages/ClefError",
  564. "Invalid clef found -> using default clef."
  565. );
  566. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  567. staffNumber = 1;
  568. }
  569. }
  570. let clefInstruction: ClefInstruction = new ClefInstruction(clefEnum, clefOctaveOffset, line);
  571. this.abstractInstructions[staffNumber] = clefInstruction;
  572. }
  573. }
  574. if (node.element("key") !== undefined && this.instrument.MidiInstrumentId !== MidiInstrument.Percussion) {
  575. let key: number = 0;
  576. let keyNode: IXmlElement = node.element("key").element("fifths");
  577. if (keyNode !== undefined) {
  578. try {
  579. key = parseInt(keyNode.value, 10);
  580. } catch (ex) {
  581. errorMsg = ITextTranslation.translateText(
  582. "ReaderErrorMessages/KeyError",
  583. "Invalid key found -> set to default."
  584. );
  585. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  586. key = 0;
  587. Logging.debug("InstrumentReader.addAbstractInstruction", errorMsg, ex);
  588. }
  589. }
  590. let keyEnum: KeyEnum = KeyEnum.none;
  591. let modeNode: IXmlElement = node.element("key");
  592. if (modeNode !== undefined) { modeNode = modeNode.element("mode"); }
  593. if (modeNode !== undefined) {
  594. try {
  595. keyEnum = KeyEnum[modeNode.value];
  596. } catch (ex) {
  597. errorMsg = ITextTranslation.translateText(
  598. "ReaderErrorMessages/KeyError",
  599. "Invalid key found -> set to default."
  600. );
  601. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  602. keyEnum = KeyEnum.major;
  603. Logging.debug("InstrumentReader.addAbstractInstruction", errorMsg, ex);
  604. }
  605. }
  606. let keyInstruction: KeyInstruction = new KeyInstruction(undefined, key, keyEnum);
  607. this.abstractInstructions[1] = keyInstruction;
  608. }
  609. if (node.element("time") !== undefined) {
  610. let symbolEnum: RhythmSymbolEnum = RhythmSymbolEnum.NONE;
  611. let timeNode: IXmlElement = node.element("time");
  612. if (timeNode !== undefined && timeNode.hasAttributes) {
  613. let firstAttr: IXmlAttribute = timeNode.firstAttribute;
  614. if (firstAttr.name === "symbol") {
  615. if (firstAttr.value === "common") {
  616. symbolEnum = RhythmSymbolEnum.COMMON;
  617. } else if (firstAttr.value === "cut") {
  618. symbolEnum = RhythmSymbolEnum.CUT;
  619. }
  620. }
  621. }
  622. let num: number = 0;
  623. let denom: number = 0;
  624. let senzaMisura: boolean = (timeNode !== undefined && timeNode.element("senza-misura") !== undefined);
  625. let timeList: IXmlElement[] = node.elements("time");
  626. let beatsList: IXmlElement[] = [];
  627. let typeList: IXmlElement[] = [];
  628. for (let idx: number = 0, len: number = timeList.length; idx < len; ++idx) {
  629. let xmlNode: IXmlElement = timeList[idx];
  630. beatsList.push.apply(beatsList, xmlNode.elements("beats"));
  631. typeList.push.apply(typeList, xmlNode.elements("beat-type"));
  632. }
  633. if (!senzaMisura) {
  634. try {
  635. if (beatsList !== undefined && beatsList.length > 0 && typeList !== undefined && beatsList.length === typeList.length) {
  636. let length: number = beatsList.length;
  637. let fractions: Fraction[] = new Array(length);
  638. let maxDenom: number = 0;
  639. for (let i: number = 0; i < length; i++) {
  640. let s: string = beatsList[i].value;
  641. let n: number = 0;
  642. let d: number = 0;
  643. if (s.indexOf("+") !== -1) {
  644. let numbers: string[] = s.split("+");
  645. for (let idx: number = 0, len: number = numbers.length; idx < len; ++idx) {
  646. n += parseInt(numbers[idx], 10);
  647. }
  648. } else {
  649. n = parseInt(s, 10);
  650. }
  651. d = parseInt(typeList[i].value, 10);
  652. maxDenom = Math.max(maxDenom, d);
  653. fractions[i] = new Fraction(n, d, false);
  654. }
  655. for (let i: number = 0; i < length; i++) {
  656. if (fractions[i].Denominator === maxDenom) {
  657. num += fractions[i].Numerator;
  658. } else {
  659. num += (maxDenom / fractions[i].Denominator) * fractions[i].Numerator;
  660. }
  661. }
  662. denom = maxDenom;
  663. } else {
  664. num = parseInt(node.element("time").element("beats").value, 10);
  665. denom = parseInt(node.element("time").element("beat-type").value, 10);
  666. }
  667. } catch (ex) {
  668. errorMsg = ITextTranslation.translateText("ReaderErrorMessages/RhythmError", "Invalid rhythm found -> set to default.");
  669. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  670. num = 4;
  671. denom = 4;
  672. Logging.debug("InstrumentReader.addAbstractInstruction", errorMsg, ex);
  673. }
  674. if ((num === 4 && denom === 4) || (num === 2 && denom === 2)) {
  675. symbolEnum = RhythmSymbolEnum.NONE;
  676. }
  677. this.abstractInstructions[1] = new RhythmInstruction(
  678. new Fraction(num, denom, false), num, denom, symbolEnum
  679. );
  680. } else {
  681. this.abstractInstructions[1] = new RhythmInstruction(new Fraction(4, 4, false), 4, 4, RhythmSymbolEnum.NONE);
  682. }
  683. }
  684. }
  685. private saveAbstractInstructionList(numberOfStaves: number, beginOfMeasure: boolean): void {
  686. // FIXME TODO
  687. Logging.debug("saveAbstractInstructionList still to implement! See InstrumentReader.ts");
  688. }
  689. /*private saveAbstractInstructionList(numberOfStaves: number, beginOfMeasure: boolean): void {
  690. for (let i: number = this.abstractInstructions.length - 1; i >= 0; i--) {
  691. let keyValuePair: KeyValuePairClass<number, AbstractNotationInstruction> = this.abstractInstructions[i];
  692. if (keyValuePair.value instanceof ClefInstruction) {
  693. let clefInstruction: ClefInstruction = <ClefInstruction>keyValuePair.value;
  694. if (this.currentXmlMeasureIndex === 0 || (keyValuePair.key <= this.activeClefs.length && clefInstruction !== this.activeClefs[keyValuePair.key - 1])) {
  695. if (!beginOfMeasure && this.currentStaffEntry !== undefined && !this.currentStaffEntry.hasNotes() && keyValuePair.key - 1
  696. === this.instrument.Staves.IndexOf(this.currentStaffEntry.ParentStaff)) {
  697. let newClefInstruction: ClefInstruction = clefInstruction;
  698. newClefInstruction.Parent = this.currentStaffEntry;
  699. this.currentStaffEntry.removeFirstInstructionOfType<ClefInstruction>();
  700. this.currentStaffEntry.Instructions.Add(newClefInstruction);
  701. this.activeClefs[keyValuePair.key - 1] = clefInstruction;
  702. this.abstractInstructions.Remove(keyValuePair);
  703. } else if (beginOfMeasure) {
  704. let firstStaffEntry: SourceStaffEntry;
  705. if (this.currentMeasure !== undefined) {
  706. let newClefInstruction: ClefInstruction = clefInstruction;
  707. if (this.currentXmlMeasureIndex === 0) {
  708. if (this.currentMeasure.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1] === undefined) {
  709. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  710. this.currentMeasure.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1] = firstStaffEntry;
  711. newClefInstruction.Parent = firstStaffEntry;
  712. firstStaffEntry.Instructions.Add(newClefInstruction);
  713. this.activeClefsHaveBeenInitialized[keyValuePair.key - 1] = true;
  714. } else if (this.currentMeasure.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1]
  715. !==
  716. undefined && !(this.currentMeasure.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1].Instructions[0]
  717. instanceof ClefInstruction)) {
  718. firstStaffEntry = this.currentMeasure.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1];
  719. newClefInstruction.Parent = firstStaffEntry;
  720. firstStaffEntry.removeFirstInstructionOfType<ClefInstruction>();
  721. firstStaffEntry.Instructions.Insert(0, newClefInstruction);
  722. this.activeClefsHaveBeenInitialized[keyValuePair.key - 1] = true;
  723. } else {
  724. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  725. this.currentMeasure.LastInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1] = lastStaffEntry;
  726. newClefInstruction.Parent = lastStaffEntry;
  727. lastStaffEntry.Instructions.Add(newClefInstruction);
  728. }
  729. } else if (!this.activeClefsHaveBeenInitialized[keyValuePair.key - 1]) {
  730. let first: SourceMeasure = this.musicSheet2.SourceMeasures[0];
  731. if (first.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1] === undefined) {
  732. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  733. } else {
  734. firstStaffEntry = first.FirstInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1];
  735. firstStaffEntry.removeFirstInstructionOfType<ClefInstruction>();
  736. }
  737. newClefInstruction.Parent = firstStaffEntry;
  738. firstStaffEntry.Instructions.Insert(0, newClefInstruction);
  739. this.activeClefsHaveBeenInitialized[keyValuePair.key - 1] = true;
  740. } else {
  741. let lastStaffEntry: SourceStaffEntry = new SourceStaffEntry(undefined, undefined);
  742. this.previousMeasure.LastInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + keyValuePair.key - 1] = lastStaffEntry;
  743. newClefInstruction.Parent = lastStaffEntry;
  744. lastStaffEntry.Instructions.Add(newClefInstruction);
  745. }
  746. this.activeClefs[keyValuePair.key - 1] = clefInstruction;
  747. this.abstractInstructions.Remove(keyValuePair);
  748. }
  749. }
  750. }
  751. if (keyValuePair.key <= this.activeClefs.length && clefInstruction === this.activeClefs[keyValuePair.key - 1])
  752. this.abstractInstructions.Remove(keyValuePair);
  753. }
  754. if (keyValuePair.value instanceof KeyInstruction) {
  755. let keyInstruction: KeyInstruction = <KeyInstruction>keyValuePair.value;
  756. if (this.activeKey === undefined || this.activeKey.Key !== keyInstruction.Key) {
  757. this.activeKey = keyInstruction;
  758. this.abstractInstructions.Remove(keyValuePair);
  759. let sourceMeasure: SourceMeasure;
  760. if (!this.activeKeyHasBeenInitialized) {
  761. this.activeKeyHasBeenInitialized = true;
  762. if (this.currentXmlMeasureIndex > 0) {
  763. sourceMeasure = this.musicSheet2.SourceMeasures[0];
  764. } else {
  765. sourceMeasure = this.currentMeasure;
  766. }
  767. } else {
  768. sourceMeasure = this.currentMeasure;
  769. }
  770. if (sourceMeasure !== undefined) {
  771. for (let j: number = this.inSourceMeasureInstrumentIndex; j < this.inSourceMeasureInstrumentIndex + numberOfStaves; j++) {
  772. let newKeyInstruction: KeyInstruction = keyInstruction;
  773. if (sourceMeasure.FirstInstructionsStaffEntries[j] === undefined) {
  774. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  775. sourceMeasure.FirstInstructionsStaffEntries[j] = firstStaffEntry;
  776. newKeyInstruction.Parent = firstStaffEntry;
  777. firstStaffEntry.Instructions.Add(newKeyInstruction);
  778. } else {
  779. firstStaffEntry = sourceMeasure.FirstInstructionsStaffEntries[j];
  780. newKeyInstruction.Parent = firstStaffEntry;
  781. firstStaffEntry.removeFirstInstructionOfTypeKeyInstruction();
  782. if (firstStaffEntry.Instructions.length === 0) {
  783. firstStaffEntry.Instructions.Add(newKeyInstruction);
  784. } else {
  785. if (firstStaffEntry.Instructions[0] instanceof ClefInstruction) {
  786. firstStaffEntry.Instructions.Insert(1, newKeyInstruction);
  787. } else {
  788. firstStaffEntry.Instructions.Insert(0, newKeyInstruction);
  789. }
  790. }
  791. }
  792. }
  793. }
  794. }
  795. if (this.activeKey !== undefined && this.activeKey === keyInstruction)
  796. this.abstractInstructions.Remove(keyValuePair);
  797. }
  798. if (keyValuePair.value instanceof RhythmInstruction) {
  799. let rhythmInstruction: RhythmInstruction = <RhythmInstruction>keyValuePair.value;
  800. if (this.activeRhythm === undefined || this.activeRhythm !== rhythmInstruction) {
  801. this.activeRhythm = rhythmInstruction;
  802. this.abstractInstructions.Remove(keyValuePair);
  803. if (this.currentMeasure !== undefined) {
  804. for (let j: number = this.inSourceMeasureInstrumentIndex; j < this.inSourceMeasureInstrumentIndex + numberOfStaves; j++) {
  805. let newRhythmInstruction: RhythmInstruction = rhythmInstruction;
  806. if (this.currentMeasure.FirstInstructionsStaffEntries[j] === undefined) {
  807. firstStaffEntry = new SourceStaffEntry(undefined, undefined);
  808. this.currentMeasure.FirstInstructionsStaffEntries[j] = firstStaffEntry;
  809. } else {
  810. firstStaffEntry = this.currentMeasure.FirstInstructionsStaffEntries[j];
  811. firstStaffEntry.removeFirstInstructionOfType<RhythmInstruction>();
  812. }
  813. newRhythmInstruction.Parent = firstStaffEntry;
  814. firstStaffEntry.Instructions.Add(newRhythmInstruction);
  815. }
  816. }
  817. }
  818. if (this.activeRhythm !== undefined && this.activeRhythm === rhythmInstruction)
  819. this.abstractInstructions.Remove(keyValuePair);
  820. }
  821. }
  822. }
  823. */
  824. private saveClefInstructionAtEndOfMeasure(): void {
  825. for (let key in this.abstractInstructions) {
  826. if (this.abstractInstructions.hasOwnProperty(key)) {
  827. let value: AbstractNotationInstruction = this.abstractInstructions[key];
  828. if (value instanceof ClefInstruction) {
  829. let clefInstruction: ClefInstruction = <ClefInstruction>value;
  830. if (
  831. (this.activeClefs[+key - 1] === undefined) ||
  832. (clefInstruction.ClefType !== this.activeClefs[+key - 1].ClefType || (
  833. clefInstruction.ClefType === this.activeClefs[+key - 1].ClefType &&
  834. clefInstruction.Line !== this.activeClefs[+key - 1].Line
  835. ))) {
  836. let lastStaffEntry: SourceStaffEntry = new SourceStaffEntry(undefined, undefined);
  837. this.currentMeasure.LastInstructionsStaffEntries[this.inSourceMeasureInstrumentIndex + (+key) - 1] = lastStaffEntry;
  838. let newClefInstruction: ClefInstruction = clefInstruction;
  839. newClefInstruction.Parent = lastStaffEntry;
  840. lastStaffEntry.Instructions.push(newClefInstruction);
  841. this.activeClefs[+key - 1] = clefInstruction;
  842. delete this.abstractInstructions[+key]; // FIXME Andrea: might hurt performance?
  843. }
  844. }
  845. }
  846. }
  847. }
  848. private getNoteDurationForTuplet(xmlNode: IXmlElement): Fraction {
  849. let duration: Fraction = new Fraction(0, 1);
  850. let typeDuration: Fraction = this.getNoteDurationFromTypeNode(xmlNode);
  851. if (xmlNode.element("time-modification") !== undefined) {
  852. let time: IXmlElement = xmlNode.element("time-modification");
  853. if (time !== undefined) {
  854. if (time.element("actual-notes") !== undefined && time.element("normal-notes") !== undefined) {
  855. let actualNotes: IXmlElement = time.element("actual-notes");
  856. let normalNotes: IXmlElement = time.element("normal-notes");
  857. if (actualNotes !== undefined && normalNotes !== undefined) {
  858. let actual: number = parseInt(actualNotes.value, 10);
  859. let normal: number = parseInt(normalNotes.value, 10);
  860. duration = new Fraction(normal * typeDuration.Numerator, actual * typeDuration.Denominator);
  861. }
  862. }
  863. }
  864. }
  865. return duration;
  866. }
  867. //private readExpressionStaffNumber(xmlNode: IXmlElement): number {
  868. // let directionStaffNumber: number = 1;
  869. // if (xmlNode.element("staff") !== undefined) {
  870. // let staffNode: IXmlElement = xmlNode.element("staff");
  871. // if (staffNode !== undefined) {
  872. // try {
  873. // directionStaffNumber = parseInt(staffNode.value, 10);
  874. // } catch (ex) {
  875. // let errorMsg: string = ITextTranslation.translateText(
  876. // "ReaderErrorMessages/ExpressionStaffError", "Invalid Expression staff number -> set to default."
  877. // );
  878. // this.musicSheet.SheetErrors.pushTemp(errorMsg);
  879. // directionStaffNumber = 1;
  880. // logging.debug("InstrumentReader.readExpressionStaffNumber", errorMsg, ex);
  881. // }
  882. //
  883. // }
  884. // }
  885. // return directionStaffNumber;
  886. //}
  887. private readDivisionsFromNotes(): number {
  888. let divisionsFromNote: number = 0;
  889. let xmlMeasureIndex: number = this.currentXmlMeasureIndex;
  890. let read: boolean = false;
  891. while (!read) {
  892. let xmlMeasureListArr: IXmlElement[] = this.xmlMeasureList[xmlMeasureIndex].elements();
  893. for (let idx: number = 0, len: number = xmlMeasureListArr.length; idx < len; ++idx) {
  894. let xmlNode: IXmlElement = xmlMeasureListArr[idx];
  895. if (xmlNode.name === "note" && xmlNode.element("time-modification") === undefined) {
  896. if (xmlNode.element("duration") !== undefined && xmlNode.element("type") !== undefined) {
  897. let durationNode: IXmlElement = xmlNode.element("duration");
  898. let typeNode: IXmlElement = xmlNode.element("type");
  899. if (durationNode !== undefined && typeNode !== undefined) {
  900. let type: string = typeNode.value;
  901. let noteDuration: number = 0;
  902. try {
  903. noteDuration = parseInt(durationNode.value, 10);
  904. } catch (ex) {
  905. Logging.debug("InstrumentReader.readDivisionsFromNotes", ex);
  906. continue;
  907. }
  908. switch (type) {
  909. case "1024th":
  910. divisionsFromNote = (noteDuration / 4) * 1024;
  911. break;
  912. case "512th":
  913. divisionsFromNote = (noteDuration / 4) * 512;
  914. break;
  915. case "256th":
  916. divisionsFromNote = (noteDuration / 4) * 256;
  917. break;
  918. case "128th":
  919. divisionsFromNote = (noteDuration / 4) * 128;
  920. break;
  921. case "64th":
  922. divisionsFromNote = (noteDuration / 4) * 64;
  923. break;
  924. case "32nd":
  925. divisionsFromNote = (noteDuration / 4) * 32;
  926. break;
  927. case "16th":
  928. divisionsFromNote = (noteDuration / 4) * 16;
  929. break;
  930. case "eighth":
  931. divisionsFromNote = (noteDuration / 4) * 8;
  932. break;
  933. case "quarter":
  934. divisionsFromNote = (noteDuration / 4) * 4;
  935. break;
  936. case "half":
  937. divisionsFromNote = (noteDuration / 4) * 2;
  938. break;
  939. case "whole":
  940. divisionsFromNote = (noteDuration / 4);
  941. break;
  942. case "breve":
  943. divisionsFromNote = (noteDuration / 4) / 2;
  944. break;
  945. case "long":
  946. divisionsFromNote = (noteDuration / 4) / 4;
  947. break;
  948. case "maxima":
  949. divisionsFromNote = (noteDuration / 4) / 8;
  950. break;
  951. default: break;
  952. }
  953. }
  954. }
  955. }
  956. if (divisionsFromNote > 0) {
  957. read = true;
  958. break;
  959. }
  960. }
  961. if (divisionsFromNote === 0) {
  962. xmlMeasureIndex++;
  963. if (xmlMeasureIndex === this.xmlMeasureList.length) {
  964. let errorMsg: string = ITextTranslation.translateText("ReaderErrorMEssages/DivisionsError", "Invalid divisions value at Instrument: ");
  965. throw new MusicSheetReadingException(errorMsg + this.instrument.Name);
  966. }
  967. }
  968. }
  969. return divisionsFromNote;
  970. }
  971. }