|
| 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 FilterType: |
| 14 | +//| LOW_PASS: FilterType |
| 15 | +//| HIGH_PASS: FilterType |
| 16 | +//| BAND_PASS: FilterType |
| 17 | +//| |
| 18 | + |
| 19 | +MAKE_ENUM_VALUE(synthio_filter_type, kind, LOW_PASS, SYNTHIO_LOW_PASS); |
| 20 | +MAKE_ENUM_VALUE(synthio_filter_type, kind, HIGH_PASS, SYNTHIO_HIGH_PASS); |
| 21 | +MAKE_ENUM_VALUE(synthio_filter_type, kind, BAND_PASS, SYNTHIO_BAND_PASS); |
| 22 | + |
| 23 | +MAKE_ENUM_MAP(synthio_filter) { |
| 24 | + MAKE_ENUM_MAP_ENTRY(kind, LOW_PASS), |
| 25 | + MAKE_ENUM_MAP_ENTRY(kind, HIGH_PASS), |
| 26 | + MAKE_ENUM_MAP_ENTRY(kind, BAND_PASS), |
| 27 | +}; |
| 28 | + |
| 29 | +static MP_DEFINE_CONST_DICT(synthio_filter_locals_dict, synthio_filter_locals_table); |
| 30 | + |
| 31 | +MAKE_PRINTER(synthio, synthio_filter); |
| 32 | + |
| 33 | +MAKE_ENUM_TYPE(synthio, FilterKind, synthio_filter); |
| 34 | + |
| 35 | +static synthio_filter_e validate_synthio_filter(mp_obj_t obj, qstr arg_name) { |
| 36 | + return cp_enum_value(&synthio_filter_type, obj, arg_name); |
| 37 | +} |
| 38 | + |
| 39 | +//| class BlockBiquad: |
| 40 | +//| def _init__(kind: FilterKind, f0: BlockInput, Q: BlockInput = 0.7071067811865475): ... |
| 41 | + |
| 42 | +static const mp_arg_t block_biquad_properties[] = { |
| 43 | + { 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 } }, |
| 45 | + { MP_QSTR_Q, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } }, |
| 46 | +}; |
| 47 | + |
| 48 | +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 }; |
| 50 | + |
| 51 | + mp_arg_val_t args[MP_ARRAY_SIZE(block_biquad_properties)]; |
| 52 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(block_biquad_properties), block_biquad_properties, args); |
| 53 | + |
| 54 | + if (args[ARG_Q].u_obj == MP_OBJ_NULL) { |
| 55 | + args[ARG_Q].u_obj = mp_obj_new_float(MICROPY_FLOAT_CONST(0.7071067811865475)); |
| 56 | + } |
| 57 | + |
| 58 | + 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); |
| 60 | +} |
| 61 | + |
| 62 | +//| |
| 63 | +//| kind: BiquadKind |
| 64 | +//| """The kind of filter (read-only)""" |
| 65 | +static mp_obj_t synthio_block_biquad_get_kind(mp_obj_t self_in) { |
| 66 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 67 | + return cp_enum_find(&synthio_filter_type, common_hal_synthio_block_biquad_get_kind(self)); |
| 68 | +} |
| 69 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_kind_obj, synthio_block_biquad_get_kind); |
| 70 | + |
| 71 | +MP_PROPERTY_GETTER(synthio_block_biquad_kind_obj, |
| 72 | + (mp_obj_t)&synthio_block_biquad_get_kind_obj); |
| 73 | + |
| 74 | +//| |
| 75 | +//| f0: BlockInput |
| 76 | +//| """The central frequency (in Hz) of the filter""" |
| 77 | +static mp_obj_t synthio_block_biquad_get_f0(mp_obj_t self_in) { |
| 78 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 79 | + return common_hal_synthio_block_biquad_get_f0(self); |
| 80 | +} |
| 81 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_f0_obj, synthio_block_biquad_get_f0); |
| 82 | + |
| 83 | +static mp_obj_t synthio_block_biquad_set_f0(mp_obj_t self_in, mp_obj_t arg) { |
| 84 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 85 | + common_hal_synthio_block_biquad_set_f0(self, arg); |
| 86 | + return mp_const_none; |
| 87 | +} |
| 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); |
| 92 | + |
| 93 | + |
| 94 | +//| |
| 95 | +//| Q: BlockInput |
| 96 | +//| """The sharpness (Q) of the filter""" |
| 97 | +//| |
| 98 | +static mp_obj_t synthio_block_biquad_get_Q(mp_obj_t self_in) { |
| 99 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 100 | + return common_hal_synthio_block_biquad_get_Q(self); |
| 101 | +} |
| 102 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_Q_obj, synthio_block_biquad_get_Q); |
| 103 | + |
| 104 | +static mp_obj_t synthio_block_biquad_set_Q(mp_obj_t self_in, mp_obj_t arg) { |
| 105 | + synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in); |
| 106 | + common_hal_synthio_block_biquad_set_Q(self, arg); |
| 107 | + return mp_const_none; |
| 108 | +} |
| 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); |
| 113 | + |
| 114 | +static const mp_rom_map_elem_t synthio_block_biquad_locals_dict_table[] = { |
| 115 | + { 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) }, |
| 118 | +}; |
| 119 | +static MP_DEFINE_CONST_DICT(synthio_block_biquad_locals_dict, synthio_block_biquad_locals_dict_table); |
| 120 | + |
| 121 | +static void block_biquad_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 122 | + (void)kind; |
| 123 | + properties_print_helper(print, self_in, block_biquad_properties, MP_ARRAY_SIZE(block_biquad_properties)); |
| 124 | +} |
| 125 | + |
| 126 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 127 | + synthio_block_biquad_type_obj, |
| 128 | + MP_QSTR_BlockBiquad, |
| 129 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 130 | + make_new, synthio_block_biquad_make_new, |
| 131 | + locals_dict, &synthio_block_biquad_locals_dict, |
| 132 | + print, block_biquad_print |
| 133 | + ); |
0 commit comments