|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2023 Jeff Epler for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "py/enum.h" |
| 8 | +#include "py/objproperty.h" |
| 9 | +#include "py/runtime.h" |
| 10 | +#include "shared-bindings/synthio/BlockBiquad.h" |
| 11 | +#include "shared-bindings/util.h" |
| 12 | + |
| 13 | +//| class FilterMode: |
| 14 | +//| """The type of filter""" |
| 15 | +//| |
| 16 | +//| LOW_PASS: FilterMode |
| 17 | +//| """A low-pass filter""" |
| 18 | +//| HIGH_PASS: FilterMode |
| 19 | +//| """A high-pass filter""" |
| 20 | +//| BAND_PASS: FilterMode |
| 21 | +//| """A band-pass filter""" |
| 22 | +//| NOTCH: FilterMode |
| 23 | +//| """A notch filter""" |
| 24 | +//| |
| 25 | + |
| 26 | +MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, LOW_PASS, SYNTHIO_LOW_PASS); |
| 27 | +MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, HIGH_PASS, SYNTHIO_HIGH_PASS); |
| 28 | +MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, BAND_PASS, SYNTHIO_BAND_PASS); |
| 29 | +MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, NOTCH, SYNTHIO_NOTCH); |
| 30 | + |
| 31 | +MAKE_ENUM_MAP(synthio_filter_mode) { |
| 32 | + MAKE_ENUM_MAP_ENTRY(mode, LOW_PASS), |
| 33 | + MAKE_ENUM_MAP_ENTRY(mode, HIGH_PASS), |
| 34 | + MAKE_ENUM_MAP_ENTRY(mode, BAND_PASS), |
| 35 | + MAKE_ENUM_MAP_ENTRY(mode, NOTCH), |
| 36 | +}; |
| 37 | + |
| 38 | +static MP_DEFINE_CONST_DICT(synthio_filter_mode_locals_dict, synthio_filter_mode_locals_table); |
| 39 | + |
| 40 | +MAKE_PRINTER(synthio, synthio_filter_mode); |
| 41 | + |
| 42 | +MAKE_ENUM_TYPE(synthio, FilterMode, synthio_filter_mode); |
| 43 | + |
| 44 | +static synthio_filter_mode validate_synthio_filter_mode(mp_obj_t obj, qstr arg_name) { |
| 45 | + return cp_enum_value(&synthio_filter_mode_type, obj, arg_name); |
| 46 | +} |
| 47 | + |
| 48 | +//| class BlockBiquad: |
| 49 | +//| def __init__( |
| 50 | +//| self, |
| 51 | +//| mode: FilterMode, |
| 52 | +//| frequency: BlockInput, |
| 53 | +//| Q: BlockInput = 0.7071067811865475, |
| 54 | +//| ) -> None: |
| 55 | +//| """Construct a biquad filter object with dynamic center frequency & q factor |
| 56 | +//| |
| 57 | +//| Since ``frequency`` and ``Q`` are `BlockInput` objects, they can |
| 58 | +//| be varied dynamically. Internally, this is evaluated as "direct form 1" |
| 59 | +//| biquad filter. |
| 60 | +//| |
| 61 | +//| The internal filter state x[] and y[] is not updated when the filter |
| 62 | +//| coefficients change, and there is no theoretical justification for why |
| 63 | +//| this should result in a stable filter output. However, in practice, |
| 64 | +//| slowly varying the filter's characteristic frequency and sharpness |
| 65 | +//| appears to work as you'd expect.""" |
| 66 | + |
| 67 | +static const mp_arg_t block_biquad_properties[] = { |
| 68 | + { MP_QSTR_mode, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } }, |
| 69 | + { MP_QSTR_frequency, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } }, |
| 70 | + { MP_QSTR_Q, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } }, |
| 71 | +}; |
| 72 | + |
| 73 | +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) { |
| 74 | + enum { ARG_mode, ARG_frequency, ARG_Q }; |
| 75 | + |
| 76 | + mp_arg_val_t args[MP_ARRAY_SIZE(block_biquad_properties)]; |
| 77 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(block_biquad_properties), block_biquad_properties, args); |
| 78 | + |
| 79 | + if (args[ARG_Q].u_obj == MP_OBJ_NULL) { |
| 80 | + args[ARG_Q].u_obj = mp_obj_new_float(MICROPY_FLOAT_CONST(0.7071067811865475)); |
| 81 | + } |
| 82 | + |
| 83 | + synthio_filter_mode mode = validate_synthio_filter_mode(args[ARG_mode].u_obj, MP_QSTR_mode); |
| 84 | + return common_hal_synthio_block_biquad_new(mode, args[ARG_frequency].u_obj, args[ARG_Q].u_obj); |
| 85 | +} |
| 86 | + |
| 87 | +//| |
| 88 | +//| mode: FilterMode |
| 89 | +//| """The mode of filter (read-only)""" |
| 90 | +static mp_obj_t synthio_block_biquad_get_mode(mp_obj_t self_in) { |
| 91 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 92 | + return cp_enum_find(&synthio_filter_mode_type, common_hal_synthio_block_biquad_get_mode(self)); |
| 93 | +} |
| 94 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_mode_obj, synthio_block_biquad_get_mode); |
| 95 | + |
| 96 | +MP_PROPERTY_GETTER(synthio_block_biquad_mode_obj, |
| 97 | + (mp_obj_t)&synthio_block_biquad_get_mode_obj); |
| 98 | + |
| 99 | +//| |
| 100 | +//| frequency: BlockInput |
| 101 | +//| """The central frequency (in Hz) of the filter""" |
| 102 | +static mp_obj_t synthio_block_biquad_get_frequency(mp_obj_t self_in) { |
| 103 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 104 | + return common_hal_synthio_block_biquad_get_frequency(self); |
| 105 | +} |
| 106 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_frequency_obj, synthio_block_biquad_get_frequency); |
| 107 | + |
| 108 | +static mp_obj_t synthio_block_biquad_set_frequency(mp_obj_t self_in, mp_obj_t arg) { |
| 109 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 110 | + common_hal_synthio_block_biquad_set_frequency(self, arg); |
| 111 | + return mp_const_none; |
| 112 | +} |
| 113 | +MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_frequency_obj, synthio_block_biquad_set_frequency); |
| 114 | +MP_PROPERTY_GETSET(synthio_block_biquad_frequency_obj, |
| 115 | + (mp_obj_t)&synthio_block_biquad_get_frequency_obj, |
| 116 | + (mp_obj_t)&synthio_block_biquad_set_frequency_obj); |
| 117 | + |
| 118 | + |
| 119 | +//| |
| 120 | +//| Q: BlockInput |
| 121 | +//| """The sharpness (Q) of the filter""" |
| 122 | +//| |
| 123 | +static mp_obj_t synthio_block_biquad_get_Q(mp_obj_t self_in) { |
| 124 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 125 | + return common_hal_synthio_block_biquad_get_Q(self); |
| 126 | +} |
| 127 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_Q_obj, synthio_block_biquad_get_Q); |
| 128 | + |
| 129 | +static mp_obj_t synthio_block_biquad_set_Q(mp_obj_t self_in, mp_obj_t arg) { |
| 130 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 131 | + common_hal_synthio_block_biquad_set_Q(self, arg); |
| 132 | + return mp_const_none; |
| 133 | +} |
| 134 | +MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_Q_obj, synthio_block_biquad_set_Q); |
| 135 | +MP_PROPERTY_GETSET(synthio_block_biquad_Q_obj, |
| 136 | + (mp_obj_t)&synthio_block_biquad_get_Q_obj, |
| 137 | + (mp_obj_t)&synthio_block_biquad_set_Q_obj); |
| 138 | + |
| 139 | +static const mp_rom_map_elem_t synthio_block_biquad_locals_dict_table[] = { |
| 140 | + { MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&synthio_block_biquad_mode_obj) }, |
| 141 | + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&synthio_block_biquad_frequency_obj) }, |
| 142 | + { MP_ROM_QSTR(MP_QSTR_Q), MP_ROM_PTR(&synthio_block_biquad_Q_obj) }, |
| 143 | +}; |
| 144 | +static MP_DEFINE_CONST_DICT(synthio_block_biquad_locals_dict, synthio_block_biquad_locals_dict_table); |
| 145 | + |
| 146 | +static void block_biquad_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 147 | + (void)kind; |
| 148 | + properties_print_helper(print, self_in, block_biquad_properties, MP_ARRAY_SIZE(block_biquad_properties)); |
| 149 | +} |
| 150 | + |
| 151 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 152 | + synthio_block_biquad_type_obj, |
| 153 | + MP_QSTR_BlockBiquad, |
| 154 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 155 | + make_new, synthio_block_biquad_make_new, |
| 156 | + locals_dict, &synthio_block_biquad_locals_dict, |
| 157 | + print, block_biquad_print |
| 158 | + ); |
0 commit comments