diff options
author | XANTRONIX Development | 2022-02-01 02:37:15 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-02-01 02:37:15 -0500 |
commit | 2ba47727bc4690336e5d9d5f5b83d82577ce7515 (patch) | |
tree | cb72ee3ed3284e19d60b0546320ff3b6ad735f2d | |
parent | 09d69d9f83b83326c670600c5f9dfc4f843df86a (diff) | |
download | xas-2ba47727bc4690336e5d9d5f5b83d82577ce7515.tar.gz xas-2ba47727bc4690336e5d9d5f5b83d82577ce7515.tar.bz2 xas-2ba47727bc4690336e5d9d5f5b83d82577ce7515.zip |
Everything accounted for!
-rw-r--r-- | src/audio.c | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/src/audio.c b/src/audio.c index d0a1e63..8853164 100644 --- a/src/audio.c +++ b/src/audio.c @@ -109,51 +109,44 @@ ssize_t xas_audio_stream_write(xas_audio_stream *stream, size_t left = count, index = 0; - /* - * First, if the requested number of samples to write to the stream, plus - * the current number in the buffer, is greater than the buffer size, fill - * the buffer to capacity and flush. - */ - if (stream->buffer_count + count > stream->buffer_size) { + if (stream->buffer_count > 0) { size_t remainder = stream->buffer_size - stream->buffer_count; - if (remainder) { - memcpy(ptr(stream, stream + 1, stream->buffer_count), - samples, - stream->sample_size * stream->channels * remainder); - } + memcpy(ptr(stream, stream + 1, stream->buffer_count), + ptr(stream, samples, 0), + stream->sample_size * stream->channels * remainder); + + stream->buffer_count += remainder; if (stream_flush(stream) < 0) { goto error_stream_flush; } - if (remainder) { - left -= remainder; - index += remainder; - } + left -= remainder; + index += remainder; } while (left > stream->buffer_size) { - ssize_t drained; - - if ((drained = stream->drain(stream->ctx, - ptr(stream, samples, index), - stream->buffer_size, - stream)) < 0) { + if (stream->drain(stream->ctx, + ptr(stream, samples, index), + stream->buffer_size, + stream) < 0) { goto error_stream_drain; } - left -= drained; - index += drained; + left -= stream->buffer_size; + index += stream->buffer_size; } - memcpy(ptr(stream, stream + 1, stream->buffer_count), + memcpy(ptr(stream, stream + 1, 0), ptr(stream, samples, index), stream->sample_size * stream->channels * left); - stream->buffer_count += left; + stream->buffer_count = left; + + index += left; - return count; + return index; error_stream_drain: error_stream_flush: |