From eee3c3f62592f024dd1308edeedc1b98565bbcd3 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Sun, 3 Jan 2021 23:43:32 -0800 Subject: [PATCH] exmaple impo --- examples/wave-deinter.rs | 72 +++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/examples/wave-deinter.rs b/examples/wave-deinter.rs index 8ee9c78..767dff1 100644 --- a/examples/wave-deinter.rs +++ b/examples/wave-deinter.rs @@ -5,24 +5,71 @@ //! polyphonic wave file. use std::io; +use std::path::Path; extern crate bwavfile; +use bwavfile::{Error,WaveReader, ChannelDescriptor, ChannelMask}; #[macro_use] extern crate clap; use clap::{Arg, App}; -fn main() -> io::Result<()> { +fn name_suffix(force_numeric : bool, delim : &str, index: usize, channel_descriptor : &ChannelDescriptor) -> String { + if force_numeric || channel_descriptor.speaker == ChannelMask::DirectOut { + format!("{}A{:02}", delim, index) + } else { + let chan_name = match channel_descriptor.speaker { + ChannelMask::FrontLeft => "L", + ChannelMask::FrontCenter => "C", + ChannelMask::FrontRight => "R", + ChannelMask::BackLeft => "Ls", + ChannelMask::BackRight => "Rs", + ChannelMask::BackCenter => "S", + ChannelMask::TopCenter => "Tc", + ChannelMask::LowFrequency => "Lfe", + ChannelMask::SideLeft => "Lss", + ChannelMask::SideRight => "Rss", + ChannelMask::FrontCenterLeft => "Lc", + ChannelMask::FrontCenterRight => "Rc", + ChannelMask::TopFrontLeft => "Ltf", + ChannelMask::TopFrontCenter => "Ctf", + ChannelMask::TopFrontRight => "Rtf", + ChannelMask::TopBackLeft => "Ltb", + ChannelMask::TopBackCenter => "Ctb", + ChannelMask::TopBackRight => "Rtb", + ChannelMask::DirectOut => panic!("Error, can't get here") + }; + format!("{}{}", delim, chan_name) + } +} +fn process_file(infile: &str, delim : &str, numeric_channel_names : bool) -> Result<(), Error> { + let mut read_wave = WaveReader::open(infile)?; + let channel_desc = read_wave.channels()?; + + if channel_desc.len() == 1 { + println!("Input file in monoaural, exiting."); + return Ok(()); + } + + let infile_path = Path::new(infile); + let basename = infile_path.file_stem().expect("Unable to extract file basename").to_str().unwrap(); + let output_dir = infile_path.parent().expect("Unable to derive parent directory"); + + for (n, channel) in channel_desc.iter().enumerate() { + let suffix = name_suffix(numeric_channel_names, delim, n + 1, channel); + let outfile_name = output_dir.join(format!("{}{}.wav", basename, suffix)).into_os_string().into_string().unwrap(); + println!("Will create file {}", outfile_name); + } + + Ok(()) +} + +fn main() -> io::Result<()> { let matches = App::new("wave-deinter") .version(crate_version!()) .author(crate_authors!()) .about("Extract each channel of a polyphonic wave file as a new monoaural wave file.") - .arg(Arg::with_name("OUTPUT") - .long("output") - .short("o") - .help("Output file basename. If absent, will be the basename of INPUT.") - ) .arg(Arg::with_name("numeric_names") .long("numeric") .short("n") @@ -38,10 +85,17 @@ fn main() -> io::Result<()> { .arg(Arg::with_name("INPUT") .help("Input wave file") .required(true) + .multiple(true) ) .get_matches(); + + let delimiter = matches.value_of("channel_delimiter").unwrap(); + let use_numeric_names = matches.is_present("numeric_names"); + let infile = matches.value_of("INPUT").unwrap(); - println!("Command line opts: {:?}", matches); - - todo!("Finish implementation"); + match process_file(infile, delimiter, use_numeric_names) { + Err(Error::IOError(io)) => Err(io), + Err(e) => panic!("Error: {:?}", e), + Ok(()) => Ok(()) + } } \ No newline at end of file