From ac02a2668e1f2844c2deaec542fe10c55ad1d22c Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 21 May 2023 13:22:29 -0500 Subject: [PATCH] synthio: avoid exceptions inside get_buffer .. in case the items in lfos are not actually LFOs --- shared-module/synthio/Synthesizer.c | 6 ++++-- shared-module/synthio/__init__.c | 16 ++++++++++++---- shared-module/synthio/block.h | 1 + 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/shared-module/synthio/Synthesizer.c b/shared-module/synthio/Synthesizer.c index f773f4d640..5371a67350 100644 --- a/shared-module/synthio/Synthesizer.c +++ b/shared-module/synthio/Synthesizer.c @@ -78,8 +78,10 @@ audioio_get_buffer_result_t synthio_synthesizer_get_buffer(synthio_synthesizer_o mp_obj_t iterable = mp_getiter(self->lfos, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - synthio_block_slot_t slot; - synthio_block_assign_slot(item, &slot, MP_QSTR_arg); + if (!synthio_obj_is_block(item)) { + continue; + } + synthio_block_slot_t slot = { item }; (void)synthio_block_slot_get(&slot); } return GET_BUFFER_MORE_DATA; diff --git a/shared-module/synthio/__init__.c b/shared-module/synthio/__init__.c index bc81beadb3..85e98329fd 100644 --- a/shared-module/synthio/__init__.c +++ b/shared-module/synthio/__init__.c @@ -584,19 +584,27 @@ int32_t synthio_block_slot_get_scaled(synthio_block_slot_t *lfo_slot, mp_float_t return (int32_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(ldexp)(value, 15)); } -void synthio_block_assign_slot(mp_obj_t obj, synthio_block_slot_t *slot, qstr arg_name) { - if (mp_proto_get(MP_QSTR_synthio_block, obj)) { +bool synthio_block_assign_slot_maybe(mp_obj_t obj, synthio_block_slot_t *slot) { + if (synthio_obj_is_block(obj)) { slot->obj = obj; - return; + return true; } mp_float_t value = MICROPY_FLOAT_CONST(0.); if (obj != mp_const_none && !mp_obj_get_float_maybe(obj, &value)) { - mp_raise_TypeError_varg(translate("%q must be of type %q, not %q"), arg_name, MP_QSTR_BlockInput, mp_obj_get_type_qstr(obj)); + return false; } slot->obj = mp_obj_new_float(value); + return true; } + +void synthio_block_assign_slot(mp_obj_t obj, synthio_block_slot_t *slot, qstr arg_name) { + if (!synthio_block_assign_slot_maybe(obj, slot)) { + mp_raise_TypeError_varg(translate("%q must be of type %q, not %q"), arg_name, MP_QSTR_BlockInput, mp_obj_get_type_qstr(obj)); + } +} + bool synthio_obj_is_block(mp_obj_t obj) { return mp_proto_get(MP_QSTR_synthio_block, obj); } diff --git a/shared-module/synthio/block.h b/shared-module/synthio/block.h index 9f7da06ab2..d89896ca61 100644 --- a/shared-module/synthio/block.h +++ b/shared-module/synthio/block.h @@ -56,4 +56,5 @@ int32_t synthio_block_slot_get_scaled(synthio_block_slot_t *block_slot, mp_float // Assign an object (which may be a float or a synthio_block_obj_t) to an block slot void synthio_block_assign_slot(mp_obj_t obj, synthio_block_slot_t *block_slot, qstr arg_name); +bool synthio_block_assign_slot_maybe(mp_obj_t obj, synthio_block_slot_t *block_slot); bool synthio_obj_is_block(mp_obj_t obj);