diff --git a/src/API.ts b/src/API.ts index 428401f..9cb48b9 100644 --- a/src/API.ts +++ b/src/API.ts @@ -1955,15 +1955,23 @@ export class UserAPI { // High Order Functions // ============================================================= - register = (name: string, operation: EventOperation): void => { + register = (name: string, operation: EventOperation): true => { AbstractEvent.prototype[name] = function( this: AbstractEvent, ...args: any[] ) { return operation(this, ...args); }; + return true; }; + all = (operation: EventOperation): true => { + AbstractEvent.prototype.chainAll = function (...args: any[]) { + return operation(this, ...args); + }; + return true; + } + 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 256d060..14b65ab 100644 --- a/src/classes/AbstractEvents.ts +++ b/src/classes/AbstractEvents.ts @@ -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; + } + } diff --git a/src/classes/MidiEvent.ts b/src/classes/MidiEvent.ts index 1515b23..9ea01a5 100644 --- a/src/classes/MidiEvent.ts +++ b/src/classes/MidiEvent.ts @@ -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[]; diff --git a/src/classes/SoundEvent.ts b/src/classes/SoundEvent.ts index 314252b..4c77d5a 100644 --- a/src/classes/SoundEvent.ts +++ b/src/classes/SoundEvent.ts @@ -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", diff --git a/src/documentation/patterns/chaining.ts b/src/documentation/patterns/chaining.ts index 73eb1cc..963d845 100644 --- a/src/documentation/patterns/chaining.ts +++ b/src/documentation/patterns/chaining.ts @@ -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 log() 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: | almostAlways | With a 98.5% probability. | .almostAlways(s => s.note(70)) | | always | Always transforms the Event. | .always(s => s.note(71)) | - - ### MIDI Chaining The conditional chaining also applies to MIDI. Values can also be incremented using += notation.