Skip to content

Conversation

FoamyGuy
Copy link
Collaborator

The learn guide here: https://learn.adafruit.com/circuitpython-essentials/circuitpython-mp3-audio mentions:

Because creating an MP3Decoder object takes a lot of memory, it's best to do this just once when your program starts, and then update the .file property of the MP3Decoder when you want to play a different file. Otherwise, you may encounter the dreaded MemoryError.

And the constructor of MP3Decoder notes about the file argument https://docs.circuitpython.org/en/latest/shared-bindings/audiomp3/index.html#audiomp3.MP3Decoder

file (Union[str, BinaryIO]) – The name of a mp3 file (preferred) or an already opened mp3 file

So the learn guide recommends to use .file property if you will be playing more than one different mp3 file, and the MP3Decoder constructor docs recommend to use a string instead of an open file. But currently the .file property only allows an open file to be set, not a string with a filename.

This PR updates the .file property to allow it to accept a string with the filename the same way that the constructor does so that both recommendations can be followed by user code.

Originally noticed here: adafruit/Adafruit_CircuitPython_FruitJam#15 I will update the code there to make use of this functionality if this gets merged.

@tannewt
Copy link
Member

tannewt commented Aug 28, 2025

I'd rather just use the existing open:

//| def open(self, filepath: str) -> None:
//| """Takes in the name of a mp3 file, opens it, and replaces the old playback file."""
//| ...
//|
static mp_obj_t audiomp3_mp3file_obj_open(mp_obj_t self_in, mp_obj_t path) {
audiomp3_mp3file_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_obj_t file = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), path, MP_ROM_QSTR(MP_QSTR_rb));
common_hal_audiomp3_mp3file_set_file(self, file);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiomp3_mp3file_open_obj, audiomp3_mp3file_obj_open);

@tannewt tannewt closed this Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants