summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2019-12-12 08:44:15 -0600
committerJeff Epler <jepler@gmail.com>2019-12-12 08:44:15 -0600
commit1cdac3f16a9244c992e8494a7246ab643cdb47c0 (patch)
treebc50ab0a7659defa75b867027330192c802fd1c5
parent8c841af9461eba6252690c38bc3e726f2fd09e8b (diff)
MP3File: Fix stereo playback on samd AudioOut
There were several problems with the way this worked -- the read_count approach was too complicated and I made a mistake "simplifying" it from WaveFile. And when the right channel was returned, it was off by 1 byte, making it into static. Instead, directly track which is the "other" channel that has data available, and by using the right data type make the "+ channel" arithmetic give the right result. This requires a double cast (int16_t*)(void*) due to an alignment warning; the alignment is now ensured manually, but the compiler doesn't make the necessary inference that the low address bit must be clear.
-rw-r--r--shared-module/audiomp3/MP3File.c46
-rw-r--r--shared-module/audiomp3/MP3File.h6
2 files changed, 30 insertions, 22 deletions
diff --git a/shared-module/audiomp3/MP3File.c b/shared-module/audiomp3/MP3File.c
index f0c4adc26..f4861da36 100644
--- a/shared-module/audiomp3/MP3File.c
+++ b/shared-module/audiomp3/MP3File.c
@@ -151,10 +151,13 @@ void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t* self,
self->channel_count = fi.nChans;
self->frame_buffer_size = fi.outputSamps*sizeof(int16_t);
+ if ((intptr_t)buffer & 1) {
+ buffer += 1; buffer_size -= 1;
+ }
if (buffer_size >= 2 * self->frame_buffer_size) {
self->len = buffer_size / 2 / self->frame_buffer_size * self->frame_buffer_size;
- self->buffers[0] = buffer;
- self->buffers[1] = buffer + self->len;
+ self->buffers[0] = (int16_t*)(void*)buffer;
+ self->buffers[1] = (int16_t*)(void*)buffer + self->len;
} else {
self->len = 2 * self->frame_buffer_size;
self->buffers[0] = m_malloc(self->len, false);
@@ -218,6 +221,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t* self,
f_lseek(&self->file->fp, 0);
self->inbuf_offset = self->inbuf_length;
self->eof = 0;
+ self->other_channel = -1;
mp3file_update_inbuf(self);
mp3file_find_sync_word(self);
}
@@ -234,26 +238,30 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
channel = 0;
}
- uint16_t channel_read_count = self->channel_read_count[channel]++;
- bool need_more_data = self->read_count++ == channel_read_count;
-
- *bufptr = self->buffers[self->buffer_index] + channel;
+ *bufptr = (uint8_t*)(self->buffers[self->buffer_index] + channel);
*buffer_length = self->frame_buffer_size;
- if (need_more_data) {
- self->buffer_index = !self->buffer_index;
- int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
+ if (channel == self->other_channel) {
+ *bufptr = (uint8_t*)(self->buffers[self->other_buffer_index] + channel);
+ self->other_channel = -1;
+ return GET_BUFFER_MORE_DATA;
+ }
- if (!mp3file_find_sync_word(self)) {
- return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
- }
- int bytes_left = BYTES_LEFT(self);
- uint8_t *inbuf = READ_PTR(self);
- int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
- CONSUME(self, BYTES_LEFT(self) - bytes_left);
- if (err) {
- return GET_BUFFER_DONE;
- }
+ self->other_channel = 1-channel;
+ self->other_buffer_index = self->buffer_index;
+
+ self->buffer_index = !self->buffer_index;
+ int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
+
+ if (!mp3file_find_sync_word(self)) {
+ return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
+ }
+ int bytes_left = BYTES_LEFT(self);
+ uint8_t *inbuf = READ_PTR(self);
+ int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
+ CONSUME(self, BYTES_LEFT(self) - bytes_left);
+ if (err) {
+ return GET_BUFFER_DONE;
}
return GET_BUFFER_MORE_DATA;
diff --git a/shared-module/audiomp3/MP3File.h b/shared-module/audiomp3/MP3File.h
index 12649ac1a..9d99e8d1f 100644
--- a/shared-module/audiomp3/MP3File.h
+++ b/shared-module/audiomp3/MP3File.h
@@ -39,7 +39,7 @@ typedef struct {
uint8_t* inbuf;
uint32_t inbuf_length;
uint32_t inbuf_offset;
- uint8_t* buffers[2];
+ int16_t* buffers[2];
uint32_t len;
uint32_t frame_buffer_size;
@@ -50,8 +50,8 @@ typedef struct {
uint8_t channel_count;
bool eof;
- uint16_t read_count;
- uint16_t channel_read_count[2];
+ int8_t other_channel;
+ int8_t other_buffer_index;
} audiomp3_mp3file_obj_t;
// These are not available from Python because it may be called in an interrupt.