@@ -10,7 +10,39 @@ function heap2Str(buf: Uint8Array) {
10
10
return str ;
11
11
}
12
12
13
- const loadedProcessors : string [ ] = [ ] ;
13
+ const processorModules : Record < string , Promise < void > > = { } ;
14
+ async function loadProcessorModule ( context : IAudioContext , url : string ) {
15
+ if ( ! context . audioWorklet ) {
16
+ console . error (
17
+ "Error loading FaustAudioProcessorNode: standardized-audio-context AudioWorklet isn't supported in this environment."
18
+ ) ;
19
+ return null ;
20
+ }
21
+
22
+ const existing = processorModules [ url ] ;
23
+
24
+ if ( existing ) {
25
+ return existing ;
26
+ }
27
+
28
+ processorModules [ url ] = context . audioWorklet . addModule ( url ) ;
29
+ return processorModules [ url ] ;
30
+ }
31
+
32
+ const wasmModules : Record < string , Promise < WebAssembly . Module > > = { } ;
33
+ async function getWasmModule ( url : string ) {
34
+ const existing = wasmModules [ url ] ;
35
+
36
+ if ( existing ) {
37
+ return existing ;
38
+ }
39
+
40
+ wasmModules [ url ] = fetch ( url )
41
+ . then ( ( response ) => response . arrayBuffer ( ) )
42
+ . then ( ( dspBuffer ) => WebAssembly . compile ( dspBuffer ) ) ;
43
+ return wasmModules [ url ] ;
44
+ }
45
+
14
46
const importObject = {
15
47
env : {
16
48
memoryBase : 0 ,
@@ -81,32 +113,19 @@ export default async function loadProcessor(
81
113
baseURL : string
82
114
) {
83
115
const cleanedBaseURL = baseURL . endsWith ( "/" ) ? baseURL : `${ baseURL } /` ;
84
- // Load DSP wasm
85
- const dspFile = await fetch ( `${ cleanedBaseURL } ${ name } .wasm` ) ;
86
- const dspBuffer = await dspFile . arrayBuffer ( ) ;
87
- const dspModule = await WebAssembly . compile ( dspBuffer ) ;
116
+
117
+ const [ dspModule ] = await Promise . all ( [
118
+ getWasmModule ( `${ cleanedBaseURL } ${ name } .wasm` ) ,
119
+ loadProcessorModule ( context , `${ cleanedBaseURL } ${ name } -processor.js` ) ,
120
+ ] ) ;
121
+
88
122
const dspInstance = await WebAssembly . instantiate ( dspModule , importObject ) ;
89
123
90
124
const HEAPU8 = new Uint8Array ( dspInstance . exports . memory . buffer ) ;
91
125
const json = heap2Str ( HEAPU8 ) ;
92
126
const json_object = JSON . parse ( json ) ;
93
127
const processorOptions = { wasm_module : dspModule , json : json } ;
94
128
95
- if ( ! context . audioWorklet ) {
96
- console . error (
97
- "Error loading FaustAudioProcessorNode: standardized-audio-context AudioWorklet isn't supported in this environment."
98
- ) ;
99
- return null ;
100
- }
101
-
102
- // Load processor script, if necessary
103
- if ( ! loadedProcessors . includes ( name ) ) {
104
- await context . audioWorklet . addModule (
105
- `${ cleanedBaseURL } ${ name } -processor.js`
106
- ) ;
107
- loadedProcessors . push ( name ) ;
108
- }
109
-
110
129
const nodeOptions = {
111
130
numberOfInputs : parseInt ( json_object . inputs ) > 0 ? 1 : 0 ,
112
131
numberOfOutputs : parseInt ( json_object . outputs ) > 0 ? 1 : 0 ,
0 commit comments