From 33a13abbc26c16c004be4e85b20878a50623212e Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sun, 14 Jun 2015 14:46:15 -0700 Subject: [PATCH] Refactor : Better segregation and modularization of the audio backend functions --- src/audio/mockingboard.c | 4 ++-- src/audio/soundcore-backend.h | 0 src/audio/soundcore-openal.c | 26 ++++++++++++++++++++------ src/audio/soundcore.c | 20 +++++++++++--------- src/audio/soundcore.h | 24 +++++++++++++++++------- src/interface.c | 4 ++-- 6 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 src/audio/soundcore-backend.h diff --git a/src/audio/mockingboard.c b/src/audio/mockingboard.c index e18e18c2..1a2d05e3 100644 --- a/src/audio/mockingboard.c +++ b/src/audio/mockingboard.c @@ -1377,7 +1377,7 @@ static bool MB_DSInit() unsigned long dwDSLockedBufferSize = 0; // Size of the locked DirectSound buffer int16_t* pDSLockedBuffer; - if(!soundcore_isAvailable) + if(!audio_isAvailable) return false; int hr = DSGetSoundBuffer(&MockingboardVoice, DSBCAPS_CTRLVOLUME, g_dwDSBufferSize, SAMPLE_RATE, 2); @@ -1763,7 +1763,7 @@ static void ResetState() void MB_Reset() { - if(!soundcore_isAvailable) + if(!audio_isAvailable) return; for(int i=0; iimplementation_specific; @@ -212,7 +213,7 @@ long SoundSystemDestroy(SoundSystemStruct **sound_struct) return 0; } -long SoundSystemEnumerate(char ***device_list, const int limit) +static long openal_systemEnumerate(char ***device_list, const int limit) { assert(*device_list == NULL); *device_list = malloc(sizeof(char*)*2); @@ -223,7 +224,7 @@ long SoundSystemEnumerate(char ***device_list, const int limit) } // pause all audio -long SoundSystemPause() +static long openal_systemPause(void) { ALVoices *vnode = NULL; ALVoices *tmp = NULL; @@ -241,7 +242,7 @@ long SoundSystemPause() return 0; } -long SoundSystemUnpause() +static long openal_systemResume(void) { ALVoices *vnode = NULL; ALVoices *tmp = NULL; @@ -820,3 +821,16 @@ static long OpenALDestroySoundBuffer(ALSoundBufferStruct **soundbuf_struct) return 0; } +__attribute__((constructor(CTOR_PRIORITY_EARLY))) +static void _init_openal(void) { + LOG("Initializing OpenAL sound system"); + + openal_audio_backend.init = &openal_systemInit; + openal_audio_backend.shutdown = &openal_systemShutdown; + openal_audio_backend.pause = &openal_systemPause; + openal_audio_backend.resume = &openal_systemResume; + openal_audio_backend.enumerateDevices = &openal_systemEnumerate; + + audio_backend = &openal_audio_backend; +} + diff --git a/src/audio/soundcore.c b/src/audio/soundcore.c index 69b86848..75acef80 100644 --- a/src/audio/soundcore.c +++ b/src/audio/soundcore.c @@ -50,7 +50,9 @@ static VOICE* g_pSpeakerVoice = NULL; //------------------------------------- -bool soundcore_isAvailable = false; +bool audio_isAvailable = false; + +audio_backend_s *audio_backend = NULL; //----------------------------------------------------------------------------- @@ -239,14 +241,14 @@ static void _destroy_enumerated_sound_devices(void) { bool DSInit() { - if(soundcore_isAvailable) + if(audio_isAvailable) { g_uDSInitRefCount++; return true; // Already initialised successfully } _destroy_enumerated_sound_devices(); - num_sound_devices = SoundSystemEnumerate(&sound_devices, MAX_SOUND_DEVICES); + num_sound_devices = audio_backend->enumerateDevices(&sound_devices, MAX_SOUND_DEVICES); int hr = (num_sound_devices <= 0); if(hr) { @@ -261,9 +263,9 @@ bool DSInit() { if (g_lpDS) { - SoundSystemDestroy((SoundSystemStruct**)&g_lpDS); + audio_backend->shutdown((SoundSystemStruct**)&g_lpDS); } - hr = (int)SoundSystemCreate(sound_devices[x], (SoundSystemStruct**)&g_lpDS); + hr = (int)audio_backend->init(sound_devices[x], (SoundSystemStruct**)&g_lpDS); if(hr == 0) { bCreatedOK = true; @@ -278,7 +280,7 @@ bool DSInit() return false; } - soundcore_isAvailable = true; + audio_isAvailable = true; g_uDSInitRefCount = 1; @@ -291,7 +293,7 @@ void DSUninit() { _destroy_enumerated_sound_devices(); - if(!soundcore_isAvailable) + if(!audio_isAvailable) return; assert(g_uDSInitRefCount); @@ -308,8 +310,8 @@ void DSUninit() assert(g_uNumVoices == 0); - SoundSystemDestroy((SoundSystemStruct**)&g_lpDS); - soundcore_isAvailable = false; + audio_backend->shutdown((SoundSystemStruct**)&g_lpDS); + audio_isAvailable = false; } //============================================================================= diff --git a/src/audio/soundcore.h b/src/audio/soundcore.h index e67e946f..4aa50af1 100644 --- a/src/audio/soundcore.h +++ b/src/audio/soundcore.h @@ -66,6 +66,7 @@ typedef struct IDirectSound { int (*CreateSoundBuffer)(AudioParams_s *pcDSBufferDesc, LPDIRECTSOUNDBUFFER * ppDSBuffer, void *pUnkOuter); int (*DestroySoundBuffer)(LPDIRECTSOUNDBUFFER * ppDSBuffer); } IDirectSound, *LPDIRECTSOUND; +typedef struct IDirectSound SoundSystemStruct; typedef struct { @@ -101,13 +102,22 @@ void SoundCore_SetErrorMax(const int nErrorMax); bool DSInit(); void DSUninit(); -extern bool soundcore_isAvailable; +extern bool audio_isAvailable; -typedef struct IDirectSound SoundSystemStruct; -long SoundSystemCreate(const char *sound_device, SoundSystemStruct **sound_struct); -long SoundSystemDestroy(SoundSystemStruct **sound_struct); -long SoundSystemPause(); -long SoundSystemUnpause(); -long SoundSystemEnumerate(char ***sound_devices, const int maxcount); +typedef struct audio_backend_s { + + // mandatory audio backend functions + long (*init)(const char *sound_device, SoundSystemStruct **sound_struct); + long (*shutdown)(SoundSystemStruct **sound_struct); + long (*pause)(void); + long (*resume)(void); + long (*enumerateDevices)(char ***sound_devices, const int maxcount); + +} audio_backend_s; + +/* + * The registered audio backend (renderer). + */ +extern audio_backend_s *audio_backend; #endif /* whole file */ diff --git a/src/interface.c b/src/interface.c index 62e08586..53ff05d7 100644 --- a/src/interface.c +++ b/src/interface.c @@ -1549,7 +1549,7 @@ static void *interface_thread(void *current_key) { pthread_mutex_lock(&interface_mutex); #ifdef AUDIO_ENABLED - SoundSystemPause(); + audio_backend->pause(); #endif in_interface = true; @@ -1593,7 +1593,7 @@ static void *interface_thread(void *current_key) } #ifdef AUDIO_ENABLED - SoundSystemUnpause(); + audio_backend->resume(); #endif pthread_mutex_unlock(&interface_mutex); in_interface = false;