Skip to content

Commit 3770dfe

Browse files
committed
Simply debug checks and logging in libwebaudio.js. NFC
Split out from #25649
1 parent 87d9a75 commit 3770dfe

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

src/lib/libwebaudio.js

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ var LibraryWebAudio = {
2828
return EmAudioCounter;
2929
},
3030

31+
#if ASSERTIONS || WEBAUDIO_DEBUG
32+
$emAudioCheckHandle__internal: true,
33+
$emAudioCheckHandle(handle, methodName, isNode = false) {
34+
#if WEBAUDIO_DEBUG
35+
dbg(`called ${methodName}() with ID ${handle}`);
36+
#endif
37+
#if ASSERTIONS
38+
var obj = EmAudio[handle];
39+
assert(obj, `Called ${methodName}() on a nonexisting handle ${handle}`);
40+
if (isNode == 2) {
41+
// Some methods accept either a node or an audio context
42+
assert(obj instanceof window.AudioNode || obj instanceof (window.AudioContext || window.webkitAudioContext), `Called ${methodName}() on a context handle ${handle} that is not an AudioNode, but of type ${typeof obj}`);
43+
} else if (isNode) {
44+
assert(obj instanceof window.AudioNode, `Called ${methodName}() on a context handle ${handle} that is not an AudioNode, but of type ${typeof obj}`);
45+
} else {
46+
assert(obj instanceof (window.AudioContext || window.webkitAudioContext), `Called ${methodName}() on a context handle ${handle} that is not an AudioContext, but of type ${typeof obj}`);
47+
}
48+
#endif
49+
},
50+
#endif
51+
3152
// Call this function from JavaScript to destroy a Wasm-side handle to an AudioContext.
3253
// After calling this function, it is no longer possible to reference this AudioContext
3354
// from Wasm code - and the GC can reclaim it after all references to it are cleared.
@@ -68,7 +89,7 @@ var LibraryWebAudio = {
6889
} : undefined;
6990

7091
#if WEBAUDIO_DEBUG
71-
console.log(`Creating new WebAudio context with parameters:`);
92+
dbg(`Creating new WebAudio context with parameters:`);
7293
console.dir(opts);
7394
#endif
7495

@@ -88,49 +109,36 @@ var LibraryWebAudio = {
88109
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, state, userData);
89110
}
90111
#if WEBAUDIO_DEBUG
91-
console.log(`emscripten_resume_audio_context_async() resuming...`);
112+
dbg('emscripten_resume_audio_context_async() resuming...');
92113
#endif
93114
EmAudio[contextHandle].resume().then(() => { cb(1/*running*/) }).catch(() => { cb(0/*suspended*/) });
94115
},
95116

96117
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 ${typeof EmAudio[contextHandle]}`);
100-
#endif
101-
#if WEBAUDIO_DEBUG
102-
console.log(`AudioContext.resume() on WebAudio context with ID ${contextHandle}`);
118+
#if ASSERTIONS || WEBAUDIO_DEBUG
119+
emAudioCheckHandle(contextHandle, 'emscripten_resume_audio_context_sync');
103120
#endif
104121
EmAudio[contextHandle].resume();
105122
},
106123

107124
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 ${typeof EmAudio[contextHandle]}`);
125+
#if ASSERTIONS || WEBAUDIO_DEBUG
126+
emAudioCheckHandle(contextHandle, 'emscripten_audio_context_state');
111127
#endif
112128
return ['suspended', 'running', 'closed', 'interrupted'].indexOf(EmAudio[contextHandle].state);
113129
},
114130

115131
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 ${typeof EmAudio[contextHandle]}`);
119-
#endif
120-
#if WEBAUDIO_DEBUG
121-
console.log(`Destroyed WebAudio context with ID ${contextHandle}`);
132+
#if ASSERTIONS || WEBAUDIO_DEBUG
133+
emAudioCheckHandle(contextHandle, 'emscripten_destroy_audio_context');
122134
#endif
123135
EmAudio[contextHandle].suspend();
124136
delete EmAudio[contextHandle];
125137
},
126138

127139
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 ${typeof EmAudio[objectHandle]}`);
131-
#endif
132-
#if WEBAUDIO_DEBUG
133-
console.log(`Destroyed Web Audio Node with ID ${objectHandle}`);
140+
#if ASSERTIONS || WEBAUDIO_DEBUG
141+
emAudioCheckHandle(objectHandle, 'emscripten_destroy_web_audio_node', true);
134142
#endif
135143
// Explicitly disconnect the node from Web Audio graph before letting it GC,
136144
// to work around browser bugs such as https://webkit.org/b/222098#c23
@@ -147,10 +155,8 @@ var LibraryWebAudio = {
147155
'$stackAlloc', '$stackRestore', '$stackSave'],
148156
emscripten_start_wasm_audio_worklet_thread_async: (contextHandle, stackLowestAddress, stackSize, callback, userData) => {
149157

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 ${typeof EmAudio[contextHandle]}`);
158+
#if ASSERTIONS || WEBAUDIO_DEBUG
159+
emAudioCheckHandle(contextHandle, 'emscripten_start_wasm_audio_worklet_thread_async');
154160
#endif
155161

156162
var audioContext = EmAudio[contextHandle];
@@ -166,12 +172,12 @@ var LibraryWebAudio = {
166172
#endif
167173

168174
#if WEBAUDIO_DEBUG
169-
console.log(`emscripten_start_wasm_audio_worklet_thread_async() adding audioworklet.js...`);
175+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() adding audioworklet.js...`);
170176
#endif
171177

172178
var audioWorkletCreationFailed = () => {
173179
#if ASSERTIONS || WEBAUDIO_DEBUG
174-
console.error(`emscripten_start_wasm_audio_worklet_thread_async() addModule() failed!`);
180+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() failed!`);
175181
#endif
176182
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, 0/*EM_FALSE*/, userData);
177183
};
@@ -190,7 +196,7 @@ var LibraryWebAudio = {
190196

191197
audioWorklet.addModule({{{ wasmWorkerJs }}}).then(() => {
192198
#if WEBAUDIO_DEBUG
193-
console.log(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
199+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
194200
#endif
195201

196202
#if MIN_FIREFOX_VERSION < 138 || MIN_CHROME_VERSION != TARGET_NOT_SUPPORTED || MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED
@@ -250,10 +256,8 @@ var LibraryWebAudio = {
250256
},
251257

252258
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 ${typeof EmAudio[contextHandle]}`);
259+
#if ASSERTIONS || WEBAUDIO_DEBUG
260+
emAudioCheckHandle(contextHandle, 'emscripten_create_wasm_audio_worklet_processor_async');
257261
#endif
258262

259263
var processorName = UTF8ToString({{{ makeGetValue('options', C_STRUCTS.WebAudioWorkletProcessorCreateOptions.name, '*') }}});
@@ -299,10 +303,8 @@ var LibraryWebAudio = {
299303

300304
emscripten_create_wasm_audio_worklet_node__deps: ['$emscriptenGetContextQuantumSize'],
301305
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 ${typeof EmAudio[contextHandle]}`);
306+
#if ASSERTIONS || WEBAUDIO_DEBUG
307+
emAudioCheckHandle(contextHandle, 'emscripten_create_wasm_audio_worklet_node');
306308
#endif
307309

308310
function readChannelCountArray(heapIndex, numOutputs) {
@@ -329,7 +331,7 @@ var LibraryWebAudio = {
329331
} : undefined;
330332

331333
#if WEBAUDIO_DEBUG
332-
console.log(`Creating AudioWorkletNode "${UTF8ToString(name)}" on context=${contextHandle} with options:`);
334+
dbg(`Creating AudioWorkletNode "${UTF8ToString(name)}" on context=${contextHandle} with options:`);
333335
console.dir(opts);
334336
#endif
335337
return emscriptenRegisterAudioObject(new AudioWorkletNode(EmAudio[contextHandle], UTF8ToString(name), opts));
@@ -338,32 +340,28 @@ var LibraryWebAudio = {
338340

339341
emscripten_audio_context_quantum_size__deps: ['$emscriptenGetContextQuantumSize'],
340342
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]}`);
343+
#if ASSERTIONS || WEBAUDIO_DEBUG
344+
emAudioCheckHandle(contextHandle, 'emscripten_audio_context_quantum_size')
344345
#endif
345346
return emscriptenGetContextQuantumSize(contextHandle);
346347
},
347348

348349
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]}`);
350+
#if ASSERTIONS || WEBAUDIO_DEBUG
351+
emAudioCheckHandle(contextHandle, 'emscripten_audio_context_sample_rate');
352352
#endif
353353
return EmAudio[contextHandle]['sampleRate'];
354354
},
355355

356356
emscripten_audio_node_connect: (source, destination, outputIndex, inputIndex) => {
357+
#if ASSERTIONS || WEBAUDIO_DEBUG
358+
emAudioCheckHandle(source, 'emscripten_audio_node_connect', 1);
359+
emAudioCheckHandle(destination, 'emscripten_audio_node_connect', 2);
360+
#endif
357361
var srcNode = EmAudio[source];
358362
var dstNode = EmAudio[destination];
359-
#if ASSERTIONS
360-
assert(srcNode, `Called emscripten_audio_node_connect() with an invalid AudioNode handle ${source}`);
361-
assert(srcNode instanceof window.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(dstNode instanceof (window.AudioContext || window.webkitAudioContext) || dstNode instanceof window.AudioNode, `Called emscripten_audio_node_connect() on handle ${destination} that is not an AudioContext or AudioNode, but of type ${dstNode}`);
364-
#endif
365363
#if WEBAUDIO_DEBUG
366-
console.log(`Connecting audio node ID ${source} to audio node ID ${destination} (${srcNode} to ${dstNode})`);
364+
dbg(`Connecting audio node ID ${source} to audio node ID ${destination} (${srcNode} to ${dstNode})`);
367365
#endif
368366
srcNode.connect(dstNode.destination || dstNode, outputIndex, inputIndex);
369367
},
@@ -427,4 +425,8 @@ var LibraryWebAudio = {
427425
}
428426
};
429427

428+
#if ASSERTIONS || WEBAUDIO_DEBUG
429+
autoAddDeps(LibraryWebAudio, '$emAudioCheckHandle');
430+
#endif
431+
430432
addToLibrary(LibraryWebAudio);

0 commit comments

Comments
 (0)