pub struct Resampler { /* private fields */ }
Expand description
How the Resampler works For audio stretching:
- The input audio remains its original length, and zero-padding is applied at the end to reach the target length.
- Perform FFT transformation to obtain the frequency domain.
- In the frequency domain, scale down the frequency values proportionally (shift them lower).
- Perform inverse FFT to obtain the stretched audio.
For audio compression:
- Take the input audio.
- Perform FFT transformation.
- In the frequency domain, scale up the frequency values proportionally (shift them higher).
- Perform inverse FFT to obtain audio with increased pitch but unchanged length.
- Truncate the audio to shorten its duration.
This implies: the FFT length must be chosen as the longest possible length involved.
Implementations§
Source§impl Resampler
impl Resampler
pub fn new(fft_size: usize) -> Resampler
Sourcepub fn resample_core(
&self,
samples: &[f32],
desired_length: usize,
) -> Result<Vec<f32>, ResamplerError>
pub fn resample_core( &self, samples: &[f32], desired_length: usize, ) -> Result<Vec<f32>, ResamplerError>
desired_length
: The target audio length to achieve, which must not exceed the FFT size.
When samples.len() < desired_length, it indicates audio stretching to desired_length.
When samples.len() > desired_length, it indicates audio compression to desired_length.
Sourcepub fn get_process_size(
&self,
orig_size: usize,
src_sample_rate: u32,
dst_sample_rate: u32,
) -> usize
pub fn get_process_size( &self, orig_size: usize, src_sample_rate: u32, dst_sample_rate: u32, ) -> usize
The processing unit size should be adjusted to work in “chunks per second”,
and artifacts will vanish when the chunk count aligns with the maximum infrasonic frequency.
Calling self.get_desired_length()
determines the processed chunk size calculated based on the target sample rate.
Sourcepub fn get_desired_length(
&self,
proc_size: usize,
src_sample_rate: u32,
dst_sample_rate: u32,
) -> usize
pub fn get_desired_length( &self, proc_size: usize, src_sample_rate: u32, dst_sample_rate: u32, ) -> usize
Get the processed chunk size calculated based on the target sample rate.