diff --git a/repl/src/evaluate.ts b/repl/src/evaluate.ts index a4e8e6f1..8e43f34a 100644 --- a/repl/src/evaluate.ts +++ b/repl/src/evaluate.ts @@ -3,6 +3,7 @@ import './tone'; import './midi'; import './voicings'; import './tonal.mjs'; +import './xen.mjs'; import shapeshifter from './shapeshifter'; import { minify } from './parse'; import * as Tone from 'tone'; diff --git a/repl/src/xen.mjs b/repl/src/xen.mjs new file mode 100644 index 00000000..6ae1285a --- /dev/null +++ b/repl/src/xen.mjs @@ -0,0 +1,37 @@ +import { Pattern } from '../../strudel.mjs'; +import { mod } from '../../util.mjs'; + +function edo(name) { + if (!/^[1-9]+edo$/.test(name)) { + throw new Error('not an edo scale: "' + name + '"'); + } + const [_, divisions] = name.match(/^([1-9]+)edo$/); + return Array.from({ length: divisions }, (_, i) => Math.pow(2, i / divisions)); +} + +function getXenScale(scale, indices) { + if (typeof scale === 'string') { + if (/^[1-9]+edo$/.test(scale)) { + return edo(scale); + } + throw new Error('unknown scale name: "' + scale + '"'); + } + if (!indices) { + return scale; + } + return scale.filter((_, i) => indices.includes(i)); +} + +function xenOffset(xenScale, offset, index = 0) { + const i = mod(index + offset, xenScale.length); + const oct = Math.floor(offset / xenScale.length); + return xenScale[i] * 440 * Math.pow(2, oct); +} + +Pattern.prototype._xen = function (scaleNameOrRatios) { + return this._asNumber()._withEvent((event) => { + const frequency = xenOffset(getXenScale(scaleNameOrRatios), event.value); + return event.withValue(() => frequency).setContext({ ...event.context, type: 'frequency' }); + }); +}; +Pattern.prototype.define('xen', (scale, pat) => pat.xen(scale), { composable: true, patternified: true });