Fix for some keys and added some kwargs

This commit is contained in:
2023-11-03 17:09:53 +02:00
parent a09e3a76be
commit f39625a985
2 changed files with 62 additions and 23 deletions

View File

@ -7,6 +7,7 @@
* *
*/ */
export function objectWithArraysToArrayOfObjects(input: Record<string, any>, ignoredKeys: string[]): Record<string, any>[] { export function objectWithArraysToArrayOfObjects(input: Record<string, any>, ignoredKeys: string[]): Record<string, any>[] {
ignoredKeys = ignoredKeys.map((k) => Array.isArray(input[k]) ? undefined : k).filter((k) => k !== undefined) as string[];
const keys = Object.keys(input).filter((k) => !ignoredKeys.includes(k)); const keys = Object.keys(input).filter((k) => !ignoredKeys.includes(k));
const maxLength = Math.max( const maxLength = Math.max(
...keys.map((k) => ...keys.map((k) =>

View File

@ -2,9 +2,7 @@ import { type Editor } from "../main";
import { import {
freqToMidi, freqToMidi,
resolvePitchBend, resolvePitchBend,
getScale, safeScale
isScale,
parseScala,
} from "zifferjs"; } from "zifferjs";
export abstract class Event { export abstract class Event {
@ -204,11 +202,20 @@ export abstract class Event {
return this.modify(func); return this.modify(func);
}; };
noteLength = (value: number): Event => { noteLength = (value: number | number[], ...kwargs: number[]): Event => {
/** /**
* This function is used to set the note length of the Event. * This function is used to set the note length of the Event.
*/ */
this.values["noteLength"] = value; if(kwargs.length > 0) {
value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
}
if(Array.isArray(value)) {
this.values["noteLength"] = value;
this.values.dur = value.map((v) => this.app.clock.convertPulseToSecond(v*4*this.app.clock.ppqn));
} else {
this.values["noteLength"] = value;
this.values.dur = this.app.clock.convertPulseToSecond(value*4*this.app.clock.ppqn);
}
return this; return this;
}; };
} }
@ -218,12 +225,15 @@ export abstract class AudibleEvent extends Event {
super(app); super(app);
} }
pitch = (value: number): this => { pitch = (value: number | number[], ...kwargs: number[]): this => {
/* /*
* This function is used to set the pitch of the Event. * This function is used to set the pitch of the Event.
* @param value - The pitch value * @param value - The pitch value
* @returns The Event * @returns The Event
*/ */
if(kwargs.length > 0) {
value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
}
this.values["pitch"] = value; this.values["pitch"] = value;
if(this.values.key && this.values.parsedScale) this.update(); if(this.values.key && this.values.parsedScale) this.update();
return this; return this;
@ -231,57 +241,85 @@ export abstract class AudibleEvent extends Event {
pc = this.pitch; pc = this.pitch;
octave = (value: number): this => { octave = (value: number | number[], ...kwargs: number[]): this => {
/* /*
* This function is used to set the octave of the Event. * This function is used to set the octave of the Event.
* @param value - The octave value * @param value - The octave value
* @returns The Event * @returns The Event
*/ */
if(kwargs.length > 0) {
value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
}
this.values["octave"] = value; this.values["octave"] = value;
if(this.values.key && this.values.pitch && this.values.parsedScale) this.update(); if(this.values.key && (this.values.pitch || this.values.pitch === 0) && this.values.parsedScale) this.update();
return this; return this;
}; };
key = (value: string): this => { key = (value: string | string[], ...kwargs: string[]): this => {
/* /*
* This function is used to set the key of the Event. * This function is used to set the key of the Event.
* @param value - The key value * @param value - The key value
* @returns The Event * @returns The Event
*/ */
if(kwargs.length > 0) {
value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
}
this.values["key"] = value; this.values["key"] = value;
if(this.values.pitch && this.values.parsedScale) this.update(); if((this.values.pitch || this.values.pitch === 0) && this.values.parsedScale) this.update();
return this; return this;
}; };
scale = (value: string): this => { scale = (value: string | number | (number|string)[], ...kwargs: (string|number)[]): this => {
/* /*
* This function is used to set the scale of the Event. * This function is used to set the scale of the Event.
* @param value - The scale value * @param value - The scale value
* @returns The Event * @returns The Event
*/ */
if (!isScale(value)) { if(kwargs.length > 0) {
this.values.parsedScale = parseScala(value) as number[]; value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
} else { }
this.values.scaleName = value; if (typeof value === "string" || typeof value === "number") {
this.values.parsedScale = getScale(value) as number[]; this.values.parsedScale = safeScale(value) as number[];
} else if(Array.isArray(value)) {
this.values.parsedScale = value.map((v) => safeScale(v));
}
if(this.values.key && (this.values.pitch || this.values.pitch === 0)) {
this.update();
} }
if(this.values.key && this.values.pitch) this.update();
return this; return this;
}; };
freq = (value: number): this => { freq = (value: number | number[], ...kwargs: number[]): this => {
/* /*
* This function is used to set the frequency of the Event. * This function is used to set the frequency of the Event.
* @param value - The frequency value * @param value - The frequency value
* @returns The Event * @returns The Event
*/ */
if(kwargs.length > 0) {
value = (Array.isArray(value) ? value.concat(kwargs) : [value, ...kwargs]);
}
this.values["freq"] = value; this.values["freq"] = value;
const midiNote = freqToMidi(value); if(Array.isArray(value)) {
if (midiNote % 1 !== 0) { this.values["note"] = [];
this.values["note"] = Math.floor(midiNote); this.values["bend"] = [];
this.values["bend"] = resolvePitchBend(midiNote)[1]; for(const v of value) {
const midiNote = freqToMidi(v);
if (midiNote % 1 !== 0) {
this.values["note"].push(Math.floor(midiNote));
this.values["bend"].push(resolvePitchBend(midiNote)[1]);
} else {
this.values["note"].push(midiNote);
}
}
if(this.values.bend.length === 0) delete this.values.bend;
} else { } else {
this.values["note"] = midiNote; const midiNote = freqToMidi(value);
if (midiNote % 1 !== 0) {
this.values["note"] = Math.floor(midiNote);
this.values["bend"] = resolvePitchBend(midiNote)[1];
} else {
this.values["note"] = midiNote;
}
} }
return this; return this;
}; };