Add register to API for registering reusable function chains
This commit is contained in:
@ -29,6 +29,7 @@ import { Speaker } from "./extensions/StringExtensions";
|
|||||||
import { getScaleNotes } from "zifferjs";
|
import { getScaleNotes } from "zifferjs";
|
||||||
import { OscilloscopeConfig, blinkScript } from "./AudioVisualisation";
|
import { OscilloscopeConfig, blinkScript } from "./AudioVisualisation";
|
||||||
import { SkipEvent } from "./classes/SkipEvent";
|
import { SkipEvent } from "./classes/SkipEvent";
|
||||||
|
import { AbstractEvent, EventOperation } from "./classes/AbstractEvents";
|
||||||
|
|
||||||
interface ControlChange {
|
interface ControlChange {
|
||||||
channel: number;
|
channel: number;
|
||||||
@ -1897,6 +1898,12 @@ export class UserAPI {
|
|||||||
// High Order Functions
|
// High Order Functions
|
||||||
// =============================================================
|
// =============================================================
|
||||||
|
|
||||||
|
register = (name: string, operation: EventOperation<AbstractEvent>): void => {
|
||||||
|
AbstractEvent.prototype[name] = function (this: AbstractEvent) {
|
||||||
|
return operation(this);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public shuffle = <T>(array: T[]): T[] => {
|
public shuffle = <T>(array: T[]): T[] => {
|
||||||
/**
|
/**
|
||||||
* Returns a shuffled version of an array.
|
* Returns a shuffled version of an array.
|
||||||
|
|||||||
@ -5,7 +5,13 @@ import {
|
|||||||
safeScale
|
safeScale
|
||||||
} from "zifferjs";
|
} from "zifferjs";
|
||||||
|
|
||||||
export abstract class Event {
|
export type EventOperation<T> = (instance: T) => void;
|
||||||
|
|
||||||
|
export interface AbstractEvent {
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AbstractEvent {
|
||||||
/**
|
/**
|
||||||
* The Event class is the base class for all events. It provides a set of
|
* The Event class is the base class for all events. It provides a set of
|
||||||
* functions that can be used to transform an Event. The functions are used
|
* functions that can be used to transform an Event. The functions are used
|
||||||
@ -66,7 +72,7 @@ export abstract class Event {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
odds = (probability: number, func: Function): Event => {
|
odds = (probability: number, func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a given probability.
|
* Returns a transformed Event with a given probability.
|
||||||
*
|
*
|
||||||
@ -80,7 +86,7 @@ export abstract class Event {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
never = (func: Function): Event => {
|
never = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Never return a transformed Event.
|
* Never return a transformed Event.
|
||||||
*
|
*
|
||||||
@ -90,7 +96,7 @@ export abstract class Event {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
almostNever = (func: Function): Event => {
|
almostNever = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.025.
|
* Returns a transformed Event with a probability of 0.025.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -99,7 +105,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.025, func);
|
return this.odds(0.025, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
rarely = (func: Function): Event => {
|
rarely = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.1.
|
* Returns a transformed Event with a probability of 0.1.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -108,7 +114,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.1, func);
|
return this.odds(0.1, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
scarcely = (func: Function): Event => {
|
scarcely = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.25.
|
* Returns a transformed Event with a probability of 0.25.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -117,7 +123,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.25, func);
|
return this.odds(0.25, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
sometimes = (func: Function): Event => {
|
sometimes = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.5.
|
* Returns a transformed Event with a probability of 0.5.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -126,7 +132,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.5, func);
|
return this.odds(0.5, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
often = (func: Function): Event => {
|
often = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.75.
|
* Returns a transformed Event with a probability of 0.75.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -135,7 +141,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.75, func);
|
return this.odds(0.75, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
frequently = (func: Function): Event => {
|
frequently = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.9.
|
* Returns a transformed Event with a probability of 0.9.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -144,7 +150,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.9, func);
|
return this.odds(0.9, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
almostAlways = (func: Function): Event => {
|
almostAlways = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 0.985.
|
* Returns a transformed Event with a probability of 0.985.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -153,7 +159,7 @@ export abstract class Event {
|
|||||||
return this.odds(0.985, func);
|
return this.odds(0.985, func);
|
||||||
};
|
};
|
||||||
|
|
||||||
always = (func: Function): Event => {
|
always = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event with a probability of 1.
|
* Returns a transformed Event with a probability of 1.
|
||||||
* @param func - The function to be applied to the Event
|
* @param func - The function to be applied to the Event
|
||||||
@ -162,7 +168,7 @@ export abstract class Event {
|
|||||||
return this.modify(func);
|
return this.modify(func);
|
||||||
};
|
};
|
||||||
|
|
||||||
modify = (func: Function): Event => {
|
modify = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* Returns a transformed Event. This function is used internally by the
|
* Returns a transformed Event. This function is used internally by the
|
||||||
* other functions of this class. It is just a wrapper for the function
|
* other functions of this class. It is just a wrapper for the function
|
||||||
@ -171,7 +177,7 @@ export abstract class Event {
|
|||||||
return func(this);
|
return func(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
seed = (value: string | number): Event => {
|
seed = (value: string | number): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* This function is used to set the seed of the random number generator.
|
* This function is used to set the seed of the random number generator.
|
||||||
* @param value - The seed value
|
* @param value - The seed value
|
||||||
@ -182,7 +188,7 @@ export abstract class Event {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
clear = (): Event => {
|
clear = (): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* This function is used to clear the seed of the random number generator.
|
* This function is used to clear the seed of the random number generator.
|
||||||
* @returns The Event
|
* @returns The Event
|
||||||
@ -191,7 +197,7 @@ export abstract class Event {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
apply = (func: Function): Event => {
|
apply = (func: Function): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* This function is used to apply a function to the Event.
|
* This function is used to apply a function to the Event.
|
||||||
* Simple function applicator
|
* Simple function applicator
|
||||||
@ -202,7 +208,7 @@ export abstract class Event {
|
|||||||
return this.modify(func);
|
return this.modify(func);
|
||||||
};
|
};
|
||||||
|
|
||||||
noteLength = (value: number | number[], ...kwargs: number[]): Event => {
|
noteLength = (value: number | number[], ...kwargs: number[]): AbstractEvent => {
|
||||||
/**
|
/**
|
||||||
* This function is used to set the note length of the Event.
|
* This function is used to set the note length of the Event.
|
||||||
*/
|
*/
|
||||||
@ -220,7 +226,7 @@ export abstract class Event {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class AudibleEvent extends Event {
|
export abstract class AudibleEvent extends AbstractEvent {
|
||||||
constructor(app: Editor) {
|
constructor(app: Editor) {
|
||||||
super(app);
|
super(app);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import { type Editor } from "../main";
|
import { type Editor } from "../main";
|
||||||
import { Event } from "./AbstractEvents";
|
import { AbstractEvent } from "./AbstractEvents";
|
||||||
|
|
||||||
export class RestEvent extends Event {
|
export class RestEvent extends AbstractEvent {
|
||||||
constructor(length: number, app: Editor) {
|
constructor(length: number, app: Editor) {
|
||||||
super(app);
|
super(app);
|
||||||
this.values["noteLength"] = length;
|
this.values["noteLength"] = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
_fallbackMethod = (): Event => {
|
_fallbackMethod = (): AbstractEvent => {
|
||||||
return RestEvent.createRestProxy(this.values["noteLength"], this.app);
|
return RestEvent.createRestProxy(this.values["noteLength"], this.app);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { Chord, Pitch, Rest as ZRest, Ziffers } from "zifferjs";
|
import { Chord, Pitch, Rest as ZRest, Ziffers } from "zifferjs";
|
||||||
import { Editor } from "../main";
|
import { Editor } from "../main";
|
||||||
import { Event } from "./AbstractEvents";
|
import { AbstractEvent } from "./AbstractEvents";
|
||||||
import { SkipEvent } from "./SkipEvent";
|
import { SkipEvent } from "./SkipEvent";
|
||||||
import { SoundEvent, SoundParams } from "./SoundEvent";
|
import { SoundEvent, SoundParams } from "./SoundEvent";
|
||||||
import { MidiEvent, MidiParams } from "./MidiEvent";
|
import { MidiEvent, MidiParams } from "./MidiEvent";
|
||||||
@ -10,7 +10,7 @@ import { TonnetzSpaces } from "zifferjs/src/tonnetz";
|
|||||||
|
|
||||||
export type InputOptions = { [key: string]: string | number };
|
export type InputOptions = { [key: string]: string | number };
|
||||||
|
|
||||||
export class Player extends Event {
|
export class Player extends AbstractEvent {
|
||||||
input: string|number;
|
input: string|number;
|
||||||
ziffers: Ziffers;
|
ziffers: Ziffers;
|
||||||
initCallTime: number = 0;
|
initCallTime: number = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user