Async

Struct Async 

Source
pub struct Async<T> { /* private fields */ }
Expand description

An asynchronous resampler that uses either polynomial or sinc interpolation.

The fixed argument determines if input of output should be fixed size. When the input size is fixed, the output size varies from call to call, and when output size is fixed, the input size varies.

The number of frames on the fixed side is determined by the chunk size argument to the constructor. This value can be changed by the set_chunk_size() method, to let the resampler process smaller chunks of audio data. Note that the chunk size cannot exceed the value given at creation time.

When the input size is fixed, the maximum value can be retrieved using the input_size_max() method, and input_frames_next() gives the current value. When the output size is fixed, the corresponding values are instead provided by the output_size_max() and output_size_next() methods.

§Interpolation

This resampler can use either polynomial or sinc interpolation. Sinc interpolation gives the best quality, while polynomial interpolation runs significantly faster.

§Polynomial

The resampling is done by interpolating between the input samples by fitting a polynomial. The polynomial degree can be selected, see PolynomialDegree for the available options. Higher polynomial degrees give better quality but run slower.

Note that no anti-aliasing filter is used. This makes it run considerably faster than the corresponding Sinc resampler, which performs anti-aliasing filtering. The price is that the resampling creates some artefacts in the output, mainly at higher frequencies. Use a Sinc resampler if this can not be tolerated.

§Sinc

The resampling is done by creating a number of intermediate points (defined by oversampling_factor) by sinc interpolation. The new samples are then calculated by interpolating between these points.

§Adjusting the resampling ratio

The resampling ratio can be freely adjusted within the range specified to the constructor. Adjusting the ratio does not recalculate the sinc functions used by the anti-aliasing filter. This causes no issue when increasing the ratio (which slows down the output). However, when decreasing more than a few percent (or speeding up the output), the filters can no longer suppress all aliasing and this may lead to some artefacts.

The resampling ratio can be freely adjusted within the range specified to the constructor. Higher maximum ratios require more memory to be allocated by an internal buffer, and increase the maximum length of the variable length input or output buffer.

Implementations§

Source§

impl<T> Async<T>
where T: Sample,

Source

pub fn new_poly( resample_ratio: f64, max_resample_ratio_relative: f64, interpolation_type: PolynomialDegree, chunk_size: usize, nbr_channels: usize, fixed: FixedAsync, ) -> Result<Self, ResamplerConstructionError>

Create a new Async resampler that uses polynomial interpolation.

Parameters are:

  • resample_ratio: Starting ratio between output and input sample rates, must be > 0.
  • max_resample_ratio_relative: Maximum ratio that can be set with Resampler::set_resample_ratio relative to resample_ratio, must be >= 1.0. The minimum relative ratio is the reciprocal of the maximum. For example, with max_resample_ratio_relative of 10.0, the ratio can be set between resample_ratio * 10.0 and resample_ratio / 10.0.
  • interpolation_type: Degree of polynomial used for interpolation, see PolynomialDegree.
  • chunk_size: Size of input data in frames.
  • nbr_channels: Number of channels in input/output.
  • fixed: Deciding whether input or output size is fixed.
Source

pub fn new_sinc( resample_ratio: f64, max_resample_ratio_relative: f64, parameters: &SincInterpolationParameters, chunk_size: usize, nbr_channels: usize, fixed: FixedAsync, ) -> Result<Self, ResamplerConstructionError>

Create a new Async resampler that uses sinc interpolation.

Parameters are:

  • resample_ratio: Starting ratio between output and input sample rates, must be > 0.
  • max_resample_ratio_relative: Maximum ratio that can be set with Resampler::set_resample_ratio relative to resample_ratio, must be >= 1.0. The minimum relative ratio is the reciprocal of the maximum. For example, with max_resample_ratio_relative of 10.0, the ratio can be set between resample_ratio * 10.0 and resample_ratio / 10.0.
  • parameters: Parameters for interpolation, see SincInterpolationParameters.
  • chunk_size: Size of input data in frames.
  • nbr_channels: Number of channels in input/output.

Trait Implementations§

Source§

impl<T> Debug for Async<T>

Source§

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

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

impl<T> Resampler<T> for Async<T>
where T: Sample,

Source§

fn process_into_buffer<'a>( &mut self, buffer_in: &dyn Adapter<'a, T>, buffer_out: &mut dyn AdapterMut<'a, T>, indexing: Option<&Indexing>, ) -> ResampleResult<(usize, usize)>

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

fn output_frames_max(&self) -> usize

Get the maximum possible number of output frames per channel.
Source§

fn output_frames_next(&self) -> usize

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

fn output_delay(&self) -> usize

Get the delay for the resampler, reported as a number of output frames. This gives how many frames any event in the input is delayed before it appears in the output.
Source§

fn nbr_channels(&self) -> usize

Get the number of channels this Resampler is configured for.
Source§

fn input_frames_max(&self) -> usize

Get the maximum possible number of input frames per channel the resampler could require.
Source§

fn input_frames_next(&self) -> usize

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

fn set_resample_ratio( &mut self, new_ratio: f64, ramp: bool, ) -> ResampleResult<()>

Update the resample ratio. Read more
Source§

fn resample_ratio(&self) -> f64

Get the current resample ratio, defined as output sample rate divided by input sample rate.
Source§

fn set_resample_ratio_relative( &mut self, rel_ratio: f64, ramp: bool, ) -> ResampleResult<()>

Update the resample ratio as a factor relative to the original one. Read more
Source§

fn reset(&mut self)

Reset the resampler state and clear all internal buffers.
Source§

fn set_chunk_size(&mut self, chunksize: usize) -> ResampleResult<()>

Change the chunk size for the resampler. This is not supported by all resampler types. The value must be equal to or smaller than the chunk size value that the resampler was created with. ResampleError::InvalidChunkSize is returned if the value is zero or too large. Read more
Source§

fn process( &mut self, buffer_in: &dyn Adapter<'_, T>, input_offset: usize, active_channels_mask: Option<&[bool]>, ) -> ResampleResult<InterleavedOwned<T>>

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 pre-allocated buffer instead of this function. Read more
Source§

fn process_all_into_buffer<'a>( &mut self, buffer_in: &dyn Adapter<'a, T>, buffer_out: &mut dyn AdapterMut<'a, T>, input_len: usize, active_channels_mask: Option<&[bool]>, ) -> ResampleResult<(usize, usize)>

Convenience method for processing audio clips of arbitrary length from and to buffers in memory. This method repeatedly calls process_into_buffer until all frames of the input buffer have been processed. The processed frames are written to the output buffer, with the initial silence (caused by the resampler delay) trimmed off. Read more
Source§

fn process_all_needed_output_len(&mut self, input_len: usize) -> usize

Calculate the minimal length of the output buffer needed to process a clip of length input_len using the process_all_into_buffer method. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Async<T>

§

impl<T> !RefUnwindSafe for Async<T>

§

impl<T> Send for Async<T>
where T: Send,

§

impl<T> !Sync for Async<T>

§

impl<T> Unpin for Async<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Async<T>

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.