Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Rust 2018 and make encoding an optional feature #18

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ authors = ["Pierre Krieger <[email protected]>"]
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"
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 with recent vorbis-sys is not on crates.io
git = "https://github.com/Hossein-Noroozpour/vorbis-encoder-rs"
optional = true

[dev-dependencies]
rand = "0.7"

[features]
default = ["encoder"]
encoder = ["vorbis-encoder"]

[[example]]
name = "decode-encode"
required-features = ["encoder"]
3 changes: 2 additions & 1 deletion examples/decode-encode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate vorbis;
extern crate rand;

use std::io::Write;

Expand Down Expand Up @@ -55,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");
}
59 changes: 29 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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.",
})
}
}

Expand Down Expand Up @@ -165,12 +161,12 @@ impl<R> Decoder<R> where R: Read + Seek {
unsafe {
let data_ptr = &mut *data as *mut DecoderData<R>;
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,
})
}

Expand Down Expand Up @@ -283,31 +279,34 @@ fn check_errors(code: libc::c_int) -> Result<(), VorbisError> {
}
}

#[cfg(feature = "encoder")]
#[derive(Debug)]
pub enum VorbisQuality {
VeryHighQuality,
HighQuality,
Quality,
Midium,
Medium,
Performance,
HighPerforamnce,
HighPerformance,
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<Self, VorbisError> {
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) {
Expand Down