Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chunk, chunkBack and iterBack #25

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 34 additions & 2 deletions packages/core/strudel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,24 @@ class Pattern {
return this._echoWith(times, time, (pat, i) => pat.velocity(Math.pow(feedback, i)));
}

iter(times) {
return slowcat(...range(0, times - 1).map((i) => this.early(i / times)));
iter(times, back = false) {
return slowcat(...range(0, times - 1).map((i) => (back ? this.late(i / times) : this.early(i / times))));
}

// known as iter' in tidalcycles
iterBack(times) {
return this.iter(times,true)
}

_chunk(n, func, back = false) {
const binary = Array(n - 1).fill(false);
binary.unshift(true);
const binary_pat = sequence(...binary).iter(n, back);
return this.when(binary_pat, func);
}

_chunkBack(n, func) {
return this._chunk(n, func, true)
}

edit(...funcs) {
Expand Down Expand Up @@ -1026,6 +1042,10 @@ const mask = curry((a, pat) => pat.mask(a));
const echo = curry((a, b, c, pat) => pat.echo(a, b, c));
const invert = (pat) => pat.invert();
const inv = (pat) => pat.inv();
const iter = curry((a, pat) => pat.iter(a));
const iterBack = curry((a, pat) => pat.iter(a));
const chunk = curry((a, pat) => pat.chunk(a));
const chunkBack = curry((a, pat) => pat.chunkBack(a));

// problem: curried functions with spread arguments must have pat at the beginning
// with this, we cannot keep the pattern open at the end.. solution for now: use array to keep using pat as last arg
Expand Down Expand Up @@ -1076,6 +1096,14 @@ Pattern.prototype.echoWith = function (...args) {
args = args.map(reify);
return patternify3(Pattern.prototype._echoWith)(...args, this);
};
Pattern.prototype.chunk = function (...args) {
args = args.map(reify);
return patternify2(Pattern.prototype._chunk)(...args, this);
};
Pattern.prototype.chunkBack = function (...args) {
args = args.map(reify);
return patternify2(Pattern.prototype._chunkBack)(...args, this);
};

// call this after all Patter.prototype.define calls have been executed! (right before evaluate)
Pattern.prototype.bootstrap = function () {
Expand Down Expand Up @@ -1170,4 +1198,8 @@ export {
id,
range,
echo,
iter,
iterBack,
chunk,
chunkBack,
};