Skip to main content

Resampler

Struct Resampler 

Source
pub struct Resampler<T: Transcendental> { /* private fields */ }
Expand description

Sample-rate converter wrapping InterpolatedReader.

Accepts a pre-loaded buffer with a known source sample rate and outputs at a target rate that can differ from the source rate.

§Examples

use rill_core::traits::Algorithm;
use rill_core_dsp::generators::Resampler;

let source_rate = 44100.0;
let samples = vec![0.0f32; 1024];
let mut rs = Resampler::new(samples, source_rate);
rs.init(48000.0);
let mut out = vec![0.0f32; 512];
rs.process(None, &mut out).unwrap();
assert!((rs.position() - 512.0 * 44100.0 / 48000.0).abs() < 1.0);

Implementations§

Source§

impl<T: Transcendental> Resampler<T>

Source

pub fn new(buffer: Vec<T>, source_rate: f64) -> Self

Create a new resampler from a sample buffer and its source sample rate.

The resampler starts with target_rate = source_rate (passthrough). Call Algorithm::init or [set_target_rate] to convert to a different rate.

Source

pub fn from_boxed(buffer: Box<[T]>, source_rate: f64) -> Self

Create a resampler from a pre-allocated boxed slice.

Source

pub fn len(&self) -> usize

Number of samples in the source buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the source buffer is empty.

Source

pub fn source_rate(&self) -> f64

Source sample rate in Hz.

Source

pub fn set_source_rate(&mut self, hz: f64)

Set the source sample rate and recompute the interpolation ratio.

Source

pub fn target_rate(&self) -> f64

Target sample rate in Hz.

Source

pub fn set_target_rate(&mut self, hz: f64)

Set the target sample rate and recompute the interpolation ratio.

Source

pub fn set_cubic(&mut self, cubic: bool)

Enable (true) or disable (false) cubic Hermite interpolation.

Linear interpolation (default) is faster; cubic gives higher quality at the cost of more computation.

Source

pub fn is_cubic(&self) -> bool

Returns true if cubic interpolation is enabled.

Source

pub fn position(&self) -> f64

Current read position in the source buffer (in source samples).

Source

pub fn set_position(&mut self, pos: f64)

Set the read position in the source buffer.

Source

pub fn set_buffer(&mut self, buffer: Vec<T>)

Replace the source buffer and reset position to 0.

The new buffer is assumed to have the same source rate.

Source

pub fn as_slice(&self) -> &[T]

Return the internal buffer as an immutable slice.

Source

pub fn ratio(&self) -> f64

The computed interpolation ratio (source_rate / target_rate).

When ratio > 1.0 the source runs faster → downsampling. When ratio < 1.0 the source runs slower → upsampling.

Trait Implementations§

Source§

impl<T: Transcendental> Algorithm<T> for Resampler<T>

Source§

fn init(&mut self, sample_rate: f32)

Initialise the algorithm with a sample rate. Read more
Source§

fn reset(&mut self)

Reset the algorithm to its initial state. Read more
Source§

fn process( &mut self, _input: Option<&[T]>, output: &mut [T], ) -> ProcessResult<()>

Process one block of signal. Read more
Source§

fn metadata(&self) -> AlgorithmMetadata

Descriptive metadata (defaults to empty).
Source§

fn apply_command(&mut self, _value: T)

Receive a real-time command value from the control path. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Resampler<T>

§

impl<T> RefUnwindSafe for Resampler<T>
where T: RefUnwindSafe,

§

impl<T> Send for Resampler<T>

§

impl<T> Sync for Resampler<T>

§

impl<T> Unpin for Resampler<T>

§

impl<T> UnsafeUnpin for Resampler<T>

§

impl<T> UnwindSafe for Resampler<T>
where T: UnwindSafe,

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> AsAny for T
where T: 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert to &dyn std::any::Any
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert to &mut dyn std::any::Any
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V