Skip to content

Commit

Permalink
Parse square roots in val equaves
Browse files Browse the repository at this point in the history
ref #314
  • Loading branch information
frostburn committed May 11, 2024
1 parent 07d998e commit ebbf3b9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/parser/__tests__/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,4 +1620,14 @@ describe('SonicWeave parser', () => {
'1\\1',
]);
});

it('supports radicals in SOV', () => {
const scale = expand(`
9/8
3/2
3/^2
[√3]8@2.√3
`);
expect(scale).toEqual(['1\\8<3>', '3\\8<3>', '1\\2<3>']);
});
});
2 changes: 1 addition & 1 deletion src/parser/chord-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function parseVals(
if (node.type === 'WartsLiteral') {
val = wartsToVal(node);
} else if (node.type === 'SparseOffsetVal') {
val = sparseOffsetToVal(node);
[val] = sparseOffsetToVal(node);
}
result.push(basis.map(e => val.dot(e).valueOf()));
}
Expand Down
8 changes: 1 addition & 7 deletions src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,7 @@ export class ExpressionVisitor {
}

protected visitSparseOffsetVal(node: SparseOffsetVal) {
const val = sparseOffsetToVal(node);
let equave = TWO_MONZO;
if (node.equave) {
equave = TimeMonzo.fromFraction(
new Fraction(node.equave.numerator, node.equave.denominator ?? 1)
);
}
const [val, equave] = sparseOffsetToVal(node);
return new Val(val, equave, node);
}

Expand Down
43 changes: 24 additions & 19 deletions src/stdlib/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,27 +442,32 @@ export function temper(
interval: Interval | Interval[]
): typeof interval {
const divisions = val.divisions;
const equave = val.equave.toFraction();
let equaveNumerator: number | null = null;
let equaveDenominator: number | null = null;
if (equave.compare(TWO)) {
equaveNumerator = equave.n;
if (equave.d !== 1) {
equaveDenominator = equave.d;
let step: Interval;
try {
const equave = val.equave.toFraction();
let equaveNumerator: number | null = null;
let equaveDenominator: number | null = null;
if (equave.compare(TWO)) {
equaveNumerator = equave.n;
if (equave.d !== 1) {
equaveDenominator = equave.d;
}
}
step = new Interval(
TimeMonzo.fromFraction(equave).pow(divisions.inverse()),
'logarithmic',
0,
{
type: 'NedjiLiteral',
numerator: divisions.d,
denominator: divisions.n,
equaveNumerator,
equaveDenominator,
}
);
} catch {
step = new Interval(val.equave.pow(divisions.inverse()), 'logarithmic');
}
const step = new Interval(
TimeMonzo.fromFraction(equave).pow(divisions.inverse()),
'logarithmic',
0,
{
type: 'NedjiLiteral',
numerator: divisions.d,
denominator: divisions.n,
equaveNumerator,
equaveDenominator,
}
);
const rel = relative.bind(this);
if (Array.isArray(interval)) {
return interval.map(i => {
Expand Down
17 changes: 14 additions & 3 deletions src/warts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from './expression';
import {NEGATIVE_ONE, ONE, ZERO} from './utils';

const TWO_MONZO = new TimeMonzo(ZERO, [ONE]);
const SECOND_MONZO = new TimeMonzo(ONE, []);
const HERTZ_MONZO = new TimeMonzo(NEGATIVE_ONE, []);
const REAL_CENT_MONZO = new TimeReal(0, 1.0005777895065548);
Expand Down Expand Up @@ -271,12 +272,22 @@ export function wartsToVal(node: WartsLiteral) {
return valToTimeMonzo(val, subgroup);
}

export function sparseOffsetToVal(node: SparseOffsetVal) {
export function sparseOffsetToVal(
node: SparseOffsetVal
): [TimeMonzo, TimeMonzo] {
const {subgroup} = parseValSubgroup(node.basis);
let equave = TWO_MONZO;
if (node.equave) {
const equave = TimeMonzo.fromFraction(
equave = TimeMonzo.fromFraction(
new Fraction(node.equave.numerator, node.equave.denominator ?? 1)
);
if (node.equave.radical) {
const splitEquave = equave.sqrt();
if (splitEquave instanceof TimeReal) {
throw new Error('Unable to split val equave.');
}
equave = splitEquave;
}
shiftEquave(equave, subgroup);
}
const val = patentVal(node.divisions, subgroup);
Expand All @@ -295,5 +306,5 @@ export function sparseOffsetToVal(node: SparseOffsetVal) {
throw new Error('Tweak outside subgroup.');
}
}
return valToTimeMonzo(val, subgroup);
return [valToTimeMonzo(val, subgroup), equave];
}

0 comments on commit ebbf3b9

Please sign in to comment.