Skip to main content

Crate timestretch

Crate timestretch 

Source
Expand description

Pure Rust audio time stretching library optimized for electronic dance music.

timestretch changes the tempo of audio without altering its pitch, using a hybrid algorithm that combines WSOLA (for transients) with a phase vocoder (for tonal content). It ships with five EDM-tuned presets and a streaming API for real-time use.

§Quick Start

use timestretch::{StretchParams, EdmPreset};

// 1 second of 440 Hz sine at 44.1 kHz
let input: Vec<f32> = (0..44100)
    .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
    .collect();

let params = StretchParams::new(1.5)
    .with_sample_rate(44100)
    .with_channels(1)
    .with_preset(EdmPreset::HouseLoop);

let output = timestretch::stretch(&input, &params).unwrap();
assert!(output.len() > input.len()); // ~1.5x longer

§Streaming

For real-time use, feed audio in chunks via StreamProcessor:

use timestretch::{EdmPreset, StreamProcessor, StretchParams};

let params = StretchParams::new(1.0)
    .with_preset(EdmPreset::DjBeatmatch)
    .with_sample_rate(44_100)
    .with_channels(2);

let mut processor = StreamProcessor::new(params);

let input_chunk = vec![0.0f32; 256 * 2];
let mut output_chunk = Vec::with_capacity(1_024);
processor.process_into(&input_chunk, &mut output_chunk).unwrap();
processor.set_stretch_ratio(1.05).unwrap();
let mut remaining = Vec::with_capacity(1_024);
processor.flush_into(&mut remaining).unwrap();

Re-exports§

pub use analysis::beat::BeatGrid;
pub use analysis::preanalysis::analyze_for_dj;
pub use core::preanalysis::read_preanalysis_json;
pub use core::preanalysis::write_preanalysis_json;
pub use core::preanalysis::PreAnalysisArtifact;
pub use core::types::AudioBuffer;
pub use core::types::Channels;
pub use core::types::CrossfadeMode;
pub use core::types::EdmPreset;
pub use core::types::EnvelopePreset;
pub use core::types::FrameIter;
pub use core::types::QualityMode;
pub use core::types::Sample;
pub use core::types::StretchParams;
pub use core::types::TransientThresholdPolicy;
pub use core::window::WindowType;
pub use error::StretchError;
pub use stream::StreamProcessor;
pub use stream::TransientResetStats;
pub use stretch::phase_locking::PhaseLockingMode;
pub use stretch::stereo::StereoMode;

Modules§

analysis
Audio analysis: transient detection, beat tracking, frequency analysis, and HPSS.
core
Core types, window functions, and resampling utilities.
error
Error types for the timestretch crate.
io
Audio file I/O (WAV format).
stream
Streaming (chunked) audio processing for real-time use.
stretch
Time stretching algorithms: phase vocoder, WSOLA, and the hybrid combiner.

Functions§

bpm_ratio
Returns the stretch ratio needed to change from one BPM to another.
detect_beat_grid
Detects beats and returns a BeatGrid with BPM and beat positions.
detect_beat_grid_buffer
Detects beats in an AudioBuffer and returns a BeatGrid.
detect_bpm
Detects the BPM of a mono audio signal.
detect_bpm_buffer
Detects the BPM of an AudioBuffer.
pitch_shift
Shifts the pitch of audio without changing its duration.
pitch_shift_buffer
Shifts the pitch of an AudioBuffer without changing its duration.
pitch_shift_wav_file
Reads a WAV file, pitch-shifts it, and writes the result to another WAV file.
stretch
Stretches audio samples by the given parameters.
stretch_bpm_buffer
Stretches an AudioBuffer from one BPM to another.
stretch_bpm_buffer_auto
Stretches an AudioBuffer to a target BPM, auto-detecting the source BPM.
stretch_buffer
Stretches an AudioBuffer and returns a new AudioBuffer.
stretch_into
Stretches audio samples, appending the result to a caller-provided buffer.
stretch_to_bpm
Stretches audio from one BPM to another.
stretch_to_bpm_auto
Stretches audio to a target BPM, auto-detecting the source BPM.
stretch_to_bpm_auto_wav_file
Reads a WAV file, auto-detects its BPM, stretches to the target BPM, and writes the result.
stretch_to_bpm_wav_file
Reads a WAV file, stretches it from one BPM to another, and writes the result.
stretch_wav_file
Reads a WAV file, stretches it, and writes the result to another WAV file.