From dce8f6a1ff141530edece4b68c07f75265ed167b Mon Sep 17 00:00:00 2001 From: OV2 Date: Sun, 22 Jan 2012 20:18:15 +0100 Subject: [PATCH] Win32: use event for soundsync --- win32/CDirectSound.cpp | 3 ++- win32/CFMOD.cpp | 4 +++- win32/CFMODEx.cpp | 4 +++- win32/CXAudio2.cpp | 1 + win32/render.cpp | 2 +- win32/win32.cpp | 2 ++ win32/wsnes9x.cpp | 12 +++--------- win32/wsnes9x.h | 1 + 8 files changed, 16 insertions(+), 13 deletions(-) diff --git a/win32/CDirectSound.cpp b/win32/CDirectSound.cpp index 8dc6c948..bd640a92 100644 --- a/win32/CDirectSound.cpp +++ b/win32/CDirectSound.cpp @@ -489,7 +489,8 @@ void CDirectSound::MixSound() S9xMixSamples(B2,(Settings.SixteenBitSound?S2>>1:S2)); } LeaveCriticalSection(&GUI.SoundCritSect); - + + SetEvent(GUI.SoundSyncEvent); hResult = lpDSB -> Unlock (B1, S1, B2, S2); if (!SUCCEEDED(hResult)) diff --git a/win32/CFMOD.cpp b/win32/CFMOD.cpp index 2c2c9a14..ffd93daf 100644 --- a/win32/CFMOD.cpp +++ b/win32/CFMOD.cpp @@ -357,6 +357,8 @@ F_CALLBACKAPI CFMOD::FMODStreamCallback (FSOUND_STREAM *stream, void *buff, int LeaveCriticalSection(&GUI.SoundCritSect); + SetEvent(GUI.SoundSyncEvent); + #if defined (FSOUND_LOADRAW) @@ -364,4 +366,4 @@ F_CALLBACKAPI CFMOD::FMODStreamCallback (FSOUND_STREAM *stream, void *buff, int #endif } -#endif \ No newline at end of file +#endif diff --git a/win32/CFMODEx.cpp b/win32/CFMODEx.cpp index 09f9035e..b5a6b4d9 100644 --- a/win32/CFMODEx.cpp +++ b/win32/CFMODEx.cpp @@ -135,7 +135,9 @@ FMOD_RESULT F_CALLBACK CFMODEx::FMODExStreamCallback( LeaveCriticalSection(&GUI.SoundCritSect); + SetEvent(GUI.SoundSyncEvent); + return FMOD_OK; } #endif -#endif \ No newline at end of file +#endif diff --git a/win32/CXAudio2.cpp b/win32/CXAudio2.cpp index ba4297be..90286f1b 100644 --- a/win32/CXAudio2.cpp +++ b/win32/CXAudio2.cpp @@ -308,6 +308,7 @@ pBufferContext - unused void CXAudio2::OnBufferEnd(void *pBufferContext) { InterlockedDecrement(&bufferCount); + SetEvent(GUI.SoundSyncEvent); } /* CXAudio2::PushBuffer diff --git a/win32/render.cpp b/win32/render.cpp index f565de08..a5ac894b 100644 --- a/win32/render.cpp +++ b/win32/render.cpp @@ -2624,7 +2624,7 @@ void RenderBlarggNTSCRgb( SSurface Src, SSurface Dst, RECT *rect) void RenderBlarggNTSC( SSurface Src, SSurface Dst, RECT *rect) { SetRect(rect, 256, 239, 2); - rect->right = 604; + rect->right = SNES_NTSC_OUT_WIDTH(256); const unsigned int srcRowPixels = Src.Pitch/2; diff --git a/win32/win32.cpp b/win32/win32.cpp index aba6d171..7d4a52fb 100644 --- a/win32/win32.cpp +++ b/win32/win32.cpp @@ -986,6 +986,7 @@ void InitSnes9X( void) GFX.Screen = (uint16*)(ScreenBuffer); InitializeCriticalSection(&GUI.SoundCritSect); + GUI.SoundSyncEvent = CreateEvent(NULL,TRUE,TRUE,NULL); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); S9xInitAPU(); @@ -1005,6 +1006,7 @@ void DeinitS9x() delete [] ScreenBuf; DeleteCriticalSection(&GUI.SoundCritSect); + CloseHandle(GUI.SoundSyncEvent); CoUninitialize(); if(GUI.GunSight) DestroyCursor(GUI.GunSight);//= LoadCursor (hInstance, MAKEINTRESOURCE (IDC_CURSOR_SCOPE)); diff --git a/win32/wsnes9x.cpp b/win32/wsnes9x.cpp index aaa21d5a..85d737db 100644 --- a/win32/wsnes9x.cpp +++ b/win32/wsnes9x.cpp @@ -3304,7 +3304,6 @@ int WINAPI WinMain( ChangeInputDevice(); DWORD lastTime = timeGetTime(); - DWORD sSyncTime,sSyncWaited; MSG msg; @@ -3421,15 +3420,10 @@ int WINAPI WinMain( { ProcessInput(); - sSyncTime=timeGetTime(); - while(!S9xSyncSound()) { - Sleep(2); - sSyncWaited=timeGetTime(); - if(sSyncWaited-sSyncTime > 1000) { - S9xClearSamples(); - break; - } + ResetEvent(GUI.SoundSyncEvent); + if(WaitForSingleObject(GUI.SoundSyncEvent,1000) != WAIT_OBJECT_0) + S9xClearSamples(); } S9xMainLoop(); diff --git a/win32/wsnes9x.h b/win32/wsnes9x.h index 894f4c90..4a49f3cd 100644 --- a/win32/wsnes9x.h +++ b/win32/wsnes9x.h @@ -369,6 +369,7 @@ struct sGUI { bool Mute; // used for sync sound synchronization CRITICAL_SECTION SoundCritSect; + HANDLE SoundSyncEvent; TCHAR RomDir [_MAX_PATH]; TCHAR ScreensDir [_MAX_PATH];