Working scale fonction :)
This commit is contained in:
@ -7,6 +7,7 @@ import { Editor } from "./main";
|
|||||||
import { SoundEvent } from "./classes/SoundEvent";
|
import { SoundEvent } from "./classes/SoundEvent";
|
||||||
import { MidiEvent } from "./classes/MidiEvent";
|
import { MidiEvent } from "./classes/MidiEvent";
|
||||||
import { LRUCache } from "lru-cache";
|
import { LRUCache } from "lru-cache";
|
||||||
|
import { SCALES } from "./Scales"
|
||||||
import { InputOptions, Player } from "./classes/ZPlayer";
|
import { InputOptions, Player } from "./classes/ZPlayer";
|
||||||
import {
|
import {
|
||||||
samples,
|
samples,
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { type UserAPI } from "./API";
|
import { type UserAPI } from "./API";
|
||||||
|
import { SCALES } from "./Scales";
|
||||||
export {};
|
export {};
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
@ -238,7 +239,8 @@ export const makeArrayExtensions = (api: UserAPI) => {
|
|||||||
for (let j = 0; j < amount; j++) {
|
for (let j = 0; j < amount; j++) {
|
||||||
result.push(this[i]);
|
result.push(this[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
this.push(...result);
|
this.push(...result);
|
||||||
return this;
|
return this;
|
||||||
@ -341,3 +343,27 @@ export const makeArrayExtensions = (api: UserAPI) => {
|
|||||||
};
|
};
|
||||||
Array.prototype.rand = Array.prototype.random;
|
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;
|
||||||
|
};
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
const SCALES: Record<string, number[]> = {
|
export const SCALES: Record<string, number[]> = {
|
||||||
major: [0, 2, 4, 5, 7, 9, 11],
|
major: [0, 2, 4, 5, 7, 9, 11],
|
||||||
naturalMinor: [0, 2, 3, 5, 7, 8, 10],
|
naturalMinor: [0, 2, 3, 5, 7, 8, 10],
|
||||||
harmonicMinor: [0, 2, 3, 5, 7, 8, 11],
|
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],
|
orientalA: [0, 1, 4, 5, 6, 9, 10],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Legacy function, see Array.prototype.scale @ ArrayExtensions.ts
|
||||||
|
/*
|
||||||
export function scale(
|
export function scale(
|
||||||
n: number,
|
n: number,
|
||||||
scaleName: string = "major",
|
scaleName: string = "major",
|
||||||
octave: number = 4
|
octave: number = 4
|
||||||
): number {
|
): number {
|
||||||
/**
|
/*
|
||||||
* Returns the MIDI note number for the given scale degree in the given scale.
|
* 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 {number} n - The scale degree, where 0 is the tonic.
|
||||||
* @param {string} scaleName - The name of the scale.
|
* @param {string} scaleName - The name of the scale.
|
||||||
* @param {number} octave - The octave number.
|
* @param {number} octave - The octave number.
|
||||||
* @returns {number} The MIDI note number.
|
* @returns {number} The MIDI note number.
|
||||||
*/
|
* /
|
||||||
const scale = SCALES[scaleName];
|
const scale = SCALES[scaleName];
|
||||||
|
|
||||||
if (!scale) {
|
if (!scale) {
|
||||||
@ -76,3 +79,5 @@ export function scale(
|
|||||||
let additionalOctaves = Math.floor(n / scale.length);
|
let additionalOctaves = Math.floor(n / scale.length);
|
||||||
return 60 + (octave + additionalOctaves) * 12 + scale[index];
|
return 60 + (octave + additionalOctaves) * 12 + scale[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|||||||
@ -66,6 +66,8 @@ const bindings = Object.keys(classMap).map((key) => ({
|
|||||||
replace: (match, p1) => `<${key} class="${classMap[key]}" ${p1}>`,
|
replace: (match, p1) => `<${key} class="${classMap[key]}" ${p1}>`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class Editor {
|
export class Editor {
|
||||||
universes: Universes = template_universes;
|
universes: Universes = template_universes;
|
||||||
selected_universe: string;
|
selected_universe: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user