SourceStaffEntry.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import {ClefInstruction} from "./Instructions/ClefInstruction";
  10. import {KeyInstruction} from "./Instructions/KeyInstruction";
  11. import {RhythmInstruction} from "./Instructions/RhythmInstruction";
  12. export class SourceStaffEntry {
  13. constructor(verticalContainerParent: VerticalSourceStaffEntryContainer, parentStaff: Staff) {
  14. this.verticalContainerParent = verticalContainerParent;
  15. this.parentStaff = parentStaff;
  16. }
  17. private parentStaff: Staff;
  18. private verticalContainerParent: VerticalSourceStaffEntryContainer;
  19. private voiceEntries: VoiceEntry[] = [];
  20. private staffEntryLink: StaffEntryLink;
  21. private instructions: AbstractNotationInstruction[] = [];
  22. //private graceVoiceEntriesBefore: VoiceEntry[] = [];
  23. //private graceVoiceEntriesAfter: VoiceEntry[] = [];
  24. private chordSymbolContainer: ChordSymbolContainer;
  25. public get ParentStaff(): Staff {
  26. return this.parentStaff;
  27. }
  28. public get VerticalContainerParent(): VerticalSourceStaffEntryContainer {
  29. return this.verticalContainerParent;
  30. }
  31. public get Timestamp(): Fraction {
  32. if (this.VerticalContainerParent !== undefined) {
  33. return this.VerticalContainerParent.Timestamp;
  34. }
  35. return undefined;
  36. }
  37. public get AbsoluteTimestamp(): Fraction {
  38. if (this.VerticalContainerParent !== undefined) {
  39. return Fraction.plus(this.VerticalContainerParent.ParentMeasure.AbsoluteTimestamp, this.VerticalContainerParent.Timestamp);
  40. }
  41. return undefined;
  42. }
  43. public get VoiceEntries(): VoiceEntry[] {
  44. return this.voiceEntries;
  45. }
  46. public set VoiceEntries(value: VoiceEntry[]) {
  47. this.voiceEntries = value;
  48. }
  49. public get Link(): StaffEntryLink {
  50. return this.staffEntryLink;
  51. }
  52. public set Link(value: StaffEntryLink) {
  53. this.staffEntryLink = value;
  54. }
  55. public get Instructions(): AbstractNotationInstruction[] {
  56. return this.instructions;
  57. }
  58. public set Instructions(value: AbstractNotationInstruction[]) {
  59. this.instructions = value;
  60. }
  61. public get ChordContainer(): ChordSymbolContainer {
  62. return this.chordSymbolContainer;
  63. }
  64. public set ChordContainer(value: ChordSymbolContainer) {
  65. this.chordSymbolContainer = value;
  66. }
  67. //public removeAllInstructionsOfType<T>(): number {
  68. // let i: number = 0;
  69. // let ret: number = 0;
  70. // while (i < this.instructions.length) {
  71. // if (this.instructions[i] instanceof T) {
  72. // this.instructions.splice(i, 1);
  73. // ret++;
  74. // } else { i++; }
  75. // }
  76. // return ret;
  77. //}
  78. //public removeFirstInstructionOfType<T>(): boolean {
  79. // for (let i: number = 0; i < this.instructions.length; i++) {
  80. // if (this.instructions[i] instanceof T) {
  81. // this.instructions.splice(i, 1);
  82. // return true;
  83. // }
  84. // }
  85. // return false;
  86. //}
  87. public removeAllInstructionsOfTypeClefInstruction(): number {
  88. let i: number = 0;
  89. let ret: number = 0;
  90. while (i < this.instructions.length) {
  91. if (this.instructions[i] instanceof ClefInstruction) {
  92. this.instructions.splice(i, 1);
  93. ret++;
  94. } else { i++; }
  95. }
  96. return ret;
  97. }
  98. public removeFirstInstructionOfTypeClefInstruction(): boolean {
  99. for (let i: number = 0; i < this.instructions.length; i++) {
  100. if (this.instructions[i] instanceof ClefInstruction) {
  101. this.instructions.splice(i, 1);
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public removeAllInstructionsOfTypeKeyInstruction(): number {
  108. let i: number = 0;
  109. let ret: number = 0;
  110. while (i < this.instructions.length) {
  111. if (this.instructions[i] instanceof KeyInstruction) {
  112. this.instructions.splice(i, 1);
  113. ret++;
  114. } else { i++; }
  115. }
  116. return ret;
  117. }
  118. public removeFirstInstructionOfTypeKeyInstruction(): boolean {
  119. for (let i: number = 0; i < this.instructions.length; i++) {
  120. if (this.instructions[i] instanceof KeyInstruction) {
  121. this.instructions.splice(i, 1);
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. public removeAllInstructionsOfTypeRhythmInstruction(): number {
  128. let i: number = 0;
  129. let ret: number = 0;
  130. while (i < this.instructions.length) {
  131. if (this.instructions[i] instanceof RhythmInstruction) {
  132. this.instructions.splice(i, 1);
  133. ret++;
  134. } else { i++; }
  135. }
  136. return ret;
  137. }
  138. public calculateMinNoteLength(): Fraction {
  139. let duration: Fraction = new Fraction(Number.MAX_VALUE, 1);
  140. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  141. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  142. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  143. let note: Note = voiceEntry.Notes[idx2];
  144. if (note.NoteTie !== undefined) {
  145. if (duration > note.calculateNoteLengthWithoutTie()) {
  146. duration = note.calculateNoteLengthWithoutTie();
  147. }
  148. } else if (duration > note.length) {
  149. duration = note.length;
  150. }
  151. }
  152. }
  153. return duration;
  154. }
  155. public calculateMaxNoteLength(): Fraction {
  156. let duration: Fraction = new Fraction(0, 1);
  157. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  158. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  159. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  160. let note: Note = voiceEntry.Notes[idx2];
  161. if (note.NoteTie !== undefined) {
  162. if (duration < note.calculateNoteLengthWithoutTie()) {
  163. duration = note.calculateNoteLengthWithoutTie();
  164. for (let idx3: number = 0, len3: number = note.NoteTie.Fractions.length; idx3 < len3; ++idx3) {
  165. let fraction: Fraction = note.NoteTie.Fractions[idx3];
  166. duration.push(fraction);
  167. }
  168. }
  169. } else if (duration < note.length) { duration = note.length; }
  170. }
  171. }
  172. return duration;
  173. }
  174. public hasNotes(): boolean {
  175. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  176. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  177. if (voiceEntry.Notes.length > 0) { return true; }
  178. }
  179. return false;
  180. }
  181. public hasTie(): boolean {
  182. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  183. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  184. if (voiceEntry.hasTie()) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. public findLinkedNotes(linkedNotes: Note[]): void {
  191. for (let idx: number = 0, len: number = this.voiceEntries.length; idx < len; ++idx) {
  192. let voiceEntry: VoiceEntry = this.voiceEntries[idx];
  193. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  194. let note: Note = voiceEntry.Notes[idx2];
  195. if (note.ParentStaffEntry === this) {
  196. linkedNotes.push(note);
  197. }
  198. }
  199. }
  200. }
  201. }