MusicSheetCalculator.ts 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. import {GraphicalStaffEntry} from "./GraphicalStaffEntry";
  2. import {StaffLine} from "./StaffLine";
  3. import {GraphicalMusicSheet} from "./GraphicalMusicSheet";
  4. import {EngravingRules} from "./EngravingRules";
  5. import {Tie} from "../VoiceData/Tie";
  6. import {Fraction} from "../../Common/DataObjects/fraction";
  7. import {Note} from "../VoiceData/Note";
  8. import {MusicSheet} from "../MusicSheet";
  9. import {StaffMeasure} from "./StaffMeasure";
  10. import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
  11. import {LyricWord} from "../VoiceData/Lyrics/LyricsWord";
  12. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  13. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  14. import {GraphicalNote} from "./GraphicalNote";
  15. import {Beam} from "../VoiceData/Beam";
  16. import {OctaveEnum} from "../VoiceData/Expressions/ContinuousExpressions/octaveShift";
  17. import {LyricsEntry} from "../VoiceData/Lyrics/LyricsEntry";
  18. import {VoiceEntry} from "../VoiceData/VoiceEntry";
  19. import {OrnamentContainer} from "../VoiceData/OrnamentContainer";
  20. import {ArticulationEnum} from "../VoiceData/VoiceEntry";
  21. import {Tuplet} from "../VoiceData/Tuplet";
  22. import {MusicSystem} from "./MusicSystem";
  23. import {GraphicalTie} from "./GraphicalTie";
  24. import {RepetitionInstruction} from "../VoiceData/Instructions/RepetitionInstruction";
  25. import {MultiExpression} from "../VoiceData/Expressions/multiExpression";
  26. import {StaffEntryLink} from "../VoiceData/StaffEntryLink";
  27. import {MusicSystemBuilder} from "./MusicSystemBuilder";
  28. import {MultiTempoExpression} from "../VoiceData/Expressions/multiTempoExpression";
  29. import {Repetition} from "../MusicSource/Repetition";
  30. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  31. import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
  32. import {BoundingBox} from "./BoundingBox";
  33. import {Instrument} from "../Instrument";
  34. import {GraphicalLabel} from "./GraphicalLabel";
  35. import {TextAlignment} from "../../Common/Enums/TextAlignment";
  36. import {VerticalGraphicalStaffEntryContainer} from "./VerticalGraphicalStaffEntryContainer";
  37. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  38. import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
  39. import {ClefEnum} from "../VoiceData/Instructions/ClefInstruction";
  40. import {TechnicalInstruction} from "../VoiceData/Instructions/TechnicalInstruction";
  41. import {Pitch} from "../../Common/DataObjects/pitch";
  42. import {LinkedVoice} from "../VoiceData/LinkedVoice";
  43. import {ColDirEnum} from "./BoundingBox";
  44. import {IGraphicalSymbolFactory} from "../Interfaces/IGraphicalSymbolFactory";
  45. import {ITextMeasurer} from "../Interfaces/ITextMeasurer";
  46. import {ITransposeCalculator} from "../Interfaces/ITransposeCalculator";
  47. import {OctaveShiftParams} from "./OctaveShiftParams";
  48. import {AccidentalCalculator} from "./AccidentalCalculator";
  49. import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
  50. import {Staff} from "../VoiceData/Staff";
  51. import {OctaveShift} from "../VoiceData/Expressions/ContinuousExpressions/octaveShift";
  52. import {Logging} from "../../Common/logging";
  53. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  54. import {CollectionUtil} from "../../Util/collectionUtil";
  55. export abstract class MusicSheetCalculator {
  56. public static transposeCalculator: ITransposeCalculator;
  57. protected static textMeasurer: ITextMeasurer;
  58. protected staffEntriesWithGraphicalTies: GraphicalStaffEntry[] = [];
  59. protected staffEntriesWithOrnaments: GraphicalStaffEntry[] = [];
  60. protected staffEntriesWithChordSymbols: GraphicalStaffEntry[] = [];
  61. protected staffLinesWithLyricWords: StaffLine[] = [];
  62. protected staffLinesWithGraphicalExpressions: StaffLine[] = [];
  63. protected graphicalMusicSheet: GraphicalMusicSheet;
  64. protected rules: EngravingRules;
  65. protected symbolFactory: IGraphicalSymbolFactory;
  66. constructor(symbolFactory: IGraphicalSymbolFactory) {
  67. this.symbolFactory = symbolFactory;
  68. }
  69. public static get TextMeasurer(): ITextMeasurer {
  70. return MusicSheetCalculator.textMeasurer;
  71. }
  72. public static set TextMeasurer(value: ITextMeasurer) {
  73. MusicSheetCalculator.textMeasurer = value;
  74. }
  75. protected get leadSheet(): boolean {
  76. return this.graphicalMusicSheet.LeadSheet;
  77. }
  78. private static addTieToTieTimestampsDict(tieTimestampListDict: Dictionary<Tie, Fraction[]>, note: Note): void {
  79. note.NoteTie.initializeBoolList();
  80. let tieTimestampList: Fraction[] = [];
  81. for (let m: number = 0; m < note.NoteTie.Fractions.length; m++) {
  82. let musicTimestamp: Fraction;
  83. if (m === 0) {
  84. musicTimestamp = Fraction.plus(note.calculateNoteLengthWithoutTie(), note.getAbsoluteTimestamp());
  85. } else {
  86. musicTimestamp = Fraction.plus(tieTimestampList[m - 1], note.NoteTie.Fractions[m - 1]);
  87. }
  88. tieTimestampList.push(musicTimestamp);
  89. }
  90. tieTimestampListDict.setValue(note.NoteTie, tieTimestampList);
  91. }
  92. private static setMeasuresMinStaffEntriesWidth(measures: StaffMeasure[], minimumStaffEntriesWidth: number): void {
  93. for (let idx: number = 0, len: number = measures.length; idx < len; ++idx) {
  94. let measure: StaffMeasure = measures[idx];
  95. measure.minimumStaffEntriesWidth = minimumStaffEntriesWidth;
  96. }
  97. }
  98. public initialize(graphicalMusicSheet: GraphicalMusicSheet): void {
  99. this.graphicalMusicSheet = graphicalMusicSheet;
  100. this.rules = graphicalMusicSheet.ParentMusicSheet.rules;
  101. this.prepareGraphicalMusicSheet();
  102. this.calculate();
  103. }
  104. public prepareGraphicalMusicSheet(): void {
  105. //this.graphicalMusicSheet.SystemImages.length = 0;
  106. let musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  107. this.staffEntriesWithGraphicalTies = [];
  108. this.staffEntriesWithOrnaments = [];
  109. this.staffEntriesWithChordSymbols = [];
  110. this.staffLinesWithLyricWords = [];
  111. this.staffLinesWithGraphicalExpressions = [];
  112. this.graphicalMusicSheet.Initialize();
  113. let measureList: StaffMeasure[][] = this.graphicalMusicSheet.MeasureList;
  114. let accidentalCalculators: AccidentalCalculator[] = this.createAccidentalCalculators();
  115. let activeClefs: ClefInstruction[] = this.graphicalMusicSheet.initializeActiveClefs();
  116. let lyricWords: LyricWord[] = [];
  117. let completeNumberOfStaves: number = musicSheet.getCompleteNumberOfStaves();
  118. let openOctaveShifts: OctaveShiftParams[] = [];
  119. let tieTimestampListDictList: Dictionary<Tie, Fraction[]>[] = [];
  120. for (let i: number = 0; i < completeNumberOfStaves; i++) {
  121. let tieTimestampListDict: Dictionary<Tie, Fraction[]> = new Dictionary<Tie, Fraction[]>();
  122. tieTimestampListDictList.push(tieTimestampListDict);
  123. openOctaveShifts.push(undefined);
  124. }
  125. for (let idx: number = 0, len: number = musicSheet.SourceMeasures.length; idx < len; ++idx) {
  126. let sourceMeasure: SourceMeasure = musicSheet.SourceMeasures[idx];
  127. let graphicalMeasures: StaffMeasure[] = this.createGraphicalMeasuresForSourceMeasure(
  128. sourceMeasure,
  129. accidentalCalculators,
  130. lyricWords,
  131. tieTimestampListDictList,
  132. openOctaveShifts,
  133. activeClefs
  134. );
  135. measureList.push(graphicalMeasures);
  136. }
  137. this.handleStaffEntries();
  138. this.calculateVerticalContainersList();
  139. this.setIndecesToVerticalGraphicalContainers();
  140. }
  141. public calculate(): void {
  142. this.clearSystemsAndMeasures();
  143. this.clearRecreatedObjects();
  144. this.createGraphicalTies();
  145. this.calculateSheetLabelBoundingBoxes();
  146. this.calculateXLayout(this.graphicalMusicSheet, this.maxInstrNameLabelLength());
  147. this.graphicalMusicSheet.MusicPages = [];
  148. this.calculateMusicSystems();
  149. this.graphicalMusicSheet.MusicPages[0].PositionAndShape.BorderMarginBottom += 9;
  150. GraphicalMusicSheet.transformRelativeToAbsolutePosition(this.graphicalMusicSheet);
  151. }
  152. public calculateXLayout(graphicalMusicSheet: GraphicalMusicSheet, maxInstrNameLabelLength: number): void {
  153. let minLength: number = 0;
  154. let maxInstructionsLength: number = this.rules.MaxInstructionsConstValue;
  155. if (this.graphicalMusicSheet.MeasureList.length > 0) {
  156. let measures: StaffMeasure[] = this.graphicalMusicSheet.MeasureList[0];
  157. let minimumStaffEntriesWidth: number = this.calculateMeasureXLayout(measures);
  158. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  159. minLength = minimumStaffEntriesWidth * 1.2 + maxInstrNameLabelLength + maxInstructionsLength;
  160. for (let i: number = 1; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  161. measures = this.graphicalMusicSheet.MeasureList[i];
  162. minimumStaffEntriesWidth = this.calculateMeasureXLayout(measures);
  163. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  164. minLength = Math.max(minLength, minimumStaffEntriesWidth * 1.2 + maxInstructionsLength);
  165. }
  166. }
  167. this.graphicalMusicSheet.MinAllowedSystemWidth = minLength;
  168. }
  169. protected calculateMeasureXLayout(measures: StaffMeasure[]): number {
  170. throw new Error("abstract, not implemented");
  171. }
  172. protected calculateSystemYLayout(): void {
  173. throw new Error("abstract, not implemented");
  174. }
  175. protected initStaffMeasuresCreation(): void {
  176. throw new Error("abstract, not implemented");
  177. }
  178. protected handleBeam(graphicalNote: GraphicalNote, beam: Beam, openBeams: Beam[]): void {
  179. throw new Error("abstract, not implemented");
  180. }
  181. protected createGraphicalTieNote(beams: Beam[], activeClef: ClefInstruction,
  182. octaveShiftValue: OctaveEnum,
  183. graphicalStaffEntry: GraphicalStaffEntry, duration: Fraction, numberOfDots: number,
  184. openTie: Tie, isLastTieNote: boolean): void {
  185. throw new Error("abstract, not implemented");
  186. }
  187. protected handleVoiceEntryLyrics(lyricsEntries: Dictionary<number, LyricsEntry>, voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  188. openLyricWords: LyricWord[]): void {
  189. throw new Error("abstract, not implemented");
  190. }
  191. protected handleVoiceEntryOrnaments(ornamentContainer: OrnamentContainer, voiceEntry: VoiceEntry,
  192. graphicalStaffEntry: GraphicalStaffEntry): void {
  193. throw new Error("abstract, not implemented");
  194. }
  195. protected handleVoiceEntryArticulations(articulations: ArticulationEnum[],
  196. voiceEntry: VoiceEntry,
  197. graphicalStaffEntry: GraphicalStaffEntry): void {
  198. throw new Error("abstract, not implemented");
  199. }
  200. protected handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet, openTuplets: Tuplet[]): void {
  201. throw new Error("abstract, not implemented");
  202. }
  203. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[],
  204. graphicalStaffEntry: GraphicalStaffEntry, hasPitchedNote: boolean, isGraceStaffEntry: boolean): void {
  205. throw new Error("abstract, not implemented");
  206. }
  207. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  208. throw new Error("abstract, not implemented");
  209. }
  210. protected handleTie(tie: Tie, startGraphicalStaffEntry: GraphicalStaffEntry, staffIndex: number,
  211. measureIndex: number): void {
  212. throw new Error("abstract, not implemented");
  213. }
  214. protected updateStaffLineBorders(staffLine: StaffLine): void {
  215. throw new Error("abstract, not implemented");
  216. }
  217. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  218. throw new Error("abstract, not implemented");
  219. }
  220. protected layoutGraphicalTie(tie: GraphicalTie, tieIsAtSystemBreak: boolean): void {
  221. throw new Error("abstract, not implemented");
  222. }
  223. protected calculateSingleStaffLineLyricsPosition(staffLine: StaffLine, lyricVersesNumber: number[]): void {
  224. throw new Error("abstract, not implemented");
  225. }
  226. protected calculateSingleOctaveShift(sourceMeasure: SourceMeasure, multiExpression: MultiExpression,
  227. measureIndex: number, staffIndex: number): void {
  228. throw new Error("abstract, not implemented");
  229. }
  230. protected calculateWordRepetitionInstruction(repetitionInstruction: RepetitionInstruction,
  231. measureIndex: number): void {
  232. throw new Error("abstract, not implemented");
  233. }
  234. protected calculateMoodAndUnknownExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  235. throw new Error("abstract, not implemented");
  236. }
  237. protected clearRecreatedObjects(): void {
  238. Logging.log("clearRecreatedObjects not implemented");
  239. }
  240. protected handleStaffEntryLink(graphicalStaffEntry: GraphicalStaffEntry,
  241. staffEntryLinks: StaffEntryLink[]): void {
  242. Logging.log("handleStaffEntryLink not implemented");
  243. }
  244. protected calculateMusicSystems(): void {
  245. if (this.graphicalMusicSheet.MeasureList === undefined) {
  246. return;
  247. }
  248. let allMeasures: StaffMeasure[][] = this.graphicalMusicSheet.MeasureList;
  249. if (allMeasures === undefined) {
  250. return;
  251. }
  252. let visibleMeasureList: StaffMeasure[][] = [];
  253. for (let idx: number = 0, len: number = allMeasures.length; idx < len; ++idx) {
  254. let staffMeasures: StaffMeasure[] = allMeasures[idx];
  255. let visibleStaffMeasures: StaffMeasure[] = [];
  256. for (let idx2: number = 0, len2: number = staffMeasures.length; idx2 < len2; ++idx2) {
  257. let staffMeasure: StaffMeasure = allMeasures[idx][idx2];
  258. if (staffMeasure.isVisible()) {
  259. visibleStaffMeasures.push(staffMeasure);
  260. }
  261. }
  262. visibleMeasureList.push(visibleStaffMeasures);
  263. }
  264. let numberOfStaffLines: number = 0;
  265. for (let idx: number = 0, len: number = visibleMeasureList.length; idx < len; ++idx) {
  266. let gmlist: StaffMeasure[] = visibleMeasureList[idx];
  267. numberOfStaffLines = Math.max(gmlist.length, numberOfStaffLines);
  268. break;
  269. }
  270. if (numberOfStaffLines === 0) {
  271. return;
  272. }
  273. let musicSystemBuilder: MusicSystemBuilder = new MusicSystemBuilder();
  274. musicSystemBuilder.initialize(this.graphicalMusicSheet, visibleMeasureList, numberOfStaffLines, this.symbolFactory);
  275. musicSystemBuilder.buildMusicSystems();
  276. this.checkMeasuresForWholeRestNotes();
  277. if (!this.leadSheet) {
  278. this.calculateBeams();
  279. this.optimizeRestPlacement();
  280. this.calculateStaffEntryArticulationMarks();
  281. this.calculateTieCurves();
  282. }
  283. this.calculateSkyBottomLines();
  284. this.calculateTupletNumbers();
  285. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  286. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  287. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  288. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  289. this.calculateMeasureNumberPlacement(musicSystem);
  290. }
  291. }
  292. if (!this.leadSheet) {
  293. this.calculateSlurs();
  294. }
  295. if (!this.leadSheet) {
  296. this.calculateOrnaments();
  297. }
  298. this.updateSkyBottomLines();
  299. this.calculateChordSymbols();
  300. if (!this.leadSheet) {
  301. this.calculateDynamicExpressions();
  302. this.optimizeStaffLineDynamicExpressionsPositions();
  303. this.calculateMoodAndUnknownExpressions();
  304. this.calculateOctaveShifts();
  305. this.calculateWordRepetitionInstructions();
  306. }
  307. this.calculateRepetitionEndings();
  308. if (!this.leadSheet) {
  309. this.calculateTempoExpressions();
  310. }
  311. this.calculateLyricsPosition();
  312. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  313. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  314. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  315. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  316. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  317. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  318. this.updateStaffLineBorders(staffLine);
  319. }
  320. }
  321. }
  322. this.calculateComments();
  323. this.calculateSystemYLayout();
  324. this.calculateMarkedAreas();
  325. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  326. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  327. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  328. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  329. musicSystem.setMusicSystemLabelsYPosition();
  330. if (!this.leadSheet) {
  331. musicSystem.setYPositionsToVerticalLineObjectsAndCreateLines(this.rules);
  332. musicSystem.createSystemLeftVerticalLineObject(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin);
  333. musicSystem.createInstrumentBrackets(this.graphicalMusicSheet.ParentMusicSheet.Instruments, this.rules.StaffHeight);
  334. musicSystem.createGroupBrackets(this.graphicalMusicSheet.ParentMusicSheet.InstrumentalGroups, this.rules.StaffHeight, 0);
  335. musicSystem.alignBeginInstructions();
  336. } else if (musicSystem === musicSystem.Parent.MusicSystems[0]) {
  337. musicSystem.createSystemLeftVerticalLineObject(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin);
  338. }
  339. musicSystem.calculateBorders(this.rules);
  340. }
  341. let distance: number = graphicalMusicPage.MusicSystems[0].PositionAndShape.BorderTop;
  342. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  343. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  344. // let newPosition: PointF2D = new PointF2D(musicSystem.PositionAndShape.RelativePosition.x,
  345. // musicSystem.PositionAndShape.RelativePosition.y - distance);
  346. musicSystem.PositionAndShape.RelativePosition =
  347. new PointF2D(musicSystem.PositionAndShape.RelativePosition.x, musicSystem.PositionAndShape.RelativePosition.y - distance);
  348. }
  349. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  350. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  351. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  352. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  353. staffLine.addActivitySymbolClickArea();
  354. }
  355. }
  356. if (graphicalMusicPage === this.graphicalMusicSheet.MusicPages[0]) {
  357. this.calculatePageLabels(graphicalMusicPage);
  358. }
  359. graphicalMusicPage.PositionAndShape.calculateTopBottomBorders();
  360. }
  361. }
  362. protected updateSkyBottomLine(staffLine: StaffLine): void {
  363. Logging.log("updateSkyBottomLine not implemented");
  364. }
  365. protected calculateSkyBottomLine(staffLine: StaffLine): void {
  366. Logging.log("calculateSkyBottomLine not implemented");
  367. }
  368. protected calculateMarkedAreas(): void {
  369. Logging.log("calculateMarkedAreas not implemented");
  370. }
  371. protected calculateComments(): void {
  372. Logging.log("calculateComments not implemented");
  373. }
  374. protected optimizeStaffLineDynamicExpressionsPositions(): void {
  375. return;
  376. }
  377. protected calculateChordSymbols(): void {
  378. return;
  379. }
  380. protected layoutMeasureWithWholeRest(rest: GraphicalNote, gse: GraphicalStaffEntry,
  381. measure: StaffMeasure): void {
  382. return;
  383. }
  384. protected layoutBeams(staffEntry: GraphicalStaffEntry): void {
  385. return;
  386. }
  387. protected layoutArticulationMarks(articulations: ArticulationEnum[], voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  388. return;
  389. }
  390. protected layoutOrnament(ornaments: OrnamentContainer, voiceEntry: VoiceEntry,
  391. graphicalStaffEntry: GraphicalStaffEntry): void {
  392. return;
  393. }
  394. protected calculateRestNotePlacementWithinGraphicalBeam(graphicalStaffEntry: GraphicalStaffEntry,
  395. restNote: GraphicalNote,
  396. previousNote: GraphicalNote,
  397. nextStaffEntry: GraphicalStaffEntry,
  398. nextNote: GraphicalNote): void {
  399. return;
  400. }
  401. protected calculateTupletNumbers(): void {
  402. return;
  403. }
  404. protected calculateSlurs(): void {
  405. return;
  406. }
  407. protected calculateDynamicExpressionsForSingleMultiExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  408. return;
  409. }
  410. protected calcGraphicalRepetitionEndingsRecursively(repetition: Repetition): void {
  411. return;
  412. }
  413. protected layoutSingleRepetitionEnding(start: StaffMeasure, end: StaffMeasure, numberText: string,
  414. offset: number, leftOpen: boolean, rightOpen: boolean): void {
  415. return;
  416. }
  417. protected calculateTempoExpressionsForSingleMultiTempoExpression(sourceMeasure: SourceMeasure, multiTempoExpression: MultiTempoExpression,
  418. measureIndex: number): void {
  419. return;
  420. }
  421. protected clearSystemsAndMeasures(): void {
  422. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  423. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  424. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  425. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  426. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  427. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  428. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  429. let graphicalMeasure: StaffMeasure = staffLine.Measures[idx4];
  430. if (graphicalMeasure.FirstInstructionStaffEntry !== undefined) {
  431. let index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  432. graphicalMeasure.FirstInstructionStaffEntry.PositionAndShape
  433. );
  434. if (index > -1) {
  435. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  436. }
  437. graphicalMeasure.FirstInstructionStaffEntry = undefined;
  438. graphicalMeasure.beginInstructionsWidth = 0.0;
  439. }
  440. if (graphicalMeasure.LastInstructionStaffEntry !== undefined) {
  441. let index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  442. graphicalMeasure.LastInstructionStaffEntry.PositionAndShape
  443. );
  444. if (index > -1) {
  445. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  446. }
  447. graphicalMeasure.LastInstructionStaffEntry = undefined;
  448. graphicalMeasure.endInstructionsWidth = 0.0;
  449. }
  450. }
  451. staffLine.Measures = [];
  452. staffLine.PositionAndShape.ChildElements = [];
  453. }
  454. musicSystem.StaffLines.length = 0;
  455. musicSystem.PositionAndShape.ChildElements = [];
  456. }
  457. graphicalMusicPage.MusicSystems = [];
  458. graphicalMusicPage.PositionAndShape.ChildElements = [];
  459. }
  460. this.graphicalMusicSheet.MusicPages = [];
  461. }
  462. protected handleVoiceEntry(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  463. accidentalCalculator: AccidentalCalculator, openLyricWords: LyricWord[],
  464. tieTimestampListDict: Dictionary<Tie, Fraction[]>, activeClef: ClefInstruction,
  465. openTuplets: Tuplet[], openBeams: Beam[],
  466. octaveShiftValue: OctaveEnum, grace: boolean = false, linkedNotes: Note[] = undefined,
  467. sourceStaffEntry: SourceStaffEntry = undefined): OctaveEnum {
  468. let graphicalNotes: GraphicalNote[] = graphicalStaffEntry.findOrCreateGraphicalNotesListFromVoiceEntry(voiceEntry);
  469. for (let idx: number = 0, len: number = voiceEntry.Notes.length; idx < len; ++idx) {
  470. let note: Note = voiceEntry.Notes[idx];
  471. if (sourceStaffEntry !== undefined && sourceStaffEntry.Link !== undefined && linkedNotes !== undefined && linkedNotes.indexOf(note) > -1) {
  472. continue;
  473. }
  474. let graphicalNote: GraphicalNote;
  475. let numberOfDots: number = note.calculateNumberOfNeededDots();
  476. if (grace) {
  477. graphicalNote = this.symbolFactory.createGraceNote(note, numberOfDots, graphicalStaffEntry, activeClef, octaveShiftValue);
  478. } else {
  479. graphicalNote = this.symbolFactory.createNote(note, numberOfDots, graphicalStaffEntry, activeClef, octaveShiftValue);
  480. }
  481. if (note.NoteTie !== undefined) {
  482. MusicSheetCalculator.addTieToTieTimestampsDict(tieTimestampListDict, note);
  483. }
  484. if (note.Pitch !== undefined) {
  485. this.checkNoteForAccidental(graphicalNote, accidentalCalculator, activeClef, octaveShiftValue, grace);
  486. }
  487. this.resetYPositionForLeadSheet(graphicalNote.PositionAndShape);
  488. graphicalStaffEntry.addGraphicalNoteToListAtCorrectYPosition(graphicalNotes, graphicalNote);
  489. graphicalStaffEntry.PositionAndShape.ChildElements.push(graphicalNote.PositionAndShape);
  490. graphicalNote.PositionAndShape.calculateBoundingBox();
  491. if (!this.leadSheet) {
  492. if (note.NoteBeam !== undefined) {
  493. this.handleBeam(graphicalNote, note.NoteBeam, openBeams);
  494. }
  495. if (note.NoteTuplet !== undefined) {
  496. this.handleTuplet(graphicalNote, note.NoteTuplet, openTuplets);
  497. }
  498. }
  499. }
  500. if (voiceEntry.Articulations.length > 0) {
  501. this.handleVoiceEntryArticulations(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  502. }
  503. if (voiceEntry.TechnicalInstructions.length > 0) {
  504. this.checkVoiceEntriesForTechnicalInstructions(voiceEntry, graphicalStaffEntry);
  505. }
  506. if (voiceEntry.LyricsEntries.size() > 0) {
  507. this.handleVoiceEntryLyrics(voiceEntry.LyricsEntries, voiceEntry, graphicalStaffEntry, openLyricWords);
  508. }
  509. if (voiceEntry.OrnamentContainer !== undefined) {
  510. this.handleVoiceEntryOrnaments(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  511. }
  512. return octaveShiftValue;
  513. }
  514. protected handleVoiceEntryGraceNotes(graceEntries: VoiceEntry[], graphicalGraceEntries: GraphicalStaffEntry[], graphicalStaffEntry: GraphicalStaffEntry,
  515. accidentalCalculator: AccidentalCalculator, activeClef: ClefInstruction,
  516. octaveShiftValue: OctaveEnum, lyricWords: LyricWord[],
  517. tieTimestampListDict: Dictionary<Tie, Fraction[]>,
  518. tuplets: Tuplet[], beams: Beam[]): void {
  519. if (graceEntries !== undefined) {
  520. for (let idx: number = 0, len: number = graceEntries.length; idx < len; ++idx) {
  521. let graceVoiceEntry: VoiceEntry = graceEntries[idx];
  522. let graceStaffEntry: GraphicalStaffEntry = this.symbolFactory.createGraceStaffEntry(
  523. graphicalStaffEntry,
  524. graphicalStaffEntry.parentMeasure
  525. );
  526. graphicalGraceEntries.push(graceStaffEntry);
  527. graphicalStaffEntry.PositionAndShape.ChildElements.push(graceStaffEntry.PositionAndShape);
  528. this.handleVoiceEntry(
  529. graceVoiceEntry, graceStaffEntry, accidentalCalculator, lyricWords,
  530. tieTimestampListDict, activeClef, tuplets,
  531. beams, octaveShiftValue, true
  532. );
  533. }
  534. }
  535. }
  536. protected handleOpenTies(measure: StaffMeasure, beams: Beam[], tieTimestampListDict: Dictionary<Tie, Fraction[]>,
  537. activeClef: ClefInstruction, octaveShiftParams: OctaveShiftParams): void {
  538. CollectionUtil.removeDictElementIfTrue(tieTimestampListDict, function (openTie: Tie, tieTimestamps: Fraction[]): boolean {
  539. // for (let m: number = tieTimestampListDict.size() - 1; m >= 0; m--) {
  540. // let keyValuePair: KeyValuePair<Tie, Fraction[]> = tieTimestampListDict.ElementAt(m);
  541. // let openTie: Tie = keyValuePair.Key;
  542. // let tieTimestamps: Fraction[] = keyValuePair.Value;
  543. let absoluteTimestamp: Fraction = undefined;
  544. let k: number;
  545. let removeTie: boolean = false;
  546. for (; k < tieTimestamps.length; k++) {
  547. if (!openTie.NoteHasBeenCreated[k]) {
  548. absoluteTimestamp = tieTimestamps[k];
  549. if (absoluteTimestamp >= Fraction.plus(measure.parentSourceMeasure.AbsoluteTimestamp, measure.parentSourceMeasure.Duration)) {
  550. continue;
  551. }
  552. let graphicalStaffEntry: GraphicalStaffEntry = undefined;
  553. if (absoluteTimestamp !== undefined) {
  554. for (let idx: number = 0, len: number = measure.staffEntries.length; idx < len; ++idx) {
  555. let gse: GraphicalStaffEntry = measure.staffEntries[idx];
  556. if (gse.getAbsoluteTimestamp() === absoluteTimestamp) {
  557. graphicalStaffEntry = gse;
  558. break;
  559. }
  560. }
  561. if (graphicalStaffEntry === undefined) {
  562. graphicalStaffEntry = this.createStaffEntryForTieNote(measure, absoluteTimestamp, openTie);
  563. }
  564. }
  565. if (graphicalStaffEntry !== undefined) {
  566. let octaveShiftValue: OctaveEnum = OctaveEnum.NONE;
  567. if (octaveShiftParams !== undefined) {
  568. if (graphicalStaffEntry.getAbsoluteTimestamp() >= octaveShiftParams.getAbsoluteStartTimestamp &&
  569. graphicalStaffEntry.getAbsoluteTimestamp() <= octaveShiftParams.getAbsoluteEndTimestamp) {
  570. octaveShiftValue = octaveShiftParams.getOpenOctaveShift.Type;
  571. }
  572. }
  573. let isLastTieNote: boolean = k === tieTimestamps.length - 1;
  574. let tieFraction: Fraction = openTie.Fractions[k];
  575. let numberOfDots: number = openTie.Start.calculateNumberOfNeededDots();
  576. this.createGraphicalTieNote(
  577. beams, activeClef, octaveShiftValue, graphicalStaffEntry, tieFraction, numberOfDots, openTie, isLastTieNote
  578. );
  579. let tieStartNote: Note = openTie.Start;
  580. if (isLastTieNote && tieStartNote.ParentVoiceEntry.Articulations.length === 1 &&
  581. tieStartNote.ParentVoiceEntry.Articulations[0] === ArticulationEnum.fermata) {
  582. this.symbolFactory.addFermataAtTiedEndNote(tieStartNote, graphicalStaffEntry);
  583. }
  584. openTie.NoteHasBeenCreated[k] = true;
  585. if (openTie.allGraphicalNotesHaveBeenCreated()) {
  586. removeTie = true;
  587. //tieTimestampListDict.remove(openTie);
  588. }
  589. }
  590. }
  591. }
  592. return removeTie;
  593. });
  594. }
  595. protected resetYPositionForLeadSheet(psi: BoundingBox): void {
  596. if (this.leadSheet) {
  597. psi.RelativePosition = new PointF2D(psi.RelativePosition.x, 0.0);
  598. }
  599. }
  600. protected layoutVoiceEntries(graphicalStaffEntry: GraphicalStaffEntry): void {
  601. graphicalStaffEntry.PositionAndShape.RelativePosition = new PointF2D(0.0, 0.0);
  602. let isGraceStaffEntry: boolean = graphicalStaffEntry.staffEntryParent !== undefined;
  603. if (!this.leadSheet) {
  604. let graphicalStaffEntryNotes: GraphicalNote[][] = graphicalStaffEntry.notes;
  605. for (let idx4: number = 0, len4: number = graphicalStaffEntryNotes.length; idx4 < len4; ++idx4) {
  606. let graphicalNotes: GraphicalNote[] = graphicalStaffEntryNotes[idx4];
  607. if (graphicalNotes.length === 0) {
  608. continue;
  609. }
  610. let voiceEntry: VoiceEntry = graphicalNotes[0].sourceNote.ParentVoiceEntry;
  611. let hasPitchedNote: boolean = graphicalNotes[0].sourceNote.Pitch !== undefined;
  612. this.layoutVoiceEntry(voiceEntry, graphicalNotes, graphicalStaffEntry, hasPitchedNote, isGraceStaffEntry);
  613. }
  614. }
  615. }
  616. protected maxInstrNameLabelLength(): number {
  617. let maxLabelLength: number = 0.0;
  618. for (let instrument of this.graphicalMusicSheet.ParentMusicSheet.Instruments) {
  619. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  620. let graphicalLabel: GraphicalLabel = new GraphicalLabel(instrument.NameLabel, this.rules.InstrumentLabelTextHeight, TextAlignment.LeftCenter);
  621. graphicalLabel.setLabelPositionAndShapeBorders();
  622. maxLabelLength = Math.max(maxLabelLength, graphicalLabel.PositionAndShape.MarginSize.width);
  623. }
  624. }
  625. return maxLabelLength;
  626. }
  627. protected calculateSheetLabelBoundingBoxes(): void {
  628. let musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  629. if (musicSheet.Title !== undefined) {
  630. let title: GraphicalLabel = new GraphicalLabel(musicSheet.Title, this.rules.SheetTitleHeight, TextAlignment.CenterBottom);
  631. this.graphicalMusicSheet.Title = title;
  632. title.setLabelPositionAndShapeBorders();
  633. }
  634. if (musicSheet.Subtitle !== undefined) {
  635. let subtitle: GraphicalLabel = new GraphicalLabel(musicSheet.Subtitle, this.rules.SheetSubtitleHeight, TextAlignment.CenterCenter);
  636. this.graphicalMusicSheet.Subtitle = subtitle;
  637. subtitle.setLabelPositionAndShapeBorders();
  638. }
  639. if (musicSheet.Composer !== undefined) {
  640. let composer: GraphicalLabel = new GraphicalLabel(musicSheet.Composer, this.rules.SheetComposerHeight, TextAlignment.RightCenter);
  641. this.graphicalMusicSheet.Composer = composer;
  642. composer.setLabelPositionAndShapeBorders();
  643. }
  644. if (musicSheet.Lyricist !== undefined) {
  645. let lyricist: GraphicalLabel = new GraphicalLabel(musicSheet.Lyricist, this.rules.SheetAuthorHeight, TextAlignment.LeftCenter);
  646. this.graphicalMusicSheet.Lyricist = lyricist;
  647. lyricist.setLabelPositionAndShapeBorders();
  648. }
  649. }
  650. protected checkMeasuresForWholeRestNotes(): void {
  651. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  652. let musicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  653. for (let idx2: number = 0, len2: number = musicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  654. let musicSystem: MusicSystem = musicPage.MusicSystems[idx2];
  655. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  656. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  657. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  658. let measure: StaffMeasure = staffLine.Measures[idx4];
  659. if (measure.staffEntries.length === 1) {
  660. let gse: GraphicalStaffEntry = measure.staffEntries[0];
  661. if (gse.notes.length > 0 && gse.notes[0].length > 0) {
  662. let graphicalNote: GraphicalNote = gse.notes[0][0];
  663. if (graphicalNote.sourceNote.Pitch === undefined && (new Fraction(1, 2)).lt(graphicalNote.sourceNote.Length)) {
  664. this.layoutMeasureWithWholeRest(graphicalNote, gse, measure);
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }
  673. protected optimizeRestNotePlacement(graphicalStaffEntry: GraphicalStaffEntry, measure: StaffMeasure): void {
  674. if (graphicalStaffEntry.notes.length === 0) {
  675. return;
  676. }
  677. let voice1Notes: GraphicalNote[] = graphicalStaffEntry.notes[0];
  678. if (voice1Notes.length === 0) {
  679. return;
  680. }
  681. let voice1Note1: GraphicalNote = voice1Notes[0];
  682. let voice1Note1IsRest: boolean = voice1Note1.sourceNote.Pitch === undefined;
  683. if (graphicalStaffEntry.notes.length === 2) {
  684. let voice2Note1IsRest: boolean = false;
  685. let voice2Notes: GraphicalNote[] = graphicalStaffEntry.notes[1];
  686. if (voice2Notes.length > 0) {
  687. let voice2Note1: GraphicalNote = voice1Notes[0];
  688. voice2Note1IsRest = voice2Note1.sourceNote.Pitch === undefined;
  689. }
  690. if (voice1Note1IsRest && voice2Note1IsRest) {
  691. this.calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry);
  692. } else if (voice1Note1IsRest || voice2Note1IsRest) {
  693. this.calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry);
  694. }
  695. } else if (voice1Note1IsRest && graphicalStaffEntry !== measure.staffEntries[0] &&
  696. graphicalStaffEntry !== measure.staffEntries[measure.staffEntries.length - 1]) {
  697. let staffEntryIndex: number = measure.staffEntries.indexOf(graphicalStaffEntry);
  698. let previousStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex - 1];
  699. let nextStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex + 1];
  700. if (previousStaffEntry.notes.length === 1) {
  701. let previousNote: GraphicalNote = previousStaffEntry.notes[0][0];
  702. if (previousNote.sourceNote.NoteBeam !== undefined && nextStaffEntry.notes.length === 1) {
  703. let nextNote: GraphicalNote = nextStaffEntry.notes[0][0];
  704. if (nextNote.sourceNote.NoteBeam !== undefined && previousNote.sourceNote.NoteBeam === nextNote.sourceNote.NoteBeam) {
  705. this.calculateRestNotePlacementWithinGraphicalBeam(
  706. graphicalStaffEntry, voice1Note1, previousNote,
  707. nextStaffEntry, nextNote
  708. );
  709. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  710. }
  711. }
  712. }
  713. }
  714. }
  715. protected getRelativePositionInStaffLineFromTimestamp(timestamp: Fraction, verticalIndex: number, staffLine: StaffLine,
  716. multiStaffInstrument: boolean, firstVisibleMeasureRelativeX: number = 0.0): PointF2D {
  717. let relative: PointF2D = new PointF2D();
  718. let leftStaffEntry: GraphicalStaffEntry = undefined;
  719. let rightStaffEntry: GraphicalStaffEntry = undefined;
  720. let numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  721. let index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  722. let leftIndex: number = <number>Math.min(Math.floor(index), numEntries - 1);
  723. let rightIndex: number = <number>Math.min(Math.ceil(index), numEntries - 1);
  724. if (leftIndex < 0 || verticalIndex < 0) {
  725. return relative;
  726. }
  727. leftStaffEntry = this.getFirstLeftNotNullStaffEntryFromContainer(leftIndex, verticalIndex, multiStaffInstrument);
  728. rightStaffEntry = this.getFirstRightNotNullStaffEntryFromContainer(rightIndex, verticalIndex, multiStaffInstrument);
  729. if (leftStaffEntry !== undefined && rightStaffEntry !== undefined) {
  730. let measureRelativeX: number = leftStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  731. if (firstVisibleMeasureRelativeX > 0) {
  732. measureRelativeX = firstVisibleMeasureRelativeX;
  733. }
  734. let leftX: number = leftStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  735. let rightX: number = rightStaffEntry.PositionAndShape.RelativePosition.x + rightStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  736. if (firstVisibleMeasureRelativeX > 0) {
  737. rightX = rightStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  738. }
  739. let timestampQuotient: number = 0.0;
  740. if (leftStaffEntry !== rightStaffEntry) {
  741. let leftTimestamp: Fraction = leftStaffEntry.getAbsoluteTimestamp();
  742. let rightTimestamp: Fraction = rightStaffEntry.getAbsoluteTimestamp();
  743. let leftDifference: Fraction = Fraction.minus(timestamp, leftTimestamp);
  744. timestampQuotient = leftDifference.RealValue / Fraction.minus(rightTimestamp, leftTimestamp).RealValue;
  745. }
  746. if (leftStaffEntry.parentMeasure.ParentStaffLine !== rightStaffEntry.parentMeasure.ParentStaffLine) {
  747. if (leftStaffEntry.parentMeasure.ParentStaffLine === staffLine) {
  748. rightX = staffLine.PositionAndShape.Size.width;
  749. } else {
  750. leftX = staffLine.PositionAndShape.RelativePosition.x;
  751. }
  752. }
  753. relative = new PointF2D(leftX + (rightX - leftX) * timestampQuotient, 0.0);
  754. }
  755. return relative;
  756. }
  757. protected getRelativeXPositionFromTimestamp(timestamp: Fraction): number {
  758. let numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  759. let index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  760. let discreteIndex: number = <number>Math.max(0, Math.min(Math.round(index), numEntries - 1));
  761. let gse: GraphicalStaffEntry = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[discreteIndex].getFirstNonNullStaffEntry();
  762. let posX: number = gse.PositionAndShape.RelativePosition.x + gse.parentMeasure.PositionAndShape.RelativePosition.x;
  763. return posX;
  764. }
  765. private createAccidentalCalculators(): AccidentalCalculator[] {
  766. let accidentalCalculators: AccidentalCalculator[] = [];
  767. let firstSourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
  768. if (firstSourceMeasure !== undefined) {
  769. for (let i: number = 0; i < firstSourceMeasure.CompleteNumberOfStaves; i++) {
  770. let accidentalCalculator: AccidentalCalculator = new AccidentalCalculator(this.symbolFactory);
  771. accidentalCalculators.push(accidentalCalculator);
  772. if (firstSourceMeasure.FirstInstructionsStaffEntries[i] !== undefined) {
  773. for (let idx: number = 0, len: number = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions.length; idx < len; ++idx) {
  774. let abstractNotationInstruction: AbstractNotationInstruction = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions[idx];
  775. if (abstractNotationInstruction instanceof KeyInstruction) {
  776. let keyInstruction: KeyInstruction = <KeyInstruction>abstractNotationInstruction;
  777. accidentalCalculator.ActiveKeyInstruction = keyInstruction;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. return accidentalCalculators;
  784. }
  785. private calculateVerticalContainersList(): void {
  786. let numberOfEntries: number = this.graphicalMusicSheet.MeasureList[0].length;
  787. for (let i: number = 0; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  788. for (let j: number = 0; j < numberOfEntries; j++) {
  789. let measure: StaffMeasure = this.graphicalMusicSheet.MeasureList[i][j];
  790. for (let idx: number = 0, len: number = measure.staffEntries.length; idx < len; ++idx) {
  791. let graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx];
  792. let verticalContainer: VerticalGraphicalStaffEntryContainer =
  793. this.graphicalMusicSheet.getOrCreateVerticalContainer(graphicalStaffEntry.getAbsoluteTimestamp());
  794. if (verticalContainer !== undefined) {
  795. verticalContainer.StaffEntries[j] = graphicalStaffEntry;
  796. graphicalStaffEntry.parentVerticalContainer = verticalContainer;
  797. } else {
  798. ;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. private setIndecesToVerticalGraphicalContainers(): void {
  805. for (let i: number = 0; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  806. this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].Index = i;
  807. }
  808. }
  809. private createGraphicalMeasuresForSourceMeasure(sourceMeasure: SourceMeasure, accidentalCalculators: AccidentalCalculator[],
  810. openLyricWords: LyricWord[],
  811. tieTimestampListDictList: Dictionary<Tie, Fraction[]>[],
  812. openOctaveShifts: OctaveShiftParams[], activeClefs: ClefInstruction[]): StaffMeasure[] {
  813. this.initStaffMeasuresCreation();
  814. let verticalMeasureList: StaffMeasure[] = [];
  815. let openBeams: Beam[] = [];
  816. let openTuplets: Tuplet[] = [];
  817. let staffEntryLinks: StaffEntryLink[] = [];
  818. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  819. let measure: StaffMeasure = this.createGraphicalMeasure(
  820. sourceMeasure, tieTimestampListDictList[staffIndex], openTuplets, openBeams,
  821. accidentalCalculators[staffIndex], activeClefs, openOctaveShifts, openLyricWords, staffIndex, staffEntryLinks
  822. );
  823. verticalMeasureList.push(measure);
  824. }
  825. this.graphicalMusicSheet.sourceToGraphicalMeasureLinks.setValue(sourceMeasure, verticalMeasureList);
  826. return verticalMeasureList;
  827. }
  828. private createGraphicalMeasure(sourceMeasure: SourceMeasure, tieTimestampListDict: Dictionary<Tie, Fraction[]>, openTuplets: Tuplet[], openBeams: Beam[],
  829. accidentalCalculator: AccidentalCalculator, activeClefs: ClefInstruction[],
  830. openOctaveShifts: OctaveShiftParams[], openLyricWords: LyricWord[], staffIndex: number,
  831. staffEntryLinks: StaffEntryLink[]): StaffMeasure {
  832. let staff: Staff = this.graphicalMusicSheet.ParentMusicSheet.getStaffFromIndex(staffIndex);
  833. let measure: StaffMeasure = this.symbolFactory.createStaffMeasure(sourceMeasure, staff);
  834. measure.hasError = sourceMeasure.getErrorInMeasure(staffIndex);
  835. if (sourceMeasure.FirstInstructionsStaffEntries[staffIndex] !== undefined) {
  836. for (let idx: number = 0, len: number = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions.length; idx < len; ++idx) {
  837. let instruction: AbstractNotationInstruction = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[idx];
  838. if (instruction instanceof KeyInstruction) {
  839. let key: KeyInstruction = new KeyInstruction(<KeyInstruction>instruction);
  840. if (this.graphicalMusicSheet.ParentMusicSheet.Transpose !== 0 &&
  841. measure.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion &&
  842. MusicSheetCalculator.transposeCalculator !== undefined) {
  843. MusicSheetCalculator.transposeCalculator.transposeKey(
  844. key, this.graphicalMusicSheet.ParentMusicSheet.Transpose
  845. );
  846. }
  847. accidentalCalculator.ActiveKeyInstruction = key;
  848. }
  849. }
  850. }
  851. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  852. let multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  853. if (multiExpression.OctaveShiftStart !== undefined) {
  854. let openOctaveShift: OctaveShift = multiExpression.OctaveShiftStart;
  855. openOctaveShifts[staffIndex] = new OctaveShiftParams(
  856. openOctaveShift, multiExpression.AbsoluteTimestamp,
  857. openOctaveShift.ParentEndMultiExpression.AbsoluteTimestamp
  858. );
  859. }
  860. }
  861. for (let entryIndex: number = 0; entryIndex < sourceMeasure.VerticalSourceStaffEntryContainers.length; entryIndex++) {
  862. if (sourceMeasure.VerticalSourceStaffEntryContainers[entryIndex][staffIndex] !== undefined) {
  863. let sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[entryIndex][staffIndex];
  864. for (let idx: number = 0, len: number = sourceStaffEntry.Instructions.length; idx < len; ++idx) {
  865. let abstractNotationInstruction: AbstractNotationInstruction = sourceStaffEntry.Instructions[idx];
  866. if (abstractNotationInstruction instanceof ClefInstruction) {
  867. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  868. }
  869. }
  870. let graphicalStaffEntry: GraphicalStaffEntry = this.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  871. if (measure.staffEntries.length > entryIndex) {
  872. measure.addGraphicalStaffEntryAtTimestamp(graphicalStaffEntry);
  873. } else {
  874. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  875. }
  876. let linkedNotes: Note[] = [];
  877. if (sourceStaffEntry.Link !== undefined) {
  878. sourceStaffEntry.findLinkedNotes(linkedNotes);
  879. this.handleStaffEntryLink(graphicalStaffEntry, staffEntryLinks);
  880. }
  881. let octaveShiftValue: OctaveEnum = OctaveEnum.NONE;
  882. if (openOctaveShifts[staffIndex] !== undefined) {
  883. let octaveShiftParams: OctaveShiftParams = openOctaveShifts[staffIndex];
  884. if (sourceStaffEntry.AbsoluteTimestamp >= octaveShiftParams.getAbsoluteStartTimestamp &&
  885. sourceStaffEntry.AbsoluteTimestamp <= octaveShiftParams.getAbsoluteEndTimestamp) {
  886. octaveShiftValue = octaveShiftParams.getOpenOctaveShift.Type;
  887. }
  888. }
  889. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  890. let voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  891. this.handleVoiceEntryGraceNotes(
  892. voiceEntry.graceVoiceEntriesBefore, graphicalStaffEntry.graceStaffEntriesBefore, graphicalStaffEntry,
  893. accidentalCalculator, activeClefs[staffIndex], octaveShiftValue, openLyricWords,
  894. tieTimestampListDict, openTuplets, openBeams
  895. );
  896. octaveShiftValue = this.handleVoiceEntry(
  897. voiceEntry, graphicalStaffEntry,
  898. accidentalCalculator, openLyricWords,
  899. tieTimestampListDict,
  900. activeClefs[staffIndex], openTuplets,
  901. openBeams, octaveShiftValue, false, linkedNotes,
  902. sourceStaffEntry
  903. );
  904. this.handleVoiceEntryGraceNotes(
  905. voiceEntry.graceVoiceEntriesAfter, graphicalStaffEntry.graceStaffEntriesAfter, graphicalStaffEntry,
  906. accidentalCalculator, activeClefs[staffIndex], octaveShiftValue, openLyricWords,
  907. tieTimestampListDict, openTuplets, openBeams
  908. );
  909. }
  910. if (sourceStaffEntry.Instructions.length > 0) {
  911. let clefInstruction: ClefInstruction = <ClefInstruction>sourceStaffEntry.Instructions[0];
  912. this.symbolFactory.createInStaffClef(graphicalStaffEntry, clefInstruction);
  913. }
  914. if (sourceStaffEntry.ChordContainer !== undefined) {
  915. sourceStaffEntry.ParentStaff.ParentInstrument.HasChordSymbols = true;
  916. this.symbolFactory.createChordSymbol(sourceStaffEntry, graphicalStaffEntry, this.graphicalMusicSheet.ParentMusicSheet.Transpose);
  917. }
  918. }
  919. }
  920. if (tieTimestampListDict.size() > 0) {
  921. this.handleOpenTies(
  922. measure, openBeams,
  923. tieTimestampListDict, activeClefs[staffIndex], openOctaveShifts[staffIndex]
  924. );
  925. }
  926. accidentalCalculator.doCalculationsAtEndOfMeasure();
  927. if (sourceMeasure.LastInstructionsStaffEntries[staffIndex] !== undefined) {
  928. let lastStaffEntry: SourceStaffEntry = sourceMeasure.LastInstructionsStaffEntries[staffIndex];
  929. for (let idx: number = 0, len: number = lastStaffEntry.Instructions.length; idx < len; ++idx) {
  930. let abstractNotationInstruction: AbstractNotationInstruction = lastStaffEntry.Instructions[idx];
  931. if (abstractNotationInstruction instanceof ClefInstruction) {
  932. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  933. }
  934. }
  935. }
  936. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  937. let multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  938. if (multiExpression.OctaveShiftEnd !== undefined && openOctaveShifts[staffIndex] !== undefined &&
  939. multiExpression.OctaveShiftEnd === openOctaveShifts[staffIndex].getOpenOctaveShift) {
  940. openOctaveShifts[staffIndex] = undefined;
  941. }
  942. }
  943. if (measure.staffEntries.length === 0) {
  944. let sourceStaffEntry: SourceStaffEntry = new SourceStaffEntry(undefined, staff);
  945. let note: Note = new Note(undefined, sourceStaffEntry, Fraction.createFromFraction(sourceMeasure.Duration), undefined);
  946. let graphicalStaffEntry: GraphicalStaffEntry = this.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  947. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  948. graphicalStaffEntry.relInMeasureTimestamp = new Fraction(0, 1);
  949. let graphicalNotes: GraphicalNote[] = [];
  950. graphicalStaffEntry.notes.push(graphicalNotes);
  951. let numberOfDots: number = note.calculateNumberOfNeededDots();
  952. let graphicalNote: GraphicalNote = this.symbolFactory.createNote( note,
  953. numberOfDots,
  954. graphicalStaffEntry,
  955. new ClefInstruction(ClefEnum.G, 0, 2),
  956. OctaveEnum.NONE);
  957. graphicalNotes.push(graphicalNote);
  958. graphicalStaffEntry.PositionAndShape.ChildElements.push(graphicalNote.PositionAndShape);
  959. }
  960. return measure;
  961. }
  962. private calculatePageLabels(page: GraphicalMusicPage): void {
  963. let relative: PointF2D = new PointF2D();
  964. let firstSystemAbsoluteTopMargin: number = 10;
  965. if (page.MusicSystems.length > 0) {
  966. let firstMusicSystem: MusicSystem = page.MusicSystems[0];
  967. firstSystemAbsoluteTopMargin = firstMusicSystem.PositionAndShape.RelativePosition.y + firstMusicSystem.PositionAndShape.BorderTop;
  968. }
  969. if (this.graphicalMusicSheet.Title !== undefined) {
  970. let title: GraphicalLabel = this.graphicalMusicSheet.Title;
  971. title.PositionAndShape.Parent = page.PositionAndShape;
  972. page.PositionAndShape.ChildElements.push(title.PositionAndShape);
  973. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  974. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight;
  975. title.PositionAndShape.RelativePosition = relative;
  976. page.Labels.push(title);
  977. }
  978. if (this.graphicalMusicSheet.Subtitle !== undefined) {
  979. let subtitle: GraphicalLabel = this.graphicalMusicSheet.Subtitle;
  980. subtitle.PositionAndShape.Parent = page.PositionAndShape;
  981. page.PositionAndShape.ChildElements.push(subtitle.PositionAndShape);
  982. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  983. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight + this.rules.SheetMinimumDistanceBetweenTitleAndSubtitle;
  984. subtitle.PositionAndShape.RelativePosition = relative;
  985. page.Labels.push(subtitle);
  986. }
  987. if (this.graphicalMusicSheet.Composer !== undefined) {
  988. let composer: GraphicalLabel = this.graphicalMusicSheet.Composer;
  989. composer.PositionAndShape.Parent = page.PositionAndShape;
  990. page.PositionAndShape.ChildElements.push(composer.PositionAndShape);
  991. composer.setLabelPositionAndShapeBorders();
  992. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth - this.rules.PageRightMargin;
  993. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  994. composer.PositionAndShape.RelativePosition = relative;
  995. page.Labels.push(composer);
  996. }
  997. if (this.graphicalMusicSheet.Lyricist !== undefined) {
  998. let lyricist: GraphicalLabel = this.graphicalMusicSheet.Lyricist;
  999. lyricist.PositionAndShape.Parent = page.PositionAndShape;
  1000. page.PositionAndShape.ChildElements.push(lyricist.PositionAndShape);
  1001. lyricist.setLabelPositionAndShapeBorders();
  1002. relative.x = this.rules.PageLeftMargin;
  1003. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  1004. lyricist.PositionAndShape.RelativePosition = relative;
  1005. page.Labels.push(lyricist);
  1006. }
  1007. }
  1008. private checkVoiceEntriesForTechnicalInstructions(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  1009. for (let idx: number = 0, len: number = voiceEntry.TechnicalInstructions.length; idx < len; ++idx) {
  1010. let technicalInstruction: TechnicalInstruction = voiceEntry.TechnicalInstructions[idx];
  1011. this.symbolFactory.createGraphicalTechnicalInstruction(technicalInstruction, graphicalStaffEntry);
  1012. }
  1013. }
  1014. private checkNoteForAccidental(graphicalNote: GraphicalNote, accidentalCalculator: AccidentalCalculator, activeClef: ClefInstruction,
  1015. octaveEnum: OctaveEnum, grace: boolean = false): void {
  1016. let pitch: Pitch = graphicalNote.sourceNote.Pitch;
  1017. let transpose: number = this.graphicalMusicSheet.ParentMusicSheet.Transpose;
  1018. if (transpose !== 0 && graphicalNote.sourceNote.ParentStaffEntry.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion) {
  1019. pitch = graphicalNote.Transpose(
  1020. accidentalCalculator.ActiveKeyInstruction, activeClef, transpose, octaveEnum
  1021. );
  1022. if (graphicalNote.sourceNote.NoteTie !== undefined) {
  1023. graphicalNote.sourceNote.NoteTie.BaseNoteYPosition = graphicalNote.PositionAndShape.RelativePosition.y;
  1024. }
  1025. }
  1026. graphicalNote.sourceNote.halfTone = pitch.getHalfTone();
  1027. let scalingFactor: number = 1.0;
  1028. if (grace) {
  1029. scalingFactor = this.rules.GraceNoteScalingFactor;
  1030. }
  1031. accidentalCalculator.checkAccidental(graphicalNote, pitch, grace, scalingFactor);
  1032. }
  1033. // needed to disable linter, as it doesn't recognize the existing usage of this method.
  1034. // ToDo: check if a newer version doesn't have the problem.
  1035. /* tslint:disable:no-unused-variable */
  1036. private createStaffEntryForTieNote(measure: StaffMeasure, absoluteTimestamp: Fraction, openTie: Tie): GraphicalStaffEntry {
  1037. /* tslint:enable:no-unused-variable */
  1038. let graphicalStaffEntry: GraphicalStaffEntry;
  1039. graphicalStaffEntry = this.symbolFactory.createStaffEntry(openTie.Start.ParentStaffEntry, measure);
  1040. graphicalStaffEntry.relInMeasureTimestamp = Fraction.minus(absoluteTimestamp, measure.parentSourceMeasure.AbsoluteTimestamp);
  1041. this.resetYPositionForLeadSheet(graphicalStaffEntry.PositionAndShape);
  1042. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  1043. return graphicalStaffEntry;
  1044. }
  1045. private updateSkyBottomLines(): void {
  1046. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1047. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1048. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1049. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1050. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1051. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1052. this.updateSkyBottomLine(staffLine);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. private handleStaffEntries(): void {
  1058. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MeasureList.length; idx < len; ++idx) {
  1059. let measures: StaffMeasure[] = this.graphicalMusicSheet.MeasureList[idx];
  1060. for (let idx2: number = 0, len2: number = measures.length; idx2 < len2; ++idx2) {
  1061. let measure: StaffMeasure = measures[idx2];
  1062. for (let idx3: number = 0, len3: number = measure.staffEntries.length; idx3 < len3; ++idx3) {
  1063. let graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx3];
  1064. if (graphicalStaffEntry.parentMeasure !== undefined && graphicalStaffEntry.notes.length > 0 && graphicalStaffEntry.notes[0].length > 0) {
  1065. this.layoutVoiceEntries(graphicalStaffEntry);
  1066. this.layoutStaffEntry(graphicalStaffEntry);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. private createGraphicalTies(): void {
  1073. for (let measureIndex: number = 0; measureIndex < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; measureIndex++) {
  1074. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[measureIndex];
  1075. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  1076. for (let j: number = 0; j < sourceMeasure.VerticalSourceStaffEntryContainers.length; j++) {
  1077. if (sourceMeasure.VerticalSourceStaffEntryContainers[j][staffIndex] !== undefined) {
  1078. let sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[j][staffIndex];
  1079. let startStaffEntry: GraphicalStaffEntry = this.graphicalMusicSheet.findGraphicalStaffEntryFromMeasureList(
  1080. staffIndex, measureIndex, sourceStaffEntry
  1081. );
  1082. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  1083. let voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  1084. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  1085. let note: Note = voiceEntry.Notes[idx2];
  1086. if (note.NoteTie !== undefined) {
  1087. let tie: Tie = note.NoteTie;
  1088. this.handleTie(tie, startStaffEntry, staffIndex, measureIndex);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. }
  1096. }
  1097. private calculateSkyBottomLines(): void {
  1098. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1099. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1100. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1101. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1102. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1103. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1104. this.calculateSkyBottomLine(staffLine);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. private calculateBeams(): void {
  1110. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1111. let musicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1112. for (let idx2: number = 0, len2: number = musicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1113. let musicSystem: MusicSystem = musicPage.MusicSystems[idx2];
  1114. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1115. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1116. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1117. let measure: StaffMeasure = staffLine.Measures[idx4];
  1118. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1119. let staffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1120. this.layoutBeams(staffEntry);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }
  1127. private calculateStaffEntryArticulationMarks(): void {
  1128. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1129. let page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1130. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1131. let system: MusicSystem = page.MusicSystems[idx2];
  1132. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1133. let line: StaffLine = system.StaffLines[idx3];
  1134. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1135. let measure: StaffMeasure = line.Measures[idx4];
  1136. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1137. let graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1138. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  1139. let voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  1140. if (voiceEntry.Articulations.length > 0) {
  1141. this.layoutArticulationMarks(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  1142. }
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }
  1148. }
  1149. }
  1150. private calculateOrnaments(): void {
  1151. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1152. let page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1153. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1154. let system: MusicSystem = page.MusicSystems[idx2];
  1155. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1156. let line: StaffLine = system.StaffLines[idx3];
  1157. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1158. let measure: StaffMeasure = line.Measures[idx4];
  1159. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1160. let graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1161. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  1162. let voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  1163. if (voiceEntry.OrnamentContainer !== undefined) {
  1164. if (voiceEntry.hasTie() && graphicalStaffEntry.relInMeasureTimestamp !== voiceEntry.Timestamp) {
  1165. continue;
  1166. }
  1167. this.layoutOrnament(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  1168. if (!(this.staffEntriesWithOrnaments.indexOf(graphicalStaffEntry) !== -1)) {
  1169. this.staffEntriesWithOrnaments.push(graphicalStaffEntry);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. private optimizeRestPlacement(): void {
  1180. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1181. let page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1182. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1183. let system: MusicSystem = page.MusicSystems[idx2];
  1184. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1185. let line: StaffLine = system.StaffLines[idx3];
  1186. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1187. let measure: StaffMeasure = line.Measures[idx4];
  1188. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1189. let graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1190. this.optimizeRestNotePlacement(graphicalStaffEntry, measure);
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. private calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry: GraphicalStaffEntry): void {
  1198. let firstRestNote: GraphicalNote = graphicalStaffEntry.notes[0][0];
  1199. let secondRestNote: GraphicalNote = graphicalStaffEntry.notes[1][0];
  1200. secondRestNote.PositionAndShape.RelativePosition = new PointF2D(0.0, 2.5);
  1201. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  1202. firstRestNote.PositionAndShape.computeNonOverlappingPositionWithMargin(
  1203. graphicalStaffEntry.PositionAndShape, ColDirEnum.Up,
  1204. new PointF2D(0.0, secondRestNote.PositionAndShape.RelativePosition.y)
  1205. );
  1206. let relative: PointF2D = firstRestNote.PositionAndShape.RelativePosition;
  1207. relative.y -= 1.0;
  1208. firstRestNote.PositionAndShape.RelativePosition = relative;
  1209. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  1210. }
  1211. private calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry: GraphicalStaffEntry): void {
  1212. let restNote: GraphicalNote;
  1213. let graphicalNotes: GraphicalNote[];
  1214. if (graphicalStaffEntry.notes[0][0].sourceNote.Pitch === undefined) {
  1215. restNote = graphicalStaffEntry.notes[0][0];
  1216. graphicalNotes = graphicalStaffEntry.notes[1];
  1217. } else {
  1218. graphicalNotes = graphicalStaffEntry.notes[0];
  1219. restNote = graphicalStaffEntry.notes[1][0];
  1220. }
  1221. let collision: boolean = false;
  1222. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  1223. for (let idx: number = 0, len: number = graphicalNotes.length; idx < len; ++idx) {
  1224. let graphicalNote: GraphicalNote = graphicalNotes[idx];
  1225. if (restNote.PositionAndShape.marginCollisionDetection(graphicalNote.PositionAndShape)) {
  1226. collision = true;
  1227. break;
  1228. }
  1229. }
  1230. if (collision) {
  1231. if (restNote.sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  1232. let bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  1233. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  1234. } else {
  1235. let last: GraphicalNote = graphicalNotes[graphicalNotes.length - 1];
  1236. let topBorder: number = last.PositionAndShape.BorderMarginTop + last.PositionAndShape.RelativePosition.y;
  1237. if (graphicalNotes[0].sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  1238. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.5);
  1239. } else {
  1240. let bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  1241. if (bottomBorder < 2.0) {
  1242. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  1243. } else {
  1244. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.0);
  1245. }
  1246. }
  1247. }
  1248. }
  1249. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  1250. }
  1251. private calculateTieCurves(): void {
  1252. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1253. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1254. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1255. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1256. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1257. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1258. for (let idx4: number = 0, len5: number = staffLine.Measures.length; idx4 < len5; ++idx4) {
  1259. let measure: StaffMeasure = staffLine.Measures[idx4];
  1260. for (let idx6: number = 0, len6: number = measure.staffEntries.length; idx6 < len6; ++idx6) {
  1261. let staffEntry: GraphicalStaffEntry = measure.staffEntries[idx6];
  1262. let graphicalTies: GraphicalTie[] = staffEntry.GraphicalTies;
  1263. for (let idx7: number = 0, len7: number = graphicalTies.length; idx7 < len7; ++idx7) {
  1264. let graphicalTie: GraphicalTie = graphicalTies[idx7];
  1265. if (graphicalTie.StartNote !== undefined && graphicalTie.StartNote.parentStaffEntry === staffEntry) {
  1266. let tieIsAtSystemBreak: boolean = (
  1267. graphicalTie.StartNote.parentStaffEntry.parentMeasure.ParentStaffLine !==
  1268. graphicalTie.EndNote.parentStaffEntry.parentMeasure.ParentStaffLine
  1269. );
  1270. this.layoutGraphicalTie(graphicalTie, tieIsAtSystemBreak);
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. // Commented because unused:
  1280. //private calculateFingering(): void {
  1281. // for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1282. // let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1283. // for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1284. // let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1285. // for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1286. // let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1287. // let skyBottomLineCalculator: SkyBottomLineCalculator = new SkyBottomLineCalculator(this.rules);
  1288. // for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1289. // let measure: StaffMeasure = staffLine.Measures[idx4];
  1290. // let measureRelativePosition: PointF2D = measure.PositionAndShape.RelativePosition;
  1291. // for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1292. // let staffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1293. // let hasTechnicalInstruction: boolean = false;
  1294. // for (let idx6: number = 0, len6: number = staffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  1295. // let ve: VoiceEntry = staffEntry.sourceStaffEntry.VoiceEntries[idx6];
  1296. // if (ve.TechnicalInstructions.length > 0) {
  1297. // hasTechnicalInstruction = true;
  1298. // break;
  1299. // }
  1300. // }
  1301. // if (hasTechnicalInstruction) {
  1302. // this.layoutFingering(staffLine, skyBottomLineCalculator, staffEntry, measureRelativePosition);
  1303. // }
  1304. // }
  1305. // }
  1306. // }
  1307. // }
  1308. // }
  1309. //}
  1310. private calculateLyricsPosition(): void {
  1311. for (let idx: number = 0, len: number = this.graphicalMusicSheet.ParentMusicSheet.Instruments.length; idx < len; ++idx) {
  1312. let instrument: Instrument = this.graphicalMusicSheet.ParentMusicSheet.Instruments[idx];
  1313. if (instrument.HasLyrics && instrument.LyricVersesNumbers.length > 0) {
  1314. instrument.LyricVersesNumbers.sort();
  1315. }
  1316. }
  1317. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1318. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1319. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1320. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1321. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1322. let staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1323. this.calculateSingleStaffLineLyricsPosition(staffLine, staffLine.ParentStaff.ParentInstrument.LyricVersesNumbers);
  1324. }
  1325. }
  1326. }
  1327. }
  1328. private calculateDynamicExpressions(): void {
  1329. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1330. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1331. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1332. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1333. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1334. if (sourceMeasure.StaffLinkedExpressions[j][k].InstantaniousDynamic !== undefined ||
  1335. (sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic !== undefined &&
  1336. sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic.StartMultiExpression ===
  1337. sourceMeasure.StaffLinkedExpressions[j][k] && sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length === 0)
  1338. ) {
  1339. this.calculateDynamicExpressionsForSingleMultiExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1340. }
  1341. }
  1342. }
  1343. }
  1344. }
  1345. }
  1346. private calculateOctaveShifts(): void {
  1347. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1348. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1349. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1350. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1351. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1352. if ((sourceMeasure.StaffLinkedExpressions[j][k].OctaveShiftStart !== undefined)) {
  1353. this.calculateSingleOctaveShift(sourceMeasure, sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. private getFirstLeftNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  1361. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  1362. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  1363. }
  1364. for (let i: number = horizontalIndex - 1; i >= 0; i--) {
  1365. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  1366. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  1367. }
  1368. }
  1369. return undefined;
  1370. }
  1371. private getFirstRightNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  1372. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  1373. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  1374. }
  1375. for (let i: number = horizontalIndex + 1; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  1376. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  1377. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  1378. }
  1379. }
  1380. return undefined;
  1381. }
  1382. private calculateWordRepetitionInstructions(): void {
  1383. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1384. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1385. for (let idx: number = 0, len: number = sourceMeasure.FirstRepetitionInstructions.length; idx < len; ++idx) {
  1386. let instruction: RepetitionInstruction = sourceMeasure.FirstRepetitionInstructions[idx];
  1387. this.calculateWordRepetitionInstruction(instruction, i);
  1388. }
  1389. for (let idx: number = 0, len: number = sourceMeasure.LastRepetitionInstructions.length; idx < len; ++idx) {
  1390. let instruction: RepetitionInstruction = sourceMeasure.LastRepetitionInstructions[idx];
  1391. this.calculateWordRepetitionInstruction(instruction, i);
  1392. }
  1393. }
  1394. }
  1395. private calculateRepetitionEndings(): void {
  1396. let musicsheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  1397. for (let idx: number = 0, len: number = musicsheet.Repetitions.length; idx < len; ++idx) {
  1398. let partListEntry: Repetition = musicsheet.Repetitions[idx];
  1399. this.calcGraphicalRepetitionEndingsRecursively(partListEntry);
  1400. }
  1401. }
  1402. private calculateTempoExpressions(): void {
  1403. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1404. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1405. for (let j: number = 0; j < sourceMeasure.TempoExpressions.length; j++) {
  1406. this.calculateTempoExpressionsForSingleMultiTempoExpression(sourceMeasure, sourceMeasure.TempoExpressions[j], i);
  1407. }
  1408. }
  1409. }
  1410. private calculateMoodAndUnknownExpressions(): void {
  1411. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1412. let sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1413. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1414. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1415. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1416. if ((sourceMeasure.StaffLinkedExpressions[j][k].MoodList.length > 0) ||
  1417. (sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length > 0)) {
  1418. this.calculateMoodAndUnknownExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1419. }
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. }