Skip to main content

sva_samples/
frames.rs

1// Concern: holds one buffer's short-time spectra as (frame,bin) complex planes | Non-concern: the transform itself (stft.rs), reading one (measure/spectrum.rs) | IO: (c, frame, bin) -> Complex
2
3/// `edited` is set by any `write`, and is what makes a later `istft` label measured.
4#[derive(Clone, Debug, PartialEq)]
5pub struct Frames {
6    pub rate: u32,
7    pub window: usize,
8    pub hop: usize,
9    pub width: usize,
10    pub frames: usize,
11    pub bins: usize,
12    pub start: i64,
13    pub samples: usize,
14    re: Vec<f64>,
15    im: Vec<f64>,
16    pub edited: bool,
17}
18
19impl Frames {
20    pub fn silence(
21        rate: u32,
22        window: usize,
23        hop: usize,
24        width: usize,
25        frames: usize,
26        samples: usize,
27    ) -> Frames {
28        let bins = window / 2 + 1;
29        Frames {
30            rate,
31            window,
32            hop,
33            width,
34            frames,
35            bins,
36            start: 0,
37            samples,
38            re: vec![0.0; width * frames * bins],
39            im: vec![0.0; width * frames * bins],
40            edited: false,
41        }
42    }
43
44    fn slot(&self, c: usize, frame: usize, bin: usize) -> usize {
45        (c * self.frames + frame) * self.bins + bin
46    }
47
48    pub fn at(&self, c: usize, frame: usize, bin: usize) -> (f64, f64) {
49        let k = self.slot(c, frame, bin);
50        (self.re[k], self.im[k])
51    }
52
53    pub fn write(&mut self, c: usize, frame: usize, bin: usize, re: f64, im: f64) {
54        let k = self.slot(c, frame, bin);
55        self.re[k] = re;
56        self.im[k] = im;
57        self.edited = true;
58    }
59
60    pub(crate) fn place(&mut self, c: usize, frame: usize, bin: usize, re: f64, im: f64) {
61        let k = self.slot(c, frame, bin);
62        self.re[k] = re;
63        self.im[k] = im;
64    }
65}