pub struct NonRtResampler<T: Sample> { /* private fields */ }
Expand description
An easy to use resampler for use in non-realtime applications.
Implementations§
Source§impl<T: Sample> NonRtResampler<T>
impl<T: Sample> NonRtResampler<T>
Sourcepub fn new(
in_sample_rate: u32,
out_sample_rate: u32,
num_channels: usize,
quality: ResampleQuality,
) -> Self
pub fn new( in_sample_rate: u32, out_sample_rate: u32, num_channels: usize, quality: ResampleQuality, ) -> Self
Create a new non-realtime resampler.
in_sample_rate
- The sample rate of the input stream.out_sample_rate
- The sample rate of the output stream.num_channels
- The number of channels.interleaved
- If you are planning to useNonRtResampler::process_interleaved
, set this totrue
. Otherwise you can set this tofalse
to save a bit of memory.quality
- The quality of the resampling algorithm.
§Panics
Panics if:
in_sample_rate == 0
out_sample_rate == 0
num_channels == 0
,
Sourcepub fn from_custom(resampler: impl Into<ResamplerType<T>>) -> Self
pub fn from_custom(resampler: impl Into<ResamplerType<T>>) -> Self
Create a new non-realtime resampler using the given rubato resampler.
§Panics
Panics if max_out_frames == 0
.
Sourcepub fn num_channels(&self) -> NonZeroUsize
pub fn num_channels(&self) -> NonZeroUsize
Get the number of channels this resampler is configured for.
Sourcepub fn output_delay(&self) -> usize
pub fn output_delay(&self) -> usize
Get the delay of the internal resampler, reported as a number of output frames.
Note, this resampler automatically truncates the starting padded zero frames for you.
Sourcepub fn max_processed_frames(&self) -> usize
pub fn max_processed_frames(&self) -> usize
The maximum number of frames that can appear in a single call to the closure in
NonRtResampler::process()
and NonRtResampler::process_interleaved()
.
Sourcepub fn out_frames(
&self,
in_sample_rate: u32,
out_sample_rate: u32,
in_frames: usize,
) -> usize
pub fn out_frames( &self, in_sample_rate: u32, out_sample_rate: u32, in_frames: usize, ) -> usize
The length of an output buffer in frames for a given input buffer.
Note, the resampler may add extra padded zeros to the end of the output buffer,
so prefer to use NonRtResampler::out_alloc_frames()
instead to reserve the
capacity for an output buffer.
Sourcepub fn out_alloc_frames(
&self,
in_sample_rate: u32,
out_sample_rate: u32,
in_frames: usize,
) -> usize
pub fn out_alloc_frames( &self, in_sample_rate: u32, out_sample_rate: u32, in_frames: usize, ) -> usize
The number of frames needed to allocate an output buffer for a given input buffer.
Sourcepub fn process<Vin: AsRef<[T]>>(
&mut self,
input: &[Vin],
on_processed: impl FnMut(&[Vec<T>], usize),
is_last_packet: bool,
)
pub fn process<Vin: AsRef<[T]>>( &mut self, input: &[Vin], on_processed: impl FnMut(&[Vec<T>], usize), is_last_packet: bool, )
Resample the given input data.
input
- The input data.on_processed
- Called whenever there is new resampled data. The first argument is the buffers containing the new data, and the second argument is the length of the new data in frames (NOTE, the second argument may be less than the length of theVec
s in the first argument).is_last_packet
- Whether or not this is the last (or only) packet of data that will be resampled. This ensures that any leftover samples in the internal resampler are flushed to the output.
If you want to flush the remaining samples out of the internal resampler when there
is no more input data left, set the input
to an empty slice with no channels
(&[]
), and set is_last_packet
to true
.
Note, when flushing the remaining data with is_last_packet
, the resulting output
may have a few extra padded zero samples on the end.
The delay of the internal resampler is automatically accounted for (the starting
padded zero frames are automatically truncated). Call NonRtResampler::reset()
before reusing this resampler for a new sample.
Note, this method is NOT realtime-safe.
Sourcepub fn process_interleaved(
&mut self,
input: &[T],
on_processed: impl FnMut(&[T]),
is_last_packet: bool,
)
pub fn process_interleaved( &mut self, input: &[T], on_processed: impl FnMut(&[T]), is_last_packet: bool, )
Resample the given input data in interleaved format.
input
- The input data in interleaved format.on_processed
- Called whenever there is new resampled data. This data is in interleaved format.is_last_packet
- Whether or not this is the last (or only) packet of data that will be resampled. This ensures that any leftover samples in the internal resampler are flushed to the output.
If you want to flush the remaining samples out of the internal resampler when there
is no more input data left, set the input
to an empty slice (&[]
), and set
is_last_packet
to true
.
Note, when flushing the remaining data with is_last_packet
, the resulting output
may have a few extra padded zero samples on the end.
The delay of the internal resampler is automatically accounted for (the starting
padded zero frames are automatically truncated). Call NonRtResampler::reset()
before reusing this resampler for a new sample.
Note, this method is NOT realtime-safe.