mirror of
https://github.com/iluvcapra/bwavfile.git
synced 2026-01-01 17:30:44 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e1c368862 | ||
|
|
8985361029 | ||
|
|
e23529e7b6 | ||
|
|
538aedd413 | ||
|
|
09a9413ff2 | ||
|
|
016f5e3e3b | ||
|
|
11b834be76 | ||
|
|
a4ded50112 | ||
|
|
9b2a9783a0 | ||
|
|
1e53436d4b | ||
|
|
a9d9081fad | ||
|
|
c43144c9db | ||
|
|
082a8596af | ||
|
|
208aa7f064 | ||
|
|
69da33b3dc | ||
|
|
9cbb2664d4 | ||
|
|
a4c4936665 |
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.1.5"
|
version = "0.1.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"encoding",
|
"encoding",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bwavfile"
|
name = "bwavfile"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@@ -16,5 +16,9 @@ keywords = ["audio", "broadcast", "multimedia","smpte"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.3.4"
|
byteorder = "1.3.4"
|
||||||
encoding = "0.2.33"
|
encoding = "0.2.33"
|
||||||
|
serde_json = "1.0.59"
|
||||||
uuid = "0.8.1"
|
uuid = "0.8.1"
|
||||||
serde_json = "1.0.59"
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
|
|||||||
18
README.md
18
README.md
@@ -8,9 +8,25 @@ Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support
|
|||||||
|
|
||||||
This is currently a work-in-progress!
|
This is currently a work-in-progress!
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Read standard WAV, Broadcast-Wave, and 64-bit RF64 and BW64 wave files with one interface for
|
||||||
|
all types with transparent format detection.
|
||||||
|
- Unified format definition interface for standard and extended-format wave files.
|
||||||
|
- Read channel/speaker map metadata.
|
||||||
|
- Read standard EBU Broadcast-Wave metadata and decode to fields, including timestamp and SMPTE UMID.
|
||||||
|
- Validate the compatibility of a given wave file for certain regimes.
|
||||||
|
- Metadata support for ambisonic B-format.
|
||||||
|
|
||||||
|
### In Progress
|
||||||
|
|
||||||
|
- Wave/RF64 file writing.
|
||||||
|
- iXML and ADM XML metadata reading/writing.
|
||||||
|
|
||||||
|
|
||||||
## Use Examples
|
## Use Examples
|
||||||
|
|
||||||
### Reading a File
|
### Reading Audio Frames From a File
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::io::{Read, Seek};
|
use std::io::{Read, Seek};
|
||||||
use std::io::SeekFrom::{Start,};
|
use std::io::SeekFrom::{Start,Current,};
|
||||||
|
|
||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use byteorder::ReadBytesExt;
|
use byteorder::ReadBytesExt;
|
||||||
@@ -16,7 +16,9 @@ use super::CommonFormat;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct AudioFrameReader<R: Read + Seek> {
|
pub struct AudioFrameReader<R: Read + Seek> {
|
||||||
inner : R,
|
inner : R,
|
||||||
format: WaveFmt
|
format: WaveFmt,
|
||||||
|
start: u64,
|
||||||
|
length: u64
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Read + Seek> AudioFrameReader<R> {
|
impl<R: Read + Seek> AudioFrameReader<R> {
|
||||||
@@ -29,16 +31,21 @@ impl<R: Read + Seek> AudioFrameReader<R> {
|
|||||||
/// parameter to confirm the `block_alignment` law is fulfilled
|
/// parameter to confirm the `block_alignment` law is fulfilled
|
||||||
/// and the format tag is readable by this implementation (only
|
/// and the format tag is readable by this implementation (only
|
||||||
/// format 0x01 is supported at this time.)
|
/// format 0x01 is supported at this time.)
|
||||||
pub fn new(inner: R, format: WaveFmt) -> Self {
|
pub fn new(mut inner: R, format: WaveFmt, start: u64, length: u64) -> Result<Self, Error> {
|
||||||
assert!(format.block_alignment * 8 == format.bits_per_sample * format.channel_count,
|
assert!(format.block_alignment * 8 == format.bits_per_sample * format.channel_count,
|
||||||
"Unable to read audio frames from packed formats: block alignment is {}, should be {}",
|
"Unable to read audio frames from packed formats: block alignment is {}, should be {}",
|
||||||
format.block_alignment, (format.bits_per_sample / 8 ) * format.channel_count);
|
format.block_alignment, (format.bits_per_sample / 8 ) * format.channel_count);
|
||||||
|
|
||||||
|
|
||||||
assert!(format.common_format() == CommonFormat::IntegerPCM ,
|
assert!(format.common_format() == CommonFormat::IntegerPCM ,
|
||||||
"Unsupported format tag {:?}", format.tag);
|
"Unsupported format tag {:?}", format.tag);
|
||||||
|
|
||||||
AudioFrameReader { inner , format }
|
inner.seek(Start(start))?;
|
||||||
|
Ok( AudioFrameReader { inner , format , start, length} )
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unwrap the inner reader.
|
||||||
|
pub fn into_inner(self) -> R {
|
||||||
|
self.inner
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locate the read position to a different frame
|
/// Locate the read position to a different frame
|
||||||
@@ -46,10 +53,13 @@ impl<R: Read + Seek> AudioFrameReader<R> {
|
|||||||
/// Seeks within the audio stream.
|
/// Seeks within the audio stream.
|
||||||
///
|
///
|
||||||
/// Returns the new location of the read position.
|
/// Returns the new location of the read position.
|
||||||
|
///
|
||||||
|
/// locate() behaves similarly to Read methods in that
|
||||||
|
/// seeking after the end of the audio data is not an error.
|
||||||
pub fn locate(&mut self, to :u64) -> Result<u64,Error> {
|
pub fn locate(&mut self, to :u64) -> Result<u64,Error> {
|
||||||
let position = to * self.format.block_alignment as u64;
|
let position = to * self.format.block_alignment as u64;
|
||||||
let seek_result = self.inner.seek(Start(position))?;
|
let seek_result = self.inner.seek(Start(self.start + position))?;
|
||||||
Ok( seek_result / self.format.block_alignment as u64 )
|
Ok( (seek_result - self.start) / self.format.block_alignment as u64 )
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a frame buffer sized to hold frames of the reader
|
/// Create a frame buffer sized to hold frames of the reader
|
||||||
@@ -67,8 +77,8 @@ impl<R: Read + Seek> AudioFrameReader<R> {
|
|||||||
///
|
///
|
||||||
/// Regardless of the number of bits in the audio sample, this method
|
/// Regardless of the number of bits in the audio sample, this method
|
||||||
/// always writes `i32` samples back to the buffer. These samples are
|
/// always writes `i32` samples back to the buffer. These samples are
|
||||||
/// written back "left-aligned" so samples that are shorter than i32
|
/// written back "right-aligned" so samples that are shorter than i32
|
||||||
/// will leave the MSB bits empty.
|
/// will leave the MSB bits empty.
|
||||||
///
|
///
|
||||||
/// For example: A full-code sample in 16 bit (0xFFFF) will be written
|
/// For example: A full-code sample in 16 bit (0xFFFF) will be written
|
||||||
/// back to the buffer as 0x0000FFFF.
|
/// back to the buffer as 0x0000FFFF.
|
||||||
@@ -85,17 +95,22 @@ impl<R: Read + Seek> AudioFrameReader<R> {
|
|||||||
|
|
||||||
let framed_bits_per_sample = self.format.block_alignment * 8 / self.format.channel_count;
|
let framed_bits_per_sample = self.format.block_alignment * 8 / self.format.channel_count;
|
||||||
|
|
||||||
for n in 0..(self.format.channel_count as usize) {
|
let tell = self.inner.seek(Current(0))?;
|
||||||
buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) {
|
|
||||||
(0..=8,8) => self.inner.read_u8()? as i32 - 0x80_i32, // EBU 3285 §A2.2
|
|
||||||
(9..=16,16) => self.inner.read_i16::<LittleEndian>()? as i32,
|
|
||||||
(10..=24,24) => self.inner.read_i24::<LittleEndian>()?,
|
|
||||||
(25..=32,32) => self.inner.read_i32::<LittleEndian>()?,
|
|
||||||
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
|
||||||
b, self.format.channel_count, self.format.block_alignment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok( 1 )
|
if (tell - self.start) < self.length {
|
||||||
|
for n in 0..(self.format.channel_count as usize) {
|
||||||
|
buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) {
|
||||||
|
(0..=8,8) => self.inner.read_u8()? as i32 - 0x80_i32, // EBU 3285 §A2.2
|
||||||
|
(9..=16,16) => self.inner.read_i16::<LittleEndian>()? as i32,
|
||||||
|
(10..=24,24) => self.inner.read_i24::<LittleEndian>()?,
|
||||||
|
(25..=32,32) => self.inner.read_i32::<LittleEndian>()?,
|
||||||
|
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
||||||
|
b, self.format.channel_count, self.format.block_alignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok( 1 )
|
||||||
|
} else {
|
||||||
|
Ok( 0 )
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
17
src/fmt.rs
17
src/fmt.rs
@@ -1,8 +1,7 @@
|
|||||||
use std::convert::TryFrom;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use super::errors::Error;
|
|
||||||
use super::common_format::CommonFormat;
|
use super::common_format::CommonFormat;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
|
||||||
/// ADM Audio ID record
|
/// ADM Audio ID record
|
||||||
///
|
///
|
||||||
@@ -149,7 +148,9 @@ pub struct WaveFmt {
|
|||||||
/// Count of audio channels in each frame
|
/// Count of audio channels in each frame
|
||||||
pub channel_count: u16,
|
pub channel_count: u16,
|
||||||
|
|
||||||
/// Sample rate of the audio data
|
/// Playback rate of the audio data
|
||||||
|
///
|
||||||
|
/// In frames per second.
|
||||||
pub sample_rate: u32,
|
pub sample_rate: u32,
|
||||||
|
|
||||||
/// Count of bytes per second
|
/// Count of bytes per second
|
||||||
@@ -163,6 +164,16 @@ pub struct WaveFmt {
|
|||||||
pub block_alignment: u16,
|
pub block_alignment: u16,
|
||||||
|
|
||||||
/// Count of bits stored in the file per sample
|
/// Count of bits stored in the file per sample
|
||||||
|
///
|
||||||
|
/// By rule, `bits_per_sample % 8 == 0` for Broadcast-Wave files.
|
||||||
|
///
|
||||||
|
/// Modern clients will encode
|
||||||
|
/// unusual sample sizes in normal byte sizes but will set the valid_bits
|
||||||
|
/// flag in extended format record.
|
||||||
|
///
|
||||||
|
/// Generally speaking this will be true for all modern wave files, though
|
||||||
|
/// there was an historical "packed" stereo format of 20 bits per sample,
|
||||||
|
/// 5 bytes per frame, 5 bytes block alignment.
|
||||||
pub bits_per_sample: u16,
|
pub bits_per_sample: u16,
|
||||||
|
|
||||||
/// Extended format description
|
/// Extended format description
|
||||||
|
|||||||
@@ -106,7 +106,6 @@ pub const BEXT_SIG: FourCC = FourCC::make(b"bext");
|
|||||||
pub const JUNK_SIG: FourCC = FourCC::make(b"JUNK");
|
pub const JUNK_SIG: FourCC = FourCC::make(b"JUNK");
|
||||||
pub const FLLR_SIG: FourCC = FourCC::make(b"FLLR");
|
pub const FLLR_SIG: FourCC = FourCC::make(b"FLLR");
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -119,7 +119,8 @@ mod wavereader;
|
|||||||
mod wavewriter;
|
mod wavewriter;
|
||||||
|
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
pub use wavereader::{WaveReader};
|
pub use wavereader::WaveReader;
|
||||||
|
pub use wavewriter::WaveWriter;
|
||||||
pub use bext::Bext;
|
pub use bext::Bext;
|
||||||
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor, ChannelMask};
|
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor, ChannelMask};
|
||||||
pub use common_format::CommonFormat;
|
pub use common_format::CommonFormat;
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use std::io::{Seek,Read,Error,ErrorKind};
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RawChunkReader<'a, R: Read + Seek> {
|
pub struct RawChunkReader<'a, R: Read + Seek> {
|
||||||
reader: &'a mut R,
|
reader: &'a mut R,
|
||||||
start: u64,
|
pub start: u64,
|
||||||
length: u64,
|
pub length: u64,
|
||||||
position: u64
|
position: u64
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,10 +23,6 @@ impl<'a,R: Read + Seek> RawChunkReader<'a, R> {
|
|||||||
position: 0
|
position: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn length(&self) -> u64 {
|
|
||||||
self.length
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R:Read + Seek> Read for RawChunkReader<'_, R> {
|
impl<'a, R:Read + Seek> Read for RawChunkReader<'_, R> {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ impl<R: Read + Seek> WaveReader<R> {
|
|||||||
* This function does a minimal validation on the provided stream and
|
* This function does a minimal validation on the provided stream and
|
||||||
* will return an `Err(errors::Error)` immediately if there is a structural
|
* will return an `Err(errors::Error)` immediately if there is a structural
|
||||||
* inconsistency that makes the stream unreadable or if it's missing
|
* inconsistency that makes the stream unreadable or if it's missing
|
||||||
* essential components that make interpreting the audio data impoossible.
|
* essential components that make interpreting the audio data impossible.
|
||||||
*
|
*
|
||||||
* ```rust
|
* ```rust
|
||||||
* use std::fs::File;
|
* use std::fs::File;
|
||||||
@@ -100,13 +100,13 @@ impl<R: Read + Seek> WaveReader<R> {
|
|||||||
return self.inner;
|
return self.inner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
///
|
||||||
* Create an `AudioFrameReader` for reading each audio frame.
|
/// Create an `AudioFrameReader` for reading each audio frame and consume the `WaveReader`.
|
||||||
*/
|
///
|
||||||
pub fn audio_frame_reader(&mut self) -> Result<AudioFrameReader<RawChunkReader<R>>, ParserError> {
|
pub fn audio_frame_reader(mut self) -> Result<AudioFrameReader<R>, ParserError> {
|
||||||
let format = self.format()?;
|
let format = self.format()?;
|
||||||
let audio_chunk_reader = self.chunk_reader(DATA_SIG, 0)?;
|
let audio_chunk_reader = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
||||||
Ok(AudioFrameReader::new(audio_chunk_reader, format))
|
Ok(AudioFrameReader::new(self.inner, format, audio_chunk_reader.0, audio_chunk_reader.1)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,6 +163,38 @@ impl<R: Read + Seek> WaveReader<R> {
|
|||||||
.collect() )
|
.collect() )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read iXML data.
|
||||||
|
///
|
||||||
|
/// If there are no iXML metadata present in the file,
|
||||||
|
/// Err(Error::ChunkMissing { "iXML" } will be returned.
|
||||||
|
pub fn ixml(&mut self, buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
let ixml_sig: FourCC = FourCC::make(b"iXML");
|
||||||
|
let mut chunk = self.chunk_reader(ixml_sig, 0)?;
|
||||||
|
|
||||||
|
match chunk.read_to_end(buffer) {
|
||||||
|
Ok(read) => Ok(read),
|
||||||
|
Err(error) => Err(error.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read AXML data.
|
||||||
|
///
|
||||||
|
/// By convention this will generally be ADM metadata.
|
||||||
|
///
|
||||||
|
/// If there are no iXML metadata present in the file,
|
||||||
|
/// Err(Error::ChunkMissing { "axml" } will be returned.
|
||||||
|
pub fn axml(&mut self, buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
let axml_sig: FourCC = FourCC::make(b"axml");
|
||||||
|
let mut chunk = self.chunk_reader(axml_sig, 0)?;
|
||||||
|
|
||||||
|
match chunk.read_to_end(buffer) {
|
||||||
|
Ok(read) => Ok(read),
|
||||||
|
Err(error) => Err(error.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate file is readable.
|
* Validate file is readable.
|
||||||
*
|
*
|
||||||
@@ -317,3 +349,4 @@ impl<R:Read+Seek> WaveReader<R> { /* Private Implementation */
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ use super::fourcc::{FourCC, RIFF_SIG, WAVE_SIG, FMT__SIG, JUNK_SIG, BEXT_SIG, DA
|
|||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use byteorder::WriteBytesExt;
|
use byteorder::WriteBytesExt;
|
||||||
|
|
||||||
struct WaveWriter<W> where W: Write + Seek {
|
/// This isn't working yet, do not use.
|
||||||
|
pub struct WaveWriter<W> where W: Write + Seek {
|
||||||
inner : W
|
inner : W
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,14 +92,3 @@ impl<W:Write + Seek> WaveWriter<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_chunk_append() -> Result<(), Error> {
|
|
||||||
let mut test :Vec<u8> = vec![];
|
|
||||||
let mut cursor = Cursor::new(test);
|
|
||||||
let f = WaveFmt::new_pcm(48000, 16, 1);
|
|
||||||
let mut w = WaveWriter::make(cursor, f, None)?;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ fn test_minimal_wave() {
|
|||||||
fn test_read() {
|
fn test_read() {
|
||||||
let path = "tests/media/audacity_16bit.wav";
|
let path = "tests/media/audacity_16bit.wav";
|
||||||
|
|
||||||
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
let w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
|
||||||
let mut reader = w.audio_frame_reader().unwrap();
|
let mut reader = w.audio_frame_reader().unwrap();
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ fn test_read() {
|
|||||||
fn test_locate_multichannel_read() {
|
fn test_locate_multichannel_read() {
|
||||||
let path = "tests/media/ff_pink.wav";
|
let path = "tests/media/ff_pink.wav";
|
||||||
|
|
||||||
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
let w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
|
||||||
let mut reader = w.audio_frame_reader().unwrap();
|
let mut reader = w.audio_frame_reader().unwrap();
|
||||||
|
|
||||||
@@ -157,3 +157,23 @@ fn test_channels_stereo_no_fmt_extended() {
|
|||||||
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
|
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
|
||||||
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
|
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//See issue 6 and 7
|
||||||
|
#[test]
|
||||||
|
fn test_frame_reader_consumes_reader() {
|
||||||
|
// Issue #6
|
||||||
|
use bwavfile::WaveFmt;
|
||||||
|
use bwavfile::AudioFrameReader;
|
||||||
|
use std::fs::File;
|
||||||
|
fn from_wav_filename(wav_filename: &str) -> Result<(WaveFmt, AudioFrameReader<File>), ()> {
|
||||||
|
if let Ok(mut r) = WaveReader::open(&wav_filename) {
|
||||||
|
let format = r.format().unwrap();
|
||||||
|
let frame_reader = r.audio_frame_reader().unwrap();
|
||||||
|
Ok((format, frame_reader))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _result = from_wav_filename("tests/media/pt_24bit_stereo.wav").unwrap();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user