#resampler #processing #resampler-rs

resampler-rs

A waveform resampler mainly for audio processing written in Rust

1 unstable release

Uses new Rust 2024

new 0.0.1 Apr 28, 2025

#9 in #resampler

Download history 94/week @ 2025-04-24

94 downloads per month
Used in rustwav

Custom license

14KB
133 lines

Resampler for audio waveform processing

The purpose of this library is to help you change the sample rate of the audio file. For example, you have a 22050 Hz sample rate audio file, but you want a 48000 Hz sample rate audio file, you need resampling. Here is the resampler to help you losslessly stretch/compress the waveform to fit the sample rate.

How to use

First determine which FFT size fits you.

The best FFT size should be the minimum 2^n number of your sample rate. You have source sample rate and target sample rate, choose the largest. For example, you want to stretch 22050 to 48000,the best FFT size should be 65536.

Create the Resampler struct

let resampler = Resampler::new(65536);

Ask the resampler for the best process size

let source_sample_rate = 22050;
let target_sample_rate = 48000;

let process_size = resampler.get_process_size(resampler.get_fft_size(), source_sample_rate, target_sample_rate);

Do resampling in blocks. Each block size should be process_size

let source_audio = vec![0.0f32; 114514]; // Assume this is the waveform you want to resample
let target_audio = vec![0.0f32; 0];

let mut iter = source_audio.iter();
loop {
    let block: Vec<f32> = iter.by_ref().take(process_size).copied().collect();
    if block.is_empty() {
        break;
    }
    let block = resampler.resample(block, source_sample_rate, target_sample_rate).unwarp();
    target_audio.extend(block);
}

After that, your target_audio is the resampled audio. No artifacts are introduced by the resampler.

Dependencies

~3MB
~52K SLoC