diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-06-25 20:41:56 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-06-25 20:41:56 -0700 |
commit | 6c4cc9b0b8e1f6451fc4d376ef8260979f169c05 (patch) | |
tree | c3bdbb2d2da8a91c8678a295f8326b6477f89c20 | |
parent | d1b22871c34dc9b0d5ea88f991602666d9143011 (diff) | |
parent | 5940f3189736ba5a935b35485feac1d0ec00f7fc (diff) |
Merge pull request #490 from ehsan/audio
Implement Mix_QuickLoad_RAW
-rw-r--r-- | src/library_sdl.js | 37 | ||||
-rwxr-xr-x | tests/runner.py | 7 | ||||
-rw-r--r-- | tests/sdl_audio_quickload.c | 44 |
3 files changed, 86 insertions, 2 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 41898056..78f8e1e1 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -23,6 +23,10 @@ var LibrarySDL = { audio: null, volume: 1.0 }, + mixerFrequency: 22050, + mixerFormat: 0x8010, // AUDIO_S16LSB + mixerNumChannels: 2, + mixerChunkSize: 1024, keyboardState: null, shiftKey: false, @@ -1065,6 +1069,10 @@ var LibrarySDL = { Mix_OpenAudio: function(frequency, format, channels, chunksize) { SDL.allocateChannels(32); + SDL.mixerFrequency = frequency; + SDL.mixerFormat = format; + SDL.mixerNumChannels = channels; + SDL.mixerChunkSize = chunksize; return 0; }, @@ -1108,6 +1116,23 @@ var LibrarySDL = { return id; }, + Mix_QuickLoad_RAW: function(mem, len) { + var audio = new Audio(); + audio['mozSetup'](SDL.mixerNumChannels, SDL.mixerFrequency); + var numSamples = (len / (SDL.mixerNumChannels * 2)) | 0; + var buffer = new Float32Array(numSamples); + for (var i = 0; i < numSamples; ++i) { + buffer[i] = ({{{ makeGetValue('mem', 'i*2', 'i16', 0, 0) }}}) / 0x8000; // hardcoded 16-bit audio, signed (TODO: reSign if not ta2?) + } + var id = SDL.audios.length; + SDL.audios.push({ + source: '', + audio: audio, + buffer: buffer + }); + return id; + }, + Mix_FreeChunk: function(id) { SDL.audios[id] = null; }, @@ -1132,7 +1157,11 @@ var LibrarySDL = { Runtime.getFuncWrapper(SDL.channelFinished)(channel); } } - info.audio.play(); + if (SDL.audios[id].buffer) { + audio["mozWriteAudio"](SDL.audios[id].buffer); + } else { + info.audio.play(); + } info.audio.volume = info.volume; return channel; }, @@ -1176,7 +1205,11 @@ var LibrarySDL = { var audio = SDL.audios[id].audio; if (!audio) return 0; audio.loop = loops != 1; // TODO: handle N loops for finite N - audio.play(); + if (SDL.audios[id].buffer) { + audio["mozWriteAudio"](SDL.audios[id].buffer); + } else { + audio.play(); + } audio.volume = SDL.music.volume; audio['onended'] = _Mix_HaltMusic; // will send callback SDL.music.audio = audio; diff --git a/tests/runner.py b/tests/runner.py index 89ea2099..08086e2f 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -7649,6 +7649,13 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play", "_play2"]']).communicate() self.run_browser('page.html', '', '/report_result?1') + def test_sdl_audio_quickload(self): + open(os.path.join(self.get_dir(), 'sdl_audio_quickload.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_audio_quickload.c')).read())) + + # use closure to check for a possible bug with closure minifying away newer Audio() attributes + Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio_quickload.c'), '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play"]']).communicate() + self.run_browser('page.html', '', '/report_result?1') + def test_sdl_gl_read(self): # SDL, OpenGL, readPixels open(os.path.join(self.get_dir(), 'sdl_gl_read.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_gl_read.c')).read())) diff --git a/tests/sdl_audio_quickload.c b/tests/sdl_audio_quickload.c new file mode 100644 index 00000000..1525d048 --- /dev/null +++ b/tests/sdl_audio_quickload.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <SDL/SDL.h> +#include <SDL/SDL_mixer.h> +#include <assert.h> +#include <limits.h> +#include <emscripten.h> + +Mix_Chunk *sound; + +void play() { + int channel = Mix_PlayChannel(-1, sound, 1); + assert(channel == 0); + + int result = 1; + REPORT_RESULT(); +} + +int main(int argc, char **argv) { + SDL_Init(SDL_INIT_AUDIO); + + int ret = Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); + assert(ret == 0); + + Uint16* buffer = (Uint16*)malloc(10*44100*sizeof(Uint16)); + for (Uint32 i = 0; i < 10*44100; ++i) { + buffer[i] = (i * 5) % UINT32_MAX; + } + sound = Mix_QuickLoad_RAW((Uint8*) buffer, 10*44100*sizeof(Uint16)); + assert(sound); + + play(); + + emscripten_run_script("element = document.createElement('input');" + "element.setAttribute('type', 'button');" + "element.setAttribute('value', 'replay!');" + "element.setAttribute('onclick', 'Module[\"_play\"]()');" + "document.body.appendChild(element);"); + + printf("you should one sounds. press the button to replay!\n"); + + return 0; +} + |