VexFlowGraphicalNote.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Vex = require("vexflow");
  2. import {GraphicalNote} from "../GraphicalNote";
  3. import {Note} from "../../VoiceData/Note";
  4. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  5. import {VexFlowConverter} from "./VexFlowConverter";
  6. import {Pitch} from "../../../Common/DataObjects/Pitch";
  7. import {Fraction} from "../../../Common/DataObjects/Fraction";
  8. import {OctaveEnum, OctaveShift} from "../../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  9. import { GraphicalVoiceEntry } from "../GraphicalVoiceEntry";
  10. /**
  11. * The VexFlow version of a [[GraphicalNote]].
  12. */
  13. export class VexFlowGraphicalNote extends GraphicalNote {
  14. constructor(note: Note, parent: GraphicalVoiceEntry, activeClef: ClefInstruction,
  15. octaveShift: OctaveEnum = OctaveEnum.NONE, graphicalNoteLength: Fraction = undefined) {
  16. super(note, parent, graphicalNoteLength);
  17. this.clef = activeClef;
  18. this.octaveShift = octaveShift;
  19. if (note.Pitch) {
  20. // TODO: Maybe shift to Transpose function when available
  21. const drawPitch: Pitch = OctaveShift.getPitchFromOctaveShift(note.Pitch, octaveShift);
  22. this.vfpitch = VexFlowConverter.pitch(this, drawPitch);
  23. this.vfpitch[1] = undefined;
  24. }
  25. }
  26. public octaveShift: OctaveEnum;
  27. // The pitch of this note as given by VexFlowConverter.pitch
  28. public vfpitch: [string, string, ClefInstruction];
  29. // The corresponding VexFlow StaveNote (plus its index in the chord)
  30. public vfnote: [Vex.Flow.StaveNote, number];
  31. // The current clef
  32. private clef: ClefInstruction;
  33. /**
  34. * Update the pitch of this note. Necessary in order to display accidentals correctly.
  35. * This is called by VexFlowGraphicalSymbolFactory.addGraphicalAccidental.
  36. * @param pitch
  37. */
  38. public setPitch(pitch: Pitch): void {
  39. if (this.vfnote) {
  40. const acc: string = Pitch.accidentalVexflow(pitch.Accidental);
  41. if (acc) {
  42. alert(acc);
  43. this.vfnote[0].addAccidental(this.vfnote[1], new Vex.Flow.Accidental(acc));
  44. }
  45. } else {
  46. // revert octave shift, as the placement of the note is independent of octave brackets
  47. const drawPitch: Pitch = OctaveShift.getPitchFromOctaveShift(pitch, this.octaveShift);
  48. this.vfpitch = VexFlowConverter.pitch(this, drawPitch);
  49. }
  50. }
  51. /**
  52. * Set the VexFlow StaveNote corresponding to this GraphicalNote, together with its index in the chord.
  53. * @param note
  54. * @param index
  55. */
  56. public setIndex(note: Vex.Flow.StaveNote, index: number): void {
  57. this.vfnote = [note, index];
  58. }
  59. /**
  60. * Gets the clef for this note
  61. */
  62. public Clef(): ClefInstruction {
  63. return this.clef;
  64. }
  65. }