VexFlowMeasure.ts 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 StemmableNote = Vex.Flow.StemmableNote;
  18. import NoteSubGroup = Vex.Flow.NoteSubGroup;
  19. import * as log from "loglevel";
  20. import {unitInPixels} from "./VexFlowMusicSheetDrawer";
  21. import {Tuplet} from "../../VoiceData/Tuplet";
  22. import {RepetitionInstructionEnum, RepetitionInstruction, AlignmentType} from "../../VoiceData/Instructions/RepetitionInstruction";
  23. import {SystemLinePosition} from "../SystemLinePosition";
  24. import {StemDirectionType} from "../../VoiceData/VoiceEntry";
  25. import {GraphicalVoiceEntry} from "../GraphicalVoiceEntry";
  26. import {VexFlowVoiceEntry} from "./VexFlowVoiceEntry";
  27. import {Fraction} from "../../../Common/DataObjects/Fraction";
  28. import {Voice} from "../../VoiceData/Voice";
  29. import {LinkedVoice} from "../../VoiceData/LinkedVoice";
  30. import {EngravingRules} from "../EngravingRules";
  31. import {OrnamentContainer} from "../../VoiceData/OrnamentContainer";
  32. import {TechnicalInstruction} from "../../VoiceData/Instructions/TechnicalInstruction";
  33. import {PlacementEnum} from "../../VoiceData/Expressions/AbstractExpression";
  34. import {ArpeggioType} from "../../VoiceData/Arpeggio";
  35. import {VexFlowGraphicalNote} from "./VexFlowGraphicalNote";
  36. import {AutoBeamOptions} from "../../../OpenSheetMusicDisplay/OSMDOptions";
  37. export class VexFlowMeasure extends GraphicalMeasure {
  38. constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
  39. super(staff, sourceMeasure, staffLine);
  40. this.minimumStaffEntriesWidth = -1;
  41. this.resetLayout();
  42. }
  43. /** octaveOffset according to active clef */
  44. public octaveOffset: number = 3;
  45. /** The VexFlow Voices in the measure */
  46. public vfVoices: { [voiceID: number]: Vex.Flow.Voice; } = {};
  47. /** Call this function (if present) to x-format all the voices in the measure */
  48. public formatVoices: (width: number) => void;
  49. /** The VexFlow Ties in the measure */
  50. public vfTies: Vex.Flow.StaveTie[] = [];
  51. /** The repetition instructions given as words or symbols (coda, dal segno..) */
  52. public vfRepetitionWords: Vex.Flow.Repetition[] = [];
  53. /** The VexFlow Stave (= one measure in a staffline) */
  54. private stave: Vex.Flow.Stave;
  55. /** VexFlow StaveConnectors (vertical lines) */
  56. private connectors: Vex.Flow.StaveConnector[] = [];
  57. /** Intermediate object to construct beams */
  58. private beams: { [voiceID: number]: [Beam, VexFlowVoiceEntry[]][]; } = {};
  59. /** Beams created by (optional) autoBeam function. */
  60. private autoVfBeams: Vex.Flow.Beam[];
  61. /** Beams of tuplet notes created by (optional) autoBeam function. */
  62. private autoTupletVfBeams: Vex.Flow.Beam[];
  63. /** VexFlow Beams */
  64. private vfbeams: { [voiceID: number]: Vex.Flow.Beam[]; };
  65. /** Intermediate object to construct tuplets */
  66. private tuplets: { [voiceID: number]: [Tuplet, VexFlowVoiceEntry[]][]; } = {};
  67. /** VexFlow Tuplets */
  68. private vftuplets: { [voiceID: number]: Vex.Flow.Tuplet[]; } = {};
  69. // Sets the absolute coordinates of the VFStave on the canvas
  70. public setAbsoluteCoordinates(x: number, y: number): void {
  71. this.stave.setX(x).setY(y);
  72. }
  73. /**
  74. * Reset all the geometric values and parameters of this measure and put it in an initialized state.
  75. * This is needed to evaluate a measure a second time by system builder.
  76. */
  77. public resetLayout(): void {
  78. // Take into account some space for the begin and end lines of the stave
  79. // Will be changed when repetitions will be implemented
  80. //this.beginInstructionsWidth = 20 / UnitInPixels;
  81. //this.endInstructionsWidth = 20 / UnitInPixels;
  82. this.stave = new Vex.Flow.Stave(0, 0, 0, {
  83. space_above_staff_ln: 0,
  84. space_below_staff_ln: 0,
  85. });
  86. this.updateInstructionWidth();
  87. }
  88. public clean(): void {
  89. this.vfTies.length = 0;
  90. this.connectors = [];
  91. // Clean up instructions
  92. this.resetLayout();
  93. }
  94. /**
  95. * returns the x-width (in units) of a given measure line {SystemLinesEnum}.
  96. * @param line
  97. * @returns the x-width in osmd units
  98. */
  99. public getLineWidth(line: SystemLinesEnum): number {
  100. switch (line) {
  101. // return 0 for the normal lines, as the line width will be considered at the updateInstructionWidth() method using the stavemodifiers.
  102. // case SystemLinesEnum.SingleThin:
  103. // return 5.0 / unitInPixels;
  104. // case SystemLinesEnum.DoubleThin:
  105. // return 5.0 / unitInPixels;
  106. // case SystemLinesEnum.ThinBold:
  107. // return 5.0 / unitInPixels;
  108. // but just add a little extra space for repetitions (cosmetics):
  109. case SystemLinesEnum.BoldThinDots:
  110. case SystemLinesEnum.DotsThinBold:
  111. return 10.0 / unitInPixels;
  112. case SystemLinesEnum.DotsBoldBoldDots:
  113. return 10.0 / unitInPixels;
  114. default:
  115. return 0;
  116. }
  117. }
  118. /**
  119. * adds the given clef to the begin of the measure.
  120. * This has to update/increase BeginInstructionsWidth.
  121. * @param clef
  122. */
  123. public addClefAtBegin(clef: ClefInstruction): void {
  124. this.octaveOffset = clef.OctaveOffset;
  125. const vfclef: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clef, "default");
  126. this.stave.addClef(vfclef.type, vfclef.size, vfclef.annotation, Vex.Flow.Modifier.Position.BEGIN);
  127. this.updateInstructionWidth();
  128. }
  129. /**
  130. * adds the given key to the begin of the measure.
  131. * This has to update/increase BeginInstructionsWidth.
  132. * @param currentKey the new valid key.
  133. * @param previousKey the old cancelled key. Needed to show which accidentals are not valid any more.
  134. * @param currentClef the valid clef. Needed to put the accidentals on the right y-positions.
  135. */
  136. public addKeyAtBegin(currentKey: KeyInstruction, previousKey: KeyInstruction, currentClef: ClefInstruction): void {
  137. this.stave.setKeySignature(
  138. VexFlowConverter.keySignature(currentKey),
  139. VexFlowConverter.keySignature(previousKey),
  140. undefined
  141. );
  142. this.updateInstructionWidth();
  143. }
  144. /**
  145. * adds the given rhythm to the begin of the measure.
  146. * This has to update/increase BeginInstructionsWidth.
  147. * @param rhythm
  148. */
  149. public addRhythmAtBegin(rhythm: RhythmInstruction): void {
  150. const timeSig: Vex.Flow.TimeSignature = VexFlowConverter.TimeSignature(rhythm);
  151. this.stave.addModifier(
  152. timeSig,
  153. Vex.Flow.Modifier.Position.BEGIN
  154. );
  155. this.updateInstructionWidth();
  156. }
  157. /**
  158. * adds the given clef to the end of the measure.
  159. * This has to update/increase EndInstructionsWidth.
  160. * @param clef
  161. */
  162. public addClefAtEnd(clef: ClefInstruction): void {
  163. const vfclef: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clef, "small");
  164. this.stave.setEndClef(vfclef.type, vfclef.size, vfclef.annotation);
  165. this.updateInstructionWidth();
  166. }
  167. public addMeasureLine(lineType: SystemLinesEnum, linePosition: SystemLinePosition): void {
  168. switch (linePosition) {
  169. case SystemLinePosition.MeasureBegin:
  170. switch (lineType) {
  171. case SystemLinesEnum.BoldThinDots:
  172. this.stave.setBegBarType(VexFlowBarlineType.REPEAT_BEGIN);
  173. break;
  174. default:
  175. break;
  176. }
  177. break;
  178. case SystemLinePosition.MeasureEnd:
  179. switch (lineType) {
  180. case SystemLinesEnum.DotsBoldBoldDots:
  181. this.stave.setEndBarType(VexFlowBarlineType.REPEAT_BOTH);
  182. break;
  183. case SystemLinesEnum.DotsThinBold:
  184. this.stave.setEndBarType(VexFlowBarlineType.REPEAT_END);
  185. break;
  186. case SystemLinesEnum.DoubleThin:
  187. this.stave.setEndBarType(VexFlowBarlineType.DOUBLE);
  188. break;
  189. case SystemLinesEnum.ThinBold:
  190. this.stave.setEndBarType(VexFlowBarlineType.END);
  191. break;
  192. default:
  193. break;
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. /**
  201. * Adds a measure number to the top left corner of the measure
  202. * This method is not used currently in favor of the calculateMeasureNumberPlacement
  203. * method in the MusicSheetCalculator.ts
  204. */
  205. public addMeasureNumber(): void {
  206. const text: string = this.MeasureNumber.toString();
  207. const position: number = StavePositionEnum.ABOVE; //Vex.Flow.StaveModifier.Position.ABOVE;
  208. const options: any = {
  209. justification: 1,
  210. shift_x: 0,
  211. shift_y: 0,
  212. };
  213. this.stave.setText(text, position, options);
  214. }
  215. public addWordRepetition(repetitionInstruction: RepetitionInstruction): void {
  216. let instruction: VexFlowRepetitionType = undefined;
  217. let position: any = Vex.Flow.Modifier.Position.END;
  218. switch (repetitionInstruction.type) {
  219. case RepetitionInstructionEnum.Segno:
  220. // create Segno Symbol:
  221. instruction = VexFlowRepetitionType.SEGNO_LEFT;
  222. position = Vex.Flow.Modifier.Position.BEGIN;
  223. break;
  224. case RepetitionInstructionEnum.Coda:
  225. // create Coda Symbol:
  226. instruction = VexFlowRepetitionType.CODA_LEFT;
  227. position = Vex.Flow.Modifier.Position.BEGIN;
  228. break;
  229. case RepetitionInstructionEnum.DaCapo:
  230. instruction = VexFlowRepetitionType.DC;
  231. break;
  232. case RepetitionInstructionEnum.DalSegno:
  233. instruction = VexFlowRepetitionType.DS;
  234. break;
  235. case RepetitionInstructionEnum.Fine:
  236. instruction = VexFlowRepetitionType.FINE;
  237. break;
  238. case RepetitionInstructionEnum.ToCoda:
  239. //instruction = "To Coda";
  240. break;
  241. case RepetitionInstructionEnum.DaCapoAlFine:
  242. instruction = VexFlowRepetitionType.DC_AL_FINE;
  243. break;
  244. case RepetitionInstructionEnum.DaCapoAlCoda:
  245. instruction = VexFlowRepetitionType.DC_AL_CODA;
  246. break;
  247. case RepetitionInstructionEnum.DalSegnoAlFine:
  248. instruction = VexFlowRepetitionType.DS_AL_FINE;
  249. break;
  250. case RepetitionInstructionEnum.DalSegnoAlCoda:
  251. instruction = VexFlowRepetitionType.DS_AL_CODA;
  252. break;
  253. default:
  254. break;
  255. }
  256. if (instruction !== undefined) {
  257. this.stave.addModifier(new Vex.Flow.Repetition(instruction, 0, 0), position);
  258. return;
  259. }
  260. this.addVolta(repetitionInstruction);
  261. }
  262. private addVolta(repetitionInstruction: RepetitionInstruction): void {
  263. let voltaType: number = Vex.Flow.Volta.type.BEGIN;
  264. if (repetitionInstruction.type === RepetitionInstructionEnum.Ending) {
  265. switch (repetitionInstruction.alignment) {
  266. case AlignmentType.Begin:
  267. if (this.parentSourceMeasure.endsRepetitionEnding()) {
  268. voltaType = Vex.Flow.Volta.type.BEGIN_END;
  269. } else {
  270. voltaType = Vex.Flow.Volta.type.BEGIN;
  271. }
  272. break;
  273. case AlignmentType.End:
  274. if (this.parentSourceMeasure.beginsRepetitionEnding()) {
  275. //voltaType = Vex.Flow.Volta.type.BEGIN_END;
  276. // don't add BEGIN_END volta a second time:
  277. return;
  278. } else {
  279. voltaType = Vex.Flow.Volta.type.END;
  280. }
  281. break;
  282. default:
  283. break;
  284. }
  285. this.stave.setVoltaType(voltaType, repetitionInstruction.endingIndices[0], 0);
  286. }
  287. }
  288. /**
  289. * Sets the overall x-width of the measure.
  290. * @param width
  291. */
  292. public setWidth(width: number): void {
  293. super.setWidth(width);
  294. // Set the width of the Vex.Flow.Stave
  295. this.stave.setWidth(width * unitInPixels);
  296. // Force the width of the Begin Instructions
  297. //this.stave.setNoteStartX(this.beginInstructionsWidth * UnitInPixels);
  298. }
  299. /**
  300. * This method is called after the StaffEntriesScaleFactor has been set.
  301. * Here the final x-positions of the staff entries have to be set.
  302. * (multiply the minimal positions with the scaling factor, considering the BeginInstructionsWidth)
  303. */
  304. public layoutSymbols(): void {
  305. // vexflow does the x-layout
  306. }
  307. /**
  308. * Draw this measure on a VexFlow CanvasContext
  309. * @param ctx
  310. */
  311. public draw(ctx: Vex.Flow.RenderContext): void {
  312. // Draw stave lines
  313. this.stave.setContext(ctx).draw();
  314. // Draw all voices
  315. for (const voiceID in this.vfVoices) {
  316. if (this.vfVoices.hasOwnProperty(voiceID)) {
  317. ctx.save();
  318. this.vfVoices[voiceID].draw(ctx, this.stave);
  319. ctx.restore();
  320. // this.vfVoices[voiceID].tickables.forEach(t => t.getBoundingBox().draw(ctx));
  321. // this.vfVoices[voiceID].tickables.forEach(t => t.getBoundingBox().draw(ctx));
  322. }
  323. }
  324. // Draw beams
  325. for (const voiceID in this.vfbeams) {
  326. if (this.vfbeams.hasOwnProperty(voiceID)) {
  327. for (const beam of this.vfbeams[voiceID]) {
  328. beam.setContext(ctx).draw();
  329. }
  330. }
  331. }
  332. // Draw auto-generated beams from Beam.generateBeams()
  333. if (this.autoVfBeams) {
  334. for (const beam of this.autoVfBeams) {
  335. beam.setContext(ctx).draw();
  336. }
  337. }
  338. if (this.autoTupletVfBeams) {
  339. for (const beam of this.autoTupletVfBeams) {
  340. beam.setContext(ctx).draw();
  341. }
  342. }
  343. // Draw tuplets
  344. for (const voiceID in this.vftuplets) {
  345. if (this.vftuplets.hasOwnProperty(voiceID)) {
  346. for (const tuplet of this.vftuplets[voiceID]) {
  347. tuplet.setContext(ctx).draw();
  348. }
  349. }
  350. }
  351. // Draw ties
  352. for (const tie of this.vfTies) {
  353. tie.setContext(ctx).draw();
  354. }
  355. // Draw vertical lines
  356. for (const connector of this.connectors) {
  357. connector.setContext(ctx).draw();
  358. }
  359. }
  360. // this currently formats multiple measures, see VexFlowMusicSheetCalculator.formatMeasures()
  361. public format(): void {
  362. // If this is the first stave in the vertical measure, call the format
  363. // method to set the width of all the voices
  364. if (this.formatVoices) {
  365. // set the width of the voices to the current measure width:
  366. // (The width of the voices does not include the instructions (StaveModifiers))
  367. this.formatVoices((this.PositionAndShape.Size.width - this.beginInstructionsWidth - this.endInstructionsWidth) * unitInPixels);
  368. }
  369. }
  370. /**
  371. * Returns all the voices that are present in this measure
  372. */
  373. public getVoicesWithinMeasure(): Voice[] {
  374. const voices: Voice[] = [];
  375. for (const gse of this.staffEntries) {
  376. for (const gve of gse.graphicalVoiceEntries) {
  377. if (voices.indexOf(gve.parentVoiceEntry.ParentVoice) === -1) {
  378. voices.push(gve.parentVoiceEntry.ParentVoice);
  379. }
  380. }
  381. }
  382. return voices;
  383. }
  384. /**
  385. * Returns all the graphicalVoiceEntries of a given Voice.
  386. * @param voice the voice for which the graphicalVoiceEntries shall be returned.
  387. */
  388. public getGraphicalVoiceEntriesPerVoice(voice: Voice): GraphicalVoiceEntry[] {
  389. const voiceEntries: GraphicalVoiceEntry[] = [];
  390. for (const gse of this.staffEntries) {
  391. for (const gve of gse.graphicalVoiceEntries) {
  392. if (gve.parentVoiceEntry.ParentVoice === voice) {
  393. voiceEntries.push(gve);
  394. }
  395. }
  396. }
  397. return voiceEntries;
  398. }
  399. /**
  400. * Finds the gaps between the existing notes within a measure.
  401. * Problem here is, that the graphicalVoiceEntry does not exist yet and
  402. * that Tied notes are not present in the normal voiceEntries.
  403. * To handle this, calculation with absolute timestamps is needed.
  404. * And the graphical notes have to be analysed directly (and not the voiceEntries, as it actually should be -> needs refactoring)
  405. * @param voice the voice for which the ghost notes shall be searched.
  406. */
  407. private getRestFilledVexFlowStaveNotesPerVoice(voice: Voice): GraphicalVoiceEntry[] {
  408. let latestVoiceTimestamp: Fraction = undefined;
  409. const gvEntries: GraphicalVoiceEntry[] = this.getGraphicalVoiceEntriesPerVoice(voice);
  410. for (let idx: number = 0, len: number = gvEntries.length; idx < len; ++idx) {
  411. const gve: GraphicalVoiceEntry = gvEntries[idx];
  412. const gNotesStartTimestamp: Fraction = gve.notes[0].sourceNote.getAbsoluteTimestamp();
  413. // find the voiceEntry end timestamp:
  414. let gNotesEndTimestamp: Fraction = new Fraction();
  415. for (const graphicalNote of gve.notes) {
  416. const noteEnd: Fraction = Fraction.plus(graphicalNote.sourceNote.getAbsoluteTimestamp(), graphicalNote.sourceNote.Length);
  417. if (gNotesEndTimestamp < noteEnd) {
  418. gNotesEndTimestamp = noteEnd;
  419. }
  420. }
  421. // check if this voice has just been found the first time:
  422. if (latestVoiceTimestamp === undefined) {
  423. // if this voice is new, check for a gap from measure start to the start of the current voice entry:
  424. const gapFromMeasureStart: Fraction = Fraction.minus(gNotesStartTimestamp, this.parentSourceMeasure.AbsoluteTimestamp);
  425. if (gapFromMeasureStart.RealValue > 0) {
  426. log.debug("Ghost Found at start");
  427. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(gapFromMeasureStart);
  428. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  429. ghostGve.vfStaveNote = vfghost;
  430. gvEntries.splice(0, 0, ghostGve);
  431. idx++;
  432. }
  433. } else {
  434. // get the length of the empty space between notes:
  435. const inBetweenLength: Fraction = Fraction.minus(gNotesStartTimestamp, latestVoiceTimestamp);
  436. if (inBetweenLength.RealValue > 0) {
  437. log.debug("Ghost Found in between");
  438. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(inBetweenLength);
  439. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  440. ghostGve.vfStaveNote = vfghost;
  441. // add element before current element:
  442. gvEntries.splice(idx, 0, ghostGve);
  443. // and increase index, as we added an element:
  444. idx++;
  445. }
  446. }
  447. // finally set the latest timestamp of this voice to the end timestamp of the longest note in the current voiceEntry:
  448. latestVoiceTimestamp = gNotesEndTimestamp;
  449. }
  450. const measureEndTimestamp: Fraction = Fraction.plus(this.parentSourceMeasure.AbsoluteTimestamp, this.parentSourceMeasure.Duration);
  451. const restLength: Fraction = Fraction.minus(measureEndTimestamp, latestVoiceTimestamp);
  452. if (restLength.RealValue > 0) {
  453. // fill the gap with a rest ghost note
  454. // starting from lastFraction
  455. // with length restLength:
  456. log.debug("Ghost Found at end");
  457. const vfghost: Vex.Flow.GhostNote = VexFlowConverter.GhostNote(restLength);
  458. const ghostGve: VexFlowVoiceEntry = new VexFlowVoiceEntry(undefined, undefined);
  459. ghostGve.vfStaveNote = vfghost;
  460. gvEntries.push(ghostGve);
  461. }
  462. return gvEntries;
  463. }
  464. /**
  465. * Add a note to a beam
  466. * @param graphicalNote
  467. * @param beam
  468. */
  469. public handleBeam(graphicalNote: GraphicalNote, beam: Beam): void {
  470. const voiceID: number = graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice.VoiceId;
  471. let beams: [Beam, VexFlowVoiceEntry[]][] = this.beams[voiceID];
  472. if (beams === undefined) {
  473. beams = this.beams[voiceID] = [];
  474. }
  475. let data: [Beam, VexFlowVoiceEntry[]];
  476. for (const mybeam of beams) {
  477. if (mybeam[0] === beam) {
  478. data = mybeam;
  479. }
  480. }
  481. if (data === undefined) {
  482. data = [beam, []];
  483. beams.push(data);
  484. }
  485. const parent: VexFlowVoiceEntry = graphicalNote.parentVoiceEntry as VexFlowVoiceEntry;
  486. if (data[1].indexOf(parent) < 0) {
  487. data[1].push(parent);
  488. }
  489. }
  490. public handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet): void {
  491. const voiceID: number = graphicalNote.sourceNote.ParentVoiceEntry.ParentVoice.VoiceId;
  492. tuplet = graphicalNote.sourceNote.NoteTuplet;
  493. let tuplets: [Tuplet, VexFlowVoiceEntry[]][] = this.tuplets[voiceID];
  494. if (tuplets === undefined) {
  495. tuplets = this.tuplets[voiceID] = [];
  496. }
  497. let currentTupletBuilder: [Tuplet, VexFlowVoiceEntry[]];
  498. for (const t of tuplets) {
  499. if (t[0] === tuplet) {
  500. currentTupletBuilder = t;
  501. }
  502. }
  503. if (currentTupletBuilder === undefined) {
  504. currentTupletBuilder = [tuplet, []];
  505. tuplets.push(currentTupletBuilder);
  506. }
  507. const parent: VexFlowVoiceEntry = graphicalNote.parentVoiceEntry as VexFlowVoiceEntry;
  508. if (currentTupletBuilder[1].indexOf(parent) < 0) {
  509. currentTupletBuilder[1].push(parent);
  510. }
  511. }
  512. /**
  513. * Complete the creation of VexFlow Beams in this measure
  514. */
  515. public finalizeBeams(): void {
  516. // The following line resets the created Vex.Flow Beams and
  517. // created them brand new. Is this needed? And more importantly,
  518. // should the old beams be removed manually by the notes?
  519. this.vfbeams = {};
  520. const beamedNotes: StaveNote[] = []; // already beamed notes, will be ignored by this.autoBeamNotes()
  521. for (const voiceID in this.beams) {
  522. if (this.beams.hasOwnProperty(voiceID)) {
  523. let vfbeams: Vex.Flow.Beam[] = this.vfbeams[voiceID];
  524. if (vfbeams === undefined) {
  525. vfbeams = this.vfbeams[voiceID] = [];
  526. }
  527. for (const beam of this.beams[voiceID]) {
  528. const notes: Vex.Flow.StaveNote[] = [];
  529. const psBeam: Beam = beam[0];
  530. const voiceEntries: VexFlowVoiceEntry[] = beam[1];
  531. let autoStemBeam: boolean = true;
  532. for (const gve of voiceEntries) {
  533. if (gve.parentVoiceEntry.ParentVoice === psBeam.Notes[0].ParentVoiceEntry.ParentVoice) {
  534. autoStemBeam = gve.parentVoiceEntry.WantedStemDirection === StemDirectionType.Undefined;
  535. }
  536. }
  537. let isGraceBeam: boolean = false;
  538. let beamColor: string;
  539. const stemColors: string[] = [];
  540. for (const entry of voiceEntries) {
  541. const note: Vex.Flow.StaveNote = ((<VexFlowVoiceEntry>entry).vfStaveNote as StaveNote);
  542. if (note !== undefined) {
  543. notes.push(note);
  544. beamedNotes.push(note);
  545. }
  546. if (entry.parentVoiceEntry.IsGrace) {
  547. isGraceBeam = true;
  548. }
  549. if (entry.parentVoiceEntry.StemColor && EngravingRules.Rules.ColoringEnabled) {
  550. stemColors.push(entry.parentVoiceEntry.StemColor);
  551. }
  552. }
  553. if (notes.length > 1) {
  554. const vfBeam: Vex.Flow.Beam = new Vex.Flow.Beam(notes, autoStemBeam);
  555. if (isGraceBeam) {
  556. // smaller beam, as in Vexflow.GraceNoteGroup.beamNotes()
  557. (<any>vfBeam).render_options.beam_width = 3;
  558. (<any>vfBeam).render_options.partial_beam_length = 4;
  559. }
  560. if (stemColors.length >= 2 && EngravingRules.Rules.ColorBeams) {
  561. beamColor = stemColors[0];
  562. for (const stemColor of stemColors) {
  563. if (stemColor !== beamColor) {
  564. beamColor = undefined;
  565. break;
  566. }
  567. }
  568. vfBeam.setStyle({ fillStyle: beamColor, strokeStyle: beamColor });
  569. }
  570. vfbeams.push(vfBeam);
  571. } else {
  572. log.debug("Warning! Beam with no notes!");
  573. }
  574. }
  575. }
  576. }
  577. if (EngravingRules.Rules.AutoBeamNotes) {
  578. this.autoBeamNotes(beamedNotes); // try to autobeam notes except those that are already beamed (beamedNotes).
  579. }
  580. }
  581. /** Automatically creates beams for notes except beamedNotes, using Vexflow's Beam.generateBeams().
  582. * Takes options from EngravingRules.Rules.AutoBeamOptions.
  583. * @param beamedNotes notes that will not be autobeamed (usually because they are already beamed)
  584. */
  585. private autoBeamNotes(beamedNotes: StemmableNote[]): void {
  586. const notesToAutoBeam: StemmableNote[] = [];
  587. let consecutiveBeamableNotes: StemmableNote[] = [];
  588. let currentTuplet: Tuplet;
  589. let tupletNotesToAutoBeam: StaveNote[] = [];
  590. this.autoTupletVfBeams = [];
  591. for (const staffEntry of this.staffEntries) {
  592. for (const gve of staffEntry.graphicalVoiceEntries) {
  593. const vfStaveNote: StaveNote = <StaveNote> (gve as VexFlowVoiceEntry).vfStaveNote;
  594. if (gve.parentVoiceEntry.IsGrace || // don't beam grace notes
  595. gve.notes[0].graphicalNoteLength.CompareTo(new Fraction(1, 4)) >= 0 || // don't beam quarter or longer notes
  596. beamedNotes.contains(vfStaveNote)) { // don't beam already beamed notes
  597. if (consecutiveBeamableNotes.length >= 2) { // don't beam notes surrounded by quarter notes etc.
  598. for (const note of consecutiveBeamableNotes) {
  599. notesToAutoBeam.push(note);
  600. }
  601. }
  602. consecutiveBeamableNotes = [];
  603. continue;
  604. }
  605. // create beams for tuplets separately
  606. const noteTuplet: Tuplet = gve.notes[0].sourceNote.NoteTuplet;
  607. if (noteTuplet) {
  608. if (currentTuplet === undefined) {
  609. currentTuplet = noteTuplet;
  610. } else {
  611. if (currentTuplet !== noteTuplet) { // new tuplet, finish old one
  612. if (tupletNotesToAutoBeam.length > 1) {
  613. this.autoTupletVfBeams.push(new Vex.Flow.Beam(tupletNotesToAutoBeam, true));
  614. }
  615. tupletNotesToAutoBeam = [];
  616. currentTuplet = noteTuplet;
  617. }
  618. }
  619. tupletNotesToAutoBeam.push(<StaveNote>(gve as VexFlowVoiceEntry).vfStaveNote);
  620. continue;
  621. } else {
  622. currentTuplet = undefined;
  623. }
  624. consecutiveBeamableNotes.push((gve as VexFlowVoiceEntry).vfStaveNote);
  625. }
  626. }
  627. if (tupletNotesToAutoBeam.length >= 2) {
  628. this.autoTupletVfBeams.push(new Vex.Flow.Beam(tupletNotesToAutoBeam, true));
  629. }
  630. if (consecutiveBeamableNotes.length >= 2) {
  631. for (const note of consecutiveBeamableNotes) {
  632. notesToAutoBeam.push(note);
  633. }
  634. }
  635. // create options for generateBeams
  636. const autoBeamOptions: AutoBeamOptions = EngravingRules.Rules.AutoBeamOptions;
  637. const generateBeamOptions: any = {
  638. beam_middle_only: autoBeamOptions.beam_middle_rests_only,
  639. beam_rests: autoBeamOptions.beam_rests,
  640. maintain_stem_directions: autoBeamOptions.maintain_stem_directions,
  641. };
  642. if (autoBeamOptions.groups && autoBeamOptions.groups.length) {
  643. const groups: Vex.Flow.Fraction[] = [];
  644. for (const fraction of autoBeamOptions.groups) {
  645. groups.push(new Vex.Flow.Fraction(fraction[0], fraction[1]));
  646. }
  647. generateBeamOptions.groups = groups;
  648. }
  649. this.autoVfBeams = Vex.Flow.Beam.generateBeams(notesToAutoBeam, generateBeamOptions);
  650. }
  651. /**
  652. * Complete the creation of VexFlow Tuplets in this measure
  653. */
  654. public finalizeTuplets(): void {
  655. // The following line resets the created Vex.Flow Tuplets and
  656. // created them brand new. Is this needed? And more importantly,
  657. // should the old tuplets be removed manually from the notes?
  658. this.vftuplets = {};
  659. for (const voiceID in this.tuplets) {
  660. if (this.tuplets.hasOwnProperty(voiceID)) {
  661. let vftuplets: Vex.Flow.Tuplet[] = this.vftuplets[voiceID];
  662. if (vftuplets === undefined) {
  663. vftuplets = this.vftuplets[voiceID] = [];
  664. }
  665. for (const tupletBuilder of this.tuplets[voiceID]) {
  666. const tupletStaveNotes: Vex.Flow.StaveNote[] = [];
  667. const tupletVoiceEntries: VexFlowVoiceEntry[] = tupletBuilder[1];
  668. for (const tupletVoiceEntry of tupletVoiceEntries) {
  669. tupletStaveNotes.push(((tupletVoiceEntry).vfStaveNote as StaveNote));
  670. }
  671. if (tupletStaveNotes.length > 1) {
  672. const notesOccupied: number = 2;
  673. const tuplet: Tuplet = tupletBuilder[0];
  674. const bracketed: boolean = tuplet.Bracket ||
  675. (tuplet.TupletLabelNumber === 3 && EngravingRules.Rules.TripletsBracketed) ||
  676. (tuplet.TupletLabelNumber !== 3 && EngravingRules.Rules.TupletsBracketed);
  677. vftuplets.push(new Vex.Flow.Tuplet( tupletStaveNotes,
  678. {
  679. bracketed: bracketed,
  680. notes_occupied: notesOccupied,
  681. num_notes: tupletStaveNotes.length, //, location: -1, ratioed: true
  682. ratioed: EngravingRules.Rules.TupletsRatioed,
  683. }));
  684. } else {
  685. log.debug("Warning! Tuplet with no notes! Trying to ignore, but this is a serious problem.");
  686. }
  687. }
  688. }
  689. }
  690. }
  691. public layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  692. return;
  693. }
  694. public graphicalMeasureCreatedCalculations(): void {
  695. let graceSlur: boolean;
  696. let graceGVoiceEntriesBefore: GraphicalVoiceEntry[];
  697. for (const graphicalStaffEntry of this.staffEntries as VexFlowStaffEntry[]) {
  698. graceSlur = false;
  699. graceGVoiceEntriesBefore = [];
  700. // create vex flow Stave Notes:
  701. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  702. if (gve.parentVoiceEntry.IsGrace) {
  703. graceGVoiceEntriesBefore.push(gve);
  704. if (!graceSlur) {
  705. graceSlur = gve.parentVoiceEntry.GraceSlur;
  706. }
  707. continue;
  708. }
  709. if (gve.notes[0].sourceNote.PrintObject) {
  710. (gve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.StaveNote(gve);
  711. } else {
  712. // don't render note. add ghost note, otherwise Vexflow can have issues with layouting when voices not complete.
  713. (gve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.GhostNote(gve.notes[0].sourceNote.Length);
  714. graceGVoiceEntriesBefore = []; // if note is not rendered, its grace notes might need to be removed
  715. continue;
  716. }
  717. if (graceGVoiceEntriesBefore.length > 0) {
  718. const graceNotes: Vex.Flow.GraceNote[] = [];
  719. for (let i: number = 0; i < graceGVoiceEntriesBefore.length; i++) {
  720. const gveGrace: VexFlowVoiceEntry = <VexFlowVoiceEntry>graceGVoiceEntriesBefore[i];
  721. if (gveGrace.notes[0].sourceNote.PrintObject) {
  722. const vfStaveNote: StaveNote = VexFlowConverter.StaveNote(gveGrace);
  723. gveGrace.vfStaveNote = vfStaveNote;
  724. graceNotes.push(vfStaveNote);
  725. }
  726. }
  727. const graceNoteGroup: Vex.Flow.GraceNoteGroup = new Vex.Flow.GraceNoteGroup(graceNotes, graceSlur);
  728. (gve as VexFlowVoiceEntry).vfStaveNote.addModifier(0, graceNoteGroup);
  729. graceGVoiceEntriesBefore = [];
  730. }
  731. }
  732. }
  733. // remaining grace notes at end of measure, turned into stand-alone grace notes:
  734. if (graceGVoiceEntriesBefore.length > 0) {
  735. for (const graceGve of graceGVoiceEntriesBefore) {
  736. (graceGve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.StaveNote(graceGve);
  737. graceGve.parentVoiceEntry.GraceAfterMainNote = true;
  738. }
  739. }
  740. this.finalizeBeams();
  741. this.finalizeTuplets();
  742. const voices: Voice[] = this.getVoicesWithinMeasure();
  743. for (const voice of voices) {
  744. if (voice === undefined) {
  745. continue;
  746. }
  747. const isMainVoice: boolean = !(voice instanceof LinkedVoice);
  748. // add a vexFlow voice for this voice:
  749. this.vfVoices[voice.VoiceId] = new Vex.Flow.Voice({
  750. beat_value: this.parentSourceMeasure.Duration.Denominator,
  751. num_beats: this.parentSourceMeasure.Duration.Numerator,
  752. resolution: Vex.Flow.RESOLUTION,
  753. }).setMode(Vex.Flow.Voice.Mode.SOFT);
  754. const restFilledEntries: GraphicalVoiceEntry[] = this.getRestFilledVexFlowStaveNotesPerVoice(voice);
  755. // create vex flow voices and add tickables to it:
  756. for (const voiceEntry of restFilledEntries) {
  757. if (voiceEntry.parentVoiceEntry) {
  758. if (voiceEntry.parentVoiceEntry.IsGrace && !voiceEntry.parentVoiceEntry.GraceAfterMainNote) {
  759. continue;
  760. }
  761. }
  762. const vexFlowVoiceEntry: VexFlowVoiceEntry = voiceEntry as VexFlowVoiceEntry;
  763. if (voiceEntry.notes.length === 0 || !voiceEntry.notes[0] || !voiceEntry.notes[0].sourceNote.PrintObject) {
  764. // GhostNote, don't add modifiers like in-measure clefs
  765. this.vfVoices[voice.VoiceId].addTickable(vexFlowVoiceEntry.vfStaveNote);
  766. continue;
  767. }
  768. // check for in-measure clefs:
  769. // only add clefs in main voice (to not add them twice)
  770. if (isMainVoice) {
  771. const vfse: VexFlowStaffEntry = vexFlowVoiceEntry.parentStaffEntry as VexFlowStaffEntry;
  772. if (vfse && vfse.vfClefBefore !== undefined) {
  773. // add clef as NoteSubGroup so that we get modifier layouting
  774. const clefModifier: NoteSubGroup = new NoteSubGroup( [vfse.vfClefBefore] );
  775. vexFlowVoiceEntry.vfStaveNote.addModifier(0, clefModifier);
  776. }
  777. }
  778. // add fingering
  779. if (voiceEntry.parentVoiceEntry && EngravingRules.Rules.RenderFingerings) {
  780. this.createFingerings(voiceEntry);
  781. }
  782. // add Arpeggio
  783. if (voiceEntry.parentVoiceEntry && voiceEntry.parentVoiceEntry.Arpeggio !== undefined) {
  784. const type: ArpeggioType = voiceEntry.parentVoiceEntry.Arpeggio.type;
  785. vexFlowVoiceEntry.vfStaveNote.addStroke(0, new Vex.Flow.Stroke(type));
  786. }
  787. this.vfVoices[voice.VoiceId].addTickable(vexFlowVoiceEntry.vfStaveNote);
  788. }
  789. }
  790. this.createArticulations();
  791. this.createOrnaments();
  792. this.setStemDirectionFromVexFlow();
  793. }
  794. /**
  795. * Copy the stem directions chosen by VexFlow to the StemDirection variable of the graphical notes
  796. */
  797. private setStemDirectionFromVexFlow(): void {
  798. //if StemDirection was not set then read out what VexFlow has chosen
  799. for ( const vfStaffEntry of this.staffEntries ) {
  800. for ( const gVoiceEntry of vfStaffEntry.graphicalVoiceEntries) {
  801. for ( const gnote of gVoiceEntry.notes) {
  802. const vfnote: [StaveNote, number] = (gnote as VexFlowGraphicalNote).vfnote;
  803. if (vfnote === undefined || vfnote[0] === undefined) {
  804. continue;
  805. }
  806. const vfStemDir: number = vfnote[0].getStemDirection();
  807. switch (vfStemDir) {
  808. case (Vex.Flow.Stem.UP):
  809. gVoiceEntry.parentVoiceEntry.StemDirection = StemDirectionType.Up;
  810. break;
  811. case (Vex.Flow.Stem.DOWN):
  812. gVoiceEntry.parentVoiceEntry.StemDirection = StemDirectionType.Down;
  813. break;
  814. default:
  815. }
  816. }
  817. }
  818. }
  819. }
  820. /**
  821. * Create the articulations for all notes of the current staff entry
  822. */
  823. private createArticulations(): void {
  824. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  825. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  826. // create vex flow articulation:
  827. const graphicalVoiceEntries: GraphicalVoiceEntry[] = graphicalStaffEntry.graphicalVoiceEntries;
  828. for (const gve of graphicalVoiceEntries) {
  829. const vfStaveNote: StemmableNote = (gve as VexFlowVoiceEntry).vfStaveNote;
  830. VexFlowConverter.generateArticulations(vfStaveNote, gve.notes[0].sourceNote.ParentVoiceEntry.Articulations);
  831. }
  832. }
  833. }
  834. /**
  835. * Create the ornaments for all notes of the current staff entry
  836. */
  837. private createOrnaments(): void {
  838. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  839. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  840. const gvoices: { [voiceID: number]: GraphicalVoiceEntry; } = graphicalStaffEntry.graphicalVoiceEntries;
  841. for (const voiceID in gvoices) {
  842. if (gvoices.hasOwnProperty(voiceID)) {
  843. const vfStaveNote: StemmableNote = (gvoices[voiceID] as VexFlowVoiceEntry).vfStaveNote;
  844. const ornamentContainer: OrnamentContainer = gvoices[voiceID].notes[0].sourceNote.ParentVoiceEntry.OrnamentContainer;
  845. if (ornamentContainer !== undefined) {
  846. VexFlowConverter.generateOrnaments(vfStaveNote, ornamentContainer);
  847. }
  848. }
  849. }
  850. }
  851. }
  852. private createFingerings(voiceEntry: GraphicalVoiceEntry): void {
  853. const vexFlowVoiceEntry: VexFlowVoiceEntry = voiceEntry as VexFlowVoiceEntry;
  854. const technicalInstructions: TechnicalInstruction[] = voiceEntry.parentVoiceEntry.TechnicalInstructions;
  855. const fingeringsCount: number = technicalInstructions.length;
  856. for (let i: number = 0; i < technicalInstructions.length; i++) {
  857. const technicalInstruction: TechnicalInstruction = technicalInstructions[i];
  858. let fingeringPosition: PlacementEnum = EngravingRules.Rules.FingeringPosition;
  859. if (technicalInstruction.placement !== PlacementEnum.NotYetDefined) {
  860. fingeringPosition = technicalInstruction.placement;
  861. }
  862. let modifierPosition: any; // Vex.Flow.Modifier.Position
  863. switch (fingeringPosition) {
  864. default:
  865. case PlacementEnum.Left:
  866. modifierPosition = Vex.Flow.Modifier.Position.LEFT;
  867. break;
  868. case PlacementEnum.Right:
  869. modifierPosition = Vex.Flow.Modifier.Position.RIGHT;
  870. break;
  871. case PlacementEnum.Above:
  872. modifierPosition = Vex.Flow.Modifier.Position.ABOVE;
  873. break;
  874. case PlacementEnum.Below:
  875. modifierPosition = Vex.Flow.Modifier.Position.BELOW;
  876. break;
  877. case PlacementEnum.NotYetDefined: // automatic fingering placement, could be more complex/customizable
  878. const sourceStaff: Staff = voiceEntry.parentStaffEntry.sourceStaffEntry.ParentStaff;
  879. if (voiceEntry.notes.length > 1 || voiceEntry.parentStaffEntry.graphicalVoiceEntries.length > 1) {
  880. modifierPosition = Vex.Flow.Modifier.Position.LEFT;
  881. } else if (sourceStaff.idInMusicSheet === 0) {
  882. modifierPosition = Vex.Flow.Modifier.Position.ABOVE;
  883. fingeringPosition = PlacementEnum.Above;
  884. } else {
  885. modifierPosition = Vex.Flow.Modifier.Position.BELOW;
  886. fingeringPosition = PlacementEnum.Below;
  887. }
  888. }
  889. const fretFinger: Vex.Flow.FretHandFinger = new Vex.Flow.FretHandFinger(technicalInstruction.value);
  890. fretFinger.setPosition(modifierPosition);
  891. if (fingeringPosition === PlacementEnum.Above || fingeringPosition === PlacementEnum.Below) {
  892. const offsetYSign: number = fingeringPosition === PlacementEnum.Above ? -1 : 1; // minus y is up
  893. const ordering: number = fingeringPosition === PlacementEnum.Above ? i :
  894. technicalInstructions.length - 1 - i; // reverse order for fingerings below staff
  895. if (EngravingRules.Rules.FingeringInsideStafflines && fingeringsCount > 1) { // y-shift for single fingering is ok
  896. // experimental, bounding boxes wrong for fretFinger above/below, better would be creating Labels
  897. // set y-shift. vexflow fretfinger simply places directly above/below note
  898. const perFingeringShift: number = fretFinger.getWidth() / 2;
  899. const shiftCount: number = fingeringsCount * 2.5;
  900. (<any>fretFinger).setOffsetY(offsetYSign * (ordering + shiftCount) * perFingeringShift);
  901. } else if (!EngravingRules.Rules.FingeringInsideStafflines) { // use StringNumber for placement above/below stafflines
  902. const stringNumber: Vex.Flow.StringNumber = new Vex.Flow.StringNumber(technicalInstruction.value);
  903. (<any>stringNumber).radius = 0; // hack to remove the circle around the number
  904. stringNumber.setPosition(modifierPosition);
  905. stringNumber.setOffsetY(offsetYSign * ordering * stringNumber.getWidth() * 2 / 3);
  906. vexFlowVoiceEntry.vfStaveNote.addModifier(i, stringNumber);
  907. continue;
  908. }
  909. }
  910. vexFlowVoiceEntry.vfStaveNote.addModifier(i, fretFinger);
  911. }
  912. }
  913. /**
  914. * Creates a line from 'top' to this measure, of type 'lineType'
  915. * @param top
  916. * @param lineType
  917. */
  918. public lineTo(top: VexFlowMeasure, lineType: any): void {
  919. const connector: StaveConnector = new Vex.Flow.StaveConnector(top.getVFStave(), this.stave);
  920. connector.setType(lineType);
  921. this.connectors.push(connector);
  922. }
  923. /**
  924. * Return the VexFlow Stave corresponding to this graphicalMeasure
  925. * @returns {Vex.Flow.Stave}
  926. */
  927. public getVFStave(): Vex.Flow.Stave {
  928. return this.stave;
  929. }
  930. /**
  931. * After re-running the formatting on the VexFlow Stave, update the
  932. * space needed by Instructions (in VexFlow: StaveModifiers)
  933. */
  934. private updateInstructionWidth(): void {
  935. let vfBeginInstructionsWidth: number = 0;
  936. let vfEndInstructionsWidth: number = 0;
  937. const modifiers: Vex.Flow.StaveModifier[] = this.stave.getModifiers();
  938. for (const mod of modifiers) {
  939. if (mod.getPosition() === StavePositionEnum.BEGIN) { //Vex.Flow.StaveModifier.Position.BEGIN) {
  940. vfBeginInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  941. } else if (mod.getPosition() === StavePositionEnum.END) { //Vex.Flow.StaveModifier.Position.END) {
  942. vfEndInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  943. }
  944. }
  945. this.beginInstructionsWidth = vfBeginInstructionsWidth / unitInPixels;
  946. this.endInstructionsWidth = vfEndInstructionsWidth / unitInPixels;
  947. }
  948. }
  949. // Gives the position of the Stave - replaces the function get Position() in the description of class StaveModifier in vexflow.d.ts
  950. // The latter gave an error because function cannot be defined in the class descriptions in vexflow.d.ts
  951. export enum StavePositionEnum {
  952. LEFT = 1,
  953. RIGHT = 2,
  954. ABOVE = 3,
  955. BELOW = 4,
  956. BEGIN = 5,
  957. END = 6
  958. }