From fc148fe8f7c765fd37f3f723a1fc6fe870a43a67 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 15 Jan 2022 16:58:50 -0500 Subject: Good drone!!! --- include/xas/audio.h | 8 ++++---- src/audio.c | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- src/riff.c | 5 ----- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/include/xas/audio.h b/include/xas/audio.h index 4ffc430..8a7fe4e 100644 --- a/include/xas/audio.h +++ b/include/xas/audio.h @@ -11,10 +11,10 @@ enum xas_audio_stream_type { typedef struct _xas_audio_stream xas_audio_stream; -typedef int (*xas_audio_drain)(void *ctx, - void *samples, - size_t count, - xas_audio_stream *stream); +typedef ssize_t (*xas_audio_drain)(void *ctx, + void *samples, + size_t count, + xas_audio_stream *stream); typedef ssize_t (*xas_audio_fill)(void *ctx, void *samples, diff --git a/src/audio.c b/src/audio.c index 8922698..5902589 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -81,7 +80,6 @@ void xas_audio_stream_destroy(xas_audio_stream *stream) { } static inline int stream_flush(xas_audio_stream *stream) { - printf("Flushing stream\n"); if (stream->buffer_count == 0) { return 0; } @@ -101,26 +99,63 @@ error_stream_drain: return -1; } +static inline void *ptr(xas_audio_stream *stream, void *buf, size_t index) { + return ((uint8_t *)buf) + stream->sample_size * stream->channels * index; +} + ssize_t xas_audio_stream_write(xas_audio_stream *stream, void *samples, size_t count) { - size_t total = stream->buffer_count + count, - offset = stream->sample_size * stream->channels * stream->buffer_count; + 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) { + 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); + } - if (total > stream->buffer_size) { if (stream_flush(stream) < 0) { goto error_stream_flush; } + + if (remainder) { + left -= remainder; + index += remainder; + } } - memcpy((uint8_t *)(stream + 1) + offset, - samples, - stream->sample_size * stream->channels * count); + while (left > stream->buffer_size) { + ssize_t drained; - stream->buffer_count += count; + if ((drained = stream->drain(stream->ctx, + ptr(stream, samples, index), + stream->buffer_size, + stream)) < 0) { + goto error_stream_drain; + } + + left -= drained; + index += drained; + } + + memcpy(ptr(stream, stream + 1, stream->buffer_count), + ptr(stream, samples, index), + stream->sample_size * stream->channels * left); + + stream->buffer_count += left; return count; +error_stream_drain: error_stream_flush: return -1; } diff --git a/src/riff.c b/src/riff.c index cae821e..73ccad5 100644 --- a/src/riff.c +++ b/src/riff.c @@ -48,11 +48,6 @@ static int header_write(xas_riff *riff) { } }; - printf("channels %zu sample size %zu sample rate %zu\n", - riff->channels, - riff->sample_size, - riff->sample_rate); - if (lseek(riff->fd, 0, SEEK_SET) < 0) { goto error_io; } -- cgit v1.2.3