VexFlowMeasure.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. import Vex = require("vexflow");
  2. import {GraphicalMeasure} from "../GraphicalMeasure";
  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, VexFlowRepetitionType, VexFlowBarlineType} 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 StaveNote = Vex.Flow.StaveNote;
  17. import * as log from "loglevel";
  18. import {unitInPixels} from "./VexFlowMusicSheetDrawer";
  19. import {Tuplet} from "../../VoiceData/Tuplet";
  20. import {RepetitionInstructionEnum} from "../../VoiceData/Instructions/RepetitionInstruction";
  21. import {SystemLinePosition} from "../SystemLinePosition";
  22. import {StemDirectionType} from "../../VoiceData/VoiceEntry";
  23. import {GraphicalVoiceEntry} from "../GraphicalVoiceEntry";
  24. import {VexFlowVoiceEntry} from "./VexFlowVoiceEntry";
  25. import {Fraction} from "../../../Common/DataObjects/Fraction";
  26. import { Voice } from "../../VoiceData/Voice";
  27. export class VexFlowMeasure extends GraphicalMeasure {
  28. constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
  29. super(staff, sourceMeasure, staffLine);
  30. this.minimumStaffEntriesWidth = -1;
  31. this.resetLayout();
  32. }
  33. // octaveOffset according to active clef
  34. public octaveOffset: number = 3;
  35. // The VexFlow Voices in the measure
  36. public vfVoices: { [voiceID: number]: Vex.Flow.Voice; } = {};
  37. // Call this function (if present) to x-format all the voices in the measure
  38. public formatVoices: (width: number) => void;
  39. // The VexFlow Ties in the measure
  40. public vfTies: Vex.Flow.StaveTie[] = [];
  41. // The repetition instructions given as words or symbols (coda, dal segno..)
  42. public vfRepetitionWords: Vex.Flow.Repetition[] = [];
  43. // The VexFlow Stave (= one measure in a staffline)
  44. private stave: Vex.Flow.Stave;
  45. // VexFlow StaveConnectors (vertical lines)
  46. private connectors: Vex.Flow.StaveConnector[] = [];
  47. // Intermediate object to construct beams
  48. private beams: { [voiceID: number]: [Beam, VexFlowVoiceEntry[]][]; } = {};
  49. // VexFlow Beams
  50. private vfbeams: { [voiceID: number]: Vex.Flow.Beam[]; };
  51. // Intermediate object to construct tuplets
  52. private tuplets: { [voiceID: number]: [Tuplet, VexFlowVoiceEntry[]][]; } = {};
  53. // VexFlow Tuplets
  54. private vftuplets: { [voiceID: number]: Vex.Flow.Tuplet[]; } = {};
  55. // Sets the absolute coordinates of the VFStave on the canvas
  56. public setAbsoluteCoordinates(x: number, y: number): void {
  57. this.stave.setX(x).setY(y);
  58. }
  59. /**
  60. * Reset all the geometric values and parameters of this measure and put it in an initialized state.
  61. * This is needed to evaluate a measure a second time by system builder.
  62. */
  63. public resetLayout(): void {
  64. // Take into account some space for the begin and end lines of the stave
  65. // Will be changed when repetitions will be implemented
  66. //this.beginInstructionsWidth = 20 / UnitInPixels;
  67. //this.endInstructionsWidth = 20 / UnitInPixels;
  68. this.stave = new Vex.Flow.Stave(0, 0, 0, {
  69. space_above_staff_ln: 0,
  70. space_below_staff_ln: 0,
  71. });
  72. this.updateInstructionWidth();
  73. }
  74. public clean(): void {
  75. this.vfTies.length = 0;
  76. this.connectors = [];
  77. // Clean up instructions
  78. this.resetLayout();
  79. }
  80. /**
  81. * returns the x-width (in units) of a given measure line {SystemLinesEnum}.
  82. * @param line
  83. * @returns the x-width in osmd units
  84. */
  85. public getLineWidth(line: SystemLinesEnum): number {
  86. switch (line) {
  87. // return 0 for the normal lines, as the line width will be considered at the updateInstructionWidth() method using the stavemodifiers.
  88. // case SystemLinesEnum.SingleThin:
  89. // return 5.0 / unitInPixels;
  90. // case SystemLinesEnum.DoubleThin:
  91. // return 5.0 / unitInPixels;
  92. // case SystemLinesEnum.ThinBold:
  93. // return 5.0 / unitInPixels;
  94. // but just add a little extra space for repetitions (cosmetics):
  95. case SystemLinesEnum.BoldThinDots:
  96. case SystemLinesEnum.DotsThinBold:
  97. return 10.0 / unitInPixels;
  98. case SystemLinesEnum.DotsBoldBoldDots:
  99. return 10.0 / unitInPixels;
  100. default:
  101. return 0;
  102. }
  103. }
  104. /**
  105. * adds the given clef to the begin of the measure.
  106. * This has to update/increase BeginInstructionsWidth.
  107. * @param clef
  108. */
  109. public addClefAtBegin(clef: ClefInstruction): void {
  110. this.octaveOffset = clef.OctaveOffset;
  111. const vfclef: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clef, "default");
  112. this.stave.addClef(vfclef.type, vfclef.size, vfclef.annotation, Vex.Flow.Modifier.Position.BEGIN);
  113. this.updateInstructionWidth();
  114. }
  115. /**
  116. * adds the given key to the begin of the measure.
  117. * This has to update/increase BeginInstructionsWidth.
  118. * @param currentKey the new valid key.
  119. * @param previousKey the old cancelled key. Needed to show which accidentals are not valid any more.
  120. * @param currentClef the valid clef. Needed to put the accidentals on the right y-positions.
  121. */
  122. public addKeyAtBegin(currentKey: KeyInstruction, previousKey: KeyInstruction, currentClef: ClefInstruction): void {
  123. this.stave.setKeySignature(
  124. VexFlowConverter.keySignature(currentKey),
  125. VexFlowConverter.keySignature(previousKey),
  126. undefined
  127. );
  128. this.updateInstructionWidth();
  129. }
  130. /**
  131. * adds the given rhythm to the begin of the measure.
  132. * This has to update/increase BeginInstructionsWidth.
  133. * @param rhythm
  134. */
  135. public addRhythmAtBegin(rhythm: RhythmInstruction): void {
  136. const timeSig: Vex.Flow.TimeSignature = VexFlowConverter.TimeSignature(rhythm);
  137. this.stave.addModifier(
  138. timeSig,
  139. Vex.Flow.Modifier.Position.BEGIN
  140. );
  141. this.updateInstructionWidth();
  142. }
  143. /**
  144. * adds the given clef to the end of the measure.
  145. * This has to update/increase EndInstructionsWidth.
  146. * @param clef
  147. */
  148. public addClefAtEnd(clef: ClefInstruction): void {
  149. const vfclef: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clef, "small");
  150. this.stave.setEndClef(vfclef.type, vfclef.size, vfclef.annotation);
  151. this.updateInstructionWidth();
  152. }
  153. public addMeasureLine(lineType: SystemLinesEnum, linePosition: SystemLinePosition): void {
  154. switch (linePosition) {
  155. case SystemLinePosition.MeasureBegin:
  156. switch (lineType) {
  157. case SystemLinesEnum.BoldThinDots:
  158. this.stave.setBegBarType(VexFlowBarlineType.REPEAT_BEGIN);
  159. break;
  160. default:
  161. break;
  162. }
  163. break;
  164. case SystemLinePosition.MeasureEnd:
  165. switch (lineType) {
  166. case SystemLinesEnum.DotsBoldBoldDots:
  167. this.stave.setEndBarType(VexFlowBarlineType.REPEAT_BOTH);
  168. break;
  169. case SystemLinesEnum.DotsThinBold:
  170. this.stave.setEndBarType(VexFlowBarlineType.REPEAT_END);
  171. break;
  172. case SystemLinesEnum.DoubleThin:
  173. this.stave.setEndBarType(VexFlowBarlineType.DOUBLE);
  174. break;
  175. case SystemLinesEnum.ThinBold:
  176. this.stave.setEndBarType(VexFlowBarlineType.END);
  177. break;
  178. default:
  179. break;
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. /**
  187. * Adds a measure number to the top left corner of the measure
  188. * This method is not used currently in favor of the calculateMeasureNumberPlacement
  189. * method in the MusicSheetCalculator.ts
  190. */
  191. public addMeasureNumber(): void {
  192. const text: string = this.MeasureNumber.toString();
  193. const position: number = Vex.Flow.StaveModifier.Position.ABOVE;
  194. const options: any = {
  195. justification: 1,
  196. shift_x: 0,
  197. shift_y: 0,
  198. };
  199. this.stave.setText(text, position, options);
  200. }
  201. public addWordRepetition(repetitionInstruction: RepetitionInstructionEnum): void {
  202. let instruction: VexFlowRepetitionType = undefined;
  203. let position: any = Vex.Flow.Modifier.Position.END;
  204. switch (repetitionInstruction) {
  205. case RepetitionInstructionEnum.Segno:
  206. // create Segno Symbol:
  207. instruction = VexFlowRepetitionType.SEGNO_LEFT;
  208. position = Vex.Flow.Modifier.Position.BEGIN;
  209. break;
  210. case RepetitionInstructionEnum.Coda:
  211. // create Coda Symbol:
  212. instruction = VexFlowRepetitionType.CODA_LEFT;
  213. position = Vex.Flow.Modifier.Position.BEGIN;
  214. break;
  215. case RepetitionInstructionEnum.DaCapo:
  216. instruction = VexFlowRepetitionType.DC;
  217. break;
  218. case RepetitionInstructionEnum.DalSegno:
  219. instruction = VexFlowRepetitionType.DS;
  220. break;
  221. case RepetitionInstructionEnum.Fine:
  222. instruction = VexFlowRepetitionType.FINE;
  223. break;
  224. case RepetitionInstructionEnum.ToCoda:
  225. //instruction = "To Coda";
  226. break;
  227. case RepetitionInstructionEnum.DaCapoAlFine:
  228. instruction = VexFlowRepetitionType.DC_AL_FINE;
  229. break;
  230. case RepetitionInstructionEnum.DaCapoAlCoda:
  231. instruction = VexFlowRepetitionType.DC_AL_CODA;
  232. break;
  233. case RepetitionInstructionEnum.DalSegnoAlFine:
  234. instruction = VexFlowRepetitionType.DS_AL_FINE;
  235. break;
  236. case RepetitionInstructionEnum.DalSegnoAlCoda:
  237. instruction = VexFlowRepetitionType.DS_AL_CODA;
  238. break;
  239. default:
  240. break;
  241. }
  242. if (instruction !== undefined) {
  243. this.stave.addModifier(new Vex.Flow.Repetition(instruction, 0, 0), position);
  244. }
  245. }
  246. /**
  247. * Sets the overall x-width of the measure.
  248. * @param width
  249. */
  250. public setWidth(width: number): void {
  251. super.setWidth(width);
  252. // Set the width of the Vex.Flow.Stave
  253. this.stave.setWidth(width * unitInPixels);
  254. // Force the width of the Begin Instructions
  255. //this.stave.setNoteStartX(this.beginInstructionsWidth * UnitInPixels);
  256. }
  257. /**
  258. * This method is called after the StaffEntriesScaleFactor has been set.
  259. * Here the final x-positions of the staff entries have to be set.
  260. * (multiply the minimal positions with the scaling factor, considering the BeginInstructionsWidth)
  261. */
  262. public layoutSymbols(): void {
  263. // vexflow does the x-layout
  264. }
  265. /**
  266. * Draw this measure on a VexFlow CanvasContext
  267. * @param ctx
  268. */
  269. public draw(ctx: Vex.Flow.RenderContext): void {
  270. // Draw stave lines
  271. this.stave.setContext(ctx).draw();
  272. // Draw all voices
  273. for (const voiceID in this.vfVoices) {
  274. if (this.vfVoices.hasOwnProperty(voiceID)) {
  275. this.vfVoices[voiceID].draw(ctx, this.stave);
  276. }
  277. }
  278. // Draw beams
  279. for (const voiceID in this.vfbeams) {
  280. if (this.vfbeams.hasOwnProperty(voiceID)) {
  281. for (const beam of this.vfbeams[voiceID]) {
  282. beam.setContext(ctx).draw();
  283. }
  284. }
  285. }
  286. // Draw tuplets
  287. for (const voiceID in this.vftuplets) {
  288. if (this.vftuplets.hasOwnProperty(voiceID)) {
  289. for (const tuplet of this.vftuplets[voiceID]) {
  290. tuplet.setContext(ctx).draw();
  291. }
  292. }
  293. }
  294. // Draw ties
  295. for (const tie of this.vfTies) {
  296. tie.setContext(ctx).draw();
  297. }
  298. // Draw vertical lines
  299. for (const connector of this.connectors) {
  300. connector.setContext(ctx).draw();
  301. }
  302. }
  303. public format(): void {
  304. // If this is the first stave in the vertical measure, call the format
  305. // method to set the width of all the voices
  306. if (this.formatVoices) {
  307. // The width of the voices does not include the instructions (StaveModifiers)
  308. this.formatVoices((this.PositionAndShape.BorderRight - this.beginInstructionsWidth - this.endInstructionsWidth) * unitInPixels);
  309. }
  310. // Force the width of the Begin Instructions
  311. this.stave.setNoteStartX(this.stave.getX() + unitInPixels * this.beginInstructionsWidth);
  312. }
  313. /**
  314. * Returns all the voices that are present in this measure
  315. */
  316. public getVoicesWithinMeasure(): Voice[] {
  317. const voices: Voice[] = [];
  318. for (const gse of this.staffEntries) {
  319. for (const gve of gse.graphicalVoiceEntries) {
  320. if (voices.indexOf(gve.parentVoiceEntry.ParentVoice) === -1) {
  321. voices.push(gve.parentVoiceEntry.ParentVoice);
  322. }
  323. }
  324. }
  325. return voices;
  326. }
  327. /**
  328. * Returns all the graphicalVoiceEntries of a given Voice.
  329. * @param voice the voice for which the graphicalVoiceEntries shall be returned.
  330. */
  331. public getGraphicalVoiceEntriesPerVoice(voice: Voice): GraphicalVoiceEntry[] {
  332. const voiceEntries: GraphicalVoiceEntry[] = [];
  333. for (const gse of this.staffEntries) {
  334. for (const gve of gse.graphicalVoiceEntries) {
  335. if (gve.parentVoiceEntry.ParentVoice === voice) {
  336. voiceEntries.push(gve);
  337. }
  338. }
  339. }
  340. return voiceEntries;
  341. }
  342. /**
  343. * Finds the gaps between the existing notes within a measure.
  344. * Problem here is, that the graphicalVoiceEntry does not exist yet and
  345. * that Tied notes are not present in the normal voiceEntries.
  346. * To handle this, calculation with absolute timestamps is needed.
  347. * And the graphical notes have to be analysed directly (and not the voiceEntries, as it actually should be -> needs refactoring)
  348. * @param voice the voice for which the ghost notes shall be searched.
  349. */
  350. private getRestFilledVexFlowStaveNotesPerVoice(voice: Voice): GraphicalVoiceEntry[] {
  351. let latestVoiceTimestamp: Fraction = undefined;
  352. const gvEntries: GraphicalVoiceEntry[] = this.getGraphicalVoiceEntriesPerVoice(voice);
  353. for (let idx: number = 0, len: number = gvEntries.length; idx < len; ++idx) {
  354. const gve: GraphicalVoiceEntry = gvEntries[idx];
  355. const gNotesStartTimestamp: Fraction = gve.notes[0].sourceNote.getAbsoluteTimestamp();
  356. // find the voiceEntry end timestamp:
  357. let gNotesEndTimestamp: Fraction = new Fraction();
  358. for (const graphicalNote of gve.notes) {
  359. const noteEnd: Fraction = Fraction.plus(graphicalNote.sourceNote.getAbsoluteTimestamp(), graphicalNote.sourceNote.Length);
  360. if (gNotesEndTimestamp < noteEnd) {
  361. gNotesEndTimestamp = noteEnd;
  362. }
  363. }
  364. // check if this voice has just been found the first time:
  365. if (latestVoiceTimestamp === undefined) {
  366. // if this voice is new, check for a gap from measure start to the start of the current voice entry:
  367. const gapFromMeasureStart: Fraction = Fraction.minus(gNotesStartTimestamp, this.parentSourceMeasure.AbsoluteTimestamp);
  368. if (gapFromMeasureStart.RealValue > 0) {
  369. log.debug("Ghost Found at start");
  370. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(gapFromMeasureStart);
  371. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  372. ghostGve.vfStaveNote = vfghost;
  373. gvEntries.splice(0, 0, ghostGve);
  374. idx++;
  375. }
  376. } else {
  377. // get the length of the empty space between notes:
  378. const inBetweenLength: Fraction = Fraction.minus(gNotesStartTimestamp, latestVoiceTimestamp);
  379. if (inBetweenLength.RealValue > 0) {
  380. log.debug("Ghost Found in between");
  381. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(inBetweenLength);
  382. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  383. ghostGve.vfStaveNote = vfghost;
  384. // add element before current element:
  385. gvEntries.splice(idx, 0, ghostGve);
  386. // and increase index, as we added an element:
  387. idx++;
  388. }
  389. }
  390. // finally set the latest timestamp of this voice to the end timestamp of the longest note in the current voiceEntry:
  391. latestVoiceTimestamp = gNotesEndTimestamp;
  392. }
  393. const measureEndTimestamp: Fraction = Fraction.plus(this.parentSourceMeasure.AbsoluteTimestamp, this.parentSourceMeasure.Duration);
  394. const restLength: Fraction = Fraction.minus(measureEndTimestamp, latestVoiceTimestamp);
  395. if (restLength.RealValue > 0) {
  396. // fill the gap with a rest ghost note
  397. // starting from lastFraction
  398. // with length restLength:
  399. log.debug("Ghost Found at end");
  400. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(restLength);
  401. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  402. ghostGve.vfStaveNote = vfghost;
  403. gvEntries.push(ghostGve);
  404. }
  405. return gvEntries;
  406. }
  407. /**
  408. * Add a note to a beam
  409. * @param graphicalNote
  410. * @param beam
  411. */
  412. public handleBeam(graphicalNote: GraphicalNote, beam: Beam): void {
  413. const voiceID: number = graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice.VoiceId;
  414. let beams: [Beam, VexFlowVoiceEntry[]][] = this.beams[voiceID];
  415. if (beams === undefined) {
  416. beams = this.beams[voiceID] = [];
  417. }
  418. let data: [Beam, VexFlowVoiceEntry[]];
  419. for (const mybeam of beams) {
  420. if (mybeam[0] === beam) {
  421. data = mybeam;
  422. }
  423. }
  424. if (data === undefined) {
  425. data = [beam, []];
  426. beams.push(data);
  427. }
  428. const parent: VexFlowVoiceEntry = graphicalNote.parentVoiceEntry as VexFlowVoiceEntry;
  429. if (data[1].indexOf(parent) < 0) {
  430. data[1].push(parent);
  431. }
  432. }
  433. public handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet): void {
  434. const voiceID: number = graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice.VoiceId;
  435. tuplet = graphicalNote.sourceNote.NoteTuplet;
  436. let tuplets: [Tuplet, VexFlowVoiceEntry[]][] = this.tuplets[voiceID];
  437. if (tuplets === undefined) {
  438. tuplets = this.tuplets[voiceID] = [];
  439. }
  440. let currentTupletBuilder: [Tuplet, VexFlowVoiceEntry[]];
  441. for (const t of tuplets) {
  442. if (t[0] === tuplet) {
  443. currentTupletBuilder = t;
  444. }
  445. }
  446. if (currentTupletBuilder === undefined) {
  447. currentTupletBuilder = [tuplet, []];
  448. tuplets.push(currentTupletBuilder);
  449. }
  450. const parent: VexFlowVoiceEntry = graphicalNote.parentVoiceEntry as VexFlowVoiceEntry;
  451. if (currentTupletBuilder[1].indexOf(parent) < 0) {
  452. currentTupletBuilder[1].push(parent);
  453. }
  454. }
  455. /**
  456. * Complete the creation of VexFlow Beams in this measure
  457. */
  458. public finalizeBeams(): void {
  459. // The following line resets the created Vex.Flow Beams and
  460. // created them brand new. Is this needed? And more importantly,
  461. // should the old beams be removed manually by the notes?
  462. this.vfbeams = {};
  463. for (const voiceID in this.beams) {
  464. if (this.beams.hasOwnProperty(voiceID)) {
  465. let vfbeams: Vex.Flow.Beam[] = this.vfbeams[voiceID];
  466. if (vfbeams === undefined) {
  467. vfbeams = this.vfbeams[voiceID] = [];
  468. }
  469. for (const beam of this.beams[voiceID]) {
  470. const notes: Vex.Flow.StaveNote[] = [];
  471. const psBeam: Beam = beam[0];
  472. const voiceEntries: VexFlowVoiceEntry[] = beam[1];
  473. let autoStemBeam: boolean = true;
  474. for (const gve of voiceEntries) {
  475. if (gve.parentVoiceEntry.ParentVoice === psBeam.Notes[0].ParentVoiceEntry.ParentVoice) {
  476. autoStemBeam = gve.parentVoiceEntry.StemDirection === StemDirectionType.Undefined;
  477. }
  478. }
  479. for (const entry of voiceEntries) {
  480. const note: Vex.Flow.StaveNote = ((<VexFlowVoiceEntry>entry).vfStaveNote as StaveNote);
  481. if (note !== undefined) {
  482. notes.push(note);
  483. }
  484. }
  485. if (notes.length > 1) {
  486. const vfBeam: Vex.Flow.Beam = new Vex.Flow.Beam(notes, autoStemBeam);
  487. vfbeams.push(vfBeam);
  488. // just a test for coloring the notes:
  489. // for (let note of notes) {
  490. // (<Vex.Flow.StaveNote> note).setStyle({fillStyle: "green", strokeStyle: "green"});
  491. // }
  492. } else {
  493. log.debug("Warning! Beam with no notes!");
  494. }
  495. }
  496. }
  497. }
  498. }
  499. /**
  500. * Complete the creation of VexFlow Tuplets in this measure
  501. */
  502. public finalizeTuplets(): void {
  503. // The following line resets the created Vex.Flow Tuplets and
  504. // created them brand new. Is this needed? And more importantly,
  505. // should the old tuplets be removed manually from the notes?
  506. this.vftuplets = {};
  507. for (const voiceID in this.tuplets) {
  508. if (this.tuplets.hasOwnProperty(voiceID)) {
  509. let vftuplets: Vex.Flow.Tuplet[] = this.vftuplets[voiceID];
  510. if (vftuplets === undefined) {
  511. vftuplets = this.vftuplets[voiceID] = [];
  512. }
  513. for (const tupletBuilder of this.tuplets[voiceID]) {
  514. const tupletStaveNotes: Vex.Flow.StaveNote[] = [];
  515. const tupletVoiceEntries: VexFlowVoiceEntry[] = tupletBuilder[1];
  516. for (const tupletVoiceEntry of tupletVoiceEntries) {
  517. tupletStaveNotes.push(((tupletVoiceEntry).vfStaveNote as StaveNote));
  518. }
  519. if (tupletStaveNotes.length > 1) {
  520. const notesOccupied: number = 2;
  521. vftuplets.push(new Vex.Flow.Tuplet( tupletStaveNotes,
  522. {
  523. notes_occupied: notesOccupied,
  524. num_notes: tupletStaveNotes.length //, location: -1, ratioed: true
  525. }));
  526. } else {
  527. log.debug("Warning! Tuplet with no notes! Trying to ignore, but this is a serious problem.");
  528. }
  529. }
  530. }
  531. }
  532. }
  533. public layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  534. return;
  535. }
  536. public graphicalMeasureCreatedCalculations(): void {
  537. for (const graphicalStaffEntry of this.staffEntries as VexFlowStaffEntry[]) {
  538. // create vex flow Stave Notes:
  539. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  540. (gve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.StaveNote(gve);
  541. }
  542. }
  543. this.finalizeBeams();
  544. this.finalizeTuplets();
  545. const voices: Voice[] = this.getVoicesWithinMeasure();
  546. for (const voice of voices) {
  547. // add a vexFlow voice for this voice:
  548. this.vfVoices[voice.VoiceId] = new Vex.Flow.Voice({
  549. beat_value: this.parentSourceMeasure.Duration.Denominator,
  550. num_beats: this.parentSourceMeasure.Duration.Numerator,
  551. resolution: Vex.Flow.RESOLUTION,
  552. }).setMode(Vex.Flow.Voice.Mode.SOFT);
  553. const restFilledEntries: GraphicalVoiceEntry[] = this.getRestFilledVexFlowStaveNotesPerVoice(voice);
  554. // create vex flow voices and add tickables to it:
  555. for (const voiceEntry of restFilledEntries) {
  556. this.vfVoices[voice.VoiceId].addTickable((voiceEntry as VexFlowVoiceEntry).vfStaveNote);
  557. }
  558. }
  559. this.createArticulations();
  560. this.createOrnaments();
  561. }
  562. /**
  563. * Create the articulations for all notes of the current staff entry
  564. */
  565. private createArticulations(): void {
  566. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  567. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  568. // create vex flow articulation:
  569. const graphicalVoiceEntries: GraphicalVoiceEntry[] = graphicalStaffEntry.graphicalVoiceEntries;
  570. for (const gve of graphicalVoiceEntries) {
  571. // TODO this is a lot of casting, and a VexFlowVoiceEntry is not supposed to have a StaveNote,
  572. // only a StemmableNote, which is a superclass of StaveNote, meaning StaveNote has more functionality.
  573. // so, either VexFlowVoiceEntry should have a StaveNote member again instead of StemmableNote,
  574. // or we need to get the StaveNote from somewhere else.
  575. // same issue in createOrnaments(). [sschmid]
  576. const vfStaveNote: StaveNote = ((gve as VexFlowVoiceEntry).vfStaveNote as StaveNote);
  577. VexFlowConverter.generateArticulations(vfStaveNote, gve.notes[0].sourceNote.ParentVoiceEntry.Articulations);
  578. }
  579. }
  580. }
  581. /**
  582. * Create the ornaments for all notes of the current staff entry
  583. */
  584. private createOrnaments(): void {
  585. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  586. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  587. // create vex flow Notes:
  588. // TODO VexFlowConverter.StaveNote may have something to do with this.
  589. // Hint: GraphicalNote[][] is now graphicalVoiceEntries : GraphicalVoiceEntry[]
  590. // also, VewFlowVoiceEntry now uses a Vex.Flow.StemmableNote instead of a StaveNote, which generateOrnaments() demands.
  591. const gvoices: { [voiceID: number]: GraphicalVoiceEntry; } = graphicalStaffEntry.graphicalVoiceEntries;
  592. for (const voiceID in gvoices) {
  593. if (gvoices.hasOwnProperty(voiceID)) {
  594. const vfStaveNote: StaveNote = ((gvoices[voiceID] as VexFlowVoiceEntry).vfStaveNote as StaveNote);
  595. if (gvoices[voiceID].notes[0].sourceNote.ParentVoiceEntry.OrnamentContainer !== undefined) {
  596. VexFlowConverter.generateOrnaments(vfStaveNote, gvoices[voiceID].notes[0].sourceNote.ParentVoiceEntry.OrnamentContainer);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. /**
  603. * Creates a line from 'top' to this measure, of type 'lineType'
  604. * @param top
  605. * @param lineType
  606. */
  607. public lineTo(top: VexFlowMeasure, lineType: any): void {
  608. const connector: StaveConnector = new Vex.Flow.StaveConnector(top.getVFStave(), this.stave);
  609. connector.setType(lineType);
  610. this.connectors.push(connector);
  611. }
  612. /**
  613. * Return the VexFlow Stave corresponding to this graphicalMeasure
  614. * @returns {Vex.Flow.Stave}
  615. */
  616. public getVFStave(): Vex.Flow.Stave {
  617. return this.stave;
  618. }
  619. /**
  620. * After re-running the formatting on the VexFlow Stave, update the
  621. * space needed by Instructions (in VexFlow: StaveModifiers)
  622. */
  623. private updateInstructionWidth(): void {
  624. let beginInstructionsWidth: number = 0;
  625. let endInstructionsWidth: number = 0;
  626. const modifiers: Vex.Flow.StaveModifier[] = this.stave.getModifiers();
  627. for (const mod of modifiers) {
  628. if (mod.getPosition() === Vex.Flow.StaveModifier.Position.BEGIN) {
  629. beginInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  630. } else if (mod.getPosition() === Vex.Flow.StaveModifier.Position.END) {
  631. endInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  632. }
  633. }
  634. this.beginInstructionsWidth = beginInstructionsWidth / unitInPixels;
  635. this.endInstructionsWidth = endInstructionsWidth / unitInPixels;
  636. }
  637. }