summaryrefslogtreecommitdiff
path: root/shared-module/audioio/WaveFile.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2018-10-02 18:06:52 -0700
committerScott Shawcroft <scott@tannewt.org>2018-10-05 15:12:23 -0700
commit76008ce304e09394733a10d56b944218b41630ca (patch)
treef111205ae7dd7dadd9a3d978e55f3012f20f3969 /shared-module/audioio/WaveFile.c
parent7b95818c4f456736a8737e40c37d5555855f51a6 (diff)
Introduce audioio.Mixer which can mix multiple audio samples
to produce a single sample. Only works with 16 bit samples on the M4. Fixes #987
Diffstat (limited to 'shared-module/audioio/WaveFile.c')
-rw-r--r--shared-module/audioio/WaveFile.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/shared-module/audioio/WaveFile.c b/shared-module/audioio/WaveFile.c
index 7efad05e0..e5a7b1a3b 100644
--- a/shared-module/audioio/WaveFile.c
+++ b/shared-module/audioio/WaveFile.c
@@ -200,13 +200,29 @@ audioio_get_buffer_result_t audioio_wavefile_get_buffer(audioio_wavefile_obj_t*
if (f_read(&self->file->fp, *buffer, num_bytes_to_load, &length_read) != FR_OK) {
return GET_BUFFER_ERROR;
}
+ self->bytes_remaining -= length_read;
+ // Pad the last buffer to word align it.
+ if (self->bytes_remaining == 0 && length_read % sizeof(uint32_t) != 0) {
+ uint32_t pad = length_read % sizeof(uint32_t);
+ length_read += pad;
+ if (self->bits_per_sample == 8) {
+ for (uint32_t i = 0; i < pad; i++) {
+ ((uint8_t*) (*buffer))[length_read / sizeof(uint8_t) - i - 1] = 0x80;
+ }
+ } else if (self->bits_per_sample == 16) {
+ // We know the buffer is aligned because we allocated it onto the heap ourselves.
+ #pragma GCC diagnostic push
+ #pragma GCC diagnostic ignored "-Wcast-align"
+ ((int16_t*) (*buffer))[length_read / sizeof(int16_t) - 1] = 0;
+ #pragma GCC diagnostic pop
+ }
+ }
*buffer_length = length_read;
if (self->buffer_index % 2 == 1) {
self->second_buffer_length = length_read;
} else {
self->buffer_length = length_read;
}
- self->bytes_remaining -= length_read;
self->buffer_index += 1;
self->read_count += 1;
}