fix _fastGap, add compressSpan

This commit is contained in:
alex 2022-02-09 22:41:44 +00:00
parent ab2cfdeeec
commit f5fb5857df
2 changed files with 25 additions and 4 deletions

View File

@ -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))

View File

@ -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)