timestretch 0.5.0

Pure Rust audio time stretching library optimized for EDM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Tests that verify streaming output is consistent with batch output.
//!
//! Streaming (PV-only mode) and batch (hybrid) use different algorithms,
//! so we cannot expect bit-exact parity. These tests verify that both
//! modes produce structurally similar output: same approximate length,
//! comparable energy, preserved frequency content, and finite samples.

use std::f32::consts::PI;
use timestretch::{stretch, EdmPreset, StreamProcessor, StretchParams};

fn sine_wave(freq: f32, sample_rate: u32, num_samples: usize) -> Vec<f32> {
    (0..num_samples)
        .map(|i| (2.0 * PI * freq * i as f32 / sample_rate as f32).sin())
        .collect()
}

fn rms(signal: &[f32]) -> f64 {
    if signal.is_empty() {
        return 0.0;
    }
    let sum: f64 = signal.iter().map(|&x| (x as f64) * (x as f64)).sum();
    (sum / signal.len() as f64).sqrt()
}

fn dft_energy_at(signal: &[f32], sample_rate: u32, freq: f32) -> f64 {
    let n = signal.len();
    if n == 0 {
        return 0.0;
    }
    let two_pi = 2.0 * PI;
    let mut real = 0.0f64;
    let mut imag = 0.0f64;
    for (i, &s) in signal.iter().enumerate() {
        let angle = two_pi * freq * i as f32 / sample_rate as f32;
        real += s as f64 * angle.cos() as f64;
        imag += s as f64 * angle.sin() as f64;
    }
    (real * real + imag * imag).sqrt() / n as f64
}

fn stream_stretch(input: &[f32], params: StretchParams, chunk_size: usize) -> Vec<f32> {
    let mut processor = StreamProcessor::new(params);
    let mut output = Vec::new();
    for chunk in input.chunks(chunk_size) {
        if let Ok(out) = processor.process(chunk) {
            output.extend_from_slice(&out);
        }
    }
    if let Ok(remaining) = processor.flush() {
        output.extend_from_slice(&remaining);
    }
    output
}

// ========== Length parity across stretch ratios ==========

#[test]
fn test_parity_length_expansion() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(1.5)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Both should expand audio (ratio > 1.0)
    assert!(batch.len() > input.len(), "Batch should expand");
    assert!(stream.len() > input.len(), "Stream should expand");

    // Length ratio should be within 30% of each other
    let batch_ratio = batch.len() as f64 / input.len() as f64;
    let stream_ratio = stream.len() as f64 / input.len() as f64;
    assert!(
        (batch_ratio - stream_ratio).abs() < 0.3,
        "Length parity at 1.5x: batch={:.3}, stream={:.3}",
        batch_ratio,
        stream_ratio
    );
}

#[test]
fn test_parity_length_compression() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(0.75)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Both should compress audio (ratio < 1.0)
    assert!(batch.len() < input.len(), "Batch should compress");
    assert!(stream.len() < input.len(), "Stream should compress");
}

#[test]
fn test_parity_length_identity() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(1.0)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Identity: both should be close to input length
    let batch_ratio = batch.len() as f64 / input.len() as f64;
    let stream_ratio = stream.len() as f64 / input.len() as f64;
    assert!(
        (batch_ratio - 1.0).abs() < 0.15,
        "Batch identity length ratio {:.3} too far from 1.0",
        batch_ratio
    );
    assert!(
        (stream_ratio - 1.0).abs() < 0.15,
        "Stream identity length ratio {:.3} too far from 1.0",
        stream_ratio
    );
}

// ========== Energy parity ==========

#[test]
fn test_parity_rms_energy_preserved() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);
    let input_rms = rms(&input);

    for &ratio in &[0.75, 1.0, 1.25, 1.5, 2.0] {
        let params = StretchParams::new(ratio)
            .with_sample_rate(sample_rate)
            .with_channels(1);

        let batch = stretch(&input, &params).unwrap();
        let stream = stream_stretch(&input, params, 4096);

        let batch_rms = rms(&batch);
        let stream_rms = rms(&stream);

        // Both should preserve reasonable energy (within 3x of input)
        assert!(
            batch_rms > input_rms * 0.2 && batch_rms < input_rms * 3.0,
            "Batch RMS {:.4} out of range at ratio {} (input={:.4})",
            batch_rms,
            ratio,
            input_rms
        );
        assert!(
            stream_rms > input_rms * 0.2 && stream_rms < input_rms * 3.0,
            "Stream RMS {:.4} out of range at ratio {} (input={:.4})",
            stream_rms,
            ratio,
            input_rms
        );
    }
}

#[test]
fn test_parity_rms_close_at_small_ratios() {
    // For small DJ-style adjustments, batch and stream RMS should be very close
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    for &ratio in &[0.97, 0.98, 0.99, 1.01, 1.02, 1.03] {
        let params = StretchParams::new(ratio)
            .with_sample_rate(sample_rate)
            .with_channels(1);

        let batch = stretch(&input, &params).unwrap();
        let stream = stream_stretch(&input, params, 4096);

        let batch_rms = rms(&batch);
        let stream_rms = rms(&stream);

        // At small ratios, RMS should be within 50% of each other
        let diff = (batch_rms - stream_rms).abs();
        let avg = (batch_rms + stream_rms) / 2.0;
        assert!(
            diff < avg * 0.5,
            "DJ ratio {}: batch_rms={:.4}, stream_rms={:.4}, diff={:.4}",
            ratio,
            batch_rms,
            stream_rms,
            diff
        );
    }
}

// ========== Frequency preservation parity ==========

#[test]
fn test_parity_440hz_preserved_both_modes() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(1.5)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    let batch_440 = dft_energy_at(&batch, sample_rate, 440.0);
    let stream_440 = dft_energy_at(&stream, sample_rate, 440.0);

    // Both should preserve 440 Hz content
    assert!(
        batch_440 > 0.03,
        "Batch should preserve 440 Hz (energy={:.4})",
        batch_440
    );
    assert!(
        stream_440 > 0.03,
        "Stream should preserve 440 Hz (energy={:.4})",
        stream_440
    );
}

#[test]
fn test_parity_two_tone_preserved() {
    let sample_rate = 44100;
    let n = sample_rate as usize * 2;
    let input: Vec<f32> = (0..n)
        .map(|i| {
            let t = i as f32 / sample_rate as f32;
            0.5 * (2.0 * PI * 440.0 * t).sin() + 0.5 * (2.0 * PI * 880.0 * t).sin()
        })
        .collect();

    let params = StretchParams::new(1.25)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Both tones should be present in both outputs
    assert!(dft_energy_at(&batch, sample_rate, 440.0) > 0.001);
    assert!(dft_energy_at(&batch, sample_rate, 880.0) > 0.001);
    assert!(dft_energy_at(&stream, sample_rate, 440.0) > 0.001);
    assert!(dft_energy_at(&stream, sample_rate, 880.0) > 0.001);
}

// ========== Finiteness and no-clipping parity ==========

#[test]
fn test_parity_all_finite_output() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    for &ratio in &[0.5, 0.75, 1.0, 1.5, 2.0] {
        let params = StretchParams::new(ratio)
            .with_sample_rate(sample_rate)
            .with_channels(1);

        let batch = stretch(&input, &params).unwrap();
        let stream = stream_stretch(&input, params, 4096);

        assert!(
            batch.iter().all(|s| s.is_finite()),
            "Batch output has non-finite at ratio {}",
            ratio
        );
        assert!(
            stream.iter().all(|s| s.is_finite()),
            "Stream output has non-finite at ratio {}",
            ratio
        );
    }
}

// ========== Stereo parity ==========

#[test]
fn test_parity_stereo_both_channels() {
    let sample_rate = 44100;
    let n = sample_rate as usize;
    let mut input = vec![0.0f32; n * 2];
    for i in 0..n {
        let t = i as f32 / sample_rate as f32;
        input[i * 2] = (2.0 * PI * 440.0 * t).sin(); // L
        input[i * 2 + 1] = (2.0 * PI * 880.0 * t).sin(); // R
    }

    let params = StretchParams::new(1.25)
        .with_sample_rate(sample_rate)
        .with_channels(2);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Both should have even sample count (stereo)
    assert_eq!(batch.len() % 2, 0, "Batch stereo must be even");
    assert_eq!(stream.len() % 2, 0, "Stream stereo must be even");

    // Both channels should have energy
    let batch_l_rms: f64 = rms(&batch.iter().step_by(2).copied().collect::<Vec<f32>>());
    let batch_r_rms: f64 = rms(&batch
        .iter()
        .skip(1)
        .step_by(2)
        .copied()
        .collect::<Vec<f32>>());
    let stream_l_rms: f64 = rms(&stream.iter().step_by(2).copied().collect::<Vec<f32>>());
    let stream_r_rms: f64 = rms(&stream
        .iter()
        .skip(1)
        .step_by(2)
        .copied()
        .collect::<Vec<f32>>());

    assert!(batch_l_rms > 0.1, "Batch L should have energy");
    assert!(batch_r_rms > 0.1, "Batch R should have energy");
    assert!(stream_l_rms > 0.1, "Stream L should have energy");
    assert!(stream_r_rms > 0.1, "Stream R should have energy");
}

// ========== Chunk size independence ==========

#[test]
fn test_parity_chunk_size_independent() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(1.25)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let out_small = stream_stretch(&input, params.clone(), 1024);
    let out_medium = stream_stretch(&input, params.clone(), 4096);
    let out_large = stream_stretch(&input, params, 16384);

    let rms_small = rms(&out_small);
    let rms_medium = rms(&out_medium);
    let rms_large = rms(&out_large);

    // RMS should be similar regardless of chunk size
    let avg = (rms_small + rms_medium + rms_large) / 3.0;
    assert!(
        (rms_small - avg).abs() < avg * 0.3,
        "Small chunk RMS {:.4} too far from average {:.4}",
        rms_small,
        avg
    );
    assert!(
        (rms_medium - avg).abs() < avg * 0.3,
        "Medium chunk RMS {:.4} too far from average {:.4}",
        rms_medium,
        avg
    );
    assert!(
        (rms_large - avg).abs() < avg * 0.3,
        "Large chunk RMS {:.4} too far from average {:.4}",
        rms_large,
        avg
    );
}

// ========== DJ beatmatch scenario ==========

#[test]
fn test_parity_dj_beatmatch_scenario() {
    // Simulate DJ matching 126 BPM to 128 BPM with both batch and streaming
    let sample_rate = 44100;
    let ratio = 126.0 / 128.0; // ~0.984
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 4);

    let params = StretchParams::new(ratio)
        .with_sample_rate(sample_rate)
        .with_channels(1)
        .with_preset(EdmPreset::DjBeatmatch);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    // Batch should be slightly shorter (speeding up)
    assert!(batch.len() < input.len(), "Batch DJ should compress");
    // Stream may have flush padding, so just check it produced output
    assert!(!stream.is_empty(), "Stream DJ should produce output");

    // Batch should preserve 440 Hz
    assert!(
        dft_energy_at(&batch, sample_rate, 440.0) > 0.01,
        "Batch should preserve 440 Hz"
    );
    // Stream has flush padding that dilutes DFT energy, so just check RMS
    let stream_rms = rms(&stream);
    assert!(
        stream_rms > 0.01,
        "Stream DJ should have non-trivial energy (rms={:.4})",
        stream_rms
    );

    // No NaN/Inf in either output
    assert!(batch.iter().all(|s| s.is_finite()));
    assert!(stream.iter().all(|s| s.is_finite()));
}

// ========== Preset parity ==========

#[test]
fn test_parity_all_presets_produce_output() {
    let sample_rate = 44100;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let presets = [
        (EdmPreset::DjBeatmatch, 0.98),
        (EdmPreset::HouseLoop, 1.25),
        (EdmPreset::Halftime, 2.0),
        (EdmPreset::Ambient, 2.5),
        (EdmPreset::VocalChop, 1.5),
    ];

    for (preset, ratio) in &presets {
        let params = StretchParams::new(*ratio)
            .with_sample_rate(sample_rate)
            .with_channels(1)
            .with_preset(*preset);

        let batch = stretch(&input, &params).unwrap();
        let stream = stream_stretch(&input, params, 4096);

        assert!(
            !batch.is_empty(),
            "Batch should produce output for {:?}",
            preset
        );
        assert!(
            !stream.is_empty(),
            "Stream should produce output for {:?}",
            preset
        );
        assert!(
            batch.iter().all(|s| s.is_finite()),
            "Batch {:?} has non-finite",
            preset
        );
        assert!(
            stream.iter().all(|s| s.is_finite()),
            "Stream {:?} has non-finite",
            preset
        );
    }
}

// ========== 48kHz parity ==========

#[test]
fn test_parity_48khz() {
    let sample_rate = 48000;
    let input = sine_wave(440.0, sample_rate, sample_rate as usize * 2);

    let params = StretchParams::new(1.5)
        .with_sample_rate(sample_rate)
        .with_channels(1);

    let batch = stretch(&input, &params).unwrap();
    let stream = stream_stretch(&input, params, 4096);

    assert!(!batch.is_empty());
    assert!(!stream.is_empty());

    let batch_ratio = batch.len() as f64 / input.len() as f64;
    let stream_ratio = stream.len() as f64 / input.len() as f64;

    assert!(
        (batch_ratio - 1.5).abs() < 0.5,
        "Batch 48kHz ratio {:.3} too far from 1.5",
        batch_ratio
    );
    assert!(
        (stream_ratio - 1.5).abs() < 0.5,
        "Stream 48kHz ratio {:.3} too far from 1.5",
        stream_ratio
    );
}