Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2821,6 +2821,7 @@ var LibrarySDL = {
Mix_ReserveChannels: (num) => {
SDL.channelMinimumNumber = num;
},
Mix_PlayChannelTimed__deps: ['Mix_HaltChannel'],
Mix_PlayChannelTimed__proxy: 'sync',
Mix_PlayChannelTimed: (channel, id, loops, ticks) => {
// TODO: handle fixed amount of N loops. Currently loops either 0 or infinite times.
Expand Down Expand Up @@ -2864,9 +2865,14 @@ var LibrarySDL = {
audio.frequency = info.audio.frequency;
}
audio['onended'] = function() { // TODO: cache these
if (channelInfo.audio == this) { channelInfo.audio.paused = true; channelInfo.audio = null; }
if (channelInfo.audio === this || channelInfo.audio.webAudioNode === this) {
channelInfo.audio.paused = true; channelInfo.audio = null;
}
if (SDL.channelFinished) {{{ makeDynCall('vi', 'SDL.channelFinished') }}}(channel);
}
if (channelInfo.audio) {
_Mix_HaltChannel(channel);
}
channelInfo.audio = audio;
// TODO: handle N loops. Behavior matches Mix_PlayMusic
audio.loop = loops != 0;
Expand Down Expand Up @@ -2946,7 +2952,11 @@ var LibrarySDL = {
} else if (info.audio) { // Play via the <audio> element
audio = info.audio;
}
audio['onended'] = function() { if (SDL.music.audio == this) _Mix_HaltMusic(); } // will send callback
audio['onended'] = function() {
if (SDL.music.audio === this || SDL.music.audio?.webAudioNode === this) {
_Mix_HaltMusic(); // will send callback
}
}
audio.loop = loops != 0 && loops != 1; // TODO: handle N loops for finite N
audio.volume = SDL.music.volume;
SDL.music.audio = audio;
Expand Down
42 changes: 42 additions & 0 deletions test/browser/test_sdl_audio_mix_channels_halt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <SDL/SDL_mixer.h>
#include <emscripten.h>

Mix_Chunk* chunk = 0;
uint64_t started = 0;
uint64_t lastRun = 0;

void loop() {
uint64_t now = emscripten_get_now();
if (now - lastRun > 300) {
Mix_PlayChannel(0, chunk, -1);
lastRun = now;
}
if (now - started > 3000) {
emscripten_force_exit(0);
}
}

int main(int argc, char** argv) {
if ((Mix_Init(MIX_INIT_OGG) & MIX_INIT_OGG) != MIX_INIT_OGG) {
printf("ERR! Mix_Init: Failed to init with required flags support!\n");
printf("ERR! Mix_Init: %s\n", Mix_GetError());
return 1;
}

if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 1024) == -1) {
printf("ERR! Mix_OpenAudio: %s\n", Mix_GetError());
return 1;
}

Mix_AllocateChannels(1);
chunk = Mix_LoadWAV("/the_entertainer.ogg");
if (!chunk) {
printf("ERR! Mix_LoadWAV: %s\n", Mix_GetError());
return 1;
}

printf("The sound should reset to start every 300ms, if you hear overapplied music then the test has failed.\n");
started = emscripten_get_now();
emscripten_set_main_loop(loop, 0, 0);
return 0;
}
42 changes: 42 additions & 0 deletions test/browser/test_sdl_audio_mix_playing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <SDL/SDL_mixer.h>
#include <emscripten.h>

Mix_Chunk* chunk = 0;
uint64_t started = 0;

void loop() {
uint64_t now = emscripten_get_now();
if (now - started > 2000) {
// length of sound is 1sec
printf("isPlaying? %s\n", Mix_Playing(0) ? "yes" : "no");
emscripten_force_exit(Mix_Playing(0) ? 1 : 0);
}
}

int main(int argc, char** argv) {
if ((Mix_Init(MIX_INIT_OGG) & MIX_INIT_OGG) != MIX_INIT_OGG) {
printf("ERR! Mix_Init: Failed to init with required flags support!\n");
printf("ERR! Mix_Init: %s\n", Mix_GetError());
return 1;
}

if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 1024) == -1) {
printf("ERR! Mix_OpenAudio: %s\n", Mix_GetError());
return 1;
}

Mix_AllocateChannels(1);
chunk = Mix_LoadWAV("/noise.ogg");
if (!chunk) {
printf("ERR! Mix_LoadWAV: %s\n", Mix_GetError());
return 1;
}
Mix_PlayChannel(0, chunk, 0);
if (!Mix_Playing(0)) {
printf("ERR! Channel is not playing\n");
return 1;
}
started = emscripten_get_now();
emscripten_set_main_loop(loop, 0, 0);
return 0;
}
10 changes: 10 additions & 0 deletions test/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def test_sdl_audio_mix_channels(self, args):

self.btest_exit('test_sdl_audio_mix_channels.c', args=['-O2', '--minify=0', '--preload-file', 'sound.ogg'] + args)

def test_sdl_audio_mix_channels_halt(self):
shutil.copy(test_file('sounds/the_entertainer.ogg'), '.')

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

def test_sdl_audio_mix_playing(self):
shutil.copy(test_file('sounds/noise.ogg'), '.')

self.btest_exit('test_sdl_audio_mix_playing.c', args=['-O2', '--minify=0', '--preload-file', 'noise.ogg'])

@parameterized({
'': ([],),
'wasmfs': (['-sWASMFS'],),
Expand Down
Loading