From 4ae4b1c8d4dd25b4aca2af155576a7b11804414f Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Wed, 2 Aug 2023 23:57:10 +0200 Subject: [PATCH] adding a simple drunk walk mechanism --- src/API.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/src/API.ts b/src/API.ts index e45df86..6968075 100644 --- a/src/API.ts +++ b/src/API.ts @@ -14,10 +14,57 @@ const sound = (value: any) => ({ ensureObjectValue: () => { } }); +class DrunkWalk { + public min: number; + public max: number; + private wrap: boolean; + public position: number; + + + constructor(min: number, max: number, wrap: boolean) { + this.min = min; + this.max = max; + this.wrap = wrap; + this.position = 0; + } + + step(): void { + // The "drunk" makes a step: +1, 0, or -1 + const stepSize: number = Math.floor(Math.random() * 3) - 1; + this.position += stepSize; + + // Checks for wrap + if (this.wrap) { + if (this.position > this.max) { + this.position = this.min; + } else if (this.position < this.min) { + this.position = this.max; + } + } else { + // Enforces min and max constraints + if (this.position < this.min) { + this.position = this.min; + } else if (this.position > this.max) { + this.position = this.max; + } + } + } + + getPosition(): number { + return this.position; + } + + toggleWrap(b: boolean): void { + this.wrap = b; + } +} + export class UserAPI { - variables: { [key: string]: any } = {} - iterators: { [key: string]: any } = {} + private variables: { [key: string]: any } = {} + private iterators: { [key: string]: any } = {} + private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false); + MidiConnection: MidiConnection = new MidiConnection() strudelSound = webaudioOutput() load: samples @@ -128,6 +175,32 @@ export class UserAPI { // Return current iterator value return this.iterators[name].value; } + it = this.iterator + + // ============================================================= + // Drunk mechanism + // ============================================================= + + get drunk() { + this._drunk.step(); + return this._drunk.getPosition(); + } + + set drunk(position: number) { + this._drunk.position = position; + } + + set drunk_max(max: number) { + this._drunk.max = max; + } + + set drunk_min(min: number) { + this._drunk.min = min; + } + + set drunk_wrap(wrap: boolean) { + this._drunk.toggleWrap(wrap); + } // ============================================================= // Variable related functions