Skip to content

Commit 1cdac3f

Browse files
committed
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.
1 parent 8c841af commit 1cdac3f

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

shared-module/audiomp3/MP3File.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t* self,
151151
self->channel_count = fi.nChans;
152152
self->frame_buffer_size = fi.outputSamps*sizeof(int16_t);
153153

154+
if ((intptr_t)buffer & 1) {
155+
buffer += 1; buffer_size -= 1;
156+
}
154157
if (buffer_size >= 2 * self->frame_buffer_size) {
155158
self->len = buffer_size / 2 / self->frame_buffer_size * self->frame_buffer_size;
156-
self->buffers[0] = buffer;
157-
self->buffers[1] = buffer + self->len;
159+
self->buffers[0] = (int16_t*)(void*)buffer;
160+
self->buffers[1] = (int16_t*)(void*)buffer + self->len;
158161
} else {
159162
self->len = 2 * self->frame_buffer_size;
160163
self->buffers[0] = m_malloc(self->len, false);
@@ -218,6 +221,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t* self,
218221
f_lseek(&self->file->fp, 0);
219222
self->inbuf_offset = self->inbuf_length;
220223
self->eof = 0;
224+
self->other_channel = -1;
221225
mp3file_update_inbuf(self);
222226
mp3file_find_sync_word(self);
223227
}
@@ -234,26 +238,30 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
234238
channel = 0;
235239
}
236240

237-
uint16_t channel_read_count = self->channel_read_count[channel]++;
238-
bool need_more_data = self->read_count++ == channel_read_count;
239-
240-
*bufptr = self->buffers[self->buffer_index] + channel;
241+
*bufptr = (uint8_t*)(self->buffers[self->buffer_index] + channel);
241242
*buffer_length = self->frame_buffer_size;
242243

243-
if (need_more_data) {
244-
self->buffer_index = !self->buffer_index;
245-
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
244+
if (channel == self->other_channel) {
245+
*bufptr = (uint8_t*)(self->buffers[self->other_buffer_index] + channel);
246+
self->other_channel = -1;
247+
return GET_BUFFER_MORE_DATA;
248+
}
246249

247-
if (!mp3file_find_sync_word(self)) {
248-
return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
249-
}
250-
int bytes_left = BYTES_LEFT(self);
251-
uint8_t *inbuf = READ_PTR(self);
252-
int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
253-
CONSUME(self, BYTES_LEFT(self) - bytes_left);
254-
if (err) {
255-
return GET_BUFFER_DONE;
256-
}
250+
self->other_channel = 1-channel;
251+
self->other_buffer_index = self->buffer_index;
252+
253+
self->buffer_index = !self->buffer_index;
254+
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
255+
256+
if (!mp3file_find_sync_word(self)) {
257+
return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
258+
}
259+
int bytes_left = BYTES_LEFT(self);
260+
uint8_t *inbuf = READ_PTR(self);
261+
int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
262+
CONSUME(self, BYTES_LEFT(self) - bytes_left);
263+
if (err) {
264+
return GET_BUFFER_DONE;
257265
}
258266

259267
return GET_BUFFER_MORE_DATA;

shared-module/audiomp3/MP3File.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct {
3939
uint8_t* inbuf;
4040
uint32_t inbuf_length;
4141
uint32_t inbuf_offset;
42-
uint8_t* buffers[2];
42+
int16_t* buffers[2];
4343
uint32_t len;
4444
uint32_t frame_buffer_size;
4545

@@ -50,8 +50,8 @@ typedef struct {
5050
uint8_t channel_count;
5151
bool eof;
5252

53-
uint16_t read_count;
54-
uint16_t channel_read_count[2];
53+
int8_t other_channel;
54+
int8_t other_buffer_index;
5555
} audiomp3_mp3file_obj_t;
5656

5757
// These are not available from Python because it may be called in an interrupt.

0 commit comments

Comments
 (0)