From 5b8c34e50444cf275c5039226f80436cc7954ce0 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 9 Feb 2022 23:50:06 +0000 Subject: [PATCH] timeCat seems to work --- strudel.mjs | 15 +++++++++++++-- test/pattern.test.mjs | 10 +++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/strudel.mjs b/strudel.mjs index 0bb4fecb..5eaa8a20 100644 --- a/strudel.mjs +++ b/strudel.mjs @@ -456,7 +456,6 @@ class Pattern { return this._fastGap(Fraction(1).div(e.sub(b)))._late(b) } - _fast(factor) { const fastQuery = this.withQueryTime(t => t.mul(factor)) return fastQuery.withEventTime(t => t.div(factor)) @@ -604,6 +603,18 @@ function cat(...pats) { return fastcat(...pats) } +function timeCat(...timepats) { + const total = timepats.map(a => a[0]).reduce((a,b) => a.add(b), Fraction(0)) + let begin = Fraction(0) + const pats = [] + for (const [time, pat] of timepats) { + const end = begin.add(time) + pats.push(reify(pat)._compressSpan(new TimeSpan(begin.div(total), end.div(total)))) + begin = end + } + return stack(...pats) +} + function _sequenceCount(x) { if(Array.isArray(x)) { if (x.length == 0) { @@ -670,7 +681,7 @@ const late = curry((a, pat) => pat.late(a)) const rev = pat => pat.rev() export {Fraction, TimeSpan, Hap, Pattern, - pure, stack, slowcat, fastcat, cat, 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 } diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index 8dbb33a3..f234ad77 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -2,7 +2,7 @@ import Fraction from 'fraction.js' import { strict as assert } from 'assert'; -import {TimeSpan, Hap, Pattern, pure, stack, fastcat, slowcat, cat, sequence, polyrhythm, silence, fast} from "../strudel.mjs"; +import {TimeSpan, Hap, Pattern, pure, stack, fastcat, slowcat, cat, sequence, polyrhythm, silence, fast, timeCat} from "../strudel.mjs"; //import { Time } from 'tone'; import pkg from 'tone'; const { Time } = pkg; @@ -224,4 +224,12 @@ describe('Pattern', function() { ) }) }) + describe('timeCat()', function() { + it('Can concatenate patterns with different relative durations', function() { + assert.deepStrictEqual( + sequence("a", ["a", "a"]).firstCycle, + timeCat([1,"a"], [0.5, "a"], [0.5, "a"]).firstCycle + ) + }) + }) })