Added and documented new scale methods: semitones, cents and ratios

This commit is contained in:
2023-12-12 23:54:42 +02:00
parent 0e8ef2ad75
commit 818e1a62ef
5 changed files with 91 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import {
} from "zifferjs";
import { SkipEvent } from "./SkipEvent";
import { SoundParams } from "./SoundEvent";
import { centsToSemitones, ratiosToSemitones } from "zifferjs/src/scale";
export type EventOperation<T> = (instance: T, ...args: any[]) => void;
@ -360,6 +361,36 @@ export abstract class AudibleEvent extends AbstractEvent {
return this;
};
updateKeyAndPitch() {
if (!this.values.key) this.values.key = 60;
if (!(this.values.pitch || this.values.pitch === 0)) this.values.pitch = 0;
}
semitones(values: number|number[], ...rest: number[]) {
const scaleValues = typeof values === "number" ? [values, ...rest] : values;
this.values.parsedScale = safeScale(scaleValues);
this.updateKeyAndPitch();
this.update();
return this;
}
steps = this.semitones;
cents(values: number|number[], ...rest: number[]) {
const scaleValues = typeof values === "number" ? [values, ...rest] : values;
this.values.parsedScale = safeScale(centsToSemitones(scaleValues));
this.updateKeyAndPitch();
this.update();
return this;
}
ratios(values: number|number[], ...rest: number[]) {
const scaleValues = typeof values === "number" ? [values, ...rest] : values;
this.values.parsedScale = safeScale(ratiosToSemitones(scaleValues));
this.updateKeyAndPitch();
this.update();
return this;
}
protected updateValue<T>(key: string, value: T | T[] | null): this {
if (value == null) return this;
this.values[key] = value;