From 983c416ee30eead6833da541cf9431f79d339151 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 21 May 2024 21:27:42 +0200 Subject: [PATCH] Support conversion to XA audio --- src/Tools/jaby_engine_fconv/Cargo.toml | 2 +- .../jaby_engine_fconv/src/audio/xa/mod.rs | 67 ++++++++++++++++++- src/Tools/jaby_engine_fconv/src/main.rs | 4 +- src/Tools/tool_helper/Cargo.toml | 2 +- src/Tools/tool_helper/src/lib.rs | 1 + src/Tools/tool_helper/src/vec_helper.rs | 9 +++ 6 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 src/Tools/tool_helper/src/vec_helper.rs diff --git a/src/Tools/jaby_engine_fconv/Cargo.toml b/src/Tools/jaby_engine_fconv/Cargo.toml index 612dbd67..add43e6a 100644 --- a/src/Tools/jaby_engine_fconv/Cargo.toml +++ b/src/Tools/jaby_engine_fconv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jaby_engine_fconv" -version = "0.1.5" +version = "0.1.6" edition = "2021" [profile.release] diff --git a/src/Tools/jaby_engine_fconv/src/audio/xa/mod.rs b/src/Tools/jaby_engine_fconv/src/audio/xa/mod.rs index 81c7790d..b35399d9 100644 --- a/src/Tools/jaby_engine_fconv/src/audio/xa/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/audio/xa/mod.rs @@ -1,5 +1,5 @@ use clap::{Args, ValueEnum}; -use std::path::PathBuf; +use std::{env, path::PathBuf, process::Command, str}; use tool_helper::Error; #[derive(Args)] @@ -16,12 +16,73 @@ pub enum Quality { High } +impl Quality { + fn get_frequency(&self) -> u64 { + match self { + Self::Low => 18900, + Self::High => 37800, + } + } + + fn get_bith_depth(&self) -> u64 { + match self { + Self::Low => 4, + Self::High => 8, + } + } +} + #[derive(Copy, Clone, ValueEnum)] pub enum Sample { Mono, Stereo } -pub fn convert(_args: Arguments, _input: PathBuf, _output: PathBuf) -> Result<(), Error> { - Err(Error::not_implemented("XA Audio convert")) +impl Sample { + fn get_channel(&self) -> u64 { + match self { + Self::Mono => 1, + Self::Stereo => 2, + } + } +} + +pub fn convert(args: Arguments, input: PathBuf, output: PathBuf) -> Result<(), Error> { + let quality = args.quality; + let sample = args.sample; + let tool_path = { + let mut my_path = env::current_exe()?; + + my_path.pop(); + my_path.push("extern"); + my_path.push("psxavenc"); + + my_path + }; + + if !tool_path.exists() { + return Err(Error::from_str("Could not locate psxavenc. Make sure it is installed under \"/bin/extern\"")); + } + + let result = Command::new(tool_path).args([ + "-t", "xacd", + "-f", quality.get_frequency().to_string().as_ref(), + "-b", quality.get_bith_depth().to_string().as_ref(), + "-c", sample.get_channel().to_string().as_ref(), + "-F", "0", + "-C", "0", + input.to_string_lossy().as_ref(), output.to_string_lossy().as_ref() + ]) .output()?; + + let stderr = tool_helper::vec_helper::to_string(result.stderr)?; + let stdout = tool_helper::vec_helper::to_string(result.stdout)?; + + if !result.status.success() { + return Err(Error::from_text(format!("psxavenc returned: {}. {}", result.status, stderr))); + } + + if !stdout.is_empty() { + println!("{}", stdout); + } + Ok(()) } \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/src/main.rs b/src/Tools/jaby_engine_fconv/src/main.rs index 0592a3f2..219dae79 100644 --- a/src/Tools/jaby_engine_fconv/src/main.rs +++ b/src/Tools/jaby_engine_fconv/src/main.rs @@ -44,7 +44,7 @@ fn run_internal_conversion(cmd: CommandLine) -> Result<(), Error> { match cmd.sub_command { SubCommands::Nothing => nothing::copy(&mut input, dst_buffer), SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, dst_buffer), - _ => Err(Error::not_implemented("External functions can not be called internal")) + _ => Err(Error::not_implemented("External functions can not be called for internal conversion")) } }; @@ -75,7 +75,7 @@ fn run_external_conversion(cmd: CommandLine) -> Result<(), Error> { match cmd.sub_command { SubCommands::XA(args) => xa::convert(args, input_file, output_file), - _ => Err(Error::not_implemented("Internal functions can not be called external")) + _ => Err(Error::not_implemented("Internal functions can not be called for external conversion")) } } diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index 349bb2c6..803b9125 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.9.5" +version = "0.9.6" edition = "2021" [profile.release] diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index feb27da0..86a73458 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -5,6 +5,7 @@ use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::Pa pub mod bits; pub mod compress; pub mod raw; +pub mod vec_helper; pub type BufferedInputFile = BufReader; pub type Output = Box; diff --git a/src/Tools/tool_helper/src/vec_helper.rs b/src/Tools/tool_helper/src/vec_helper.rs new file mode 100644 index 00000000..ed90eb7d --- /dev/null +++ b/src/Tools/tool_helper/src/vec_helper.rs @@ -0,0 +1,9 @@ +use super::Error; +use std::str; + +pub fn to_string(output: Vec) -> Result { + match str::from_utf8(&output) { + Ok(str) => Ok(str.to_owned()), + Err(_) => Err(Error::from_str("Invalid UTF8 sequence")) + } +} \ No newline at end of file