use register for degradeBy / undegradeBy

This commit is contained in:
Felix Roos 2022-12-10 19:25:51 +01:00
parent 4340f024d3
commit 09b15a07c6
3 changed files with 11 additions and 22 deletions

View File

@ -1277,9 +1277,9 @@ export function register(name, func) {
args = args.map(reify); args = args.map(reify);
// For methods that take a single argument (plus 'this'), allow // For methods that take a single argument (plus 'this'), allow
// multiple arguments but sequence them // multiple arguments but sequence them
if (arity == 2 && args.length != 1) { if (arity === 2 && args.length !== 1) {
args = [sequence(...args)]; args = [sequence(...args)];
} else if (arity != args.length + 1) { } else if (arity !== args.length + 1) {
throw new Error(`.${name}() expects ${arity - 1} inputs but got ${args.length}.`); throw new Error(`.${name}() expects ${arity - 1} inputs but got ${args.length}.`);
} }
return pfunc(...args, this); return pfunc(...args, this);

View File

@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
*/ */
import { Hap } from './hap.mjs'; import { Hap } from './hap.mjs';
import { Pattern, fastcat, reify, silence, stack, isPattern } from './pattern.mjs'; import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs';
import Fraction from './fraction.mjs'; import Fraction from './fraction.mjs';
import { id } from './util.mjs'; import { id } from './util.mjs';
@ -276,9 +276,9 @@ Pattern.prototype._degradeByWith = function (withPat, x) {
* @example * @example
* s("[hh?0.2]*8") * s("[hh?0.2]*8")
*/ */
Pattern.prototype._degradeBy = function (x) { export const degradeBy = register('degradeBy', function (x, pat) {
return this._degradeByWith(rand, x); return pat._degradeByWith(rand, x);
}; });
/** /**
* *
@ -292,9 +292,7 @@ Pattern.prototype._degradeBy = function (x) {
* @example * @example
* s("[hh?]*8") * s("[hh?]*8")
*/ */
Pattern.prototype.degrade = function () { export const degrade = register('degrade', (pat) => pat._degradeBy(0.5));
return this._degradeBy(0.5);
};
/** /**
* Inverse of {@link Pattern#degradeBy}: Randomly removes events from the pattern by a given amount. * Inverse of {@link Pattern#degradeBy}: Randomly removes events from the pattern by a given amount.
@ -309,16 +307,14 @@ Pattern.prototype.degrade = function () {
* @example * @example
* s("hh*8").undegradeBy(0.2) * s("hh*8").undegradeBy(0.2)
*/ */
Pattern.prototype._undegradeBy = function (x) { export const undegradeBy = register('undegradeBy', function (x, pat) {
return this._degradeByWith( return pat._degradeByWith(
rand.fmap((r) => 1 - r), rand.fmap((r) => 1 - r),
x, x,
); );
}; });
Pattern.prototype.undegrade = function () { export const undegrade = register('degrade', (pat) => pat._undegradeBy(0.5));
return this._undegradeBy(0.5);
};
Pattern.prototype._sometimesBy = function (x, func) { Pattern.prototype._sometimesBy = function (x, func) {
return stack(this._degradeBy(x), func(this._undegradeBy(1 - x))); return stack(this._degradeBy(x), func(this._undegradeBy(1 - x)));
@ -505,5 +501,3 @@ Pattern.prototype.never = function (func) {
Pattern.prototype.always = function (func) { Pattern.prototype.always = function (func) {
return func(this); return func(this);
}; };
Pattern.prototype.patternified.push('degradeBy', 'undegradeBy');

View File

@ -178,11 +178,6 @@ export const h = (string) => {
return patternifyAST(ast); return patternifyAST(ast);
}; };
// shorthand for mini
Pattern.prototype.define('mini', mini, { composable: true });
Pattern.prototype.define('m', mini, { composable: true });
Pattern.prototype.define('h', h, { composable: true });
export function minify(thing) { export function minify(thing) {
if (typeof thing === 'string') { if (typeof thing === 'string') {
return mini(thing); return mini(thing);