Skip to content

Commit 97bb46c

Browse files
committed
MP3File: tweak buffer handling
After adding the ability to change files in an existing MP3File object, it became apparent that at the beginning of a track some part of an existing buffer was playing first. I noticed that in get_buffer, the just-populated buffer wasn't being returned, but the other one was. But still after fixing this, I heard wrong audio at the beginning of a track, so I took the heavy duty approach and zeroed the buffers out. That means there's a remaining bug to chase, which is merely hidden by the memset()s.
1 parent 5aa3e3e commit 97bb46c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

shared-module/audiomp3/MP3File.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t* self, pyb_file
215215
self->other_channel = -1;
216216
mp3file_update_inbuf(self);
217217
mp3file_find_sync_word(self);
218+
// It **SHOULD** not be necessary to do this; the buffer should be filled
219+
// with fresh content before it is returned by get_buffer(). The fact that
220+
// this is necessary to avoid a glitch at the start of playback of a second
221+
// track using the same decoder object means there's still a bug in
222+
// get_buffer() that I didn't understand.
223+
memset(self->buffers[0], 0, MAX_BUFFER_LEN);
224+
memset(self->buffers[1], 0, MAX_BUFFER_LEN);
218225
MP3FrameInfo fi;
219226
if(!mp3file_get_next_frame_info(self, &fi)) {
220227
mp_raise_msg(&mp_type_RuntimeError,
@@ -290,7 +297,6 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
290297
channel = 0;
291298
}
292299

293-
*bufptr = (uint8_t*)(self->buffers[self->buffer_index] + channel);
294300
*buffer_length = self->frame_buffer_size;
295301

296302
if (channel == self->other_channel) {
@@ -299,11 +305,12 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
299305
return GET_BUFFER_MORE_DATA;
300306
}
301307

302-
self->other_channel = 1-channel;
303-
self->other_buffer_index = self->buffer_index;
304308

305309
self->buffer_index = !self->buffer_index;
310+
self->other_channel = 1-channel;
311+
self->other_buffer_index = self->buffer_index;
306312
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
313+
*bufptr = (uint8_t*)buffer;
307314

308315
mp3file_skip_id3v2(self);
309316
if (!mp3file_find_sync_word(self)) {

0 commit comments

Comments
 (0)