SlurReader.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { MusicSheet } from "../../MusicSheet";
  2. import { IXmlElement, IXmlAttribute } from "../../../Common/FileIO/Xml";
  3. import { Slur } from "../../VoiceData/Expressions/ContinuousExpressions/Slur";
  4. import { Note } from "../../VoiceData/Note";
  5. import log from "loglevel";
  6. import { ITextTranslation } from "../../Interfaces/ITextTranslation";
  7. export class SlurReader {
  8. private musicSheet: MusicSheet;
  9. private openSlurDict: { [_: number]: Slur; } = {};
  10. constructor(musicSheet: MusicSheet) {
  11. this.musicSheet = musicSheet;
  12. }
  13. public addSlur(slurNodes: IXmlElement[], currentNote: Note): void {
  14. try {
  15. if (slurNodes) {
  16. for (const slurNode of slurNodes) {
  17. if (slurNode.attributes().length > 0) {
  18. const type: string = slurNode.attribute("type").value;
  19. let slurNumber: number = 1;
  20. try {
  21. const slurNumberAttribute: IXmlAttribute = slurNode.attribute("number");
  22. if (slurNumberAttribute) {
  23. slurNumber = parseInt(slurNode.attribute("number").value, 10);
  24. }
  25. } catch (ex) {
  26. log.debug("VoiceGenerator.addSlur number: ", ex);
  27. }
  28. if (type === "start") {
  29. let slur: Slur = this.openSlurDict[slurNumber];
  30. if (!slur) {
  31. slur = new Slur();
  32. this.openSlurDict[slurNumber] = slur;
  33. }
  34. slur.StartNote = currentNote;
  35. } else if (type === "stop") {
  36. const slur: Slur = this.openSlurDict[slurNumber];
  37. if (slur) {
  38. slur.EndNote = currentNote;
  39. // check if not already a slur with same notes has been given:
  40. if (!currentNote.checkForDoubleSlur(slur)) {
  41. // if not, link slur to notes:
  42. currentNote.NoteSlurs.push(slur);
  43. const slurStartNote: Note = slur.StartNote;
  44. slurStartNote.NoteSlurs.push(slur);
  45. }
  46. delete this.openSlurDict[slurNumber];
  47. }
  48. }
  49. }
  50. }
  51. }
  52. } catch (err) {
  53. const errorMsg: string = ITextTranslation.translateText("ReaderErrorMessages/SlurError", "Error while reading slur.");
  54. this.musicSheet.SheetErrors.pushMeasureError(errorMsg);
  55. }
  56. }
  57. }