Fix chord & note issues
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { type Editor } from "../main";
|
||||
import { freqToMidi, resolvePitchBend, safeScale } from "zifferjs";
|
||||
import { freqToMidi, chord as parseChord, noteNameToMidi, resolvePitchBend, safeScale } from "zifferjs";
|
||||
import { SkipEvent } from "./SkipEvent";
|
||||
|
||||
export type EventOperation<T> = (instance: T, ...args: any[]) => void;
|
||||
|
||||
@ -310,6 +311,49 @@ export abstract class AudibleEvent extends AbstractEvent {
|
||||
return this;
|
||||
};
|
||||
|
||||
protected updateValue<T>(
|
||||
key: string,
|
||||
value: T | T[] | null
|
||||
): this {
|
||||
if (value == null) return this;
|
||||
this.values[key] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public note = (value: number | string | null, ...kwargs: number[]|string[]) => {
|
||||
if (typeof value === "string") {
|
||||
const parsedNote = noteNameToMidi(value);
|
||||
return this.updateValue("note", [parsedNote, ...kwargs].flat(Infinity));
|
||||
} else if (typeof value == null || value == undefined) {
|
||||
return new SkipEvent();
|
||||
} else {
|
||||
return this.updateValue("note", [value, ...kwargs].flat(Infinity));
|
||||
}
|
||||
};
|
||||
|
||||
public chord = (value: number|string, ...kwargs: number[]) => {
|
||||
if(typeof value === "string") {
|
||||
const chord = parseChord(value);
|
||||
return this.updateValue("note", chord);
|
||||
} else {
|
||||
const chord = [value, ...kwargs].flat(Infinity);
|
||||
return this.updateValue("note", chord);
|
||||
}
|
||||
};
|
||||
|
||||
public invert = (howMany: number = 0) => {
|
||||
if (this.values.note) {
|
||||
let notes = [...this.values.note];
|
||||
notes = howMany < 0 ? [...notes].reverse() : notes;
|
||||
for (let i = 0; i < Math.abs(howMany); i++) {
|
||||
notes[i % notes.length] += howMany <= 0 ? -12 : 12;
|
||||
}
|
||||
return this.updateValue("note", notes);
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
freq = (value: number | number[], ...kwargs: number[]): this => {
|
||||
/*
|
||||
* This function is used to set the frequency of the Event.
|
||||
|
||||
Reference in New Issue
Block a user