fix: use full repl in web package

This commit is contained in:
Felix Roos
2024-05-30 14:36:28 +02:00
parent 65856409b6
commit 3cd31b2290
3 changed files with 17 additions and 23 deletions
+13 -15
View File
@@ -6,9 +6,9 @@ export * from '@strudel/mini';
export * from '@strudel/tonal';
export * from '@strudel/webaudio';
import { Pattern, evalScope, setTime } from '@strudel/core';
import { initAudioOnFirstClick, registerSynthSounds, webaudioScheduler } from '@strudel/webaudio';
import { initAudioOnFirstClick, registerSynthSounds, webaudioRepl } from '@strudel/webaudio';
// import { registerSoundfonts } from '@strudel/soundfonts';
import { evaluate as _evaluate } from '@strudel/transpiler';
import { evaluate as _evaluate, transpiler } from '@strudel/transpiler';
import { miniAllStrings } from '@strudel/mini';
// init logic
@@ -27,19 +27,18 @@ export async function defaultPrebake() {
// when this function finishes, everything is initialized
let initDone;
let scheduler;
let repl;
export function initStrudel(options = {}) {
initAudioOnFirstClick();
miniAllStrings();
const { prebake, ...schedulerOptions } = options;
scheduler = webaudioScheduler(schedulerOptions);
options.miniAllStrings !== false && miniAllStrings();
const { prebake, ...replOptions } = options;
repl = webaudioRepl({ ...replOptions, transpiler });
initDone = (async () => {
await defaultPrebake();
await prebake?.();
return scheduler;
return repl;
})();
setTime(() => scheduler.now());
setTime(() => repl.scheduler.now());
return initDone;
}
@@ -47,22 +46,21 @@ window.initStrudel = initStrudel;
// this method will play the pattern on the default scheduler
Pattern.prototype.play = function () {
if (!scheduler) {
throw new Error('.play: no scheduler found. Have you called init?');
if (!repl) {
throw new Error('.play: no repl found. Have you called initStrudel?');
}
initDone.then(() => {
scheduler.setPattern(this, true);
repl.setPattern(this, true);
});
return this;
};
// stop playback
export function hush() {
scheduler.stop();
repl.stop();
}
// evaluate and play the given code using the transpiler
export async function evaluate(code, autoplay = true) {
const { pattern } = await _evaluate(code);
autoplay && pattern.play();
return repl.evaluate(code, autoplay);
}