VexFlowMeasure.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import Vex = require("vexflow");
  2. import {StaffMeasure} from "../StaffMeasure";
  3. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  4. import {Staff} from "../../VoiceData/Staff";
  5. import {StaffLine} from "../StaffLine";
  6. import {SystemLinesEnum} from "../SystemLinesEnum";
  7. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  8. import {KeyInstruction} from "../../VoiceData/Instructions/KeyInstruction";
  9. import {RhythmInstruction} from "../../VoiceData/Instructions/RhythmInstruction";
  10. import {VexFlowConverter} from "./VexFlowConverter";
  11. import {VexFlowStaffEntry} from "./VexFlowStaffEntry";
  12. import {Beam} from "../../VoiceData/Beam";
  13. import {GraphicalNote} from "../GraphicalNote";
  14. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  15. import StaveConnector = Vex.Flow.StaveConnector;
  16. import StaveModifier = Vex.Flow.StaveModifier;
  17. import StaveNote = Vex.Flow.StaveNote;
  18. export class VexFlowMeasure extends StaffMeasure {
  19. constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
  20. super(staff, sourceMeasure, staffLine);
  21. this.minimumStaffEntriesWidth = -1;
  22. this.resetLayout();
  23. }
  24. // octaveOffset according to active clef
  25. public octaveOffset: number = 3;
  26. // The VexFlow Voices in the measure
  27. public vfVoices: { [voiceID: number]: Vex.Flow.Voice; } = {};
  28. // Call this function (if present) to x-format all the voices in the measure
  29. public formatVoices: (width: number) => void;
  30. // The unit
  31. public unit: number = 10.0;
  32. // The VexFlow Stave (one measure in one line)
  33. private stave: Vex.Flow.Stave;
  34. // VexFlow StaveConnectors (vertical lines)
  35. private connectors: Vex.Flow.StaveConnector[] = [];
  36. // Intermediate object to construct beams
  37. private beams: { [voiceID: number]: [Beam, VexFlowStaffEntry[]][]; } = {};
  38. // VexFlow Beams
  39. private vfbeams: { [voiceID: number]: Vex.Flow.Beam[]; } = {};
  40. // Sets the absolute coordinates of the VFStave on the canvas
  41. public setAbsoluteCoordinates(x: number, y: number): void {
  42. this.stave.setX(x).setY(y);
  43. }
  44. /**
  45. * Reset all the geometric values and parameters of this measure and put it in an initialized state.
  46. * This is needed to evaluate a measure a second time by system builder.
  47. */
  48. public resetLayout(): void {
  49. // Take into account some space for the begin and end lines of the stave
  50. // Will be changed when repetitions will be implemented
  51. this.beginInstructionsWidth = 20 / this.unit;
  52. this.endInstructionsWidth = 20 / this.unit;
  53. this.stave = new Vex.Flow.Stave(0, 0, 0);
  54. }
  55. public clean(): void {
  56. //this.beams = {};
  57. //this.vfbeams = {};
  58. this.connectors = [];
  59. console.log("clean!");
  60. }
  61. /**
  62. * returns the x-width of a given measure line.
  63. * @param line
  64. * @returns {SystemLinesEnum} the x-width
  65. */
  66. public getLineWidth(line: SystemLinesEnum): number {
  67. // FIXME: See values in VexFlow's stavebarline.js
  68. let vfline: any = VexFlowConverter.line(line);
  69. switch (vfline) {
  70. case Vex.Flow.StaveConnector.type.SINGLE:
  71. return 1.0 / this.unit;
  72. case Vex.Flow.StaveConnector.type.DOUBLE:
  73. return 3.0 / this.unit;
  74. default:
  75. return 0;
  76. }
  77. }
  78. /**
  79. * adds the given clef to the begin of the measure.
  80. * This has to update/increase BeginInstructionsWidth.
  81. * @param clef
  82. */
  83. public addClefAtBegin(clef: ClefInstruction): void {
  84. this.octaveOffset = clef.OctaveOffset;
  85. let vfclef: string = VexFlowConverter.Clef(clef);
  86. this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.Modifier.Position.BEGIN);
  87. this.increaseBeginInstructionWidth();
  88. }
  89. /**
  90. * adds the given key to the begin of the measure.
  91. * This has to update/increase BeginInstructionsWidth.
  92. * @param currentKey the new valid key.
  93. * @param previousKey the old cancelled key. Needed to show which accidentals are not valid any more.
  94. * @param currentClef the valid clef. Needed to put the accidentals on the right y-positions.
  95. */
  96. public addKeyAtBegin(currentKey: KeyInstruction, previousKey: KeyInstruction, currentClef: ClefInstruction): void {
  97. let keySig: Vex.Flow.KeySignature = new Vex.Flow.KeySignature(
  98. VexFlowConverter.keySignature(currentKey),
  99. VexFlowConverter.keySignature(previousKey)
  100. );
  101. this.stave.addModifier(keySig, Vex.Flow.Modifier.Position.BEGIN);
  102. }
  103. /**
  104. * adds the given rhythm to the begin of the measure.
  105. * This has to update/increase BeginInstructionsWidth.
  106. * @param rhythm
  107. */
  108. public addRhythmAtBegin(rhythm: RhythmInstruction): void {
  109. let timeSig: Vex.Flow.TimeSignature = VexFlowConverter.TimeSignature(rhythm);
  110. this.stave.addModifier(
  111. timeSig,
  112. Vex.Flow.Modifier.Position.BEGIN
  113. );
  114. this.increaseBeginInstructionWidth();
  115. }
  116. /**
  117. * adds the given clef to the end of the measure.
  118. * This has to update/increase EndInstructionsWidth.
  119. * @param clef
  120. */
  121. public addClefAtEnd(clef: ClefInstruction): void {
  122. let vfclef: string = VexFlowConverter.Clef(clef);
  123. this.stave.setEndClef(vfclef, undefined, undefined);
  124. this.increaseEndInstructionWidth();
  125. }
  126. /**
  127. * Sets the overall x-width of the measure.
  128. * @param width
  129. */
  130. public setWidth(width: number): void {
  131. super.setWidth(width);
  132. // Set the width of the Vex.Flow.Stave
  133. this.stave.setWidth(width * this.unit);
  134. // If this is the first stave in the vertical measure, call the format
  135. // method to set the width of all the voices
  136. if (this.formatVoices) {
  137. // The width of the voices does not include the instructions (StaveModifiers)
  138. this.formatVoices((width - this.beginInstructionsWidth - this.endInstructionsWidth) * this.unit);
  139. }
  140. }
  141. /**
  142. * This method is called after the StaffEntriesScaleFactor has been set.
  143. * Here the final x-positions of the staff entries have to be set.
  144. * (multiply the minimal positions with the scaling factor, considering the BeginInstructionsWidth)
  145. */
  146. public layoutSymbols(): void {
  147. this.stave.format();
  148. }
  149. //public addGraphicalStaffEntry(entry: VexFlowStaffEntry): void {
  150. // super.addGraphicalStaffEntry(entry);
  151. //}
  152. //
  153. //public addGraphicalStaffEntryAtTimestamp(entry: VexFlowStaffEntry): void {
  154. // super.addGraphicalStaffEntryAtTimestamp(entry);
  155. // // TODO
  156. //}
  157. /**
  158. * Draw this measure on a VexFlow CanvasContext
  159. * @param ctx
  160. */
  161. public draw(ctx: Vex.Flow.CanvasContext): void {
  162. // Draw stave lines
  163. this.stave.setContext(ctx).draw();
  164. // Draw all voices
  165. for (let voiceID in this.vfVoices) {
  166. if (this.vfVoices.hasOwnProperty(voiceID)) {
  167. this.vfVoices[voiceID].draw(ctx, this.stave);
  168. }
  169. }
  170. // Draw beams
  171. for (let voiceID in this.vfbeams) {
  172. if (this.vfbeams.hasOwnProperty(voiceID)) {
  173. for (let beam of this.vfbeams[voiceID]) {
  174. beam.setContext(ctx).draw();
  175. }
  176. }
  177. }
  178. // Draw vertical lines
  179. for (let connector of this.connectors) {
  180. connector.setContext(ctx).draw();
  181. }
  182. }
  183. /**
  184. * Add a note to a beam
  185. * @param graphicalNote
  186. * @param beam
  187. */
  188. public handleBeam(graphicalNote: GraphicalNote, beam: Beam): void {
  189. let voiceID: number = graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice.VoiceId;
  190. let beams: [Beam, VexFlowStaffEntry[]][] = this.beams[voiceID];
  191. if (beams === undefined) {
  192. beams = this.beams[voiceID] = [];
  193. }
  194. let data: [Beam, VexFlowStaffEntry[]];
  195. for (let mybeam of beams) {
  196. if (mybeam[0] === beam) {
  197. data = mybeam;
  198. }
  199. }
  200. if (data === undefined) {
  201. data = [beam, []];
  202. beams.push(data);
  203. }
  204. let parent: VexFlowStaffEntry = graphicalNote.parentStaffEntry as VexFlowStaffEntry;
  205. if (data[1].indexOf(parent) === -1) {
  206. data[1].push(parent);
  207. }
  208. }
  209. /**
  210. * Complete the creation of VexFlow Beams in this measure
  211. */
  212. public finalizeBeams(): void {
  213. for (let voiceID in this.beams) {
  214. if (this.beams.hasOwnProperty(voiceID)) {
  215. let vfbeams: Vex.Flow.Beam[] = this.vfbeams[voiceID];
  216. if (vfbeams === undefined) {
  217. vfbeams = this.vfbeams[voiceID] = [];
  218. }
  219. for (let beam of this.beams[voiceID]) {
  220. let notes: Vex.Flow.StaveNote[] = [];
  221. for (let entry of beam[1]) {
  222. notes.push((<VexFlowStaffEntry>entry).vfNotes[voiceID]);
  223. }
  224. if (notes.length > 1) {
  225. vfbeams.push(new Vex.Flow.Beam(notes, true));
  226. } else {
  227. console.log("Warning! Beam with no notes! Trying to ignore, but this is a serious problem.");
  228. }
  229. }
  230. }
  231. }
  232. }
  233. public layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  234. let gnotes: { [voiceID: number]: GraphicalNote[]; } = (graphicalStaffEntry as VexFlowStaffEntry).graphicalNotes;
  235. let vfVoices: { [voiceID: number]: Vex.Flow.Voice; } = this.vfVoices;
  236. for (let voiceID in gnotes) {
  237. if (gnotes.hasOwnProperty(voiceID)) {
  238. if (!(voiceID in vfVoices)) {
  239. vfVoices[voiceID] = new Vex.Flow.Voice({
  240. beat_value: 4, //this.parentSourceMeasure.Duration.Denominator,
  241. num_beats: 3, //this.parentSourceMeasure.Duration.Numerator,
  242. resolution: Vex.Flow.RESOLUTION,
  243. }).setMode(Vex.Flow.Voice.Mode.SOFT);
  244. }
  245. let vfnote: StaveNote = VexFlowConverter.StaveNote(gnotes[voiceID]);
  246. (graphicalStaffEntry as VexFlowStaffEntry).vfNotes[voiceID] = vfnote;
  247. vfVoices[voiceID].addTickable(vfnote);
  248. }
  249. }
  250. }
  251. /**
  252. * Creates a line from 'top' to this measure, of type 'lineType'
  253. * @param top
  254. * @param lineType
  255. */
  256. public lineTo(top: VexFlowMeasure, lineType: any): void {
  257. let connector: StaveConnector = new Vex.Flow.StaveConnector(top.getVFStave(), this.stave);
  258. connector.setType(lineType);
  259. this.connectors.push(connector);
  260. }
  261. public getVFStave(): Vex.Flow.Stave {
  262. return this.stave;
  263. }
  264. private increaseBeginInstructionWidth(): void {
  265. let modifiers: StaveModifier[] = this.stave.getModifiers();
  266. let modifier: StaveModifier = modifiers[modifiers.length - 1];
  267. //let padding: number = modifier.getCategory() === "keysignatures" ? modifier.getPadding(2) : 0;
  268. let padding: number = modifier.getPadding(20);
  269. let width: number = modifier.getWidth();
  270. this.beginInstructionsWidth += (padding + width) / this.unit;
  271. }
  272. private increaseEndInstructionWidth(): void {
  273. let modifiers: StaveModifier[] = this.stave.getModifiers();
  274. let modifier: StaveModifier = modifiers[modifiers.length - 1];
  275. let padding: number = 0;
  276. let width: number = modifier.getWidth();
  277. this.endInstructionsWidth += (padding + width) / this.unit;
  278. }
  279. }