Skip to content

Commit

Permalink
experimental pattern support in kabelsalat
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Jan 16, 2025
1 parent e80c1cc commit 5b8229c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
28 changes: 28 additions & 0 deletions kabelsalat.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,35 @@
// console.log('eval', event.data.msg);
const { body: code, docId } = event.data.msg;
try {
let patterns = [];
window.P = (pattern) => {
if (!window.parent.strudel) return;
const valueID = `minivalue-${patterns.length}`;
const gateID = `minigate-${patterns.length}`;
const reified = window.parent.strudelWindow.m(pattern, 7).onTrigger((_, hap, ct, cps, t) => {
const onset = t - ct;
const offset = onset + hap.duration / cps - 0.05;
parent.kabelsalat.audio.setControls([
{ id: gateID, time: onset, value: 1 },
{ id: gateID, time: offset, value: 0 },
{ id: valueID, time: onset, value: hap.value.value },
]);
});
patterns.push(reified);
const value = cc(valueID),
gate = cc(gateID);
return { value, gate, v: value, g: gate };
};

kabelsalat.run(code);

if (window.parent.strudelWindow && patterns.length) {
const miniLocations = [[8, 15]];
window.parent.strudel.onUpdateMiniLocations(docId, miniLocations);
const allPatterns = window.parent.strudelWindow.stack(...patterns);
console.log(patterns);
window.parent.strudel.setDocPattern(docId, allPatterns);
}
} catch (err) {
send('onError', [`kabelsalat error: ${err.message}`, docId]);
}
Expand Down
13 changes: 8 additions & 5 deletions src/strudel.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export class StrudelSession {
});
}

async setDocPattern(docId, pattern) {
this.patterns[docId] = pattern.docId(docId); // docId is needed for highlighting
//console.log("this.patterns", this.patterns);
const allPatterns = stack(...Object.values(this.patterns));
await this.scheduler.setPattern(allPatterns, true);
}

async eval(msg, conversational = false) {
const { body: code, docId } = msg;
try {
Expand Down Expand Up @@ -199,11 +206,7 @@ export class StrudelSession {
return;
}
//console.log("evaluated patterns", this.pPatterns);
this.patterns[docId] = pattern.docId(docId); // docId is needed for highlighting
//console.log("this.patterns", this.patterns);
const allPatterns = stack(...Object.values(this.patterns));

await this.scheduler.setPattern(allPatterns, true);
await this.setDocPattern(docId, pattern);

//console.log("afterEval", meta);
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions strudel.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
});

window.parent.strudel = strudel;
window.parent.strudelWindow = window;
console.log('[strudel] waiting for document click to init');
window.parent.document.addEventListener('click', async function interaction() {
window.parent.document.removeEventListener('click', interaction);
Expand Down

0 comments on commit 5b8229c

Please sign in to comment.