synthio: avoid exceptions inside get_buffer

.. in case the items in lfos are not actually LFOs
This commit is contained in:
Jeff Epler 2023-05-21 13:22:29 -05:00
parent 78e75f6977
commit ac02a2668e
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
3 changed files with 17 additions and 6 deletions

View File

@ -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 iterable = mp_getiter(self->lfos, &iter_buf);
mp_obj_t item; mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
synthio_block_slot_t slot; if (!synthio_obj_is_block(item)) {
synthio_block_assign_slot(item, &slot, MP_QSTR_arg); continue;
}
synthio_block_slot_t slot = { item };
(void)synthio_block_slot_get(&slot); (void)synthio_block_slot_get(&slot);
} }
return GET_BUFFER_MORE_DATA; return GET_BUFFER_MORE_DATA;

View File

@ -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)); 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) { bool synthio_block_assign_slot_maybe(mp_obj_t obj, synthio_block_slot_t *slot) {
if (mp_proto_get(MP_QSTR_synthio_block, obj)) { if (synthio_obj_is_block(obj)) {
slot->obj = obj; slot->obj = obj;
return; return true;
} }
mp_float_t value = MICROPY_FLOAT_CONST(0.); mp_float_t value = MICROPY_FLOAT_CONST(0.);
if (obj != mp_const_none && !mp_obj_get_float_maybe(obj, &value)) { 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); 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) { bool synthio_obj_is_block(mp_obj_t obj) {
return mp_proto_get(MP_QSTR_synthio_block, obj); return mp_proto_get(MP_QSTR_synthio_block, obj);
} }

View File

@ -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 // 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); 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); bool synthio_obj_is_block(mp_obj_t obj);