Add ption for asNumber to return original event

on fail.
Make numerical composers use asNumber.
This commit is contained in:
alex 2022-05-01 23:17:21 +01:00
parent 42269cf72b
commit 469db7d5bd

View File

@ -287,7 +287,7 @@ export class Pattern {
return otherPat.fmap((b) => this.fmap((a) => func(a)(b)))._TrigzeroJoin(); return otherPat.fmap((b) => this.fmap((a) => func(a)(b)))._TrigzeroJoin();
} }
_asNumber(silent = false) { _asNumber(softfail = false) {
return this._withEvent((event) => { return this._withEvent((event) => {
const asNumber = Number(event.value); const asNumber = Number(event.value);
if (!isNaN(asNumber)) { if (!isNaN(asNumber)) {
@ -304,10 +304,11 @@ export class Pattern {
// set context type to midi to let the player know its meant as midi number and not as frequency // set context type to midi to let the player know its meant as midi number and not as frequency
return new Hap(event.whole, event.part, toMidi(event.value), { ...event.context, type: 'midi' }); return new Hap(event.whole, event.part, toMidi(event.value), { ...event.context, type: 'midi' });
} }
if (!silent) { if (!softfail) {
throw new Error('cannot parse as number: "' + event.value + '"'); throw new Error('cannot parse as number: "' + event.value + '"');
} }
return event.withValue(() => undefined); // silent error // Not a number, just return original hap
return event;
})._removeUndefineds(); })._removeUndefineds();
} }
@ -824,39 +825,49 @@ function _composeOp(a, b, func) {
// pattern composers // pattern composers
const composers = { const composers = {
set: (a, b) => b, set: [(a, b) => b],
keep: (a, b) => a, keep: [(a, b) => a],
keepif: (a, b) => (b ? a : undefined), keepif: [(a, b) => (b ? a : undefined)],
add: (a, b) => a + b,
sub: (a, b) => a - b, // numerical functions
mul: (a, b) => a * b, add: [(a, b) => a + b, true],
div: (a, b) => a / b, sub: [(a, b) => a - b, true],
mod: mod, mul: [(a, b) => a * b, true],
pow: Math.pow, div: [(a, b) => a / b, true],
lt: (a, b) => a < b, mod: [mod, true],
gt: (a, b) => a > b, pow: [Math.pow, true],
lte: (a, b) => a <= b, _and: [(a, b) => a & b, true],
gte: (a, b) => a >= b, _or: [(a, b) => a | b, true],
eq: (a, b) => a == b, _xor: [(a, b) => a ^ b, true],
eqt: (a, b) => a === b, _lshift: [(a, b) => a << b, true],
ne: (a, b) => a != b, _rshift: [(a, b) => a >> b, true],
net: (a, b) => a !== b,
and: (a, b) => a && b, lt: [(a, b) => a < b],
or: (a, b) => a || b, gt: [(a, b) => a > b],
lte: [(a, b) => a <= b],
gte: [(a, b) => a >= b],
eq: [(a, b) => a == b],
eqt: [(a, b) => a === b],
ne: [(a, b) => a != b],
net: [(a, b) => a !== b],
and: [(a, b) => a && b],
or: [(a, b) => a || b],
// bitwise ops // bitwise ops
_and: (a, b) => a & b, func: [(a, b) => b(a)],
_or: (a, b) => a | b,
_xor: (a, b) => a ^ b,
_lshift: (a, b) => a << b,
_rshift: (a, b) => a >> b,
func: (a, b) => b(a),
}; };
// generate methods to do what and how // generate methods to do what and how
for (const [what, op] of Object.entries(composers)) { for (const [what, [op,numerical]] of Object.entries(composers)) {
for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) { for (const how of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Trig', 'Trigzero']) {
Pattern.prototype[what + how] = function (...other) { Pattern.prototype[what + how] = function (...other) {
var result = this['_op' + how](sequence(other), (a) => (b) => _composeOp(a, b, op)); var pat = this;
other = sequence(other);
if (numerical) {
pat = pat._asNumber();
other = other._asNumber();
}
var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op));
// hack to remove undefs when doing 'keepif' // hack to remove undefs when doing 'keepif'
if (what === 'keepif') { if (what === 'keepif') {
result = result._removeUndefineds(); result = result._removeUndefineds();