Cleanup xaudio.h/xaudio.c

This commit is contained in:
twinaphex 2017-09-28 16:54:09 +02:00
parent 0a29c4711a
commit 51cdf5a6d7
2 changed files with 11 additions and 45 deletions

View File

@ -130,21 +130,13 @@ static void xaudio2_enumerate_devices(xaudio2_t *xa)
(void)i; (void)i;
(void)dev_count; (void)dev_count;
#ifndef _XBOX #ifndef _XBOX
#ifdef __cplusplus
xa->pXAudio2->GetDeviceCount(&dev_count);
#else
IXAudio2_GetDeviceCount(xa->pXAudio2, &dev_count); IXAudio2_GetDeviceCount(xa->pXAudio2, &dev_count);
#endif
fprintf(stderr, "XAudio2 devices:\n"); fprintf(stderr, "XAudio2 devices:\n");
for (i = 0; i < dev_count; i++) for (i = 0; i < dev_count; i++)
{ {
XAUDIO2_DEVICE_DETAILS dev_detail; XAUDIO2_DEVICE_DETAILS dev_detail;
#ifdef __cplusplus
xa->pXAudio2->GetDeviceDetails(i, &dev_detail);
#else
IXAudio2_GetDeviceDetails(xa->pXAudio2, i, &dev_detail); IXAudio2_GetDeviceDetails(xa->pXAudio2, i, &dev_detail);
#endif
fwprintf(stderr, L"\t%u: %s\n", i, dev_detail.DisplayName); fwprintf(stderr, L"\t%u: %s\n", i, dev_detail.DisplayName);
} }
#endif #endif
@ -171,32 +163,19 @@ static void xaudio2_free(xaudio2_t *handle)
if (handle->pSourceVoice) if (handle->pSourceVoice)
{ {
#ifdef __cplusplus
handle->pSourceVoice->Stop(0, XAUDIO2_COMMIT_NOW);
handle->pSourceVoice->DestroyVoice();
#else
IXAudio2SourceVoice_Stop(handle->pSourceVoice, IXAudio2SourceVoice_Stop(handle->pSourceVoice,
0, XAUDIO2_COMMIT_NOW); 0, XAUDIO2_COMMIT_NOW);
IXAudio2SourceVoice_DestroyVoice(handle->pSourceVoice); IXAudio2SourceVoice_DestroyVoice(handle->pSourceVoice);
#endif
} }
if (handle->pMasterVoice) if (handle->pMasterVoice)
{ {
#ifdef __cplusplus
handle->pMasterVoice->DestroyVoice();
#else
IXAudio2MasteringVoice_DestroyVoice(handle->pMasterVoice); IXAudio2MasteringVoice_DestroyVoice(handle->pMasterVoice);
#endif
} }
if (handle->pXAudio2) if (handle->pXAudio2)
{ {
#ifdef __cplusplus
handle->pXAudio2->Release();
#else
IXAudio2_Release(handle->pXAudio2); IXAudio2_Release(handle->pXAudio2);
#endif
} }
if (handle->hEvent) if (handle->hEvent)
@ -241,29 +220,16 @@ static xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels,
if (FAILED(XAudio2Create(&handle->pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR))) if (FAILED(XAudio2Create(&handle->pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)))
goto error; goto error;
#ifdef __cplusplus
if (FAILED(handle->pXAudio2->CreateMasteringVoice(&handle->pMasterVoice,
channels, samplerate, 0, device, NULL)))
goto error;
#else
if (FAILED(IXAudio2_CreateMasteringVoice(handle->pXAudio2, &handle->pMasterVoice, channels, samplerate, 0, device, NULL))) if (FAILED(IXAudio2_CreateMasteringVoice(handle->pXAudio2, &handle->pMasterVoice, channels, samplerate, 0, device, NULL)))
goto error; goto error;
#endif
xaudio2_set_wavefmt(&wfx, channels, samplerate); xaudio2_set_wavefmt(&wfx, channels, samplerate);
#ifdef __cplusplus
if (FAILED(handle->pXAudio2->CreateSourceVoice(&handle->pSourceVoice, &wfx,
XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO,
handle)))
goto error;
#else
if (FAILED(IXAudio2_CreateSourceVoice(handle->pXAudio2, if (FAILED(IXAudio2_CreateSourceVoice(handle->pXAudio2,
&handle->pSourceVoice, &wfx, &handle->pSourceVoice, &wfx,
XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO,
(IXAudio2VoiceCallback*)handle, 0, 0))) (IXAudio2VoiceCallback*)handle, 0, 0)))
goto error; goto error;
#endif
handle->hEvent = CreateEvent(0, FALSE, FALSE, 0); handle->hEvent = CreateEvent(0, FALSE, FALSE, 0);
if (!handle->hEvent) if (!handle->hEvent)
@ -274,14 +240,9 @@ static xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels,
if (!handle->buf) if (!handle->buf)
goto error; goto error;
#ifdef __cplusplus
if (FAILED(handle->pSourceVoice->Start(0)))
goto error;
#else
if (FAILED(IXAudio2SourceVoice_Start(handle->pSourceVoice, 0, if (FAILED(IXAudio2SourceVoice_Start(handle->pSourceVoice, 0,
XAUDIO2_COMMIT_NOW))) XAUDIO2_COMMIT_NOW)))
goto error; goto error;
#endif
return handle; return handle;
@ -322,13 +283,8 @@ static size_t xaudio2_write(xaudio2_t *handle, const void *buf, size_t bytes_)
xa2buffer.AudioBytes = handle->bufsize; xa2buffer.AudioBytes = handle->bufsize;
xa2buffer.pAudioData = handle->buf + handle->write_buffer * handle->bufsize; xa2buffer.pAudioData = handle->buf + handle->write_buffer * handle->bufsize;
#ifdef __cplusplus
if (FAILED(handle->pSourceVoice->SubmitSourceBuffer(&xa2buffer, NULL)))
return 0;
#else
if (FAILED(IXAudio2SourceVoice_SubmitSourceBuffer(handle->pSourceVoice, &xa2buffer, NULL))) if (FAILED(IXAudio2SourceVoice_SubmitSourceBuffer(handle->pSourceVoice, &xa2buffer, NULL)))
return 0; return 0;
#endif
InterlockedIncrement((LONG volatile*)&handle->buffers); InterlockedIncrement((LONG volatile*)&handle->buffers);
handle->bufptr = 0; handle->bufptr = 0;

View File

@ -287,7 +287,17 @@ DECLARE_INTERFACE_(IXAudio2, IUnknown)
void *pReserved X2DEFAULT(NULL)) PURE; void *pReserved X2DEFAULT(NULL)) PURE;
}; };
#ifndef __cplusplus #ifdef __cplusplus
/* C++ hooks */
#define IXAudio2SourceVoice_SubmitSourceBuffer(handle, a, b) handle->SubmitSourceBuffer(a, b)
#define IXAudio2SourceVoice_Stop(handle, a, b) handle->Stop(a, b)
#define IXAudio2SourceVoice_DestroyVoice(handle) handle->DestroyVoice()
#define IXAudio2MasteringVoice_DestroyVoice(handle) handle->DestroyVoice()
#define IXAudio2_Release(handle) handle->Release()
#define IXAudio2_CreateSourceVoice(handle, a, b, c, d, e, f, g) handle->CreateSourceVoice(a, b, c, d, e, f, g)
#define IXAudio2_CreateMasteringVoice(handle, a, b, c, d, e, f) handle->CreateMasteringVoice(a, b, c, d, e, f)
#define IXAudio2SourceVoice_Start(handle, a, b) handle->Start(a, b)
#else
/* C hooks */ /* C hooks */
#define IXAudio2_Initialize(THIS, ...) (THIS)->lpVtbl->Initialize(THIS, __VA_ARGS__) #define IXAudio2_Initialize(THIS, ...) (THIS)->lpVtbl->Initialize(THIS, __VA_ARGS__)
#define IXAudio2_Release(THIS) (THIS)->lpVtbl->Release(THIS) #define IXAudio2_Release(THIS) (THIS)->lpVtbl->Release(THIS)