Skip to content

Commit ad8210f

Browse files
authored
Cleanup library_sdl.js. NFC (#23030)
1 parent c85a097 commit ad8210f

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

src/library_sdl.js

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ var LibrarySDL = {
412412
};
413413

414414
#if OFFSCREEN_FRAMEBUFFER
415-
// TODO: Make SDL explicitly aware of whether it is being proxied or not, and set these to true only when proxying is being performed.
415+
// TODO: Make SDL explicitly aware of whether it is being proxied or not,
416+
// and set these to true only when proxying is being performed.
416417
GL.enableOffscreenFramebufferAttributes(webGLContextAttributes);
417418
#endif
418419
var ctx = Browser.createContext(canvas, is_SDL_OPENGL, usePageCanvas, webGLContextAttributes);
@@ -1136,12 +1137,7 @@ var LibrarySDL = {
11361137
},
11371138

11381139
setPannerPosition(info, x, y, z) {
1139-
if (!info) return;
1140-
if (info.audio) {
1141-
if (info.audio.webAudioPannerNode) {
1142-
info.audio.webAudioPannerNode['setPosition'](x, y, z);
1143-
}
1144-
}
1140+
info?.audio?.webAudioPannerNode?.['setPosition'](x, y, z);
11451141
},
11461142

11471143
// Plays out an SDL audio resource that was loaded with the Mix_Load APIs, when using Web Audio..
@@ -1153,7 +1149,9 @@ var LibrarySDL = {
11531149
var webAudio = audio.resource.webAudio;
11541150
audio.paused = false;
11551151
if (!webAudio.decodedBuffer) {
1156-
if (webAudio.onDecodeComplete === undefined) abort("Cannot play back audio object that was not loaded");
1152+
if (webAudio.onDecodeComplete === undefined) {
1153+
abort("Cannot play back audio object that was not loaded");
1154+
}
11571155
webAudio.onDecodeComplete.push(() => { if (!audio.paused) SDL.playWebAudio(audio); });
11581156
return;
11591157
}
@@ -1189,11 +1187,13 @@ var LibrarySDL = {
11891187
if (!audio) return;
11901188
if (audio.webAudioNode) {
11911189
try {
1192-
// Remember where we left off, so that if/when we resume, we can restart the playback at a proper place.
1190+
// Remember where we left off, so that if/when we resume, we can
1191+
// restart the playback at a proper place.
11931192
audio.currentPosition = (SDL.audioContext['currentTime'] - audio.startTime) % audio.resource.webAudio.decodedBuffer.duration;
1194-
// Important: When we reach here, the audio playback is stopped by the user. But when calling .stop() below, the Web Audio
1195-
// graph will send the onended signal, but we don't want to process that, since pausing should not clear/destroy the audio
1196-
// channel.
1193+
// Important: When we reach here, the audio playback is stopped by the
1194+
// user. But when calling .stop() below, the Web Audio graph will send
1195+
// the onended signal, but we don't want to process that, since
1196+
// pausing should not clear/destroy the audio channel.
11971197
audio.webAudioNode['onended'] = undefined;
11981198
audio.webAudioNode.stop(0); // 0 is a default parameter, but WebKit is confused by it #3861
11991199
audio.webAudioNode = undefined;
@@ -1205,20 +1205,27 @@ var LibrarySDL = {
12051205
},
12061206

12071207
openAudioContext() {
1208-
// Initialize Web Audio API if we haven't done so yet. Note: Only initialize Web Audio context ever once on the web page,
1209-
// since initializing multiple times fails on Chrome saying 'audio resources have been exhausted'.
1208+
// Initialize Web Audio API if we haven't done so yet. Note: Only
1209+
// initialize Web Audio context ever once on the web page, since
1210+
// initializing multiple times fails on Chrome saying 'audio resources
1211+
// have been exhausted'.
12101212
if (!SDL.audioContext) {
1211-
if (typeof AudioContext != 'undefined') SDL.audioContext = new AudioContext();
1212-
else if (typeof webkitAudioContext != 'undefined') SDL.audioContext = new webkitAudioContext();
1213+
if (typeof AudioContext != 'undefined') {
1214+
SDL.audioContext = new AudioContext();
1215+
} else if (typeof webkitAudioContext != 'undefined') {
1216+
SDL.audioContext = new webkitAudioContext();
1217+
}
12131218
}
12141219
},
12151220

12161221
webAudioAvailable: () => !!SDL.audioContext,
12171222

12181223
fillWebAudioBufferFromHeap(heapPtr, sizeSamplesPerChannel, dstAudioBuffer) {
1219-
// The input audio data is interleaved across the channels, i.e. [L, R, L, R, L, R, ...] and is either 8-bit, 16-bit or float as
1220-
// supported by the SDL API. The output audio wave data for Web Audio API must be in planar buffers of [-1,1]-normalized Float32 data,
1221-
// so perform a buffer conversion for the data.
1224+
// The input audio data is interleaved across the channels, i.e. [L, R, L,
1225+
// R, L, R, ...] and is either 8-bit, 16-bit or float as supported by the
1226+
// SDL API. The output audio wave data for Web Audio API must be in planar
1227+
// buffers of [-1,1]-normalized Float32 data, so perform a buffer
1228+
// conversion for the data.
12221229
var audio = SDL.audio;
12231230
var numChannels = audio.channels;
12241231
for (var c = 0; c < numChannels; ++c) {
@@ -1963,8 +1970,9 @@ var LibrarySDL = {
19631970
var h = srcData.height * y;
19641971
var ret = SDL.makeSurface(Math.abs(w), Math.abs(h), srcData.flags, false, 'zoomSurface');
19651972
var dstData = SDL.surfaces[ret];
1966-
if (x >= 0 && y >= 0) dstData.ctx.drawImage(srcData.canvas, 0, 0, w, h);
1967-
else {
1973+
if (x >= 0 && y >= 0) {
1974+
dstData.ctx.drawImage(srcData.canvas, 0, 0, w, h);
1975+
} else {
19681976
dstData.ctx.save();
19691977
dstData.ctx.scale(x < 0 ? -1 : 1, y < 0 ? -1 : 1);
19701978
dstData.ctx.drawImage(srcData.canvas, w < 0 ? w : 0, h < 0 ? h : 0, Math.abs(w), Math.abs(h));
@@ -2705,8 +2713,7 @@ var LibrarySDL = {
27052713
if (stream) {
27062714
rwops = { filename: stream.path };
27072715
}
2708-
}
2709-
else if (type === 4/*SDL_RWOPS_MEMORY*/ || type === 5/*SDL_RWOPS_MEMORY_RO*/) {
2716+
} else if (type === 4/*SDL_RWOPS_MEMORY*/ || type === 5/*SDL_RWOPS_MEMORY_RO*/) {
27102717
var base = {{{ makeGetValue('rwopsID', C_STRUCTS.SDL_RWops.hidden.mem.base, 'i32') }}};
27112718
var stop = {{{ makeGetValue('rwopsID', C_STRUCTS.SDL_RWops.hidden.mem.stop, 'i32') }}};
27122719
@@ -2742,14 +2749,16 @@ var LibrarySDL = {
27422749
Browser.preloadedAudios[filename] = null;
27432750
}
27442751
audio = raw;
2745-
}
2746-
else if (rwops.bytes !== undefined) {
2747-
// For Web Audio context buffer decoding, we must make a clone of the audio data, but for <media> element,
2748-
// a view to existing data is sufficient.
2749-
if (SDL.webAudioAvailable()) bytes = HEAPU8.buffer.slice(rwops.bytes, rwops.bytes + rwops.count);
2750-
else bytes = HEAPU8.subarray(rwops.bytes, rwops.bytes + rwops.count);
2751-
}
2752-
else {
2752+
} else if (rwops.bytes !== undefined) {
2753+
// For Web Audio context buffer decoding, we must make a clone of the
2754+
// audio data, but for <media> element, a view to existing data is
2755+
// sufficient.
2756+
if (SDL.webAudioAvailable()) {
2757+
bytes = HEAPU8.buffer.slice(rwops.bytes, rwops.bytes + rwops.count);
2758+
} else {
2759+
bytes = HEAPU8.subarray(rwops.bytes, rwops.bytes + rwops.count);
2760+
}
2761+
} else {
27532762
return 0;
27542763
}
27552764
@@ -2769,16 +2778,17 @@ var LibrarySDL = {
27692778
// is complete, which will then start the playback (with some delay).
27702779
onDecodeComplete: [], // While this member array exists, decoding hasn't finished yet.
27712780
}
2772-
var onDecodeComplete = (data) => {
2781+
SDL.audioContext['decodeAudioData'](arrayBuffer, (data) => {
27732782
webAudio.decodedBuffer = data;
2774-
// Call all handlers that were waiting for this decode to finish, and clear the handler list.
2783+
// Call all handlers that were waiting for this decode to finish, and
2784+
// clear the handler list.
27752785
webAudio.onDecodeComplete.forEach((e) => e());
2776-
webAudio.onDecodeComplete = undefined; // Don't allow more callback handlers since audio has finished decoding.
2777-
};
2778-
SDL.audioContext['decodeAudioData'](arrayBuffer, onDecodeComplete);
2786+
// Don't allow more callback handlers since audio has finished decoding.
2787+
delete webAudio.onDecodeComplete;
2788+
});
27792789
} else if (audio === undefined && bytes) {
2780-
// Here, we didn't find a preloaded audio but we either were passed a filepath for
2781-
// which we loaded bytes, or we were passed some bytes
2790+
// Here, we didn't find a preloaded audio but we either were passed a
2791+
// filepath for which we loaded bytes, or we were passed some bytes
27822792
var blob = new Blob([bytes], {type: rwops.mimetype});
27832793
var url = URL.createObjectURL(blob);
27842794
audio = new Audio();
@@ -2893,7 +2903,7 @@ var LibrarySDL = {
28932903
if (channelInfo.audio === this || channelInfo.audio.webAudioNode === this) {
28942904
channelInfo.audio.paused = true; channelInfo.audio = null;
28952905
}
2896-
if (SDL.channelFinished) {{{ makeDynCall('vi', 'SDL.channelFinished') }}}(channel);
2906+
if (SDL.channelFinished) {{{ makeDynCall('vi', 'SDL.channelFinished') }}}(channel);
28972907
}
28982908
if (channelInfo.audio) {
28992909
_Mix_HaltChannel(channel);
@@ -2976,7 +2986,7 @@ var LibrarySDL = {
29762986
} else if (info.audio) { // Play via the <audio> element
29772987
audio = info.audio;
29782988
}
2979-
audio['onended'] = function() {
2989+
audio['onended'] = function() {
29802990
if (SDL.music.audio === this || SDL.music.audio?.webAudioNode === this) {
29812991
_Mix_HaltMusic(); // will send callback
29822992
}
@@ -3068,10 +3078,7 @@ var LibrarySDL = {
30683078
return pausedCount;
30693079
}
30703080
var info = SDL.channels[channel];
3071-
if (info?.audio?.paused) {
3072-
return 1;
3073-
}
3074-
return 0;
3081+
return info?.audio?.paused ? 1 : 0;
30753082
},
30763083
30773084
Mix_PausedMusic__proxy: 'sync',
@@ -3375,8 +3382,11 @@ var LibrarySDL = {
33753382

33763383
SDL_GL_GetSwapInterval__proxy: 'sync',
33773384
SDL_GL_GetSwapInterval: () => {
3378-
if (MainLoop.timingMode == {{{ cDefs.EM_TIMING_RAF }}}) return MainLoop.timingValue;
3379-
else return 0;
3385+
if (MainLoop.timingMode == {{{ cDefs.EM_TIMING_RAF }}}) {
3386+
return MainLoop.timingValue;
3387+
} else {
3388+
return 0;
3389+
}
33803390
},
33813391

33823392
SDL_GL_SetSwapInterval__deps: ['emscripten_set_main_loop_timing'],

0 commit comments

Comments
 (0)