xen fixes
This commit is contained in:
parent
29802b0f4d
commit
43f861e219
1 changed files with 22 additions and 11 deletions
|
|
@ -2,10 +2,10 @@ import { Pattern } from '../../strudel.mjs';
|
||||||
import { mod } from '../../util.mjs';
|
import { mod } from '../../util.mjs';
|
||||||
|
|
||||||
function edo(name) {
|
function edo(name) {
|
||||||
if (!/^[1-9]+edo$/.test(name)) {
|
if (!/^[1-9]+[0-9]*edo$/.test(name)) {
|
||||||
throw new Error('not an edo scale: "' + name + '"');
|
throw new Error('not an edo scale: "' + name + '"');
|
||||||
}
|
}
|
||||||
const [_, divisions] = name.match(/^([1-9]+)edo$/);
|
const [_, divisions] = name.match(/^([1-9]+[0-9]*)edo$/);
|
||||||
return Array.from({ length: divisions }, (_, i) => Math.pow(2, i / divisions));
|
return Array.from({ length: divisions }, (_, i) => Math.pow(2, i / divisions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -13,16 +13,23 @@ const presets = {
|
||||||
'12ji': [1 / 1, 16 / 15, 9 / 8, 6 / 5, 5 / 4, 4 / 3, 45 / 32, 3 / 2, 8 / 5, 5 / 3, 16 / 9, 15 / 8],
|
'12ji': [1 / 1, 16 / 15, 9 / 8, 6 / 5, 5 / 4, 4 / 3, 45 / 32, 3 / 2, 8 / 5, 5 / 3, 16 / 9, 15 / 8],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function withBase(freq, scale) {
|
||||||
|
return scale.map((r) => r * freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultBase = 220;
|
||||||
|
|
||||||
function getXenScale(scale, indices) {
|
function getXenScale(scale, indices) {
|
||||||
if (typeof scale === 'string') {
|
if (typeof scale === 'string') {
|
||||||
if (/^[1-9]+edo$/.test(scale)) {
|
if (/^[1-9]+[0-9]*edo$/.test(scale)) {
|
||||||
return edo(scale);
|
scale = edo(scale);
|
||||||
|
} else if (presets[scale]) {
|
||||||
|
scale = presets[scale];
|
||||||
|
} else {
|
||||||
|
throw new Error('unknown scale name: "' + scale + '"');
|
||||||
}
|
}
|
||||||
if (presets[scale]) {
|
|
||||||
return presets[scale];
|
|
||||||
}
|
|
||||||
throw new Error('unknown scale name: "' + scale + '"');
|
|
||||||
}
|
}
|
||||||
|
scale = withBase(defaultBase, scale);
|
||||||
if (!indices) {
|
if (!indices) {
|
||||||
return scale;
|
return scale;
|
||||||
}
|
}
|
||||||
|
|
@ -32,15 +39,19 @@ function getXenScale(scale, indices) {
|
||||||
function xenOffset(xenScale, offset, index = 0) {
|
function xenOffset(xenScale, offset, index = 0) {
|
||||||
const i = mod(index + offset, xenScale.length);
|
const i = mod(index + offset, xenScale.length);
|
||||||
const oct = Math.floor(offset / xenScale.length);
|
const oct = Math.floor(offset / xenScale.length);
|
||||||
return xenScale[i] * 440 * Math.pow(2, oct);
|
return xenScale[i] * Math.pow(2, oct);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pattern.prototype._xen = function (scaleNameOrRatios) {
|
// scaleNameOrRatios: string || number[], steps?: number
|
||||||
|
Pattern.prototype._xen = function (scaleNameOrRatios, steps) {
|
||||||
return this._asNumber()._withEvent((event) => {
|
return this._asNumber()._withEvent((event) => {
|
||||||
const frequency = xenOffset(getXenScale(scaleNameOrRatios), event.value);
|
const scale = getXenScale(scaleNameOrRatios);
|
||||||
|
steps = steps || scale.length;
|
||||||
|
const frequency = xenOffset(scale, event.value);
|
||||||
return event.withValue(() => frequency).setContext({ ...event.context, type: 'frequency' });
|
return event.withValue(() => frequency).setContext({ ...event.context, type: 'frequency' });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Pattern.prototype.tuning = function (steps) {
|
Pattern.prototype.tuning = function (steps) {
|
||||||
return this._asNumber()._withEvent((event) => {
|
return this._asNumber()._withEvent((event) => {
|
||||||
const frequency = xenOffset(steps, event.value);
|
const frequency = xenOffset(steps, event.value);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue