-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresweep.h
278 lines (224 loc) · 6.37 KB
/
resweep.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
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************/
/******************************************************************************/
#ifdef __cplusplus
}
#endif
#ifdef RESWEEP_IMPLEMENTATION
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_1_PI
#define M_1_PI 0.31830988618379067154
#endif
#define SIDELOBE_HEIGHT 96
#define UP_TRANSITION_WIDTH (1.0 / 32.0)
#define DOWN_TRANSITION_WIDTH (1.0 / 128.0)
#define MAX_SINC_WINDOW_SIZE 2048
#define RESAMPLE_LUT_STEP 128
typedef struct
{
float value;
float delta;
}
lutEntry_t;
lutEntry_t dynamicLut[RESAMPLE_LUT_STEP * MAX_SINC_WINDOW_SIZE];
static inline unsigned int calc_gcd(unsigned int a, unsigned int b)
{
while (b)
{
unsigned int t = b;
b = a % b;
a = t;
}
return a;
}
static inline double exact_nsinc(double x)
{
if (x == 0.0)
return 1.0;
return ((double)(M_1_PI) / x) * sin(M_PI * x);
}
// Modified Bessel function of the first kind, order 0
// https://ccrma.stanford.edu/~jos/sasp/Kaiser_Window.html
static inline double I0(double x)
{
double r = 1.0, xx = x * x, xpow = xx, coeff = 0.25;
int k;
// iterations until coeff ~= 0
// 19 for float32, 89 for float64, 880 for float80
for (k = 1; k < 89; k++)
{
r += xpow * coeff;
coeff /= (4 * k + 8) * k + 4;
xpow *= xx;
}
return r;
}
// https://ccrma.stanford.edu/~jos/sasp/Kaiser_Window.html
static inline double kaiser(int n, int length, double beta)
{
double mid = 2 * n / (double)(length - 1) - 1.0;
return I0(beta * sqrt(1.0 - mid * mid)) / I0(beta);
}
static inline void sinc_resample_createLut(int inFreq, int cutoffFreq2, int windowSize, double beta)
{
double windowLut[windowSize];
double freqAdjust = (double)cutoffFreq2 / (double)inFreq;
lutEntry_t *out, *in;
int i, j;
for (i = 0; i < windowSize; i++)
windowLut[i] = kaiser(i, windowSize, beta);
out = dynamicLut;
for (i = 0; i < RESAMPLE_LUT_STEP; i++)
{
double offset = i / (double)(RESAMPLE_LUT_STEP - 1) - windowSize / 2;
double sum = 0.0;
for (j = 0; j < windowSize; j++)
{
double s = exact_nsinc((j + offset) * freqAdjust);
out->value = s * windowLut[j];
sum += s;
out++;
}
out -= windowSize;
for (j = 0; j < windowSize; j++)
{
out->value /= sum;
out++;
}
}
out = dynamicLut;
in = out + windowSize;
for (i = 0; i < RESAMPLE_LUT_STEP - 1; i++)
{
for (j = 0; j < windowSize; j++)
{
out->delta = in->value - out->value;
out++;
in++;
}
}
for (j = 0; j < windowSize; j++)
{
out->delta = 0;
out++;
}
}
static inline void sinc_resample_internal(short *wavOut, int sizeOut, int outFreq, const short *wavIn, int sizeIn, int inFreq, int cutoffFreq2, int numChannels, int windowSize, double beta)
{
float y[windowSize * numChannels];
const short *sampleIn, *wavInEnd = wavIn + (sizeIn / 2);
short *sampleOut, *wavOutEnd = wavOut + (sizeOut / 2);
float outPeriod;
int subpos = 0;
int gcd = calc_gcd(inFreq, outFreq);
int i, c, next;
float dither[numChannels];
sinc_resample_createLut(inFreq, cutoffFreq2, windowSize, beta);
inFreq /= gcd;
outFreq /= gcd;
outPeriod = 1.0f / outFreq;
for (c = 0; c < numChannels; c++)
dither[c] = 0.0f;
for (i = 0; i < windowSize / 2 - 1; i++)
{
for (c = 0; c < numChannels; c++)
y[i * numChannels + c] = 0;
}
sampleIn = wavIn;
for (; i < windowSize; i++)
{
for (c = 0; c < numChannels; c++)
y[i * numChannels + c] = (sampleIn < wavInEnd) ? *sampleIn++ : 0;
}
sampleOut = wavOut;
next = 0;
while (sampleOut < wavOutEnd)
{
float samples[numChannels];
float offset = 1.0f - subpos * outPeriod;
float interp;
lutEntry_t *lutPart;
int index;
for (c = 0; c < numChannels; c++)
samples[c] = 0.0f;
interp = offset * (RESAMPLE_LUT_STEP - 1);
index = interp;
interp -= index;
lutPart = dynamicLut + index * windowSize;
for (i = next; i < windowSize; i++, lutPart++)
{
float scale = lutPart->value + lutPart->delta * interp;
for (c = 0; c < numChannels; c++)
samples[c] += y[i * numChannels + c] * scale;
}
for (i = 0; i < next; i++, lutPart++)
{
float scale = lutPart->value + lutPart->delta * interp;
for (c = 0; c < numChannels; c++)
samples[c] += y[i * numChannels + c] * scale;
}
for (c = 0; c < numChannels; c++)
{
float r = roundf(samples[c] + dither[c]);
dither[c] += samples[c] - r;
if (r > 32767)
*sampleOut++ = 32767;
else if (r < -32768)
*sampleOut++ = -32768;
else
*sampleOut++ = r;
}
subpos += inFreq;
while (subpos >= outFreq)
{
subpos -= outFreq;
for (c = 0; c < numChannels; c++)
y[next * numChannels + c] = (sampleIn < wavInEnd) ? *sampleIn++ : 0;
next = (next + 1) % windowSize;
}
}
}
void sinc_resample(short *wavOut, int sizeOut, int outFreq, const short *wavIn, int sizeIn, int inFreq, int numChannels)
{
double sidelobeHeight = SIDELOBE_HEIGHT;
double transitionWidth;
double beta = 0.0;
int cutoffFreq2;
int windowSize;
// Just copy if no resampling necessary
if (outFreq == inFreq)
{
memcpy(wavOut, wavIn, (sizeOut < sizeIn) ? sizeOut : sizeIn);
return;
}
transitionWidth = (outFreq > inFreq) ? UP_TRANSITION_WIDTH : DOWN_TRANSITION_WIDTH;
// cutoff freq is ideally half transition width away from output freq
cutoffFreq2 = outFreq - transitionWidth * inFreq * 0.5;
// FIXME: Figure out why there are bad effects with cutoffFreq2 > inFreq
if (cutoffFreq2 > inFreq)
cutoffFreq2 = inFreq;
// https://www.mathworks.com/help/signal/ug/kaiser-window.html
if (sidelobeHeight > 50)
beta = 0.1102 * (sidelobeHeight - 8.7);
else if (sidelobeHeight >= 21)
beta = 0.5842 * pow(sidelobeHeight - 21.0, 0.4) + 0.07886 * (sidelobeHeight - 21.0);
windowSize = (sidelobeHeight - 8.0) / (2.285 * transitionWidth * M_PI) + 1;
if (windowSize > MAX_SINC_WINDOW_SIZE)
windowSize = MAX_SINC_WINDOW_SIZE;
// should compile as different paths
// number of channels need to be compiled as separate paths to ensure good
// vectorization by the compiler
if (numChannels == 1)
sinc_resample_internal(wavOut, sizeOut, outFreq, wavIn, sizeIn, inFreq, cutoffFreq2, 1, windowSize, beta);
else if (numChannels == 2)
sinc_resample_internal(wavOut, sizeOut, outFreq, wavIn, sizeIn, inFreq, cutoffFreq2, 2, windowSize, beta);
else
sinc_resample_internal(wavOut, sizeOut, outFreq, wavIn, sizeIn, inFreq, cutoffFreq2, numChannels, windowSize, beta);
}
#endif // RESWEEP_IMPLEMENTATION