Skip to content

Commit 2768c9f

Browse files
authored
Refactor src/lib/libopenal.js. NFC (emscripten-core#27425)
- Use `for .. of` in a few more places. - Remove code duplication in `setSourceParam(AL_BUFFER)`. The `if` condition here had a lot of the same code on each branch.
1 parent 370104f commit 2768c9f

3 files changed

Lines changed: 54 additions & 66 deletions

File tree

src/lib/libopenal.js

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ var LibraryOpenAL = {
143143

144144
var buf = src.bufQueue[bufCursor % src.bufQueue.length];
145145
// If the buffer contains no data, skip it
146-
if (buf.length === 0) {
146+
if (!buf.length) {
147147
skipCount++;
148148
// If we've gone through the whole queue and everything is 0 length, just give up
149149
if (skipCount === src.bufQueue.length) {
@@ -249,7 +249,7 @@ var LibraryOpenAL = {
249249
} else if (src.type === {{{ cDefs.AL_STATIC }}} && src.looping) {
250250
// If the source is a looping static buffer, determine the buffer offset based on the loop points
251251
var buf = src.bufQueue[0];
252-
if (buf.length === 0) {
252+
if (!buf.length) {
253253
src.bufOffset = 0.0;
254254
} else {
255255
var delta = (currentTime - src.bufStartTime) * src.playbackRate;
@@ -331,8 +331,8 @@ var LibraryOpenAL = {
331331
},
332332

333333
stopSourceAudio: (src) => {
334-
for (var i = 0; i < src.audioQueue.length; i++) {
335-
src.audioQueue[i].stop();
334+
for (var audioSrc of src.audioQueue) {
335+
audioSrc.stop();
336336
}
337337
src.audioQueue.length = 0;
338338
},
@@ -399,9 +399,9 @@ var LibraryOpenAL = {
399399

400400
// Find the first non-zero buffer in the queue to determine the proper format
401401
var templateBuf = AL.buffers[0];
402-
for (var i = 0; i < src.bufQueue.length; i++) {
403-
if (src.bufQueue[i].id !== 0) {
404-
templateBuf = src.bufQueue[i];
402+
for (var buf of src.bufQueue) {
403+
if (buf.id) {
404+
templateBuf = buf;
405405
break;
406406
}
407407
}
@@ -697,9 +697,8 @@ var LibraryOpenAL = {
697697

698698
sourceDuration: (src) => {
699699
var length = 0.0;
700-
for (var i = 0; i < src.bufQueue.length; i++) {
701-
var audioBuf = src.bufQueue[i].audioBuf;
702-
length += audioBuf ? audioBuf.duration : 0.0;
700+
for (var buf of src.bufQueue) {
701+
length += buf.audioBuf?.duration ?? 0.0;
703702
}
704703
return length;
705704
},
@@ -709,9 +708,7 @@ var LibraryOpenAL = {
709708

710709
var offset = 0.0;
711710
for (var i = 0; i < src.bufsProcessed; i++) {
712-
if (src.bufQueue[i].audioBuf) {
713-
offset += src.bufQueue[i].audioBuf.duration;
714-
}
711+
offset += src.bufQueue[i].audioBuf?.duration ?? 0.0;
715712
}
716713
offset += src.bufOffset;
717714

@@ -947,7 +944,7 @@ var LibraryOpenAL = {
947944
return;
948945
}
949946
var buf = AL.buffers[bufferId];
950-
if (!buf || bufferId === 0) {
947+
if (!buf || !bufferId) {
951948
#if OPENAL_DEBUG
952949
dbg(`${funcname}() called with an invalid buffer`);
953950
#endif
@@ -965,7 +962,7 @@ var LibraryOpenAL = {
965962
case 0x2004 /* AL_SIZE */:
966963
return buf.length * buf.bytesPerSample * buf.channels;
967964
case 0x2015 /* AL_LOOP_POINTS_SOFT */:
968-
if (buf.length === 0) {
965+
if (!buf.length) {
969966
return [0, 0];
970967
}
971968
return [
@@ -989,7 +986,7 @@ var LibraryOpenAL = {
989986
return;
990987
}
991988
var buf = AL.buffers[bufferId];
992-
if (!buf || bufferId === 0) {
989+
if (!buf || !bufferId) {
993990
#if OPENAL_DEBUG
994991
dbg(`${funcname}() called with an invalid buffer`);
995992
#endif
@@ -1006,7 +1003,7 @@ var LibraryOpenAL = {
10061003
10071004
switch (param) {
10081005
case 0x2004 /* AL_SIZE */:
1009-
if (value !== 0) {
1006+
if (value) {
10101007
#if OPENAL_DEBUG
10111008
dbg(`${funcname}() param AL_SIZE value ${value} is out of range`);
10121009
#endif
@@ -1093,12 +1090,12 @@ var LibraryOpenAL = {
10931090
case 0x1010 /* AL_SOURCE_STATE */:
10941091
return src.state;
10951092
case 0x1015 /* AL_BUFFERS_QUEUED */:
1096-
if (src.bufQueue.length === 1 && src.bufQueue[0].id === 0) {
1093+
if (src.bufQueue.length === 1 && !src.bufQueue[0].id) {
10971094
return 0;
10981095
}
10991096
return src.bufQueue.length;
11001097
case 0x1016 /* AL_BUFFERS_PROCESSED */:
1101-
if ((src.bufQueue.length === 1 && src.bufQueue[0].id === 0) || src.looping) {
1098+
if ((src.bufQueue.length === 1 && !src.bufQueue[0].id) || src.looping) {
11021099
return 0;
11031100
}
11041101
return src.bufsProcessed;
@@ -1131,17 +1128,17 @@ var LibraryOpenAL = {
11311128
case 0x2009 /* AL_BYTE_LENGTH_SOFT */:
11321129
var length = 0;
11331130
var bytesPerFrame = 0;
1134-
for (var i = 0; i < src.bufQueue.length; i++) {
1135-
length += src.bufQueue[i].length;
1136-
if (src.bufQueue[i].id !== 0) {
1137-
bytesPerFrame = src.bufQueue[i].bytesPerSample * src.bufQueue[i].channels;
1131+
for (var buf of src.bufQueue) {
1132+
length += buf.length;
1133+
if (buf.id) {
1134+
bytesPerFrame = buf.bytesPerSample * buf.channels;
11381135
}
11391136
}
11401137
return length * bytesPerFrame;
11411138
case 0x200A /* AL_SAMPLE_LENGTH_SOFT */:
11421139
var length = 0;
1143-
for (var i = 0; i < src.bufQueue.length; i++) {
1144-
length += src.bufQueue[i].length;
1140+
for (var buf of src.bufQueue) {
1141+
length += buf.length;
11451142
}
11461143
return length;
11471144
case 0x200B /* AL_SEC_LENGTH_SOFT */:
@@ -1318,8 +1315,8 @@ var LibraryOpenAL = {
13181315
}
13191316
13201317
if (value === 0) {
1321-
for (var i in src.bufQueue) {
1322-
src.bufQueue[i].refCount--;
1318+
for (var b of src.bufQueue) {
1319+
b.refCount--;
13231320
}
13241321
src.bufQueue.length = 1;
13251322
src.bufQueue[0] = AL.buffers[0];
@@ -1336,8 +1333,8 @@ var LibraryOpenAL = {
13361333
return;
13371334
}
13381335
1339-
for (var i in src.bufQueue) {
1340-
src.bufQueue[i].refCount--;
1336+
for (var b of src.bufQueue) {
1337+
b.refCount--;
13411338
}
13421339
src.bufQueue.length = 0;
13431340
@@ -1454,7 +1451,7 @@ var LibraryOpenAL = {
14541451
if (srcLen > 0.0) {
14551452
var frequency;
14561453
for (var buf of src.bufQueue) {
1457-
if (buf.id !== 0) {
1454+
if (buf.id) {
14581455
frequency = buf.frequency;
14591456
break;
14601457
}
@@ -1476,7 +1473,7 @@ var LibraryOpenAL = {
14761473
if (srcLen > 0.0) {
14771474
var bytesPerSec;
14781475
for (var buf of src.bufQueue) {
1479-
if (buf.id !== 0) {
1476+
if (buf.id) {
14801477
bytesPerSec = buf.frequency * buf.bytesPerSample * buf.channels;
14811478
break;
14821479
}
@@ -1561,7 +1558,7 @@ var LibraryOpenAL = {
15611558
// people might assume that most alcCapture functions
15621559
// accept NULL as a 'use the default' device.
15631560
requireValidCaptureDevice: (deviceId, funcname) => {
1564-
if (deviceId === 0) {
1561+
if (!deviceId) {
15651562
#if OPENAL_DEBUG
15661563
dbg(`${funcname}() on a NULL device is an error`);
15671564
#endif
@@ -1616,7 +1613,7 @@ var LibraryOpenAL = {
16161613
var resolvedDeviceName = AL.CAPTURE_DEVICE_NAME;
16171614
16181615
// NULL is a valid device name here (resolves to default);
1619-
if (pDeviceName !== 0) {
1616+
if (pDeviceName) {
16201617
resolvedDeviceName = UTF8ToString(pDeviceName);
16211618
if (resolvedDeviceName !== AL.CAPTURE_DEVICE_NAME) {
16221619
#if OPENAL_DEBUG
@@ -2088,7 +2085,7 @@ var LibraryOpenAL = {
20882085
while (true) {
20892086
attr = HEAP32[pAttrList++];
20902087
attrs.push(attr);
2091-
if (attr === 0) {
2088+
if (!attr) {
20922089
break;
20932090
}
20942091
val = HEAP32[pAttrList++];
@@ -2125,7 +2122,7 @@ var LibraryOpenAL = {
21252122
}
21262123
break;
21272124
case 0x1996 /* ALC_HRTF_ID_SOFT */:
2128-
if (val !== 0) {
2125+
if (val) {
21292126
#if OPENAL_DEBUG
21302127
dbg(`Invalid ALC_HRTF_ID_SOFT index ${val}`);
21312128
#endif
@@ -2254,20 +2251,11 @@ var LibraryOpenAL = {
22542251
},
22552252
22562253
alcGetCurrentContext__proxy: 'sync',
2257-
alcGetCurrentContext: () => {
2258-
if (AL.currentCtx !== null) {
2259-
return AL.currentCtx.id;
2260-
}
2261-
return 0;
2262-
},
2254+
alcGetCurrentContext: () => AL.currentCtx ? AL.currentCtx.id : 0,
22632255
22642256
alcMakeContextCurrent__proxy: 'sync',
22652257
alcMakeContextCurrent: (contextId) => {
2266-
if (contextId === 0) {
2267-
AL.currentCtx = null;
2268-
} else {
2269-
AL.currentCtx = AL.contexts[contextId];
2270-
}
2258+
AL.currentCtx = AL.contexts[contextId];
22712259
return {{{ cDefs.ALC_TRUE }}};
22722260
},
22732261
@@ -2295,7 +2283,7 @@ var LibraryOpenAL = {
22952283
// Spec says :
22962284
// Using a NULL handle is legal, but only the
22972285
// tokens defined by the AL core are guaranteed.
2298-
if (deviceId !== 0 && !(deviceId in AL.deviceRefCounts)) {
2286+
if (deviceId && !(deviceId in AL.deviceRefCounts)) {
22992287
#if OPENAL_DEBUG
23002288
dbg('alcGetEnumValue() called with an invalid device');
23012289
#endif
@@ -2399,7 +2387,7 @@ var LibraryOpenAL = {
23992387
ret = AL.CAPTURE_DEVICE_NAME;
24002388
break;
24012389
case 0x310 /* ALC_CAPTURE_DEVICE_SPECIFIER */:
2402-
if (deviceId === 0) {
2390+
if (!deviceId) {
24032391
ret = AL.CAPTURE_DEVICE_NAME + '\0';
24042392
} else {
24052393
var c = AL.requireValidCaptureDevice(deviceId, 'alcGetString');
@@ -2429,7 +2417,7 @@ var LibraryOpenAL = {
24292417
24302418
alcGetIntegerv__proxy: 'sync',
24312419
alcGetIntegerv: (deviceId, param, size, pValues) => {
2432-
if (size === 0 || !pValues) {
2420+
if (!size || !pValues) {
24332421
// Ignore the query, per the spec
24342422
return;
24352423
}
@@ -2620,7 +2608,7 @@ var LibraryOpenAL = {
26202608
var ret;
26212609
switch (param) {
26222610
case 0x1995 /* ALC_HRTF_SPECIFIER_SOFT */:
2623-
if (index === 0) {
2611+
if (!index) {
26242612
ret = 'Web Audio HRTF';
26252613
} else {
26262614
#if OPENAL_DEBUG
@@ -2631,7 +2619,7 @@ var LibraryOpenAL = {
26312619
}
26322620
break;
26332621
default:
2634-
if (index !== 0) {
2622+
if (index) {
26352623
#if OPENAL_DEBUG
26362624
dbg(`alcGetStringiSOFT() with param ${ptrToString(param)} not implemented yet`);
26372625
#endif
@@ -2664,7 +2652,7 @@ var LibraryOpenAL = {
26642652
var val = 0;
26652653
while (true) {
26662654
attr = HEAP32[pAttrList++];
2667-
if (attr === 0) {
2655+
if (!attr) {
26682656
break;
26692657
}
26702658
val = HEAP32[pAttrList++];
@@ -2740,8 +2728,8 @@ var LibraryOpenAL = {
27402728
27412729
for (var i = 0; i < count; ++i) {
27422730
var bufId = {{{ makeGetValue('pBufferIds', 'i*4', 'i32') }}};
2743-
/// Deleting the zero buffer is a legal NOP, so ignore it
2744-
if (bufId === 0) {
2731+
// Deleting the zero buffer is a legal NOP, so ignore it
2732+
if (!bufId) {
27452733
continue;
27462734
}
27472735
@@ -2766,7 +2754,7 @@ var LibraryOpenAL = {
27662754
27672755
for (var i = 0; i < count; ++i) {
27682756
var bufId = {{{ makeGetValue('pBufferIds', 'i*4', 'i32') }}};
2769-
if (bufId === 0) {
2757+
if (!bufId) {
27702758
continue;
27712759
}
27722760
@@ -3250,7 +3238,7 @@ var LibraryOpenAL = {
32503238
case {{{ cDefs.AL_DOPPLER_FACTOR }}}:
32513239
case {{{ cDefs.AL_SPEED_OF_SOUND }}}:
32523240
case {{{ cDefs.AL_DISTANCE_MODEL }}}:
3253-
return val !== 0 ? {{{ cDefs.AL_TRUE }}} : {{{ cDefs.AL_FALSE }}};
3241+
return val ? {{{ cDefs.AL_TRUE }}} : {{{ cDefs.AL_FALSE }}};
32543242
default:
32553243
#if OPENAL_DEBUG
32563244
dbg(`alGetBoolean(): param ${ptrToString(param)} has wrong signature`);
@@ -4032,14 +4020,14 @@ var LibraryOpenAL = {
40324020
return;
40334021
}
40344022
4035-
if (count === 0) {
4023+
if (!count) {
40364024
return;
40374025
}
40384026
40394027
// Find the first non-zero buffer in the queue to determine the proper format
40404028
var templateBuf = AL.buffers[0];
40414029
for (var buf of src.bufQueue) {
4042-
if (buf.id !== 0) {
4030+
if (buf.id) {
40434031
templateBuf = buf;
40444032
break;
40454033
}
@@ -4057,7 +4045,7 @@ var LibraryOpenAL = {
40574045
}
40584046
40594047
// Check that the added buffer has the correct format. If the template is the zero buffer, any format is valid.
4060-
if (templateBuf.id !== 0 && (
4048+
if (templateBuf.id && (
40614049
buf.frequency !== templateBuf.frequency
40624050
|| buf.bytesPerSample !== templateBuf.bytesPerSample
40634051
|| buf.channels !== templateBuf.channels)
@@ -4070,7 +4058,7 @@ var LibraryOpenAL = {
40704058
}
40714059
40724060
// If the only buffer in the queue is the zero buffer, clear the queue before we add anything.
4073-
if (src.bufQueue.length === 1 && src.bufQueue[0].id === 0) {
4061+
if (src.bufQueue.length === 1 && !src.bufQueue[0].id) {
40744062
src.bufQueue.length = 0;
40754063
}
40764064
@@ -4107,12 +4095,12 @@ var LibraryOpenAL = {
41074095
AL.currentCtx.err = {{{ cDefs.AL_INVALID_NAME }}};
41084096
return;
41094097
}
4110-
if (count > (src.bufQueue.length === 1 && src.bufQueue[0].id === 0 ? 0 : src.bufsProcessed)) {
4098+
if (count > (src.bufQueue.length === 1 && !src.bufQueue[0].id ? 0 : src.bufsProcessed)) {
41114099
AL.currentCtx.err = {{{ cDefs.AL_INVALID_VALUE }}};
41124100
return;
41134101
}
41144102
4115-
if (count === 0) {
4103+
if (!count) {
41164104
return;
41174105
}
41184106
@@ -4125,7 +4113,7 @@ var LibraryOpenAL = {
41254113
}
41264114
41274115
/// If the queue is empty, put the zero buffer back in
4128-
if (src.bufQueue.length === 0) {
4116+
if (!src.bufQueue.length) {
41294117
src.bufQueue.push(AL.buffers[0]);
41304118
}
41314119

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 268215,
2+
"a.out.js": 267949,
33
"a.out.nodebug.wasm": 588309,
4-
"total": 856524,
4+
"total": 856258,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

tools/maint/sync_deps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def sync_repo(name, repo_dir, revision, url):
9898
return
9999

100100
if is_dirty(repo_dir):
101-
utils.exit_with_error("Directory for {name} is dirty: '{repo_dir}'")
101+
utils.exit_with_error(f"Directory for {name} is dirty: '{repo_dir}'")
102102

103103
if not has_revision(repo_dir, revision):
104104
print(' Fetching revision')

0 commit comments

Comments
 (0)