Fixing logic
This commit is contained in:
@ -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;
|
||||
@ -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}`);
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user