35#include "utils/types.h"
56constexpr auto MINUS_INFINITY = -HUGE_VAL;
63template <std::
floating_po
int T>
67 return std::abs (a - b) < e;
73template <std::
floating_po
int T>
77 return floats_near (a, b, std::numeric_limits<T>::epsilon ());
83template <std::
floating_po
int T>
87 if constexpr (std::is_same_v<T, float>)
100template <std::
floating_po
int T>
104 if constexpr (std::is_same_v<T, float>)
130 int *
const exp_ptr = &t.i;
132 const auto log_2 = (float) (((x >> 23) & 255) - 128);
139 val = ((-1.0f / 3) * t.f + 2) * t.f - 2.0f / 3;
141 return (val + log_2);
146fast_log (
const float val)
148 return (
fast_log2 (val) * std::numbers::ln2_v<float>);
153fast_log10 (
const float val)
162template <std::
floating_po
int T>
165get_fader_val_from_amp (T amp)
167 constexpr T fader_coefficient1 =
169 133.084258667509499408f;
170 constexpr T fader_coefficient2 =
172 1.25870863180257576e17f;
188 (6.f * std::logf (amp)) + fader_coefficient1, 8.f)
189 / fader_coefficient2;
196template <std::
floating_po
int T>
199get_amp_val_from_fader (T fader)
201 constexpr float val1 = 1.f / 6.f;
203 2.f, (val1) * (-192.f + 198.f * std::powf (fader, 1.f / 8.f)));
209template <std::
floating_po
int T>
214 return 20.f * std::log10f (amp);
230template <std::
floating_po
int T>
232calculate_rms_db (
const T * buf,
nframes_t nframes)
240template <std::
floating_po
int T>
245 return std::powf (10.f, (dbfs / 20.f));
251template <std::
floating_po
int T>
254dbfs_to_fader_val (T dbfs)
256 return get_fader_val_from_amp (dbfs_to_amp (dbfs));
uint32_t nframes_t
Frame count.
constexpr bool floats_equal(T a, T b)
Checks if 2 floating point numbers are equal.
constexpr float fast_log2(float val)
Fast log calculation to be used where precision is not required (like log curves).
constexpr long round_to_signed_32(T x)
Rounds a double to a (minimum) signed 32-bit integer.
constexpr bool floats_near(T a, T b, T e)
Returns whether 2 floating point numbers are equal.
constexpr long long round_to_signed_64(T x)
Rounds a double to a (minimum) signed 64-bit integer.
bool is_string_valid_float(const std::string &str, float *ret)
Returns whether the given string is a valid float.
constexpr float ALMOST_SILENCE
Tiny number to be used for denormaml prevention (-140dB).
constexpr unsigned RMS_FRAMES
Frames to skip when calculating the RMS.
bool assert_nonnann(float x)
Asserts that the value is non-nan.
float calculate_rms_amp(const float *buf, nframes_t nframes)
Gets the RMS of the given signal as amplitude (0-2).