Skip to content

Commit

Permalink
use register for degradeBy / undegradeBy
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Dec 10, 2022
1 parent 4340f02 commit 09b15a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/core/pattern.mjs
Expand Up @@ -1277,9 +1277,9 @@ export function register(name, func) {
args = args.map(reify);
// For methods that take a single argument (plus 'this'), allow
// multiple arguments but sequence them
if (arity == 2 && args.length != 1) {
if (arity === 2 && args.length !== 1) {
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}.`);
}
return pfunc(...args, this);
Expand Down
24 changes: 9 additions & 15 deletions packages/core/signal.mjs
Expand Up @@ -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 { 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 { id } from './util.mjs';

Expand Down Expand Up @@ -276,9 +276,9 @@ Pattern.prototype._degradeByWith = function (withPat, x) {
* @example
* s("[hh?0.2]*8")
*/
Pattern.prototype._degradeBy = function (x) {
return this._degradeByWith(rand, x);
};
export const degradeBy = register('degradeBy', function (x, pat) {
return pat._degradeByWith(rand, x);
});

/**
*
Expand All @@ -292,9 +292,7 @@ Pattern.prototype._degradeBy = function (x) {
* @example
* s("[hh?]*8")
*/
Pattern.prototype.degrade = function () {
return this._degradeBy(0.5);
};
export const degrade = register('degrade', (pat) => pat._degradeBy(0.5));

/**
* Inverse of {@link Pattern#degradeBy}: Randomly removes events from the pattern by a given amount.
Expand All @@ -309,16 +307,14 @@ Pattern.prototype.degrade = function () {
* @example
* s("hh*8").undegradeBy(0.2)
*/
Pattern.prototype._undegradeBy = function (x) {
return this._degradeByWith(
export const undegradeBy = register('undegradeBy', function (x, pat) {
return pat._degradeByWith(
rand.fmap((r) => 1 - r),
x,
);
};
});

Pattern.prototype.undegrade = function () {
return this._undegradeBy(0.5);
};
export const undegrade = register('degrade', (pat) => pat._undegradeBy(0.5));

Pattern.prototype._sometimesBy = function (x, func) {
return stack(this._degradeBy(x), func(this._undegradeBy(1 - x)));
Expand Down Expand Up @@ -505,5 +501,3 @@ Pattern.prototype.never = function (func) {
Pattern.prototype.always = function (func) {
return func(this);
};

Pattern.prototype.patternified.push('degradeBy', 'undegradeBy');
5 changes: 0 additions & 5 deletions packages/mini/mini.mjs
Expand Up @@ -178,11 +178,6 @@ export const h = (string) => {
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) {
if (typeof thing === 'string') {
return mini(thing);
Expand Down

0 comments on commit 09b15a0

Please sign in to comment.