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

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

View file

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

View file

@ -1,6 +1,8 @@
#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) {
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>
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

View file

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