XDSP
Audio signal processing and synthesis library.
Loading...
Searching...
No Matches
decibel.h
1#ifndef DECIBEL_H
2#define DECIBEL_H
3
4#include <cmath>
5
9namespace xdsp::decibel {
10
14constexpr double DB_REF = 1.0;
15
22inline double to_linear(double db) { return pow(10.0, db / 20.0); }
23
27inline float to_linear(float db) { return powf(10.0f, db / 20.0f); }
28
37inline double to_linear_off(double db, double threshold) {
38 if (db <= threshold) {
39 return 0.0;
40 } else {
41 return to_linear(db);
42 }
43}
44
48inline float to_linear_off(float db, double threshold) {
49 if (db <= threshold) {
50 return 0.0f;
51 } else {
52 return to_linear(db);
53 }
54}
55
62inline double to_db(double linear) { return 20.0 * log10(linear); }
63
67inline float to_db(float linear) { return 20.0f * log10f(linear); }
68
76inline double apply(double lin, double gain) { return lin * to_linear(gain); }
77
81inline float apply(float lin, float gain) { return lin * to_linear(gain); }
82
92inline double apply_off(double lin, double gain, double threshold) {
93 return lin * to_linear_off(gain, threshold);
94}
95
99inline float apply(float lin, float gain, float threshold) {
100 return lin * to_linear_off(gain, threshold);
101}
102
103} // namespace xdsp::decibel
104
105#endif // DECIBEL_H
Decibel conversion functions.
Definition decibel.h:9
double to_db(double linear)
Converts linear gain factor to decibel (relative to 1.0) value.
Definition decibel.h:62
double apply_off(double lin, double gain, double threshold)
Apply a decibel gain to a linear signal, muting if below threshold.
Definition decibel.h:92
double to_linear_off(double db, double threshold)
Converts decibel (relative to 1.0) value to linear gain factor. If value is below given threshold,...
Definition decibel.h:37
constexpr double DB_REF
Reference value for all decibel conversions.
Definition decibel.h:14
double apply(double lin, double gain)
Apply a decibel gain to a linear signal.
Definition decibel.h:76
double to_linear(double db)
Converts decibel (relative to 1.0) value to linear gain factor.
Definition decibel.h:22