diff --git a/packages/core/signal.mjs b/packages/core/signal.mjs index a6868937..d8460e17 100644 --- a/packages/core/signal.mjs +++ b/packages/core/signal.mjs @@ -1,5 +1,5 @@ import { Hap } from './hap.mjs'; -import { Pattern, fastcat, reify, silence } from './pattern.mjs'; +import { Pattern, fastcat, reify, silence, stack } from './pattern.mjs'; import Fraction from './fraction.mjs'; import { id } from './util.mjs'; @@ -101,4 +101,38 @@ Pattern.prototype._degrade = function () { return this._degradeBy(0.5); }; -Pattern.prototype.patternified.push('degradeBy', 'degrade'); +Pattern.prototype._undegradeBy = function (x) { + return this._degradeByWith(rand.fmap((r) => 1 - r), x); +}; + +Pattern.prototype._undegrade = function () { + return this._undegradeBy(0.5); +}; + +Pattern.prototype._sometimesBy = function (x, func) { + return stack(this._degradeBy(x), func(this._undegradeBy(1-x))); +}; + +Pattern.prototype.sometimesBy = function (patx, func) { + const pat = this; + return reify(patx).fmap((x) => pat._sometimesBy(x, func)).innerJoin(); +}; + +Pattern.prototype._sometimesByPre = function (x, func) { + return stack(this._degradeBy(x), func(this).undegradeBy(1-x)); +}; + +Pattern.prototype.sometimesByPre = function (patx, func) { + const pat = this; + return reify(patx).fmap((x) => pat._sometimesByPre(x, func)).innerJoin(); +}; + +Pattern.prototype.sometimes = function (func) { + return this._sometimesBy(0.5, func); +}; + +Pattern.prototype.sometimesPre = function (func) { + return this._sometimesByPre(0.5, func); +}; + +Pattern.prototype.patternified.push('degradeBy', 'undegradeBy');