Compare commits

...

2 Commits

Author SHA1 Message Date
Jade (Rose) Rowland 376db2f7c2 li 2025-06-17 15:00:27 +02:00
Jade (Rose) Rowland 294581f824 working 2025-06-17 14:58:27 +02:00
+43
View File
@@ -21,6 +21,7 @@ import {
numeralArgs,
parseNumeral,
pairs,
noteToMidi,
} from './util.mjs';
import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs';
@@ -3340,3 +3341,45 @@ export const { beat } = register(
['beat'],
__beat((x) => x.innerJoin()),
);
const __quantizeBy = (lens, scale, pat) => {
// Supports ':' list syntax in mininotation
scale = (Array.isArray(scale) ? scale.flat() : [scale]).flatMap((val) =>
typeof val === 'number' ? val : noteToMidi(val) - 48,
);
return pat.withHap((hap) => {
const isObject = typeof hap.value === 'object';
let note = isObject ? hap.value.n : hap.value;
if (typeof note === 'number') {
note = note;
}
if (typeof note === 'string') {
note = noteToMidi(note);
}
if (isObject) {
delete hap.value.n; // remove n so it won't cause trouble
}
const octave = (note / lens) >> 0;
const transpose = octave * lens;
const goal = note - transpose;
note =
scale.reduce((prev, curr) => {
return Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev;
}) + transpose;
return hap.withValue(() => (isObject ? { ...hap.value, note } : note));
});
};
/**
* Snap note values to a chosen array of notes within a 12 note/octave scale
* @name quantize
* @example
* note(irand(35).add(48).seg(16).quantize("d:a:a#:f")).s("pulse")
*/
export const { quantize } = register(['quantize'], (scale, pat) => {
return __quantizeBy(12, scale, pat);
});