Skip to content

Commit 60e5204

Browse files
committed
Add assertions to emscriptenGetAudioObject. NFC
This API was added when audioworklets were first added, but it doesn't look like it was intended to work with invalid inputs: 5402fc9. At least none of the original usages in 5402fc9 checked for undefined return values.
1 parent d1ba27c commit 60e5204

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

src/lib/libwebaudio.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ var LibraryWebAudio = {
7373

7474
// Call this function from JavaScript to get the Web Audio object corresponding to the given
7575
// Wasm handle ID.
76-
$emscriptenGetAudioObject: (objectHandle) => emAudio[objectHandle],
76+
$emscriptenGetAudioObject: (objectHandle) => {
77+
#if ASSERTIONS || WEBAUDIO_DEBUG
78+
emAudioExpectNodeOrContext(objectHandle, 'emscriptenGetAudioObject');
79+
#endif
80+
return emAudio[objectHandle];
81+
},
7782

7883
// Performs the work of getting the AudioContext's render quantum size.
7984
$emscriptenGetContextQuantumSize: (contextHandle) => {

test/webaudio/audioworklet_params_mixing.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
9898
// it's already fading up or down, we reverse the fade direction).
9999
EM_JS(void, doFade, (EMSCRIPTEN_AUDIO_WORKLET_NODE_T workletID), {
100100
var worklet = emscriptenGetAudioObject(workletID);
101-
if (worklet) {
102-
// Emscripten's API creates these from a C array, indexing them instead of a
103-
// name. Chrome and FF work with 0 but Safari requires the correct "0".
104-
var param = worklet.parameters.get("0");
105-
if (param) {
106-
param.setTargetAtTime((param.value > 0.5) ? 0 : 1, 0 /* same as context.currentTime */, 0.5);
107-
}
101+
// Emscripten's API creates these from a C array, indexing them instead of a
102+
// name. Chrome and FF work with 0 but Safari requires the correct "0".
103+
var param = worklet.parameters.get("0");
104+
if (param) {
105+
param.setTargetAtTime((param.value > 0.5) ? 0 : 1, 0 /* same as context.currentTime */, 0.5);
108106
}
109107
})
110108

test/webaudio/audioworklet_test_shared.inc

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,25 @@ EMSCRIPTEN_WEBAUDIO_T bassID = 0;
3030
// registered as an internal audio object and the ID returned).
3131
EM_JS(EMSCRIPTEN_WEBAUDIO_T, createTrack, (EMSCRIPTEN_WEBAUDIO_T ctxID, const char* url, bool looping), {
3232
var context = emscriptenGetAudioObject(ctxID);
33-
if (context) {
34-
var audio = document.createElement('audio');
35-
// Number() wrapper is a workaround for UTF8ToString() needing a JS number
36-
// and from64() not being available in EM_JS macros. Fix in UTF8ToString?
37-
audio.src = UTF8ToString(Number(url));
38-
audio.loop = looping;
39-
var track = context.createMediaElementSource(audio);
40-
return emscriptenRegisterAudioObject(track);
41-
}
42-
return 0;
33+
var audio = document.createElement('audio');
34+
// Number() wrapper is a workaround for UTF8ToString() needing a JS number
35+
// and from64() not being available in EM_JS macros. Fix in UTF8ToString?
36+
audio.src = UTF8ToString(Number(url));
37+
audio.loop = looping;
38+
var track = context.createMediaElementSource(audio);
39+
return emscriptenRegisterAudioObject(track);
4340
})
4441

4542
// Toggles the play/pause of a MediaElementAudioSourceNode given its ID
4643
EM_JS(void, toggleTrack, (EMSCRIPTEN_WEBAUDIO_T srcID), {
4744
var source = emscriptenGetAudioObject(srcID);
48-
if (source) {
49-
var audio = source.mediaElement;
50-
if (audio) {
51-
if (audio.paused) {
52-
audio.currentTime = 0;
53-
audio.play();
54-
} else {
55-
audio.pause();
56-
}
45+
var audio = source.mediaElement;
46+
if (audio) {
47+
if (audio.paused) {
48+
audio.currentTime = 0;
49+
audio.play();
50+
} else {
51+
audio.pause();
5752
}
5853
}
5954
})

0 commit comments

Comments
 (0)