audio_processor_file/
output_file_processor.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23use std::{fs, io};
24
25use audio_processor_traits::{AudioBuffer, AudioProcessorSettings};
26
27pub struct OutputFileSettings {
28    audio_file_path: String,
29}
30
31pub struct OutputAudioFileProcessor {
32    audio_settings: AudioProcessorSettings,
33    output_file_settings: OutputFileSettings,
34    writer: Option<hound::WavWriter<io::BufWriter<fs::File>>>,
35}
36
37impl OutputAudioFileProcessor {
38    pub fn from_path(audio_settings: AudioProcessorSettings, audio_file_path: &str) -> Self {
39        let output_file_settings = OutputFileSettings {
40            audio_file_path: audio_file_path.to_string(),
41        };
42        Self::new(audio_settings, output_file_settings)
43    }
44
45    pub fn new(
46        audio_settings: AudioProcessorSettings,
47        output_file_settings: OutputFileSettings,
48    ) -> Self {
49        OutputAudioFileProcessor {
50            audio_settings,
51            output_file_settings,
52            writer: None,
53        }
54    }
55}
56
57impl OutputAudioFileProcessor {
58    pub fn prepare(&mut self, settings: AudioProcessorSettings) {
59        self.audio_settings = settings;
60        let sample_rate = settings.sample_rate() as u32;
61        log::info!("Wav file will be written with sample rate: {}", sample_rate);
62        let spec = hound::WavSpec {
63            channels: settings.output_channels() as u16,
64            sample_rate,
65            bits_per_sample: 32,
66            sample_format: hound::SampleFormat::Float,
67        };
68        self.writer = Some(
69            hound::WavWriter::create(&self.output_file_settings.audio_file_path, spec).unwrap(),
70        );
71    }
72
73    pub fn process(&mut self, data: &mut AudioBuffer<f32>) -> hound::Result<()> {
74        if let Some(writer) = self.writer.as_mut() {
75            for sample_num in 0..data.num_samples() {
76                for channel_num in 0..data.num_channels() {
77                    let sample = *data.get(channel_num, sample_num);
78                    writer.write_sample(sample)?;
79                }
80            }
81        }
82
83        Ok(())
84    }
85}