From 3767f55846ed42761b83d4a074a4370eb8823dcb Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Fri, 12 Jun 2015 22:32:04 -0700 Subject: [PATCH] Refactor : rename and coalesce audio buffer initialization structs --- src/audio/ds-shim.h | 22 ++++++---------------- src/audio/soundcore-openal.c | 12 ++++++------ src/audio/soundcore-openal.h | 2 -- src/audio/soundcore.c | 23 ++++++++--------------- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/src/audio/ds-shim.h b/src/audio/ds-shim.h index 7e6dac8e..138fad3e 100644 --- a/src/audio/ds-shim.h +++ b/src/audio/ds-shim.h @@ -147,31 +147,21 @@ static inline bool SUCCEEDED(int x) { return x == DS_OK; } #define DSBCAPS_CTRLVOLUME 0x00000080 #define DSBCAPS_CTRLPOSITIONNOTIFY 0x00000100 - -typedef struct { - uint16_t wFormatTag; - uint16_t nChannels; +typedef struct AudioParams_s { + uint16_t nChannels; unsigned long nSamplesPerSec; unsigned long nAvgBytesPerSec; - uint16_t nBlockAlign; - uint16_t wBitsPerSample; - uint16_t cbSize; -} WAVEFORMATEX, *LPWAVEFORMATEX; - -typedef struct DSBUFFERDESC { - unsigned long dwSize; - unsigned long dwFlags; + uint16_t nBlockAlign; + uint16_t wBitsPerSample; unsigned long dwBufferBytes; - unsigned long dwReserved; - LPWAVEFORMATEX lpwfxFormat; -} DSBUFFERDESC; +} AudioParams_s; typedef struct IDirectSound { void *implementation_specific; #define LPUNKNOWN void* - int (*CreateSoundBuffer)(DSBUFFERDESC *pcDSBufferDesc, LPDIRECTSOUNDBUFFER * ppDSBuffer, LPUNKNOWN pUnkOuter); + int (*CreateSoundBuffer)(AudioParams_s *pcDSBufferDesc, LPDIRECTSOUNDBUFFER * ppDSBuffer, LPUNKNOWN pUnkOuter); int (*DestroySoundBuffer)(LPDIRECTSOUNDBUFFER * ppDSBuffer); } IDirectSound, *LPDIRECTSOUND; diff --git a/src/audio/soundcore-openal.c b/src/audio/soundcore-openal.c index 0067f583..cb026f74 100644 --- a/src/audio/soundcore-openal.c +++ b/src/audio/soundcore-openal.c @@ -18,7 +18,7 @@ #include "audio/soundcore-openal.h" #include "audio/alhelpers.h" -static long OpenALCreateSoundBuffer(ALBufferParamsStruct *params, ALSoundBufferStruct **soundbuf_struct, void *extra_data); +static long OpenALCreateSoundBuffer(AudioParams_s *params, ALSoundBufferStruct **soundbuf_struct, void *extra_data); static long OpenALDestroySoundBuffer(ALSoundBufferStruct **soundbuf_struct); typedef struct ALVoices { @@ -138,7 +138,7 @@ long SoundSystemCreate(const char *sound_device, SoundSystemStruct **sound_struc } (*sound_struct)->implementation_specific = ctx; - (*sound_struct)->CreateSoundBuffer = (int (*)(DSBUFFERDESC *, LPDIRECTSOUNDBUFFER *, void *))OpenALCreateSoundBuffer; + (*sound_struct)->CreateSoundBuffer = (int (*)(AudioParams_s *, LPDIRECTSOUNDBUFFER *, void *))OpenALCreateSoundBuffer; (*sound_struct)->DestroySoundBuffer = (int (*)(LPDIRECTSOUNDBUFFER *))OpenALDestroySoundBuffer; return 0; @@ -257,7 +257,7 @@ static void DeleteVoice(ALVoice *voice) /* Creates a new voice object, and allocates the needed OpenAL source and * buffer objects. Error checking is simplified for the purposes of this * example, and will cause an abort if needed. */ -static ALVoice *NewVoice(ALBufferParamsStruct *params) +static ALVoice *NewVoice(AudioParams_s *params) { ALVoice *voice = NULL; @@ -328,10 +328,10 @@ static ALVoice *NewVoice(ALBufferParamsStruct *params) voice->avail_buffers = node; } - voice->rate = (ALuint)params->lpwfxFormat->nSamplesPerSec; + voice->rate = (ALuint)params->nSamplesPerSec; // Emulator supports only mono and stereo - if (params->lpwfxFormat->nChannels == 2) + if (params->nChannels == 2) { voice->format = AL_FORMAT_STEREO16; } @@ -687,7 +687,7 @@ static long ALGetStatus(void *_this, unsigned long *status) return 0; } -static long OpenALCreateSoundBuffer(ALBufferParamsStruct *params, ALSoundBufferStruct **soundbuf_struct, void *extra_data) +static long OpenALCreateSoundBuffer(AudioParams_s *params, ALSoundBufferStruct **soundbuf_struct, void *extra_data) { LOG("OpenALCreateSoundBuffer ..."); assert(*soundbuf_struct == NULL); diff --git a/src/audio/soundcore-openal.h b/src/audio/soundcore-openal.h index 6842399b..c3c938a0 100644 --- a/src/audio/soundcore-openal.h +++ b/src/audio/soundcore-openal.h @@ -31,8 +31,6 @@ typedef struct IDirectSoundBuffer ALSoundBufferStruct; -typedef struct DSBUFFERDESC ALBufferParamsStruct; - struct ALPlayBuf; typedef struct ALPlayBuf { const ALuint bufid; // the hash id diff --git a/src/audio/soundcore.c b/src/audio/soundcore.c index e93115f9..8827912d 100644 --- a/src/audio/soundcore.c +++ b/src/audio/soundcore.c @@ -101,21 +101,14 @@ bool DSGetLock(LPDIRECTSOUNDBUFFER pVoice, unsigned long dwOffset, unsigned long int DSGetSoundBuffer(VOICE* pVoice, unsigned long dwFlags, unsigned long dwBufferSize, unsigned long nSampleRate, int nChannels) { - WAVEFORMATEX wavfmt; - DSBUFFERDESC dsbdesc; + AudioParams_s params = { 0 }; - wavfmt.wFormatTag = WAVE_FORMAT_PCM; - wavfmt.nChannels = nChannels; - wavfmt.nSamplesPerSec = nSampleRate; - wavfmt.wBitsPerSample = 16; - wavfmt.nBlockAlign = wavfmt.nChannels==1 ? 2 : 4; - wavfmt.nAvgBytesPerSec = wavfmt.nBlockAlign * wavfmt.nSamplesPerSec; - - memset (&dsbdesc, 0, sizeof (dsbdesc)); - dsbdesc.dwSize = sizeof (dsbdesc); - dsbdesc.dwBufferBytes = dwBufferSize; - dsbdesc.lpwfxFormat = &wavfmt; - dsbdesc.dwFlags = dwFlags | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS; + params.nChannels = nChannels; + params.nSamplesPerSec = nSampleRate; + params.wBitsPerSample = 16; + params.nBlockAlign = (params.nChannels == 1) ? 2 : 4; + params.nAvgBytesPerSec = params.nBlockAlign * params.nSamplesPerSec; + params.dwBufferBytes = dwBufferSize; // Are buffers released when g_lpDS OR pVoice->lpDSBvoice is released? // . From DirectX doc: @@ -126,7 +119,7 @@ int DSGetSoundBuffer(VOICE* pVoice, unsigned long dwFlags, unsigned long dwBuffe g_lpDS->DestroySoundBuffer(&pVoice->lpDSBvoice); //DSReleaseSoundBuffer(pVoice); } - int hr = g_lpDS->CreateSoundBuffer(&dsbdesc, &pVoice->lpDSBvoice, g_lpDS); + int hr = g_lpDS->CreateSoundBuffer(¶ms, &pVoice->lpDSBvoice, g_lpDS); if(FAILED(hr)) return hr;