Debian Codegraph @

wasm/browser/src/mains/sab.main.js

raw
1 import * as Comlink from "comlink/dist/esm/comlink.mjs";
2 import { api as API } from "../libcsound";
3 import { messageEventHandler, IPCMessagePorts } from "./messages.main";
4 import {
5 AUDIO_STATE,
6 MAX_CHANNELS,
7 RING_BUFFER_SIZE,
8 MIDI_BUFFER_PAYLOAD_SIZE,
9 MIDI_BUFFER_SIZE,
10 initialSharedState,
11 } from "../constants";
12 import { logSABMain as log } from "../logger";
13 import { isEmpty } from "rambda/dist/rambda.esm.js";
14 import { csoundApiRename, fetchPlugins, makeProxyCallback, stopableStates } from "../utils";
15 import { EventPromises } from "../utils/event-promises";
16 import { PublicEventAPI } from "../events";
17
18 const SABWorker = goog.require("worker.sab");
19
20 class SharedArrayBufferMainThread {
21 constructor({
22 audioContext,
23 audioWorker,
24 audioContextIsProvided,
25 inputChannelCount,
26 outputChannelCount,
27 }) {
28 this.hasSharedArrayBuffer = true;
29 this.ipcMessagePorts = new IPCMessagePorts();
30 this.eventPromises = new EventPromises();
31 this.publicEvents = new PublicEventAPI(this);
32 audioWorker.ipcMessagePorts = this.ipcMessagePorts;
33
34 this.audioContextIsProvided = audioContextIsProvided;
35 this.audioWorker = audioWorker;
36 this.audioWorker.onPlayStateChange = this.audioWorker.onPlayStateChange.bind(audioWorker);
37 this.csoundInstance = undefined;
38 this.currentPlayState = undefined;
39 this.currentDerivedPlayState = "stop";
40 this.exportApi = {};
41
42 this.callbackId = 0;
43 this.callbackBuffer = {};
44
45 this.audioStateBuffer = new SharedArrayBuffer(
46 initialSharedState.length * Int32Array.BYTES_PER_ELEMENT,
47 );
48
49 this.audioStatePointer = new Int32Array(this.audioStateBuffer);
50
51 if (audioContextIsProvided) {
52 Atomics.store(this.audioStatePointer, AUDIO_STATE.SAMPLE_RATE, audioContext.sampleRate);
53 }
54
55 if (inputChannelCount) {
56 Atomics.store(this.audioStatePointer, AUDIO_STATE.NCHNLS_I, inputChannelCount);
57 }
58
59 if (outputChannelCount) {
60 Atomics.store(this.audioStatePointer, AUDIO_STATE.NCHNLS, outputChannelCount);
61 }
62
63 this.audioStreamIn = new SharedArrayBuffer(
64 MAX_CHANNELS * RING_BUFFER_SIZE * Float64Array.BYTES_PER_ELEMENT,
65 );
66 this.audioStreamOut = new SharedArrayBuffer(
67 MAX_CHANNELS * RING_BUFFER_SIZE * Float64Array.BYTES_PER_ELEMENT,
68 );
69
70 this.midiBufferSAB = new SharedArrayBuffer(
71 MIDI_BUFFER_SIZE * MIDI_BUFFER_PAYLOAD_SIZE * Int32Array.BYTES_PER_ELEMENT,
72 );
73
74 this.midiBuffer = new Int32Array(this.midiBufferSAB);
75
76 this.onPlayStateChange = this.onPlayStateChange.bind(this);
77 this.prepareRealtimePerformance = this.prepareRealtimePerformance.bind(this);
78
79 log(`SharedArrayBufferMainThread got constructed`)();
80 }
81
82 async terminateInstance() {
83 if (this.csoundWorker) {
84 this.csoundWorker.terminate();
85 delete this.csoundWorker;
86 }
87 if (this.audioWorker && this.audioWorker.terminateInstance) {
88 await this.audioWorker.terminateInstance();
89 delete this.audioWorker.terminateInstance;
90 }
91 if (this.proxyPort) {
92 this.proxyPort[Comlink.releaseProxy]();
93 delete this.proxyPort;
94 }
95 if (this.publicEvents) {
96 this.publicEvents.terminateInstance();
97 }
98 Object.keys(this.exportApi).forEach((key) => delete this.exportApi[key]);
99 Object.keys(this).forEach((key) => delete this[key]);
100 }
101
102 get api() {
103 return this.exportApi;
104 }
105
106 handleMidiInput({ data: [status, data1, data2] }) {
107 const currentQueueLength = Atomics.load(
108 this.audioStatePointer,
109 AUDIO_STATE.AVAIL_RTMIDI_EVENTS,
110 );
111 const rtmidiBufferIndex = Atomics.load(this.audioStatePointer, AUDIO_STATE.RTMIDI_INDEX);
112 const nextIndex =
113 (currentQueueLength * MIDI_BUFFER_PAYLOAD_SIZE + rtmidiBufferIndex) % MIDI_BUFFER_SIZE;
114
115 Atomics.store(this.midiBuffer, nextIndex, status);
116 Atomics.store(this.midiBuffer, nextIndex + 1, data1);
117 Atomics.store(this.midiBuffer, nextIndex + 2, data2);
118 Atomics.add(this.audioStatePointer, AUDIO_STATE.AVAIL_RTMIDI_EVENTS, 1);
119 }
120
121 async csoundPause() {
122 if (this.eventPromises.isWaiting("pause")) {
123 return -1;
124 } else {
125 this.eventPromises.createPausePromise();
126
127 Atomics.store(this.audioStatePointer, AUDIO_STATE.IS_PAUSED, 1);
128 await this.eventPromises.waitForPause();
129 this.onPlayStateChange("realtimePerformancePaused");
130 return 0;
131 }
132 }
133
134 async csoundResume() {
135 if (
136 Atomics.load(this.audioStatePointer, AUDIO_STATE.IS_PAUSED) === 1 &&
137 Atomics.load(this.audioStatePointer, AUDIO_STATE.STOP) !== 1 &&
138 Atomics.load(this.audioStatePointer, AUDIO_STATE.IS_PERFORMING) === 1
139 ) {
140 Atomics.store(this.audioStatePointer, AUDIO_STATE.IS_PAUSED, 0);
141 Atomics.notify(this.audioStatePointer, AUDIO_STATE.IS_PAUSED);
142 this.onPlayStateChange("realtimePerformanceResumed");
143 }
144 }
145
146 async onPlayStateChange(newPlayState) {
147 if (typeof this === "undefined") {
148 console.log("Failed to announce playstatechange", newPlayState);
149 return;
150 }
151 this.currentPlayState = newPlayState;
152 if (!this.publicEvents || !newPlayState) {
153 // prevent late timers from calling terminated fn
154 return;
155 }
156 switch (newPlayState) {
157 case "realtimePerformanceStarted": {
158 log(
159 `event: realtimePerformanceStarted received,` +
160 ` proceeding to call prepareRealtimePerformance`,
161 )();
162 try {
163 await this.prepareRealtimePerformance();
164 } catch (error) {
165 console.error(error);
166 }
167 break;
168 }
169 case "realtimePerformanceEnded": {
170 this.eventPromises.createStopPromise();
171
172 // flush out events sent during the time which the worker was stopping
173 Object.values(this.callbackBuffer).forEach(({ argumentz, apiKey, resolveCallback }) =>
174 this.proxyPort.callUncloned(apiKey, argumentz).then(resolveCallback),
175 );
176 this.callbackBuffer = {};
177 log(`event: realtimePerformanceEnded received, beginning cleanup`)();
178 // re-initialize SAB
179 initialSharedState.forEach((value, index) => {
180 Atomics.store(this.audioStatePointer, index, value);
181 });
182 break;
183 }
184 case "renderStarted": {
185 this.publicEvents.triggerRenderStarted(this);
186 this.eventPromises.releaseStartPromise();
187 break;
188 }
189 case "renderEnded": {
190 log(`event: renderEnded received, beginning cleanup`)();
191 this.publicEvents.triggerRenderEnded(this);
192 this.eventPromises && this.eventPromises.releaseStopPromise();
193 break;
194 }
195 default: {
196 break;
197 }
198 }
199
200 // forward the message from worker to the audioWorker
201 try {
202 await this.audioWorker.onPlayStateChange(newPlayState);
203 } catch (error) {
204 console.error(error);
205 }
206 }
207
208 async prepareRealtimePerformance() {
209 log(`prepareRealtimePerformance`)();
210 const outputsCount = Atomics.load(this.audioStatePointer, AUDIO_STATE.NCHNLS);
211 const inputCount = Atomics.load(this.audioStatePointer, AUDIO_STATE.NCHNLS_I);
212
213 this.audioWorker.isRequestingInput = Atomics.load(
214 this.audioStatePointer,
215 AUDIO_STATE.IS_REQUESTING_MIC,
216 );
217 this.audioWorker.isRequestingMidi = Atomics.load(
218 this.audioStatePointer,
219 AUDIO_STATE.IS_REQUESTING_RTMIDI,
220 );
221
222 const ksmps = Atomics.load(this.audioStatePointer, AUDIO_STATE.KSMPS);
223 const sampleRate = Atomics.load(this.audioStatePointer, AUDIO_STATE.SAMPLE_RATE);
224
225 this.audioWorker.ksmps = ksmps;
226 this.audioWorker.sampleRate = sampleRate;
227 this.audioWorker.inputCount = inputCount;
228 this.audioWorker.outputsCount = outputsCount;
229 }
230
231 async initialize({ wasmDataURI, withPlugins }) {
232 if (withPlugins && !isEmpty(withPlugins)) {
233 withPlugins = await fetchPlugins(withPlugins);
234 }
235
236 log(`initialization: instantiate the SABWorker Thread`)();
237 // clearFsLastmods();
238 const csoundWorker = new Worker(SABWorker());
239 this.csoundWorker = csoundWorker;
240 const audioStateBuffer = this.audioStateBuffer;
241 const audioStatePointer = this.audioStatePointer;
242 const audioStreamIn = this.audioStreamIn;
243 const audioStreamOut = this.audioStreamOut;
244 const midiBuffer = this.midiBuffer;
245
246 log(`providing the audioWorker a pointer to SABMain's instance`)();
247 this.audioWorker.csoundWorkerMain = this;
248
249 // both audio worker and csound worker use 1 handler
250 // simplifies flow of data (csound main.worker is always first to receive)
251 log(`adding message eventListeners for mainMessagePort and mainMessagePortAudio`)();
252 this.ipcMessagePorts.mainMessagePort.addEventListener("message", messageEventHandler(this));
253 this.ipcMessagePorts.mainMessagePort.start();
254 this.ipcMessagePorts.mainMessagePortAudio.addEventListener(
255 "message",
256 messageEventHandler(this),
257 );
258 this.ipcMessagePorts.mainMessagePortAudio.start();
259 log(`(postMessage) making a message channel from SABMain to SABWorker via workerMessagePort`)();
260
261 this.ipcMessagePorts.sabMainCallbackReply.addEventListener("message", (event) => {
262 switch (event.data) {
263 case "poll": {
264 this.ipcMessagePorts &&
265 this.ipcMessagePorts.sabMainCallbackReply.postMessage(
266 Object.keys(this.callbackBuffer).map((id) => ({
267 id,
268 apiKey: this.callbackBuffer[id].apiKey,
269 argumentz: this.callbackBuffer[id].argumentz,
270 })),
271 );
272 break;
273 }
274 case "releaseStop": {
275 this.onPlayStateChange(
276 this.currentPlayState === "renderStarted" ? "renderEnded" : "realtimePerformanceEnded",
277 );
278 this.publicEvents && this.publicEvents.triggerRealtimePerformanceEnded(this);
279 this.eventPromises && this.eventPromises.releaseStopPromise();
280 break;
281 }
282 case "releasePause": {
283 this.publicEvents.triggerRealtimePerformancePaused(this);
284 this.eventPromises.releasePausePromise();
285 break;
286 }
287 case "releaseResumed": {
288 this.publicEvents.triggerRealtimePerformanceResumed(this);
289 this.eventPromises.releaseResumePromise();
290 break;
291 }
292 default: {
293 event.data.forEach(({ id, answer }) => {
294 this.callbackBuffer[id].resolveCallback(answer);
295 delete this.callbackBuffer[id];
296 });
297 }
298 }
299 });
300 this.ipcMessagePorts.sabMainCallbackReply.start();
301
302 const proxyPort = Comlink.wrap(csoundWorker);
303 const wasmBytes = wasmDataURI();
304 this.proxyPort = proxyPort;
305 const csoundInstance = await proxyPort.initialize(
306 Comlink.transfer(
307 {
308 wasmDataURI: wasmBytes,
309 wasmTransformerDataURI: this.wasmTransformerDataURI,
310 messagePort: this.ipcMessagePorts.workerMessagePort,
311 callbackPort: this.ipcMessagePorts.sabWorkerCallbackReply,
312 withPlugins,
313 },
314 [
315 wasmBytes,
316 this.ipcMessagePorts.workerMessagePort,
317 this.ipcMessagePorts.sabWorkerCallbackReply,
318 ],
319 ),
320 );
321 this.csoundInstance = csoundInstance;
322
323 this.ipcMessagePorts.mainMessagePort.start();
324 this.ipcMessagePorts.mainMessagePortAudio.start();
325
326 log(`A proxy port from SABMain to SABWorker established`)();
327
328 this.exportApi.pause = this.csoundPause.bind(this);
329 this.exportApi.resume = this.csoundResume.bind(this);
330 this.exportApi.terminateInstance = this.terminateInstance.bind(this);
331 // this.exportApi.fs = this.fs;
332
333 this.exportApi.enableAudioInput = () =>
334 console.warn(
335 `enableAudioInput was ignored: please use -iadc option before calling start with useWorker=true`,
336 );
337
338 this.exportApi.getNode = async () => {
339 const maybeNode = this.audioWorker.audioWorkletNode;
340 if (maybeNode) {
341 return maybeNode;
342 } else {
343 const node = await new Promise((resolve) => {
344 this.exportApi.once("onAudioNodeCreated", resolve);
345 });
346 return node;
347 }
348 };
349
350 this.exportApi.getAudioContext = async () => this.audioWorker.audioContext;
351
352 this.exportApi = this.publicEvents.decorateAPI(this.exportApi);
353
354 // the default message listener
355 this.exportApi.addListener("message", console.log);
356
357 for (const apiKey of Object.keys(API)) {
358 const proxyCallback = makeProxyCallback(
359 proxyPort,
360 csoundInstance,
361 apiKey,
362 this.currentPlayState,
363 );
364 const reference = API[apiKey];
365
366 switch (apiKey) {
367 case "csoundCreate": {
368 break;
369 }
370 case "csoundStart": {
371 const csoundStart = async function () {
372 if (!csoundInstance || typeof csoundInstance !== "number") {
373 console.error("starting csound failed because csound instance wasn't created");
374 return -1;
375 }
376 if (this.eventPromises.isWaiting("start")) {
377 return -1;
378 } else {
379 this.eventPromises.createStartPromise();
380
381 const startResult = await proxyCallback({
382 audioStateBuffer,
383 audioStreamIn,
384 audioStreamOut,
385 midiBuffer,
386 csound: csoundInstance,
387 });
388
389 await this.eventPromises.waitForStart();
390
391 this.ipcMessagePorts &&
392 this.ipcMessagePorts.sabMainCallbackReply.postMessage({ unlock: true });
393
394 return startResult;
395 }
396 };
397
398 csoundStart.toString = () => reference.toString();
399 this.exportApi.start = csoundStart.bind(this);
400 break;
401 }
402 case "csoundStop": {
403 const csoundStop = async () => {
404 log(
405 [
406 "Checking if it's safe to call stop:",
407 stopableStates.has(this.currentPlayState),
408 "currentPlayState is",
409 this.currentPlayState,
410 ].join("\n"),
411 )();
412 if (this.eventPromises.isWaiting("stop")) {
413 log("already waiting to stop, doing nothing")();
414 return -1;
415 } else if (stopableStates.has(this.currentPlayState)) {
416 log("Marking SAB's state to STOP")();
417
418 this.eventPromises.createStopPromise();
419
420 Atomics.store(this.audioStatePointer, AUDIO_STATE.STOP, 1);
421 log("Marking that performance is not running anymore (stops the audio too)")();
422 Atomics.store(this.audioStatePointer, AUDIO_STATE.IS_PERFORMING, 0);
423
424 // A potential case where the thread is locked because of pause
425 if (this.currentPlayState === "realtimePerformancePaused") {
426 Atomics.store(this.audioStatePointer, AUDIO_STATE.IS_PAUSED, 0);
427 Atomics.notify(this.audioStatePointer, AUDIO_STATE.IS_PAUSED);
428 }
429 if (this.currentPlayState !== "renderStarted") {
430 !Atomics.compareExchange(this.audioStatePointer, AUDIO_STATE.CSOUND_LOCK, 0, 1) &&
431 Atomics.notify(this.audioStatePointer, AUDIO_STATE.CSOUND_LOCK);
432 }
433 await this.eventPromises.waitForStop();
434 return 0;
435 } else {
436 return -1;
437 }
438 };
439 this.exportApi.stop = csoundStop.bind(this);
440 csoundStop.toString = () => reference.toString();
441 break;
442 }
443
444 case "csoundReset": {
445 const csoundReset = async () => {
446 // no start = noReset
447 if (!this.currentPlayState) {
448 return;
449 }
450
451 if (this.eventPromises.isWaiting("reset")) {
452 return -1;
453 } else {
454 if (stopableStates.has(this.currentPlayState)) {
455 await this.exportApi.stop();
456 }
457 this.ipcMessagePorts.restartAudioWorkerPorts();
458 if (!this.audioContextIsProvided) {
459 await this.audioWorker.terminateInstance();
460 delete this.audioWorker.audioContext;
461 }
462 const resetResult = await proxyCallback([]);
463 return resetResult;
464 }
465 };
466 this.exportApi.reset = csoundReset.bind(this);
467 csoundReset.toString = () => reference.toString();
468 break;
469 }
470 case "csoundPushMidiMessage": {
471 const midiMessage = async (status = 0, data1 = 0, data2 = 0) => {
472 this.handleMidiInput({ data: [status, data1, data2] });
473 };
474 this.exportApi.midiMessage = midiMessage.bind(this);
475 midiMessage.toString = () => reference.toString();
476 break;
477 }
478
479 case "fs": {
480 this.exportApi.fs = {};
481 Object.keys(reference).forEach((method) => {
482 const proxyFsCallback = makeProxyCallback(
483 proxyPort,
484 csoundInstance,
485 method,
486 this.currentPlayState,
487 );
488 proxyFsCallback.toString = () => reference[method].toString();
489 this.exportApi.fs[method] = proxyFsCallback;
490 });
491 break;
492 }
493
494 default: {
495 // avoiding deadlock by sending the IPC callback
496 // while thread is unlocked
497 const bufferWrappedCallback = async (...arguments_) => {
498 if (
499 this.currentPlayState === "realtimePerformanceStarted" ||
500 this.currentPlayState === "renderStarted" ||
501 this.eventPromises.isWaitingToStart()
502 // startPromiz indicates that startup is in progress
503 // and any events send before it's resolved are swallowed
504 ) {
505 const callbackId = this.callbackId;
506 this.callbackId += 1;
507 const returnPromise = new Promise((resolve, reject) => {
508 const timeout = setTimeout(
509 () =>
510 reject(
511 new Error(`Worker timed out so ${csoundApiRename(apiKey)}() wasn't called!`),
512 ),
513 10000,
514 );
515 const resolveCallback = (answer) => {
516 clearTimeout(timeout);
517 resolve(answer);
518 };
519 this.callbackBuffer[callbackId] = {
520 resolveCallback,
521 apiKey,
522 argumentz: [csoundInstance, ...arguments_],
523 };
524 });
525 Atomics.compareExchange(audioStatePointer, AUDIO_STATE.HAS_PENDING_CALLBACKS, 0, 1);
526 return await returnPromise;
527 } else {
528 return await proxyCallback.apply(undefined, arguments_);
529 }
530 };
531 bufferWrappedCallback.toString = () => reference.toString();
532 this.exportApi[csoundApiRename(apiKey)] = bufferWrappedCallback;
533 break;
534 }
535 }
536 }
537 log(`PUBLIC API Generated and stored`)();
538 }
539 }
540
541 export default SharedArrayBufferMainThread;