Fixing logic

This commit is contained in:
2023-09-21 21:06:35 +02:00
parent 20189941c4
commit 6076ea226f

View File

@ -1,6 +1,6 @@
import { type UserAPI } from "./API";
import { SCALES } from "./Scales";
export {};
export { };
declare global {
interface Array<T> {
@ -21,6 +21,7 @@ declare global {
pick(): T;
loop(index: number): T;
shuffle(): this;
scale(name: string, base_note?: number): this;
rotate(steps: number): this;
unique(): this;
in(value: T): boolean;
@ -35,14 +36,14 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.includes(value);
};
Array.prototype.square = function (): number[] {
Array.prototype.square = function(): number[] {
/**
* @returns New array with squared values.
*/
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.
*/
@ -51,7 +52,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* @returns New array with added values.
@ -59,7 +60,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* @returns New array with subtracted values.
@ -67,7 +68,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* @returns New array with multiplied values.
@ -75,7 +76,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* @returns New array with divided values. Throws if division by zero.
@ -84,7 +85,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x / amount);
};
Array.prototype.pick = function () {
Array.prototype.pick = function() {
/**
* Returns a random element from an array.
*
@ -93,7 +94,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* @param min - The minimum value of the random numbers
@ -110,7 +111,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.
*
@ -119,7 +120,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
*
@ -128,7 +129,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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 timepos = api.app.clock.pulses_since_origin;
const slice_count = Math.floor(
@ -137,7 +138,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[slice_count % this.length];
};
Array.prototype.shuffle = function () {
Array.prototype.shuffle = function() {
/**
* Shuffles the array in place.
*
@ -156,7 +157,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.rotate = function (steps: number) {
Array.prototype.rotate = function(steps: number) {
/**
* Rotates the array in place.
*
@ -176,7 +177,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.unique = function () {
Array.prototype.unique = function() {
/**
* Removes duplicate elements from the array in place.
*
@ -209,7 +210,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
if (this.length <= 1) {
return this;
}
for (let i = 0; i < this.length; ) {
for (let i = 0; i < this.length;) {
const rand = api.randomGen() * 100;
if (rand < amount) {
if (this.length > 1) {
@ -239,7 +240,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
for (let j = 0; j < amount; j++) {
result.push(this[i]);
}
}
}
this.length = 0;
this.push(...result);
@ -322,7 +323,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
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.
* The index will wrap over the array.
@ -333,7 +334,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[index % this.length];
};
Array.prototype.random = function () {
Array.prototype.random = function() {
/**
* Returns a random element from the array.
*
@ -344,25 +345,18 @@ export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.rand = Array.prototype.random;
};
Array.prototype.scale = function <T>(this: T[], scaleName: string = "major") {
const scale = SCALES[scaleName];
//input protect from unknow scale
if (!scale) {
throw new Error(`Unknown scale ${scaleName}`);
}
let result = [];
for (let j = 0; j < scale.length; j++) {
for (let i = 0; i < this.length; i++) {
result.push(this[i] + scale[j]);
}
}
return result;
Array.prototype.scale = function(scale: string = "major", base_note: number = 0) {
/**
* Returns a note from an array containing a specific scale.
*
* @param scale - the scale name (string)
* @param base_note - the base note to start at (MIDI note number)
*
*/
if (SCALES.hasOwnProperty(scale)) {
const selected_scale = SCALES[scale]
return this.map((value) => selected_scale[value % selected_scale.length] + base_note);
} else {
return this.map((value) => value + base_note);
}
};