import {AccidentalEnum} from "../../Common/DataObjects/Pitch"; import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction"; import {GraphicalNote} from "./GraphicalNote"; import {Pitch} from "../../Common/DataObjects/Pitch"; import {NoteEnum} from "../../Common/DataObjects/Pitch"; import Dictionary from "typescript-collections/dist/lib/Dictionary"; import { MusicSheetCalculator } from "./MusicSheetCalculator"; /** * Compute the accidentals for notes according to the current key instruction */ export class AccidentalCalculator { private keySignatureNoteAlterationsDict: Dictionary = new Dictionary(); private currentAlterationsComparedToKeyInstructionList: number[] = []; private currentInMeasureNoteAlterationsDict: Dictionary = new Dictionary(); private activeKeyInstruction: KeyInstruction; public get ActiveKeyInstruction(): KeyInstruction { return this.activeKeyInstruction; } public set ActiveKeyInstruction(value: KeyInstruction) { this.activeKeyInstruction = value; this.reactOnKeyInstructionChange(); } /** * This method is called after each Measure */ public doCalculationsAtEndOfMeasure(): void { this.currentInMeasureNoteAlterationsDict.clear(); for (const key of this.keySignatureNoteAlterationsDict.keys()) { this.currentInMeasureNoteAlterationsDict.setValue(key, this.keySignatureNoteAlterationsDict.getValue(key)); } } public checkAccidental(graphicalNote: GraphicalNote, pitch: Pitch): void { if (pitch === undefined) { return; } const pitchKey: number = pitch.FundamentalNote + pitch.Octave * 12; /*let pitchKeyGivenInMeasureDict: boolean = this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey); if ( (pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) || (!pitchKeyGivenInMeasureDict && pitch.Accidental !== AccidentalEnum.NONE) ) { if (this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) === -1) { this.currentAlterationsComparedToKeyInstructionList.push(pitchKey); } this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental); this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch); } else if ( this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) !== -1 && ((pitchKeyGivenInMeasureDict && this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) || (!pitchKeyGivenInMeasureDict && pitch.Accidental === AccidentalEnum.NONE)) ) { this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1); this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental); this.symbolFactory.addGraphicalAccidental(graphicalNote, pitch); }*/ const isInCurrentAlterationsToKeyList: boolean = this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey) >= 0; if (this.currentInMeasureNoteAlterationsDict.containsKey(pitchKey)) { if (isInCurrentAlterationsToKeyList) { this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1); } if (this.currentInMeasureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) { if (this.keySignatureNoteAlterationsDict.containsKey(pitchKey) && this.keySignatureNoteAlterationsDict.getValue(pitchKey) !== pitch.Accidental) { this.currentAlterationsComparedToKeyInstructionList.push(pitchKey); this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental); } else { this.currentInMeasureNoteAlterationsDict.remove(pitchKey); } MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch); } } else { if (pitch.Accidental !== AccidentalEnum.NONE) { if (!isInCurrentAlterationsToKeyList) { this.currentAlterationsComparedToKeyInstructionList.push(pitchKey); } this.currentInMeasureNoteAlterationsDict.setValue(pitchKey, pitch.Accidental); MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch); } else { if (isInCurrentAlterationsToKeyList) { this.currentAlterationsComparedToKeyInstructionList.splice(this.currentAlterationsComparedToKeyInstructionList.indexOf(pitchKey), 1); MusicSheetCalculator.symbolFactory.addGraphicalAccidental(graphicalNote, pitch); } } } } private reactOnKeyInstructionChange(): void { const noteEnums: NoteEnum[] = KeyInstruction.getNoteEnumList(this.activeKeyInstruction); let keyAccidentalType: AccidentalEnum; if (this.activeKeyInstruction.Key > 0) { keyAccidentalType = AccidentalEnum.SHARP; } else { keyAccidentalType = AccidentalEnum.FLAT; } this.keySignatureNoteAlterationsDict.clear(); this.currentAlterationsComparedToKeyInstructionList.length = 0; for (let octave: number = -9; octave < 9; octave++) { for (let i: number = 0; i < noteEnums.length; i++) { this.keySignatureNoteAlterationsDict.setValue(noteEnums[i] + octave * 12, keyAccidentalType); } } this.doCalculationsAtEndOfMeasure(); } }