Samplerate

Struct Samplerate 

Source
pub struct Samplerate { /* private fields */ }
Expand description

A samplerate converter. This is a wrapper around libsamplerate’s SRC_STATE which also stores the source and target samplerates.

§Example

use samplerate_rs::{Samplerate, ConverterType};

// Generate a 880Hz sine wave for 1 second in 44100Hz with one channel.
let freq = std::f32::consts::PI * 880f32 / 44100f32;
let mut input: Vec<f32> = (0..44100).map(|i| (freq * i as f32).sin()).collect();

// Instanciate a new converter.
let mut converter = Samplerate::new(ConverterType::SincBestQuality, 44100, 48000, 1).unwrap();

// Resample the input from 44100Hz to 48000Hz.
let resampled = converter.process_last(&input).unwrap();
assert_eq!(resampled.len(), 48000);

Implementations§

Source§

impl Samplerate

Source

pub fn new( converter_type: ConverterType, from_rate: u32, to_rate: u32, channels: usize, ) -> Result<Samplerate, Error>

Create a new samplerate converter with the given rates and channels.

Source

pub fn reset(&mut self) -> Result<(), Error>

Reset the internal converter’s state.

Source

pub fn from_rate(&self) -> u32

Retrieve the currently used source samplerate.

Source

pub fn to_rate(&self) -> u32

Retrieve the currently used target samplerate.

Source

pub fn set_from_rate(&mut self, from_rate: u32)

Change the source samplerate.

Source

pub fn set_to_rate(&mut self, to_rate: u32)

Change the target samplerate.

Source

pub fn ratio(&self) -> f64

Calculate the ratio (target samplerate divided by source samplerate).

Source

pub fn channels(&self) -> Result<usize, Error>

Retrieve the number of channels used.

Source

pub fn process(&self, input: &[f32]) -> Result<Vec<f32>, Error>

Perform a samplerate conversion on a block of data (use process_last if it is the last one) If the number of channels used was not 1 (Mono), the samples are expected to be stored interleaved.

Source

pub fn process_last(&self, input: &[f32]) -> Result<Vec<f32>, Error>

Perform a samplerate conversion on last block of given input data. If the number of channels used was not 1 (Mono), the samples are expected to be stored interleaved.

Trait Implementations§

Source§

impl Clone for Samplerate

Source§

fn clone(&self) -> Samplerate

Might panic if the underlying src_clone method from libsamplerate returns an error.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for Samplerate

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.