Debian Codegraph @

wasm/browser/src/mains/old-spn.main.js

raw
1 import * as Comlink from "comlink/dist/esm/comlink.mjs";
2 import { logOldSpnMain as log } from "../logger";
3 import { WebkitAudioContext } from "../utils";
4 import { requestMidi } from "../utils/request-midi";
5 import { requestMicrophoneNode } from "./io.utils";
6
7 const ScriptProcessorNodeWorker = goog.require("worker.old_spn");
8
9 // we reuse the spnWorker
10 // since it handles multiple
11 // audio Contexts via UID.
12 let spnWorker;
13 let proxyPort;
14
15 let UID = 0;
16
17 class ScriptProcessorNodeMainThread {
18 constructor({ audioContext, audioContextIsProvided, autoConnect }) {
19 this.autoConnect = autoConnect;
20 this.audioContextIsProvided = audioContextIsProvided;
21
22 this.audioContext = audioContext;
23 this.currentPlayState = undefined;
24 this.csoundWorkerMain = undefined;
25
26 // never default these, get it from
27 // csound-worker before starting
28 this.sampleRate = undefined;
29 this.inputsCount = undefined;
30 this.outputsCount = undefined;
31 this.hardwareBufferSize = undefined;
32 this.softwareBufferSize = undefined;
33
34 this.scriptProcessorNode = true;
35 log("ScriptProcessorNodeMainThread was constructed")();
36 }
37
38 async terminateInstance() {
39 if (window[`__csound_wasm_iframe_parent_${this.contextUid}Node`]) {
40 window[`__csound_wasm_iframe_parent_${this.contextUid}Node`].disconnect();
41 delete window[`__csound_wasm_iframe_parent_${this.contextUid}Node`];
42 }
43 if (this.audioContext) {
44 if (this.audioContext.state !== "closed") {
45 try {
46 await this.audioContext.close();
47 } catch (error) {}
48 }
49 delete this.audioContext;
50 }
51 if (proxyPort) {
52 proxyPort[Comlink.releaseProxy]();
53 proxyPort = undefined;
54 }
55 if (this.iFrameElement) {
56 this.iFrameElement.remove();
57 }
58 spnWorker = undefined;
59 UID = 0;
60 }
61
62 async onPlayStateChange(newPlayState) {
63 if (this.currentPlayState === newPlayState) {
64 return;
65 }
66
67 switch (newPlayState) {
68 case "realtimePerformanceStarted": {
69 log("event received: realtimePerformanceStarted")();
70 this.currentPlayState = newPlayState;
71 await this.initialize();
72 await this.csoundWorkerMain.eventPromises.releaseStartPromise();
73 this.publicEvents.triggerRealtimePerformanceStarted(this.csoundWorkerMain);
74 break;
75 }
76 case "realtimePerformanceEnded": {
77 log("event received: realtimePerformanceEnded")();
78 if (window[`__csound_wasm_iframe_parent_${this.contextUid}Node`]) {
79 window[`__csound_wasm_iframe_parent_${this.contextUid}Node`].disconnect();
80 delete window[`__csound_wasm_iframe_parent_${this.contextUid}Node`];
81 }
82 break;
83 }
84
85 case "realtimePerformancePaused": {
86 if (this.csoundWorkerMain && this.csoundWorkerMain.eventPromises) {
87 this.csoundWorkerMain.publicEvents &&
88 this.csoundWorkerMain.publicEvents.triggerRealtimePerformancePaused(this);
89 await this.csoundWorkerMain.eventPromises.releasePausePromise();
90 }
91 break;
92 }
93
94 case "realtimePerformanceResumed": {
95 if (this.csoundWorkerMain && this.csoundWorkerMain.eventPromises) {
96 this.csoundWorkerMain.publicEvents &&
97 this.csoundWorkerMain.publicEvents.triggerRealtimePerformanceResumed(this);
98 await this.csoundWorkerMain.eventPromises.releaseResumePromise();
99 }
100 break;
101 }
102
103 default: {
104 break;
105 }
106 }
107 this.currentPlayState = newPlayState;
108 if (
109 proxyPort &&
110 (newPlayState !== "realtimePerformanceStarted" || newPlayState !== "renderStarted")
111 ) {
112 await proxyPort.setPlayState({
113 contextUid: this.contextUid,
114 newPlayState,
115 });
116 }
117 }
118
119 async initIframe() {
120 // HACK FROM (but it works just fine when adding modern security models)
121 // https://github.com/GoogleChromeLabs/audioworklet-polyfill/blob/274792e5e3d189e04c9496bed24129118539b4b5/src/realm.js#L18-L20
122 if (typeof window === "undefined" || typeof window.document === "undefined") {
123 throw new TypeError("Can only run SPN in Browser scope");
124 }
125
126 const parentScope = window.document;
127 // eslint-disable-next-line unicorn/prevent-abbreviations
128 const iFrameHtml = [
129 `<!doctype html>`,
130 `<html lang="en">`,
131 `<head>`,
132 `<meta charset="UTF-8">`,
133 `</head>`,
134 `<body>`,
135 `<script type="text/javascript" src="${ScriptProcessorNodeWorker()}"></script>`,
136 `</body>`,
137 ].join("\n");
138
139 // eslint-disable-next-line unicorn/prevent-abbreviations
140 const iFrameBlob = new Blob([iFrameHtml], { type: "text/html" });
141 this.iFrameElement = document.createElement("iframe");
142
143 this.iFrameElement.src = URL.createObjectURL(iFrameBlob);
144 this.iFrameElement.sandbox.add("allow-scripts", "allow-same-origin");
145
146 this.iFrameElement.style.cssText = "position:absolute;left:0;top:-999px;width:1px;height:1px;";
147
148 // appending early to have access to contentWindow
149 // eslint-disable-next-line unicorn/prevent-abbreviations
150 const iFrameOnLoad = new Promise((resolve) => {
151 // eslint-disable-next-line unicorn/prefer-add-event-listener
152 this.iFrameElement.onload = () => {
153 resolve();
154 };
155 });
156
157 parentScope.body.append(this.iFrameElement);
158
159 try {
160 await iFrameOnLoad;
161 } catch (error) {
162 console.error(error);
163 }
164
165 spnWorker = this.iFrameElement.contentWindow;
166 }
167
168 async initialize() {
169 if (!spnWorker) {
170 await this.initIframe();
171 if (!spnWorker) {
172 console.error("SPN FATAL: Couldn't create iFrame");
173 return;
174 }
175 }
176 const contextUid = `audioWorklet${UID}`;
177 this.contextUid = contextUid;
178 UID += 1;
179
180 if (!proxyPort) {
181 proxyPort = Comlink.wrap(Comlink.windowEndpoint(spnWorker));
182 }
183
184 if (!this.audioContext) {
185 if (this.audioContextIsProvided) {
186 console.error(`fatal: the provided AudioContext was undefined`);
187 }
188 this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate });
189 }
190 if (this.audioContext.state === "closed") {
191 if (this.audioContextIsProvided) {
192 console.error(`fatal: the provided AudioContext was closed, falling back new AudioContext`);
193 }
194 this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate });
195 }
196
197 if (!this.audioContextIsProvided && this.sampleRate !== this.audioContext.sampleRate) {
198 this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate });
199 }
200
201 // just set it both on parent and iframe
202 // since 1 works on linux and other one on mac
203 // leaking globals indeed
204 spnWorker[contextUid] = this.audioContext;
205 window[`__csound_wasm_iframe_parent_${contextUid}`] = this.audioContext;
206
207 let liveInput;
208 if (this.isRequestingInput) {
209 await new Promise((resolve) => {
210 const microphoneCallback = (stream) => {
211 if (stream) {
212 liveInput = this.audioContext.createMediaStreamSource(stream);
213 }
214 resolve();
215 };
216 requestMicrophoneNode(microphoneCallback);
217 });
218 }
219
220 log("initializing proxyPort")();
221
222 await proxyPort.initialize(
223 Comlink.transfer(
224 {
225 contextUid,
226 hardwareBufferSize: 32768,
227 softwareBufferSize: 2048,
228 inputsCount: this.inputsCount,
229 outputsCount: this.outputsCount,
230 sampleRate: this.sampleRate,
231 audioInputPort: this.ipcMessagePorts.audioWorkerAudioInputPort,
232 messagePort: this.ipcMessagePorts.workerMessagePort2,
233 requestPort: this.ipcMessagePorts.audioWorkerFrameRequestPort,
234 audioContextIsProvided: this.audioContextIsProvided,
235 autoConnect: this.autoConnect,
236 initialPlayState: this.currentPlayState,
237 },
238 [
239 this.ipcMessagePorts.audioWorkerAudioInputPort,
240 this.ipcMessagePorts.workerMessagePort2,
241 this.ipcMessagePorts.audioWorkerFrameRequestPort,
242 ],
243 ),
244 );
245 log("done initializing proxyPort")();
246
247 const audioNode =
248 spnWorker[`${contextUid}Node`] || window[`__csound_wasm_iframe_parent_${contextUid}Node`];
249 audioNode && liveInput && liveInput.connect(audioNode);
250
251 this.publicEvents.triggerOnAudioNodeCreated(audioNode);
252
253 if (this.isRequestingMidi && this.csoundWorkerMain && this.csoundWorkerMain.handleMidiInput) {
254 log("requesting for web-midi connection")();
255 requestMidi({
256 onMidiMessage: this.csoundWorkerMain.handleMidiInput.bind(this.csoundWorkerMain),
257 });
258 }
259 }
260 }
261
262 export default ScriptProcessorNodeMainThread;