From 09a2192e979e5051d373c73fd2524923d6edd63b Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 17 Oct 2024 21:42:14 +0200 Subject: [PATCH] Support end frame --- src/Tools/psxfileconv/Cargo.toml | 1 + src/Tools/psxfileconv/src/audio/my_vag/mod.rs | 2 + .../psxfileconv/src/audio/my_vag/types.rs | 44 ++++++++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Tools/psxfileconv/Cargo.toml b/src/Tools/psxfileconv/Cargo.toml index ddef77de..e098b84f 100644 --- a/src/Tools/psxfileconv/Cargo.toml +++ b/src/Tools/psxfileconv/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" panic = "abort" [dependencies] +bitflags = "2.6.0" clap = {version = "4.4.11", features = ["derive"]} image = "0.24.7" hound = "3.5.1" diff --git a/src/Tools/psxfileconv/src/audio/my_vag/mod.rs b/src/Tools/psxfileconv/src/audio/my_vag/mod.rs index d1f39101..cce6664e 100644 --- a/src/Tools/psxfileconv/src/audio/my_vag/mod.rs +++ b/src/Tools/psxfileconv/src/audio/my_vag/mod.rs @@ -20,6 +20,8 @@ pub fn convert(input: Input, output: &mut dyn Write) -> Result<(), Error> { sample_count += 1; } + samples.push(VAGADPCM::end()); + sample_count += 1; // TODO: Restructure tool_helper::raw::write_raw(output, &VAGHeader::create(sample_count, wav_header.sample_rate, "Planschi"))?; diff --git a/src/Tools/psxfileconv/src/audio/my_vag/types.rs b/src/Tools/psxfileconv/src/audio/my_vag/types.rs index d47be849..dd2e4250 100644 --- a/src/Tools/psxfileconv/src/audio/my_vag/types.rs +++ b/src/Tools/psxfileconv/src/audio/my_vag/types.rs @@ -1,3 +1,4 @@ +use bitflags::bitflags; use tool_helper::{raw::RawConversion, Error}; #[repr(packed)] @@ -20,13 +21,11 @@ impl VAGHeader { const RESERVED2: [u8; 12] = [0; 12]; // TODO: Ensure big endianes - pub fn create(vagpcm_samples: usize, sampling_frequency: u32, _name: &str) -> VAGHeader { + pub fn create(vagadpcm_samples: usize, sampling_frequency: u32, _name: &str) -> VAGHeader { // TODO: Support naming feature - let data_size = (vagpcm_samples*VAGADPCM::SIZE) as u32; + let data_size = (vagadpcm_samples*VAGADPCM::SIZE) as u32; let name = ['A' as u8; 16]; - println!("Got {} samples; Writing {} bytes", vagpcm_samples, data_size); - VAGHeader{ _id: VAGHeader::ID, _version: VAGHeader::VERSION.to_be(), @@ -48,6 +47,14 @@ impl RawConversion<{VAGHeader::SIZE}> for VAGHeader { } } +bitflags! { + struct VAGFlagBits : u8 { + const LoopEnd = (1 << 0); + const Repeat = (1 << 1); + const LoopStart = (1 << 2); + } +} + pub struct VAGADPCM { data: [u32; 4] } @@ -60,10 +67,14 @@ impl VAGADPCM { VAGADPCM{data: [0; 4]} } - pub fn create_for_filter_shift(filter: u32, shift: u32) -> VAGADPCM { + fn create_for_filter_shift(filter: u32, shift: u32) -> VAGADPCM { VAGADPCM{data: [(12 - shift) | filter << 4, 0, 0, 0]} } + pub fn end() -> VAGADPCM { + VAGADPCM::empty().set_loop_self() + } + pub fn create(samples: ADPCMSampleForVag, lpc_tap: LPC) -> (VAGADPCM, LPC) { fn cap_value(value: i32, min: i32, max: i32) -> i32 { if value < min { @@ -107,9 +118,30 @@ impl VAGADPCM { } } } - (best_frame, best_tap) } + + fn set_vag_flags(&mut self, flags: VAGFlagBits) { + let mut first_sample = self.data[0].to_ne_bytes(); + + first_sample[1] = flags.bits(); + self.data[0] = u32::from_ne_bytes(first_sample); + } + + pub fn set_loop_start(mut self) -> Self { + self.set_vag_flags(VAGFlagBits::LoopStart); + self + } + + pub fn set_loop_end_repeat(mut self) -> Self { + self.set_vag_flags(VAGFlagBits::LoopEnd | VAGFlagBits::Repeat); + self + } + + pub fn set_loop_self(mut self) -> Self { + self.set_vag_flags(VAGFlagBits::LoopStart | VAGFlagBits::LoopEnd); + self + } } impl RawConversion<{VAGADPCM::SIZE}> for VAGADPCM {