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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! A wrapper for the hound library that writes binauralized audio to
//! the user-speciifed output file.

use crate::component::{Component, ComponentError};
use hound::{Error as HoundError, SampleFormat, WavReader, WavSpec, WavWriter};

use std::fs::File;
use std::io::BufWriter;
use std::path::Path;

/// A monitor wrapper for the hound WavWriter that writes out binauralized
/// audio
pub struct HoundWriter {
    writer: Option<WavWriter<BufWriter<File>>>,
}

impl HoundWriter {
    /// Instantiates a new HoundWriter, which wraps the hound WavWriter
    pub fn new(file: impl AsRef<Path>, wave_spec: WavSpec) -> Self {
        let writer = WavWriter::create(file, wave_spec).unwrap();

        Self {
            writer: Some(writer),
        }
    }
}

impl Component for HoundWriter {
    type InData = (Vec<f32>, Vec<f32>);
    type OutData = Result<(), HoundError>;

    /// Appends binauralized audio data to the specified output WAV file
    fn convert(&mut self, input: (Vec<f32>, Vec<f32>)) -> Result<(), HoundError> {
        let (left_samps, right_samps) = input;
        let mut writer = self.writer.take().unwrap();

        // interleave the two streams and write the samples to the WAV file
        for (left, right) in std::iter::zip(left_samps, right_samps) {
            writer.write_sample(left).unwrap();
            writer.write_sample(right).unwrap();
        }

        // flush after each write to save state of the WAV file in the header
        let result = writer.flush();
        self.writer = Some(writer);
        result
    }

    /// Clean up WavWriter after writing all audio data from pipeline. This
    /// This happens automatically when the WavWriter is dropped, but
    /// calling this gives us controlled error checking.
    fn finalize(&mut self) -> Result<(), ComponentError> {
        let writer = self.writer.take().unwrap();

        writer.finalize().map_err(ComponentError::HoundError)
    }

    /// Converts the HoundWriter's name
    fn name(&self) -> String {
        "HoundWriter".to_string()
    }
}

/// This function, given a Vec of filenames, uses hound to read the audio
/// data into a 2D Vec, where each Vec represents the audio file data.
///
/// IMPORTANT NOTE:
///
/// This function does not meaningfully handle audio data
/// with multiple channels. Only use mono files!
pub fn hound_reader(filenames: Vec<String>) -> Vec<Vec<f32>> {
    let mut all_samples: Vec<Vec<f32>> = vec![];

    for file in filenames {
        let mut reader = WavReader::open(file).unwrap();

        // collect wav file data into Vec of interleaved f32 samples
        let samples = reader
            .samples::<i32>()
            .map(|x| x.unwrap() as f32)
            .collect::<Vec<_>>();

        all_samples.push(samples);
    }

    all_samples
}

/// Writes two vectors of samples to a file on the disk in WAV format
pub fn hound_writer(left_samps: Vec<f32>, right_samps: Vec<f32>, out_file: impl AsRef<Path>) {
    let spec = WavSpec {
        channels: 2,
        sample_rate: 44100,
        bits_per_sample: 16,
        sample_format: SampleFormat::Int,
    };

    let mut writer = WavWriter::create(out_file, spec).unwrap();

    for (left, right) in std::iter::zip(left_samps, right_samps) {
        writer.write_sample(left as i16).unwrap();
        writer.write_sample(right as i16).unwrap();
    }

    writer.finalize().unwrap();
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::component::run_component;
    use hound::{SampleFormat, WavReader};
    use tempfile::NamedTempFile;

    use std::f32::consts::PI;
    use std::fs::remove_file;
    use std::{io::BufReader, sync::mpsc::channel};

    const SAMP_RATE: u32 = 44100;
    const BITS_PER_SAMPLE: u16 = 32;
    const FRAME_SIZE: i32 = 128;

    const C: f32 = 261.61;

    fn create_sine_wave(frames: i32, note: f32) -> Vec<f32> {
        (0..frames)
            .map(|x| (x % FRAME_SIZE) as f32 / FRAME_SIZE as f32)
            .map(|t| (t * note * 2.0 * PI).sin() * (i16::MAX as f32))
            .collect()
    }

    // Write 100 sine wav frames to an output file using a WavWriter, and
    // read it back properly using a WavReader
    #[test]
    fn test_wav_writer_reader() {
        let file = NamedTempFile::new().unwrap();
        let file_name = file.path();

        let left_samps: Vec<f32> = create_sine_wave(100, C);
        let right_samps: Vec<f32> = create_sine_wave(100, C);

        // Create WavWriter and write 2 channel sine wav to output file
        let spec = WavSpec {
            channels: 2,
            sample_rate: SAMP_RATE,
            bits_per_sample: BITS_PER_SAMPLE,
            sample_format: SampleFormat::Float,
        };

        let mut writer = WavWriter::create(file_name, spec).unwrap();

        for (&left, &right) in std::iter::zip(&left_samps, &right_samps) {
            writer.write_sample(left).unwrap();
            writer.write_sample(right).unwrap();
        }

        assert!(writer.finalize().is_ok());

        // Create WavReader and read interleaved data back into 2 channel data
        let mut reader = WavReader::open(file_name).unwrap();

        let all_samps = reader
            .samples::<f32>()
            .collect::<Result<Vec<f32>, hound::Error>>()
            .unwrap();

        let left_samps_out = all_samps
            .clone()
            .into_iter()
            .step_by(2)
            .collect::<Vec<f32>>();
        let right_samps_out = all_samps
            .into_iter()
            .skip(1)
            .step_by(2)
            .collect::<Vec<f32>>();

        assert_eq!(left_samps, left_samps_out);
        assert_eq!(right_samps, right_samps_out);

        assert!(remove_file(file_name).is_ok());
    }

    /// Write 100 sine wav frames to an output file using a HoundWriter
    /// running as a Component thread. Then, read the data back using a
    /// WavReader and assert that all values are the same.
    #[test]
    fn test_hound_writer_read_write_one_frame() {
        let file = NamedTempFile::new().unwrap();
        let file_name = file.path();

        let left_samps: Vec<f32> = create_sine_wave(100, C);
        let right_samps: Vec<f32> = create_sine_wave(100, C);

        let hound_spec: WavSpec = WavSpec {
            channels: 2,
            sample_rate: SAMP_RATE,
            bits_per_sample: BITS_PER_SAMPLE,
            sample_format: SampleFormat::Float,
        };
        // Create HoundWriter and spawn as new Component thread
        let hound_writer = HoundWriter::new(file_name, hound_spec);

        let (hound_tx, hound_rx) = channel::<(Vec<f32>, Vec<f32>)>();
        let (result_tx, result_rx) = channel::<Result<(), HoundError>>();

        run_component(Box::new(hound_writer), hound_rx, result_tx);

        // Write single frame of sine wave data to the HoundWriter
        assert!(hound_tx
            .send((left_samps.clone(), right_samps.clone()))
            .is_ok());

        // Before creating reader, confirm that HoundWriter is done
        assert!(result_rx.recv().is_ok());

        // Read data back from file using WavReader
        let mut hound_reader: WavReader<BufReader<File>> = WavReader::open(file_name).unwrap();

        let all_samps = hound_reader
            .samples::<f32>()
            .collect::<Result<Vec<f32>, hound::Error>>()
            .unwrap();

        let left_samps_out = all_samps
            .clone()
            .into_iter()
            .step_by(2)
            .collect::<Vec<f32>>();
        let right_samps_out = all_samps
            .into_iter()
            .skip(1)
            .step_by(2)
            .collect::<Vec<f32>>();

        assert_eq!(left_samps, left_samps_out);
        assert_eq!(right_samps, right_samps_out);

        assert!(remove_file(file_name).is_ok());
    }
}