forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

window.rs103 lines, 3.9 KiBHistoryRawDownload
1//! Vorbis windows (§4.3.1), including the asymmetric long-block shapes.
2//!
3//! A long block next to a short one does not use the long slope: it uses the
4//! *short* slope, centred where the short block's own slope will be, and is
5//! flat or zero either side of it. That is what makes the two windows still add
6//! to one across the overlap, and it is the whole reason a Vorbis decoder needs
7//! four long-window variants rather than one.
8
9/// The Vorbis slope of `n` samples: `sin(pi/2 * sin^2((i + 1/2)/n * pi/2))`,
10/// rising from 0 to 1.
11fn slope(n: usize) -> Vec<f32> {
12 (0..n)
13 .map(|i| {
14 let x = (i as f64 + 0.5) / n as f64 * std::f64::consts::FRAC_PI_2;
15 let s = x.sin();
16 (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32
17 })
18 .collect()
19}
20
21/// One window of `n` samples for a block whose neighbours are long or short.
22///
23/// `short` is the stream's short blocksize; it is what sets the slope width
24/// when a neighbour is short. For a short block itself both flags are true by
25/// construction (its slopes are already the narrow ones).
26pub fn build(n: usize, short: usize, previous_long: bool, next_long: bool) -> Vec<f32> {
27 let mut window = vec![0.0f32; n];
28 let (left_start, left_n) = match previous_long {
29 true => (0usize, n / 2),
30 false => (n / 4 - short / 4, short / 2),
31 };
32 let (right_start, right_n) = match next_long {
33 true => (n / 2, n / 2),
34 false => (n * 3 / 4 - short / 4, short / 2),
35 };
36 let left = slope(left_n);
37 let right = slope(right_n);
38 for (i, value) in left.iter().enumerate() {
39 window[left_start + i] = *value;
40 }
41 window[left_start + left_n..right_start].fill(1.0);
42 // The right slope is the left one read backwards, which is what makes the
43 // pair Princen-Bradley.
44 for i in 0..right_n {
45 window[right_start + i] = right[right_n - 1 - i];
46 }
47 window
48}
49
50/// The four long-block variants plus the short one, built once per stream.
51pub struct Windows {
52 short: Vec<f32>,
53 long: [Vec<f32>; 4],
54}
55
56impl Windows {
57 /// Build every window a stream with these blocksizes can ask for.
58 pub fn new(blocksize_0: usize, blocksize_1: usize) -> Windows {
59 Windows {
60 short: build(blocksize_0, blocksize_0, true, true),
61 long: [
62 build(blocksize_1, blocksize_0, false, false),
63 build(blocksize_1, blocksize_0, false, true),
64 build(blocksize_1, blocksize_0, true, false),
65 build(blocksize_1, blocksize_0, true, true),
66 ],
67 }
68 }
69
70 /// The window for one block.
71 pub fn get(&self, long: bool, previous_long: bool, next_long: bool) -> &[f32] {
72 match long {
73 false => &self.short,
74 true => &self.long[usize::from(previous_long) * 2 + usize::from(next_long)],
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn overlapping_windows_add_to_one() {
85 // Long block against long block: the two slopes are the classic
86 // Princen-Bradley pair, so squares sum to one across the overlap.
87 let long = build(2048, 256, true, true);
88 for i in 0..1024 {
89 let sum = long[1024 + i] * long[1024 + i] + long[i] * long[i];
90 assert!((sum - 1.0).abs() < 1e-6, "{i}: {sum}");
91 }
92 // Long block against a short neighbour: the long window's right slope
93 // is the short window's own slope, sitting where the short block will
94 // put its left one, and is zero past it.
95 let to_short = build(2048, 256, true, false);
96 let short = build(256, 256, true, true);
97 assert!(to_short[2047] == 0.0 && to_short[1600] == 0.0);
98 for i in 0..128 {
99 let sum = to_short[1472 + i] * to_short[1472 + i] + short[i] * short[i];
100 assert!((sum - 1.0).abs() < 1e-6, "{i}: {sum}");
101 }
102 }
103}