From a7138e533202113647b3f1d9943e6d99113f2813 Mon Sep 17 00:00:00 2001 From: rogerman Date: Mon, 6 Aug 2012 23:03:05 +0000 Subject: [PATCH] Cocoa OpenEmu Plug-in (v0.9.8): - Backport changes from r4354 to 0.9.8 branch. --- desmume/src/cocoa/openemu/OESoundInterface.h | 2 + desmume/src/cocoa/openemu/OESoundInterface.mm | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/desmume/src/cocoa/openemu/OESoundInterface.h b/desmume/src/cocoa/openemu/OESoundInterface.h index d2ca0fff5..6515451e6 100644 --- a/desmume/src/cocoa/openemu/OESoundInterface.h +++ b/desmume/src/cocoa/openemu/OESoundInterface.h @@ -41,6 +41,8 @@ void SNDOpenEmuMuteAudio(); void SNDOpenEmuUnMuteAudio(); void SNDOpenEmuSetVolume(int volume); void SNDOpenEmuClearBuffer(); +void SNDOpenEmuFetchSamples(s16 *sampleBuffer, size_t sampleCount, ESynchMode synchMode, ISynchronizingAudioBuffer *theSynchronizer); +size_t SNDOpenEmuPostProcessSamples(s16 *postProcessBuffer, size_t requestedSampleCount, ESynchMode synchMode, ISynchronizingAudioBuffer *theSynchronizer); #ifdef __cplusplus } diff --git a/desmume/src/cocoa/openemu/OESoundInterface.mm b/desmume/src/cocoa/openemu/OESoundInterface.mm index 832b82bb2..cc9a8f501 100644 --- a/desmume/src/cocoa/openemu/OESoundInterface.mm +++ b/desmume/src/cocoa/openemu/OESoundInterface.mm @@ -21,6 +21,8 @@ OERingBuffer *openEmuSoundInterfaceBuffer = nil; +static pthread_mutex_t *mutexAudioSampleReadWrite = NULL; +pthread_mutex_t *mutexAudioEmulateCore = NULL; // Sound interface to the SPU SoundInterface_struct SNDOpenEmu = { @@ -33,7 +35,9 @@ SoundInterface_struct SNDOpenEmu = { SNDOpenEmuMuteAudio, SNDOpenEmuUnMuteAudio, SNDOpenEmuSetVolume, - SNDOpenEmuClearBuffer + SNDOpenEmuClearBuffer, + SNDOpenEmuFetchSamples, + SNDOpenEmuPostProcessSamples }; SoundInterface_struct *SNDCoreList[] = { @@ -46,12 +50,23 @@ int SNDOpenEmuInit(int buffer_size) { [openEmuSoundInterfaceBuffer setLength:buffer_size]; + if (mutexAudioSampleReadWrite == NULL) + { + mutexAudioSampleReadWrite = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(mutexAudioSampleReadWrite, NULL); + } + return 0; } void SNDOpenEmuDeInit() { - // Do nothing. The OpenEmu frontend will take care of this. + if (mutexAudioSampleReadWrite != NULL) + { + pthread_mutex_destroy(mutexAudioSampleReadWrite); + free(mutexAudioSampleReadWrite); + mutexAudioSampleReadWrite = NULL; + } } int SNDOpenEmuReset() @@ -89,3 +104,46 @@ void SNDOpenEmuClearBuffer() { // Do nothing. The OpenEmu frontend will take care of this. } + +void SNDOpenEmuFetchSamples(s16 *sampleBuffer, size_t sampleCount, ESynchMode synchMode, ISynchronizingAudioBuffer *theSynchronizer) +{ + if (mutexAudioSampleReadWrite == NULL) + { + return; + } + + pthread_mutex_lock(mutexAudioSampleReadWrite); + SPU_DefaultFetchSamples(sampleBuffer, sampleCount, synchMode, theSynchronizer); + pthread_mutex_unlock(mutexAudioSampleReadWrite); +} + +size_t SNDOpenEmuPostProcessSamples(s16 *postProcessBuffer, size_t requestedSampleCount, ESynchMode synchMode, ISynchronizingAudioBuffer *theSynchronizer) +{ + size_t processedSampleCount = 0; + + switch (synchMode) + { + case ESynchMode_DualSynchAsynch: + if (mutexAudioEmulateCore != NULL) + { + pthread_mutex_lock(mutexAudioEmulateCore); + processedSampleCount = SPU_DefaultPostProcessSamples(postProcessBuffer, requestedSampleCount, synchMode, theSynchronizer); + pthread_mutex_unlock(mutexAudioEmulateCore); + } + break; + + case ESynchMode_Synchronous: + if (mutexAudioSampleReadWrite != NULL) + { + pthread_mutex_lock(mutexAudioSampleReadWrite); + processedSampleCount = SPU_DefaultPostProcessSamples(postProcessBuffer, requestedSampleCount, synchMode, theSynchronizer); + pthread_mutex_unlock(mutexAudioSampleReadWrite); + } + break; + + default: + break; + } + + return processedSampleCount; +}