-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.js
49 lines (38 loc) · 1.57 KB
/
host.js
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
const player = document.querySelector('#player');
const mount = document.querySelector('#mount');
// Safari...
const AudioContext = window.AudioContext // Default
|| window.webkitAudioContext // Safari and old versions of Chrome
|| false;
const audioContext = new AudioContext();
const mediaElementSource = audioContext.createMediaElementSource(player);
// Very simple function to connect the plugin audionode to the host
const connectPlugin = (audioNode) => {
mediaElementSource.connect(audioNode);
audioNode.connect(audioContext.destination);
};
// Very simple function to append the plugin root dom node to the host
const mountPlugin = (domNode) => {
mount.innerHtml = '';
mount.appendChild(domNode);
};
(async () => {
// Init WamEnv
const { default: initializeWamHost } = await import("./utils/sdk/src/initializeWamHost.js");
const [hostGroupId] = await initializeWamHost(audioContext);
// Import WAM
const { default: WAM } = await import('./index.js');
// Create a new instance of the plugin
// You can can optionnally give more options such as the initial state of the plugin
const instance = await WAM.createInstance(hostGroupId, audioContext);
window.instance = instance;
// Connect the audionode to the host
connectPlugin(instance.audioNode);
// Load the GUI if need (ie. if the option noGui was set to true)
// And calls the method createElement of the Gui module
const pluginDomNode = await instance.createGui();
mountPlugin(pluginDomNode);
player.onplay = () => {
audioContext.resume(); // audio context must be resumed because browser restrictions
};
})();