pub struct FixedResampler<T: Sample, const MAX_CHANNELS: usize> { /* private fields */ }
Expand description
An easy-to-use resampler with a fixed ratio.
Internally this uses the rubato
crate.
Implementations§
Source§impl<T: Sample, const MAX_CHANNELS: usize> FixedResampler<T, MAX_CHANNELS>
impl<T: Sample, const MAX_CHANNELS: usize> FixedResampler<T, MAX_CHANNELS>
Sourcepub fn new(
num_channels: NonZeroUsize,
in_sample_rate: u32,
out_sample_rate: u32,
quality: ResampleQuality,
interleaved: bool,
) -> Self
pub fn new( num_channels: NonZeroUsize, in_sample_rate: u32, out_sample_rate: u32, quality: ResampleQuality, interleaved: bool, ) -> Self
Create a new FixedResampler
.
num_channels
- The number of audio channels.in_sample_rate
- The sample rate of the input data.out_sample_rate
- The sample rate of the output data.quality
- The quality of the resampling algorithm to use.interleaved
- If you plan on usingFixedResampler::process_interleaved
, then set this totrue
. Otherwise, you can set this tofalse
to save a bit of memory.
§Panics
Panics if:
num_channels == 0
num_channels > MAX_CHANNELS
in_sample_rate == 0
out_sample_rate == 0
Sourcepub fn arbitrary_ratio_sinc(
in_sample_rate: u32,
ratio: f64,
num_channels: NonZeroUsize,
interleaved: bool,
) -> Self
pub fn arbitrary_ratio_sinc( in_sample_rate: u32, ratio: f64, num_channels: NonZeroUsize, interleaved: bool, ) -> Self
Create a new resampler that uses the SincFixedIn
resampler from rubato.
This has similar quality to the rubato::FftFixedIn
resampler used
for ResampleQuality::High
, but with much lower performance. Use
this if you need a non-integer ratio (i.e. repitching a sample).
in_sample_rate
- The sample rate of the input data.ratio
- The resampling ratio (output / input
)num_channels
- The number of channels
More specifically, this creates a resampler with the following parameters:
SincInterpolationParameters {
sinc_len: 128,
f_cutoff: rubato::calculate_cutoff(128, WindowFunction::Blackman2),
interpolation: SincInterpolationType::Cubic,
oversampling_factor: 512,
window: WindowFunction::Blackman2,
}
§Panics
Panics if:
in_sample_rate == 0
ratio <= 0.0
num_channels > MAX_CHANNELS
Sourcepub fn from_custom(
resampler: impl Into<ResamplerType<T>>,
in_sample_rate: u32,
out_sample_rate: u32,
interleaved: bool,
) -> Self
pub fn from_custom( resampler: impl Into<ResamplerType<T>>, in_sample_rate: u32, out_sample_rate: u32, interleaved: bool, ) -> Self
Create a new FixedResampler
using the custom resampler.
resampler
- The resampler to use.in_sample_rate
- The sample rate of the input data.out_sample_rate
- The sample rate of the output data.interleaved
- If you plan on usingFixedResampler::process_interleaved
, then set this totrue
. Otherwise, you can set this tofalse
to save a bit of memory.
§Panics
Panics if:
resampler.num_channels() == 0
resampler.num_channels() == 0 > MAX_CHANNELS
in_sample_rate == 0
out_sample_rate == 0
Sourcepub fn num_channels(&self) -> NonZeroUsize
pub fn num_channels(&self) -> NonZeroUsize
The number of channels configured for this resampler.
Sourcepub fn in_sample_rate(&self) -> u32
pub fn in_sample_rate(&self) -> u32
The input sample rate configured for this resampler.
Sourcepub fn out_sample_rate(&self) -> u32
pub fn out_sample_rate(&self) -> u32
The output sample rate configured for this resampler.
Sourcepub fn input_block_frames(&self) -> usize
pub fn input_block_frames(&self) -> usize
The number of frames (samples in a single channel of audio) that appear in a single packet of input data in the internal resampler.
Sourcepub fn max_output_block_frames(&self) -> usize
pub fn max_output_block_frames(&self) -> usize
The maximum number of frames (samples in a single channel of audio) that can
appear in a single call to the on_output_packet
closure in
FixedResampler::process
and FixedResampler::process_interleaved
.
Sourcepub fn output_delay(&self) -> usize
pub fn output_delay(&self) -> usize
The delay introduced by the internal resampler in number of output frames ( samples in a single channel of audio).
Sourcepub fn is_interleaved(&self) -> bool
pub fn is_interleaved(&self) -> bool
Whether or not the interleaved
argument was set to true
in the constructor.
Sourcepub fn out_alloc_frames(&self, input_frames: u64) -> u64
pub fn out_alloc_frames(&self, input_frames: u64) -> u64
The number of frames (samples in a single channel of audio) that are needed for an output buffer given the number of input frames.
Sourcepub fn process<Vin: AsRef<[T]>>(
&mut self,
input: &[Vin],
input_range: Range<usize>,
on_output_packet: impl FnMut(ArrayVec<&[T], MAX_CHANNELS>),
last_packet: Option<LastPacketInfo>,
trim_delay: bool,
)
pub fn process<Vin: AsRef<[T]>>( &mut self, input: &[Vin], input_range: Range<usize>, on_output_packet: impl FnMut(ArrayVec<&[T], MAX_CHANNELS>), last_packet: Option<LastPacketInfo>, trim_delay: bool, )
Process the given de-interleaved input data and return packets of de-interleaved resampled output data.
input
- The de-interleaved channels of input data.input_range
- The range in each input channel to read from.on_output_packet
- Gets called whenever there is a new packet of resampled output data. The output data is in de-interleaved format.last_packet
- If this isSome
, then any leftover input samples in the buffer will be flushed out and the resampler reset. Use this if this is the last/only packet of input data when used in a non-realtime context.trim_delay
- Iftrue
, then the initial padded zeros introduced by the internal resampler will be trimmed off. If this is being used in a realtime context, then prefer to set this tofalse
.
This method is realtime-safe.
§Panics
Panics if:
input.len() < self.num_channels()
- The
input_range
is out of bounds for any of the input channels.
Sourcepub fn process_interleaved(
&mut self,
input: &[T],
on_output_packet: impl FnMut(&[T]),
last_packet: Option<LastPacketInfo>,
trim_delay: bool,
)
pub fn process_interleaved( &mut self, input: &[T], on_output_packet: impl FnMut(&[T]), last_packet: Option<LastPacketInfo>, trim_delay: bool, )
Process the given interleaved input data and return packets of interleaved resampled output data.
input
- The interleaved input data.on_output_packet
- Gets called whenever there is a new packet of resampled output data. The output data is in interleaved format.last_packet
- If this isSome
, then any leftover input samples in the buffer will be flushed out and the resampler reset. Use this if this is the last/only packet of input data when used in a non-realtime context.trim_delay
- Iftrue
, then the initial padded zeros introduced by the internal resampler will be trimmed off. If this is being used in a realtime context, then prefer to set this tofalse
.
This method is realtime-safe.
§Panics
Panics if the interleaved
argument in the constructor was false
.