Note.ts 5.8 KB

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