GraphicalNote.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {Note} from "../VoiceData/Note";
  2. import {Fraction} from "../../Common/DataObjects/Fraction";
  3. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  4. import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
  5. import {OctaveEnum} from "../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  6. import {AccidentalEnum, Pitch} from "../../Common/DataObjects/Pitch";
  7. import {GraphicalObject} from "./GraphicalObject";
  8. import {MusicSheetCalculator} from "./MusicSheetCalculator";
  9. import {BoundingBox} from "./BoundingBox";
  10. import {GraphicalVoiceEntry} from "./GraphicalVoiceEntry";
  11. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  12. import { EngravingRules } from "./EngravingRules";
  13. /**
  14. * The graphical counterpart of a [[Note]]
  15. */
  16. export class GraphicalNote extends GraphicalObject {
  17. constructor(note: Note, parent: GraphicalVoiceEntry, rules: EngravingRules, graphicalNoteLength: Fraction = undefined) {
  18. super();
  19. this.sourceNote = note;
  20. this.parentVoiceEntry = parent;
  21. this.PositionAndShape = new BoundingBox(this, parent.PositionAndShape);
  22. if (graphicalNoteLength) {
  23. this.graphicalNoteLength = graphicalNoteLength;
  24. } else {
  25. this.graphicalNoteLength = note.Length;
  26. }
  27. this.numberOfDots = this.calculateNumberOfNeededDots(this.graphicalNoteLength);
  28. this.rules = rules;
  29. this.rules.addGraphicalNoteToNoteMap(note, this);
  30. }
  31. public sourceNote: Note;
  32. public DrawnAccidental: AccidentalEnum = AccidentalEnum.NONE;
  33. public graphicalNoteLength: Fraction;
  34. public parentVoiceEntry: GraphicalVoiceEntry;
  35. public numberOfDots: number;
  36. public rules: EngravingRules;
  37. public staffLine: number;
  38. public baseFingeringXOffset: number;
  39. public baseStringNumberXOffset: number;
  40. public lineShift: number = 0;
  41. public Transpose(keyInstruction: KeyInstruction, activeClef: ClefInstruction, halfTones: number, octaveEnum: OctaveEnum): Pitch {
  42. let transposedPitch: Pitch = this.sourceNote.Pitch;
  43. if (MusicSheetCalculator.transposeCalculator) {
  44. transposedPitch = MusicSheetCalculator.transposeCalculator.transposePitch(this.sourceNote.Pitch, keyInstruction, halfTones);
  45. }
  46. return transposedPitch;
  47. }
  48. /**
  49. * Return the number of dots needed to represent the given fraction.
  50. * @param fraction
  51. * @returns {number}
  52. */
  53. private calculateNumberOfNeededDots(fraction: Fraction): number {
  54. if (!this.sourceNote || !this.sourceNote.NoteTuplet) {
  55. return fraction.calculateNumberOfNeededDots();
  56. }
  57. return 0;
  58. }
  59. public get ParentMusicPage(): GraphicalMusicPage {
  60. return this.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentMusicSystem.Parent;
  61. }
  62. /** Get a GraphicalNote from a Note. Use osmd.rules as the second parameter (instance reference).
  63. * Also more easily available via osmd.rules.GNote(note). */
  64. public static FromNote(note: Note, rules: EngravingRules): GraphicalNote {
  65. return rules.NoteToGraphicalNoteMap.getValue(note.NoteToGraphicalNoteObjectId);
  66. }
  67. public ToStringShort(octaveOffset: number = 0): string {
  68. return this.sourceNote?.ToStringShort(octaveOffset);
  69. }
  70. public get ToStringShortGet(): string {
  71. return this.ToStringShort(0);
  72. }
  73. }