aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-03-27 13:24:49 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-03-27 13:24:49 -0700
commit7450114c4d6c3cac7c262f72f04bb7b1ac72e674 (patch)
tree22eff2ff392dfd551a20251d1b85aa6ff9307314
parente39cb11407e0d646dca3cbea244ce4189680aa4a (diff)
support wav and mp3 audio suffixes too
-rwxr-xr-xemcc7
-rw-r--r--src/library_sdl.js20
-rwxr-xr-xtests/runner.py3
-rw-r--r--tests/sdl_audio.c16
-rw-r--r--tests/sounds/alarmcreatemiltaryfoot_1.wavbin0 -> 443856 bytes
5 files changed, 25 insertions, 21 deletions
diff --git a/emcc b/emcc
index 72895065..1015760c 100755
--- a/emcc
+++ b/emcc
@@ -330,9 +330,10 @@ LIB_PREFIXES = ('', 'lib')
IMAGE_SUFFIXES = ('.jpg', '.png', '.bmp')
AUDIO_SUFFIXES = ('.ogg', '.wav', '.mp3')
+AUDIO_MIMETYPES = { 'ogg': 'audio/ogg', 'wav': 'audio/wav', 'mp3': 'audio/mpeg' }
def suffix(name):
- return name.split('.')[:-1]
+ return name.split('.')[-1]
def unsuffixed(name):
return '.'.join(name.split('.')[:-1])
@@ -929,7 +930,7 @@ try:
''' % { 'filename': filename }
elif audio:
finish = '''
- var b = new Blob([byteArray.buffer], { type: 'audio/ogg' });
+ var b = new Blob([byteArray.buffer], { type: '%(mimetype)s' });
var url = URLObject.createObjectURL(b); // XXX we never revoke this!
var audio = new Audio();
audio.oncanplaythrough = function() {
@@ -941,7 +942,7 @@ try:
console.log('Audio %(filename)s could not be decoded');
};
audio.src = url;
-''' % { 'filename': filename }
+''' % { 'filename': filename, 'mimetype': AUDIO_MIMETYPES[suffix(filename)] }
else:
finish = 'removeRunDependency();\n'
diff --git a/src/library_sdl.js b/src/library_sdl.js
index db3771e9..2f3261fc 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -737,6 +737,7 @@ mergeInto(LibraryManager.library, {
assert(raw, 'Cannot find preloaded audio ' + filename);
var id = SDL.audios.length;
SDL.audios.push({
+ source: filename,
audio: raw
});
return id;
@@ -748,26 +749,15 @@ mergeInto(LibraryManager.library, {
},
Mix_PlayChannel: function(channel, id, loops) {
+ // TODO: handle loops
var audio = SDL.audios[id].audio;
audio.play();
- return 0; // XXX should return channel
+ return 1; // XXX should return channel
},
Mix_PlayChannelTimed: 'Mix_PlayChannel', // XXX ignore Timing
- Mix_LoadMUS: function(filename) {
- filename = FS.standardizePath(Pointer_stringify(filename));
- var id = SDL.audios.length;
- SDL.audios.push({
- audio: new Audio(filename)
- });
- return id;
- },
-
- Mix_FreeMusic: function(id) {
- SDL.audios[id].audio.pause();
- SDL.audios[id] = null;
- return 0;
- },
+ Mix_LoadMUS: 'Mix_LoadWAV_RW',
+ Mix_FreeMusic: 'Mix_FreeChunk',
Mix_PlayMusic: function(id, loops) {
if (loops == 0) return;
diff --git a/tests/runner.py b/tests/runner.py
index 1db6d3a7..6f94e6e2 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6447,9 +6447,10 @@ f.close()
def test_sdl_audio(self):
shutil.copyfile(path_from_root('tests', 'sounds', 'alarmvictory_1.ogg'), os.path.join(self.get_dir(), 'sound.ogg'))
+ shutil.copyfile(path_from_root('tests', 'sounds', 'alarmcreatemiltaryfoot_1.wav'), os.path.join(self.get_dir(), 'sound2.wav'))
open(os.path.join(self.get_dir(), 'sdl_audio.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_audio.c')).read()))
- Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '-o', 'page.html']).communicate()
+ Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '-o', 'page.html']).communicate()
self.run_browser('page.html', '', '/report_result?1')
def test_worker(self):
diff --git a/tests/sdl_audio.c b/tests/sdl_audio.c
index 2539b982..65e79006 100644
--- a/tests/sdl_audio.c
+++ b/tests/sdl_audio.c
@@ -4,11 +4,20 @@
#include <assert.h>
#include <emscripten.h>
-Mix_Chunk *sound;
+Mix_Chunk *sound, *sound2;
+
+void play2();
void play() {
int channel = Mix_PlayChannel(-1, sound, 1);
assert(channel >= 0);
+
+ emscripten_run_script("setTimeout(_play2, 1000)");
+}
+
+void play2() {
+ int channel2 = Mix_PlayChannel(-1, sound2, 1);
+ assert(channel2 >= 0);
}
int main() {
@@ -19,8 +28,11 @@ int main() {
sound = Mix_LoadWAV("sound.ogg");
assert(sound);
+ sound2 = Mix_LoadWAV("sound2.wav");
+ assert(sound);
play();
+ if (ret == 12121) play2(); // keep it alive
emscripten_run_script("element = document.createElement('input');"
"element.setAttribute('type', 'button');"
@@ -28,7 +40,7 @@ int main() {
"element.setAttribute('onclick', '_play()');"
"document.body.appendChild(element);");
- printf("you should hear a victory sound. press the button to replay!\n");
+ printf("you should hear two sounds. press the button to replay!\n");
int result = 1;
REPORT_RESULT();
diff --git a/tests/sounds/alarmcreatemiltaryfoot_1.wav b/tests/sounds/alarmcreatemiltaryfoot_1.wav
new file mode 100644
index 00000000..2de6314a
--- /dev/null
+++ b/tests/sounds/alarmcreatemiltaryfoot_1.wav
Binary files differ