Implementation of complex formats

This commit is contained in:
Jamie Hardt
2020-11-23 22:38:10 -08:00
parent 358aa06f7c
commit 34e473dc49
10 changed files with 275 additions and 62 deletions

7
Cargo.lock generated
View File

@@ -7,6 +7,7 @@ dependencies = [
"byteorder",
"encoding",
"serde_json",
"uuid",
]
[[package]]
@@ -107,3 +108,9 @@ dependencies = [
"ryu",
"serde",
]
[[package]]
name = "uuid"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11"

View File

@@ -16,4 +16,5 @@ keywords = ["audio", "broadcast", "multimedia","smpte"]
[dependencies]
byteorder = "1.3.4"
encoding = "0.2.33"
uuid = "0.8.1"
serde_json = "1.0.59"

View File

@@ -4,7 +4,7 @@ use std::io::SeekFrom::{Start,};
use byteorder::LittleEndian;
use byteorder::ReadBytesExt;
use super::fmt::WaveFmt;
use super::fmt::{WaveFmt};
use super::errors::Error;
/// Read audio frames
@@ -33,7 +33,10 @@ impl<R: Read + Seek> AudioFrameReader<R> {
"Unable to read audio frames from packed formats: block alignment is {}, should be {}",
format.block_alignment, (format.bits_per_sample / 8 ) * format.channel_count);
assert!(format.tag == 1, "Unsupported format tag {}", format.tag);
assert!(format.tag == 0x01 ,
"Unsupported format tag {:?}", format.tag);
AudioFrameReader { inner , format }
}
@@ -84,4 +87,3 @@ impl<R: Read + Seek> AudioFrameReader<R> {
Ok( 1 )
}
}

View File

@@ -1,7 +1,5 @@
use std::io::{Read, Write};
use super::errors::Error as ParserError;
use encoding::{DecoderTrap, EncoderTrap};
use encoding::{Encoding};
use encoding::all::ASCII;
@@ -9,7 +7,10 @@ use encoding::all::ASCII;
use byteorder::LittleEndian;
use byteorder::{ReadBytesExt, WriteBytesExt};
use super::fmt::WaveFmt;
use uuid::Uuid;
use super::errors::Error as ParserError;
use super::fmt::{WaveFmt, WaveFmtExtended};
use super::bext::Bext;
pub trait ReadBWaveChunks: Read {
@@ -26,7 +27,7 @@ pub trait WriteBWaveChunks: Write {
impl<T> WriteBWaveChunks for T where T: Write {
fn write_wave_fmt(&mut self, format : &WaveFmt) -> Result<(), ParserError> {
self.write_u16::<LittleEndian>(format.tag)?;
self.write_u16::<LittleEndian>(format.tag as u16 )?;
self.write_u16::<LittleEndian>(format.channel_count)?;
self.write_u32::<LittleEndian>(format.sample_rate)?;
self.write_u32::<LittleEndian>(format.bytes_per_second)?;
@@ -86,14 +87,34 @@ impl<T> WriteBWaveChunks for T where T: Write {
impl<T> ReadBWaveChunks for T where T: Read {
fn read_wave_fmt(&mut self) -> Result<WaveFmt, ParserError> {
let tag_value : u16;
Ok(WaveFmt {
tag: self.read_u16::<LittleEndian>()?,
tag: {
tag_value = self.read_u16::<LittleEndian>()?;
tag_value
},
channel_count: self.read_u16::<LittleEndian>()?,
sample_rate: self.read_u32::<LittleEndian>()?,
bytes_per_second: self.read_u32::<LittleEndian>()?,
block_alignment: self.read_u16::<LittleEndian>()?,
bits_per_sample: self.read_u16::<LittleEndian>()?,
extended_format: None
extended_format: {
if tag_value == 0xFFFE {
let cb_size = self.read_u16::<LittleEndian>()?;
assert!(cb_size >= 22, "Format extension is not correct size");
Some(WaveFmtExtended {
valid_bits_per_sample: self.read_u16::<LittleEndian>()?,
channel_mask: self.read_u32::<LittleEndian>()?,
type_guid: {
let mut buf : [u8; 16] = [0; 16];
self.read_exact(&mut buf)?;
Uuid::from_slice(&buf)?
}
})
} else {
None
}
}
})
}
@@ -143,7 +164,7 @@ impl<T> ReadBWaveChunks for T where T: Read {
if version > 1 { Some(val) } else { None }
},
coding_history: {
for _ in 0..=180 { self.read_u8()?; }
for _ in 0..180 { self.read_u8()?; }
let mut buf = vec![];
self.read_to_end(&mut buf)?;
ASCII.decode(&buf, DecoderTrap::Ignore).expect("Error decoding text")
@@ -151,3 +172,28 @@ impl<T> ReadBWaveChunks for T where T: Read {
})
}
}
#[test]
fn test_read_51_wav() {
use super::fmt::ChannelMask;
use super::common_format::CommonFormat;
let path = "tests/media/pt_24bit_51.wav";
let mut w = super::wavereader::WaveReader::open(path).unwrap();
let format = w.format().unwrap();
assert_eq!(format.tag, 0xFFFE);
assert_eq!(format.channel_count, 6);
assert_eq!(format.sample_rate, 48000);
let extended = format.extended_format.unwrap();
assert_eq!(extended.valid_bits_per_sample, 24);
let channels = ChannelMask::channels(extended.channel_mask, format.channel_count);
assert_eq!(channels, [ChannelMask::FrontLeft, ChannelMask::FrontRight,
ChannelMask::FrontCenter, ChannelMask::LowFrequency,
ChannelMask::BackLeft, ChannelMask::BackRight]);
assert_eq!(format.common_format(), CommonFormat::IntegerPCM);
}

86
src/common_format.rs Normal file
View File

@@ -0,0 +1,86 @@
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_FLOAT: u16 = 0x0003;
const BASIC_MPEG: u16 = 0x0050;
const BASIC_EXTENDED: u16 = 0xFFFE;
/* RC 2361 §4:
WAVE Format IDs are converted to GUIDs by inserting the hexadecimal
value of the WAVE Format ID into the XXXXXXXX part of the following
template: {XXXXXXXX-0000-0010-8000-00AA00389B71}. For example, a WAVE
Format ID of 123 has the GUID value of {00000123-0000-0010-8000-
00AA00389B71}.
*/
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,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]);
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,
0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]);
const UUID_BFORMAT_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11,
0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]);
fn uuid_from_basic_tag(tag: u16) -> Uuid {
let tail : [u8; 6] = [0x00,0xaa,0x00,0x38,0x9b,0x71];
Uuid::from_fields_le(tag as u32, 0x0000, 0x0010, &tail).unwrap()
}
/// Sample format of the Wave file.
///
///
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum CommonFormat {
IntegerPCM,
IeeeFloatPCM,
Mpeg,
AmbisonicBFormatIntegerPCM,
AmbisonicBFormatIeeeFloatPCM,
UnknownBasic(u16),
UnknownExtended(Uuid),
}
impl CommonFormat {
pub fn make(basic: u16, uuid: Option<Uuid>) -> Self {
match (basic, uuid) {
(BASIC_PCM, _) => Self::IntegerPCM,
(BASIC_FLOAT, _) => Self::IeeeFloatPCM,
(BASIC_MPEG, _) => Self::Mpeg,
(BASIC_EXTENDED, Some(UUID_PCM)) => Self::IntegerPCM,
(BASIC_EXTENDED, Some(UUID_FLOAT))=> Self::IeeeFloatPCM,
(BASIC_EXTENDED, Some(UUID_BFORMAT_PCM)) => Self::AmbisonicBFormatIntegerPCM,
(BASIC_EXTENDED, Some(UUID_BFORMAT_FLOAT)) => Self::AmbisonicBFormatIeeeFloatPCM,
(BASIC_EXTENDED, Some(x)) => CommonFormat::UnknownExtended(x),
(x, _) => CommonFormat::UnknownBasic(x)
}
}
pub fn take(self) -> (u16, Uuid) {
match self {
Self::IntegerPCM => (BASIC_PCM, UUID_PCM),
Self::IeeeFloatPCM => (BASIC_FLOAT, UUID_FLOAT),
Self::Mpeg => (BASIC_MPEG, UUID_MPEG),
Self::AmbisonicBFormatIntegerPCM => (BASIC_EXTENDED, UUID_BFORMAT_PCM),
Self::AmbisonicBFormatIeeeFloatPCM => (BASIC_EXTENDED, UUID_BFORMAT_FLOAT),
Self::UnknownBasic(x) => ( x, uuid_from_basic_tag(x) ),
Self::UnknownExtended(x) => ( BASIC_EXTENDED, x)
}
}
}

View File

@@ -1,6 +1,8 @@
use std::io;
use super::fourcc::FourCC;
use uuid;
/// Errors returned by methods in this crate.
#[derive(Debug)]
pub enum Error {
@@ -8,6 +10,9 @@ pub enum Error {
/// An `io::Error` occurred
IOError(io::Error),
/// An error occured reading a tag UUID
UuidError(uuid::Error),
/// The file does not begin with a recognized WAVE header
HeaderNotRecognized,
@@ -34,7 +39,8 @@ pub enum Error {
InsufficientDS64Reservation {expected: u64, actual: u64},
/// The file is not optimized for writing new data
DataChunkNotPreparedForAppend
DataChunkNotPreparedForAppend,
}
@@ -43,3 +49,9 @@ impl From<io::Error> for Error {
Error::IOError(error)
}
}
impl From <uuid::Error> for Error {
fn from(error: uuid::Error) -> Error {
Error::UuidError(error)
}
}

View File

@@ -1,36 +1,8 @@
use std::convert::TryFrom;
use uuid::Uuid;
use super::errors::Error;
use super::common_format::CommonFormat;
/**
* References:
* - http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf
*/
#[derive(PartialEq)]
enum FormatTags {
Integer = 0x0001,
Float = 0x0003,
Extensible = 0xFFFE
}
const PCM_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x01,0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xaa,0x00, 0x38, 0x9b, 0x71];
const FLOAT_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x03,0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xaa,0x00, 0x38, 0x9b, 0x71];
/*
http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
Integer format:
SUBTYPE_AMBISONIC_B_FORMAT_PCM
{00000001-0721-11d3-8644-C8C1CA000000}
Floating-point format:
SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT
{00000003-0721-11d3-8644-C8C1CA000000}
In the case of ambisonics, I'm guessing we'd ignore the channel map and implied
channels W, X, Y, Z
*/
/// ADM Audio ID record
///
@@ -38,7 +10,7 @@ channels W, X, Y, Z
/// channel definition or further definition in the WAV file's ADM metadata
/// chunk.
///
/// An individial channel in a WAV file can have multiple Audio IDs in an ADM
/// An individual channel in a WAV file can have multiple Audio IDs in an ADM
/// AudioProgramme.
///
/// See BS.2088-1 § 8, also BS.2094, also blahblahblah...
@@ -57,7 +29,7 @@ pub struct ChannelDescriptor {
///
/// This is either implied (in the case of mono or stereo wave files) or
/// explicitly given in `WaveFormatExtentended` for files with more tracks.
speaker: WaveFmtExtendedChannelMask,
speaker: ChannelMask,
/// ADM audioTrackUIDs
adm_track_audio_ids: Vec<ADMAudioID>,
@@ -71,8 +43,8 @@ https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/subformat-guids-
These are from http://dream.cs.bath.ac.uk/researchdev/wave-ex/mulchaud.rtf
*/
#[derive(Debug)]
pub enum WaveFmtExtendedChannelMask {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ChannelMask {
DirectOut = 0x0,
FrontLeft = 0x1,
FrontRight = 0x2,
@@ -94,13 +66,53 @@ pub enum WaveFmtExtendedChannelMask {
TopBackRight = 0x20000,
}
impl From<u32> for ChannelMask {
fn from(value: u32) -> Self {
match value {
0x1 => Self::FrontLeft,
0x2 => Self::FrontRight,
0x4 => Self::FrontCenter,
0x8 => Self::LowFrequency,
0x10 => Self::BackLeft,
0x20 => Self::BackRight,
0x40 => Self::FrontCenterLeft,
0x80 => Self::FrontCenterRight,
0x100 => Self::BackCenter,
0x200 => Self::SideLeft,
0x400 => Self::SideRight,
0x800 => Self::TopCenter,
0x1000 => Self::TopFrontLeft,
0x2000 => Self::TopFrontCenter,
0x4000 => Self::TopFrontRight,
0x8000 => Self::TopBackLeft,
0x10000 => Self::TopBackCenter,
0x20000 => Self::TopBackRight,
_ => Self::DirectOut
}
}
}
impl ChannelMask {
pub fn channels(input_mask : u32, channel_count: u16) -> Vec<ChannelMask> {
let reserved_mask = 0xfff2_0000_u32;
if (input_mask & reserved_mask) > 0 {
vec![ ChannelMask::DirectOut ; channel_count as usize ]
} else {
(0..18).map(|i| 1 << i )
.filter(|mask| mask & input_mask > 0)
.map(|mask| Into::<ChannelMask>::into(mask))
.collect()
}
}
}
/**
* Extended Wave Format
*
* https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible
*/
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct WaveFmtExtended {
/// Valid bits per sample
@@ -109,12 +121,12 @@ pub struct WaveFmtExtended {
/// Channel mask
///
/// Identifies the speaker assignment for each channel in the file
pub channel_mask : WaveFmtExtendedChannelMask,
pub channel_mask : u32,
/// Codec GUID
///
/// Identifies the codec of the audio stream
pub type_guid : [u8; 16],
pub type_guid : Uuid,
}
/**
@@ -125,7 +137,7 @@ pub struct WaveFmtExtended {
* rate, sample binary format, channel count, etc.
*
*/
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct WaveFmt {
/// A tag identifying the codec in use.
@@ -169,9 +181,9 @@ impl WaveFmt {
let container_bytes_per_sample= container_bits_per_sample / 8;
let tag : u16 = match channel_count {
0 => panic!("Error"),
1..=2 => FormatTags::Integer as u16,
_ => FormatTags::Extensible as u16,
1..=2 => 0x01,
x if x > 2 => 0xFFFE,
x => panic!("Invalid channel count {}", x)
};
WaveFmt {
@@ -184,5 +196,47 @@ impl WaveFmt {
extended_format: None
}
}
pub fn common_format(&self) -> CommonFormat {
CommonFormat::make( self.tag, self.extended_format.map(|ext| ext.type_guid))
}
pub fn channels(&self) -> Vec<ChannelDescriptor> {
match self.channel_count {
1 => vec![
ChannelDescriptor {
index: 0,
speaker: ChannelMask::FrontCenter,
adm_track_audio_ids: vec![]
}
],
2 => vec![
ChannelDescriptor {
index: 0,
speaker: ChannelMask::FrontLeft,
adm_track_audio_ids: vec![]
},
ChannelDescriptor {
index: 1,
speaker: ChannelMask::FrontRight,
adm_track_audio_ids: vec![]
}
],
x if x > 2 => {
let channel_mask = self.extended_format.map(|x| x.channel_mask).unwrap_or(0);
let channels = ChannelMask::channels(channel_mask, self.channel_count);
let channels_expanded = channels.iter().chain(std::iter::repeat(&ChannelMask::DirectOut));
(0..self.channel_count)
.zip(channels_expanded)
.map(|(n,chan)| ChannelDescriptor {
index: n,
speaker: *chan,
adm_track_audio_ids: vec![]
}).collect()
},
x => panic!("Channel count ({}) was illegal!", x),
}
}
}

View File

@@ -100,10 +100,14 @@ Things that are _not_ necessarily in the scope of this package:
extern crate encoding;
extern crate byteorder;
extern crate uuid;
mod parser;
mod fourcc;
mod errors;
mod common_format;
mod parser;
mod raw_chunk_reader;
mod audio_frame_reader;
@@ -114,8 +118,9 @@ mod fmt;
mod wavereader;
mod wavewriter;
pub use errors::Error;
pub use wavereader::{WaveReader};
pub use bext::Bext;
pub use fmt::{WaveFmt, WaveFmtExtended};
pub use errors::Error;
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor};
pub use common_format::CommonFormat;
pub use audio_frame_reader::AudioFrameReader;

View File

@@ -9,8 +9,8 @@ use super::fmt::WaveFmt;
use super::bext::Bext;
use super::audio_frame_reader::AudioFrameReader;
use super::chunks::ReadBWaveChunks;
//use super::validation;
//use std::io::SeekFrom::{Start};
use std::io::{Read, Seek};

View File

@@ -27,7 +27,7 @@ fn test_format_silence() -> Result<(),Error> {
assert_eq!(format.sample_rate, 44100);
assert_eq!(format.channel_count, 1);
assert_eq!(format.tag, 1);
assert_eq!(format.tag as u16, 1);
Ok( () )
}