Change to allow WaveFile and MP3Decoder to accept a file path

This commit is contained in:
snkYmkrct 2022-09-20 20:56:17 +02:00
parent b6f67be3e2
commit 21551a82e3
2 changed files with 25 additions and 6 deletions

View File

@ -69,15 +69,24 @@
//| a.play(wav) //| a.play(wav)
//| while a.playing: //| while a.playing:
//| pass //| 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) { 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_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); audioio_wavefile_obj_t *self = m_new_obj(audioio_wavefile_obj_t);
self->base.type = &audioio_wavefile_type; 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")); mp_raise_TypeError(translate("file must be a file opened in byte mode"));
} }
uint8_t *buffer = NULL; 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 = bufinfo.buf;
buffer_size = mp_arg_validate_length_range(bufinfo.len, 8, 1024, MP_QSTR_buffer); 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); buffer, buffer_size);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);

View File

@ -85,15 +85,25 @@
//| a.play(mp3) //| a.play(mp3)
//| while a.playing: //| while a.playing:
//| pass //| 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) { 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_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); audiomp3_mp3file_obj_t *self = m_new_obj(audiomp3_mp3file_obj_t);
self->base.type = &audiomp3_mp3file_type; 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")); mp_raise_TypeError(translate("file must be a file opened in byte mode"));
} }
uint8_t *buffer = NULL; 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 = bufinfo.buf;
buffer_size = bufinfo.len; 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); buffer, buffer_size);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);