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.
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { AudibleEvent } from "./AbstractEvents";
|
||||
import { type Editor } from "../main";
|
||||
import { MidiConnection } from "../IO/MidiConnection";
|
||||
import { noteFromPc, chord as parseChord } from "zifferjs";
|
||||
import { noteFromPc } from "zifferjs";
|
||||
import {
|
||||
filterObject,
|
||||
arrayOfObjectsToObjectWithArrays,
|
||||
@ -29,16 +29,6 @@ export class MidiEvent extends AudibleEvent {
|
||||
this.midiConnection = app.api.MidiConnection;
|
||||
}
|
||||
|
||||
public chord = (value: string) => {
|
||||
this.values.note = parseChord(value);
|
||||
return this;
|
||||
};
|
||||
|
||||
note = (value: number | number[]): this => {
|
||||
this.values["note"] = value;
|
||||
return this;
|
||||
};
|
||||
|
||||
sustain = (value: number | number[]): this => {
|
||||
this.values["sustain"] = value;
|
||||
return this;
|
||||
|
||||
@ -6,10 +6,8 @@ import {
|
||||
objectWithArraysToArrayOfObjects,
|
||||
} from "../Utils/Generic";
|
||||
import {
|
||||
chord as parseChord,
|
||||
midiToFreq,
|
||||
noteFromPc,
|
||||
noteNameToMidi,
|
||||
} from "zifferjs";
|
||||
|
||||
import {
|
||||
@ -36,15 +34,6 @@ export class SoundEvent extends AudibleEvent {
|
||||
nudge: number;
|
||||
sound: any;
|
||||
|
||||
public updateValue<T>(
|
||||
key: string,
|
||||
value: T | T[] | SoundParams[] | null,
|
||||
): this {
|
||||
if (value == null) return this;
|
||||
this.values[key] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
private static methodMap = {
|
||||
volume: ["volume", "vol"],
|
||||
zrand: ["zrand", "zr"],
|
||||
@ -453,38 +442,6 @@ export class SoundEvent extends AudibleEvent {
|
||||
this.values.freq = newArrays.freq;
|
||||
};
|
||||
|
||||
public chord = (value: string) => {
|
||||
const chord = parseChord(value);
|
||||
return this.updateValue("note", chord);
|
||||
};
|
||||
|
||||
public invert = (howMany: number = 0) => {
|
||||
if (this.values.chord) {
|
||||
let notes = this.values.chord.map(
|
||||
(obj: { [key: string]: number }) => obj.note,
|
||||
);
|
||||
notes = howMany < 0 ? [...notes].reverse() : notes;
|
||||
for (let i = 0; i < Math.abs(howMany); i++) {
|
||||
notes[i % notes.length] += howMany <= 0 ? -12 : 12;
|
||||
}
|
||||
const chord = notes.map((note: number) => {
|
||||
return { note: note, freq: midiToFreq(note) };
|
||||
});
|
||||
return this.updateValue("chord", chord);
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
public note = (value: number | string | null) => {
|
||||
if (typeof value === "string") {
|
||||
return this.updateValue("note", noteNameToMidi(value));
|
||||
} else if (typeof value == null || value == undefined) {
|
||||
return this.updateValue("note", 0).updateValue("gain", 0);
|
||||
} else {
|
||||
return this.updateValue("note", value);
|
||||
}
|
||||
};
|
||||
|
||||
out = (orbit?: number | number[]): void => {
|
||||
if (orbit) this.values["orbit"] = orbit;
|
||||
const events = objectWithArraysToArrayOfObjects(this.values, [
|
||||
|
||||
@ -2,6 +2,7 @@ import { type UserAPI } from "../API";
|
||||
import { MidiEvent } from "../classes/MidiEvent";
|
||||
import { Player } from "../classes/ZPlayer";
|
||||
import { SoundEvent } from "../classes/SoundEvent";
|
||||
import { SkipEvent } from "../classes/SkipEvent";
|
||||
|
||||
declare global {
|
||||
interface Number {
|
||||
@ -24,7 +25,7 @@ declare global {
|
||||
z15(): Player;
|
||||
z16(): Player;
|
||||
midi(): MidiEvent;
|
||||
sound(name: string): SoundEvent;
|
||||
sound(name: string): SoundEvent|SkipEvent;
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,11 +102,11 @@ export const makeNumberExtensions = (api: UserAPI) => {
|
||||
return api.midi(this.valueOf(), ...kwargs);
|
||||
};
|
||||
|
||||
Number.prototype.sound = function (name: string) {
|
||||
Number.prototype.sound = function (name: string): SoundEvent|SkipEvent {
|
||||
if (Number.isInteger(this.valueOf())) {
|
||||
return (api.sound(name) as SoundEvent).note(this.valueOf());
|
||||
} else {
|
||||
return (api.sound(name) as SoundEvent).freq(this.valueOf());
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user