This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
72 lines (62 loc) · 2.37 KB
/
main.rs
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
use structopt::StructOpt;
use std::fs::File;
use std::io::{stdin,Read};
use hound::{WavReader, WavWriter};
mod dsp;
#[derive(StructOpt, Debug)]
#[structopt(version = "\0", about = "The loudness of the samples are normalized \
by calculating the RMS then the gain is changed to \
bring the average amplitude of the source signal to a target level.")]
struct RootOptions {
#[structopt(help = "Input WAVE file to process (- as STDIN)", required = true)]
input: String,
#[structopt(help = "Output File to dump the data", required = true)]
output: String,
#[structopt(short, long, help = "Targetted average amplitude of signal", default_value = "1.0")]
target_ampl: f32
}
fn main() {
let _root_params = RootOptions::from_args();
/**
* Read the data from the STDIN or a *actual file*
* from the filesystem.
*/
let mut wave_input = WavReader::new(
match _root_params.output.as_ref() {
"-" => Box::new(stdin()) as Box<dyn Read>,
_ => Box::new(File::create(&_root_params.output).unwrap()) as Box<dyn Read>
}
).unwrap();
/**
* The sample writer, encodes the data into
* the bistream and inherits the sample specifications
* as the source file.
*/
let mut wave_output = WavWriter::create(
_root_params.output, wave_input.spec()
).unwrap();
/**
* This is where the real magic happens.
* The signal sent to process via the DSP filter and later collected
* as 32-bit floating point samples which is written to a WAVE file.
*/
let mut wave_samples = wave_input.samples::<f32>().map(Result::unwrap).collect();
let signal_info = dsp::normalize_samples(&mut wave_samples, _root_params.target_ampl)
.unwrap();
/* Write each sample-data into the buffer which would be later
* flushed to a file and that's the WAVE file of output
*/
wave_samples.iter().for_each(|sample| wave_output
.write_sample(*sample)
.unwrap()
);
// Verbose information, that a user might be eager
// know about the source signal.
println!(
"Gain applied: {} dBFS\
\nRMS of signal: {} dBFS",
20.0 * signal_info.gain.log10(),
20.0 * signal_info.rms.log10()
);
wave_output.finalize().unwrap();
}