AccidentalCalculator.js 3.8 KB

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