pub trait Resampler<T>: Send {
    fn process_into_buffer<V: AsRef<[T]>>(
        &mut self,
        wave_in: &[V],
        wave_out: &mut [Vec<T>],
        active_channels_mask: Option<&[bool]>
    ) -> ResampleResult<()>;
fn input_frames_max(&self) -> usize;
fn input_frames_next(&self) -> usize;
fn nbr_channels(&self) -> usize;
fn output_frames_max(&self) -> usize;
fn output_frames_next(&self) -> usize;
fn set_resample_ratio(&mut self, new_ratio: f64) -> ResampleResult<()>;
fn set_resample_ratio_relative(
        &mut self,
        rel_ratio: f64
    ) -> ResampleResult<()>; fn process<V: AsRef<[T]>>(
        &mut self,
        wave_in: &[V],
        active_channels_mask: Option<&[bool]>
    ) -> ResampleResult<Vec<Vec<T>>> { ... }
fn input_buffer_allocate(&self) -> Vec<Vec<T>> { ... }
fn output_buffer_allocate(&self) -> Vec<Vec<T>> { ... } }
Expand description

A resampler that us used to resample a chunk of audio to a new sample rate. For asynchronous resamplers, the rate can be adjusted as required.

This trait is not object safe. If you need an object safe resampler, use the VecResampler wrapper trait.

Required methods

Resample a buffer of audio to a pre-allocated output buffer. Use this in real-time applications where the unpredictable time required to allocate memory from the heap can cause glitches. If this is not a problem, you may use the process method instead.

The input and output buffers are noninterleaved. The input is a slice, where each element of the slice is itself referenceable as a slice (AsRef<[T]>) which contains the samples for a single channel. Because Vec implements AsRef<[T]>, the input may be Vec<Vec<T>>.

The output data is a slice, where each element of the slice is a Vec which contains the samples for a single channel. If the output channel vectors do not have sufficient capacity for all output samples, they will be resized by this function. To avoid these allocations during this function, allocate the output buffer with output_buffer_allocate before calling this function and reuse the same buffer for each call.

The active_channels_mask is optional. Any channel marked as inactive by a false value will be skipped during processing and the corresponding output will be left unchanged. If None is given, all channels will be considered active unless their length is 0.

Get the maximum number of input frames per channel the resampler could require

Get the number of frames per channel needed for the next call to process_into_buffer or process

Get the maximum number of channels this Resampler is configured for

Get the max number of output frames per channel

Get the number of frames per channel that will be output from the next call to process_into_buffer or process

Update the resample ratio

For asynchronous resamplers, the ratio must be within original / maximum to original * maximum, where the original and maximum are the resampling ratios that were provided to the constructor. Trying to set the ratio outside these bounds will return ResampleError::RatioOutOfBounds.

For synchronous resamplers, this will always return ResampleError::SyncNotAdjustable.

Update the resample ratio as a factor relative to the original one

For asynchronous resamplers, the relative ratio must be within 1 / maximum to maximum, where maximum is the maximum resampling ratio that was provided to the constructor. Trying to set the ratio outside these bounds will return ResampleError::RatioOutOfBounds.

Higher ratios above 1.0 slow down the output and lower the pitch. Lower ratios below 1.0 speed up the output and raise the pitch.

For synchronous resamplers, this will always return ResampleError::SyncNotAdjustable.

Provided methods

This is a convenience wrapper for process_into_buffer that allocates the output buffer with each call. For realtime applications, use process_into_buffer with a buffer allocated by output_buffer_allocate instead of this function.

Convenience method for allocating an input buffer suitable for use with process_into_buffer. The buffer’s capacity is big enough to prevent allocating additional heap memory before any call to process_into_buffer regardless of the current resampling ratio.

Convenience method for allocating an output buffer suitable for use with process_into_buffer. The buffer’s capacity is big enough to prevent allocating additional heap memory during any call to process_into_buffer regardless of the current resampling ratio.

Implementors