-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsf2parse.cpp
More file actions
227 lines (163 loc) · 3.77 KB
/
sf2parse.cpp
File metadata and controls
227 lines (163 loc) · 3.77 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
226
227
#include "sf2parse.h"
double NSinc(double x) {
if (x == 0.0)
return 1.0;
double pix = PI * x;
return sin(pix) / pix;
}
double Window(double x) {
// Hamming window, with x in the range [0.0, 1.0]
return 0.54 - (0.46 * cos(2.0 * PI * x));
}
float HermiteResp(float x) {
// 6-point, 3rd-order Hermite impulse response.
// god this was a pain to research
x = abs(x);
double x2 = x * x;
double x3 = x2 * x;
if (x < 1.0)
return 1.0 - (2.333333 * x2) + (1.333333 * x3);
if (x < 2.0)
return 2.5 - ((59.0 / 12.0) * x) + (3.0 * x2) - ((7.0 / 12.0) * x3);
if (x < 3.0)
return -1.5 + (1.75 * x) - (0.666666 * x2) + ((1.0 / 12.0) * x3);
return 0.0;
// 6-point, 5th-order Hermite impulse response.
/*
x = abs(x);
float x2 = x * x;
float x3 = x2 * x;
float x4 = x3 * x;
float x5 = x4 * x;
if (x < 1.f)
return 1.f - (25.f / 12.f * x2) + (5.f / 12.f * x3) + (13.f / 12.f * x4) - (5.f / 12.f * x5);
if (x < 2.f)
return 1.f + (5.f / 12.f * x) - (35.f / 8.f * x2) + (35.f / 8.f * x3) - (13.f / 8.f * x4) + (5.f / 24.f * x5);
if (x < 3.f)
return 3.f - (29.f / 4.f * x) + (155.f / 24.f * x2) - (65.f / 24.f * x3) + (13.f / 24.f * x4) - (1.f / 24.f * x5);
return 0.f;
*/
/*
// 4-point, 3rd-order Hermite impulse response.
x = abs(x);
double x2 = x * x;
double x3 = x2 * x;
if (x < 1.0)
return 1.0 - (5.0 / 2.0 * x2) + (3.0 / 2.0 * x3);
if (x < 2.0)
return 2.0 - (4.0 * x) + (5.0 / 2.0 * x2) - (0.5 * x3);
return 0.0;
*/
}
float ClampSymmetric(float a, float b, float val) {
if (b > a) {
return max(min(val, b), a);
} else if (b < a) {
return max(min(val, a), b);
} else {
return a;
}
}
double ClampSymmetric(double a, double b, double val) {
if (b > a) {
return max(min(val, b), a);
}
else if (b < a) {
return max(min(val, a), b);
}
else {
return a;
}
}
float Lerp(float a, float b, float t, bool clamp) {
auto res = (1.0f - t) * a + t * b;
if (clamp)
return ClampSymmetric(a, b, res);
return res;
}
double Lerp(double a, double b, double t, bool clamp) {
auto res = (1.0 - t) * a + t * b;
if (clamp)
return ClampSymmetric(a, b, res);
return res;
}
float FastLerp(float a, float b, float t) {
return a + t * (b - a);
}
double FastLerp(double a, double b, double t) {
return a + t * (b - a);
}
void riff_copy(void const* src, void* dest, size_t len, bool flip_endian) {
if (flip_endian) {
char* dchar = (char*)dest;
char* schar = (char*)src;
for (size_t i = 0; i < len; i++) {
dchar[i] = schar[len - (i + 1)];
}
}
else {
memcpy(dest, src, len);
}
}
float ConcaveCurve(float t) {
if (t <= 0.f)
return 0.f;
if (t >= 1.f)
return 1.f;
t = 1.f - t;
return (-20.f / 96.0f) * log(t * t);
}
double ConcaveCurve(double t) {
if (t <= 0.0)
return 0.0;
if (t >= 1.0)
return 1.0;
t = 1.0 - t;
return (-20.0 / 96.0) * log(t * t);
}
float SfCurve(float t, SfControllerType type, bool clamp) {
float result;
switch (type) {
case SfControllerType::Linear:
result = t;
break;
case SfControllerType::Concave:
result = ConcaveCurve(t);
break;
case SfControllerType::Convex:
result = 1.f - ConcaveCurve(1.f - t);
break;
case SfControllerType::Switch:
result = t >= 0.5f ? 1.0f : 0.0f;
break;
default:
result = t;
}
if (clamp)
return ClampSymmetric(0.0f, 1.0f, result);
else
return result;
}
double SfCurve( double t, SfControllerType type, bool clamp) {
double result;
switch (type) {
case SfControllerType::Linear:
result = t;
break;
case SfControllerType::Concave:
result = ConcaveCurve(t);
break;
case SfControllerType::Convex:
result = 1.0 - ConcaveCurve(1.0 - t);
break;
case SfControllerType::Switch:
result = t >= 0.5 ? 1.0 : 0.0;
break;
default:
result = t;
}
if (clamp)
return ClampSymmetric(0.0, 1.0, result);
else
return result;
}