From 512523d74f076a1832c0849df3f9686c36e51b43 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Thu, 21 Sep 2023 21:12:50 +0200 Subject: [PATCH] adding octaviation up or down --- src/ArrayExtensions.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ArrayExtensions.ts b/src/ArrayExtensions.ts index a6663a7..202a5e3 100644 --- a/src/ArrayExtensions.ts +++ b/src/ArrayExtensions.ts @@ -347,15 +347,35 @@ export const makeArrayExtensions = (api: UserAPI) => { 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 scale - the scale name * @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); + const selected_scale = SCALES[scale]; + return this.map((value) => { + const octaveShift = Math.floor(value / selected_scale.length) * 12 * Math.sign(value); + return selected_scale[Math.abs(value) % selected_scale.length] + base_note + octaveShift; + }); + } else { + return this.map((value) => value + base_note); + } +}; + + +Array.prototype.scale = function(scale: string = "major", base_note: number = 0) { + /** + * @param scale - the scale name + * @param base_note - the base note to start at (MIDI note number) + */ + + // This is a helper function to handle up or down octaviation. + const mod = (n: number, m: number) => ((n % m) + m) % m; + if (SCALES.hasOwnProperty(scale)) { + const selected_scale = SCALES[scale]; + return this.map((value) => { + const octaveShift = Math.floor(value / selected_scale.length) * 12; + return selected_scale[mod(value, selected_scale.length)] + base_note + octaveShift; + }); } else { return this.map((value) => value + base_note); }