VexFlowGraphicalNote.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, octaveShift: OctaveEnum = OctaveEnum.NONE, graphicalNoteLength: Fraction = undefined) {
  15. super(note, parent, graphicalNoteLength);
  16. this.clef = activeClef;
  17. if (note.Pitch) {
  18. this.vfpitch = VexFlowConverter.pitch(note.Pitch, this.clef);
  19. this.vfpitch[1] = undefined;
  20. }
  21. }
  22. // The pitch of this note as given by VexFlowConverter.pitch
  23. public vfpitch: [string, string, ClefInstruction];
  24. // The corresponding VexFlow StaveNote (plus its index in the chord)
  25. private vfnote: [Vex.Flow.StaveNote, number];
  26. // The current clef
  27. private clef: ClefInstruction;
  28. /**
  29. * Update the pitch of this note. Necessary in order to display correctly
  30. * accidentals, this is called by VexFlowGraphicalSymbolFactory.addGraphicalAccidental.
  31. * @param pitch
  32. */
  33. public setPitch(pitch: Pitch): void {
  34. if (this.vfnote) {
  35. let acc: string = VexFlowConverter.accidental(pitch.Accidental);
  36. if (acc) {
  37. alert(acc);
  38. this.vfnote[0].addAccidental(this.vfnote[1], new Vex.Flow.Accidental(acc));
  39. }
  40. } else {
  41. this.vfpitch = VexFlowConverter.pitch(pitch, this.clef);
  42. }
  43. }
  44. /**
  45. * Set the VexFlow StaveNote corresponding to this GraphicalNote, together with its index in the chord.
  46. * @param note
  47. * @param index
  48. */
  49. public setIndex(note: Vex.Flow.StaveNote, index: number): void {
  50. this.vfnote = [note, index];
  51. }
  52. }