Working scale fonction :)

This commit is contained in:
JulienH2000
2023-09-21 19:09:06 +02:00
parent 4e4a2ac0bf
commit a838624ac8
4 changed files with 38 additions and 4 deletions

View File

@ -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,

View File

@ -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 <T>(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;
};

View File

@ -1,4 +1,4 @@
const SCALES: Record<string, number[]> = {
export const SCALES: Record<string, number[]> = {
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<string, number[]> = {
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];
}
*/

View File

@ -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;