From fb652e6ab3af5243f5f5e46edf2598bb881054e1 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sat, 12 Aug 2023 14:45:09 +0200 Subject: [PATCH] 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