diff options
-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: |