This commit is contained in:
tyow 2026-01-23 13:30:36 -05:00
parent 3abcc59874
commit 1e771d6150

View file

@ -6,6 +6,8 @@ This program is free software: you can redistribute it and/or modify it under th
import { register, _mod, parseNumeral, removeUndefineds } from '@strudel/core'; import { register, _mod, parseNumeral, removeUndefineds } from '@strudel/core';
// returns a list of frequency ratios for given edo scale
export function edo(name) { export function edo(name) {
if (!/^[1-9]+[0-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 + '"');
@ -18,12 +20,15 @@ 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],
}; };
// Given a base frequency such as 220 and an edo scale, returns
// an array of frequencies representing the given edo scale in that base
function withBase(freq, scale) { function withBase(freq, scale) {
return scale.map((r) => r * freq); return scale.map((r) => r * freq);
} }
const defaultBase = 220; const defaultBase = 220;
// Assumes a base of 220. Returns a filtered scale based on 'indices'
function getXenScale(scale, indices) { function getXenScale(scale, indices) {
if (typeof scale === 'string') { if (typeof scale === 'string') {
if (/^[1-9]+[0-9]*edo$/.test(scale)) { if (/^[1-9]+[0-9]*edo$/.test(scale)) {
@ -41,12 +46,17 @@ function getXenScale(scale, indices) {
return scale.filter((_, i) => indices.includes(i)); return scale.filter((_, i) => indices.includes(i));
} }
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] * Math.pow(2, oct); return xenScale[i] * Math.pow(2, oct);
} }
// accepts a scale name such as 31edo, and a pattern
// pattern expected to follow format such that a value can be mapped
// to an edostep within the scale. Returns the pattern with
// values mapped to the frequencies associated with the given edosteps
// scaleNameOrRatios: string || number[], steps?: number // scaleNameOrRatios: string || number[], steps?: number
export const xen = register('xen', function (scaleNameOrRatios, pat) { export const xen = register('xen', function (scaleNameOrRatios, pat) {
return pat.withHaps((haps) => { return pat.withHaps((haps) => {
@ -65,6 +75,7 @@ export const xen = register('xen', function (scaleNameOrRatios, pat) {
}) })
}); });
// not sure there's a point to having this and the above, seems like a proto version of the above.
export const tuning = register('tuning', function (ratios, pat) { export const tuning = register('tuning', function (ratios, pat) {
return pat.withHap((hap) => { return pat.withHap((hap) => {
const frequency = xenOffset(ratios, parseNumeral(hap.value)); const frequency = xenOffset(ratios, parseNumeral(hap.value));