-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdsp.h
145 lines (104 loc) · 2.35 KB
/
dsp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef DSP_H
#define DSP_H
#include <cmath>
#include <limits>
extern double db_scale;
inline float db2lin (float db)
{
return (float) exp (db * db_scale);
}
/*
static inline float db2lin (float db)
{
return powf (10.0f, db / 20);
}
*/
inline float float2db (float v)
{
if (v == 0.0f)
return 0.0f;
if (v > 0.0f)
return (float) 20 * log10 (v / 1.0);
return (float) 20 * log10 (v / -1.0);
}
void init_db();
inline bool float_greater_than (float a, float b)
{
return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<double>::epsilon());
}
inline bool float_less_than (float a, float b)
{
return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<double>::epsilon());
}
inline bool float_equal (float x, float y)
{
return std::abs(x - y) <= std::numeric_limits<double>::epsilon() * std::abs(x);
}
inline float conv (float v, float middle, float max)
{
if (v == middle)
return 0;
if (v > middle)
return (max - middle - v);
else
return middle - v;
}
inline float conv_to_db (float v, float v_min, float v_max, float range_negative, float range_positive)
{
if (v == 0)
return 0;
if (v > 0)
{
float x = v_max / range_positive;
float y = v_max / v;
return v / (y * x);
}
else
{
float x = v_min / range_negative;
float y = v_min / v;
return v / (y * x);
}
}
inline float scale_val (float val, float from_min, float from_max, float to_min, float to_max)
{
return (val - from_min) * (to_max - to_min) /
(from_max - from_min) + to_min;
}
#define PANLAW_SINCOS 0
#define PANLAW_SQRT 1
#define PANLAW_LINEAR0 2
#define PANLAW_LINEAR6 3
#define PANLAW_SINCOSV2 4
//linear panner, law: -6 dB
inline void pan_linear6 (float &l, float& r, float p)
{
l = 1 - p;
r = p;
}
//linear panner, law: 0 dB
inline void pan_linear0 (float &l, float& r, float p)
{
l = 0.5 + (1 - p);
r = 0.5 + p;
}
//square root panner, law: -3 dB
inline void pan_sqrt (float &l, float& r, float p)
{
l = sqrt (1 - p);
r = sqrt (p);
}
//sin/cos panner, law: -3 dB
inline void pan_sincos (float &l, float& r, float p)
{
float pan = 0.5 * M_PI * p;
l = cos (pan);
r = sin (pan);
}
inline void pan_sincos_v2 (float &l, float& r, float p)
{
float pan = p * M_PI / 2;
l = l * sin (pan);
r = r * cos (pan);
}
#endif