VexFlowGraphicalNote.ts 2.2 KB

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