ExpressionReader.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. import {MusicSheet} from "../../MusicSheet";
  2. import {Fraction} from "../../../Common/DataObjects/Fraction";
  3. import {MultiTempoExpression} from "../../VoiceData/Expressions/MultiTempoExpression";
  4. import {ContDynamicEnum, ContinuousDynamicExpression} from "../../VoiceData/Expressions/ContinuousExpressions/ContinuousDynamicExpression";
  5. import {ContinuousTempoExpression} from "../../VoiceData/Expressions/ContinuousExpressions/ContinuousTempoExpression";
  6. import {InstantaneousDynamicExpression} from "../../VoiceData/Expressions/InstantaneousDynamicExpression";
  7. import {OctaveShift} from "../../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  8. import {Instrument} from "../../Instrument";
  9. import {MultiExpression} from "../../VoiceData/Expressions/MultiExpression";
  10. import {IXmlAttribute, IXmlElement} from "../../../Common/FileIO/Xml";
  11. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  12. import {InstantaneousTempoExpression} from "../../VoiceData/Expressions/InstantaneousTempoExpression";
  13. import {MoodExpression} from "../../VoiceData/Expressions/MoodExpression";
  14. import {UnknownExpression} from "../../VoiceData/Expressions/UnknownExpression";
  15. import {PlacementEnum} from "../../VoiceData/Expressions/AbstractExpression";
  16. import {TextAlignmentEnum} from "../../../Common/Enums/TextAlignment";
  17. import {ITextTranslation} from "../../Interfaces/ITextTranslation";
  18. import log from "loglevel";
  19. export class ExpressionReader {
  20. private musicSheet: MusicSheet;
  21. private placement: PlacementEnum;
  22. private soundTempo: number;
  23. private soundDynamic: number;
  24. private offsetDivisions: number;
  25. private staffNumber: number;
  26. private globalStaffIndex: number;
  27. private directionTimestamp: Fraction;
  28. private currentMultiTempoExpression: MultiTempoExpression;
  29. private openContinuousDynamicExpression: ContinuousDynamicExpression;
  30. private openContinuousTempoExpression: ContinuousTempoExpression;
  31. private activeInstantaneousDynamic: InstantaneousDynamicExpression;
  32. private openOctaveShift: OctaveShift;
  33. constructor(musicSheet: MusicSheet, instrument: Instrument, staffNumber: number) {
  34. this.musicSheet = musicSheet;
  35. this.staffNumber = staffNumber;
  36. this.globalStaffIndex = musicSheet.getGlobalStaffIndexOfFirstStaff(instrument) + (staffNumber - 1);
  37. this.initialize();
  38. }
  39. public getMultiExpression: MultiExpression;
  40. public readExpressionParameters(xmlNode: IXmlElement, currentInstrument: Instrument, divisions: number,
  41. inSourceMeasureCurrentFraction: Fraction,
  42. inSourceMeasureFormerFraction: Fraction,
  43. currentMeasureIndex: number,
  44. ignoreDivisionsOffset: boolean): void {
  45. this.initialize();
  46. const offsetNode: IXmlElement = xmlNode.element("offset");
  47. if (offsetNode !== undefined && !ignoreDivisionsOffset) {
  48. try {
  49. this.offsetDivisions = parseInt(offsetNode.value, 10);
  50. } catch (ex) {
  51. const errorMsg: string = "ReaderErrorMessages/ExpressionOffsetError" + ", Invalid expression offset -> set to default.";
  52. log.debug("ExpressionReader.readExpressionParameters", errorMsg, ex);
  53. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  54. this.offsetDivisions = 0;
  55. }
  56. }
  57. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  58. let offsetFraction: Fraction = new Fraction(Math.abs(this.offsetDivisions), divisions * 4);
  59. if (this.offsetDivisions > 0) {
  60. if (inSourceMeasureCurrentFraction.RealValue > 0) {
  61. offsetFraction = Fraction.multiply(Fraction.minus(inSourceMeasureCurrentFraction, inSourceMeasureFormerFraction), offsetFraction);
  62. this.directionTimestamp = Fraction.plus(offsetFraction, inSourceMeasureCurrentFraction);
  63. } else { this.directionTimestamp = Fraction.createFromFraction(offsetFraction); }
  64. } else if (this.offsetDivisions < 0) {
  65. if (inSourceMeasureCurrentFraction.RealValue > 0) {
  66. offsetFraction = Fraction.multiply(Fraction.minus(inSourceMeasureCurrentFraction, inSourceMeasureFormerFraction), offsetFraction);
  67. this.directionTimestamp = Fraction.minus(inSourceMeasureCurrentFraction, offsetFraction);
  68. } else { this.directionTimestamp = Fraction.createFromFraction(offsetFraction); }
  69. }
  70. const placeAttr: IXmlAttribute = xmlNode.attribute("placement");
  71. if (placeAttr !== undefined && placeAttr !== null) {
  72. try {
  73. const placementString: string = placeAttr.value;
  74. if (placementString === "below") {
  75. this.placement = PlacementEnum.Below;
  76. } else if (placementString === "above") {
  77. this.placement = PlacementEnum.Above;
  78. }
  79. } catch (ex) {
  80. const errorMsg: string = ITextTranslation.translateText( "ReaderErrorMessages/ExpressionPlacementError",
  81. "Invalid expression placement -> set to default.");
  82. log.debug("ExpressionReader.readExpressionParameters", errorMsg, ex);
  83. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  84. this.placement = PlacementEnum.Below;
  85. }
  86. }
  87. if (this.placement === PlacementEnum.NotYetDefined) {
  88. try {
  89. const directionTypeNode: IXmlElement = xmlNode.element("direction-type");
  90. if (directionTypeNode !== undefined) {
  91. const dynamicsNode: IXmlElement = directionTypeNode.element("dynamics");
  92. if (dynamicsNode !== undefined) {
  93. const defAttr: IXmlAttribute = dynamicsNode.attribute("default-y");
  94. if (defAttr !== undefined && defAttr !== null) {
  95. this.readExpressionPlacement(defAttr, "read dynamics y pos");
  96. }
  97. }
  98. const wedgeNode: IXmlElement = directionTypeNode.element("wedge");
  99. if (wedgeNode !== undefined) {
  100. const defAttr: IXmlAttribute = wedgeNode.attribute("default-y");
  101. if (defAttr !== undefined && defAttr !== null) {
  102. this.readExpressionPlacement(defAttr, "read wedge y pos");
  103. }
  104. }
  105. const wordsNode: IXmlElement = directionTypeNode.element("words");
  106. if (wordsNode !== undefined) {
  107. const defAttr: IXmlAttribute = wordsNode.attribute("default-y");
  108. if (defAttr !== undefined && defAttr !== null) {
  109. this.readExpressionPlacement(defAttr, "read words y pos");
  110. }
  111. }
  112. }
  113. } catch (ex) {
  114. const errorMsg: string = ITextTranslation.translateText( "ReaderErrorMessages/ExpressionPlacementError",
  115. "Invalid expression placement -> set to default.");
  116. log.debug("ExpressionReader.readExpressionParameters", errorMsg, ex);
  117. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  118. this.placement = PlacementEnum.Below;
  119. }
  120. }
  121. if (this.placement === PlacementEnum.NotYetDefined) {
  122. if (currentInstrument.Staves.length > 1) {
  123. this.placement = PlacementEnum.Below;
  124. } else if (currentInstrument.HasLyrics) {
  125. this.placement = PlacementEnum.Above;
  126. } else { this.placement = PlacementEnum.Below; }
  127. }
  128. }
  129. public read(directionNode: IXmlElement, currentMeasure: SourceMeasure, inSourceMeasureCurrentFraction: Fraction): void {
  130. let isTempoInstruction: boolean = false;
  131. let isDynamicInstruction: boolean = false;
  132. const n: IXmlElement = directionNode.element("sound");
  133. if (n !== undefined) {
  134. const tempoAttr: IXmlAttribute = n.attribute("tempo");
  135. const dynAttr: IXmlAttribute = n.attribute("dynamics");
  136. if (tempoAttr) {
  137. const match: string[] = tempoAttr.value.match(/\d+/);
  138. this.soundTempo = match !== undefined ? parseInt(match[0], 10) : 100;
  139. currentMeasure.TempoInBPM = this.soundTempo;
  140. this.musicSheet.HasBPMInfo = true;
  141. isTempoInstruction = true;
  142. }
  143. if (dynAttr) {
  144. const match: string[] = dynAttr.value.match(/\d+/);
  145. this.soundDynamic = match !== undefined ? parseInt(match[0], 10) : 100;
  146. isDynamicInstruction = true;
  147. }
  148. }
  149. const dirNode: IXmlElement = directionNode.element("direction-type");
  150. if (dirNode === undefined) {
  151. return;
  152. }
  153. let dirContentNode: IXmlElement = dirNode.element("metronome");
  154. if (dirContentNode !== undefined) {
  155. const beatUnit: IXmlElement = dirContentNode.element("beat-unit");
  156. const dotted: boolean = dirContentNode.element("beat-unit-dot") !== undefined;
  157. const bpm: IXmlElement = dirContentNode.element("per-minute");
  158. // TODO check print-object = false -> don't render invisible metronome mark
  159. if (beatUnit !== undefined && bpm !== undefined) {
  160. const useCurrentFractionForPositioning: boolean = (dirContentNode.hasAttributes && dirContentNode.attribute("default-x") !== undefined);
  161. if (useCurrentFractionForPositioning) {
  162. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  163. }
  164. const bpmNumber: number = parseInt(bpm.value, 10);
  165. this.createNewTempoExpressionIfNeeded(currentMeasure);
  166. const instantaneousTempoExpression: InstantaneousTempoExpression =
  167. new InstantaneousTempoExpression(undefined,
  168. this.placement,
  169. this.staffNumber,
  170. bpmNumber,
  171. this.currentMultiTempoExpression,
  172. true);
  173. this.soundTempo = bpmNumber;
  174. currentMeasure.TempoInBPM = this.soundTempo;
  175. this.musicSheet.HasBPMInfo = true;
  176. instantaneousTempoExpression.dotted = dotted;
  177. instantaneousTempoExpression.beatUnit = beatUnit.value;
  178. this.currentMultiTempoExpression.addExpression(instantaneousTempoExpression, "");
  179. this.currentMultiTempoExpression.CombinedExpressionsText = "test";
  180. }
  181. return;
  182. }
  183. dirContentNode = dirNode.element("dynamics");
  184. if (dirContentNode !== undefined) {
  185. const fromNotation: boolean = directionNode.element("notations") !== undefined;
  186. this.interpretInstantaneousDynamics(dirContentNode, currentMeasure, inSourceMeasureCurrentFraction, fromNotation);
  187. return;
  188. }
  189. dirContentNode = dirNode.element("words");
  190. if (dirContentNode !== undefined) {
  191. if (isTempoInstruction) {
  192. this.createNewTempoExpressionIfNeeded(currentMeasure);
  193. this.currentMultiTempoExpression.CombinedExpressionsText = dirContentNode.value;
  194. const instantaneousTempoExpression: InstantaneousTempoExpression =
  195. new InstantaneousTempoExpression(dirContentNode.value, this.placement, this.staffNumber, this.soundTempo, this.currentMultiTempoExpression);
  196. this.currentMultiTempoExpression.addExpression(instantaneousTempoExpression, "");
  197. } else if (!isDynamicInstruction) {
  198. this.interpretWords(dirContentNode, currentMeasure, inSourceMeasureCurrentFraction);
  199. }
  200. return;
  201. }
  202. dirContentNode = dirNode.element("wedge");
  203. if (dirContentNode !== undefined) {
  204. this.interpretWedge(dirContentNode, currentMeasure, inSourceMeasureCurrentFraction, currentMeasure.MeasureNumber);
  205. return;
  206. }
  207. }
  208. public checkForOpenExpressions(sourceMeasure: SourceMeasure, timestamp: Fraction): void {
  209. if (this.openContinuousDynamicExpression !== undefined) {
  210. this.createNewMultiExpressionIfNeeded(sourceMeasure, timestamp);
  211. this.closeOpenContinuousDynamic();
  212. }
  213. if (this.openContinuousTempoExpression !== undefined) {
  214. this.closeOpenContinuousTempo(Fraction.plus(sourceMeasure.AbsoluteTimestamp, timestamp));
  215. }
  216. }
  217. public addOctaveShift(directionNode: IXmlElement, currentMeasure: SourceMeasure, endTimestamp: Fraction): void {
  218. let octaveStaffNumber: number = 1;
  219. const staffNode: IXmlElement = directionNode.element("staff");
  220. if (staffNode !== undefined) {
  221. try {
  222. octaveStaffNumber = parseInt(staffNode.value, 10);
  223. } catch (ex) {
  224. const errorMsg: string = ITextTranslation.translateText( "ReaderErrorMessages/OctaveShiftStaffError",
  225. "Invalid octave shift staff number -> set to default");
  226. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  227. octaveStaffNumber = 1;
  228. log.debug("ExpressionReader.addOctaveShift", errorMsg, ex);
  229. }
  230. }
  231. const directionTypeNode: IXmlElement = directionNode.element("direction-type");
  232. if (directionTypeNode !== undefined) {
  233. const octaveShiftNode: IXmlElement = directionTypeNode.element("octave-shift");
  234. if (octaveShiftNode !== undefined && octaveShiftNode.hasAttributes) {
  235. try {
  236. if (octaveShiftNode.attribute("size") !== undefined) {
  237. const size: number = parseInt(octaveShiftNode.attribute("size").value, 10);
  238. let octave: number = 0;
  239. if (size === 8) {
  240. octave = 1;
  241. } else if (size === 15) {
  242. octave = 2;
  243. }
  244. if (octaveShiftNode.attribute("type") !== undefined) {
  245. const type: string = octaveShiftNode.attribute("type").value;
  246. if (type === "up" || type === "down") {
  247. const octaveShift: OctaveShift = new OctaveShift(type, octave);
  248. octaveShift.StaffNumber = octaveStaffNumber;
  249. this.createNewMultiExpressionIfNeeded(currentMeasure);
  250. this.getMultiExpression.OctaveShiftStart = octaveShift;
  251. octaveShift.ParentStartMultiExpression = this.getMultiExpression;
  252. this.openOctaveShift = octaveShift;
  253. } else if (type === "stop") {
  254. if (this.openOctaveShift !== undefined) {
  255. this.createNewMultiExpressionIfNeeded(currentMeasure, endTimestamp);
  256. this.getMultiExpression.OctaveShiftEnd = this.openOctaveShift;
  257. this.openOctaveShift.ParentEndMultiExpression = this.getMultiExpression;
  258. this.openOctaveShift = undefined;
  259. }
  260. }
  261. }
  262. }
  263. } catch (ex) {
  264. const errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/OctaveShiftError", "Error while reading octave shift.");
  265. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  266. log.debug("ExpressionReader.addOctaveShift", errorMsg, ex);
  267. }
  268. }
  269. }
  270. }
  271. private initialize(): void {
  272. this.placement = PlacementEnum.NotYetDefined;
  273. this.soundTempo = 0;
  274. this.soundDynamic = 0;
  275. this.offsetDivisions = 0;
  276. }
  277. private readExpressionPlacement(defAttr: IXmlAttribute, catchLogMessage: string): void {
  278. try {
  279. const y: number = parseInt(defAttr.value, 10);
  280. if (y < 0) {
  281. this.placement = PlacementEnum.Below;
  282. } else if (y > 0) {
  283. this.placement = PlacementEnum.Above;
  284. }
  285. } catch (ex) {
  286. log.debug("ExpressionReader.readExpressionParameters", catchLogMessage, ex);
  287. }
  288. }
  289. private interpretInstantaneousDynamics(dynamicsNode: IXmlElement,
  290. currentMeasure: SourceMeasure,
  291. inSourceMeasureCurrentFraction: Fraction,
  292. fromNotation: boolean): void {
  293. if (dynamicsNode.hasElements) {
  294. if (dynamicsNode.hasAttributes && dynamicsNode.attribute("default-x") !== undefined) {
  295. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  296. }
  297. let expressionText: string = dynamicsNode.elements()[0].name;
  298. if (expressionText === "other-dynamics") {
  299. expressionText = dynamicsNode.elements()[0].value;
  300. }
  301. if (expressionText !== undefined) {
  302. // // ToDo: add doublettes recognition again as a afterReadingModule, as we can't check here if there is a repetition:
  303. // // Make here a comparisson with the active dynamic expression and only add it, if there is a change in dynamic
  304. // // Exception is when there starts a repetition, where this might be different when repeating.
  305. // let dynamicEnum: DynamicEnum;
  306. // try {
  307. // dynamicEnum = DynamicEnum[expressionText];
  308. // } catch (err) {
  309. // const errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/DynamicError", "Error while reading dynamic.");
  310. // this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  311. // return;
  312. // }
  313. // if (this.activeInstantaneousDynamic === undefined ||
  314. // (this.activeInstantaneousDynamic !== undefined && this.activeInstantaneousDynamic.DynEnum !== dynamicEnum)) {
  315. if (!fromNotation) {
  316. this.createNewMultiExpressionIfNeeded(currentMeasure);
  317. } else { this.createNewMultiExpressionIfNeeded(currentMeasure, Fraction.createFromFraction(inSourceMeasureCurrentFraction)); }
  318. if (this.openContinuousDynamicExpression !== undefined &&
  319. this.openContinuousDynamicExpression.StartMultiExpression !== this.getMultiExpression) {
  320. this.closeOpenContinuousDynamic();
  321. }
  322. const instantaneousDynamicExpression: InstantaneousDynamicExpression = new InstantaneousDynamicExpression( expressionText,
  323. this.soundDynamic,
  324. this.placement,
  325. this.staffNumber);
  326. this.getMultiExpression.addExpression(instantaneousDynamicExpression, "");
  327. this.initialize();
  328. if (this.activeInstantaneousDynamic !== undefined) {
  329. this.activeInstantaneousDynamic.DynEnum = instantaneousDynamicExpression.DynEnum;
  330. } else { this.activeInstantaneousDynamic = new InstantaneousDynamicExpression(expressionText, 0, PlacementEnum.NotYetDefined, 1); }
  331. //}
  332. }
  333. }
  334. }
  335. private interpretWords(wordsNode: IXmlElement, currentMeasure: SourceMeasure, inSourceMeasureCurrentFraction: Fraction): void {
  336. const text: string = wordsNode.value;
  337. if (text.length > 0) {
  338. if (wordsNode.hasAttributes && wordsNode.attribute("default-x") !== undefined) {
  339. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  340. }
  341. if (this.checkIfWordsNodeIsRepetitionInstruction(text)) {
  342. return;
  343. }
  344. this.fillMultiOrTempoExpression(text, currentMeasure);
  345. this.initialize();
  346. }
  347. }
  348. private interpretWedge(wedgeNode: IXmlElement, currentMeasure: SourceMeasure, inSourceMeasureCurrentFraction: Fraction, currentMeasureIndex: number): void {
  349. if (wedgeNode !== undefined && wedgeNode.hasAttributes && wedgeNode.attribute("default-x") !== undefined) {
  350. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  351. }
  352. this.createNewMultiExpressionIfNeeded(currentMeasure);
  353. this.addWedge(wedgeNode, currentMeasureIndex);
  354. this.initialize();
  355. }
  356. private createNewMultiExpressionIfNeeded(currentMeasure: SourceMeasure, timestamp: Fraction = undefined): void {
  357. if (timestamp === undefined) {
  358. timestamp = this.directionTimestamp;
  359. }
  360. if (this.getMultiExpression === undefined ||
  361. this.getMultiExpression !== undefined &&
  362. (this.getMultiExpression.SourceMeasureParent !== currentMeasure ||
  363. (this.getMultiExpression.SourceMeasureParent === currentMeasure && this.getMultiExpression.Timestamp !== timestamp))) {
  364. this.getMultiExpression = new MultiExpression(currentMeasure, Fraction.createFromFraction(timestamp));
  365. currentMeasure.StaffLinkedExpressions[this.globalStaffIndex].push(this.getMultiExpression);
  366. }
  367. }
  368. private createNewTempoExpressionIfNeeded(currentMeasure: SourceMeasure): void {
  369. if (this.currentMultiTempoExpression === undefined ||
  370. this.currentMultiTempoExpression.SourceMeasureParent !== currentMeasure ||
  371. this.currentMultiTempoExpression.Timestamp !== this.directionTimestamp) {
  372. this.currentMultiTempoExpression = new MultiTempoExpression(currentMeasure, Fraction.createFromFraction(this.directionTimestamp));
  373. currentMeasure.TempoExpressions.push(this.currentMultiTempoExpression);
  374. }
  375. }
  376. private addWedge(wedgeNode: IXmlElement, currentMeasureIndex: number): void {
  377. if (wedgeNode !== undefined && wedgeNode.hasAttributes) {
  378. const type: string = wedgeNode.attribute("type").value.toLowerCase();
  379. try {
  380. if (type === "crescendo" || type === "diminuendo") {
  381. const continuousDynamicExpression: ContinuousDynamicExpression = new ContinuousDynamicExpression(ContDynamicEnum[type],
  382. this.placement, this.staffNumber);
  383. if (this.openContinuousDynamicExpression !== undefined) {
  384. this.closeOpenContinuousDynamic();
  385. }
  386. this.openContinuousDynamicExpression = continuousDynamicExpression;
  387. this.getMultiExpression.StartingContinuousDynamic = continuousDynamicExpression;
  388. continuousDynamicExpression.StartMultiExpression = this.getMultiExpression;
  389. if (this.activeInstantaneousDynamic !== undefined &&
  390. this.activeInstantaneousDynamic.StaffNumber === continuousDynamicExpression.StaffNumber) {
  391. this.activeInstantaneousDynamic = undefined;
  392. }
  393. } else if (type === "stop") {
  394. if (this.openContinuousDynamicExpression !== undefined) {
  395. this.closeOpenContinuousDynamic();
  396. }
  397. }
  398. } catch (ex) {
  399. const errorMsg: string = "ReaderErrorMessages/WedgeError" + ", Error while reading Crescendo / Diminuendo.";
  400. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  401. log.debug("ExpressionReader.addWedge", errorMsg, ex);
  402. }
  403. }
  404. }
  405. private fillMultiOrTempoExpression(inputString: string, currentMeasure: SourceMeasure): void {
  406. if (inputString === undefined) {
  407. return;
  408. }
  409. const tmpInputString: string = inputString.trim();
  410. // split string at enumerating words or signs
  411. const splitStrings: string[] = tmpInputString.split(/([\s,\r\n]and[\s,\r\n]|[\s,\r\n]und[\s,\r\n]|[\s,\r\n]e[\s,\r\n]|[\s,\r\n])+/g);
  412. for (const splitStr of splitStrings) {
  413. this.createExpressionFromString("", splitStr, currentMeasure, inputString);
  414. }
  415. }
  416. /*
  417. private splitStringRecursive(input: [string, string], stringSeparators: string[]): [string, string][] {
  418. let text: string = input[1];
  419. let lastSeparator: string = input[0];
  420. let resultList: [string, string][] = [];
  421. for (let idx: number = 0, len: number = stringSeparators.length; idx < len; ++idx) {
  422. let stringSeparator: string = stringSeparators[idx];
  423. if (text.indexOf(stringSeparator) < 0) {
  424. continue;
  425. }
  426. let splitStrings: string[] = text.split(stringSeparator, StringSplitOptions.RemoveEmptyEntries);
  427. if (splitStrings.length !== 0) {
  428. resultList.push(...this.splitStringRecursive([lastSeparator, splitStrings[0]], stringSeparators));
  429. for (let index: number = 1; index < splitStrings.length; index++) {
  430. resultList.push(...this.splitStringRecursive([stringSeparator, splitStrings[index]], stringSeparators));
  431. }
  432. } else {
  433. resultList.push(["", stringSeparator]);
  434. }
  435. break;
  436. }
  437. if (resultList.length === 0) {
  438. resultList.push(input);
  439. }
  440. return resultList;
  441. }
  442. */
  443. private createExpressionFromString(prefix: string, stringTrimmed: string,
  444. currentMeasure: SourceMeasure, inputString: string): boolean {
  445. if (InstantaneousTempoExpression.isInputStringInstantaneousTempo(stringTrimmed) ||
  446. ContinuousTempoExpression.isInputStringContinuousTempo(stringTrimmed)) {
  447. // first check if there is already a tempo expression with the same function
  448. if (currentMeasure.TempoExpressions.length > 0) {
  449. for (let idx: number = 0, len: number = currentMeasure.TempoExpressions.length; idx < len; ++idx) {
  450. const multiTempoExpression: MultiTempoExpression = currentMeasure.TempoExpressions[idx];
  451. if (multiTempoExpression.Timestamp === this.directionTimestamp &&
  452. multiTempoExpression.InstantaneousTempo !== undefined &&
  453. multiTempoExpression.InstantaneousTempo.Label.indexOf(stringTrimmed) !== -1) {
  454. return false;
  455. }
  456. }
  457. }
  458. this.createNewTempoExpressionIfNeeded(currentMeasure);
  459. this.currentMultiTempoExpression.CombinedExpressionsText = inputString;
  460. if (InstantaneousTempoExpression.isInputStringInstantaneousTempo(stringTrimmed)) {
  461. const instantaneousTempoExpression: InstantaneousTempoExpression = new InstantaneousTempoExpression( stringTrimmed,
  462. this.placement,
  463. this.staffNumber,
  464. this.soundTempo,
  465. this.currentMultiTempoExpression);
  466. this.currentMultiTempoExpression.addExpression(instantaneousTempoExpression, prefix);
  467. return true;
  468. }
  469. if (ContinuousTempoExpression.isInputStringContinuousTempo(stringTrimmed)) {
  470. const continuousTempoExpression: ContinuousTempoExpression = new ContinuousTempoExpression( stringTrimmed,
  471. this.placement,
  472. this.staffNumber,
  473. this.currentMultiTempoExpression);
  474. this.currentMultiTempoExpression.addExpression(continuousTempoExpression, prefix);
  475. return true;
  476. }
  477. }
  478. if (InstantaneousDynamicExpression.isInputStringInstantaneousDynamic(stringTrimmed) ||
  479. ContinuousDynamicExpression.isInputStringContinuousDynamic(stringTrimmed)) {
  480. this.createNewMultiExpressionIfNeeded(currentMeasure);
  481. if (InstantaneousDynamicExpression.isInputStringInstantaneousDynamic(stringTrimmed)) {
  482. if (this.openContinuousDynamicExpression !== undefined && this.openContinuousDynamicExpression.EndMultiExpression === undefined) {
  483. this.closeOpenContinuousDynamic();
  484. }
  485. const instantaneousDynamicExpression: InstantaneousDynamicExpression = new InstantaneousDynamicExpression(stringTrimmed,
  486. this.soundDynamic,
  487. this.placement,
  488. this.staffNumber);
  489. this.getMultiExpression.addExpression(instantaneousDynamicExpression, prefix);
  490. return true;
  491. }
  492. if (ContinuousDynamicExpression.isInputStringContinuousDynamic(stringTrimmed)) {
  493. const continuousDynamicExpression: ContinuousDynamicExpression = new ContinuousDynamicExpression( undefined,
  494. this.placement,
  495. this.staffNumber,
  496. stringTrimmed);
  497. if (this.openContinuousDynamicExpression !== undefined && this.openContinuousDynamicExpression.EndMultiExpression === undefined) {
  498. this.closeOpenContinuousDynamic();
  499. }
  500. if (this.activeInstantaneousDynamic !== undefined && this.activeInstantaneousDynamic.StaffNumber === continuousDynamicExpression.StaffNumber) {
  501. this.activeInstantaneousDynamic = undefined;
  502. }
  503. this.openContinuousDynamicExpression = continuousDynamicExpression;
  504. continuousDynamicExpression.StartMultiExpression = this.getMultiExpression;
  505. this.getMultiExpression.addExpression(continuousDynamicExpression, prefix);
  506. return true;
  507. }
  508. }
  509. if (MoodExpression.isInputStringMood(stringTrimmed)) {
  510. this.createNewMultiExpressionIfNeeded(currentMeasure);
  511. const moodExpression: MoodExpression = new MoodExpression(stringTrimmed, this.placement, this.staffNumber);
  512. this.getMultiExpression.addExpression(moodExpression, prefix);
  513. return true;
  514. }
  515. // create unknown:
  516. this.createNewMultiExpressionIfNeeded(currentMeasure);
  517. if (currentMeasure.TempoExpressions.length > 0) {
  518. for (let idx: number = 0, len: number = currentMeasure.TempoExpressions.length; idx < len; ++idx) {
  519. const multiTempoExpression: MultiTempoExpression = currentMeasure.TempoExpressions[idx];
  520. if (multiTempoExpression.Timestamp === this.directionTimestamp &&
  521. multiTempoExpression.InstantaneousTempo !== undefined &&
  522. multiTempoExpression.EntriesList.length > 0 &&
  523. !this.hasDigit(stringTrimmed)) {
  524. if (this.globalStaffIndex > 0) {
  525. if (multiTempoExpression.EntriesList[0].label.indexOf(stringTrimmed) >= 0) {
  526. return false;
  527. } else {
  528. break;
  529. }
  530. }
  531. }
  532. }
  533. }
  534. let textAlignment: TextAlignmentEnum = TextAlignmentEnum.CenterBottom;
  535. if (this.musicSheet.Rules.CompactMode) {
  536. textAlignment = TextAlignmentEnum.LeftBottom;
  537. }
  538. const unknownExpression: UnknownExpression = new UnknownExpression(
  539. stringTrimmed, this.placement, textAlignment, this.staffNumber);
  540. this.getMultiExpression.addExpression(unknownExpression, prefix);
  541. return false;
  542. }
  543. private closeOpenContinuousDynamic(): void {
  544. this.openContinuousDynamicExpression.EndMultiExpression = this.getMultiExpression;
  545. this.getMultiExpression.EndingContinuousDynamic = this.openContinuousDynamicExpression;
  546. this.openContinuousDynamicExpression = undefined;
  547. }
  548. private closeOpenContinuousTempo(endTimestamp: Fraction): void {
  549. this.openContinuousTempoExpression.AbsoluteEndTimestamp = endTimestamp;
  550. this.openContinuousTempoExpression = undefined;
  551. }
  552. private checkIfWordsNodeIsRepetitionInstruction(inputString: string): boolean {
  553. inputString = inputString.trim().toLowerCase();
  554. if (inputString === "coda" ||
  555. inputString === "tocoda" ||
  556. inputString === "to coda" ||
  557. inputString === "fine" ||
  558. inputString === "d.c." ||
  559. inputString === "dacapo" ||
  560. inputString === "da capo" ||
  561. inputString === "d.s." ||
  562. inputString === "dalsegno" ||
  563. inputString === "dal segno" ||
  564. inputString === "d.c. al fine" ||
  565. inputString === "d.s. al fine" ||
  566. inputString === "d.c. al coda" ||
  567. inputString === "d.s. al coda") {
  568. return true;
  569. }
  570. return false;
  571. }
  572. private hasDigit(input: string): boolean {
  573. return /\d/.test(input);
  574. }
  575. }