resampler 0.5.1

A small audio resampling library
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use crate::fft::Complex32;

#[cfg(all(
    target_arch = "x86_64",
    any(
        all(target_feature = "avx", target_feature = "fma"),
        not(feature = "no_std")
    )
))]
mod avx;

#[cfg(all(
    target_arch = "x86_64",
    target_feature = "sse2",
    any(
        test,
        not(feature = "no_std"),
        not(all(feature = "no_std", target_feature = "avx", target_feature = "fma"))
    ),
))]
mod sse2;

#[cfg(all(
    target_arch = "aarch64",
    any(not(feature = "no_std"), target_feature = "neon")
))]
mod neon;

/// Scalar implementation of postprocess_fft (fallback).
#[cfg(any(
    test,
    all(
        not(all(target_arch = "x86_64", target_feature = "sse2")),
        not(target_arch = "aarch64")
    )
))]
pub(crate) fn postprocess_fft_scalar(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    let iter_count = output_left_middle
        .len()
        .min(output_right_middle.len())
        .min(twiddles.len());

    let right_len = output_right_middle.len();
    for i in 0..iter_count {
        let out = output_left_middle[i];
        let out_rev_idx = right_len - 1 - i;
        let out_rev = output_right_middle[out_rev_idx];
        let twiddle = twiddles[i];

        let sum = out.add(&out_rev);
        let diff = out.sub(&out_rev);

        let twiddled_re_sum = Complex32::new(sum.re * twiddle.re, sum.im * twiddle.re);
        let twiddled_im_sum = Complex32::new(sum.re * twiddle.im, sum.im * twiddle.im);
        let twiddled_re_diff = Complex32::new(diff.re * twiddle.re, diff.im * twiddle.re);
        let twiddled_im_diff = Complex32::new(diff.re * twiddle.im, diff.im * twiddle.im);

        let half = 0.5;
        let half_sum_real = half * sum.re;
        let half_diff_imaginary = half * diff.im;

        let real = twiddled_re_sum.im + twiddled_im_diff.re;
        let imaginary = twiddled_im_sum.im - twiddled_re_diff.re;

        output_left_middle[i] =
            Complex32::new(half_sum_real + real, half_diff_imaginary + imaginary);
        output_right_middle[out_rev_idx] =
            Complex32::new(half_sum_real - real, imaginary - half_diff_imaginary);
    }
}

/// Scalar implementation of preprocess_ifft (fallback)
#[cfg(any(
    test,
    all(
        not(all(target_arch = "x86_64", target_feature = "sse2")),
        not(target_arch = "aarch64")
    )
))]
pub(crate) fn preprocess_ifft_scalar(
    input_left_middle: &mut [Complex32],
    input_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    let iter_count = input_left_middle
        .len()
        .min(input_right_middle.len())
        .min(twiddles.len());

    for i in 0..iter_count {
        let inp = input_left_middle[i];
        let inp_rev_idx = input_right_middle.len() - 1 - i;
        let inp_rev = input_right_middle[inp_rev_idx];
        let twiddle = twiddles[i];

        let sum = inp.add(&inp_rev);
        let diff = inp.sub(&inp_rev);

        let twiddled_re_sum = Complex32::new(sum.re * twiddle.re, sum.im * twiddle.re);
        let twiddled_im_sum = Complex32::new(sum.re * twiddle.im, sum.im * twiddle.im);
        let twiddled_re_diff = Complex32::new(diff.re * twiddle.re, diff.im * twiddle.re);
        let twiddled_im_diff = Complex32::new(diff.re * twiddle.im, diff.im * twiddle.im);

        let real = twiddled_re_sum.im + twiddled_im_diff.re;
        let imaginary = twiddled_im_sum.im - twiddled_re_diff.re;

        input_left_middle[i] = Complex32::new(sum.re - real, diff.im - imaginary);
        input_right_middle[inp_rev_idx] = Complex32::new(sum.re + real, -imaginary - diff.im);
    }
}

#[cfg(all(
    target_arch = "x86_64",
    any(
        all(target_feature = "avx", target_feature = "fma"),
        not(feature = "no_std")
    )
))]
pub(crate) fn postprocess_fft_avx_fma_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { avx::postprocess_fft_avx_fma(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(all(
    target_arch = "x86_64",
    any(
        all(target_feature = "avx", target_feature = "fma"),
        not(feature = "no_std")
    )
))]
pub(crate) fn preprocess_ifft_avx_fma_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { avx::preprocess_ifft_avx_fma(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(all(
    target_arch = "x86_64",
    any(
        test,
        not(feature = "no_std"),
        all(feature = "no_std", not(target_feature = "avx"))
    )
))]
pub(crate) fn postprocess_fft_sse2_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { sse2::postprocess_fft_sse2(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(all(
    target_arch = "x86_64",
    any(
        test,
        not(feature = "no_std"),
        all(feature = "no_std", not(target_feature = "avx"))
    )
))]
pub(crate) fn preprocess_ifft_sse2_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { sse2::preprocess_ifft_sse2(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(target_arch = "aarch64")]
pub(crate) fn postprocess_fft_neon_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { neon::postprocess_fft_neon(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(target_arch = "aarch64")]
pub(crate) fn preprocess_ifft_neon_wrapper(
    output_left_middle: &mut [Complex32],
    output_right_middle: &mut [Complex32],
    twiddles: &[Complex32],
) {
    unsafe { neon::preprocess_ifft_neon(output_left_middle, output_right_middle, twiddles) }
}

#[cfg(any(not(target_arch = "x86_64"), feature = "no_std"))]
pub(crate) fn select_postprocess_fn() -> fn(&mut [Complex32], &mut [Complex32], &[Complex32]) {
    #[cfg(all(target_arch = "x86_64", target_feature = "avx", target_feature = "fma"))]
    {
        postprocess_fft_avx_fma_wrapper
    }

    #[cfg(all(
        target_arch = "x86_64",
        target_feature = "avx",
        not(target_feature = "fma")
    ))]
    {
        postprocess_fft_avx_wrapper
    }

    #[cfg(all(target_arch = "x86_64", not(target_feature = "avx")))]
    {
        postprocess_fft_sse2_wrapper
    }

    #[cfg(target_arch = "aarch64")]
    {
        postprocess_fft_neon_wrapper
    }

    #[cfg(all(not(target_arch = "x86_64"), not(target_arch = "aarch64")))]
    postprocess_fft_scalar
}

#[cfg(any(not(target_arch = "x86_64"), feature = "no_std"))]
pub(crate) fn select_preprocess_fn() -> fn(&mut [Complex32], &mut [Complex32], &[Complex32]) {
    #[cfg(all(target_arch = "x86_64", target_feature = "avx", target_feature = "fma"))]
    {
        preprocess_ifft_avx_fma_wrapper
    }

    #[cfg(all(target_arch = "x86_64", not(target_feature = "avx")))]
    {
        preprocess_ifft_sse2_wrapper
    }

    #[cfg(target_arch = "aarch64")]
    {
        preprocess_ifft_neon_wrapper
    }

    #[cfg(all(not(target_arch = "x86_64"), not(target_arch = "aarch64")))]
    preprocess_ifft_scalar
}

#[cfg(test)]
mod tests {
    use alloc::{format, vec};
    use core::f32;

    use super::*;

    fn approx_eq_complex(a: &Complex32, b: &Complex32, epsilon: f32) -> bool {
        (a.re - b.re).abs() < epsilon && (a.im - b.im).abs() < epsilon
    }

    fn assert_complex_arrays_approx_eq(
        actual: &[Complex32],
        expected: &[Complex32],
        epsilon: f32,
        context: &str,
    ) {
        assert_eq!(
            actual.len(),
            expected.len(),
            "{context}: Array lengths differ"
        );

        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_complex(a, e, epsilon),
                "{context}: Mismatch at index {i}: actual = ({}, {}), expected = ({}, {}), diff = ({}, {})",
                a.re,
                a.im,
                e.re,
                e.im,
                (a.re - e.re).abs(),
                (a.im - e.im).abs()
            );
        }
    }

    fn test_postprocess_against_scalar(
        scalar_fn: fn(&mut [Complex32], &mut [Complex32], &[Complex32]),
        simd_fn: fn(&mut [Complex32], &mut [Complex32], &[Complex32]),
        test_name: &str,
    ) {
        let test_sizes = vec![
            1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 127, 128,
        ];

        for middle_size in test_sizes {
            // Create test data
            let mut scalar_left = vec![Complex32::zero(); middle_size];
            let mut scalar_right = vec![Complex32::zero(); middle_size];
            let mut simd_left = vec![Complex32::zero(); middle_size];
            let mut simd_right = vec![Complex32::zero(); middle_size];

            for i in 0..middle_size {
                #[cfg(not(feature = "no_std"))]
                let val_left = Complex32::new((i as f32 * 0.5).sin(), (i as f32 * 0.3).cos());
                #[cfg(feature = "no_std")]
                let val_left =
                    Complex32::new(libm::sinf(i as f32 * 0.5), libm::cosf(i as f32 * 0.3));

                #[cfg(not(feature = "no_std"))]
                let val_right = Complex32::new((i as f32 * 0.7).cos(), (i as f32 * 0.4).sin());
                #[cfg(feature = "no_std")]
                let val_right =
                    Complex32::new(libm::cosf(i as f32 * 0.7), libm::sinf(i as f32 * 0.4));

                scalar_left[i] = val_left;
                scalar_right[i] = val_right;
                simd_left[i] = val_left;
                simd_right[i] = val_right;
            }

            // Create twiddle factors
            let mut twiddles = vec![Complex32::zero(); middle_size];
            for i in 0..middle_size {
                let angle = 2.0 * f32::consts::PI * (i as f32) / ((middle_size + 1) as f32);
                #[cfg(not(feature = "no_std"))]
                let tw = Complex32::new(angle.cos(), angle.sin());
                #[cfg(feature = "no_std")]
                let tw = Complex32::new(libm::cosf(angle), libm::sinf(angle));
                twiddles[i] = tw;
            }

            scalar_fn(&mut scalar_left, &mut scalar_right, &twiddles);
            simd_fn(&mut simd_left, &mut simd_right, &twiddles);

            let context_left = format!("{test_name} - left channel with size {middle_size}");
            let context_right = format!("{test_name} - right channel with size {middle_size}");

            assert_complex_arrays_approx_eq(&simd_left, &scalar_left, 1e-6, &context_left);
            assert_complex_arrays_approx_eq(&simd_right, &scalar_right, 1e-6, &context_right);
        }
    }

    fn test_preprocess_against_scalar(
        scalar_fn: fn(&mut [Complex32], &mut [Complex32], &[Complex32]),
        simd_fn: fn(&mut [Complex32], &mut [Complex32], &[Complex32]),
        test_name: &str,
    ) {
        let test_sizes = vec![
            1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 127, 128,
        ];

        for middle_size in test_sizes {
            // Create test data
            let mut scalar_left = vec![Complex32::zero(); middle_size];
            let mut scalar_right = vec![Complex32::zero(); middle_size];
            let mut simd_left = vec![Complex32::zero(); middle_size];
            let mut simd_right = vec![Complex32::zero(); middle_size];

            for i in 0..middle_size {
                #[cfg(not(feature = "no_std"))]
                let val_left = Complex32::new((i as f32 * 0.5).sin(), (i as f32 * 0.3).cos());
                #[cfg(feature = "no_std")]
                let val_left =
                    Complex32::new(libm::sinf(i as f32 * 0.5), libm::cosf(i as f32 * 0.3));

                #[cfg(not(feature = "no_std"))]
                let val_right = Complex32::new((i as f32 * 0.7).cos(), (i as f32 * 0.4).sin());
                #[cfg(feature = "no_std")]
                let val_right =
                    Complex32::new(libm::cosf(i as f32 * 0.7), libm::sinf(i as f32 * 0.4));

                scalar_left[i] = val_left;
                scalar_right[i] = val_right;
                simd_left[i] = val_left;
                simd_right[i] = val_right;
            }

            let mut twiddles = vec![Complex32::zero(); middle_size];
            for i in 0..middle_size {
                let angle = 2.0 * f32::consts::PI * (i as f32) / ((middle_size + 1) as f32);
                #[cfg(not(feature = "no_std"))]
                let tw = Complex32::new(angle.cos(), angle.sin());
                #[cfg(feature = "no_std")]
                let tw = Complex32::new(libm::cosf(angle), libm::sinf(angle));
                twiddles[i] = tw;
            }

            scalar_fn(&mut scalar_left, &mut scalar_right, &twiddles);
            simd_fn(&mut simd_left, &mut simd_right, &twiddles);

            let context_left = format!("{test_name} - left channel with size {middle_size}");
            let context_right = format!("{test_name} - right channel with size {middle_size}");

            assert_complex_arrays_approx_eq(&simd_left, &scalar_left, 1e-6, &context_left);
            assert_complex_arrays_approx_eq(&simd_right, &scalar_right, 1e-6, &context_right);
        }
    }

    #[test]
    #[cfg(all(
        target_arch = "x86_64",
        any(not(feature = "no_std"), target_feature = "avx")
    ))]
    fn test_preprocess_ifft_avx_fma_vs_scalar() {
        test_preprocess_against_scalar(
            preprocess_ifft_scalar,
            preprocess_ifft_avx_fma_wrapper,
            "preprocess_ifft AVX FMA",
        );
    }

    #[test]
    #[cfg(all(
        target_arch = "x86_64",
        any(
            not(feature = "no_std"),
            all(target_feature = "avx", target_feature = "fma")
        )
    ))]
    fn test_postprocess_fft_avx_fma_vs_scalar() {
        test_postprocess_against_scalar(
            postprocess_fft_scalar,
            postprocess_fft_avx_fma_wrapper,
            "postprocess_fft AVX FMA",
        );
    }

    #[test]
    #[cfg(all(
        target_arch = "x86_64",
        any(not(feature = "no_std"), target_feature = "sse2")
    ))]
    fn test_postprocess_fft_sse2_vs_scalar() {
        test_postprocess_against_scalar(
            postprocess_fft_scalar,
            postprocess_fft_sse2_wrapper,
            "postprocess_fft SSE2",
        );
    }

    #[test]
    #[cfg(all(
        target_arch = "x86_64",
        any(not(feature = "no_std"), target_feature = "sse2")
    ))]
    fn test_preprocess_ifft_sse2_vs_scalar() {
        test_preprocess_against_scalar(
            preprocess_ifft_scalar,
            preprocess_ifft_sse2_wrapper,
            "preprocess_ifft SSE2",
        );
    }

    #[test]
    #[cfg(target_arch = "aarch64")]
    fn test_postprocess_fft_neon_vs_scalar() {
        test_postprocess_against_scalar(
            postprocess_fft_scalar,
            postprocess_fft_neon_wrapper,
            "postprocess_fft NEON",
        );
    }

    #[test]
    #[cfg(target_arch = "aarch64")]
    fn test_preprocess_ifft_neon_vs_scalar() {
        test_preprocess_against_scalar(
            preprocess_ifft_scalar,
            preprocess_ifft_neon_wrapper,
            "preprocess_ifft NEON",
        );
    }
}