Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to use async process ALIST on Windows #54

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Source/Interface/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifdef DAEDALUS_PSP
static const EAudioPluginMode kDefaultAudioPluginMode = APM_DISABLED;
static const ETextureHashFrequency kDefaultTextureHashFrequency = THF_DISABLED;
#elif defined(DAEDALUS_W32)
static const EAudioPluginMode kDefaultAudioPluginMode = APM_ENABLED_ASYNC;
static const ETextureHashFrequency kDefaultTextureHashFrequency = THF_EVERY_FRAME;
#else
static const EAudioPluginMode kDefaultAudioPluginMode = APM_ENABLED_SYNC;
static const EAudioPluginMode kDefaultAudioPluginMode = APM_ENABLED_SYNC;
static const ETextureHashFrequency kDefaultTextureHashFrequency = THF_EVERY_FRAME;
#endif

Expand Down
85 changes: 69 additions & 16 deletions Source/SysW32/HLEAudio/AudioPluginW32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "HLEAudio/HLEAudioInternal.h"
#include "Utility/FastMemcpy.h"
#include "System/Thread.h"
#include "System/Mutex.h"
#include "System/Condition.h"

#define INITGUID

#include <windows.h>
#include <mmsystem.h>
#include <dsound.h>

//This is disabled, it doesn't work well, causes random deadlocks/Lock failures :(
//Would be nice to get it working correctly, since running audio in the main thread is abit jerky
#define AUDIO_THREADED
#define RSP_AUDIO_INTR_CYCLES 1

#define NUMCAPTUREEVENTS 3
#define BufferSize 0x3000
Expand Down Expand Up @@ -74,9 +74,9 @@ class CAudioPluginW32 : public CAudioPlugin
u32 Frequency, Dacrate, Snd1Len, SpaceLeft, SndBuffer[3], Playing;
u8 *Snd1ReadPos;
bool AIReady;
#ifdef AUDIO_THREADED
static u32 __stdcall AudioThread(void * arg);
#endif
static u32 __stdcall AudioProcessThread(void *arg);

LPDIRECTSOUNDBUFFER lpdsbuf;
LPDIRECTSOUND lpds;
LPDIRECTSOUNDNOTIFY lpdsNotify;
Expand All @@ -88,11 +88,17 @@ class CAudioPluginW32 : public CAudioPlugin

void FillSectionWithSilence( int buffer );
void FillBuffer ( int buffer );

Cond *audioRequest;
Mutex * requestMutex;
bool is_running;
ThreadHandle audiothread, audioprocessthread;

};
//*****************************************************************************
//
//*****************************************************************************
EAudioPluginMode gAudioPluginEnabled( APM_ENABLED_SYNC );
EAudioPluginMode gAudioPluginEnabled( APM_ENABLED_ASYNC );

//*****************************************************************************
//
Expand Down Expand Up @@ -138,15 +144,29 @@ bool CAudioPluginW32::StartEmulation()
SndBuffer[0] = Buffer_Empty;
SndBuffer[1] = Buffer_Empty;
SndBuffer[2] = Buffer_Empty;
#ifdef AUDIO_THREADED
if (CreateThread( "Audio", AudioThread, this ) == kInvalidThreadHandle)

is_running = true;

if ((audiothread = CreateThread( "Audio", AudioThread, this )) == kInvalidThreadHandle)
{
DAEDALUS_ERROR("Failed to start the audio thread!");
gAudioPluginEnabled = APM_DISABLED;
}
#endif
return true;

if (gAudioPluginEnabled == APM_ENABLED_ASYNC)
{
audioRequest = CondCreate();
requestMutex = new Mutex();

if ((audioprocessthread = CreateThread( "AudioProcess", AudioProcessThread, this )) == kInvalidThreadHandle)
{
DAEDALUS_ERROR("Failed to start the audio thread!");
gAudioPluginEnabled = APM_DISABLED;
}
}


return true;
}

//*****************************************************************************
Expand All @@ -156,9 +176,20 @@ void CAudioPluginW32::StopEmulation()
{
Audio_Reset();

#ifndef AUDIO_THREADED
//ToDo: Terminate thread
#endif
is_running = false;

if (gAudioPluginEnabled == APM_ENABLED_ASYNC)
{
CondSignal(audioRequest);
JoinThread(audioprocessthread, INFINITE);
CondDestroy(audioRequest);
delete requestMutex;
ReleaseThreadHandle(audioprocessthread);
}

JoinThread(audiothread, INFINITE);

ReleaseThreadHandle(audiothread);

if (lpdsbuf)
{
Expand All @@ -172,19 +203,33 @@ void CAudioPluginW32::StopEmulation()
lpds = NULL;
}
}
#ifdef AUDIO_THREADED

u32 DAEDALUS_THREAD_CALL_TYPE CAudioPluginW32::AudioThread(void * arg)
{
CAudioPluginW32 * plugin = static_cast<CAudioPluginW32 *>(arg);
while(1)
while(plugin->is_running)
{
plugin->Update(true);
ThreadSleepMs(1);
}

return 0;
}
#endif

u32 DAEDALUS_THREAD_CALL_TYPE CAudioPluginW32::AudioProcessThread(void *arg)
{
CAudioPluginW32 * plugin = static_cast<CAudioPluginW32 *>(arg);
MutexLock lock(plugin->requestMutex);

while(plugin->is_running)
{
CondWait(plugin->audioRequest, plugin->requestMutex, INFINITE);

if(plugin->is_running) Audio_Ucode();
}

return 0;
}

//*****************************************************************************
//
Expand Down Expand Up @@ -337,6 +382,7 @@ void CAudioPluginW32::Update( bool Wait )
}
}


EProcessResult CAudioPluginW32::ProcessAList()
{
Memory_SP_SetRegisterBits(SP_STATUS_REG, SP_STATUS_HALT);
Expand All @@ -353,6 +399,13 @@ EProcessResult CAudioPluginW32::ProcessAList()
Audio_Ucode();
result = PR_COMPLETED;
break;

case APM_ENABLED_ASYNC:
CondSignal(audioRequest);

CPU_AddEvent(RSP_AUDIO_INTR_CYCLES, CPU_EVENT_AUDIO);
result = PR_STARTED;
break;
}

return result;
Expand Down
Loading