From 727d0a16832076577db68d97301bc9ff6347d36f Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 21 Jul 2019 15:49:59 +0200 Subject: [PATCH] (Xaudio2) Optimize xa_write --- audio/drivers/xaudio.c | 101 ++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/audio/drivers/xaudio.c b/audio/drivers/xaudio.c index 5ee0fb6a7a..80d9ed9c9e 100644 --- a/audio/drivers/xaudio.c +++ b/audio/drivers/xaudio.c @@ -254,52 +254,6 @@ error: return NULL; } -static size_t xaudio2_write(xaudio2_t *handle, const uint8_t *buffer, size_t bytes_) -{ - unsigned bytes = bytes_; - - while (bytes) - { - unsigned need = MIN(bytes, handle->bufsize - handle->bufptr); - - memcpy(handle->buf + handle->write_buffer * - handle->bufsize + handle->bufptr, - buffer, need); - - handle->bufptr += need; - buffer += need; - bytes -= need; - - if (handle->bufptr == handle->bufsize) - { - XAUDIO2_BUFFER xa2buffer; - - while (handle->buffers == MAX_BUFFERS - 1) - WaitForSingleObject(handle->hEvent, INFINITE); - - xa2buffer.Flags = 0; - xa2buffer.AudioBytes = handle->bufsize; - xa2buffer.pAudioData = handle->buf + handle->write_buffer * handle->bufsize; - xa2buffer.PlayBegin = 0; - xa2buffer.PlayLength = 0; - xa2buffer.LoopBegin = 0; - xa2buffer.LoopLength = 0; - xa2buffer.LoopCount = 0; - xa2buffer.pContext = NULL; - - if (FAILED(IXAudio2SourceVoice_SubmitSourceBuffer( - handle->pSourceVoice, &xa2buffer, NULL))) - return 0; - - InterlockedIncrement((LONG volatile*)&handle->buffers); - handle->bufptr = 0; - handle->write_buffer = (handle->write_buffer + 1) & MAX_BUFFERS_MASK; - } - } - - return bytes_; -} - static void *xa_init(const char *device, unsigned rate, unsigned latency, unsigned block_frames, unsigned *new_rate) @@ -337,7 +291,10 @@ static void *xa_init(const char *device, unsigned rate, unsigned latency, static ssize_t xa_write(void *data, const void *buf, size_t size) { size_t ret; - xa_t *xa = (xa_t*)data; + unsigned bytes; + xa_t *xa = (xa_t*)data; + xaudio2_t *handle = xa->xa; + const uint8_t *buffer = (const uint8_t*)buf; if (xa->nonblock) { @@ -349,10 +306,52 @@ static ssize_t xa_write(void *data, const void *buf, size_t size) size = avail; } - ret = xaudio2_write(xa->xa, (const uint8_t*)buf, size); - if (ret == 0 && size > 0) - return -1; - return ret; + bytes = size; + + while (bytes) + { + unsigned need = MIN(bytes, handle->bufsize - handle->bufptr); + + memcpy(handle->buf + handle->write_buffer * + handle->bufsize + handle->bufptr, + buffer, need); + + handle->bufptr += need; + buffer += need; + bytes -= need; + + if (handle->bufptr == handle->bufsize) + { + XAUDIO2_BUFFER xa2buffer; + + while (handle->buffers == MAX_BUFFERS - 1) + WaitForSingleObject(handle->hEvent, INFINITE); + + xa2buffer.Flags = 0; + xa2buffer.AudioBytes = handle->bufsize; + xa2buffer.pAudioData = handle->buf + handle->write_buffer * handle->bufsize; + xa2buffer.PlayBegin = 0; + xa2buffer.PlayLength = 0; + xa2buffer.LoopBegin = 0; + xa2buffer.LoopLength = 0; + xa2buffer.LoopCount = 0; + xa2buffer.pContext = NULL; + + if (FAILED(IXAudio2SourceVoice_SubmitSourceBuffer( + handle->pSourceVoice, &xa2buffer, NULL))) + { + if (size > 0) + return -1; + return 0; + } + + InterlockedIncrement((LONG volatile*)&handle->buffers); + handle->bufptr = 0; + handle->write_buffer = (handle->write_buffer + 1) & MAX_BUFFERS_MASK; + } + } + + return size; } static bool xa_stop(void *data)