Skip to content

Commit

Permalink
Fix float wav file header after refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed Mar 18, 2024
1 parent 68b7a6d commit f000258
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/vpx/expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,10 @@ fn read_gameitem_binaries(
.zip(normals.iter())
{
let (normal, vpx_vertex_normal_data) = vn;
let mut nx = normal.0 as f32;
let mut ny = normal.1 as f32;
let nx = normal.0 as f32;
let ny = normal.1 as f32;
// invert the z axis
let mut nz = -(normal.2 as f32);
let nz = -(normal.2 as f32);

let vertext = crate::vpx::expanded::Vertex3dNoTex2 {
x: v.0 as f32,
Expand Down
3 changes: 2 additions & 1 deletion src/vpx/obj.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::BufRead;
use std::path::PathBuf;
use wavefront_rs::obj::entity::{Entity, FaceVertex};
use wavefront_rs::obj::parser::Parser;
Expand Down Expand Up @@ -251,6 +251,7 @@ pub(crate) struct ObjData {
mod test {
use super::*;
use pretty_assertions::assert_eq;
use std::io::BufReader;
use testdir::testdir;
use testresult::TestResult;

Expand Down
12 changes: 9 additions & 3 deletions src/vpx/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const WAV_HEADER_SIZE: usize = 44;
fn write_wav_header2(sound_data: &SoundData) -> Vec<u8> {
let data_len = if sound_data.wave_form.format_tag == 1 {
// In the vpx file for PCM this is always 0,
// so we can use the length of the data.
// so we use the length of the data.
sound_data.data.len() as u32 // 4
} else {
sound_data.wave_form.cb_size as u32 // 4
Expand All @@ -103,6 +103,12 @@ fn write_wav_header2(sound_data: &SoundData) -> Vec<u8> {
* sound_data.wave_form.bits_per_sample as u32
* sound_data.wave_form.channels as u32
/ 8;
let (extension_size, extra_fields) = if sound_data.wave_form.format_tag == 1 {
(None, Vec::<u8>::new())
} else {
(Some(0), Vec::<u8>::new())
};

let wav_header = WavHeader {
size: sound_data.data.len() as u32 + 36,
fmt_size: 16,
Expand All @@ -112,8 +118,8 @@ fn write_wav_header2(sound_data: &SoundData) -> Vec<u8> {
avg_bytes_per_sec: bytes_per_sec,
block_align: sound_data.wave_form.block_align,
bits_per_sample: sound_data.wave_form.bits_per_sample,
extension_size: None,
extra_fields: Vec::new(),
extension_size,
extra_fields,
data_size: data_len,
};
let mut buf = BytesMut::with_capacity(WAV_HEADER_SIZE);
Expand Down
9 changes: 9 additions & 0 deletions src/vpx/wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use bytes::{Buf, BufMut, BytesMut};
// TODO replace with a library that can read and write wav file headers
// one option could be "hound"

// An example of a float format wav file can be found in
// FirePower II (Williams 1983) 1.1.vpx Ding_01.wav

#[derive(Debug, PartialEq)]
pub(crate) struct WavHeader {
pub(crate) size: u32,
Expand Down Expand Up @@ -69,6 +72,12 @@ pub(crate) fn write_wav_header(wav_header: &WavHeader, writer: &mut BytesMut) {
writer.put_u32_le(wav_header.avg_bytes_per_sec);
writer.put_u16_le(wav_header.block_align);
writer.put_u16_le(wav_header.bits_per_sample);
if wav_header.format_tag != 1 && wav_header.extension_size.is_none() {
panic!(
"format_tag {} requires extension_size",
wav_header.format_tag
);
}
if let Some(extension_size) = wav_header.extension_size {
writer.put_u16_le(extension_size);
writer.put(&wav_header.extra_fields[..]);
Expand Down

0 comments on commit f000258

Please sign in to comment.