VexflowVibratoBracket.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { WavyLine } from "../../VoiceData/Expressions/ContinuousExpressions/WavyLine";
  2. import { BoundingBox } from "../BoundingBox";
  3. import { GraphicalStaffEntry } from "../GraphicalStaffEntry";
  4. import { GraphicalWavyLine } from "../GraphicalWavyLine";
  5. import { VexFlowVoiceEntry } from "./VexFlowVoiceEntry";
  6. import Vex from "vexflow";
  7. export class VexflowVibratoBracket extends GraphicalWavyLine {
  8. /** Defines the note where the pedal starts */
  9. public startNote: Vex.Flow.StemmableNote;
  10. /** Defines the note where the pedal ends */
  11. public endNote: Vex.Flow.StemmableNote;
  12. public startVfVoiceEntry: VexFlowVoiceEntry;
  13. public endVfVoiceEntry: VexFlowVoiceEntry;
  14. //Line where vexflow renders the bracket. VF default is 1
  15. public line: number = 1;
  16. private isVibrato: boolean = false;
  17. private toEndOfStopStave: boolean = false;
  18. public get ToEndOfStopStave(): boolean {
  19. return this.toEndOfStopStave;
  20. }
  21. constructor(wavyLine: WavyLine, parentBBox: BoundingBox, tabVibrato: boolean = false) {
  22. super(wavyLine, parentBBox);
  23. this.isVibrato = tabVibrato;
  24. }
  25. /**
  26. * Set a start note using a staff entry
  27. * @param graphicalStaffEntry the staff entry that holds the start note
  28. */
  29. public setStartNote(graphicalStaffEntry: GraphicalStaffEntry): boolean {
  30. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  31. const vve: VexFlowVoiceEntry = (gve as VexFlowVoiceEntry);
  32. if (vve?.vfStaveNote) {
  33. this.startNote = vve.vfStaveNote;
  34. this.startVfVoiceEntry = vve;
  35. return true;
  36. }
  37. }
  38. return false; // couldn't find a startNote
  39. }
  40. /**
  41. * Set an end note using a staff entry
  42. * @param graphicalStaffEntry the staff entry that holds the end note
  43. */
  44. public setEndNote(graphicalStaffEntry: GraphicalStaffEntry): boolean {
  45. // this is duplicate code from setStartNote, but if we make one general method, we add a lot of branching.
  46. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  47. const vve: VexFlowVoiceEntry = (gve as VexFlowVoiceEntry);
  48. if (vve?.vfStaveNote) {
  49. this.endNote = vve.vfStaveNote;
  50. this.endVfVoiceEntry = vve;
  51. const parentMeasureStaffEntries: GraphicalStaffEntry[] = this.endVfVoiceEntry.parentStaffEntry.parentMeasure.staffEntries;
  52. const lastStaffEntry: GraphicalStaffEntry = parentMeasureStaffEntries[parentMeasureStaffEntries.length - 1];
  53. //If this is the last staff entry of the stave (measure), render line to end of measure
  54. this.toEndOfStopStave = (lastStaffEntry === this.endVfVoiceEntry.parentStaffEntry);
  55. return true;
  56. }
  57. }
  58. return false; // couldn't find an endNote
  59. }
  60. public CalculateBoundingBox(): void {
  61. const vfBracket: any = this.getVibratoBracket();
  62. //Double the height of the wave, coverted to units
  63. this.boundingBox.Size.height = vfBracket.render_options.wave_height * 0.2;
  64. }
  65. public getVibratoBracket(): Vex.Flow.VibratoBracket {
  66. const bracket: Vex.Flow.VibratoBracket = new Vex.Flow.VibratoBracket({
  67. start: this.startNote,
  68. stop: this.endNote,
  69. toEndOfStopStave: this.toEndOfStopStave
  70. });
  71. bracket.setLine(this.line);
  72. if (this.isVibrato) {
  73. //Render options for vibrato style
  74. (bracket as any).render_options.vibrato_width = 20;
  75. } else {
  76. (bracket as any).render_options.wave_girth = 4;
  77. }
  78. return bracket;
  79. }
  80. }