SourceStaffEntry.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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 Fraction.plus(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(type: AbstractNotationInstruction): number {
  66. // let i: number = 0;
  67. // let ret: number = 0;
  68. // while (i < this.instructions.length) {
  69. // let instruction: AbstractNotationInstruction = this.instructions[i];
  70. // if (instruction instanceof type) {
  71. // this.instructions.splice(i, 1);
  72. // ret++;
  73. // } else {
  74. // i++;
  75. // }
  76. // }
  77. // return ret;
  78. // }
  79. //
  80. // public removeFirstInstructionOfType(type: AbstractNotationInstruction): boolean {
  81. // for (let i: number = 0; i < this.instructions.length; i++) {
  82. // if (this.instructions[i] instanceof type) {
  83. // this.instructions.splice(i, 1);
  84. // return true;
  85. // }
  86. // }
  87. // return false;
  88. // }
  89. public removeAllInstructionsOfTypeClefInstruction(): number {
  90. let i: number = 0;
  91. let ret: number = 0;
  92. while (i < this.instructions.length) {
  93. if (this.instructions[i] instanceof ClefInstruction) {
  94. this.instructions.splice(i, 1);
  95. ret++;
  96. } else {
  97. i++;
  98. }
  99. }
  100. return ret;
  101. }
  102. public removeFirstInstructionOfTypeClefInstruction(): boolean {
  103. for (let i: number = 0; i < this.instructions.length; i++) {
  104. if (this.instructions[i] instanceof ClefInstruction) {
  105. this.instructions.splice(i, 1);
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. public removeAllInstructionsOfTypeKeyInstruction(): number {
  112. let i: number = 0;
  113. let ret: number = 0;
  114. while (i < this.instructions.length) {
  115. if (this.instructions[i] instanceof KeyInstruction) {
  116. this.instructions.splice(i, 1);
  117. ret++;
  118. } else {
  119. i++;
  120. }
  121. }
  122. return ret;
  123. }
  124. public removeFirstInstructionOfTypeKeyInstruction(): boolean {
  125. for (let i: number = 0; i < this.instructions.length; i++) {
  126. if (this.instructions[i] instanceof KeyInstruction) {
  127. this.instructions.splice(i, 1);
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. public removeAllInstructionsOfTypeRhythmInstruction(): number {
  134. let i: number = 0;
  135. let ret: number = 0;
  136. while (i < this.instructions.length) {
  137. if (this.instructions[i] instanceof RhythmInstruction) {
  138. this.instructions.splice(i, 1);
  139. ret++;
  140. } else {
  141. i++;
  142. }
  143. }
  144. return ret;
  145. }
  146. public removeFirstInstructionOfTypeRhythmInstruction(): boolean {
  147. for (let i: number = 0; i < this.instructions.length; i++) {
  148. if (this.instructions[i] instanceof RhythmInstruction) {
  149. this.instructions.splice(i, 1);
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. public calculateMinNoteLength(): Fraction {
  156. let duration: Fraction = new Fraction(Number.MAX_VALUE, 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. }
  165. } else if (duration > note.Length) {
  166. duration = note.Length;
  167. }
  168. }
  169. }
  170. return duration;
  171. }
  172. public calculateMaxNoteLength(): Fraction {
  173. let duration: Fraction = new Fraction(0, 1);
  174. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  175. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  176. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  177. let note: Note = voiceEntry.Notes[idx2];
  178. if (note.NoteTie !== undefined) {
  179. if (duration < note.calculateNoteLengthWithoutTie()) {
  180. duration = note.calculateNoteLengthWithoutTie();
  181. for (let idx3: number = 0, len3: number = note.NoteTie.Fractions.length; idx3 < len3; ++idx3) {
  182. let fraction: Fraction = note.NoteTie.Fractions[idx3];
  183. duration.Add(fraction);
  184. }
  185. }
  186. } else if (duration < note.Length) {
  187. duration = note.Length;
  188. }
  189. }
  190. }
  191. return duration;
  192. }
  193. public hasNotes(): boolean {
  194. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  195. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  196. if (voiceEntry.Notes.length > 0) {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. public hasTie(): boolean {
  203. for (let idx: number = 0, len: number = this.VoiceEntries.length; idx < len; ++idx) {
  204. let voiceEntry: VoiceEntry = this.VoiceEntries[idx];
  205. if (voiceEntry.hasTie()) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. public findLinkedNotes(linkedNotes: Note[]): void {
  212. for (let idx: number = 0, len: number = this.voiceEntries.length; idx < len; ++idx) {
  213. let voiceEntry: VoiceEntry = this.voiceEntries[idx];
  214. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  215. let note: Note = voiceEntry.Notes[idx2];
  216. if (note.ParentStaffEntry === this) {
  217. linkedNotes.push(note);
  218. }
  219. }
  220. }
  221. }
  222. }