Debian Codegraph @

wasm/browser/src/workers/worklet.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 { AUDIO_STATE, RING_BUFFER_SIZE } from "../constants";
5 import { instantiateAudioPacket } from "./common.utils";
6 import { logWorkletWorker as log } from "../logger";
7
8 const VANILLA_INPUT_WRITE_BUFFER_LEN = 2048;
9
10 const activeNodes = new Map();
11
12 function processSharedArrayBuffer(inputs, outputs) {
13 const isPerforming = Atomics.load(this.sharedArrayBuffer, AUDIO_STATE.IS_PERFORMING) === 1;
14 const isPaused = Atomics.load(this.sharedArrayBuffer, AUDIO_STATE.IS_PAUSED) === 1;
15 const isStopped = Atomics.load(this.sharedArrayBuffer, AUDIO_STATE.STOP) === 1;
16
17 if (this.startPromiz) {
18 this.startPromiz();
19 delete this.startPromiz;
20 }
21
22 if (!this.sharedArrayBuffer || isPaused || !isPerforming || isStopped) {
23 this.isPerformingLastTime = isPerforming;
24 this.firstBufferReady = false;
25 this.notifiedOnce = false;
26 // this.preProcessCount = 0;
27
28 // Fix for that chrome 64 bug which doesn't 0 the arrays
29 // https://github.com/csound/web-ide/issues/102#issuecomment-663894059
30 (outputs[0] || []).forEach((array) => array.fill(0));
31 return true;
32 }
33
34 this.isPerformingLastTime = isPerforming;
35
36 const writeableInputChannels = inputs && inputs[0];
37 const writeableOutputChannels = outputs && outputs[0];
38 const bufferLength = writeableOutputChannels[0].length;
39
40 if (this.bufferLength !== bufferLength) {
41 this.bufferLength = bufferLength;
42 Atomics.store(this.sharedArrayBuffer, AUDIO_STATE.BUFFER_LEN, bufferLength);
43 }
44
45 const nextInputWriteIndex =
46 writeableInputChannels && writeableInputChannels.length > 0
47 ? (this.inputWriteIndex + bufferLength) % RING_BUFFER_SIZE
48 : 0;
49
50 const nextOutputReadIndex =
51 writeableOutputChannels && writeableOutputChannels.length > 0
52 ? (this.outputReadIndex + bufferLength) % RING_BUFFER_SIZE
53 : 0;
54
55 if (Atomics.load(this.sharedArrayBuffer, AUDIO_STATE.AVAIL_OUT_BUFS) >= bufferLength) {
56 this.bufferUnderrunCount && (this.bufferUnderrunCount = 0);
57 writeableOutputChannels.forEach((channelBuffer, channelIndex) => {
58 // a simple set, where the len is the destination size and 2nd arg the offset
59
60 channelBuffer.set(
61 this.sabOutputChannels[channelIndex].subarray(
62 this.outputReadIndex,
63 nextOutputReadIndex < this.outputReadIndex ? RING_BUFFER_SIZE : nextOutputReadIndex,
64 ),
65 );
66 });
67
68 if (
69 writeableInputChannels &&
70 writeableInputChannels[0] &&
71 writeableInputChannels[0].length > 0
72 ) {
73 writeableInputChannels.forEach((channelBuffer, channelIndex) => {
74 this.sabInputChannels[channelIndex].set(channelBuffer, this.inputWriteIndex);
75 });
76
77 this.inputWriteIndex = nextInputWriteIndex;
78 // Atomics.store(this.sharedArrayBuffer, AUDIO_STATE.INPUT_WRITE_INDEX, nextInputWriteIndex);
79
80 // increase availability of new input data
81 Atomics.add(
82 this.sharedArrayBuffer,
83 AUDIO_STATE.AVAIL_IN_BUFS,
84 writeableInputChannels[0].length,
85 );
86 }
87
88 this.outputReadIndex = nextOutputReadIndex;
89
90 // subtract the available output buffers, all channels are the same length
91 Atomics.sub(this.sharedArrayBuffer, AUDIO_STATE.AVAIL_OUT_BUFS, bufferLength);
92 Atomics.store(this.sharedArrayBuffer, AUDIO_STATE.OUTPUT_READ_INDEX, this.outputReadIndex);
93 } else {
94 if (this.outputReadIndex > 4098) {
95 console.log("buffer underrun");
96 } else {
97 // a not so pretty way to prevent buffer underrun messages
98 // from being delivered before the first buffers are received
99 return true;
100 }
101
102 this.bufferUnderrunCount += 1;
103 if (this.bufferUnderrunCount === 100) {
104 // 100 buffer Underruns in a row
105 // means a fatal situation and browser
106 // may crash
107 this.workerMessagePort.post("FATAL: 100 buffers failed in a row");
108 this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
109 return false;
110 }
111 }
112
113 return true;
114 }
115
116 function processVanillaBuffers(inputs, outputs) {
117 if (!this.vanillaInitialized) {
118 // this minimizes startup glitches
119 const firstTransferSize = 8192;
120 this.audioFramePort.requestFrames({
121 readIndex: 0,
122 numFrames: firstTransferSize,
123 });
124 this.pendingFrames += firstTransferSize;
125 this.vanillaInitialized = true;
126 if (this.startPromiz) {
127 this.startPromiz();
128 delete this.startPromiz;
129 }
130 return true;
131 }
132
133 if (!this.vanillaFirstTransferDone) {
134 ((outputs && outputs[0]) || []).forEach((array) => array.fill(0));
135 return true;
136 }
137
138 const writeableInputChannels = inputs && inputs[0];
139 const writeableOutputChannels = outputs && outputs[0];
140 const bufferLength = writeableOutputChannels ? writeableOutputChannels[0].length : 0;
141
142 const nextOutputReadIndex =
143 writeableOutputChannels && writeableOutputChannels.length > 0
144 ? (this.vanillaOutputReadIndex + writeableOutputChannels[0].length) % RING_BUFFER_SIZE
145 : 0;
146
147 const nextInputReadIndex =
148 writeableInputChannels && writeableInputChannels.length > 0
149 ? (this.vanillaInputReadIndex + writeableInputChannels[0].length) % RING_BUFFER_SIZE
150 : 0;
151
152 if (bufferLength && this.vanillaAvailableFrames >= bufferLength) {
153 writeableOutputChannels.forEach((channelBuffer, channelIndex) => {
154 channelBuffer.set(
155 this.vanillaOutputChannels[channelIndex].subarray(
156 this.vanillaOutputReadIndex,
157 nextOutputReadIndex < this.vanillaOutputReadIndex
158 ? RING_BUFFER_SIZE
159 : nextOutputReadIndex,
160 ),
161 );
162 });
163
164 if (writeableInputChannels && writeableInputChannels.length > 0) {
165 writeableInputChannels.forEach((channelBuffer, channelIndex) => {
166 this.vanillaInputChannels[channelIndex].set(channelBuffer, this.vanillaInputReadIndex);
167 });
168 if (nextInputReadIndex % VANILLA_INPUT_WRITE_BUFFER_LEN === 0) {
169 const packet = [];
170 const pastBufferBegin =
171 (nextInputReadIndex === 0 ? RING_BUFFER_SIZE : nextInputReadIndex) -
172 VANILLA_INPUT_WRITE_BUFFER_LEN;
173 const thisBufferEnd = nextInputReadIndex === 0 ? RING_BUFFER_SIZE : nextInputReadIndex;
174 this.vanillaInputChannels.forEach((channelBuffer) => {
175 packet.push(channelBuffer.subarray(pastBufferBegin, thisBufferEnd));
176 });
177 this.audioInputPort.transferInputFrames(packet);
178 }
179 }
180
181 this.vanillaOutputReadIndex = nextOutputReadIndex;
182 this.vanillaInputReadIndex = nextInputReadIndex;
183 this.vanillaAvailableFrames -= bufferLength;
184 this.bufferUnderrunCount = 0;
185 } else {
186 // minimize noise
187 if (this.bufferUnderrunCount > 1 && this.bufferUnderrunCount < 12) {
188 this.workerMessagePort.post("Buffer underrun");
189 this.bufferUnderrunCount += 1;
190 }
191
192 if (this.bufferUnderrunCount === 100) {
193 // 100 buffer Underruns in a row
194 // means a fatal situation and browser
195 // may crash
196 this.workerMessagePort.post("FATAL: 100 buffers failed in a row");
197 this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded");
198 return false;
199 }
200 }
201
202 // 2048 is max buffer
203 const framesRequest = 2048 - this.vanillaAvailableFrames;
204 if (framesRequest > 0) {
205 const futureOutputReadIndex =
206 (this.vanillaAvailableFrames + nextOutputReadIndex + this.pendingFrames) % RING_BUFFER_SIZE;
207
208 this.audioFramePort.requestFrames({
209 readIndex: futureOutputReadIndex,
210 numFrames: framesRequest,
211 });
212 this.pendingFrames += framesRequest;
213 }
214
215 return true;
216 }
217
218 class CsoundWorkletProcessor extends AudioWorkletProcessor {
219 constructor({
220 processorOptions: {
221 contextUid,
222 inputsCount,
223 outputsCount,
224 ksmps,
225 // sampleRate,
226 maybeSharedArrayBuffer,
227 maybeSharedArrayBufferAudioIn,
228 maybeSharedArrayBufferAudioOut,
229 },
230 }) {
231 super();
232 const nodeUid = `${contextUid}Node`;
233 activeNodes.set(nodeUid, this);
234 this.messagePortsReady = false;
235 this.currentPlayState = undefined;
236 this.pause = this.pause.bind(this);
237 this.resume = this.resume.bind(this);
238 this.isPaused = false;
239 // this.sampleRate = sampleRate;
240 this.ksmps = ksmps;
241 this.inputsCount = inputsCount;
242 this.outputsCount = outputsCount;
243 this.inputWriteIndex = 0;
244 this.outputReadIndex = 0;
245 this.bufferUnderrunCount = 0;
246
247 // NON-SAB PROCESS
248 this.isPerformingLastTime = false;
249 this.preProcessCount = 0;
250
251 if (maybeSharedArrayBuffer) {
252 this.sharedArrayBuffer = maybeSharedArrayBuffer;
253 this.audioStreamIn = maybeSharedArrayBufferAudioIn;
254 this.audioStreamOut = maybeSharedArrayBufferAudioOut;
255 this.sabOutputChannels = [];
256 this.sabInputChannels = [];
257
258 for (let channelIndex = 0; channelIndex < inputsCount; ++channelIndex) {
259 this.sabInputChannels.push(
260 new Float64Array(this.audioStreamIn, RING_BUFFER_SIZE * channelIndex, RING_BUFFER_SIZE),
261 );
262 }
263
264 for (let channelIndex = 0; channelIndex < outputsCount; ++channelIndex) {
265 this.sabOutputChannels.push(
266 new Float64Array(this.audioStreamOut, RING_BUFFER_SIZE * channelIndex, RING_BUFFER_SIZE),
267 );
268 }
269 this.actualProcess = processSharedArrayBuffer.bind(this);
270 } else {
271 this.vanillaOutputChannels = [];
272 this.vanillaInputChannels = [];
273 this.vanillaOutputReadIndex = 0;
274 this.vanillaInputReadIndex = 0;
275 this.vanillaAvailableFrames = 0;
276 this.pendingFrames = 0;
277
278 this.vanillaInitialized = false;
279 this.vanillaFirstTransferDone = false;
280 this.minBufferSize = 4096;
281 this.vanillaInputChannels = instantiateAudioPacket(inputsCount, RING_BUFFER_SIZE);
282 this.vanillaOutputChannels = instantiateAudioPacket(outputsCount, RING_BUFFER_SIZE);
283
284 this.actualProcess = processVanillaBuffers.bind(this);
285 this.updateVanillaFrames = this.updateVanillaFrames.bind(this);
286 }
287 expose({ initialize, pause: this.pause, resume: this.resume }, this.port);
288 log(`Worker thread was constructed`)();
289 }
290
291 initCallbacks({ workerMessagePort, audioInputPort, audioFramePort, startPromiz }) {
292 log(`initCallbacks in worker`)();
293 if (workerMessagePort) {
294 this.workerMessagePort = workerMessagePort;
295 }
296
297 if (audioInputPort) {
298 this.audioInputPort = audioInputPort;
299 }
300 if (audioFramePort) {
301 this.audioFramePort = audioFramePort;
302 }
303 this.messagePortsReady = true;
304 this.startPromiz = startPromiz;
305 }
306
307 updateVanillaFrames({ audioPacket, numFrames, readIndex }) {
308 // aways dec pending Frames even for empty ones
309 this.pendingFrames -= numFrames;
310 if (audioPacket) {
311 for (let channelIndex = 0; channelIndex < this.outputsCount; ++channelIndex) {
312 let hasLeftover = false;
313 let framesLeft;
314 const nextReadIndex = (readIndex + numFrames) % RING_BUFFER_SIZE;
315 if (nextReadIndex < readIndex) {
316 hasLeftover = true;
317 framesLeft = RING_BUFFER_SIZE - readIndex;
318 }
319
320 if (!hasLeftover) {
321 this.vanillaOutputChannels[channelIndex].set(audioPacket[channelIndex], readIndex);
322 } else {
323 this.vanillaOutputChannels[channelIndex].set(
324 audioPacket[channelIndex].subarray(0, framesLeft),
325 readIndex,
326 );
327 this.vanillaOutputChannels[channelIndex].set(
328 audioPacket[channelIndex].subarray(framesLeft),
329 0,
330 );
331 }
332 }
333 this.vanillaAvailableFrames += numFrames;
334 if (!this.vanillaFirstTransferDone) {
335 this.vanillaFirstTransferDone = true;
336 }
337 }
338 }
339
340 pause() {
341 this.isPaused = true;
342 this.workerMessagePort.broadcastPlayState("realtimePerformancePaused");
343 }
344
345 resume() {
346 this.isPaused = false;
347 this.workerMessagePort.broadcastPlayState("realtimePerformanceResumed");
348 }
349
350 process(inputs, outputs) {
351 return this.isPaused || !this.messagePortsReady ? true : this.actualProcess(inputs, outputs);
352 }
353 }
354
355 function initMessagePort({ port }) {
356 log(`initMessagePort in worker`)();
357 const workerMessagePort = new MessagePortState();
358 workerMessagePort.post = (logMessage) => port.postMessage({ log: logMessage });
359 workerMessagePort.broadcastPlayState = (playStateChange) => port.postMessage({ playStateChange });
360
361 workerMessagePort.ready = true;
362 return workerMessagePort;
363 }
364
365 function initRequestPort({ requestPort, audioNode }) {
366 log(`initRequestPort in worker`)();
367 requestPort.addEventListener("message", (requestPortEvent) => {
368 const { audioPacket, readIndex, numFrames } = requestPortEvent.data;
369 audioNode.updateVanillaFrames({ audioPacket, numFrames, readIndex });
370 });
371 const requestFrames = (arguments_) => requestPort.postMessage(arguments_);
372
373 requestPort.start();
374 return {
375 requestFrames,
376 ready: true,
377 };
378 }
379
380 function initAudioInputPort({ inputPort }) {
381 log(`initAudioInputPort in worker`)();
382 return {
383 ready: false,
384 transferInputFrames: (frames) => inputPort.postMessage(frames),
385 };
386 }
387
388 const initialize = async ({ contextUid, inputPort, messagePort, requestPort }) => {
389 const nodeUid = `${contextUid}Node`;
390 const audioNode = activeNodes.get(nodeUid);
391 const workerMessagePort = initMessagePort({ port: messagePort });
392
393 const audioInputPort = initAudioInputPort({ inputPort });
394 const audioFramePort = initRequestPort({ requestPort, audioNode });
395 let startPromiz;
396 const startPromise = new Promise((resolve) => {
397 startPromiz = resolve;
398 });
399 audioNode.initCallbacks({ workerMessagePort, audioInputPort, audioFramePort, startPromiz });
400 await startPromise;
401 };
402
403 registerProcessor("csound-worklet-processor", CsoundWorkletProcessor);