Patternify range (#231)

* patternify range

* patternify range

* fix test
This commit is contained in:
Alex McLean 2022-10-26 22:53:49 +01:00 committed by GitHub
parent 2ed290a214
commit 5f381cf153
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View file

@ -526,7 +526,7 @@ export class Pattern {
* @example * @example
* s("bd sd,hh*4").cutoff(sine.range(500,2000).slow(4)).out() * s("bd sd,hh*4").cutoff(sine.range(500,2000).slow(4)).out()
*/ */
range(min, max) { _range(min, max) {
return this.mul(max - min).add(min); return this.mul(max - min).add(min);
} }
@ -538,8 +538,8 @@ export class Pattern {
* @param {Number} max * @param {Number} max
* @returns Pattern * @returns Pattern
*/ */
rangex(min, max) { _rangex(min, max) {
return this.range(Math.log(min), Math.log(max)).fmap(Math.exp); return this._range(Math.log(min), Math.log(max)).fmap(Math.exp);
} }
/** /**
@ -549,8 +549,8 @@ export class Pattern {
* @param {Number} max * @param {Number} max
* @returns Pattern * @returns Pattern
*/ */
range2(min, max) { _range2(min, max) {
return this._fromBipolar().range(min, max); return this._fromBipolar()._range(min, max);
} }
_bindWhole(choose_whole, func) { _bindWhole(choose_whole, func) {
@ -1754,6 +1754,7 @@ export const mul = curry((a, pat) => pat.mul(a));
export const off = curry((t, f, pat) => pat.off(t, f)); export const off = curry((t, f, pat) => pat.off(t, f));
export const ply = curry((a, pat) => pat.ply(a)); export const ply = curry((a, pat) => pat.ply(a));
export const range = curry((a, b, pat) => pat.range(a, b)); export const range = curry((a, b, pat) => pat.range(a, b));
export const rangex = curry((a, b, pat) => pat.rangex(a, b));
export const range2 = curry((a, b, pat) => pat.range2(a, b)); export const range2 = curry((a, b, pat) => pat.range2(a, b));
export const rev = (pat) => pat.rev(); export const rev = (pat) => pat.rev();
export const slow = curry((a, pat) => pat.slow(a)); export const slow = curry((a, pat) => pat.slow(a));
@ -1840,6 +1841,18 @@ Pattern.prototype.inside = function (...args) {
args = args.map(reify); args = args.map(reify);
return patternify2(Pattern.prototype._inside)(...args, this); return patternify2(Pattern.prototype._inside)(...args, this);
}; };
Pattern.prototype.range = function (...args) {
args = args.map(reify);
return patternify2(Pattern.prototype._range)(...args, this);
};
Pattern.prototype.rangex = function (...args) {
args = args.map(reify);
return patternify2(Pattern.prototype._rangex)(...args, this);
};
Pattern.prototype.range2 = function (...args) {
args = args.map(reify);
return patternify2(Pattern.prototype._range2)(...args, this);
};
// call this after all Patter.prototype.define calls have been executed! (right before evaluate) // call this after all Patter.prototype.define calls have been executed! (right before evaluate)
Pattern.prototype.bootstrap = function () { Pattern.prototype.bootstrap = function () {

View file

@ -853,6 +853,13 @@ describe('Pattern', () => {
); );
}); });
}); });
describe('range', () => {
it('Can be patterned', () => {
expect(sequence(0, 0).range(sequence(0, 0.5), 1).firstCycle()).toStrictEqual(
sequence(0, 0.5).firstCycle(),
);
});
});
describe('range2', () => { describe('range2', () => {
it('Can change the range of a bipolar pattern', () => { it('Can change the range of a bipolar pattern', () => {
expect(sequence(-1, -0.5, 0, 0.5).range2(1000, 1100).firstCycle()).toStrictEqual( expect(sequence(-1, -0.5, 0, 0.5).range2(1000, 1100).firstCycle()).toStrictEqual(