From 9fe741d913666927e79bef3b736f1d36a8c8133a Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Sun, 22 Aug 2021 23:35:47 +0200 Subject: [PATCH 1/2] implement std::error::Error for Error --- src/errors.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/errors.rs b/src/errors.rs index 291812e..fe2eff5 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,5 @@ -use std::io; +use std::{fmt::{Debug,Display}, io}; +use std::error::Error as StdError; use super::fourcc::FourCC; use uuid; @@ -43,6 +44,14 @@ pub enum Error { } +impl StdError for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(self, f) + } +} + impl From for Error { fn from(error: io::Error) -> Error { From 663b9fad439dbbb6731ba37654cadc83776b4f07 Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Sun, 22 Aug 2021 23:36:10 +0200 Subject: [PATCH 2/2] derive Debug for ADMAudioID and ChannelDescriptor and implement f32 reading --- src/fmt.rs | 2 ++ src/wavereader.rs | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/fmt.rs b/src/fmt.rs index 9dc6d76..dae4793 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -18,6 +18,7 @@ use byteorder::{WriteBytesExt, ReadBytesExt}; /// `AudioProgramme`. /// /// See BS.2088-1 ยง 8, also BS.2094, also blahblahblah... +#[derive(Debug)] pub struct ADMAudioID { pub track_uid: [char; 12], pub channel_format_ref: [char; 14], @@ -28,6 +29,7 @@ pub struct ADMAudioID { /// /// This information is correlated from the Wave format ChannelMap field and /// the `chna` chunk, if present. +#[derive(Debug)] pub struct ChannelDescriptor { /// Index, the offset of this channel's samples in one frame. pub index: u16, diff --git a/src/wavereader.rs b/src/wavereader.rs index a3d9780..5fa7b2e 100644 --- a/src/wavereader.rs +++ b/src/wavereader.rs @@ -50,7 +50,7 @@ impl AudioFrameReader { "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 , + assert!(format.common_format() == CommonFormat::IntegerPCM || format.common_format() == CommonFormat::IeeeFloatPCM, "Unsupported format tag {:?}", format.tag); inner.seek(Start(start))?; @@ -121,8 +121,27 @@ impl AudioFrameReader { } } - pub fn read_float_frame(&mut self, buffer:&mut [f32]) -> Result { - todo!() + pub fn read_float_frame(&mut self, buffer: &mut [f32]) -> Result { + assert!(buffer.len() as u16 == self.format.channel_count, + "read_float_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) { + (25..=32,32) => self.inner.read_f32::()?, + (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 ) + } } }