diff options
author | XANTRONIX Development | 2022-01-15 16:58:50 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-01-15 16:58:50 -0500 |
commit | fc148fe8f7c765fd37f3f723a1fc6fe870a43a67 (patch) | |
tree | cb77a5dc2ffe0d14bcd69877ef73e553d4e5ea60 /src | |
parent | 712c1bafab413143b1edcd868a736f0fe6f026b9 (diff) | |
download | xas-fc148fe8f7c765fd37f3f723a1fc6fe870a43a67.tar.gz xas-fc148fe8f7c765fd37f3f723a1fc6fe870a43a67.tar.bz2 xas-fc148fe8f7c765fd37f3f723a1fc6fe870a43a67.zip |
Good drone!!!
Diffstat (limited to 'src')
-rw-r--r-- | src/audio.c | 53 | ||||
-rw-r--r-- | src/riff.c | 5 |
2 files changed, 44 insertions, 14 deletions
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 <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> @@ -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; } @@ -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; } |