Note.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  13. * Represents a single pitch with a duration (length)
  14. */
  15. export class Note {
  16. constructor(voiceEntry: VoiceEntry, parentStaffEntry: SourceStaffEntry, length: Fraction, pitch: Pitch) {
  17. this.voiceEntry = voiceEntry;
  18. this.parentStaffEntry = parentStaffEntry;
  19. this.length = length;
  20. this.pitch = pitch;
  21. if (pitch !== undefined) {
  22. this.halfTone = pitch.getHalfTone();
  23. } else {
  24. this.halfTone = 0;
  25. }
  26. }
  27. /**
  28. * The transposed (!!!) HalfTone of this note.
  29. */
  30. public halfTone: number;
  31. public state: NoteState;
  32. private voiceEntry: VoiceEntry;
  33. private parentStaffEntry: SourceStaffEntry;
  34. private length: Fraction;
  35. /**
  36. * The untransposed (!!!) source data.
  37. */
  38. private pitch: Pitch;
  39. private beam: Beam;
  40. private tuplet: Tuplet;
  41. private tie: Tie;
  42. private slurs: Slur[] = [];
  43. private playbackInstrumentId: string = undefined;
  44. private noteHead: NoteHead = undefined;
  45. public get ParentVoiceEntry(): VoiceEntry {
  46. return this.voiceEntry;
  47. }
  48. public set ParentVoiceEntry(value: VoiceEntry) {
  49. this.voiceEntry = value;
  50. }
  51. public get ParentStaffEntry(): SourceStaffEntry {
  52. return this.parentStaffEntry;
  53. }
  54. public get ParentStaff(): Staff {
  55. return this.parentStaffEntry.ParentStaff;
  56. }
  57. public get Length(): Fraction {
  58. return this.length;
  59. }
  60. public set Length(value: Fraction) {
  61. this.length = value;
  62. }
  63. public get Pitch(): Pitch {
  64. return this.pitch;
  65. }
  66. public get NoteBeam(): Beam {
  67. return this.beam;
  68. }
  69. public set NoteBeam(value: Beam) {
  70. this.beam = value;
  71. }
  72. public get NoteTuplet(): Tuplet {
  73. return this.tuplet;
  74. }
  75. public set NoteTuplet(value: Tuplet) {
  76. this.tuplet = value;
  77. }
  78. public get NoteTie(): Tie {
  79. return this.tie;
  80. }
  81. public set NoteTie(value: Tie) {
  82. this.tie = value;
  83. }
  84. public get NoteSlurs(): Slur[] {
  85. return this.slurs;
  86. }
  87. public set NoteSlurs(value: Slur[]) {
  88. this.slurs = value;
  89. }
  90. public get PlaybackInstrumentId(): string {
  91. return this.playbackInstrumentId;
  92. }
  93. public set PlaybackInstrumentId(value: string) {
  94. this.playbackInstrumentId = value;
  95. }
  96. public set NoteHead(value: NoteHead) {
  97. this.noteHead = value;
  98. }
  99. public get NoteHead(): NoteHead {
  100. return this.noteHead;
  101. }
  102. public isRest(): boolean {
  103. return this.Pitch === undefined;
  104. }
  105. public ToString(): string {
  106. if (this.pitch !== undefined) {
  107. return this.Pitch.ToString() + ", length: " + this.length.toString();
  108. } else {
  109. return "rest note, length: " + this.length.toString();
  110. }
  111. }
  112. public getAbsoluteTimestamp(): Fraction {
  113. return Fraction.plus(
  114. this.voiceEntry.Timestamp,
  115. this.parentStaffEntry.VerticalContainerParent.ParentMeasure.AbsoluteTimestamp
  116. );
  117. }
  118. public checkForDoubleSlur(slur: Slur): boolean {
  119. for (let idx: number = 0, len: number = this.slurs.length; idx < len; ++idx) {
  120. const noteSlur: Slur = this.slurs[idx];
  121. if (
  122. noteSlur.StartNote !== undefined &&
  123. noteSlur.EndNote !== undefined &&
  124. slur.StartNote !== undefined &&
  125. slur.StartNote === noteSlur.StartNote &&
  126. noteSlur.EndNote === this
  127. ) { return true; }
  128. }
  129. return false;
  130. }
  131. }
  132. export enum Appearance {
  133. Normal,
  134. Grace,
  135. Cue
  136. }