web.mjsannotatedweb.mjssource68 lines · 1.9 KB · raw
1export * from '@strudel/core';
2export * from '@strudel/webaudio';

export * from '@strudel/soundfonts';

4export * from '@strudel/transpiler';
5export * from '@strudel/mini';
6export * from '@strudel/edo';
7export * from '@strudel/tonal';
8export * from '@strudel/webaudio';
9import { Pattern, evalScope, setTime } from '@strudel/core';
10import { initAudioOnFirstClick, registerSynthSounds, webaudioRepl } from '@strudel/webaudio';

import { registerSoundfonts } from '@strudel/soundfonts';

12import { evaluate as _evaluate, transpiler } from '@strudel/transpiler';
13import { miniAllStrings } from '@strudel/mini';

init logic

16export async function defaultPrebake() {
17  const loadModules = evalScope(
18    evalScope,
19    import('@strudel/core'),
20    import('@strudel/mini'),
21    import('@strudel/edo'),
22    import('@strudel/tonal'),
23    import('@strudel/webaudio'),
24    { hush, evaluate },
25  );
26  await Promise.all([loadModules, registerSynthSounds() /* , registerSoundfonts() */]);
27}

when this function finishes, everything is initialized

30let initDone;
32let repl;
33export function initStrudel(options = {}) {
34  initAudioOnFirstClick();
35  options.miniAllStrings !== false && miniAllStrings();
36  const { prebake, ...replOptions } = options;
37  repl = webaudioRepl({ ...replOptions, transpiler });
38  initDone = (async () => {
39    await defaultPrebake();
40    await prebake?.();
41    return repl;
42  })();
43  setTime(() => repl.scheduler.now());
44  return initDone;
45}
46
47window.initStrudel = initStrudel;

this method will play the pattern on the default scheduler

50Pattern.prototype.play = function () {
51  if (!repl) {
52    throw new Error('.play: no repl found. Have you called initStrudel?');
53  }
54  initDone.then(() => {
55    repl.setPattern(this, true);
56  });
57  return this;
58};

stop playback

61export function hush() {
62  repl.stop();
63}

evaluate and play the given code using the transpiler

66export async function evaluate(code, autoplay = true) {
67  return repl.evaluate(code, autoplay);
68}