From 88b29a22d90dc91e77ac31f78b95caef0a68175c Mon Sep 17 00:00:00 2001 From: AlexApps99 Date: Thu, 20 Aug 2020 17:15:14 +1200 Subject: [PATCH 1/4] Made encoding optional, updated dependencies --- Cargo.toml | 19 ++++++++++++++++--- examples/decode-encode.rs | 1 + src/lib.rs | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 871c8d9..746f1f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,20 @@ license = "Apache-2.0" [dependencies] libc = "0.2" ogg-sys = "0.0.9" -rand = "0.3" -vorbis-sys = "0.0.8" -vorbis-encoder = "0.1" +vorbis-sys = "0.1.0" vorbisfile-sys = "0.0.8" + +[dependencies.vorbis-encoder] +version = "0.1" +optional = true + +[dev-dependencies] +rand = "0.7" + +[features] +default = ["encoder"] +encoder = ["vorbis-encoder"] + +[[example]] +name = "decode-encode" +required-features = ["encoder"] diff --git a/examples/decode-encode.rs b/examples/decode-encode.rs index 1528d95..1f4965b 100644 --- a/examples/decode-encode.rs +++ b/examples/decode-encode.rs @@ -1,4 +1,5 @@ extern crate vorbis; +extern crate rand; use std::io::Write; diff --git a/src/lib.rs b/src/lib.rs index b02af69..e599070 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ extern crate ogg_sys; extern crate vorbis_sys; extern crate vorbisfile_sys; +#[cfg(feature = "encoder")] extern crate vorbis_encoder; extern crate libc; -extern crate rand; use std::io::{self, Read, Seek}; @@ -283,6 +283,7 @@ fn check_errors(code: libc::c_int) -> Result<(), VorbisError> { } } +#[cfg(feature = "encoder")] #[derive(Debug)] pub enum VorbisQuality { VeryHighQuality, @@ -294,10 +295,12 @@ pub enum VorbisQuality { VeryHighPerformance, } +#[cfg(feature = "encoder")] pub struct Encoder { e: vorbis_encoder::Encoder, } +#[cfg(feature = "encoder")] impl Encoder { pub fn new(channels: u8, rate: u64, quality: VorbisQuality) -> Result { let quality = match quality { From b93a3b98988c006dfea97a7fdd53691a644a83e3 Mon Sep 17 00:00:00 2001 From: AlexApps99 Date: Thu, 20 Aug 2020 18:39:09 +1200 Subject: [PATCH 2/4] Update to Rust 2018 --- Cargo.toml | 1 + src/lib.rs | 36 ++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 746f1f3..a622c49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Pierre Krieger "] description = "High-level bindings for the official libvorbis library." repository = "https://github.com/tomaka/vorbis-rs" license = "Apache-2.0" +edition = "2018" [dependencies] libc = "0.2" diff --git a/src/lib.rs b/src/lib.rs index e599070..72e1e48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,29 +32,25 @@ pub enum VorbisError { } impl std::error::Error for VorbisError { - fn description(&self) -> &str { - match self { - &VorbisError::ReadError(_) => "A read from media returned an error", - &VorbisError::NotVorbis => "Bitstream does not contain any Vorbis data", - &VorbisError::VersionMismatch => "Vorbis version mismatch", - &VorbisError::BadHeader => "Invalid Vorbis bitstream header", - &VorbisError::InvalidSetup => "Invalid setup request, eg, out of range argument or initial file headers are corrupt", - &VorbisError::Hole => "Interruption of data", - &VorbisError::Unimplemented => "Unimplemented mode; unable to comply with quality level request.", - } - } - - fn cause(&self) -> Option<&std::error::Error> { - match self { - &VorbisError::ReadError(ref err) => Some(err as &std::error::Error), - _ => None + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match *self { + VorbisError::ReadError(ref err) => Some(err as &dyn std::error::Error), + _ => None, } } } impl std::fmt::Display for VorbisError { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!(fmt, "{}", std::error::Error::description(self)) + write!(fmt, "{}", match *self { + VorbisError::ReadError(_) => "A read from media returned an error", + VorbisError::NotVorbis => "Bitstream does not contain any Vorbis data", + VorbisError::VersionMismatch => "Vorbis version mismatch", + VorbisError::BadHeader => "Invalid Vorbis bitstream header", + VorbisError::InvalidSetup => "Invalid setup request, eg, out of range argument or initial file headers are corrupt", + VorbisError::Hole => "Interruption of data", + VorbisError::Unimplemented => "Unimplemented mode; unable to comply with quality level request.", + }) } } @@ -165,12 +161,12 @@ impl Decoder where R: Read + Seek { unsafe { let data_ptr = &mut *data as *mut DecoderData; let data_ptr = data_ptr as *mut libc::c_void; - try!(check_errors(vorbisfile_sys::ov_open_callbacks(data_ptr, &mut data.vorbis, - std::ptr::null(), 0, callbacks))); + check_errors(vorbisfile_sys::ov_open_callbacks(data_ptr, &mut data.vorbis, + std::ptr::null(), 0, callbacks))?; } Ok(Decoder { - data: data, + data, }) } From 2291ca984db7bce56de584bf9ad568b7dbe50c96 Mon Sep 17 00:00:00 2001 From: AlexApps99 Date: Thu, 20 Aug 2020 18:40:53 +1200 Subject: [PATCH 3/4] Correct typos (breaking change) --- examples/decode-encode.rs | 2 +- src/lib.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/decode-encode.rs b/examples/decode-encode.rs index 1f4965b..2a6c8da 100644 --- a/examples/decode-encode.rs +++ b/examples/decode-encode.rs @@ -56,7 +56,7 @@ fn main() { println!("bitrate_nominal: {:?}", bitrate_nominal); println!("bitrate_lower: {:?}", bitrate_lower); println!("bitrate_window: {:?}", bitrate_window); - let mut encoder = vorbis::Encoder::new(channels as u8, rate, vorbis::VorbisQuality::Midium).expect("Error in creating encoder"); + let mut encoder = vorbis::Encoder::new(channels as u8, rate, vorbis::VorbisQuality::Medium).expect("Error in creating encoder"); out_file.write(encoder.encode(&data).expect("Error in encoding.").as_slice()).expect("Error in writing"); out_file.write(encoder.flush().expect("Error in flushing.").as_slice()).expect("Error in writing"); } diff --git a/src/lib.rs b/src/lib.rs index 72e1e48..862f5d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -285,9 +285,9 @@ pub enum VorbisQuality { VeryHighQuality, HighQuality, Quality, - Midium, + Medium, Performance, - HighPerforamnce, + HighPerformance, VeryHighPerformance, } @@ -300,13 +300,13 @@ pub struct Encoder { impl Encoder { pub fn new(channels: u8, rate: u64, quality: VorbisQuality) -> Result { let quality = match quality { - VorbisQuality::VeryHighQuality => {1.0f32}, - VorbisQuality::HighQuality => {0.8f32}, - VorbisQuality::Quality => {0.6f32}, - VorbisQuality::Midium => {0.4f32}, - VorbisQuality::Performance => {0.3f32}, - VorbisQuality::HighPerforamnce => {0.1f32}, - VorbisQuality::VeryHighPerformance => {-0.1f32}, + VorbisQuality::VeryHighQuality => 1.0f32, + VorbisQuality::HighQuality => 0.8f32, + VorbisQuality::Quality => 0.6f32, + VorbisQuality::Medium => 0.4f32, + VorbisQuality::Performance => 0.3f32, + VorbisQuality::HighPerformance => 0.1f32, + VorbisQuality::VeryHighPerformance => -0.1f32, }; Ok(Encoder { e: match vorbis_encoder::Encoder::new(channels as u32, rate, quality) { From 43acb00e4a8afa94bf9cf384aaaf313b7e18a8a1 Mon Sep 17 00:00:00 2001 From: AlexApps99 Date: Thu, 20 Aug 2020 19:00:43 +1200 Subject: [PATCH 4/4] Update encoder to Git version --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a622c49..90a60e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,8 @@ vorbis-sys = "0.1.0" vorbisfile-sys = "0.0.8" [dependencies.vorbis-encoder] -version = "0.1" +# Version with recent vorbis-sys is not on crates.io +git = "https://github.com/Hossein-Noroozpour/vorbis-encoder-rs" optional = true [dev-dependencies]