AccidentalCalculator.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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";
  7. // import { Dictionary } from "typescript-collections/dist/lib";
  8. import { MusicSheetCalculator } from "./MusicSheetCalculator";
  9. /**
  10. * Compute the accidentals for notes according to the current key instruction
  11. */
  12. export class AccidentalCalculator {
  13. private keySignatureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  14. private currentAlterationsComparedToKeyInstructionList: number[] = [];
  15. private currentInMeasureNoteAlterationsDict: Dictionary<number, AccidentalEnum> = new Dictionary<number, AccidentalEnum>();
  16. private activeKeyInstruction: KeyInstruction;
  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. /**
  25. * This method is called after each Measure
  26. * It clears the in-measure alterations dict for the next measure
  27. * and pre-loads with the alterations of the key signature
  28. */
  29. public doCalculationsAtEndOfMeasure(): void {
  30. this.currentInMeasureNoteAlterationsDict.clear();
  31. for (const key of this.keySignatureNoteAlterationsDict.keys()) {
  32. this.currentInMeasureNoteAlterationsDict.setValue(key, this.keySignatureNoteAlterationsDict.getValue(key));
  33. }
  34. }
  35. public checkAccidental(graphicalNote: GraphicalNote, pitch: Pitch): void {
  36. if (!pitch) {
  37. return;
  38. }
  39. const pitchKey: number = <number>pitch.FundamentalNote + pitch.Octave * 12;
  40. /*let pitchKeyGivenInMeasureDict: boolean = this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey);
  41. if (
  42. (pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  43. || (!pitchKeyGivenInMeasureDict && pitch.Accidental !== AccidentalEnum.NONE)
  44. ) {
  45. if (this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) === -1) {
  46. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  47. }
  48. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  49. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  50. } else if (
  51. this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) !== -1
  52. && ((pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental)
  53. || (!pitchKeyGivenInMeasureDict && pitch.Accidental === AccidentalEnum.NONE))
  54. ) {
  55. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  56. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental);
  57. this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  58. }*/
  59. const isInCurrentAlterationsToKeyList: boolean = this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) >= 0;
  60. if (this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey)) {
  61. if (isInCurrentAlterationsToKeyList) {
  62. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  63. }
  64. if (this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.AccidentalHalfTones) {
  65. if (this.keySignatureNoteAlterationsDict.containsKey(pitchKey) &&
  66. this.keySignatureNoteAlterationsDict.getValue(pitchKey) !== pitch.AccidentalHalfTones) {
  67. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  68. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.AccidentalHalfTones);
  69. } else {
  70. this.currentInMeasureNoteAlterationsDict.remove(pitchKey);
  71. }
  72. if (pitch.Accidental === AccidentalEnum.NONE) {
  73. // If an AccidentalEnum.NONE is given, it would not be rendered.
  74. // We need here to convert to a AccidentalEnum.NATURAL:
  75. pitch = new Pitch(pitch.FundamentalNote, pitch.Octave, AccidentalEnum.NATURAL);
  76. }
  77. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  78. }
  79. } else { // pitchkey not in measure dict:
  80. if (pitch.Accidental !== AccidentalEnum.NONE) {
  81. if (!isInCurrentAlterationsToKeyList) {
  82. this.currentAlterationsComparedToKeyInstructionList.push(pitchKey);
  83. }
  84. this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.AccidentalHalfTones);
  85. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  86. } else {
  87. if (isInCurrentAlterationsToKeyList) {
  88. // we need here a AccidentalEnum.NATURAL now to get it rendered - AccidentalEnum.NONE would not be rendered
  89. pitch = new Pitch(pitch.FundamentalNote, pitch.Octave, AccidentalEnum.NATURAL);
  90. this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1);
  91. MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch);
  92. }
  93. }
  94. }
  95. }
  96. private reactOnKeyInstructionChange(): void {
  97. const noteEnums: NoteEnum[] = this.activeKeyInstruction.AlteratedNotes;
  98. let keyAccidentalType: AccidentalEnum;
  99. if (this.activeKeyInstruction.Key > 0) {
  100. keyAccidentalType = AccidentalEnum.SHARP;
  101. } else {
  102. keyAccidentalType = AccidentalEnum.FLAT;
  103. }
  104. this.keySignatureNoteAlterationsDict.clear();
  105. this.currentAlterationsComparedToKeyInstructionList.length = 0;
  106. for (let octave: number = -9; octave < 9; octave++) {
  107. for (let i: number = 0; i < noteEnums.length; i++) {
  108. this.keySignatureNoteAlterationsDict.setValue(<number>noteEnums[i] + octave * 12, Pitch.HalfTonesFromAccidental(keyAccidentalType));
  109. }
  110. }
  111. this.doCalculationsAtEndOfMeasure();
  112. }
  113. }