Debian Codegraph @

wasm/browser/src/workers/worklet.singlethread.worker.js

raw
1 /* eslint-disable unicorn/require-post-message-target-origin */
2 /*
3 worklet.singlethread.worker.js
4
5 Copyright (C) 2018 Steven Yi, Victor Lazzarini
6
7 This file is part of Csound.
8
9 The Csound Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 Csound is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with Csound; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 02110-1301 USA
23 */
24
25 import * as Comlink from "comlink/dist/esm/comlink.mjs";
26 import MessagePortState from "../utils/message-port-state";
27 import libcsoundFactory from "../libcsound";
28 import loadWasm from "../module";
29 import { assoc, pipe } from "rambda/dist/rambda.esm.js";
30 import { clearArray } from "../utils/clear-array";
31 import { logSinglethreadWorkletWorker as log } from "../logger";
32 import { renderFunction } from "./common.utils";
33
34 let libraryCsound;
35 let combined;
36 const rtmidiQueue = [];
37
38 const callUncloned = async (k, arguments_) => {
39 const caller = combined.get(k);
40 const returnValue = caller && caller.apply({}, arguments_ || []);
41 return returnValue;
42 };
43
44 class WorkletSinglethreadWorker extends AudioWorkletProcessor {
45 static get parameterDescriptors() {
46 return [];
47 }
48
49 constructor(options) {
50 super(options);
51 // eslint-disable-next-line no-undef
52 this.sampleRate = sampleRate;
53 this.options = options;
54 this.initialize = this.initialize.bind(this);
55 this.pause = this.pause.bind(this);
56 this.process = this.process.bind(this);
57 this.resume = this.resume.bind(this);
58 this.start = this.start.bind(this);
59 this.needsStartNotification = false;
60 this.isPaused = false;
61 this.callUncloned = () => console.error("Csound worklet thread is still uninitialized!");
62 this.port.start();
63 Comlink.expose(this, this.port);
64 this.workerMessagePort = new MessagePortState();
65
66 this.initializeMessagePort = ({ messagePort, rtmidiPort }) => {
67 this.workerMessagePort.post = (messageLog) => messagePort.postMessage({ log: messageLog });
68 this.workerMessagePort.broadcastPlayState = (playStateChange) => {
69 if (this.workerMessagePort.workerState !== playStateChange) {
70 this.workerMessagePort.workerState = playStateChange;
71 }
72 messagePort.postMessage({ playStateChange });
73 };
74 this.workerMessagePort.ready = true;
75 log(`initRtMidiEventPort`)();
76 this.rtmidiPort = rtmidiPort;
77 this.rtmidiPort.addEventListener("message", ({ data: payload }) => {
78 rtmidiQueue.push(payload);
79 });
80 this.rtmidiPort.start();
81 };
82 }
83
84 async initialize(wasmDataURI, withPlugins) {
85 log("initializing worklet.singlethread.worker")();
86
87 let resolver;
88 const waiter = new Promise((resolve) => {
89 resolver = resolve;
90 });
91
92 loadWasm({
93 wasmDataURI,
94 withPlugins,
95 messagePort: this.workerMessagePort,
96 }).then(([wasm, wasi]) => {
97 this.wasm = wasm;
98 this.wasi = wasi;
99 wasm.wasi = wasi;
100
101 libraryCsound = libcsoundFactory(wasm);
102 this.callUncloned = callUncloned;
103 this.csound = libraryCsound.csoundCreate(0);
104 this.result = 0;
105 this.running = false;
106 this.isRendering = false;
107 this.started = false;
108 this.resetCsound(false);
109
110 const csoundCreate = async (v) => {
111 return this.csound;
112 };
113
114 const allAPI = pipe(
115 assoc("csoundCreate", csoundCreate),
116 assoc("csoundReset", this.resetCsound.bind(this)),
117 assoc("csoundStart", this.start.bind(this)),
118 assoc("csoundStop", this.stop.bind(this)),
119 assoc("wasm", wasm),
120 )(libraryCsound);
121
122 combined = new Map(Object.entries(allAPI));
123 log("wasm initialized and api generated")();
124 resolver();
125 });
126 log("waiting on wasm initialization to complete")();
127 await waiter;
128 }
129
130 async resetCsound(callReset) {
131 // no start = no reset
132 if (callReset && !this.workerMessagePort) {
133 return -1;
134 }
135 if (
136 callReset &&
137 this.workerMessagePort.workerState !== "realtimePerformanceEnded" &&
138 this.workerMessagePort.workerState !== "realtimePerformanceStarted"
139 ) {
140 // reset can't be called until performance has started or ended!
141 return -1;
142 }
143 if (callReset && this.workerMessagePort.workerState === "realtimePerformanceStarted") {
144 this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
145 }
146
147 this.running = false;
148 this.started = false;
149 this.result = 0;
150
151 const cs = this.csound;
152
153 if (callReset) {
154 libraryCsound.csoundReset(cs);
155 }
156
157 libraryCsound.csoundSetMidiCallbacks(cs);
158 libraryCsound.csoundSetOption(cs, "-iadc");
159 libraryCsound.csoundSetOption(cs, "-odac");
160 this.sampleRate && libraryCsound.csoundSetOption(cs, "--sample-rate=" + this.sampleRate);
161 this.nchnls = -1;
162 this.nchnls_i = -1;
163 delete this.csoundOutputBuffer;
164 }
165
166 stop() {
167 if (this.csound) {
168 libraryCsound.csoundStop(this.csound);
169 }
170 this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
171 }
172
173 pause() {
174 if (!this.isPaused) {
175 this.workerMessagePort.broadcastPlayState("realtimePerformancePaused");
176 this.isPaused = true;
177 }
178 }
179
180 resume() {
181 if (this.isPaused) {
182 this.workerMessagePort.broadcastPlayState("realtimePerformanceResumed");
183 this.isPaused = false;
184 }
185 }
186
187 process(inputs, outputs) {
188 if (!this.isRendering && (this.isPaused || !this.csoundOutputBuffer || !this.running)) {
189 const output = outputs[0];
190 const bufferLength = output[0].length;
191 for (let index = 0; index < bufferLength; index++) {
192 for (let channel = 0; channel < output.numberOfChannels; channel++) {
193 const outputChannel = output[channel];
194 outputChannel[index] = 0;
195 }
196 }
197 return true;
198 }
199
200 // if we are starting, we need to bordcast it
201 // this late in order to avoid timing issues
202 if (this.needsStartNotification) {
203 this.needsStartNotification = false;
204 this.workerMessagePort.broadcastPlayState("realtimePerformanceStarted");
205 }
206
207 if (rtmidiQueue.length > 0) {
208 rtmidiQueue.forEach((event) => {
209 libraryCsound.csoundPushMidiMessage(this.csound, event[0], event[1], event[2]);
210 });
211 clearArray(rtmidiQueue);
212 }
213
214 const input = inputs[0];
215 const output = outputs[0];
216
217 const bufferLength = output[0].length;
218
219 let csOut = this.csoundOutputBuffer;
220 let csIn = this.csoundInputBuffer;
221 const ksmps = this.ksmps;
222 const zerodBFS = this.zerodBFS;
223
224 let cnt = this.cnt;
225 const nchnls = this.nchnls;
226 const nchnlsIn = this.nchnls_i;
227 let result = this.result;
228
229 for (let index = 0; index < bufferLength; index++, cnt++) {
230 if (cnt >= ksmps && result === 0) {
231 // if we need more samples from Csound
232 result = libraryCsound.csoundPerformKsmps(this.csound);
233 cnt = 0;
234
235 if (result !== 0) {
236 this.running = false;
237 this.started = false;
238 libraryCsound.csoundCleanup(this.csound);
239 this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
240 // if (this.stopPromiz) {
241 // this.stopPromiz();
242 // delete this.stopPromiz;
243 // }
244 }
245 }
246
247 /* Check if MEMGROWTH occured from csoundPerformKsmps or otherwise. If so,
248 rest output ant input buffers to new pointer locations. */
249 if (!csOut || csOut.length === 0) {
250 csOut = this.csoundOutputBuffer = new Float64Array(
251 this.wasm.wasi.memory.buffer,
252 libraryCsound.csoundGetSpout(this.csound),
253 ksmps * nchnls,
254 );
255 }
256
257 if (!csIn || csIn.length === 0) {
258 csIn = this.csoundInputBuffer = new Float64Array(
259 this.wasm.wasi.memory.buffer,
260 libraryCsound.csoundGetSpin(this.csound),
261 ksmps * nchnlsIn,
262 );
263 }
264
265 // handle 1->1, 1->2, 2->1, 2->2 input channel count mixing and nchnls_i
266 const inputChanMax = Math.min(this.nchnls_i, input.length);
267 for (let channel = 0; channel < inputChanMax; channel++) {
268 const inputChannel = input[channel];
269 csIn[cnt * nchnlsIn + channel] = inputChannel[index] * zerodBFS;
270 }
271
272 // Channel mixing matches behavior of:
273 // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Up-mixing_and_down-mixing
274
275 // handle 1->1, 1->2, 2->1, 2->2 output channel count mixing and nchnls
276 if (this.nchnls === output.length) {
277 for (const [channel, outputChannel] of output.entries()) {
278 if (result === 0) outputChannel[index] = csOut[cnt * nchnls + channel] / zerodBFS;
279 else outputChannel[index] = 0;
280 }
281 } else if (this.nchnls === 2 && output.length === 1) {
282 const outputChannel = output[0];
283 if (result === 0) {
284 const left = csOut[cnt * nchnls] / zerodBFS;
285 const right = csOut[cnt * nchnls + 1] / zerodBFS;
286 outputChannel[index] = 0.5 * (left + right);
287 } else {
288 outputChannel[index] = 0;
289 }
290 } else if (this.nchnls === 1 && output.length === 2) {
291 const outChan0 = output[0];
292 const outChan1 = output[1];
293
294 if (result === 0) {
295 const value = csOut[cnt * nchnls] / zerodBFS;
296 outChan0[index] = value;
297 outChan1[index] = value;
298 } else {
299 outChan0[index] = 0;
300 outChan1[index] = 0;
301 }
302 } else {
303 // FIXME: we do not support other cases at this time
304 }
305 }
306
307 this.cnt = cnt;
308 this.result = result;
309
310 return true;
311 }
312
313 async isRequestingInput() {
314 const cs = this.csound;
315 const inputName = libraryCsound.csoundGetInputName(cs) || "";
316 return inputName.includes("adc");
317 }
318
319 async start() {
320 let returnValueValue = -1;
321 if (!this.started) {
322 log("worklet thread is starting..")();
323 const cs = this.csound;
324 const ksmps = libraryCsound.csoundGetKsmps(cs);
325 this.ksmps = ksmps;
326 this.cnt = ksmps;
327 this.nchnls = libraryCsound.csoundGetNchnls(cs);
328 this.nchnls_i = libraryCsound.csoundGetNchnlsInput(cs);
329
330 this.zerodBFS = libraryCsound.csoundGet0dBFS(cs);
331
332 returnValueValue = libraryCsound.csoundStart(cs);
333
334 if (returnValueValue !== 0) {
335 return returnValueValue;
336 }
337
338 const outputName = libraryCsound.csoundGetOutputName(cs) || "test.wav";
339 const isExpectingRealtimeOutput = outputName.includes("dac");
340
341 if (isExpectingRealtimeOutput) {
342 this.csoundOutputBuffer = new Float64Array(
343 this.wasm.wasi.memory.buffer,
344 libraryCsound.csoundGetSpout(cs),
345 ksmps * this.nchnls,
346 );
347 this.csoundInputBuffer = new Float64Array(
348 this.wasm.wasi.memory.buffer,
349 libraryCsound.csoundGetSpin(cs),
350 ksmps * this.nchnls_i,
351 );
352
353 log("csoundStart called with {} return val", returnValueValue)();
354 this.started = true;
355 this.needsStartNotification = true;
356 } else {
357 const renderer = renderFunction({
358 libraryCsound,
359 workerMessagePort: this.workerMessagePort,
360 wasi: this.wasi,
361 });
362 this.isRendering = true;
363 this.workerMessagePort.broadcastPlayState("renderStarted");
364 renderer({ csound: cs }).then(() => {
365 this.isRendering = false;
366 });
367 return 0;
368 }
369 } else {
370 log("worklet was asked to start but it already has!")();
371 }
372 this.running = true;
373 return returnValueValue;
374 }
375 }
376
377 registerProcessor("csound-singlethread-worklet-processor", WorkletSinglethreadWorker);