ExpressionReader.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. // // see PR #767 where this was removed
  306. // let dynamicEnum: DynamicEnum;
  307. // try {
  308. // dynamicEnum = DynamicEnum[expressionText];
  309. // } catch (err) {
  310. // const errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/DynamicError", "Error while reading dynamic.");
  311. // this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  312. // return;
  313. // }
  314. // if (this.activeInstantaneousDynamic === undefined ||
  315. // (this.activeInstantaneousDynamic !== undefined && this.activeInstantaneousDynamic.DynEnum !== dynamicEnum)) {
  316. if (!fromNotation) {
  317. this.createNewMultiExpressionIfNeeded(currentMeasure);
  318. } else { this.createNewMultiExpressionIfNeeded(currentMeasure, Fraction.createFromFraction(inSourceMeasureCurrentFraction)); }
  319. if (this.openContinuousDynamicExpression !== undefined &&
  320. this.openContinuousDynamicExpression.StartMultiExpression !== this.getMultiExpression) {
  321. this.closeOpenContinuousDynamic();
  322. }
  323. const instantaneousDynamicExpression: InstantaneousDynamicExpression = new InstantaneousDynamicExpression( expressionText,
  324. this.soundDynamic,
  325. this.placement,
  326. this.staffNumber);
  327. this.getMultiExpression.addExpression(instantaneousDynamicExpression, "");
  328. this.initialize();
  329. if (this.activeInstantaneousDynamic !== undefined) {
  330. this.activeInstantaneousDynamic.DynEnum = instantaneousDynamicExpression.DynEnum;
  331. } else { this.activeInstantaneousDynamic = new InstantaneousDynamicExpression(expressionText, 0, PlacementEnum.NotYetDefined, 1); }
  332. //}
  333. }
  334. }
  335. }
  336. private interpretWords(wordsNode: IXmlElement, currentMeasure: SourceMeasure, inSourceMeasureCurrentFraction: Fraction): void {
  337. const text: string = wordsNode.value;
  338. if (text.length > 0) {
  339. if (wordsNode.hasAttributes && wordsNode.attribute("default-x") !== undefined) {
  340. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  341. }
  342. if (this.checkIfWordsNodeIsRepetitionInstruction(text)) {
  343. return;
  344. }
  345. this.fillMultiOrTempoExpression(text, currentMeasure);
  346. this.initialize();
  347. }
  348. }
  349. private interpretWedge(wedgeNode: IXmlElement, currentMeasure: SourceMeasure, inSourceMeasureCurrentFraction: Fraction, currentMeasureIndex: number): void {
  350. if (wedgeNode !== undefined && wedgeNode.hasAttributes && wedgeNode.attribute("default-x") !== undefined) {
  351. this.directionTimestamp = Fraction.createFromFraction(inSourceMeasureCurrentFraction);
  352. }
  353. this.createNewMultiExpressionIfNeeded(currentMeasure);
  354. this.addWedge(wedgeNode, currentMeasureIndex);
  355. this.initialize();
  356. }
  357. private createNewMultiExpressionIfNeeded(currentMeasure: SourceMeasure, timestamp: Fraction = undefined): void {
  358. if (timestamp === undefined) {
  359. timestamp = this.directionTimestamp;
  360. }
  361. if (this.getMultiExpression === undefined ||
  362. this.getMultiExpression !== undefined &&
  363. (this.getMultiExpression.SourceMeasureParent !== currentMeasure ||
  364. (this.getMultiExpression.SourceMeasureParent === currentMeasure && this.getMultiExpression.Timestamp !== timestamp))) {
  365. this.getMultiExpression = new MultiExpression(currentMeasure, Fraction.createFromFraction(timestamp));
  366. currentMeasure.StaffLinkedExpressions[this.globalStaffIndex].push(this.getMultiExpression);
  367. }
  368. }
  369. private createNewTempoExpressionIfNeeded(currentMeasure: SourceMeasure): void {
  370. if (this.currentMultiTempoExpression === undefined ||
  371. this.currentMultiTempoExpression.SourceMeasureParent !== currentMeasure ||
  372. this.currentMultiTempoExpression.Timestamp !== this.directionTimestamp) {
  373. this.currentMultiTempoExpression = new MultiTempoExpression(currentMeasure, Fraction.createFromFraction(this.directionTimestamp));
  374. currentMeasure.TempoExpressions.push(this.currentMultiTempoExpression);
  375. }
  376. }
  377. private addWedge(wedgeNode: IXmlElement, currentMeasureIndex: number): void {
  378. if (wedgeNode !== undefined && wedgeNode.hasAttributes) {
  379. const type: string = wedgeNode.attribute("type").value.toLowerCase();
  380. try {
  381. if (type === "crescendo" || type === "diminuendo") {
  382. const continuousDynamicExpression: ContinuousDynamicExpression = new ContinuousDynamicExpression(ContDynamicEnum[type],
  383. this.placement, this.staffNumber);
  384. if (this.openContinuousDynamicExpression !== undefined) {
  385. this.closeOpenContinuousDynamic();
  386. }
  387. this.openContinuousDynamicExpression = continuousDynamicExpression;
  388. this.getMultiExpression.StartingContinuousDynamic = continuousDynamicExpression;
  389. continuousDynamicExpression.StartMultiExpression = this.getMultiExpression;
  390. if (this.activeInstantaneousDynamic !== undefined &&
  391. this.activeInstantaneousDynamic.StaffNumber === continuousDynamicExpression.StaffNumber) {
  392. this.activeInstantaneousDynamic = undefined;
  393. }
  394. } else if (type === "stop") {
  395. if (this.openContinuousDynamicExpression !== undefined) {
  396. this.closeOpenContinuousDynamic();
  397. }
  398. }
  399. } catch (ex) {
  400. const errorMsg: string = "ReaderErrorMessages/WedgeError" + ", Error while reading Crescendo / Diminuendo.";
  401. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  402. log.debug("ExpressionReader.addWedge", errorMsg, ex);
  403. }
  404. }
  405. }
  406. private fillMultiOrTempoExpression(inputString: string, currentMeasure: SourceMeasure): void {
  407. if (inputString === undefined) {
  408. return;
  409. }
  410. const tmpInputString: string = inputString.trim();
  411. // split string at enumerating words or signs
  412. 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);
  413. for (const splitStr of splitStrings) {
  414. this.createExpressionFromString("", splitStr, currentMeasure, inputString);
  415. }
  416. }
  417. /*
  418. private splitStringRecursive(input: [string, string], stringSeparators: string[]): [string, string][] {
  419. let text: string = input[1];
  420. let lastSeparator: string = input[0];
  421. let resultList: [string, string][] = [];
  422. for (let idx: number = 0, len: number = stringSeparators.length; idx < len; ++idx) {
  423. let stringSeparator: string = stringSeparators[idx];
  424. if (text.indexOf(stringSeparator) < 0) {
  425. continue;
  426. }
  427. let splitStrings: string[] = text.split(stringSeparator, StringSplitOptions.RemoveEmptyEntries);
  428. if (splitStrings.length !== 0) {
  429. resultList.push(...this.splitStringRecursive([lastSeparator, splitStrings[0]], stringSeparators));
  430. for (let index: number = 1; index < splitStrings.length; index++) {
  431. resultList.push(...this.splitStringRecursive([stringSeparator, splitStrings[index]], stringSeparators));
  432. }
  433. } else {
  434. resultList.push(["", stringSeparator]);
  435. }
  436. break;
  437. }
  438. if (resultList.length === 0) {
  439. resultList.push(input);
  440. }
  441. return resultList;
  442. }
  443. */
  444. private createExpressionFromString(prefix: string, stringTrimmed: string,
  445. currentMeasure: SourceMeasure, inputString: string): boolean {
  446. if (InstantaneousTempoExpression.isInputStringInstantaneousTempo(stringTrimmed) ||
  447. ContinuousTempoExpression.isInputStringContinuousTempo(stringTrimmed)) {
  448. // first check if there is already a tempo expression with the same function
  449. if (currentMeasure.TempoExpressions.length > 0) {
  450. for (let idx: number = 0, len: number = currentMeasure.TempoExpressions.length; idx < len; ++idx) {
  451. const multiTempoExpression: MultiTempoExpression = currentMeasure.TempoExpressions[idx];
  452. if (multiTempoExpression.Timestamp === this.directionTimestamp &&
  453. multiTempoExpression.InstantaneousTempo !== undefined &&
  454. multiTempoExpression.InstantaneousTempo.Label.indexOf(stringTrimmed) !== -1) {
  455. return false;
  456. }
  457. }
  458. }
  459. this.createNewTempoExpressionIfNeeded(currentMeasure);
  460. this.currentMultiTempoExpression.CombinedExpressionsText = inputString;
  461. if (InstantaneousTempoExpression.isInputStringInstantaneousTempo(stringTrimmed)) {
  462. const instantaneousTempoExpression: InstantaneousTempoExpression = new InstantaneousTempoExpression( stringTrimmed,
  463. this.placement,
  464. this.staffNumber,
  465. this.soundTempo,
  466. this.currentMultiTempoExpression);
  467. this.currentMultiTempoExpression.addExpression(instantaneousTempoExpression, prefix);
  468. return true;
  469. }
  470. if (ContinuousTempoExpression.isInputStringContinuousTempo(stringTrimmed)) {
  471. const continuousTempoExpression: ContinuousTempoExpression = new ContinuousTempoExpression( stringTrimmed,
  472. this.placement,
  473. this.staffNumber,
  474. this.currentMultiTempoExpression);
  475. this.currentMultiTempoExpression.addExpression(continuousTempoExpression, prefix);
  476. return true;
  477. }
  478. }
  479. if (InstantaneousDynamicExpression.isInputStringInstantaneousDynamic(stringTrimmed) ||
  480. ContinuousDynamicExpression.isInputStringContinuousDynamic(stringTrimmed)) {
  481. this.createNewMultiExpressionIfNeeded(currentMeasure);
  482. if (InstantaneousDynamicExpression.isInputStringInstantaneousDynamic(stringTrimmed)) {
  483. if (this.openContinuousDynamicExpression !== undefined && this.openContinuousDynamicExpression.EndMultiExpression === undefined) {
  484. this.closeOpenContinuousDynamic();
  485. }
  486. const instantaneousDynamicExpression: InstantaneousDynamicExpression = new InstantaneousDynamicExpression(stringTrimmed,
  487. this.soundDynamic,
  488. this.placement,
  489. this.staffNumber);
  490. this.getMultiExpression.addExpression(instantaneousDynamicExpression, prefix);
  491. return true;
  492. }
  493. if (ContinuousDynamicExpression.isInputStringContinuousDynamic(stringTrimmed)) {
  494. const continuousDynamicExpression: ContinuousDynamicExpression = new ContinuousDynamicExpression( undefined,
  495. this.placement,
  496. this.staffNumber,
  497. stringTrimmed);
  498. if (this.openContinuousDynamicExpression !== undefined && this.openContinuousDynamicExpression.EndMultiExpression === undefined) {
  499. this.closeOpenContinuousDynamic();
  500. }
  501. if (this.activeInstantaneousDynamic !== undefined && this.activeInstantaneousDynamic.StaffNumber === continuousDynamicExpression.StaffNumber) {
  502. this.activeInstantaneousDynamic = undefined;
  503. }
  504. this.openContinuousDynamicExpression = continuousDynamicExpression;
  505. continuousDynamicExpression.StartMultiExpression = this.getMultiExpression;
  506. this.getMultiExpression.addExpression(continuousDynamicExpression, prefix);
  507. return true;
  508. }
  509. }
  510. if (MoodExpression.isInputStringMood(stringTrimmed)) {
  511. this.createNewMultiExpressionIfNeeded(currentMeasure);
  512. const moodExpression: MoodExpression = new MoodExpression(stringTrimmed, this.placement, this.staffNumber);
  513. this.getMultiExpression.addExpression(moodExpression, prefix);
  514. return true;
  515. }
  516. // create unknown:
  517. this.createNewMultiExpressionIfNeeded(currentMeasure);
  518. if (currentMeasure.TempoExpressions.length > 0) {
  519. for (let idx: number = 0, len: number = currentMeasure.TempoExpressions.length; idx < len; ++idx) {
  520. const multiTempoExpression: MultiTempoExpression = currentMeasure.TempoExpressions[idx];
  521. if (multiTempoExpression.Timestamp === this.directionTimestamp &&
  522. multiTempoExpression.InstantaneousTempo !== undefined &&
  523. multiTempoExpression.EntriesList.length > 0 &&
  524. !this.hasDigit(stringTrimmed)) {
  525. if (this.globalStaffIndex > 0) {
  526. if (multiTempoExpression.EntriesList[0].label.indexOf(stringTrimmed) >= 0) {
  527. return false;
  528. } else {
  529. break;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. let textAlignment: TextAlignmentEnum = TextAlignmentEnum.CenterBottom;
  536. if (this.musicSheet.Rules.CompactMode) {
  537. textAlignment = TextAlignmentEnum.LeftBottom;
  538. }
  539. const unknownExpression: UnknownExpression = new UnknownExpression(
  540. stringTrimmed, this.placement, textAlignment, this.staffNumber);
  541. this.getMultiExpression.addExpression(unknownExpression, prefix);
  542. return false;
  543. }
  544. private closeOpenContinuousDynamic(): void {
  545. this.openContinuousDynamicExpression.EndMultiExpression = this.getMultiExpression;
  546. this.getMultiExpression.EndingContinuousDynamic = this.openContinuousDynamicExpression;
  547. this.openContinuousDynamicExpression = undefined;
  548. }
  549. private closeOpenContinuousTempo(endTimestamp: Fraction): void {
  550. this.openContinuousTempoExpression.AbsoluteEndTimestamp = endTimestamp;
  551. this.openContinuousTempoExpression = undefined;
  552. }
  553. private checkIfWordsNodeIsRepetitionInstruction(inputString: string): boolean {
  554. inputString = inputString.trim().toLowerCase();
  555. if (inputString === "coda" ||
  556. inputString === "tocoda" ||
  557. inputString === "to coda" ||
  558. inputString === "fine" ||
  559. inputString === "d.c." ||
  560. inputString === "dacapo" ||
  561. inputString === "da capo" ||
  562. inputString === "d.s." ||
  563. inputString === "dalsegno" ||
  564. inputString === "dal segno" ||
  565. inputString === "d.c. al fine" ||
  566. inputString === "d.s. al fine" ||
  567. inputString === "d.c. al coda" ||
  568. inputString === "d.s. al coda") {
  569. return true;
  570. }
  571. return false;
  572. }
  573. private hasDigit(input: string): boolean {
  574. return /\d/.test(input);
  575. }
  576. }