Top level functions

This commit is contained in:
Aria 2025-11-16 20:28:22 -06:00
parent 7a3bea6f90
commit f5ed6dbe79
4 changed files with 56 additions and 28 deletions

View file

@ -3644,35 +3644,51 @@ export const parray = (pats) => {
* Can also be used to create a new synth via `s('user').partials(...)`
*
* @name partials
* @param {number[] | Pattern} partials List of [0, 1] magnitudes for partials. 0th entry is the first harmonic (i.e. DC offset is skipped)
* @param {number[] | Pattern} magnitudes List of [0, 1] magnitudes for partials. 0th entry is the fundamental harmonic (i.e. DC offset is skipped)
* @example
* s("user").seg(16).n(irand(8)).scale("A:major")
* .partials([1, 0, 1, 0, 0, 1])
* @example
* s("saw").seg(8).n(irand(12)).scale("G#:minor")
* .partials(binaryL(256))
* .partials(randL(16))
*/
export const { partials } = register('partials', (list, pat) => {
debugger;
register('partials', (list, pat) => {
if (Array.isArray(list)) {
list = parray(list);
}
return pat.withValue((v) => (l) => ({...v, partials: l})).appLeft(list);
return pat.withValue((v) => (l) => ({ ...v, partials: l })).appLeft(list);
});
// Also create a top-level function
export const partials = (list) => {
if (Array.isArray(list)) {
list = parray(list);
}
return list.as('partials');
};
/**
* Rotates the harmonics of one of the core synths ('sine', 'tri', 'saw', 'user', ..) by a list of phases
*
* @name phases
* @param {number[] | Pattern} phases List of [0, 1) phases for partials. 0th entry is the first phase (i.e. DC offset is skipped)
* @param {number[] | Pattern} phases List of [0, 1) phases for partials. 0th entry is the fundamental phase (i.e. DC offset is skipped)
* @example
* s("saw").seg(8).n(irand(12)).scale("G#:minor")
* .partials(binaryL(256))
* .phases(randL(20))
* .partials(randL(16))
* .phases(randL(16))
*/
export const { phases } = register('phases', (list, pat) => {
register('phases', (list, pat) => {
if (Array.isArray(list)) {
list = parray(list);
}
return pat.withValue((v) => (l) => ({...v, phases: l})).appLeft(list);
pat = pat ?? pure({});
return pat.withValue((v) => (l) => ({ ...v, phases: l })).appLeft(list);
});
// Also create a top-level function
export const phases = (list) => {
if (Array.isArray(list)) {
list = parray(list);
}
return list.as('phases');
};

View file

@ -796,7 +796,7 @@ describe('Pattern', () => {
});
});
describe('apply', () => {
(it('Can apply a function', () => {
it('Can apply a function', () => {
expect(sequence('a', 'b').apply(fast(2)).firstCycle()).toStrictEqual(sequence('a', 'b').fast(2).firstCycle());
}),
it('Can apply a pattern of functions', () => {
@ -804,7 +804,7 @@ describe('Pattern', () => {
expect(sequence('a', 'b').apply(fast(2), fast(3)).firstCycle()).toStrictEqual(
sequence('a', 'b').fast(2, 3).firstCycle(),
);
}));
});
});
describe('layer', () => {
it('Can layer up multiple functions', () => {