Skip to content

Commit

Permalink
basic strudel setup
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Dec 30, 2024
1 parent b28b957 commit 03003a8
Show file tree
Hide file tree
Showing 3 changed files with 1,165 additions and 6 deletions.
116 changes: 111 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,109 @@ import { UndoManager } from "yjs";

import "./style.css";

// strudel
import { controls, evalScope, repl, stack, evaluate } from "@strudel/core";
// import { Framer } from "@strudel/draw";
import { registerSoundfonts } from "@strudel/soundfonts";
import { transpiler } from "@strudel/transpiler";
import {
getAudioContext,
initAudio,
registerSynthSounds,
samples,
webaudioOutput,
} from "@strudel/webaudio";

const audioReady = initAudio().then(() => {
console.log("audio ready");
});
const onError = (err) => {
console.error(err);
};

async function loadSamples() {
const ds = "https://raw.githubusercontent.com/felixroos/dough-samples/main/";
return Promise.all([
samples(`${ds}/tidal-drum-machines.json`),
samples(`${ds}/piano.json`),
samples(`${ds}/Dirt-Samples.json`),
samples(`${ds}/EmuSP12.json`),
samples(`${ds}/vcsl.json`),
]);
}

class StrudelSession {
constructor() {
this.init().then(() => {
console.log("strudel init done", this.repl);
});
this.patterns = {};
}
async init() {
await evalScope(
import("@strudel/core"),
import("@strudel/mini"),
import("@strudel/tonal"),
import("@strudel/soundfonts"),
import("@strudel/webaudio"),
controls
);
try {
await Promise.all([
loadSamples(),
registerSynthSounds(),
registerSoundfonts(),
]);
} catch (err) {
onError(err);
}

this.repl = repl({
defaultOutput: webaudioOutput,
afterEval: (options) => {
// assumes docId is injected at end end as a comment
/* const docId = options.code.split("//").slice(-1)[0];
if (!docId) return;
const miniLocations = options.meta?.miniLocations;
updateDocumentsContext(docId, { miniLocations }); */
},
beforeEval: () => {},
onSchedulerError: (e) => onError(`${e}`),
onEvalError: (e) => onError(`${e}`),
getTime: () => getAudioContext().currentTime,
transpiler,
});
}
async eval(msg) {
try {
const { body: code, docId } = msg;
// little hack that injects the docId at the end of the code to make it available in afterEval
//const pattern = await this._repl.evaluate(`${code}//${docId}`);
const { pattern } = await evaluate(
//`${code}//${docId}`,
code,
transpiler
// { id: '?' }
);
if (pattern) {
//this.patterns[docId] = pattern.docId(docId); // docId is needed for highlighting
this.patterns[docId] = pattern; // docId is needed for highlighting
console.log("this.patterns", this.patterns);
const allPatterns = stack(...Object.values(this.patterns));
await this.repl.scheduler.setPattern(allPatterns, true);
}
} catch (err) {
console.error(err);
this._onError(`${err}`);
}
}
}

const strudel = new StrudelSession();

let patterns = {};
//

const flokBasicSetup = (doc) => {
const text = doc.getText();
const undoManager = new UndoManager(text);
Expand Down Expand Up @@ -55,16 +158,19 @@ const createEditor = (doc) => {

const handleMessage = (msg) => {
console.log("message", msg);
try {
const pattern = evaluate(msg.body);
console.log("pattern");
patterns[msg.docId] = pattern;
} catch (err) {
console.error(`eval error: ${err.message}`);
}
};

const handleEvalHydra = (msg) => {
console.log("eval:hydra", msg);
// evaluate hydra code here...
};
const handleEvalStrudel = (msg) => {
console.log("eval:strudel", msg);
// evaluate hydra code here...
};

const { port, protocol } = window.location;
const isSecure = protocol === "https:";
Expand All @@ -79,7 +185,7 @@ window.session = session;
session.on("change", (...args) => console.log("change", ...args));
session.on("message", handleMessage);
session.on("eval:hydra", handleEvalHydra);
session.on("eval:strudel", handleEvalStrudel);
session.on("eval:strudel", (msg) => strudel.eval(msg));

session.on("sync", () => {
// If session is empty, create two documents
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
"y-protocols": "^1.0.5",
"y-webrtc": "^10.2.5",
"y-websocket": "^1.5.0",
"yjs": "^13.5.51"
"yjs": "^13.5.51",
"@strudel/codemirror": "^1.1.0",
"@strudel/core": "^1.1.0",
"@strudel/mini": "^1.1.0",
"@strudel/soundfonts": "^1.1.0",
"@strudel/tonal": "^1.1.0",
"@strudel/transpiler": "^1.1.0",
"@strudel/webaudio": "^1.1.0"
}
}
Loading

0 comments on commit 03003a8

Please sign in to comment.