Skip to content

Commit 96179f6

Browse files
authored
Merge pull request #6931 from snkYmkrct/main
Change to allow WaveFile and MP3Decoder to accept a file path
2 parents b6f67be + 9c5abb6 commit 96179f6

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

shared-bindings/audiocore/WaveFile.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@
4040
//| be 8 bit unsigned or 16 bit signed. If a buffer is provided, it will be used instead of allocating
4141
//| an internal buffer, which can prevent memory fragmentation."""
4242
//|
43-
//| def __init__(self, file: typing.BinaryIO, buffer: WriteableBuffer) -> None:
43+
//| def __init__(self, file: Union[str, typing.BinaryIO], buffer: WriteableBuffer) -> None:
4444
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
4545
//|
46-
//| :param typing.BinaryIO file: Already opened wave file
46+
//| :param Union[str, typing.BinaryIO] file: The name of a wave file (preferred) or an already opened wave file
4747
//| :param ~circuitpython_typing.WriteableBuffer buffer: Optional pre-allocated buffer,
4848
//| that will be split in half and used for double-buffering of the data.
4949
//| The buffer must be 8 to 1024 bytes long.
5050
//| If not provided, two 256 byte buffers are initially allocated internally.
5151
//|
52-
//|
5352
//| Playing a wave file from flash::
5453
//|
5554
//| import board
@@ -61,23 +60,28 @@
6160
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
6261
//| speaker_enable.switch_to_output(value=True)
6362
//|
64-
//| data = open("cplay-5.1-16bit-16khz.wav", "rb")
65-
//| wav = audiocore.WaveFile(data)
63+
//| wav = audiocore.WaveFile("cplay-5.1-16bit-16khz.wav")
6664
//| a = audioio.AudioOut(board.A0)
6765
//|
6866
//| print("playing")
6967
//| a.play(wav)
7068
//| while a.playing:
7169
//| pass
72-
//| print("stopped")"""
70+
//| print("stopped")
71+
//| """
7372
//| ...
7473
//|
7574
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) {
7675
mp_arg_check_num(n_args, n_kw, 1, 2, false);
76+
mp_obj_t arg = args[0];
77+
78+
if (mp_obj_is_str(arg)) {
79+
arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb));
80+
}
7781

7882
audioio_wavefile_obj_t *self = m_new_obj(audioio_wavefile_obj_t);
7983
self->base.type = &audioio_wavefile_type;
80-
if (!mp_obj_is_type(args[0], &mp_type_fileio)) {
84+
if (!mp_obj_is_type(arg, &mp_type_fileio)) {
8185
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
8286
}
8387
uint8_t *buffer = NULL;
@@ -88,7 +92,7 @@ STATIC mp_obj_t audioio_wavefile_make_new(const mp_obj_type_t *type, size_t n_ar
8892
buffer = bufinfo.buf;
8993
buffer_size = mp_arg_validate_length_range(bufinfo.len, 8, 1024, MP_QSTR_buffer);
9094
}
91-
common_hal_audioio_wavefile_construct(self, MP_OBJ_TO_PTR(args[0]),
95+
common_hal_audioio_wavefile_construct(self, MP_OBJ_TO_PTR(arg),
9296
buffer, buffer_size);
9397

9498
return MP_OBJ_FROM_PTR(self);

shared-bindings/audiomp3/MP3Decoder.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
//| https://learn.adafruit.com/Memory-saving-tips-for-CircuitPython/reducing-memory-fragmentation
4545
//| """
4646
//|
47-
//| def __init__(self, file: typing.BinaryIO, buffer: WriteableBuffer) -> None:
47+
//| def __init__(self, file: Union[str, typing.BinaryIO], buffer: WriteableBuffer) -> None:
4848
//|
4949
//| """Load a .mp3 file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
5050
//|
51-
//| :param typing.BinaryIO file: Already opened mp3 file
51+
//| :param Union[str, typing.BinaryIO] file: The name of a mp3 file (preferred) or an already opened mp3 file
5252
//| :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.
5353
//|
5454
//| Playback of mp3 audio is CPU intensive, and the
@@ -77,23 +77,29 @@
7777
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
7878
//| speaker_enable.switch_to_output(value=True)
7979
//|
80-
//| data = open("cplay-16bit-16khz-64kbps.mp3", "rb")
81-
//| mp3 = audiomp3.MP3Decoder(data)
80+
//| mp3 = audiomp3.MP3Decoder("cplay-16bit-16khz-64kbps.mp3")
8281
//| a = audioio.AudioOut(board.A0)
8382
//|
8483
//| print("playing")
8584
//| a.play(mp3)
8685
//| while a.playing:
8786
//| pass
88-
//| print("stopped")"""
87+
//| print("stopped")
88+
//| """
8989
//| ...
9090
//|
91+
9192
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) {
9293
mp_arg_check_num(n_args, n_kw, 1, 2, false);
94+
mp_obj_t arg = args[0];
95+
96+
if (mp_obj_is_str(arg)) {
97+
arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb));
98+
}
9399

94100
audiomp3_mp3file_obj_t *self = m_new_obj(audiomp3_mp3file_obj_t);
95101
self->base.type = &audiomp3_mp3file_type;
96-
if (!mp_obj_is_type(args[0], &mp_type_fileio)) {
102+
if (!mp_obj_is_type(arg, &mp_type_fileio)) {
97103
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
98104
}
99105
uint8_t *buffer = NULL;
@@ -104,7 +110,7 @@ STATIC mp_obj_t audiomp3_mp3file_make_new(const mp_obj_type_t *type, size_t n_ar
104110
buffer = bufinfo.buf;
105111
buffer_size = bufinfo.len;
106112
}
107-
common_hal_audiomp3_mp3file_construct(self, MP_OBJ_TO_PTR(args[0]),
113+
common_hal_audiomp3_mp3file_construct(self, MP_OBJ_TO_PTR(arg),
108114
buffer, buffer_size);
109115

110116
return MP_OBJ_FROM_PTR(self);

0 commit comments

Comments
 (0)