@zeeuw/riff 0.0.0-stage → 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/data/registry-data.js +3 -0
- package/data/signatures.js +3 -0
- package/docs/clean-room.md +450 -0
- package/package.json +69 -4
- package/src/audio/clock.js +383 -0
- package/src/audio/engine.js +678 -0
- package/src/audio/index.js +394 -0
- package/src/audio/offline.js +73 -0
- package/src/audio/params.js +397 -0
- package/src/audio/samples.js +274 -0
- package/src/audio/unlock.js +70 -0
- package/src/audio/voices.js +44 -0
- package/src/combinators.js +150 -0
- package/src/compat.js +16 -0
- package/src/constructors.js +236 -0
- package/src/controls.js +199 -0
- package/src/draw.js +252 -0
- package/src/euclid.js +57 -0
- package/src/evaluate.js +251 -0
- package/src/extras/check.js +678 -0
- package/src/extras/describe.js +433 -0
- package/src/extras/editor.js +369 -0
- package/src/extras/index.js +10 -0
- package/src/fraction.js +300 -0
- package/src/hap.js +64 -0
- package/src/helpers.js +68 -0
- package/src/index.js +74 -0
- package/src/mini/index.js +36 -0
- package/src/mini/parser.js +400 -0
- package/src/ops.js +293 -0
- package/src/outline.js +175 -0
- package/src/pattern.js +365 -0
- package/src/random.js +344 -0
- package/src/register.js +83 -0
- package/src/registry.js +52 -0
- package/src/samples.js +14 -0
- package/src/sampling.js +123 -0
- package/src/scope.js +77 -0
- package/src/signals.js +76 -0
- package/src/stepwise.js +150 -0
- package/src/structure.js +636 -0
- package/src/time.js +70 -0
- package/src/tonal.js +583 -0
- package/src/transforms.js +125 -0
- package/src/transpile.js +394 -0
- package/src/util.js +80 -0
- package/src/visuals.js +216 -0
package/src/structure.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
// Structure, time windows and the pick family: compress/zoom/fastGap, pick,
|
|
2
|
+
// inhabit, squeeze, arrange, binary, beat, arp, ribbon, xfade, brak, press,
|
|
3
|
+
// within, euclidish, plyWith, bite, chunkInto, repeatCycles, replicate,
|
|
4
|
+
// stack-aligned, focus, the *All variants, pickOut, seqPLoop, morph and stut.
|
|
5
|
+
// Each follows its spec file in spec/functions/ (named in the section
|
|
6
|
+
// comment). Regrouped from the Tier 2/3 batch modules (audit 6).
|
|
7
|
+
import { chunk } from './combinators.js';
|
|
8
|
+
import { fastBy, fastcat, shiftBy, stack } from './constructors.js';
|
|
9
|
+
import { euclidBools } from './euclid.js';
|
|
10
|
+
import { ONE, ZERO, fraction } from './fraction.js';
|
|
11
|
+
import { Hap, combineContext } from './hap.js';
|
|
12
|
+
import { applyOp } from './ops.js';
|
|
13
|
+
import { Pattern, assignMap, pure, reify, silence } from './pattern.js';
|
|
14
|
+
import { alias, register, registerFree } from './register.js';
|
|
15
|
+
import { TimeSpan } from './time.js';
|
|
16
|
+
import { isTrue, struct } from './transforms.js';
|
|
17
|
+
import { asMap, floorMod, frac, isValueMap, wholeCount as count } from './util.js';
|
|
18
|
+
|
|
19
|
+
// --- compress, zoom, fastGap (spec/functions/compress.md) ---------------------
|
|
20
|
+
|
|
21
|
+
// Source cycle c played n x faster from c; what does not fit in the cycle is
|
|
22
|
+
// dropped (n < 1 plays source [c, c + n)).
|
|
23
|
+
export function fastGapBy(pat, n) {
|
|
24
|
+
const f = fraction(n);
|
|
25
|
+
if (f.sign() <= 0) return silence;
|
|
26
|
+
const len = f.gte(1) ? ONE.div(f) : ONE;
|
|
27
|
+
return new Pattern((state) => {
|
|
28
|
+
const out = [];
|
|
29
|
+
for (const span of state.span.spanCycles()) {
|
|
30
|
+
const c = span.begin.sam();
|
|
31
|
+
const q = span.clip(new TimeSpan(c, c.add(len)));
|
|
32
|
+
if (!q) continue;
|
|
33
|
+
const back = (x) => c.add(x.sub(c).div(f));
|
|
34
|
+
for (const h of pat.query(state.setSpan(q.withTime((t) => c.add(t.sub(c).mul(f)))))) out.push(h.withTime(back));
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}, pat._steps);
|
|
38
|
+
}
|
|
39
|
+
export const fastGap = register('fastGap', fastGapBy, { aliases: ['fastgap'], memo: true, curry: 1 });
|
|
40
|
+
export const fastgap = fastGap;
|
|
41
|
+
|
|
42
|
+
// Cycle c squeezed into [c + b, c + e); silence unless 0 <= b < e <= 1.
|
|
43
|
+
export const compress = register('compress', (pat, b, e) => {
|
|
44
|
+
b = fraction(b); e = fraction(e);
|
|
45
|
+
if (!b.lt(e) || b.sign() < 0 || e.gt(1)) return silence;
|
|
46
|
+
return shiftBy(fastGapBy(pat, ONE.div(e.sub(b))), b);
|
|
47
|
+
}, { curry: 2 });
|
|
48
|
+
|
|
49
|
+
// Cycle c plays source [c + b, c + e) stretched over the cycle; times map back
|
|
50
|
+
// by the query's cycle, so e > 1 reads into the next source cycle.
|
|
51
|
+
export function zoomSpan(pat, b, e) {
|
|
52
|
+
b = fraction(b); e = fraction(e);
|
|
53
|
+
if (!b.lt(e)) return silence;
|
|
54
|
+
const d = e.sub(b);
|
|
55
|
+
return new Pattern((state) => {
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const span of state.span.spanCycles()) {
|
|
58
|
+
const c = span.begin.sam();
|
|
59
|
+
const off = c.add(b);
|
|
60
|
+
const back = (x) => c.add(x.sub(off).div(d));
|
|
61
|
+
for (const h of pat.query(state.setSpan(span.withTime((t) => off.add(t.sub(c).mul(d)))))) out.push(h.withTime(back));
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}, pat._steps === undefined ? undefined : fraction(pat._steps).mul(d));
|
|
65
|
+
}
|
|
66
|
+
export const zoom = register('zoom', zoomSpan, { curry: 2 });
|
|
67
|
+
|
|
68
|
+
// --- pick family (spec/functions/pick.md) ----------------------------------------
|
|
69
|
+
|
|
70
|
+
const isLookup = (x) => Array.isArray(x) || isValueMap(x);
|
|
71
|
+
// Index rules per family: clamp (pick, inhabit, ...), mod (*mod: wrap, negative
|
|
72
|
+
// is silence), squeeze (past the end clamps, negative counts from the end).
|
|
73
|
+
function entryOf(table, i, mode) {
|
|
74
|
+
if (Array.isArray(table)) {
|
|
75
|
+
const len = table.length;
|
|
76
|
+
let k = Math.floor(Number(i));
|
|
77
|
+
if (len === 0 || Number.isNaN(k)) return undefined;
|
|
78
|
+
if (mode === 'mod') { if (k < 0) return undefined; k %= len; }
|
|
79
|
+
else if (mode === 'squeeze') k = k >= len ? len - 1 : k < 0 ? Math.max(0, len + k) : k;
|
|
80
|
+
else k = Math.min(Math.max(k, 0), len - 1);
|
|
81
|
+
return table[k];
|
|
82
|
+
}
|
|
83
|
+
return Object.prototype.hasOwnProperty.call(table, i) ? table[i] : undefined;
|
|
84
|
+
}
|
|
85
|
+
// Entries reified once (strings are mini-notation).
|
|
86
|
+
function tableOf(lookup) {
|
|
87
|
+
if (Array.isArray(lookup)) return lookup.map(reify);
|
|
88
|
+
const t = {};
|
|
89
|
+
for (const [k, v] of Object.entries(lookup)) t[k] = reify(v);
|
|
90
|
+
return t;
|
|
91
|
+
}
|
|
92
|
+
const chooser = (mode, join) => (index, lookup) => {
|
|
93
|
+
if (!isLookup(lookup)) return silence;
|
|
94
|
+
const table = tableOf(lookup);
|
|
95
|
+
return index.fmap((i) => entryOf(table, i, mode) ?? silence)[join]();
|
|
96
|
+
};
|
|
97
|
+
const pickImpl = chooser('clamp', 'innerJoin');
|
|
98
|
+
const pickmodImpl = chooser('mod', 'innerJoin');
|
|
99
|
+
const inhabitImpl = chooser('clamp', 'squeezeJoin');
|
|
100
|
+
const inhabitmodImpl = chooser('mod', 'squeezeJoin');
|
|
101
|
+
|
|
102
|
+
// Free forms accept (index, lookup) and (lookup, index).
|
|
103
|
+
const eitherOrder = (impl) => (a, b) => (isLookup(a) && !isLookup(b) ? impl(reify(b), a) : impl(reify(a), b));
|
|
104
|
+
for (const [name, impl] of [['pick', pickImpl], ['pickmod', pickmodImpl], ['inhabit', inhabitImpl], ['inhabitmod', inhabitmodImpl]]) {
|
|
105
|
+
register(name, impl, { patternArgs: 0, free: false });
|
|
106
|
+
registerFree(name, eitherOrder(impl));
|
|
107
|
+
}
|
|
108
|
+
export const pick = eitherOrder(pickImpl);
|
|
109
|
+
export const pickmod = eitherOrder(pickmodImpl);
|
|
110
|
+
export const inhabit = eitherOrder(inhabitImpl);
|
|
111
|
+
export const inhabitmod = eitherOrder(inhabitmodImpl);
|
|
112
|
+
|
|
113
|
+
// fns[i](pat) per index hap (inner join).
|
|
114
|
+
export const pickF = register('pickF', (pat, index, fns) => {
|
|
115
|
+
const done = fns.map((f) => reify(f(pat)));
|
|
116
|
+
return reify(index).fmap((i) => entryOf(done, i, 'clamp') ?? silence).innerJoin();
|
|
117
|
+
}, { patternArgs: 0 });
|
|
118
|
+
// Silent for every input in Strudel 1.2.5; kept as the contract.
|
|
119
|
+
export const pickmodF = register('pickmodF', () => silence, { patternArgs: 0 });
|
|
120
|
+
|
|
121
|
+
// At each index hap onset the chosen pattern starts again: from its cycle 0
|
|
122
|
+
// (restart) or from the start of the current cycle (reset).
|
|
123
|
+
const restarter = (mode, fromZero) => (index, lookup) => {
|
|
124
|
+
if (!isLookup(lookup)) return silence;
|
|
125
|
+
const table = tableOf(lookup);
|
|
126
|
+
return new Pattern((state) => {
|
|
127
|
+
const out = [];
|
|
128
|
+
for (const oh of index.query(state)) {
|
|
129
|
+
const pat = entryOf(table, oh.value, mode);
|
|
130
|
+
if (!pat) continue;
|
|
131
|
+
const b = oh.wholeOrPart().begin;
|
|
132
|
+
const shifted = shiftBy(pat, fromZero ? b : b.sub(b.sam()));
|
|
133
|
+
for (const ih of shifted.query(state.setSpan(oh.part))) {
|
|
134
|
+
const part = ih.part.intersect(oh.part);
|
|
135
|
+
if (!part) continue;
|
|
136
|
+
let whole = ih.whole;
|
|
137
|
+
if (whole && oh.whole) { whole = whole.intersect(oh.whole); if (!whole) continue; }
|
|
138
|
+
out.push(new Hap(whole, part, ih.value, combineContext(oh.context, ih.context)));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
// Free forms are pattern-last, (lookup, index): `pickRestart(index, lookup)`
|
|
145
|
+
// therefore gives silence, as recorded (fixture pickRestart-fn).
|
|
146
|
+
for (const [name, impl] of [
|
|
147
|
+
['pickRestart', restarter('clamp', true)], ['pickReset', restarter('clamp', false)],
|
|
148
|
+
['pickmodRestart', restarter('mod', true)], ['pickmodReset', restarter('mod', false)],
|
|
149
|
+
]) register(name, impl, { patternArgs: 0 });
|
|
150
|
+
|
|
151
|
+
// squeeze(index, list): the chosen pattern's cycle squeezed into each index
|
|
152
|
+
// hap. The method form makes the receiver the lookup: `pat.squeeze(list)`
|
|
153
|
+
// squeezes the list (as a sequence) into each receiver hap, values merged.
|
|
154
|
+
export function squeeze(index, list) {
|
|
155
|
+
if (!Array.isArray(list)) throw new TypeError('e.map is not a function');
|
|
156
|
+
const table = list.map(reify);
|
|
157
|
+
return reify(index).fmap((i) => entryOf(table, i, 'squeeze') ?? silence).squeezeJoin();
|
|
158
|
+
}
|
|
159
|
+
registerFree('squeeze', squeeze);
|
|
160
|
+
register('squeeze', (pat, list) => applyOp('set', 'squeeze', pat, list), { patternArgs: 0, free: false });
|
|
161
|
+
|
|
162
|
+
// --- arrangement and structure helpers ------------------------------------------
|
|
163
|
+
|
|
164
|
+
// Sections in turn, period = the sum of lengths; a section's own time advances
|
|
165
|
+
// only while it plays (spec/functions/arrange.md). Haps by section, then time.
|
|
166
|
+
export function arrange(...sections) {
|
|
167
|
+
const secs = [];
|
|
168
|
+
let total = ZERO;
|
|
169
|
+
for (const [n, p] of sections) {
|
|
170
|
+
const len = fraction(n);
|
|
171
|
+
secs.push({ start: total, len, pat: reify(p) });
|
|
172
|
+
total = total.add(len);
|
|
173
|
+
}
|
|
174
|
+
if (total.sign() <= 0) return silence;
|
|
175
|
+
return new Pattern((state) => {
|
|
176
|
+
const { begin, end } = state.span;
|
|
177
|
+
const point = begin.eq(end);
|
|
178
|
+
const out = [];
|
|
179
|
+
for (const s of secs) {
|
|
180
|
+
if (s.len.sign() <= 0) continue;
|
|
181
|
+
for (let P = begin.sub(s.start).div(total).floor(); ; P = P.add(1)) {
|
|
182
|
+
const bb = P.mul(total).add(s.start);
|
|
183
|
+
if (bb.gt(end) || (bb.eq(end) && !point)) break;
|
|
184
|
+
const be = bb.add(s.len);
|
|
185
|
+
const qb = bb.max(begin), qe = be.min(end);
|
|
186
|
+
if (qb.gt(qe) || (qb.eq(qe) && !point)) continue;
|
|
187
|
+
const shift = P.mul(s.len).sub(bb); // source time = t + shift
|
|
188
|
+
for (const h of s.pat.query(state.setSpan(new TimeSpan(qb.add(shift), qe.add(shift))))) out.push(h.withTime((x) => x.sub(shift)));
|
|
189
|
+
if (point) break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
registerFree('arrange', arrange);
|
|
196
|
+
|
|
197
|
+
// binaryN(n, bits): the lowest `bits` bits of n, most significant first.
|
|
198
|
+
const bitsOf = (n, bits) => {
|
|
199
|
+
const v = Math.floor(Number(n));
|
|
200
|
+
const k = Math.floor(Number(bits));
|
|
201
|
+
if (!(k > 0)) return silence;
|
|
202
|
+
const out = [];
|
|
203
|
+
for (let i = k - 1; i >= 0; i--) out.push(Math.floor(v / 2 ** i) % 2);
|
|
204
|
+
return fastcat(...out);
|
|
205
|
+
};
|
|
206
|
+
const isPat = (a) => a instanceof Pattern || typeof a === 'string';
|
|
207
|
+
// Patterned: the bits pattern's locations appear four times, then n's (the
|
|
208
|
+
// recorded context shape of binaryN-patterned and binary-patterned).
|
|
209
|
+
export function binaryN(n, bits) {
|
|
210
|
+
if (!isPat(n) && !isPat(bits)) return bitsOf(n, bits);
|
|
211
|
+
const bp = reify(bits).withContext((c) => {
|
|
212
|
+
const l = c?.locations ?? [];
|
|
213
|
+
return { ...c, locations: [...l, ...l, ...l, ...l] };
|
|
214
|
+
});
|
|
215
|
+
const np = reify(n);
|
|
216
|
+
return bp.fmap((k) => np.fmap((v) => bitsOf(v, k)).innerJoin()).innerJoin();
|
|
217
|
+
}
|
|
218
|
+
const bitLength = (a) => { const v = Math.floor(Number(a)); return v > 0 ? v.toString(2).length : 0; };
|
|
219
|
+
export function binary(n) {
|
|
220
|
+
if (!isPat(n)) return bitsOf(n, bitLength(n));
|
|
221
|
+
const np = reify(n);
|
|
222
|
+
return binaryN(np, np.fmap(bitLength));
|
|
223
|
+
}
|
|
224
|
+
registerFree('binaryN', binaryN);
|
|
225
|
+
registerFree('binary', binary);
|
|
226
|
+
|
|
227
|
+
// Haps of length 1/div at step `step` of each cycle, values from the source.
|
|
228
|
+
export const beat = register('beat', (pat, step, div) => {
|
|
229
|
+
const d = fraction(div);
|
|
230
|
+
if (d.sign() <= 0) return silence;
|
|
231
|
+
const k = fraction(step);
|
|
232
|
+
const b0 = k.div(d), b1 = k.add(1).div(d);
|
|
233
|
+
const bools = new Pattern((state) => {
|
|
234
|
+
const out = [];
|
|
235
|
+
for (const span of state.span.spanCycles()) {
|
|
236
|
+
const c = span.begin.sam();
|
|
237
|
+
const w = new TimeSpan(c.add(b0), c.add(b1));
|
|
238
|
+
const part = span.clip(w);
|
|
239
|
+
if (part) out.push(new Hap(w, part, true));
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
});
|
|
243
|
+
return struct(bools, pat);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Groups of haps with the same whole; f picks one. The result's whole is the
|
|
247
|
+
// cycle, its part the chosen hap's span (spec/functions/arpWith.md).
|
|
248
|
+
export const arpWith = register('arpWith', (pat, f) => new Pattern((state) => {
|
|
249
|
+
const groups = new Map();
|
|
250
|
+
for (const h of pat.query(state)) {
|
|
251
|
+
if (!h.whole) continue;
|
|
252
|
+
const key = `${h.whole}`;
|
|
253
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
254
|
+
groups.get(key).push(h);
|
|
255
|
+
}
|
|
256
|
+
const out = [];
|
|
257
|
+
for (const haps of groups.values()) {
|
|
258
|
+
const ch = f(haps);
|
|
259
|
+
if (!ch || !ch.part) continue;
|
|
260
|
+
for (const ph of pure(ch.value).query(state.setSpan(ch.part))) out.push(new Hap(ph.whole, ph.part, ph.value, ch.context));
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}), { patternArgs: 0 });
|
|
264
|
+
|
|
265
|
+
// chunk under another name: every recorded case equals chunk(n, f).
|
|
266
|
+
export const fastChunk = register('fastChunk', (pat, n, f) => chunk(n, f, pat), { patternArgs: 0, aliases: ['fastchunk'], curry: 2 });
|
|
267
|
+
export const fastchunk = fastChunk;
|
|
268
|
+
|
|
269
|
+
// The source plays only during trigger haps and starts again at each onset.
|
|
270
|
+
register('reset', (pat, trigger) => applyOp('keep', 'reset', pat, trigger), { patternArgs: 0, free: false });
|
|
271
|
+
register('restart', (pat, trigger) => applyOp('keep', 'restart', pat, trigger), { patternArgs: 0, free: false });
|
|
272
|
+
|
|
273
|
+
// Loop source [offset, offset + cycles) from time 0; haps cut at the loop edge
|
|
274
|
+
// take the cut span as whole (a restart on a 1-per-`cycles` trigger).
|
|
275
|
+
export const ribbon = register('ribbon', (pat, offset, cycles) =>
|
|
276
|
+
applyOp('keep', 'restart', shiftBy(pat, fraction(offset).neg()), reify(true).slow(cycles)), { curry: 2 });
|
|
277
|
+
|
|
278
|
+
// Stack of a and b with gains from pos; an existing gain is multiplied.
|
|
279
|
+
const scaleGain = (pat, g) => pat.fmap((v) => { const o = asMap(v); o.gain = (o.gain ?? 1) * g; return o; });
|
|
280
|
+
register('xfade', (a, pos, b) => {
|
|
281
|
+
const p = Number(pos);
|
|
282
|
+
return stack(scaleGain(a, Math.min(1, 2 * (1 - p))), scaleGain(reify(b), Math.min(1, 2 * p)));
|
|
283
|
+
}, { patternArgs: 1, free: false });
|
|
284
|
+
export const xfade = (a, pos, b) => reify(a).xfade(pos, b);
|
|
285
|
+
registerFree('xfade', xfade);
|
|
286
|
+
|
|
287
|
+
// `set` with structure from both sides (mix) or from the source (in).
|
|
288
|
+
register('mix', (pat, other) => applyOp('set', 'mix', pat, other), { patternArgs: 0, free: false });
|
|
289
|
+
register('in', (pat, other) => applyOp('set', 'in', pat, other), { patternArgs: 0, free: false });
|
|
290
|
+
|
|
291
|
+
// The source with gain 1, then count - 1 echoes late by i time, gain fb^i.
|
|
292
|
+
export const stut = register('stut', (pat, count, feedback, time) => {
|
|
293
|
+
const fb = Number(feedback);
|
|
294
|
+
const t = fraction(time);
|
|
295
|
+
const layers = [pat.fmap(assignMap([['gain', 1]]))];
|
|
296
|
+
for (let i = 1; i < Number(count); i++) layers.push(shiftBy(pat, t.mul(i)).fmap(assignMap([['gain', fb ** i]])));
|
|
297
|
+
return stack(...layers);
|
|
298
|
+
}, { patternArgs: 1 });
|
|
299
|
+
|
|
300
|
+
// Haps of `pat` over source span [sb, se), placed linearly onto [ob, oe), for
|
|
301
|
+
// the query part q (inside [ob, oe)).
|
|
302
|
+
function placed(pat, state, q, sb, se, ob, oe) {
|
|
303
|
+
const scale = se.sub(sb).div(oe.sub(ob));
|
|
304
|
+
const toSrc = (t) => sb.add(t.sub(ob).mul(scale));
|
|
305
|
+
const toOut = (t) => ob.add(t.sub(sb).div(scale));
|
|
306
|
+
return pat.query(state.setSpan(q.withTime(toSrc))).map((h) => h.withTime(toOut));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Tier 3 alias (spec/functions/tier3-aliases.md): the target's own function.
|
|
310
|
+
export const rib = alias('rib', 'ribbon');
|
|
311
|
+
|
|
312
|
+
// --- brak (spec/functions/brak.md) --------------------------------------------
|
|
313
|
+
// Odd cycles: the cycle squeezed into [c + 1/4, c + 3/4).
|
|
314
|
+
const QUARTER = frac(1, 4);
|
|
315
|
+
const THREE_Q = frac(3, 4);
|
|
316
|
+
export const brak = register('brak', (pat) => new Pattern((state) => {
|
|
317
|
+
const out = [];
|
|
318
|
+
for (const span of state.span.spanCycles()) {
|
|
319
|
+
const c = span.begin.sam();
|
|
320
|
+
if (floorMod(c.toNumber(), 2) === 0) { for (const h of pat.query(state.setSpan(span))) out.push(h); continue; }
|
|
321
|
+
const ob = c.add(QUARTER);
|
|
322
|
+
const oe = c.add(THREE_Q);
|
|
323
|
+
const q = span.clip(new TimeSpan(ob, oe));
|
|
324
|
+
if (q) for (const h of placed(pat, state, q, c, c.add(ONE), ob, oe)) out.push(h);
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}, pat._steps), { patternArgs: 0 });
|
|
328
|
+
|
|
329
|
+
// --- press / pressBy (spec/functions/press.md) ----------------------------------
|
|
330
|
+
function pressImpl(pat, r) {
|
|
331
|
+
r = fraction(r);
|
|
332
|
+
return pat.withHaps((haps) => {
|
|
333
|
+
const out = [];
|
|
334
|
+
for (const h of haps) {
|
|
335
|
+
if (!h.whole) { out.push(h); continue; }
|
|
336
|
+
const w = h.whole;
|
|
337
|
+
const nw = new TimeSpan(w.begin.add(r.mul(w.end.sub(w.begin))), w.end);
|
|
338
|
+
const np = h.part.clip(nw);
|
|
339
|
+
if (np) out.push(new Hap(nw, np, h.value, h.context));
|
|
340
|
+
}
|
|
341
|
+
return out;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
export const pressBy = register('pressBy', pressImpl, { memo: true });
|
|
345
|
+
export const press = register('press', (pat) => pressImpl(pat, frac(1, 2)), { patternArgs: 0 });
|
|
346
|
+
|
|
347
|
+
// --- within (spec/functions/within.md) ------------------------------------------
|
|
348
|
+
// Inside = haps whose whole begins at cycle position p with b <= p <= e.
|
|
349
|
+
export const within = register('within', (pat, b, e, f) => {
|
|
350
|
+
b = fraction(b); e = fraction(e);
|
|
351
|
+
const inside = (h) => { const p = h.wholeOrPart().begin.cyclePos(); return p.gte(b) && p.lte(e); };
|
|
352
|
+
const ins = pat.filterHaps(inside);
|
|
353
|
+
const outs = pat.filterHaps((h) => !inside(h));
|
|
354
|
+
const fin = f(ins);
|
|
355
|
+
return new Pattern((state) => [...fin.query(state), ...outs.query(state)], pat._steps);
|
|
356
|
+
}, { patternArgs: 0 });
|
|
357
|
+
|
|
358
|
+
// --- inv (spec/functions/inv.md) ----------------------------------------------
|
|
359
|
+
export const inv = register('inv', (pat) => pat.fmap((v) => !isTrue(v)), { patternArgs: 0 });
|
|
360
|
+
|
|
361
|
+
// --- euclidish / eish (spec/functions/euclidish.md) -----------------------------
|
|
362
|
+
// Pulse i begins at u_i + groove * (i/k - u_i), lasts 1/n.
|
|
363
|
+
export const euclidish = register('euclidish', (pat, k, n, groove) => {
|
|
364
|
+
const bools = euclidBools(k, n);
|
|
365
|
+
const N = bools.length;
|
|
366
|
+
if (N === 0) return silence;
|
|
367
|
+
const onsets = [];
|
|
368
|
+
bools.forEach((b, i) => { if (b) onsets.push(frac(i, N)); });
|
|
369
|
+
const K = onsets.length;
|
|
370
|
+
if (K === 0) return silence;
|
|
371
|
+
const g = fraction(groove);
|
|
372
|
+
const len = frac(1, N);
|
|
373
|
+
const pulses = onsets.map((u, i) => { const b = u.add(g.mul(frac(i, K).sub(u))); return [b, b.add(len)]; });
|
|
374
|
+
const rhythm = new Pattern((state) => {
|
|
375
|
+
const out = [];
|
|
376
|
+
for (const span of state.span.spanCycles()) {
|
|
377
|
+
const c = span.begin.sam();
|
|
378
|
+
for (const [pb, pe] of pulses) {
|
|
379
|
+
const whole = new TimeSpan(c.add(pb), c.add(pe));
|
|
380
|
+
const part = span.clip(whole);
|
|
381
|
+
if (part) out.push(new Hap(whole, part, true));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return out;
|
|
385
|
+
}, N);
|
|
386
|
+
return pat.struct(rhythm);
|
|
387
|
+
}, { aliases: ['eish'] });
|
|
388
|
+
export const eish = euclidish;
|
|
389
|
+
|
|
390
|
+
// --- plyWith / plyForEach (spec/functions/plyWith.md) ----------------------------
|
|
391
|
+
function plyer(each) {
|
|
392
|
+
return (pat, n, f) => {
|
|
393
|
+
const k = count(n);
|
|
394
|
+
if (k === 0) return silence;
|
|
395
|
+
const out = pat.fmap((v) => {
|
|
396
|
+
const copies = [];
|
|
397
|
+
let x = pure(v);
|
|
398
|
+
for (let i = 0; i < k; i++) {
|
|
399
|
+
if (each) copies.push(i === 0 ? pure(v) : f(pure(v), i));
|
|
400
|
+
else { if (i > 0) x = f(x); copies.push(x); }
|
|
401
|
+
}
|
|
402
|
+
return fastcat(...copies);
|
|
403
|
+
}).squeezeJoin();
|
|
404
|
+
out._steps = pat._steps;
|
|
405
|
+
return out;
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
export const plyWith = register('plyWith', plyer(false), { patternArgs: 1, aliases: ['plywith'] });
|
|
409
|
+
export const plywith = plyWith;
|
|
410
|
+
export const plyForEach = register('plyForEach', plyer(true), { patternArgs: 1, aliases: ['plyforeach'] });
|
|
411
|
+
export const plyforeach = plyForEach;
|
|
412
|
+
|
|
413
|
+
// --- bite (spec/functions/bite.md) --------------------------------------------
|
|
414
|
+
// Each index hap plays piece index (mod n) of the current cycle, squeezed in.
|
|
415
|
+
// A patterned n is joined here (not by register) so the locations read
|
|
416
|
+
// source, n, index, as the oracle lists them.
|
|
417
|
+
function biteQuery(pat, n, idx, state, nctx) {
|
|
418
|
+
const out = [];
|
|
419
|
+
const k = count(n);
|
|
420
|
+
if (k === 0) return out;
|
|
421
|
+
for (const ih of idx.query(state)) {
|
|
422
|
+
if (!ih.whole) continue;
|
|
423
|
+
const w = ih.whole;
|
|
424
|
+
const c = w.begin.sam();
|
|
425
|
+
const i = floorMod(Math.floor(Number(ih.value)), k);
|
|
426
|
+
if (!Number.isFinite(i)) continue;
|
|
427
|
+
for (const h of placed(pat, state, ih.part, c.add(frac(i, k)), c.add(frac(i + 1, k)), w.begin, w.end)) {
|
|
428
|
+
const part = h.part.clip(ih.part);
|
|
429
|
+
const whole = h.whole && h.whole.intersect(w);
|
|
430
|
+
if (!part || (h.whole && !whole)) continue;
|
|
431
|
+
const ctx = nctx ? combineContext(h.context, nctx) : h.context;
|
|
432
|
+
out.push(new Hap(whole, part, h.value, combineContext(ctx, ih.context)));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return out;
|
|
436
|
+
}
|
|
437
|
+
export const bite = register('bite', (pat, n, ipat) => {
|
|
438
|
+
const idx = reify(ipat);
|
|
439
|
+
if (!(n instanceof Pattern) && typeof n !== 'string') return new Pattern((state) => biteQuery(pat, n, idx, state, null));
|
|
440
|
+
const npat = reify(n);
|
|
441
|
+
return new Pattern((state) => {
|
|
442
|
+
const out = [];
|
|
443
|
+
for (const nh of npat.query(state)) {
|
|
444
|
+
for (const h of biteQuery(pat, nh.value, idx, state.setSpan(nh.part), nh.context)) out.push(h);
|
|
445
|
+
}
|
|
446
|
+
return out;
|
|
447
|
+
});
|
|
448
|
+
}, { patternArgs: 0 });
|
|
449
|
+
|
|
450
|
+
// --- chunkInto / chunkBackInto (spec/functions/chunkInto.md) ----------------------
|
|
451
|
+
// Chunk j of cycle c: f works on the chunk zoomed to a cycle, squeezed back.
|
|
452
|
+
const chunkIntoImpl = (back) => (pat, n, f) => {
|
|
453
|
+
const k = count(n);
|
|
454
|
+
if (k === 0) return silence;
|
|
455
|
+
const inner = [];
|
|
456
|
+
const changed = (j) => (inner[j] ??= shiftBy(fastGapBy(f(zoomSpan(pat, frac(j, k), frac(j + 1, k))), k), frac(j, k)));
|
|
457
|
+
return new Pattern((state) => {
|
|
458
|
+
const out = [];
|
|
459
|
+
for (const span of state.span.spanCycles()) {
|
|
460
|
+
const cyc = span.begin.sam();
|
|
461
|
+
const m = floorMod(cyc.toNumber(), k);
|
|
462
|
+
const w = back ? k - 1 - m : m;
|
|
463
|
+
for (let j = 0; j < k; j++) {
|
|
464
|
+
const win = span.clip(new TimeSpan(cyc.add(frac(j, k)), cyc.add(frac(j + 1, k))));
|
|
465
|
+
if (!win) continue;
|
|
466
|
+
for (const h of (j === w ? changed(j) : pat).query(state.setSpan(win))) out.push(h);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return out;
|
|
470
|
+
}, pat._steps);
|
|
471
|
+
};
|
|
472
|
+
export const chunkInto = register('chunkInto', chunkIntoImpl(false), { patternArgs: 1, aliases: ['chunkinto'], curry: 2 });
|
|
473
|
+
export const chunkinto = chunkInto;
|
|
474
|
+
export const chunkBackInto = register('chunkBackInto', chunkIntoImpl(true), { patternArgs: 1, aliases: ['chunkbackinto'], curry: 2 });
|
|
475
|
+
export const chunkbackinto = chunkBackInto;
|
|
476
|
+
|
|
477
|
+
// --- repeatCycles (spec/functions/repeatCycles.md) --------------------------------
|
|
478
|
+
export const repeatCycles = register('repeatCycles', (pat, n) => {
|
|
479
|
+
const k = count(n);
|
|
480
|
+
if (k === 0) return silence;
|
|
481
|
+
return new Pattern((state) => {
|
|
482
|
+
const out = [];
|
|
483
|
+
for (const span of state.span.spanCycles()) {
|
|
484
|
+
const c = span.begin.sam().toNumber();
|
|
485
|
+
const shift = fraction(c - Math.floor(c / k));
|
|
486
|
+
for (const h of pat.query(state.setSpan(span.withTime((t) => t.sub(shift))))) out.push(h.withTime((t) => t.add(shift)));
|
|
487
|
+
}
|
|
488
|
+
return out;
|
|
489
|
+
}, pat._steps);
|
|
490
|
+
}, { memo: true });
|
|
491
|
+
|
|
492
|
+
// --- replicate (spec/functions/replicate.md) ---------------------------------------
|
|
493
|
+
// Output cycle c: source cycle c, n times in slots of 1/n.
|
|
494
|
+
export const replicate = register('replicate', (pat, n) => {
|
|
495
|
+
const k = count(n);
|
|
496
|
+
if (k === 0) return silence;
|
|
497
|
+
return new Pattern((state) => {
|
|
498
|
+
const out = [];
|
|
499
|
+
for (const span of state.span.spanCycles()) {
|
|
500
|
+
const c = span.begin.sam();
|
|
501
|
+
for (let i = 0; i < k; i++) {
|
|
502
|
+
const ob = c.add(frac(i, k));
|
|
503
|
+
const oe = c.add(frac(i + 1, k));
|
|
504
|
+
const q = span.clip(new TimeSpan(ob, oe));
|
|
505
|
+
if (q) for (const h of placed(pat, state, q, c, c.add(ONE), ob, oe)) out.push(h);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return out;
|
|
509
|
+
});
|
|
510
|
+
}, { patternArgs: 0 });
|
|
511
|
+
|
|
512
|
+
// Tier 3 alias (spec/functions/tier3-aliases.md): the target's own function.
|
|
513
|
+
export const pickSqueeze = alias('pickSqueeze', 'inhabit');
|
|
514
|
+
|
|
515
|
+
// Tier 3 alias (spec/functions/tier3-aliases.md): the target's own function.
|
|
516
|
+
export const pickmodSqueeze = alias('pickmodSqueeze', 'inhabitmod');
|
|
517
|
+
|
|
518
|
+
// --- stackLeft, stackRight, stackCentre (spec/functions/stack-aligned.md) ---------
|
|
519
|
+
// Each layer keeps a step size of 1/N (N = the largest step count) and sits at
|
|
520
|
+
// the start, end or middle of the cycle.
|
|
521
|
+
const alignedStack = (where) => (...items) => {
|
|
522
|
+
const pats = items.map(reify);
|
|
523
|
+
const steps = pats.map((p) => (p._steps === undefined ? undefined : fraction(p._steps)));
|
|
524
|
+
const known = steps.filter((s) => s !== undefined && s.sign() > 0);
|
|
525
|
+
if (known.length === 0) return stack(...pats);
|
|
526
|
+
const N = known.reduce((a, b) => a.max(b));
|
|
527
|
+
const layers = pats.map((p, i) => {
|
|
528
|
+
const s = steps[i];
|
|
529
|
+
if (s === undefined || s.sign() <= 0 || s.eq(N)) return p;
|
|
530
|
+
const len = s.div(N);
|
|
531
|
+
const free = ONE.sub(len);
|
|
532
|
+
const off = where === 'left' ? fraction(0) : where === 'right' ? free : free.div(2);
|
|
533
|
+
return p.compress(off, off.add(len));
|
|
534
|
+
});
|
|
535
|
+
return stack(...layers).withSteps(N);
|
|
536
|
+
};
|
|
537
|
+
export const stackLeft = registerFree('stackLeft', alignedStack('left'));
|
|
538
|
+
export const stackRight = registerFree('stackRight', alignedStack('right'));
|
|
539
|
+
export const stackCentre = registerFree('stackCentre', alignedStack('centre'));
|
|
540
|
+
|
|
541
|
+
// --- focus (spec/functions/focus.md) -----------------------------------------------
|
|
542
|
+
// early(sam(b)).fast(1 / (e - b)).late(b).
|
|
543
|
+
export const focus = register('focus', (pat, b, e) => {
|
|
544
|
+
const fb = fraction(b);
|
|
545
|
+
const d = fraction(e).sub(fb);
|
|
546
|
+
if (d.sign() <= 0) return silence;
|
|
547
|
+
return shiftBy(fastBy(shiftBy(pat, fb.sam().neg()), ONE.div(d)), fb);
|
|
548
|
+
}, { curry: 2 });
|
|
549
|
+
|
|
550
|
+
// --- maskAll, structAll, resetAll, restartAll (spec/functions/all-variants.md) ------
|
|
551
|
+
// Every control hap counts as true; only a rest removes or skips.
|
|
552
|
+
const everyHap = (x) => reify(x).fmap(() => true);
|
|
553
|
+
export const maskAll = register('maskAll', (pat, m) => pat.mask(everyHap(m)), { patternArgs: 0 });
|
|
554
|
+
export const structAll = register('structAll', (pat, s) => pat.struct(everyHap(s)), { patternArgs: 0 });
|
|
555
|
+
export const resetAll = register('resetAll', (pat, t) => pat.reset(everyHap(t)), { patternArgs: 0 });
|
|
556
|
+
export const restartAll = register('restartAll', (pat, t) => pat.restart(everyHap(t)), { patternArgs: 0 });
|
|
557
|
+
|
|
558
|
+
// --- pickOut, pickmodOut (spec/functions/pickOut.md) -------------------------------
|
|
559
|
+
// The pick lookup with an outer join: each result keeps the index hap's whole.
|
|
560
|
+
const outerPick = (mod) => (index, lookup) => {
|
|
561
|
+
if (!isLookup(lookup)) return silence;
|
|
562
|
+
const table = Array.isArray(lookup) ? lookup.map(reify) : Object.fromEntries(Object.entries(lookup).map(([k, v]) => [k, reify(v)]));
|
|
563
|
+
return new Pattern((state) => {
|
|
564
|
+
const out = [];
|
|
565
|
+
for (const oh of index.query(state)) {
|
|
566
|
+
const inner = entryOf(table, oh.value, mod ? 'mod' : 'clamp');
|
|
567
|
+
if (!inner) continue;
|
|
568
|
+
for (const ih of inner.query(state.setSpan(oh.part))) {
|
|
569
|
+
const part = oh.part.clip(ih.part);
|
|
570
|
+
if (part) out.push(new Hap(oh.whole, part, ih.value, combineContext(oh.context, ih.context)));
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return out;
|
|
574
|
+
}, index._steps).splitQueries();
|
|
575
|
+
};
|
|
576
|
+
for (const [name, impl] of [['pickOut', outerPick(false)], ['pickmodOut', outerPick(true)]]) {
|
|
577
|
+
register(name, impl, { patternArgs: 0, free: false });
|
|
578
|
+
registerFree(name, eitherOrder(impl));
|
|
579
|
+
}
|
|
580
|
+
export const pickOut = eitherOrder(outerPick(false));
|
|
581
|
+
export const pickmodOut = eitherOrder(outerPick(true));
|
|
582
|
+
|
|
583
|
+
// --- seqPLoop, slowcatPrime (spec/functions/sequencing-variants.md) ----------------
|
|
584
|
+
// Per cycle: which patterns play; each is queried at the real time.
|
|
585
|
+
const perCycle = (choose, steps) => new Pattern((state) => {
|
|
586
|
+
const out = [];
|
|
587
|
+
for (const span of state.span.spanCycles()) {
|
|
588
|
+
for (const p of choose(span.begin.sam().toNumber())) for (const h of p.query(state.setSpan(span))) out.push(h);
|
|
589
|
+
}
|
|
590
|
+
return out;
|
|
591
|
+
}, steps);
|
|
592
|
+
export function seqPLoop(...entries) {
|
|
593
|
+
const items = entries.filter(Array.isArray).map(([b, e, p]) => [Number(b), Number(e), reify(p)]);
|
|
594
|
+
const L = Math.max(0, ...items.map((x) => x[1]));
|
|
595
|
+
if (!(L > 0)) return silence;
|
|
596
|
+
return perCycle((c) => {
|
|
597
|
+
const m = floorMod(c, L);
|
|
598
|
+
return items.filter(([b, e]) => b <= m && m < e).map((x) => x[2]);
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
export function slowcatPrime(...items) {
|
|
602
|
+
const pats = items.map(reify);
|
|
603
|
+
if (pats.length === 0) return silence;
|
|
604
|
+
return perCycle((c) => [pats[floorMod(c, pats.length)]]);
|
|
605
|
+
}
|
|
606
|
+
registerFree('seqPLoop', seqPLoop);
|
|
607
|
+
registerFree('slowcatPrime', slowcatPrime);
|
|
608
|
+
|
|
609
|
+
// --- morph (spec/functions/morph.md) -----------------------------------------------
|
|
610
|
+
// The k-th onset of `from` moves towards the k-th onset of `to` by `by`.
|
|
611
|
+
function morphBy(from, to, by) {
|
|
612
|
+
const L = from.length;
|
|
613
|
+
const ones = (xs) => xs.flatMap((x, i) => (Number(x) ? [i] : []));
|
|
614
|
+
const a = ones(from);
|
|
615
|
+
const b = ones(to);
|
|
616
|
+
const n = Math.min(a.length, b.length);
|
|
617
|
+
const len = fraction(1).div(L);
|
|
618
|
+
const begins = [];
|
|
619
|
+
for (let k = 0; k < n; k++) begins.push(fraction(a[k]).add(fraction(Number(by)).mul(b[k] - a[k])).div(L));
|
|
620
|
+
return new Pattern((state) => {
|
|
621
|
+
const out = [];
|
|
622
|
+
const { begin, end } = state.span;
|
|
623
|
+
for (let c = begin.sam().toNumber() - 1; c <= end.ceil().toNumber(); c++) {
|
|
624
|
+
for (const s of begins) {
|
|
625
|
+
const whole = new TimeSpan(s.add(c), s.add(c).add(len));
|
|
626
|
+
const part = state.span.clip(whole);
|
|
627
|
+
if (part) out.push(new Hap(whole, part, true));
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return out;
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
export const morph = registerFree('morph', (from, to, by) => {
|
|
634
|
+
if (!Array.isArray(from) || !Array.isArray(to) || from.length === 0) return silence;
|
|
635
|
+
return isPat(by) ? reify(by).fmap((v) => morphBy(from, to, v)).innerJoin() : morphBy(from, to, by);
|
|
636
|
+
});
|