Changes for thread based player.

This commit is contained in:
Jean-Philip Desjardins 2021-10-21 13:16:02 -04:00
parent 231ffb8059
commit 82800241aa
6 changed files with 111 additions and 9 deletions

View file

@ -12,7 +12,7 @@ let updateTimer : any;
let updateFct = async function() {
let releaseLock = await tickMutex.acquire();
await tickPsf();
// await tickPsf();
updateTimer = setTimeout(updateFct, updateDelay);
releaseLock();
};

View file

@ -6,7 +6,8 @@ let module_overrides = {
locateFile: function(path : string) {
const baseURL = window.location.origin + window.location.pathname.substring(0, window.location.pathname.lastIndexOf( "/" ));
return baseURL + '/' + path;
}
},
mainScriptUrlOrBlob: "",
};
function convertStringVectorToArray(strVector : any) {
@ -33,9 +34,10 @@ function convertStringMapToDictionary(strMap : any) {
}
export let initPsfPlayerModule = async function() {
module_overrides.mainScriptUrlOrBlob = module_overrides.locateFile('PsfPlayer.js');
PsfPlayerModule = await PsfPlayer(module_overrides);
PsfPlayerModule.FS.mkdir("/work");
await PsfPlayerModule.ccall("initVm", "", [], [], { async: true });
PsfPlayerModule.ccall("initVm", "", [], []);
};
export let getPsfArchiveFileList = function(archivePath : string) {

View file

@ -26,6 +26,8 @@ list(APPEND PROJECT_LIBS sh_openal)
add_executable(PsfPlayer
Main.cpp
SH_AudioProxy.cpp
SH_AudioProxy.h
SH_FileOutput.cpp
SH_FileOutput.h
)
@ -44,6 +46,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ALLOW_TABLE_GROWTH")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ALLOW_MEMORY_GROWTH")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ASSERTIONS=2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lnodefs.js")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXPORT_NAME=PsfPlayer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s PTHREAD_POOL_SIZE=4")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ENVIRONMENT=web,worker")

View file

@ -6,11 +6,13 @@
#include "Jitter_CodeGen_Wasm.h"
#include "BasicBlock.h"
#include "MemoryUtils.h"
#include "SH_AudioProxy.h"
#include "SH_FileOutput.h"
#include "../SH_OpenAL.h"
#include <emscripten/bind.h>
CPsfVm* g_virtualMachine = nullptr;
CSoundHandler* g_soundHandler = nullptr;
CPsfBase::TagMap g_tags;
int main(int argc, const char** argv)
@ -33,10 +35,11 @@ extern "C" void initVm()
Jitter::CWasmFunctionRegistry::RegisterFunction(reinterpret_cast<uintptr_t>(&MemoryUtils_SetByteProxy), "_MemoryUtils_SetByteProxy", "viii");
Jitter::CWasmFunctionRegistry::RegisterFunction(reinterpret_cast<uintptr_t>(&MemoryUtils_SetHalfProxy), "_MemoryUtils_SetHalfProxy", "viii");
Jitter::CWasmFunctionRegistry::RegisterFunction(reinterpret_cast<uintptr_t>(&MemoryUtils_SetWordProxy), "_MemoryUtils_SetWordProxy", "viii");
});
}, true);
g_virtualMachine->SetSpuHandlerImpl(&CSH_FileOutput::HandlerFactory);
//g_virtualMachine->SetSpuHandlerImpl(&CSH_OpenAL::HandlerFactory);
//g_soundHandler = new CSH_FileOutput();
g_soundHandler = new CSH_OpenAL();
g_virtualMachine->SetSpuHandlerImpl(CSH_AudioProxy::GetFactoryFunction(g_soundHandler));
}
catch(const std::exception& ex)
{
@ -58,7 +61,7 @@ extern "C" void loadPsf(const char* archivePath, const char* psfPath)
auto fileToken = CArchivePsfStreamProvider::GetPathTokenFromFilePath(psfPath);
CPsfLoader::LoadPsf(*g_virtualMachine, fileToken, archivePath, &g_tags);
g_virtualMachine->Resume();
});
}, true);
}
catch(const std::exception& ex)
{

View file

@ -0,0 +1,76 @@
#include "SH_AudioProxy.h"
#include <emscripten/threading.h>
#include <cstdio>
extern "C" void SoundHandlerResetProxy(CSoundHandler* handler)
{
handler->Reset();
}
extern "C" void SoundHandlerWriteProxy(CSoundHandler* handler, int16* samples, unsigned int sampleCount, unsigned int sampleRate)
{
handler->Write(samples, sampleCount, sampleRate);
delete [] samples;
}
extern "C" int SoundHandlerHasFreeBuffersProxy(CSoundHandler* handler)
{
return handler->HasFreeBuffers();
}
extern "C" void SoundHandlerRecycleBuffersProxy(CSoundHandler* handler)
{
handler->RecycleBuffers();
}
CSH_AudioProxy::CSH_AudioProxy(CSoundHandler* parent)
: m_parent(parent)
{
}
CSoundHandler::FactoryFunction CSH_AudioProxy::GetFactoryFunction(CSoundHandler* soundHandler)
{
return [soundHandler]() { return new CSH_AudioProxy(soundHandler); };
}
void CSH_AudioProxy::Reset()
{
emscripten_async_run_in_main_runtime_thread(
EM_FUNC_SIG_RETURN_VALUE_V |
EM_FUNC_SIG_WITH_N_PARAMETERS(1) |
EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I),
&SoundHandlerResetProxy, m_parent);
}
void CSH_AudioProxy::Write(int16* samples, unsigned int sampleCount, unsigned int sampleRate)
{
int16* proxySamples = new int16[sampleCount];
memcpy(proxySamples, samples, sizeof(int16) * sampleCount);
emscripten_async_run_in_main_runtime_thread(
EM_FUNC_SIG_RETURN_VALUE_V |
EM_FUNC_SIG_WITH_N_PARAMETERS(4) |
EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I) |
EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(3, EM_FUNC_SIG_PARAM_I),
&SoundHandlerWriteProxy, m_parent, proxySamples, sampleCount, sampleRate);
}
bool CSH_AudioProxy::HasFreeBuffers()
{
// int result = emscripten_sync_run_in_main_runtime_thread(
// EM_FUNC_SIG_RETURN_VALUE_I |
// EM_FUNC_SIG_WITH_N_PARAMETERS(1) |
// EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I),
// &SoundHandlerHasFreeBuffersProxy, m_parent);
// return result;
return m_parent->HasFreeBuffers();
}
void CSH_AudioProxy::RecycleBuffers()
{
emscripten_async_run_in_main_runtime_thread(
EM_FUNC_SIG_RETURN_VALUE_V |
EM_FUNC_SIG_WITH_N_PARAMETERS(1) |
EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I),
&SoundHandlerRecycleBuffersProxy, m_parent);
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "../SoundHandler.h"
class CSH_AudioProxy : public CSoundHandler
{
public:
CSH_AudioProxy(CSoundHandler*);
static FactoryFunction GetFactoryFunction(CSoundHandler*);
void Reset() override;
void Write(int16*, unsigned int, unsigned int) override;
bool HasFreeBuffers() override;
void RecycleBuffers() override;
private:
CSoundHandler* m_parent = nullptr;
};