Skip to content

Commit

Permalink
Add register to API for registering reusable function chains
Browse files Browse the repository at this point in the history
  • Loading branch information
amiika committed Nov 16, 2023
1 parent 02fc138 commit 9c362d6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 23 additions & 17 deletions src/classes/AbstractEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -80,7 +86,7 @@ export abstract class Event {
};

// @ts-ignore
never = (func: Function): Event => {
never = (func: Function): AbstractEvent => {
/**
* Never return a transformed Event.
*
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
*/
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/classes/RestEvent.ts
Original file line number Diff line number Diff line change
@@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions src/classes/ZPlayer.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down

0 comments on commit 9c362d6

Please sign in to comment.