diff options
author | XANTRONIX Development | 2022-02-03 18:04:47 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-02-03 18:04:47 -0500 |
commit | c3ce5c1fdd5525c9618f0e4c24ef75349ec2ac4e (patch) | |
tree | 2755c2dd2b2f7eb97955770470d8dcc3177d53e7 | |
parent | 0f2d7d778142da30ef5d7c8d740f7d7a8d2a87ae (diff) | |
download | xas-c3ce5c1fdd5525c9618f0e4c24ef75349ec2ac4e.tar.gz xas-c3ce5c1fdd5525c9618f0e4c24ef75349ec2ac4e.tar.bz2 xas-c3ce5c1fdd5525c9618f0e4c24ef75349ec2ac4e.zip |
holy shit idk what just happened but it works
-rw-r--r-- | src/bank.c | 93 |
1 files changed, 65 insertions, 28 deletions
@@ -9,11 +9,11 @@ xas_bank *xas_bank_new(size_t sample_size, size_t entry_count) { xas_bank *bank; - size_t entry_size_total = sizeof(xas_bank_entry) + entry_size; + size_t entry_size_total = sizeof(xas_bank_entry) + sample_size + * entry_size; - size_t total = sizeof(xas_bank) + sample_size - * entry_size_total - * entry_count; + size_t total = sizeof(xas_bank) + entry_count + * entry_size_total; if ((bank = malloc(total)) == NULL) { goto error_malloc_bank; @@ -60,50 +60,87 @@ static inline void *ptr(xas_audio_stream *stream, void *buf, size_t index) { return ((uint8_t *)buf) + stream->channels * stream->sample_size * index; } -static ssize_t bank_fill(xas_bank *bank, - int16_t *samples, - size_t count, - xas_audio_stream *stream) { - size_t index_o = 0, - left = count; +ssize_t xas_bank_record(xas_bank *bank, + xas_audio_stream *input, + size_t entry_index, + size_t count) { + xas_bank_entry *entry = &((xas_bank_entry *)(bank + 1))[entry_index]; - if (!(bank->flags & XAS_BANK_ACTIVE)) { - memset(samples, '\0', count * bank->sample_size); + size_t left = count, + index_o = 0; - return count; + if (count > bank->entry_size) { + count = bank->entry_size; } while (left) { - xas_bank_entry *entry = - &((xas_bank_entry *)(bank + 1))[bank->entry]; + ssize_t readlen, + amount = left > count? count: left; - size_t remaining = bank->entry_size - bank->index, - amount = remaining < left? remaining: left; + void *buf; - memcpy(ptr(stream, samples, index_o), - ptr(stream, entry + 1, bank->index), - amount * stream->channels * stream->sample_size); + if ((readlen = xas_audio_stream_read(input, &buf, amount)) < 0) { + goto error_audio_stream_read; + } + + memcpy(ptr(input, entry + 1, index_o), + ptr(input, buf, 0), + readlen * bank->sample_size); + + left -= readlen; + index_o += readlen; + } + + return entry->duration = index_o; + +error_audio_stream_read: + return -1; +} + +static ssize_t stream_fill(xas_bank *bank, + int16_t *samples, + size_t count, + xas_audio_stream *stream) { + size_t index_o = 0, + left = count; + + while (left) { + if (bank->flags & XAS_BANK_ACTIVE) { + xas_bank_entry *entry = + &((xas_bank_entry *)(bank + 1))[bank->entry]; + + size_t remaining = entry->duration - bank->index, + amount = remaining < left? remaining: left; + + memcpy(ptr(stream, samples, index_o), + ptr(stream, entry + 1, bank->index), + amount * stream->channels * stream->sample_size); - left -= amount; - index_o += amount; + left -= amount; + bank->index += amount; + index_o += amount; - bank->index += amount; + if (bank->index == entry->duration) { + xas_bank_stop(bank); + } + } else { + memset(ptr(stream, samples, index_o), '\0', left); - if (bank->index == entry->duration) { - bank->index = 0; + index_o += left; + left = 0; } } return count; } -void bank_cleanup(xas_bank *bank, xas_audio_stream *stream) { +void stream_cleanup(xas_bank *bank, xas_audio_stream *stream) { return; } xas_audio_stream *xas_bank_stream_new(xas_bank *bank) { - return xas_audio_stream_new_source((xas_audio_fill)bank_fill, - (xas_audio_cleanup)bank_cleanup, + return xas_audio_stream_new_source((xas_audio_fill)stream_fill, + (xas_audio_cleanup)stream_cleanup, bank, XAS_AUDIO_MONO, bank->sample_size, |