Skip to content

Commit

Permalink
Store full MOS config for reconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed May 14, 2024
1 parent b5cceb8 commit 3c7aa91
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {SonicWeaveValue} from './stdlib';
import {Interval} from './interval';
import {TimeMonzo} from './monzo';
import {ZERO} from './utils';
import {MosConfig, scaleMonzos} from './diamond-mos';
import {stepString} from './words';
import {MosConfig} from './diamond-mos';

/**
* Root context of a SonicWeave runtime containing the scale title, root pitch, value of the 'up' inflection etc.
Expand Down Expand Up @@ -168,18 +167,9 @@ export class RootContext {
lines.push(`/ = ${this.lift.toString()}`);
}
if (this.mosConfig) {
const monzos = scaleMonzos(this.mosConfig);
const ss = stepString(monzos);
let large: TimeMonzo;
let small: TimeMonzo;
if (ss.startsWith('L')) {
large = monzos[0];
small = large.div(this.mosConfig.am) as TimeMonzo;
} else {
small = monzos[0];
large = small.mul(this.mosConfig.am) as TimeMonzo;
}
lines.push(`MOS {${ss};L=${large};s=${small}}`);
lines.push(
`MOS {${this.mosConfig.pattern};L=${this.mosConfig.large};s=${this.mosConfig.small}}`
);
}
return lines.join('\n');
}
Expand Down
12 changes: 12 additions & 0 deletions src/diamond-mos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ export type MosConfig = {
* Intervals for relative notation. Use period to reach larger intervals.
*/
degrees: MosDegree[];
/**
* Pattern for reconstructing the MOS declaration.
*/
pattern: string;
/**
* Value of the large step.
*/
large: TimeMonzo;
/**
* Value of the small step.
*/
small: TimeMonzo;
};

/**
Expand Down
68 changes: 68 additions & 0 deletions src/parser/__tests__/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1652,4 +1652,72 @@ describe('SonicWeave parser', () => {
145.94801056814464, 165.55065597696213, 176.31825099920434, 200,
]);
});

it('allows you to call the smaller step L in MOS declaration', () => {
const scale = expand(`
MOS {
3L 2s
L = 9/8
}
automos()
relative
linear
`);
expect(scale).toEqual([
'MOS {LLsLs;L=9/8;s=32/27}',
'9/8',
'81/64',
'3/2',
'27/16',
'2',
]);
});

it('allows you to create negative steps', () => {
const scale = expand(`
MOS {
6L 1s
L = 9/8
}
automos()
relative
linear
`);
expect(scale).toEqual([
'MOS {LLLLLLs;L=9/8;s=524288/531441}',
'9/8',
'81/64',
'729/512',
'6561/4096',
'59049/32768',
'531441/262144',
'2',
]);
});

it('preserves equalized MOS declaration', () => {
const scale = expand(`
MOS {
5L 2s
L = 1\\7
}
M1ms
M2ms
P3ms
P4ms
M5ms
M6ms
P7ms
`);
expect(scale).toEqual([
'MOS {LLLsLLs;L=2^1/7;s=2^1/7}',
'M1ms',
'M2ms',
'P3ms',
'P4ms',
'M5ms',
'M6ms',
'P7ms',
]);
});
});
3 changes: 3 additions & 0 deletions src/parser/mos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export class Tardigrade {
period: r(notation.period),
scale,
degrees,
pattern: this.pattern,
large,
small,
};
}

Expand Down

0 comments on commit 3c7aa91

Please sign in to comment.