GraphicalStaffEntry.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var BoundingBox_1 = require("./BoundingBox");
  8. var fraction_1 = require("../../Common/DataObjects/fraction");
  9. var LinkedVoice_1 = require("../VoiceData/LinkedVoice");
  10. var GraphicalObject_1 = require("./GraphicalObject");
  11. var collectionUtil_1 = require("../../Util/collectionUtil");
  12. var GraphicalStaffEntry = (function (_super) {
  13. __extends(GraphicalStaffEntry, _super);
  14. function GraphicalStaffEntry(parentMeasure, sourceStaffEntry, staffEntryParent) {
  15. if (sourceStaffEntry === void 0) { sourceStaffEntry = undefined; }
  16. if (staffEntryParent === void 0) { staffEntryParent = undefined; }
  17. _super.call(this);
  18. this.graphicalInstructions = [];
  19. this.graphicalTies = [];
  20. this.lyricsEntries = [];
  21. this.parentMeasure = parentMeasure;
  22. this.notes = [];
  23. this.graceStaffEntriesBefore = [];
  24. this.graceStaffEntriesAfter = [];
  25. this.sourceStaffEntry = sourceStaffEntry;
  26. if (staffEntryParent !== undefined) {
  27. this.staffEntryParent = staffEntryParent;
  28. this.parentVerticalContainer = staffEntryParent.parentVerticalContainer;
  29. this.PositionAndShape = new BoundingBox_1.BoundingBox(this, staffEntryParent.PositionAndShape);
  30. }
  31. else {
  32. this.PositionAndShape = new BoundingBox_1.BoundingBox(this, parentMeasure.PositionAndShape);
  33. }
  34. if (sourceStaffEntry !== undefined) {
  35. this.relInMeasureTimestamp = sourceStaffEntry.Timestamp;
  36. }
  37. }
  38. Object.defineProperty(GraphicalStaffEntry.prototype, "GraphicalInstructions", {
  39. get: function () {
  40. return this.graphicalInstructions;
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. Object.defineProperty(GraphicalStaffEntry.prototype, "GraphicalTies", {
  46. get: function () {
  47. return this.graphicalTies;
  48. },
  49. enumerable: true,
  50. configurable: true
  51. });
  52. Object.defineProperty(GraphicalStaffEntry.prototype, "LyricsEntries", {
  53. get: function () {
  54. return this.lyricsEntries;
  55. },
  56. enumerable: true,
  57. configurable: true
  58. });
  59. GraphicalStaffEntry.prototype.getAbsoluteTimestamp = function () {
  60. var result = fraction_1.Fraction.createFromFraction(this.parentMeasure.parentSourceMeasure.AbsoluteTimestamp);
  61. if (this.relInMeasureTimestamp !== undefined) {
  62. result.Add(this.relInMeasureTimestamp);
  63. }
  64. return result;
  65. };
  66. GraphicalStaffEntry.prototype.findEndTieGraphicalNoteFromNote = function (tieNote) {
  67. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  68. var graphicalNotes = this.notes[idx];
  69. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  70. var graphicalNote = graphicalNotes[idx2];
  71. var note = graphicalNote.sourceNote;
  72. if (note.Pitch !== undefined && note.Pitch.FundamentalNote === tieNote.Pitch.FundamentalNote
  73. && note.Pitch.Octave === tieNote.Pitch.Octave && note.getAbsoluteTimestamp() === tieNote.getAbsoluteTimestamp()) {
  74. return graphicalNote;
  75. }
  76. }
  77. }
  78. return undefined;
  79. };
  80. GraphicalStaffEntry.prototype.findEndTieGraphicalNoteFromNoteWithStartingSlur = function (tieNote, slur) {
  81. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  82. var graphicalNotes = this.notes[idx];
  83. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  84. var graphicalNote = graphicalNotes[idx2];
  85. var note = graphicalNote.sourceNote;
  86. if (note.NoteTie !== undefined && note.NoteSlurs.indexOf(slur) !== -1) {
  87. return graphicalNote;
  88. }
  89. }
  90. }
  91. return undefined;
  92. };
  93. GraphicalStaffEntry.prototype.findEndTieGraphicalNoteFromNoteWithEndingSlur = function (tieNote) {
  94. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  95. var graphicalNotes = this.notes[idx];
  96. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  97. var graphicalNote = graphicalNotes[idx2];
  98. var note = graphicalNote.sourceNote;
  99. if (note.Pitch !== undefined && note.Pitch.FundamentalNote === tieNote.Pitch.FundamentalNote
  100. && note.Pitch.Octave === tieNote.Pitch.Octave && this.getAbsoluteTimestamp() === tieNote.getAbsoluteTimestamp()) {
  101. return graphicalNote;
  102. }
  103. }
  104. }
  105. return undefined;
  106. };
  107. GraphicalStaffEntry.prototype.findGraphicalNoteFromGraceNote = function (graceNote) {
  108. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  109. var graphicalNotes = this.notes[idx];
  110. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  111. var graphicalNote = graphicalNotes[idx2];
  112. if (graphicalNote.sourceNote === graceNote) {
  113. return graphicalNote;
  114. }
  115. }
  116. }
  117. return undefined;
  118. };
  119. GraphicalStaffEntry.prototype.findGraphicalNoteFromNote = function (baseNote) {
  120. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  121. var graphicalNotes = this.notes[idx];
  122. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  123. var graphicalNote = graphicalNotes[idx2];
  124. if (graphicalNote.sourceNote === baseNote && this.getAbsoluteTimestamp() === baseNote.getAbsoluteTimestamp()) {
  125. return graphicalNote;
  126. }
  127. }
  128. }
  129. return undefined;
  130. };
  131. GraphicalStaffEntry.prototype.getGraphicalNoteDurationFromVoice = function (voice) {
  132. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  133. var graphicalNotes = this.notes[idx];
  134. if (graphicalNotes[0].sourceNote.ParentVoiceEntry.ParentVoice === voice) {
  135. return graphicalNotes[0].graphicalNoteLength;
  136. }
  137. }
  138. return new fraction_1.Fraction(0, 1);
  139. };
  140. GraphicalStaffEntry.prototype.findLinkedNotes = function (notLinkedNotes) {
  141. if (this.sourceStaffEntry !== undefined && this.sourceStaffEntry.Link !== undefined) {
  142. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  143. var graphicalNotes = this.notes[idx];
  144. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  145. var graphicalNote = graphicalNotes[idx2];
  146. if (graphicalNote.parentStaffEntry === this) {
  147. notLinkedNotes.push(graphicalNote);
  148. }
  149. }
  150. }
  151. }
  152. };
  153. GraphicalStaffEntry.prototype.findVoiceEntryGraphicalNotes = function (voiceEntry) {
  154. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  155. var graphicalNotes = this.notes[idx];
  156. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  157. var graphicalNote = graphicalNotes[idx2];
  158. if (graphicalNote.sourceNote.ParentVoiceEntry === voiceEntry) {
  159. return graphicalNotes;
  160. }
  161. }
  162. }
  163. return undefined;
  164. };
  165. GraphicalStaffEntry.prototype.isVoiceEntryPartOfLinkedVoiceEntry = function (voiceEntry) {
  166. if (this.sourceStaffEntry.Link !== undefined) {
  167. for (var idx = 0, len = this.sourceStaffEntry.Link.LinkStaffEntries.length; idx < len; ++idx) {
  168. var sEntry = this.sourceStaffEntry.Link.LinkStaffEntries[idx];
  169. if (sEntry.VoiceEntries.indexOf(voiceEntry) !== -1 && sEntry !== this.sourceStaffEntry) {
  170. return true;
  171. }
  172. }
  173. }
  174. return false;
  175. };
  176. GraphicalStaffEntry.prototype.getMainVoice = function () {
  177. for (var idx = 0, len = this.sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  178. var voiceEntry = this.sourceStaffEntry.VoiceEntries[idx];
  179. if (!(voiceEntry.ParentVoice instanceof LinkedVoice_1.LinkedVoice)) {
  180. return voiceEntry.ParentVoice;
  181. }
  182. }
  183. return this.notes[0][0].sourceNote.ParentVoiceEntry.ParentVoice;
  184. };
  185. GraphicalStaffEntry.prototype.findStaffEntryMinNoteLength = function () {
  186. var minLength = new fraction_1.Fraction(Number.MAX_VALUE, 1);
  187. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  188. var graphicalNotes = this.notes[idx];
  189. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  190. var graphicalNote = graphicalNotes[idx2];
  191. var calNoteLen = graphicalNote.graphicalNoteLength;
  192. if (calNoteLen < minLength && calNoteLen.Numerator > 0) {
  193. minLength = calNoteLen;
  194. }
  195. }
  196. }
  197. return minLength;
  198. };
  199. GraphicalStaffEntry.prototype.findStaffEntryMaxNoteLength = function () {
  200. var maxLength = new fraction_1.Fraction(0, 1);
  201. for (var idx = 0, len = this.notes.length; idx < len; ++idx) {
  202. var graphicalNotes = this.notes[idx];
  203. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  204. var graphicalNote = graphicalNotes[idx2];
  205. var calNoteLen = graphicalNote.graphicalNoteLength;
  206. if (calNoteLen > maxLength && calNoteLen.Numerator > 0) {
  207. maxLength = calNoteLen;
  208. }
  209. }
  210. }
  211. return maxLength;
  212. };
  213. GraphicalStaffEntry.prototype.findOrCreateGraphicalNotesListFromVoiceEntry = function (voiceEntry) {
  214. var graphicalNotes;
  215. if (this.notes.length === 0) {
  216. graphicalNotes = [];
  217. this.notes.push(graphicalNotes);
  218. }
  219. else {
  220. for (var i = 0; i < this.notes.length; i++) {
  221. if (this.notes[i][0].sourceNote.ParentVoiceEntry.ParentVoice === voiceEntry.ParentVoice) {
  222. return this.notes[i];
  223. }
  224. }
  225. graphicalNotes = [];
  226. this.notes.push(graphicalNotes);
  227. }
  228. return graphicalNotes;
  229. };
  230. GraphicalStaffEntry.prototype.findOrCreateGraphicalNotesListFromGraphicalNote = function (graphicalNote) {
  231. var graphicalNotes;
  232. var tieStartSourceStaffEntry = graphicalNote.sourceNote.ParentStaffEntry;
  233. if (this.sourceStaffEntry !== tieStartSourceStaffEntry) {
  234. graphicalNotes = this.findOrCreateGraphicalNotesListFromVoiceEntry(graphicalNote.sourceNote.ParentVoiceEntry);
  235. }
  236. else {
  237. if (this.notes.length === 0) {
  238. graphicalNotes = [];
  239. this.notes.push(graphicalNotes);
  240. }
  241. else {
  242. for (var i = 0; i < this.notes.length; i++) {
  243. if (this.notes[i][0].sourceNote.ParentVoiceEntry.ParentVoice === graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice) {
  244. return this.notes[i];
  245. }
  246. }
  247. graphicalNotes = [];
  248. this.notes.push(graphicalNotes);
  249. }
  250. }
  251. return graphicalNotes;
  252. };
  253. GraphicalStaffEntry.prototype.addGraphicalNoteToListAtCorrectYPosition = function (graphicalNotes, graphicalNote) {
  254. if (graphicalNotes.length === 0 ||
  255. graphicalNote.PositionAndShape.RelativePosition.y < collectionUtil_1.CollectionUtil.last(graphicalNotes).PositionAndShape.RelativePosition.Y) {
  256. graphicalNotes.push(graphicalNote);
  257. }
  258. else {
  259. for (var i = graphicalNotes.length - 1; i >= 0; i--) {
  260. if (graphicalNotes[i].PositionAndShape.RelativePosition.y > graphicalNote.PositionAndShape.RelativePosition.y) {
  261. graphicalNotes.splice(i + 1, 0, graphicalNote);
  262. break;
  263. }
  264. if (i === 0) {
  265. graphicalNotes.splice(0, 0, graphicalNote);
  266. break;
  267. }
  268. }
  269. }
  270. };
  271. return GraphicalStaffEntry;
  272. }(GraphicalObject_1.GraphicalObject));
  273. exports.GraphicalStaffEntry = GraphicalStaffEntry;