Merge pull request #6931 from snkYmkrct/main
Change to allow WaveFile and MP3Decoder to accept a file path
This commit is contained in:
commit
96179f6b4c
@ -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
|
||||
@ -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;
|
||||
@ -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);
|
||||
|
@ -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
|
||||
@ -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;
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user