Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2018-2023 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3/*
4 * This file incorporates work covered by the following copyright and
5 * permission notices:
6 *
7 * ---
8 *
9 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * ---
26 */
27
37#ifndef __UTILS_MATH_H__
38#define __UTILS_MATH_H__
39
40#include <cfloat>
41#include <cmath>
42#include <cstdint>
43
44#include "utils/types.h"
45
57#define MATH_RMS_FRAMES 1
58
61#define MATH_TINY_NUMBER (0.0000001)
62
63#define MATH_MINUS_INFINITY (-HUGE_VAL)
64
70#define math_floats_equal_epsilon(a, b, e) \
71 ((a) > (b) ? (a) - (b) < e : (b) - (a) < e)
72
73#define math_doubles_equal_epsilon math_floats_equal_epsilon
74
78#define math_floats_equal(a, b) \
79 ((a) > (b) ? (a) - (b) < FLT_EPSILON : (b) - (a) < FLT_EPSILON)
80
81#define math_doubles_equal(a, b) \
82 ((a) > (b) ? (a) - (b) < DBL_EPSILON : (b) - (a) < DBL_EPSILON)
83
88#define math_round_double_to_signed_32(x) (lround (x))
89
94#define math_round_double_to_signed_64(x) (llround (x))
95
96#define math_round_double_to_signed_frame_t(x) \
97 math_round_double_to_signed_64 (x)
98
103#define math_round_float_to_signed_32(x) (lroundf (x))
104
109#define math_round_float_to_signed_64(x) (llroundf (x))
110
111#define math_round_float_to_signed_frame_t(x) math_round_float_to_signed_64 (x)
112
119CONST
120static inline float
121math_fast_log2 (float val)
122{
123 union
124 {
125 float f;
126 int i;
127 } t;
128 t.f = val;
129 int * const exp_ptr = &t.i;
130 int x = *exp_ptr;
131 const float log_2 = (float) (((x >> 23) & 255) - 128);
132
133 x &= ~(255 << 23);
134 x += 127 << 23;
135
136 *exp_ptr = x;
137
138 val = ((-1.0f / 3) * t.f + 2) * t.f - 2.0f / 3;
139
140 return (val + log_2);
141}
142
143CONST
144static inline float
145math_fast_log (const float val)
146{
147 return (math_fast_log2 (val) * 0.69314718f);
148}
149
150CONST
151static inline float
152math_fast_log10 (const float val)
153{
154 return math_fast_log2 (val) / 3.312500f;
155}
156
161CONST
162static inline sample_t
163math_get_fader_val_from_amp (sample_t amp)
164{
165 const float fader_coefficient1 =
166 /*192.f * logf (2.f);*/
167 133.084258667509499408f;
168 const float fader_coefficient2 =
169 /*powf (logf (2.f), 8.f) * powf (198.f, 8.f);*/
170 1.25870863180257576e17f;
171
172 /* to prevent weird values when amp is very
173 * small */
174 if (amp <= 0.00001f)
175 {
176 return 1e-20f;
177 }
178 else
179 {
180 if (math_floats_equal (amp, 1.f))
181 {
182 amp = 1.f + 1e-20f;
183 }
184 sample_t fader =
185 powf (
186 /* note: don't use fast_log here - it causes
187 * weirdness in faders */
188 6.f * logf (amp) + fader_coefficient1, 8.f)
189 / fader_coefficient2;
190 return (sample_t) fader;
191 }
192}
193
198CONST
199static inline sample_t
200math_get_amp_val_from_fader (sample_t fader)
201{
202 static const float val1 = 1.f / 6.f;
203 return powf (2.f, (val1) * (-192.f + 198.f * powf (fader, 1.f / 8.f)));
204}
205
209CONST
210static inline sample_t
211math_amp_to_dbfs (sample_t amp)
212{
213 return 20.f * log10f (amp);
214}
215
217math_calculate_rms_amp (sample_t * buf, const nframes_t nframes);
218
227
231CONST
232static inline sample_t
233math_dbfs_to_amp (sample_t dbfs)
234{
235 return powf (10.f, (dbfs / 20.f));
236}
237
241CONST
242static inline sample_t
243math_dbfs_to_fader_val (sample_t dbfs)
244{
245 return math_get_fader_val_from_amp (math_dbfs_to_amp (dbfs));
246}
247
255bool
257
263NONNULL_ARGS (1)
264bool math_is_string_valid_float (const char * str, float * ret);
265
270#endif
NONNULL_ARGS(1) int undo_manager_undo(UndoManager *self
Undo last action.
#define math_floats_equal(a, b)
Checks if 2 doubles are equal.
Definition math.h:78
bool math_is_string_valid_float(const char *str, float *ret)
Returns whether the given string is a valid float.
uint32_t nframes_t
Frame count.
Definition types.h:39
bool math_assert_nonnann(float x)
Asserts that the value is non-nan.
sample_t math_calculate_rms_db(sample_t *buf, const nframes_t nframes)
Calculate db using RMS method.
float sample_t
The sample type.
Definition types.h:51
Custom types.