Added all() method for chaining all events

This commit is contained in:
2023-12-22 15:26:51 +02:00
parent 98f431f6b2
commit 30983147ea
5 changed files with 44 additions and 5 deletions

View File

@ -1955,15 +1955,23 @@ export class UserAPI {
// High Order Functions
// =============================================================
register = (name: string, operation: EventOperation<AbstractEvent>): void => {
register = (name: string, operation: EventOperation<AbstractEvent>): true => {
AbstractEvent.prototype[name] = function(
this: AbstractEvent,
...args: any[]
) {
return operation(this, ...args);
};
return true;
};
all = (operation: EventOperation<AbstractEvent>): true => {
AbstractEvent.prototype.chainAll = function (...args: any[]) {
return operation(this, ...args);
};
return true;
}
public shuffle = <T>(array: T[]): T[] => {
/**
* Returns a shuffled version of an array.

View File

@ -521,4 +521,13 @@ export abstract class AudibleEvent extends AbstractEvent {
this.app.api.cue(functionName);
return this;
}
runChain = (): this => {
// chainAll is defined using all() in the API
if("chainAll" in this && typeof this.chainAll === "function") {
this.values = this.chainAll().values;
}
return this;
}
}

View File

@ -115,7 +115,7 @@ export class MidiEvent extends AudibleEvent {
return this;
};
out = (): void => {
out = (outChannel?: number|number[]): void => {
function play(event: MidiEvent, params: MidiParams): void {
const channel = params.channel ? params.channel : 0;
const velocity = params.velocity ? params.velocity : 100;
@ -141,6 +141,9 @@ export class MidiEvent extends AudibleEvent {
);
}
this.runChain();
if(outChannel) this.channel(outChannel);
const events = objectWithArraysToArrayOfObjects(this.values, [
"parsedScale",
]) as MidiParams[];

View File

@ -423,6 +423,7 @@ export class SoundEvent extends AudibleEvent {
};
out = (orbit?: number | number[]): void => {
this.runChain();
if (orbit) this.values["orbit"] = orbit;
const events = objectWithArraysToArrayOfObjects(this.values, [
"parsedScale",

View File

@ -37,7 +37,7 @@ beat(1)::sound('fhh').juxrev().out()
This is an extremely powerful construct. For example, you can use it to create synthesizer presets and reuse them later on. You can also define parameters for your registered functions. For example:
${makeExample(
"Re-creating a classic Tidal function",
"Creating synth presets",
`
// Registering a specific synth architecture
register('sub', (n,x=4,y=80)=>n.ad(0, .25)
@ -54,6 +54,26 @@ rhythm(.25, [6, 8].beat(), 12)::sound('sine')
true,
)}
## Registering chain for all events
The chain can also be registered automatically for all events. This is useful if you want to add a specific effect to all your events.
${makeExample(
"Registering chain to all events at once",
`
z0("h 9 ^ <7 5 3 1>")
.sound("sine")
.out()
z1("0 4 3 2")
.sound("sine")
.out()
all(x=>x.room(1).delay(rI(0,0.5)))
`,
true,
)}
## Logging values from the chain
You can use the <ic>log()</ic> function to print values from the current event. This can be useful to debug your code. Useful parameters to log could be **note**, **pitch**, **dur**, **octave** etc...
@ -120,8 +140,6 @@ There is a growing collection of probability and chance methods you can use:
| <ic>almostAlways</ic> | With a 98.5% probability. | <ic>.almostAlways(s => s.note(70))</ic> |
| <ic>always</ic> | Always transforms the Event. | <ic>.always(s => s.note(71))</ic> |
### MIDI Chaining
The conditional chaining also applies to MIDI. Values can also be incremented using <ic>+=</ic> notation.