VexFlowMusicSheetCalculator.ts 9.5 KB

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