1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use autd3_driver::derive::*;

use std::{
    fs::File,
    io::{BufReader, Read},
    path::{Path, PathBuf},
};

use crate::error::AudioFileError;

/// Modulation constructed from a raw PCM data
///
/// The raw PCM data must be 8bit unsigned integer.
///
/// The raw PCM data is resampled to the sampling frequency of Modulation.
#[derive(Modulation, Clone, PartialEq, Debug)]
pub struct RawPCM {
    sample_rate: u32,
    path: PathBuf,
    config: SamplingConfiguration,
    loop_behavior: LoopBehavior,
}

impl RawPCM {
    /// Constructor
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the raw PCM file
    /// * `sample_rate` - Sampling frequency of the raw PCM file
    ///
    pub fn new(path: impl AsRef<Path>, sample_rate: u32) -> Self {
        Self {
            sample_rate,
            path: path.as_ref().to_path_buf(),
            config: SamplingConfiguration::FREQ_4K_HZ,
            loop_behavior: LoopBehavior::Infinite,
        }
    }

    fn read_buf(&self) -> Result<Vec<f32>, AudioFileError> {
        let f = File::open(&self.path)?;
        let mut reader = BufReader::new(f);
        let mut raw_buffer = Vec::new();
        reader.read_to_end(&mut raw_buffer)?;
        Ok(raw_buffer.into_iter().map(f32::from).collect())
    }
}

impl Modulation for RawPCM {
    fn calc(&self) -> Result<Vec<EmitIntensity>, AUTDInternalError> {
        Ok(wav_io::resample::linear(
            self.read_buf()?,
            1,
            self.sample_rate,
            self.sampling_config().frequency() as u32,
        )
        .iter()
        .map(|&d| EmitIntensity::new(d.round() as u8))
        .collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    fn create_dat(path: impl AsRef<Path>, data: &[u8]) -> anyhow::Result<()> {
        let mut f = File::create(path)?;
        f.write_all(data)?;
        Ok(())
    }

    #[test]
    fn test_rawpcm_new() -> anyhow::Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("tmp.dat");
        create_dat(&path, &[0xFF, 0x7F, 0x00])?;
        let m = RawPCM::new(&path, 4000);
        assert_eq!(
            m.calc().unwrap(),
            vec![
                EmitIntensity::new(0xFF),
                EmitIntensity::new(0x7F),
                EmitIntensity::new(0x00)
            ]
        );

        let m = RawPCM::new("not_exists.dat", 4000);
        assert!(m.calc().is_err());

        Ok(())
    }

    #[test]
    fn test_rawpcm_clone() -> anyhow::Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("tmp.dat");
        create_dat(&path, &[0xFF, 0xFF])?;
        let m = RawPCM::new(&path, 4000);
        assert_eq!(m, m.clone());
        Ok(())
    }
}