ff-preview 0.15.0

Real-time video/audio preview and proxy workflow
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
//! Multi-track audio mixer for real-time preview.
//!
//! [`AudioMixer`] combines `N` mono tracks into a single interleaved stereo
//! `f32` output at 48 kHz. Per-track volume and pan are controlled from any
//! thread via the cloneable [`AudioTrackHandle`].

use std::collections::VecDeque;
use std::f32::consts;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};

// ── AudioTrack (private) ──────────────────────────────────────────────────────

struct AudioTrack {
    buf: Arc<Mutex<VecDeque<f32>>>,
    volume: Arc<AtomicU32>,
    pan: Arc<AtomicU32>,
}

// ── AudioTrackHandle ──────────────────────────────────────────────────────────

/// Cloneable handle for filling a track and adjusting its gain from any thread.
///
/// Obtained by calling [`AudioMixer::add_track`]. All methods are lock-free
/// on the hot path (volume/pan reads) and only lock for buffer access.
#[derive(Clone)]
pub struct AudioTrackHandle {
    buf: Arc<Mutex<VecDeque<f32>>>,
    volume: Arc<AtomicU32>,
    pan: Arc<AtomicU32>,
}

impl AudioTrackHandle {
    /// Set per-track volume.
    ///
    /// `1.0` = unity gain. Values above `1.0` amplify; values below reduce.
    /// Negative values are clamped to `0.0` (silence). The mixer output is
    /// always clipped to `[-1.0, 1.0]`, so amplification may cause saturation
    /// on loud signals.
    pub fn set_volume(&self, v: f32) {
        self.volume.store(v.max(0.0).to_bits(), Ordering::Relaxed);
    }

    /// Set stereo pan. Clamped to `[-1.0` (full left) `.. +1.0` (full right)`]`.
    pub fn set_pan(&self, p: f32) {
        self.pan
            .store(p.clamp(-1.0, 1.0).to_bits(), Ordering::Relaxed);
    }

    /// Push decoded mono PCM samples into the track buffer.
    ///
    /// Called by the background audio-decode thread. The samples should be
    /// `f32` mono at 48 kHz (i.e., one value per time step).
    pub fn push_samples(&self, samples: &[f32]) {
        self.buf
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .extend(samples.iter().copied());
    }

    /// Number of samples currently buffered.
    ///
    /// Used by background audio threads to implement back-pressure.
    #[cfg(feature = "timeline")]
    pub(crate) fn buffered_samples(&self) -> usize {
        self.buf
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .len()
    }

    /// Drain all buffered samples.
    ///
    /// Called on seek to discard audio that is no longer relevant.
    #[cfg(feature = "timeline")]
    pub(crate) fn clear(&self) {
        self.buf
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clear();
    }
}

// ── AudioMixer ────────────────────────────────────────────────────────────────

/// Multi-track, constant-power-panned stereo mixer.
///
/// Combines `N` mono tracks into a single interleaved stereo `f32` output at
/// 48 kHz.  Per-track volume and pan adjustments take effect on the next call
/// to [`mix`](Self::mix).
///
/// # Pan law
///
/// For a pan position `p ∈ [-1.0, +1.0]`:
/// ```text
/// p_norm = (p + 1.0) * π / 4
/// l_gain = volume * cos(p_norm)
/// r_gain = volume * sin(p_norm)
/// ```
/// At `p = 0` (center): `l_gain == r_gain ≈ 0.707 × volume` (constant-power
/// law — equal loudness in both ears).
///
/// # Example
///
/// ```ignore
/// let mut mixer = AudioMixer::new(48_000);
/// let track = mixer.add_track();
///
/// // Background audio-decode thread:
/// track.push_samples(&mono_pcm_chunk);
///
/// // Audio-device output callback:
/// let stereo = mixer.mix(output_buf.len());
/// output_buf[..stereo.len()].copy_from_slice(&stereo);
/// ```
pub struct AudioMixer {
    tracks: Vec<AudioTrack>,
    /// Output sample rate in Hz.
    pub sample_rate: u32,
    /// Number of output channels — always 2 (stereo).
    pub channels: u16,
}

impl AudioMixer {
    /// Create a new mixer with no tracks.
    #[must_use]
    pub fn new(sample_rate: u32) -> Self {
        Self {
            tracks: Vec::new(),
            sample_rate,
            channels: 2,
        }
    }

    /// Add a new mono track and return a cloneable handle.
    ///
    /// The track starts with `volume = 1.0` and `pan = 0.0` (center).
    pub fn add_track(&mut self) -> AudioTrackHandle {
        let buf = Arc::new(Mutex::new(VecDeque::new()));
        let volume = Arc::new(AtomicU32::new(1.0_f32.to_bits()));
        let pan = Arc::new(AtomicU32::new(0.0_f32.to_bits()));
        let handle = AudioTrackHandle {
            buf: Arc::clone(&buf),
            volume: Arc::clone(&volume),
            pan: Arc::clone(&pan),
        };
        self.tracks.push(AudioTrack { buf, volume, pan });
        handle
    }

    /// Mix `n_samples` interleaved stereo `f32` values from all tracks.
    ///
    /// `n_samples` is the total number of `f32` elements to produce (L + R
    /// interleaved). Tracks with insufficient buffered data are zero-padded.
    /// The output is clipped to `[-1.0, 1.0]`.
    #[allow(clippy::cast_precision_loss)]
    pub fn mix(&mut self, n_samples: usize) -> Vec<f32> {
        let n_frames = n_samples / 2;
        let mut out = vec![0.0_f32; n_frames * 2];

        for track in &self.tracks {
            let volume = f32::from_bits(track.volume.load(Ordering::Relaxed));
            let pan = f32::from_bits(track.pan.load(Ordering::Relaxed));

            // Constant-power pan law.
            let p_norm = (pan + 1.0) * consts::FRAC_PI_4;
            let l_gain = volume * p_norm.cos();
            let r_gain = volume * p_norm.sin();

            let mut guard = track
                .buf
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            for i in 0..n_frames {
                let s = guard.pop_front().unwrap_or(0.0);
                out[i * 2] += s * l_gain;
                out[i * 2 + 1] += s * r_gain;
            }
        }

        // Clip to [-1.0, 1.0].
        for sample in &mut out {
            *sample = sample.clamp(-1.0, 1.0);
        }

        out
    }

    /// Drain all track buffers.
    ///
    /// Called on seek to discard stale audio across all tracks.
    #[cfg(feature = "timeline")]
    pub(crate) fn invalidate_all(&mut self) {
        for track in &self.tracks {
            track
                .buf
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .clear();
        }
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn audio_mixer_mix_two_tracks_should_sum_and_clip_left_channel() {
        // Two tracks, full-left pan (l_gain = volume = 1.0), amplitude 0.8.
        // Without clipping: L = 0.8 + 0.8 = 1.6. After clip: 1.0.
        let mut mixer = AudioMixer::new(48_000);
        let t1 = mixer.add_track();
        let t2 = mixer.add_track();
        t1.set_pan(-1.0);
        t2.set_pan(-1.0);
        t1.push_samples(&[0.8, 0.8]);
        t2.push_samples(&[0.8, 0.8]);

        let out = mixer.mix(4); // 2 stereo frames
        assert_eq!(out.len(), 4);
        assert!(
            (out[0] - 1.0).abs() < 1e-6,
            "L must clip to 1.0; got {}",
            out[0]
        );
        assert!(
            out[1].abs() < 1e-6,
            "R must be 0.0 for full-left pan; got {}",
            out[1]
        );
    }

    #[test]
    fn audio_mixer_pan_full_left_should_produce_zero_right_channel() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.set_pan(-1.0);
        track.push_samples(&[0.5, 0.5, 0.5, 0.5]);

        let out = mixer.mix(8); // 4 stereo frames
        assert_eq!(out.len(), 8);
        for i in (1..8usize).step_by(2) {
            assert!(
                out[i].abs() < 1e-6,
                "R channel must be 0.0 for full-left pan; got {} at index {i}",
                out[i]
            );
        }
    }

    #[test]
    fn audio_mixer_pan_full_right_should_produce_zero_left_channel() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.set_pan(1.0);
        track.push_samples(&[0.5, 0.5, 0.5, 0.5]);

        let out = mixer.mix(8);
        for i in (0..8usize).step_by(2) {
            assert!(
                out[i].abs() < 1e-6,
                "L channel must be 0.0 for full-right pan; got {} at index {i}",
                out[i]
            );
        }
    }

    #[test]
    fn audio_mixer_two_tracks_volume_sum_exceeding_one_should_be_clipped() {
        // Two tracks, volume 0.7, full-left pan, amplitude 0.8.
        // L = 0.8 * 0.7 + 0.8 * 0.7 = 1.12 > 1.0 → clipped to 1.0.
        let mut mixer = AudioMixer::new(48_000);
        let t1 = mixer.add_track();
        let t2 = mixer.add_track();
        t1.set_volume(0.7);
        t2.set_volume(0.7);
        t1.set_pan(-1.0);
        t2.set_pan(-1.0);
        t1.push_samples(&[0.8, 0.8]);
        t2.push_samples(&[0.8, 0.8]);

        let out = mixer.mix(4);
        for &s in &out {
            assert!(
                s >= -1.0 && s <= 1.0,
                "all output must be within [-1.0, 1.0]; got {s}"
            );
        }
    }

    #[test]
    fn audio_mixer_center_pan_should_apply_constant_power_law() {
        // At pan = 0.0: p_norm = π/4, cos = sin = 1/√2 ≈ 0.7071.
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        // pan = 0 (center) by default, volume = 1.0 by default.
        track.push_samples(&[1.0]);

        let out = mixer.mix(2); // 1 stereo frame
        let expected = (std::f32::consts::FRAC_PI_4).cos(); // ≈ 0.7071
        assert!(
            (out[0] - expected).abs() < 1e-5,
            "L at center should be cos(π/4) ≈ {expected:.5}; got {}",
            out[0]
        );
        assert!(
            (out[1] - expected).abs() < 1e-5,
            "R at center should be sin(π/4) ≈ {expected:.5}; got {}",
            out[1]
        );
    }

    #[test]
    fn audio_mixer_underrun_should_zero_pad_remaining_frames() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.set_pan(-1.0); // full left for determinism
        track.push_samples(&[0.5]); // only one sample, but we request 4 frames

        let out = mixer.mix(8);
        assert_eq!(out.len(), 8);

        // Frames 1-3 must be zero (underrun).
        for i in 2..8 {
            assert_eq!(out[i], 0.0, "underrun frame must be silent; got {}", out[i]);
        }
    }

    #[test]
    fn audio_mixer_empty_tracks_should_produce_silence() {
        let mut mixer = AudioMixer::new(48_000);
        let _track = mixer.add_track();
        let out = mixer.mix(8);
        assert_eq!(out.len(), 8);
        assert!(
            out.iter().all(|&s| s == 0.0),
            "empty track must produce silence"
        );
    }

    #[cfg(feature = "timeline")]
    #[test]
    fn audio_mixer_invalidate_all_should_clear_all_buffers() {
        let mut mixer = AudioMixer::new(48_000);
        let t1 = mixer.add_track();
        let t2 = mixer.add_track();
        t1.push_samples(&[0.5, 0.5]);
        t2.push_samples(&[0.5, 0.5]);

        mixer.invalidate_all();

        let out = mixer.mix(4);
        assert!(
            out.iter().all(|&s| s == 0.0),
            "after invalidate_all, mix must be silent"
        );
    }

    #[test]
    fn audio_track_handle_set_volume_above_one_should_amplify() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.set_volume(2.0);
        track.set_pan(-1.0); // full left for determinism
        track.push_samples(&[0.4]);
        let out = mixer.mix(2); // 1 stereo frame
        // L = 0.4 * 2.0 = 0.8 (below clip threshold).
        assert!(
            (out[0] - 0.8).abs() < 1e-5,
            "volume 2.0 should amplify to 0.8; got {}",
            out[0]
        );
    }

    #[test]
    fn audio_track_handle_set_negative_volume_should_be_silent() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.set_volume(-1.0); // clamped to 0.0
        track.push_samples(&[1.0]);
        let out = mixer.mix(2);
        assert!(
            out.iter().all(|&s| s.abs() < 1e-6),
            "negative volume must be silent"
        );
    }

    #[cfg(feature = "timeline")]
    #[test]
    fn audio_track_handle_clear_should_drain_buffered_samples() {
        let mut mixer = AudioMixer::new(48_000);
        let track = mixer.add_track();
        track.push_samples(&[0.5, 0.5, 0.5, 0.5]);
        assert_eq!(track.buffered_samples(), 4);
        track.clear();
        assert_eq!(
            track.buffered_samples(),
            0,
            "clear() must drain all samples"
        );
    }
}