Continue adaptation work
This commit is contained in:
@ -83,10 +83,9 @@ export class UserAPI {
|
||||
*/
|
||||
|
||||
public codeExamples: { [key: string]: string } = {};
|
||||
public counters: { [key: string]: any } = {};
|
||||
//@ts-ignore
|
||||
private counters: { [key: string]: any } = {};
|
||||
//@ts-ignore
|
||||
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
|
||||
public _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
|
||||
public randomGen = Math.random;
|
||||
public currentSeed: string | undefined = undefined;
|
||||
public localSeeds = new Map<string, Function>();
|
||||
@ -274,11 +273,11 @@ export class UserAPI {
|
||||
this.MidiConnection = new MidiConnection(this, app.settings);
|
||||
this.global = {};
|
||||
this.g = this.global;
|
||||
this.time = Transport.time(this.app);
|
||||
this.play = Transport.play(this.app);
|
||||
this.pause = Transport.pause(this.app);
|
||||
this.stop = Transport.stop(this.app);
|
||||
this.silence = Transport.silence(this.app);
|
||||
this.time = Transport.time(this);
|
||||
this.play = Transport.play(this);
|
||||
this.pause = Transport.pause(this);
|
||||
this.stop = Transport.stop(this);
|
||||
this.silence = Transport.silence(this);
|
||||
this.tempo = Transport.tempo(this.app);
|
||||
this.bpb = Transport.bpb(this.app);
|
||||
this.ppqn = Transport.ppqn(this.app);
|
||||
@ -324,9 +323,9 @@ export class UserAPI {
|
||||
this.animals = Canvas.animals();
|
||||
this.expressions = Canvas.expressions();
|
||||
this.generateCacheKey = Cache.generateCacheKey();
|
||||
this.resetAllFromCache = Cache.resetAllFromCache(this.app);
|
||||
this.clearPatternCache = Cache.clearPatternCache(this.app);
|
||||
this.removePatternFromCache = Cache.removePatternFromCache(this.app);
|
||||
this.resetAllFromCache = Cache.resetAllFromCache(this);
|
||||
this.clearPatternCache = Cache.clearPatternCache(this);
|
||||
this.removePatternFromCache = Cache.removePatternFromCache(this);
|
||||
this.script = Script.script(this.app);
|
||||
this.s = this.script;
|
||||
this.delete_script = Script.delete_script(this.app);
|
||||
@ -336,10 +335,10 @@ export class UserAPI {
|
||||
this.copy_universe = Script.copy_universe(this.app);
|
||||
this.delete_universe = Script.delete_universe(this.app);
|
||||
this.big_bang = Script.big_bang(this.app);
|
||||
this.drunk = Drunk.drunk(this.app);
|
||||
this.drunk_max = Drunk.drunk_max(this.app);
|
||||
this.drunk_min = Drunk.drunk_min(this.app);
|
||||
this.drunk_wrap = Drunk.drunk_wrap(this.app);
|
||||
this.drunk = Drunk.drunk(this);
|
||||
this.drunk_max = Drunk.drunk_max(this);
|
||||
this.drunk_min = Drunk.drunk_min(this);
|
||||
this.drunk_wrap = Drunk.drunk_wrap(this);
|
||||
this.warp = Warp.warp(this.app);
|
||||
this.beat_warp = Warp.beat_warp(this.app);
|
||||
this.min = Mathematics.min();
|
||||
@ -359,7 +358,7 @@ export class UserAPI {
|
||||
this.flip = Filters.flip(this.app);
|
||||
this.flipbar = Filters.flipbar(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.oneuclid = Filters.oneuclid(this.app);
|
||||
this.euclid = Filters.euclid();
|
||||
@ -378,23 +377,23 @@ export class UserAPI {
|
||||
this.usaw = LFO.usaw(this.app);
|
||||
this.triangle = LFO.triangle(this.app);
|
||||
this.utriangle = LFO.utriangle(this.app);
|
||||
this.square = LFO.square();
|
||||
this.usquare = LFO.usquare();
|
||||
this.noise = LFO.noise(this.app);
|
||||
this.unoise = LFO.unoise(this.app);
|
||||
this.prob = Probability.prob(this.app);
|
||||
this.toss = Probability.toss(this.app);
|
||||
this.odds = Probability.odds(this.app);
|
||||
this.square = LFO.square(this.app);
|
||||
this.usquare = LFO.usquare(this.app);
|
||||
this.noise = LFO.noise(this);
|
||||
this.unoise = LFO.unoise(this);
|
||||
this.prob = Probability.prob(this);
|
||||
this.toss = Probability.toss(this);
|
||||
this.odds = Probability.odds(this);
|
||||
this.never = Probability.never();
|
||||
this.almostNever = Probability.almostNever(this.app);
|
||||
this.rarely = Probability.rarely(this.app);
|
||||
this.scarcely = Probability.scarcely(this.app);
|
||||
this.sometimes = Probability.sometimes(this.app);
|
||||
this.often = Probability.often(this.app);
|
||||
this.frequently = Probability.frequently(this.app);
|
||||
this.almostAlways = Probability.almostAlways(this.app);
|
||||
this.almostNever = Probability.almostNever(this);
|
||||
this.rarely = Probability.rarely(this);
|
||||
this.scarcely = Probability.scarcely(this);
|
||||
this.sometimes = Probability.sometimes(this);
|
||||
this.often = Probability.often(this);
|
||||
this.frequently = Probability.frequently(this);
|
||||
this.almostAlways = Probability.almostAlways(this);
|
||||
this.always = Probability.always();
|
||||
this.dice = Probability.dice(this.app);
|
||||
this.dice = Probability.dice(this);
|
||||
this.osc = OSC.osc(this.app);
|
||||
this.getOSC = OSC.getOSC(this.app);
|
||||
this.gif = Canvas.gif(this.app);
|
||||
@ -404,16 +403,16 @@ export class UserAPI {
|
||||
this.seed = Randomness.seed(this.app);
|
||||
this.localSeededRandom = Randomness.localSeededRandom(this.app);
|
||||
this.clearLocalSeed = Randomness.clearLocalSeed(this.app);
|
||||
this.once = Counter.once(this.app);
|
||||
this.counter = Counter.counter(this.app);
|
||||
this.once = Counter.once(this);
|
||||
this.counter = Counter.counter(this);
|
||||
this.$ = this.counter;
|
||||
this.count = this.counter;
|
||||
this.i = Counter.i(this.app);
|
||||
this.sound = Sound.sound(this.app);
|
||||
this.snd = this.sound;
|
||||
this.speak = Sound.speak();
|
||||
this.log = Console.log(this.app);
|
||||
this.logOnce = Console.logOnce(this.app);
|
||||
this.log = Console.log(this);
|
||||
this.logOnce = Console.logOnce(this);
|
||||
this.cbar = Transport.cbar(this.app);
|
||||
this.ctick = Transport.ctick(this.app);
|
||||
this.cpulse = Transport.cpulse(this.app);
|
||||
@ -474,7 +473,7 @@ export class UserAPI {
|
||||
? code
|
||||
: (this.app.selectedExample as string);
|
||||
}
|
||||
this.clearPatternCache();
|
||||
this.patternCache.clear();
|
||||
this.stop();
|
||||
this.play();
|
||||
};
|
||||
|
||||
@ -1,60 +1,61 @@
|
||||
import { isGenerator, isGeneratorFunction, maybeToNumber } from "../Utils/Generic";
|
||||
import { type Player } from "../Classes/ZPlayer";
|
||||
import { type UserAPI } from "./API";
|
||||
|
||||
|
||||
export const generateCacheKey = () => (...args: any[]): string => {
|
||||
return args.map((arg) => JSON.stringify(arg)).join(",");
|
||||
};
|
||||
|
||||
export const resetAllFromCache = (app: any) => (): void => {
|
||||
app.patternCache.forEach((player: Player) => player.reset());
|
||||
export const resetAllFromCache = (api: UserAPI) => (): void => {
|
||||
api.patternCache.forEach((player: Player) => player.reset());
|
||||
};
|
||||
|
||||
export const clearPatternCache = (app: any) => (): void => {
|
||||
app.patternCache.clear();
|
||||
export const clearPatternCache = (api: UserAPI) => (): void => {
|
||||
api.patternCache.clear();
|
||||
};
|
||||
|
||||
export const removePatternFromCache = (app: any) => (id: string): void => {
|
||||
app.patternCache.delete(id);
|
||||
export const removePatternFromCache = (api: UserAPI) => (id: string): void => {
|
||||
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 (isGenerator(value)) {
|
||||
if (app.patternCache.has(key)) {
|
||||
const cachedValue = (app.patternCache.get(key) as Generator<any>).next().value;
|
||||
if (api.patternCache.has(key)) {
|
||||
const cachedValue = (api.patternCache.get(key) as Generator<any>).next().value;
|
||||
if (cachedValue !== 0 && !cachedValue) {
|
||||
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(cachedValue);
|
||||
} else {
|
||||
const generator = value as unknown as Generator<any>;
|
||||
app.patternCache.set(key, generator);
|
||||
api.patternCache.set(key, generator);
|
||||
return maybeToNumber(generator.next().value);
|
||||
}
|
||||
} else if (isGeneratorFunction(value)) {
|
||||
if (app.patternCache.has(key)) {
|
||||
const cachedValue = (app.patternCache.get(key) as Generator<any>).next().value;
|
||||
if (api.patternCache.has(key)) {
|
||||
const cachedValue = (api.patternCache.get(key) as Generator<any>).next().value;
|
||||
if (cachedValue || cachedValue === 0 || cachedValue === 0n) {
|
||||
return maybeToNumber(cachedValue);
|
||||
} else {
|
||||
const generator = value();
|
||||
app.patternCache.set(key, generator);
|
||||
api.patternCache.set(key, generator);
|
||||
return maybeToNumber(generator.next().value);
|
||||
}
|
||||
} else {
|
||||
const generator = value();
|
||||
app.patternCache.set(key, generator);
|
||||
api.patternCache.set(key, generator);
|
||||
return maybeToNumber(generator.next().value);
|
||||
}
|
||||
} else {
|
||||
app.patternCache.set(key, value);
|
||||
api.patternCache.set(key, value);
|
||||
return maybeToNumber(value);
|
||||
}
|
||||
} else {
|
||||
return maybeToNumber(app.patternCache.get(key));
|
||||
return maybeToNumber(api.patternCache.get(key));
|
||||
}
|
||||
};
|
||||
@ -353,7 +353,7 @@ export const expressions = () => (n: number = 1): string => {
|
||||
return randomChar()(n, 0x1f910, 0x1f92f);
|
||||
};
|
||||
|
||||
export const gif = (app: any) => (options: any): void => {
|
||||
export const gif = (app: Editor) => (options: any): void => {
|
||||
const {
|
||||
url,
|
||||
posX = 0,
|
||||
@ -402,7 +402,7 @@ export const gif = (app: any) => (options: any): void => {
|
||||
}, real_duration * 1000);
|
||||
};
|
||||
|
||||
export const scope = (app: any) => (config: OscilloscopeConfig): void => {
|
||||
export const scope = (app: Editor) => (config: OscilloscopeConfig): void => {
|
||||
/**
|
||||
* Configures the oscilloscope.
|
||||
* @param config - The configuration object for the oscilloscope.
|
||||
|
||||
@ -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.
|
||||
* @param message - The message to log.
|
||||
*/
|
||||
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.
|
||||
* @param message - The message to log.
|
||||
*/
|
||||
if (app.onceEvaluator) {
|
||||
if (api.onceEvaluator) {
|
||||
console.log(message);
|
||||
app._logMessage(message, false);
|
||||
app.onceEvaluator = false;
|
||||
api._logMessage(message, false);
|
||||
api.onceEvaluator = false;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,37 +1,40 @@
|
||||
export const once = (app: any) => (): boolean => {
|
||||
const firstTime = app.api.onceEvaluator;
|
||||
app.api.onceEvaluator = false;
|
||||
import { type UserAPI } from "./API";
|
||||
import { type Editor } from "../main";
|
||||
|
||||
export const once = (api: UserAPI) => (): boolean => {
|
||||
const firstTime = api.onceEvaluator;
|
||||
api.onceEvaluator = false;
|
||||
return firstTime;
|
||||
};
|
||||
|
||||
export const counter = (app: any) => (name: string | number, limit?: number, step?: number): number => {
|
||||
if (!(name in app.counters)) {
|
||||
app.counters[name] = {
|
||||
export const counter = (api: UserAPI) => (name: string | number, limit?: number, step?: number): number => {
|
||||
if (!(name in api.counters)) {
|
||||
api.counters[name] = {
|
||||
value: 0,
|
||||
step: step ?? 1,
|
||||
limit,
|
||||
};
|
||||
} else {
|
||||
if (app.counters[name].limit !== limit) {
|
||||
app.counters[name].value = 0;
|
||||
app.counters[name].limit = limit;
|
||||
if (api.counters[name].limit !== limit) {
|
||||
api.counters[name].value = 0;
|
||||
api.counters[name].limit = limit;
|
||||
}
|
||||
|
||||
if (app.counters[name].step !== step) {
|
||||
app.counters[name].step = step ?? app.counters[name].step;
|
||||
if (api.counters[name].step !== 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) {
|
||||
app.counters[name].value = 0;
|
||||
if (api.counters[name].limit !== undefined && api.counters[name].value > api.counters[name].limit) {
|
||||
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) {
|
||||
app.universes[app.selected_universe].global.evaluations = n;
|
||||
return app.universes[app.selected_universe];
|
||||
|
||||
@ -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.
|
||||
* @param n - [optional] The value to set the drunk mechanism to
|
||||
* @returns The current value of the drunk mechanism
|
||||
*/
|
||||
if (n !== undefined) {
|
||||
app._drunk.position = n;
|
||||
return app._drunk.getPosition();
|
||||
api._drunk.position = n;
|
||||
return api._drunk.getPosition();
|
||||
}
|
||||
app._drunk.step();
|
||||
return app._drunk.getPosition();
|
||||
api._drunk.step();
|
||||
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.
|
||||
* @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.
|
||||
* @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
|
||||
* @param wrap - Whether the drunk mechanism should wrap around
|
||||
*/
|
||||
app._drunk.toggleWrap(wrap);
|
||||
api._drunk.toggleWrap(wrap);
|
||||
};
|
||||
@ -1,8 +1,11 @@
|
||||
import { type Editor } from "../main";
|
||||
import { UserAPI } from "./API";
|
||||
|
||||
const _euclidean_cycle = (
|
||||
pulses: number,
|
||||
length: number,
|
||||
rotate: number = 0,
|
||||
): boolean[] => {
|
||||
): boolean[] => {
|
||||
if (pulses == length) return Array.from({ length }, () => true);
|
||||
function startsDescent(list: number[], i: number): boolean {
|
||||
const length = list.length;
|
||||
@ -19,7 +22,7 @@ const _euclidean_cycle = (
|
||||
cycle = cycle.slice(rotate).concat(cycle.slice(0, rotate));
|
||||
}
|
||||
return cycle;
|
||||
}
|
||||
}
|
||||
|
||||
export const fullseq = () => (sequence: string, duration: number): boolean | Array<boolean> => {
|
||||
if (sequence.split("").every((c) => c === "x" || c === "o")) {
|
||||
@ -46,7 +49,7 @@ export const seq = (app: any) => (expr: string, duration: number = 0.5): boolean
|
||||
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 results: boolean[] = nArray.map(
|
||||
(value) =>
|
||||
@ -56,7 +59,7 @@ export const beat = (app: any) => (n: number | number[] = 1, nudge: number = 0):
|
||||
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 barLength = app.clock.time_signature[1] * app.clock.ppqn;
|
||||
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);
|
||||
};
|
||||
|
||||
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 results: boolean[] = nArray.map(
|
||||
(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);
|
||||
};
|
||||
|
||||
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 results: boolean[] = nArray.map(
|
||||
(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);
|
||||
};
|
||||
|
||||
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];
|
||||
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;
|
||||
const time_pos = app.clock.pulses_since_origin;
|
||||
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;
|
||||
};
|
||||
|
||||
export const flipbar = (app: any) => (chunk: number = 1): boolean => {
|
||||
export const flipbar = (app: Editor) => (chunk: number = 1): boolean => {
|
||||
let realFlip = chunk;
|
||||
const time_pos = app.clock.time_position.bar;
|
||||
const current_chunk = Math.floor(time_pos / realFlip);
|
||||
return current_chunk % 2 === 0;
|
||||
};
|
||||
|
||||
export const onbar = (app: any) => (
|
||||
export const onbar = (app: Editor) => (
|
||||
bars: number[] | number,
|
||||
n: number = app.clock.time_signature[0],
|
||||
): boolean => {
|
||||
@ -116,23 +119,23 @@ export const onbar = (app: any) => (
|
||||
: 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[] = [];
|
||||
beat.forEach((b) => {
|
||||
let beatNumber = b % app.nominator() || app.nominator();
|
||||
let beatNumber = b % api.nominator() || api.nominator();
|
||||
let integral_part = Math.floor(beatNumber);
|
||||
integral_part = integral_part === 0 ? app.nominator() : integral_part;
|
||||
let decimal_part = Math.floor((beatNumber - integral_part) * app.clock.ppqn + 1);
|
||||
integral_part = integral_part === 0 ? api.nominator() : integral_part;
|
||||
let decimal_part = Math.floor((beatNumber - integral_part) * api.app.clock.ppqn + 1);
|
||||
if (decimal_part <= 0)
|
||||
decimal_part += app.clock.ppqn * app.nominator();
|
||||
decimal_part += api.app.clock.ppqn * api.nominator();
|
||||
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);
|
||||
};
|
||||
|
||||
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];
|
||||
const origin = app.clock.pulses_since_origin;
|
||||
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);
|
||||
};
|
||||
|
||||
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 beats = cycle.reduce((acc: number[], x: boolean, i: number) => {
|
||||
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 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.
|
||||
*/
|
||||
@ -172,7 +175,7 @@ export const rhythm = (app: any) => (div: number, pulses: number, length: number
|
||||
};
|
||||
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.
|
||||
*/
|
||||
@ -192,7 +195,7 @@ export const bin = () => (iterator: number, n: number): boolean => {
|
||||
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.
|
||||
*/
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
import { Editor } from "../main";
|
||||
import { UserAPI } from "./API";
|
||||
|
||||
export const line = () => (start: number, end: number, step: number = 1): number[] => {
|
||||
const countPlaces = (num: number) => {
|
||||
var text = num.toString();
|
||||
@ -22,44 +25,44 @@ export const line = () => (start: number, end: number, step: number = 1): number
|
||||
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));
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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 t = (Date.now() / 1000) % period;
|
||||
const t = (app.clock.ctx.currentTime % period);
|
||||
return (t / period < duty ? 1 : -1);
|
||||
};
|
||||
|
||||
export const usquare = () => (freq: number = 1, duty: number = 0.5): number => {
|
||||
return ((square()(freq, duty) + 1) / 2);
|
||||
export const usquare = (app: Editor) => (freq: number = 1, duty: number = 0.5): number => {
|
||||
return ((square(app)(freq, duty) + 1) / 2);
|
||||
};
|
||||
|
||||
export const noise = (app: any) => (): number => {
|
||||
return (app.randomGen() * 2 - 1); // Assuming randomGen() is defined in the app context
|
||||
export const noise = (api: UserAPI) => (): number => {
|
||||
return (api.randomGen() * 2 - 1); // Assuming randomGen() is defined in the app context
|
||||
};
|
||||
|
||||
export const unoise = (app: any) => (): number => {
|
||||
return ((noise(app)() + 1) / 2);
|
||||
export const unoise = (api: UserAPI) => (): number => {
|
||||
return ((noise(api)() + 1) / 2);
|
||||
};
|
||||
|
||||
147
src/API/MIDI.ts
147
src/API/MIDI.ts
@ -4,6 +4,8 @@ import {
|
||||
MidiNoteEvent,
|
||||
} from "../IO/MidiConnection";
|
||||
import { MidiEvent, MidiParams } from "../Classes/MidiEvent";
|
||||
import { UserAPI } from './API';
|
||||
import { Editor } from '../main';
|
||||
|
||||
interface ControlChange {
|
||||
channel: number;
|
||||
@ -11,20 +13,19 @@ interface ControlChange {
|
||||
value: number;
|
||||
}
|
||||
|
||||
|
||||
export const midi_outputs = (app: any) => (): void => {
|
||||
app._logMessage(app.MidiConnection.listMidiOutputs(), false);
|
||||
export const midi_outputs = (api: UserAPI) => (): void => {
|
||||
api._logMessage(api.MidiConnection.listMidiOutputs(), false);
|
||||
};
|
||||
|
||||
export const midi_output = (app: any) => (outputName: string): void => {
|
||||
export const midi_output = (api: UserAPI) => (outputName: string): void => {
|
||||
if (!outputName) {
|
||||
console.log(app.MidiConnection.getCurrentMidiPort());
|
||||
console.log(api.MidiConnection.getCurrentMidiPort());
|
||||
} else {
|
||||
app.MidiConnection.switchMidiOutput(outputName);
|
||||
api.MidiConnection.switchMidiOutput(outputName);
|
||||
}
|
||||
};
|
||||
|
||||
export const midi = (app: any) => (
|
||||
export const midi = (app: Editor) => (
|
||||
value: number | number[] = 60,
|
||||
velocity?: number | number[],
|
||||
channel?: number | number[],
|
||||
@ -34,164 +35,164 @@ export const midi = (app: any) => (
|
||||
return new MidiEvent(event, app);
|
||||
};
|
||||
|
||||
export const sysex = (app: any) => (data: Array<number>): void => {
|
||||
app.MidiConnection.sendSysExMessage(data);
|
||||
export const sysex = (api: UserAPI) => (data: Array<number>): void => {
|
||||
api.MidiConnection.sendSysExMessage(data);
|
||||
};
|
||||
|
||||
export const pitch_bend = (app: any) => (value: number, channel: number): void => {
|
||||
app.MidiConnection.sendPitchBend(value, channel);
|
||||
export const pitch_bend = (api: UserAPI) => (value: number, channel: number): void => {
|
||||
api.MidiConnection.sendPitchBend(value, channel);
|
||||
};
|
||||
|
||||
export const program_change = (app: any) => (program: number, channel: number): void => {
|
||||
app.MidiConnection.sendProgramChange(program, channel);
|
||||
export const program_change = (api: UserAPI) => (program: number, channel: number): void => {
|
||||
api.MidiConnection.sendProgramChange(program, channel);
|
||||
};
|
||||
|
||||
export const midi_clock = (app: any) => (): void => {
|
||||
app.MidiConnection.sendMidiClock();
|
||||
export const midi_clock = (api: UserAPI) => (): void => {
|
||||
api.MidiConnection.sendMidiClock();
|
||||
};
|
||||
|
||||
export const control_change = (app: any) => ({
|
||||
export const control_change = (api: UserAPI) => ({
|
||||
control = 20,
|
||||
value = 0,
|
||||
channel = 0,
|
||||
}: ControlChange): void => {
|
||||
app.MidiConnection.sendMidiControlChange(control, value, channel);
|
||||
api.MidiConnection.sendMidiControlChange(control, value, channel);
|
||||
};
|
||||
|
||||
export const cc = control_change;
|
||||
|
||||
export const midi_panic = (app: any) => (): void => {
|
||||
app.MidiConnection.panic();
|
||||
export const midi_panic = (api: UserAPI) => (): void => {
|
||||
api.MidiConnection.panic();
|
||||
};
|
||||
|
||||
export const active_note_events = (app: any) => (
|
||||
export const active_note_events = (api: UserAPI) => (
|
||||
channel?: number,
|
||||
): MidiNoteEvent[] | undefined => {
|
||||
let events;
|
||||
if (channel) {
|
||||
events = app.MidiConnection.activeNotesFromChannel(channel);
|
||||
events = api.MidiConnection.activeNotesFromChannel(channel);
|
||||
} else {
|
||||
events = app.MidiConnection.activeNotes;
|
||||
events = api.MidiConnection.activeNotes;
|
||||
}
|
||||
if (events.length > 0) return events;
|
||||
else return undefined;
|
||||
};
|
||||
|
||||
export const transmission = (app: any) => (): boolean => {
|
||||
return app.MidiConnection.activeNotes.length > 0;
|
||||
export const transmission = (api: UserAPI) => (): boolean => {
|
||||
return api.MidiConnection.activeNotes.length > 0;
|
||||
};
|
||||
|
||||
export const active_notes = (app: any) => (channel?: number): number[] | undefined => {
|
||||
const events = active_note_events(app)(channel);
|
||||
export const active_notes = (api: UserAPI) => (channel?: number): number[] | undefined => {
|
||||
const events = active_note_events(api)(channel);
|
||||
if (events && events.length > 0) return events.map((e) => e.note);
|
||||
else return undefined;
|
||||
};
|
||||
|
||||
export const kill_active_notes = (app: any) => (): void => {
|
||||
app.MidiConnection.activeNotes = [];
|
||||
export const kill_active_notes = (api: UserAPI) => (): void => {
|
||||
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;
|
||||
if (channel) notes = app.MidiConnection.stickyNotesFromChannel(channel);
|
||||
else notes = app.MidiConnection.stickyNotes;
|
||||
if (channel) notes = api.MidiConnection.stickyNotesFromChannel(channel);
|
||||
else notes = api.MidiConnection.stickyNotes;
|
||||
if (notes.length > 0) return notes.map((e: any) => e.note);
|
||||
else return undefined;
|
||||
};
|
||||
|
||||
export const kill_sticky_notes = (app: any) => (): void => {
|
||||
app.MidiConnection.stickyNotes = [];
|
||||
export const kill_sticky_notes = (api: UserAPI) => (): void => {
|
||||
api.MidiConnection.stickyNotes = [];
|
||||
};
|
||||
|
||||
export const buffer = (app: any) => (channel?: number): boolean => {
|
||||
export const buffer = (api: UserAPI) => (channel?: number): boolean => {
|
||||
if (channel)
|
||||
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)
|
||||
return app.MidiConnection.findNoteFromBufferInChannel(channel);
|
||||
else return app.MidiConnection.noteInputBuffer.shift();
|
||||
return api.MidiConnection.findNoteFromBufferInChannel(channel);
|
||||
else return api.MidiConnection.noteInputBuffer.shift();
|
||||
};
|
||||
|
||||
export const buffer_note = (app: any) => (channel?: number): number | undefined => {
|
||||
const note = buffer_event(app)(channel);
|
||||
export const buffer_note = (api: UserAPI) => (channel?: number): number | undefined => {
|
||||
const note = buffer_event(api)(channel);
|
||||
return note ? note.note : undefined;
|
||||
};
|
||||
|
||||
export const last_note_event = (app: any) => (channel?: number): MidiNoteEvent | undefined => {
|
||||
if (channel) return app.MidiConnection.lastNoteInChannel[channel];
|
||||
else return app.MidiConnection.lastNote;
|
||||
export const last_note_event = (api: UserAPI) => (channel?: number): MidiNoteEvent | undefined => {
|
||||
if (channel) return api.MidiConnection.lastNoteInChannel[channel];
|
||||
else return api.MidiConnection.lastNote;
|
||||
};
|
||||
|
||||
export const last_note = (app: any) => (channel?: number): number => {
|
||||
const note = last_note_event(app)(channel);
|
||||
export const last_note = (api: UserAPI) => (channel?: number): number => {
|
||||
const note = last_note_event(api)(channel);
|
||||
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 (app.MidiConnection.lastCCInChannel[channel]) {
|
||||
return app.MidiConnection.lastCCInChannel[channel][control];
|
||||
if (api.MidiConnection.lastCCInChannel[channel]) {
|
||||
return api.MidiConnection.lastCCInChannel[channel][control];
|
||||
} 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)
|
||||
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 => {
|
||||
if (channel) return app.MidiConnection.findCCFromBufferInChannel(channel);
|
||||
else return app.MidiConnection.ccInputBuffer.shift();
|
||||
export const buffer_cc = (api: UserAPI) => (channel?: number): MidiCCEvent | undefined => {
|
||||
if (channel) return api.MidiConnection.findCCFromBufferInChannel(channel);
|
||||
else return api.MidiConnection.ccInputBuffer.shift();
|
||||
};
|
||||
|
||||
export const show_scale = (app: any) => (
|
||||
export const show_scale = (api: UserAPI) => (
|
||||
root: number | string,
|
||||
scale: number | string,
|
||||
channel: number = 0,
|
||||
port: number | string = app.MidiConnection.currentOutputIndex || 0,
|
||||
port: number | string = api.MidiConnection.currentOutputIndex || 0,
|
||||
soundOff: boolean = false,
|
||||
): void => {
|
||||
if (!app.scale_aid || scale !== app.scale_aid) {
|
||||
hide_scale(app)(channel, port);
|
||||
if (!api.scale_aid || scale !== api.scale_aid) {
|
||||
hide_scale(api)(channel, port);
|
||||
const scaleNotes = getAllScaleNotes(scale, root);
|
||||
scaleNotes.forEach((note) => {
|
||||
app.MidiConnection.sendMidiOn(note, channel, 1, port);
|
||||
if (soundOff) app.MidiConnection.sendAllSoundOff(channel, port);
|
||||
api.MidiConnection.sendMidiOn(note, channel, 1, 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,
|
||||
port: number | string = app.MidiConnection.currentOutputIndex || 0,
|
||||
port: number | string = api.MidiConnection.currentOutputIndex || 0,
|
||||
): void => {
|
||||
const allNotes = Array.from(Array(128).keys());
|
||||
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,
|
||||
port: number | string = app.MidiConnection.currentOutputIndex || 0,
|
||||
port: number | string = api.MidiConnection.currentOutputIndex || 0,
|
||||
): 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,
|
||||
port: number | string = app.MidiConnection.currentOutputIndex || 0,
|
||||
port: number | string = api.MidiConnection.currentOutputIndex || 0,
|
||||
): void => {
|
||||
app.MidiConnection.sendAllSoundOff(channel, port);
|
||||
api.MidiConnection.sendAllSoundOff(channel, port);
|
||||
};
|
||||
|
||||
@ -1,31 +1,33 @@
|
||||
import { Editor } from "../main";
|
||||
|
||||
// mouse.ts
|
||||
export const onmousemove = (app: any) => (e: MouseEvent): void => {
|
||||
export const onmousemove = (app: Editor) => (e: MouseEvent): void => {
|
||||
app._mouseX = e.pageX;
|
||||
app._mouseY = e.pageY;
|
||||
};
|
||||
|
||||
export const mouseX = (app: any) => (): number => {
|
||||
export const mouseX = (app: Editor) => (): number => {
|
||||
/**
|
||||
* @returns The current x position of the mouse
|
||||
*/
|
||||
return app._mouseX;
|
||||
};
|
||||
|
||||
export const mouseY = (app: any) => (): number => {
|
||||
export const mouseY = (app: Editor) => (): number => {
|
||||
/**
|
||||
* @returns The current y position of the mouse
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
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.
|
||||
*/
|
||||
@ -12,11 +14,10 @@ export const osc = (app: any) => (address: string, port: number, ...args: any[])
|
||||
} 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.
|
||||
*/
|
||||
let oscMessages = app.oscMessages; // Assuming `oscMessages` is stored in `app`
|
||||
if (address) {
|
||||
let messages = oscMessages.filter((msg: { address: string; }) => msg.address === address);
|
||||
messages = messages.map((msg: { data: any; }) => msg.data);
|
||||
|
||||
@ -1,53 +1,53 @@
|
||||
// Probability.ts
|
||||
import { type UserAPI } from "./API";
|
||||
|
||||
export const prob = (app: any) => (p: number): boolean => {
|
||||
return app.randomGen() * 100 < p;
|
||||
export const prob = (api: UserAPI) => (p: number): boolean => {
|
||||
return api.randomGen() * 100 < p;
|
||||
};
|
||||
|
||||
export const toss = (app: any) => (): boolean => {
|
||||
return app.randomGen() > 0.5;
|
||||
export const toss = (api: UserAPI) => (): boolean => {
|
||||
return api.randomGen() > 0.5;
|
||||
};
|
||||
|
||||
export const odds = (app: any) => (n: number, beats: number = 1): boolean => {
|
||||
return app.randomGen() < (n * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const odds = (api: UserAPI) => (n: number, beats: number = 1): boolean => {
|
||||
return api.randomGen() < (n * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const never = () => (): boolean => {
|
||||
return false;
|
||||
};
|
||||
|
||||
export const almostNever = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.025 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const almostNever = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.025 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const rarely = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.1 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const rarely = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.1 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const scarcely = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.25 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const scarcely = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.25 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const sometimes = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.5 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const sometimes = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.5 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const often = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.75 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const often = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.75 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const frequently = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.9 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const frequently = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.9 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const almostAlways = (app: any) => (beats: number = 1): boolean => {
|
||||
return app.randomGen() < (0.985 * app.ppqn()) / (app.ppqn() * beats);
|
||||
export const almostAlways = (api: UserAPI) => (beats: number = 1): boolean => {
|
||||
return api.randomGen() < (0.985 * api.ppqn()) / (api.ppqn() * beats);
|
||||
};
|
||||
|
||||
export const always = () => (): boolean => {
|
||||
return true;
|
||||
};
|
||||
|
||||
export const dice = (app: any) => (sides: number): number => {
|
||||
return Math.floor(app.randomGen() * sides) + 1;
|
||||
export const dice = (api: UserAPI) => (sides: number): number => {
|
||||
return Math.floor(api.randomGen() * sides) + 1;
|
||||
};
|
||||
@ -1,33 +1,34 @@
|
||||
import { seededRandom } from "zifferjs";
|
||||
import { UserAPI } from "./API";
|
||||
|
||||
export const randI = (app: any) => (min: number, max: number): number => {
|
||||
return Math.floor(app.randomGen() * (max - min + 1)) + min;
|
||||
export const randI = (api: UserAPI) => (min: number, max: number): number => {
|
||||
return Math.floor(api.randomGen() * (max - min + 1)) + min;
|
||||
};
|
||||
|
||||
export const rand = (app: any) => (min: number, max: number): number => {
|
||||
return app.randomGen() * (max - min) + min;
|
||||
export const rand = (api: UserAPI) => (min: number, max: number): number => {
|
||||
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 (app.currentSeed !== seed) {
|
||||
app.currentSeed = seed;
|
||||
app.randomGen = seededRandom(seed);
|
||||
if (api.currentSeed !== seed) {
|
||||
api.currentSeed = 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 (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);
|
||||
app.localSeeds.set(seed, newSeededRandom);
|
||||
api.localSeeds.set(seed, 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) {
|
||||
app.localSeeds.delete(seed.toString());
|
||||
api.localSeeds.delete(seed.toString());
|
||||
} else {
|
||||
app.localSeeds.clear();
|
||||
api.localSeeds.clear();
|
||||
}
|
||||
};
|
||||
@ -1,8 +1,9 @@
|
||||
import { tryEvaluate } from "../Evaluator";
|
||||
import { blinkScript } from "../DOM/Visuals/Blinkers";
|
||||
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) => {
|
||||
if (arg >= 1 && arg <= 9) {
|
||||
blinkScript(app, "local", arg);
|
||||
@ -16,7 +17,7 @@ export const script = (app: any) => (...args: number[]): void => {
|
||||
|
||||
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] = {
|
||||
candidate: "",
|
||||
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[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[from],
|
||||
};
|
||||
};
|
||||
|
||||
export const delete_universe = (app: any) => (universe: string): void => {
|
||||
export const delete_universe = (app: Editor) => (universe: string): void => {
|
||||
if (app.selected_universe === universe) {
|
||||
app.selected_universe = "Default";
|
||||
}
|
||||
@ -48,7 +49,7 @@ export const delete_universe = (app: any) => (universe: string): void => {
|
||||
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?")) {
|
||||
app.universes = {
|
||||
...template_universes, // Assuming template_universes is defined elsewhere
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { SoundEvent } from "../Classes/SoundEvent";
|
||||
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.
|
||||
* @param sound - The sound identifier or array of identifiers to play.
|
||||
|
||||
@ -1,28 +1,31 @@
|
||||
export const time = (app: any) => (): number => {
|
||||
return app.audioContext.currentTime;
|
||||
import { type UserAPI } from "./API";
|
||||
import { type Editor } from "../main";
|
||||
|
||||
export const time = (api: UserAPI) => (): number => {
|
||||
return api.app.audioContext.currentTime;
|
||||
};
|
||||
|
||||
export const play = (app: any) => (): void => {
|
||||
app.setButtonHighlighting("play", true);
|
||||
app.MidiConnection.sendStartMessage();
|
||||
app.clock.start();
|
||||
export const play = (api: UserAPI) => (): void => {
|
||||
api.app.setButtonHighlighting("play", true);
|
||||
api.MidiConnection.sendStartMessage();
|
||||
api.app.clock.start();
|
||||
};
|
||||
|
||||
export const pause = (app: any) => (): void => {
|
||||
app.setButtonHighlighting("pause", true);
|
||||
app.clock.pause();
|
||||
export const pause = (api: UserAPI) => (): void => {
|
||||
api.app.setButtonHighlighting("pause", true);
|
||||
api.app.clock.pause();
|
||||
};
|
||||
|
||||
export const stop = (app: any) => (): void => {
|
||||
app.setButtonHighlighting("stop", true);
|
||||
app.clock.stop();
|
||||
export const stop = (api: UserAPI) => (): void => {
|
||||
api.app.setButtonHighlighting("stop", true);
|
||||
api.app.clock.stop();
|
||||
};
|
||||
|
||||
export const silence = (app: any) => (): void => {
|
||||
return stop(app)();
|
||||
export const silence = (api: UserAPI) => (): void => {
|
||||
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.
|
||||
*/
|
||||
@ -36,7 +39,7 @@ export const tempo = (app: any) => (n?: number): number => {
|
||||
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.
|
||||
*/
|
||||
@ -50,7 +53,7 @@ export const bpb = (app: any) => (n?: number): number => {
|
||||
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.
|
||||
*/
|
||||
@ -64,7 +67,7 @@ export const ppqn = (app: any) => (n?: number): number => {
|
||||
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.
|
||||
*/
|
||||
@ -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;
|
||||
};
|
||||
|
||||
export const ctick = (app: any) => (): number => {
|
||||
export const ctick = (app: Editor) => (): number => {
|
||||
return app.clock.tick + 1;
|
||||
};
|
||||
|
||||
export const cpulse = (app: any) => (): number => {
|
||||
export const cpulse = (app: Editor) => (): number => {
|
||||
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;
|
||||
};
|
||||
|
||||
export const ebeat = (app: any) => (): number => {
|
||||
export const ebeat = (app: Editor) => (): number => {
|
||||
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;
|
||||
};
|
||||
|
||||
export const nominator = (app: any) => (): number => {
|
||||
export const nominator = (app: Editor) => (): number => {
|
||||
return app.clock.time_signature[0];
|
||||
};
|
||||
|
||||
export const meter = (app: any) => (): number => {
|
||||
export const meter = (app: Editor) => (): number => {
|
||||
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;
|
||||
};
|
||||
@ -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.
|
||||
*/
|
||||
@ -6,7 +8,7 @@ export const warp = (app: any) => (n: number): void => {
|
||||
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.
|
||||
*/
|
||||
|
||||
@ -1,51 +1,50 @@
|
||||
import { InputOptions, Player } from "../Classes/ZPlayer";
|
||||
import { UserAPI } from "./API";
|
||||
import { generateCacheKey, removePatternFromCache } from "./Cache"
|
||||
|
||||
|
||||
// ziffersFunctions.ts
|
||||
export const z = (app: any) => (input: string | Generator<number>, options: InputOptions = {}, id: number | string = ""): Player => {
|
||||
export const z = (api: UserAPI) => (input: string | Generator<number>, options: InputOptions = {}, id: number | string = ""): Player => {
|
||||
const zid = "z" + id.toString();
|
||||
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 replace = false;
|
||||
|
||||
if (app.patternCache.has(key)) {
|
||||
player = app.patternCache.get(key) as Player;
|
||||
if (api.patternCache.has(key)) {
|
||||
player = api.patternCache.get(key) as Player;
|
||||
|
||||
if (typeof input === "string" &&
|
||||
player.input !== input &&
|
||||
(player.atTheBeginning() || app.forceEvaluator)) {
|
||||
(player.atTheBeginning() || api.forceEvaluator)) {
|
||||
replace = true;
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
app.logOnce(`Invalid syntax: ${input}`);
|
||||
api.logOnce(`Invalid syntax: ${input}`);
|
||||
};
|
||||
app.forceEvaluator = false;
|
||||
api.forceEvaluator = false;
|
||||
} 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()) {
|
||||
player = newPlayer;
|
||||
app.patternCache.set(key, player);
|
||||
api.patternCache.set(key, player);
|
||||
} else if (typeof input === "string") {
|
||||
app.invalidPatterns[input] = true;
|
||||
api.invalidPatterns[input] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (player) {
|
||||
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) {
|
||||
removePatternFromCache(app)(key);
|
||||
removePatternFromCache(api)(key);
|
||||
}
|
||||
|
||||
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
|
||||
export const generateZFunctions = (app: any) => {
|
||||
export const generateZFunctions = (api: UserAPI) => {
|
||||
const zFunctions: { [key: string]: (input: string, opts: InputOptions) => Player } = {};
|
||||
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;
|
||||
};
|
||||
|
||||
@ -126,7 +126,7 @@ beat([.25,.125].beat(2))::snd('arpy')
|
||||
.cutoff(usine(.5) * 5000).resonance(20).gain(0.3)
|
||||
.end(0.8).room(0.9).size(0.9).n(3).out();
|
||||
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!
|
||||
beat(1) :: beat_warp([2,4,5,10,11].pick())
|
||||
`,
|
||||
|
||||
Reference in New Issue
Block a user