| 1 |
import { expose } from "comlink/dist/esm/comlink.mjs"; |
| 2 |
import MessagePortState from "../utils/message-port-state"; |
| 3 |
import libcsoundFactory from "../libcsound.js"; |
| 4 |
import loadWasm from "../module"; |
| 5 |
import { logSABWorker as log } from "../logger"; |
| 6 |
import { handleCsoundStart } from "../workers/common.utils"; |
| 7 |
import { |
| 8 |
AUDIO_STATE, |
| 9 |
RING_BUFFER_SIZE, |
| 10 |
MIDI_BUFFER_SIZE, |
| 11 |
MIDI_BUFFER_PAYLOAD_SIZE, |
| 12 |
initialSharedState, |
| 13 |
} from "../constants"; |
| 14 |
import { assoc, pipe } from "rambda/dist/rambda.esm.js"; |
| 15 |
|
| 16 |
/** |
| 17 |
* @type {Map.<LibcsoundUncloned>} |
| 18 |
*/ |
| 19 |
let combined; |
| 20 |
let pollPromise; |
| 21 |
let unlockPromise; |
| 22 |
|
| 23 |
const callUncloned = async (k, arguments_) => { |
| 24 |
const caller = combined.get(k); |
| 25 |
const returnValue = caller && caller.apply({}, arguments_ || []); |
| 26 |
return returnValue; |
| 27 |
}; |
| 28 |
|
| 29 |
const sabCreateRealtimeAudioThread = |
| 30 |
({ |
| 31 |
libraryCsound, |
| 32 |
callbacksRequest, |
| 33 |
releaseStop, |
| 34 |
releasePause, |
| 35 |
releaseResumed, |
| 36 |
wasm, |
| 37 |
wasi, |
| 38 |
workerMessagePort, |
| 39 |
}) => |
| 40 |
async ({ audioStateBuffer, audioStreamIn, audioStreamOut, midiBuffer, csound }) => { |
| 41 |
const audioStatePointer = new Int32Array(audioStateBuffer); |
| 42 |
|
| 43 |
// In case of multiple performances, let's reset the sab state |
| 44 |
initialSharedState.forEach((value, index) => { |
| 45 |
Atomics.store(audioStatePointer, index, value); |
| 46 |
}); |
| 47 |
|
| 48 |
// Prompt for midi-input on demand |
| 49 |
const isRequestingRtMidiInput = libraryCsound._isRequestingRtMidiInput(csound); |
| 50 |
|
| 51 |
// Prompt for microphone only on demand! |
| 52 |
const isExpectingInput = |
| 53 |
Atomics.load(audioStatePointer, AUDIO_STATE.NCHNLS_I) === 0 && |
| 54 |
libraryCsound.csoundGetInputName(csound).includes("adc"); |
| 55 |
|
| 56 |
// Store Csound AudioParams for upcoming performance |
| 57 |
const userProvidedNchnls = Atomics.load(audioStatePointer, AUDIO_STATE.NCHNLS); |
| 58 |
const userProvidedNchnlsIn = Atomics.load(audioStatePointer, AUDIO_STATE.NCHNLS_I); |
| 59 |
const userProvidedSr = Atomics.load(audioStatePointer, AUDIO_STATE.SAMPLE_RATE); |
| 60 |
|
| 61 |
userProvidedNchnls > -1 && |
| 62 |
libraryCsound.csoundSetOption(csound, `--nchnls=${userProvidedNchnls}`); |
| 63 |
userProvidedNchnlsIn > -1 && |
| 64 |
libraryCsound.csoundSetOption(csound, `--nchnls_i=${userProvidedNchnlsIn}`); |
| 65 |
userProvidedSr > -1 && libraryCsound.csoundSetOption(csound, `--sr=${userProvidedSr}`); |
| 66 |
|
| 67 |
const nchnls = libraryCsound.csoundGetNchnls(csound); |
| 68 |
|
| 69 |
const nchnlsInput = |
| 70 |
userProvidedNchnlsIn || isExpectingInput ? libraryCsound.csoundGetNchnlsInput(csound) : 0; |
| 71 |
const sampleRate = |
| 72 |
Atomics.load(audioStatePointer, AUDIO_STATE.SAMPLE_RATE) || libraryCsound.csoundGetSr(csound); |
| 73 |
|
| 74 |
// a final merge of user configuration and csound options |
| 75 |
Atomics.store(audioStatePointer, AUDIO_STATE.NCHNLS, nchnls); |
| 76 |
Atomics.store(audioStatePointer, AUDIO_STATE.NCHNLS_I, nchnlsInput); |
| 77 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_REQUESTING_MIC, isExpectingInput ? 1 : 0); |
| 78 |
Atomics.store(audioStatePointer, AUDIO_STATE.SAMPLE_RATE, libraryCsound.csoundGetSr(csound)); |
| 79 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_REQUESTING_RTMIDI, isRequestingRtMidiInput); |
| 80 |
|
| 81 |
const ksmps = libraryCsound.csoundGetKsmps(csound); |
| 82 |
Atomics.store(audioStatePointer, AUDIO_STATE.KSMPS, ksmps); |
| 83 |
|
| 84 |
const zeroDecibelFullScale = libraryCsound.csoundGet0dBFS(csound); |
| 85 |
|
| 86 |
// Get the Worklet channels |
| 87 |
const channelsOutput = []; |
| 88 |
const channelsInput = []; |
| 89 |
|
| 90 |
for (let channelIndex = 0; channelIndex < nchnls; ++channelIndex) { |
| 91 |
channelsOutput.push( |
| 92 |
new Float64Array(audioStreamOut, RING_BUFFER_SIZE * channelIndex, RING_BUFFER_SIZE), |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
for (let channelIndex = 0; channelIndex < nchnlsInput; ++channelIndex) { |
| 97 |
channelsInput.push( |
| 98 |
new Float64Array(audioStreamIn, RING_BUFFER_SIZE * channelIndex, RING_BUFFER_SIZE), |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
workerMessagePort.broadcastPlayState("realtimePerformanceStarted"); |
| 103 |
// Let's notify the audio-worker that performance has started |
| 104 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_PERFORMING, 1); |
| 105 |
|
| 106 |
log(`Atomic.wait started (thread is now locked)\n`)(); |
| 107 |
|
| 108 |
let firstRound = true; |
| 109 |
let lastReturn = 0; |
| 110 |
let currentCsoundBufferPos = 0; |
| 111 |
let currentInputReadIndex = 0; |
| 112 |
let currentOutputWriteIndex = 0; |
| 113 |
let waitResult; |
| 114 |
|
| 115 |
const maybeStop = (forceStop = false) => { |
| 116 |
if ( |
| 117 |
Atomics.load(audioStatePointer, AUDIO_STATE.STOP) === 1 || |
| 118 |
Atomics.load(audioStatePointer, AUDIO_STATE.IS_PERFORMING) !== 1 || |
| 119 |
lastReturn !== 0 || |
| 120 |
forceStop |
| 121 |
) { |
| 122 |
if (lastReturn === 0) { |
| 123 |
log(`calling csoundStop and one performKsmps to trigger endof logs`)(); |
| 124 |
// Trigger "performance ended" logs |
| 125 |
libraryCsound.csoundStop(csound); |
| 126 |
libraryCsound.csoundPerformKsmps(csound); |
| 127 |
} |
| 128 |
log(`triggering realtimePerformanceEnded event`)(); |
| 129 |
workerMessagePort.broadcastPlayState("realtimePerformanceEnded"); |
| 130 |
log(`End of realtimePerformance loop!`)(); |
| 131 |
releaseStop(); |
| 132 |
return true; |
| 133 |
} else { |
| 134 |
return false; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
let firstPerformKsmps = true; |
| 139 |
|
| 140 |
while ( |
| 141 |
!firstPerformKsmps || |
| 142 |
(waitResult = Atomics.wait(audioStatePointer, AUDIO_STATE.CSOUND_LOCK, 1, 10 * 1000)) |
| 143 |
) { |
| 144 |
if (waitResult === "timed-out") { |
| 145 |
maybeStop(true); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (firstRound) { |
| 150 |
firstRound = false; |
| 151 |
await new Promise((resolve) => { |
| 152 |
unlockPromise = resolve; |
| 153 |
workerMessagePort.broadcastSabUnlocked(); |
| 154 |
}); |
| 155 |
|
| 156 |
log(`Atomic.wait unlocked, performance started`)(); |
| 157 |
} |
| 158 |
|
| 159 |
if (Atomics.load(audioStatePointer, AUDIO_STATE.IS_PAUSED) === 1) { |
| 160 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 161 |
releasePause(); |
| 162 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 163 |
Atomics.wait(audioStatePointer, AUDIO_STATE.IS_PAUSED, 0); |
| 164 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 165 |
releaseResumed(); |
| 166 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 167 |
} |
| 168 |
|
| 169 |
if (maybeStop()) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
if (isRequestingRtMidiInput) { |
| 174 |
const availableMidiEvents = Atomics.load( |
| 175 |
audioStatePointer, |
| 176 |
AUDIO_STATE.AVAIL_RTMIDI_EVENTS, |
| 177 |
); |
| 178 |
if (availableMidiEvents > 0) { |
| 179 |
const rtmidiBufferIndex = Atomics.load(audioStatePointer, AUDIO_STATE.RTMIDI_INDEX); |
| 180 |
let absIndex = rtmidiBufferIndex; |
| 181 |
for (let index = 0; index < availableMidiEvents; index++) { |
| 182 |
absIndex = (rtmidiBufferIndex + MIDI_BUFFER_PAYLOAD_SIZE * index) % MIDI_BUFFER_SIZE; |
| 183 |
const status = Atomics.load(midiBuffer, absIndex); |
| 184 |
const data1 = Atomics.load(midiBuffer, absIndex + 1); |
| 185 |
const data2 = Atomics.load(midiBuffer, absIndex + 2); |
| 186 |
libraryCsound.csoundPushMidiMessage(csound, status, data1, data2); |
| 187 |
} |
| 188 |
|
| 189 |
Atomics.store( |
| 190 |
audioStatePointer, |
| 191 |
AUDIO_STATE.RTMIDI_INDEX, |
| 192 |
(absIndex + 1) % MIDI_BUFFER_SIZE, |
| 193 |
); |
| 194 |
Atomics.sub(audioStatePointer, AUDIO_STATE.AVAIL_RTMIDI_EVENTS, availableMidiEvents); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
const bufferLength = Atomics.load(audioStatePointer, AUDIO_STATE.BUFFER_LEN); |
| 199 |
const availableInputFrames = Atomics.load(audioStatePointer, AUDIO_STATE.AVAIL_IN_BUFS); |
| 200 |
|
| 201 |
const hasInput = availableInputFrames >= bufferLength; |
| 202 |
const inputBufferPtr = libraryCsound.csoundGetSpin(csound); |
| 203 |
const outputBufferPtr = libraryCsound.csoundGetSpout(csound); |
| 204 |
|
| 205 |
const csoundInputBuffer = |
| 206 |
hasInput && new Float64Array(wasm.wasi.memory.buffer, inputBufferPtr, ksmps * nchnlsInput); |
| 207 |
|
| 208 |
const csoundOutputBuffer = new Float64Array( |
| 209 |
wasm.wasi.memory.buffer, |
| 210 |
outputBufferPtr, |
| 211 |
ksmps * nchnls, |
| 212 |
); |
| 213 |
|
| 214 |
const framesRequested = Atomics.load(audioStatePointer, AUDIO_STATE.FRAMES_REQUESTED); |
| 215 |
|
| 216 |
for (let index = 0; index < framesRequested; index++) { |
| 217 |
if (currentCsoundBufferPos === 0) { |
| 218 |
if (lastReturn === 0) { |
| 219 |
lastReturn = libraryCsound.csoundPerformKsmps(csound); |
| 220 |
!firstPerformKsmps && Atomics.add(audioStatePointer, AUDIO_STATE.AVAIL_OUT_BUFS, ksmps); |
| 221 |
firstPerformKsmps = false; |
| 222 |
} else if (lastReturn !== 0) { |
| 223 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_PERFORMING, 0); |
| 224 |
maybeStop(true); |
| 225 |
return; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
channelsOutput.forEach((channel, channelIndex) => { |
| 230 |
channel[currentOutputWriteIndex] = |
| 231 |
(csoundOutputBuffer[currentCsoundBufferPos * nchnls + channelIndex] || 0) / |
| 232 |
zeroDecibelFullScale; |
| 233 |
}); |
| 234 |
|
| 235 |
if (hasInput) { |
| 236 |
channelsInput.forEach((channel, channelIndex) => { |
| 237 |
csoundInputBuffer[currentCsoundBufferPos * nchnlsInput + channelIndex] = |
| 238 |
(channel[currentInputReadIndex] || 0) * zeroDecibelFullScale; |
| 239 |
}); |
| 240 |
|
| 241 |
currentInputReadIndex = hasInput && (currentInputReadIndex + 1) % RING_BUFFER_SIZE; |
| 242 |
} |
| 243 |
|
| 244 |
currentOutputWriteIndex = (currentOutputWriteIndex + 1) % RING_BUFFER_SIZE; |
| 245 |
currentCsoundBufferPos = (currentCsoundBufferPos + 1) % ksmps; |
| 246 |
} |
| 247 |
|
| 248 |
hasInput && Atomics.sub(audioStatePointer, AUDIO_STATE.AVAIL_IN_BUFS, framesRequested); |
| 249 |
|
| 250 |
if ( |
| 251 |
Atomics.compareExchange(audioStatePointer, AUDIO_STATE.HAS_PENDING_CALLBACKS, 1, 0) === 1 |
| 252 |
) { |
| 253 |
await new Promise((resolve) => { |
| 254 |
pollPromise = resolve; |
| 255 |
callbacksRequest(); |
| 256 |
}); |
| 257 |
} |
| 258 |
|
| 259 |
if (maybeStop()) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
const readIndex = Atomics.load(audioStatePointer, AUDIO_STATE.OUTPUT_READ_INDEX); |
| 264 |
|
| 265 |
const ioDelay = |
| 266 |
currentOutputWriteIndex < readIndex |
| 267 |
? currentOutputWriteIndex + RING_BUFFER_SIZE - readIndex |
| 268 |
: currentOutputWriteIndex - readIndex; |
| 269 |
|
| 270 |
// 2048 is only an indicator of MAX latency |
| 271 |
const nextFramesRequested = Math.max(2048 - ioDelay, 0); |
| 272 |
|
| 273 |
Atomics.store(audioStatePointer, AUDIO_STATE.FRAMES_REQUESTED, nextFramesRequested); |
| 274 |
|
| 275 |
if (nextFramesRequested === 0) { |
| 276 |
await new Promise((resolve) => setTimeout(resolve, 1000 * (bufferLength / sampleRate))); |
| 277 |
} |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
const initMessagePort = ({ port }) => { |
| 282 |
const workerMessagePort = new MessagePortState(); |
| 283 |
workerMessagePort.post = (messageLog) => port.postMessage({ log: messageLog }); |
| 284 |
workerMessagePort.broadcastPlayState = (playStateChange) => port.postMessage({ playStateChange }); |
| 285 |
workerMessagePort.broadcastSabUnlocked = () => port.postMessage({ sabWorker: "unlocked" }); |
| 286 |
workerMessagePort.ready = true; |
| 287 |
return workerMessagePort; |
| 288 |
}; |
| 289 |
|
| 290 |
const initCallbackReplyPort = ({ port }) => { |
| 291 |
port.addEventListener("message", (event) => { |
| 292 |
if (event.data && event.data.unlock) { |
| 293 |
const unlockPromise_ = unlockPromise; |
| 294 |
unlockPromise = undefined; |
| 295 |
unlockPromise_ && unlockPromise_(); |
| 296 |
} else { |
| 297 |
const callbacks = event.data; |
| 298 |
const answers = callbacks.reduce((accumulator, { id, argumentz, apiKey }) => { |
| 299 |
try { |
| 300 |
const caller = combined.get(apiKey); |
| 301 |
const answer = caller && caller.apply({}, argumentz || []); |
| 302 |
accumulator.push({ id, answer }); |
| 303 |
} catch (error) { |
| 304 |
throw new Error(error); |
| 305 |
} |
| 306 |
return accumulator; |
| 307 |
}, []); |
| 308 |
port.postMessage(answers); |
| 309 |
const pollPromise_ = pollPromise; |
| 310 |
pollPromise = undefined; |
| 311 |
pollPromise_ && pollPromise_(callbacks); |
| 312 |
} |
| 313 |
}); |
| 314 |
port.start(); |
| 315 |
}; |
| 316 |
|
| 317 |
const renderFunction = |
| 318 |
({ |
| 319 |
libraryCsound, |
| 320 |
callbacksRequest, |
| 321 |
releaseStop, |
| 322 |
releasePause, |
| 323 |
releaseResumed, |
| 324 |
wasi, |
| 325 |
workerMessagePort, |
| 326 |
}) => |
| 327 |
async ({ audioStateBuffer, csound }) => { |
| 328 |
const audioStatePointer = new Int32Array(audioStateBuffer); |
| 329 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_RENDERING, 1); |
| 330 |
workerMessagePort.broadcastSabUnlocked(); |
| 331 |
|
| 332 |
while ( |
| 333 |
Atomics.load(audioStatePointer, AUDIO_STATE.STOP) !== 1 && |
| 334 |
libraryCsound.csoundPerformKsmps(csound) === 0 |
| 335 |
) { |
| 336 |
if (Atomics.load(audioStatePointer, AUDIO_STATE.IS_PAUSED) === 1) { |
| 337 |
releasePause(); |
| 338 |
// eslint-disable-next-line no-unused-expressions |
| 339 |
Atomics.wait(audioStatePointer, AUDIO_STATE.IS_PAUSED, 0); |
| 340 |
releaseResumed(); |
| 341 |
} |
| 342 |
if ( |
| 343 |
Atomics.compareExchange(audioStatePointer, AUDIO_STATE.HAS_PENDING_CALLBACKS, 1, 0) === 1 |
| 344 |
) { |
| 345 |
await new Promise((resolve) => { |
| 346 |
pollPromise = resolve; |
| 347 |
callbacksRequest(); |
| 348 |
}); |
| 349 |
} |
| 350 |
} |
| 351 |
Atomics.store(audioStatePointer, AUDIO_STATE.IS_RENDERING, 0); |
| 352 |
workerMessagePort.broadcastPlayState("renderEnded"); |
| 353 |
releaseStop(); |
| 354 |
}; |
| 355 |
|
| 356 |
const initialize = async ({ wasmDataURI, withPlugins = [], messagePort, callbackPort }) => { |
| 357 |
log(`initializing SABWorker and WASM`)(); |
| 358 |
const workerMessagePort = initMessagePort({ port: messagePort }); |
| 359 |
const callbacksRequest = () => callbackPort.postMessage("poll"); |
| 360 |
const releaseStop = () => callbackPort.postMessage("releaseStop"); |
| 361 |
const releasePause = () => callbackPort.postMessage("releasePause"); |
| 362 |
const releaseResumed = () => callbackPort.postMessage("releaseResumed"); |
| 363 |
|
| 364 |
initCallbackReplyPort({ port: callbackPort }); |
| 365 |
|
| 366 |
const [wasm, wasi] = await loadWasm({ |
| 367 |
wasmDataURI, |
| 368 |
withPlugins, |
| 369 |
messagePort: workerMessagePort, |
| 370 |
}); |
| 371 |
|
| 372 |
wasm.wasi = wasi; |
| 373 |
|
| 374 |
const libraryCsound = libcsoundFactory(wasm); |
| 375 |
|
| 376 |
const startHandler = (_, arguments_) => |
| 377 |
handleCsoundStart( |
| 378 |
workerMessagePort, |
| 379 |
libraryCsound, |
| 380 |
wasi, |
| 381 |
sabCreateRealtimeAudioThread({ |
| 382 |
libraryCsound, |
| 383 |
callbacksRequest, |
| 384 |
wasm, |
| 385 |
workerMessagePort, |
| 386 |
releaseStop, |
| 387 |
releasePause, |
| 388 |
releaseResumed, |
| 389 |
}), |
| 390 |
renderFunction({ |
| 391 |
libraryCsound, |
| 392 |
callbacksRequest, |
| 393 |
workerMessagePort, |
| 394 |
wasi, |
| 395 |
releaseStop, |
| 396 |
releasePause, |
| 397 |
releaseResumed, |
| 398 |
}), |
| 399 |
)(arguments_); |
| 400 |
|
| 401 |
const allAPI = pipe(assoc("csoundStart", startHandler), assoc("wasm", wasm))(libraryCsound); |
| 402 |
combined = new Map(Object.entries(allAPI)); |
| 403 |
|
| 404 |
libraryCsound.csoundInitialize(0); |
| 405 |
const csoundInstance = libraryCsound.csoundCreate(); |
| 406 |
|
| 407 |
return csoundInstance; |
| 408 |
}; |
| 409 |
|
| 410 |
export const sabWorker = { initialize, callUncloned }; |
| 411 |
|
| 412 |
expose({ initialize: sabWorker.initialize, callUncloned: sabWorker.callUncloned }); |