VexFlowMusicSheetCalculator.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {MusicSheetCalculator} from "../MusicSheetCalculator";
  2. import {VexFlowGraphicalSymbolFactory} from "./VexFlowGraphicalSymbolFactory";
  3. import {GraphicalMusicSheet} from "../GraphicalMusicSheet";
  4. import {StaffMeasure} from "../StaffMeasure";
  5. import {MusicSystemBuilder} from "../MusicSystemBuilder";
  6. import {StaffLine} from "../StaffLine";
  7. import {VoiceEntry} from "../../VoiceData/VoiceEntry";
  8. import {MusicSystem} from "../MusicSystem";
  9. import {GraphicalNote} from "../GraphicalNote";
  10. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  11. import {GraphicalMusicPage} from "../GraphicalMusicPage";
  12. import {GraphicalTie} from "../GraphicalTie";
  13. import {Tie} from "../../VoiceData/Tie";
  14. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  15. import {MultiExpression} from "../../VoiceData/Expressions/multiExpression";
  16. import {RepetitionInstruction} from "../../VoiceData/Instructions/RepetitionInstruction";
  17. import {Beam} from "../../VoiceData/Beam";
  18. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  19. import {OctaveEnum} from "../../VoiceData/Expressions/ContinuousExpressions/octaveShift";
  20. import {Fraction} from "../../../Common/DataObjects/fraction";
  21. import {LyricsEntry} from "../../VoiceData/Lyrics/LyricsEntry";
  22. import {LyricWord} from "../../VoiceData/Lyrics/LyricsWord";
  23. import {OrnamentContainer} from "../../VoiceData/OrnamentContainer";
  24. import {ArticulationEnum} from "../../VoiceData/VoiceEntry";
  25. import {Tuplet} from "../../VoiceData/Tuplet";
  26. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  27. import {VexFlowMeasure} from "./VexFlowMeasure";
  28. import {VexFlowTextMeasurer} from "./VexFlowTextMeasurer";
  29. //import {VexFlowMeasure} from "./VexFlowMeasure";
  30. import Vex = require("vexflow");
  31. export class VexFlowMusicSheetCalculator extends MusicSheetCalculator {
  32. constructor() {
  33. super(new VexFlowGraphicalSymbolFactory());
  34. MusicSheetCalculator.TextMeasurer = new VexFlowTextMeasurer();
  35. }
  36. /**
  37. * The main method for the Calculator.
  38. */
  39. public calculate(): void {
  40. this.clearSystemsAndMeasures();
  41. this.clearRecreatedObjects();
  42. this.calculateXLayout(this.graphicalMusicSheet, this.maxInstrNameLabelLength());
  43. this.graphicalMusicSheet.MusicPages.length = 0;
  44. this.calculateMusicSystems();
  45. this.graphicalMusicSheet.MusicPages[0].PositionAndShape.BorderMarginBottom += 9;
  46. GraphicalMusicSheet.transformRelativeToAbsolutePosition(this.graphicalMusicSheet);
  47. }
  48. /**
  49. * Calculates the x layout of the staff entries within the staff measures belonging to one source measure.
  50. * All staff entries are x-aligned throughout all vertically aligned staff measures.
  51. * This method is called within calculateXLayout.
  52. * The staff entries are aligned with minimum needed x distances.
  53. * The MinimumStaffEntriesWidth of every measure will be set - needed for system building.
  54. * @param measures
  55. * @returns the minimum required x width of the source measure (=list of staff measures)
  56. */
  57. protected calculateMeasureXLayout(measures: StaffMeasure[]): number {
  58. // Finalize beams
  59. for (let measure of measures) {
  60. (measure as VexFlowMeasure).finalizeBeams();
  61. }
  62. // Format the voices
  63. let allVoices: Vex.Flow.Voice[] = [];
  64. let formatter: Vex.Flow.Formatter = new Vex.Flow.Formatter();
  65. for (let measure of measures) {
  66. let mvoices: { [voiceID: number]: Vex.Flow.Voice; } = (measure as VexFlowMeasure).vfVoices;
  67. let voices: Vex.Flow.Voice[] = [];
  68. for (let voiceID in mvoices) {
  69. if (mvoices.hasOwnProperty(voiceID)) {
  70. voices.push(mvoices[voiceID]);
  71. allVoices.push(mvoices[voiceID]);
  72. }
  73. }
  74. if (voices.length === 0) {
  75. console.log("Found a measure with no voices... Continuing anyway.", mvoices);
  76. continue;
  77. }
  78. formatter.joinVoices(voices);
  79. }
  80. let firstMeasure: VexFlowMeasure = measures[0] as VexFlowMeasure;
  81. let width: number = formatter.preCalculateMinTotalWidth(allVoices) / firstMeasure.unit;
  82. for (let measure of measures) {
  83. measure.minimumStaffEntriesWidth = width;
  84. (measure as VexFlowMeasure).formatVoices = undefined;
  85. }
  86. firstMeasure.formatVoices = (w: number) => {
  87. formatter.format(allVoices, w);
  88. };
  89. return width;
  90. }
  91. /**
  92. * Creates the music systems and calculates their layout.
  93. */
  94. protected calculateMusicSystems(): void {
  95. let measureList: StaffMeasure[][] = [];
  96. for (let mlist of this.graphicalMusicSheet.MeasureList) {
  97. let list: StaffMeasure[] = [];
  98. for (let m of mlist) {
  99. if (m.isVisible()) {
  100. list.push(m);
  101. }
  102. }
  103. measureList.push(list);
  104. }
  105. let numberOfStaffLines: number = 0;
  106. for (let idx: number = 0, len: number = measureList.length; idx < len; ++idx) {
  107. let gmlist: StaffMeasure[] = measureList[idx];
  108. numberOfStaffLines = Math.max(gmlist.length, numberOfStaffLines);
  109. }
  110. if (numberOfStaffLines === 0) { return; }
  111. let musicSystemBuilder: MusicSystemBuilder = new MusicSystemBuilder();
  112. musicSystemBuilder.initialize(this.graphicalMusicSheet, measureList, numberOfStaffLines, this.symbolFactory);
  113. musicSystemBuilder.buildMusicSystems();
  114. this.checkMeasuresForWholeRestNotes();
  115. this.calculateSystemYLayout();
  116. }
  117. protected updateStaffLineBorders(staffLine: StaffLine): void {
  118. return;
  119. }
  120. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  121. return;
  122. }
  123. /**
  124. * Can be used to calculate stem directions, helper(ledger) lines, and overlapping note x-displacement.
  125. * Is Excecuted per voice entry of a staff entry.
  126. * After that layoutStaffEntry is called.
  127. * @param voiceEntry
  128. * @param graphicalNotes
  129. * @param graphicalStaffEntry
  130. * @param hasPitchedNote
  131. * @param isGraceStaffEntry
  132. */
  133. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[], graphicalStaffEntry: GraphicalStaffEntry,
  134. hasPitchedNote: boolean, isGraceStaffEntry: boolean): void {
  135. return;
  136. }
  137. /**
  138. * Do all layout calculations that have to be done per staff entry, like dots, ornaments, arpeggios....
  139. * This method is called after the voice entries are handled by layoutVoiceEntry().
  140. * @param graphicalStaffEntry
  141. */
  142. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  143. (graphicalStaffEntry.parentMeasure as VexFlowMeasure).layoutStaffEntry(graphicalStaffEntry);
  144. }
  145. /**
  146. * calculates the y positions of the staff lines within a system and
  147. * furthermore the y positions of the systems themselves.
  148. */
  149. protected calculateSystemYLayout(): void {
  150. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  151. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  152. if (!this.leadSheet) {
  153. let globalY: number = 0;
  154. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  155. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  156. // calculate y positions of stafflines within system
  157. let y: number = 0;
  158. for (let line of musicSystem.StaffLines) {
  159. line.PositionAndShape.RelativePosition.y = y;
  160. y += 10;
  161. }
  162. // set y positions of systems using the previous system and a fixed distance.
  163. musicSystem.PositionAndShape.BorderBottom = y + 10;
  164. musicSystem.PositionAndShape.RelativePosition.y = globalY;
  165. globalY += y + 10;
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Is called at the begin of the method for creating the vertically aligned staff measures belonging to one source measure.
  172. */
  173. protected initStaffMeasuresCreation(): void {
  174. return;
  175. }
  176. protected handleTie(tie: Tie, startGraphicalStaffEntry: GraphicalStaffEntry, staffIndex: number, measureIndex: number): void {
  177. return;
  178. }
  179. protected layoutGraphicalTie(tie: GraphicalTie, tieIsAtSystemBreak: boolean): void {
  180. return;
  181. }
  182. protected calculateSingleStaffLineLyricsPosition(staffLine: StaffLine, lyricVersesNumber: number[]): void {
  183. return;
  184. }
  185. protected calculateSingleOctaveShift(sourceMeasure: SourceMeasure, multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  186. return;
  187. }
  188. protected calculateWordRepetitionInstruction(repetitionInstruction: RepetitionInstruction, measureIndex: number): void {
  189. return;
  190. }
  191. protected calculateMoodAndUnknownExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  192. return;
  193. }
  194. protected createGraphicalTieNote(beams: Beam[], activeClef: ClefInstruction,
  195. octaveShiftValue: OctaveEnum, graphicalStaffEntry: GraphicalStaffEntry, duration: Fraction, numberOfDots: number,
  196. openTie: Tie, isLastTieNote: boolean): void {
  197. return;
  198. }
  199. /**
  200. * Is called if a note is part of a beam.
  201. * @param graphicalNote
  202. * @param beam
  203. * @param openBeams a list of all currently open beams
  204. */
  205. protected handleBeam(graphicalNote: GraphicalNote, beam: Beam, openBeams: Beam[]): void {
  206. (graphicalNote.parentStaffEntry.parentMeasure as VexFlowMeasure).handleBeam(graphicalNote, beam);
  207. }
  208. protected handleVoiceEntryLyrics(lyricsEntries: Dictionary<number, LyricsEntry>, voiceEntry: VoiceEntry,
  209. graphicalStaffEntry: GraphicalStaffEntry, openLyricWords: LyricWord[]): void {
  210. return;
  211. }
  212. protected handleVoiceEntryOrnaments(ornamentContainer: OrnamentContainer, voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  213. return;
  214. }
  215. protected handleVoiceEntryArticulations(articulations: ArticulationEnum[], voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  216. return;
  217. }
  218. /**
  219. * Is called if a note is part of a tuplet.
  220. * @param graphicalNote
  221. * @param tuplet
  222. * @param openTuplets a list of all currently open tuplets
  223. */
  224. protected handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet, openTuplets: Tuplet[]): void {
  225. return;
  226. }
  227. }