oximedia-audio 0.1.4

Audio codec implementations for OxiMedia
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! FFT implementation and window functions.

use oxifft::api::{Direction, Flags, Plan};
use oxifft::Complex;
use std::f64::consts::PI;

/// Window function type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum WindowFunction {
    /// Rectangular window (no windowing).
    Rectangle,
    /// Hann window (raised cosine).
    #[default]
    Hann,
    /// Hamming window.
    Hamming,
    /// Blackman window.
    Blackman,
    /// Blackman-Harris window.
    BlackmanHarris,
    /// Kaiser window.
    Kaiser(u32),
    /// Tukey window (tapered cosine).
    Tukey(u32),
    /// Bartlett window (triangular).
    Bartlett,
    /// Welch window.
    Welch,
    /// Flat-top window.
    FlatTop,
}

impl WindowFunction {
    /// Generate window coefficients.
    #[must_use]
    pub fn generate(&self, size: usize) -> Vec<f64> {
        match self {
            Self::Rectangle => vec![1.0; size],
            Self::Hann => Self::generate_hann(size),
            Self::Hamming => Self::generate_hamming(size),
            Self::Blackman => Self::generate_blackman(size),
            Self::BlackmanHarris => Self::generate_blackman_harris(size),
            Self::Kaiser(beta) => Self::generate_kaiser(size, f64::from(*beta)),
            Self::Tukey(alpha) => Self::generate_tukey(size, f64::from(*alpha) / 100.0),
            Self::Bartlett => Self::generate_bartlett(size),
            Self::Welch => Self::generate_welch(size),
            Self::FlatTop => Self::generate_flattop(size),
        }
    }

    /// Hann window: 0.5 * (1 - cos(2*pi*n/(N-1))).
    fn generate_hann(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        (0..size)
            .map(|i| {
                let n = i as f64;
                let n_max = (size - 1) as f64;
                0.5 * (1.0 - (2.0 * PI * n / n_max).cos())
            })
            .collect()
    }

    /// Hamming window: 0.54 - 0.46 * cos(2*pi*n/(N-1)).
    fn generate_hamming(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        (0..size)
            .map(|i| {
                let n = i as f64;
                let n_max = (size - 1) as f64;
                0.54 - 0.46 * (2.0 * PI * n / n_max).cos()
            })
            .collect()
    }

    /// Blackman window.
    fn generate_blackman(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        const A0: f64 = 0.42;
        const A1: f64 = 0.5;
        const A2: f64 = 0.08;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let n_max = (size - 1) as f64;
                A0 - A1 * (2.0 * PI * n / n_max).cos() + A2 * (4.0 * PI * n / n_max).cos()
            })
            .collect()
    }

    /// Blackman-Harris window.
    fn generate_blackman_harris(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        const A0: f64 = 0.35875;
        const A1: f64 = 0.48829;
        const A2: f64 = 0.14128;
        const A3: f64 = 0.01168;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let n_max = (size - 1) as f64;
                A0 - A1 * (2.0 * PI * n / n_max).cos() + A2 * (4.0 * PI * n / n_max).cos()
                    - A3 * (6.0 * PI * n / n_max).cos()
            })
            .collect()
    }

    /// Kaiser window.
    fn generate_kaiser(size: usize, beta: f64) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        let i0_beta = Self::bessel_i0(beta);
        let n_max = (size - 1) as f64;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let arg = beta * (1.0 - ((2.0 * n / n_max) - 1.0).powi(2)).sqrt();
                Self::bessel_i0(arg) / i0_beta
            })
            .collect()
    }

    /// Modified Bessel function of the first kind, order 0.
    fn bessel_i0(x: f64) -> f64 {
        let mut sum = 1.0;
        let mut term = 1.0;
        let x_half = x / 2.0;

        for k in 1..=50 {
            term *= (x_half / k as f64).powi(2);
            sum += term;
            if term < 1e-12 * sum {
                break;
            }
        }

        sum
    }

    /// Tukey window (tapered cosine).
    fn generate_tukey(size: usize, alpha: f64) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        let alpha = alpha.clamp(0.0, 1.0);
        let n_max = (size - 1) as f64;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let ratio = n / n_max;

                if ratio < alpha / 2.0 {
                    0.5 * (1.0 + (2.0 * PI * ratio / alpha - PI).cos())
                } else if ratio > 1.0 - alpha / 2.0 {
                    0.5 * (1.0 + (2.0 * PI * (1.0 - ratio) / alpha - PI).cos())
                } else {
                    1.0
                }
            })
            .collect()
    }

    /// Bartlett window (triangular).
    fn generate_bartlett(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        let n_max = (size - 1) as f64;

        (0..size)
            .map(|i| {
                let n = i as f64;
                1.0 - ((n - n_max / 2.0) / (n_max / 2.0)).abs()
            })
            .collect()
    }

    /// Welch window.
    fn generate_welch(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        let n_max = (size - 1) as f64;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let ratio = (n - n_max / 2.0) / (n_max / 2.0);
                1.0 - ratio * ratio
            })
            .collect()
    }

    /// Flat-top window.
    fn generate_flattop(size: usize) -> Vec<f64> {
        if size == 0 {
            return Vec::new();
        }
        if size == 1 {
            return vec![1.0];
        }

        const A0: f64 = 0.21557895;
        const A1: f64 = 0.41663158;
        const A2: f64 = 0.277263158;
        const A3: f64 = 0.083578947;
        const A4: f64 = 0.006947368;

        (0..size)
            .map(|i| {
                let n = i as f64;
                let n_max = (size - 1) as f64;
                A0 - A1 * (2.0 * PI * n / n_max).cos() + A2 * (4.0 * PI * n / n_max).cos()
                    - A3 * (6.0 * PI * n / n_max).cos()
                    + A4 * (8.0 * PI * n / n_max).cos()
            })
            .collect()
    }

    /// Get the coherent gain of the window (sum of coefficients divided by size).
    #[must_use]
    pub fn coherent_gain(&self, size: usize) -> f64 {
        if size == 0 {
            return 0.0;
        }
        let window = self.generate(size);
        window.iter().sum::<f64>() / size as f64
    }

    /// Get the equivalent noise bandwidth factor.
    #[must_use]
    pub fn enbw(&self, size: usize) -> f64 {
        if size == 0 {
            return 0.0;
        }
        let window = self.generate(size);
        let sum_squares: f64 = window.iter().map(|&x| x * x).sum();
        let sum: f64 = window.iter().sum();
        size as f64 * sum_squares / (sum * sum)
    }
}

/// FFT processor.
pub struct FftProcessor {
    fft_size: usize,
    window: Vec<f64>,
}

impl FftProcessor {
    /// Create a new FFT processor.
    #[must_use]
    pub fn new(fft_size: usize, window_fn: WindowFunction) -> Self {
        Self {
            fft_size,
            window: window_fn.generate(fft_size),
        }
    }

    /// Get FFT size.
    #[must_use]
    pub const fn size(&self) -> usize {
        self.fft_size
    }

    /// Process audio samples and return frequency domain representation.
    pub fn process(&mut self, samples: &[f64]) -> Vec<Complex<f64>> {
        // Apply zero-padding if needed
        let input_size = samples.len().min(self.fft_size);

        // Apply window and convert to complex
        let buffer: Vec<Complex<f64>> = (0..self.fft_size)
            .map(|i| {
                if i < input_size {
                    Complex::new(samples[i] * self.window[i], 0.0)
                } else {
                    Complex::new(0.0, 0.0)
                }
            })
            .collect();

        // Perform FFT using OxiFFT plan API
        let mut output = vec![Complex::zero(); self.fft_size];
        if let Some(plan) = Plan::dft_1d(self.fft_size, Direction::Forward, Flags::ESTIMATE) {
            plan.execute(&buffer, &mut output);
        }

        output
    }

    /// Process and return magnitude spectrum.
    pub fn magnitude_spectrum(&mut self, samples: &[f64]) -> Vec<f64> {
        let spectrum = self.process(samples);
        spectrum.iter().map(|c| c.norm()).collect()
    }

    /// Process and return power spectrum (magnitude squared).
    pub fn power_spectrum(&mut self, samples: &[f64]) -> Vec<f64> {
        let spectrum = self.process(samples);
        spectrum.iter().map(|c| c.norm_sqr()).collect()
    }

    /// Process and return phase spectrum.
    pub fn phase_spectrum(&mut self, samples: &[f64]) -> Vec<f64> {
        let spectrum = self.process(samples);
        spectrum.iter().map(|c| c.arg()).collect()
    }

    /// Convert magnitude to decibels.
    #[must_use]
    pub fn to_db(magnitude: f64, reference: f64) -> f64 {
        if magnitude <= 0.0 {
            -100.0
        } else {
            20.0 * (magnitude / reference).log10()
        }
    }

    /// Convert power to decibels.
    #[must_use]
    pub fn power_to_db(power: f64, reference: f64) -> f64 {
        if power <= 0.0 {
            -100.0
        } else {
            10.0 * (power / reference).log10()
        }
    }

    /// Get frequency bin for a given index.
    #[must_use]
    pub fn bin_frequency(&self, bin: usize, sample_rate: f64) -> f64 {
        bin as f64 * sample_rate / self.fft_size as f64
    }

    /// Get bin index for a given frequency.
    #[must_use]
    pub fn frequency_to_bin(&self, frequency: f64, sample_rate: f64) -> usize {
        ((frequency * self.fft_size as f64) / sample_rate)
            .round()
            .max(0.0) as usize
    }

    /// Get the Nyquist bin index.
    #[must_use]
    pub const fn nyquist_bin(&self) -> usize {
        self.fft_size / 2
    }
}

/// Mel scale conversion utilities.
pub struct MelScale;

impl MelScale {
    /// Convert frequency (Hz) to mel scale.
    #[must_use]
    pub fn hz_to_mel(hz: f64) -> f64 {
        2595.0 * (1.0 + hz / 700.0).log10()
    }

    /// Convert mel scale to frequency (Hz).
    #[must_use]
    pub fn mel_to_hz(mel: f64) -> f64 {
        700.0 * (10.0_f64.powf(mel / 2595.0) - 1.0)
    }

    /// Create mel filterbank.
    #[must_use]
    pub fn create_filterbank(
        num_filters: usize,
        fft_size: usize,
        sample_rate: f64,
        min_freq: f64,
        max_freq: f64,
    ) -> Vec<Vec<f64>> {
        let min_mel = Self::hz_to_mel(min_freq);
        let max_mel = Self::hz_to_mel(max_freq);

        // Create equally spaced mel points
        let mel_points: Vec<f64> = (0..=num_filters + 1)
            .map(|i| min_mel + (max_mel - min_mel) * i as f64 / (num_filters + 1) as f64)
            .collect();

        // Convert back to Hz
        let hz_points: Vec<f64> = mel_points.iter().map(|&m| Self::mel_to_hz(m)).collect();

        // Convert to FFT bin numbers
        let bin_points: Vec<usize> = hz_points
            .iter()
            .map(|&f| ((fft_size + 1) as f64 * f / sample_rate).floor() as usize)
            .collect();

        // Create filterbank
        let mut filters = Vec::new();

        for i in 0..num_filters {
            let mut filter = vec![0.0; fft_size / 2 + 1];

            let start = bin_points[i];
            let center = bin_points[i + 1];
            let end = bin_points[i + 2];

            // Rising slope
            for k in start..center {
                if center > start {
                    filter[k] = (k - start) as f64 / (center - start) as f64;
                }
            }

            // Falling slope
            for k in center..end {
                if end > center {
                    filter[k] = (end - k) as f64 / (end - center) as f64;
                }
            }

            filters.push(filter);
        }

        filters
    }

    /// Apply mel filterbank to power spectrum.
    #[must_use]
    pub fn apply_filterbank(spectrum: &[f64], filterbank: &[Vec<f64>]) -> Vec<f64> {
        filterbank
            .iter()
            .map(|filter| {
                spectrum
                    .iter()
                    .zip(filter.iter())
                    .map(|(&s, &f)| s * f)
                    .sum()
            })
            .collect()
    }

    /// Compute Mel Frequency Cepstral Coefficients (MFCC).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn compute_mfcc(mel_spectrum: &[f64], num_coeffs: usize) -> Vec<f64> {
        let n = mel_spectrum.len();

        (0..num_coeffs)
            .map(|i| {
                mel_spectrum
                    .iter()
                    .enumerate()
                    .map(|(k, &val)| {
                        let log_val = if val > 0.0 { val.ln() } else { -100.0 };
                        log_val * (PI * i as f64 * (k as f64 + 0.5) / n as f64).cos()
                    })
                    .sum()
            })
            .collect()
    }
}

/// Overlap-add processing for streaming FFT.
pub struct OverlapAdd {
    fft_size: usize,
    hop_size: usize,
    buffer: Vec<f64>,
}

impl OverlapAdd {
    /// Create a new overlap-add processor.
    #[must_use]
    pub fn new(fft_size: usize, hop_size: usize) -> Self {
        Self {
            fft_size,
            hop_size,
            buffer: Vec::new(),
        }
    }

    /// Add samples to buffer and return available frames.
    pub fn push(&mut self, samples: &[f64]) -> Vec<Vec<f64>> {
        self.buffer.extend_from_slice(samples);

        let mut frames = Vec::new();
        while self.buffer.len() >= self.fft_size {
            frames.push(self.buffer[..self.fft_size].to_vec());
            self.buffer.drain(..self.hop_size);
        }

        frames
    }

    /// Get remaining buffered samples.
    #[must_use]
    pub fn remaining(&self) -> &[f64] {
        &self.buffer
    }

    /// Clear the buffer.
    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    /// Get buffer length.
    #[must_use]
    pub fn buffer_len(&self) -> usize {
        self.buffer.len()
    }
}