GraphicalMusicSheet.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. "use strict";
  2. var VerticalGraphicalStaffEntryContainer_1 = require("./VerticalGraphicalStaffEntryContainer");
  3. var GraphicalLine_1 = require("./GraphicalLine");
  4. var PointF2D_1 = require("../../Common/DataObjects/PointF2D");
  5. var ClefInstruction_1 = require("../VoiceData/Instructions/ClefInstruction");
  6. var KeyInstruction_1 = require("../VoiceData/Instructions/KeyInstruction");
  7. var fraction_1 = require("../../Common/DataObjects/fraction");
  8. var BoundingBox_1 = require("./BoundingBox");
  9. var Logging_1 = require("../../Common/Logging");
  10. var Dictionary_1 = require("typescript-collections/dist/lib/Dictionary");
  11. var collectionUtil_1 = require("../../Util/collectionUtil");
  12. var GraphicalMusicSheet = (function () {
  13. function GraphicalMusicSheet(musicSheet, calculator) {
  14. this.musicPages = [];
  15. this.measureList = [];
  16. this.verticalGraphicalStaffEntryContainers = [];
  17. this.cursors = [];
  18. this.leadSheet = false;
  19. this.musicSheet = musicSheet;
  20. this.numberOfStaves = this.musicSheet.Staves.length;
  21. this.calculator = calculator;
  22. this.sourceToGraphicalMeasureLinks = new Dictionary_1.default();
  23. this.calculator.initialize(this);
  24. }
  25. Object.defineProperty(GraphicalMusicSheet.prototype, "ParentMusicSheet", {
  26. get: function () {
  27. return this.musicSheet;
  28. },
  29. enumerable: true,
  30. configurable: true
  31. });
  32. Object.defineProperty(GraphicalMusicSheet.prototype, "GetCalculator", {
  33. get: function () {
  34. return this.calculator;
  35. },
  36. enumerable: true,
  37. configurable: true
  38. });
  39. Object.defineProperty(GraphicalMusicSheet.prototype, "MusicPages", {
  40. get: function () {
  41. return this.musicPages;
  42. },
  43. set: function (value) {
  44. this.musicPages = value;
  45. },
  46. enumerable: true,
  47. configurable: true
  48. });
  49. Object.defineProperty(GraphicalMusicSheet.prototype, "MeasureList", {
  50. //public get FontInfo(): FontInfo {
  51. // return this.fontInfo;
  52. //}
  53. get: function () {
  54. return this.measureList;
  55. },
  56. set: function (value) {
  57. this.measureList = value;
  58. },
  59. enumerable: true,
  60. configurable: true
  61. });
  62. Object.defineProperty(GraphicalMusicSheet.prototype, "VerticalGraphicalStaffEntryContainers", {
  63. get: function () {
  64. return this.verticalGraphicalStaffEntryContainers;
  65. },
  66. set: function (value) {
  67. this.verticalGraphicalStaffEntryContainers = value;
  68. },
  69. enumerable: true,
  70. configurable: true
  71. });
  72. Object.defineProperty(GraphicalMusicSheet.prototype, "Title", {
  73. get: function () {
  74. return this.title;
  75. },
  76. set: function (value) {
  77. this.title = value;
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82. Object.defineProperty(GraphicalMusicSheet.prototype, "Subtitle", {
  83. get: function () {
  84. return this.subtitle;
  85. },
  86. set: function (value) {
  87. this.subtitle = value;
  88. },
  89. enumerable: true,
  90. configurable: true
  91. });
  92. Object.defineProperty(GraphicalMusicSheet.prototype, "Composer", {
  93. get: function () {
  94. return this.composer;
  95. },
  96. set: function (value) {
  97. this.composer = value;
  98. },
  99. enumerable: true,
  100. configurable: true
  101. });
  102. Object.defineProperty(GraphicalMusicSheet.prototype, "Lyricist", {
  103. get: function () {
  104. return this.lyricist;
  105. },
  106. set: function (value) {
  107. this.lyricist = value;
  108. },
  109. enumerable: true,
  110. configurable: true
  111. });
  112. Object.defineProperty(GraphicalMusicSheet.prototype, "Cursors", {
  113. get: function () {
  114. return this.cursors;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. Object.defineProperty(GraphicalMusicSheet.prototype, "SelectionStartSymbol", {
  120. get: function () {
  121. return this.selectionStartSymbol;
  122. },
  123. enumerable: true,
  124. configurable: true
  125. });
  126. Object.defineProperty(GraphicalMusicSheet.prototype, "SelectionEndSymbol", {
  127. get: function () {
  128. return this.selectionEndSymbol;
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. Object.defineProperty(GraphicalMusicSheet.prototype, "MinAllowedSystemWidth", {
  134. get: function () {
  135. return this.minAllowedSystemWidth;
  136. },
  137. set: function (value) {
  138. this.minAllowedSystemWidth = value;
  139. },
  140. enumerable: true,
  141. configurable: true
  142. });
  143. Object.defineProperty(GraphicalMusicSheet.prototype, "NumberOfStaves", {
  144. // public get SystemImages(): Dictionary<MusicSystem, SystemImageProperties> {
  145. // return this.systemImages;
  146. // }
  147. get: function () {
  148. return this.numberOfStaves;
  149. },
  150. enumerable: true,
  151. configurable: true
  152. });
  153. Object.defineProperty(GraphicalMusicSheet.prototype, "LeadSheet", {
  154. get: function () {
  155. return this.leadSheet;
  156. },
  157. set: function (value) {
  158. this.leadSheet = value;
  159. },
  160. enumerable: true,
  161. configurable: true
  162. });
  163. GraphicalMusicSheet.transformRelativeToAbsolutePosition = function (graphicalMusicSheet) {
  164. for (var i = 0; i < graphicalMusicSheet.MusicPages.length; i++) {
  165. var pageAbsolute = graphicalMusicSheet.MusicPages[i].setMusicPageAbsolutePosition(i, graphicalMusicSheet.ParentMusicSheet.rules);
  166. var page = graphicalMusicSheet.MusicPages[i];
  167. page.PositionAndShape.calculateAbsolutePositionsRecursive(pageAbsolute.x, pageAbsolute.y);
  168. }
  169. };
  170. GraphicalMusicSheet.prototype.Initialize = function () {
  171. this.verticalGraphicalStaffEntryContainers = [];
  172. this.musicPages = [];
  173. this.measureList = [];
  174. };
  175. GraphicalMusicSheet.prototype.reCalculate = function () {
  176. this.calculator.calculate();
  177. };
  178. GraphicalMusicSheet.prototype.prepare = function () {
  179. this.calculator.prepareGraphicalMusicSheet();
  180. };
  181. GraphicalMusicSheet.prototype.EnforceRedrawOfMusicSystems = function () {
  182. for (var idx = 0, len = this.musicPages.length; idx < len; ++idx) {
  183. var graphicalMusicPage = this.musicPages[idx];
  184. for (var idx2 = 0, len2 = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  185. var musicSystem = graphicalMusicPage.MusicSystems[idx2];
  186. musicSystem.needsToBeRedrawn = true;
  187. }
  188. }
  189. };
  190. GraphicalMusicSheet.prototype.getClickedObject = function (positionOnMusicSheet) {
  191. for (var idx = 0, len = this.MusicPages.length; idx < len; ++idx) {
  192. var graphicalMusicPage = this.MusicPages[idx];
  193. return graphicalMusicPage.PositionAndShape.getClickedObjectOfType(positionOnMusicSheet);
  194. }
  195. return undefined;
  196. };
  197. GraphicalMusicSheet.prototype.findGraphicalStaffEntryFromMeasureList = function (staffIndex, measureIndex, sourceStaffEntry) {
  198. for (var i = measureIndex; i < this.measureList.length; i++) {
  199. var graphicalMeasure = this.measureList[i][staffIndex];
  200. for (var idx = 0, len = graphicalMeasure.staffEntries.length; idx < len; ++idx) {
  201. var graphicalStaffEntry = graphicalMeasure.staffEntries[idx];
  202. if (graphicalStaffEntry.sourceStaffEntry === sourceStaffEntry) {
  203. return graphicalStaffEntry;
  204. }
  205. }
  206. }
  207. return undefined;
  208. };
  209. GraphicalMusicSheet.prototype.findNextGraphicalStaffEntry = function (staffIndex, measureIndex, graphicalStaffEntry) {
  210. var graphicalMeasure = graphicalStaffEntry.parentMeasure;
  211. var graphicalStaffEntryIndex = graphicalMeasure.staffEntries.indexOf(graphicalStaffEntry);
  212. if (graphicalStaffEntryIndex < graphicalMeasure.staffEntries.length - 1) {
  213. return graphicalMeasure.staffEntries[graphicalStaffEntryIndex + 1];
  214. }
  215. else if (measureIndex < this.measureList.length - 1) {
  216. var nextMeasure = this.measureList[measureIndex + 1][staffIndex];
  217. if (nextMeasure.staffEntries.length > 0) {
  218. return nextMeasure.staffEntries[0];
  219. }
  220. }
  221. return undefined;
  222. };
  223. GraphicalMusicSheet.prototype.getFirstVisibleMeasuresListFromIndeces = function (start, end) {
  224. var graphicalMeasures = [];
  225. var numberOfStaves = this.measureList[0].length;
  226. for (var i = start; i <= end; i++) {
  227. for (var j = 0; j < numberOfStaves; j++) {
  228. if (this.measureList[i][j].isVisible()) {
  229. graphicalMeasures.push(this.measureList[i][j]);
  230. break;
  231. }
  232. }
  233. }
  234. return graphicalMeasures;
  235. };
  236. GraphicalMusicSheet.prototype.orderMeasuresByStaffLine = function (measures) {
  237. var orderedMeasures = [];
  238. var mList = [];
  239. orderedMeasures.push(mList);
  240. for (var i = 0; i < measures.length; i++) {
  241. if (i === 0) {
  242. mList.push(measures[0]);
  243. }
  244. else {
  245. if (measures[i].ParentStaffLine === measures[i - 1].ParentStaffLine) {
  246. mList.push(measures[i]);
  247. }
  248. else {
  249. if (orderedMeasures.indexOf(mList) === -1) {
  250. orderedMeasures.push(mList);
  251. }
  252. mList = [];
  253. orderedMeasures.push(mList);
  254. mList.push(measures[i]);
  255. }
  256. }
  257. }
  258. return orderedMeasures;
  259. };
  260. GraphicalMusicSheet.prototype.initializeActiveClefs = function () {
  261. var activeClefs = [];
  262. var firstSourceMeasure = this.musicSheet.getFirstSourceMeasure();
  263. if (firstSourceMeasure !== undefined) {
  264. for (var i = 0; i < firstSourceMeasure.CompleteNumberOfStaves; i++) {
  265. var clef = new ClefInstruction_1.ClefInstruction();
  266. if (firstSourceMeasure.FirstInstructionsStaffEntries[i] !== undefined) {
  267. for (var idx = 0, len = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions.length; idx < len; ++idx) {
  268. var abstractNotationInstruction = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions[idx];
  269. if (abstractNotationInstruction instanceof ClefInstruction_1.ClefInstruction) {
  270. clef = abstractNotationInstruction;
  271. }
  272. }
  273. }
  274. activeClefs.push(clef);
  275. }
  276. }
  277. return activeClefs;
  278. };
  279. GraphicalMusicSheet.prototype.GetMainKey = function () {
  280. var firstSourceMeasure = this.musicSheet.getFirstSourceMeasure();
  281. if (firstSourceMeasure !== undefined) {
  282. for (var i = 0; i < firstSourceMeasure.CompleteNumberOfStaves; i++) {
  283. for (var idx = 0, len = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions.length; idx < len; ++idx) {
  284. var abstractNotationInstruction = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions[idx];
  285. if (abstractNotationInstruction instanceof KeyInstruction_1.KeyInstruction) {
  286. return abstractNotationInstruction;
  287. }
  288. }
  289. }
  290. }
  291. return undefined;
  292. };
  293. GraphicalMusicSheet.prototype.getOrCreateVerticalContainer = function (timestamp) {
  294. if (this.verticalGraphicalStaffEntryContainers.length === 0 ||
  295. timestamp.lt(collectionUtil_1.CollectionUtil.getLastElement(this.verticalGraphicalStaffEntryContainers).AbsoluteTimestamp)) {
  296. var verticalGraphicalStaffEntryContainer = new VerticalGraphicalStaffEntryContainer_1.VerticalGraphicalStaffEntryContainer(this.numberOfStaves, timestamp);
  297. this.verticalGraphicalStaffEntryContainers.push(verticalGraphicalStaffEntryContainer);
  298. return verticalGraphicalStaffEntryContainer;
  299. }
  300. for (var i = this.verticalGraphicalStaffEntryContainers.length - 1; i >= 0; i--) {
  301. if (this.verticalGraphicalStaffEntryContainers[i].AbsoluteTimestamp.lt(timestamp)) {
  302. var verticalGraphicalStaffEntryContainer = new VerticalGraphicalStaffEntryContainer_1.VerticalGraphicalStaffEntryContainer(this.numberOfStaves, timestamp);
  303. this.verticalGraphicalStaffEntryContainers.splice(i + 1, 0, verticalGraphicalStaffEntryContainer);
  304. return verticalGraphicalStaffEntryContainer;
  305. }
  306. if (this.verticalGraphicalStaffEntryContainers[i].AbsoluteTimestamp.Equals(timestamp)) {
  307. return this.verticalGraphicalStaffEntryContainers[i];
  308. }
  309. }
  310. return undefined;
  311. };
  312. GraphicalMusicSheet.prototype.GetVerticalContainerFromTimestamp = function (timestamp, startIndex) {
  313. if (startIndex === void 0) { startIndex = 0; }
  314. var index = collectionUtil_1.CollectionUtil.binarySearch(this.verticalGraphicalStaffEntryContainers, new VerticalGraphicalStaffEntryContainer_1.VerticalGraphicalStaffEntryContainer(0, timestamp), VerticalGraphicalStaffEntryContainer_1.VerticalGraphicalStaffEntryContainer.compareByTimestamp, startIndex, this.verticalGraphicalStaffEntryContainers.length - startIndex);
  315. if (index >= 0) {
  316. return this.verticalGraphicalStaffEntryContainers[index];
  317. }
  318. return undefined;
  319. };
  320. GraphicalMusicSheet.prototype.GetInterpolatedIndexInVerticalContainers = function (musicTimestamp) {
  321. var containers = this.verticalGraphicalStaffEntryContainers;
  322. var leftIndex = 0;
  323. var rightIndex = containers.length - 1;
  324. var foundIndex;
  325. var leftTS = undefined;
  326. var rightTS = undefined;
  327. if (musicTimestamp.lte(containers[containers.length - 1].AbsoluteTimestamp)) {
  328. while (rightIndex - leftIndex > 1) {
  329. var middleIndex = Math.floor((rightIndex + leftIndex) / 2);
  330. if (containers[leftIndex].AbsoluteTimestamp.Equals(musicTimestamp)) {
  331. rightIndex = leftIndex;
  332. break;
  333. }
  334. else if (containers[rightIndex].AbsoluteTimestamp.Equals(musicTimestamp)) {
  335. leftIndex = rightIndex;
  336. break;
  337. }
  338. else if (containers[middleIndex].AbsoluteTimestamp.Equals(musicTimestamp)) {
  339. return this.verticalGraphicalStaffEntryContainers.indexOf(containers[middleIndex]);
  340. }
  341. else if (musicTimestamp.lt(containers[middleIndex].AbsoluteTimestamp)) {
  342. rightIndex = middleIndex;
  343. }
  344. else {
  345. leftIndex = middleIndex;
  346. }
  347. }
  348. if (leftIndex === rightIndex) {
  349. return this.verticalGraphicalStaffEntryContainers.indexOf(containers[leftIndex]);
  350. }
  351. leftTS = containers[leftIndex].AbsoluteTimestamp;
  352. rightTS = containers[rightIndex].AbsoluteTimestamp;
  353. }
  354. else {
  355. leftTS = containers[containers.length - 1].AbsoluteTimestamp;
  356. rightTS = fraction_1.Fraction.plus(this.getLongestStaffEntryDuration(containers.length - 1), leftTS);
  357. rightIndex = containers.length;
  358. }
  359. var diff = rightTS.RealValue - leftTS.RealValue;
  360. var diffTS = rightTS.RealValue - musicTimestamp.RealValue;
  361. foundIndex = rightIndex - (diffTS / diff);
  362. return Math.min(foundIndex, this.verticalGraphicalStaffEntryContainers.length);
  363. };
  364. GraphicalMusicSheet.prototype.getVisibleStavesIndecesFromSourceMeasure = function (visibleMeasures) {
  365. var visibleInstruments = [];
  366. var visibleStavesIndeces = [];
  367. for (var idx = 0, len = visibleMeasures.length; idx < len; ++idx) {
  368. var graphicalMeasure = visibleMeasures[idx];
  369. var instrument = graphicalMeasure.ParentStaff.ParentInstrument;
  370. if (visibleInstruments.indexOf(instrument) === -1) {
  371. visibleInstruments.push(instrument);
  372. }
  373. }
  374. for (var idx = 0, len = visibleInstruments.length; idx < len; ++idx) {
  375. var instrument = visibleInstruments[idx];
  376. var index = this.musicSheet.getGlobalStaffIndexOfFirstStaff(instrument);
  377. for (var j = 0; j < instrument.Staves.length; j++) {
  378. visibleStavesIndeces.push(index + j);
  379. }
  380. }
  381. return visibleStavesIndeces;
  382. };
  383. GraphicalMusicSheet.prototype.getGraphicalMeasureFromSourceMeasureAndIndex = function (sourceMeasure, index) {
  384. for (var i = 0; i < this.measureList.length; i++) {
  385. if (this.measureList[i][0].parentSourceMeasure === sourceMeasure) {
  386. return this.measureList[i][index];
  387. }
  388. }
  389. return undefined;
  390. };
  391. GraphicalMusicSheet.prototype.getMeasureIndex = function (graphicalMeasure, measureIndex, inListIndex) {
  392. measureIndex = 0;
  393. inListIndex = 0;
  394. for (; measureIndex < this.measureList.length; measureIndex++) {
  395. for (var idx = 0, len = this.measureList[measureIndex].length; idx < len; ++idx) {
  396. var measure = this.measureList[measureIndex][idx];
  397. if (measure === graphicalMeasure) {
  398. return true;
  399. }
  400. }
  401. }
  402. return false;
  403. };
  404. GraphicalMusicSheet.prototype.GetNearesNote = function (clickPosition, maxClickDist) {
  405. var initialSearchArea = 10;
  406. var foundNotes = [];
  407. var region = new BoundingBox_1.BoundingBox();
  408. region.BorderLeft = clickPosition.x - initialSearchArea;
  409. region.BorderTop = clickPosition.y - initialSearchArea;
  410. region.BorderRight = clickPosition.x + initialSearchArea;
  411. region.BorderBottom = clickPosition.y + initialSearchArea;
  412. region.AbsolutePosition = new PointF2D_1.PointF2D(0, 0);
  413. for (var idx = 0, len = this.MusicPages.length; idx < len; ++idx) {
  414. var graphicalMusicPage = this.MusicPages[idx];
  415. var entries = graphicalMusicPage.PositionAndShape.getObjectsInRegion(region);
  416. //let entriesArr: GraphicalNote[] = __as__<GraphicalNote[]>(entries, GraphicalNote[]) ? ? entries;
  417. if (entries === undefined) {
  418. continue;
  419. }
  420. else {
  421. for (var idx2 = 0, len2 = entries.length; idx2 < len2; ++idx2) {
  422. var note = entries[idx2];
  423. if (Math.abs(note.PositionAndShape.AbsolutePosition.x - clickPosition.x) < maxClickDist.x
  424. && Math.abs(note.PositionAndShape.AbsolutePosition.y - clickPosition.y) < maxClickDist.y) {
  425. foundNotes.push(note);
  426. }
  427. }
  428. }
  429. }
  430. var closest = undefined;
  431. for (var idx = 0, len = foundNotes.length; idx < len; ++idx) {
  432. var note = foundNotes[idx];
  433. if (closest === undefined) {
  434. closest = note;
  435. }
  436. else {
  437. if (note.parentStaffEntry.relInMeasureTimestamp === undefined) {
  438. continue;
  439. }
  440. var deltaNew = this.CalculateDistance(note.PositionAndShape.AbsolutePosition, clickPosition);
  441. var deltaOld = this.CalculateDistance(closest.PositionAndShape.AbsolutePosition, clickPosition);
  442. if (deltaNew < deltaOld) {
  443. closest = note;
  444. }
  445. }
  446. }
  447. if (closest !== undefined) {
  448. return closest;
  449. }
  450. return undefined;
  451. };
  452. GraphicalMusicSheet.prototype.GetClickableLabel = function (clickPosition) {
  453. var initialSearchAreaX = 4;
  454. var initialSearchAreaY = 4;
  455. var region = new BoundingBox_1.BoundingBox();
  456. region.BorderLeft = clickPosition.x - initialSearchAreaX;
  457. region.BorderTop = clickPosition.y - initialSearchAreaY;
  458. region.BorderRight = clickPosition.x + initialSearchAreaX;
  459. region.BorderBottom = clickPosition.y + initialSearchAreaY;
  460. region.AbsolutePosition = new PointF2D_1.PointF2D(0, 0);
  461. for (var idx = 0, len = this.MusicPages.length; idx < len; ++idx) {
  462. var graphicalMusicPage = this.MusicPages[idx];
  463. var entries = graphicalMusicPage.PositionAndShape.getObjectsInRegion(region);
  464. if (entries.length !== 1) {
  465. continue;
  466. }
  467. else {
  468. for (var idx2 = 0, len2 = entries.length; idx2 < len2; ++idx2) {
  469. var clickedLabel = entries[idx2];
  470. return clickedLabel;
  471. }
  472. }
  473. }
  474. return undefined;
  475. };
  476. GraphicalMusicSheet.prototype.GetNearestStaffEntry = function (clickPosition) {
  477. var initialSearchArea = 10;
  478. var foundEntries = [];
  479. var region = new BoundingBox_1.BoundingBox(undefined);
  480. region.BorderLeft = clickPosition.x - initialSearchArea;
  481. region.BorderTop = clickPosition.y - initialSearchArea;
  482. region.BorderRight = clickPosition.x + initialSearchArea;
  483. region.BorderBottom = clickPosition.y + initialSearchArea;
  484. region.AbsolutePosition = new PointF2D_1.PointF2D(0, 0);
  485. for (var idx = 0, len = this.MusicPages.length; idx < len; ++idx) {
  486. var graphicalMusicPage = this.MusicPages[idx];
  487. var entries = graphicalMusicPage.PositionAndShape.getObjectsInRegion(region, false);
  488. if (entries === undefined || entries.length === 0) {
  489. continue;
  490. }
  491. else {
  492. for (var idx2 = 0, len2 = entries.length; idx2 < len2; ++idx2) {
  493. var gse = entries[idx2];
  494. foundEntries.push(gse);
  495. }
  496. }
  497. }
  498. var closest = undefined;
  499. for (var idx = 0, len = foundEntries.length; idx < len; ++idx) {
  500. var gse = foundEntries[idx];
  501. if (closest === undefined) {
  502. closest = gse;
  503. }
  504. else {
  505. if (gse.relInMeasureTimestamp === undefined) {
  506. continue;
  507. }
  508. var deltaNew = this.CalculateDistance(gse.PositionAndShape.AbsolutePosition, clickPosition);
  509. var deltaOld = this.CalculateDistance(closest.PositionAndShape.AbsolutePosition, clickPosition);
  510. if (deltaNew < deltaOld) {
  511. closest = gse;
  512. }
  513. }
  514. }
  515. if (closest !== undefined) {
  516. return closest;
  517. }
  518. return undefined;
  519. };
  520. GraphicalMusicSheet.prototype.GetPossibleCommentAnchor = function (clickPosition) {
  521. var entry = this.GetNearestStaffEntry(clickPosition);
  522. if (entry === undefined) {
  523. return undefined;
  524. }
  525. return entry.sourceStaffEntry;
  526. };
  527. GraphicalMusicSheet.prototype.getClickedObjectOfType = function (positionOnMusicSheet) {
  528. for (var idx = 0, len = this.musicPages.length; idx < len; ++idx) {
  529. var page = this.musicPages[idx];
  530. var o = page.PositionAndShape.getClickedObjectOfType(positionOnMusicSheet);
  531. if (o !== undefined) {
  532. return o;
  533. }
  534. }
  535. return undefined;
  536. };
  537. GraphicalMusicSheet.prototype.tryGetTimestampFromPosition = function (positionOnMusicSheet) {
  538. var entry = this.getClickedObjectOfType(positionOnMusicSheet);
  539. if (entry === undefined) {
  540. return undefined;
  541. }
  542. return entry.getAbsoluteTimestamp();
  543. };
  544. GraphicalMusicSheet.prototype.tryGetClickableLabel = function (positionOnMusicSheet) {
  545. try {
  546. return this.GetClickableLabel(positionOnMusicSheet);
  547. }
  548. catch (ex) {
  549. Logging_1.Logging.log("GraphicalMusicSheet.tryGetClickableObject", "positionOnMusicSheet: " + positionOnMusicSheet, ex);
  550. }
  551. return undefined;
  552. };
  553. GraphicalMusicSheet.prototype.tryGetTimeStampFromPosition = function (positionOnMusicSheet) {
  554. try {
  555. var entry = this.GetNearestStaffEntry(positionOnMusicSheet);
  556. if (entry === undefined) {
  557. return undefined;
  558. }
  559. return entry.getAbsoluteTimestamp();
  560. }
  561. catch (ex) {
  562. Logging_1.Logging.log("GraphicalMusicSheet.tryGetTimeStampFromPosition", "positionOnMusicSheet: " + positionOnMusicSheet, ex);
  563. }
  564. return undefined;
  565. };
  566. GraphicalMusicSheet.prototype.getStaffEntry = function (index) {
  567. var container = this.VerticalGraphicalStaffEntryContainers[index];
  568. var staffEntry = undefined;
  569. try {
  570. for (var idx = 0, len = container.StaffEntries.length; idx < len; ++idx) {
  571. var entry = container.StaffEntries[idx];
  572. if (entry === undefined || !entry.sourceStaffEntry.ParentStaff.ParentInstrument.Visible) {
  573. continue;
  574. }
  575. if (staffEntry === undefined) {
  576. staffEntry = entry;
  577. }
  578. else if (entry.PositionAndShape !== undefined && staffEntry.PositionAndShape !== undefined) {
  579. if (staffEntry.PositionAndShape.RelativePosition.x > entry.PositionAndShape.RelativePosition.x) {
  580. staffEntry = entry;
  581. }
  582. }
  583. }
  584. }
  585. catch (ex) {
  586. Logging_1.Logging.log("GraphicalMusicSheet.getStaffEntry", ex);
  587. }
  588. return staffEntry;
  589. };
  590. GraphicalMusicSheet.prototype.GetPreviousVisibleContainerIndex = function (index) {
  591. for (var i = index - 1; i >= 0; i--) {
  592. var entries = this.verticalGraphicalStaffEntryContainers[i].StaffEntries;
  593. for (var idx = 0, len = entries.length; idx < len; ++idx) {
  594. var entry = entries[idx];
  595. if (entry !== undefined && entry.sourceStaffEntry.ParentStaff.ParentInstrument.Visible) {
  596. return i;
  597. }
  598. }
  599. }
  600. return -1;
  601. };
  602. GraphicalMusicSheet.prototype.GetNextVisibleContainerIndex = function (index) {
  603. for (var i = index + 1; i < this.verticalGraphicalStaffEntryContainers.length; ++i) {
  604. var entries = this.verticalGraphicalStaffEntryContainers[i].StaffEntries;
  605. for (var idx = 0, len = entries.length; idx < len; ++idx) {
  606. var entry = entries[idx];
  607. if (entry !== undefined && entry.sourceStaffEntry.ParentStaff.ParentInstrument.Visible) {
  608. return i;
  609. }
  610. }
  611. }
  612. return -1;
  613. };
  614. GraphicalMusicSheet.prototype.findClosestLeftStaffEntry = function (fractionalIndex, searchOnlyVisibleEntries) {
  615. var foundEntry = undefined;
  616. var leftIndex = Math.floor(fractionalIndex);
  617. leftIndex = Math.min(this.VerticalGraphicalStaffEntryContainers.length - 1, leftIndex);
  618. for (var i = leftIndex; i >= 0; i--) {
  619. foundEntry = this.getStaffEntry(i);
  620. if (foundEntry !== undefined) {
  621. if (searchOnlyVisibleEntries) {
  622. if (foundEntry.sourceStaffEntry.ParentStaff.ParentInstrument.Visible) {
  623. return foundEntry;
  624. }
  625. }
  626. else {
  627. return foundEntry;
  628. }
  629. }
  630. }
  631. return undefined;
  632. };
  633. GraphicalMusicSheet.prototype.findClosestRightStaffEntry = function (fractionalIndex, returnOnlyVisibleEntries) {
  634. var foundEntry = undefined;
  635. var rightIndex = Math.max(0, Math.ceil(fractionalIndex));
  636. for (var i = rightIndex; i < this.VerticalGraphicalStaffEntryContainers.length; i++) {
  637. foundEntry = this.getStaffEntry(i);
  638. if (foundEntry !== undefined) {
  639. if (returnOnlyVisibleEntries) {
  640. if (foundEntry.sourceStaffEntry.ParentStaff.ParentInstrument.Visible) {
  641. return foundEntry;
  642. }
  643. }
  644. else {
  645. return foundEntry;
  646. }
  647. }
  648. }
  649. return undefined;
  650. };
  651. GraphicalMusicSheet.prototype.calculateCursorLineAtTimestamp = function (musicTimestamp, styleEnum) {
  652. var result = this.calculateXPositionFromTimestamp(musicTimestamp);
  653. var xPos = result[0];
  654. var correspondingMusicSystem = result[1];
  655. if (correspondingMusicSystem === undefined || correspondingMusicSystem.StaffLines.length === 0) {
  656. return undefined;
  657. }
  658. var yCoordinate = correspondingMusicSystem.PositionAndShape.AbsolutePosition.y;
  659. var height = collectionUtil_1.CollectionUtil.last(correspondingMusicSystem.StaffLines).PositionAndShape.RelativePosition.y + 4;
  660. return new GraphicalLine_1.GraphicalLine(new PointF2D_1.PointF2D(xPos, yCoordinate), new PointF2D_1.PointF2D(xPos, yCoordinate + height), 3, styleEnum);
  661. };
  662. GraphicalMusicSheet.prototype.calculateXPositionFromTimestamp = function (timeStamp) {
  663. var currentMusicSystem = undefined;
  664. var fractionalIndex = this.GetInterpolatedIndexInVerticalContainers(timeStamp);
  665. var previousStaffEntry = this.findClosestLeftStaffEntry(fractionalIndex, true);
  666. var nextStaffEntry = this.findClosestRightStaffEntry(fractionalIndex, true);
  667. var currentTimeStamp = timeStamp.RealValue;
  668. if (previousStaffEntry === undefined && nextStaffEntry === undefined) {
  669. return [0, undefined];
  670. }
  671. var previousStaffEntryMusicSystem = undefined;
  672. if (previousStaffEntry !== undefined) {
  673. previousStaffEntryMusicSystem = previousStaffEntry.parentMeasure.ParentStaffLine.ParentMusicSystem;
  674. }
  675. else {
  676. previousStaffEntryMusicSystem = nextStaffEntry.parentMeasure.ParentStaffLine.ParentMusicSystem;
  677. }
  678. var nextStaffEntryMusicSystem = undefined;
  679. if (nextStaffEntry !== undefined) {
  680. nextStaffEntryMusicSystem = nextStaffEntry.parentMeasure.ParentStaffLine.ParentMusicSystem;
  681. }
  682. else {
  683. nextStaffEntryMusicSystem = previousStaffEntry.parentMeasure.ParentStaffLine.ParentMusicSystem;
  684. }
  685. if (previousStaffEntryMusicSystem === nextStaffEntryMusicSystem) {
  686. currentMusicSystem = previousStaffEntryMusicSystem;
  687. var fraction = void 0;
  688. var previousStaffEntryPositionX = void 0;
  689. var nextStaffEntryPositionX = void 0;
  690. if (previousStaffEntry === undefined) {
  691. previousStaffEntryPositionX = nextStaffEntryPositionX = nextStaffEntry.PositionAndShape.AbsolutePosition.x;
  692. fraction = 0;
  693. }
  694. else if (nextStaffEntry === undefined) {
  695. previousStaffEntryPositionX = previousStaffEntry.PositionAndShape.AbsolutePosition.x;
  696. nextStaffEntryPositionX = currentMusicSystem.GetRightBorderAbsoluteXPosition();
  697. var sm = previousStaffEntry.parentMeasure.parentSourceMeasure;
  698. fraction = (currentTimeStamp - previousStaffEntry.getAbsoluteTimestamp().RealValue) / (fraction_1.Fraction.plus(sm.AbsoluteTimestamp, sm.Duration).RealValue - previousStaffEntry.getAbsoluteTimestamp().RealValue);
  699. }
  700. else {
  701. previousStaffEntryPositionX = previousStaffEntry.PositionAndShape.AbsolutePosition.x;
  702. nextStaffEntryPositionX = nextStaffEntry.PositionAndShape.AbsolutePosition.x;
  703. if (previousStaffEntry === nextStaffEntry) {
  704. fraction = 0;
  705. }
  706. else {
  707. fraction = (currentTimeStamp - previousStaffEntry.getAbsoluteTimestamp().RealValue) /
  708. (nextStaffEntry.getAbsoluteTimestamp().RealValue - previousStaffEntry.getAbsoluteTimestamp().RealValue);
  709. }
  710. }
  711. fraction = Math.min(1, Math.max(0, fraction));
  712. var interpolatedXPosition = previousStaffEntryPositionX + fraction * (nextStaffEntryPositionX - previousStaffEntryPositionX);
  713. return [interpolatedXPosition, currentMusicSystem];
  714. }
  715. else {
  716. var nextSystemLeftBorderTimeStamp = nextStaffEntry.parentMeasure.parentSourceMeasure.AbsoluteTimestamp.RealValue;
  717. var fraction = void 0;
  718. var interpolatedXPosition = void 0;
  719. if (currentTimeStamp < nextSystemLeftBorderTimeStamp) {
  720. currentMusicSystem = previousStaffEntryMusicSystem;
  721. var previousStaffEntryPositionX = previousStaffEntry.PositionAndShape.AbsolutePosition.x;
  722. var previousSystemRightBorderX = currentMusicSystem.GetRightBorderAbsoluteXPosition();
  723. fraction = (currentTimeStamp - previousStaffEntry.getAbsoluteTimestamp().RealValue) /
  724. (nextSystemLeftBorderTimeStamp - previousStaffEntry.getAbsoluteTimestamp().RealValue);
  725. fraction = Math.min(1, Math.max(0, fraction));
  726. interpolatedXPosition = previousStaffEntryPositionX + fraction * (previousSystemRightBorderX - previousStaffEntryPositionX);
  727. }
  728. else {
  729. currentMusicSystem = nextStaffEntryMusicSystem;
  730. var nextStaffEntryPositionX = nextStaffEntry.PositionAndShape.AbsolutePosition.x;
  731. var nextSystemLeftBorderX = currentMusicSystem.GetLeftBorderAbsoluteXPosition();
  732. fraction = (currentTimeStamp - nextSystemLeftBorderTimeStamp) /
  733. (nextStaffEntry.getAbsoluteTimestamp().RealValue - nextSystemLeftBorderTimeStamp);
  734. fraction = Math.min(1, Math.max(0, fraction));
  735. interpolatedXPosition = nextSystemLeftBorderX + fraction * (nextStaffEntryPositionX - nextSystemLeftBorderX);
  736. }
  737. return [interpolatedXPosition, currentMusicSystem];
  738. }
  739. };
  740. GraphicalMusicSheet.prototype.GetNumberOfVisibleInstruments = function () {
  741. var visibleInstrumentCount = 0;
  742. for (var idx = 0, len = this.musicSheet.Instruments.length; idx < len; ++idx) {
  743. var instrument = this.musicSheet.Instruments[idx];
  744. if (instrument.Visible === true) {
  745. visibleInstrumentCount++;
  746. }
  747. }
  748. return visibleInstrumentCount;
  749. };
  750. GraphicalMusicSheet.prototype.GetNumberOfFollowedInstruments = function () {
  751. var followedInstrumentCount = 0;
  752. for (var idx = 0, len = this.musicSheet.Instruments.length; idx < len; ++idx) {
  753. var instrument = this.musicSheet.Instruments[idx];
  754. if (instrument.Following === true) {
  755. followedInstrumentCount++;
  756. }
  757. }
  758. return followedInstrumentCount;
  759. };
  760. GraphicalMusicSheet.prototype.GetGraphicalFromSourceMeasure = function (sourceMeasure) {
  761. return this.sourceToGraphicalMeasureLinks.getValue(sourceMeasure);
  762. };
  763. GraphicalMusicSheet.prototype.GetGraphicalFromSourceStaffEntry = function (sourceStaffEntry) {
  764. var graphicalMeasure = this.GetGraphicalFromSourceMeasure(sourceStaffEntry.VerticalContainerParent.ParentMeasure)[sourceStaffEntry.ParentStaff.idInMusicSheet];
  765. return graphicalMeasure.findGraphicalStaffEntryFromTimestamp(sourceStaffEntry.Timestamp);
  766. };
  767. GraphicalMusicSheet.prototype.GetGraphicalNoteFromSourceNote = function (note, containingGse) {
  768. for (var idx = 0, len = containingGse.notes.length; idx < len; ++idx) {
  769. var graphicalNotes = containingGse.notes[idx];
  770. for (var idx2 = 0, len2 = graphicalNotes.length; idx2 < len2; ++idx2) {
  771. var graphicalNote = graphicalNotes[idx2];
  772. if (graphicalNote.sourceNote === note) {
  773. return graphicalNote;
  774. }
  775. }
  776. }
  777. return undefined;
  778. };
  779. GraphicalMusicSheet.prototype.CalculateDistance = function (pt1, pt2) {
  780. var deltaX = pt1.x - pt2.x;
  781. var deltaY = pt1.y - pt2.y;
  782. return (deltaX * deltaX) + (deltaY * deltaY);
  783. };
  784. GraphicalMusicSheet.prototype.getLongestStaffEntryDuration = function (index) {
  785. var maxLength = new fraction_1.Fraction(0, 1);
  786. for (var idx = 0, len = this.verticalGraphicalStaffEntryContainers[index].StaffEntries.length; idx < len; ++idx) {
  787. var graphicalStaffEntry = this.verticalGraphicalStaffEntryContainers[index].StaffEntries[idx];
  788. if (graphicalStaffEntry === undefined) {
  789. continue;
  790. }
  791. for (var idx2 = 0, len2 = graphicalStaffEntry.notes.length; idx2 < len2; ++idx2) {
  792. var graphicalNotes = graphicalStaffEntry.notes[idx2];
  793. for (var idx3 = 0, len3 = graphicalNotes.length; idx3 < len3; ++idx3) {
  794. var note = graphicalNotes[idx3];
  795. if (note.graphicalNoteLength > maxLength) {
  796. maxLength = note.graphicalNoteLength;
  797. }
  798. }
  799. }
  800. }
  801. return maxLength;
  802. };
  803. return GraphicalMusicSheet;
  804. }());
  805. exports.GraphicalMusicSheet = GraphicalMusicSheet;
  806. var SystemImageProperties = (function () {
  807. function SystemImageProperties() {
  808. }
  809. return SystemImageProperties;
  810. }());
  811. exports.SystemImageProperties = SystemImageProperties;