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