From f5fb5857df58513aa2d7f76a6c6a0b1eabdcea4e Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 9 Feb 2022 22:41:44 +0000 Subject: [PATCH] fix _fastGap, add compressSpan --- strudel.mjs | 18 ++++++++++++++---- test/pattern.test.mjs | 11 +++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/strudel.mjs b/strudel.mjs index 1c249711..0bb4fecb 100644 --- a/strudel.mjs +++ b/strudel.mjs @@ -434,19 +434,29 @@ class Pattern { // } const qf = function(span) { const cycle = span.begin.sam() - const begin = cycle.add(span.begin.sub(span.cycle).mul(factor).min(1)) - const end = cycle.add(span.end.sub(span.cycle).mul(factor).min(1)) + const begin = cycle.add(span.begin.sub(cycle).mul(factor).min(1)) + const end = cycle.add(span.end.sub(cycle).mul(factor).min(1)) return new TimeSpan(begin, end) } const ef = function(span) { const cycle = span.begin.sam() - const begin = cycle.add(span.begin.sub(span.cycle).div(factor).min(1)) - const end = cycle.add(span.end.sub(span.cycle).div(factor).min(1)) + const begin = cycle.add(span.begin.sub(cycle).div(factor).min(1)) + const end = cycle.add(span.end.sub(cycle).div(factor).min(1)) return new TimeSpan(begin, end) } return this.withQuerySpan(qf).withEventSpan(ef)._splitQueries() } + _compressSpan(span) { + const b = span.begin + const e = span.end + if (b > e || b > 1 || e > 1 || b < 0 || e < 0) { + return silence + } + 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)) diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index b1afb3d9..8dbb33a3 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -3,6 +3,9 @@ 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 { Time } from 'tone'; +import pkg from 'tone'; +const { Time } = pkg; const ts = (begin, end) => new TimeSpan(Fraction(begin), Fraction(end)); const hap = (whole, part, value) => new Hap(whole, part, value) @@ -107,6 +110,14 @@ describe('Pattern', function() { ) }) }) + describe('_compressSpan()', function () { + it('Can squash cycles of a pattern into a given timespan', function () { + assert.deepStrictEqual( + pure("a")._compressSpan(new TimeSpan(0.25, 0.5)).firstCycle, + sequence(silence, "a", silence, silence).firstCycle + ) + }) + }) describe('fast()', function () { it('Makes things faster', function () { assert.equal(pure("a").fast(2).firstCycle.length, 2)