GraphicalNote.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 {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. /**
  13. * The graphical counterpart of a [[Note]]
  14. */
  15. export class GraphicalNote extends GraphicalObject {
  16. constructor(note: Note, parent: GraphicalVoiceEntry, graphicalNoteLength: Fraction = undefined) {
  17. super();
  18. this.sourceNote = note;
  19. this.parentVoiceEntry = parent;
  20. this.PositionAndShape = new BoundingBox(this, parent.PositionAndShape);
  21. if (graphicalNoteLength !== undefined) {
  22. this.graphicalNoteLength = graphicalNoteLength;
  23. } else {
  24. this.graphicalNoteLength = note.Length;
  25. }
  26. this.numberOfDots = this.calculateNumberOfNeededDots(this.graphicalNoteLength);
  27. }
  28. public sourceNote: Note;
  29. public graphicalNoteLength: Fraction;
  30. public parentVoiceEntry: GraphicalVoiceEntry;
  31. public numberOfDots: number;
  32. public Transpose(keyInstruction: KeyInstruction, activeClef: ClefInstruction, halfTones: number, octaveEnum: OctaveEnum): Pitch {
  33. let transposedPitch: Pitch = this.sourceNote.Pitch;
  34. if (MusicSheetCalculator.transposeCalculator !== undefined) {
  35. transposedPitch = MusicSheetCalculator.transposeCalculator.transposePitch(this.sourceNote.Pitch, keyInstruction, halfTones);
  36. }
  37. return transposedPitch;
  38. }
  39. /**
  40. * Return the number of dots needed to represent the given fraction.
  41. * @param fraction
  42. * @returns {number}
  43. */
  44. private calculateNumberOfNeededDots(fraction: Fraction): number {
  45. let num: number = 1;
  46. let product: number = 2;
  47. const expandedNumerator: number = fraction.GetExpandedNumerator();
  48. if (this.sourceNote === undefined || this.sourceNote.NoteTuplet === undefined) {
  49. while (product < expandedNumerator) {
  50. num++;
  51. product = Math.pow(2, num);
  52. }
  53. }
  54. return Math.min(3, num - 1);
  55. }
  56. public get ParentMusicPage(): GraphicalMusicPage {
  57. return this.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentMusicSystem.Parent;
  58. }
  59. }