Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
dsp.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2020-2022 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
13#ifndef __UTILS_DSP_H__
14#define __UTILS_DSP_H__
15
16#include "zrythm-config.h"
17
18#include <stdbool.h>
19#include <stddef.h>
20
21#include "utils/math.h"
22#include "zrythm.h"
23
24#include <glib.h>
25
26#ifdef HAVE_LSP_DSP
27# include <lsp-plug.in/dsp/dsp.h>
28#endif
29
33NONNULL HOT static inline void
34dsp_fill (float * buf, float val, size_t size)
35{
36#ifdef HAVE_LSP_DSP
37 if (ZRYTHM_USE_OPTIMIZED_DSP)
38 {
39 lsp_dsp_fill (buf, val, size);
40 }
41 else
42 {
43#endif
44 for (size_t i = 0; i < size; i++)
45 {
46 buf[i] = val;
47 }
48#ifdef HAVE_LSP_DSP
49 }
50#endif
51}
52
56NONNULL HOT static inline void
57dsp_limit1 (float * buf, float minf, float maxf, size_t size)
58{
59#ifdef HAVE_LSP_DSP
60 if (ZRYTHM_USE_OPTIMIZED_DSP)
61 {
62 lsp_dsp_limit1 (buf, minf, maxf, size);
63 }
64 else
65 {
66#endif
67 for (size_t i = 0; i < size; i++)
68 {
69 buf[i] = CLAMP (buf[i], minf, maxf);
70 }
71#ifdef HAVE_LSP_DSP
72 }
73#endif
74}
75
79NONNULL HOT static inline void
80dsp_copy (float * dest, const float * src, size_t size)
81{
82#ifdef HAVE_LSP_DSP
83 if (ZRYTHM_USE_OPTIMIZED_DSP)
84 {
85 lsp_dsp_copy (dest, src, size);
86 }
87 else
88 {
89#endif
90 for (size_t i = 0; i < size; i++)
91 {
92 dest[i] = src[i];
93 }
94#ifdef HAVE_LSP_DSP
95 }
96#endif
97}
98
102NONNULL HOT static inline void
103dsp_mul_k2 (float * dest, float k, size_t size)
104{
105#ifdef HAVE_LSP_DSP
106 if (ZRYTHM_USE_OPTIMIZED_DSP)
107 {
108 lsp_dsp_mul_k2 (dest, k, size);
109 }
110 else
111 {
112#endif
113 for (size_t i = 0; i < size; i++)
114 {
115 dest[i] *= k;
116 }
117#ifdef HAVE_LSP_DSP
118 }
119#endif
120}
121
125NONNULL WARN_UNUSED_RESULT static inline float
126dsp_abs_max (float * buf, size_t size)
127{
128#ifdef HAVE_LSP_DSP
129 if (ZRYTHM_USE_OPTIMIZED_DSP)
130 {
131 return lsp_dsp_abs_max (buf, size);
132 }
133 else
134 {
135#endif
136 float ret = 1e-20f;
137 for (size_t i = 0; i < size; i++)
138 {
139 if (fabsf (buf[i]) > ret)
140 {
141 ret = fabsf (buf[i]);
142 }
143 }
144 return ret;
145#ifdef HAVE_LSP_DSP
146 }
147#endif
148}
149
155HOT NONNULL static inline bool
156dsp_abs_max_with_existing_peak (float * buf, float * cur_peak, size_t size)
157{
158 float new_peak = *cur_peak;
159
160#ifdef HAVE_LSP_DSP
161 if (ZRYTHM_USE_OPTIMIZED_DSP)
162 {
163 new_peak = lsp_dsp_abs_max (buf, size);
164 }
165 else
166 {
167#endif
168 for (size_t i = 0; i < size; i++)
169 {
170 float val = fabsf (buf[i]);
171 if (val > new_peak)
172 {
173 new_peak = val;
174 }
175 }
176#ifdef HAVE_LSP_DSP
177 }
178#endif
179
180 bool changed = !math_floats_equal (new_peak, *cur_peak);
181 *cur_peak = new_peak;
182
183 return changed;
184}
185
189NONNULL float
190dsp_min (float * buf, size_t size);
191
195NONNULL float
196dsp_max (float * buf, size_t size);
197
201NONNULL HOT static inline void
202dsp_add2 (float * dest, const float * src, size_t count)
203{
204#ifdef HAVE_LSP_DSP
205 if (ZRYTHM_USE_OPTIMIZED_DSP)
206 {
207 lsp_dsp_add2 (dest, src, count);
208 }
209 else
210 {
211#endif
212 for (size_t i = 0; i < count; i++)
213 {
214 dest[i] = dest[i] + src[i];
215 }
216#ifdef HAVE_LSP_DSP
217 }
218#endif
219}
220
224NONNULL HOT static inline void
225dsp_mix2 (float * dest, const float * src, float k1, float k2, size_t size)
226{
227#ifdef HAVE_LSP_DSP
228 if (ZRYTHM_USE_OPTIMIZED_DSP)
229 {
230 lsp_dsp_mix2 (dest, src, k1, k2, size);
231 }
232 else
233 {
234#endif
235 for (size_t i = 0; i < size; i++)
236 {
237 dest[i] = dest[i] * k1 + src[i] * k2;
238 }
239#ifdef HAVE_LSP_DSP
240 }
241#endif
242}
243
247NONNULL HOT static inline void
248dsp_reverse1 (float * dest, size_t size)
249{
250#ifdef HAVE_LSP_DSP
251 if (ZRYTHM_USE_OPTIMIZED_DSP)
252 {
253 lsp_dsp_reverse1 (dest, size);
254 }
255 else
256 {
257#endif
258 for (size_t i = 0; i < size; i++)
259 {
260 dest[i] = dest[(size - i) - 1];
261 }
262#ifdef HAVE_LSP_DSP
263 }
264#endif
265}
266
270NONNULL HOT static inline void
271dsp_reverse2 (float * dest, float * src, size_t size)
272{
273#ifdef HAVE_LSP_DSP
274 if (ZRYTHM_USE_OPTIMIZED_DSP)
275 {
276 lsp_dsp_reverse2 (dest, src, size);
277 }
278 else
279 {
280#endif
281 for (size_t i = 0; i < size; i++)
282 {
283 dest[i] = src[(size - i) - 1];
284 }
285#ifdef HAVE_LSP_DSP
286 }
287#endif
288}
289
294NONNULL HOT static inline void
295dsp_normalize (float * dest, const float * src, size_t size)
296{
297#ifdef HAVE_LSP_DSP
298 if (ZRYTHM_USE_OPTIMIZED_DSP)
299 {
300 lsp_dsp_normalize (dest, src, size);
301 }
302 else
303 {
304#endif
305 dsp_copy (dest, src, size);
306 float abs_peak = dsp_abs_max (dest, size);
307 dsp_mul_k2 (dest, 1.f / abs_peak, size);
308#ifdef HAVE_LSP_DSP
309 }
310#endif
311}
312
317NONNULL void
319 float * dest,
320 const float * src1,
321 const float * src2,
322 float k1,
323 float k2,
324 size_t size);
325
340NONNULL void
342 float * dest,
343 int32_t start_offset,
344 int32_t total_frames_to_fade,
345 size_t size,
346 float fade_from_multiplier);
347
359NONNULL void
361 float * dest,
362 int32_t start_offset,
363 int32_t total_frames_to_fade,
364 size_t size,
365 float fade_to_multiplier);
366
378NONNULL void
379dsp_make_mono (float * l, float * r, size_t size, bool equal_power);
380
381#endif
NONNULL float dsp_min(float *buf, size_t size)
Gets the minimum of the buffer.
NONNULL void dsp_make_mono(float *l, float *r, size_t size, bool equal_power)
Makes the two signals mono.
NONNULL void dsp_mix_add2(float *dest, const float *src1, const float *src2, float k1, float k2, size_t size)
Calculate dst[i] = dst[i] + src1[i] * k1 + src2[i] * k2.
NONNULL void dsp_linear_fade_in_from(float *dest, int32_t start_offset, int32_t total_frames_to_fade, size_t size, float fade_from_multiplier)
Calculate linear fade by multiplying from 0 to 1 for.
NONNULL float dsp_max(float *buf, size_t size)
Gets the maximum of the buffer.
NONNULL void dsp_linear_fade_out_to(float *dest, int32_t start_offset, int32_t total_frames_to_fade, size_t size, float fade_to_multiplier)
Calculate linear fade by multiplying from 0 to 1 for.
#define math_floats_equal(a, b)
Checks if 2 doubles are equal.
Definition math.h:80
Math utils.
The main Zrythm struct.