1use std::{fs::File, io::Read};
2
3use super::{format::{SampleParser, Signed16PCM}, stream_to_matrix, Matrix};
4
5pub struct FileSource {
6 file: File,
7 buffer: Vec<u8>,
8 channels: usize,
9 _sample_rate: usize,
10 _limit_rate: bool,
11 }
15
16impl FileSource {
17 #[allow(clippy::new_ret_no_self)]
18 pub fn new(path: &str, opts: &crate::cfg::SourceOptions, limit_rate: bool) -> Result<Box<dyn super::DataSource<f64>>, std::io::Error> {
19 Ok(Box::new(
20 FileSource {
21 channels: opts.channels,
22 _sample_rate: opts.sample_rate as usize,
23 _limit_rate: limit_rate,
24 file: File::open(path)?,
25 buffer: vec![0u8; opts.buffer as usize * opts.channels],
26 }
27 ))
28 }
29}
30
31impl super::DataSource<f64> for FileSource {
32 fn recv(&mut self) -> Option<Matrix<f64>> {
33 match self.file.read_exact(&mut self.buffer) {
34 Ok(()) => Some(
35 stream_to_matrix(
36 self.buffer.chunks(2).map(Signed16PCM::parse),
37 self.channels,
38 32768.0,
39 )
40 ),
41 Err(_e) => None, }
43 }
44}