From fb652e6ab3af5243f5f5e46edf2598bb881054e1 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sat, 12 Aug 2023 14:45:09 +0200 Subject: [PATCH 1/5] nothing new under the sun --- src/API.ts | 82 +++++----------------------------------------- src/Utils/Drunk.ts | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 src/Utils/Drunk.ts diff --git a/src/API.ts b/src/API.ts index b50c473..551810b 100644 --- a/src/API.ts +++ b/src/API.ts @@ -2,6 +2,7 @@ import { Editor } from "./main"; import { scale } from './Scales'; import { tryEvaluate } from "./Evaluator"; import { MidiConnection } from "./IO/MidiConnection"; +import { DrunkWalk } from './Utils/Drunk'; import { next } from "zifferjs"; import { superdough, samples, @@ -13,75 +14,10 @@ import { const init = Promise.all([ initAudioOnFirstClick(), samples('github:tidalcycles/Dirt-Samples/master'), + samples('github:kindohm/expedition/tree/master/samples'), registerSynthSounds(), ]); -class DrunkWalk { - - /** - * A class that implements a "drunk walk" algorithm. This is useful for generating random - * numbers in a constrained range. The "drunk" starts at a position, and then makes a step - * of +1, 0, or -1. The "drunk" can be constrained to a range, and can wrap around the range. - * - * @param min - The minimum value of the range - * @param max - The maximum value of the range - * @param wrap - Whether or not the "drunk" should wrap around the range - * @param position - The starting/current position of the "drunk" - */ - - 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 { - - /** - * Makes a step in the "drunk walk" algorithm. This is a random step of +1, 0, or -1. - */ - - const stepSize: number = Math.floor(Math.random() * 3) - 1; - this.position += stepSize; - - if (this.wrap) { - if (this.position > this.max) { - this.position = this.min; - } else if (this.position < this.min) { - this.position = this.max; - } - } else { - if (this.position < this.min) { - this.position = this.min; - } else if (this.position > this.max) { - this.position = this.max; - } - } - } - - getPosition(): number { - /** - * @returns The current position of the "drunk" - */ - return this.position; - } - - toggleWrap(b: boolean): void { - /** - * Whether or not the "drunk" should wrap around the range - * - * @param b - Whether or not the "drunk" should wrap around the range - */ - this.wrap = b; - } -} - export class UserAPI { /** @@ -213,17 +149,17 @@ export class UserAPI { } } - public note(note: number, channel: number = 0, velocity: number = 100, duration: number = 0.5): void { + public note(note: number, options: {[key: stirng]: number} = {}): void { /** * Sends a MIDI note to the current MIDI output. - * TODO: Fix note duration - * - * @param note - The MIDI note to send - * @param channel - The MIDI channel to send the note on - * @param velocity - The velocity of the note - * @param duration - The duration of the note (in ms) * + * @param note - the MIDI note number to send + * @param options - an object containing options for that note + * { channel: 0, velocity: 100, duration: 0.5 } */ + const channel = options.channel ? options.channel : 0; + const velocity = options.velocity ? options.velocity : 100; + const duration = options.duration ? options.duration: 0.5; this.MidiConnection.sendMidiNote(note, channel, velocity, duration) } diff --git a/src/Utils/Drunk.ts b/src/Utils/Drunk.ts new file mode 100644 index 0000000..4d7babf --- /dev/null +++ b/src/Utils/Drunk.ts @@ -0,0 +1,65 @@ +export class DrunkWalk { + + /** + * A class that implements a "drunk walk" algorithm. This is useful for generating random + * numbers in a constrained range. The "drunk" starts at a position, and then makes a step + * of +1, 0, or -1. The "drunk" can be constrained to a range, and can wrap around the range. + * + * @param min - The minimum value of the range + * @param max - The maximum value of the range + * @param wrap - Whether or not the "drunk" should wrap around the range + * @param position - The starting/current position of the "drunk" + */ + + 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 { + + /** + * Makes a step in the "drunk walk" algorithm. This is a random step of +1, 0, or -1. + */ + + const stepSize: number = Math.floor(Math.random() * 3) - 1; + this.position += stepSize; + + if (this.wrap) { + if (this.position > this.max) { + this.position = this.min; + } else if (this.position < this.min) { + this.position = this.max; + } + } else { + if (this.position < this.min) { + this.position = this.min; + } else if (this.position > this.max) { + this.position = this.max; + } + } + } + + getPosition(): number { + /** + * @returns The current position of the "drunk" + */ + return this.position; + } + + toggleWrap(b: boolean): void { + /** + * Whether or not the "drunk" should wrap around the range + * + * @param b - Whether or not the "drunk" should wrap around the range + */ + this.wrap = b; + } +} \ No newline at end of file From 42d65a1df16c61238ff2aad335baf5833ce814aa Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 13 Aug 2023 11:21:29 +0200 Subject: [PATCH 2/5] new methods --- src/API.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/API.ts b/src/API.ts index 551810b..499abc2 100644 --- a/src/API.ts +++ b/src/API.ts @@ -10,6 +10,19 @@ import { registerSynthSounds } from 'superdough'; +/** + * We are overriding the includes method which is rather + * useful to check the position of an event on a specific beat. + */ +declare global { + interface Array { + in(value: T): boolean; + } +} +Array.prototype.in = function(this: T[], value: T): boolean { + return this.includes(value); +}; + const init = Promise.all([ initAudioOnFirstClick(), @@ -829,6 +842,28 @@ export class UserAPI { // Low Frequency Oscillators // ============================================================= + line(start: number, end: number, step: number = 1): number[] { + /** + * Returns an array of values between start and end, with a given step. + * + * @param start - The start value of the array + * @param end - The end value of the array + * @param step - The step value of the array + * @returns An array of values between start and end, with a given step + */ + const result: number[] = []; + + if ((end > start && step > 0) || (end < start && step < 0)) { + for (let value = start; value <= end; value += step) { + result.push(value); + } + } else { + console.error("Invalid range or step provided."); + } + + return result; + } + sine(freq: number = 1, offset: number=0): number { /** * Returns a sine wave between -1 and 1. From 07d74c28b90a3e5b73727156aa60b1234d3b7191 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 13 Aug 2023 11:26:21 +0200 Subject: [PATCH 3/5] adding global iterator setter but doesn't reset properly --- src/API.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/API.ts b/src/API.ts index 499abc2..90f0538 100644 --- a/src/API.ts +++ b/src/API.ts @@ -601,6 +601,10 @@ export class UserAPI { return this.app.universes[this.app.selected_universe].global.evaluations } + set i(n: number) { + this.app.universes[this.app.selected_universe].global.evaluations = 0; + } + // ============================================================= // Time markers // ============================================================= From 8aa4255cffde3b395300e05f96cc0f81b93120ca Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 13 Aug 2023 11:26:36 +0200 Subject: [PATCH 4/5] fix --- src/API.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API.ts b/src/API.ts index 90f0538..6d06918 100644 --- a/src/API.ts +++ b/src/API.ts @@ -602,7 +602,7 @@ export class UserAPI { } set i(n: number) { - this.app.universes[this.app.selected_universe].global.evaluations = 0; + this.app.universes[this.app.selected_universe].global.evaluations = n; } // ============================================================= From 0cb03c74735be61ddbf688c803ef947d393ee7c6 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 13 Aug 2023 12:03:53 +0200 Subject: [PATCH 5/5] adding new important method --- src/API.ts | 4 ++-- src/Clock.ts | 6 +++++- src/Evaluator.ts | 10 +++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/API.ts b/src/API.ts index 6d06918..207b96c 100644 --- a/src/API.ts +++ b/src/API.ts @@ -645,12 +645,12 @@ export class UserAPI { return this.app.clock.time_position.beat } - get t_beat(): number { + get ebeat(): number { /** * Returns the current beat number since the origin of time * TODO: fix! Why is this not working? */ - return Math.floor(this.app.clock.tick / this.app.clock.ppqn) + return this.app.clock.beats_since_origin } diff --git a/src/Clock.ts b/src/Clock.ts index f0fdb23..82827d4 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -47,7 +47,7 @@ export class Clock { this.time_position = { bar: 0, beat: 0, pulse: 0 } this.bpm = 120; this.time_signature = [4, 4]; - this.ppqn = 48*2; + this.ppqn = 48; ctx.audioWorklet.addModule(TransportProcessor).then((e) => { this.transportNode = new TransportNode(ctx, {}, this.app); this.transportNode.connect(ctx.destination); @@ -65,6 +65,10 @@ export class Clock { return this.time_signature[0]; } + get beats_since_origin(): number { + return (this.time_position.bar - 1) * this.beats_per_bar + this.time_position.beat; + } + get pulse_duration(): number { /** * Returns the duration of a pulse in seconds. diff --git a/src/Evaluator.ts b/src/Evaluator.ts index 6896d28..efb3d69 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -1,6 +1,10 @@ import type { Editor } from './main'; import type { File } from './AppSettings'; +function codeInterceptor(code: string) { + return code.replace(/->/g, "&&") +} + const delay = (ms: number) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms)); const tryCatchWrapper = (application: Editor, code: string): Promise => { @@ -15,7 +19,7 @@ const tryCatchWrapper = (application: Editor, code: string): Promise => */ return new Promise((resolve, _) => { try { - Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api); + Function(`with (this) {try{${codeInterceptor(code)}} catch (e) {console.log(e)}};`).call(application.api); resolve(true); } catch (error) { console.log(error); @@ -43,7 +47,7 @@ export const tryEvaluate = async ( code.evaluations!++; const isCodeValid = await Promise.race([tryCatchWrapper( application, - `let i = ${code.evaluations};` + code.candidate, + `let i = ${code.evaluations};` + codeInterceptor(code.candidate as string), ), delay(timeout)]); if (isCodeValid) { @@ -66,7 +70,7 @@ export const evaluate = async (application: Editor, code: File, timeout = 1000): * @returns A promise that resolves to void */ try { - await Promise.race([tryCatchWrapper(application, code.committed as string), delay(timeout)]); + await Promise.race([tryCatchWrapper(application, codeInterceptor(code.committed as string)), delay(timeout)]); if (code.evaluations) code.evaluations++; } catch (error) {