doc + Mask

This commit is contained in:
JulienH2000
2023-09-21 22:02:58 +02:00

View File

@ -1,6 +1,7 @@
import { type UserAPI } from "./API"; import { type UserAPI } from "./API";
import { SCALES } from "./Scales"; import { SCALES } from "./Scales";
export {}; import { safeScale } from "zifferjs";
export { };
declare global { declare global {
interface Array<T> { interface Array<T> {
@ -21,6 +22,8 @@ declare global {
pick(): T; pick(): T;
loop(index: number): T; loop(index: number): T;
shuffle(): this; shuffle(): this;
scale(name: string, base_note?: number): this;
arp(scaleName: string): this;
rotate(steps: number): this; rotate(steps: number): this;
unique(): this; unique(): this;
in(value: T): boolean; in(value: T): boolean;
@ -35,14 +38,14 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.includes(value); return this.includes(value);
}; };
Array.prototype.square = function (): number[] { Array.prototype.square = function(): number[] {
/** /**
* @returns New array with squared values. * @returns New array with squared values.
*/ */
return this.map((x: number) => x * x); return this.map((x: number) => x * x);
}; };
Array.prototype.sqrt = function (): number[] { Array.prototype.sqrt = function(): number[] {
/** /**
* @returns New array with square roots of values. Throws if any element is negative. * @returns New array with square roots of values. Throws if any element is negative.
*/ */
@ -51,7 +54,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => Math.sqrt(x)); return this.map((x: number) => Math.sqrt(x));
}; };
Array.prototype.add = function (amount: number): number[] { Array.prototype.add = function(amount: number): number[] {
/** /**
* @param amount - The value to add to each element in the array. * @param amount - The value to add to each element in the array.
* @returns New array with added values. * @returns New array with added values.
@ -59,7 +62,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x + amount); return this.map((x: number) => x + amount);
}; };
Array.prototype.sub = function (amount: number): number[] { Array.prototype.sub = function(amount: number): number[] {
/** /**
* @param amount - The value to subtract from each element in the array. * @param amount - The value to subtract from each element in the array.
* @returns New array with subtracted values. * @returns New array with subtracted values.
@ -67,7 +70,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x - amount); return this.map((x: number) => x - amount);
}; };
Array.prototype.mult = function (amount: number): number[] { Array.prototype.mult = function(amount: number): number[] {
/** /**
* @param amount - The value to multiply with each element in the array. * @param amount - The value to multiply with each element in the array.
* @returns New array with multiplied values. * @returns New array with multiplied values.
@ -75,7 +78,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x * amount); return this.map((x: number) => x * amount);
}; };
Array.prototype.div = function (amount: number): number[] { Array.prototype.div = function(amount: number): number[] {
/** /**
* @param amount - The value to divide each element in the array by. * @param amount - The value to divide each element in the array by.
* @returns New array with divided values. Throws if division by zero. * @returns New array with divided values. Throws if division by zero.
@ -84,7 +87,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x / amount); return this.map((x: number) => x / amount);
}; };
Array.prototype.pick = function () { Array.prototype.pick = function() {
/** /**
* Returns a random element from an array. * Returns a random element from an array.
* *
@ -93,7 +96,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[Math.floor(api.randomGen() * this.length)]; return this[Math.floor(api.randomGen() * this.length)];
}; };
Array.prototype.gen = function (min: number, max: number, times: number) { Array.prototype.gen = function(min: number, max: number, times: number) {
/** /**
* Returns an array of random numbers. * Returns an array of random numbers.
* @param min - The minimum value of the random numbers * @param min - The minimum value of the random numbers
@ -110,7 +113,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
); );
}; };
Array.prototype.bar = function () { Array.prototype.bar = function() {
/** /**
* Returns an element from an array based on the current bar. * Returns an element from an array based on the current bar.
* *
@ -119,7 +122,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[api.app.clock.time_position.bar % this.length]; return this[api.app.clock.time_position.bar % this.length];
}; };
Array.prototype.pulse = function () { Array.prototype.pulse = function() {
/** /**
* Returns an element from an array based on the current pulse. * Returns an element from an array based on the current pulse.
* *
@ -128,7 +131,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[api.app.clock.time_position.pulse % this.length]; return this[api.app.clock.time_position.pulse % this.length];
}; };
Array.prototype.beat = function (divisor: number = 1) { Array.prototype.beat = function(divisor: number = 1) {
const chunk_size = divisor; // Get the first argument (chunk size) const chunk_size = divisor; // Get the first argument (chunk size)
const timepos = api.app.clock.pulses_since_origin; const timepos = api.app.clock.pulses_since_origin;
const slice_count = Math.floor( const slice_count = Math.floor(
@ -137,7 +140,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[slice_count % this.length]; return this[slice_count % this.length];
}; };
Array.prototype.shuffle = function () { Array.prototype.shuffle = function() {
/** /**
* Shuffles the array in place. * Shuffles the array in place.
* *
@ -156,7 +159,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this; return this;
}; };
Array.prototype.rotate = function (steps: number) { Array.prototype.rotate = function(steps: number) {
/** /**
* Rotates the array in place. * Rotates the array in place.
* *
@ -176,7 +179,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this; return this;
}; };
Array.prototype.unique = function () { Array.prototype.unique = function() {
/** /**
* Removes duplicate elements from the array in place. * Removes duplicate elements from the array in place.
* *
@ -209,7 +212,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
if (this.length <= 1) { if (this.length <= 1) {
return this; return this;
} }
for (let i = 0; i < this.length; ) { for (let i = 0; i < this.length;) {
const rand = api.randomGen() * 100; const rand = api.randomGen() * 100;
if (rand < amount) { if (rand < amount) {
if (this.length > 1) { if (this.length > 1) {
@ -239,7 +242,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
for (let j = 0; j < amount; j++) { for (let j = 0; j < amount; j++) {
result.push(this[i]); result.push(this[i]);
} }
} }
this.length = 0; this.length = 0;
this.push(...result); this.push(...result);
@ -322,7 +325,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return left_to_right.concat(right_to_left); return left_to_right.concat(right_to_left);
}; };
Array.prototype.loop = function (index: number) { Array.prototype.loop = function(index: number) {
/** /**
* Returns an element from the array based on the index. * Returns an element from the array based on the index.
* The index will wrap over the array. * The index will wrap over the array.
@ -333,7 +336,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[index % this.length]; return this[index % this.length];
}; };
Array.prototype.random = function () { Array.prototype.random = function() {
/** /**
* Returns a random element from the array. * Returns a random element from the array.
* *
@ -354,10 +357,11 @@ Array.prototype.scale = function <T>(this: T[], scaleName: string = "major", mas
const scale = SCALES[scaleName]; const scale = SCALES[scaleName];
let mask = maskLength; let mask = maskLength;
//input protect from unknow scale //input protect from unknow scale
if (!scale) {
throw new Error(`Unknown scale ${scaleName}`); if (!scale) {
} throw new Error(`Unknown scale ${scaleName}`);
}
//input protect from to long mask //input protect from to long mask
if (mask > scale.length) { if (mask > scale.length) {