forked from stuerp/foo_vis_spectrum_analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupport.h
More file actions
225 lines (176 loc) · 5.7 KB
/
Support.h
File metadata and controls
225 lines (176 loc) · 5.7 KB
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
/** $VER: Support.h (2024.08.18) P. Stuer **/
#pragma once
#include <sdkddkver.h>
#include <Windows.h>
#include <math.h>
#include "Constants.h"
HRESULT GetDPI(HWND hWnd, UINT & dpi);
/// <summary>
/// Returns the minimum value of the specified values.
/// </summary>
template <class T>
inline static T Min(T a, T b)
{
return (a < b) ? a : b;
}
/// <summary>
/// Returns the maximum value of the specified values.
/// </summary>
template <class T>
inline static T Max(T a, T b)
{
return (a > b) ? a : b;
}
/// <summary>
/// Returns the input value clamped between min and max.
/// </summary>
template <class T>
inline static T Clamp(T value, T minValue, T maxValue)
{
return Min(Max(value, minValue), maxValue);
}
/// <summary>
/// Returns true of the input value is in the interval between min and max.
/// </summary>
template <class T>
inline static T InRange(T value, T minValue, T maxValue)
{
return (minValue <= value) && (value <= maxValue);
}
/// <summary>
/// Converts magnitude to decibel (dB).
/// </summary>
inline static double ToDecibel(double magnitude)
{
return 20.0 * ::log10(magnitude);
}
/// <summary>
/// Converts decibel (dB) to magnitude.
/// </summary>
inline static double ToMagnitude(double dB)
{
return ::pow(10.0, dB / 20.0);
}
/// <summary>
/// Converts points to DIPs (Device Independent Pixels).
/// </summary>
inline static FLOAT ToDIPs(FLOAT points)
{
return (points / 72.0f) * (FLOAT) USER_DEFAULT_SCREEN_DPI; // FIXME: Should 96.0 change on high DPI screens?
}
/// <summary>
/// Wraps around a value.
/// </summary>
template<class T>
inline static T Wrap(T value, T max)
{
return (max + (value % max)) % max;
}
/// <summary>
/// Maps a value from one range (srcMin, srcMax) to another (dstMin, dstMax).
/// </summary>
template<class T, class U>
inline static U Map(T value, T srcMin, T srcMax, U dstMin, U dstMax)
{
return dstMin + (U) (((double) (value - srcMin) * (double) (dstMax - dstMin)) / (double) (srcMax - srcMin));
}
/// <summary>
/// Returns true if the specified flags are set.
/// </summary>
template <class T>
inline static bool IsSet(T a, T b)
{
return (a & b) == b;
}
/// <summary>
/// Gets the handle of the module that contains the executing code.
/// </summary>
inline HMODULE GetCurrentModule() noexcept
{
HMODULE hModule = NULL;
::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR) GetCurrentModule, &hModule);
return hModule;
}
/// <summary>
/// Calculates the scale factor of the specified frequency.
/// </summary>
inline double ScaleF(double f, ScalingFunction function, double skewFactor)
{
switch (function)
{
default:
case ScalingFunction::Linear:
return f;
case ScalingFunction::Logarithmic:
return ::log2(f);
case ScalingFunction::ShiftedLogarithmic:
return ::log2(::pow(10, skewFactor * 4.0) + f);
case ScalingFunction::Mel:
return ::log2(1.0 + f / 700.0);
case ScalingFunction::Bark: // "Critical bands"
return (26.81 * f) / (1960.0 + f) - 0.53;
case ScalingFunction::AdjustableBark:
return (26.81 * f) / (::pow(10, skewFactor * 4.0) + f);
case ScalingFunction::ERB: // Equivalent Rectangular Bandwidth
return ::log2(1.0 + 0.00437 * f);
case ScalingFunction::Cams:
return ::log2((f / 1000.0 + 0.312) / (f / 1000.0 + 14.675));
case ScalingFunction::HyperbolicSine:
return ::asinh(f / ::pow(10, skewFactor * 4));
case ScalingFunction::NthRoot:
return ::pow(f, (1.0 / (11.0 - skewFactor * 10.0)));
case ScalingFunction::NegativeExponential:
return -::exp2(-f / ::exp2(7 + skewFactor * 8));
case ScalingFunction::Period:
return 1.0 / f;
}
}
/// <summary>
/// Calculates the frequency of the specified scale factor.
/// </summary>
inline double DeScaleF(double x, ScalingFunction function, double skewFactor)
{
switch (function)
{
default:
case ScalingFunction::Linear:
return x;
case ScalingFunction::Logarithmic:
return ::exp2(x);
case ScalingFunction::ShiftedLogarithmic:
return ::exp2(x) - ::pow(10.0, skewFactor * 4.0);
case ScalingFunction::Mel:
return 700.0 * (::exp2(x) - 1.0);
case ScalingFunction::Bark: // "Critical bands"
return 1960.0 / (26.81 / (x + 0.53) - 1.0);
case ScalingFunction::AdjustableBark:
return ::pow(10.0, (skewFactor * 4.0)) / (26.81 / x - 1.0);
case ScalingFunction::ERB: // Equivalent Rectangular Bandwidth
return (1 / 0.00437) * (::exp2(x) - 1);
case ScalingFunction::Cams:
return (14.675 * ::exp2(x) - 0.312) / (1.0 - ::exp2(x)) * 1000.0;
case ScalingFunction::HyperbolicSine:
return ::sinh(x) * ::pow(10.0, skewFactor * 4);
case ScalingFunction::NthRoot:
return ::pow(x, ((11.0 - skewFactor * 10.0)));
case ScalingFunction::NegativeExponential:
return -::log2(-x) * ::exp2(7.0 + skewFactor * 8.0);
case ScalingFunction::Period:
return 1.0 / x;
}
}
/// <summary>
///
/// </summary>
inline double LogSpace(double minFreq, double maxFreq, double bandIndex, size_t maxBands, double skewFactor)
{
const double CenterFreq = minFreq * ::pow((maxFreq / minFreq), (bandIndex / (double) maxBands));
return CenterFreq * (1 - skewFactor) + (minFreq + ((maxFreq - minFreq) * bandIndex * (1. / (double) maxBands))) * skewFactor;
}
/// <summary>
/// Converts the specified value from degrees to radians.
/// </summary>
inline double Degrees2Radians(double degrees)
{
return (degrees * 2 * M_PI) / 360.;
}