2021-10-25 10:08:59 -04:00
|
|
|
#include "SH_OpenALProxy.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
extern "C" void SH_OpenAL_Reset_Proxy(CSH_OpenAL* handler)
|
|
|
|
{
|
|
|
|
handler->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void SH_OpenAL_Write_Proxy(CSH_OpenAL* handler, int16* samples, unsigned int sampleCount, unsigned int sampleRate)
|
|
|
|
{
|
|
|
|
handler->Write(samples, sampleCount, sampleRate);
|
2021-10-28 15:49:42 -04:00
|
|
|
delete[] samples;
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int SH_OpenAL_RecycleBuffers_Proxy(CSH_OpenAL* handler)
|
|
|
|
{
|
|
|
|
handler->RecycleBuffers();
|
2021-10-28 15:49:42 -04:00
|
|
|
return handler->GetFreeBufferCount();
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CSH_OpenALProxy::CSH_OpenALProxy(CSH_OpenAL* parent)
|
2021-10-28 15:49:42 -04:00
|
|
|
: m_parent(parent)
|
2021-10-25 10:08:59 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSoundHandler::FactoryFunction CSH_OpenALProxy::GetFactoryFunction(CSH_OpenAL* soundHandler)
|
|
|
|
{
|
|
|
|
return [soundHandler]() { return new CSH_OpenALProxy(soundHandler); };
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSH_OpenALProxy::Reset()
|
|
|
|
{
|
2021-10-28 15:49:42 -04:00
|
|
|
if(m_recycleBuffersCall != nullptr)
|
|
|
|
{
|
|
|
|
emscripten_async_waitable_close(m_recycleBuffersCall);
|
|
|
|
m_recycleBuffersCall = nullptr;
|
|
|
|
}
|
|
|
|
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),
|
|
|
|
&SH_OpenAL_Reset_Proxy, m_parent);
|
|
|
|
m_bufferCount = CSH_OpenAL::MAX_BUFFERS;
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSH_OpenALProxy::Write(int16* samples, unsigned int sampleCount, unsigned int sampleRate)
|
|
|
|
{
|
2021-10-28 15:49:42 -04:00
|
|
|
assert(m_bufferCount != 0);
|
|
|
|
if(m_bufferCount == 0) return;
|
|
|
|
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),
|
|
|
|
&SH_OpenAL_Write_Proxy, m_parent, proxySamples, sampleCount, sampleRate);
|
|
|
|
m_bufferCount--;
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSH_OpenALProxy::HasFreeBuffers()
|
|
|
|
{
|
2021-10-28 15:49:42 -04:00
|
|
|
return (m_bufferCount != 0);
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSH_OpenALProxy::RecycleBuffers()
|
|
|
|
{
|
2021-10-28 15:49:42 -04:00
|
|
|
if(m_recycleBuffersCall != nullptr)
|
|
|
|
{
|
|
|
|
int bufferCount = 0;
|
|
|
|
EMSCRIPTEN_RESULT result = emscripten_wait_for_call_i(m_recycleBuffersCall, 0, &bufferCount);
|
|
|
|
if(result == EMSCRIPTEN_RESULT_TIMED_OUT) return;
|
|
|
|
assert(result == EMSCRIPTEN_RESULT_SUCCESS);
|
|
|
|
m_bufferCount = bufferCount;
|
|
|
|
emscripten_async_waitable_close(m_recycleBuffersCall);
|
|
|
|
m_recycleBuffersCall = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_recycleBuffersCall = emscripten_async_waitable_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),
|
|
|
|
&SH_OpenAL_RecycleBuffers_Proxy, m_parent);
|
|
|
|
}
|
2021-10-25 10:08:59 -04:00
|
|
|
}
|