diff --git a/Cargo.lock b/Cargo.lock index 1c94e8a..f7ddc24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ # It is not intended for manual editing. [[package]] name = "bwavfile" -version = "0.1.6" +version = "0.1.7" dependencies = [ "byteorder", "encoding", diff --git a/Cargo.toml b/Cargo.toml index 760cccd..6d99161 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bwavfile" -version = "0.1.6" +version = "0.1.7" authors = ["Jamie Hardt "] edition = "2018" license = "MIT" diff --git a/src/cue.rs b/src/cue.rs index c8d499e..02f3ba5 100644 --- a/src/cue.rs +++ b/src/cue.rs @@ -1,9 +1,13 @@ use super::fourcc::{FourCC,ReadFourCC, LABL_SIG, NOTE_SIG, LTXT_SIG}; use super::list_form::collect_list_form; + use byteorder::{ReadBytesExt, LittleEndian}; +use encoding::{DecoderTrap}; +use encoding::{Encoding}; +use encoding::all::ASCII; + use std::io::{Cursor, Error, Read}; -use std::str; #[derive(Copy,Clone, Debug)] struct RawCue { @@ -46,13 +50,12 @@ impl RawLabel { fn read_from(data : &[u8]) -> Result { let mut rdr = Cursor::new(data); let length = data.len(); - + Ok( Self { cue_point_id : rdr.read_u32::()?, text : { let mut buf = vec![0u8; (length - 4) as usize ]; rdr.read_exact(&mut buf)?; - //if buf.len() % 2 == 1 { rdr.read_u8()?; }; buf } }) @@ -75,7 +78,6 @@ impl RawNote { text : { let mut buf = vec![0u8; (length - 4) as usize ]; rdr.read_exact(&mut buf)?; - //if length % 2 == 1 { rdr.read_u8()?; }; buf } }) @@ -111,9 +113,7 @@ impl RawLtxt { if length - 20 > 0 { let mut buf = vec![0u8; (length - 20) as usize]; rdr.read_exact(&mut buf)?; - //if length % 2 == 1 { rdr.read_u8()?; }; Some( buf ) - //Some( String::from_utf8(buf).unwrap_or(String::from("")) ) } else { None } @@ -210,6 +210,11 @@ pub struct Cue { } +fn convert_to_cue_string(buffer : &[u8]) -> String { + let trimmed : Vec = buffer.iter().take_while(|c| **c != 0 as u8).cloned().collect(); + ASCII.decode(&trimmed, DecoderTrap::Ignore).expect("Error decoding text") +} + impl Cue { pub fn collect_from(cue_chunk : &[u8], adtl_chunk : Option<&[u8]>) -> Result, Error> { @@ -230,18 +235,19 @@ impl Cue { ident : i.cue_point_id, frame : i.frame, length: { - raw_adtl.ltxt_for_cue_point(i.cue_point_id).first().map(|x| x.frame_length) + raw_adtl.ltxt_for_cue_point(i.cue_point_id).first() + .filter(|x| x.purpose == FourCC::make(b"rgn ")) + .map(|x| x.frame_length) }, label: { raw_adtl.labels_for_cue_point(i.cue_point_id).iter() - .filter_map(|x| str::from_utf8(&x.text).ok()) - .map(|s| String::from(s)) + .map(|s| convert_to_cue_string(&s.text)) .next() }, note : { raw_adtl.notes_for_cue_point(i.cue_point_id).iter() - .filter_map(|x| str::from_utf8(&x.text).ok()) - .map(|s| String::from(s)) + //.filter_map(|x| str::from_utf8(&x.text).ok()) + .map(|s| convert_to_cue_string(&s.text)) .next() } } diff --git a/src/wavereader.rs b/src/wavereader.rs index 6426957..f8c5592 100644 --- a/src/wavereader.rs +++ b/src/wavereader.rs @@ -185,13 +185,22 @@ impl WaveReader { /// assert_eq!(cue_points.len(), 3); /// assert_eq!(cue_points[0].ident, 1); /// assert_eq!(cue_points[0].frame, 12532); - /// //assert_eq!(cue_points[0].label, Some(String::from("Marker 1"))); + /// assert_eq!(cue_points[0].length, None); + /// assert_eq!(cue_points[0].label, Some(String::from("Marker 1"))); + /// assert_eq!(cue_points[0].note, Some(String::from("Marker 1 Comment"))); /// /// assert_eq!(cue_points[1].ident, 2); /// assert_eq!(cue_points[1].frame, 20997); + /// assert_eq!(cue_points[1].length, None); + /// assert_eq!(cue_points[1].label, Some(String::from("Marker 2"))); + /// assert_eq!(cue_points[1].note, Some(String::from("Marker 2 Comment"))); /// /// assert_eq!(cue_points[2].ident, 3); /// assert_eq!(cue_points[2].frame, 26711); + /// assert_eq!(cue_points[2].length, Some(6465)); + /// assert_eq!(cue_points[2].label, Some(String::from("Timed Region"))); + /// assert_eq!(cue_points[2].note, Some(String::from("Region Comment"))); + /// /// ``` pub fn cue_points(&mut self) -> Result,ParserError> { let mut cue_buffer : Vec = vec![];