diff options
-rw-r--r-- | src/library_sdl.js | 24 | ||||
-rwxr-xr-x | tests/runner.py | 7 | ||||
-rw-r--r-- | tests/sdl_audio_mix_channels.c | 59 |
3 files changed, 83 insertions, 7 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 356c9746..176a2fae 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -1385,26 +1385,28 @@ var LibrarySDL = { // If the user asks us to allocate a channel automatically, get the first // free one. if (channel == -1) { - channel = SDL.channelMinimumNumber; for (var i = SDL.channelMinimumNumber; i < SDL.numChannels; i++) { if (!SDL.channels[i].audio) { channel = i; break; } } + if (channel == -1) { + Module.printErr('All ' + SDL.numChannels + ' channels in use!'); + return -1; + } } - // We clone the audio node to utilize the preloaded audio buffer, since // the browser has already preloaded the audio file. var channelInfo = SDL.channels[channel]; channelInfo.audio = audio = audio.cloneNode(true); audio.numChannels = info.audio.numChannels; audio.frequency = info.audio.frequency; - // TODO: handle N loops. Behavior matches Mix_PlayMusic audio.loop = loops != 0; - if (SDL.channelFinished) { - audio['onended'] = function() { // TODO: cache these + audio['onended'] = function() { // TODO: cache these + channelInfo.audio = null; + if (SDL.channelFinished) { Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel); } } @@ -1461,7 +1463,6 @@ var LibrarySDL = { audio.play(); } audio.volume = channelInfo.volume; - audio.paused = false; return channel; }, Mix_PlayChannelTimed: 'Mix_PlayChannel', // XXX ignore Timing @@ -1475,6 +1476,8 @@ var LibrarySDL = { if (info.audio) { info.audio.pause(); info.audio = null; + } else { + Module.printErr('No Audio for channel: ' + channel); } if (SDL.channelFinished) { Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel); @@ -1512,6 +1515,12 @@ var LibrarySDL = { } audio.volume = SDL.music.volume; audio['onended'] = _Mix_HaltMusic; // will send callback + if (SDL.music.audio) { + if (!SDL.music.audio.paused) { + Module.printErr('Music is already playing. ' + SDL.music.source); + } + SDL.music.audio.pause(); + } SDL.music.audio = audio; return 0; }, @@ -1577,7 +1586,8 @@ var LibrarySDL = { var info = SDL.channels[channel]; if (info && info.audio) { info.audio.pause(); - info.audio.paused = true; + } else { + Module.printErr('Mix_Pause: no sound found for channel: ' + channel); } }, diff --git a/tests/runner.py b/tests/runner.py index 672ae456..637d2e52 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -12406,6 +12406,13 @@ elif 'browser' in str(sys.argv): Popen([PYTHON, EMCC, '-O2', '--closure', '1', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '--preload-file', 'bad.ogg', '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play", "_play2"]']).communicate() self.run_browser('page.html', '', '/report_result?1') + def test_sdl_audio_mix_channels(self): + shutil.copyfile(path_from_root('tests', 'sounds', 'noise.ogg'), os.path.join(self.get_dir(), 'sound.ogg')) + open(os.path.join(self.get_dir(), 'sdl_audio_mix_channels.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_audio_mix_channels.c')).read())) + + Popen([PYTHON, EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio_mix_channels.c'), '--preload-file', 'sound.ogg', '-o', 'page.html']).communicate() + self.run_browser('page.html', '', '/report_result?1') + def test_sdl_audio_mix(self): shutil.copyfile(path_from_root('tests', 'sounds', 'pluck.ogg'), os.path.join(self.get_dir(), 'sound.ogg')) shutil.copyfile(path_from_root('tests', 'sounds', 'the_entertainer.ogg'), os.path.join(self.get_dir(), 'music.ogg')) diff --git a/tests/sdl_audio_mix_channels.c b/tests/sdl_audio_mix_channels.c new file mode 100644 index 00000000..dd91d594 --- /dev/null +++ b/tests/sdl_audio_mix_channels.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <SDL/SDL.h> +#include <SDL/SDL_mixer.h> +#include <assert.h> +#include <emscripten.h> + +static Mix_Chunk *sound = NULL; +static Mix_Chunk *noiseLoop = NULL; +static Mix_Music *music = NULL; + +static int soundChannel = 0; +static int noiseLoopChannel = 0; + +static const int kNumChannels = 40; + +static int loadAndPlay() +{ + return Mix_PlayChannel(-1, sound, -1); +} + +int main(int argc, char **argv) { + SDL_Init(SDL_INIT_AUDIO); + Mix_Init(MIX_INIT_OGG); + + int ret = Mix_OpenAudio(0, 0, 0, 0); // we ignore all these.. + assert(ret == 0); + + Mix_AllocateChannels(kNumChannels); + + sound = Mix_LoadWAV("sound.ogg"); + + // allocate all the channels + for ( int i = 0; i < kNumChannels; i++ ) + { + assert(loadAndPlay() != -1); + } + + // This point, we should have exhausted our channels + + + + + int lastChannel = loadAndPlay(); + +#if EMSCRIPTEN + int result = (lastChannel == -1); + REPORT_RESULT(); +#endif + + assert(lastChannel == -1); + + // force a quit + while(Mix_Init(0)) + Mix_Quit(); + Mix_CloseAudio(); + + return 0; +} + |