From e4fc4732b55f0387f2a754ad4bed1354657ad2e6 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Fri, 25 Dec 2020 20:55:38 -0800 Subject: [PATCH] Wavewriter impl --- src/common_format.rs | 10 +++++----- src/fmt.rs | 14 ++++++++++++-- src/wavewriter.rs | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/common_format.rs b/src/common_format.rs index 211774d..72ee11b 100644 --- a/src/common_format.rs +++ b/src/common_format.rs @@ -22,19 +22,19 @@ const BASIC_EXTENDED: u16 = 0xFFFE; */ -const UUID_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, +pub const UUID_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); -const UUID_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, +pub const UUID_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); -const UUID_MPEG: Uuid = Uuid::from_bytes([0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, +pub const UUID_MPEG: Uuid = Uuid::from_bytes([0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); -const UUID_BFORMAT_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, +pub const UUID_BFORMAT_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]); -const UUID_BFORMAT_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, +pub const UUID_BFORMAT_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]); diff --git a/src/fmt.rs b/src/fmt.rs index b4ab4a8..465c8f0 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -1,5 +1,5 @@ use uuid::Uuid; -use super::common_format::CommonFormat; +use super::common_format::{CommonFormat, UUID_PCM}; #[allow(dead_code)] @@ -200,7 +200,17 @@ impl WaveFmt { bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32, block_alignment: container_bytes_per_sample * channel_count, bits_per_sample: container_bits_per_sample, - extended_format: None + extended_format: { + if channel_count > 2 { + Some( WaveFmtExtended { + channel_mask : !(0xFFFF_FFFF << channel_count), + type_guid: UUID_PCM, + valid_bits_per_sample: bits_per_sample + }) + } else { + None + } + } } } diff --git a/src/wavewriter.rs b/src/wavewriter.rs index a4a5964..6876182 100644 --- a/src/wavewriter.rs +++ b/src/wavewriter.rs @@ -2,8 +2,9 @@ use std::fs::File; use std::io::{Write,Seek,SeekFrom}; use super::Error; -use super::fourcc::{FourCC, WriteFourCC, RIFF_SIG, WAVE_SIG, FMT__SIG,}; +use super::fourcc::{FourCC, WriteFourCC, RIFF_SIG, WAVE_SIG, FMT__SIG,FACT_SIG}; use super::fmt::WaveFmt; +use super::common_format::CommonFormat; use super::chunks::WriteBWaveChunks; use byteorder::LittleEndian; @@ -66,7 +67,9 @@ impl Write for WaveChunkWriter where W: Write + Seek { pub struct WaveWriter where W: Write + Seek { inner : W, form_length: u64, - format: WaveFmt + + /// Format of the wave file. + pub format: WaveFmt } impl WaveWriter { @@ -97,7 +100,14 @@ impl WaveWriter where W: Write + Seek { chunk.write_wave_fmt(&format)?; let retval = chunk.end(); - Ok( retval ) + if format.common_format() != CommonFormat::IntegerPCM { + let mut chunk = retval.begin_chunk(FACT_SIG)?; + chunk.write_u32::(0)?; + let retval = chunk.end(); + Ok( retval ) + } else { + Ok( retval ) + } } /// Create a new chunk writer, which takes posession of the `WaveWriter`. @@ -135,4 +145,5 @@ fn test_new() { let fmt_size = cursor.read_u32::().unwrap(); assert_eq!(form_size, fmt_size + 8 + 4); -} \ No newline at end of file +} +