AccidentalCalculator.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {IGraphicalSymbolFactory} from "../Interfaces/IGraphicalSymbolFactory";
  2. import {AccidentalEnum} from "../../Common/DataObjects/pitch";
  3. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  4. import {GraphicalNote} from "./GraphicalNote";
  5. import {Pitch} from "../../Common/DataObjects/pitch";
  6. import {NoteEnum} from "../../Common/DataObjects/pitch";
  7. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  8. export class AccidentalCalculator {
  9. private symbolFactory: IGraphicalSymbolFactory;
  10. private keySignatureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  11. private currentAlterationsComparedToKeyInstructionDict: number[] = [];
  12. private currentInMeasureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  13. private activeKeyInstruction: KeyInstruction;
  14. constructor(symbolFactory: IGraphicalSymbolFactory) {
  15. this.symbolFactory = symbolFactory;
  16. }
  17. public get ActiveKeyInstruction(): KeyInstruction {
  18. return this.activeKeyInstruction;
  19. }
  20. public set ActiveKeyInstruction(value: KeyInstruction) {
  21. this.activeKeyInstruction = value;
  22. this.reactOnKeyInstructionChange();
  23. }
  24. public doCalculationsAtEndOfMeasure(): void {
  25. this.currentInMeasureNoteAlterationsDict.clear();
  26. for (let key of this.keySignatureNoteAlterationsDict.keys()) {
  27. this.currentInMeasureNoteAlterationsDict.setValue(key, this.keySignatureNoteAlterationsDict.getValue(key));
  28. }
  29. }
  30. public checkAccidental(graphicalNote: GraphicalNote, pitch: Pitch, grace: boolean, graceScalingFactor: number): void {
  31. if (pitch === undefined) {
  32. return;
  33. }
  34. let pitchKey: number = <number>pitch.FundamentalNote + pitch.Octave * 12;
  35. let pitchKeyGivenInMeasureDict: boolean = this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey);
  36. if (
  37. (pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  38. || (!pitchKeyGivenInMeasureDict && pitch.Accidental !== AccidentalEnum.NONE)
  39. ) {
  40. if (this.currentAlterationsComparedToKeyInstructionDict.indexOf(pitchKey) === -1) {
  41. this.currentAlterationsComparedToKeyInstructionDict.push(pitchKey);
  42. }
  43. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  44. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch, grace, graceScalingFactor);
  45. } else if (
  46. this.currentAlterationsComparedToKeyInstructionDict.indexOf(pitchKey) !== -1
  47. && ((pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  48. || (!pitchKeyGivenInMeasureDict && pitch.Accidental === AccidentalEnum.NONE))
  49. ) {
  50. delete this.currentAlterationsComparedToKeyInstructionDict[pitchKey];
  51. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  52. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch, grace, graceScalingFactor);
  53. }
  54. }
  55. private reactOnKeyInstructionChange(): void {
  56. let noteEnums: NoteEnum[] = KeyInstruction.getNoteEnumList(this.activeKeyInstruction);
  57. let keyAccidentalType: AccidentalEnum;
  58. if (this.activeKeyInstruction.Key > 0) {
  59. keyAccidentalType = AccidentalEnum.SHARP;
  60. } else {
  61. keyAccidentalType = AccidentalEnum.FLAT;
  62. }
  63. this.keySignatureNoteAlterationsDict.clear();
  64. this.currentAlterationsComparedToKeyInstructionDict.length = 0;
  65. for (let octave: number = -9; octave < 9; octave++) {
  66. for (let i: number = 0; i < noteEnums.length; i++) {
  67. this.keySignatureNoteAlterationsDict.setValue(<number>noteEnums[i] + octave * 12, keyAccidentalType);
  68. }
  69. }
  70. this.doCalculationsAtEndOfMeasure();
  71. }
  72. }