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

Use pure-Rust version of libsamplerate #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ keywords = ["audio", "samplerate"]
license = "BSD-2-Clause"

[dependencies]
libsamplerate-sys = "0.1.5"
libsamplerate-sys = { version = "0.1.5", optional = true }
libsamplerate = { version = "0.1.0", optional = true }

[features]
default = ["pure-rust"]
pure-rust = ["libsamplerate"]
sys = ["libsamplerate-sys"]

[dev-dependencies]
hound = "3.4"
4 changes: 4 additions & 0 deletions src/converter_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#[cfg(feature = "sys")]
use libsamplerate_sys::*;
#[cfg(feature = "pure-rust")]
use libsamplerate::*;

use std::ffi::CStr;
use crate::error::{Error, ErrorCode};

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#[cfg(feature = "sys")]
use libsamplerate_sys::*;
#[cfg(feature = "pure-rust")]
use libsamplerate::*;

use std::ffi::CStr;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[cfg(feature = "sys")]
extern crate libsamplerate_sys;
#[cfg(feature = "pure-rust")]
extern crate libsamplerate;

pub mod converter_type;
pub mod error;
Expand All @@ -7,7 +10,12 @@ pub mod samplerate;
pub use crate::converter_type::*;
pub use crate::error::*;
pub use crate::samplerate::*;

#[cfg(feature = "sys")]
use libsamplerate_sys::*;
#[cfg(feature = "pure-rust")]
use libsamplerate::*;

use std::ffi::CStr;

/// Perform a simple samplerate conversion of a large chunk of audio.
Expand Down Expand Up @@ -40,7 +48,7 @@ pub fn convert(from_rate: u32, to_rate: u32, channels: usize, converter_type: Co
end_of_input: 0,
input_frames_used: 0,
output_frames_gen: 0,
..Default::default()
..unsafe { std::mem::zeroed() }
};
let error_int = unsafe { src_simple(&mut src as *mut SRC_DATA, converter_type as i32, channels as i32) };
let error_code = ErrorCode::from_int(error_int);
Expand Down
7 changes: 6 additions & 1 deletion src/samplerate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::converter_type::ConverterType;
use crate::error::{Error, ErrorCode};

#[cfg(feature = "sys")]
use libsamplerate_sys::*;
#[cfg(feature = "pure-rust")]
use libsamplerate::*;

use std::clone::Clone;

/// A samplerate converter. This is a wrapper around libsamplerate's `SRC_STATE` which also
Expand Down Expand Up @@ -107,7 +112,7 @@ impl Samplerate {
end_of_input: if end_of_input { 1 } else { 0 },
input_frames_used: 0,
output_frames_gen: 0,
..Default::default()
..unsafe { std::mem::zeroed() }
};
let error_int = unsafe { src_process(self.ptr, &mut src as *mut SRC_DATA) };
match ErrorCode::from_int(error_int) {
Expand Down