forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMath.c
286 lines (251 loc) · 11.1 KB
/
Math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Artyom Skrobov
// SPDX-FileCopyrightText: Copyright (c) 2023 Jeff Epler for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "py/obj.h"
#include "py/objproperty.h"
#include "py/proto.h"
#include "py/runtime.h"
#include "shared-bindings/util.h"
#include "shared-bindings/synthio/Math.h"
#include "shared-module/synthio/Math.h"
static const mp_arg_t math_properties[4];
static mp_obj_t synthio_math_make_new_common(mp_arg_val_t args[MP_ARRAY_SIZE(math_properties)]);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, SUM, OP_SUM);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, ADD_SUB, OP_ADD_SUB);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, PRODUCT, OP_PRODUCT);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, MUL_DIV, OP_MUL_DIV);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, SCALE_OFFSET, OP_SCALE_OFFSET);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, OFFSET_SCALE, OP_OFFSET_SCALE);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, LERP, OP_LERP);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, CONSTRAINED_LERP, OP_CONSTRAINED_LERP);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, DIV_ADD, OP_DIV_ADD);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, ADD_DIV, OP_ADD_DIV);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, MID, OP_MID);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, MAX, OP_MAX);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, MIN, OP_MIN);
MAKE_ENUM_VALUE(synthio_math_operation_type, math_op, ABS, OP_ABS);
//| class MathOperation:
//| """Operation for a Math block"""
//|
//| def __call__(self, a: BlockInput, b: BlockInput = 0.0, c: BlockInput = 1.0) -> Math:
//| """A MathOperation enumeration value can be called to construct a Math block that performs that operation"""
//|
//| SUM: "MathOperation"
//| """Computes ``a+b+c``. For 2-input sum, set one argument to ``0.0``. To hold a control value for multiple subscribers, set two arguments to ``0.0``."""
//|
//| ADD_SUB: "MathOperation"
//| """Computes ``a+b-c``. For 2-input subtraction, set ``b`` to ``0.0``."""
//|
//| PRODUCT: "MathOperation"
//| """Computes ``a*b*c``. For 2-input product, set one argument to ``1.0``."""
//|
//| MUL_DIV: "MathOperation"
//| """Computes ``a*b/c``. If ``c`` is zero, the output is ``1.0``."""
//|
//| SCALE_OFFSET: "MathOperation"
//| """Computes ``(a*b)+c``."""
//|
//| OFFSET_SCALE: "MathOperation"
//| """Computes ``(a+b)*c``. For 2-input multiplication, set ``b`` to 0."""
//|
//| LERP: "MathOperation"
//| """Computes ``a * (1-c) + b * c``."""
//|
//| CONSTRAINED_LERP: "MathOperation"
//| """Computes ``a * (1-c') + b * c'``, where ``c'`` is constrained to be between ``0.0`` and ``1.0``."""
//|
//| DIV_ADD: "MathOperation"
//| """Computes ``a/b+c``. If ``b`` is zero, the output is ``c``."""
//|
//| ADD_DIV: "MathOperation"
//| """Computes ``(a+b)/c``. For 2-input product, set ``b`` to ``0.0``."""
//|
//| MID: "MathOperation"
//| """Returns the middle of the 3 input values."""
//|
//| MAX: "MathOperation"
//| """Returns the biggest of the 3 input values."""
//|
//| MIN: "MathOperation"
//| """Returns the smallest of the 3 input values."""
//|
//| ABS: "MathOperation"
//| """Returns the absolute value of ``a``."""
//|
//|
MAKE_ENUM_MAP(synthio_math_operation) {
MAKE_ENUM_MAP_ENTRY(math_op, SUM),
MAKE_ENUM_MAP_ENTRY(math_op, ADD_SUB),
MAKE_ENUM_MAP_ENTRY(math_op, PRODUCT),
MAKE_ENUM_MAP_ENTRY(math_op, MUL_DIV),
MAKE_ENUM_MAP_ENTRY(math_op, SCALE_OFFSET),
MAKE_ENUM_MAP_ENTRY(math_op, OFFSET_SCALE),
MAKE_ENUM_MAP_ENTRY(math_op, LERP),
MAKE_ENUM_MAP_ENTRY(math_op, CONSTRAINED_LERP),
MAKE_ENUM_MAP_ENTRY(math_op, DIV_ADD),
MAKE_ENUM_MAP_ENTRY(math_op, ADD_DIV),
MAKE_ENUM_MAP_ENTRY(math_op, MID),
MAKE_ENUM_MAP_ENTRY(math_op, MAX),
MAKE_ENUM_MAP_ENTRY(math_op, MIN),
MAKE_ENUM_MAP_ENTRY(math_op, ABS),
};
static mp_obj_t mathop_call(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
mp_arg_val_t args[4];
args[0].u_obj = fun;
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(math_properties) - 1, math_properties + 1, &args[1]);
return synthio_math_make_new_common(args);
}
static MP_DEFINE_CONST_DICT(synthio_math_operation_locals_dict, synthio_math_operation_locals_table);
MAKE_PRINTER(synthio, synthio_math_operation);
MAKE_ENUM_TYPE(synthio, MathOperation, synthio_math_operation,
call, mathop_call
);
//| class Math:
//| """An arithmetic block
//|
//| Performs an arithmetic operation on up to 3 inputs. See the
//| documentation of ``MathOperation`` for the specific functions available.
//|
//| The properties can all be changed at run-time.
//|
//| An Math only updates if it is actually associated with a playing `Synthesizer`,
//| including indirectly via a `Note` or another intermediate Math.
//|
//| Using the same Math as an input to multiple other Maths or Notes is OK, but
//| the result if an Math is tied to multiple `Synthesizer` objects is undefined.
//|
//| In the current implementation, Maths are updated every 256 samples. This
//| should be considered an implementation detail.
//| """
//|
//| def __init__(
//| self,
//| operation: MathOperation,
//| a: BlockInput,
//| b: BlockInput = 0.0,
//| c: BlockInput = 1.0,
//| ) -> None:
//| pass
//|
static const mp_arg_t math_properties[] = {
{ MP_QSTR_operation, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = NULL } },
{ MP_QSTR_a, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = NULL } },
{ MP_QSTR_b, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
{ MP_QSTR_c, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(1) } },
};
static mp_obj_t synthio_math_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
mp_arg_val_t args[MP_ARRAY_SIZE(math_properties)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(math_properties), math_properties, args);
return synthio_math_make_new_common(args);
}
static mp_obj_t synthio_math_make_new_common(mp_arg_val_t args[MP_ARRAY_SIZE(math_properties)]) {
synthio_math_obj_t *self = mp_obj_malloc(synthio_math_obj_t, &synthio_math_type);
self->base.last_tick = synthio_global_tick;
mp_obj_t result = MP_OBJ_FROM_PTR(self);
properties_construct_helper(result, math_properties, args, MP_ARRAY_SIZE(math_properties));
return result;
};
//| a: BlockInput
//| """The first input to the operation"""
static mp_obj_t synthio_math_get_a(mp_obj_t self_in) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_synthio_math_get_input_obj(self, 0);
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_math_get_a_obj, synthio_math_get_a);
static mp_obj_t synthio_math_set_a(mp_obj_t self_in, mp_obj_t arg) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_math_set_input_obj(self, 0, arg, MP_QSTR_a);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_math_set_a_obj, synthio_math_set_a);
MP_PROPERTY_GETSET(synthio_math_a_obj,
(mp_obj_t)&synthio_math_get_a_obj,
(mp_obj_t)&synthio_math_set_a_obj);
//| b: BlockInput
//| """The second input to the operation"""
static mp_obj_t synthio_math_get_b(mp_obj_t self_in) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_synthio_math_get_input_obj(self, 1);
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_math_get_b_obj, synthio_math_get_b);
static mp_obj_t synthio_math_set_b(mp_obj_t self_in, mp_obj_t arg) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_math_set_input_obj(self, 1, arg, MP_QSTR_b);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_math_set_b_obj, synthio_math_set_b);
MP_PROPERTY_GETSET(synthio_math_b_obj,
(mp_obj_t)&synthio_math_get_b_obj,
(mp_obj_t)&synthio_math_set_b_obj);
//| c: BlockInput
//| """The third input to the operation"""
static mp_obj_t synthio_math_get_c(mp_obj_t self_in) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_synthio_math_get_input_obj(self, 2);
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_math_get_c_obj, synthio_math_get_c);
static mp_obj_t synthio_math_set_c(mp_obj_t self_in, mp_obj_t arg) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_math_set_input_obj(self, 2, arg, MP_QSTR_c);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_math_set_c_obj, synthio_math_set_c);
MP_PROPERTY_GETSET(synthio_math_c_obj,
(mp_obj_t)&synthio_math_get_c_obj,
(mp_obj_t)&synthio_math_set_c_obj);
//| operation: MathOperation
//| """The function to compute"""
static mp_obj_t synthio_math_get_operation(mp_obj_t self_in) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
return cp_enum_find(&synthio_math_operation_type, common_hal_synthio_math_get_operation(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_math_get_operation_obj, synthio_math_get_operation);
static mp_obj_t synthio_math_set_operation(mp_obj_t self_in, mp_obj_t arg) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_math_set_operation(self, cp_enum_value(&synthio_math_operation_type, arg, MP_QSTR_operation));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_math_set_operation_obj, synthio_math_set_operation);
MP_PROPERTY_GETSET(synthio_math_operation_obj,
(mp_obj_t)&synthio_math_get_operation_obj,
(mp_obj_t)&synthio_math_set_operation_obj);
//|
//| value: float
//| """The value of the oscillator (read-only)"""
//|
//|
static mp_obj_t synthio_math_get_value(mp_obj_t self_in) {
synthio_math_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_float(common_hal_synthio_math_get_value(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_math_get_value_obj, synthio_math_get_value);
MP_PROPERTY_GETTER(synthio_math_value_obj,
(mp_obj_t)&synthio_math_get_value_obj);
static void math_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
properties_print_helper(print, self_in, math_properties, MP_ARRAY_SIZE(math_properties));
}
static const mp_rom_map_elem_t synthio_math_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_a), MP_ROM_PTR(&synthio_math_a_obj) },
{ MP_ROM_QSTR(MP_QSTR_b), MP_ROM_PTR(&synthio_math_b_obj) },
{ MP_ROM_QSTR(MP_QSTR_c), MP_ROM_PTR(&synthio_math_c_obj) },
{ MP_ROM_QSTR(MP_QSTR_operation), MP_ROM_PTR(&synthio_math_operation_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&synthio_math_value_obj) },
};
static MP_DEFINE_CONST_DICT(synthio_math_locals_dict, synthio_math_locals_dict_table);
static const synthio_block_proto_t math_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_synthio_block)
.tick = common_hal_synthio_math_tick,
};
MP_DEFINE_CONST_OBJ_TYPE(
synthio_math_type,
MP_QSTR_Math,
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
make_new, synthio_math_make_new,
locals_dict, &synthio_math_locals_dict,
print, math_print,
protocol, &math_proto
);