From db4cf3eefa4c8a30c6065b8ba19a8af1572c2963 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 26 May 2024 17:55:54 +0200 Subject: [PATCH] Update wav rust library --- src/Tools/psxcdgen_ex/Cargo.toml | 2 +- src/Tools/psxcdgen_ex/src/lib.rs | 51 ++++++++++++++++++-------------- src/Tools/tool_helper/Cargo.toml | 1 + src/Tools/tool_helper/src/lib.rs | 6 ++++ 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Tools/psxcdgen_ex/Cargo.toml b/src/Tools/psxcdgen_ex/Cargo.toml index 8c43c0ad..4c744654 100644 --- a/src/Tools/psxcdgen_ex/Cargo.toml +++ b/src/Tools/psxcdgen_ex/Cargo.toml @@ -10,8 +10,8 @@ panic = "abort" cdtypes = {path = "../cdtypes"} clap = {version = "4.4.11", features = ["derive"]} colored = "2.1.0" +hound = "3.5.1" no-comment = "0.0.3" paste = "1.0.14" roxmltree = "0.19.0" tool_helper = {path = "../tool_helper"} -wav = "1.0.0" diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 004b27a7..aa3e71da 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -206,39 +206,46 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<(CDDesc, } fn parse_cd_da(cd_da_tracks: &mut Vec>, cd_da_files: Vec) -> Result<(), Error> { - fn convert_into_16(data: wav::bit_depth::BitDepth, file_path: &PathBuf) -> Result, Error> { - match data.try_into_sixteen() { - Ok(data) => { - let mut samples = Vec::new(); - - for (left, right) in data.iter().step_by(2).zip(data.iter().skip(1).step_by(2)) { - samples.push(AudioSample::new(*left, *right)) + fn convert_into_16(samples: hound::WavSamples, file_path: &PathBuf) -> Result, Error> { + let mut sample_buffer = 0; + let mut sample_id = 0; + let mut cd_samples = Vec::new(); + + for sample in samples { + match sample { + Ok(sample) => { + if sample_id == 0 { + sample_buffer = sample.as_i16(); + sample_id = 1; + } + + else { + cd_samples.push(AudioSample::new(sample_buffer, sample.as_i16())); + sample_id = 0; + } + }, + Err(err) => { + return Err(Error::from_text(format!("{}: {}", file_path.to_string_lossy(), err))); } - - Ok(samples) - }, - Err(_) => Err(Error::from_text(format!("{}: Couldn't convert samples to 16bit", file_path.display()))), + } } + + Ok(cd_samples) } for cd_da_file in cd_da_files { - let mut audio_io = { - match std::fs::File::open(&cd_da_file) { - Ok(io) => Ok(io), - Err(error) => Err(Error::from_text(format!("Opening CD Audio file \"{}\" failed with: {}", cd_da_file.display(), error))) - } - }?; - let (header, raw) = wav::read(&mut audio_io)?; + let mut audio_io = hound::WavReader::open(&cd_da_file)?; + let header = audio_io.spec(); - if header.audio_format != wav::header::WAV_FORMAT_PCM { - return Err(Error::from_text(format!("{}: Only WAV PCM is supported for Audio tracks", cd_da_file.display()))); + if header.sample_format != hound::SampleFormat::Int { + return Err(Error::from_text(format!("{}: Only integer WAV format (PCM) is supported for Audio tracks", cd_da_file.display()))); } - if header.sampling_rate != 44_100 { + if header.sample_rate != 44_100 { return Err(Error::from_text(format!("{}: Only a sampling rate of 44.1kHz is supported for Audio tracks", cd_da_file.display()))); } - cd_da_tracks.push(convert_into_16(raw, &cd_da_file)?); + cd_da_tracks.push(convert_into_16(audio_io.samples::(), &cd_da_file)?); } Ok(()) } diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index 803b9125..c14270aa 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -11,6 +11,7 @@ byteorder = "1.5.0" cdtypes = {path = "../cdtypes"} colored = "2.0.4" envmnt = "0.10.4" +hound = "3.5.1" lz4 = "1.24.0" paste = "1.0.14" wslpath = {path = "../wslpath"} \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 86a73458..44a97326 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -123,6 +123,12 @@ impl std::convert::From for Error { } } +impl std::convert::From for Error { + fn from(error: hound::Error) -> Self { + Error::from_error(error) + } +} + pub fn exit_with_error(error: Error) { error.print_to_std_err(); std::process::exit(error.exit_code);