Debian Codegraph @

src/pt2_downsample2x.c

raw symbols SCIP
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include "pt2_config.h" // config.maxSampleLength
6
7 #define NUM_TAPS 59 /* should be 4m+3 (3, 7, 11, 15, 19, ...) */
8 #define CENTER_TAP ((NUM_TAPS - 1) / 2)
9
10 // half-band FIR coeffs (Remez algorithm - numtaps=59, bands=[0.0, 0.2, 0.3, 0.5], desired=[1.0, 0.0])
11 #define C00 0.50000000000000f
12 #define C01 0.31679609962928f
13 #define C03 -0.10163877066856f
14 #define C05 0.05646939759172f
15 #define C07 -0.03589869172828f
16 #define C09 0.02384893442862f
17 #define C11 -0.01596102646846f
18 #define C13 0.01054794795196f
19 #define C15 -0.00678935474634f
20 #define C17 0.00420731862183f
21 #define C19 -0.00248066436637f
22 #define C21 0.00137207386220f
23 #define C23 -0.00069823637245f
24 #define C25 0.00031710491117f
25 #define C27 -0.00012143320790f
26 #define C29 0.00003501888526f
27
28 /* Python code for generating the Cxx coefficient constants:
29 **
30 ** import scipy
31 **
32 ** numtaps = 59 # should be 4m+3 (3, 7, 11, 15, 19, ...)
33 ** bands = [0.0, 0.2, 0.3, 0.5]
34 ** desired = [1.0, 0.0]
35 ** h = scipy.signal.remez(numtaps, bands, desired)
36 **
37 ** print('#define C00 % .14ff' % h[(numtaps-1)//2])
38 ** for i in range(1+(numtaps//4)):
39 ** print('#define C%02d % .14ff' % (1+(i*2), h[((numtaps-1)//2)+1+(i*2)]))
40 */
41
42 // ----------------------------------------------------------
43 // 2x downsampler for main audio mixer (simpler/faster, but has output sample delay)
44 // ----------------------------------------------------------
45
46 static float t01_L,t02_L,t03_L,t04_L,t05_L,t06_L,t07_L,t08_L,t09_L,t10_L,t11_L,t12_L,t13_L,t14_L;
47 static float t15_L,t16_L,t17_L,t18_L,t19_L,t20_L,t21_L,t22_L,t23_L,t24_L,t25_L,t26_L,t27_L,t28_L,t29_L;
48 static float t01_R,t02_R,t03_R,t04_R,t05_R,t06_R,t07_R,t08_R,t09_R,t10_R,t11_R,t12_R,t13_R,t14_R;
49 static float t15_R,t16_R,t17_R,t18_R,t19_R,t20_R,t21_R,t22_R,t23_R,t24_R,t25_R,t26_R,t27_R,t28_R,t29_R;
50
51 void clearDownsample2xStates(void)
52 {
53 t01_L=t02_L=t03_L=t04_L=t05_L=t06_L=t07_L=t08_L=t09_L=t10_L=t11_L=t12_L=t13_L=t14_L=
54 t15_L=t16_L=t17_L=t18_L=t19_L=t20_L=t21_L=t22_L=t23_L=t24_L=t25_L=t26_L=t27_L=t28_L=t29_L = 0.0f;
55
56 t01_R=t02_R=t03_R=t04_R=t05_R=t06_R=t07_R=t08_R=t09_R=t10_R=t11_R=t12_R=t13_R=t14_R=
57 t15_R=t16_R=t17_R=t18_R=t19_R=t20_R=t21_R=t22_R=t23_R=t24_R=t25_R=t26_R=t27_R=t28_R=t29_R = 0.0f;
58 }
59
60 float downsample2x_L(float sample1, float sample2)
61 {
62 const float x00 = sample2 * C00, x01 = sample1 * C01;
63 const float x03 = sample1 * C03, x05 = sample1 * C05;
64 const float x07 = sample1 * C07, x09 = sample1 * C09;
65 const float x11 = sample1 * C11, x13 = sample1 * C13;
66 const float x15 = sample1 * C15, x17 = sample1 * C17;
67 const float x19 = sample1 * C19, x21 = sample1 * C21;
68 const float x23 = sample1 * C23, x25 = sample1 * C25;
69 const float x27 = sample1 * C27, x29 = sample1 * C29;
70
71 const float out = t29_L + x29;
72
73 t29_L = t28_L + x27;
74 t28_L = t27_L + x25;
75 t27_L = t26_L + x23;
76 t26_L = t25_L + x21;
77 t25_L = t24_L + x19;
78 t24_L = t23_L + x17;
79 t23_L = t22_L + x15;
80 t22_L = t21_L + x13;
81 t21_L = t20_L + x11;
82 t20_L = t19_L + x09;
83 t19_L = t18_L + x07;
84 t18_L = t17_L + x05;
85 t17_L = t16_L + x03;
86 t16_L = t15_L + x01;
87 t15_L = t14_L + x01 + x00;
88 t14_L = t13_L + x03;
89 t13_L = t12_L + x05;
90 t12_L = t11_L + x07;
91 t11_L = t10_L + x09;
92 t10_L = t09_L + x11;
93 t09_L = t08_L + x13;
94 t08_L = t07_L + x15;
95 t07_L = t06_L + x17;
96 t06_L = t05_L + x19;
97 t05_L = t04_L + x21;
98 t04_L = t03_L + x23;
99 t03_L = t02_L + x25;
100 t02_L = t01_L + x27;
101 t01_L = x29;
102
103 return out;
104 }
105
106 float downsample2x_R(float sample1, float sample2)
107 {
108 const float x00 = sample2 * C00, x01 = sample1 * C01;
109 const float x03 = sample1 * C03, x05 = sample1 * C05;
110 const float x07 = sample1 * C07, x09 = sample1 * C09;
111 const float x11 = sample1 * C11, x13 = sample1 * C13;
112 const float x15 = sample1 * C15, x17 = sample1 * C17;
113 const float x19 = sample1 * C19, x21 = sample1 * C21;
114 const float x23 = sample1 * C23, x25 = sample1 * C25;
115 const float x27 = sample1 * C27, x29 = sample1 * C29;
116
117 const float out = t29_R + x29;
118
119 t29_R = t28_R + x27;
120 t28_R = t27_R + x25;
121 t27_R = t26_R + x23;
122 t26_R = t25_R + x21;
123 t25_R = t24_R + x19;
124 t24_R = t23_R + x17;
125 t23_R = t22_R + x15;
126 t22_R = t21_R + x13;
127 t21_R = t20_R + x11;
128 t20_R = t19_R + x09;
129 t19_R = t18_R + x07;
130 t18_R = t17_R + x05;
131 t17_R = t16_R + x03;
132 t16_R = t15_R + x01;
133 t15_R = t14_R + x01 + x00;
134 t14_R = t13_R + x03;
135 t13_R = t12_R + x05;
136 t12_R = t11_R + x07;
137 t11_R = t10_R + x09;
138 t10_R = t09_R + x11;
139 t09_R = t08_R + x13;
140 t08_R = t07_R + x15;
141 t07_R = t06_R + x17;
142 t06_R = t05_R + x19;
143 t05_R = t04_R + x21;
144 t04_R = t03_R + x23;
145 t03_R = t02_R + x25;
146 t02_R = t01_R + x27;
147 t01_R = x29;
148
149 return out;
150 }
151
152 // ----------------------------------------------------------
153 // 2x downsamplers for sample loaders
154 // ----------------------------------------------------------
155
156 static const float fHalfbandKernel[NUM_TAPS] =
157 {
158 C29, 0.0f,
159 C27, 0.0f,
160 C25, 0.0f,
161 C23, 0.0f,
162 C21, 0.0f,
163 C19, 0.0f,
164 C17, 0.0f,
165 C15, 0.0f,
166 C13, 0.0f,
167 C11, 0.0f,
168 C09, 0.0f,
169 C07, 0.0f,
170 C05, 0.0f,
171 C03, 0.0f,
172 C01, C00,
173 C01, 0.0f,
174 C03, 0.0f,
175 C05, 0.0f,
176 C07, 0.0f,
177 C09, 0.0f,
178 C11, 0.0f,
179 C13, 0.0f,
180 C15, 0.0f,
181 C17, 0.0f,
182 C19, 0.0f,
183 C21, 0.0f,
184 C23, 0.0f,
185 C25, 0.0f,
186 C27, 0.0f,
187 C29
188 };
189
190 static double dDownsample2x(double *fSamples, int32_t offset, int32_t sampleLength)
191 {
192 double dVal = 0.0;
193 for (int32_t i = 0; i < NUM_TAPS; i++)
194 {
195 const int32_t tapOffset = offset + (i - CENTER_TAP);
196
197 double dSmp;
198 if (tapOffset < 0)
199 dSmp = fSamples[0];
200 else if (tapOffset >= sampleLength)
201 dSmp = fSamples[sampleLength-1];
202 else
203 dSmp = fSamples[tapOffset];
204
205 dVal += dSmp * (double)fHalfbandKernel[i];
206 }
207
208 return dVal;
209 }
210
211 static float fDownsample2x(float *fSamples, int32_t offset, int32_t sampleLength)
212 {
213 float fVal = 0.0f;
214 for (int32_t i = 0; i < NUM_TAPS; i++)
215 {
216 const int32_t tapOffset = offset + (i - CENTER_TAP);
217
218 float fSmp;
219 if (tapOffset < 0)
220 fSmp = fSamples[0];
221 else if (tapOffset >= sampleLength)
222 fSmp = fSamples[sampleLength-1];
223 else
224 fSmp = fSamples[tapOffset];
225
226 fVal += fSmp * fHalfbandKernel[i];
227 }
228
229 return fVal;
230 }
231
232 static float fDownsample2x_U8(uint8_t *samplesU8, int32_t offset, int32_t sampleLength)
233 {
234 float fVal = 0.0f;
235 for (int32_t i = 0; i < NUM_TAPS; i++)
236 {
237 const int32_t tapOffset = offset + (i - CENTER_TAP);
238
239 float fSmp;
240 if (tapOffset < 0)
241 fSmp = samplesU8[0];
242 else if (tapOffset >= sampleLength)
243 fSmp = samplesU8[sampleLength-1];
244 else
245 fSmp = samplesU8[tapOffset];
246
247 fVal += (fSmp - 128.0f) * fHalfbandKernel[i];
248 }
249
250 return fVal;
251 }
252
253 static float fDownsample2x_S8(int8_t *samplesS8, int32_t offset, int32_t sampleLength)
254 {
255 float fVal = 0.0f;
256 for (int32_t i = 0; i < NUM_TAPS; i++)
257 {
258 const int32_t tapOffset = offset + (i - CENTER_TAP);
259
260 float fSmp;
261 if (tapOffset < 0)
262 fSmp = samplesS8[0];
263 else if (tapOffset >= sampleLength)
264 fSmp = samplesS8[sampleLength-1];
265 else
266 fSmp = samplesS8[tapOffset];
267
268 fVal += fSmp * fHalfbandKernel[i];
269 }
270
271 return fVal;
272 }
273
274 static float fDownsample2x_S16(int16_t *samplesS16, int32_t offset, int32_t sampleLength)
275 {
276 float fVal = 0.0f;
277 for (int32_t i = 0; i < NUM_TAPS; i++)
278 {
279 const int32_t tapOffset = offset + (i - CENTER_TAP);
280
281 float fSmp;
282 if (tapOffset < 0)
283 fSmp = samplesS16[0];
284 else if (tapOffset >= sampleLength)
285 fSmp = samplesS16[sampleLength-1];
286 else
287 fSmp = samplesS16[tapOffset];
288
289 fVal += fSmp * fHalfbandKernel[i];
290 }
291
292 return fVal;
293 }
294
295 static double dDownsample2x_S32(int32_t *samplesS32, int32_t offset, int32_t sampleLength)
296 {
297 double dVal = 0.0;
298 for (int32_t i = 0; i < NUM_TAPS; i++)
299 {
300 const int32_t tapOffset = offset + (i - CENTER_TAP);
301
302 double dSmp;
303 if (tapOffset < 0)
304 dSmp = samplesS32[0];
305 else if (tapOffset >= sampleLength)
306 dSmp = samplesS32[sampleLength-1];
307 else
308 dSmp = samplesS32[tapOffset];
309
310 dVal += dSmp * (double)fHalfbandKernel[i];
311 }
312
313 return dVal;
314 }
315
316 // Warning: These can exceed original range because of undershoot/overshoot!
317
318 void downsample2xDouble(double *buffer, uint32_t originalLength)
319 {
320 int32_t offset = 0;
321 for (uint32_t i = 0; i < originalLength / 2; i++, offset += 2)
322 buffer[i] = dDownsample2x(buffer, offset, originalLength);
323 }
324
325 void downsample2xFloat(float *buffer, uint32_t originalLength)
326 {
327 int32_t offset = 0;
328 for (uint32_t i = 0; i < originalLength / 2; i++, offset += 2)
329 buffer[i] = fDownsample2x(buffer, offset, originalLength);
330 }
331
332 // Warning: These are slow and use normalization to prevent clipping from undershoot/overshoot!
333
334 bool downsample2x8BitU(uint8_t *buffer, uint32_t originalLength)
335 {
336 uint32_t newLength = originalLength / 2;
337 if (newLength > (uint32_t)config.maxSampleLength)
338 newLength = config.maxSampleLength;
339
340 float *fBuffer = (float *)malloc(newLength * sizeof (float));
341 if (fBuffer == NULL)
342 return false;
343
344 float fPeak = 0.0f;
345
346 int32_t offset = 0;
347 for (uint32_t i = 0; i < newLength; i++, offset += 2)
348 {
349 fBuffer[i] = fDownsample2x_U8(buffer, offset, originalLength);
350
351 const float fAbsSmp = fabsf(fBuffer[i]);
352 if (fAbsSmp > fPeak)
353 fPeak = fAbsSmp;
354 }
355
356 // normalize
357
358 float fAmp = 1.0f;
359 if (fPeak > 0.0f)
360 fAmp = INT8_MAX / fPeak;
361
362 for (uint32_t i = 0; i < newLength; i++)
363 buffer[i] = (uint8_t)roundf(fBuffer[i] * fAmp) + 128;
364
365 free(fBuffer);
366 return true;
367 }
368
369 bool downsample2x8Bit(int8_t *buffer, uint32_t originalLength)
370 {
371 uint32_t newLength = originalLength / 2;
372 if (newLength > (uint32_t)config.maxSampleLength)
373 newLength = config.maxSampleLength;
374
375 float *fBuffer = (float *)malloc(newLength * sizeof (float));
376 if (fBuffer == NULL)
377 return false;
378
379 float fPeak = 0.0f;
380
381 int32_t offset = 0;
382 for (uint32_t i = 0; i < newLength; i++, offset += 2)
383 {
384 fBuffer[i] = fDownsample2x_S8(buffer, offset, originalLength);
385
386 const float fAbsSmp = fabsf(fBuffer[i]);
387 if (fAbsSmp > fPeak)
388 fPeak = fAbsSmp;
389 }
390
391 // normalize
392
393 float fAmp = 1.0f;
394 if (fPeak > 0.0f)
395 fAmp = INT8_MAX / fPeak;
396
397 for (uint32_t i = 0; i < newLength; i++)
398 buffer[i] = (int8_t)roundf(fBuffer[i] * fAmp);
399
400 free(fBuffer);
401 return true;
402 }
403
404 bool downsample2x16Bit(int16_t *buffer, uint32_t originalLength)
405 {
406 uint32_t newLength = originalLength / 2;
407
408 if (newLength > (uint32_t)config.maxSampleLength)
409 newLength = config.maxSampleLength;
410
411 float *fBuffer = (float *)malloc(newLength * sizeof (float));
412 if (fBuffer == NULL)
413 return false;
414
415 float fPeak = 0.0f;
416
417 int32_t offset = 0;
418 for (uint32_t i = 0; i < newLength; i++, offset += 2)
419 {
420 fBuffer[i] = fDownsample2x_S16(buffer, offset, originalLength);
421
422 const float fAbsSmp = fabsf(fBuffer[i]);
423 if (fAbsSmp > fPeak)
424 fPeak = fAbsSmp;
425 }
426
427 // normalize
428
429 float fAmp = 1.0f;
430 if (fPeak > 0.0f)
431 fAmp = INT16_MAX / fPeak;
432
433 for (uint32_t i = 0; i < newLength; i++)
434 buffer[i] = (int16_t)roundf(fBuffer[i] * fAmp);
435
436 free(fBuffer);
437 return true;
438 }
439
440 bool downsample2x32Bit(int32_t *buffer, uint32_t originalLength)
441 {
442 uint32_t newLength = originalLength / 2;
443 if (newLength > (uint32_t)config.maxSampleLength)
444 newLength = config.maxSampleLength;
445
446 double *dBuffer = (double *)malloc(newLength * sizeof (double));
447 if (dBuffer == NULL)
448 return false;
449
450 double dPeak = 0.0;
451
452 int32_t offset = 0;
453 for (uint32_t i = 0; i < newLength; i++, offset += 2)
454 {
455 dBuffer[i] = dDownsample2x_S32(buffer, offset, originalLength);
456
457 const double dAbsSmp = fabs(dBuffer[i]);
458 if (dAbsSmp > dPeak)
459 dPeak = dAbsSmp;
460 }
461
462 // normalize
463
464 double dAmp = 1.0;
465 if (dPeak > 0.0)
466 dAmp = INT32_MAX / dPeak;
467
468 for (uint32_t i = 0; i < newLength; i++)
469 buffer[i] = (int32_t)round(dBuffer[i] * dAmp);
470
471 free(dBuffer);
472 return true;
473 }