forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberScale.h
283 lines (251 loc) · 6.06 KB
/
NumberScale.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**********************************************************************
Sneedacity: A Digital Audio Editor
NumberScale.h
Paul Licameli
**********************************************************************/
#ifndef __SNEEDACITY_NUMBER_SCALE__
#define __SNEEDACITY_NUMBER_SCALE__
#include <algorithm>
#include <cmath>
#include <wx/defs.h>
enum NumberScaleType {
nstLinear,
nstLogarithmic,
nstMel,
nstBark,
nstErb,
nstPeriod,
nstNumScaleTypes,
nstNone,
};
class NumberScale
{
public:
NumberScale()
: mType(nstNone), mValue0(0), mValue1(1)
{}
NumberScale(NumberScaleType type, float value0, float value1)
: mType(type)
{
switch (mType) {
case nstLinear:
case nstNone:
{
mValue0 = value0;
mValue1 = value1;
}
break;
case nstLogarithmic:
{
mValue0 = logf(value0);
mValue1 = logf(value1);
}
break;
case nstMel:
{
mValue0 = hzToMel(value0);
mValue1 = hzToMel(value1);
}
break;
case nstBark:
{
mValue0 = hzToBark(value0);
mValue1 = hzToBark(value1);
}
break;
case nstErb:
{
mValue0 = hzToErb(value0);
mValue1 = hzToErb(value1);
}
break;
case nstPeriod:
{
mValue0 = hzToPeriod(value0);
mValue1 = hzToPeriod(value1);
}
break;
default:
wxASSERT(false);
}
}
NumberScale Reversal() const
{
NumberScale result(*this);
std::swap(result.mValue0, result.mValue1);
return result;
}
bool operator == (const NumberScale& other) const
{
return mType == other.mType
&& mValue0 == other.mValue0
&& mValue1 == other.mValue1;
}
bool operator != (const NumberScale &other) const
{
return !(*this == other);
}
static inline float hzToMel(float hz)
{
return 1127 * log(1 + hz / 700);
}
static inline float melToHz(float mel)
{
return 700 * (exp(mel / 1127) - 1);
}
static inline float hzToBark(float hz)
{
// Traunmueller's formula
const float z1 = 26.81 * hz / (1960 + hz) - 0.53;
if (z1 < 2.0)
return z1 + 0.15 * (2.0 - z1);
else if (z1 > 20.1)
return z1 + 0.22 * (z1 - 20.1);
else
return z1;
}
static inline float barkToHz(float z1)
{
if (z1 < 2.0)
z1 = 2.0 + (z1 - 2.0) / 0.85;
else if (z1 > 20.1)
z1 = 20.1 + (z1 - 20.1) / 1.22;
return 1960 * (z1 + 0.53) / (26.28 - z1);
}
static inline float hzToErb(float hz)
{
return 11.17268 * log(1 + (46.06538 * hz) / (hz + 14678.49));
}
static inline float erbToHz(float erb)
{
return 676170.4 / (47.06538 - exp(0.08950404 * erb)) - 14678.49;
}
static inline float hzToPeriod(float hz)
{
return -1.0 / std::max (1.0f, hz);
}
static inline float periodToHz(float u)
{
return -1.0 / u;
}
// Random access
float PositionToValue(float pp) const
{
switch (mType) {
default:
wxASSERT(false);
case nstLinear:
case nstNone:
return mValue0 + pp * (mValue1 - mValue0);
case nstLogarithmic:
return exp(mValue0 + pp * (mValue1 - mValue0));
case nstMel:
return melToHz(mValue0 + pp * (mValue1 - mValue0));
case nstBark:
return barkToHz(mValue0 + pp * (mValue1 - mValue0));
case nstErb:
return erbToHz(mValue0 + pp * (mValue1 - mValue0));
case nstPeriod:
return periodToHz(mValue0 + pp * (mValue1 - mValue0));
}
}
// STL-idiom iteration
class Iterator
{
public:
Iterator(NumberScaleType type, float step, float value)
: mType(type), mStep(step), mValue(value)
{
}
float operator * () const
{
switch (mType) {
default:
wxASSERT(false);
case nstLinear:
case nstNone:
case nstLogarithmic:
return mValue;
case nstMel:
return melToHz(mValue);
case nstBark:
return barkToHz(mValue);
case nstErb:
return erbToHz(mValue);
case nstPeriod:
return periodToHz(mValue);
}
}
Iterator &operator ++()
{
switch (mType) {
case nstLinear:
case nstNone:
case nstMel:
case nstBark:
case nstErb:
case nstPeriod:
mValue += mStep;
break;
case nstLogarithmic:
mValue *= mStep;
break;
default:
wxASSERT(false);
}
return *this;
}
private:
const NumberScaleType mType;
const float mStep;
float mValue;
};
Iterator begin(float nPositions) const
{
switch (mType) {
default:
wxASSERT(false);
case nstLinear:
case nstNone:
case nstMel:
case nstBark:
case nstErb:
case nstPeriod:
return Iterator
(mType,
nPositions == 1 ? 0 : (mValue1 - mValue0) / (nPositions - 1),
mValue0);
case nstLogarithmic:
return Iterator
(mType,
nPositions == 1 ? 1 : exp((mValue1 - mValue0) / (nPositions - 1)),
exp(mValue0));
}
}
// Inverse
float ValueToPosition(float val) const
{
switch (mType) {
default:
wxASSERT(false);
case nstLinear:
case nstNone:
return ((val - mValue0) / (mValue1 - mValue0));
case nstLogarithmic:
return ((log(val) - mValue0) / (mValue1 - mValue0));
case nstMel:
return ((hzToMel(val) - mValue0) / (mValue1 - mValue0));
case nstBark:
return ((hzToBark(val) - mValue0) / (mValue1 - mValue0));
case nstErb:
return ((hzToErb(val) - mValue0) / (mValue1 - mValue0));
case nstPeriod:
return ((hzToPeriod(val) - mValue0) / (mValue1 - mValue0));
}
}
private:
NumberScaleType mType;
float mValue0;
float mValue1;
};
#endif