some fun functions

This commit is contained in:
2023-08-23 15:48:49 +02:00
parent e5dd7194ba
commit 2ca6e8ee31
3 changed files with 77 additions and 55 deletions

View File

@ -22,6 +22,18 @@ interface ControlChange {
value: number; value: number;
} }
// @ts-ignore
Array.prototype.loop = function (index) {
return this[index % this.length];
};
// @ts-ignore
Array.prototype.random = function (index) {
return this[Math.floor(Math.random() * this.length)];
};
// @ts-ignore
Array.prototype.rand = Array.prototype.random;
/** /**
* This is an override of the basic "includes" method. * This is an override of the basic "includes" method.
*/ */
@ -48,7 +60,7 @@ async function loadSamples() {
loadSamples(); loadSamples();
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 class UserAPI { export class UserAPI {
@ -63,9 +75,9 @@ export class UserAPI {
private counters: { [key: string]: any } = {}; private counters: { [key: string]: any } = {};
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false); 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>();
public patternCache = new LRUCache({max: 1000, ttl: 1000 * 60 * 5}); public patternCache = new LRUCache({ max: 1000, ttl: 1000 * 60 * 5 });
MidiConnection: MidiConnection = new MidiConnection(); MidiConnection: MidiConnection = new MidiConnection();
load: samples; load: samples;
@ -75,11 +87,11 @@ export class UserAPI {
} }
_reportError = (error: any): void => { _reportError = (error: any): void => {
console.log(error) console.log(error);
if (!this.app.show_error) { if (!this.app.show_error) {
this.app.error_line.innerHTML = error as string; this.app.error_line.innerHTML = error as string;
this.app.error_line.classList.remove('hidden'); this.app.error_line.classList.remove("hidden");
setInterval(() => this.app.error_line.classList.add('hidden'), 2000) setTimeout(() => this.app.error_line.classList.add("hidden"), 2000);
} }
}; };
@ -142,7 +154,6 @@ export class UserAPI {
*/ */
return Math.floor((this.app._mouseY / document.body.clientHeight) * 127); return Math.floor((this.app._mouseY / document.body.clientHeight) * 127);
}; };
// ============================================================= // =============================================================
// Utility functions // Utility functions
@ -217,9 +228,7 @@ export class UserAPI {
} }
}; };
public note = ( public note = (value: number = 60): Note => {
value: number = 60
): Note => {
/** /**
* Sends a MIDI note to the current MIDI output. * Sends a MIDI note to the current MIDI output.
* *
@ -293,21 +302,24 @@ export class UserAPI {
// Ziffers related functions // Ziffers related functions
// ============================================================= // =============================================================
public z = (input: string, options: { [key: string]: string | number } = {}) => { public z = (
input: string,
options: { [key: string]: string | number } = {}
) => {
const key = generateCacheKey(input, options); const key = generateCacheKey(input, options);
let player; let player;
if(this.app.api.patternCache.has(key)) { if (this.app.api.patternCache.has(key)) {
player = this.app.api.patternCache.get(key) as Player; player = this.app.api.patternCache.get(key) as Player;
} else { } else {
player = new Player(input, options, this.app); player = new Player(input, options, this.app);
this.app.api.patternCache.set(key, player); this.app.api.patternCache.set(key, player);
}
if(player && player.ziffers.index === -1 || player.played) {
player.callTime = this.epulse();
player.played = false;
}
return player;
} }
if ((player && player.ziffers.index === -1) || player.played) {
player.callTime = this.epulse();
player.played = false;
}
return player;
};
// ============================================================= // =============================================================
// Counter related functions // Counter related functions
@ -565,25 +577,25 @@ export class UserAPI {
* Seed the random numbers globally in UserAPI. * Seed the random numbers globally in UserAPI.
* @param seed - The seed to use * @param seed - The seed to use
*/ */
if(typeof seed === "number") seed = seed.toString(); if (typeof seed === "number") seed = seed.toString();
if(this.currentSeed!==seed) { if (this.currentSeed !== seed) {
this.currentSeed = seed; this.currentSeed = seed;
this.randomGen = seededRandom(seed); this.randomGen = seededRandom(seed);
} }
} };
localSeededRandom = (seed: string | number): Function => { localSeededRandom = (seed: string | number): Function => {
if(typeof seed === "number") seed = seed.toString(); if (typeof seed === "number") seed = seed.toString();
if(this.localSeeds.has(seed)) return this.localSeeds.get(seed) as Function; if (this.localSeeds.has(seed)) return this.localSeeds.get(seed) as Function;
const newSeededRandom = seededRandom(seed) const newSeededRandom = seededRandom(seed);
this.localSeeds.set(seed,newSeededRandom); this.localSeeds.set(seed, newSeededRandom);
return newSeededRandom; return newSeededRandom;
} };
clearLocalSeed = (seed: string | number | undefined = undefined): void => { clearLocalSeed = (seed: string | number | undefined = undefined): void => {
if(seed) this.localSeeds.delete(seed.toString()); if (seed) this.localSeeds.delete(seed.toString());
this.localSeeds.clear(); this.localSeeds.clear();
} };
// ============================================================= // =============================================================
// Quantification functions // Quantification functions
@ -685,88 +697,88 @@ export class UserAPI {
public odds = (n: number, sec: number = 15): boolean => { public odds = (n: number, sec: number = 15): boolean => {
/** /**
* Returns true n% of the time. * Returns true n% of the time.
* *
* @param n - The probability of returning true. 1/4 = 25% = 0.25, 80/127 = 62.9% = 0.6299212598425197, etc... * @param n - The probability of returning true. 1/4 = 25% = 0.25, 80/127 = 62.9% = 0.6299212598425197, etc...
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True n% of the time * @returns True n% of the time
*/ */
return this.randomGen() < n*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (n * this.ppqn()) / (this.ppqn() * sec);
}; };
public almostNever = (sec: number = 15): boolean => { public almostNever = (sec: number = 15): boolean => {
/** /**
* Returns true 2.5% of the time in given time frame. * Returns true 2.5% of the time in given time frame.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 2.5% of the time * @returns True 2.5% of the time
*/ */
return this.randomGen() < 0.025*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.025 * this.ppqn()) / (this.ppqn() * sec);
}; };
public rarely = (sec: number = 15): boolean => { public rarely = (sec: number = 15): boolean => {
/** /**
* Returns true 10% of the time. * Returns true 10% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 10% of the time. * @returns True 10% of the time.
*/ */
return this.randomGen() < 0.1*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.1 * this.ppqn()) / (this.ppqn() * sec);
}; };
public scarcely = (sec: number = 15): boolean => { public scarcely = (sec: number = 15): boolean => {
/** /**
* Returns true 25% of the time. * Returns true 25% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 25% of the time * @returns True 25% of the time
*/ */
return this.randomGen() < 0.25*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.25 * this.ppqn()) / (this.ppqn() * sec);
}; };
public sometimes = (sec: number = 15): boolean => { public sometimes = (sec: number = 15): boolean => {
/** /**
* Returns true 50% of the time. * Returns true 50% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 50% of the time * @returns True 50% of the time
*/ */
return this.randomGen() < 0.5*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.5 * this.ppqn()) / (this.ppqn() * sec);
}; };
public often = (sec: number = 15): boolean => { public often = (sec: number = 15): boolean => {
/** /**
* Returns true 75% of the time. * Returns true 75% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 75% of the time * @returns True 75% of the time
*/ */
return this.randomGen() < 0.75*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.75 * this.ppqn()) / (this.ppqn() * sec);
}; };
public frequently = (sec: number = 15): boolean => { public frequently = (sec: number = 15): boolean => {
/** /**
* Returns true 90% of the time. * Returns true 90% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 90% of the time * @returns True 90% of the time
*/ */
return this.randomGen() < 0.9*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.9 * this.ppqn()) / (this.ppqn() * sec);
}; };
public almostAlways = (sec: number = 15): boolean => { public almostAlways = (sec: number = 15): boolean => {
/** /**
* Returns true 98.5% of the time. * Returns true 98.5% of the time.
* *
* @param sec - The time frame in seconds * @param sec - The time frame in seconds
* @returns True 98.5% of the time * @returns True 98.5% of the time
*/ */
return this.randomGen() < 0.985*this.ppqn()/(this.ppqn()*sec); return this.randomGen() < (0.985 * this.ppqn()) / (this.ppqn() * sec);
}; };
public dice = (sides: number): number => { public dice = (sides: number): number => {
/** /**
* Returns the value of a dice roll with n sides. * Returns the value of a dice roll with n sides.
* *
* @param sides - The number of sides on the dice * @param sides - The number of sides on the dice
* @returns The value of a dice roll with n sides * @returns The value of a dice roll with n sides
*/ */

View File

@ -130,7 +130,7 @@ export class AppSettings {
} }
} }
get_universe(universe_name: string) { get_universe() {
this.universes.universe_name; this.universes.universe_name;
} }

View File

@ -570,6 +570,15 @@ export class Editor {
Object.entries(this.api).forEach(([name, value]) => { Object.entries(this.api).forEach(([name, value]) => {
(globalThis as Record<string, any>)[name] = value; (globalThis as Record<string, any>)[name] = value;
}); });
// Loading from URL bar
// let url = new URLSearchParams(window.location.search).toString();
// const queryString = url.split("universe=");
// console.log(queryString);
// const encodedData = queryString.split("=")[1];
// const decodedData = decodeURIComponent(queryString);
// console.log(decodedData);
// console.log(decodedData);
} }
get note_buffer() { get note_buffer() {
@ -594,15 +603,16 @@ export class Editor {
window.history.replaceState({}, document.title, "/"); window.history.replaceState({}, document.title, "/");
}; };
parseHash = (hash: string) => {
return JSON.parse(atob(hash));
};
share() { share() {
const hashed_table = JSON.stringify( const hashed_table = JSON.stringify(
this.settings.universes[this.selected_universe] this.settings.universes[this.selected_universe]
); );
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.searchParams.set( url.searchParams.set("universe", hashed_table);
"universe",
this.selected_universe + "-" + hashed_table
);
window.history.replaceState({}, "", url.toString()); window.history.replaceState({}, "", url.toString());
} }