8000 BlockBiquad: improve docs & make parameter names match old filter met… · adafruit/circuitpython@161e109 · GitHub
[go: up one dir, main page]

Skip to content

Commit 161e109

Browse files
committed
BlockBiquad: improve docs & make parameter names match old filter methods
1 parent 5b45b9a commit 161e109

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

shared-bindings/synthio/BlockBiquad.c

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
#include "shared-bindings/util.h"
1212

1313
//| class FilterType:
14+
//| """The type of filter"""
15+
//|
1416
//| LOW_PASS: FilterType
17+
//| """A low-pass filter"""
1518
//| HIGH_PASS: FilterType
19+
//| """A high-pass filter"""
1620
//| BAND_PASS: FilterType
21+
//| """A band-pass filter"""
1722
//|
1823

1924
MAKE_ENUM_VALUE(synthio_filter_type, kind, LOW_PASS, SYNTHIO_LOW_PASS);
@@ -37,16 +42,28 @@ static synthio_filter_e validate_synthio_filter(mp_obj_t obj, qstr arg_name) {
3742
}
3843

3944
//| class BlockBiquad:
40-
//| def _init__(kind: FilterKind, f0: BlockInput, Q: BlockInput = 0.7071067811865475): ...
45+
//| def __init__(
46+
//| kind: FilterKind, frequency: BlockInput, q_factor: BlockInput = 0.7071067811865475
47+
//| ):
48+
//| """Construct a biquad filter object with dynamic center frequency & q factor
49+
//|
50+
//| Since ``frequency`` and ``q_factor`` are `BlockInput`s, they can be varied dynamically.
51+
//| Internally, this is evaluated as "direct form 1" biquad filter.
52+
//|
53+
//| The internal filter state x[] and y[] is not updated when the filter
54+
//| coefficients change, and there is no theoretical justification for why
55+
//| this should result in a stable filter output. However, in practice,
56+
//| slowly varying the filter's characteristic frequency and sharpness
57+
//| appears to work as you'd expect."""
4158

4259
static const mp_arg_t block_biquad_properties[] = {
4360
{ MP_QSTR_kind, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } },
44-
{ MP_QSTR_f0, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } },
61+
{ MP_QSTR_frequency, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } },
4562
{ MP_QSTR_Q, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } },
4663
};
4764

4865
static mp_obj_t synthio_block_biquad_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
49-
enum { ARG_kind, ARG_f0, ARG_Q };
66+
enum { ARG_kind, ARG_frequency, ARG_Q };
5067

5168
mp_arg_val_t args[MP_ARRAY_SIZE(block_biquad_properties)];
5269
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(block_biquad_properties), block_biquad_properties, args);
@@ -56,7 +73,7 @@ static mp_obj_t synthio_block_biquad_make_new(const mp_obj_type_t *type_in, size
5673
}
5774

5875
synthio_filter_e kind = validate_synthio_filter(args[ARG_kind].u_obj, MP_QSTR_kind);
59-
return common_hal_synthio_block_biquad_new(kind, args[ARG_f0].u_obj, args[ARG_Q].u_obj);
76+
return common_hal_synthio_block_biquad_new(kind, args[ARG_frequency].u_obj, args[ARG_Q].u_obj);
6077
}
6178

6279
//|
@@ -72,49 +89,49 @@ MP_PROPERTY_GETTER(synthio_block_biquad_kind_obj,
7289
(mp_obj_t)&synthio_block_biquad_get_kind_obj);
7390

7491
//|
75-
//| f0: BlockInput
92+
//| frequency: BlockInput
7693
//| """The central frequency (in Hz) of the filter"""
77-
static mp_obj_t synthio_block_biquad_get_f0(mp_obj_t self_in) {
94+
static mp_obj_t synthio_block_biquad_get_frequency(mp_obj_t self_in) {
7895
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
79-
return common_hal_synthio_block_biquad_get_f0(self);
96+
return common_hal_synthio_block_biquad_get_frequency(self);
8097
}
81-
MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_f0_obj, synthio_block_biquad_get_f0);
98+
MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_frequency_obj, synthio_block_biquad_get_frequency);
8299

83-
static mp_obj_t synthio_block_biquad_set_f0(mp_obj_t self_in, mp_obj_t arg) {
100+
static mp_obj_t synthio_block_biquad_set_frequency(mp_obj_t self_in, mp_obj_t arg) {
84101
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
85-
common_hal_synthio_block_biquad_set_f0(self, arg);
102+
common_hal_synthio_block_biquad_set_frequency(self, arg);
86103
return mp_const_none;
87104
}
88-
MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_f0_obj, synthio_block_biquad_set_f0);
89-
MP_PROPERTY_GETSET(synthio_block_biquad_f0_obj,
90-
(mp_obj_t)&synthio_block_biquad_get_f0_obj,
91-
(mp_obj_t)&synthio_block_biquad_set_f0_obj);
105+
MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_frequency_obj, synthio_block_biquad_set_frequency);
106+
MP_PROPERTY_GETSET(synthio_block_biquad_frequency_obj,
107+
(mp_obj_t)&synthio_block_biquad_get_frequency_obj,
108+
(mp_obj_t)&synthio_block_biquad_set_frequency_obj);
92109

93110

94111
//|
95-
//| Q: BlockInput
96-
//| """The sharpness (Q) of the filter"""
112+
//| q_factor: BlockInput
113+
//| """The sharpness (q_factor) of the filter"""
97114
//|
98-
static mp_obj_t synthio_block_biquad_get_Q(mp_obj_t self_in) {
115+
static mp_obj_t synthio_block_biquad_get_q_factor(mp_obj_t self_in) {
99116
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
100-
return common_hal_synthio_block_biquad_get_Q(self);
117+
return common_hal_synthio_block_biquad_get_q_factor(self);
101118
}
102-
MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_Q_obj, synthio_block_biquad_get_Q);
119+
MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_q_factor_obj, synthio_block_biquad_get_q_factor);
103120

104-
static mp_obj_t synthio_block_biquad_set_Q(mp_obj_t self_in, mp_obj_t arg) {
121+
static mp_obj_t synthio_block_biquad_set_q_factor(mp_obj_t self_in, mp_obj_t arg) {
105122
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
106-
common_hal_synthio_block_biquad_set_Q(self, arg);
123+
common_hal_synthio_block_biquad_set_q_factor(self, arg);
107124
return mp_const_none;
108125
}
109-
MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_Q_obj, synthio_block_biquad_set_Q);
110-
MP_PROPERTY_GETSET(synthio_block_biquad_Q_obj,
111-
(mp_obj_t)&synthio_block_biquad_get_Q_obj,
112-
(mp_obj_t)&synthio_block_biquad_set_Q_obj);
126+
MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_q_factor_obj, synthio_block_biquad_set_q_factor);
127+
MP_PROPERTY_GETSET(synthio_block_biquad_q_factor_obj,
128+
(mp_obj_t)&synthio_block_biquad_get_q_factor_obj,
129+
(mp_obj_t)&synthio_block_biquad_set_q_factor_obj);
113130

114131
static const mp_rom_map_elem_t synthio_block_biquad_locals_dict_table[] = {
115132
{ MP_ROM_QSTR(MP_QSTR_kind), MP_ROM_PTR(&synthio_block_biquad_kind_obj) },
116-
{ MP_ROM_QSTR(MP_QSTR_f0), MP_ROM_PTR(&synthio_block_biquad_f0_obj) },
117-
{ MP_ROM_QSTR(MP_QSTR_Q), MP_ROM_PTR(&synthio_block_biquad_Q_obj) },
133+
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&synthio_block_biquad_frequency_obj) },
134+
{ MP_ROM_QSTR(MP_QSTR_q_factor), MP_ROM_PTR(&synthio_block_biquad_q_factor_obj) },
118135
};
119136
static MP_DEFINE_CONST_DICT(synthio_block_biquad_locals_dict, synthio_block_biquad_locals_dict_table);
120137

shared-bindings/synthio/BlockBiquad.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ typedef enum {
1717
} synthio_filter_e;
1818

1919

20-
mp_obj_t common_hal_synthio_block_biquad_get_Q(synthio_block_biquad_t *self);
21-
void common_hal_synthio_block_biquad_set_Q(synthio_block_biquad_t *self, mp_obj_t Q);
20+
mp_obj_t common_hal_synthio_block_biquad_get_q_factor(synthio_block_biquad_t *self);
21+
void common_hal_synthio_block_biquad_set_q_factor(synthio_block_biquad_t *self, mp_obj_t Q);
2222

23-
mp_obj_t common_hal_synthio_block_biquad_get_f0(synthio_block_biquad_t *self);
24-
void common_hal_synthio_block_biquad_set_f0(synthio_block_biquad_t *self, mp_obj_t f0);
23+
mp_obj_t common_hal_synthio_block_biquad_get_frequency(synthio_block_biquad_t *self);
24+
void common_hal_synthio_block_biquad_set_frequency(synthio_block_biquad_t *self, mp_obj_t frequency);
2525

2626
synthio_filter_e common_hal_synthio_block_biquad_get_kind(synthio_block_biquad_t *self);
2727

28-
mp_obj_t common_hal_synthio_block_biquad_new(synthio_filter_e kind, mp_obj_t f0, mp_obj_t Q);
28+
mp_obj_t common_hal_synthio_block_biquad_new(synthio_filter_e kind, mp_obj_t frequency, mp_obj_t Q);

shared-module/synthio/BlockBiquad.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ static void fast_sincos(mp_float_t theta, sincos_result_t *result) {
3333
mp_obj_t common_hal_synthio_block_biquad_new(synthio_filter_e kind, mp_obj_t f0, mp_obj_t Q) {
3434
synthio_block_biquad_t *self = mp_obj_malloc(synthio_block_biquad_t, &synthio_block_biquad_type_obj);
3535
self->kind = kind;
36-
synthio_block_assign_slot(f0, &self->f0, MP_QSTR_f0);
37-
synthio_block_assign_slot(Q, &self->Q, MP_QSTR_Q);
36+
synthio_block_assign_slot(f0, &self->f0, MP_QSTR_frequency);
37+
synthio_block_assign_slot(Q, &self->Q, MP_QSTR_q_factor);
3838
return MP_OBJ_FROM_PTR(self);
3939
}
4040

4141
synthio_filter_e common_hal_synthio_block_biquad_get_kind(synthio_block_biquad_t *self) {
4242
return self->kind;
4343
}
4444

45-
mp_obj_t common_hal_synthio_block_biquad_get_Q(synthio_block_biquad_t *self) {
45+
mp_obj_t common_hal_synthio_block_biquad_get_q_factor(synthio_block_biquad_t *self) {
4646
return self->Q.obj;
4747
}
4848

49-
void common_hal_synthio_block_biquad_set_Q(synthio_block_biquad_t *self, mp_obj_t Q) {
50-
synthio_block_assign_slot(Q, &self->Q, MP_QSTR_Q);
49+
void common_hal_synthio_block_biquad_set_q_factor(synthio_block_biquad_t *self, mp_obj_t q_factor) {
50+
synthio_block_assign_slot(q_factor, &self->Q, MP_QSTR_q_factor);
5151
}
5252

53-
mp_obj_t common_hal_synthio_block_biquad_get_f0(synthio_block_biquad_t *self) {
53+
mp_obj_t common_hal_synthio_block_biquad_get_frequency(synthio_block_biquad_t *self) {
5454
return self->f0.obj;
5555
}
5656

57-
void common_hal_synthio_block_biquad_set_f0(synthio_block_biquad_t *self, mp_obj_t f0) {
58-
synthio_block_assign_slot(f0, &self->f0, MP_QSTR_f0);
57+
void common_hal_synthio_block_biquad_set_frequency(synthio_block_biquad_t *self, mp_obj_t frequency) {
58+
synthio_block_assign_slot(frequency, &self->f0, MP_QSTR_frequency);
5959
}
6060

6161
static int32_t biquad_scale_arg_float(mp_float_t arg) {

0 commit comments

Comments
 (0)
0