Small corrections

This commit is contained in:
2023-09-21 22:28:28 +02:00
parent ab17f5d6e9
commit ba271e1f3a

View File

@ -22,7 +22,7 @@ declare global {
loop(index: number): T; loop(index: number): T;
shuffle(): this; shuffle(): this;
scale(name: string, base_note?: number): this; scale(name: string, base_note?: number): this;
arp(scaleName: string): this; scaleArp(scaleName: string): this;
rotate(steps: number): this; rotate(steps: number): this;
unique(): this; unique(): this;
in(value: T): boolean; in(value: T): boolean;
@ -374,47 +374,40 @@ Array.prototype.scale = function (
* @param scale - the scale name * @param scale - the scale name
* @param base_note - the base note to start at (MIDI note number) * @param base_note - the base note to start at (MIDI note number)
* *
* @returns notes from the desired scalek * @returns notes from the desired scale
*/ */
// This is a helper function to handle up or down octaviation. // This is a helper function to handle up or down octaviation.
const mod = (n: number, m: number) => ((n % m) + m) % m; const mod = (n: number, m: number) => ((n % m) + m) % m;
const selected_scale = safeScale[scale];
if (SCALES.hasOwnProperty(scale)) { return this.map((value) => {
const selected_scale = SCALES[scale]; const octaveShift = Math.floor(value / selected_scale.length) * 12;
return this.map((value) => { return (
const octaveShift = Math.floor(value / selected_scale.length) * 12; selected_scale[mod(value, selected_scale.length)] +
return ( base_note +
selected_scale[mod(value, selected_scale.length)] + octaveShift
base_note + );
octaveShift });
);
});
} else {
return this.map((value) => value + base_note);
}
}; };
Array.prototype.arp = function (scaleName: string = "major", mask: number = 0) { Array.prototype.scaleArp = function (
scaleName: string = "major",
boundary: number = 0
) {
/* /*
* @param scaleName - the scale name * @param scaleName - the scale name
* @param mask - the length of the mask * @param mask - the length of the mask
* *
* @returns arpeggiated notes from the scale
*/ */
const scale = safeScale[scaleName]; const scale = safeScale[scaleName];
//input protect from unknow scale
if (!scale) {
throw new Error(`Unknown scale ${scaleName}`);
}
let result = []; let result = [];
mask = mask > scale.length ? scale.length : mask; boundary = boundary > scale.length ? scale.length : boundary;
mask = mask == 0 ? scale.length : mask; boundary = boundary == 0 ? scale.length : boundary;
for (let j = 0; j < mask; j++) { for (let j = 0; j < boundary; j++) {
for (let i = 0; i < this.length; i++) { for (let i = 0; i < this.length; i++) {
result.push(this[i] + scale[j]); result.push(this[i] + scale[j]);
} }