-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathResampler.cs
311 lines (273 loc) · 12 KB
/
Resampler.cs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// -----------------------------------------------------------------------
// <copyright file="Resampler.cs" company="">
// Original C++ implementation by Lukas Lalinsky, http://acoustid.org/chromaprint
// </copyright>
// -----------------------------------------------------------------------
namespace AcoustID.Audio
{
using System;
/// <summary>
/// Audio resampling as implemented in FFmpeg.
/// </summary>
public class Resampler
{
struct ResampleContext
{
public short[] filter_bank;
public int filter_length;
public int ideal_dst_incr;
public int dst_incr;
public int index;
public int frac;
public int src_incr;
public int compensation_distance;
public int phase_shift;
public int phase_mask;
public bool linear;
}
static int FILTER_SHIFT = 15;
static int WINDOW_TYPE = 9;
ResampleContext ctx;
/// <summary>
/// Initialize the audio resampler.
/// </summary>
/// <param name="out_rate">Output sample rate</param>
/// <param name="in_rate">Input sample rate</param>
/// <param name="filter_size">Length of each FIR filter in the filterbank relative to the
/// cutoff freq</param>
/// <param name="phase_shift">Log2 of the number of entries in the polyphase filterbank</param>
/// <param name="linear">If true then the used FIR filter will be linearly interpolated between
/// the 2 closest, if false the closest will be used</param>
/// <param name="cutoff">Cutoff frequency, 1.0 corresponds to half the output sampling rate</param>
public void Init(int out_rate, int in_rate, int filter_size, int phase_shift,
bool linear, double cutoff)
{
ctx = default(ResampleContext);
double factor = Min(out_rate * cutoff / in_rate, 1.0);
int phase_count = 1 << phase_shift;
//if (!c)
// return NULL;
ctx.phase_shift = phase_shift;
ctx.phase_mask = phase_count - 1;
ctx.linear = linear;
ctx.filter_length = (int)Max((int)Math.Ceiling(filter_size / factor), 1);
ctx.filter_bank = new short[ctx.filter_length * (phase_count + 1)];
//if (!c.filter_bank)
// goto error;
BuildFilter(ctx.filter_bank, factor, ctx.filter_length, phase_count, 1 << FILTER_SHIFT, WINDOW_TYPE);
// goto error;
Array.Copy(ctx.filter_bank, 0, ctx.filter_bank, ctx.filter_length * phase_count + 1, ctx.filter_length - 1);
//memcpy(c.filter_bank[c.filter_length * phase_count + 1], c.filter_bank, (c.filter_length - 1) * sizeof(FELEM));
ctx.filter_bank[ctx.filter_length * phase_count] = ctx.filter_bank[ctx.filter_length - 1];
ctx.src_incr = out_rate;
ctx.ideal_dst_incr = ctx.dst_incr = in_rate * phase_count;
ctx.index = -phase_count * ((ctx.filter_length - 1) / 2);
}
/// <summary>
/// Resample an array of samples using a previously configured context.
/// </summary>
/// <param name="dst"></param>
/// <param name="src">Array of unconsumed samples </param>
/// <param name="consumed">Number of samples of src which have been consumed are returned here</param>
/// <param name="src_size">Number of unconsumed samples available </param>
/// <param name="dst_size">Amount of space in samples available in dst</param>
/// <param name="update_ctx">If this is false then the context will not be modified, that way several
/// channels can be resampled with the same context. </param>
/// <returns>Number of samples written in dst or -1 if an error occurred</returns>
public int Resample(short[] dst, short[] src, ref int consumed, int src_size, int dst_size, bool update_ctx)
{
int dst_index, i;
int index = ctx.index;
int frac = ctx.frac;
int dst_incr_frac = ctx.dst_incr % ctx.src_incr;
int dst_incr = ctx.dst_incr / ctx.src_incr;
int compensation_distance = ctx.compensation_distance;
if (compensation_distance == 0 && ctx.filter_length == 1 && ctx.phase_shift == 0)
{
long index2 = ((long)index) << 32;
long incr = (1L << 32) * ctx.dst_incr / ctx.src_incr;
dst_size = (int)Math.Min(dst_size, (src_size - 1 - index) * (long)ctx.src_incr / ctx.dst_incr);
for (dst_index = 0; dst_index < dst_size; dst_index++)
{
dst[dst_index] = src[index2 >> 32];
index2 += incr;
}
frac += dst_index * dst_incr_frac;
index += dst_index * dst_incr;
index += frac / ctx.src_incr;
frac %= ctx.src_incr;
}
else
{
for (dst_index = 0; dst_index < dst_size; dst_index++)
{
short[] filter = ctx.filter_bank;
int filter_offset = ctx.filter_length * (index & ctx.phase_mask);
int sample_index = index >> ctx.phase_shift;
int val = 0;
if (sample_index < 0)
{
for (i = 0; i < ctx.filter_length; i++)
val += src[Abs(sample_index + i) % src_size] * filter[filter_offset + i];
}
else if (sample_index + ctx.filter_length > src_size)
{
break;
}
else if (ctx.linear)
{
int v2 = 0;
for (i = 0; i < ctx.filter_length; i++)
{
val += src[sample_index + i] * (int)filter[filter_offset + i];
v2 += src[sample_index + i] * (int)filter[filter_offset + i + ctx.filter_length];
}
val += (int)((v2 - val) * (long)frac / ctx.src_incr);
}
else
{
for (i = 0; i < ctx.filter_length; i++)
{
val += src[sample_index + i] * (int)filter[filter_offset + i];
}
}
val = (val + (1 << (FILTER_SHIFT - 1))) >> FILTER_SHIFT;
dst[dst_index] = (short)((uint)(val + 32768) > 65535 ? (val >> 31) ^ 32767 : val);
frac += dst_incr_frac;
index += dst_incr;
if (frac >= ctx.src_incr)
{
frac -= ctx.src_incr;
index++;
}
if (dst_index + 1 == compensation_distance)
{
compensation_distance = 0;
dst_incr_frac = ctx.ideal_dst_incr % ctx.src_incr;
dst_incr = ctx.ideal_dst_incr / ctx.src_incr;
}
}
}
consumed = Math.Max(index, 0) >> ctx.phase_shift;
if (index >= 0) index &= ctx.phase_mask;
if (compensation_distance != 0)
{
compensation_distance -= dst_index;
// TODO: assert(compensation_distance > 0);
}
if (update_ctx)
{
ctx.frac = frac;
ctx.index = index;
ctx.dst_incr = dst_incr_frac + ctx.src_incr * dst_incr;
ctx.compensation_distance = compensation_distance;
}
return dst_index;
}
public void Close()
{
ctx.filter_bank = null;
}
private void Compensate(int sample_delta, int compensation_distance)
{
ctx.compensation_distance = compensation_distance;
ctx.dst_incr = ctx.ideal_dst_incr - (int)(ctx.ideal_dst_incr * (long)sample_delta / compensation_distance);
}
static int BuildFilter(short[] filter, double factor, int tap_count, int phase_count, int scale, int type)
{
int ph, i;
double x, y, w;
double[] tab = new double[tap_count];
int center = (tap_count - 1) / 2;
//if (!tab)
// return AVERROR(ENOMEM);
// if upsampling, only need to interpolate, no filter
if (factor > 1.0)
factor = 1.0;
for (ph = 0; ph < phase_count; ph++)
{
double norm = 0;
for (i = 0; i < tap_count; i++)
{
x = Math.PI * ((double)(i - center) - (double)ph / phase_count) * factor;
if (x == 0) y = 1.0;
else y = Math.Sin(x) / x;
switch (type)
{
case 0:
float d = -0.5f; //first order derivative = -0.5
x = Math.Abs(((double)(i - center) - (double)ph / phase_count) * factor);
if (x < 1.0) y = 1 - 3 * x * x + 2 * x * x * x + d * (-x * x + x * x * x);
else y = d * (-4 + 8 * x - 5 * x * x + x * x * x);
break;
case 1:
w = 2.0 * x / (factor * tap_count) + Math.PI;
y *= 0.3635819 - 0.4891775 * Math.Cos(w) + 0.1365995 * Math.Cos(2 * w) - 0.0106411 * Math.Cos(3 * w);
break;
default:
w = 2.0 * x / (factor * tap_count * Math.PI);
y *= Bessel(type * Math.Sqrt(Max(1 - w * w, 0)));
break;
}
tab[i] = y;
norm += y;
}
// normalize so that an uniform color remains the same
for (i = 0; i < tap_count; i++)
{
filter[ph * tap_count + i] = Clip(Floor(tab[i] * scale / norm), short.MinValue, short.MaxValue);
}
}
//av_free(tab);
return 0;
}
#region Math helper
static int Abs(int a)
{
return ((a) >= 0 ? (a) : (-(a)));
}
static int Sign(int a)
{
return ((a) > 0 ? 1 : -1);
}
static double Max(double a, double b)
{
return ((a) > (b) ? (a) : (b));
}
static double Min(double a, double b)
{
return ((a) > (b) ? (b) : (a));
}
static int Floor(double x)
{
return (int)Math.Floor(x + 0.5);
}
static short Clip(int a, short amin, short amax)
{
if (a < amin) return amin;
else if (a > amax) return amax;
else return (short)a; // TODO: casting to short ok?
}
/// <summary>
/// 0th order modified bessel function of the first kind.
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
static double Bessel(double x)
{
double v = 1;
double lastv = 0;
double t = 1;
int i;
x = x * x / 4;
for (i = 1; v != lastv; i++)
{
lastv = v;
t *= x / (i * i);
v += t;
}
return v;
}
#endregion
}
}