Debian Codegraph @

modules/lsp-runtime-lib/src/main/expr/functions.cpp

raw
1 /*
2 * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2024 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 30 мар. 2024 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/expr/functions.h>
23 #include <lsp-plug.in/stdlib/math.h>
24
25 namespace lsp
26 {
27 namespace expr
28 {
29 status_t numeric_compare(value_t *result, const value_t *a, const value_t *b)
30 {
31 // Check for undef
32 if ((a->type == VT_UNDEF) || (b->type == VT_UNDEF))
33 {
34 set_value_undef(result);
35 return STATUS_OK;
36 }
37
38 // Check for null
39 if (a->type == VT_NULL)
40 {
41 set_value_bool(result, b->type == VT_NULL);
42 return STATUS_OK;
43 }
44 else if (b->type == VT_NULL)
45 {
46 set_value_bool(result, false);
47 return STATUS_OK;
48 }
49
50 // Need to perform forced cast to numeric value?
51 value_t tmp_a, tmp_b;
52 status_t res;
53 init_value(&tmp_a);
54 init_value(&tmp_b);
55 lsp_finally {
56 destroy_value(&tmp_a);
57 destroy_value(&tmp_b);
58 };
59
60 if (a->type == VT_STRING)
61 {
62 if ((res = cast_non_string(&tmp_a, a)) != STATUS_OK)
63 return res;
64 a = &tmp_a;
65 }
66 if (b->type == VT_STRING)
67 {
68 if ((res = cast_non_string(&tmp_b, b)) != STATUS_OK)
69 return res;
70 b = &tmp_b;
71 }
72
73 // Compare arguments
74 ssize_t cmp_result = 0;
75 switch (a->type)
76 {
77 case VT_INT:
78 // Analyze type of b
79 switch (b->type)
80 {
81 case VT_INT:
82 cmp_result =
83 (a->v_int < b->v_int) ? -1 :
84 ((a->v_int > b->v_int) ? 1 : 0);
85 break;
86 case VT_FLOAT:
87 cmp_result =
88 (a->v_int < b->v_float) ? -1 :
89 ((a->v_int > b->v_float) ? 1 : 0);
90 break;
91 case VT_BOOL:
92 {
93 ssize_t value = (b->v_bool) ? 1 : 0;
94 cmp_result =
95 (a->v_int < value) ? -1 :
96 ((a->v_int > value) ? 1 : 0);
97 break;
98 }
99 default:
100 set_value_undef(result);
101 return STATUS_OK;
102 }
103 break;
104
105 case VT_FLOAT:
106 // Analyze type of b
107 switch (b->type)
108 {
109 case VT_INT:
110 cmp_result =
111 (a->v_float < b->v_int) ? -1 :
112 ((a->v_float > b->v_int) ? 1 : 0);
113 break;
114 case VT_FLOAT:
115 cmp_result =
116 (a->v_float < b->v_float) ? -1 :
117 ((a->v_float > b->v_float) ? 1 : 0);
118 break;
119 case VT_BOOL:
120 {
121 double value = (b->v_bool) ? 1.0 : 0.0;
122 cmp_result =
123 (a->v_float < value) ? -1 :
124 ((a->v_float > value) ? 1 : 0);
125 break;
126 }
127 default:
128 set_value_undef(result);
129 return STATUS_OK;
130 }
131 break;
132
133 case VT_BOOL:
134 // Analyze type of b
135 switch (b->type)
136 {
137 case VT_INT:
138 {
139 ssize_t value = (a->v_bool) ? 1 : 0;
140 cmp_result =
141 (value < b->v_int) ? -1 :
142 ((value > b->v_int) ? 1 : 0);
143 break;
144 }
145 case VT_FLOAT:
146 {
147 double value = (a->v_bool) ? 1.0 : 0.0;
148 cmp_result =
149 (value < b->v_float) ? -1 :
150 (value > b->v_float) ? 1 : 0;
151 break;
152 }
153 case VT_BOOL:
154 {
155 cmp_result =
156 (a->v_bool == b->v_bool) ? 0 :
157 (a->v_bool ? 1 : -1);
158 break;
159 }
160 default:
161 set_value_undef(result);
162 return STATUS_OK;
163 }
164 break;
165
166 default:
167 set_value_undef(result);
168 return STATUS_OK;
169 }
170
171 set_value_int(result, cmp_result);
172 return STATUS_OK;
173 }
174
175 status_t stdfunc_min(value_t *result, size_t num_args, const value_t *args)
176 {
177 // Initialize accumulator
178 if (num_args <= 0)
179 {
180 set_value_undef(result);
181 return STATUS_OK;
182 }
183 status_t res = copy_value(result, &args[0]);
184 if (res != STATUS_OK)
185 return res;
186
187 // Do the stuff
188 value_t cmp;
189 init_value(&cmp);
190 lsp_finally { destroy_value(&cmp); };
191
192 for (size_t i=1; i<num_args; ++i)
193 {
194 // Compare values
195 if ((res = numeric_compare(&cmp, result, &args[i])) != STATUS_OK)
196 {
197 destroy_value(result);
198 return res;
199 }
200 // Ensure that comparison succeeded
201 if (cmp.type != VT_INT)
202 {
203 set_value_undef(result);
204 return STATUS_OK;
205 }
206 // Replace accumulator if the comparison result is greater
207 if (cmp.v_int > 0)
208 {
209 if ((res = copy_value(result, &args[i])) != STATUS_OK)
210 return res;
211 }
212 }
213
214 return STATUS_OK;
215 }
216
217 status_t stdfunc_max(value_t *result, size_t num_args, const value_t *args)
218 {
219 // Initialize accumulator
220 if (num_args <= 0)
221 {
222 set_value_undef(result);
223 return STATUS_OK;
224 }
225 status_t res = copy_value(result, &args[0]);
226 if (res != STATUS_OK)
227 return res;
228
229 // Do the stuff
230 value_t cmp;
231 init_value(&cmp);
232 lsp_finally { destroy_value(&cmp); };
233
234 for (size_t i=1; i<num_args; ++i)
235 {
236 // Compare values
237 if ((res = numeric_compare(&cmp, result, &args[i])) != STATUS_OK)
238 {
239 destroy_value(result);
240 return res;
241 }
242 // Ensure that comparison succeeded
243 if (cmp.type != VT_INT)
244 {
245 set_value_undef(result);
246 return STATUS_OK;
247 }
248 // Replace accumulator if the comparison result is greater
249 if (cmp.v_int < 0)
250 {
251 if ((res = copy_value(result, &args[i])) != STATUS_OK)
252 return res;
253 }
254 }
255
256 return STATUS_OK;
257 }
258
259 status_t stdfunc_avg(value_t *result, size_t num_args, const value_t *args)
260 {
261 // Initialize accumulator
262 if (num_args <= 0)
263 {
264 set_value_undef(result);
265 return STATUS_OK;
266 }
267
268 value_t tmp;
269 init_value(&tmp);
270 lsp_finally { destroy_value(&tmp); };
271 set_value_float(result, 0.0);
272
273 for (size_t i=0; i<num_args; ++i)
274 {
275 status_t res = cast_float(&tmp, &args[i]);
276 // Compare values
277 if (res != STATUS_OK)
278 {
279 destroy_value(result);
280 return res;
281 }
282
283 // Ensure that comparison succeeded
284 if (tmp.type != VT_FLOAT)
285 {
286 set_value_undef(result);
287 return STATUS_OK;
288 }
289
290 // Accumulate result
291 result->v_float += tmp.v_float;
292 }
293
294 // Compute the average
295 result->v_float /= num_args;
296
297 return STATUS_OK;
298 }
299
300 status_t stdfunc_rms(value_t *result, size_t num_args, const value_t *args)
301 {
302 // Initialize accumulator
303 if (num_args <= 0)
304 {
305 set_value_undef(result);
306 return STATUS_OK;
307 }
308
309 value_t tmp;
310 init_value(&tmp);
311 lsp_finally { destroy_value(&tmp); };
312 set_value_float(result, 0.0);
313
314 for (size_t i=0; i<num_args; ++i)
315 {
316 status_t res = cast_float(&tmp, &args[i]);
317 // Compare values
318 if (res != STATUS_OK)
319 {
320 destroy_value(result);
321 return res;
322 }
323
324 // Ensure that comparison succeeded
325 if (tmp.type != VT_FLOAT)
326 {
327 set_value_undef(result);
328 return STATUS_OK;
329 }
330
331 // Accumulate result
332 result->v_float += tmp.v_float * tmp.v_float;
333 }
334
335 // Compute the average
336 result->v_float = sqrtf(result->v_float / num_args);
337
338 return STATUS_OK;
339 }
340
341 } /* namespace exp */
342 } /* namespace lsp */
343