| 1 |
import * as Comlink from "comlink/dist/esm/comlink.mjs"; |
| 2 |
import { logWorkletMain as log } from "../logger"; |
| 3 |
import { WebkitAudioContext } from "../utils"; |
| 4 |
import { requestMidi } from "../utils/request-midi"; |
| 5 |
import { messageEventHandler } from "./messages.main"; |
| 6 |
|
| 7 |
const WorkletWorker = goog.require("worker.worklet"); |
| 8 |
|
| 9 |
let UID = 0; |
| 10 |
|
| 11 |
class AudioWorkletMainThread { |
| 12 |
constructor({ audioContext, audioContextIsProvided, autoConnect }) { |
| 13 |
this.autoConnect = autoConnect; |
| 14 |
this.audioContextIsProvided = audioContextIsProvided; |
| 15 |
this.ipcMessagePorts = undefined; |
| 16 |
this.audioContext = audioContext; |
| 17 |
this.audioWorkletNode = undefined; |
| 18 |
this.currentPlayState = undefined; |
| 19 |
this.csoundWorkerMain = undefined; |
| 20 |
this.workletProxy = undefined; |
| 21 |
|
| 22 |
// never default these, get it from |
| 23 |
// csound-worker before starting |
| 24 |
this.ksmps = undefined; |
| 25 |
this.sampleRate = undefined; |
| 26 |
this.inputsCount = undefined; |
| 27 |
this.outputsCount = undefined; |
| 28 |
this.hardwareBufferSize = undefined; |
| 29 |
this.softwareBufferSize = undefined; |
| 30 |
|
| 31 |
this.initialize = this.initialize.bind(this); |
| 32 |
this.onPlayStateChange = this.onPlayStateChange.bind(this); |
| 33 |
this.terminateInstance = this.terminateInstance.bind(this); |
| 34 |
this.createWorkletNode = this.createWorkletNode.bind(this); |
| 35 |
log("AudioWorkletMainThread was constructed")(); |
| 36 |
} |
| 37 |
|
| 38 |
async terminateInstance() { |
| 39 |
if (this.audioWorkletNode) { |
| 40 |
this.audioWorkletNode.disconnect(); |
| 41 |
delete this.audioWorkletNode; |
| 42 |
} |
| 43 |
if (this.audioContext) { |
| 44 |
if (this.audioContext.state !== "closed") { |
| 45 |
try { |
| 46 |
await this.audioContext.close(); |
| 47 |
} catch {} |
| 48 |
} |
| 49 |
delete this.audioContext; |
| 50 |
} |
| 51 |
if (this.workletProxy) { |
| 52 |
this.workletProxy[Comlink.releaseProxy](); |
| 53 |
delete this.workletProxy; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
createWorkletNode(audioContext, inputsCount, contextUid) { |
| 58 |
const audioNode = new AudioWorkletNode(audioContext, "csound-worklet-processor", { |
| 59 |
inputChannelCount: inputsCount ? [inputsCount] : 0, |
| 60 |
outputChannelCount: [this.outputsCount || 2], |
| 61 |
processorOptions: { |
| 62 |
contextUid, |
| 63 |
isRequestingInput: this.isRequestingInput, |
| 64 |
inputsCount, |
| 65 |
outputsCount: this.outputsCount, |
| 66 |
ksmps: this.ksmps, |
| 67 |
maybeSharedArrayBuffer: |
| 68 |
this.csoundWorkerMain.hasSharedArrayBuffer && this.csoundWorkerMain.audioStatePointer, |
| 69 |
maybeSharedArrayBufferAudioIn: |
| 70 |
this.csoundWorkerMain.hasSharedArrayBuffer && this.csoundWorkerMain.audioStreamIn, |
| 71 |
maybeSharedArrayBufferAudioOut: |
| 72 |
this.csoundWorkerMain.hasSharedArrayBuffer && this.csoundWorkerMain.audioStreamOut, |
| 73 |
}, |
| 74 |
}); |
| 75 |
this.csoundWorkerMain.publicEvents.triggerOnAudioNodeCreated(audioNode); |
| 76 |
return audioNode; |
| 77 |
} |
| 78 |
|
| 79 |
async onPlayStateChange(newPlayState) { |
| 80 |
this.currentPlayState = newPlayState; |
| 81 |
|
| 82 |
switch (newPlayState) { |
| 83 |
case "realtimePerformanceStarted": { |
| 84 |
log("event received: realtimePerformanceStarted")(); |
| 85 |
await this.initialize(); |
| 86 |
|
| 87 |
if (this.csoundWorkerMain && this.csoundWorkerMain.eventPromises) { |
| 88 |
this.csoundWorkerMain.publicEvents && |
| 89 |
this.csoundWorkerMain.publicEvents.triggerRealtimePerformanceStarted(this); |
| 90 |
this.csoundWorkerMain.eventPromises && |
| 91 |
(await this.csoundWorkerMain.eventPromises.releaseStartPromise()); |
| 92 |
} |
| 93 |
break; |
| 94 |
} |
| 95 |
case "realtimePerformanceEnded": { |
| 96 |
log( |
| 97 |
"event received: realtimePerformanceEnded" + !this.csoundWorkerMain.hasSharedArrayBuffer |
| 98 |
? ` cleaning up ports` |
| 99 |
: "", |
| 100 |
)(); |
| 101 |
if ( |
| 102 |
!this.audioContextIsProvided && |
| 103 |
this.autoConnect && |
| 104 |
this.audioContext && |
| 105 |
this.audioContext.state !== "closed" |
| 106 |
) { |
| 107 |
try { |
| 108 |
await this.audioContext.close(); |
| 109 |
} catch {} |
| 110 |
} |
| 111 |
|
| 112 |
if (this.autoConnect && this.audioWorkletNode) { |
| 113 |
this.audioWorkletNode.disconnect(); |
| 114 |
delete this.audioWorkletNode; |
| 115 |
} |
| 116 |
if (this.workletProxy) { |
| 117 |
this.workletProxy[Comlink.releaseProxy](); |
| 118 |
delete this.workletProxy; |
| 119 |
} |
| 120 |
|
| 121 |
if (this.workletWorkerUrl) { |
| 122 |
(window.URL || window.webkitURL).revokeObjectURL(this.workletWorkerUrl); |
| 123 |
} |
| 124 |
|
| 125 |
this.audioWorkletNode && delete this.audioWorkletNode; |
| 126 |
this.currentPlayState = undefined; |
| 127 |
this.sampleRate = undefined; |
| 128 |
this.inputsCount = undefined; |
| 129 |
this.outputsCount = undefined; |
| 130 |
this.hardwareBufferSize = undefined; |
| 131 |
this.softwareBufferSize = undefined; |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
case "realtimePerformancePaused": { |
| 136 |
if (this.csoundWorkerMain && this.csoundWorkerMain.eventPromises) { |
| 137 |
this.csoundWorkerMain.publicEvents && |
| 138 |
this.csoundWorkerMain.publicEvents.triggerRealtimePerformancePaused(this); |
| 139 |
await this.csoundWorkerMain.eventPromises.releasePausePromise(); |
| 140 |
} |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
case "realtimePerformanceResumed": { |
| 145 |
if (this.csoundWorkerMain && this.csoundWorkerMain.eventPromises) { |
| 146 |
this.csoundWorkerMain.publicEvents && |
| 147 |
this.csoundWorkerMain.publicEvents.triggerRealtimePerformanceResumed(this); |
| 148 |
await this.csoundWorkerMain.eventPromises.releaseResumePromise(); |
| 149 |
} |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
default: { |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
async initialize() { |
| 160 |
if (!this.audioContext) { |
| 161 |
if (this.audioContextIsProvided) { |
| 162 |
console.error(`fatal: the provided AudioContext was undefined`); |
| 163 |
} |
| 164 |
this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate }); |
| 165 |
} |
| 166 |
|
| 167 |
if (this.audioContext.state === "closed") { |
| 168 |
if (this.audioContextIsProvided) { |
| 169 |
console.error(`fatal: the provided AudioContext was closed, falling back new AudioContext`); |
| 170 |
} |
| 171 |
this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate }); |
| 172 |
} |
| 173 |
|
| 174 |
if (this.sampleRate !== this.audioContext.sampleRate) { |
| 175 |
this.audioContext = new (WebkitAudioContext())({ sampleRate: this.sampleRate }); |
| 176 |
// if this.audioContextIsProvided is true |
| 177 |
// it should already be picked |
| 178 |
if (this.audioContextIsProvided) { |
| 179 |
console.error("Internal error: sample rate was ignored from provided audioContext"); |
| 180 |
} |
| 181 |
} |
| 182 |
this.workletWorkerUrl = WorkletWorker(); |
| 183 |
|
| 184 |
try { |
| 185 |
await this.audioContext.audioWorklet.addModule(this.workletWorkerUrl); |
| 186 |
} catch (error) { |
| 187 |
console.error("Error calling audioWorklet.addModule", error); |
| 188 |
} |
| 189 |
|
| 190 |
log("WorkletWorker module added")(); |
| 191 |
|
| 192 |
if (!this.csoundWorkerMain) { |
| 193 |
console.error(`fatal: worker not reachable from worklet-main thread`); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
const contextUid = `audioWorklet${UID}`; |
| 198 |
UID += 1; |
| 199 |
|
| 200 |
if (this.isRequestingMidi) { |
| 201 |
log("requesting for web-midi connection"); |
| 202 |
requestMidi({ |
| 203 |
onMidiMessage: this.csoundWorkerMain.handleMidiInput.bind(this.csoundWorkerMain), |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
let microphonePromise; |
| 208 |
|
| 209 |
if (this.isRequestingInput) { |
| 210 |
let resolveMicrophonePromise; |
| 211 |
microphonePromise = new Promise((resolve) => { |
| 212 |
resolveMicrophonePromise = resolve; |
| 213 |
}); |
| 214 |
const getUserMedia = |
| 215 |
typeof navigator.mediaDevices !== "undefined" |
| 216 |
? navigator.mediaDevices.getUserMedia |
| 217 |
: navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; |
| 218 |
|
| 219 |
const microphoneCallback = (stream) => { |
| 220 |
if (stream) { |
| 221 |
const liveInput = this.audioContext.createMediaStreamSource(stream); |
| 222 |
this.inputsCount = liveInput.channelCount; |
| 223 |
const newNode = this.createWorkletNode( |
| 224 |
this.audioContext, |
| 225 |
liveInput.channelCount, |
| 226 |
contextUid, |
| 227 |
); |
| 228 |
this.audioWorkletNode = newNode; |
| 229 |
if (this.autoConnect) { |
| 230 |
liveInput.connect(newNode).connect(this.audioContext.destination); |
| 231 |
} |
| 232 |
} else { |
| 233 |
// Continue as before if user cancels |
| 234 |
this.inputsCount = 0; |
| 235 |
const newNode = this.createWorkletNode(this.audioContext, 0, contextUid); |
| 236 |
this.audioWorkletNode = newNode; |
| 237 |
if (this.autoConnect) { |
| 238 |
this.audioWorkletNode.connect(this.audioContext.destination); |
| 239 |
} |
| 240 |
} |
| 241 |
resolveMicrophonePromise && resolveMicrophonePromise(); |
| 242 |
}; |
| 243 |
|
| 244 |
log("requesting microphone access")(); |
| 245 |
typeof navigator.mediaDevices !== "undefined" |
| 246 |
? getUserMedia |
| 247 |
.call(navigator.mediaDevices, { |
| 248 |
audio: { echoCancellation: false, sampleSize: 32 }, |
| 249 |
}) |
| 250 |
.then(microphoneCallback) |
| 251 |
.catch(console.error) |
| 252 |
: getUserMedia.call( |
| 253 |
navigator, |
| 254 |
{ |
| 255 |
audio: { |
| 256 |
optional: [{ echoCancellation: false, sampleSize: 32 }], |
| 257 |
}, |
| 258 |
}, |
| 259 |
microphoneCallback, |
| 260 |
console.error, |
| 261 |
); |
| 262 |
} else { |
| 263 |
const newNode = this.createWorkletNode(this.audioContext, 0, contextUid); |
| 264 |
this.audioWorkletNode = newNode; |
| 265 |
|
| 266 |
log("connecting Node to AudioContext destination")(); |
| 267 |
if (this.autoConnect) { |
| 268 |
this.audioWorkletNode.connect(this.audioContext.destination); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
microphonePromise && (await microphonePromise); |
| 273 |
this.workletProxy = Comlink.wrap(this.audioWorkletNode.port); |
| 274 |
|
| 275 |
this.ipcMessagePorts.mainMessagePortAudio.addEventListener( |
| 276 |
"message", |
| 277 |
messageEventHandler(this), |
| 278 |
); |
| 279 |
this.ipcMessagePorts.mainMessagePortAudio.start(); |
| 280 |
|
| 281 |
await this.workletProxy.initialize( |
| 282 |
Comlink.transfer( |
| 283 |
{ |
| 284 |
contextUid, |
| 285 |
messagePort: this.ipcMessagePorts.workerMessagePortAudio, |
| 286 |
requestPort: this.ipcMessagePorts.audioWorkerFrameRequestPort, |
| 287 |
inputPort: this.ipcMessagePorts.audioWorkerAudioInputPort, |
| 288 |
}, |
| 289 |
[ |
| 290 |
this.ipcMessagePorts.workerMessagePortAudio, |
| 291 |
this.ipcMessagePorts.audioWorkerFrameRequestPort, |
| 292 |
this.ipcMessagePorts.audioWorkerAudioInputPort, |
| 293 |
], |
| 294 |
), |
| 295 |
); |
| 296 |
|
| 297 |
log("initialization finished in main")(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
export default AudioWorkletMainThread; |