Note.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 calculateNoteLengthWithDots(): Fraction {
  116. // FIXME is this function the same as this.calculateNoteLengthWithoutTie?
  117. if (this.tie !== undefined) {
  118. return this.calculateNoteLengthWithoutTie();
  119. }
  120. return this.length;
  121. }
  122. public calculateNumberOfNeededDots(fraction: Fraction = this.length): number {
  123. // FIXME (Andrea) Test if correct
  124. if (this.tuplet === undefined) {
  125. return Math.floor(Math.log(fraction.Numerator) / Math.LN2);
  126. } else {
  127. return 0;
  128. }
  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. let 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. //public calculateTailSymbol(): number {
  157. // let length: number = this.Length.RealValue;
  158. // if (this.NoteTuplet) {
  159. // length = this.NoteTuplet.Fractions[this.NoteTuplet.getNoteIndex(this)].RealValue;
  160. // }
  161. // if (length < 0.25 && length >= 0.125) {
  162. // return 8;
  163. // } else if (length < 0.125 && length >= 0.0625) {
  164. // return 16;
  165. // } else if (length < 0.0625 && length >= 0.03125) {
  166. // return 32;
  167. // } else {
  168. // return 64;
  169. // }
  170. //}
  171. }
  172. export enum Appearance {
  173. Normal,
  174. Grace,
  175. Cue
  176. }