timestretch 0.8.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
//! New-engine port of the counting-allocator gate (ROADMAP new Stage 1):
//! zero heap activity in steady state on the audio-thread `process` path,
//! including under per-callback tempo retargets. The host-side source feed
//! and control writes are also covered — the whole runtime loop must be
//! allocation-free after construction.

use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use timestretch::engine::{Engine, EngineConfig, EngineProfile};

struct CountingAllocator;

static TRACK_ALLOCATIONS: AtomicBool = AtomicBool::new(false);
static ALLOC_CALLS: AtomicUsize = AtomicUsize::new(0);
static ALLOC_BYTES: AtomicUsize = AtomicUsize::new(0);
static REALLOC_CALLS: AtomicUsize = AtomicUsize::new(0);
static REALLOC_BYTES: AtomicUsize = AtomicUsize::new(0);
static ALLOC_TEST_MUTEX: Mutex<()> = Mutex::new(());

#[global_allocator]
static GLOBAL_ALLOCATOR: CountingAllocator = CountingAllocator;

unsafe impl GlobalAlloc for CountingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let ptr = unsafe { System.alloc(layout) };
        if TRACK_ALLOCATIONS.load(Ordering::Relaxed) {
            ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
            ALLOC_BYTES.fetch_add(layout.size(), Ordering::Relaxed);
        }
        ptr
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        let ptr = unsafe { System.alloc_zeroed(layout) };
        if TRACK_ALLOCATIONS.load(Ordering::Relaxed) {
            ALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
            ALLOC_BYTES.fetch_add(layout.size(), Ordering::Relaxed);
        }
        ptr
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        let out = unsafe { System.realloc(ptr, layout, new_size) };
        if TRACK_ALLOCATIONS.load(Ordering::Relaxed) {
            REALLOC_CALLS.fetch_add(1, Ordering::Relaxed);
            REALLOC_BYTES.fetch_add(new_size, Ordering::Relaxed);
            if std::env::var_os("ALLOC_TRACE").is_some() {
                TRACK_ALLOCATIONS.store(false, Ordering::SeqCst);
                eprintln!(
                    "realloc {} -> {} bytes\n{}",
                    layout.size(),
                    new_size,
                    std::backtrace::Backtrace::force_capture()
                );
                TRACK_ALLOCATIONS.store(true, Ordering::SeqCst);
            }
        }
        out
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        unsafe { System.dealloc(ptr, layout) }
    }
}

fn begin_alloc_tracking() {
    ALLOC_CALLS.store(0, Ordering::Relaxed);
    ALLOC_BYTES.store(0, Ordering::Relaxed);
    REALLOC_CALLS.store(0, Ordering::Relaxed);
    REALLOC_BYTES.store(0, Ordering::Relaxed);
    TRACK_ALLOCATIONS.store(true, Ordering::SeqCst);
}

fn end_alloc_tracking() -> (usize, usize, usize, usize) {
    TRACK_ALLOCATIONS.store(false, Ordering::SeqCst);
    (
        ALLOC_CALLS.load(Ordering::Relaxed),
        REALLOC_CALLS.load(Ordering::Relaxed),
        ALLOC_BYTES.load(Ordering::Relaxed),
        REALLOC_BYTES.load(Ordering::Relaxed),
    )
}

fn test_chunk_stereo(frames: usize, sample_rate: f32, phase_frames: usize) -> Vec<f32> {
    let freq_l = 95.0f32;
    let freq_r = 142.0f32;
    let mut out = Vec::with_capacity(frames * 2);
    for n in 0..frames {
        let t = (phase_frames + n) as f32 / sample_rate;
        out.push((2.0 * std::f32::consts::PI * freq_l * t).sin());
        out.push((2.0 * std::f32::consts::PI * freq_r * t).sin());
    }
    out
}

#[test]
fn engine_process_steady_state_no_heap_activity() {
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const SAMPLE_RATE: u32 = 44_100;
    const CALLBACK_FRAMES: usize = 256;
    const WARMUP_ITERS: usize = 64;
    const MEASURE_ITERS: usize = 96;

    let handles = Engine::build(EngineConfig::default()).expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);

    // Preallocate the feed chunk and output outside the tracked region.
    let feed = test_chunk_stereo(2048, SAMPLE_RATE as f32, 0);
    let mut out = vec![0.0f32; CALLBACK_FRAMES * 2];

    source.push(&feed);
    for _ in 0..WARMUP_ITERS {
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }

    begin_alloc_tracking();
    for i in 0..MEASURE_ITERS {
        // Per-callback tempo retargets: the DJ ride case this path exists
        // for. Both the control write and the audio pull must be silent.
        let t = i as f64 / MEASURE_ITERS as f64;
        controller.set_tempo_rate(1.0 + 0.06 * (2.0 * std::f64::consts::PI * t).sin());
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert_eq!(
        controller.underrun_frames(),
        0,
        "steady state must not underrun"
    );
    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "engine steady state allocated: alloc_calls={alloc_calls}, realloc_calls={realloc_calls}, \
         alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
}

#[test]
fn engine_process_varied_callback_sizes_no_heap_activity() {
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const SAMPLE_RATE: u32 = 44_100;
    const WARMUP_ITERS: usize = 64;
    const MEASURE_ITERS: usize = 96;
    // The block scheduler must adapt caller sizes 64–1024 without touching
    // the heap.
    const SIZES: [usize; 5] = [64, 128, 333, 512, 1024];

    let handles = Engine::build(EngineConfig::default()).expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);

    let feed = test_chunk_stereo(4096, SAMPLE_RATE as f32, 0);
    let mut out = vec![0.0f32; 1024 * 2];

    source.push(&feed);
    for i in 0..WARMUP_ITERS {
        let frames = SIZES[i % SIZES.len()];
        if source.occupied_frames() < source.demand_hint(frames, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out[..frames * 2]);
    }

    begin_alloc_tracking();
    for i in 0..MEASURE_ITERS {
        let frames = SIZES[i % SIZES.len()];
        controller.set_tempo_rate(if i % 2 == 0 { 1.08 } else { 0.92 });
        if source.occupied_frames() < source.demand_hint(frames, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out[..frames * 2]);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "varied-callback steady state allocated: alloc_calls={alloc_calls}, \
         realloc_calls={realloc_calls}, alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
    let _ = controller;
}

#[test]
fn engine_keylock_steady_state_no_heap_activity() {
    // ROADMAP Stage 2 exit criterion: zero-alloc steady state holds with
    // keylock engaged (band split + PV corrector + post-resampler), under
    // per-callback tempo retargets.
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const SAMPLE_RATE: u32 = 44_100;
    const CALLBACK_FRAMES: usize = 256;
    const WARMUP_ITERS: usize = 128;
    const MEASURE_ITERS: usize = 96;

    let handles = Engine::build(EngineConfig {
        profile: EngineProfile::Keylock,
        ..EngineConfig::default()
    })
    .expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);

    let feed = test_chunk_stereo(2048, SAMPLE_RATE as f32, 0);
    let mut out = vec![0.0f32; CALLBACK_FRAMES * 2];

    source.push(&feed);
    for i in 0..WARMUP_ITERS {
        // Warm every transposition-dependent buffer before measuring.
        let t = i as f64 / WARMUP_ITERS as f64;
        controller.set_tempo_rate(1.0 + 0.2 * (2.0 * std::f64::consts::PI * t).sin());
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.3) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }

    begin_alloc_tracking();
    for i in 0..MEASURE_ITERS {
        let t = i as f64 / MEASURE_ITERS as f64;
        controller.set_tempo_rate(1.0 + 0.08 * (2.0 * std::f64::consts::PI * t).sin());
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert_eq!(
        controller.underrun_frames(),
        0,
        "keylock steady state must not underrun"
    );
    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "keylock steady state allocated: alloc_calls={alloc_calls}, realloc_calls={realloc_calls}, \
         alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
}

#[test]
fn engine_keylock_with_large_artifact_no_heap_activity() {
    // ROADMAP Stage 4 exit criterion: zero-alloc steady state with a large
    // artifact attached — thousands of onsets and beats crossing the
    // cursor, mapped through the varispeed timeline every block, firing
    // splice guidance and PV resets.
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const SAMPLE_RATE: u32 = 44_100;
    const CALLBACK_FRAMES: usize = 256;
    const WARMUP_ITERS: usize = 128;
    const MEASURE_ITERS: usize = 96;

    let onset_spacing = 2_048usize;
    let transient_onsets: Vec<usize> = (1..4_000).map(|i| i * onset_spacing).collect();
    let transient_strengths = vec![0.9f32; transient_onsets.len()];
    let beat_positions: Vec<usize> = (1..2_000).map(|i| i * 20_671).collect();
    let artifact = timestretch::PreAnalysisArtifact {
        version: timestretch::PREANALYSIS_VERSION,
        sample_rate: SAMPLE_RATE,
        bpm: 128.0,
        downbeat_offset_samples: 0,
        confidence: 0.95,
        beat_positions,
        transient_onsets,
        transient_strengths,
        onset_band_flux: Vec::new(),
        analysis_hop_size: 512,
        source_len_samples: 0,
        content_hash: 0,
        ..Default::default()
    };

    let handles = Engine::build(EngineConfig {
        profile: EngineProfile::Keylock,
        pre_analysis: Some(std::sync::Arc::new(artifact)),
        ..EngineConfig::default()
    })
    .expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);
    source.set_track_position(0);

    let feed = test_chunk_stereo(2048, SAMPLE_RATE as f32, 0);
    let mut out = vec![0.0f32; CALLBACK_FRAMES * 2];

    source.push(&feed);
    for i in 0..WARMUP_ITERS {
        let t = i as f64 / WARMUP_ITERS as f64;
        controller.set_tempo_rate(1.0 + 0.06 * (2.0 * std::f64::consts::PI * t).sin());
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }

    begin_alloc_tracking();
    for i in 0..MEASURE_ITERS {
        let t = i as f64 / MEASURE_ITERS as f64;
        controller.set_tempo_rate(1.0 + 0.06 * (2.0 * std::f64::consts::PI * t).sin());
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "artifact-driven steady state allocated: alloc_calls={alloc_calls}, \
         realloc_calls={realloc_calls}, alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
}

#[test]
fn engine_warm_start_no_heap_activity() {
    // ROADMAP Stage 5 exit criterion: allocation-free warm start — the
    // reset, the priming passes, and the declick all run on the audio
    // thread with zero heap activity.
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const CALLBACK_FRAMES: usize = 256;

    let handles = Engine::build(EngineConfig {
        profile: EngineProfile::Keylock,
        ..EngineConfig::default()
    })
    .expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);

    let feed = test_chunk_stereo(4096, 44_100.0, 0);
    let mut out = vec![0.0f32; CALLBACK_FRAMES * 2];

    // Warm up ordinary playback plus one full warm-start cycle so every
    // buffer reaches steady capacity before measuring.
    source.push(&feed);
    for _ in 0..64 {
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }
    let preroll = processor.warm_start_preroll_frames();
    processor.reset();
    source.set_track_position(0);
    controller.warm_start(preroll as u32);
    for _ in 0..32 {
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }

    // The measured warm start.
    begin_alloc_tracking();
    processor.reset();
    source.set_track_position(0);
    controller.warm_start(preroll as u32);
    for _ in 0..32 {
        if source.occupied_frames() < source.demand_hint(CALLBACK_FRAMES, 1.1) {
            source.push(&feed);
        }
        processor.process(&mut out);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "warm start allocated: alloc_calls={alloc_calls}, realloc_calls={realloc_calls}, \
         alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
}

#[test]
fn engine_underrun_and_recovery_no_heap_activity() {
    let _guard = ALLOC_TEST_MUTEX
        .lock()
        .expect("allocation test mutex poisoned");
    const CALLBACK_FRAMES: usize = 256;

    let handles = Engine::build(EngineConfig::default()).expect("engine builds");
    let (controller, mut processor, mut source) =
        (handles.controller, handles.processor, handles.source);

    let feed = test_chunk_stereo(1024, 44_100.0, 0);
    let mut out = vec![0.0f32; CALLBACK_FRAMES * 2];

    source.push(&feed);
    for _ in 0..16 {
        processor.process(&mut out);
    }

    // Starve the ring, then refill: the underrun path (silence fill,
    // counters) and the recovery path must both be allocation-free.
    begin_alloc_tracking();
    for _ in 0..8 {
        processor.process(&mut out);
    }
    source.push(&feed);
    for _ in 0..8 {
        processor.process(&mut out);
    }
    let (alloc_calls, realloc_calls, alloc_bytes, realloc_bytes) = end_alloc_tracking();

    assert!(
        controller.underrun_frames() > 0,
        "starvation must be counted"
    );
    assert_eq!(
        alloc_calls + realloc_calls,
        0,
        "underrun/recovery allocated: alloc_calls={alloc_calls}, realloc_calls={realloc_calls}, \
         alloc_bytes={alloc_bytes}, realloc_bytes={realloc_bytes}"
    );
}