17#include "utils/math_utils.h"
19#include "juce_wrapper.h"
21namespace zrythm::utils::float_ranges
27[[
using gnu: nonnull, hot]]
static inline void
28fill (
float * buf,
float val,
size_t size,
bool optimized =
true)
32 juce::FloatVectorOperations::fill (buf, val, size);
36 std::fill_n (buf, size, val);
43[[
using gnu: nonnull, hot]]
static inline void
44clip (
float * buf,
float minf,
float maxf,
size_t size,
bool optimized =
true)
48 juce::FloatVectorOperations::clip (buf, buf, minf, maxf, size);
52 std::transform (buf, buf + size, buf, [minf, maxf] (
float x) {
53 return std::clamp (x, minf, maxf);
61[[
using gnu: nonnull, hot]]
static inline void
62copy (
float * dest,
const float * src,
size_t size,
bool optimized =
true)
66 juce::FloatVectorOperations::copy (dest, src, size);
70 std::copy_n (src, size, dest);
77[[
using gnu: nonnull, hot]]
static inline void
78mul_k2 (
float * dest,
float k,
size_t size,
bool optimized =
true)
82 juce::FloatVectorOperations::multiply (dest, k, size);
86 std::transform (dest, dest + size, dest, [k] (
float x) {
return x * k; });
93[[nodiscard]] [[
using gnu: nonnull]]
static inline float
94abs_max (
const float * buf,
size_t size,
bool optimized =
true)
98 auto min_and_max = juce::FloatVectorOperations::findMinAndMax (buf, size);
100 std::abs (min_and_max.getStart ()), std::abs (min_and_max.getEnd ()));
103 return std::abs (*std::max_element (buf, buf + size, [] (
float a,
float b) {
104 return std::abs (a) < std::abs (b);
113[[
using gnu: nonnull, hot]]
static inline bool
114abs_max_with_existing_peak (
118 bool optimized =
true)
120 float new_peak = *cur_peak;
124 new_peak = abs_max (buf, size);
128 for (
size_t i = 0; i < size; i++)
130 float val = fabsf (buf[i]);
131 new_peak = std::max (val, new_peak);
136 *cur_peak = new_peak;
144[[gnu::nonnull]]
static inline float
145min (
const float * buf,
size_t size,
bool optimized =
true)
149 return juce::FloatVectorOperations::findMinimum (buf, size);
152 return *std::min_element (buf, buf + size, [] (
float a,
float b) {
160[[gnu::nonnull]]
static inline float
161max (
const float * buf,
size_t size,
bool optimized =
true)
165 return juce::FloatVectorOperations::findMaximum (buf, size);
168 return *std::max_element (buf, buf + size, [] (
float a,
float b) {
176[[
using gnu: nonnull, hot]]
static inline void
177add2 (
float * dest,
const float * src,
size_t count,
bool optimized =
true)
181 juce::FloatVectorOperations::add (dest, src, count);
185 std::transform (dest, dest + count, src, dest, std::plus<> ());
192[[
using gnu: nonnull, hot]]
static inline void
198 bool optimized =
true)
202 juce::FloatVectorOperations::copyWithMultiply (dest, src, k, size);
206 std::transform (dest, dest + size, src, dest, [k] (
float x,
float y) {
215[[
using gnu: nonnull, hot]]
static inline void
221 bool optimized =
true)
225 juce::FloatVectorOperations::addWithMultiply (dest, src, k, size);
229 std::transform (dest, dest + size, src, dest, [k] (
float x,
float y) {
238[[
using gnu: nonnull, hot]]
static inline void
239reverse (
float * dest,
const float * src,
size_t size)
241 std::reverse_copy (src, src + size, dest);
248[[
using gnu: nonnull, hot]]
static inline void
249normalize (
float * dest,
const float * src,
size_t size,
bool optimize =
true)
251 copy (dest, src, size, optimize);
252 const float abs_peak = abs_max (dest, size, optimize);
253 mul_k2 (dest, 1.f / abs_peak, size, optimize);
273 int32_t start_offset,
274 int32_t total_frames_to_fade,
276 float fade_from_multiplier);
292 int32_t start_offset,
293 int32_t total_frames_to_fade,
295 float fade_to_multiplier);
314 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.