| 1 |
import * as Comlink from "comlink/dist/esm/comlink.mjs"; |
| 2 |
import { instantiateAudioPacket } from "./common.utils"; |
| 3 |
import MessagePortState from "../utils/message-port-state"; |
| 4 |
import { newAudioContext } from "../utils/new-audio-context"; |
| 5 |
import { logOldSpnWorker as log } from "../logger"; |
| 6 |
import { range } from "rambda/dist/rambda.esm.js"; |
| 7 |
|
| 8 |
let startPromize; |
| 9 |
|
| 10 |
const getAudioContext = (contextUid) => { |
| 11 |
return ( |
| 12 |
window[contextUid] || |
| 13 |
window.parent[`__csound_wasm_iframe_parent_${contextUid}`] || |
| 14 |
newAudioContext() |
| 15 |
); |
| 16 |
}; |
| 17 |
|
| 18 |
const closeAndDeleteContext = async (contextUid) => { |
| 19 |
const maybeContext1 = window[contextUid]; |
| 20 |
const maybeContext2 = window.parent[`__csound_wasm_iframe_parent_${contextUid}`]; |
| 21 |
if (maybeContext1) { |
| 22 |
if (maybeContext1.state !== "closed") { |
| 23 |
try { |
| 24 |
await maybeContext1.close(); |
| 25 |
} catch (error) {} |
| 26 |
} |
| 27 |
delete window[contextUid]; |
| 28 |
} |
| 29 |
if (maybeContext2) { |
| 30 |
// if false, then the above close statement should cut it |
| 31 |
if (maybeContext1 !== maybeContext2 && maybeContext2.state !== "closed") { |
| 32 |
try { |
| 33 |
await maybeContext2.close(); |
| 34 |
} catch (error) {} |
| 35 |
} |
| 36 |
|
| 37 |
delete window.parent[`__csound_wasm_iframe_parent_${contextUid}`]; |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
const getAudioNode = (contextUid) => { |
| 42 |
return ( |
| 43 |
window[contextUid + "Node"] || window.parent[`__csound_wasm_iframe_parent_${contextUid}Node`] |
| 44 |
); |
| 45 |
}; |
| 46 |
|
| 47 |
const setAudioNode = (contextUid, audioNode) => { |
| 48 |
if (window[contextUid]) { |
| 49 |
window[`${contextUid}Node`] = audioNode; |
| 50 |
} else { |
| 51 |
window.parent[`__csound_wasm_iframe_parent_${contextUid}Node`] = audioNode; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
const disconnectAudioNode = (contextUid) => { |
| 56 |
const audoNode = getAudioNode(contextUid); |
| 57 |
audoNode && audoNode.disconnect(); |
| 58 |
}; |
| 59 |
|
| 60 |
const deleteAudioNode = (contextUid) => { |
| 61 |
if (window[contextUid + "Node"]) { |
| 62 |
delete window[contextUid + "Node"]; |
| 63 |
} |
| 64 |
if (window.parent[`__csound_wasm_iframe_parent_${contextUid}Node`]) { |
| 65 |
delete window.parent[`__csound_wasm_iframe_parent_${contextUid}Node`]; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
const PERIODS = 4; |
| 70 |
const spnInstances = new Map(); |
| 71 |
|
| 72 |
class CsoundScriptNodeProcessor { |
| 73 |
constructor({ |
| 74 |
audioContext, |
| 75 |
contextUid, |
| 76 |
hardwareBufferSize, |
| 77 |
softwareBufferSize, |
| 78 |
inputsCount, |
| 79 |
outputsCount, |
| 80 |
sampleRate, |
| 81 |
audioContextIsProvided, |
| 82 |
autoConnect, |
| 83 |
}) { |
| 84 |
this.autoConnect = autoConnect; |
| 85 |
this.audioContextIsProvided = audioContextIsProvided; |
| 86 |
this.hardwareBufferSize = hardwareBufferSize; |
| 87 |
this.softwareBufferSize = softwareBufferSize; |
| 88 |
this.inputsCount = inputsCount; |
| 89 |
this.outputsCount = outputsCount; |
| 90 |
this.sampleRate = sampleRate; |
| 91 |
|
| 92 |
this.vanillaOutputChannels = []; |
| 93 |
this.vanillaInputChannels = []; |
| 94 |
this.vanillaOutputReadIndex = 0; |
| 95 |
this.vanillaInputReadIndex = 0; |
| 96 |
this.vanillaAvailableFrames = 0; |
| 97 |
this.pendingFrames = 0; |
| 98 |
|
| 99 |
this.vanillaInitialized = false; |
| 100 |
this.vanillaFirstTransferDone = false; |
| 101 |
this.vanillaInputChannels = instantiateAudioPacket(inputsCount, hardwareBufferSize); |
| 102 |
this.vanillaOutputChannels = instantiateAudioPacket(outputsCount, hardwareBufferSize); |
| 103 |
|
| 104 |
this.audioContext = audioContext; |
| 105 |
this.contextUid = contextUid; |
| 106 |
// this.nodeUid = `${contextUid}Node`; |
| 107 |
this.scriptNode = this.audioContext.createScriptProcessor( |
| 108 |
this.softwareBufferSize, |
| 109 |
inputsCount, |
| 110 |
outputsCount, |
| 111 |
); |
| 112 |
this.process = this.process.bind(this); |
| 113 |
const processor = this.process.bind(this); |
| 114 |
this.scriptNode.onaudioprocess = processor; |
| 115 |
|
| 116 |
if (this.autoConnect) { |
| 117 |
setAudioNode(contextUid, this.scriptNode.connect(this.audioContext.destination)); |
| 118 |
} else { |
| 119 |
setAudioNode(contextUid, this.scriptNode.context.destination); |
| 120 |
} |
| 121 |
|
| 122 |
this.updateVanillaFrames = this.updateVanillaFrames.bind(this); |
| 123 |
this.initCallbacks = this.initCallbacks.bind(this); |
| 124 |
} |
| 125 |
|
| 126 |
async initCallbacks({ workerMessagePort, transferInputFrames, requestPort }) { |
| 127 |
this.workerMessagePort = workerMessagePort; |
| 128 |
|
| 129 |
this.transferInputFrames = transferInputFrames; |
| 130 |
this.requestPort = requestPort; |
| 131 |
|
| 132 |
// Safari autoplay cancer :( |
| 133 |
if (this.audioContext.state === "suspended") { |
| 134 |
// firefox timing issue fix (starts suspended but goes then to running state) |
| 135 |
let retryCount = 0; |
| 136 |
while (retryCount < 100) { |
| 137 |
await new Promise((resolve) => setTimeout(resolve, 100)); |
| 138 |
if (this.audioContext.state === "suspended") { |
| 139 |
retryCount += 1; |
| 140 |
} else { |
| 141 |
return; |
| 142 |
} |
| 143 |
} |
| 144 |
this.workerMessagePort.broadcastPlayState("realtimePerformancePaused"); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
updateVanillaFrames({ audioPacket, numFrames, readIndex }) { |
| 149 |
// aways dec pending Frames even for empty ones |
| 150 |
this.pendingFrames -= numFrames; |
| 151 |
if (audioPacket) { |
| 152 |
for (let channelIndex = 0; channelIndex < this.outputsCount; ++channelIndex) { |
| 153 |
let hasLeftover = false; |
| 154 |
let framesLeft = numFrames; |
| 155 |
const nextReadIndex = (readIndex + numFrames) % this.hardwareBufferSize; |
| 156 |
if (nextReadIndex < readIndex) { |
| 157 |
hasLeftover = true; |
| 158 |
framesLeft = this.hardwareBufferSize - readIndex; |
| 159 |
} |
| 160 |
|
| 161 |
this.vanillaOutputChannels[channelIndex].set( |
| 162 |
audioPacket[channelIndex].subarray(0, framesLeft), |
| 163 |
readIndex, |
| 164 |
); |
| 165 |
|
| 166 |
if (hasLeftover) { |
| 167 |
this.vanillaOutputChannels[channelIndex].set( |
| 168 |
audioPacket[channelIndex].subarray(framesLeft), |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
this.vanillaAvailableFrames += numFrames; |
| 173 |
} |
| 174 |
if (!this.vanillaFirstTransferDone) { |
| 175 |
this.vanillaFirstTransferDone = true; |
| 176 |
if (startPromize) { |
| 177 |
startPromize(); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// try to do the same here as in Vanilla+Worklet |
| 183 |
process({ inputBuffer, outputBuffer }) { |
| 184 |
if (this.workerMessagePort.vanillaWorkerState === "realtimePerformanceEnded") { |
| 185 |
setPlayState({ contextUid: this.contextUid, newPlayState: "realtimePerformanceEnded" }); |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
if (!this.vanillaInitialized) { |
| 190 |
// this minimizes startup glitches |
| 191 |
const firstTransferSize = this.softwareBufferSize * PERIODS; |
| 192 |
|
| 193 |
this.requestPort.postMessage({ |
| 194 |
readIndex: 0, |
| 195 |
numFrames: firstTransferSize, |
| 196 |
}); |
| 197 |
|
| 198 |
this.pendingFrames += firstTransferSize; |
| 199 |
this.vanillaInitialized = true; |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
if (!this.vanillaFirstTransferDone) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
const writeableInputChannels = range(0, this.inputsCount).map((index) => |
| 208 |
inputBuffer.getChannelData(index), |
| 209 |
); |
| 210 |
const writeableOutputChannels = range(0, this.outputsCount).map((index) => |
| 211 |
outputBuffer.getChannelData(index), |
| 212 |
); |
| 213 |
|
| 214 |
const hasWriteableInputChannels = writeableInputChannels.length > 0; |
| 215 |
|
| 216 |
const nextOutputReadIndex = |
| 217 |
(this.vanillaOutputReadIndex + writeableOutputChannels[0].length) % this.hardwareBufferSize; |
| 218 |
|
| 219 |
const nextInputReadIndex = hasWriteableInputChannels |
| 220 |
? (this.vanillaInputReadIndex + writeableInputChannels[0].length) % this.hardwareBufferSize |
| 221 |
: 0; |
| 222 |
|
| 223 |
if ( |
| 224 |
this.workerMessagePort.vanillaWorkerState !== "realtimePerformanceStarted" && |
| 225 |
this.workerMessagePort.vanillaWorkerState !== "realtimePerformanceResumed" |
| 226 |
) { |
| 227 |
writeableOutputChannels.forEach((channelBuffer) => { |
| 228 |
channelBuffer.fill(0); |
| 229 |
}); |
| 230 |
return true; |
| 231 |
} else if (this.vanillaAvailableFrames >= writeableOutputChannels[0].length) { |
| 232 |
writeableOutputChannels.forEach((channelBuffer, channelIndex) => { |
| 233 |
channelBuffer.set( |
| 234 |
this.vanillaOutputChannels[channelIndex].subarray( |
| 235 |
this.vanillaOutputReadIndex, |
| 236 |
nextOutputReadIndex < this.vanillaOutputReadIndex |
| 237 |
? this.hardwareBufferSize |
| 238 |
: nextOutputReadIndex, |
| 239 |
), |
| 240 |
); |
| 241 |
}); |
| 242 |
|
| 243 |
if ( |
| 244 |
this.inputsCount > 0 && |
| 245 |
hasWriteableInputChannels && |
| 246 |
writeableInputChannels[0].length > 0 |
| 247 |
) { |
| 248 |
const inputBufferLength = this.softwareBufferSize * PERIODS; |
| 249 |
writeableInputChannels.forEach((channelBuffer, channelIndex) => { |
| 250 |
this.vanillaInputChannels[channelIndex].set(channelBuffer, this.vanillaInputReadIndex); |
| 251 |
}); |
| 252 |
if (nextInputReadIndex % inputBufferLength === 0) { |
| 253 |
const packet = []; |
| 254 |
const pastBufferBegin = |
| 255 |
(nextInputReadIndex === 0 ? this.hardwareBufferSize : nextInputReadIndex) - |
| 256 |
inputBufferLength; |
| 257 |
const thisBufferEnd = |
| 258 |
nextInputReadIndex === 0 ? this.hardwareBufferSize : nextInputReadIndex; |
| 259 |
this.vanillaInputChannels.forEach((channelBuffer) => { |
| 260 |
packet.push(channelBuffer.subarray(pastBufferBegin, thisBufferEnd)); |
| 261 |
}); |
| 262 |
this.transferInputFrames(packet); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
this.vanillaOutputReadIndex = nextOutputReadIndex; |
| 267 |
this.vanillaInputReadIndex = nextInputReadIndex; |
| 268 |
this.vanillaAvailableFrames -= writeableOutputChannels[0].length; |
| 269 |
this.bufferUnderrunCount = 0; |
| 270 |
} else { |
| 271 |
// minimize noise |
| 272 |
if (this.bufferUnderrunCount > 1 && this.bufferUnderrunCount < 12) { |
| 273 |
this.workerMessagePort.post("Buffer underrun"); |
| 274 |
this.bufferUnderrunCount += 1; |
| 275 |
} |
| 276 |
|
| 277 |
if (this.bufferUnderrunCount === 100) { |
| 278 |
// 100 buffer Underruns in a row |
| 279 |
// means a fatal situation and browser |
| 280 |
// may crash |
| 281 |
this.workerMessagePort.post("FATAL: 100 buffers failed in a row"); |
| 282 |
this.workerMessagePort.broadcastPlayState("realtimePerformanceEnded"); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if ( |
| 287 |
this.vanillaAvailableFrames < this.softwareBufferSize * PERIODS && |
| 288 |
this.pendingFrames < this.softwareBufferSize * PERIODS |
| 289 |
) { |
| 290 |
const futureOutputReadIndex = |
| 291 |
(this.vanillaAvailableFrames + nextOutputReadIndex + this.pendingFrames) % |
| 292 |
this.hardwareBufferSize; |
| 293 |
|
| 294 |
this.requestPort.postMessage({ |
| 295 |
readIndex: futureOutputReadIndex, |
| 296 |
numFrames: this.softwareBufferSize * PERIODS, |
| 297 |
}); |
| 298 |
this.pendingFrames += this.softwareBufferSize * PERIODS; |
| 299 |
} |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
} |
| 304 |
const initAudioInputPort = |
| 305 |
({ audioInputPort }) => |
| 306 |
(frames) => |
| 307 |
audioInputPort.postMessage(frames); |
| 308 |
|
| 309 |
const initMessagePort = ({ port }) => { |
| 310 |
const workerMessagePort = new MessagePortState(); |
| 311 |
workerMessagePort.post = (messageLog) => port.postMessage({ log: messageLog }); |
| 312 |
workerMessagePort.broadcastPlayState = (playStateChange) => { |
| 313 |
if ( |
| 314 |
workerMessagePort.vanillaWorkerState === "realtimePerformanceStarted" && |
| 315 |
playStateChange === "realtimePerformanceStarted" |
| 316 |
) { |
| 317 |
return; |
| 318 |
} |
| 319 |
port.postMessage({ playStateChange }); |
| 320 |
}; |
| 321 |
workerMessagePort.ready = true; |
| 322 |
return workerMessagePort; |
| 323 |
}; |
| 324 |
|
| 325 |
const initRequestPort = ({ requestPort, spnClassInstance }) => { |
| 326 |
requestPort.addEventListener("message", (requestPortEvent) => { |
| 327 |
const { audioPacket, readIndex, numFrames } = requestPortEvent.data; |
| 328 |
spnClassInstance.updateVanillaFrames({ audioPacket, numFrames, readIndex }); |
| 329 |
}); |
| 330 |
requestPort.start(); |
| 331 |
return requestPort; |
| 332 |
}; |
| 333 |
|
| 334 |
const setPlayState = async ({ contextUid, newPlayState }) => { |
| 335 |
const spnClassInstance = spnInstances.get(contextUid); |
| 336 |
if (!spnClassInstance) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
// unclear I know, but it's just here to imperatively |
| 341 |
// resume with audioplay policy in mind |
| 342 |
if (newPlayState === "resume") { |
| 343 |
spnClassInstance.audioContext.state === "suspended" && spnClassInstance.audioContext.resume(); |
| 344 |
if (spnClassInstance.audioContext.state === "running") { |
| 345 |
spnClassInstance.workerMessagePort.broadcastPlayState("realtimePerformanceResumed"); |
| 346 |
} |
| 347 |
return; |
| 348 |
} |
| 349 |
if (!spnClassInstance.workerMessagePort) { |
| 350 |
// perhaps we are rendering, so this is just ignored |
| 351 |
return; |
| 352 |
} |
| 353 |
if ( |
| 354 |
newPlayState === "realtimePerformanceEnded" && |
| 355 |
spnClassInstance.workerMessagePort.vanillaWorkerState !== "realtimePerformanceEnded" |
| 356 |
) { |
| 357 |
// ping-pong |
| 358 |
spnClassInstance.workerMessagePort.broadcastPlayState("realtimePerformanceEnded"); |
| 359 |
|
| 360 |
if (spnClassInstance.autoConnect) { |
| 361 |
disconnectAudioNode(contextUid); |
| 362 |
} |
| 363 |
deleteAudioNode(contextUid); |
| 364 |
spnInstances.delete(contextUid); |
| 365 |
if (spnClassInstance.autoConnect && !spnClassInstance.audioContextIsProvided) { |
| 366 |
closeAndDeleteContext(contextUid); |
| 367 |
} |
| 368 |
} else if (newPlayState === "realtimePerformanceResumed") { |
| 369 |
spnClassInstance.audioContext.state === "suspended" && spnClassInstance.audioContext.resume(); |
| 370 |
} |
| 371 |
|
| 372 |
spnClassInstance.workerMessagePort.vanillaWorkerState = newPlayState; |
| 373 |
}; |
| 374 |
|
| 375 |
const initialize = async ({ |
| 376 |
contextUid, |
| 377 |
hardwareBufferSize, |
| 378 |
softwareBufferSize, |
| 379 |
inputsCount, |
| 380 |
outputsCount, |
| 381 |
sampleRate, |
| 382 |
audioInputPort, |
| 383 |
messagePort, |
| 384 |
requestPort, |
| 385 |
audioContextIsProvided, |
| 386 |
autoConnect, |
| 387 |
initialPlayState, |
| 388 |
}) => { |
| 389 |
log("initializing old-spn worker in iframe")(); |
| 390 |
startPromize = undefined; |
| 391 |
const audioContext = getAudioContext(contextUid); |
| 392 |
|
| 393 |
const spnClassInstance = new CsoundScriptNodeProcessor({ |
| 394 |
audioContext, |
| 395 |
contextUid, |
| 396 |
hardwareBufferSize, |
| 397 |
softwareBufferSize, |
| 398 |
inputsCount, |
| 399 |
outputsCount, |
| 400 |
sampleRate, |
| 401 |
audioContextIsProvided, |
| 402 |
autoConnect, |
| 403 |
}); |
| 404 |
|
| 405 |
const workerMessagePort = initMessagePort({ port: messagePort }); |
| 406 |
const transferInputFrames = initAudioInputPort({ audioInputPort, spnClassInstance }); |
| 407 |
initRequestPort({ requestPort, spnClassInstance }); |
| 408 |
// don't await this promise! |
| 409 |
spnClassInstance.initCallbacks({ workerMessagePort, transferInputFrames, requestPort }); |
| 410 |
spnInstances.set(contextUid, spnClassInstance); |
| 411 |
|
| 412 |
if (initialPlayState === "realtimePerformanceStarted") { |
| 413 |
const startPromise = new Promise((resolve, reject) => { |
| 414 |
startPromize = resolve; |
| 415 |
setTimeout(() => { |
| 416 |
if (typeof startPromize === "function") { |
| 417 |
reject(new Error(`a call to start() timed out`)); |
| 418 |
startPromize = undefined; |
| 419 |
return -1; |
| 420 |
} |
| 421 |
// 10 second timeout |
| 422 |
}, 10 * 60 * 1000); |
| 423 |
}); |
| 424 |
return await startPromise; |
| 425 |
} |
| 426 |
}; |
| 427 |
|
| 428 |
Comlink.expose({ initialize, setPlayState }, Comlink.windowEndpoint(window.parent)); |