Skip to content

Change to allow WaveFile and MP3Decoder to accept a file path #6931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions shared-bindings/audiocore/WaveFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@
//| be 8 bit unsigned or 16 bit signed. If a buffer is provided, it will be used instead of allocating
//| an internal buffer, which can prevent memory fragmentation."""
//|
//| def __init__(self, file: typing.BinaryIO, buffer: WriteableBuffer) -> None:
//| def __init__(self, file: Union[str, typing.BinaryIO], buffer: WriteableBuffer) -> None:
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
//|
//| :param typing.BinaryIO file: Already opened wave file
//| :param Union[str, typing.BinaryIO] file: The name of a wave file (preferred) or an already opened wave file
//| :param ~circuitpython_typing.WriteableBuffer buffer: Optional pre-allocated buffer,
//| that will be split in half and used for double-buffering of the data.
//| The buffer must be 8 to 1024 bytes long.
//| If not provided, two 256 byte buffers are initially allocated internally.
//|
//|
//| Playing a wave file from flash::
//|
//| import board
Expand All @@ -61,23 +60,28 @@
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
//| speaker_enable.switch_to_output(value=True)
//|
//| data = open("cplay-5.1-16bit-16khz.wav", "rb")
//| wav = audiocore.WaveFile(data)
//| wav = audiocore.WaveFile("cplay-5.1-16bit-16khz.wav")
//| a = audioio.AudioOut(board.A0)
//|
//| print("playing")
//| a.play(wav)
//| while a.playing:
//| pass
//| print("stopped")"""
//| print("stopped")
//| """
//| ...
//|
STATIC mp_obj_t audioio_wavefile_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 2, false);
mp_obj_t arg = args[0];

if (mp_obj_is_str(arg)) {
arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb));
}

audioio_wavefile_obj_t *self = m_new_obj(audioio_wavefile_obj_t);
self->base.type = &audioio_wavefile_type;
if (!mp_obj_is_type(args[0], &mp_type_fileio)) {
if (!mp_obj_is_type(arg, &mp_type_fileio)) {
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
}
uint8_t *buffer = NULL;
Expand All @@ -88,7 +92,7 @@ STATIC mp_obj_t audioio_wavefile_make_new(const mp_obj_type_t *type, size_t n_ar
buffer = bufinfo.buf;
buffer_size = mp_arg_validate_length_range(bufinfo.len, 8, 1024, MP_QSTR_buffer);
}
common_hal_audioio_wavefile_construct(self, MP_OBJ_TO_PTR(args[0]),
common_hal_audioio_wavefile_construct(self, MP_OBJ_TO_PTR(arg),
buffer, buffer_size);

return MP_OBJ_FROM_PTR(self);
Expand Down
20 changes: 13 additions & 7 deletions shared-bindings/audiomp3/MP3Decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
//| https://learn.adafruit.com/Memory-saving-tips-for-CircuitPython/reducing-memory-fragmentation
//| """
//|
//| def __init__(self, file: typing.BinaryIO, buffer: WriteableBuffer) -> None:
//| def __init__(self, file: Union[str, typing.BinaryIO], buffer: WriteableBuffer) -> None:
//|
//| """Load a .mp3 file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
//|
//| :param typing.BinaryIO file: Already opened mp3 file
//| :param Union[str, typing.BinaryIO] file: The name of a mp3 file (preferred) or an already opened mp3 file
//| :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.
//|
//| Playback of mp3 audio is CPU intensive, and the
Expand Down Expand Up @@ -77,23 +77,29 @@
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
//| speaker_enable.switch_to_output(value=True)
//|
//| data = open("cplay-16bit-16khz-64kbps.mp3", "rb")
//| mp3 = audiomp3.MP3Decoder(data)
//| mp3 = audiomp3.MP3Decoder("cplay-16bit-16khz-64kbps.mp3")
//| a = audioio.AudioOut(board.A0)
//|
//| print("playing")
//| a.play(mp3)
//| while a.playing:
//| pass
//| print("stopped")"""
//| print("stopped")
//| """
//| ...
//|

STATIC mp_obj_t audiomp3_mp3file_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 2, false);
mp_obj_t arg = args[0];

if (mp_obj_is_str(arg)) {
arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb));
}

audiomp3_mp3file_obj_t *self = m_new_obj(audiomp3_mp3file_obj_t);
self->base.type = &audiomp3_mp3file_type;
if (!mp_obj_is_type(args[0], &mp_type_fileio)) {
if (!mp_obj_is_type(arg, &mp_type_fileio)) {
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
}
uint8_t *buffer = NULL;
Expand All @@ -104,7 +110,7 @@ STATIC mp_obj_t audiomp3_mp3file_make_new(const mp_obj_type_t *type, size_t n_ar
buffer = bufinfo.buf;
buffer_size = bufinfo.len;
}
common_hal_audiomp3_mp3file_construct(self, MP_OBJ_TO_PTR(args[0]),
common_hal_audiomp3_mp3file_construct(self, MP_OBJ_TO_PTR(arg),
buffer, buffer_size);

return MP_OBJ_FROM_PTR(self);
Expand Down