Add register to API for registering reusable function chains

This commit is contained in:
2023-11-16 22:39:51 +02:00
parent 02fc13803f
commit 9c362d6a5d
4 changed files with 35 additions and 22 deletions

View File

@ -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<AbstractEvent>): void => {
AbstractEvent.prototype[name] = function (this: AbstractEvent) {
return operation(this);
};
}
public shuffle = <T>(array: T[]): T[] => {
/**
* Returns a shuffled version of an array.

View File

@ -5,7 +5,13 @@ import {
safeScale
} 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
* 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);
}

View File

@ -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);
};

View File

@ -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;