Skip to content

Commit

Permalink
fix fastgap for events that go across cycle boundaries (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaxu committed Oct 6, 2022
1 parent b7b576e commit a5bf8f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/core/pattern.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,20 @@ export class Pattern {
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(cycle).div(factor).min(1));
const end = cycle.add(span.end.sub(cycle).div(factor).min(1));
return new TimeSpan(begin, end);
const ef = function (hap) {
const begin = hap.part.begin;
const end = hap.part.end;
const cycle = begin.sam();
const beginPos = begin.sub(cycle).div(factor).min(1);
const endPos = end.sub(cycle).div(factor).min(1);
const newPart = new TimeSpan(cycle.add(beginPos), cycle.add(endPos))
const newWhole = !hap.whole ? undefined : new TimeSpan(
newPart.begin.sub(begin.sub(hap.whole.begin).div(factor)),
newPart.end.add(hap.whole.end.sub(end).div(factor))
);
return new Hap(newWhole, newPart, hap.value, hap.context);
};
return this.withQuerySpan(qf).withHapSpan(ef)._splitQueries();
return this.withQuerySpan(qf)._withHap(ef)._splitQueries();
}

// Compress each cycle into the given timespan, leaving a gap
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/pattern.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ describe('Pattern', () => {
sequence(['a', 'b', 'c'], silence, ['a', 'b', 'c'], silence).firstCycle(),
);
});
it('copes with breaking up events across cycles', () => {
expect(pure('a').slow(2)._fastGap(2)._setContext({}).query(st(0, 2))).toStrictEqual(
[hap(ts(0, 1), ts(0, 0.5), 'a'), hap(ts(0.5, 1.5), ts(1, 1.5), 'a')]
);
});
});
describe('_compressSpan()', () => {
it('Can squash cycles of a pattern into a given timespan', () => {
Expand Down

0 comments on commit a5bf8f6

Please sign in to comment.