VexFlowMusicSheetCalculator.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import {MusicSheetCalculator} from "../MusicSheetCalculator";
  2. import {VexFlowGraphicalSymbolFactory} from "./VexFlowGraphicalSymbolFactory";
  3. import {StaffMeasure} from "../StaffMeasure";
  4. import {StaffLine} from "../StaffLine";
  5. import {VoiceEntry} from "../../VoiceData/VoiceEntry";
  6. import {MusicSystem} from "../MusicSystem";
  7. import {GraphicalNote} from "../GraphicalNote";
  8. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  9. import {GraphicalMusicPage} from "../GraphicalMusicPage";
  10. import {GraphicalTie} from "../GraphicalTie";
  11. import {Tie} from "../../VoiceData/Tie";
  12. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  13. import {MultiExpression} from "../../VoiceData/Expressions/multiExpression";
  14. import {RepetitionInstruction} from "../../VoiceData/Instructions/RepetitionInstruction";
  15. import {Beam} from "../../VoiceData/Beam";
  16. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  17. import {OctaveEnum} from "../../VoiceData/Expressions/ContinuousExpressions/octaveShift";
  18. import {Fraction} from "../../../Common/DataObjects/fraction";
  19. import {LyricsEntry} from "../../VoiceData/Lyrics/LyricsEntry";
  20. import {LyricWord} from "../../VoiceData/Lyrics/LyricsWord";
  21. import {OrnamentContainer} from "../../VoiceData/OrnamentContainer";
  22. import {ArticulationEnum} from "../../VoiceData/VoiceEntry";
  23. import {Tuplet} from "../../VoiceData/Tuplet";
  24. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  25. import {VexFlowMeasure} from "./VexFlowMeasure";
  26. import {VexFlowTextMeasurer} from "./VexFlowTextMeasurer";
  27. import Vex = require("vexflow");
  28. import {Logging} from "../../../Common/Logging";
  29. export class VexFlowMusicSheetCalculator extends MusicSheetCalculator {
  30. constructor() {
  31. super(new VexFlowGraphicalSymbolFactory());
  32. MusicSheetCalculator.TextMeasurer = new VexFlowTextMeasurer();
  33. }
  34. protected clearRecreatedObjects(): void {
  35. super.clearRecreatedObjects();
  36. for (let staffMeasures of this.graphicalMusicSheet.MeasureList) {
  37. for (let staffMeasure of staffMeasures) {
  38. (<VexFlowMeasure>staffMeasure).clean();
  39. }
  40. }
  41. }
  42. //protected clearSystemsAndMeasures(): void {
  43. // for (let measure of measures) {
  44. //
  45. // }
  46. //}
  47. /**
  48. * Calculates the x layout of the staff entries within the staff measures belonging to one source measure.
  49. * All staff entries are x-aligned throughout all vertically aligned staff measures.
  50. * This method is called within calculateXLayout.
  51. * The staff entries are aligned with minimum needed x distances.
  52. * The MinimumStaffEntriesWidth of every measure will be set - needed for system building.
  53. * @param measures
  54. * @returns the minimum required x width of the source measure (=list of staff measures)
  55. */
  56. protected calculateMeasureXLayout(measures: StaffMeasure[]): number {
  57. // Finalize beams
  58. for (let measure of measures) {
  59. (measure as VexFlowMeasure).finalizeBeams();
  60. }
  61. // Format the voices
  62. let allVoices: Vex.Flow.Voice[] = [];
  63. let formatter: Vex.Flow.Formatter = new Vex.Flow.Formatter({
  64. align_rests: true,
  65. });
  66. for (let measure of measures) {
  67. let mvoices: { [voiceID: number]: Vex.Flow.Voice; } = (measure as VexFlowMeasure).vfVoices;
  68. let voices: Vex.Flow.Voice[] = [];
  69. for (let voiceID in mvoices) {
  70. if (mvoices.hasOwnProperty(voiceID)) {
  71. voices.push(mvoices[voiceID]);
  72. allVoices.push(mvoices[voiceID]);
  73. }
  74. }
  75. if (voices.length === 0) {
  76. Logging.warn("Found a measure with no voices... Continuing anyway.", mvoices);
  77. continue;
  78. }
  79. formatter.joinVoices(voices);
  80. }
  81. let firstMeasure: VexFlowMeasure = measures[0] as VexFlowMeasure;
  82. // FIXME: The following ``+ 5.0'' is temporary: it was added as a workaround for
  83. // FIXME: a more relaxed formatting of voices
  84. let width: number = formatter.preCalculateMinTotalWidth(allVoices) / 10.0 + 5.0;
  85. for (let measure of measures) {
  86. measure.minimumStaffEntriesWidth = width;
  87. (measure as VexFlowMeasure).formatVoices = undefined;
  88. }
  89. firstMeasure.formatVoices = (w: number) => {
  90. formatter.format(allVoices, w);
  91. };
  92. return width;
  93. }
  94. protected updateStaffLineBorders(staffLine: StaffLine): void {
  95. return;
  96. }
  97. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  98. return;
  99. }
  100. /**
  101. * Can be used to calculate stem directions, helper(ledger) lines, and overlapping note x-displacement.
  102. * Is Excecuted per voice entry of a staff entry.
  103. * After that layoutStaffEntry is called.
  104. * @param voiceEntry
  105. * @param graphicalNotes
  106. * @param graphicalStaffEntry
  107. * @param hasPitchedNote
  108. * @param isGraceStaffEntry
  109. */
  110. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[], graphicalStaffEntry: GraphicalStaffEntry,
  111. hasPitchedNote: boolean, isGraceStaffEntry: boolean): void {
  112. return;
  113. }
  114. /**
  115. * Do all layout calculations that have to be done per staff entry, like dots, ornaments, arpeggios....
  116. * This method is called after the voice entries are handled by layoutVoiceEntry().
  117. * @param graphicalStaffEntry
  118. */
  119. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  120. (graphicalStaffEntry.parentMeasure as VexFlowMeasure).layoutStaffEntry(graphicalStaffEntry);
  121. }
  122. /**
  123. * calculates the y positions of the staff lines within a system and
  124. * furthermore the y positions of the systems themselves.
  125. */
  126. protected calculateSystemYLayout(): void {
  127. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  128. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  129. if (!this.leadSheet) {
  130. let globalY: number = 0;
  131. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  132. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  133. // calculate y positions of stafflines within system
  134. let y: number = 10;
  135. for (let line of musicSystem.StaffLines) {
  136. line.PositionAndShape.RelativePosition.y = y;
  137. y += 10;
  138. }
  139. // set y positions of systems using the previous system and a fixed distance.
  140. musicSystem.PositionAndShape.BorderBottom = y + 0;
  141. musicSystem.PositionAndShape.RelativePosition.y = globalY;
  142. globalY += y + 0;
  143. }
  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. (graphicalNote.parentStaffEntry.parentMeasure as VexFlowMeasure).handleBeam(graphicalNote, beam);
  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. }