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, ¶ms).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
BeatGridwith BPM and beat positions. - detect_
beat_ grid_ buffer - Detects beats in an
AudioBufferand returns aBeatGrid. - 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
AudioBufferwithout 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
AudioBufferfrom one BPM to another. - stretch_
bpm_ buffer_ auto - Stretches an
AudioBufferto a target BPM, auto-detecting the source BPM. - stretch_
buffer - Stretches an
AudioBufferand returns a newAudioBuffer. - 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.