From a838624ac8231b76d63f188c01aad5bfa7762c69 Mon Sep 17 00:00:00 2001 From: JulienH2000 Date: Thu, 21 Sep 2023 19:09:06 +0200 Subject: [PATCH] Working scale fonction :) --- src/API.ts | 1 + src/ArrayExtensions.ts | 28 +++++++++++++++++++++++++++- src/Scales.ts | 11 ++++++++--- src/main.ts | 2 ++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/API.ts b/src/API.ts index 871b99e..ee919a0 100644 --- a/src/API.ts +++ b/src/API.ts @@ -7,6 +7,7 @@ import { Editor } from "./main"; import { SoundEvent } from "./classes/SoundEvent"; import { MidiEvent } from "./classes/MidiEvent"; import { LRUCache } from "lru-cache"; +import { SCALES } from "./Scales" import { InputOptions, Player } from "./classes/ZPlayer"; import { samples, diff --git a/src/ArrayExtensions.ts b/src/ArrayExtensions.ts index b87e53a..894a19f 100644 --- a/src/ArrayExtensions.ts +++ b/src/ArrayExtensions.ts @@ -1,4 +1,5 @@ import { type UserAPI } from "./API"; +import { SCALES } from "./Scales"; export {}; declare global { @@ -238,7 +239,8 @@ export const makeArrayExtensions = (api: UserAPI) => { for (let j = 0; j < amount; j++) { result.push(this[i]); } - } + } + this.length = 0; this.push(...result); return this; @@ -341,3 +343,27 @@ export const makeArrayExtensions = (api: UserAPI) => { }; Array.prototype.rand = Array.prototype.random; }; + +Array.prototype.scale = function (this: T[], scaleName: string = "major") { + + const scale = SCALES[scaleName]; + + if (!scale) { + throw new Error(`Unknown scale ${scaleName}`); + } + + let result = []; + + for (let i = 0; i < scale.length; i++) { + + if (!this[i]) { + result.push(this[0] + scale[i]); + } else { + result.push(this[i] + scale[i]); + } + } + + this.shift() + this.push(...result); + return this; +}; diff --git a/src/Scales.ts b/src/Scales.ts index de046f8..1850fc3 100644 --- a/src/Scales.ts +++ b/src/Scales.ts @@ -1,4 +1,4 @@ -const SCALES: Record = { +export const SCALES: Record = { major: [0, 2, 4, 5, 7, 9, 11], naturalMinor: [0, 2, 3, 5, 7, 8, 10], harmonicMinor: [0, 2, 3, 5, 7, 8, 11], @@ -53,18 +53,21 @@ const SCALES: Record = { orientalA: [0, 1, 4, 5, 6, 9, 10], }; + +// Legacy function, see Array.prototype.scale @ ArrayExtensions.ts +/* export function scale( n: number, scaleName: string = "major", octave: number = 4 ): number { - /** + /* * Returns the MIDI note number for the given scale degree in the given scale. * @param {number} n - The scale degree, where 0 is the tonic. * @param {string} scaleName - The name of the scale. * @param {number} octave - The octave number. * @returns {number} The MIDI note number. - */ + * / const scale = SCALES[scaleName]; if (!scale) { @@ -76,3 +79,5 @@ export function scale( let additionalOctaves = Math.floor(n / scale.length); return 60 + (octave + additionalOctaves) * 12 + scale[index]; } + +*/ diff --git a/src/main.ts b/src/main.ts index 6ae6b33..fc2e8e4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -66,6 +66,8 @@ const bindings = Object.keys(classMap).map((key) => ({ replace: (match, p1) => `<${key} class="${classMap[key]}" ${p1}>`, })); + + export class Editor { universes: Universes = template_universes; selected_universe: string;