13#ifndef __COMMON_UTILS_DSP_H__
14#define __COMMON_UTILS_DSP_H__
16#include "zrythm-config.h"
18#include "utils/math_utils.h"
20#include "juce_wrapper.h"
22namespace zrythm::utils::float_ranges
28[[
using gnu: nonnull, hot]]
static inline void
29fill (
float * buf,
float val,
size_t size,
bool optimized =
true)
33 juce::FloatVectorOperations::fill (buf, val, size);
37 std::fill_n (buf, size, val);
44[[
using gnu: nonnull, hot]]
static inline void
45clip (
float * buf,
float minf,
float maxf,
size_t size,
bool optimized =
true)
49 juce::FloatVectorOperations::clip (buf, buf, minf, maxf, size);
53 std::transform (buf, buf + size, buf, [minf, maxf] (
float x) {
54 return std::clamp (x, minf, maxf);
62[[
using gnu: nonnull, hot]]
static inline void
63copy (
float * dest,
const float * src,
size_t size,
bool optimized =
true)
67 juce::FloatVectorOperations::copy (dest, src, size);
71 std::copy_n (src, size, dest);
78[[
using gnu: nonnull, hot]]
static inline void
79mul_k2 (
float * dest,
float k,
size_t size,
bool optimized =
true)
83 juce::FloatVectorOperations::multiply (dest, k, size);
87 std::transform (dest, dest + size, dest, [k] (
float x) {
return x * k; });
94[[nodiscard]] [[
using gnu: nonnull]]
static inline float
95abs_max (
const float * buf,
size_t size,
bool optimized =
true)
99 auto min_and_max = juce::FloatVectorOperations::findMinAndMax (buf, size);
101 std::abs (min_and_max.getStart ()), std::abs (min_and_max.getEnd ()));
104 return std::abs (*std::max_element (buf, buf + size, [] (
float a,
float b) {
105 return std::abs (a) < std::abs (b);
114[[
using gnu: nonnull, hot]]
static inline bool
115abs_max_with_existing_peak (
119 bool optimized =
true)
121 float new_peak = *cur_peak;
125 new_peak = abs_max (buf, size);
129 for (
size_t i = 0; i < size; i++)
131 float val = fabsf (buf[i]);
140 *cur_peak = new_peak;
148[[gnu::nonnull]]
static inline float
149min (
const float * buf,
size_t size,
bool optimized =
true)
153 return juce::FloatVectorOperations::findMinimum (buf, size);
156 return *std::min_element (buf, buf + size, [] (
float a,
float b) {
164[[gnu::nonnull]]
static inline float
165max (
const float * buf,
size_t size,
bool optimized =
true)
169 return juce::FloatVectorOperations::findMaximum (buf, size);
172 return *std::max_element (buf, buf + size, [] (
float a,
float b) {
180[[
using gnu: nonnull, hot]]
static inline void
181add2 (
float * dest,
const float * src,
size_t count,
bool optimized =
true)
185 juce::FloatVectorOperations::add (dest, src, count);
189 std::transform (dest, dest + count, src, dest, std::plus<> ());
196[[
using gnu: nonnull, hot]]
static inline void
202 bool optimized =
true)
206 juce::FloatVectorOperations::copyWithMultiply (dest, src, k, size);
210 std::transform (dest, dest + size, src, dest, [k] (
float x,
float y) {
219[[
using gnu: nonnull, hot]]
static inline void
225 bool optimized =
true)
229 juce::FloatVectorOperations::addWithMultiply (dest, src, k, size);
233 std::transform (dest, dest + size, src, dest, [k] (
float x,
float y) {
242[[
using gnu: nonnull, hot]]
static inline void
243reverse (
float * dest,
const float * src,
size_t size)
245 std::reverse_copy (src, src + size, dest);
252[[
using gnu: nonnull, hot]]
static inline void
253normalize (
float * dest,
const float * src,
size_t size,
bool optimize =
true)
255 copy (dest, src, size, optimize);
256 const float abs_peak = abs_max (dest, size, optimize);
257 mul_k2 (dest, 1.f / abs_peak, size, optimize);
277 int32_t start_offset,
278 int32_t total_frames_to_fade,
280 float fade_from_multiplier);
296 int32_t start_offset,
297 int32_t total_frames_to_fade,
299 float fade_to_multiplier);
318 bool optimize =
true);
constexpr bool floats_equal(T a, T b)
Checks if 2 floating point numbers are equal.
void make_mono(float *l, float *r, size_t size, bool equal_power, bool optimize=true)
Makes the two signals mono.
void 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.
void 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.