GraphicalNote.ts 2.4 KB

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