Skip to content

Commit 5acebea

Browse files
committed
SDL_mixer, fix onended callback to set correct audio status
1 parent abcf6bc commit 5acebea

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/library_sdl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,7 +2861,7 @@ var LibrarySDL = {
28612861
audio.frequency = info.audio.frequency;
28622862
}
28632863
audio['onended'] = function() { // TODO: cache these
2864-
if (channelInfo.audio == this) { channelInfo.audio.paused = true; channelInfo.audio = null; }
2864+
if (channelInfo.audio === this || channelInfo.audio.webAudioNode === this) { channelInfo.audio.paused = true; channelInfo.audio = null; }
28652865
if (SDL.channelFinished) {{{ makeDynCall('vi', 'SDL.channelFinished') }}}(channel);
28662866
}
28672867
if (channelInfo.audio) {
@@ -2946,7 +2946,7 @@ var LibrarySDL = {
29462946
} else if (info.audio) { // Play via the <audio> element
29472947
audio = info.audio;
29482948
}
2949-
audio['onended'] = function() { if (SDL.music.audio == this) _Mix_HaltMusic(); } // will send callback
2949+
audio['onended'] = function() { if (SDL.music.audio === this || SDL.music.audio.webAudioNode === this) _Mix_HaltMusic(); } // will send callback
29502950
audio.loop = loops != 0 && loops != 1; // TODO: handle N loops for finite N
29512951
audio.volume = SDL.music.volume;
29522952
SDL.music.audio = audio;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <SDL/SDL_mixer.h>
2+
#include <chrono>
3+
#include <emscripten.h>
4+
5+
using namespace std::chrono;
6+
7+
Mix_Chunk* chunk = nullptr;
8+
uint64_t started = 0;
9+
10+
void loop() {
11+
uint64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
12+
if (now - started > 2000) {
13+
// length of sound is 1sec
14+
printf("isPlaying? %s\n", Mix_Playing(0) ? "yes" : "no");
15+
emscripten_force_exit(Mix_Playing(0) ? -1 : 0);
16+
}
17+
}
18+
19+
int main(int argc, char** argv) {
20+
if ((Mix_Init(MIX_INIT_OGG) & MIX_INIT_OGG) != MIX_INIT_OGG) {
21+
printf("ERR! Mix_Init: Failed to init with required flags support!\n");
22+
printf("ERR! Mix_Init: %s\n", Mix_GetError());
23+
return -1;
24+
}
25+
26+
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 1024) == -1) {
27+
printf("ERR! Mix_OpenAudio: %s\n", Mix_GetError());
28+
return -1;
29+
}
30+
31+
Mix_AllocateChannels(1);
32+
chunk = Mix_LoadWAV("/sound.ogg");
33+
if (!chunk) {
34+
printf("ERR! Mix_LoadWAV: %s\n", Mix_GetError());
35+
return -1;
36+
}
37+
Mix_PlayChannel(0, chunk, 0);
38+
if (!Mix_Playing(0)) {
39+
printf("ERR! Channel is not playing\n");
40+
return -1;
41+
}
42+
started = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
43+
emscripten_set_main_loop(loop, 0, 0);
44+
return 0;
45+
}

test/test_interactive.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ def test_sdl_audio_mix_channels_halt(self, args):
9191

9292
self.btest_exit('test_sdl_audio_mix_channels_halt.cpp', args=['-O2', '--minify=0', '--preload-file', 'the_entertainer.ogg'] + args)
9393

94+
@parameterized({
95+
'': ([],),
96+
'wasmfs': (['-sWASMFS'],),
97+
})
98+
def test_sdl_audio_mix_playing(self, args):
99+
shutil.copyfile(test_file('sounds/noise.ogg'), self.in_dir('sound.ogg'))
100+
101+
self.btest_exit('test_sdl_audio_mix_playing.cpp', args=['-O2', '--minify=0', '--preload-file', 'sound.ogg'] + args)
102+
94103
@parameterized({
95104
'': ([],),
96105
'wasmfs': (['-sWASMFS'],),

0 commit comments

Comments
 (0)