8000 BlockBiquad: Only recalculate when needed. · sparkfun/circuitpython@c377123 · GitHub
[go: up one dir, main page]

Skip to content

Commit c377123

Browse files
committed
BlockBiquad: Only recalculate when needed.
1 parent c06e6ee commit c377123

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

shared-module/synthio/BlockBiquad.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,29 @@ static int32_t biquad_scale_arg_float(mp_float_t arg) {
6262
return (int32_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(ldexp)(arg, BIQUAD_SHIFT));
6363
}
6464

65+
static int float_equal_or_update(
66+
mp_float_t *cached,
67+
mp_float_t new) {
68+
// uses memcmp to avoid error about equality float comparison
69+
if (memcmp(cached, &new, sizeof(mp_float_t))) {
70+
*cached = new;
71+
return false;
72+
}
73+
return true;
74+
}
75+
6576
void common_hal_synthio_block_biquad_tick(mp_obj_t self_in, biquad_filter_state *filter_state) {
6677
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
6778

6879
mp_float_t W0 = synthio_block_slot_get(&self->f0) * synthio_global_W_scale;
6980
mp_float_t Q = synthio_block_slot_get(&self->Q);
7081

82+
// n.b., assumes that the `mode` field is read-only
83+
// n.b., use of `&` is deliberate, avoids short-circuiting behavior
84+
if (float_equal_or_update(&self->cached_W0, W0) & float_equal_or_update(&self->cached_Q, Q)) {
85+
return;
86+
}
87+
7188
sincos_result_t sc;
7289
fast_sincos(W0, &sc);
7390

shared-module/synthio/BlockBiquad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct synthio_block_biquad {
1515
mp_obj_base_t base;
1616
synthio_filter_mode mode;
1717
synthio_block_slot_t f0, Q;
18+
mp_float_t cached_W0, cached_Q;
1819
} synthio_block_biquad_t;
1920

2021
void common_hal_synthio_block_biquad_tick(mp_obj_t self_in, biquad_filter_state *filter_state);

0 commit comments

Comments
 (0)
0