mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-22 02:58:32 +00:00
Fix struct() and add mask()
This commit is contained in:
parent
7b34f3f524
commit
0039b60020
20
strudel.mjs
20
strudel.mjs
@ -514,14 +514,25 @@ class Pattern {
|
|||||||
struct(...binary_pats) {
|
struct(...binary_pats) {
|
||||||
// Re structure the pattern according to a binary pattern (false values are dropped)
|
// Re structure the pattern according to a binary pattern (false values are dropped)
|
||||||
const binary_pat = sequence(binary_pats)
|
const binary_pat = sequence(binary_pats)
|
||||||
|
return binary_pat.fmap(b => val => b ? val : undefined).appLeft(this)._removeUndefineds()
|
||||||
|
}
|
||||||
|
|
||||||
|
mask(...binary_pats) {
|
||||||
|
// Only let through parts of pattern corresponding to true values in the given binary pattern
|
||||||
|
const binary_pat = sequence(binary_pats)
|
||||||
return binary_pat.fmap(b => val => b ? val : undefined).appRight(this)._removeUndefineds()
|
return binary_pat.fmap(b => val => b ? val : undefined).appRight(this)._removeUndefineds()
|
||||||
}
|
}
|
||||||
|
|
||||||
inv() {
|
invert() {
|
||||||
// Swap true/false in a binary pattern
|
// Swap true/false in a binary pattern
|
||||||
return this.fmap(x => !x)
|
return this.fmap(x => !x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inv() {
|
||||||
|
// alias for invert()
|
||||||
|
return this.invert()
|
||||||
|
}
|
||||||
|
|
||||||
when(binary_pat, func) {
|
when(binary_pat, func) {
|
||||||
//binary_pat = sequence(binary_pat)
|
//binary_pat = sequence(binary_pat)
|
||||||
const true_pat = binary_pat._filterValues(id)
|
const true_pat = binary_pat._filterValues(id)
|
||||||
@ -791,6 +802,10 @@ const off = curry((t, f, pat) => pat.off(t,f))
|
|||||||
const jux = curry((f, pat) => pat.jux(f))
|
const jux = curry((f, pat) => pat.jux(f))
|
||||||
const append = curry((a, pat) => pat.append(a))
|
const append = curry((a, pat) => pat.append(a))
|
||||||
const superimpose = curry((array, pat) => pat.superimpose(...array))
|
const superimpose = curry((array, pat) => pat.superimpose(...array))
|
||||||
|
const struct = curry((a, pat) => pat.struct(a))
|
||||||
|
const mask = curry((a, pat) => pat.mask(a))
|
||||||
|
const invert = pat => pat.invert()
|
||||||
|
const inv = pat => pat.inv()
|
||||||
|
|
||||||
// problem: curried functions with spread arguments must have pat at the beginning
|
// 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
|
// with this, we cannot keep the pattern open at the end.. solution for now: use array to keep using pat as last arg
|
||||||
@ -845,6 +860,7 @@ Pattern.prototype.bootstrap = () => {
|
|||||||
export {Fraction, TimeSpan, Hap, Pattern,
|
export {Fraction, TimeSpan, Hap, Pattern,
|
||||||
pure, stack, slowcat, fastcat, cat, timeCat, sequence, polymeter, pm, polyrhythm, pr, reify, silence,
|
pure, stack, slowcat, fastcat, cat, timeCat, sequence, polymeter, pm, polyrhythm, pr, reify, silence,
|
||||||
fast, slow, early, late, rev,
|
fast, slow, early, late, rev,
|
||||||
add, sub, mul, div, union, every, when, off, jux, append, superimpose
|
add, sub, mul, div, union, every, when, off, jux, append, superimpose,
|
||||||
|
struct, mask, invert, inv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -251,6 +251,18 @@ describe('Pattern', function() {
|
|||||||
it('Can restructure a pattern', function() {
|
it('Can restructure a pattern', function() {
|
||||||
assert.deepStrictEqual(
|
assert.deepStrictEqual(
|
||||||
sequence("a", "b").struct(sequence(true, true, true)).firstCycle,
|
sequence("a", "b").struct(sequence(true, true, true)).firstCycle,
|
||||||
|
[hap(ts(0,third), ts(0,third), "a"),
|
||||||
|
hap(ts(third, twothirds), ts(third, 0.5), "a"),
|
||||||
|
hap(ts(third, twothirds), ts(0.5, twothirds), "b"),
|
||||||
|
hap(ts(twothirds, 1), ts(twothirds, 1), "b")
|
||||||
|
]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('mask()', function() {
|
||||||
|
it('Can fragment a pattern', function() {
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
sequence("a", "b").mask(sequence(true, true, true)).firstCycle,
|
||||||
[hap(ts(0, 0.5), ts(0,third), "a"),
|
[hap(ts(0, 0.5), ts(0,third), "a"),
|
||||||
hap(ts(0, 0.5), ts(third, 0.5), "a"),
|
hap(ts(0, 0.5), ts(third, 0.5), "a"),
|
||||||
hap(ts(0.5, 1), ts(0.5, twothirds), "b"),
|
hap(ts(0.5, 1), ts(0.5, twothirds), "b"),
|
||||||
@ -258,11 +270,21 @@ describe('Pattern', function() {
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
it('Can mask off parts of a pattern', function() {
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
sequence(["a", "b"], "c").mask(sequence(true, false)).firstCycle,
|
||||||
|
sequence(["a","b"], silence).firstCycle
|
||||||
|
)
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
sequence("a").mask(sequence(true, false)).firstCycle,
|
||||||
|
[hap(ts(0,1),ts(0,0.5), "a")]
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('inv()', function() {
|
describe('invert()', function() {
|
||||||
it('Can invert a binary pattern', function() {
|
it('Can invert a binary pattern', function() {
|
||||||
assert.deepStrictEqual(
|
assert.deepStrictEqual(
|
||||||
sequence(true, false, [true, false]).inv().firstCycle,
|
sequence(true, false, [true, false]).invert().firstCycle,
|
||||||
sequence(false, true, [false, true]).firstCycle
|
sequence(false, true, [false, true]).firstCycle
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user