Support conversion to XA audio

This commit is contained in:
jaby 2024-05-21 21:27:42 +02:00
parent 98b0868d76
commit d334f19d4d
6 changed files with 78 additions and 7 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "jaby_engine_fconv"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
[profile.release]

View File

@ -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 \"<Jaby Engine>/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(())
}

View File

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

View File

@ -1,6 +1,6 @@
[package]
name = "tool_helper"
version = "0.9.5"
version = "0.9.6"
edition = "2021"
[profile.release]

View File

@ -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<std::fs::File>;
pub type Output = Box<dyn Write>;

View File

@ -0,0 +1,9 @@
use super::Error;
use std::str;
pub fn to_string(output: Vec<u8>) -> Result<String, Error> {
match str::from_utf8(&output) {
Ok(str) => Ok(str.to_owned()),
Err(_) => Err(Error::from_str("Invalid UTF8 sequence"))
}
}