Skip to content

Commit c435c46

Browse files
committed
synthio: simplify block logic a bit by converting from float just once
1 parent 4901bdc commit c435c46

File tree

4 files changed

+112
-107
lines changed

4 files changed

+112
-107
lines changed

shared-module/synthio/__init__.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,9 @@ mp_float_t synthio_block_slot_get(synthio_block_slot_t *slot) {
553553
return MICROPY_FLOAT_CONST(0.);
554554
}
555555

556-
mp_float_t value;
557-
if (mp_obj_get_float_maybe(slot->obj, &value)) {
558-
return value;
556+
// all numbers previously converted to float in synthio_block_assign_slot
557+
if (mp_obj_is_float(slot->obj)) {
558+
return mp_obj_get_float(slot->obj);
559559
}
560560

561561
synthio_block_base_t *block = MP_OBJ_TO_PTR(slot->obj);
@@ -564,9 +564,10 @@ mp_float_t synthio_block_slot_get(synthio_block_slot_t *slot) {
564564
}
565565

566566
block->last_tick = synthio_global_tick;
567-
// previously verified by call to mp_proto_get
567+
// previously verified by call to mp_proto_get in synthio_block_assign_slot
568568
const synthio_block_proto_t *p = mp_type_get_protocol_slot(mp_obj_get_type(slot->obj));
569-
block->value = value = p->tick(slot->obj);
569+
mp_float_t value = p->tick(slot->obj);
570+
block->value = value;
570571
return value;
571572
}
572573

@@ -597,5 +598,8 @@ void synthio_block_assign_slot(mp_obj_t obj, synthio_block_slot_t *slot, qstr ar
597598
mp_raise_TypeError_varg(translate("%q must be of type %q, not %q"), arg_name, MP_QSTR_BlockInput, mp_obj_get_type_qstr(obj));
598599
}
599600

600-
slot->obj = obj;
601+
slot->obj = mp_obj_new_float(value);
602+
}
603+
bool synthio_obj_is_block(mp_obj_t obj) {
604+
return mp_proto_get(MP_QSTR_synthio_block, obj);
601605
}

shared-module/synthio/block.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ int32_t synthio_block_slot_get_scaled(synthio_block_slot_t *block_slot, mp_float
5656

5757
// Assign an object (which may be a float or a synthio_block_obj_t) to an block slot
5858
void synthio_block_assign_slot(mp_obj_t obj, synthio_block_slot_t *block_slot, qstr arg_name);
59+
bool synthio_obj_is_block(mp_obj_t obj);

tests/circuitpython/synthesizer_note.py.exp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
()
22
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
3-
(Note(frequency=830.6076004423605, panning=0, amplitude=1, bend=0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0, ring_waveform=None),)
3+
(Note(frequency=830.6076004423605, panning=0.0, amplitude=1.0, bend=0.0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0.0, ring_waveform=None),)
44
[-16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383]
5-
(Note(frequency=830.6076004423605, panning=0, amplitude=1, bend=0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0, ring_waveform=None), Note(frequency=830.6076004423605, panning=0, amplitude=1, bend=0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0, ring_waveform=None))
5+
(Note(frequency=830.6076004423605, panning=0.0, amplitude=1.0, bend=0.0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0.0, ring_waveform=None), Note(frequency=830.6076004423605, panning=0.0, amplitude=1.0, bend=0.0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0.0, ring_waveform=None))
66
[-1, -1, -1, -1, -1, -1, -1, -1, 28045, -1, -1, -1, -1, -28046, -1, -1, -1, -1, 28045, -1, -1, -1, -1, -28046]
7-
(Note(frequency=830.6076004423605, panning=0, amplitude=1, bend=0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0, ring_waveform=None),)
7+
(Note(frequency=830.6076004423605, panning=0.0, amplitude=1.0, bend=0.0, waveform=None, envelope=None, filter=True, ring_frequency=0.0, ring_bend=0.0, ring_waveform=None),)
88
[-1, -1, -1, 28045, -1, -1, -1, -1, -1, -1, -1, -1, 28045, -1, -1, -1, -1, -28046, -1, -1, -1, -1, 28045, -1]
99
(-5242, 5241)
1010
(-10484, 10484)

0 commit comments

Comments
 (0)