Debian Codegraph @

wasm/browser/src/workers/vanilla.worker.js

raw
1 /* eslint-disable unicorn/require-post-message-target-origin */
2 import { expose } from "comlink/dist/esm/comlink.mjs";
3 import MessagePortState from "../utils/message-port-state";
4 // import { initFS, getWorkerFs, syncWorkerFs } from "../filesystem/worker-fs";
5 import { logVANWorker as log } from "../logger";
6 import { RING_BUFFER_SIZE } from "../constants.js";
7 import { handleCsoundStart, instantiateAudioPacket, renderFunction } from "./common.utils";
8 import libcsoundFactory from "../libcsound";
9 import loadWasm from "../module";
10 import { clearArray } from "../utils/clear-array";
11 import { assoc, pipe } from "rambda/dist/rambda.esm.js";
12
13 let combined;
14 let audioProcessCallback = () => {};
15 const rtmidiQueue = [];
16
17 const createAudioInputBuffers = (audioInputs, inputsCount) => {
18 for (let channelIndex = 0; channelIndex < inputsCount; ++channelIndex) {
19 audioInputs.buffers.push(new Float64Array(RING_BUFFER_SIZE));
20 }
21 };
22
23 const generateAudioFrames = (arguments_, workerMessagePort) => {
24 if (workerMessagePort.vanillaWorkerState !== "realtimePerformanceEnded") {
25 return audioProcessCallback(arguments_);
26 }
27 };
28
29 const createRealtimeAudioThread =
30 ({
31 libraryCsound,
32 wasm,
33 wasi,
34 workerMessagePort,
35 audioInputs,
36 inputChannelCount,
37 outputChannelCount,
38 sampleRate,
39 }) =>
40 ({ csound }) => {
41 // Prompt for midi-input on demand
42 // const isRequestingRtMidiInput = libraryCsound._isRequestingRtMidiInput(csound);
43
44 // Prompt for microphone only on demand!
45 const isExpectingInput = libraryCsound.csoundGetInputName(csound).includes("adc");
46
47 // Store Csound AudioParams for upcoming performance
48 sampleRate && libraryCsound.csoundSetOption(csound, `--sr=${sampleRate}`);
49 outputChannelCount && libraryCsound.csoundSetOption(csound, `--nchnls=${outputChannelCount}`);
50 inputChannelCount && libraryCsound.csoundSetOption(csound, `--nchnls_i=${inputChannelCount}`);
51 const nchnls = libraryCsound.csoundGetNchnls(csound);
52 const nchnlsInput =
53 inputChannelCount > 0
54 ? inputChannelCount
55 : isExpectingInput
56 ? libraryCsound.csoundGetNchnlsInput(csound)
57 : 0;
58
59 const zeroDecibelFullScale = libraryCsound.csoundGet0dBFS(csound);
60
61 // const { buffer } = wasm.wasi.memory;
62 const inputBufferPtr = libraryCsound.csoundGetSpin(csound);
63 const outputBufferPtr = libraryCsound.csoundGetSpout(csound);
64 const ksmps = libraryCsound.csoundGetKsmps(csound);
65
66 let csoundInputBuffer = new Float64Array(
67 wasm.wasi.memory.buffer,
68 inputBufferPtr,
69 ksmps * nchnlsInput,
70 );
71
72 let csoundOutputBuffer = new Float64Array(
73 wasm.wasi.memory.buffer,
74 outputBufferPtr,
75 ksmps * nchnls,
76 );
77
78 let lastPerformance = 0;
79 let currentCsoundBufferPos = 0;
80 workerMessagePort.broadcastPlayState("realtimePerformanceStarted");
81
82 audioProcessCallback = ({ numFrames /** number */ }) => {
83 const outputAudioPacket = instantiateAudioPacket(nchnls, numFrames);
84 const hasInput = audioInputs.buffers.length > 0 && audioInputs.availableFrames >= numFrames;
85
86 if (rtmidiQueue.length > 0) {
87 rtmidiQueue.forEach((event) => {
88 libraryCsound.csoundPushMidiMessage(csound, event[0], event[1], event[2]);
89 });
90 clearArray(rtmidiQueue);
91 }
92
93 for (let index = 0; index < numFrames; index++) {
94 currentCsoundBufferPos = (currentCsoundBufferPos + 1) % ksmps;
95 if (workerMessagePort.vanillaWorkerState === "realtimePerformanceEnded") {
96 if (lastPerformance === 0) {
97 libraryCsound.csoundStop(csound);
98 lastPerformance = libraryCsound.csoundPerformKsmps(csound);
99 }
100 workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
101
102 audioProcessCallback = () => {};
103 clearArray(rtmidiQueue);
104 audioInputs.port = undefined;
105 return { framesLeft: index };
106 }
107 if (currentCsoundBufferPos === 0 && lastPerformance === 0) {
108 lastPerformance = libraryCsound.csoundPerformKsmps(csound);
109 if (lastPerformance !== 0) {
110 workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
111 audioProcessCallback = () => {};
112 clearArray(rtmidiQueue);
113 audioInputs.port = undefined;
114 return { framesLeft: index };
115 }
116 }
117
118 // MEMGROW KILLS REFERENCES!
119 // https://github.com/emscripten-core/emscripten/issues/6747#issuecomment-400081465
120 if (csoundInputBuffer.length === 0) {
121 csoundInputBuffer = new Float64Array(
122 wasm.wasi.memory.buffer,
123 libraryCsound.csoundGetSpin(csound),
124 ksmps * nchnlsInput,
125 );
126 }
127 if (csoundOutputBuffer.length === 0) {
128 csoundOutputBuffer = new Float64Array(
129 wasm.wasi.memory.buffer,
130 libraryCsound.csoundGetSpout(csound),
131 ksmps * nchnls,
132 );
133 }
134
135 outputAudioPacket.forEach((channel, channelIndex) => {
136 if (csoundOutputBuffer.length > 0) {
137 channel[index] =
138 (csoundOutputBuffer[currentCsoundBufferPos * nchnls + channelIndex] || 0) /
139 zeroDecibelFullScale;
140 }
141 });
142
143 if (hasInput) {
144 for (let ii = 0; ii < nchnlsInput; ii++) {
145 csoundInputBuffer[currentCsoundBufferPos * nchnlsInput + ii] =
146 (audioInputs.buffers[ii][index + (audioInputs.inputReadIndex % RING_BUFFER_SIZE)] ||
147 0) * zeroDecibelFullScale;
148 }
149 }
150 }
151
152 if (hasInput) {
153 audioInputs.availableFrames -= numFrames;
154 audioInputs.inputReadIndex += numFrames % RING_BUFFER_SIZE;
155 }
156
157 return { audioPacket: outputAudioPacket, framesLeft: 0 };
158 };
159 };
160
161 const callUncloned = async (k, arguments_) => {
162 const caller = combined.get(k);
163 return caller && caller.apply({}, arguments_ || []);
164 };
165
166 const initMessagePort = ({ port }) => {
167 log(`initMessagePort`)();
168 const workerMessagePort = new MessagePortState();
169 workerMessagePort.port = port;
170 workerMessagePort.post = (messageLog) => port.postMessage({ log: messageLog });
171 workerMessagePort.broadcastPlayState = (playStateChange) => {
172 workerMessagePort.vanillaWorkerState = playStateChange;
173 port.postMessage({ playStateChange });
174 };
175 workerMessagePort.ready = true;
176 return workerMessagePort;
177 };
178
179 const initRequestPort = ({ csoundWorkerFrameRequestPort, workerMessagePort }) => {
180 log(`initRequestPort`)();
181 csoundWorkerFrameRequestPort.addEventListener("message", (requestEvent) => {
182 const { framesLeft = 0, audioPacket } =
183 generateAudioFrames(requestEvent.data, workerMessagePort) || {};
184 csoundWorkerFrameRequestPort.postMessage({
185 numFrames: requestEvent.data.numFrames - framesLeft,
186 audioPacket,
187 ...requestEvent.data,
188 });
189 });
190 csoundWorkerFrameRequestPort.start();
191 return csoundWorkerFrameRequestPort;
192 };
193
194 const initAudioInputPort = ({ port }) => {
195 log(`initAudioInputPort`)();
196 const audioInputs = {
197 availableFrames: 0,
198 buffers: [],
199 inputReadIndex: 0,
200 inputWriteIndex: 0,
201 port,
202 };
203 audioInputs.port.addEventListener("message", ({ data: pkgs }) => {
204 if (audioInputs.buffers.length === 0) {
205 createAudioInputBuffers(audioInputs, pkgs.length);
206 }
207 audioInputs.buffers.forEach((buf, index) => {
208 buf.set(pkgs[index], audioInputs.inputWriteIndex);
209 });
210 audioInputs.inputWriteIndex += pkgs[0].length;
211 audioInputs.availableFrames += pkgs[0].length;
212 if (audioInputs.inputWriteIndex >= RING_BUFFER_SIZE) {
213 audioInputs.inputWriteIndex = 0;
214 }
215 });
216 audioInputs.port.start();
217 return audioInputs;
218 };
219
220 const initRtMidiEventPort = ({ rtmidiPort }) => {
221 log(`initRtMidiEventPort`)();
222 rtmidiPort.addEventListener("message", ({ data: payload }) => {
223 rtmidiQueue.push(payload);
224 });
225 rtmidiPort.start();
226 return rtmidiPort;
227 };
228
229 const initialize = async ({
230 audioInputPort,
231 inputChannelCount,
232 messagePort,
233 outputChannelCount,
234 requestPort,
235 rtmidiPort,
236 sampleRate,
237 wasmDataURI,
238 wasmTransformerDataURI,
239 withPlugins = [],
240 }) => {
241 log(`initializing wasm and exposing csoundAPI functions from worker to main`)();
242 const workerMessagePort = initMessagePort({ port: messagePort });
243
244 const audioInputs = initAudioInputPort({ port: audioInputPort });
245 initRequestPort({
246 csoundWorkerFrameRequestPort: requestPort,
247 workerMessagePort,
248 });
249 initRtMidiEventPort({ rtmidiPort });
250
251 const [wasm, wasi] = await loadWasm({
252 wasmDataURI,
253 wasmTransformerDataURI,
254 withPlugins,
255 messagePort: workerMessagePort,
256 });
257
258 wasm.wasi = wasi;
259
260 const libraryCsound = libcsoundFactory(wasm);
261
262 const startHandler = (_, arguments_) =>
263 handleCsoundStart(
264 workerMessagePort,
265 libraryCsound,
266 wasi,
267 createRealtimeAudioThread({
268 audioInputs,
269 inputChannelCount,
270 libraryCsound,
271 outputChannelCount,
272 wasm,
273 wasi,
274 workerMessagePort,
275 }),
276 renderFunction({
277 inputChannelCount,
278 libraryCsound,
279 outputChannelCount,
280 wasm,
281 workerMessagePort,
282 }),
283 )(arguments_);
284
285 const allAPI = pipe(assoc("csoundStart", startHandler), assoc("wasm", wasm))(libraryCsound);
286 combined = new Map(Object.entries(allAPI));
287
288 libraryCsound.csoundInitialize(0);
289 const csoundInstance = libraryCsound.csoundCreate();
290
291 workerMessagePort.port.addEventListener("message", (event) => {
292 if (event.data && event.data.newPlayState) {
293 if (event.data.newPlayState === "realtimePerformanceEnded") {
294 libraryCsound.csoundStop(csoundInstance);
295 if (workerMessagePort.vanillaWorkerState !== "realtimePerformanceEnded") {
296 libraryCsound.csoundPerformKsmps(csoundInstance);
297 }
298 // ping-pong for better timing of events:
299 // the event is only sent from main but state isn't stored
300 // until it arrived back
301 workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
302 }
303 workerMessagePort.vanillaWorkerState = event.data.newPlayState;
304 }
305 });
306
307 workerMessagePort.port.start();
308
309 return csoundInstance;
310 };
311
312 expose({
313 initialize,
314 callUncloned,
315 });