Skip to main content

Resampler

Struct Resampler 

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

A samplerate converter.

This is a wrapper around libsamplerate’s SRC_STATE.

§Example

use resample::{Resampler, ResampleType};

// Generate a 880Hz sine wave for 1 second in 44100Hz with one channel.
let freq = PI * 880_f32 / 44100_f32;
let mut input = (0..44100).map(|i| (freq * i as f32).sin()).collect::<Vec<f32>>();
let mut output = vec![0.0; 48000];

// Instantiate a new resampler.
let mut resampler = Resampler::new(ResampleType::SincBestQuality, 1, 44100, 48000).unwrap();

// Resample the input from 44100Hz to 48000Hz.
let processed = resampler.finalize(&input, &mut output).unwrap();
assert_eq!(processed.read, 44100);
assert_eq!(processed.written, 48000);

Implementations§

Source§

impl Resampler

Source

pub fn new( converter_type: ResampleType, channels: u8, from_rate: u32, to_rate: u32, ) -> Result<Self, Error>

Create a new samplerate converter assuming the given channel count and sample rates.

Source

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

Perform a samplerate conversion on a block of data.

If the number of channels used was not 1 (Mono), the samples are expected to be stored interleaved.

§Notes

Even if all input samples are cleanly processed with this method, you will still need to finalize the conversion.

Source

pub fn finalize( &mut self, input: &[f32], output: &mut [f32], ) -> Result<Processed, Error>

Perform a samplerate conversion on last block of given input data (which may be empty).

If the number of channels used was not 1 (Mono), the samples are expected to be stored interleaved.

If the returned Processed::written value equals the size of the output buffer this way indicate that more data is available for consumption and that the method should be invoked again with potentially remaining input.

Source

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

Reset the internal converter’s state.

Trait Implementations§

Source§

impl Debug for Resampler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Resampler

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> 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, 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.