Skip to content

Commit

Permalink
Various MOS fixes
Browse files Browse the repository at this point in the history
ref #326
  • Loading branch information
frostburn committed May 22, 2024
1 parent 323fc4d commit 02cfa8a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 35 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"vitest": "^1.4.0"
},
"dependencies": {
"moment-of-symmetry": "^0.7.0",
"xen-dev-utils": "^0.7.0"
"moment-of-symmetry": "^0.8.0",
"xen-dev-utils": "^0.8.0"
},
"engines": {
"node": ">=12.0.0"
Expand Down
8 changes: 7 additions & 1 deletion src/diamond-mos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ export type AbsoluteMosPitch = {
*/
export function scaleMonzos(config: MosConfig) {
const entries = Array.from(config.scale);
entries.sort((a, b) => a[0].localeCompare(b[0]));
entries.sort((a, b) =>
a[0].length < b[0].length
? -1
: a[0].length > b[0].length
? 1
: a[0].localeCompare(b[0])
);
const monzos = entries.map(entry => entry[1]);
monzos.push(monzos.shift()!.mul(config.equave) as TimeMonzo);
return monzos;
Expand Down
13 changes: 5 additions & 8 deletions src/monzo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
monzoToCents,
sum,
primeFactorize,
modc,
} from 'xen-dev-utils';

import {
Expand Down Expand Up @@ -636,10 +637,7 @@ export class TimeReal {
}
const modulus = other.valueOf();
if (ceiling) {
return new TimeReal(
this.timeExponent,
mmod(this.value, modulus) || modulus
);
return new TimeReal(this.timeExponent, modc(this.value, modulus));
}
return new TimeReal(this.timeExponent, mmod(this.value, modulus));
}
Expand Down Expand Up @@ -2221,10 +2219,9 @@ export class TimeMonzo {
return result;
}
const modulus = other.valueOf();
let value = mmod(this.valueOf(), modulus);
if (ceiling) {
value = value || modulus;
}
const value = ceiling
? modc(this.valueOf(), modulus)
: mmod(this.valueOf(), modulus);
return new TimeReal(this.timeExponent.valueOf(), value);
}

Expand Down
29 changes: 29 additions & 0 deletions src/parser/__tests__/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1794,4 +1794,33 @@ describe('SonicWeave parser', () => {
'J5 "91\\\\91"',
]);
});

it('has automos that works with two-character nominals', () => {
const scale = expand(`
MOS 9L 9s
automos()
i => nedji(i, niente, 27)
`);
expect(scale).toEqual([
'MOS {LsLsLsLsLsLsLsLsLs;L=2^2/27;s=2^1/27}',
'2\\27',
'3\\27',
'5\\27',
'6\\27',
'8\\27',
'9\\27',
'11\\27',
'12\\27',
'14\\27',
'15\\27',
'17\\27',
'18\\27',
'20\\27',
'21\\27',
'23\\27',
'24\\27',
'26\\27',
'27\\27',
]);
});
});
26 changes: 26 additions & 0 deletions src/parser/__tests__/stdlib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1512,4 +1512,30 @@ describe('SonicWeave standard library', () => {
'18\\18',
]);
});

it('produces MOS scales of negative hardness', () => {
const scale = expand('mos(5, 2, 2, -1)');
expect(scale).toEqual([
'2\\8',
'4\\8',
'6\\8',
'5\\8',
'7\\8',
'9\\8',
'8\\8',
]);
});

it('produces descending MOS scales', () => {
const scale = expand('mos(5, 2, -2, -1, 5)');
expect(scale).toEqual([
'-2\\12',
'-4\\12',
'-5\\12',
'-7\\12',
'-9\\12',
'-11\\12',
'-12\\12',
]);
});
});
21 changes: 7 additions & 14 deletions src/parser/mos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
} from '../ast';
import {TimeMonzo, TimeReal} from '../monzo';
import {ExpressionVisitor} from './expression';
import {MosMonzo, MosOptions, generateNotation, mos} from 'moment-of-symmetry';
import {
MosMonzo,
MosOptions,
generateNotation,
stepString,
} from 'moment-of-symmetry';
import {Interval} from '../interval';
import {MosConfig, MosDegree} from '../diamond-mos';
import {ONE, TWO, ZERO} from '../utils';
Expand Down Expand Up @@ -229,19 +234,7 @@ export class Tardigrade {
throw new Error('Period must be consistent with counts if given.');
}
}
// moment-of-symmetry generates the brightest mode by default
const basic = mos(node.countLarge, node.countSmall, options);
let pattern = '';
let previous = 0;
for (const step of basic) {
if (step - previous === 2) {
pattern += 'L';
} else {
pattern += 's';
}
previous = step;
}
this.pattern = pattern;
this.pattern = stepString(node.countLarge, node.countSmall, options);
if (node.equave) {
this.equave = this.visitRationalEquave(node.equave);
}
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ riff mos(numberOfLargeSteps, numberOfSmallSteps, sizeOfLargeStep = 2, sizeOfSmal
Alternatively \`down\` defines the darkness of the mode i.e. the number of minor intervals from the root. \\
The default \`equave\` is the octave \`2/1\`.";
mosSubset(numberOfLargeSteps, numberOfSmallSteps, sizeOfLargeStep, sizeOfSmallStep, up, down);
const divisions = $[-1];
const divisions = abs $[-1];
if (equave == 2)
step => step \\ divisions;
else
Expand Down

0 comments on commit 02cfa8a

Please sign in to comment.