Make default alignment configurable

This commit is contained in:
Aria 2026-01-22 13:52:57 -06:00
parent 7f2860eb14
commit 6e61f3cdb6

View file

@ -1055,10 +1055,8 @@ function _composeOp(a, b, func) {
return func(a, b);
}
// Make composers
(function () {
// pattern composers
const composers = {
const COMPOSERS = {
set: [(a, b) => b],
keep: [(a) => a],
keepif: [(a, b) => (b ? a : undefined)],
@ -1136,10 +1134,9 @@ function _composeOp(a, b, func) {
func: [(a, b) => b(a)],
};
const hows = ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Reset', 'Restart', 'Poly'];
const _setupAlignments = () => {
// generate methods to do what and how
for (const [what, [op, preprocess]] of Object.entries(composers)) {
for (const [what, [op, preprocess]] of Object.entries(COMPOSERS)) {
// make plain version, e.g. pat._add(value) adds that plain value
// to all the values in pat
Pattern.prototype['_' + what] = function (value) {
@ -1148,16 +1145,18 @@ function _composeOp(a, b, func) {
// make patternified monster version
Object.defineProperty(Pattern.prototype, what, {
// Set to configurable so we can update if the default alignment changes
configurable: true,
// a getter that returns a function, so 'pat' can be
// accessed by closures that are methods of that function..
get: function () {
const pat = this;
// wrap the 'in' function as default behaviour
const wrapper = (...other) => pat[what]['in'](...other);
const wrapper = (...other) => pat[what][DEFAULT_ALIGNMENT](...other);
// add methods to that function for each behaviour
for (const how of hows) {
for (const how of ALIGNMENTS) {
wrapper[how.toLowerCase()] = function (...other) {
var howpat = pat;
other = sequence(other);
@ -1182,15 +1181,23 @@ function _composeOp(a, b, func) {
return wrapper;
},
});
}
};
let DEFAULT_ALIGNMENT = 'in';
const ALIGNMENTS = ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Reset', 'Restart', 'Poly'];
const ALIGNMENT_KEYS = ALIGNMENTS.map((how) => how.toLowerCase());
// Make composers
(function () {
_setupAlignments();
// Default op to 'set', e.g. pat.squeeze(pat2) = pat.set.squeeze(pat2)
for (const how of hows) {
for (const how of ALIGNMENTS) {
Pattern.prototype[how.toLowerCase()] = function (...args) {
return this.set[how.toLowerCase()](args);
};
}
}
// binary composers
/**
* Applies the given structure to the pattern:
@ -1249,6 +1256,14 @@ function _composeOp(a, b, func) {
};
})();
export const setDefaultAlignment = (alignment) => {
alignment = alignment?.toLowerCase();
if (DEFAULT_ALIGNMENT !== alignment && ALIGNMENT_KEYS.includes(alignment)) {
DEFAULT_ALIGNMENT = alignment;
_setupAlignments();
}
};
// aliases
export const polyrhythm = stack;
export const pr = stack;