From 6e6fbc5ad3a026b346198aefb7e187ff2c44d2ff Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 3 Jan 2020 14:19:31 +0100 Subject: [PATCH] (Dsound) Split up dsound_write (XAudio) Optimize xa_write_nonblock --- audio/drivers/dsound.c | 40 +++++++++++++++++++++++++++++++++++++++- audio/drivers/xaudio.c | 14 +++++--------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/audio/drivers/dsound.c b/audio/drivers/dsound.c index 948792c460..23676b419d 100644 --- a/audio/drivers/dsound.c +++ b/audio/drivers/dsound.c @@ -53,6 +53,10 @@ #pragma comment(lib, "dxguid") #endif +/* Forward declarations */ +static ssize_t dsound_write_nonblock(void *data, const void *buf_, size_t size); +static ssize_t dsound_write(void *data, const void *buf_, size_t size); + typedef struct dsound { LPDIRECTSOUND ds; @@ -485,6 +489,40 @@ static void dsound_set_nonblock_state(void *data, bool state) dsound_t *ds = (dsound_t*)data; if (ds) ds->nonblock = state; + + if (ds->nonblock) + audio_dsound.write = dsound_write_nonblock; + else + audio_dsound.write = dsound_write; +} + +static ssize_t dsound_write_nonblock(void *data, const void *buf_, size_t size) +{ + size_t written = 0; + dsound_t *ds = (dsound_t*)data; + const uint8_t *buf = (const uint8_t*)buf_; + + if (!ds->thread_alive) + return -1; + + if (size > 0) + { + size_t avail; + + EnterCriticalSection(&ds->crit); + avail = fifo_write_avail(ds->buffer); + if (avail > size) + avail = size; + + fifo_write(ds->buffer, buf, avail); + LeaveCriticalSection(&ds->crit); + + buf += avail; + size -= avail; + written += avail; + } + + return written; } static ssize_t dsound_write(void *data, const void *buf_, size_t size) @@ -512,7 +550,7 @@ static ssize_t dsound_write(void *data, const void *buf_, size_t size) size -= avail; written += avail; - if (ds->nonblock || !ds->thread_alive) + if (!ds->thread_alive) break; if (avail == 0) diff --git a/audio/drivers/xaudio.c b/audio/drivers/xaudio.c index 07e725e12c..3f5ab74fbe 100644 --- a/audio/drivers/xaudio.c +++ b/audio/drivers/xaudio.c @@ -328,16 +328,12 @@ static ssize_t xa_write_nonblock(void *data, const void *buf, size_t size) xa_t *xa = (xa_t*)data; xaudio2_t *handle = xa->xa; const uint8_t *buffer = (const uint8_t*)buf; + size_t avail = XAUDIO2_WRITE_AVAILABLE(xa->xa); - if (xa->nonblock) - { - size_t avail = XAUDIO2_WRITE_AVAILABLE(xa->xa); - - if (avail == 0) - return 0; - if (avail < size) - size = avail; - } + if (avail == 0) + return 0; + if (avail < size) + size = avail; bytes = size;