Skip to content

Commit

Permalink
Accept lone vals for commaBasis and mappingBasis
Browse files Browse the repository at this point in the history
ref #422
  • Loading branch information
frostburn committed Dec 9, 2024
1 parent 2ac30c8 commit dea3c83
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions documentation/BUILTIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Obtain a non-negative integer that is the Unicode code point value of the charac
Return the color of the interval.

### commaBasis(*temperament*)
Obtain the comma basis (null space) of a temperament.
Obtain the comma basis (null space) of a temperament or a val.

### commaList(*commas*, *basisOrLimit*, *weights*, *pureEquaves*, *fullPrimeLimit*)
Construct a Temperament instance from an array of commas. Optional weights are applied multiplicatively on top of Tenney weights. Optionally equaves are normalized to pure. Optionally the full prime limit is assumed based on the commas.
Expand Down Expand Up @@ -280,7 +280,7 @@ Obtain a "best effort" short string representing a primitive value. Vectorizes o
Map a riff over the given/current scale producing a new scale.

### mappingBasis(*temperament*)
Obtain the mapping generators (preimage) of a temperament with period first. See `generatorsOf` for the tempered generators.
Obtain the mapping generators (preimage) of a temperament or a val with period first. See `generatorsOf` for the tempered generators.

### maximum(*...args*)
Obtain the argument with the maximum value.
Expand Down
14 changes: 14 additions & 0 deletions src/parser/__tests__/temper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ describe('Features related to tempering', () => {
expect(scale).toEqual(['1901.783 "3"', '583.905 "7/5"', '1200. "2"']);
});

it('obtains the period of a single val', () => {
const scale = expand(`{
const [period] = mappingBasis([email protected])
period str(period)
11@
}`);
expect(scale).toEqual(['1\\11 "16/15"']);
});

it('breaks a val into its comma basis', () => {
const scale = expand('[...commaBasis([email protected])]');
expect(scale).toEqual(['144/125', '135/128']);
});

it('knows miracle is miracle', () => {
const yup = evaluate(
'Temperament([[email protected], [email protected]]) == commaList([S15, S7-S8])'
Expand Down
11 changes: 9 additions & 2 deletions src/stdlib/builtin/temper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,29 @@ periodsOf.__doc__ = 'Obtain the number of periods per equave in a temperament.';
periodsOf.__node__ = builtinNode(periodsOf);

function mappingBasis(this: ExpressionVisitor, temperament: SonicWeaveValue) {
if (temperament instanceof Val) {
temperament = builtinTemperament.bind(this)(temperament);
}
if (!(temperament instanceof Temperament)) {
throw new Error('A temperament is required.');
}
return temperament.preimage;
}
mappingBasis.__doc__ =
'Obtain the mapping generators (preimage) of a temperament with period first. See `generatorsOf` for the tempered generators.';
'Obtain the mapping generators (preimage) of a temperament or a val with period first. See `generatorsOf` for the tempered generators.';
mappingBasis.__node__ = builtinNode(mappingBasis);

function commaBasis(this: ExpressionVisitor, temperament: SonicWeaveValue) {
if (temperament instanceof Val) {
temperament = builtinTemperament.bind(this)(temperament);
}
if (!(temperament instanceof Temperament)) {
throw new Error('A temperament is required.');
}
return temperament.commaBasis;
}
commaBasis.__doc__ = 'Obtain the comma basis (null space) of a temperament.';
commaBasis.__doc__ =
'Obtain the comma basis (null space) of a temperament or a val.';
commaBasis.__node__ = builtinNode(commaBasis);

function PrimeMapping(
Expand Down

0 comments on commit dea3c83

Please sign in to comment.