| 1 |
/* eslint-disable no-unused-vars */ |
| 2 |
import VanillaWorkerMainThread from "./mains/vanilla.main"; |
| 3 |
import unmuteIosAudio from "unmute-ios-audio/index.js"; |
| 4 |
import SharedArrayBufferMainThread from "./mains/sab.main"; |
| 5 |
import AudioWorkletMainThread from "./mains/worklet.main"; |
| 6 |
import ScriptProcessorNodeMainThread from "./mains/old-spn.main"; |
| 7 |
import ScriptProcessorNodeSingleThread from "./mains/spn.main"; |
| 8 |
import SingleThreadAudioWorkletMainThread from "./mains/worklet.singlethread.main"; |
| 9 |
import { logIndex as log } from "./logger"; |
| 10 |
import { |
| 11 |
areWorkletsSupported, |
| 12 |
isSafari, |
| 13 |
isSabSupported, |
| 14 |
isScriptProcessorNodeSupported, |
| 15 |
WebkitAudioContext, |
| 16 |
} from "./utils"; |
| 17 |
|
| 18 |
unmuteIosAudio(); |
| 19 |
|
| 20 |
const wasmDataURI = goog.require("binary.wasm"); |
| 21 |
|
| 22 |
/** |
| 23 |
* CsoundObj API. |
| 24 |
* @async |
| 25 |
* @export |
| 26 |
* @return {Promise.<CsoundObj|undefined>} |
| 27 |
* @suppress {misplacedTypeAnnotation} |
| 28 |
*/ |
| 29 |
const Csound = async function ({ |
| 30 |
audioContext, |
| 31 |
inputChannelCount, |
| 32 |
outputChannelCount, |
| 33 |
autoConnect = true, |
| 34 |
withPlugins = [], |
| 35 |
useWorker = false, |
| 36 |
useSAB = true, |
| 37 |
useSPN = false, |
| 38 |
} = {}) { |
| 39 |
const audioContextIsProvided = |
| 40 |
audioContext && WebkitAudioContext() && audioContext instanceof WebkitAudioContext(); |
| 41 |
|
| 42 |
if (!audioContextIsProvided) { |
| 43 |
// default to creating an audio context for SingleThread |
| 44 |
audioContext = audioContext || new (WebkitAudioContext())({ latencyHint: "interactive" }); |
| 45 |
} |
| 46 |
|
| 47 |
if (isSafari()) { |
| 48 |
audioContext.resume(); |
| 49 |
} |
| 50 |
|
| 51 |
const workletSupport = areWorkletsSupported(); |
| 52 |
const spnSupport = isScriptProcessorNodeSupported(); |
| 53 |
|
| 54 |
// SingleThread implementations |
| 55 |
if (!useWorker) { |
| 56 |
if (workletSupport && !useSPN) { |
| 57 |
log("Single Thread AudioWorklet")(); |
| 58 |
const instance = new SingleThreadAudioWorkletMainThread({ |
| 59 |
audioContext, |
| 60 |
inputChannelCount: inputChannelCount || 2, |
| 61 |
outputChannelCount: outputChannelCount || 2, |
| 62 |
}); |
| 63 |
return instance.initialize({ wasmDataURI, withPlugins, autoConnect }); |
| 64 |
} else if (spnSupport) { |
| 65 |
log("Single Thread ScriptProcessorNode")(); |
| 66 |
const instance = new ScriptProcessorNodeSingleThread({ |
| 67 |
audioContext, |
| 68 |
inputChannelCount: inputChannelCount || 2, |
| 69 |
outputChannelCount: outputChannelCount || 2, |
| 70 |
}); |
| 71 |
return await instance.initialize({ |
| 72 |
wasmDataURI, |
| 73 |
withPlugins, |
| 74 |
autoConnect, |
| 75 |
}); |
| 76 |
} else { |
| 77 |
console.error("No detectable WebAudioAPI in current environment"); |
| 78 |
return; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (workletSupport) { |
| 83 |
// closure-compiler keepme |
| 84 |
log(`worklet support detected`)(); |
| 85 |
} else if (spnSupport) { |
| 86 |
// closure-compiler keepme |
| 87 |
log(`scriptProcessorNode support detected`)(); |
| 88 |
} else { |
| 89 |
console.error(`No WebAudio Support detected`); |
| 90 |
} |
| 91 |
|
| 92 |
let audioWorker; |
| 93 |
let csoundWasmApi; |
| 94 |
|
| 95 |
if (!useSPN && workletSupport) { |
| 96 |
audioWorker = new AudioWorkletMainThread({ audioContext, audioContextIsProvided, autoConnect }); |
| 97 |
} else if (spnSupport) { |
| 98 |
audioWorker = new ScriptProcessorNodeMainThread({ |
| 99 |
audioContext, |
| 100 |
audioContextIsProvided, |
| 101 |
autoConnect, |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
if (!audioWorker) { |
| 106 |
console.error("No detectable WebAudioAPI in current environment"); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
const hasSABSupport = isSabSupported(); |
| 111 |
|
| 112 |
if (!hasSABSupport) { |
| 113 |
log(`SharedArrayBuffers not found, falling back to Vanilla concurrency`)(); |
| 114 |
} else { |
| 115 |
useSAB && log(`using SharedArrayBuffers`)(); |
| 116 |
} |
| 117 |
|
| 118 |
const worker = |
| 119 |
hasSABSupport && workletSupport && useSAB |
| 120 |
? new SharedArrayBufferMainThread({ |
| 121 |
audioWorker, |
| 122 |
audioContext, |
| 123 |
audioContextIsProvided, |
| 124 |
inputChannelCount, |
| 125 |
outputChannelCount, |
| 126 |
}) |
| 127 |
: new VanillaWorkerMainThread({ |
| 128 |
audioWorker, |
| 129 |
audioContextIsProvided, |
| 130 |
}); |
| 131 |
|
| 132 |
if (worker) { |
| 133 |
log(`starting Csound thread initialization via WebWorker`)(); |
| 134 |
await worker.initialize({ wasmDataURI, withPlugins }); |
| 135 |
csoundWasmApi = worker.api; |
| 136 |
} else { |
| 137 |
console.error("No detectable WebAssembly support in current environment"); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
return csoundWasmApi; |
| 142 |
}; |