nothing new under the sun

This commit is contained in:
2023-08-12 14:45:09 +02:00
parent 594f4a98cc
commit fb652e6ab3
2 changed files with 74 additions and 73 deletions

View File

@ -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)
}

65
src/Utils/Drunk.ts Normal file
View File

@ -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;
}
}