KeyInstruction.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {AbstractNotationInstruction} from "./AbstractNotationInstruction";
  2. import {SourceStaffEntry} from "../SourceStaffEntry";
  3. import {NoteEnum} from "../../../Common/DataObjects/pitch";
  4. import {AccidentalEnum} from "../../../Common/DataObjects/pitch";
  5. import {Pitch} from "../../../Common/DataObjects/pitch";
  6. export class KeyInstruction extends AbstractNotationInstruction {
  7. constructor(first: SourceStaffEntry|KeyInstruction, key?: number, mode?: KeyEnum) {
  8. if (first === undefined) {
  9. super(undefined); // FIXME check
  10. this.Key = key;
  11. this.mode = mode;
  12. }
  13. if (first instanceof SourceStaffEntry) {
  14. let parent: SourceStaffEntry = <SourceStaffEntry> first;
  15. super(parent);
  16. this.Key = key;
  17. this.mode = mode;
  18. }
  19. if (first instanceof KeyInstruction) {
  20. let keyInstruction: KeyInstruction = <KeyInstruction> first;
  21. super(undefined); // FIXME check
  22. this(keyInstruction.parent, keyInstruction.keyType, keyInstruction.mode);
  23. this.keyType = keyInstruction.keyType;
  24. this.mode = keyInstruction.mode;
  25. }
  26. }
  27. private static sharpPositionList: NoteEnum[] = [NoteEnum.F, NoteEnum.C, NoteEnum.G, NoteEnum.D, NoteEnum.A, NoteEnum.E, NoteEnum.B];
  28. private static flatPositionList: NoteEnum[] = [NoteEnum.B, NoteEnum.E, NoteEnum.A, NoteEnum.D, NoteEnum.G, NoteEnum.C, NoteEnum.F];
  29. private keyType: number;
  30. private mode: KeyEnum;
  31. public static getNoteEnumList(instruction: KeyInstruction): NoteEnum[] {
  32. let enums: NoteEnum[] = [];
  33. if (instruction.keyType > 0) {
  34. for (let i: number = 0; i < instruction.keyType; i++) {
  35. enums.push(KeyInstruction.sharpPositionList[i]);
  36. }
  37. }
  38. if (instruction.keyType < 0) {
  39. for (let i: number = 0; i < Math.abs(instruction.keyType); i++) {
  40. enums.push(KeyInstruction.flatPositionList[i]);
  41. }
  42. }
  43. return enums;
  44. }
  45. public static getAllPossibleMajorKeyInstructions(): KeyInstruction[] {
  46. let keyInstructionList: KeyInstruction[] = [];
  47. for (let keyType: number = -7; keyType < 7; keyType++) {
  48. let currentKeyInstruction: KeyInstruction = new KeyInstruction(undefined, keyType, KeyEnum.major);
  49. keyInstructionList.push(currentKeyInstruction);
  50. }
  51. return keyInstructionList;
  52. }
  53. public get Key(): number {
  54. return this.keyType;
  55. }
  56. public set Key(value: number) {
  57. this.keyType = value;
  58. }
  59. public get Mode(): KeyEnum {
  60. return this.mode;
  61. }
  62. public set Mode(value: KeyEnum) {
  63. this.mode = value;
  64. }
  65. public getFundamentalNotesOfAccidentals(): NoteEnum[] {
  66. let noteList: NoteEnum[] = [];
  67. if (this.keyType > 0) {
  68. for (let i: number = 0; i < this.keyType; i++) {
  69. noteList.push(KeyInstruction.sharpPositionList[i]);
  70. }
  71. } else if (this.keyType < 0) {
  72. for (let i: number = 0; i < -this.keyType; i++) {
  73. noteList.push(KeyInstruction.flatPositionList[i]);
  74. }
  75. }
  76. return noteList;
  77. }
  78. public getAlterationForPitch(pitch: Pitch): AccidentalEnum {
  79. if (this.keyType > 0 && KeyInstruction.sharpPositionList.indexOf(pitch.FundamentalNote) <= this.keyType) {
  80. return AccidentalEnum.SHARP;
  81. } else if (this.keyType < 0 && KeyInstruction.flatPositionList.indexOf(pitch.FundamentalNote) <= Math.abs(this.keyType)) {
  82. return AccidentalEnum.FLAT;
  83. }
  84. return AccidentalEnum.NONE;
  85. }
  86. public ToString(): string {
  87. return "Key: " + this.keyType + "" + this.mode;
  88. }
  89. public OperatorEquals(key2: KeyInstruction): boolean {
  90. let key1: KeyInstruction = this;
  91. if (key1 === key2) {
  92. return true;
  93. }
  94. if ((key1 === undefined) || (key2 === undefined)) {
  95. return false;
  96. }
  97. return (key1.Key === key2.Key && key1.Mode === key2.Mode);
  98. }
  99. public OperatorNotEqual(key2: KeyInstruction): boolean {
  100. return !(this.OperatorEquals(key2));
  101. }
  102. }
  103. export class NoteEnumToHalfToneLink {
  104. constructor(note: NoteEnum, halftone: number) {
  105. this.note = note;
  106. this.halfTone = halftone;
  107. }
  108. public note: NoteEnum;
  109. public halfTone: number;
  110. }
  111. export enum KeyEnum {
  112. major = 0,
  113. minor = 1,
  114. none = 2,
  115. }