diff --git a/packages/xen/xen.mjs b/packages/xen/xen.mjs index 2282ea7f0..759e488f7 100644 --- a/packages/xen/xen.mjs +++ b/packages/xen/xen.mjs @@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see . */ -import { register, _mod, parseNumeral, removeUndefineds, Pattern } from '@strudel/core'; +import { register, _mod, parseNumeral, removeUndefineds, Pattern, parray, reify } from '@strudel/core'; import Tune from './tunejs.js'; // returns a list of frequency ratios for given edo scale @@ -61,20 +61,7 @@ function xenOffset(xenScale, offset, index = 0) { const trimFreq = (freq) => parseFloat(freq.toPrecision(10)); -// const JIsc3 = register('JIsc3', (input) => { -// let k = input - -// return new Pattern((state) => { -// let res = k.query(state) -// res = parray(k.query(state).map(x=>{ -// let c = x.context -// x = eval(x.value.join('/')) -// let r = reify(x).setContext(c) -// return r -// })).query(state) -// return res -// }) -// }) + // accepts a scale name such as 31edo, and a pattern // pattern expected to follow format such that a value can be mapped @@ -112,24 +99,55 @@ const trimFreq = (freq) => parseFloat(freq.toPrecision(10)); */ export const xen = register('xen', function (scaleNameOrRatios, pat) { + const JIsc3 = (input) => { + let k = input + + return new Pattern((state) => { + let res = k.query(state) + res = parray(k.query(state).map(x => { + let c = x.context + x = eval(x.value.join('/')) + let r = reify(x).setContext(c) + return r + })).query(state) + return res + }) + } + const query = state => { - let haps = pat.withHap((hap)=> { + let haps = pat.withHap((hap) => { let hVal = hap.value; const isObject = typeof hVal === 'object'; if (!isObject) { throw new Error(`Expected hap to have control 'i' set, but received ${hap.value.i}, try wrapping input in i()`); } const { i, ...otherValues } = hVal; - const scale = getXenScale(scaleNameOrRatios.query(state)[0].value); - // scale probably needs to not be turned into pure vals - // maintain hap functionality so highlighting works - + // const scale = getXenScale(JIsc3(scaleNameOrRatios).query(state)[0].value); + let scale + let edo = false + if (scaleNameOrRatios.__pure != undefined) { + scale = getXenScale(scaleNameOrRatios.__pure) + } else { + let sc = scaleNameOrRatios.splitQueries().query(state); + let nameMaybe = sc[0].value + if (typeof sc[0].value == 'string') { + scale = getXenScale(sc[0].value) + if (isEdo(nameMaybe)) { + edo = nameMaybe.match(/^([1-9]+[0-9]*)edo$/)[1] + } + hap = hap.setContext(hap.combineContext(sc[0])) + } else { + // we can do [0] here because JIsc3 uses parray + scale = getXenScale(JIsc3(scaleNameOrRatios).query(state)[0].value); + hap = hap.setContext(hap.combineContext(scaleNameOrRatios.query(state)[i])) + } + } let freq = xenOffset(scale, parseNumeral(hVal.i)); // 10 is somewhat arbitrary freq = trimFreq(freq); hap.value = { ...otherValues, freq }; - return isEdo(scaleNameOrRatios) - ? hap.setContext({ ...hap.context, edoSize: scaleNameOrRatios.match(/^([1-9]+[0-9]*)edo$/)[1] }) + return edo + ? hap.setContext({ ...hap.context, edoSize: edo }) : hap; }).query(state); return removeUndefineds(haps); diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 458904baf..54ab8a879 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -5841,29 +5841,29 @@ exports[`runs examples > example "i" example index 0 1`] = ` "[ 3/4 → 7/8 | freq:505.4272762 ]", "[ 7/8 → 1/1 | freq:580.5834807 ]", "[ 1/1 → 9/8 | freq:220 ]", - "[ 9/8 → 5/4 | freq:235.7901618 ]", - "[ 5/4 → 11/8 | freq:252.7136381 ]", - "[ 11/8 → 3/2 | freq:270.8517709 ]", - "[ 3/2 → 13/8 | freq:290.2917404 ]", - "[ 13/8 → 7/4 | freq:311.1269837 ]", - "[ 7/4 → 15/8 | freq:333.4576446 ]", - "[ 15/8 → 2/1 | freq:357.3910544 ]", + "[ 9/8 → 5/4 | freq:252.7136381 ]", + "[ 5/4 → 11/8 | freq:290.2917404 ]", + "[ 11/8 → 3/2 | freq:333.4576446 ]", + "[ 3/2 → 13/8 | freq:383.0422479 ]", + "[ 13/8 → 7/4 | freq:440 ]", + "[ 7/4 → 15/8 | freq:505.4272762 ]", + "[ 15/8 → 2/1 | freq:580.5834807 ]", "[ 2/1 → 17/8 | freq:220 ]", - "[ 17/8 → 9/4 | freq:230.404707 ]", - "[ 9/4 → 19/8 | freq:241.3014955 ]", - "[ 19/8 → 5/2 | freq:252.7136381 ]", - "[ 5/2 → 21/8 | freq:264.6655079 ]", - "[ 21/8 → 11/4 | freq:277.182631 ]", - "[ 11/4 → 23/8 | freq:290.2917404 ]", - "[ 23/8 → 3/1 | freq:304.0208336 ]", + "[ 17/8 → 9/4 | freq:252.7136381 ]", + "[ 9/4 → 19/8 | freq:290.2917404 ]", + "[ 19/8 → 5/2 | freq:333.4576446 ]", + "[ 5/2 → 21/8 | freq:383.0422479 ]", + "[ 21/8 → 11/4 | freq:440 ]", + "[ 11/4 → 23/8 | freq:505.4272762 ]", + "[ 23/8 → 3/1 | freq:580.5834807 ]", "[ 3/1 → 25/8 | freq:220 ]", - "[ 25/8 → 13/4 | freq:275 ]", - "[ 13/4 → 27/8 | freq:293.3333333 ]", - "[ 27/8 → 7/2 | freq:330 ]", - "[ 7/2 → 29/8 | freq:352 ]", + "[ 25/8 → 13/4 | freq:252.7136381 ]", + "[ 13/4 → 27/8 | freq:290.2917404 ]", + "[ 27/8 → 7/2 | freq:333.4576446 ]", + "[ 7/2 → 29/8 | freq:383.0422479 ]", "[ 29/8 → 15/4 | freq:440 ]", - "[ 15/4 → 31/8 | freq:550 ]", - "[ 31/8 → 4/1 | freq:586.6666667 ]", + "[ 15/4 → 31/8 | freq:505.4272762 ]", + "[ 31/8 → 4/1 | freq:580.5834807 ]", ] `; @@ -14637,29 +14637,29 @@ exports[`runs examples > example "xen" example index 3 1`] = ` "[ 3/4 → 7/8 | freq:505.4272762 ]", "[ 7/8 → 1/1 | freq:580.5834807 ]", "[ 1/1 → 9/8 | freq:220 ]", - "[ 9/8 → 5/4 | freq:235.7901618 ]", - "[ 5/4 → 11/8 | freq:252.7136381 ]", - "[ 11/8 → 3/2 | freq:270.8517709 ]", - "[ 3/2 → 13/8 | freq:290.2917404 ]", - "[ 13/8 → 7/4 | freq:311.1269837 ]", - "[ 7/4 → 15/8 | freq:333.4576446 ]", - "[ 15/8 → 2/1 | freq:357.3910544 ]", + "[ 9/8 → 5/4 | freq:252.7136381 ]", + "[ 5/4 → 11/8 | freq:290.2917404 ]", + "[ 11/8 → 3/2 | freq:333.4576446 ]", + "[ 3/2 → 13/8 | freq:383.0422479 ]", + "[ 13/8 → 7/4 | freq:440 ]", + "[ 7/4 → 15/8 | freq:505.4272762 ]", + "[ 15/8 → 2/1 | freq:580.5834807 ]", "[ 2/1 → 17/8 | freq:220 ]", - "[ 17/8 → 9/4 | freq:230.404707 ]", - "[ 9/4 → 19/8 | freq:241.3014955 ]", - "[ 19/8 → 5/2 | freq:252.7136381 ]", - "[ 5/2 → 21/8 | freq:264.6655079 ]", - "[ 21/8 → 11/4 | freq:277.182631 ]", - "[ 11/4 → 23/8 | freq:290.2917404 ]", - "[ 23/8 → 3/1 | freq:304.0208336 ]", + "[ 17/8 → 9/4 | freq:252.7136381 ]", + "[ 9/4 → 19/8 | freq:290.2917404 ]", + "[ 19/8 → 5/2 | freq:333.4576446 ]", + "[ 5/2 → 21/8 | freq:383.0422479 ]", + "[ 21/8 → 11/4 | freq:440 ]", + "[ 11/4 → 23/8 | freq:505.4272762 ]", + "[ 23/8 → 3/1 | freq:580.5834807 ]", "[ 3/1 → 25/8 | freq:220 ]", - "[ 25/8 → 13/4 | freq:275 ]", - "[ 13/4 → 27/8 | freq:293.3333333 ]", - "[ 27/8 → 7/2 | freq:330 ]", - "[ 7/2 → 29/8 | freq:352 ]", + "[ 25/8 → 13/4 | freq:252.7136381 ]", + "[ 13/4 → 27/8 | freq:290.2917404 ]", + "[ 27/8 → 7/2 | freq:333.4576446 ]", + "[ 7/2 → 29/8 | freq:383.0422479 ]", "[ 29/8 → 15/4 | freq:440 ]", - "[ 15/4 → 31/8 | freq:550 ]", - "[ 31/8 → 4/1 | freq:586.6666667 ]", + "[ 15/4 → 31/8 | freq:505.4272762 ]", + "[ 31/8 → 4/1 | freq:580.5834807 ]", ] `;