Skip to main content

sva_samples/
buffer.rs

1// Concern: holds one collapse's samples as planar f64 components on a rate and origin | Non-concern: producing them (collapse.rs), measuring one (measure/) | IO: (component, index) -> f64
2
3use std::borrow::Cow;
4use std::ops::Range;
5
6#[derive(Clone, Debug, PartialEq)]
7pub struct Buffer {
8    pub rate: u32,
9    pub origin_secs: f64,
10    pub width: usize,
11    pub planes: Vec<Vec<f64>>,
12}
13
14impl Buffer {
15    pub fn silence(rate: u32, width: usize, len: usize) -> Buffer {
16        Buffer {
17            rate,
18            origin_secs: 0.0,
19            width,
20            planes: vec![vec![0.0; len]; width],
21        }
22    }
23
24    pub fn mono(rate: u32, samples: Vec<f64>) -> Buffer {
25        Buffer {
26            rate,
27            origin_secs: 0.0,
28            width: 1,
29            planes: vec![samples],
30        }
31    }
32
33    pub fn of_planes(rate: u32, planes: Vec<Vec<f64>>) -> Buffer {
34        let len = planes.iter().map(Vec::len).min().unwrap_or(0);
35        Buffer {
36            rate,
37            origin_secs: 0.0,
38            width: planes.len(),
39            planes: planes
40                .into_iter()
41                .map(|mut p| {
42                    p.truncate(len);
43                    p
44                })
45                .collect(),
46        }
47    }
48
49    pub fn plane(&self, c: usize) -> &[f64] {
50        &self.planes[c]
51    }
52
53    pub fn plane_mut(&mut self, c: usize) -> &mut [f64] {
54        &mut self.planes[c]
55    }
56
57    pub fn len(&self) -> usize {
58        self.planes.iter().map(Vec::len).min().unwrap_or(0)
59    }
60
61    pub fn is_empty(&self) -> bool {
62        self.len() == 0
63    }
64
65    pub fn at(&self, c: usize, i: usize) -> f64 {
66        self.planes[c][i]
67    }
68
69    pub fn window(&self, c: usize, range: Range<usize>) -> Cow<'_, [f64]> {
70        let plane = self.plane(c);
71        if range.end <= plane.len() {
72            return Cow::Borrowed(&plane[range]);
73        }
74        let mut out = vec![0.0; range.len()];
75        let held = plane.len().clamp(range.start, range.end) - range.start;
76        out[..held].copy_from_slice(&plane[range.start.min(plane.len())..][..held]);
77        Cow::Owned(out)
78    }
79
80    /// The only narrowing in the crate: a WAV file and the wasm boundary are both f32.
81    pub fn as_f32(&self, c: usize) -> Vec<f32> {
82        self.plane(c).iter().map(|&x| x as f32).collect()
83    }
84}