some fun functions
This commit is contained in:
58
src/API.ts
58
src/API.ts
@ -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 {
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -143,7 +155,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,7 +302,10 @@ 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)) {
|
||||||
@ -302,12 +314,12 @@ export class UserAPI {
|
|||||||
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) {
|
if ((player && player.ziffers.index === -1) || player.played) {
|
||||||
player.callTime = this.epulse();
|
player.callTime = this.epulse();
|
||||||
player.played = false;
|
player.played = false;
|
||||||
}
|
}
|
||||||
return player;
|
return player;
|
||||||
}
|
};
|
||||||
|
|
||||||
// =============================================================
|
// =============================================================
|
||||||
// Counter related functions
|
// Counter related functions
|
||||||
@ -570,20 +582,20 @@ export class UserAPI {
|
|||||||
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
|
||||||
@ -690,7 +702,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -700,7 +712,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -710,7 +722,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -720,7 +732,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -730,7 +742,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -740,7 +752,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -750,7 +762,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
@ -760,7 +772,7 @@ export class UserAPI {
|
|||||||
* @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 => {
|
||||||
|
|||||||
@ -130,7 +130,7 @@ export class AppSettings {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get_universe(universe_name: string) {
|
get_universe() {
|
||||||
this.universes.universe_name;
|
this.universes.universe_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/main.ts
18
src/main.ts
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user