SourceStaffEntry.ts 5.5 KB

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