Simple extraction of wav data

This commit is contained in:
jaby 2024-08-04 14:56:44 -05:00
parent 3db5a19595
commit fdd6694824
4 changed files with 48 additions and 0 deletions

View File

@ -8,6 +8,7 @@ panic = "abort"
[dependencies] [dependencies]
clap = {version = "4.4.11", features = ["derive"]} clap = {version = "4.4.11", features = ["derive"]}
hound = "3.5.1"
image = "0.24.7" image = "0.24.7"
paste = "1.0.14" paste = "1.0.14"
png = "0.17.10" png = "0.17.10"

View File

@ -0,0 +1,41 @@
use clap::{Args, ValueEnum};
use hound::Sample;
use image::{DynamicImage, io::Reader as ImageReader};
use std::io::{Cursor, Write};
use tool_helper::{print_warning, Error, Input};
#[derive(Args)]
pub struct Arguments {
}
pub fn convert(input: Input, output: &mut dyn Write) -> Result<(), Error> {
let mut audio_io = hound::WavReader::new(input)?;
let header = audio_io.spec();
if header.sample_format != hound::SampleFormat::Int ||header.bits_per_sample != 16 {
return Err(Error::from_str("Only 16bit integer samples are supported"));
}
if header.channels > 1 {
print_warning("Found more than one audio channel. First channel will be encoded.".to_owned());
}
let active_channel = 1;
let mut sample_count = 0;
for sample in audio_io.samples::<i16>() {
match sample {
Ok(sample) => {
sample_count += 1;
if sample_count%active_channel == 0 {
output.write(&sample.to_le_bytes())?;
}
}
Err(error) => {
return Err(Error::from_text(format!("Failed iterating over samples: {}", error)));
}
}
}
Ok(())
}

View File

@ -1 +1,2 @@
pub mod adpcm;
pub mod xa; pub mod xa;

View File

@ -21,8 +21,12 @@ struct CommandLine {
#[derive(Subcommand)] #[derive(Subcommand)]
enum SubCommands { enum SubCommands {
// === Internal Commands ===
Nothing, Nothing,
SimpleTIM(reduced_tim::Arguments), SimpleTIM(reduced_tim::Arguments),
ADPCM,
// === External Commands ===
XA(xa::Arguments) XA(xa::Arguments)
} }
@ -44,6 +48,7 @@ fn run_internal_conversion(cmd: CommandLine) -> Result<(), Error> {
match cmd.sub_command { match cmd.sub_command {
SubCommands::Nothing => nothing::copy(&mut input, dst_buffer), SubCommands::Nothing => nothing::copy(&mut input, dst_buffer),
SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, dst_buffer), SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, dst_buffer),
SubCommands::ADPCM => adpcm::convert(input, dst_buffer),
_ => Err(Error::not_implemented("External functions can not be called for internal conversion")) _ => Err(Error::not_implemented("External functions can not be called for internal conversion"))
} }
}; };