| 1 |
use std::f64; |
| 2 |
|
| 3 |
use crate::DctNum; |
| 4 |
|
| 5 |
/// MP3 window function for MDCT |
| 6 |
pub fn mp3<T: DctNum>(len: usize) -> Vec<T> { |
| 7 |
let constant_term = f64::consts::PI / len as f64; |
| 8 |
|
| 9 |
(0..len) |
| 10 |
.map(|n| (constant_term * (n as f64 + 0.5f64)).sin()) |
| 11 |
.map(|w| T::from_f64(w).unwrap()) |
| 12 |
.collect() |
| 13 |
} |
| 14 |
|
| 15 |
/// MP3 window function for MDCT. Combines a scale for normalization into the window function so that the process is conveniently invertible. |
| 16 |
pub fn mp3_invertible<T: DctNum>(len: usize) -> Vec<T> { |
| 17 |
let outer_scale = (4.0 / len as f64).sqrt(); |
| 18 |
let constant_term = f64::consts::PI / len as f64; |
| 19 |
|
| 20 |
(0..len) |
| 21 |
.map(|n| (constant_term * (n as f64 + 0.5f64)).sin() * outer_scale) |
| 22 |
.map(|w| T::from_f64(w).unwrap()) |
| 23 |
.collect() |
| 24 |
} |
| 25 |
|
| 26 |
/// Ogg Vorbis window function for MDCT |
| 27 |
pub fn vorbis<T: DctNum>(len: usize) -> Vec<T> { |
| 28 |
let constant_term = f64::consts::PI / len as f64; |
| 29 |
|
| 30 |
(0..len) |
| 31 |
.map(|n| { |
| 32 |
let inner_sin = (constant_term * (n as f64 + 0.5f64)).sin(); |
| 33 |
|
| 34 |
(f64::consts::PI * 0.5f64 * inner_sin * inner_sin).sin() |
| 35 |
}) |
| 36 |
.map(|w| T::from_f64(w).unwrap()) |
| 37 |
.collect() |
| 38 |
} |
| 39 |
|
| 40 |
/// Ogg Vorbis window function for MDCT. Combines a scale for normalization into the window function so that the process is conveniently invertible. |
| 41 |
pub fn vorbis_invertible<T: DctNum>(len: usize) -> Vec<T> { |
| 42 |
let outer_scale = (4.0 / len as f64).sqrt(); |
| 43 |
let constant_term = f64::consts::PI / len as f64; |
| 44 |
|
| 45 |
(0..len) |
| 46 |
.map(|n| { |
| 47 |
let inner_sin = (constant_term * (n as f64 + 0.5f64)).sin(); |
| 48 |
|
| 49 |
(f64::consts::PI * 0.5f64 * inner_sin * inner_sin).sin() * outer_scale |
| 50 |
}) |
| 51 |
.map(|w| T::from_f64(w).unwrap()) |
| 52 |
.collect() |
| 53 |
} |
| 54 |
|
| 55 |
/// MDCT window function which is all ones (IE, no windowing will be applied) |
| 56 |
pub fn one<T: DctNum>(len: usize) -> Vec<T> { |
| 57 |
(0..len).map(|_| T::one()).collect() |
| 58 |
} |
| 59 |
|
| 60 |
/// MDCT window function which is all ones (IE, no windowing will be applied). Combines a scale for normalization into the window function so that the process is conveniently invertible. |
| 61 |
pub fn invertible<T: DctNum>(len: usize) -> Vec<T> { |
| 62 |
let constant_term = (2.0 / len as f64).sqrt(); |
| 63 |
(0..len) |
| 64 |
.map(|_| constant_term) |
| 65 |
.map(|w| T::from_f64(w).unwrap()) |
| 66 |
.collect() |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(test)] |
| 70 |
mod unit_tests { |
| 71 |
use super::*; |
| 72 |
use crate::test_utils::fuzzy_cmp; |
| 73 |
|
| 74 |
/// Verify that each of the built-in window functions does what we expect |
| 75 |
#[test] |
| 76 |
fn test_window_fns() { |
| 77 |
for test_fn in &[mp3, vorbis] { |
| 78 |
for half_size in 1..20 { |
| 79 |
let evaluated_window: Vec<f32> = test_fn(half_size * 2); |
| 80 |
|
| 81 |
//verify that for all i from 0 to half_size, window[i]^2 + window[i+half_size]^2 == 1 |
| 82 |
//also known as the "Princen-Bradley condition" |
| 83 |
for i in 0..half_size { |
| 84 |
let first = evaluated_window[i]; |
| 85 |
let second = evaluated_window[i + half_size]; |
| 86 |
assert!(fuzzy_cmp(first * first + second * second, 1f32, 0.001f32)); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |