mirror of
https://github.com/iluvcapra/bwavfile.git
synced 2026-01-01 09:20:45 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25589ea848 | ||
|
|
d242dff686 | ||
|
|
155a26ace0 | ||
|
|
1d2edcb675 | ||
|
|
213a856e41 | ||
|
|
70bf402776 | ||
|
|
3ab3a28d0e | ||
|
|
620ca8a968 | ||
|
|
bb6390a95c | ||
|
|
15b4ccf851 | ||
|
|
ea9a0b6cbe | ||
|
|
95700b642d |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2,7 +2,7 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bwavfile"
|
name = "bwavfile"
|
||||||
version = "0.9.0"
|
version = "0.9.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"encoding",
|
"encoding",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bwavfile"
|
name = "bwavfile"
|
||||||
version = "0.9.0"
|
version = "0.9.1"
|
||||||
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
28
README.md
28
README.md
@@ -10,20 +10,20 @@ Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support
|
|||||||
|
|
||||||
This is currently a work-in-progress! However many features presently work:
|
This is currently a work-in-progress! However many features presently work:
|
||||||
|
|
||||||
| Feature | Read | Write |
|
| Feature |Read |Write|
|
||||||
|---------------------------------------|:-----:|:-----:|
|
|---------------------------------------|:---:|:-----:|
|
||||||
| Standard .wav files | ☑️ | ☑️ |
|
| Standard .wav files | ☑️ | ☑ ️ |
|
||||||
| Transparent promotion to RF64/BW64 | ☑️ | |
|
| Transparent promotion to RF64/BW64 | ☑️ | ☑️ |
|
||||||
| Unified interface for regular and extended Wave format | ☑️ | |
|
| Unified interface for regular and extended Wave format | ☑️ | ☑️ |
|
||||||
| Channel/speaker map metadata | ☑️ | |
|
| Channel/speaker map metadata | ☑️ | ☑️ |
|
||||||
| Ambisonic B-format metadata | ☑️ | |
|
| Ambisonic B-format metadata | ☑️ | ☑️ |
|
||||||
| EBU Broadcast-WAVE metadata | ☑️ | |
|
| EBU Broadcast-WAVE metadata | ☑️ | ☑️ |
|
||||||
| Basic iXML/ADM metadata | ☑️ | |
|
| Basic iXML/ADM metadata | ☑️ | |
|
||||||
| Enhanced iXML metadata support | | |
|
| Enhanced iXML metadata support | | |
|
||||||
| Broadcast-WAVE Level overview `levl` metadata | | |
|
| Broadcast-WAVE Level overview `levl` metadata | | |
|
||||||
| Cue list metadata | ☑️ | |
|
| Cue list metadata | ☑️ | |
|
||||||
| Sampler and instrument metadata | | |
|
| Sampler and instrument metadata | | |
|
||||||
| Enhanced Wave file form validation | ☑️ | 🚫 |
|
| Enhanced Wave file form validation | ☑ ️ | |
|
||||||
|
|
||||||
|
|
||||||
## Use Examples
|
## Use Examples
|
||||||
|
|||||||
28
src/bext.rs
28
src/bext.rs
@@ -3,20 +3,19 @@ pub type LU = f32;
|
|||||||
pub type LUFS = f32;
|
pub type LUFS = f32;
|
||||||
pub type Decibels = f32;
|
pub type Decibels = f32;
|
||||||
|
|
||||||
/**
|
|
||||||
* Broadcast-WAV metadata record.
|
/// Broadcast-WAV metadata record.
|
||||||
*
|
///
|
||||||
* The `bext` record contains information about the original recording of the
|
/// The `bext` record contains information about the original recording of the
|
||||||
* Wave file, including a longish (256 ASCII chars) description field,
|
/// Wave file, including a longish (256 ASCII chars) description field,
|
||||||
* originator identification fields, creation calendar date and time, a
|
/// originator identification fields, creation calendar date and time, a
|
||||||
* sample-accurate recording time field, and a SMPTE UMID.
|
/// sample-accurate recording time field, and a SMPTE UMID.
|
||||||
*
|
///
|
||||||
* For a Wave file to be a complaint "Broadcast-WAV" file, it must contain
|
/// For a Wave file to be a complaint "Broadcast-WAV" file, it must contain
|
||||||
* a `bext` metadata record.
|
/// a `bext` metadata record.
|
||||||
*
|
///
|
||||||
* For reference on the structure and use of the BEXT record
|
/// For reference on the structure and use of the BEXT record
|
||||||
* check out [EBU Tech 3285](https://tech.ebu.ch/docs/tech/tech3285.pdf).
|
/// check out [EBU Tech 3285](https://tech.ebu.ch/docs/tech/tech3285.pdf).
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Bext {
|
pub struct Bext {
|
||||||
|
|
||||||
@@ -47,7 +46,6 @@ pub struct Bext {
|
|||||||
|
|
||||||
/// SMPTE 330M UMID
|
/// SMPTE 330M UMID
|
||||||
///
|
///
|
||||||
///
|
|
||||||
/// This field is `None` if the version is less than 1.
|
/// This field is `None` if the version is less than 1.
|
||||||
pub umid: Option<[u8; 64]>,
|
pub umid: Option<[u8; 64]>,
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/**
|
|
||||||
* References:
|
|
||||||
* - http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf
|
|
||||||
*/
|
|
||||||
|
|
||||||
// http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
|
|
||||||
|
|
||||||
const BASIC_PCM: u16 = 0x0001;
|
const BASIC_PCM: u16 = 0x0001;
|
||||||
const BASIC_FLOAT: u16 = 0x0003;
|
const BASIC_FLOAT: u16 = 0x0003;
|
||||||
const BASIC_MPEG: u16 = 0x0050;
|
const BASIC_MPEG: u16 = 0x0050;
|
||||||
@@ -71,6 +64,7 @@ pub enum CommonFormat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CommonFormat {
|
impl CommonFormat {
|
||||||
|
/// Resolve a tag and Uuid to a `CommonFormat`.
|
||||||
pub fn make(basic: u16, uuid: Option<Uuid>) -> Self {
|
pub fn make(basic: u16, uuid: Option<Uuid>) -> Self {
|
||||||
match (basic, uuid) {
|
match (basic, uuid) {
|
||||||
(BASIC_PCM, _) => Self::IntegerPCM,
|
(BASIC_PCM, _) => Self::IntegerPCM,
|
||||||
@@ -85,6 +79,10 @@ impl CommonFormat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the appropriate tag and `Uuid` for the callee.
|
||||||
|
///
|
||||||
|
/// If there is no appropriate tag for the format of the callee, the
|
||||||
|
/// returned tag will be 0xFFFE and the `Uuid` will describe the format.
|
||||||
pub fn take(self) -> (u16, Uuid) {
|
pub fn take(self) -> (u16, Uuid) {
|
||||||
match self {
|
match self {
|
||||||
Self::IntegerPCM => (BASIC_PCM, UUID_PCM),
|
Self::IntegerPCM => (BASIC_PCM, UUID_PCM),
|
||||||
|
|||||||
72
src/fmt.rs
72
src/fmt.rs
@@ -1,5 +1,5 @@
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use super::common_format::{CommonFormat, UUID_PCM};
|
use super::common_format::{CommonFormat, UUID_PCM,UUID_BFORMAT_PCM};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
||||||
@@ -182,17 +182,63 @@ pub struct WaveFmt {
|
|||||||
|
|
||||||
impl WaveFmt {
|
impl WaveFmt {
|
||||||
|
|
||||||
/// Create a new integer PCM format `WaveFmt`
|
/// Create a new integer PCM format for a monoaural audio stream.
|
||||||
pub fn new_pcm(sample_rate: u32, bits_per_sample: u16, channel_count: u16) -> Self {
|
pub fn new_pcm_mono(sample_rate: u32, bits_per_sample: u16) -> Self {
|
||||||
|
Self::new_pcm_multichannel(sample_rate, bits_per_sample, 0x4)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format for a standard Left-Right stereo audio
|
||||||
|
/// stream.
|
||||||
|
pub fn new_pcm_stereo(sample_rate: u32, bits_per_sample: u16) -> Self {
|
||||||
|
Self::new_pcm_multichannel(sample_rate, bits_per_sample, 0x3)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format for ambisonic b-format.
|
||||||
|
pub fn new_pcm_ambisonic(sample_rate: u32, bits_per_sample: u16, channel_count: u16) -> Self {
|
||||||
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
||||||
let container_bytes_per_sample= container_bits_per_sample / 8;
|
let container_bytes_per_sample= container_bits_per_sample / 8;
|
||||||
|
|
||||||
let tag : u16 = match channel_count {
|
WaveFmt {
|
||||||
1..=2 => 0x01,
|
tag : 0xFFFE,
|
||||||
x if x > 2 => 0xFFFE,
|
channel_count,
|
||||||
x => panic!("Invalid channel count {}", x)
|
sample_rate,
|
||||||
|
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: Some(WaveFmtExtended {
|
||||||
|
valid_bits_per_sample: bits_per_sample,
|
||||||
|
channel_mask: ChannelMask::DirectOut as u32,
|
||||||
|
type_guid: UUID_BFORMAT_PCM
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format `WaveFmt` with a custom channel bitmap.
|
||||||
|
///
|
||||||
|
/// The order of `channels` is not important. When reading or writing
|
||||||
|
/// audio frames you must use the standard multichannel order for Wave
|
||||||
|
/// files, the numerical order of the cases of `ChannelMask`.
|
||||||
|
pub fn new_pcm_multichannel(sample_rate: u32, bits_per_sample: u16, channel_bitmap: u32) -> Self {
|
||||||
|
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
||||||
|
let container_bytes_per_sample= container_bits_per_sample / 8;
|
||||||
|
|
||||||
|
let channel_count: u16 = (0..=31).fold(0u16, |accum, n| accum + (0x1 & (channel_bitmap >> n) as u16) );
|
||||||
|
|
||||||
|
let result : (u16, Option<WaveFmtExtended>) = match channel_bitmap {
|
||||||
|
ch if bits_per_sample != container_bits_per_sample => (
|
||||||
|
(0xFFFE, Some(WaveFmtExtended { valid_bits_per_sample: bits_per_sample, channel_mask: ch,
|
||||||
|
type_guid: UUID_PCM }) )
|
||||||
|
),
|
||||||
|
0b0100 => (0x0001, None),
|
||||||
|
0b0011 => (0x0001, None),
|
||||||
|
ch => (
|
||||||
|
(0xFFFE, Some( WaveFmtExtended { valid_bits_per_sample: bits_per_sample, channel_mask: ch,
|
||||||
|
type_guid: UUID_PCM}))
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (tag, extformat) = result;
|
||||||
|
|
||||||
WaveFmt {
|
WaveFmt {
|
||||||
tag,
|
tag,
|
||||||
channel_count,
|
channel_count,
|
||||||
@@ -200,17 +246,7 @@ impl WaveFmt {
|
|||||||
bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32,
|
bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32,
|
||||||
block_alignment: container_bytes_per_sample * channel_count,
|
block_alignment: container_bytes_per_sample * channel_count,
|
||||||
bits_per_sample: container_bits_per_sample,
|
bits_per_sample: container_bits_per_sample,
|
||||||
extended_format: {
|
extended_format: extformat
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ mod wavewriter;
|
|||||||
|
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
pub use wavereader::WaveReader;
|
pub use wavereader::WaveReader;
|
||||||
pub use wavewriter::WaveWriter;
|
pub use wavewriter::{WaveWriter, AudioFrameWriter};
|
||||||
pub use bext::Bext;
|
pub use bext::Bext;
|
||||||
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor, ChannelMask, ADMAudioID};
|
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor, ChannelMask, ADMAudioID};
|
||||||
pub use common_format::CommonFormat;
|
pub use common_format::CommonFormat;
|
||||||
|
|||||||
@@ -2,20 +2,27 @@ use std::fs::File;
|
|||||||
use std::io::{Write,Seek,SeekFrom};
|
use std::io::{Write,Seek,SeekFrom};
|
||||||
|
|
||||||
use super::Error;
|
use super::Error;
|
||||||
use super::fourcc::{FourCC, WriteFourCC, RIFF_SIG, WAVE_SIG, FMT__SIG, DATA_SIG, ELM1_SIG};
|
use super::fourcc::{FourCC, WriteFourCC, RIFF_SIG, RF64_SIG, DS64_SIG,
|
||||||
|
WAVE_SIG, FMT__SIG, DATA_SIG, ELM1_SIG, JUNK_SIG, BEXT_SIG};
|
||||||
use super::fmt::WaveFmt;
|
use super::fmt::WaveFmt;
|
||||||
//use super::common_format::CommonFormat;
|
//use super::common_format::CommonFormat;
|
||||||
use super::chunks::WriteBWaveChunks;
|
use super::chunks::WriteBWaveChunks;
|
||||||
|
use super::bext::Bext;
|
||||||
|
|
||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use byteorder::WriteBytesExt;
|
use byteorder::WriteBytesExt;
|
||||||
|
|
||||||
|
/// Write audio frames to a `WaveWriter`.
|
||||||
|
///
|
||||||
|
///
|
||||||
pub struct AudioFrameWriter<W> where W: Write + Seek {
|
pub struct AudioFrameWriter<W> where W: Write + Seek {
|
||||||
inner : WaveChunkWriter<W>
|
inner : WaveChunkWriter<W>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> AudioFrameWriter<W> where W: Write + Seek {
|
impl<W> AudioFrameWriter<W> where W: Write + Seek {
|
||||||
|
|
||||||
|
/// Write one audio frame.
|
||||||
|
///
|
||||||
pub fn write_integer_frame(&mut self, buffer: &[i32]) -> Result<u64,Error> {
|
pub fn write_integer_frame(&mut self, buffer: &[i32]) -> Result<u64,Error> {
|
||||||
let format = self.inner.inner.format;
|
let format = self.inner.inner.format;
|
||||||
assert!(buffer.len() as u16 == format.channel_count,
|
assert!(buffer.len() as u16 == format.channel_count,
|
||||||
@@ -34,17 +41,29 @@ impl<W> AudioFrameWriter<W> where W: Write + Seek {
|
|||||||
b, format.channel_count, format.block_alignment)
|
b, format.channel_count, format.block_alignment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.inner.flush()?;
|
||||||
Ok(1)
|
Ok(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finish writing audio frames and unwrap the inner `WaveWriter`.
|
||||||
|
///
|
||||||
|
/// This method must be called when the client has finished writing audio
|
||||||
|
/// data. This will finalize the audio data chunk.
|
||||||
pub fn end(self) -> Result<WaveWriter<W>, Error> {
|
pub fn end(self) -> Result<WaveWriter<W>, Error> {
|
||||||
self.inner.end()
|
self.inner.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write a wave data chunk.
|
||||||
|
///
|
||||||
|
/// `WaveChunkWriter` implements `Write` and as bytes are written to it,
|
||||||
|
///
|
||||||
|
/// ### Important!
|
||||||
|
///
|
||||||
|
/// When you are done writing to a chunk you must call `end()` in order to
|
||||||
|
/// finalize the chunk for storage.
|
||||||
pub struct WaveChunkWriter<W> where W: Write + Seek {
|
pub struct WaveChunkWriter<W> where W: Write + Seek {
|
||||||
|
ident : FourCC,
|
||||||
inner : WaveWriter<W>,
|
inner : WaveWriter<W>,
|
||||||
content_start_pos : u64,
|
content_start_pos : u64,
|
||||||
length : u64
|
length : u64
|
||||||
@@ -58,7 +77,7 @@ impl<W> WaveChunkWriter<W> where W: Write + Seek {
|
|||||||
inner.inner.write_u32::<LittleEndian>(length as u32)?;
|
inner.inner.write_u32::<LittleEndian>(length as u32)?;
|
||||||
inner.increment_form_length(8)?;
|
inner.increment_form_length(8)?;
|
||||||
let content_start_pos = inner.inner.seek(SeekFrom::End(0))?;
|
let content_start_pos = inner.inner.seek(SeekFrom::End(0))?;
|
||||||
Ok( WaveChunkWriter { inner , content_start_pos, length } )
|
Ok( WaveChunkWriter { ident, inner , content_start_pos, length } )
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(mut self) -> Result<WaveWriter<W>, Error> {
|
fn end(mut self) -> Result<WaveWriter<W>, Error> {
|
||||||
@@ -72,11 +91,22 @@ impl<W> WaveChunkWriter<W> where W: Write + Seek {
|
|||||||
|
|
||||||
fn increment_chunk_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
fn increment_chunk_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
||||||
self.length = self.length + amount;
|
self.length = self.length + amount;
|
||||||
if self.length < u32::MAX as u64 {
|
if !self.inner.is_rf64 {
|
||||||
self.inner.inner.seek(SeekFrom::Start(self.content_start_pos - 4))?;
|
self.inner.inner.seek(SeekFrom::Start(self.content_start_pos - 4))?;
|
||||||
self.inner.inner.write_u32::<LittleEndian>(self.length as u32)?;
|
self.inner.inner.write_u32::<LittleEndian>(self.length as u32)?;
|
||||||
} else {
|
} else {
|
||||||
todo!()
|
if self.ident == DATA_SIG {
|
||||||
|
let data_chunk_64bit_field_offset = 8 + 4 + 8 + 8;
|
||||||
|
self.inner.inner.seek(SeekFrom::Start(self.content_start_pos - 4))?;
|
||||||
|
self.inner.inner.write_u32::<LittleEndian>(0xFFFF_FFFF)?;
|
||||||
|
// this only need to happen once, not every time we increment
|
||||||
|
|
||||||
|
self.inner.inner.seek(SeekFrom::Start(data_chunk_64bit_field_offset))?;
|
||||||
|
self.inner.inner.write_u64::<LittleEndian>(self.length)?;
|
||||||
|
} else {
|
||||||
|
todo!("FIXME RF64 wave writing is not yet supported for chunks other than `data`")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -88,8 +118,8 @@ impl<W> Write for WaveChunkWriter<W> where W: Write + Seek {
|
|||||||
fn write(&mut self, buffer: &[u8]) -> Result<usize, std::io::Error> {
|
fn write(&mut self, buffer: &[u8]) -> Result<usize, std::io::Error> {
|
||||||
self.inner.inner.seek(SeekFrom::End(0))?;
|
self.inner.inner.seek(SeekFrom::End(0))?;
|
||||||
let written = self.inner.inner.write(buffer)?;
|
let written = self.inner.inner.write(buffer)?;
|
||||||
self.increment_chunk_length(written as u64)?;
|
|
||||||
self.inner.increment_form_length(written as u64)?;
|
self.inner.increment_form_length(written as u64)?;
|
||||||
|
self.increment_chunk_length(written as u64)?;
|
||||||
|
|
||||||
Ok( written )
|
Ok( written )
|
||||||
}
|
}
|
||||||
@@ -101,13 +131,33 @@ impl<W> Write for WaveChunkWriter<W> where W: Write + Seek {
|
|||||||
|
|
||||||
/// Wave, Broadcast-WAV and RF64/BW64 writer.
|
/// Wave, Broadcast-WAV and RF64/BW64 writer.
|
||||||
///
|
///
|
||||||
|
/// A `WaveWriter` creates a new wave file at the given path (with `create()`)
|
||||||
|
/// or into the given `Write`- and `Seek`-able inner writer.
|
||||||
|
///
|
||||||
|
/// Audio is added to the wave file by starting the audio data chunk with
|
||||||
|
/// `WaveWriter::audio_frame_writer()`. All of the functions that add chunks
|
||||||
|
/// move the WaveWriter and return it to the host when complete.
|
||||||
|
///
|
||||||
|
/// # Structure of New Wave Files
|
||||||
|
///
|
||||||
|
/// `WaveWriter` will create a Wave file with two chunks automatically: a 96
|
||||||
|
/// byte `JUNK` chunk and a standard `fmt ` chunk, which has the extended
|
||||||
|
/// length if the format your provided requires it. The first `JUNK` chunk is
|
||||||
|
/// a reservation for a `ds64` record which will be written over it if
|
||||||
|
/// the file needs to be upgraded to RF64 format.
|
||||||
|
///
|
||||||
|
/// Chunks are added to the file in the order the client adds them.
|
||||||
|
/// `audio_file_writer()` will add a `data` chunk for the audio data, and will
|
||||||
|
/// also add an `elm1` filler chunk prior to the data chunk to ensure that the
|
||||||
|
/// first byte of the data chunk's content is aligned with 0x4000.
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bwavfile::{WaveWriter,WaveFmt};
|
/// use bwavfile::{WaveWriter,WaveFmt};
|
||||||
/// # use std::io::Cursor;
|
/// # use std::io::Cursor;
|
||||||
///
|
///
|
||||||
/// // Write a three-sample wave file to a cursor
|
/// // Write a three-sample wave file to a cursor
|
||||||
/// let mut cursor = Cursor::new(vec![0u8;0]);
|
/// let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
/// let format = WaveFmt::new_pcm(48000, 24, 1);
|
/// let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
/// let w = WaveWriter::new(&mut cursor, format).unwrap();
|
/// let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
///
|
///
|
||||||
/// let mut frame_writer = w.audio_frame_writer().unwrap();
|
/// let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
@@ -121,6 +171,9 @@ pub struct WaveWriter<W> where W: Write + Seek {
|
|||||||
inner : W,
|
inner : W,
|
||||||
form_length: u64,
|
form_length: u64,
|
||||||
|
|
||||||
|
/// True if file is RF64
|
||||||
|
pub is_rf64: bool,
|
||||||
|
|
||||||
/// Format of the wave file.
|
/// Format of the wave file.
|
||||||
pub format: WaveFmt
|
pub format: WaveFmt
|
||||||
}
|
}
|
||||||
@@ -146,25 +199,57 @@ impl<W> WaveWriter<W> where W: Write + Seek {
|
|||||||
inner.write_u32::<LittleEndian>(0)?;
|
inner.write_u32::<LittleEndian>(0)?;
|
||||||
inner.write_fourcc(WAVE_SIG)?;
|
inner.write_fourcc(WAVE_SIG)?;
|
||||||
|
|
||||||
let mut retval = WaveWriter { inner, form_length: 0, format};
|
let mut retval = WaveWriter { inner, form_length: 0, is_rf64: false, format};
|
||||||
|
|
||||||
retval.increment_form_length(4)?;
|
retval.increment_form_length(4)?;
|
||||||
|
|
||||||
let mut chunk = retval.begin_chunk(FMT__SIG)?;
|
let mut chunk = retval.chunk(JUNK_SIG)?;
|
||||||
|
chunk.write(&[0u8; 96])?;
|
||||||
|
let retval = chunk.end()?;
|
||||||
|
|
||||||
|
let mut chunk = retval.chunk(FMT__SIG)?;
|
||||||
chunk.write_wave_fmt(&format)?;
|
chunk.write_wave_fmt(&format)?;
|
||||||
let retval = chunk.end()?;
|
let retval = chunk.end()?;
|
||||||
|
|
||||||
Ok( retval )
|
Ok( retval )
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new chunk writer, which takes posession of the `WaveWriter`.
|
fn promote_to_rf64(&mut self) -> Result<(), std::io::Error> {
|
||||||
///
|
if !self.is_rf64 {
|
||||||
/// Begin writing a chunk segment. To close the chunk (and perhaps write
|
self.inner.seek(SeekFrom::Start(0))?;
|
||||||
/// another), call `end()` on the chunk writer.
|
self.inner.write_fourcc(RF64_SIG)?;
|
||||||
pub fn begin_chunk(mut self, ident: FourCC) -> Result<WaveChunkWriter<W>,Error> {
|
self.inner.write_u32::<LittleEndian>(0xFFFF_FFFF)?;
|
||||||
|
self.inner.seek(SeekFrom::Start(12))?;
|
||||||
|
|
||||||
|
self.inner.write_fourcc(DS64_SIG)?;
|
||||||
|
self.inner.seek(SeekFrom::Current(4))?;
|
||||||
|
self.inner.write_u64::<LittleEndian>(self.form_length)?;
|
||||||
|
self.is_rf64 = true;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn chunk(mut self, ident: FourCC) -> Result<WaveChunkWriter<W>,Error> {
|
||||||
self.inner.seek(SeekFrom::End(0))?;
|
self.inner.seek(SeekFrom::End(0))?;
|
||||||
WaveChunkWriter::begin(self, ident)
|
WaveChunkWriter::begin(self, ident)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write Broadcast-Wave metadata to the file.
|
||||||
|
///
|
||||||
|
/// This function will write the metadata chunk immediately to the end of
|
||||||
|
/// the file; if you have already written and closed the audio data the
|
||||||
|
/// bext chunk will be positioned after it.
|
||||||
|
fn write_broadcast_metadata(self, bext: &Bext) -> Result<Self,Error> {
|
||||||
|
let mut b = self.chunk(BEXT_SIG)?;
|
||||||
|
b.write_bext(bext)?;
|
||||||
|
Ok(b.end()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an audio frame writer, which takes possession of the callee
|
||||||
|
/// `WaveWriter`.
|
||||||
|
///
|
||||||
|
///
|
||||||
pub fn audio_frame_writer(mut self) -> Result<AudioFrameWriter<W>, Error> {
|
pub fn audio_frame_writer(mut self) -> Result<AudioFrameWriter<W>, Error> {
|
||||||
// append elm1 chunk
|
// append elm1 chunk
|
||||||
|
|
||||||
@@ -172,19 +257,26 @@ impl<W> WaveWriter<W> where W: Write + Seek {
|
|||||||
|
|
||||||
let lip = self.inner.seek(SeekFrom::End(0))?;
|
let lip = self.inner.seek(SeekFrom::End(0))?;
|
||||||
let to_add = framing - (lip % framing) - 16;
|
let to_add = framing - (lip % framing) - 16;
|
||||||
let mut chunk = self.begin_chunk(ELM1_SIG)?;
|
let mut chunk = self.chunk(ELM1_SIG)?;
|
||||||
let buf = vec![0u8; to_add as usize];
|
let buf = vec![0u8; to_add as usize];
|
||||||
chunk.write(&buf)?;
|
chunk.write(&buf)?;
|
||||||
let closed = chunk.end()?;
|
let closed = chunk.end()?;
|
||||||
|
let inner = closed.chunk(DATA_SIG)?;
|
||||||
let inner = closed.begin_chunk(DATA_SIG)?;
|
|
||||||
Ok( AudioFrameWriter { inner } )
|
Ok( AudioFrameWriter { inner } )
|
||||||
}
|
}
|
||||||
|
|
||||||
fn increment_form_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
fn increment_form_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
||||||
self.form_length = self.form_length + amount;
|
self.form_length = self.form_length + amount;
|
||||||
self.inner.seek(SeekFrom::Start(4))?;
|
if self.is_rf64 {
|
||||||
self.inner.write_u32::<LittleEndian>(self.form_length as u32)?;
|
self.inner.seek(SeekFrom::Start(8 + 4 + 8))?;
|
||||||
|
self.inner.write_u64::<LittleEndian>(self.form_length)?;
|
||||||
|
} else if self.form_length < u32::MAX as u64 {
|
||||||
|
self.inner.seek(SeekFrom::Start(4))?;
|
||||||
|
self.inner.write_u32::<LittleEndian>(self.form_length as u32)?;
|
||||||
|
} else {
|
||||||
|
self.promote_to_rf64()?;
|
||||||
|
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,16 +289,23 @@ fn test_new() {
|
|||||||
use byteorder::ReadBytesExt;
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
let mut cursor = Cursor::new(vec![0u8;0]);
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
let format = WaveFmt::new_pcm(4800, 24, 1);
|
let format = WaveFmt::new_pcm_mono(4800, 24);
|
||||||
WaveWriter::new(&mut cursor, format).unwrap();
|
WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
cursor.seek(SeekFrom::Start(0)).unwrap();
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
||||||
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
|
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), JUNK_SIG);
|
||||||
|
let junk_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
assert_eq!(junk_size,96);
|
||||||
|
cursor.seek(SeekFrom::Current(junk_size as i64)).unwrap();
|
||||||
|
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
||||||
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
|
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
assert_eq!(form_size, fmt_size + 8 + 4);
|
assert_eq!(form_size, 4 + 8 + junk_size + 8 + fmt_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -216,7 +315,7 @@ fn test_write_audio() {
|
|||||||
use byteorder::ReadBytesExt;
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
let mut cursor = Cursor::new(vec![0u8;0]);
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
let format = WaveFmt::new_pcm(48000, 24, 1);
|
let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
let w = WaveWriter::new(&mut cursor, format).unwrap();
|
let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
let mut frame_writer = w.audio_frame_writer().unwrap();
|
let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
@@ -231,22 +330,120 @@ fn test_write_audio() {
|
|||||||
|
|
||||||
cursor.seek(SeekFrom::Start(0)).unwrap();
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
||||||
let _ = cursor.read_u32::<LittleEndian>().unwrap();
|
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
|
||||||
let seek = cursor.read_u32::<LittleEndian>().unwrap();
|
|
||||||
cursor.seek(SeekFrom::Current(seek as i64)).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), ELM1_SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG); //4
|
||||||
let seek = cursor.read_u32::<LittleEndian>().unwrap();
|
|
||||||
cursor.seek(SeekFrom::Current(seek as i64)).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.read_fourcc().unwrap(), DATA_SIG);
|
assert_eq!(cursor.read_fourcc().unwrap(), JUNK_SIG); //4
|
||||||
let data_size = cursor.read_u32::<LittleEndian>().unwrap();
|
let junk_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(junk_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG); //4
|
||||||
|
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(fmt_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), ELM1_SIG); //4
|
||||||
|
let elm1_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(elm1_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), DATA_SIG); //4
|
||||||
|
let data_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
assert_eq!(data_size, 9);
|
assert_eq!(data_size, 9);
|
||||||
|
|
||||||
let tell = cursor.seek(SeekFrom::Current(0)).unwrap();
|
let tell = cursor.seek(SeekFrom::Current(0)).unwrap();
|
||||||
assert!(tell % 0x4000 == 0);
|
assert!(tell % 0x4000 == 0);
|
||||||
|
|
||||||
|
assert_eq!(form_size, 4 + 8 + junk_size + 8 + fmt_size + 8 + elm1_size + 8 + data_size + data_size % 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_bext() {
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
|
let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
|
let bext = Bext {
|
||||||
|
description: String::from("Test description"),
|
||||||
|
originator: String::from(""),
|
||||||
|
originator_reference: String::from(""),
|
||||||
|
origination_date: String::from("2020-01-01"),
|
||||||
|
origination_time: String::from("12:34:56"),
|
||||||
|
time_reference: 0,
|
||||||
|
version: 0,
|
||||||
|
umid: None,
|
||||||
|
loudness_value: None,
|
||||||
|
loudness_range: None,
|
||||||
|
max_true_peak_level: None,
|
||||||
|
max_momentary_loudness: None,
|
||||||
|
max_short_term_loudness: None,
|
||||||
|
coding_history: String::from(""),
|
||||||
|
};
|
||||||
|
|
||||||
|
let w = w.write_broadcast_metadata(&bext).unwrap();
|
||||||
|
|
||||||
|
let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
|
|
||||||
|
frame_writer.write_integer_frame(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frame(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frame(&[0i32]).unwrap();
|
||||||
|
|
||||||
|
frame_writer.end().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE! This test of RF64 writing passes on my machine but because it takes
|
||||||
|
// nearly 5 mins to run I have omitted it from the source for now...
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn test_create_rf64() {
|
||||||
|
// use std::io::Cursor;
|
||||||
|
// use super::fourcc::ReadFourCC;
|
||||||
|
// use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
|
// let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
// let format = WaveFmt::new_pcm_stereo(48000, 24);
|
||||||
|
// let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
// let buf = format.create_frame_buffer();
|
||||||
|
|
||||||
|
// let four_and_a_half_hours = 48000 * 16_200; // 4,665,600,000 bytes / 777,600,000 frames
|
||||||
|
|
||||||
|
// let mut af = w.audio_frame_writer().unwrap();
|
||||||
|
|
||||||
|
// for _ in 0..four_and_a_half_hours {
|
||||||
|
// af.write_integer_frame(&buf).unwrap();
|
||||||
|
// }
|
||||||
|
// af.end().unwrap();
|
||||||
|
|
||||||
|
// let expected_data_length = four_and_a_half_hours * format.block_alignment as u64;
|
||||||
|
|
||||||
|
// cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), RF64_SIG);
|
||||||
|
// assert_eq!(cursor.read_u32::<LittleEndian>().unwrap(), 0xFFFF_FFFF);
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
||||||
|
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), DS64_SIG);
|
||||||
|
// let ds64_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
// let form_size = cursor.read_u64::<LittleEndian>().unwrap();
|
||||||
|
// let data_size = cursor.read_u64::<LittleEndian>().unwrap();
|
||||||
|
// assert_eq!(data_size, expected_data_length);
|
||||||
|
// cursor.seek(SeekFrom::Current(ds64_size as i64 - 16)).unwrap();
|
||||||
|
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
||||||
|
// let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
// cursor.seek(SeekFrom::Current((fmt_size + fmt_size % 2) as i64)).unwrap();
|
||||||
|
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), ELM1_SIG);
|
||||||
|
// let elm1_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
// let data_start = cursor.seek(SeekFrom::Current((elm1_size + elm1_size % 2) as i64)).unwrap();
|
||||||
|
|
||||||
|
// assert!((data_start + 8) % 0x4000 == 0, "data content start is not aligned, starts at {}", data_start + 8);
|
||||||
|
// assert_eq!(cursor.read_fourcc().unwrap(), DATA_SIG);
|
||||||
|
// assert_eq!(cursor.read_u32::<LittleEndian>().unwrap(), 0xFFFF_FFFF);
|
||||||
|
// cursor.seek(SeekFrom::Current(data_size as i64)).unwrap();
|
||||||
|
|
||||||
|
// assert_eq!(4 + 8 + ds64_size as u64 + 8 + data_size + 8 + fmt_size as u64 + 8 + elm1_size as u64, form_size)
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user