aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael J. Bishop <mbtyke@gmail.com>2013-06-19 19:30:17 -0400
committerMichael J. Bishop <mbtyke@gmail.com>2013-06-19 19:30:17 -0400
commit1911d7bdd0c4571a0cbc0a97321cc1c4dbdb863e (patch)
treec1dfac828e5b16477f953379e3eafb65e055a0cf
parente4f8c8184058d8d621d8f3ac007a0d1a55980832 (diff)
Fixed error where SDL_Mixer would run out of channels because sound
instances weren't removed when they were finished playing.
-rw-r--r--src/library_sdl.js25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 356c9746..4e75f594 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);
}
}
@@ -1475,6 +1477,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 +1516,13 @@ 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.paused = true;
+ }
SDL.music.audio = audio;
return 0;
},
@@ -1520,6 +1531,7 @@ var LibrarySDL = {
var audio = SDL.music.audio;
if (!audio) return 0;
audio.pause();
+ audio.paused = true;
return 0;
},
@@ -1527,6 +1539,7 @@ var LibrarySDL = {
var audio = SDL.music.audio;
if (!audio) return 0;
audio.play();
+ audio.paused = false;
return 0;
},
@@ -1578,6 +1591,8 @@ var LibrarySDL = {
if (info && info.audio) {
info.audio.pause();
info.audio.paused = true;
+ } else {
+ Module.printErr('Mix_Pause: no sound found for channel: ' + channel);
}
},