Debian Codegraph @

wasm/browser/src/mains/worklet.singlethread.main.js

raw
1 /*
2 worklet.singlethread.js
3
4 Copyright (C) 2018 Steven Yi, Victor Lazzarini
5
6 This file is part of Csound.
7
8 The Csound Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 Csound is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with Csound; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 02110-1301 USA
22 */
23
24 import * as Comlink from "comlink/dist/esm/comlink.mjs";
25 import { logSinglethreadWorkletMain as log } from "../logger.js";
26 import { csoundApiRename, fetchPlugins, makeProxyCallback } from "../utils.js";
27 import { messageEventHandler, IPCMessagePorts } from "./messages.main.js";
28 import { api as API } from "../libcsound.js";
29 import { PublicEventAPI } from "../events.js";
30 import { enableAudioInput } from "./io.utils.js";
31 import { requestMidi } from "../utils/request-midi.js";
32 import { EventPromises } from "../utils/event-promises.js";
33
34 const WorkletWorker = goog.require("worklet.singlethread.worker");
35
36 const initializeModule = async (audioContext) => {
37 log("Initialize Module")();
38 try {
39 await audioContext.audioWorklet.addModule(WorkletWorker());
40 } catch (error) {
41 console.error("Error calling audioWorklet.addModule", error);
42 return false;
43 }
44 return true;
45 };
46
47 class SingleThreadAudioWorkletMainThread {
48 constructor({ audioContext, inputChannelCount = 1, outputChannelCount = 2 }) {
49 this.exportApi = {};
50 this.ipcMessagePorts = new IPCMessagePorts();
51 this.publicEvents = new PublicEventAPI(this);
52 this.eventPromises = new EventPromises();
53
54 this.audioContext = audioContext;
55 this.inputChannelCount = inputChannelCount;
56 this.outputChannelCount = outputChannelCount;
57
58 this.messageCallbacks = [];
59 this.onPlayStateChange = this.onPlayStateChange.bind(this);
60 this.currentPlayState = undefined;
61 }
62
63 async terminateInstance() {
64 if (this.node) {
65 this.node.disconnect();
66 delete this.node;
67 }
68 if (this.audioContext) {
69 if (this.audioContext.state !== "closed") {
70 await this.audioContext.close();
71 }
72 delete this.audioContext;
73 }
74 if (this.workletProxy) {
75 this.workletProxy[Comlink.releaseProxy]();
76 delete this.workletProxy;
77 }
78 if (this.publicEvents) {
79 this.publicEvents.terminateInstance();
80 delete this.publicEvents;
81 }
82 Object.keys(this.exportApi).forEach((key) => delete this.exportApi[key]);
83 Object.keys(this).forEach((key) => delete this[key]);
84 }
85
86 async onPlayStateChange(newPlayState) {
87 if (this.currentPlayState === newPlayState || !this.publicEvents) {
88 return;
89 }
90
91 this.currentPlayState = newPlayState;
92
93 switch (newPlayState) {
94 case "realtimePerformanceStarted": {
95 if (this.eventPromises.isWaitingToStart()) {
96 log("Start promise resolved")();
97 this.eventPromises.releaseStartPromise();
98 }
99 this.publicEvents.triggerRealtimePerformanceStarted(this);
100 break;
101 }
102
103 case "realtimePerformanceEnded": {
104 this.midiPortStarted = false;
105 this.currentPlayState = undefined;
106 this.publicEvents && this.publicEvents.triggerRealtimePerformanceEnded(this);
107 this.eventPromises &&
108 this.eventPromises.isWaitingToStop() &&
109 this.eventPromises.releaseStopPromise();
110 // just to be double sure that there's no hanging promise
111 this.eventPromises && this.eventPromises.releaseStartPromise();
112 break;
113 }
114 case "realtimePerformancePaused": {
115 this.publicEvents.triggerRealtimePerformancePaused(this);
116 break;
117 }
118 case "realtimePerformanceResumed": {
119 this.publicEvents.triggerRealtimePerformanceResumed(this);
120 break;
121 }
122 case "renderStarted": {
123 if (this.eventPromises.isWaitingToStart()) {
124 log("Start promise resolved")();
125 this.eventPromises.releaseStartPromise();
126 }
127 this.publicEvents.triggerRenderStarted(this);
128 break;
129 }
130 case "renderEnded": {
131 this.publicEvents.triggerRenderEnded(this);
132 break;
133 }
134
135 default: {
136 break;
137 }
138 }
139 }
140
141 async csoundPause() {
142 if (typeof this.workletProxy !== "undefined") {
143 await this.workletProxy.pause();
144 }
145 }
146
147 async csoundResume() {
148 if (typeof this.workletProxy !== "undefined") {
149 await this.workletProxy.resume();
150 }
151 }
152
153 handleMidiInput({ data: payload }) {
154 this.ipcMessagePorts.csoundMainRtMidiPort.postMessage &&
155 this.ipcMessagePorts.csoundMainRtMidiPort.postMessage(payload);
156 }
157
158 async initialize({ wasmDataURI, withPlugins, autoConnect }) {
159 if (withPlugins && withPlugins.length > 0) {
160 withPlugins = await fetchPlugins(withPlugins);
161 }
162
163 await initializeModule(this.audioContext);
164
165 this.node = new AudioWorkletNode(this.audioContext, "csound-singlethread-worklet-processor", {
166 inputChannelCount: this.inputChannelCount ? [this.inputChannelCount] : 0,
167 outputChannelCount: [this.outputChannelCount || 2],
168 });
169
170 if (autoConnect) {
171 this.node.connect(this.audioContext.destination);
172 }
173
174 try {
175 log("wrapping Comlink proxy endpoint on the audioWorkletNode.port")();
176 this.workletProxy = Comlink.wrap(this.node.port);
177 } catch (error) {
178 console.error("COMLINK ERROR", error);
179 }
180
181 await this.workletProxy.initializeMessagePort(
182 Comlink.transfer(
183 {
184 messagePort: this.ipcMessagePorts.workerMessagePort,
185 rtmidiPort: this.ipcMessagePorts.csoundWorkerRtMidiPort,
186 },
187 [this.ipcMessagePorts.workerMessagePort, this.ipcMessagePorts.csoundWorkerRtMidiPort],
188 ),
189 );
190 this.ipcMessagePorts.mainMessagePort.addEventListener("message", messageEventHandler(this));
191 this.ipcMessagePorts.mainMessagePort.start();
192
193 await this.workletProxy.initialize(wasmDataURI(), withPlugins);
194 const csoundInstance = await makeProxyCallback(
195 this.workletProxy,
196 undefined,
197 "csoundCreate",
198 this.currentPlayState,
199 )();
200 this.csoundInstance = csoundInstance;
201 await makeProxyCallback(
202 this.workletProxy,
203 csoundInstance,
204 "csoundInitialize",
205 this.currentPlayState,
206 )(0);
207
208 this.exportApi.pause = this.csoundPause.bind(this);
209 this.exportApi.resume = this.csoundResume.bind(this);
210 this.exportApi.terminateInstance = this.terminateInstance.bind(this);
211
212 this.exportApi.getAudioContext = async () => this.audioContext;
213 this.exportApi.getNode = async () => this.node;
214 this.exportApi.enableAudioInput = enableAudioInput.bind(this.exportApi);
215
216 this.exportApi.name = "Csound: Audio Worklet, Single-threaded";
217 this.exportApi = this.publicEvents.decorateAPI(this.exportApi);
218 // the default message listener
219 this.exportApi.addListener("message", console.log);
220
221 for (const apiK of Object.keys(API)) {
222 const reference = API[apiK];
223 const proxyCallback = makeProxyCallback(
224 this.workletProxy,
225 csoundInstance,
226 apiK,
227 this.currentPlayState,
228 );
229 switch (apiK) {
230 case "csoundCreate": {
231 break;
232 }
233
234 case "csoundStart": {
235 const csoundStart = async function () {
236 this.eventPromises.createStartPromise();
237 const isRequestingInput = await this.workletProxy.isRequestingInput();
238 if (isRequestingInput) {
239 this.exportApi.enableAudioInput();
240 }
241
242 const startResult = await proxyCallback({ csound: csoundInstance });
243 const isRequestingMidi = await this.exportApi._isRequestingRtMidiInput(csoundInstance);
244 if (isRequestingMidi) {
245 requestMidi({
246 onMidiMessage: this.handleMidiInput.bind(this),
247 });
248 }
249 this.publicEvents.triggerOnAudioNodeCreated(this.node);
250 await this.eventPromises.waitForStart();
251 return startResult;
252 };
253
254 csoundStart.toString = () => reference.toString();
255 this.exportApi.start = csoundStart.bind(this);
256 break;
257 }
258 case "csoundStop": {
259 const csoundStop = async () => {
260 if (this.eventPromises.isWaitingToStop()) {
261 log("already waiting to stop, doing nothing")();
262 return -1;
263 } else {
264 this.eventPromises.createStopPromise();
265 const stopResult = await proxyCallback();
266 await this.eventPromises.waitForStop();
267 return stopResult;
268 }
269 };
270 csoundStop.toString = () => reference.toString();
271 this.exportApi.stop = csoundStop.bind(this);
272 break;
273 }
274
275 case "fs": {
276 this.exportApi.fs = {};
277 Object.keys(reference).forEach((method) => {
278 const proxyFsCallback = makeProxyCallback(
279 this.workletProxy,
280 csoundInstance,
281 method,
282 this.currentPlayState,
283 );
284 proxyFsCallback.toString = () => reference[method].toString();
285 this.exportApi.fs[method] = proxyFsCallback;
286 });
287 break;
288 }
289
290 default: {
291 proxyCallback.toString = () => reference.toString();
292 this.exportApi[csoundApiRename(apiK)] = proxyCallback;
293 break;
294 }
295 }
296 }
297
298 return this.exportApi;
299 }
300 }
301
302 export default SingleThreadAudioWorkletMainThread;