Add audio async

This commit is contained in:
Varun Patil 2020-05-04 18:27:45 +05:30
parent 2ea2f31c03
commit c94323ec16
6 changed files with 41 additions and 4 deletions

View File

@ -461,7 +461,7 @@ SET(EMS_FLAGS " -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_ZLIB=1 -s USE_OGG=1 -s US
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} -s TOTAL_MEMORY=1073741824 -lopenal --preload-file game -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 -s 'ASYNCIFY_IMPORTS=[\"load_file_async\"]'") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} -s TOTAL_MEMORY=1073741824 -lopenal --preload-file game -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 -s 'ASYNCIFY_IMPORTS=[\"load_file_async_js\"]'")
set_target_properties( set_target_properties(
${PROJECT_NAME} ${PROJECT_NAME}

View File

@ -35,6 +35,10 @@
#include <SDL_thread.h> #include <SDL_thread.h>
#include <SDL_timer.h> #include <SDL_timer.h>
#ifdef __EMSCRIPTEN__
#include "emscripten.hpp"
#endif
ALStream::ALStream(LoopMode loopMode, ALStream::ALStream(LoopMode loopMode,
const std::string &threadId) const std::string &threadId)
: looped(loopMode == Looped), : looped(loopMode == Looped),
@ -256,6 +260,10 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler
void ALStream::openSource(const std::string &filename) void ALStream::openSource(const std::string &filename)
{ {
#ifdef __EMSCRIPTEN__
load_file_async(filename.c_str());
#endif
ALStreamOpenHandler handler(srcOps, looped); ALStreamOpenHandler handler(srcOps, looped);
shState->fileSystem().openRead(handler, filename.c_str()); shState->fileSystem().openRead(handler, filename.c_str());
source = handler.source; source = handler.source;

View File

@ -248,7 +248,7 @@ struct BitmapOpenHandler : FileSystem::OpenHandler
bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath) bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath)
{ {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
load_file_async(fullPath); load_file_async_js(fullPath);
surf = IMG_Load(fullPath); surf = IMG_Load(fullPath);
#else #else
surf = IMG_LoadTyped_RW(&ops, 1, ext); surf = IMG_LoadTyped_RW(&ops, 1, ext);

View File

@ -1,6 +1,8 @@
#include "emscripten.hpp" #include "emscripten.hpp"
#include "filesystem.h"
#include "sharedstate.h"
EM_JS(void, load_file_async, (const char* fullPathC), { EM_JS(void, load_file_async_js, (const char* fullPathC), {
Asyncify.handleSleep(function(wakeUp) { Asyncify.handleSleep(function(wakeUp) {
const fullPath = UTF8ToString(fullPathC); const fullPath = UTF8ToString(fullPathC);
@ -31,3 +33,20 @@ EM_JS(void, load_file_async, (const char* fullPathC), {
}); });
}); });
struct LoadOpenHandler : FileSystem::OpenHandler
{
LoadOpenHandler()
{}
bool tryRead(SDL_RWops &ops, const char *ext, const char *fullPath)
{
load_file_async_js(fullPath);
return true;
}
};
void load_file_async(const char * filename) {
LoadOpenHandler handler;
shState->fileSystem().openRead(handler, filename);
}

View File

@ -5,7 +5,9 @@
#include <emscripten.h> #include <emscripten.h>
extern "C" { extern "C" {
void load_file_async(const char* fullPathC); void load_file_async_js(const char* fullPathC);
void load_file_async(const char* filename);
} }
#endif #endif

View File

@ -30,6 +30,10 @@
#include <SDL_sound.h> #include <SDL_sound.h>
#ifdef __EMSCRIPTEN__
#include "emscripten.hpp"
#endif
#define SE_CACHE_MEM (10*1024*1024) // 10 MB #define SE_CACHE_MEM (10*1024*1024) // 10 MB
struct SoundBuffer struct SoundBuffer
@ -238,6 +242,10 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename)
else else
{ {
/* Buffer not in cache, needs to be loaded */ /* Buffer not in cache, needs to be loaded */
#ifdef __EMSCRIPTEN__
load_file_async(filename.c_str());
#endif
SoundOpenHandler handler; SoundOpenHandler handler;
shState->fileSystem().openRead(handler, filename.c_str()); shState->fileSystem().openRead(handler, filename.c_str());
buffer = handler.buffer; buffer = handler.buffer;