Struct FixedResampler

Source
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>

Source

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 using FixedResampler::process_interleaved, then set this to true. Otherwise, you can set this to false to save a bit of memory.
§Panics

Panics if:

  • num_channels == 0
  • num_channels > MAX_CHANNELS
  • in_sample_rate == 0
  • out_sample_rate == 0
Source

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
Source

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 using FixedResampler::process_interleaved, then set this to true. Otherwise, you can set this to false 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
Source

pub fn num_channels(&self) -> NonZeroUsize

The number of channels configured for this resampler.

Source

pub fn in_sample_rate(&self) -> u32

The input sample rate configured for this resampler.

Source

pub fn out_sample_rate(&self) -> u32

The output sample rate configured for this resampler.

Source

pub fn ratio(&self) -> f64

The resampling ratio output / input.

Source

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.

Source

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.

Source

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).

Source

pub fn is_interleaved(&self) -> bool

Whether or not the interleaved argument was set to true in the constructor.

Source

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.

Source

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 is Some, 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 - If true, 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 to false.

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.
Source

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 is Some, 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 - If true, 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 to false.

This method is realtime-safe.

§Panics

Panics if the interleaved argument in the constructor was false.

Source

pub fn reset(&mut self)

Reset the state of the resampler.

This method is realtime-safe.

Trait Implementations§

Source§

impl<T: Sample, const MAX_CHANNELS: usize> Into<ResamplerType<T>> for FixedResampler<T, MAX_CHANNELS>

Source§

fn into(self) -> ResamplerType<T>

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl<T, const MAX_CHANNELS: usize> Freeze for FixedResampler<T, MAX_CHANNELS>

§

impl<T, const MAX_CHANNELS: usize> !RefUnwindSafe for FixedResampler<T, MAX_CHANNELS>

§

impl<T, const MAX_CHANNELS: usize> Send for FixedResampler<T, MAX_CHANNELS>

§

impl<T, const MAX_CHANNELS: usize> !Sync for FixedResampler<T, MAX_CHANNELS>

§

impl<T, const MAX_CHANNELS: usize> Unpin for FixedResampler<T, MAX_CHANNELS>
where T: Unpin,

§

impl<T, const MAX_CHANNELS: usize> !UnwindSafe for FixedResampler<T, MAX_CHANNELS>

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.