Skip to content

Commit ee63c08

Browse files
authored
[libwebaudio.js] Simplify debug checks and logging. NFC (#25655)
Split out from #25649
1 parent 49fdd76 commit ee63c08

File tree

1 file changed

+75
-53
lines changed

1 file changed

+75
-53
lines changed

‎src/lib/libwebaudio.js‎

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,49 @@ var LibraryWebAudio ={
2323
#endif
2424
EmAudio[++EmAudioCounter]=object;
2525
#if WEBAUDIO_DEBUG
26-
console.log(`Registered new WebAudio object ${object} with ID ${EmAudioCounter}`);
26+
dbg(`Registered new WebAudio object ${object} with ID ${EmAudioCounter}`);
2727
#endif
2828
returnEmAudioCounter;
2929
},
3030

31+
#if ASSERTIONS||WEBAUDIO_DEBUG
32+
$_emAudioExpectHandle__internal: true,
33+
$_emAudioExpectHandle: (handle,methodName)=>{
34+
#if WEBAUDIO_DEBUG
35+
dbg(`called ${methodName}() with ID ${handle}`);
36+
#endif
37+
#if ASSERTIONS
38+
varobj=EmAudio[handle];
39+
assert(obj,`Called ${methodName}() on a nonexisting handle ${handle}`);
40+
returnobj;
41+
#endif
42+
},
43+
44+
$emAudioExpectContext__internal: true,
45+
$emAudioExpectContext: (handle,methodName)=>{
46+
varobj=_emAudioExpectHandle(handle,methodName);
47+
#if ASSERTIONS
48+
assert(objinstanceof(window.AudioContext||window.webkitAudioContext),`${methodName}() called with ${handle} that is not an AudioContext, but of type ${typeofobj}`);
49+
#endif
50+
},
51+
52+
$emAudioExpectNode__internal: true,
53+
$emAudioExpectNode: (handle,methodName)=>{
54+
varobj=_emAudioExpectHandle(handle,methodName);
55+
#if ASSERTIONS
56+
assert(objinstanceofwindow.AudioNode,`${methodName}() called with a handle ${handle} that is not an AudioNode, but of type ${typeofobj}`);
57+
#endif
58+
},
59+
60+
$emAudioExpectNodeOrContext_internal: true,
61+
$emAudioExpectNodeOrContext: (handle,methodName)=>{
62+
varobj=_emAudioExpectHandle(handle,methodName);
63+
#if ASSERTIONS
64+
assert(objinstanceofwindow.AudioNode||objinstanceof(window.AudioContext||window.webkitAudioContext),`${methodName}() called with a handle ${handle} that is not an AudioContext or AudioNode, but of type ${typeofobj}`);
65+
#endif
66+
},
67+
#endif
68+
3169
// Call this function from JavaScript to destroy a Wasm-side handle to an AudioContext.
3270
// After calling this function, it is no longer possible to reference this AudioContext
3371
// from Wasm code - and the GC can reclaim it after all references to it are cleared.
@@ -68,7 +106,7 @@ var LibraryWebAudio ={
68106
} : undefined;
69107

70108
#if WEBAUDIO_DEBUG
71-
console.log(`Creating new WebAudio context with parameters:`);
109+
dbg(`Creating new WebAudio context with parameters:`);
72110
console.dir(opts);
73111
#endif
74112

@@ -88,49 +126,36 @@ var LibraryWebAudio ={
88126
{{{makeDynCall('viip','callback')}}}(contextHandle,state,userData);
89127
}
90128
#if WEBAUDIO_DEBUG
91-
console.log(`emscripten_resume_audio_context_async() resuming...`);
129+
dbg('emscripten_resume_audio_context_async() resuming...');
92130
#endif
93131
EmAudio[contextHandle].resume().then(()=>{cb(1/*running*/)}).catch(()=>{cb(0/*suspended*/)});
94132
},
95133

96134
emscripten_resume_audio_context_sync: (contextHandle)=>{
97-
#if ASSERTIONS
98-
assert(EmAudio[contextHandle],`Called emscripten_resume_audio_context_sync() on a nonexisting context handle ${contextHandle}`);
99-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_resume_audio_context_sync() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
100-
#endif
101-
#if WEBAUDIO_DEBUG
102-
console.log(`AudioContext.resume() on WebAudio context with ID ${contextHandle}`);
135+
#if ASSERTIONS||WEBAUDIO_DEBUG
136+
emAudioExpectContext(contextHandle,'emscripten_resume_audio_context_sync');
103137
#endif
104138
EmAudio[contextHandle].resume();
105139
},
106140

107141
emscripten_audio_context_state: (contextHandle)=>{
108-
#if ASSERTIONS
109-
assert(EmAudio[contextHandle],`Called emscripten_audio_context_state() on a nonexisting context handle ${contextHandle}`);
110-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_audio_context_state() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
142+
#if ASSERTIONS||WEBAUDIO_DEBUG
143+
emAudioExpectContext(contextHandle,'emscripten_audio_context_state');
111144
#endif
112145
return['suspended','running','closed','interrupted'].indexOf(EmAudio[contextHandle].state);
113146
},
114147

115148
emscripten_destroy_audio_context: (contextHandle)=>{
116-
#if ASSERTIONS
117-
assert(EmAudio[contextHandle],`Called emscripten_destroy_audio_context() on an already freed context handle ${contextHandle}`);
118-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_destroy_audio_context() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
119-
#endif
120-
#if WEBAUDIO_DEBUG
121-
console.log(`Destroyed WebAudio context with ID ${contextHandle}`);
149+
#if ASSERTIONS||WEBAUDIO_DEBUG
150+
emAudioExpectContext(contextHandle,'emscripten_destroy_audio_context');
122151
#endif
123152
EmAudio[contextHandle].suspend();
124153
deleteEmAudio[contextHandle];
125154
},
126155

127156
emscripten_destroy_web_audio_node: (objectHandle)=>{
128-
#if ASSERTIONS
129-
assert(EmAudio[objectHandle],`Called emscripten_destroy_web_audio_node() on a nonexisting/already freed object handle ${objectHandle}`);
130-
assert(EmAudio[objectHandle].disconnect,`Called emscripten_destroy_web_audio_node() on a handle ${objectHandle} that is not an Web Audio Node, but of type ${typeofEmAudio[objectHandle]}`);
131-
#endif
132-
#if WEBAUDIO_DEBUG
133-
console.log(`Destroyed Web Audio Node with ID ${objectHandle}`);
157+
#if ASSERTIONS||WEBAUDIO_DEBUG
158+
emAudioExpectNode(objectHandle,'emscripten_destroy_web_audio_node');
134159
#endif
135160
// Explicitly disconnect the node from Web Audio graph before letting it GC,
136161
// to work around browser bugs such as https://webkit.org/b/222098#c23
@@ -147,10 +172,8 @@ var LibraryWebAudio ={
147172
'$stackAlloc','$stackRestore','$stackSave'],
148173
emscripten_start_wasm_audio_worklet_thread_async: (contextHandle,stackLowestAddress,stackSize,callback,userData)=>{
149174

150-
#if ASSERTIONS
151-
assert(contextHandle,`Called emscripten_start_wasm_audio_worklet_thread_async() with a null Web Audio Context handle!`);
152-
assert(EmAudio[contextHandle],`Called emscripten_start_wasm_audio_worklet_thread_async() with a nonexisting/already freed Web Audio Context handle ${contextHandle}!`);
153-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_start_wasm_audio_worklet_thread_async() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
175+
#if ASSERTIONS||WEBAUDIO_DEBUG
176+
emAudioExpectContext(contextHandle,'emscripten_start_wasm_audio_worklet_thread_async');
154177
#endif
155178

156179
varaudioContext=EmAudio[contextHandle];
@@ -166,12 +189,12 @@ var LibraryWebAudio ={
166189
#endif
167190

168191
#if WEBAUDIO_DEBUG
169-
console.log(`emscripten_start_wasm_audio_worklet_thread_async() adding audioworklet.js...`);
192+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() adding audioworklet.js...`);
170193
#endif
171194

172195
varaudioWorkletCreationFailed=()=>{
173196
#if ASSERTIONS||WEBAUDIO_DEBUG
174-
console.error(`emscripten_start_wasm_audio_worklet_thread_async() addModule() failed!`);
197+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() failed!`);
175198
#endif
176199
{{{makeDynCall('viip','callback')}}}(contextHandle,0/*EM_FALSE*/,userData);
177200
};
@@ -190,7 +213,7 @@ var LibraryWebAudio ={
190213

191214
audioWorklet.addModule({{{ wasmWorkerJs }}}).then(()=>{
192215
#if WEBAUDIO_DEBUG
193-
console.log(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
216+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
194217
#endif
195218

196219
#if MIN_FIREFOX_VERSION<138||MIN_CHROME_VERSION!=TARGET_NOT_SUPPORTED||MIN_SAFARI_VERSION!=TARGET_NOT_SUPPORTED
@@ -250,10 +273,8 @@ var LibraryWebAudio ={
250273
},
251274

252275
emscripten_create_wasm_audio_worklet_processor_async: (contextHandle,options,callback,userData)=>{
253-
#if ASSERTIONS
254-
assert(contextHandle,`Called emscripten_create_wasm_audio_worklet_processor_async() with a null Web Audio Context handle!`);
255-
assert(EmAudio[contextHandle],`Called emscripten_create_wasm_audio_worklet_processor_async() with a nonexisting/already freed Web Audio Context handle ${contextHandle}!`);
256-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_create_wasm_audio_worklet_processor_async() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
276+
#if ASSERTIONS||WEBAUDIO_DEBUG
277+
emAudioExpectContext(contextHandle,'emscripten_create_wasm_audio_worklet_processor_async');
257278
#endif
258279

259280
varprocessorName=UTF8ToString({{{makeGetValue('options',C_STRUCTS.WebAudioWorkletProcessorCreateOptions.name,'*')}}});
@@ -299,10 +320,8 @@ var LibraryWebAudio ={
299320

300321
emscripten_create_wasm_audio_worklet_node__deps: ['$emscriptenGetContextQuantumSize'],
301322
emscripten_create_wasm_audio_worklet_node: (contextHandle,name,options,callback,userData)=>{
302-
#if ASSERTIONS
303-
assert(contextHandle,`Called emscripten_create_wasm_audio_worklet_node() with a null Web Audio Context handle!`);
304-
assert(EmAudio[contextHandle],`Called emscripten_create_wasm_audio_worklet_node() with a nonexisting/already freed Web Audio Context handle ${contextHandle}!`);
305-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_create_wasm_audio_worklet_node() on a context handle ${contextHandle} that is not an AudioContext, but of type ${typeofEmAudio[contextHandle]}`);
323+
#if ASSERTIONS||WEBAUDIO_DEBUG
324+
emAudioExpectContext(contextHandle,'emscripten_create_wasm_audio_worklet_node');
306325
#endif
307326

308327
functionreadChannelCountArray(heapIndex,numOutputs){
@@ -329,7 +348,7 @@ var LibraryWebAudio ={
329348
} : undefined;
330349

331350
#if WEBAUDIO_DEBUG
332-
console.log(`Creating AudioWorkletNode "${UTF8ToString(name)}" on context=${contextHandle} with options:`);
351+
dbg(`Creating AudioWorkletNode "${UTF8ToString(name)}" on context=${contextHandle} with options:`);
333352
console.dir(opts);
334353
#endif
335354
returnemscriptenRegisterAudioObject(newAudioWorkletNode(EmAudio[contextHandle],UTF8ToString(name),opts));
@@ -338,32 +357,28 @@ var LibraryWebAudio ={
338357

339358
emscripten_audio_context_quantum_size__deps: ['$emscriptenGetContextQuantumSize'],
340359
emscripten_audio_context_quantum_size: (contextHandle)=>{
341-
#if ASSERTIONS
342-
assert(EmAudio[contextHandle],`Called emscripten_audio_context_quantum_size() with an invalid Web Audio Context handle ${contextHandle}`);
343-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_audio_context_quantum_size() on handle ${contextHandle} that is not an AudioContext, but of type ${EmAudio[contextHandle]}`);
360+
#if ASSERTIONS||WEBAUDIO_DEBUG
361+
emAudioExpectContext(contextHandle,'emscripten_audio_context_quantum_size')
344362
#endif
345363
returnemscriptenGetContextQuantumSize(contextHandle);
346364
},
347365

348366
emscripten_audio_context_sample_rate: (contextHandle)=>{
349-
#if ASSERTIONS
350-
assert(EmAudio[contextHandle],`Called emscripten_audio_context_sample_rate() with an invalid Web Audio Context handle ${contextHandle}`);
351-
assert(EmAudio[contextHandle]instanceof(window.AudioContext||window.webkitAudioContext),`Called emscripten_audio_context_sample_rate() on handle ${contextHandle} that is not an AudioContext, but of type ${EmAudio[contextHandle]}`);
367+
#if ASSERTIONS||WEBAUDIO_DEBUG
368+
emAudioExpectContext(contextHandle,'emscripten_audio_context_sample_rate');
352369
#endif
353370
returnEmAudio[contextHandle]['sampleRate'];
354371
},
355372

356373
emscripten_audio_node_connect: (source,destination,outputIndex,inputIndex)=>{
374+
#if ASSERTIONS||WEBAUDIO_DEBUG
375+
emAudioExpectNode(source,'emscripten_audio_node_connect');
376+
emAudioExpectNodeOrContext(destination,'emscripten_audio_node_connect');
377+
#endif
357378
varsrcNode=EmAudio[source];
358379
vardstNode=EmAudio[destination];
359-
#if ASSERTIONS
360-
assert(srcNode,`Called emscripten_audio_node_connect() with an invalid AudioNode handle ${source}`);
361-
assert(srcNodeinstanceofwindow.AudioNode,`Called emscripten_audio_node_connect() on handle ${source} that is not an AudiotNode, but of type ${srcNode}`);
362-
assert(dstNode,`Called emscripten_audio_node_connect() with an invalid AudioNode handle ${destination}!`);
363-
assert(dstNodeinstanceof(window.AudioContext||window.webkitAudioContext)||dstNodeinstanceofwindow.AudioNode,`Called emscripten_audio_node_connect() on handle ${destination} that is not an AudioContext or AudioNode, but of type ${dstNode}`);
364-
#endif
365380
#if WEBAUDIO_DEBUG
366-
console.log(`Connecting audio node ID ${source} to audio node ID ${destination} (${srcNode} to ${dstNode})`);
381+
dbg(`Connecting audio node ID ${source} to audio node ID ${destination} (${srcNode} to ${dstNode})`);
367382
#endif
368383
srcNode.connect(dstNode.destination||dstNode,outputIndex,inputIndex);
369384
},
@@ -427,4 +442,11 @@ var LibraryWebAudio ={
427442
}
428443
};
429444

445+
#if ASSERTIONS||WEBAUDIO_DEBUG
446+
autoAddDeps(LibraryWebAudio,'$_emAudioExpectHandle');
447+
autoAddDeps(LibraryWebAudio,'$emAudioExpectNode');
448+
autoAddDeps(LibraryWebAudio,'$emAudioExpectContext');
449+
autoAddDeps(LibraryWebAudio,'$emAudioExpectNodeOrContext');
450+
#endif
451+
430452
addToLibrary(LibraryWebAudio);

0 commit comments

Comments
(0)