diff options
author | Michael J. Bishop <mbtyke@gmail.com> | 2013-07-15 18:49:34 -0400 |
---|---|---|
committer | Michael J. Bishop <mbtyke@gmail.com> | 2013-07-15 18:49:34 -0400 |
commit | ed0c109645836520c244dcc7456cf8b8ba619de6 (patch) | |
tree | 28c0cf6dc1f43986cedfbd42c8f382c64aa31beb /src/library_sdl.js | |
parent | b87be16d64b81efd5c5e9187d3cd601441b4af31 (diff) |
Added support for loading .ogg audio files using a combination of
`SDL_RWFromConstMem()` + `Mix_LoadMUS/WAV_RW()`.
`SDL_RWFromConstMem()` now returns an opaque reference that corresponds
to a Javascript-side object which retains the information passed in.
The same for `SDL_RWFromFile()` but it saves different information.
When `Mix_LoadMUS/WAV_RW()` is called, it now expects a reference to
one of those Javascript-side objects and from it, either uses a
pre-loaded Audio, or reads bytes from the file-system to make one,
or uses the bytes originally passed into `SDL_RWFromConstMem()` to
create one.
Note that with this change you can now simply embed sound files in your
app, you do not need to preload them.
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r-- | src/library_sdl.js | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index b7d73862..c2e29fab 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -28,6 +28,7 @@ var LibrarySDL = { // The currently preloaded audio elements ready to be played audios: [null], + rwops: [null], // The currently playing audio element. There's only one music track. music: { audio: null, @@ -1392,22 +1393,64 @@ var LibrarySDL = { return 0; // error }, - Mix_LoadWAV_RW: function(filename, freesrc) { - filename = FS.standardizePath(Pointer_stringify(filename)); - var raw = Module["preloadedAudios"][filename]; - if (!raw) { - if (raw === null) Module.printErr('Trying to reuse preloaded audio, but freePreloadedMediaOnUse is set!'); - Runtime.warnOnce('Cannot find preloaded audio ' + filename); + Mix_LoadWAV_RW: function(rwopsID, freesrc) { + var rwops = SDL.rwops[rwopsID]; + + if ( rwops === undefined ) return 0; + + var filename = ''; + var audio; + var bytes; + + if ( rwops.filename != undefined ) + { + filename = rwops.filename; + filename = FS.standardizePath(Pointer_stringify(filename)); + var raw = Module["preloadedAudios"][filename]; + if (!raw) { + if (raw === null) Module.printErr('Trying to reuse preloaded audio, but freePreloadedMediaOnUse is set!'); + Runtime.warnOnce('Cannot find preloaded audio ' + filename); + + // see if we can read the file-contents from the in-memory FS + var fileObject = FS.findObject(filename); + + if (fileObject === null) Module.printErr('Couldn\'t find file for: ' + filename); + + // We found the file. Load the contents + if ( fileObject && !fileObject.isFolder && fileObject.read ) { + bytes = fileObject.contents + } + else { + return 0; + } + } + if (Module['freePreloadedMediaOnUse']) { + Module["preloadedAudios"][filename] = null; + } + audio = raw; } - if (Module['freePreloadedMediaOnUse']) { - Module["preloadedAudios"][filename] = null; + else if (rwops.bytes != undefined) { + bytes = HEAPU8.subarray(rwops.bytes, rwops.bytes + rwops.count); } + else { + return 0; + } + + // Here, we didn't find a preloaded audio but we either were passed a filepath for + // which we loaded bytes, or we were passed some bytes + if (audio === undefined && bytes) { + var blob = new Blob([new Uint8Array(bytes)], {type: 'audio/ogg'}); + var url = URL.createObjectURL(blob); + audio = new Audio(); + audio.src = url; + } + var id = SDL.audios.length; // Keep the loaded audio in the audio arrays, ready for playback SDL.audios.push({ source: filename, - audio: raw + audio: audio }); return id; }, @@ -1567,8 +1610,11 @@ var LibrarySDL = { return SDL.setGetVolume(SDL.music, volume); }, - Mix_LoadMUS: 'Mix_LoadWAV_RW', Mix_LoadMUS_RW: 'Mix_LoadWAV_RW', + Mix_LoadMUS__deps: ['Mix_LoadMUS_RW', 'SDL_RWFromFile'], + Mix_LoadMUS: function(file) { + return _Mix_LoadMUS_RW(_SDL_RWFromFile(file)); + }, Mix_FreeMusic: 'Mix_FreeChunk', @@ -1966,9 +2012,20 @@ var LibrarySDL = { // Misc SDL_InitSubSystem: function(flags) { return 0 }, + SDL_RWFromConstMem: function(mem, size) { + var id = SDL.rwops.length + SDL.rwops.push( {'bytes': mem, 'count': size} ); + return id; + }, SDL_RWFromFile: function(filename, mode) { - return filename; // XXX We just forward the filename + var id = SDL.rwops.length + SDL.rwops.push( {'filename': filename} ); + return id; + }, + + SDL_FreeRW: function(rwopsID) { + SDL.rwops[rwopsID] = null; }, SDL_EnableUNICODE: function(on) { @@ -1995,7 +2052,6 @@ var LibrarySDL = { SDL_GetThreadID: function() { throw 'SDL_GetThreadID' }, SDL_ThreadID: function() { throw 'SDL_ThreadID' }, SDL_AllocRW: function() { throw 'SDL_AllocRW: TODO' }, - SDL_FreeRW: function() { throw 'SDL_FreeRW: TODO' }, SDL_CondBroadcast: function() { throw 'SDL_CondBroadcast: TODO' }, SDL_CondWaitTimeout: function() { throw 'SDL_CondWaitTimeout: TODO' }, SDL_WM_IconifyWindow: function() { throw 'SDL_WM_IconifyWindow TODO' }, |