mirror of
https://github.com/iluvcapra/bwavfile.git
synced 2025-12-31 08:50:44 +00:00
Consolidated AudioFrameWriter
This commit is contained in:
@@ -1,109 +0,0 @@
|
|||||||
use std::io::{Read, Seek};
|
|
||||||
use std::io::SeekFrom::{Start,Current,};
|
|
||||||
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use byteorder::ReadBytesExt;
|
|
||||||
|
|
||||||
use super::fmt::{WaveFmt};
|
|
||||||
use super::errors::Error;
|
|
||||||
use super::CommonFormat;
|
|
||||||
|
|
||||||
/// Read audio frames
|
|
||||||
///
|
|
||||||
/// The inner reader is interpreted as a raw audio data
|
|
||||||
/// bitstream having a format specified by `format`.
|
|
||||||
///
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct AudioFrameReader<R: Read + Seek> {
|
|
||||||
inner : R,
|
|
||||||
format: WaveFmt,
|
|
||||||
start: u64,
|
|
||||||
length: u64
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Read + Seek> AudioFrameReader<R> {
|
|
||||||
|
|
||||||
/// Create a new `AudioFrameReader`
|
|
||||||
///
|
|
||||||
/// ### Panics
|
|
||||||
///
|
|
||||||
/// This method does a few sanity checks on the provided format
|
|
||||||
/// parameter to confirm the `block_alignment` law is fulfilled
|
|
||||||
/// and the format tag is readable by this implementation (only
|
|
||||||
/// format 0x01 is supported at this time.)
|
|
||||||
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,
|
|
||||||
"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.common_format() == CommonFormat::IntegerPCM ,
|
|
||||||
"Unsupported format tag {:?}", format.tag);
|
|
||||||
|
|
||||||
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
|
|
||||||
///
|
|
||||||
/// Seeks within the audio stream.
|
|
||||||
///
|
|
||||||
/// 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> {
|
|
||||||
let position = to * self.format.block_alignment as u64;
|
|
||||||
let seek_result = self.inner.seek(Start(self.start + position))?;
|
|
||||||
Ok( (seek_result - self.start) / self.format.block_alignment as u64 )
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Read a frame
|
|
||||||
///
|
|
||||||
/// A single frame is read from the audio stream and the read location
|
|
||||||
/// is advanced one frame.
|
|
||||||
///
|
|
||||||
/// Regardless of the number of bits in the audio sample, this method
|
|
||||||
/// always writes `i32` samples back to the buffer. These samples are
|
|
||||||
/// written back "right-aligned" so samples that are shorter than i32
|
|
||||||
/// will leave the MSB bits empty.
|
|
||||||
///
|
|
||||||
/// For example: A full-code sample in 16 bit (0xFFFF) will be written
|
|
||||||
/// back to the buffer as 0x0000FFFF.
|
|
||||||
///
|
|
||||||
///
|
|
||||||
/// ### Panics
|
|
||||||
///
|
|
||||||
/// The `buffer` must have a number of elements equal to the number of
|
|
||||||
/// channels and this method will panic if this is not the case.
|
|
||||||
pub fn read_integer_frame(&mut self, buffer:&mut [i32]) -> Result<u64,Error> {
|
|
||||||
assert!(buffer.len() as u16 == self.format.channel_count,
|
|
||||||
"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;
|
|
||||||
|
|
||||||
let tell = self.inner.seek(Current(0))?;
|
|
||||||
|
|
||||||
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 )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -50,8 +50,6 @@ mod errors;
|
|||||||
mod common_format;
|
mod common_format;
|
||||||
|
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
mod audio_frame_reader;
|
|
||||||
mod list_form;
|
mod list_form;
|
||||||
|
|
||||||
mod chunks;
|
mod chunks;
|
||||||
@@ -63,10 +61,9 @@ mod wavereader;
|
|||||||
mod wavewriter;
|
mod wavewriter;
|
||||||
|
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
pub use wavereader::WaveReader;
|
pub use wavereader::{WaveReader, AudioFrameReader};
|
||||||
pub use wavewriter::{WaveWriter, AudioFrameWriter};
|
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;
|
||||||
pub use audio_frame_reader::AudioFrameReader;
|
|
||||||
pub use cue::Cue;
|
pub use cue::Cue;
|
||||||
@@ -8,14 +8,121 @@ use super::fourcc::{FourCC, ReadFourCC, FMT__SIG,DATA_SIG, BEXT_SIG, LIST_SIG, J
|
|||||||
use super::errors::Error as ParserError;
|
use super::errors::Error as ParserError;
|
||||||
use super::fmt::{WaveFmt, ChannelDescriptor, ChannelMask};
|
use super::fmt::{WaveFmt, ChannelDescriptor, ChannelMask};
|
||||||
use super::bext::Bext;
|
use super::bext::Bext;
|
||||||
use super::audio_frame_reader::AudioFrameReader;
|
|
||||||
use super::chunks::ReadBWaveChunks;
|
use super::chunks::ReadBWaveChunks;
|
||||||
use super::cue::Cue;
|
use super::cue::Cue;
|
||||||
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::io::{Read, Seek, BufReader};
|
use std::io::{Read, Seek, BufReader};
|
||||||
|
|
||||||
|
use std::io::SeekFrom::{Start,Current,};
|
||||||
|
|
||||||
|
use byteorder::LittleEndian;
|
||||||
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
|
use super::errors::Error;
|
||||||
|
use super::CommonFormat;
|
||||||
|
|
||||||
|
|
||||||
|
/// Read audio frames
|
||||||
|
///
|
||||||
|
/// The inner reader is interpreted as a raw audio data
|
||||||
|
/// bitstream having a format specified by `format`.
|
||||||
|
///
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct AudioFrameReader<R: Read + Seek> {
|
||||||
|
inner : R,
|
||||||
|
format: WaveFmt,
|
||||||
|
start: u64,
|
||||||
|
length: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: Read + Seek> AudioFrameReader<R> {
|
||||||
|
|
||||||
|
/// Create a new `AudioFrameReader`
|
||||||
|
///
|
||||||
|
/// ### Panics
|
||||||
|
///
|
||||||
|
/// This method does a few sanity checks on the provided format
|
||||||
|
/// parameter to confirm the `block_alignment` law is fulfilled
|
||||||
|
/// and the format tag is readable by this implementation (only
|
||||||
|
/// format 0x01 is supported at this time.)
|
||||||
|
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,
|
||||||
|
"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.common_format() == CommonFormat::IntegerPCM ,
|
||||||
|
"Unsupported format tag {:?}", format.tag);
|
||||||
|
|
||||||
|
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
|
||||||
|
///
|
||||||
|
/// Seeks within the audio stream.
|
||||||
|
///
|
||||||
|
/// 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> {
|
||||||
|
let position = to * self.format.block_alignment as u64;
|
||||||
|
let seek_result = self.inner.seek(Start(self.start + position))?;
|
||||||
|
Ok( (seek_result - self.start) / self.format.block_alignment as u64 )
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Read a frame
|
||||||
|
///
|
||||||
|
/// A single frame is read from the audio stream and the read location
|
||||||
|
/// is advanced one frame.
|
||||||
|
///
|
||||||
|
/// Regardless of the number of bits in the audio sample, this method
|
||||||
|
/// always writes `i32` samples back to the buffer. These samples are
|
||||||
|
/// written back "right-aligned" so samples that are shorter than i32
|
||||||
|
/// will leave the MSB bits empty.
|
||||||
|
///
|
||||||
|
/// For example: A full-code sample in 16 bit (0xFFFF) will be written
|
||||||
|
/// back to the buffer as 0x0000FFFF.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// ### Panics
|
||||||
|
///
|
||||||
|
/// The `buffer` must have a number of elements equal to the number of
|
||||||
|
/// channels and this method will panic if this is not the case.
|
||||||
|
pub fn read_integer_frame(&mut self, buffer:&mut [i32]) -> Result<u64,Error> {
|
||||||
|
assert!(buffer.len() as u16 == self.format.channel_count,
|
||||||
|
"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;
|
||||||
|
|
||||||
|
let tell = self.inner.seek(Current(0))?;
|
||||||
|
|
||||||
|
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 )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Wave, Broadcast-WAV and RF64/BW64 parser/reader.
|
/// Wave, Broadcast-WAV and RF64/BW64 parser/reader.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -162,8 +162,7 @@ fn test_channels_stereo_no_fmt_extended() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_frame_reader_consumes_reader() {
|
fn test_frame_reader_consumes_reader() {
|
||||||
// Issue #6
|
// Issue #6
|
||||||
use bwavfile::WaveFmt;
|
use bwavfile::{WaveFmt, AudioFrameReader};
|
||||||
use bwavfile::AudioFrameReader;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
fn from_wav_filename(wav_filename: &str) -> Result<(WaveFmt, AudioFrameReader<std::io::BufReader<File>>), ()> {
|
fn from_wav_filename(wav_filename: &str) -> Result<(WaveFmt, AudioFrameReader<std::io::BufReader<File>>), ()> {
|
||||||
if let Ok(mut r) = WaveReader::open(&wav_filename) {
|
if let Ok(mut r) = WaveReader::open(&wav_filename) {
|
||||||
|
|||||||
Reference in New Issue
Block a user