Resampler

Trait Resampler 

Source
pub trait Resampler<T>: Send
where T: Sample,
{
Show 15 methods // Required methods 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)>; 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 output_delay(&self) -> usize; fn set_resample_ratio( &mut self, new_ratio: f64, ramp: bool, ) -> ResampleResult<()>; fn resample_ratio(&self) -> f64; fn set_resample_ratio_relative( &mut self, rel_ratio: f64, ramp: bool, ) -> ResampleResult<()>; fn reset(&mut self); // Provided methods fn process( &mut self, buffer_in: &dyn Adapter<'_, T>, input_offset: usize, active_channels_mask: Option<&[bool]>, ) -> ResampleResult<InterleavedOwned<T>> { ... } 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)> { ... } fn process_all_needed_output_len(&mut self, input_len: usize) -> usize { ... } fn set_chunk_size(&mut self, _chunksize: usize) -> ResampleResult<()> { ... }
}
Expand description

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

Required Methods§

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.

The input and output buffers are buffers from the audioadapter crate. The input buffer must implement the Adapter trait, and the output the corresponding AdapterMut trait. This ensures that this method is able to both read and write audio data from and to buffers with different layout, as well as different sample formats.

The indexing parameter is optional. When left out, the default values are used.

  • input_offset and output_offset: these determine how many frames at the beginning of the input and output buffers will be skipped before reading or writing starts. See the process_f64 example for how these may be used to process a longer sound clip.
  • partial_len: If the input buffer has fewer frames than the required input length, set partial_len to the available number. The resampler will then insert silence in place of the missing frames. This is useful for processing a longer buffer with repeated process calls, where at the last iteration there may be fewer frames left than what the resampler needs.
  • active_channels_mask: A vector of booleans determining what channels are to be processed. 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.

Before processing, the input and output buffer sizes are checked. If either has the wrong number of channels, or if the buffer can hold too few frames, a ResampleError is returned. Both input and output are allowed to be longer than required. The number of input samples consumed and the number output samples written per channel is returned in a tuple, (input_frames, output_frames).

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 nbr_channels(&self) -> usize

Get the number of channels this Resampler is configured for.

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 set_resample_ratio( &mut self, new_ratio: f64, ramp: bool, ) -> ResampleResult<()>

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.

If the argument ramp is set to true, the ratio will be ramped from the old to the new value during processing of the next chunk. This allows smooth transitions from one ratio to another. If ramp is false, the new ratio will be applied from the start of the next chunk.

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.

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.

Ratios above 1.0 slow down the output and lower the pitch, while ratios below 1.0 speed up the output and raise the pitch.

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

Source

fn reset(&mut self)

Reset the resampler state and clear all internal buffers.

Provided Methods§

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.

The output is returned as an InterleavedOwned struct that wraps a Vec<T> of interleaved samples.

The input_offset and active_channels_mask parameters have the same meaning as in process_into_buffer.

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.

Use process_all_needed_output_len to get the minimal length of the output buffer required to resample a clip of a given length.

The active_channels_mask parameter has the same meaning as in process_into_buffer.

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.

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.

The meaning of chunk size depends on the resampler, it refers to the input size for resamplers with fixed input size, and output size for resamplers with fixed output size.

Resamplers that do not support changing the chunk size return ResampleError::ChunkSizeNotAdjustable.

Implementors§

Source§

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

Source§

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