From 21551a82e39e08297fff922d9596bc9e3bea6552 Mon Sep 17 00:00:00 2001 From: snkYmkrct Date: Tue, 20 Sep 2022 20:56:17 +0200 Subject: [PATCH 1/4] Change to allow WaveFile and MP3Decoder to accept a file path --- shared-bindings/audiocore/WaveFile.c | 15 ++++++++++++--- shared-bindings/audiomp3/MP3Decoder.c | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/shared-bindings/audiocore/WaveFile.c b/shared-bindings/audiocore/WaveFile.c index b1c51a9746..161be859bd 100644 --- a/shared-bindings/audiocore/WaveFile.c +++ b/shared-bindings/audiocore/WaveFile.c @@ -69,15 +69,24 @@ //| a.play(wav) //| while a.playing: //| pass -//| print("stopped")""" +//| print("stopped") +//| +//| Support was added for taking a filename as parameter, instead of an opened file, +//| and opening the file internally. +//| """ //| ... //| 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; @@ -88,7 +97,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); diff --git a/shared-bindings/audiomp3/MP3Decoder.c b/shared-bindings/audiomp3/MP3Decoder.c index 8251b26c73..2381c8f2cb 100644 --- a/shared-bindings/audiomp3/MP3Decoder.c +++ b/shared-bindings/audiomp3/MP3Decoder.c @@ -85,15 +85,25 @@ //| a.play(mp3) //| while a.playing: //| pass -//| print("stopped")""" +//| print("stopped") +//| +//| Support was added for taking a filename as parameter, instead of an opened file, +//| and opening the file internally. +//| """ //| ... //| + 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; @@ -104,7 +114,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); From 129c5de6709caadba1801669ea211808b1ae74a2 Mon Sep 17 00:00:00 2001 From: snkYmkrct Date: Tue, 20 Sep 2022 23:46:55 +0200 Subject: [PATCH 2/4] Made requested changes in the documentation --- shared-bindings/audiocore/WaveFile.c | 12 ++++-------- shared-bindings/audiomp3/MP3Decoder.c | 11 ++++------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/shared-bindings/audiocore/WaveFile.c b/shared-bindings/audiocore/WaveFile.c index 161be859bd..75fb2342b8 100644 --- a/shared-bindings/audiocore/WaveFile.c +++ b/shared-bindings/audiocore/WaveFile.c @@ -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(typing.BinaryIO, str), 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(typing.BinaryIO, str) 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 @@ -61,8 +60,8 @@ //| 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") # preferred use +//| # wav = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb")) //| a = audioio.AudioOut(board.A0) //| //| print("playing") @@ -70,9 +69,6 @@ //| while a.playing: //| pass //| print("stopped") -//| -//| Support was added for taking a filename as parameter, instead of an opened file, -//| and opening the file internally. //| """ //| ... //| diff --git a/shared-bindings/audiomp3/MP3Decoder.c b/shared-bindings/audiomp3/MP3Decoder.c index 2381c8f2cb..09551c3495 100644 --- a/shared-bindings/audiomp3/MP3Decoder.c +++ b/shared-bindings/audiomp3/MP3Decoder.c @@ -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(typing.BinaryIO, str), 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(typing.BinaryIO, str) 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 @@ -77,8 +77,8 @@ //| 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") # preferred use +//| # mp3 = audiomp3.MP3Decoder(open("cplay-16bit-16khz-64kbps.mp3", "rb")) //| a = audioio.AudioOut(board.A0) //| //| print("playing") @@ -86,9 +86,6 @@ //| while a.playing: //| pass //| print("stopped") -//| -//| Support was added for taking a filename as parameter, instead of an opened file, -//| and opening the file internally. //| """ //| ... //| From bb40b5c98e9acf855cc10e10ad7c11e3b26b96b0 Mon Sep 17 00:00:00 2001 From: snkYmkrct Date: Wed, 21 Sep 2022 00:36:46 +0200 Subject: [PATCH 3/4] Correction --- shared-bindings/audiocore/WaveFile.c | 4 ++-- shared-bindings/audiomp3/MP3Decoder.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/shared-bindings/audiocore/WaveFile.c b/shared-bindings/audiocore/WaveFile.c index 75fb2342b8..0f069bfe51 100644 --- a/shared-bindings/audiocore/WaveFile.c +++ b/shared-bindings/audiocore/WaveFile.c @@ -40,10 +40,10 @@ //| 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: Union(typing.BinaryIO, str), 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 Union(typing.BinaryIO, str) file: The name of a wave file (preferred) or an 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. diff --git a/shared-bindings/audiomp3/MP3Decoder.c b/shared-bindings/audiomp3/MP3Decoder.c index 09551c3495..a7fe365bdf 100644 --- a/shared-bindings/audiomp3/MP3Decoder.c +++ b/shared-bindings/audiomp3/MP3Decoder.c @@ -44,11 +44,11 @@ //| https://learn.adafruit.com/Memory-saving-tips-for-CircuitPython/reducing-memory-fragmentation //| """ //| -//| def __init__(self, file: Union(typing.BinaryIO, str), 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 Union(typing.BinaryIO, str) file: The name of a mp3 file (preferred) or an 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 From 9c5abb6d7abba342bec2ebf55c0e00cfe202e949 Mon Sep 17 00:00:00 2001 From: snkYmkrct Date: Wed, 21 Sep 2022 12:52:17 +0200 Subject: [PATCH 4/4] Change the examples in documentation --- shared-bindings/audiocore/WaveFile.c | 3 +-- shared-bindings/audiomp3/MP3Decoder.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/shared-bindings/audiocore/WaveFile.c b/shared-bindings/audiocore/WaveFile.c index 0f069bfe51..8de1a3970f 100644 --- a/shared-bindings/audiocore/WaveFile.c +++ b/shared-bindings/audiocore/WaveFile.c @@ -60,8 +60,7 @@ //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) //| speaker_enable.switch_to_output(value=True) //| -//| wav = audiocore.WaveFile("cplay-5.1-16bit-16khz.wav") # preferred use -//| # wav = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb")) +//| wav = audiocore.WaveFile("cplay-5.1-16bit-16khz.wav") //| a = audioio.AudioOut(board.A0) //| //| print("playing") diff --git a/shared-bindings/audiomp3/MP3Decoder.c b/shared-bindings/audiomp3/MP3Decoder.c index a7fe365bdf..f1b5a29cd5 100644 --- a/shared-bindings/audiomp3/MP3Decoder.c +++ b/shared-bindings/audiomp3/MP3Decoder.c @@ -77,8 +77,7 @@ //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) //| speaker_enable.switch_to_output(value=True) //| -//| mp3 = audiomp3.MP3Decoder("cplay-16bit-16khz-64kbps.mp3") # preferred use -//| # mp3 = audiomp3.MP3Decoder(open("cplay-16bit-16khz-64kbps.mp3", "rb")) +//| mp3 = audiomp3.MP3Decoder("cplay-16bit-16khz-64kbps.mp3") //| a = audioio.AudioOut(board.A0) //| //| print("playing")