Note.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {VoiceEntry} from "./VoiceEntry";
  2. import {SourceStaffEntry} from "./SourceStaffEntry";
  3. import {Fraction} from "../../Common/DataObjects/Fraction";
  4. import {Pitch} from "../../Common/DataObjects/Pitch";
  5. import {Beam} from "./Beam";
  6. import {Tuplet} from "./Tuplet";
  7. import {Tie} from "./Tie";
  8. import {Staff} from "./Staff";
  9. import {Slur} from "./Expressions/ContinuousExpressions/Slur";
  10. import {NoteState} from "../Graphical/DrawingEnums";
  11. import {NoteHead} from "./NoteHead";
  12. import {Arpeggio} from "./Arpeggio";
  13. /**
  14. * Represents a single pitch with a duration (length)
  15. */
  16. export class Note {
  17. constructor(voiceEntry: VoiceEntry, parentStaffEntry: SourceStaffEntry, length: Fraction, pitch: Pitch) {
  18. this.voiceEntry = voiceEntry;
  19. this.parentStaffEntry = parentStaffEntry;
  20. this.length = length;
  21. this.pitch = pitch;
  22. if (pitch !== undefined) {
  23. this.halfTone = pitch.getHalfTone();
  24. } else {
  25. this.halfTone = 0;
  26. }
  27. }
  28. /**
  29. * The transposed (!!!) HalfTone of this note.
  30. */
  31. public halfTone: number;
  32. public state: NoteState;
  33. private voiceEntry: VoiceEntry;
  34. private parentStaffEntry: SourceStaffEntry;
  35. private length: Fraction;
  36. /**
  37. * The untransposed (!!!) source data.
  38. */
  39. private pitch: Pitch;
  40. private beam: Beam;
  41. private tuplet: Tuplet;
  42. private tie: Tie;
  43. private slurs: Slur[] = [];
  44. private playbackInstrumentId: string = undefined;
  45. private noteHead: NoteHead = undefined;
  46. /** States whether the note should be displayed. False if xmlNode.attribute("print-object").value = "no". */
  47. private printObject: boolean = true;
  48. /** The Arpeggio this note is part of. */
  49. private arpeggio: Arpeggio;
  50. /** States whether this is a cue note (Stichnote) (smaller size). */
  51. private isCueNote: boolean;
  52. public get ParentVoiceEntry(): VoiceEntry {
  53. return this.voiceEntry;
  54. }
  55. public set ParentVoiceEntry(value: VoiceEntry) {
  56. this.voiceEntry = value;
  57. }
  58. public get ParentStaffEntry(): SourceStaffEntry {
  59. return this.parentStaffEntry;
  60. }
  61. public get ParentStaff(): Staff {
  62. return this.parentStaffEntry.ParentStaff;
  63. }
  64. public get Length(): Fraction {
  65. return this.length;
  66. }
  67. public set Length(value: Fraction) {
  68. this.length = value;
  69. }
  70. public get Pitch(): Pitch {
  71. return this.pitch;
  72. }
  73. public get NoteBeam(): Beam {
  74. return this.beam;
  75. }
  76. public set NoteBeam(value: Beam) {
  77. this.beam = value;
  78. }
  79. public get NoteTuplet(): Tuplet {
  80. return this.tuplet;
  81. }
  82. public set NoteTuplet(value: Tuplet) {
  83. this.tuplet = value;
  84. }
  85. public get NoteTie(): Tie {
  86. return this.tie;
  87. }
  88. public set NoteTie(value: Tie) {
  89. this.tie = value;
  90. }
  91. public get NoteSlurs(): Slur[] {
  92. return this.slurs;
  93. }
  94. public set NoteSlurs(value: Slur[]) {
  95. this.slurs = value;
  96. }
  97. public get PlaybackInstrumentId(): string {
  98. return this.playbackInstrumentId;
  99. }
  100. public set PlaybackInstrumentId(value: string) {
  101. this.playbackInstrumentId = value;
  102. }
  103. public set NoteHead(value: NoteHead) {
  104. this.noteHead = value;
  105. }
  106. public get NoteHead(): NoteHead {
  107. return this.noteHead;
  108. }
  109. public get PrintObject(): boolean {
  110. return this.printObject;
  111. }
  112. public set PrintObject(value: boolean) {
  113. this.printObject = value;
  114. }
  115. public get Arpeggio(): Arpeggio {
  116. return this.arpeggio;
  117. }
  118. public set Arpeggio(value: Arpeggio) {
  119. this.arpeggio = value;
  120. }
  121. public get IsCueNote(): boolean {
  122. return this.isCueNote;
  123. }
  124. public set IsCueNote(value: boolean) {
  125. this.isCueNote = value;
  126. }
  127. public isRest(): boolean {
  128. return this.Pitch === undefined;
  129. }
  130. public ToString(): string {
  131. if (this.pitch !== undefined) {
  132. return this.Pitch.ToString() + ", length: " + this.length.toString();
  133. } else {
  134. return "rest note, length: " + this.length.toString();
  135. }
  136. }
  137. public getAbsoluteTimestamp(): Fraction {
  138. return Fraction.plus(
  139. this.voiceEntry.Timestamp,
  140. this.parentStaffEntry.VerticalContainerParent.ParentMeasure.AbsoluteTimestamp
  141. );
  142. }
  143. public checkForDoubleSlur(slur: Slur): boolean {
  144. for (let idx: number = 0, len: number = this.slurs.length; idx < len; ++idx) {
  145. const noteSlur: Slur = this.slurs[idx];
  146. if (
  147. noteSlur.StartNote !== undefined &&
  148. noteSlur.EndNote !== undefined &&
  149. slur.StartNote !== undefined &&
  150. slur.StartNote === noteSlur.StartNote &&
  151. noteSlur.EndNote === this
  152. ) { return true; }
  153. }
  154. return false;
  155. }
  156. }
  157. export enum Appearance {
  158. Normal,
  159. Grace,
  160. Cue
  161. }