Fixing logic

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

View File

@ -21,6 +21,7 @@ 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;
rotate(steps: number): this; rotate(steps: number): this;
unique(): this; unique(): this;
in(value: T): boolean; in(value: T): boolean;
@ -344,25 +345,18 @@ export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.rand = Array.prototype.random; Array.prototype.rand = Array.prototype.random;
}; };
Array.prototype.scale = function <T>(this: T[], scaleName: string = "major") { Array.prototype.scale = function(scale: string = "major", base_note: number = 0) {
/**
const scale = SCALES[scaleName]; * Returns a note from an array containing a specific scale.
*
//input protect from unknow scale * @param scale - the scale name (string)
if (!scale) { * @param base_note - the base note to start at (MIDI note number)
throw new Error(`Unknown scale ${scaleName}`); *
*/
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);
} }
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;
}; };