AccidentalCalculator.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {AccidentalEnum} from "../../Common/DataObjects/Pitch";
  2. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  3. import {GraphicalNote} from "./GraphicalNote";
  4. import {Pitch} from "../../Common/DataObjects/Pitch";
  5. import {NoteEnum} from "../../Common/DataObjects/Pitch";
  6. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  7. import { MusicSheetCalculator } from "./MusicSheetCalculator";
  8. /**
  9. * Compute the accidentals for notes according to the current key instruction
  10. */
  11. export class AccidentalCalculator {
  12. private keySignatureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  13. private currentAlterationsComparedToKeyInstructionList: number[] = [];
  14. private currentInMeasureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  15. private activeKeyInstruction: KeyInstruction;
  16. public get ActiveKeyInstruction(): KeyInstruction {
  17. return this.activeKeyInstruction;
  18. }
  19. public set ActiveKeyInstruction(value: KeyInstruction) {
  20. this.activeKeyInstruction = value;
  21. this.reactOnKeyInstructionChange();
  22. }
  23. /**
  24. * This method is called after each Measure
  25. */
  26. public doCalculationsAtEndOfMeasure(): void {
  27. this.currentInMeasureNoteAlterationsDict.clear();
  28. for (const key of this.keySignatureNoteAlterationsDict.keys()) {
  29. this.currentInMeasureNoteAlterationsDict.setValue(key, this.keySignatureNoteAlterationsDict.getValue(key));
  30. }
  31. }
  32. public checkAccidental(graphicalNote: GraphicalNote, pitch: Pitch): void {
  33. if (pitch === undefined) {
  34. return;
  35. }
  36. const pitchKey: number = <number>pitch.FundamentalNote + pitch.Octave * 12;
  37. /*let pitchKeyGivenInMeasureDict: boolean = this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey);
  38. if (
  39. (pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  40. || (!pitchKeyGivenInMeasureDict && pitch.Accidental !== AccidentalEnum.NONE)
  41. ) {
  42. if (this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) === -1) {
  43. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  44. }
  45. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  46. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  47. } else if (
  48. this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) !== -1
  49. && ((pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  50. || (!pitchKeyGivenInMeasureDict && pitch.Accidental === AccidentalEnum.NONE))
  51. ) {
  52. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  53. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  54. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  55. }*/
  56. const isInCurrentAlterationsToKeyList: boolean = this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) >= 0;
  57. if (this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey)) {
  58. if (isInCurrentAlterationsToKeyList) {
  59. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  60. }
  61. if (this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) {
  62. if (this.keySignatureNoteAlterationsDict.containsKey(pitchKey) &&
  63. this.keySignatureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) {
  64. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  65. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  66. } else {
  67. this.currentInMeasureNoteAlterationsDict.remove(pitchKey);
  68. }
  69. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  70. }
  71. } else {
  72. if (pitch.Accidental !== AccidentalEnum.NONE) {
  73. if (!isInCurrentAlterationsToKeyList) {
  74. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  75. }
  76. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  77. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  78. } else {
  79. if (isInCurrentAlterationsToKeyList) {
  80. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  81. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  82. }
  83. }
  84. }
  85. }
  86. private reactOnKeyInstructionChange(): void {
  87. const noteEnums: NoteEnum[] = KeyInstruction.getNoteEnumList(this.activeKeyInstruction);
  88. let keyAccidentalType: AccidentalEnum;
  89. if (this.activeKeyInstruction.Key > 0) {
  90. keyAccidentalType = AccidentalEnum.SHARP;
  91. } else {
  92. keyAccidentalType = AccidentalEnum.FLAT;
  93. }
  94. this.keySignatureNoteAlterationsDict.clear();
  95. this.currentAlterationsComparedToKeyInstructionList.length = 0;
  96. for (let octave: number = -9; octave < 9; octave++) {
  97. for (let i: number = 0; i < noteEnums.length; i++) {
  98. this.keySignatureNoteAlterationsDict.setValue(<number>noteEnums[i] + octave * 12, keyAccidentalType);
  99. }
  100. }
  101. this.doCalculationsAtEndOfMeasure();
  102. }
  103. }