Skip to main content

bae_rs/generators/
mono_wav.rs

1//! # WAV
2//! 
3//! A wave data player.
4
5use super::*;
6use crate::utils::mono_resampler::*;
7use crate::sample_format::MonoTrackT;
8
9/// Struct for playing wave files.
10#[derive(Clone)]
11pub struct MonoWav {
12    resam: MonoResampler,
13}
14
15impl MonoWav {
16    /// Constructs from a given path.
17    pub fn from_source(s: &mut dyn std::io::Read) -> Self {
18        let (h, t) = utils::read_wav(s)
19        .expect("File could not be read");
20
21        let mut mt = MonoTrackT::new();
22
23        for s in t.first().expect("No audio data read") {
24            mt.push(Mono::from_sample(*s));
25        }
26
27        MonoWav::from_track(h.sampling_rate as MathT, mt)
28    }
29
30    /// Converts from the given track and source sample rate.
31    pub fn from_track(sampling_rate: MathT, t: MonoTrackT) -> Self {
32        MonoWav{
33            resam: MonoResampler::new(t, sampling_rate, 0, 0)
34        }
35    }
36
37    /// Borrows the resampler object.
38    pub fn get(&self) -> &MonoResampler {
39        &self.resam
40    }
41
42    /// Mutably borrows the resampler object.
43    pub fn get_mut(&mut self) -> &mut MonoResampler {
44        &mut self.resam
45    }
46}
47
48impl Generator for MonoWav {
49    fn process(&mut self) -> SampleT {
50        self.resam.process()
51    }
52}