VexFlowGraphicalNote.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 correctly
  35. * accidentals, this is called by VexFlowGraphicalSymbolFactory.addGraphicalAccidental.
  36. * @param pitch
  37. */
  38. public setPitch(pitch: Pitch): void {
  39. if (this.vfnote) {
  40. const acc: string = VexFlowConverter.accidental(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. this.vfpitch = VexFlowConverter.pitch(this, pitch);
  47. }
  48. }
  49. /**
  50. * Set the VexFlow StaveNote corresponding to this GraphicalNote, together with its index in the chord.
  51. * @param note
  52. * @param index
  53. */
  54. public setIndex(note: Vex.Flow.StaveNote, index: number): void {
  55. this.vfnote = [note, index];
  56. }
  57. /**
  58. * Gets the clef for this note
  59. */
  60. public Clef(): ClefInstruction {
  61. return this.clef;
  62. }
  63. }