Skip to content

Commit ac52a3b

Browse files
committed
Simply debug checks and logging in libwebaudio.js. NFC
Split out from #25649
1 parent 1f463d1 commit ac52a3b

File tree

1 file changed

+72
-53
lines changed

1 file changed

+72
-53
lines changed

src/lib/libwebaudio.js

Lines changed: 72 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
return EmAudioCounter;
2929
},
3030

31+
#if ASSERTIONS || WEBAUDIO_DEBUG
32+
$_emAudioCheckHandle__internal: true,
33+
$_emAudioCheckHandle: (handle, methodName) => {
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+
return obj;
41+
#endif
42+
},
43+
44+
$emAudioCheckContext__internal: true,
45+
$emAudioCheckContext: (handle, methodName) => {
46+
var obj = _emAudioCheckHandle(handle, methodName);
47+
#if ASSERTIONS
48+
assert(obj instanceof (window.AudioContext || window.webkitAudioContext), `${methodName}() called with ${handle} that is not an AudioContext, but of type ${typeof obj}`);
49+
#endif
50+
},
51+
52+
$emAudioCheckNode__internal: true,
53+
$emAudioCheckNode: (handle, methodName) => {
54+
var obj = _emAudioCheckHandle(handle, methodName);
55+
#if ASSERTIONS
56+
assert(obj instanceof window.AudioNode, `${methodName}() called with a handle ${handle} that is not an AudioNode, but of type ${typeof obj}`);
57+
#endif
58+
},
59+
60+
$emAudioCheckNodeOrContext_internal: true,
61+
$emAudioCheckNodeOrContext: (handle, methodName) => {
62+
var obj = _emAudioCheckHandle(handle, methodName);
63+
#if ASSERTIONS
64+
assert(obj instanceof window.AudioNode || obj instanceof (window.AudioContext || window.webkitAudioContext), `${methodName}() called with a handle ${handle} that is not an AudioContext or AudioNode, but of type ${typeof obj}`);
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 ${typeof EmAudio[contextHandle]}`);
100-
#endif
101-
#if WEBAUDIO_DEBUG
102-
console.log(`AudioContext.resume() on WebAudio context with ID ${contextHandle}`);
135+
#if ASSERTIONS || WEBAUDIO_DEBUG
136+
emAudioCheckContext(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 ${typeof EmAudio[contextHandle]}`);
142+
#if ASSERTIONS || WEBAUDIO_DEBUG
143+
emAudioCheckContext(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 ${typeof EmAudio[contextHandle]}`);
119-
#endif
120-
#if WEBAUDIO_DEBUG
121-
console.log(`Destroyed WebAudio context with ID ${contextHandle}`);
149+
#if ASSERTIONS || WEBAUDIO_DEBUG
150+
emAudioCheckContext(contextHandle, 'emscripten_destroy_audio_context');
122151
#endif
123152
EmAudio[contextHandle].suspend();
124153
delete EmAudio[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 ${typeof EmAudio[objectHandle]}`);
131-
#endif
132-
#if WEBAUDIO_DEBUG
133-
console.log(`Destroyed Web Audio Node with ID ${objectHandle}`);
157+
#if ASSERTIONS || WEBAUDIO_DEBUG
158+
emAudioCheckNode(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 ${typeof EmAudio[contextHandle]}`);
175+
#if ASSERTIONS || WEBAUDIO_DEBUG
176+
emAudioCheckContext(contextHandle, 'emscripten_start_wasm_audio_worklet_thread_async');
154177
#endif
155178

156179
var audioContext = 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
var audioWorkletCreationFailed = () => {
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 ${typeof EmAudio[contextHandle]}`);
276+
#if ASSERTIONS || WEBAUDIO_DEBUG
277+
emAudioCheckContext(contextHandle, 'emscripten_create_wasm_audio_worklet_processor_async');
257278
#endif
258279

259280
var processorName = 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 ${typeof EmAudio[contextHandle]}`);
323+
#if ASSERTIONS || WEBAUDIO_DEBUG
324+
emAudioCheckContext(contextHandle, 'emscripten_create_wasm_audio_worklet_node');
306325
#endif
307326

308327
function readChannelCountArray(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
return emscriptenRegisterAudioObject(new AudioWorkletNode(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+
emAudioCheckContext(contextHandle, 'emscripten_audio_context_quantum_size')
344362
#endif
345363
return emscriptenGetContextQuantumSize(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+
emAudioCheckContext(contextHandle, 'emscripten_audio_context_sample_rate');
352369
#endif
353370
return EmAudio[contextHandle]['sampleRate'];
354371
},
355372

356373
emscripten_audio_node_connect: (source, destination, outputIndex, inputIndex) => {
374+
#if ASSERTIONS || WEBAUDIO_DEBUG
375+
emAudioCheckNode(source, 'emscripten_audio_node_connect');
376+
emAudioCheckNodeOrContext(destination, 'emscripten_audio_node_connect');
377+
#endif
357378
var srcNode = EmAudio[source];
358379
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
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,8 @@ var LibraryWebAudio = {
427442
}
428443
};
429444

445+
#if ASSERTIONS || WEBAUDIO_DEBUG
446+
autoAddDeps(LibraryWebAudio, ['$emAudioCheckHandle', '$emAudioCheckNode', '$emAudioCheckContext', '$emAudioCheckNodeOrContext']);
447+
#endif
448+
430449
addToLibrary(LibraryWebAudio);

0 commit comments

Comments
 (0)