-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrudelWeb.js
More file actions
74 lines (66 loc) · 1.99 KB
/
Copy pathstrudelWeb.js
File metadata and controls
74 lines (66 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// modified @strudel/web component to export the repl directly
export * from '@strudel/core';
export * from '@strudel/webaudio';
export * from '@strudel/transpiler';
export * from '@strudel/mini';
export * from '@strudel/tonal';
export * from '@strudel/webaudio';
import { Pattern, evalScope, setTime } from '@strudel/core';
import { initAudioOnFirstClick, registerSynthSounds, webaudioRepl } from '@strudel/webaudio';
import { evaluate as _evaluate, transpiler } from '@strudel/transpiler';
import { miniAllStrings } from '@strudel/mini';
// init logic
export async function defaultPrebake() {
const loadModules = evalScope(
evalScope,
import('@strudel/core'),
import('@strudel/mini'),
import('@strudel/tonal'),
import('@strudel/webaudio'),
import('@strudel/hydra'),
{ hush, evaluate },
);
await Promise.all([loadModules, registerSynthSounds() /* , registerSoundfonts() */]);
}
// when this function finishes, everything is initialized
let initDone;
let repl;
export function initStrudel(options = {}) {
initAudioOnFirstClick();
options.miniAllStrings !== false && miniAllStrings();
const { prebake, ...replOptions } = options;
repl = webaudioRepl({ ...replOptions, transpiler });
initDone = (async () => {
await defaultPrebake();
await prebake?.();
return repl;
})();
setTime(() => repl.scheduler.now());
return initDone;
}
window.initStrudel = initStrudel;
// this method will play the pattern on the default scheduler
Pattern.prototype.play = function() {
if (!repl) {
throw new Error('.play: no repl found. Have you called initStrudel?');
}
initDone.then(() => {
repl.setPattern(this, true);
});
return this;
};
// stop playback
export function hush() {
repl.stop();
}
export function getRepl() {
if (!repl) {
throw new Error('repl: no repl found. Have you called initStrudel?');
}
console.log("getRepl", repl);
return repl;
}
// evaluate and play the given code using the transpiler
export async function evaluate(code, autoplay = true) {
return repl.evaluate(code, autoplay);
}