| 1 |
import { logCommonUtils as log } from "../logger.js"; |
| 2 |
|
| 3 |
export const handleCsoundStart = |
| 4 |
(workerMessagePort, libraryCsound, wasi, createRealtimeAudioThread, renderFunction) => |
| 5 |
(arguments_) => { |
| 6 |
const { csound } = arguments_; |
| 7 |
|
| 8 |
// If no orchestra was given, we assume realtime daemon mode |
| 9 |
// otherwise we'll end up rendering forever within the while loop |
| 10 |
const shouldDemonize = libraryCsound.csoundShouldDaemonize(csound) === 1; |
| 11 |
|
| 12 |
if (shouldDemonize) { |
| 13 |
libraryCsound.csoundSetOption(csound, "--daemon"); |
| 14 |
libraryCsound.csoundSetOption(csound, "-odac"); |
| 15 |
} |
| 16 |
const startError = libraryCsound.csoundStart(csound); |
| 17 |
|
| 18 |
const outputName = libraryCsound.csoundGetOutputName(csound) || "test.wav"; |
| 19 |
log(`handleCsoundStart: actual csoundStart result ${startError}, outputName: ${outputName}`)(); |
| 20 |
if (startError !== 0) { |
| 21 |
workerMessagePort.post( |
| 22 |
`error: csoundStart failed while trying to render ${outputName},` + |
| 23 |
" look out for errors in options and syntax", |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
setTimeout(() => { |
| 28 |
const isRequestingRtMidiInput = libraryCsound._isRequestingRtMidiInput(csound); |
| 29 |
const isExpectingRealtimeOutput = |
| 30 |
shouldDemonize || isRequestingRtMidiInput || outputName.includes("dac"); |
| 31 |
|
| 32 |
if (isExpectingRealtimeOutput) { |
| 33 |
createRealtimeAudioThread(arguments_); |
| 34 |
} else { |
| 35 |
// Do rendering |
| 36 |
workerMessagePort.broadcastPlayState("renderStarted"); |
| 37 |
if (renderFunction) { |
| 38 |
renderFunction(arguments_); |
| 39 |
} else { |
| 40 |
// eslint-disable-next-line no-empty |
| 41 |
while (libraryCsound.csoundPerformKsmps(csound) === 0) {} |
| 42 |
} |
| 43 |
} |
| 44 |
}, 0); |
| 45 |
|
| 46 |
return startError; |
| 47 |
}; |
| 48 |
|
| 49 |
export const instantiateAudioPacket = (numberChannels, numberFrames) => { |
| 50 |
const channels = []; |
| 51 |
for (let chn = 0; chn < numberChannels; chn++) { |
| 52 |
channels.push(new Float64Array(numberFrames)); |
| 53 |
} |
| 54 |
return channels; |
| 55 |
}; |
| 56 |
|
| 57 |
export const renderFunction = |
| 58 |
({ libraryCsound, workerMessagePort, wasi }) => |
| 59 |
async ({ csound }) => { |
| 60 |
const kr = libraryCsound.csoundGetKr(csound); |
| 61 |
let lastResult = 0; |
| 62 |
let cnt = 0; |
| 63 |
|
| 64 |
while ( |
| 65 |
(workerMessagePort.vanillaWorkerState === "renderStarted" || |
| 66 |
workerMessagePort.workerState === "renderStarted") && |
| 67 |
lastResult === 0 |
| 68 |
) { |
| 69 |
lastResult = libraryCsound.csoundPerformKsmps(csound); |
| 70 |
cnt += 1; |
| 71 |
|
| 72 |
if (typeof setTimeout === "function" && lastResult === 0 && cnt % kr === 0) { |
| 73 |
// this is immediately executed, but allows events to be picked up |
| 74 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 75 |
} |
| 76 |
} |
| 77 |
workerMessagePort.broadcastPlayState("renderEnded"); |
| 78 |
}; |