Update wav rust library

This commit is contained in:
Jaby 2024-05-26 17:55:54 +02:00
parent 3ce5dcff12
commit db4cf3eefa
4 changed files with 37 additions and 23 deletions

View File

@ -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"

View File

@ -206,39 +206,46 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<(CDDesc,
}
fn parse_cd_da(cd_da_tracks: &mut Vec<Vec<AudioSample>>, cd_da_files: Vec<PathBuf>) -> Result<(), Error> {
fn convert_into_16(data: wav::bit_depth::BitDepth, file_path: &PathBuf) -> Result<Vec<AudioSample>, 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<R:std::io::Read, S:hound::Sample>(samples: hound::WavSamples<R,S>, file_path: &PathBuf) -> Result<Vec<AudioSample>, 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::<i16>(), &cd_da_file)?);
}
Ok(())
}

View File

@ -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"}

View File

@ -123,6 +123,12 @@ impl std::convert::From<std::sync::mpsc::RecvError> for Error {
}
}
impl std::convert::From<hound::Error> 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);