Note.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. export class Note {
  12. constructor(voiceEntry: VoiceEntry, parentStaffEntry: SourceStaffEntry, length: Fraction, pitch: Pitch) {
  13. this.voiceEntry = voiceEntry;
  14. this.parentStaffEntry = parentStaffEntry;
  15. this.length = length;
  16. this.pitch = pitch;
  17. if (pitch !== undefined) {
  18. this.halfTone = pitch.getHalfTone();
  19. } else {
  20. this.halfTone = 0;
  21. }
  22. }
  23. public halfTone: number;
  24. public state: NoteState;
  25. private voiceEntry: VoiceEntry;
  26. private parentStaffEntry: SourceStaffEntry;
  27. private length: Fraction;
  28. private pitch: Pitch;
  29. private beam: Beam;
  30. private tuplet: Tuplet;
  31. private tie: Tie;
  32. private slurs: Slur[] = [];
  33. private graceNoteSlash: boolean = false;
  34. private playbackInstrumentId: string = undefined;
  35. public get GraceNoteSlash(): boolean {
  36. return this.graceNoteSlash;
  37. }
  38. public set GraceNoteSlash(value: boolean) {
  39. this.graceNoteSlash = value;
  40. }
  41. public get ParentVoiceEntry(): VoiceEntry {
  42. return this.voiceEntry;
  43. }
  44. public set ParentVoiceEntry(value: VoiceEntry) {
  45. this.voiceEntry = value;
  46. }
  47. public get ParentStaffEntry(): SourceStaffEntry {
  48. return this.parentStaffEntry;
  49. }
  50. public get ParentStaff(): Staff {
  51. return this.parentStaffEntry.ParentStaff;
  52. }
  53. public get Length(): Fraction {
  54. return this.length;
  55. }
  56. public set Length(value: Fraction) {
  57. this.length = value;
  58. }
  59. public get Pitch(): Pitch {
  60. return this.pitch;
  61. }
  62. public get NoteBeam(): Beam {
  63. return this.beam;
  64. }
  65. public set NoteBeam(value: Beam) {
  66. this.beam = value;
  67. }
  68. public get NoteTuplet(): Tuplet {
  69. return this.tuplet;
  70. }
  71. public set NoteTuplet(value: Tuplet) {
  72. this.tuplet = value;
  73. }
  74. public get NoteTie(): Tie {
  75. return this.tie;
  76. }
  77. public set NoteTie(value: Tie) {
  78. this.tie = value;
  79. }
  80. public get NoteSlurs(): Slur[] {
  81. return this.slurs;
  82. }
  83. public set NoteSlurs(value: Slur[]) {
  84. this.slurs = value;
  85. }
  86. public get PlaybackInstrumentId(): string {
  87. return this.playbackInstrumentId;
  88. }
  89. public set PlaybackInstrumentId(value: string) {
  90. this.playbackInstrumentId = value;
  91. }
  92. public calculateNoteLengthWithoutTie(): Fraction {
  93. let withoutTieLength: Fraction = this.length.clone();
  94. if (this.tie !== undefined) {
  95. for (let fraction of this.tie.Fractions) {
  96. withoutTieLength.Sub(fraction);
  97. }
  98. }
  99. return withoutTieLength;
  100. }
  101. public calculateNoteOriginalLength(originalLength: Fraction = this.length): Fraction {
  102. if (this.tie !== undefined) {
  103. originalLength = this.calculateNoteLengthWithoutTie();
  104. }
  105. if (this.tuplet !== undefined) {
  106. return this.length;
  107. }
  108. if (originalLength.Numerator > 1) {
  109. let exp: number = Math.floor(Math.log(originalLength.Denominator) / Math.LN2) - this.calculateNumberOfNeededDots(originalLength);
  110. originalLength.Denominator = Math.pow(2, exp);
  111. originalLength.Numerator = 1;
  112. }
  113. return originalLength;
  114. }
  115. public ToString(): string {
  116. if (this.pitch !== undefined) {
  117. return this.Pitch.ToString() + ", length: " + this.length.toString();
  118. } else {
  119. return "rest note, length: " + this.length.toString();
  120. }
  121. }
  122. public getAbsoluteTimestamp(): Fraction {
  123. return Fraction.plus(
  124. this.voiceEntry.Timestamp,
  125. this.parentStaffEntry.VerticalContainerParent.ParentMeasure.AbsoluteTimestamp
  126. );
  127. }
  128. public checkForDoubleSlur(slur: Slur): boolean {
  129. for (let idx: number = 0, len: number = this.slurs.length; idx < len; ++idx) {
  130. let noteSlur: Slur = this.slurs[idx];
  131. if (
  132. noteSlur.StartNote !== undefined &&
  133. noteSlur.EndNote !== undefined &&
  134. slur.StartNote !== undefined &&
  135. slur.StartNote === noteSlur.StartNote &&
  136. noteSlur.EndNote === this
  137. ) { return true; }
  138. }
  139. return false;
  140. }
  141. //public calculateTailSymbol(): number {
  142. // let length: number = this.Length.RealValue;
  143. // if (this.NoteTuplet) {
  144. // length = this.NoteTuplet.Fractions[this.NoteTuplet.getNoteIndex(this)].RealValue;
  145. // }
  146. // if (length < 0.25 && length >= 0.125) {
  147. // return 8;
  148. // } else if (length < 0.125 && length >= 0.0625) {
  149. // return 16;
  150. // } else if (length < 0.0625 && length >= 0.03125) {
  151. // return 32;
  152. // } else {
  153. // return 64;
  154. // }
  155. //}
  156. private calculateNumberOfNeededDots(fraction: Fraction = this.length): number {
  157. // FIXME (Andrea) Test if correct
  158. if (this.tuplet === undefined) {
  159. return Math.floor(Math.log(fraction.Numerator) / Math.LN2);
  160. } else {
  161. return 0;
  162. }
  163. }
  164. }
  165. export enum Appearance {
  166. Normal,
  167. Grace,
  168. Cue
  169. }