focus tweak for squeezeJoin - another go at fixing #216 (#221)

Fixes squeezeJoin, and in the process struct/keepif. fixes #216

Co-authored-by: Felix Roos <flix91@gmail.com>
This commit is contained in:
Alex McLean 2022-09-24 23:27:25 +01:00 committed by GitHub
parent 852f01fddd
commit 513e0d748d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 678 additions and 655 deletions

View File

@ -649,12 +649,22 @@ export class Pattern {
// pattern of values. In this case it takes whole cycles of the inner pattern to fit each event
// in the outer pattern.
_squeezeJoin() {
// A pattern of patterns, which we call the 'outer' pattern, with patterns
// as values which we call the 'inner' patterns.
const pat_of_pats = this;
function query(state) {
// Get the events with the inner patterns. Ignore continuous events (without 'wholes')
const haps = pat_of_pats.discreteOnly().query(state);
// A function to map over the events from the outer pattern.
function flatHap(outerHap) {
const pat = outerHap.value._focusSpan(outerHap.wholeOrPart());
const innerHaps = pat.query(state.setSpan(outerHap.part));
// Get the inner pattern, slowed and shifted so that the 'whole'
// timespan of the outer event corresponds to the first cycle of the
// inner event
const inner_pat = outerHap.value._focusSpan(outerHap.wholeOrPart());
// Get the inner events, from the timespan of the outer event's part
const innerHaps = inner_pat.query(state.setSpan(outerHap.part));
// A function to map over the inner events, to combine them with the
// outer event
function munge(outer, inner) {
let whole = undefined;
if (inner.whole && outer.whole) {
@ -753,7 +763,7 @@ export class Pattern {
// Similar to compress, but doesn't leave gaps, and the 'focus' can be
// bigger than a cycle
_focus(b, e) {
return this._fast(Fraction(1).div(e.sub(b))).late(b);
return this._fast(Fraction(1).div(e.sub(b))).late(b.cyclePos());
}
_focusSpan(span) {
@ -1347,11 +1357,16 @@ function _composeOp(a, b, func) {
pat = preprocess(pat);
other = preprocess(other);
}
var result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op));
var result;
// hack to remove undefs when doing 'keepif'
if (what === 'keepif') {
// avoid union, as we want to throw away the value of 'b' completely
result = pat['_op' + how](other, (a) => (b) => op(a, b));
result = result._removeUndefineds();
}
else {
result = pat['_op' + how](other, (a) => (b) => _composeOp(a, b, op));
}
return result;
};
if (how === 'Squeeze') {

View File

@ -42,6 +42,7 @@ import {
id,
ply,
rev,
time,
} from '../index.mjs';
import { steady } from '../signal.mjs';
@ -791,6 +792,13 @@ describe('Pattern', () => {
).firstCycle(),
);
});
it('Squeezes to the correct cycle', () => {
expect(
pure(time.struct(true))._squeezeJoin().queryArc(3,4).map(x => x.value)
).toStrictEqual(
[Fraction(3.5)]
)
});
});
describe('ply', () => {
it('Can ply(3)', () => {

File diff suppressed because it is too large Load Diff