SourceStaffEntry.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {Fraction} from "../../Common/DataObjects/fraction";
  2. import {VerticalSourceStaffEntryContainer} from "./VerticalSourceStaffEntryContainer";
  3. import {Staff} from "./Staff";
  4. import {AbstractNotationInstruction} from "./Instructions/AbstractNotationInstruction";
  5. import {VoiceEntry} from "./VoiceEntry";
  6. import {Note} from "./Note";
  7. import {StaffEntryLink} from "./StaffEntryLink";
  8. // FIXME
  9. type ChordSymbolContainer = any;
  10. export class SourceStaffEntry {
  11. constructor(verticalContainerParent: VerticalSourceStaffEntryContainer, parentStaff: Staff) {
  12. this.verticalContainerParent = verticalContainerParent;
  13. this.parentStaff = parentStaff;
  14. }
  15. private parentStaff: Staff;
  16. private verticalContainerParent: VerticalSourceStaffEntryContainer;
  17. private voiceEntries: VoiceEntry[] = [];
  18. private staffEntryLink: StaffEntryLink;
  19. private instructions: AbstractNotationInstruction[] = [];
  20. //private graceVoiceEntriesBefore: VoiceEntry[] = [];
  21. //private graceVoiceEntriesAfter: VoiceEntry[] = [];
  22. private chordSymbolContainer: ChordSymbolContainer;
  23. public get ParentStaff(): Staff {
  24. return this.parentStaff;
  25. }
  26. public get VerticalContainerParent(): VerticalSourceStaffEntryContainer {
  27. return this.verticalContainerParent;
  28. }
  29. public get Timestamp(): Fraction {
  30. if (this.VerticalContainerParent !== undefined) {
  31. return this.VerticalContainerParent.Timestamp;
  32. }
  33. return undefined;
  34. }
  35. public get AbsoluteTimestamp(): Fraction {
  36. if (this.VerticalContainerParent !== undefined) {
  37. return this.VerticalContainerParent.ParentMeasure.AbsoluteTimestamp + this.VerticalContainerParent.Timestamp;
  38. }
  39. return undefined;
  40. }
  41. public get VoiceEntries(): VoiceEntry[] {
  42. return this.voiceEntries;
  43. }
  44. public set VoiceEntries(value: VoiceEntry[]) {
  45. this.voiceEntries = value;
  46. }
  47. public get Link(): StaffEntryLink {
  48. return this.staffEntryLink;
  49. }
  50. public set Link(value: StaffEntryLink) {
  51. this.staffEntryLink = value;
  52. }
  53. public get Instructions(): AbstractNotationInstruction[] {
  54. return this.instructions;
  55. }
  56. public set Instructions(value: AbstractNotationInstruction[]) {
  57. this.instructions = value;
  58. }
  59. public get ChordContainer(): ChordSymbolContainer {
  60. return this.chordSymbolContainer;
  61. }
  62. public set ChordContainer(value: ChordSymbolContainer) {
  63. this.chordSymbolContainer = value;
  64. }
  65. public removeAllInstructionsOfType<T>(): number {
  66. let i: number = 0;
  67. let ret: number = 0;
  68. while (i < this.instructions.length) {
  69. if (this.instructions[i] instanceof T) {
  70. this.instructions.splice(i, 1);
  71. ret++;
  72. } else { i++; }
  73. }
  74. return ret;
  75. }
  76. public removeFirstInstructionOfType<T>(): boolean {
  77. for (let i: number = 0; i < this.instructions.length; i++) {
  78. if (this.instructions[i] instanceof T) {
  79. this.instructions.splice(i, 1);
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. public calculateMinNoteLength(): Fraction {
  86. let duration: Fraction = new Fraction(Number.MAX_VALUE, 1);
  87. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  88. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  89. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  90. let note: Note = voiceEntry.Notes[idx2];
  91. if (note.NoteTie !== undefined) {
  92. if (duration > note.calculateNoteLengthWithoutTie()) {
  93. duration = note.calculateNoteLengthWithoutTie();
  94. }
  95. } else if (duration > note.Length) {
  96. duration = note.Length;
  97. }
  98. }
  99. }
  100. return duration;
  101. }
  102. public calculateMaxNoteLength(): Fraction {
  103. let duration: Fraction = new Fraction(0, 1);
  104. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  105. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  106. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  107. let note: Note = voiceEntry.Notes[idx2];
  108. if (note.NoteTie !== undefined) {
  109. if (duration < note.calculateNoteLengthWithoutTie()) {
  110. duration = note.calculateNoteLengthWithoutTie();
  111. for (let idx3: number = 0, len3: number = note.NoteTie.Fractions.length; idx3 < len3; ++idx3) {
  112. let fraction: Fraction = note.NoteTie.Fractions[idx3];
  113. duration.Add(fraction);
  114. }
  115. }
  116. } else if (duration < note.Length) { duration = note.Length; }
  117. }
  118. }
  119. return duration;
  120. }
  121. public hasNotes(): boolean {
  122. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  123. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  124. if (voiceEntry.Notes.length > 0) { return true; }
  125. }
  126. return false;
  127. }
  128. public hasTie(): boolean {
  129. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  130. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  131. if (voiceEntry.hasTie()) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. public findLinkedNotes(linkedNotes: Note[]): void {
  138. for (let idx: number = 0, len: number = this.voiceEntries.length; idx < len; ++idx) {
  139. let voiceEntry: VoiceEntry = this.voiceEntries[idx];
  140. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  141. let note: Note = voiceEntry.Notes[idx2];
  142. if (note.ParentStaffEntry === this) {
  143. linkedNotes.push(note);
  144. }
  145. }
  146. }
  147. }
  148. }