GraphicalStaffEntry.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
  2. import {BoundingBox} from "./BoundingBox";
  3. import {Fraction} from "../../Common/DataObjects/fraction";
  4. import {VerticalGraphicalStaffEntryContainer} from "./VerticalGraphicalStaffEntryContainer";
  5. import {Note} from "../VoiceData/Note";
  6. import {Slur} from "../VoiceData/Expressions/ContinuousExpressions/Slur";
  7. import {Voice} from "../VoiceData/Voice";
  8. import {VoiceEntry} from "../VoiceData/VoiceEntry";
  9. import {LinkedVoice} from "../VoiceData/LinkedVoice";
  10. import {GraphicalTie} from "./GraphicalTie";
  11. import {GraphicalObject} from "./GraphicalObject";
  12. import {StaffMeasure} from "./StaffMeasure";
  13. import {GraphicalNote} from "./GraphicalNote";
  14. import {GraphicalChordSymbolContainer} from "./GraphicalChordSymbolContainer";
  15. import {GraphicalLyricEntry} from "./GraphicalLyricEntry";
  16. import {AbstractGraphicalInstruction} from "./AbstractGraphicalInstruction";
  17. import {GraphicalStaffEntryLink} from "./GraphicalStaffEntryLink";
  18. export class GraphicalStaffEntry extends GraphicalObject {
  19. private graphicalInstructions: List<AbstractGraphicalInstruction> = new List<AbstractGraphicalInstruction>();
  20. private graphicalTies: List<GraphicalTie> = new List<GraphicalTie>();
  21. private lyricsEntries: List<GraphicalLyricEntry> = new List<GraphicalLyricEntry>();
  22. constructor(parentMeasure: StaffMeasure, sourceStaffEntry: SourceStaffEntry = null, staffEntryParent: GraphicalStaffEntry = null) {
  23. this.ParentMeasure = parentMeasure;
  24. this.Notes = new List<List<GraphicalNote>>();
  25. this.GraceStaffEntriesBefore = new List<GraphicalStaffEntry>();
  26. this.GraceStaffEntriesAfter = new List<GraphicalStaffEntry>();
  27. this.SourceStaffEntry = sourceStaffEntry;
  28. if (staffEntryParent != null) {
  29. this.StaffEntryParent = staffEntryParent;
  30. this.ParentVerticalContainer = staffEntryParent.ParentVerticalContainer;
  31. this.PositionAndShape = new BoundingBox(staffEntryParent.PositionAndShape, this);
  32. }
  33. else this.PositionAndShape = new BoundingBox(parentMeasure.PositionAndShape, this);
  34. if (sourceStaffEntry != null)
  35. this.RelInMeasureTimestamp = sourceStaffEntry.Timestamp;
  36. }
  37. public GraphicalChordContainer: GraphicalChordSymbolContainer;
  38. public GraphicalLink: GraphicalStaffEntryLink;
  39. public RelInMeasureTimestamp: Fraction;
  40. public SourceStaffEntry: SourceStaffEntry;
  41. public ParentMeasure: StaffMeasure;
  42. public Notes: List<List<GraphicalNote>>;
  43. public GraceStaffEntriesBefore: List<GraphicalStaffEntry>;
  44. public GraceStaffEntriesAfter: List<GraphicalStaffEntry>;
  45. public StaffEntryParent: GraphicalStaffEntry;
  46. public ParentVerticalContainer: VerticalGraphicalStaffEntryContainer;
  47. public get GraphicalInstructions(): List<AbstractGraphicalInstruction> {
  48. return this.graphicalInstructions;
  49. }
  50. public get GraphicalTies(): List<GraphicalTie> {
  51. return this.graphicalTies;
  52. }
  53. public get LyricsEntries(): List<GraphicalLyricEntry> {
  54. return this.lyricsEntries;
  55. }
  56. public getAbsoluteTimestamp(): Fraction {
  57. var result: Fraction = Fraction.CreateFractionFromFraction(this.ParentMeasure.ParentSourceMeasure.AbsoluteTimestamp);
  58. if (this.RelInMeasureTimestamp != null)
  59. result += this.RelInMeasureTimestamp;
  60. return result;
  61. }
  62. public findEndTieGraphicalNoteFromNote(tieNote: Note): GraphicalNote {
  63. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  64. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  65. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  66. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  67. var note: Note = graphicalNote.SourceNote;
  68. if (note.Pitch != null && note.Pitch.FundamentalNote == tieNote.Pitch.FundamentalNote && note.Pitch.Octave == tieNote.Pitch.Octave && note.getAbsoluteTimestamp() == tieNote.getAbsoluteTimestamp())
  69. return graphicalNote;
  70. }
  71. }
  72. return null;
  73. }
  74. public findEndTieGraphicalNoteFromNoteWithStartingSlur(tieNote: Note, slur: Slur): GraphicalNote {
  75. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  76. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  77. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  78. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  79. var note: Note = graphicalNote.SourceNote;
  80. if (note.NoteTie != null && note.NoteSlurs.Contains(slur))
  81. return graphicalNote;
  82. }
  83. }
  84. return null;
  85. }
  86. public findEndTieGraphicalNoteFromNoteWithEndingSlur(tieNote: Note): GraphicalNote {
  87. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  88. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  89. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  90. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  91. var note: Note = graphicalNote.SourceNote;
  92. if (note.Pitch != null && note.Pitch.FundamentalNote == tieNote.Pitch.FundamentalNote && note.Pitch.Octave == tieNote.Pitch.Octave && this.getAbsoluteTimestamp() == tieNote.getAbsoluteTimestamp())
  93. return graphicalNote;
  94. }
  95. }
  96. return null;
  97. }
  98. public findGraphicalNoteFromGraceNote(graceNote: Note): GraphicalNote {
  99. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  100. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  101. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  102. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  103. if (graphicalNote.SourceNote == graceNote)
  104. return graphicalNote;
  105. }
  106. }
  107. return null;
  108. }
  109. public findGraphicalNoteFromNote(baseNote: Note): GraphicalNote {
  110. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  111. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  112. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  113. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  114. if (graphicalNote.SourceNote == baseNote && this.getAbsoluteTimestamp() == baseNote.getAbsoluteTimestamp())
  115. return graphicalNote;
  116. }
  117. }
  118. return null;
  119. }
  120. public getGraphicalNoteDurationFromVoice(voice: Voice): Fraction {
  121. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  122. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  123. if (graphicalNotes[0].SourceNote.ParentVoiceEntry.ParentVoice == voice)
  124. return graphicalNotes[0].GraphicalNoteLength;
  125. }
  126. return new Fraction(0, 1);
  127. }
  128. public findLinkedNotes(notLinkedNotes: List<GraphicalNote>): void {
  129. if (this.SourceStaffEntry != null && this.SourceStaffEntry.Link != null) {
  130. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  131. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  132. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  133. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  134. if (graphicalNote.ParentStaffEntry == this)
  135. notLinkedNotes.Add(graphicalNote);
  136. }
  137. }
  138. }
  139. }
  140. public findVoiceEntryGraphicalNotes(voiceEntry: VoiceEntry): List<GraphicalNote> {
  141. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  142. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  143. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  144. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  145. if (graphicalNote.SourceNote.ParentVoiceEntry == voiceEntry)
  146. return graphicalNotes;
  147. }
  148. }
  149. return null;
  150. }
  151. public isVoiceEntryPartOfLinkedVoiceEntry(voiceEntry: VoiceEntry): boolean {
  152. if (this.SourceStaffEntry.Link != null) {
  153. for (var idx: number = 0, len = this.SourceStaffEntry.Link.LinkStaffEntries.Count; idx < len; ++idx) {
  154. var sEntry: SourceStaffEntry = this.SourceStaffEntry.Link.LinkStaffEntries[idx];
  155. if (sEntry.VoiceEntries.Contains(voiceEntry) && sEntry != this.SourceStaffEntry)
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. public getMainVoice(): Voice {
  162. for (var idx: number = 0, len = this.SourceStaffEntry.VoiceEntries.Count; idx < len; ++idx) {
  163. var voiceEntry: VoiceEntry = this.SourceStaffEntry.VoiceEntries[idx];
  164. if (voiceEntry.ParentVoice.GetType() != /*typeof*/LinkedVoice)
  165. return voiceEntry.ParentVoice;
  166. }
  167. return this.Notes[0][0].SourceNote.ParentVoiceEntry.ParentVoice;
  168. }
  169. public findStaffEntryMinNoteLength(): Fraction {
  170. var minLength: Fraction = new Fraction(Int32.MaxValue, 1);
  171. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  172. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  173. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  174. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  175. var calNoteLen: Fraction = graphicalNote.GraphicalNoteLength;
  176. if (calNoteLen < minLength && calNoteLen.Numerator > 0)
  177. minLength = calNoteLen;
  178. }
  179. }
  180. return minLength;
  181. }
  182. public findStaffEntryMaxNoteLength(): Fraction {
  183. var maxLength: Fraction = new Fraction(0, 1);
  184. for (var idx: number = 0, len = this.Notes.Count; idx < len; ++idx) {
  185. var graphicalNotes: List<GraphicalNote> = this.Notes[idx];
  186. for (var idx2: number = 0, len2 = graphicalNotes.Count; idx2 < len2; ++idx2) {
  187. var graphicalNote: GraphicalNote = graphicalNotes[idx2];
  188. var calNoteLen: Fraction = graphicalNote.GraphicalNoteLength;
  189. if (calNoteLen > maxLength && calNoteLen.Numerator > 0)
  190. maxLength = calNoteLen;
  191. }
  192. }
  193. return maxLength;
  194. }
  195. public findOrCreateGraphicalNotesListFromVoiceEntry(voiceEntry: VoiceEntry): List<GraphicalNote> {
  196. var graphicalNotes: List<GraphicalNote>;
  197. if (this.Notes.Count == 0) {
  198. graphicalNotes = new List<GraphicalNote>();
  199. this.Notes.Add(graphicalNotes);
  200. }
  201. else {
  202. for (var i: number = 0; i < this.Notes.Count; i++) {
  203. if (this.Notes[i][0].SourceNote.ParentVoiceEntry.ParentVoice == voiceEntry.ParentVoice)
  204. return this.Notes[i];
  205. }
  206. graphicalNotes = new List<GraphicalNote>();
  207. this.Notes.Add(graphicalNotes);
  208. }
  209. return graphicalNotes;
  210. }
  211. public findOrCreateGraphicalNotesListFromGraphicalNote(graphicalNote: GraphicalNote): List<GraphicalNote> {
  212. var graphicalNotes: List<GraphicalNote>;
  213. var tieStartSourceStaffEntry: SourceStaffEntry = graphicalNote.SourceNote.ParentStaffEntry;
  214. if (this.SourceStaffEntry != tieStartSourceStaffEntry)
  215. graphicalNotes = this.findOrCreateGraphicalNotesListFromVoiceEntry(graphicalNote.SourceNote.ParentVoiceEntry);
  216. else {
  217. if (this.Notes.Count == 0) {
  218. graphicalNotes = new List<GraphicalNote>();
  219. this.Notes.Add(graphicalNotes);
  220. }
  221. else {
  222. for (var i: number = 0; i < this.Notes.Count; i++) {
  223. if (this.Notes[i][0].SourceNote.ParentVoiceEntry.ParentVoice == graphicalNote.SourceNote.ParentVoiceEntry.ParentVoice) {
  224. return this.Notes[i];
  225. }
  226. }
  227. graphicalNotes = new List<GraphicalNote>();
  228. this.Notes.Add(graphicalNotes);
  229. }
  230. }
  231. return graphicalNotes;
  232. }
  233. public addGraphicalNoteToListAtCorrectYPosition(graphicalNotes: List<GraphicalNote>, graphicalNote: GraphicalNote): void {
  234. if (graphicalNotes.Count == 0 || graphicalNote.PositionAndShape.RelativePosition.Y < graphicalNotes.Last().PositionAndShape.RelativePosition.Y)
  235. graphicalNotes.Add(graphicalNote);
  236. else {
  237. for (var i: number = graphicalNotes.Count - 1; i >= 0; i--) {
  238. if (graphicalNotes[i].PositionAndShape.RelativePosition.Y > graphicalNote.PositionAndShape.RelativePosition.Y) {
  239. graphicalNotes.Insert(i + 1, graphicalNote);
  240. break;
  241. }
  242. if (i == 0) {
  243. graphicalNotes.Insert(0, graphicalNote);
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. }