Skip to content

Commit cb295c5

Browse files
committed
MP3Decoder: Make it possible to start in the middle of an MP3
You can now, e.g., ``` with open("/whatever.mp3") as mp3_file: mp3_file.seek(16000*30) decoder.file = mp3_file i2s.play(decoder) ``` to start about 30 seconds into a 128kbit/s CBR track. If a track is looped, the loop will start at the beginning. This also changes the behavior if the track is started & stopped: it will continue from where it left off, except if it had prevously run to completion. To get other behavior, you can seek the file and then re-assign the file property.
1 parent 6f6b680 commit cb295c5

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

shared-bindings/audiomp3/MP3Decoder.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
//| """Load a .mp3 file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
2929
//|
3030
//| :param Union[str, typing.BinaryIO] file: The name of a mp3 file (preferred) or an already opened mp3 file
31-
//| :param ~circuitpython_typing.WriteableBuffer buffer: Optional pre-allocated buffer, that will be split in half and used for double-buffering of the data. If not provided, two buffers are allocated internally. The specific buffer size required depends on the mp3 file.
31+
//| :param ~circuitpython_typing.WriteableBuffer buffer: Optional pre-allocated buffer, that will be split and used for buffering the data. The buffer is split into two parts for decoded data and the remainder is used for pre-decoded data. When playing from a socket, a larger buffer can help reduce playback glitches at the expense of increased memory usage.
3232
//|
3333
//| Playback of mp3 audio is CPU intensive, and the
3434
//| exact limit depends on many factors such as the particular
35-
//| microcontroller, SD card or flash performance, and other
36-
//| code in use such as displayio. If playback is garbled,
37-
//| skips, or plays as static, first try using a "simpler" mp3:
35+
//| microcontroller, SD card or flash performance, network performance, and
36+
//| other code in use such as displayio. If playback is garbled, skips, or plays as
37+
//| static, first try using a "simpler" mp3:
3838
//|
3939
//| * Use constant bit rate (CBR) not VBR or ABR (variable or average bit rate) when encoding your mp3 file
4040
//| * Use a lower sample rate (e.g., 11.025kHz instead of 48kHz)
@@ -64,6 +64,14 @@
6464
//| while a.playing:
6565
//| pass
6666
//| print("stopped")
67+
//|
68+
//| It is possible to seek within a file before playing it::
69+
//|
70+
//| with open("/test.mp3", "rb") as stream:
71+
//| stream.seek(128000 * 30 // 8) # Seek about 30s into a 128kbit/s stream
72+
//| decoder.file = stream
73+
//|
74+
//| If the stream is played with ``loop = True``, the loop will start at the beginning.
6775
//| """
6876
//| ...
6977

shared-module/audiomp3/MP3Decoder.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,6 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, mp_obj_t
329329

330330
self->stream = stream;
331331

332-
// Seek the beginning of the stream if possible, but ignore any errors
333-
(void)stream_lseek(self->stream, SEEK_SET, 0);
334-
335332
INPUT_BUFFER_CLEAR(self->inbuf);
336333
self->eof = 0;
337334
self->other_channel = -1;
@@ -401,7 +398,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t *self,
401398
// We don't reset the buffer index in case we're looping and we have an odd number of buffer
402399
// loads
403400
background_callback_prevent();
404-
if (stream_lseek(self->stream, SEEK_SET, 0) == 0) {
401+
if (self->eof && stream_lseek(self->stream, SEEK_SET, 0) == 0) {
405402
INPUT_BUFFER_CLEAR(self->inbuf);
406403
self->eof = 0;
407404
self->samples_decoded = 0;

0 commit comments

Comments
 (0)