| 1 |
import * as Comlink from "comlink/dist/esm/comlink.mjs"; |
| 2 |
import { logVANMain as log } from "../logger"; |
| 3 |
import { api as API } from "../libcsound"; |
| 4 |
import { isEmpty } from "rambda/dist/rambda.esm.js"; |
| 5 |
import { csoundApiRename, fetchPlugins, makeProxyCallback, stopableStates } from "../utils"; |
| 6 |
import { IPCMessagePorts, messageEventHandler } from "./messages.main"; |
| 7 |
import { EventPromises } from "../utils/event-promises"; |
| 8 |
import { PublicEventAPI } from "../events"; |
| 9 |
|
| 10 |
const VanillaWorker = goog.require("worker.vanilla"); |
| 11 |
|
| 12 |
class VanillaWorkerMainThread { |
| 13 |
constructor({ |
| 14 |
audioContext, |
| 15 |
audioWorker, |
| 16 |
audioContextIsProvided, |
| 17 |
inputChannelCount, |
| 18 |
outputChannelCount, |
| 19 |
}) { |
| 20 |
this.ipcMessagePorts = new IPCMessagePorts(); |
| 21 |
this.eventPromises = new EventPromises(); |
| 22 |
this.publicEvents = new PublicEventAPI(this); |
| 23 |
|
| 24 |
audioWorker.ipcMessagePorts = this.ipcMessagePorts; |
| 25 |
audioWorker.csoundWorkerMain = this; |
| 26 |
audioWorker.publicEvents = this.publicEvents; |
| 27 |
|
| 28 |
this.audioWorker = audioWorker; |
| 29 |
this.audioContextIsProvided = audioContextIsProvided; |
| 30 |
|
| 31 |
if (audioContextIsProvided) { |
| 32 |
this.sampleRate = audioContext.sampleRate; |
| 33 |
} |
| 34 |
if (inputChannelCount) { |
| 35 |
this.inputChannelCount = inputChannelCount; |
| 36 |
} |
| 37 |
|
| 38 |
if (outputChannelCount) { |
| 39 |
this.outputChannelCount = outputChannelCount; |
| 40 |
} |
| 41 |
|
| 42 |
this.exportApi = {}; |
| 43 |
this.csoundInstance = undefined; |
| 44 |
this.currentPlayState = undefined; |
| 45 |
// this.messageCallbacks = []; |
| 46 |
this.midiPortStarted = false; |
| 47 |
this.onPlayStateChange = this.onPlayStateChange.bind(this); |
| 48 |
} |
| 49 |
|
| 50 |
async terminateInstance() { |
| 51 |
if (this.csoundWorker) { |
| 52 |
this.csoundWorker.terminate(); |
| 53 |
delete this.csoundWorker; |
| 54 |
} |
| 55 |
if (this.audioWorker && this.audioWorker.terminateInstance) { |
| 56 |
await this.audioWorker.terminateInstance(); |
| 57 |
delete this.audioWorker.terminateInstance; |
| 58 |
} |
| 59 |
if (this.proxyPort) { |
| 60 |
this.proxyPort[Comlink.releaseProxy](); |
| 61 |
delete this.proxyPort; |
| 62 |
} |
| 63 |
if (this.publicEvents) { |
| 64 |
this.publicEvents.terminateInstance(); |
| 65 |
} |
| 66 |
Object.keys(this.exportApi).forEach((key) => delete this.exportApi[key]); |
| 67 |
Object.keys(this).forEach((key) => delete this[key]); |
| 68 |
} |
| 69 |
|
| 70 |
get api() { |
| 71 |
return this.exportApi; |
| 72 |
} |
| 73 |
|
| 74 |
handleMidiInput({ data: payload }) { |
| 75 |
this.ipcMessagePorts.csoundMainRtMidiPort.postMessage && |
| 76 |
this.ipcMessagePorts.csoundMainRtMidiPort.postMessage(payload); |
| 77 |
} |
| 78 |
|
| 79 |
async prepareRealtimePerformance() { |
| 80 |
if (!this.csoundInstance) { |
| 81 |
console.error(`fatal error: csound instance not found?`); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
this.audioWorker.sampleRate = await this.exportApi.getSr(this.csoundInstance); |
| 86 |
const inputName = await this.exportApi.getInputName(this.csoundInstance); |
| 87 |
this.audioWorker.isRequestingInput = inputName.includes("adc"); |
| 88 |
this.audioWorker.isRequestingMidi = await this.exportApi._isRequestingRtMidiInput( |
| 89 |
this.csoundInstance, |
| 90 |
); |
| 91 |
this.audioWorker.outputsCount = await this.exportApi.getNchnls(this.csoundInstance); |
| 92 |
// TODO fix upstream: await this.exportApi.csoundGetNchnlsInput(this.csound); |
| 93 |
|
| 94 |
this.audioWorker.inputsCount = this.audioWorker.isRequestingInput ? 1 : 0; |
| 95 |
// if (this.audioWorker.scriptProcessorNode) { |
| 96 |
// this.audioWorker.softwareBufferSize *= 2; |
| 97 |
// } |
| 98 |
|
| 99 |
log(`vars for rtPerf set`)(); |
| 100 |
} |
| 101 |
|
| 102 |
async onPlayStateChange(newPlayState) { |
| 103 |
if (!this.publicEvents) { |
| 104 |
// prevent error after termination |
| 105 |
return; |
| 106 |
} |
| 107 |
this.currentPlayState = newPlayState; |
| 108 |
|
| 109 |
switch (newPlayState) { |
| 110 |
case "realtimePerformanceStarted": { |
| 111 |
log(`event: realtimePerformanceStarted from worker, now preparingRT..`)(); |
| 112 |
await this.prepareRealtimePerformance(); |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
case "realtimePerformanceEnded": { |
| 117 |
log(`event: realtimePerformanceEnded`)(); |
| 118 |
// a noop if the stop promise already exists |
| 119 |
this.eventPromises.createStopPromise(); |
| 120 |
this.midiPortStarted = false; |
| 121 |
this.publicEvents.triggerRealtimePerformanceEnded(this); |
| 122 |
await this.eventPromises.releaseStopPromise(); |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
case "renderStarted": { |
| 127 |
await this.eventPromises.releaseStartPromise(); |
| 128 |
this.publicEvents.triggerRenderStarted(this); |
| 129 |
break; |
| 130 |
} |
| 131 |
case "renderEnded": { |
| 132 |
log(`event: renderEnded received, beginning cleanup`)(); |
| 133 |
this.publicEvents.triggerRenderEnded(this); |
| 134 |
await this.eventPromises.releaseStopPromise(); |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
default: { |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// forward the message from worker to the audioWorker |
| 144 |
if (!this.audioWorker.ipcMessagePorts) { |
| 145 |
this.audioWorker.ipcMessagePorts = this.ipcMessagePorts; |
| 146 |
} |
| 147 |
await this.audioWorker.onPlayStateChange(newPlayState); |
| 148 |
} |
| 149 |
|
| 150 |
async csoundPause() { |
| 151 |
if (this.eventPromises.isWaiting("pause")) { |
| 152 |
return -1; |
| 153 |
} else { |
| 154 |
this.eventPromises.createPausePromise(); |
| 155 |
|
| 156 |
this.audioWorker && typeof this.audioWorker.workletProxy !== "undefined" |
| 157 |
? await this.audioWorker.workletProxy.pause() |
| 158 |
: await this.audioWorker.onPlayStateChange("realtimePerformancePaused"); |
| 159 |
|
| 160 |
await this.eventPromises.waitForPause(); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
async csoundResume() { |
| 166 |
if (this.eventPromises.isWaiting("resume")) { |
| 167 |
return -1; |
| 168 |
} else { |
| 169 |
this.eventPromises.createResumePromise(); |
| 170 |
this.audioWorker && typeof this.audioWorker.workletProxy !== "undefined" |
| 171 |
? await this.audioWorker.workletProxy.resume() |
| 172 |
: await this.audioWorker.onPlayStateChange("realtimePerformanceResumed"); |
| 173 |
|
| 174 |
await this.eventPromises.waitForResume(); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
async initialize({ wasmDataURI, withPlugins }) { |
| 180 |
const wasmBytes = wasmDataURI(); |
| 181 |
if (typeof this.audioWorker.initIframe === "function") { |
| 182 |
await this.audioWorker.initIframe(); |
| 183 |
} |
| 184 |
|
| 185 |
if (withPlugins && !isEmpty(withPlugins)) { |
| 186 |
withPlugins = await fetchPlugins(withPlugins); |
| 187 |
} |
| 188 |
log(`vanilla.main: initialize`)(); |
| 189 |
this.csoundWorker = this.csoundWorker || new Worker(VanillaWorker()); |
| 190 |
this.ipcMessagePorts.mainMessagePort.addEventListener("message", messageEventHandler(this)); |
| 191 |
this.ipcMessagePorts.mainMessagePort2.addEventListener("message", messageEventHandler(this)); |
| 192 |
this.ipcMessagePorts.mainMessagePort.start(); |
| 193 |
|
| 194 |
const proxyPort = Comlink.wrap(this.csoundWorker); |
| 195 |
this.proxyPort = proxyPort; |
| 196 |
|
| 197 |
this.csoundInstance = await proxyPort.initialize( |
| 198 |
Comlink.transfer( |
| 199 |
{ |
| 200 |
wasmDataURI: wasmBytes, |
| 201 |
messagePort: this.ipcMessagePorts.workerMessagePort, |
| 202 |
requestPort: this.ipcMessagePorts.csoundWorkerFrameRequestPort, |
| 203 |
audioInputPort: this.ipcMessagePorts.csoundWorkerAudioInputPort, |
| 204 |
rtmidiPort: this.ipcMessagePorts.csoundWorkerRtMidiPort, |
| 205 |
// these values are only set if the user provided them |
| 206 |
// during init or by passing audioContext |
| 207 |
sampleRate: this.sampleRate, |
| 208 |
inputChannelCount: this.inputChannelCount, |
| 209 |
outputChannelCount: this.outputChannelCount, |
| 210 |
withPlugins, |
| 211 |
}, |
| 212 |
[ |
| 213 |
wasmBytes, |
| 214 |
this.ipcMessagePorts.workerMessagePort, |
| 215 |
this.ipcMessagePorts.csoundWorkerFrameRequestPort, |
| 216 |
this.ipcMessagePorts.csoundWorkerAudioInputPort, |
| 217 |
this.ipcMessagePorts.csoundWorkerRtMidiPort, |
| 218 |
], |
| 219 |
), |
| 220 |
); |
| 221 |
|
| 222 |
this.exportApi.pause = this.csoundPause.bind(this); |
| 223 |
this.exportApi.resume = this.csoundResume.bind(this); |
| 224 |
this.exportApi.terminateInstance = this.terminateInstance.bind(this); |
| 225 |
|
| 226 |
this.exportApi.getAudioContext = async () => this.audioWorker.audioContext; |
| 227 |
|
| 228 |
this.exportApi.getNode = async () => { |
| 229 |
const maybeNode = this.audioWorker.audioWorkletNode; |
| 230 |
if (maybeNode) { |
| 231 |
return maybeNode; |
| 232 |
} else { |
| 233 |
const node = await new Promise((resolve) => { |
| 234 |
this.exportApi.once("onAudioNodeCreated", resolve); |
| 235 |
}); |
| 236 |
return node; |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
this.exportApi = this.publicEvents.decorateAPI(this.exportApi); |
| 241 |
this.exportApi.enableAudioInput = () => |
| 242 |
console.warn( |
| 243 |
`enableAudioInput was ignored: please use -iadc option before calling start with useWorker=true`, |
| 244 |
); |
| 245 |
|
| 246 |
// the default message listener |
| 247 |
this.exportApi.addListener("message", console.log); |
| 248 |
|
| 249 |
for (const apiK of Object.keys(API)) { |
| 250 |
const reference = API[apiK]; |
| 251 |
const proxyCallback = makeProxyCallback( |
| 252 |
proxyPort, |
| 253 |
this.csoundInstance, |
| 254 |
apiK, |
| 255 |
this.currentPlayState, |
| 256 |
); |
| 257 |
|
| 258 |
switch (apiK) { |
| 259 |
case "csoundCreate": { |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
case "csoundStart": { |
| 264 |
const csoundStart = async function () { |
| 265 |
if (this.eventPromises.isWaiting("start")) { |
| 266 |
return -1; |
| 267 |
} else { |
| 268 |
this.eventPromises.createStartPromise(); |
| 269 |
|
| 270 |
const startResult = await proxyCallback({ |
| 271 |
csound: this.csoundInstance, |
| 272 |
}); |
| 273 |
await this.eventPromises.waitForStart(); |
| 274 |
|
| 275 |
return startResult; |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
csoundStart.toString = () => reference.toString(); |
| 280 |
this.exportApi.start = csoundStart.bind(this); |
| 281 |
break; |
| 282 |
} |
| 283 |
|
| 284 |
case "csoundStop": { |
| 285 |
const csoundStop = async function () { |
| 286 |
if (this.eventPromises.isWaiting("stop")) { |
| 287 |
return -1; |
| 288 |
} else { |
| 289 |
this.eventPromises.createStopPromise(); |
| 290 |
this.ipcMessagePorts.mainMessagePort.postMessage({ |
| 291 |
newPlayState: |
| 292 |
this.currentPlayState === "renderStarted" |
| 293 |
? "renderEnded" |
| 294 |
: "realtimePerformanceEnded", |
| 295 |
}); |
| 296 |
|
| 297 |
await this.eventPromises.waitForStop(); |
| 298 |
return 0; |
| 299 |
} |
| 300 |
}; |
| 301 |
this.exportApi.stop = csoundStop.bind(this); |
| 302 |
csoundStop.toString = reference.toString; |
| 303 |
break; |
| 304 |
} |
| 305 |
|
| 306 |
case "csoundReset": { |
| 307 |
const csoundReset = async () => { |
| 308 |
// no start = noReset |
| 309 |
if (!this.currentPlayState) { |
| 310 |
return; |
| 311 |
} |
| 312 |
if (this.eventPromises.isWaiting("reset")) { |
| 313 |
return -1; |
| 314 |
} else { |
| 315 |
if (stopableStates.has(this.currentPlayState)) { |
| 316 |
await this.exportApi.stop(); |
| 317 |
} |
| 318 |
const resetResult = await proxyCallback([]); |
| 319 |
if (!this.audioContextIsProvided) { |
| 320 |
await this.audioWorker.terminateInstance(); |
| 321 |
delete this.audioWorker.audioContext; |
| 322 |
} |
| 323 |
|
| 324 |
this.ipcMessagePorts.restartAudioWorkerPorts(); |
| 325 |
|
| 326 |
return resetResult; |
| 327 |
} |
| 328 |
}; |
| 329 |
this.exportApi.reset = csoundReset.bind(this); |
| 330 |
csoundReset.toString = reference.toString; |
| 331 |
break; |
| 332 |
} |
| 333 |
|
| 334 |
case "fs": { |
| 335 |
this.exportApi.fs = {}; |
| 336 |
Object.keys(reference).forEach((method) => { |
| 337 |
const proxyFsCallback = makeProxyCallback( |
| 338 |
proxyPort, |
| 339 |
this.csoundInstance, |
| 340 |
method, |
| 341 |
this.currentPlayState, |
| 342 |
); |
| 343 |
proxyFsCallback.toString = reference[method].toString; |
| 344 |
this.exportApi.fs[method] = proxyFsCallback; |
| 345 |
}); |
| 346 |
break; |
| 347 |
} |
| 348 |
|
| 349 |
default: { |
| 350 |
proxyCallback.toString = reference.toString; |
| 351 |
this.exportApi[csoundApiRename(apiK)] = proxyCallback; |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
log(`exportAPI generated`)(); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
export default VanillaWorkerMainThread; |