From 7e5f79769d837afaea214dc3fb9b8a1ed7d1642d Mon Sep 17 00:00:00 2001 From: uniformization <124226059+uniformization@users.noreply.github.com> Date: Thu, 23 Apr 2026 21:21:19 -0700 Subject: [PATCH 1/3] Fix sourceSeek crash on stopped sources --- src/lib/libopenal.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/libopenal.js b/src/lib/libopenal.js index 6142a67b41a62..e22dd5da79e63 100644 --- a/src/lib/libopenal.js +++ b/src/lib/libopenal.js @@ -724,13 +724,17 @@ var LibraryOpenAL = { AL.setSourceState(src, {{{ cDefs.AL_INITIAL }}}); } - if (src.bufQueue[src.bufsProcessed].audioBuf !== null) { - src.bufsProcessed = 0; - while (offset > src.bufQueue[src.bufsProcessed].audioBuf.duration) { + src.bufsProcessed = 0; + if (src.bufQueue.length > 0 && src.bufQueue[0].audioBuf !== null) { + while (src.bufsProcessed < src.bufQueue.length && offset > src.bufQueue[src.bufsProcessed].audioBuf.duration) { offset -= src.bufQueue[src.bufsProcessed].audioBuf.duration; src.bufsProcessed++; } + if (src.bufsProcessed >= src.bufQueue.length) { + src.bufsProcessed = src.bufQueue.length -1; + } + src.bufOffset = offset; } From 723c98a8d51104cadb5a0cbd0d08c1747c0188ee Mon Sep 17 00:00:00 2001 From: uniformization <124226059+uniformization@users.noreply.github.com> Date: Sat, 25 Apr 2026 10:53:54 -0700 Subject: [PATCH 2/3] Add AL_SEC_OFFSET test for stopped sounds --- test/openal/test_openal_playback.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/openal/test_openal_playback.c b/test/openal/test_openal_playback.c index 8aac17ce3e31b..6cc752c1cc2d4 100644 --- a/test/openal/test_openal_playback.c +++ b/test/openal/test_openal_playback.c @@ -54,6 +54,9 @@ void playSource(void* arg) { alSourceStop(source); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); + alSourcef(source, AL_SEC_OFFSET, 0.5f); + alGetSourcei(source, AL_SOURCE_STATE, &state); + assert(state == AL_STOPPED); #endif alSourceRewindv(1, &source); @@ -69,6 +72,9 @@ void playSource(void* arg) { alSourceStopv(1, &source); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); + alSourcef(source, AL_SEC_OFFSET, 0.5f); + alGetSourcei(source, AL_SOURCE_STATE, &state); + assert(state == AL_STOPPED); test_finished(); #endif } From d67e7d876434df87ef7a691275eb5432aeb7c6ec Mon Sep 17 00:00:00 2001 From: ts <124226059+uniformization@users.noreply.github.com> Date: Sat, 25 Apr 2026 12:02:14 -0700 Subject: [PATCH 3/3] Add changes according the review --- src/lib/libopenal.js | 6 +----- test/openal/test_openal_playback.c | 6 +++++- test/test_interactive.py | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib/libopenal.js b/src/lib/libopenal.js index e22dd5da79e63..2874544019b3d 100644 --- a/src/lib/libopenal.js +++ b/src/lib/libopenal.js @@ -725,16 +725,12 @@ var LibraryOpenAL = { } src.bufsProcessed = 0; - if (src.bufQueue.length > 0 && src.bufQueue[0].audioBuf !== null) { + if (src.bufQueue.length && src.bufQueue[0].audioBuf) { while (src.bufsProcessed < src.bufQueue.length && offset > src.bufQueue[src.bufsProcessed].audioBuf.duration) { offset -= src.bufQueue[src.bufsProcessed].audioBuf.duration; src.bufsProcessed++; } - if (src.bufsProcessed >= src.bufQueue.length) { - src.bufsProcessed = src.bufQueue.length -1; - } - src.bufOffset = offset; } diff --git a/test/openal/test_openal_playback.c b/test/openal/test_openal_playback.c index 6cc752c1cc2d4..524b534375c46 100644 --- a/test/openal/test_openal_playback.c +++ b/test/openal/test_openal_playback.c @@ -54,9 +54,11 @@ void playSource(void* arg) { alSourceStop(source); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); +#ifdef TEST_STOPPED_SEEK alSourcef(source, AL_SEC_OFFSET, 0.5f); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); +#endif #endif alSourceRewindv(1, &source); @@ -72,9 +74,11 @@ void playSource(void* arg) { alSourceStopv(1, &source); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); +#ifdef TEST_STOPPED_SEEK alSourcef(source, AL_SEC_OFFSET, 0.5f); alGetSourcei(source, AL_SOURCE_STATE, &state); assert(state == AL_STOPPED); +#endif test_finished(); #endif } @@ -299,7 +303,7 @@ int main() { printf("You should hear a short audio clip playing back.\n"); #endif -#ifdef __EMSCRIPTEN__ +#if defined(__EMSCRIPTEN__) && !defined(TEST_STOPPED_SEEK) intptr_t first_src = sources[0]; #if defined(TEST_LOOPED_PLAYBACK) diff --git a/test/test_interactive.py b/test/test_interactive.py index 7a4f90d6a73e9..a741e0c545021 100644 --- a/test/test_interactive.py +++ b/test/test_interactive.py @@ -160,6 +160,10 @@ def test_openal_playback(self, args): copy_asset('sounds/audio.wav') self.btest('openal/test_openal_playback.c', '1', cflags=['-O2', '--preload-file', 'audio.wav'] + args) + def test_openal_stopped_seek_playback(self): + copy_asset('sounds/audio.wav') + self.btest('openal/test_openal_playback.c', '1', cflags=['-DTEST_STOPPED_SEEK=1', '-O2', '--preload-file', 'audio.wav']) + def test_openal_buffers(self): self.btest_exit('openal/test_openal_buffers.c', cflags=['-DTEST_INTERACTIVE=1', '--preload-file', test_file('sounds/the_entertainer.wav') + '@/'])