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 <cstddef>
19
20#include "utils/math.h"
21#include "zrythm.h"
22
23#include <glib.h>
24
25#ifdef HAVE_LSP_DSP
26# include <lsp-plug.in/dsp/dsp.h>
27#endif
28
32NONNULL HOT static inline void
33dsp_fill (float * buf, float val, size_t size)
34{
35#ifdef HAVE_LSP_DSP
36 if (ZRYTHM_USE_OPTIMIZED_DSP)
37 {
38 lsp::dsp::fill (buf, val, size);
39 }
40 else
41 {
42#endif
43 for (size_t i = 0; i < size; i++)
44 {
45 buf[i] = val;
46 }
47#ifdef HAVE_LSP_DSP
48 }
49#endif
50}
51
55NONNULL HOT static inline void
56dsp_limit1 (float * buf, float minf, float maxf, size_t size)
57{
58#ifdef HAVE_LSP_DSP
59 if (ZRYTHM_USE_OPTIMIZED_DSP)
60 {
61 lsp::dsp::limit1 (buf, minf, maxf, size);
62 }
63 else
64 {
65#endif
66 for (size_t i = 0; i < size; i++)
67 {
68 buf[i] = CLAMP (buf[i], minf, maxf);
69 }
70#ifdef HAVE_LSP_DSP
71 }
72#endif
73}
74
78NONNULL HOT static inline void
79dsp_copy (float * dest, const float * src, size_t size)
80{
81#ifdef HAVE_LSP_DSP
82 if (ZRYTHM_USE_OPTIMIZED_DSP)
83 {
84 lsp::dsp::copy (dest, src, size);
85 }
86 else
87 {
88#endif
89 for (size_t i = 0; i < size; i++)
90 {
91 dest[i] = src[i];
92 }
93#ifdef HAVE_LSP_DSP
94 }
95#endif
96}
97
101NONNULL HOT static inline void
102dsp_mul_k2 (float * dest, float k, size_t size)
103{
104#ifdef HAVE_LSP_DSP
105 if (ZRYTHM_USE_OPTIMIZED_DSP)
106 {
107 lsp::dsp::mul_k2 (dest, k, size);
108 }
109 else
110 {
111#endif
112 for (size_t i = 0; i < size; i++)
113 {
114 dest[i] *= k;
115 }
116#ifdef HAVE_LSP_DSP
117 }
118#endif
119}
120
124NONNULL WARN_UNUSED_RESULT static inline float
125dsp_abs_max (float * buf, size_t size)
126{
127#ifdef HAVE_LSP_DSP
128 if (ZRYTHM_USE_OPTIMIZED_DSP)
129 {
130 return lsp::dsp::abs_max (buf, size);
131 }
132 else
133 {
134#endif
135 float ret = 1e-20f;
136 for (size_t i = 0; i < size; i++)
137 {
138 if (fabsf (buf[i]) > ret)
139 {
140 ret = fabsf (buf[i]);
141 }
142 }
143 return ret;
144#ifdef HAVE_LSP_DSP
145 }
146#endif
147}
148
154HOT NONNULL static inline bool
155dsp_abs_max_with_existing_peak (float * buf, float * cur_peak, size_t size)
156{
157 float new_peak = *cur_peak;
158
159#ifdef HAVE_LSP_DSP
160 if (ZRYTHM_USE_OPTIMIZED_DSP)
161 {
162 new_peak = lsp::dsp::abs_max (buf, size);
163 }
164 else
165 {
166#endif
167 for (size_t i = 0; i < size; i++)
168 {
169 float val = fabsf (buf[i]);
170 if (val > new_peak)
171 {
172 new_peak = val;
173 }
174 }
175#ifdef HAVE_LSP_DSP
176 }
177#endif
178
179 bool changed = !math_floats_equal (new_peak, *cur_peak);
180 *cur_peak = new_peak;
181
182 return changed;
183}
184
188NONNULL float
189dsp_min (float * buf, size_t size);
190
194NONNULL float
195dsp_max (float * buf, size_t size);
196
200NONNULL HOT static inline void
201dsp_add2 (float * dest, const float * src, size_t count)
202{
203#ifdef HAVE_LSP_DSP
204 if (ZRYTHM_USE_OPTIMIZED_DSP)
205 {
206 lsp::dsp::add2 (dest, src, count);
207 }
208 else
209 {
210#endif
211 for (size_t i = 0; i < count; i++)
212 {
213 dest[i] = dest[i] + src[i];
214 }
215#ifdef HAVE_LSP_DSP
216 }
217#endif
218}
219
223NONNULL HOT static inline void
224dsp_mix2 (float * dest, const float * src, float k1, float k2, size_t size)
225{
226#ifdef HAVE_LSP_DSP
227 if (ZRYTHM_USE_OPTIMIZED_DSP)
228 {
229 lsp::dsp::mix2 (dest, src, k1, k2, size);
230 }
231 else
232 {
233#endif
234 for (size_t i = 0; i < size; i++)
235 {
236 dest[i] = dest[i] * k1 + src[i] * k2;
237 }
238#ifdef HAVE_LSP_DSP
239 }
240#endif
241}
242
246NONNULL HOT static inline void
247dsp_reverse1 (float * dest, size_t size)
248{
249#ifdef HAVE_LSP_DSP
250 if (ZRYTHM_USE_OPTIMIZED_DSP)
251 {
252 lsp::dsp::reverse1 (dest, size);
253 }
254 else
255 {
256#endif
257 for (size_t i = 0; i < size; i++)
258 {
259 dest[i] = dest[(size - i) - 1];
260 }
261#ifdef HAVE_LSP_DSP
262 }
263#endif
264}
265
269NONNULL HOT static inline void
270dsp_reverse2 (float * dest, float * src, size_t size)
271{
272#ifdef HAVE_LSP_DSP
273 if (ZRYTHM_USE_OPTIMIZED_DSP)
274 {
275 lsp::dsp::reverse2 (dest, src, size);
276 }
277 else
278 {
279#endif
280 for (size_t i = 0; i < size; i++)
281 {
282 dest[i] = src[(size - i) - 1];
283 }
284#ifdef HAVE_LSP_DSP
285 }
286#endif
287}
288
293NONNULL HOT static inline void
294dsp_normalize (float * dest, const float * src, size_t size)
295{
296#ifdef HAVE_LSP_DSP
297 if (ZRYTHM_USE_OPTIMIZED_DSP)
298 {
299 lsp::dsp::normalize (dest, src, size);
300 }
301 else
302 {
303#endif
304 dsp_copy (dest, src, size);
305 float abs_peak = dsp_abs_max (dest, size);
306 dsp_mul_k2 (dest, 1.f / abs_peak, size);
307#ifdef HAVE_LSP_DSP
308 }
309#endif
310}
311
316NONNULL void
318 float * dest,
319 const float * src1,
320 const float * src2,
321 float k1,
322 float k2,
323 size_t size);
324
339NONNULL void
341 float * dest,
342 int32_t start_offset,
343 int32_t total_frames_to_fade,
344 size_t size,
345 float fade_from_multiplier);
346
358NONNULL void
360 float * dest,
361 int32_t start_offset,
362 int32_t total_frames_to_fade,
363 size_t size,
364 float fade_to_multiplier);
365
377NONNULL void
378dsp_make_mono (float * l, float * r, size_t size, bool equal_power);
379
380#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:78
Math utils.
The main Zrythm struct.