Writer impl in progress

This commit is contained in:
Jamie Hardt
2020-12-25 12:30:08 -08:00
parent 3d7c74fc94
commit 9a275a69c3
5 changed files with 138 additions and 51 deletions

View File

@@ -20,7 +20,7 @@ This is currently a work-in-progress! However many features presently work:
| EBU Broadcast-WAVE metadata | ☑️ | |
| Basic iXML/ADM metadata | ☑️ | |
| Enhanced iXML metadata support | | |
| Broadcast-WAVE Level metadata | | |
| Broadcast-WAVE Level overview `levl` metadata | | |
| Cue list metadata | ☑️ | |
| Sampler and instrument metadata | | |
| Enhanced Wave file form validation | ☑️ | 🚫 |

View File

@@ -1,4 +1,4 @@
use std::io::{Write,Seek};
use std::io::{Write,Seek,SeekFrom};
use super::wavewriter::WaveWriter;
use super::errors::Error;
@@ -11,29 +11,25 @@ pub struct AudioFrameWriter<W: Write + Seek> {
inner : W,
form_size : u64,
data_size : u64,
data_start : u64,
format: WaveFmt
}
impl<W:Write + Seek> AudioFrameWriter<W> {
pub fn make(wave_writer : WaveWriter<W>) -> Self {
Self {
inner: wave_writer.inner,
form_size: wave_writer.form_size,
data_size: 0u64,
format : wave_writer.format
}
pub fn new(inner: W, format: WaveFmt) -> Self {
todo!();
}
pub fn write_integer_frame(&mut self, buffer: &mut [i32]) -> Result<u64, Error> {
pub fn write_audio_frame(&mut self, buffer: &[i32]) -> Result<u64,Error> {
assert!(buffer.len() as u16 == self.format.channel_count,
"write_integer_frame was called with a mis-sized buffer, expected {}, was {}",
"read_integer_frame was called with a mis-sized buffer, expected {}, was {}",
self.format.channel_count, buffer.len());
let framed_bits_per_sample = self.format.block_alignment * 8 / self.format.channel_count;
for n in 0..(self.format.channel_count as usize) {
match (self.format.bits_per_sample, framed_bits_per_sample) {
(0..=8,8) => self.inner.write_u8(buffer[n] as u8 + 0x80_u8)?, // EBU 3285 §A2.2
(0..=8,8) => self.inner.write_u8(( buffer[n] + 0x80) as u8 )?, // EBU 3285 §A2.2
(9..=16,16) => self.inner.write_i16::<LittleEndian>(buffer[n] as i16)?,
(10..=24,24) => self.inner.write_i24::<LittleEndian>(buffer[n])?,
(25..=32,32) => self.inner.write_i32::<LittleEndian>(buffer[n])?,
@@ -41,6 +37,7 @@ impl<W: Write + Seek> AudioFrameWriter<W> {
b, self.format.channel_count, self.format.block_alignment)
}
}
Ok( 1 )
}
}

View File

@@ -102,6 +102,7 @@ pub const DATA_SIG: FourCC = FourCC::make(b"data");
pub const FMT__SIG: FourCC = FourCC::make(b"fmt ");
pub const BEXT_SIG: FourCC = FourCC::make(b"bext");
pub const FACT_SIG: FourCC = FourCC::make(b"fact");
pub const JUNK_SIG: FourCC = FourCC::make(b"JUNK");
pub const FLLR_SIG: FourCC = FourCC::make(b"FLLR");

View File

@@ -45,6 +45,7 @@ pub struct Parser<R: Read + Seek> {
ds64state: HashMap<FourCC,u64>
}
#[derive(Debug, PartialEq, Eq)]
pub struct ChunkIteratorItem {
pub signature: FourCC,
pub start: u64,

View File

@@ -1,12 +1,19 @@
use std::collections::HashMap;
use std::io::{Write, Seek, SeekFrom};
use std::fs::File;
use std::io::Cursor;
use super::errors::Error;
use super::chunks::{WriteBWaveChunks};
use super::fmt::{WaveFmt};
use super::fourcc::{FourCC, RIFF_SIG, WAVE_SIG, FMT__SIG, JUNK_SIG, BEXT_SIG, DATA_SIG, FLLR_SIG, WriteFourCC};
use super::audio_frame_writer::AudioFrameWriter;
use super::chunks::WriteBWaveChunks;
use super::fmt::WaveFmt;
use super::common_format::CommonFormat;
use super::fourcc::{FourCC, RIFF_SIG, RF64_SIG, WAVE_SIG, FMT__SIG, JUNK_SIG,
DS64_SIG, FACT_SIG, DATA_SIG, BEXT_SIG, FLLR_SIG, WriteFourCC};
use super::bext::Bext;
//use super::audio_frame_writer::AudioFrameWriter;
use byteorder::LittleEndian;
use byteorder::WriteBytesExt;
@@ -15,51 +22,90 @@ use byteorder::WriteBytesExt;
pub struct WaveWriter<W> where W: Write + Seek {
pub inner : W,
pub form_size : u64,
pub format : WaveFmt
pub format : WaveFmt,
pub ds64_sizes : Option<HashMap<FourCC,u64>>,
pub bext_start : Option<u64>,
pub fact_start : Option<u64>,
}
impl WaveWriter<File> {
fn create(path: &str, format: WaveFmt) -> Result<Self, Error> {
pub fn create(path: &str, format: WaveFmt, broadcast_extension: Option<Bext>) -> Result<Self, Error> {
let f = File::create(path)?;
Self::new(f, format)
Self::new(f, format, broadcast_extension)
}
}
impl<W: Write + Seek> WaveWriter<W> {
/// Wrap a `Write` struct with a wavewriter.
fn new(inner : W, format: WaveFmt) -> Result<Self,Error> {
let mut retval = Self { inner, form_size : 0 , format: format};
pub fn new(inner : W, format: WaveFmt, broadcast_extension : Option<Bext>) -> Result<Self,Error> {
let mut retval = Self { inner, form_size : 0, format: format,
ds64_sizes : None,
bext_start : None,
fact_start : None,
};
retval.inner.seek(SeekFrom::Start(0))?;
retval.inner.write_fourcc(RIFF_SIG)?;
retval.inner.write_u32::<LittleEndian>(0)?;
retval.inner.write_fourcc(WAVE_SIG)?;
retval.update_form_size(4)?;
retval.write_ds64_reservation()?;
retval.write_format(format)?;
retval.append_head_chunks(format, broadcast_extension)?;
Ok( retval )
}
/// Unwrap the inner writer.
fn into_inner(self) -> W {
pub fn into_inner(self) -> W {
return self.inner;
}
/// Return an AudioFrameWriter, consuming the reader.
fn audio_frame_writer(mut self) -> Result<AudioFrameWriter<W>, Error> {
pub fn audio_frame_writer() {
let framing = 0x4000;
self.append_data_framing_chunk(framing)?;
}
self.inner.write_fourcc(DATA_SIG)?;
self.inner.write_u32::<LittleEndian>(0u32)?;
self.update_form_size(8)?;
}
Ok( AudioFrameWriter::make(self) )
impl<W: Write + Seek> WaveWriter<W> { /* Private implementation */
fn append_head_chunks(&mut self, format : WaveFmt, broadcast_extension : Option<Bext>) -> Result<(),Error> {
self.write_ds64_reservation()?;
self.write_format(format)?;
if format.common_format() != CommonFormat::IntegerPCM {
self.append_fact_chunk()?;
}
if let Some(bext) = broadcast_extension {
self.append_bext_chunk(bext)?;
}
self.append_data_framing_chunk(0x4000)?;
Ok(())
}
fn append_fact_chunk(&mut self) -> Result<(),Error> {
let buf = vec![0u8; 4];
self.fact_start = Some( self.inner.seek(SeekFrom::Current(0))? + 8);
self.append_chunk(FACT_SIG, &buf)?;
Ok(())
}
fn append_bext_chunk(&mut self, bext: Bext) -> Result<(),Error> {
let buf = vec![0u8;0];
let mut cursor = Cursor::new(buf);
cursor.write_bext(&bext)?;
let buf = cursor.into_inner();
self.bext_start = Some( self.inner.seek(SeekFrom::Current(0))? + 8);
self.append_chunk(BEXT_SIG, &buf)?;
Ok(())
}
fn append_data_framing_chunk(&mut self, framing: u64) -> Result<(), Error> {
let current_length = self.inner.seek(SeekFrom::End(0))?;
let size_to_add = (current_length % framing) - 8;
let size_to_add = framing - ((current_length % framing) - 8);
let chunk_size_to_add = size_to_add - 8;
let buf = vec![ 0u8; chunk_size_to_add as usize];
@@ -83,10 +129,6 @@ impl<W: Write + Seek> WaveWriter<W> {
self.update_form_size(total_chunk_size)?;
Ok( () )
}
}
impl<W: Write + Seek> WaveWriter<W> { /* Private implementation */
fn write_ds64_reservation(&mut self) -> Result<(), Error> {
let ds64_reservation_data = vec![0u8; 92];
@@ -113,6 +155,52 @@ impl<W: Write + Seek> WaveWriter<W> { /* Private implementation */
}
fn update_form_size_ds64(&mut self, new_size: u64) -> Result<(), Error> {
todo!()
if self.ds64_sizes.is_none() {
self.inner.seek(SeekFrom::Start(0))?;
self.inner.write_fourcc(RF64_SIG)?;
self.inner.seek(SeekFrom::Start(12))?;
self.inner.write_fourcc(DS64_SIG)?;
self.ds64_sizes = Some( HashMap::new() );
}
self.inner.seek(SeekFrom::Start(20))?;
self.inner.write_u64::<LittleEndian>(new_size)?;
Ok(())
}
}
#[test]
fn test_simple_create() {
use super::fourcc::ReadFourCC;
use byteorder::ReadBytesExt;
let buf = vec![0u8; 0];
let mut cursor = Cursor::new(buf);
let format = WaveFmt::new_pcm(48000, 24, 1);
let w = WaveWriter::new(cursor, format, None).unwrap();
cursor = w.into_inner();
cursor.seek(SeekFrom::Start(0)).unwrap();
assert_eq!( cursor.read_fourcc().unwrap(), RIFF_SIG);
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
assert_eq!( cursor.read_fourcc().unwrap(), WAVE_SIG);
assert_eq!( cursor.read_fourcc().unwrap(), JUNK_SIG);
let junk_size = cursor.read_u32::<LittleEndian>().unwrap();
cursor.seek(SeekFrom::Current(junk_size as i64)).unwrap();
assert_eq!( cursor.read_fourcc().unwrap(), FMT__SIG);
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
cursor.seek(SeekFrom::Current(fmt_size as i64)).unwrap();
assert_eq!( cursor.read_fourcc().unwrap(), FLLR_SIG);
let junk2_size = cursor.read_u32::<LittleEndian>().unwrap();
cursor.seek(SeekFrom::Current(junk2_size as i64)).unwrap();
assert_eq!( form_size , junk2_size + junk_size + fmt_size + 4 + 8 * 3);
}