123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import {AbstractNotationInstruction} from "./AbstractNotationInstruction";
- import {SourceStaffEntry} from "../SourceStaffEntry";
- import {NoteEnum} from "../../../Common/DataObjects/pitch";
- import {AccidentalEnum} from "../../../Common/DataObjects/pitch";
- import {Pitch} from "../../../Common/DataObjects/pitch";
- export class KeyInstruction extends AbstractNotationInstruction {
- constructor(first: SourceStaffEntry|KeyInstruction, key?: number, mode?: KeyEnum) {
- if (first === undefined) {
- super(undefined); // FIXME check
- this.Key = key;
- this.mode = mode;
- }
- if (first instanceof SourceStaffEntry) {
- let parent: SourceStaffEntry = <SourceStaffEntry> first;
- super(parent);
- this.Key = key;
- this.mode = mode;
- }
- if (first instanceof KeyInstruction) {
- let keyInstruction: KeyInstruction = <KeyInstruction> first;
- super(undefined); // FIXME check
- this(keyInstruction.parent, keyInstruction.keyType, keyInstruction.mode);
- this.keyType = keyInstruction.keyType;
- this.mode = keyInstruction.mode;
- }
- }
- private static sharpPositionList: NoteEnum[] = [NoteEnum.F, NoteEnum.C, NoteEnum.G, NoteEnum.D, NoteEnum.A, NoteEnum.E, NoteEnum.B];
- private static flatPositionList: NoteEnum[] = [NoteEnum.B, NoteEnum.E, NoteEnum.A, NoteEnum.D, NoteEnum.G, NoteEnum.C, NoteEnum.F];
- private keyType: number;
- private mode: KeyEnum;
- public static getNoteEnumList(instruction: KeyInstruction): NoteEnum[] {
- let enums: NoteEnum[] = [];
- if (instruction.keyType > 0) {
- for (let i: number = 0; i < instruction.keyType; i++) {
- enums.push(KeyInstruction.sharpPositionList[i]);
- }
- }
- if (instruction.keyType < 0) {
- for (let i: number = 0; i < Math.abs(instruction.keyType); i++) {
- enums.push(KeyInstruction.flatPositionList[i]);
- }
- }
- return enums;
- }
- public static getAllPossibleMajorKeyInstructions(): KeyInstruction[] {
- let keyInstructionList: KeyInstruction[] = [];
- for (let keyType: number = -7; keyType < 7; keyType++) {
- let currentKeyInstruction: KeyInstruction = new KeyInstruction(undefined, keyType, KeyEnum.major);
- keyInstructionList.push(currentKeyInstruction);
- }
- return keyInstructionList;
- }
- public get Key(): number {
- return this.keyType;
- }
- public set Key(value: number) {
- this.keyType = value;
- }
- public get Mode(): KeyEnum {
- return this.mode;
- }
- public set Mode(value: KeyEnum) {
- this.mode = value;
- }
- public getFundamentalNotesOfAccidentals(): NoteEnum[] {
- let noteList: NoteEnum[] = [];
- if (this.keyType > 0) {
- for (let i: number = 0; i < this.keyType; i++) {
- noteList.push(KeyInstruction.sharpPositionList[i]);
- }
- } else if (this.keyType < 0) {
- for (let i: number = 0; i < -this.keyType; i++) {
- noteList.push(KeyInstruction.flatPositionList[i]);
- }
- }
- return noteList;
- }
- public getAlterationForPitch(pitch: Pitch): AccidentalEnum {
- if (this.keyType > 0 && KeyInstruction.sharpPositionList.indexOf(pitch.FundamentalNote) <= this.keyType) {
- return AccidentalEnum.SHARP;
- } else if (this.keyType < 0 && KeyInstruction.flatPositionList.indexOf(pitch.FundamentalNote) <= Math.abs(this.keyType)) {
- return AccidentalEnum.FLAT;
- }
- return AccidentalEnum.NONE;
- }
- public ToString(): string {
- return "Key: " + this.keyType + "" + this.mode;
- }
- public OperatorEquals(key2: KeyInstruction): boolean {
- let key1: KeyInstruction = this;
- if (key1 === key2) {
- return true;
- }
- if ((key1 === undefined) || (key2 === undefined)) {
- return false;
- }
- return (key1.Key === key2.Key && key1.Mode === key2.Mode);
- }
- public OperatorNotEqual(key2: KeyInstruction): boolean {
- return !(this.OperatorEquals(key2));
- }
- }
- export class NoteEnumToHalfToneLink {
- constructor(note: NoteEnum, halftone: number) {
- this.note = note;
- this.halfTone = halftone;
- }
- public note: NoteEnum;
- public halfTone: number;
- }
- export enum KeyEnum {
- major = 0,
- minor = 1,
- none = 2,
- }
|