Merge branch 'main' of https://github.com/Bubobubobubobubo/Topos
This commit is contained in:
125
src/API.ts
125
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, Pitch, Chord, Rest } from "zifferjs";
|
||||
import {
|
||||
superdough, samples,
|
||||
@ -9,79 +10,27 @@ 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<T> {
|
||||
in(value: T): boolean;
|
||||
}
|
||||
}
|
||||
Array.prototype.in = function<T>(this: T[], value: T): boolean {
|
||||
return this.includes(value);
|
||||
};
|
||||
|
||||
|
||||
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 +162,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: string]: 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)
|
||||
}
|
||||
|
||||
@ -663,6 +612,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 = n;
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// Time markers
|
||||
// =============================================================
|
||||
@ -703,12 +656,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
|
||||
}
|
||||
|
||||
|
||||
@ -904,6 +857,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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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<boolean> => {
|
||||
@ -15,7 +19,7 @@ const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> =>
|
||||
*/
|
||||
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) {
|
||||
|
||||
65
src/Utils/Drunk.ts
Normal file
65
src/Utils/Drunk.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user