VexFlowMeasure.ts 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. let beamHasQuarterNoteOrLonger: boolean = false;
  529. for (const note of beam[0].Notes) {
  530. if (note.Length.RealValue >= new Fraction(1, 4).RealValue) {
  531. beamHasQuarterNoteOrLonger = true;
  532. break;
  533. }
  534. }
  535. if (beamHasQuarterNoteOrLonger) {
  536. log.debug("Beam between note >= quarter, likely tremolo, currently unsupported. continuing.");
  537. continue;
  538. }
  539. const notes: Vex.Flow.StaveNote[] = [];
  540. const psBeam: Beam = beam[0];
  541. const voiceEntries: VexFlowVoiceEntry[] = beam[1];
  542. let autoStemBeam: boolean = true;
  543. for (const gve of voiceEntries) {
  544. if (gve.parentVoiceEntry.ParentVoice === psBeam.Notes[0].ParentVoiceEntry.ParentVoice) {
  545. autoStemBeam = gve.parentVoiceEntry.WantedStemDirection === StemDirectionType.Undefined;
  546. }
  547. }
  548. let isGraceBeam: boolean = false;
  549. let beamColor: string;
  550. const stemColors: string[] = [];
  551. for (const entry of voiceEntries) {
  552. const note: Vex.Flow.StaveNote = ((<VexFlowVoiceEntry>entry).vfStaveNote as StaveNote);
  553. if (note !== undefined) {
  554. notes.push(note);
  555. beamedNotes.push(note);
  556. }
  557. if (entry.parentVoiceEntry.IsGrace) {
  558. isGraceBeam = true;
  559. }
  560. if (entry.parentVoiceEntry.StemColor && EngravingRules.Rules.ColoringEnabled) {
  561. stemColors.push(entry.parentVoiceEntry.StemColor);
  562. }
  563. }
  564. if (notes.length > 1) {
  565. const vfBeam: Vex.Flow.Beam = new Vex.Flow.Beam(notes, autoStemBeam);
  566. if (isGraceBeam) {
  567. // smaller beam, as in Vexflow.GraceNoteGroup.beamNotes()
  568. (<any>vfBeam).render_options.beam_width = 3;
  569. (<any>vfBeam).render_options.partial_beam_length = 4;
  570. }
  571. if (stemColors.length >= 2 && EngravingRules.Rules.ColorBeams) {
  572. beamColor = stemColors[0];
  573. for (const stemColor of stemColors) {
  574. if (stemColor !== beamColor) {
  575. beamColor = undefined;
  576. break;
  577. }
  578. }
  579. vfBeam.setStyle({ fillStyle: beamColor, strokeStyle: beamColor });
  580. }
  581. vfbeams.push(vfBeam);
  582. } else {
  583. log.debug("Warning! Beam with no notes!");
  584. }
  585. }
  586. }
  587. }
  588. if (EngravingRules.Rules.AutoBeamNotes) {
  589. this.autoBeamNotes(beamedNotes); // try to autobeam notes except those that are already beamed (beamedNotes).
  590. }
  591. }
  592. /** Automatically creates beams for notes except beamedNotes, using Vexflow's Beam.generateBeams().
  593. * Takes options from EngravingRules.Rules.AutoBeamOptions.
  594. * @param beamedNotes notes that will not be autobeamed (usually because they are already beamed)
  595. */
  596. private autoBeamNotes(beamedNotes: StemmableNote[]): void {
  597. const notesToAutoBeam: StemmableNote[] = [];
  598. let consecutiveBeamableNotes: StemmableNote[] = [];
  599. let currentTuplet: Tuplet;
  600. let tupletNotesToAutoBeam: StaveNote[] = [];
  601. this.autoTupletVfBeams = [];
  602. for (const staffEntry of this.staffEntries) {
  603. for (const gve of staffEntry.graphicalVoiceEntries) {
  604. const vfStaveNote: StaveNote = <StaveNote> (gve as VexFlowVoiceEntry).vfStaveNote;
  605. if (gve.parentVoiceEntry.IsGrace || // don't beam grace notes
  606. gve.notes[0].graphicalNoteLength.CompareTo(new Fraction(1, 4)) >= 0 || // don't beam quarter or longer notes
  607. beamedNotes.contains(vfStaveNote)) { // don't beam already beamed notes
  608. if (consecutiveBeamableNotes.length >= 2) { // don't beam notes surrounded by quarter notes etc.
  609. for (const note of consecutiveBeamableNotes) {
  610. notesToAutoBeam.push(note);
  611. }
  612. }
  613. consecutiveBeamableNotes = [];
  614. continue;
  615. }
  616. // create beams for tuplets separately
  617. const noteTuplet: Tuplet = gve.notes[0].sourceNote.NoteTuplet;
  618. if (noteTuplet) {
  619. if (currentTuplet === undefined) {
  620. currentTuplet = noteTuplet;
  621. } else {
  622. if (currentTuplet !== noteTuplet) { // new tuplet, finish old one
  623. if (tupletNotesToAutoBeam.length > 1) {
  624. this.autoTupletVfBeams.push(new Vex.Flow.Beam(tupletNotesToAutoBeam, true));
  625. }
  626. tupletNotesToAutoBeam = [];
  627. currentTuplet = noteTuplet;
  628. }
  629. }
  630. tupletNotesToAutoBeam.push(<StaveNote>(gve as VexFlowVoiceEntry).vfStaveNote);
  631. continue;
  632. } else {
  633. currentTuplet = undefined;
  634. }
  635. consecutiveBeamableNotes.push((gve as VexFlowVoiceEntry).vfStaveNote);
  636. }
  637. }
  638. if (tupletNotesToAutoBeam.length >= 2) {
  639. this.autoTupletVfBeams.push(new Vex.Flow.Beam(tupletNotesToAutoBeam, true));
  640. }
  641. if (consecutiveBeamableNotes.length >= 2) {
  642. for (const note of consecutiveBeamableNotes) {
  643. notesToAutoBeam.push(note);
  644. }
  645. }
  646. // create options for generateBeams
  647. const autoBeamOptions: AutoBeamOptions = EngravingRules.Rules.AutoBeamOptions;
  648. const generateBeamOptions: any = {
  649. beam_middle_only: autoBeamOptions.beam_middle_rests_only,
  650. beam_rests: autoBeamOptions.beam_rests,
  651. maintain_stem_directions: autoBeamOptions.maintain_stem_directions,
  652. };
  653. if (autoBeamOptions.groups && autoBeamOptions.groups.length) {
  654. const groups: Vex.Flow.Fraction[] = [];
  655. for (const fraction of autoBeamOptions.groups) {
  656. groups.push(new Vex.Flow.Fraction(fraction[0], fraction[1]));
  657. }
  658. generateBeamOptions.groups = groups;
  659. }
  660. this.autoVfBeams = Vex.Flow.Beam.generateBeams(notesToAutoBeam, generateBeamOptions);
  661. }
  662. /**
  663. * Complete the creation of VexFlow Tuplets in this measure
  664. */
  665. public finalizeTuplets(): void {
  666. // The following line resets the created Vex.Flow Tuplets and
  667. // created them brand new. Is this needed? And more importantly,
  668. // should the old tuplets be removed manually from the notes?
  669. this.vftuplets = {};
  670. for (const voiceID in this.tuplets) {
  671. if (this.tuplets.hasOwnProperty(voiceID)) {
  672. let vftuplets: Vex.Flow.Tuplet[] = this.vftuplets[voiceID];
  673. if (vftuplets === undefined) {
  674. vftuplets = this.vftuplets[voiceID] = [];
  675. }
  676. for (const tupletBuilder of this.tuplets[voiceID]) {
  677. const tupletStaveNotes: Vex.Flow.StaveNote[] = [];
  678. const tupletVoiceEntries: VexFlowVoiceEntry[] = tupletBuilder[1];
  679. for (const tupletVoiceEntry of tupletVoiceEntries) {
  680. tupletStaveNotes.push(((tupletVoiceEntry).vfStaveNote as StaveNote));
  681. }
  682. if (tupletStaveNotes.length > 1) {
  683. const notesOccupied: number = 2;
  684. const tuplet: Tuplet = tupletBuilder[0];
  685. const bracketed: boolean = tuplet.Bracket ||
  686. (tuplet.TupletLabelNumber === 3 && EngravingRules.Rules.TripletsBracketed) ||
  687. (tuplet.TupletLabelNumber !== 3 && EngravingRules.Rules.TupletsBracketed);
  688. vftuplets.push(new Vex.Flow.Tuplet( tupletStaveNotes,
  689. {
  690. bracketed: bracketed,
  691. notes_occupied: notesOccupied,
  692. num_notes: tupletStaveNotes.length, //, location: -1, ratioed: true
  693. ratioed: EngravingRules.Rules.TupletsRatioed,
  694. }));
  695. } else {
  696. log.debug("Warning! Tuplet with no notes! Trying to ignore, but this is a serious problem.");
  697. }
  698. }
  699. }
  700. }
  701. }
  702. public layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  703. return;
  704. }
  705. public graphicalMeasureCreatedCalculations(): void {
  706. let graceSlur: boolean;
  707. let graceGVoiceEntriesBefore: GraphicalVoiceEntry[];
  708. for (const graphicalStaffEntry of this.staffEntries as VexFlowStaffEntry[]) {
  709. graceSlur = false;
  710. graceGVoiceEntriesBefore = [];
  711. // create vex flow Stave Notes:
  712. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  713. if (gve.parentVoiceEntry.IsGrace) {
  714. graceGVoiceEntriesBefore.push(gve);
  715. if (!graceSlur) {
  716. graceSlur = gve.parentVoiceEntry.GraceSlur;
  717. }
  718. continue;
  719. }
  720. if (gve.notes[0].sourceNote.PrintObject) {
  721. (gve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.StaveNote(gve);
  722. } else {
  723. // don't render note. add ghost note, otherwise Vexflow can have issues with layouting when voices not complete.
  724. (gve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.GhostNote(gve.notes[0].sourceNote.Length);
  725. graceGVoiceEntriesBefore = []; // if note is not rendered, its grace notes might need to be removed
  726. continue;
  727. }
  728. if (graceGVoiceEntriesBefore.length > 0) {
  729. const graceNotes: Vex.Flow.GraceNote[] = [];
  730. for (let i: number = 0; i < graceGVoiceEntriesBefore.length; i++) {
  731. const gveGrace: VexFlowVoiceEntry = <VexFlowVoiceEntry>graceGVoiceEntriesBefore[i];
  732. if (gveGrace.notes[0].sourceNote.PrintObject) {
  733. const vfStaveNote: StaveNote = VexFlowConverter.StaveNote(gveGrace);
  734. gveGrace.vfStaveNote = vfStaveNote;
  735. graceNotes.push(vfStaveNote);
  736. }
  737. }
  738. const graceNoteGroup: Vex.Flow.GraceNoteGroup = new Vex.Flow.GraceNoteGroup(graceNotes, graceSlur);
  739. (gve as VexFlowVoiceEntry).vfStaveNote.addModifier(0, graceNoteGroup);
  740. graceGVoiceEntriesBefore = [];
  741. }
  742. }
  743. }
  744. // remaining grace notes at end of measure, turned into stand-alone grace notes:
  745. if (graceGVoiceEntriesBefore.length > 0) {
  746. for (const graceGve of graceGVoiceEntriesBefore) {
  747. (graceGve as VexFlowVoiceEntry).vfStaveNote = VexFlowConverter.StaveNote(graceGve);
  748. graceGve.parentVoiceEntry.GraceAfterMainNote = true;
  749. }
  750. }
  751. this.finalizeBeams();
  752. this.finalizeTuplets();
  753. const voices: Voice[] = this.getVoicesWithinMeasure();
  754. for (const voice of voices) {
  755. if (voice === undefined) {
  756. continue;
  757. }
  758. const isMainVoice: boolean = !(voice instanceof LinkedVoice);
  759. // add a vexFlow voice for this voice:
  760. this.vfVoices[voice.VoiceId] = new Vex.Flow.Voice({
  761. beat_value: this.parentSourceMeasure.Duration.Denominator,
  762. num_beats: this.parentSourceMeasure.Duration.Numerator,
  763. resolution: Vex.Flow.RESOLUTION,
  764. }).setMode(Vex.Flow.Voice.Mode.SOFT);
  765. const restFilledEntries: GraphicalVoiceEntry[] = this.getRestFilledVexFlowStaveNotesPerVoice(voice);
  766. // create vex flow voices and add tickables to it:
  767. for (const voiceEntry of restFilledEntries) {
  768. if (voiceEntry.parentVoiceEntry) {
  769. if (voiceEntry.parentVoiceEntry.IsGrace && !voiceEntry.parentVoiceEntry.GraceAfterMainNote) {
  770. continue;
  771. }
  772. }
  773. const vexFlowVoiceEntry: VexFlowVoiceEntry = voiceEntry as VexFlowVoiceEntry;
  774. if (voiceEntry.notes.length === 0 || !voiceEntry.notes[0] || !voiceEntry.notes[0].sourceNote.PrintObject) {
  775. // GhostNote, don't add modifiers like in-measure clefs
  776. this.vfVoices[voice.VoiceId].addTickable(vexFlowVoiceEntry.vfStaveNote);
  777. continue;
  778. }
  779. // check for in-measure clefs:
  780. // only add clefs in main voice (to not add them twice)
  781. if (isMainVoice) {
  782. const vfse: VexFlowStaffEntry = vexFlowVoiceEntry.parentStaffEntry as VexFlowStaffEntry;
  783. if (vfse && vfse.vfClefBefore !== undefined) {
  784. // add clef as NoteSubGroup so that we get modifier layouting
  785. const clefModifier: NoteSubGroup = new NoteSubGroup( [vfse.vfClefBefore] );
  786. vexFlowVoiceEntry.vfStaveNote.addModifier(0, clefModifier);
  787. }
  788. }
  789. // add fingering
  790. if (voiceEntry.parentVoiceEntry && EngravingRules.Rules.RenderFingerings) {
  791. this.createFingerings(voiceEntry);
  792. }
  793. // add Arpeggio
  794. if (voiceEntry.parentVoiceEntry && voiceEntry.parentVoiceEntry.Arpeggio !== undefined) {
  795. const type: ArpeggioType = voiceEntry.parentVoiceEntry.Arpeggio.type;
  796. vexFlowVoiceEntry.vfStaveNote.addStroke(0, new Vex.Flow.Stroke(type));
  797. }
  798. this.vfVoices[voice.VoiceId].addTickable(vexFlowVoiceEntry.vfStaveNote);
  799. }
  800. }
  801. this.createArticulations();
  802. this.createOrnaments();
  803. this.setStemDirectionFromVexFlow();
  804. }
  805. /**
  806. * Copy the stem directions chosen by VexFlow to the StemDirection variable of the graphical notes
  807. */
  808. private setStemDirectionFromVexFlow(): void {
  809. //if StemDirection was not set then read out what VexFlow has chosen
  810. for ( const vfStaffEntry of this.staffEntries ) {
  811. for ( const gVoiceEntry of vfStaffEntry.graphicalVoiceEntries) {
  812. for ( const gnote of gVoiceEntry.notes) {
  813. const vfnote: [StaveNote, number] = (gnote as VexFlowGraphicalNote).vfnote;
  814. if (vfnote === undefined || vfnote[0] === undefined) {
  815. continue;
  816. }
  817. const vfStemDir: number = vfnote[0].getStemDirection();
  818. switch (vfStemDir) {
  819. case (Vex.Flow.Stem.UP):
  820. gVoiceEntry.parentVoiceEntry.StemDirection = StemDirectionType.Up;
  821. break;
  822. case (Vex.Flow.Stem.DOWN):
  823. gVoiceEntry.parentVoiceEntry.StemDirection = StemDirectionType.Down;
  824. break;
  825. default:
  826. }
  827. }
  828. }
  829. }
  830. }
  831. /**
  832. * Create the articulations for all notes of the current staff entry
  833. */
  834. private createArticulations(): void {
  835. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  836. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  837. // create vex flow articulation:
  838. const graphicalVoiceEntries: GraphicalVoiceEntry[] = graphicalStaffEntry.graphicalVoiceEntries;
  839. for (const gve of graphicalVoiceEntries) {
  840. const vfStaveNote: StemmableNote = (gve as VexFlowVoiceEntry).vfStaveNote;
  841. VexFlowConverter.generateArticulations(vfStaveNote, gve.notes[0].sourceNote.ParentVoiceEntry.Articulations);
  842. }
  843. }
  844. }
  845. /**
  846. * Create the ornaments for all notes of the current staff entry
  847. */
  848. private createOrnaments(): void {
  849. for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
  850. const graphicalStaffEntry: VexFlowStaffEntry = (this.staffEntries[idx] as VexFlowStaffEntry);
  851. const gvoices: { [voiceID: number]: GraphicalVoiceEntry; } = graphicalStaffEntry.graphicalVoiceEntries;
  852. for (const voiceID in gvoices) {
  853. if (gvoices.hasOwnProperty(voiceID)) {
  854. const vfStaveNote: StemmableNote = (gvoices[voiceID] as VexFlowVoiceEntry).vfStaveNote;
  855. const ornamentContainer: OrnamentContainer = gvoices[voiceID].notes[0].sourceNote.ParentVoiceEntry.OrnamentContainer;
  856. if (ornamentContainer !== undefined) {
  857. VexFlowConverter.generateOrnaments(vfStaveNote, ornamentContainer);
  858. }
  859. }
  860. }
  861. }
  862. }
  863. private createFingerings(voiceEntry: GraphicalVoiceEntry): void {
  864. const vexFlowVoiceEntry: VexFlowVoiceEntry = voiceEntry as VexFlowVoiceEntry;
  865. const technicalInstructions: TechnicalInstruction[] = voiceEntry.parentVoiceEntry.TechnicalInstructions;
  866. const fingeringsCount: number = technicalInstructions.length;
  867. for (let i: number = 0; i < technicalInstructions.length; i++) {
  868. const technicalInstruction: TechnicalInstruction = technicalInstructions[i];
  869. let fingeringPosition: PlacementEnum = EngravingRules.Rules.FingeringPosition;
  870. if (technicalInstruction.placement !== PlacementEnum.NotYetDefined) {
  871. fingeringPosition = technicalInstruction.placement;
  872. }
  873. let modifierPosition: any; // Vex.Flow.Modifier.Position
  874. switch (fingeringPosition) {
  875. default:
  876. case PlacementEnum.Left:
  877. modifierPosition = Vex.Flow.Modifier.Position.LEFT;
  878. break;
  879. case PlacementEnum.Right:
  880. modifierPosition = Vex.Flow.Modifier.Position.RIGHT;
  881. break;
  882. case PlacementEnum.Above:
  883. modifierPosition = Vex.Flow.Modifier.Position.ABOVE;
  884. break;
  885. case PlacementEnum.Below:
  886. modifierPosition = Vex.Flow.Modifier.Position.BELOW;
  887. break;
  888. case PlacementEnum.NotYetDefined: // automatic fingering placement, could be more complex/customizable
  889. const sourceStaff: Staff = voiceEntry.parentStaffEntry.sourceStaffEntry.ParentStaff;
  890. if (voiceEntry.notes.length > 1 || voiceEntry.parentStaffEntry.graphicalVoiceEntries.length > 1) {
  891. modifierPosition = Vex.Flow.Modifier.Position.LEFT;
  892. } else if (sourceStaff.idInMusicSheet === 0) {
  893. modifierPosition = Vex.Flow.Modifier.Position.ABOVE;
  894. fingeringPosition = PlacementEnum.Above;
  895. } else {
  896. modifierPosition = Vex.Flow.Modifier.Position.BELOW;
  897. fingeringPosition = PlacementEnum.Below;
  898. }
  899. }
  900. const fretFinger: Vex.Flow.FretHandFinger = new Vex.Flow.FretHandFinger(technicalInstruction.value);
  901. fretFinger.setPosition(modifierPosition);
  902. if (fingeringPosition === PlacementEnum.Above || fingeringPosition === PlacementEnum.Below) {
  903. const offsetYSign: number = fingeringPosition === PlacementEnum.Above ? -1 : 1; // minus y is up
  904. const ordering: number = fingeringPosition === PlacementEnum.Above ? i :
  905. technicalInstructions.length - 1 - i; // reverse order for fingerings below staff
  906. if (EngravingRules.Rules.FingeringInsideStafflines && fingeringsCount > 1) { // y-shift for single fingering is ok
  907. // experimental, bounding boxes wrong for fretFinger above/below, better would be creating Labels
  908. // set y-shift. vexflow fretfinger simply places directly above/below note
  909. const perFingeringShift: number = fretFinger.getWidth() / 2;
  910. const shiftCount: number = fingeringsCount * 2.5;
  911. (<any>fretFinger).setOffsetY(offsetYSign * (ordering + shiftCount) * perFingeringShift);
  912. } else if (!EngravingRules.Rules.FingeringInsideStafflines) { // use StringNumber for placement above/below stafflines
  913. const stringNumber: Vex.Flow.StringNumber = new Vex.Flow.StringNumber(technicalInstruction.value);
  914. (<any>stringNumber).radius = 0; // hack to remove the circle around the number
  915. stringNumber.setPosition(modifierPosition);
  916. stringNumber.setOffsetY(offsetYSign * ordering * stringNumber.getWidth() * 2 / 3);
  917. vexFlowVoiceEntry.vfStaveNote.addModifier(i, stringNumber);
  918. continue;
  919. }
  920. }
  921. vexFlowVoiceEntry.vfStaveNote.addModifier(i, fretFinger);
  922. }
  923. }
  924. /**
  925. * Creates a line from 'top' to this measure, of type 'lineType'
  926. * @param top
  927. * @param lineType
  928. */
  929. public lineTo(top: VexFlowMeasure, lineType: any): void {
  930. const connector: StaveConnector = new Vex.Flow.StaveConnector(top.getVFStave(), this.stave);
  931. connector.setType(lineType);
  932. this.connectors.push(connector);
  933. }
  934. /**
  935. * Return the VexFlow Stave corresponding to this graphicalMeasure
  936. * @returns {Vex.Flow.Stave}
  937. */
  938. public getVFStave(): Vex.Flow.Stave {
  939. return this.stave;
  940. }
  941. /**
  942. * After re-running the formatting on the VexFlow Stave, update the
  943. * space needed by Instructions (in VexFlow: StaveModifiers)
  944. */
  945. private updateInstructionWidth(): void {
  946. let vfBeginInstructionsWidth: number = 0;
  947. let vfEndInstructionsWidth: number = 0;
  948. const modifiers: Vex.Flow.StaveModifier[] = this.stave.getModifiers();
  949. for (const mod of modifiers) {
  950. if (mod.getPosition() === StavePositionEnum.BEGIN) { //Vex.Flow.StaveModifier.Position.BEGIN) {
  951. vfBeginInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  952. } else if (mod.getPosition() === StavePositionEnum.END) { //Vex.Flow.StaveModifier.Position.END) {
  953. vfEndInstructionsWidth += mod.getWidth() + mod.getPadding(undefined);
  954. }
  955. }
  956. this.beginInstructionsWidth = vfBeginInstructionsWidth / unitInPixels;
  957. this.endInstructionsWidth = vfEndInstructionsWidth / unitInPixels;
  958. }
  959. }
  960. // Gives the position of the Stave - replaces the function get Position() in the description of class StaveModifier in vexflow.d.ts
  961. // The latter gave an error because function cannot be defined in the class descriptions in vexflow.d.ts
  962. export enum StavePositionEnum {
  963. LEFT = 1,
  964. RIGHT = 2,
  965. ABOVE = 3,
  966. BELOW = 4,
  967. BEGIN = 5,
  968. END = 6
  969. }