Continue adaptation work

This commit is contained in:
2024-04-14 23:34:53 +02:00
parent b2a8e18b0e
commit 57f0c9dfe6
20 changed files with 1079 additions and 1055 deletions

View File

@ -83,10 +83,9 @@ export class UserAPI {
*/ */
public codeExamples: { [key: string]: string } = {}; public codeExamples: { [key: string]: string } = {};
public counters: { [key: string]: any } = {};
//@ts-ignore //@ts-ignore
private counters: { [key: string]: any } = {}; public _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
//@ts-ignore
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
public randomGen = Math.random; public randomGen = Math.random;
public currentSeed: string | undefined = undefined; public currentSeed: string | undefined = undefined;
public localSeeds = new Map<string, Function>(); public localSeeds = new Map<string, Function>();
@ -274,11 +273,11 @@ export class UserAPI {
this.MidiConnection = new MidiConnection(this, app.settings); this.MidiConnection = new MidiConnection(this, app.settings);
this.global = {}; this.global = {};
this.g = this.global; this.g = this.global;
this.time = Transport.time(this.app); this.time = Transport.time(this);
this.play = Transport.play(this.app); this.play = Transport.play(this);
this.pause = Transport.pause(this.app); this.pause = Transport.pause(this);
this.stop = Transport.stop(this.app); this.stop = Transport.stop(this);
this.silence = Transport.silence(this.app); this.silence = Transport.silence(this);
this.tempo = Transport.tempo(this.app); this.tempo = Transport.tempo(this.app);
this.bpb = Transport.bpb(this.app); this.bpb = Transport.bpb(this.app);
this.ppqn = Transport.ppqn(this.app); this.ppqn = Transport.ppqn(this.app);
@ -324,9 +323,9 @@ export class UserAPI {
this.animals = Canvas.animals(); this.animals = Canvas.animals();
this.expressions = Canvas.expressions(); this.expressions = Canvas.expressions();
this.generateCacheKey = Cache.generateCacheKey(); this.generateCacheKey = Cache.generateCacheKey();
this.resetAllFromCache = Cache.resetAllFromCache(this.app); this.resetAllFromCache = Cache.resetAllFromCache(this);
this.clearPatternCache = Cache.clearPatternCache(this.app); this.clearPatternCache = Cache.clearPatternCache(this);
this.removePatternFromCache = Cache.removePatternFromCache(this.app); this.removePatternFromCache = Cache.removePatternFromCache(this);
this.script = Script.script(this.app); this.script = Script.script(this.app);
this.s = this.script; this.s = this.script;
this.delete_script = Script.delete_script(this.app); this.delete_script = Script.delete_script(this.app);
@ -336,10 +335,10 @@ export class UserAPI {
this.copy_universe = Script.copy_universe(this.app); this.copy_universe = Script.copy_universe(this.app);
this.delete_universe = Script.delete_universe(this.app); this.delete_universe = Script.delete_universe(this.app);
this.big_bang = Script.big_bang(this.app); this.big_bang = Script.big_bang(this.app);
this.drunk = Drunk.drunk(this.app); this.drunk = Drunk.drunk(this);
this.drunk_max = Drunk.drunk_max(this.app); this.drunk_max = Drunk.drunk_max(this);
this.drunk_min = Drunk.drunk_min(this.app); this.drunk_min = Drunk.drunk_min(this);
this.drunk_wrap = Drunk.drunk_wrap(this.app); this.drunk_wrap = Drunk.drunk_wrap(this);
this.warp = Warp.warp(this.app); this.warp = Warp.warp(this.app);
this.beat_warp = Warp.beat_warp(this.app); this.beat_warp = Warp.beat_warp(this.app);
this.min = Mathematics.min(); this.min = Mathematics.min();
@ -359,7 +358,7 @@ export class UserAPI {
this.flip = Filters.flip(this.app); this.flip = Filters.flip(this.app);
this.flipbar = Filters.flipbar(this.app); this.flipbar = Filters.flipbar(this.app);
this.onbar = Filters.onbar(this.app); this.onbar = Filters.onbar(this.app);
this.onbeat = Filters.onbeat(this.app); this.onbeat = Filters.onbeat(this);
this.oncount = Filters.oncount(this.app); this.oncount = Filters.oncount(this.app);
this.oneuclid = Filters.oneuclid(this.app); this.oneuclid = Filters.oneuclid(this.app);
this.euclid = Filters.euclid(); this.euclid = Filters.euclid();
@ -378,23 +377,23 @@ export class UserAPI {
this.usaw = LFO.usaw(this.app); this.usaw = LFO.usaw(this.app);
this.triangle = LFO.triangle(this.app); this.triangle = LFO.triangle(this.app);
this.utriangle = LFO.utriangle(this.app); this.utriangle = LFO.utriangle(this.app);
this.square = LFO.square(); this.square = LFO.square(this.app);
this.usquare = LFO.usquare(); this.usquare = LFO.usquare(this.app);
this.noise = LFO.noise(this.app); this.noise = LFO.noise(this);
this.unoise = LFO.unoise(this.app); this.unoise = LFO.unoise(this);
this.prob = Probability.prob(this.app); this.prob = Probability.prob(this);
this.toss = Probability.toss(this.app); this.toss = Probability.toss(this);
this.odds = Probability.odds(this.app); this.odds = Probability.odds(this);
this.never = Probability.never(); this.never = Probability.never();
this.almostNever = Probability.almostNever(this.app); this.almostNever = Probability.almostNever(this);
this.rarely = Probability.rarely(this.app); this.rarely = Probability.rarely(this);
this.scarcely = Probability.scarcely(this.app); this.scarcely = Probability.scarcely(this);
this.sometimes = Probability.sometimes(this.app); this.sometimes = Probability.sometimes(this);
this.often = Probability.often(this.app); this.often = Probability.often(this);
this.frequently = Probability.frequently(this.app); this.frequently = Probability.frequently(this);
this.almostAlways = Probability.almostAlways(this.app); this.almostAlways = Probability.almostAlways(this);
this.always = Probability.always(); this.always = Probability.always();
this.dice = Probability.dice(this.app); this.dice = Probability.dice(this);
this.osc = OSC.osc(this.app); this.osc = OSC.osc(this.app);
this.getOSC = OSC.getOSC(this.app); this.getOSC = OSC.getOSC(this.app);
this.gif = Canvas.gif(this.app); this.gif = Canvas.gif(this.app);
@ -404,16 +403,16 @@ export class UserAPI {
this.seed = Randomness.seed(this.app); this.seed = Randomness.seed(this.app);
this.localSeededRandom = Randomness.localSeededRandom(this.app); this.localSeededRandom = Randomness.localSeededRandom(this.app);
this.clearLocalSeed = Randomness.clearLocalSeed(this.app); this.clearLocalSeed = Randomness.clearLocalSeed(this.app);
this.once = Counter.once(this.app); this.once = Counter.once(this);
this.counter = Counter.counter(this.app); this.counter = Counter.counter(this);
this.$ = this.counter; this.$ = this.counter;
this.count = this.counter; this.count = this.counter;
this.i = Counter.i(this.app); this.i = Counter.i(this.app);
this.sound = Sound.sound(this.app); this.sound = Sound.sound(this.app);
this.snd = this.sound; this.snd = this.sound;
this.speak = Sound.speak(); this.speak = Sound.speak();
this.log = Console.log(this.app); this.log = Console.log(this);
this.logOnce = Console.logOnce(this.app); this.logOnce = Console.logOnce(this);
this.cbar = Transport.cbar(this.app); this.cbar = Transport.cbar(this.app);
this.ctick = Transport.ctick(this.app); this.ctick = Transport.ctick(this.app);
this.cpulse = Transport.cpulse(this.app); this.cpulse = Transport.cpulse(this.app);
@ -474,7 +473,7 @@ export class UserAPI {
? code ? code
: (this.app.selectedExample as string); : (this.app.selectedExample as string);
} }
this.clearPatternCache(); this.patternCache.clear();
this.stop(); this.stop();
this.play(); this.play();
}; };

View File

@ -1,60 +1,61 @@
import { isGenerator, isGeneratorFunction, maybeToNumber } from "../Utils/Generic"; import { isGenerator, isGeneratorFunction, maybeToNumber } from "../Utils/Generic";
import { type Player } from "../Classes/ZPlayer"; import { type Player } from "../Classes/ZPlayer";
import { type UserAPI } from "./API";
export const generateCacheKey = () => (...args: any[]): string => { export const generateCacheKey = () => (...args: any[]): string => {
return args.map((arg) => JSON.stringify(arg)).join(","); return args.map((arg) => JSON.stringify(arg)).join(",");
}; };
export const resetAllFromCache = (app: any) => (): void => { export const resetAllFromCache = (api: UserAPI) => (): void => {
app.patternCache.forEach((player: Player) => player.reset()); api.patternCache.forEach((player: Player) => player.reset());
}; };
export const clearPatternCache = (app: any) => (): void => { export const clearPatternCache = (api: UserAPI) => (): void => {
app.patternCache.clear(); api.patternCache.clear();
}; };
export const removePatternFromCache = (app: any) => (id: string): void => { export const removePatternFromCache = (api: UserAPI) => (id: string): void => {
app.patternCache.delete(id); api.patternCache.delete(id);
}; };
export const cache = (app: any) => (key: string, value: any) => { export const cache = (api: UserAPI) => (key: string, value: any) => {
if (value !== undefined) { if (value !== undefined) {
if (isGenerator(value)) { if (isGenerator(value)) {
if (app.patternCache.has(key)) { if (api.patternCache.has(key)) {
const cachedValue = (app.patternCache.get(key) as Generator<any>).next().value; const cachedValue = (api.patternCache.get(key) as Generator<any>).next().value;
if (cachedValue !== 0 && !cachedValue) { if (cachedValue !== 0 && !cachedValue) {
const generator = value as unknown as Generator<any>; const generator = value as unknown as Generator<any>;
app.patternCache.set(key, generator); api.patternCache.set(key, generator);
return maybeToNumber(generator.next().value); return maybeToNumber(generator.next().value);
} }
return maybeToNumber(cachedValue); return maybeToNumber(cachedValue);
} else { } else {
const generator = value as unknown as Generator<any>; const generator = value as unknown as Generator<any>;
app.patternCache.set(key, generator); api.patternCache.set(key, generator);
return maybeToNumber(generator.next().value); return maybeToNumber(generator.next().value);
} }
} else if (isGeneratorFunction(value)) { } else if (isGeneratorFunction(value)) {
if (app.patternCache.has(key)) { if (api.patternCache.has(key)) {
const cachedValue = (app.patternCache.get(key) as Generator<any>).next().value; const cachedValue = (api.patternCache.get(key) as Generator<any>).next().value;
if (cachedValue || cachedValue === 0 || cachedValue === 0n) { if (cachedValue || cachedValue === 0 || cachedValue === 0n) {
return maybeToNumber(cachedValue); return maybeToNumber(cachedValue);
} else { } else {
const generator = value(); const generator = value();
app.patternCache.set(key, generator); api.patternCache.set(key, generator);
return maybeToNumber(generator.next().value); return maybeToNumber(generator.next().value);
} }
} else { } else {
const generator = value(); const generator = value();
app.patternCache.set(key, generator); api.patternCache.set(key, generator);
return maybeToNumber(generator.next().value); return maybeToNumber(generator.next().value);
} }
} else { } else {
app.patternCache.set(key, value); api.patternCache.set(key, value);
return maybeToNumber(value); return maybeToNumber(value);
} }
} else { } else {
return maybeToNumber(app.patternCache.get(key)); return maybeToNumber(api.patternCache.get(key));
} }
}; };

View File

@ -353,7 +353,7 @@ export const expressions = () => (n: number = 1): string => {
return randomChar()(n, 0x1f910, 0x1f92f); return randomChar()(n, 0x1f910, 0x1f92f);
}; };
export const gif = (app: any) => (options: any): void => { export const gif = (app: Editor) => (options: any): void => {
const { const {
url, url,
posX = 0, posX = 0,
@ -402,7 +402,7 @@ export const gif = (app: any) => (options: any): void => {
}, real_duration * 1000); }, real_duration * 1000);
}; };
export const scope = (app: any) => (config: OscilloscopeConfig): void => { export const scope = (app: Editor) => (config: OscilloscopeConfig): void => {
/** /**
* Configures the oscilloscope. * Configures the oscilloscope.
* @param config - The configuration object for the oscilloscope. * @param config - The configuration object for the oscilloscope.

View File

@ -1,20 +1,22 @@
export const log = (app: any) => (message: any) => { import { type UserAPI } from "./API";
export const log = (api: UserAPI) => (message: any) => {
/** /**
* Logs a message to the console and app-specific logger. * Logs a message to the console and app-specific logger.
* @param message - The message to log. * @param message - The message to log.
*/ */
console.log(message); console.log(message);
app._logMessage(message, false); api._logMessage(message, false);
}; };
export const logOnce = (app: any) => (message: any) => { export const logOnce = (api: UserAPI) => (message: any) => {
/** /**
* Logs a message to the console and app-specific logger, but only once. * Logs a message to the console and app-specific logger, but only once.
* @param message - The message to log. * @param message - The message to log.
*/ */
if (app.onceEvaluator) { if (api.onceEvaluator) {
console.log(message); console.log(message);
app._logMessage(message, false); api._logMessage(message, false);
app.onceEvaluator = false; api.onceEvaluator = false;
} }
}; };

View File

@ -1,37 +1,40 @@
export const once = (app: any) => (): boolean => { import { type UserAPI } from "./API";
const firstTime = app.api.onceEvaluator; import { type Editor } from "../main";
app.api.onceEvaluator = false;
export const once = (api: UserAPI) => (): boolean => {
const firstTime = api.onceEvaluator;
api.onceEvaluator = false;
return firstTime; return firstTime;
}; };
export const counter = (app: any) => (name: string | number, limit?: number, step?: number): number => { export const counter = (api: UserAPI) => (name: string | number, limit?: number, step?: number): number => {
if (!(name in app.counters)) { if (!(name in api.counters)) {
app.counters[name] = { api.counters[name] = {
value: 0, value: 0,
step: step ?? 1, step: step ?? 1,
limit, limit,
}; };
} else { } else {
if (app.counters[name].limit !== limit) { if (api.counters[name].limit !== limit) {
app.counters[name].value = 0; api.counters[name].value = 0;
app.counters[name].limit = limit; api.counters[name].limit = limit;
} }
if (app.counters[name].step !== step) { if (api.counters[name].step !== step) {
app.counters[name].step = step ?? app.counters[name].step; api.counters[name].step = step ?? api.counters[name].step;
} }
app.counters[name].value += app.counters[name].step; api.counters[name].value += api.counters[name].step;
if (app.counters[name].limit !== undefined && app.counters[name].value > app.counters[name].limit) { if (api.counters[name].limit !== undefined && api.counters[name].value > api.counters[name].limit) {
app.counters[name].value = 0; api.counters[name].value = 0;
} }
} }
return app.counters[name].value; return api.counters[name].value;
}; };
export const i = (app: any) => (n?: number) => { export const i = (app: Editor) => (n?: number) => {
if (n !== undefined) { if (n !== undefined) {
app.universes[app.selected_universe].global.evaluations = n; app.universes[app.selected_universe].global.evaluations = n;
return app.universes[app.selected_universe]; return app.universes[app.selected_universe];

View File

@ -1,37 +1,39 @@
export const drunk = (app: any) => (n?: number): number => { import { type UserAPI } from './API';
export const drunk = (api: UserAPI) => (n?: number): number => {
/** /**
* This function sets or returns the current drunk mechanism's value. * This function sets or returns the current drunk mechanism's value.
* @param n - [optional] The value to set the drunk mechanism to * @param n - [optional] The value to set the drunk mechanism to
* @returns The current value of the drunk mechanism * @returns The current value of the drunk mechanism
*/ */
if (n !== undefined) { if (n !== undefined) {
app._drunk.position = n; api._drunk.position = n;
return app._drunk.getPosition(); return api._drunk.getPosition();
} }
app._drunk.step(); api._drunk.step();
return app._drunk.getPosition(); return api._drunk.getPosition();
}; };
export const drunk_max = (app: any) => (max: number): void => { export const drunk_max = (api: UserAPI) => (max: number): void => {
/** /**
* Sets the maximum value of the drunk mechanism. * Sets the maximum value of the drunk mechanism.
* @param max - The maximum value of the drunk mechanism * @param max - The maximum value of the drunk mechanism
*/ */
app._drunk.max = max; api._drunk.max = max;
}; };
export const drunk_min = (app: any) => (min: number): void => { export const drunk_min = (api: UserAPI) => (min: number): void => {
/** /**
* Sets the minimum value of the drunk mechanism. * Sets the minimum value of the drunk mechanism.
* @param min - The minimum value of the drunk mechanism * @param min - The minimum value of the drunk mechanism
*/ */
app._drunk.min = min; api._drunk.min = min;
}; };
export const drunk_wrap = (app: any) => (wrap: boolean): void => { export const drunk_wrap = (api: UserAPI) => (wrap: boolean): void => {
/** /**
* Sets whether the drunk mechanism should wrap around * Sets whether the drunk mechanism should wrap around
* @param wrap - Whether the drunk mechanism should wrap around * @param wrap - Whether the drunk mechanism should wrap around
*/ */
app._drunk.toggleWrap(wrap); api._drunk.toggleWrap(wrap);
}; };

View File

@ -1,3 +1,6 @@
import { type Editor } from "../main";
import { UserAPI } from "./API";
const _euclidean_cycle = ( const _euclidean_cycle = (
pulses: number, pulses: number,
length: number, length: number,
@ -46,7 +49,7 @@ export const seq = (app: any) => (expr: string, duration: number = 0.5): boolean
return oncount(app)(output, len); return oncount(app)(output, len);
}; };
export const beat = (app: any) => (n: number | number[] = 1, nudge: number = 0): boolean => { export const beat = (app: Editor) => (n: number | number[] = 1, nudge: number = 0): boolean => {
const nArray = Array.isArray(n) ? n : [n]; const nArray = Array.isArray(n) ? n : [n];
const results: boolean[] = nArray.map( const results: boolean[] = nArray.map(
(value) => (value) =>
@ -56,7 +59,7 @@ export const beat = (app: any) => (n: number | number[] = 1, nudge: number = 0):
return results.some((value) => value === true); return results.some((value) => value === true);
}; };
export const bar = (app: any) => (n: number | number[] = 1, nudge: number = 0): boolean => { export const bar = (app: Editor) => (n: number | number[] = 1, nudge: number = 0): boolean => {
const nArray = Array.isArray(n) ? n : [n]; const nArray = Array.isArray(n) ? n : [n];
const barLength = app.clock.time_signature[1] * app.clock.ppqn; const barLength = app.clock.time_signature[1] * app.clock.ppqn;
const nudgeInPulses = Math.floor(nudge * barLength); const nudgeInPulses = Math.floor(nudge * barLength);
@ -68,7 +71,7 @@ export const bar = (app: any) => (n: number | number[] = 1, nudge: number = 0):
return results.some((value) => value === true); return results.some((value) => value === true);
}; };
export const pulse = (app: any) => (n: number | number[] = 1, nudge: number = 0): boolean => { export const pulse = (app: Editor) => (n: number | number[] = 1, nudge: number = 0): boolean => {
const nArray = Array.isArray(n) ? n : [n]; const nArray = Array.isArray(n) ? n : [n];
const results: boolean[] = nArray.map( const results: boolean[] = nArray.map(
(value) => (app.clock.pulses_since_origin - nudge) % value === 0, (value) => (app.clock.pulses_since_origin - nudge) % value === 0,
@ -76,7 +79,7 @@ export const pulse = (app: any) => (n: number | number[] = 1, nudge: number = 0)
return results.some((value) => value === true); return results.some((value) => value === true);
}; };
export const tick = (app: any) => (tick: number | number[], offset: number = 0): boolean => { export const tick = (app: Editor) => (tick: number | number[], offset: number = 0): boolean => {
const nArray = Array.isArray(tick) ? tick : [tick]; const nArray = Array.isArray(tick) ? tick : [tick];
const results: boolean[] = nArray.map( const results: boolean[] = nArray.map(
(value) => app.clock.time_position.pulse === value + offset, (value) => app.clock.time_position.pulse === value + offset,
@ -84,13 +87,13 @@ export const tick = (app: any) => (tick: number | number[], offset: number = 0):
return results.some((value) => value === true); return results.some((value) => value === true);
}; };
export const dur = (app: any) => (n: number | number[]): boolean => { export const dur = (app: Editor) => (n: number | number[]): boolean => {
let nums: number[] = Array.isArray(n) ? n : [n]; let nums: number[] = Array.isArray(n) ? n : [n];
return beat(app)(nums.dur(...nums)); return beat(app)(nums.dur(...nums));
}; };
export const flip = (app: any) => (chunk: number, ratio: number = 50): boolean => { export const flip = (app: Editor) => (chunk: number, ratio: number = 50): boolean => {
let realChunk = chunk * 2; let realChunk = chunk * 2;
const time_pos = app.clock.pulses_since_origin; const time_pos = app.clock.pulses_since_origin;
const full_chunk = Math.floor(realChunk * app.clock.ppqn); const full_chunk = Math.floor(realChunk * app.clock.ppqn);
@ -99,14 +102,14 @@ export const flip = (app: any) => (chunk: number, ratio: number = 50): boolean =
return pos_within_chunk < threshold; return pos_within_chunk < threshold;
}; };
export const flipbar = (app: any) => (chunk: number = 1): boolean => { export const flipbar = (app: Editor) => (chunk: number = 1): boolean => {
let realFlip = chunk; let realFlip = chunk;
const time_pos = app.clock.time_position.bar; const time_pos = app.clock.time_position.bar;
const current_chunk = Math.floor(time_pos / realFlip); const current_chunk = Math.floor(time_pos / realFlip);
return current_chunk % 2 === 0; return current_chunk % 2 === 0;
}; };
export const onbar = (app: any) => ( export const onbar = (app: Editor) => (
bars: number[] | number, bars: number[] | number,
n: number = app.clock.time_signature[0], n: number = app.clock.time_signature[0],
): boolean => { ): boolean => {
@ -116,23 +119,23 @@ export const onbar = (app: any) => (
: bars.some((b) => b === current_bar); : bars.some((b) => b === current_bar);
}; };
export const onbeat = (app: any) => (...beat: number[]): boolean => { export const onbeat = (api: UserAPI) => (...beat: number[]): boolean => {
let final_pulses: boolean[] = []; let final_pulses: boolean[] = [];
beat.forEach((b) => { beat.forEach((b) => {
let beatNumber = b % app.nominator() || app.nominator(); let beatNumber = b % api.nominator() || api.nominator();
let integral_part = Math.floor(beatNumber); let integral_part = Math.floor(beatNumber);
integral_part = integral_part === 0 ? app.nominator() : integral_part; integral_part = integral_part === 0 ? api.nominator() : integral_part;
let decimal_part = Math.floor((beatNumber - integral_part) * app.clock.ppqn + 1); let decimal_part = Math.floor((beatNumber - integral_part) * api.app.clock.ppqn + 1);
if (decimal_part <= 0) if (decimal_part <= 0)
decimal_part += app.clock.ppqn * app.nominator(); decimal_part += api.app.clock.ppqn * api.nominator();
final_pulses.push( final_pulses.push(
integral_part === app.cbeat() && app.cpulse() === decimal_part, integral_part === api.cbeat() && api.cpulse() === decimal_part,
); );
}); });
return final_pulses.some((p) => p === true); return final_pulses.some((p) => p === true);
}; };
export const oncount = (app: any) => (beats: number[] | number, count: number): boolean => { export const oncount = (app: Editor) => (beats: number[] | number, count: number): boolean => {
if (typeof beats === "number") beats = [beats]; if (typeof beats === "number") beats = [beats];
const origin = app.clock.pulses_since_origin; const origin = app.clock.pulses_since_origin;
let final_pulses: boolean[] = []; let final_pulses: boolean[] = [];
@ -145,7 +148,7 @@ export const oncount = (app: any) => (beats: number[] | number, count: number):
return final_pulses.some((p) => p === true); return final_pulses.some((p) => p === true);
}; };
export const oneuclid = (app: any) => (pulses: number, length: number, rotate: number = 0): boolean => { export const oneuclid = (app: Editor) => (pulses: number, length: number, rotate: number = 0): boolean => {
const cycle = _euclidean_cycle(pulses, length, rotate); const cycle = _euclidean_cycle(pulses, length, rotate);
const beats = cycle.reduce((acc: number[], x: boolean, i: number) => { const beats = cycle.reduce((acc: number[], x: boolean, i: number) => {
if (x) acc.push(i + 1); if (x) acc.push(i + 1);
@ -162,7 +165,7 @@ export const euclid = () => (iterator: number, pulses: number, length: number, r
}; };
export const ec = euclid; export const ec = euclid;
export const rhythm = (app: any) => (div: number, pulses: number, length: number, rotate: number = 0): boolean => { export const rhythm = (app: Editor) => (div: number, pulses: number, length: number, rotate: number = 0): boolean => {
/** /**
* Returns a rhythm based on Euclidean cycle. * Returns a rhythm based on Euclidean cycle.
*/ */
@ -172,7 +175,7 @@ export const rhythm = (app: any) => (div: number, pulses: number, length: number
}; };
export const ry = rhythm; export const ry = rhythm;
export const nrhythm = (app: any) => (div: number, pulses: number, length: number, rotate: number = 0): boolean => { export const nrhythm = (app: Editor) => (div: number, pulses: number, length: number, rotate: number = 0): boolean => {
/** /**
* Returns a negated rhythm based on Euclidean cycle. * Returns a negated rhythm based on Euclidean cycle.
*/ */
@ -192,7 +195,7 @@ export const bin = () => (iterator: number, n: number): boolean => {
return tobin[iterator % tobin.length]; return tobin[iterator % tobin.length];
}; };
export const binrhythm = (app: any) => (div: number, n: number): boolean => { export const binrhythm = (app: Editor) => (div: number, n: number): boolean => {
/** /**
* Returns a binary rhythm based on division and binary cycle. * Returns a binary rhythm based on division and binary cycle.
*/ */

View File

@ -1,3 +1,6 @@
import { Editor } from "../main";
import { UserAPI } from "./API";
export const line = () => (start: number, end: number, step: number = 1): number[] => { export const line = () => (start: number, end: number, step: number = 1): number[] => {
const countPlaces = (num: number) => { const countPlaces = (num: number) => {
var text = num.toString(); var text = num.toString();
@ -22,44 +25,44 @@ export const line = () => (start: number, end: number, step: number = 1): number
return result; return result;
}; };
export const sine = (app: any) => (freq: number = 1, phase: number = 0): number => { export const sine = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return Math.sin(2 * Math.PI * freq * (app.clock.ctx.currentTime - phase)); return Math.sin(2 * Math.PI * freq * (app.clock.ctx.currentTime - phase));
}; };
export const usine = (app: any) => (freq: number = 1, phase: number = 0): number => { export const usine = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return ((sine(app)(freq, phase) + 1) / 2); return ((sine(app)(freq, phase) + 1) / 2);
}; };
export const saw = (app: any) => (freq: number = 1, phase: number = 0): number => { export const saw = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return (((app.clock.ctx.currentTime * freq + phase) % 1) * 2 - 1); return (((app.clock.ctx.currentTime * freq + phase) % 1) * 2 - 1);
}; };
export const usaw = (app: any) => (freq: number = 1, phase: number = 0): number => { export const usaw = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return ((saw(app)(freq, phase) + 1) / 2); return ((saw(app)(freq, phase) + 1) / 2);
}; };
export const triangle = (app: any) => (freq: number = 1, phase: number = 0): number => { export const triangle = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return (Math.abs(saw(app)(freq, phase)) * 2 - 1); return (Math.abs(saw(app)(freq, phase)) * 2 - 1);
}; };
export const utriangle = (app: any) => (freq: number = 1, phase: number = 0): number => { export const utriangle = (app: Editor) => (freq: number = 1, phase: number = 0): number => {
return ((triangle(app)(freq, phase) + 1) / 2); return ((triangle(app)(freq, phase) + 1) / 2);
}; };
export const square = () => (freq: number = 1, duty: number = 0.5): number => { export const square = (app: Editor) => (freq: number = 1, duty: number = 0.5): number => {
const period = 1 / freq; const period = 1 / freq;
const t = (Date.now() / 1000) % period; const t = (app.clock.ctx.currentTime % period);
return (t / period < duty ? 1 : -1); return (t / period < duty ? 1 : -1);
}; };
export const usquare = () => (freq: number = 1, duty: number = 0.5): number => { export const usquare = (app: Editor) => (freq: number = 1, duty: number = 0.5): number => {
return ((square()(freq, duty) + 1) / 2); return ((square(app)(freq, duty) + 1) / 2);
}; };
export const noise = (app: any) => (): number => { export const noise = (api: UserAPI) => (): number => {
return (app.randomGen() * 2 - 1); // Assuming randomGen() is defined in the app context return (api.randomGen() * 2 - 1); // Assuming randomGen() is defined in the app context
}; };
export const unoise = (app: any) => (): number => { export const unoise = (api: UserAPI) => (): number => {
return ((noise(app)() + 1) / 2); return ((noise(api)() + 1) / 2);
}; };

View File

@ -4,6 +4,8 @@ import {
MidiNoteEvent, MidiNoteEvent,
} from "../IO/MidiConnection"; } from "../IO/MidiConnection";
import { MidiEvent, MidiParams } from "../Classes/MidiEvent"; import { MidiEvent, MidiParams } from "../Classes/MidiEvent";
import { UserAPI } from './API';
import { Editor } from '../main';
interface ControlChange { interface ControlChange {
channel: number; channel: number;
@ -11,20 +13,19 @@ interface ControlChange {
value: number; value: number;
} }
export const midi_outputs = (api: UserAPI) => (): void => {
export const midi_outputs = (app: any) => (): void => { api._logMessage(api.MidiConnection.listMidiOutputs(), false);
app._logMessage(app.MidiConnection.listMidiOutputs(), false);
}; };
export const midi_output = (app: any) => (outputName: string): void => { export const midi_output = (api: UserAPI) => (outputName: string): void => {
if (!outputName) { if (!outputName) {
console.log(app.MidiConnection.getCurrentMidiPort()); console.log(api.MidiConnection.getCurrentMidiPort());
} else { } else {
app.MidiConnection.switchMidiOutput(outputName); api.MidiConnection.switchMidiOutput(outputName);
} }
}; };
export const midi = (app: any) => ( export const midi = (app: Editor) => (
value: number | number[] = 60, value: number | number[] = 60,
velocity?: number | number[], velocity?: number | number[],
channel?: number | number[], channel?: number | number[],
@ -34,164 +35,164 @@ export const midi = (app: any) => (
return new MidiEvent(event, app); return new MidiEvent(event, app);
}; };
export const sysex = (app: any) => (data: Array<number>): void => { export const sysex = (api: UserAPI) => (data: Array<number>): void => {
app.MidiConnection.sendSysExMessage(data); api.MidiConnection.sendSysExMessage(data);
}; };
export const pitch_bend = (app: any) => (value: number, channel: number): void => { export const pitch_bend = (api: UserAPI) => (value: number, channel: number): void => {
app.MidiConnection.sendPitchBend(value, channel); api.MidiConnection.sendPitchBend(value, channel);
}; };
export const program_change = (app: any) => (program: number, channel: number): void => { export const program_change = (api: UserAPI) => (program: number, channel: number): void => {
app.MidiConnection.sendProgramChange(program, channel); api.MidiConnection.sendProgramChange(program, channel);
}; };
export const midi_clock = (app: any) => (): void => { export const midi_clock = (api: UserAPI) => (): void => {
app.MidiConnection.sendMidiClock(); api.MidiConnection.sendMidiClock();
}; };
export const control_change = (app: any) => ({ export const control_change = (api: UserAPI) => ({
control = 20, control = 20,
value = 0, value = 0,
channel = 0, channel = 0,
}: ControlChange): void => { }: ControlChange): void => {
app.MidiConnection.sendMidiControlChange(control, value, channel); api.MidiConnection.sendMidiControlChange(control, value, channel);
}; };
export const cc = control_change; export const cc = control_change;
export const midi_panic = (app: any) => (): void => { export const midi_panic = (api: UserAPI) => (): void => {
app.MidiConnection.panic(); api.MidiConnection.panic();
}; };
export const active_note_events = (app: any) => ( export const active_note_events = (api: UserAPI) => (
channel?: number, channel?: number,
): MidiNoteEvent[] | undefined => { ): MidiNoteEvent[] | undefined => {
let events; let events;
if (channel) { if (channel) {
events = app.MidiConnection.activeNotesFromChannel(channel); events = api.MidiConnection.activeNotesFromChannel(channel);
} else { } else {
events = app.MidiConnection.activeNotes; events = api.MidiConnection.activeNotes;
} }
if (events.length > 0) return events; if (events.length > 0) return events;
else return undefined; else return undefined;
}; };
export const transmission = (app: any) => (): boolean => { export const transmission = (api: UserAPI) => (): boolean => {
return app.MidiConnection.activeNotes.length > 0; return api.MidiConnection.activeNotes.length > 0;
}; };
export const active_notes = (app: any) => (channel?: number): number[] | undefined => { export const active_notes = (api: UserAPI) => (channel?: number): number[] | undefined => {
const events = active_note_events(app)(channel); const events = active_note_events(api)(channel);
if (events && events.length > 0) return events.map((e) => e.note); if (events && events.length > 0) return events.map((e) => e.note);
else return undefined; else return undefined;
}; };
export const kill_active_notes = (app: any) => (): void => { export const kill_active_notes = (api: UserAPI) => (): void => {
app.MidiConnection.activeNotes = []; api.MidiConnection.activeNotes = [];
}; };
export const sticky_notes = (app: any) => (channel?: number): number[] | undefined => { export const sticky_notes = (api: UserAPI) => (channel?: number): number[] | undefined => {
let notes; let notes;
if (channel) notes = app.MidiConnection.stickyNotesFromChannel(channel); if (channel) notes = api.MidiConnection.stickyNotesFromChannel(channel);
else notes = app.MidiConnection.stickyNotes; else notes = api.MidiConnection.stickyNotes;
if (notes.length > 0) return notes.map((e: any) => e.note); if (notes.length > 0) return notes.map((e: any) => e.note);
else return undefined; else return undefined;
}; };
export const kill_sticky_notes = (app: any) => (): void => { export const kill_sticky_notes = (api: UserAPI) => (): void => {
app.MidiConnection.stickyNotes = []; api.MidiConnection.stickyNotes = [];
}; };
export const buffer = (app: any) => (channel?: number): boolean => { export const buffer = (api: UserAPI) => (channel?: number): boolean => {
if (channel) if (channel)
return ( return (
app.MidiConnection.findNoteFromBufferInChannel(channel) !== undefined api.MidiConnection.findNoteFromBufferInChannel(channel) !== undefined
); );
else return app.MidiConnection.noteInputBuffer.length > 0; else return api.MidiConnection.noteInputBuffer.length > 0;
}; };
export const buffer_event = (app: any) => (channel?: number): MidiNoteEvent | undefined => { export const buffer_event = (api: UserAPI) => (channel?: number): MidiNoteEvent | undefined => {
if (channel) if (channel)
return app.MidiConnection.findNoteFromBufferInChannel(channel); return api.MidiConnection.findNoteFromBufferInChannel(channel);
else return app.MidiConnection.noteInputBuffer.shift(); else return api.MidiConnection.noteInputBuffer.shift();
}; };
export const buffer_note = (app: any) => (channel?: number): number | undefined => { export const buffer_note = (api: UserAPI) => (channel?: number): number | undefined => {
const note = buffer_event(app)(channel); const note = buffer_event(api)(channel);
return note ? note.note : undefined; return note ? note.note : undefined;
}; };
export const last_note_event = (app: any) => (channel?: number): MidiNoteEvent | undefined => { export const last_note_event = (api: UserAPI) => (channel?: number): MidiNoteEvent | undefined => {
if (channel) return app.MidiConnection.lastNoteInChannel[channel]; if (channel) return api.MidiConnection.lastNoteInChannel[channel];
else return app.MidiConnection.lastNote; else return api.MidiConnection.lastNote;
}; };
export const last_note = (app: any) => (channel?: number): number => { export const last_note = (api: UserAPI) => (channel?: number): number => {
const note = last_note_event(app)(channel); const note = last_note_event(api)(channel);
return note ? note.note : 60; return note ? note.note : 60;
}; };
export const ccIn = (app: any) => (control: number, channel?: number): number => { export const ccIn = (api: UserAPI) => (control: number, channel?: number): number => {
if (channel) { if (channel) {
if (app.MidiConnection.lastCCInChannel[channel]) { if (api.MidiConnection.lastCCInChannel[channel]) {
return app.MidiConnection.lastCCInChannel[channel][control]; return api.MidiConnection.lastCCInChannel[channel][control];
} else return 0; } else return 0;
} else return app.MidiConnection.lastCC[control] || 0; } else return api.MidiConnection.lastCC[control] || 0;
}; };
export const has_cc = (app: any) => (channel?: number): boolean => { export const has_cc = (api: UserAPI) => (channel?: number): boolean => {
if (channel) if (channel)
return ( return (
app.MidiConnection.findCCFromBufferInChannel(channel) !== undefined api.MidiConnection.findCCFromBufferInChannel(channel) !== undefined
); );
else return app.MidiConnection.ccInputBuffer.length > 0; else return api.MidiConnection.ccInputBuffer.length > 0;
}; };
export const buffer_cc = (app: any) => (channel?: number): MidiCCEvent | undefined => { export const buffer_cc = (api: UserAPI) => (channel?: number): MidiCCEvent | undefined => {
if (channel) return app.MidiConnection.findCCFromBufferInChannel(channel); if (channel) return api.MidiConnection.findCCFromBufferInChannel(channel);
else return app.MidiConnection.ccInputBuffer.shift(); else return api.MidiConnection.ccInputBuffer.shift();
}; };
export const show_scale = (app: any) => ( export const show_scale = (api: UserAPI) => (
root: number | string, root: number | string,
scale: number | string, scale: number | string,
channel: number = 0, channel: number = 0,
port: number | string = app.MidiConnection.currentOutputIndex || 0, port: number | string = api.MidiConnection.currentOutputIndex || 0,
soundOff: boolean = false, soundOff: boolean = false,
): void => { ): void => {
if (!app.scale_aid || scale !== app.scale_aid) { if (!api.scale_aid || scale !== api.scale_aid) {
hide_scale(app)(channel, port); hide_scale(api)(channel, port);
const scaleNotes = getAllScaleNotes(scale, root); const scaleNotes = getAllScaleNotes(scale, root);
scaleNotes.forEach((note) => { scaleNotes.forEach((note) => {
app.MidiConnection.sendMidiOn(note, channel, 1, port); api.MidiConnection.sendMidiOn(note, channel, 1, port);
if (soundOff) app.MidiConnection.sendAllSoundOff(channel, port); if (soundOff) api.MidiConnection.sendAllSoundOff(channel, port);
}); });
app.scale_aid = scale; api.scale_aid = scale;
} }
}; };
export const hide_scale = (app: any) => ( export const hide_scale = (api: UserAPI) => (
channel: number = 0, channel: number = 0,
port: number | string = app.MidiConnection.currentOutputIndex || 0, port: number | string = api.MidiConnection.currentOutputIndex || 0,
): void => { ): void => {
const allNotes = Array.from(Array(128).keys()); const allNotes = Array.from(Array(128).keys());
allNotes.forEach((note) => { allNotes.forEach((note) => {
app.MidiConnection.sendMidiOff(note, channel, port); api.MidiConnection.sendMidiOff(note, channel, port);
}); });
app.scale_aid = undefined; api.scale_aid = undefined;
}; };
export const midi_notes_off = (app: any) => ( export const midi_notes_off = (api: UserAPI) => (
channel: number = 0, channel: number = 0,
port: number | string = app.MidiConnection.currentOutputIndex || 0, port: number | string = api.MidiConnection.currentOutputIndex || 0,
): void => { ): void => {
app.MidiConnection.sendAllNotesOff(channel, port); api.MidiConnection.sendAllNotesOff(channel, port);
}; };
export const midi_sound_off = (app: any) => ( export const midi_sound_off = (api: UserAPI) => (
channel: number = 0, channel: number = 0,
port: number | string = app.MidiConnection.currentOutputIndex || 0, port: number | string = api.MidiConnection.currentOutputIndex || 0,
): void => { ): void => {
app.MidiConnection.sendAllSoundOff(channel, port); api.MidiConnection.sendAllSoundOff(channel, port);
}; };

View File

@ -1,31 +1,33 @@
import { Editor } from "../main";
// mouse.ts // mouse.ts
export const onmousemove = (app: any) => (e: MouseEvent): void => { export const onmousemove = (app: Editor) => (e: MouseEvent): void => {
app._mouseX = e.pageX; app._mouseX = e.pageX;
app._mouseY = e.pageY; app._mouseY = e.pageY;
}; };
export const mouseX = (app: any) => (): number => { export const mouseX = (app: Editor) => (): number => {
/** /**
* @returns The current x position of the mouse * @returns The current x position of the mouse
*/ */
return app._mouseX; return app._mouseX;
}; };
export const mouseY = (app: any) => (): number => { export const mouseY = (app: Editor) => (): number => {
/** /**
* @returns The current y position of the mouse * @returns The current y position of the mouse
*/ */
return app._mouseY; return app._mouseY;
}; };
export const noteX = (app: any) => (): number => { export const noteX = (app: Editor) => (): number => {
/** /**
* @returns The current x position scaled to 0-127 using screen width * @returns The current x position scaled to 0-127 using screen width
*/ */
return Math.floor((app._mouseX / document.body.clientWidth) * 127); return Math.floor((app._mouseX / document.body.clientWidth) * 127);
}; };
export const noteY = (app: any) => (): number => { export const noteY = (app: Editor) => (): number => {
/** /**
* @returns The current y position scaled to 0-127 using screen height * @returns The current y position scaled to 0-127 using screen height
*/ */

View File

@ -1,6 +1,8 @@
import { sendToServer, type OSCMessage } from "../IO/OSC"; import { sendToServer, type OSCMessage } from "../IO/OSC";
import { Editor } from "../main";
import { oscMessages } from "../IO/OSC";
export const osc = (app: any) => (address: string, port: number, ...args: any[]): void => { export const osc = (app: Editor) => (address: string, port: number, ...args: any[]): void => {
/** /**
* Sends an OSC message to the server. * Sends an OSC message to the server.
*/ */
@ -12,11 +14,10 @@ export const osc = (app: any) => (address: string, port: number, ...args: any[])
} as OSCMessage); } as OSCMessage);
}; };
export const getOSC = (app: any) => (address?: string): any[] => { export const getOSC = () => (address?: string): any[] => {
/** /**
* Retrieves incoming OSC messages. Filters by address if provided. * Retrieves incoming OSC messages. Filters by address if provided.
*/ */
let oscMessages = app.oscMessages; // Assuming `oscMessages` is stored in `app`
if (address) { if (address) {
let messages = oscMessages.filter((msg: { address: string; }) => msg.address === address); let messages = oscMessages.filter((msg: { address: string; }) => msg.address === address);
messages = messages.map((msg: { data: any; }) => msg.data); messages = messages.map((msg: { data: any; }) => msg.data);

View File

@ -1,53 +1,53 @@
// Probability.ts import { type UserAPI } from "./API";
export const prob = (app: any) => (p: number): boolean => { export const prob = (api: UserAPI) => (p: number): boolean => {
return app.randomGen() * 100 < p; return api.randomGen() * 100 < p;
}; };
export const toss = (app: any) => (): boolean => { export const toss = (api: UserAPI) => (): boolean => {
return app.randomGen() > 0.5; return api.randomGen() > 0.5;
}; };
export const odds = (app: any) => (n: number, beats: number = 1): boolean => { export const odds = (api: UserAPI) => (n: number, beats: number = 1): boolean => {
return app.randomGen() < (n * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (n * api.ppqn()) / (api.ppqn() * beats);
}; };
export const never = () => (): boolean => { export const never = () => (): boolean => {
return false; return false;
}; };
export const almostNever = (app: any) => (beats: number = 1): boolean => { export const almostNever = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.025 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.025 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const rarely = (app: any) => (beats: number = 1): boolean => { export const rarely = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.1 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.1 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const scarcely = (app: any) => (beats: number = 1): boolean => { export const scarcely = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.25 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.25 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const sometimes = (app: any) => (beats: number = 1): boolean => { export const sometimes = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.5 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.5 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const often = (app: any) => (beats: number = 1): boolean => { export const often = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.75 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.75 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const frequently = (app: any) => (beats: number = 1): boolean => { export const frequently = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.9 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.9 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const almostAlways = (app: any) => (beats: number = 1): boolean => { export const almostAlways = (api: UserAPI) => (beats: number = 1): boolean => {
return app.randomGen() < (0.985 * app.ppqn()) / (app.ppqn() * beats); return api.randomGen() < (0.985 * api.ppqn()) / (api.ppqn() * beats);
}; };
export const always = () => (): boolean => { export const always = () => (): boolean => {
return true; return true;
}; };
export const dice = (app: any) => (sides: number): number => { export const dice = (api: UserAPI) => (sides: number): number => {
return Math.floor(app.randomGen() * sides) + 1; return Math.floor(api.randomGen() * sides) + 1;
}; };

View File

@ -1,33 +1,34 @@
import { seededRandom } from "zifferjs"; import { seededRandom } from "zifferjs";
import { UserAPI } from "./API";
export const randI = (app: any) => (min: number, max: number): number => { export const randI = (api: UserAPI) => (min: number, max: number): number => {
return Math.floor(app.randomGen() * (max - min + 1)) + min; return Math.floor(api.randomGen() * (max - min + 1)) + min;
}; };
export const rand = (app: any) => (min: number, max: number): number => { export const rand = (api: UserAPI) => (min: number, max: number): number => {
return app.randomGen() * (max - min) + min; return api.randomGen() * (max - min) + min;
}; };
export const seed = (app: any) => (seed: string | number): void => { export const seed = (api: UserAPI) => (seed: string | number): void => {
if (typeof seed === "number") seed = seed.toString(); if (typeof seed === "number") seed = seed.toString();
if (app.currentSeed !== seed) { if (api.currentSeed !== seed) {
app.currentSeed = seed; api.currentSeed = seed;
app.randomGen = seededRandom(seed); api.randomGen = seededRandom(seed);
} }
}; };
export const localSeededRandom = (app: any) => (seed: string | number): Function => { export const localSeededRandom = (api: UserAPI) => (seed: string | number): Function => {
if (typeof seed === "number") seed = seed.toString(); if (typeof seed === "number") seed = seed.toString();
if (app.localSeeds.has(seed)) return app.localSeeds.get(seed) as Function; if (api.localSeeds.has(seed)) return api.localSeeds.get(seed) as Function;
const newSeededRandom = seededRandom(seed); const newSeededRandom = seededRandom(seed);
app.localSeeds.set(seed, newSeededRandom); api.localSeeds.set(seed, newSeededRandom);
return newSeededRandom; return newSeededRandom;
}; };
export const clearLocalSeed = (app: any) => (seed: string | number | undefined = undefined): void => { export const clearLocalSeed = (api: UserAPI) => (seed: string | number | undefined = undefined): void => {
if (seed) { if (seed) {
app.localSeeds.delete(seed.toString()); api.localSeeds.delete(seed.toString());
} else { } else {
app.localSeeds.clear(); api.localSeeds.clear();
} }
}; };

View File

@ -1,8 +1,9 @@
import { tryEvaluate } from "../Evaluator"; import { tryEvaluate } from "../Evaluator";
import { blinkScript } from "../DOM/Visuals/Blinkers"; import { blinkScript } from "../DOM/Visuals/Blinkers";
import { template_universes } from "../Editor/FileManagement"; import { template_universes } from "../Editor/FileManagement";
import { Editor } from "../main";
export const script = (app: any) => (...args: number[]): void => { export const script = (app: Editor) => (...args: number[]): void => {
args.forEach((arg) => { args.forEach((arg) => {
if (arg >= 1 && arg <= 9) { if (arg >= 1 && arg <= 9) {
blinkScript(app, "local", arg); blinkScript(app, "local", arg);
@ -16,7 +17,7 @@ export const script = (app: any) => (...args: number[]): void => {
export const s = script; export const s = script;
export const delete_script = (app: any) => (script: number): void => { export const delete_script = (app: Editor) => (script: number): void => {
app.universes[app.selected_universe].locals[script] = { app.universes[app.selected_universe].locals[script] = {
candidate: "", candidate: "",
committed: "", committed: "",
@ -24,19 +25,19 @@ export const delete_script = (app: any) => (script: number): void => {
}; };
}; };
export const copy_script = (app: any) => (from: number, to: number): void => { export const copy_script = (app: Editor) => (from: number, to: number): void => {
app.universes[app.selected_universe].locals[to] = { app.universes[app.selected_universe].locals[to] = {
...app.universes[app.selected_universe].locals[from], ...app.universes[app.selected_universe].locals[from],
}; };
}; };
export const copy_universe = (app: any) => (from: string, to: string): void => { export const copy_universe = (app: Editor) => (from: string, to: string): void => {
app.universes[to] = { app.universes[to] = {
...app.universes[from], ...app.universes[from],
}; };
}; };
export const delete_universe = (app: any) => (universe: string): void => { export const delete_universe = (app: Editor) => (universe: string): void => {
if (app.selected_universe === universe) { if (app.selected_universe === universe) {
app.selected_universe = "Default"; app.selected_universe = "Default";
} }
@ -48,7 +49,7 @@ export const delete_universe = (app: any) => (universe: string): void => {
app.updateKnownUniversesView(); app.updateKnownUniversesView();
}; };
export const big_bang = (app: any) => (): void => { export const big_bang = (app: Editor) => (): void => {
if (confirm("Are you sure you want to delete all universes?")) { if (confirm("Are you sure you want to delete all universes?")) {
app.universes = { app.universes = {
...template_universes, // Assuming template_universes is defined elsewhere ...template_universes, // Assuming template_universes is defined elsewhere

View File

@ -1,7 +1,8 @@
import { SoundEvent } from "../Classes/SoundEvent"; import { SoundEvent } from "../Classes/SoundEvent";
import { SkipEvent } from "../Classes/SkipEvent"; import { SkipEvent } from "../Classes/SkipEvent";
import { Editor } from "../main";
export const sound = (app: any) => (sound: string | string[] | null | undefined) => { export const sound = (app: Editor) => (sound: string | string[] | null | undefined) => {
/** /**
* Creates a sound event if a sound is specified, otherwise returns a skip event. * Creates a sound event if a sound is specified, otherwise returns a skip event.
* @param sound - The sound identifier or array of identifiers to play. * @param sound - The sound identifier or array of identifiers to play.

View File

@ -1,28 +1,31 @@
export const time = (app: any) => (): number => { import { type UserAPI } from "./API";
return app.audioContext.currentTime; import { type Editor } from "../main";
export const time = (api: UserAPI) => (): number => {
return api.app.audioContext.currentTime;
}; };
export const play = (app: any) => (): void => { export const play = (api: UserAPI) => (): void => {
app.setButtonHighlighting("play", true); api.app.setButtonHighlighting("play", true);
app.MidiConnection.sendStartMessage(); api.MidiConnection.sendStartMessage();
app.clock.start(); api.app.clock.start();
}; };
export const pause = (app: any) => (): void => { export const pause = (api: UserAPI) => (): void => {
app.setButtonHighlighting("pause", true); api.app.setButtonHighlighting("pause", true);
app.clock.pause(); api.app.clock.pause();
}; };
export const stop = (app: any) => (): void => { export const stop = (api: UserAPI) => (): void => {
app.setButtonHighlighting("stop", true); api.app.setButtonHighlighting("stop", true);
app.clock.stop(); api.app.clock.stop();
}; };
export const silence = (app: any) => (): void => { export const silence = (api: UserAPI) => (): void => {
return stop(app)(); return stop(api)();
}; };
export const tempo = (app: any) => (n?: number): number => { export const tempo = (app: Editor) => (n?: number): number => {
/** /**
* Sets or returns the current bpm. * Sets or returns the current bpm.
*/ */
@ -36,7 +39,7 @@ export const tempo = (app: any) => (n?: number): number => {
return n; return n;
}; };
export const bpb = (app: any) => (n?: number): number => { export const bpb = (app: Editor) => (n?: number): number => {
/** /**
* Sets or returns the number of beats per bar. * Sets or returns the number of beats per bar.
*/ */
@ -50,7 +53,7 @@ export const bpb = (app: any) => (n?: number): number => {
return n; return n;
}; };
export const ppqn = (app: any) => (n?: number): number => { export const ppqn = (app: Editor) => (n?: number): number => {
/** /**
* Sets or returns the number of pulses per quarter note. * Sets or returns the number of pulses per quarter note.
*/ */
@ -64,7 +67,7 @@ export const ppqn = (app: any) => (n?: number): number => {
return n; return n;
}; };
export const time_signature = (app: any) => (numerator: number, denominator: number): void => { export const time_signature = (app: Editor) => (numerator: number, denominator: number): void => {
/** /**
* Sets the time signature. * Sets the time signature.
*/ */
@ -75,40 +78,40 @@ export const time_signature = (app: any) => (numerator: number, denominator: num
} }
}; };
export const cbar = (app: any) => (): number => { export const cbar = (app: Editor) => (): number => {
return app.clock.time_position.bar + 1; return app.clock.time_position.bar + 1;
}; };
export const ctick = (app: any) => (): number => { export const ctick = (app: Editor) => (): number => {
return app.clock.tick + 1; return app.clock.tick + 1;
}; };
export const cpulse = (app: any) => (): number => { export const cpulse = (app: Editor) => (): number => {
return app.clock.time_position.pulse + 1; return app.clock.time_position.pulse + 1;
}; };
export const cbeat = (app: any) => (): number => { export const cbeat = (app: Editor) => (): number => {
return app.clock.time_position.beat + 1; return app.clock.time_position.beat + 1;
}; };
export const ebeat = (app: any) => (): number => { export const ebeat = (app: Editor) => (): number => {
return app.clock.beats_since_origin + 1; return app.clock.beats_since_origin + 1;
}; };
export const epulse = (app: any) => (): number => { export const epulse = (app: Editor) => (): number => {
return app.clock.pulses_since_origin + 1; return app.clock.pulses_since_origin + 1;
}; };
export const nominator = (app: any) => (): number => { export const nominator = (app: Editor) => (): number => {
return app.clock.time_signature[0]; return app.clock.time_signature[0];
}; };
export const meter = (app: any) => (): number => { export const meter = (app: Editor) => (): number => {
return app.clock.time_signature[1]; return app.clock.time_signature[1];
}; };
export const denominator = meter; // Alias for meter export const denominator = meter;
export const pulsesForBar = (app: any) => (): number => { export const pulsesForBar = (app: Editor) => (): number => {
return (app.clock.bpm * app.clock.ppqn * nominator(app)()) / 60; return (app.clock.bpm * app.clock.ppqn * nominator(app)()) / 60;
}; };

View File

@ -1,4 +1,6 @@
export const warp = (app: any) => (n: number): void => { import { Editor } from "../main";
export const warp = (app: Editor) => (n: number): void => {
/** /**
* Time-warp the clock by using the tick you wish to jump to. * Time-warp the clock by using the tick you wish to jump to.
*/ */
@ -6,7 +8,7 @@ export const warp = (app: any) => (n: number): void => {
app.clock.time_position = app.clock.convertTicksToTimeposition(n); app.clock.time_position = app.clock.convertTicksToTimeposition(n);
}; };
export const beat_warp = (app: any) => (beat: number): void => { export const beat_warp = (app: Editor) => (beat: number): void => {
/** /**
* Time-warp the clock by using the tick you wish to jump to. * Time-warp the clock by using the tick you wish to jump to.
*/ */

View File

@ -1,51 +1,50 @@
import { InputOptions, Player } from "../Classes/ZPlayer"; import { InputOptions, Player } from "../Classes/ZPlayer";
import { UserAPI } from "./API";
import { generateCacheKey, removePatternFromCache } from "./Cache" import { generateCacheKey, removePatternFromCache } from "./Cache"
export const z = (api: UserAPI) => (input: string | Generator<number>, options: InputOptions = {}, id: number | string = ""): Player => {
// ziffersFunctions.ts
export const z = (app: any) => (input: string | Generator<number>, options: InputOptions = {}, id: number | string = ""): Player => {
const zid = "z" + id.toString(); const zid = "z" + id.toString();
const key = id === "" ? generateCacheKey()(input, options) : zid; const key = id === "" ? generateCacheKey()(input, options) : zid;
const validSyntax = typeof input === "string" && !app.invalidPatterns[input] const validSyntax = typeof input === "string" && !api.invalidPatterns[input]
let player; let player;
let replace = false; let replace = false;
if (app.patternCache.has(key)) { if (api.patternCache.has(key)) {
player = app.patternCache.get(key) as Player; player = api.patternCache.get(key) as Player;
if (typeof input === "string" && if (typeof input === "string" &&
player.input !== input && player.input !== input &&
(player.atTheBeginning() || app.forceEvaluator)) { (player.atTheBeginning() || api.forceEvaluator)) {
replace = true; replace = true;
} }
} }
if ((typeof input !== "string" || validSyntax) && (!player || replace)) { if ((typeof input !== "string" || validSyntax) && (!player || replace)) {
if (typeof input === "string" && player && app.forceEvaluator) { if (typeof input === "string" && player && api.forceEvaluator) {
if (!player.updatePattern(input, options)) { if (!player.updatePattern(input, options)) {
app.logOnce(`Invalid syntax: ${input}`); api.logOnce(`Invalid syntax: ${input}`);
}; };
app.forceEvaluator = false; api.forceEvaluator = false;
} else { } else {
const newPlayer = player ? new Player(input, options, app, zid, player.nextEndTime()) : new Player(input, options, app, zid); const newPlayer = player ? new Player(input, options, api.app, zid, player.nextEndTime()) : new Player(input, options, api.app, zid);
if (newPlayer.isValid()) { if (newPlayer.isValid()) {
player = newPlayer; player = newPlayer;
app.patternCache.set(key, player); api.patternCache.set(key, player);
} else if (typeof input === "string") { } else if (typeof input === "string") {
app.invalidPatterns[input] = true; api.invalidPatterns[input] = true;
} }
} }
} }
if (player) { if (player) {
if (player.atTheBeginning()) { if (player.atTheBeginning()) {
if (typeof input === "string" && !validSyntax) app.log(`Invalid syntax: ${input}`); if (typeof input === "string" && !validSyntax) api.log(`Invalid syntax: ${input}`);
} }
if (player.ziffers.generator && player.ziffers.generatorDone) { if (player.ziffers.generator && player.ziffers.generatorDone) {
removePatternFromCache(app)(key); removePatternFromCache(api)(key);
} }
if (typeof id === "number") player.zid = zid; if (typeof id === "number") player.zid = zid;
@ -64,10 +63,10 @@ export const z = (app: any) => (input: string | Generator<number>, options: Inpu
}; };
// Generating numbered functions dynamically // Generating numbered functions dynamically
export const generateZFunctions = (app: any) => { export const generateZFunctions = (api: UserAPI) => {
const zFunctions: { [key: string]: (input: string, opts: InputOptions) => Player } = {}; const zFunctions: { [key: string]: (input: string, opts: InputOptions) => Player } = {};
for (let i = 0; i <= 16; i++) { for (let i = 0; i <= 16; i++) {
zFunctions[`z${i}`] = (input: string, opts: InputOptions = {}) => z(app)(input, opts, i); zFunctions[`z${i}`] = (input: string, opts: InputOptions = {}) => z(api)(input, opts, i);
} }
return zFunctions; return zFunctions;
}; };

View File

@ -126,7 +126,7 @@ beat([.25,.125].beat(2))::snd('arpy')
.cutoff(usine(.5) * 5000).resonance(20).gain(0.3) .cutoff(usine(.5) * 5000).resonance(20).gain(0.3)
.end(0.8).room(0.9).size(0.9).n(3).out(); .end(0.8).room(0.9).size(0.9).n(3).out();
beat(.5) :: snd('arpy').note( beat(.5) :: snd('arpy').note(
[30, 33, 35].repeatAll(4).beat(1) - [12,0].beat(0.5)).out() [30, 33, 35].repeat(4).beat(1) - [12,0].beat(0.5)).out()
// Comment me to stop warping! // Comment me to stop warping!
beat(1) :: beat_warp([2,4,5,10,11].pick()) beat(1) :: beat_warp([2,4,5,10,11].pick())
`, `,