rodio_tap 0.2.0

Tap rodio audio sources and analyze them in real-time.
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
use crate::{FrameFormat, FrameReaderConfig, TapPacket, TapReader};
use arrayvec::ArrayVec;
use rtrb::Consumer;
use std::sync::Arc;
use std::time::Duration;

/// High-level reader for tapped frame batches.
///
/// `FrameReader` wraps the low-level `TapReader` consumer loop and provides:
///
/// - automatic attach/switch when the active tap changes
/// - packet-based ring-buffer reads (`TapPacket::Format` / `TapPacket::Frame`)
/// - configurable batch sizing (time-based or frame-count-based)
/// - cooperative pacing to avoid busy-spin when data is incomplete
///
/// The callback receives frame batches where each frame is an interleaved
/// `ArrayVec<f32, C>`:
///
/// - usually `batch.len() == frames_per_batch`
/// - each frame length is `channels` for that callback
/// - on in-band format change, a partial batch may be emitted before switching format
///
/// This is the recommended API for most users. If you need direct ring-buffer control,
/// use the lower-level `TapReader`/`TapAdapter` pair.
///
/// This reader is synchronous and blocks the current thread. Run it in a dedicated thread.
/// For Tokio/async usage, enable the `async` feature and use `AsyncFrameReader`.
pub struct FrameReader<const C: usize = 2> {
    tap_fn: Box<dyn Fn() -> Option<Arc<TapReader<C>>> + Send + Sync>,
    config: FrameReaderConfig,

    // live state
    active_consumer: Option<Consumer<TapPacket<C>>>,
    active_tap: Option<Arc<TapReader<C>>>,
    ch: usize,
    sr: u32,
    has_format: bool,

    // batch buffer (interleaved frames)
    batch_buf: Vec<ArrayVec<f32, C>>,
    batch_len_frames: usize,
}

impl<const C: usize> FrameReader<C> {
    /// Create a reader with [`FrameReaderConfig::default`].
    ///
    /// The returned reader will call `tap_fn` to discover the currently active tap.
    /// The closure should return the same `Arc<TapReader>` while a track is active, and
    /// a different one when playback switches.
    pub fn new<G>(tap_fn: G) -> Self
    where
        G: Fn() -> Option<Arc<TapReader<C>>> + Send + Sync + 'static,
    {
        let config = FrameReaderConfig::default();
        Self::new_with_config(config, tap_fn)
    }

    /// Create a reader with an explicit [`FrameReaderConfig`].
    ///
    /// At least one of `frames_per_batch` or `time_per_batch` must be set in the config.
    /// If both are set, `frames_per_batch` takes precedence.
    pub fn new_with_config<G>(config: FrameReaderConfig, tap_fn: G) -> Self
    where
        G: Fn() -> Option<Arc<TapReader<C>>> + Send + Sync + 'static,
    {
        assert!(C > 0, "FrameReader requires C > 0");
        assert!(
            config.frames_per_batch.is_some() || config.time_per_batch.is_some(),
            "FrameReaderConfig requires at least one batch target: frames_per_batch or time_per_batch"
        );

        Self {
            tap_fn: Box::new(tap_fn),
            config,
            active_consumer: None,
            active_tap: None,
            ch: 0,
            sr: 0,
            has_format: false,
            batch_buf: Vec::new(),
            batch_len_frames: 0,
        }
    }

    #[inline]
    fn recompute_batch_size(&mut self) {
        self.batch_len_frames = if let Some(frames) = self.config.frames_per_batch {
            frames as usize
        } else if self.sr == 0 {
            // We may attach before seeing the first Format packet.
            1
        } else {
            let batch_duration = self.config.time_per_batch.expect(
                "FrameReaderConfig must set time_per_batch when frames_per_batch is not set",
            );
            // frames = round(sr * duration_secs)
            ((self.sr as u128 * batch_duration.as_nanos() + 500_000_000) / 1_000_000_000) as usize
        }
        .max(1);
        self.batch_buf.clear();
        self.batch_buf.reserve(self.batch_len_frames);
    }

    /// Attach to current tap or switch if the current tap changed.
    fn try_attach_or_switch(&mut self, tap: Arc<TapReader<C>>) -> bool {
        let tap_changed = match &self.active_tap {
            Some(active) => !Arc::ptr_eq(active, &tap),
            None => true,
        };

        if self.active_consumer.is_none() || tap_changed {
            if let Ok(mut slot) = tap.consumer.lock()
                && let Some(cons) = slot.take()
            {
                self.active_consumer = Some(cons);
                self.active_tap = Some(Arc::clone(&tap));
                self.ch = 0;
                self.sr = 0;
                self.has_format = false;
                self.recompute_batch_size();

                #[cfg(feature = "log")]
                log::debug!("FrameReader attached tap (awaiting first Format packet)");
                return true;
            }
            self.active_consumer = None;
            self.active_tap = None;
        }
        false
    }

    #[inline]
    fn emit_batch<F>(&mut self, on_batch: &mut F)
    where
        F: FnMut(&[ArrayVec<f32, C>], usize /*channels*/, u32 /*sr*/),
    {
        if self.batch_buf.is_empty() {
            return;
        }
        on_batch(&self.batch_buf, self.ch, self.sr);
        self.batch_buf.clear();
    }

    #[inline]
    fn handle_format<F>(&mut self, fmt: FrameFormat, on_batch: &mut F)
    where
        F: FnMut(&[ArrayVec<f32, C>], usize /*channels*/, u32 /*sr*/),
    {
        let new_ch = fmt.channels as usize;
        if new_ch == 0 || new_ch > C {
            #[cfg(feature = "log")]
            log::debug!(
                "FrameReader ignoring invalid format packet ({} ch @ {} Hz)",
                fmt.channels,
                fmt.sample_rate_hz
            );
            return;
        }

        let format_changed = !self.has_format || self.ch != new_ch || self.sr != fmt.sample_rate_hz;
        if !format_changed {
            return;
        }

        if self.has_format && !self.batch_buf.is_empty() {
            // On in-band format changes, emit the partial batch in the old format.
            self.emit_batch(on_batch);
        }

        self.ch = new_ch;
        self.sr = fmt.sample_rate_hz;
        self.has_format = true;
        self.recompute_batch_size();
    }

    #[inline]
    fn handle_frame<F>(&mut self, frame: &ArrayVec<f32, C>, on_batch: &mut F)
    where
        F: FnMut(&[ArrayVec<f32, C>], usize /*channels*/, u32 /*sr*/),
    {
        // Late attach can observe frames before the first format packet.
        if !self.has_format {
            #[cfg(feature = "log")]
            log::debug!(
                "FrameReader dropping frame because it has not received a format packet yet"
            );
            return;
        }

        if frame.len() != self.ch {
            #[cfg(feature = "log")]
            log::debug!(
                "FrameReader dropping frame with len {} (expected {})",
                frame.len(),
                self.ch
            );
            return;
        }

        self.batch_buf.push(frame.clone());
        if self.batch_buf.len() == self.batch_len_frames {
            self.emit_batch(on_batch);
        }
    }

    /// Predict a conservative sleep to let missing frames arrive.
    #[inline]
    fn sleep_for_missing(&self) -> Option<Duration> {
        if self.active_consumer.is_none() || self.sr == 0 || self.ch == 0 {
            return None;
        }
        if self.batch_buf.len() >= self.batch_len_frames {
            return None;
        }
        let missing_frames = self.batch_len_frames - self.batch_buf.len();
        if missing_frames == 0 {
            return None;
        }
        // time = frames / sr seconds
        let nanos_f = (missing_frames as f64 / self.sr as f64)
            * 1_000_000_000.0
            * (self.config.sleep_bias as f64);
        let mut d = Duration::from_nanos(nanos_f as u64);
        if d < self.config.min_sleep {
            d = self.config.min_sleep;
        }
        if d > self.config.max_sleep {
            d = self.config.max_sleep;
        }
        Some(d)
    }

    /// Run forever and deliver frame batches to `on_batch`.
    ///
    /// The callback is usually invoked for complete batches. If an in-band format change
    /// arrives while a batch is in progress, the partial batch is emitted before the
    /// new format starts.
    ///
    /// Each item in `batch` is one interleaved frame (`ArrayVec<f32, C>`) with length `ch`.
    ///
    /// If `tap_fn` reports a different tap (for example, track switch), any partial batch
    /// in progress is dropped.
    ///
    /// ```no_run
    /// use std::sync::Arc;
    /// use arrayvec::ArrayVec;
    /// use rodio_tap::{FrameReader, TapReader};
    ///
    /// fn run<S>(source: S)
    /// where
    ///     S: rodio::Source + Send + 'static,
    ///     S::Item: cpal::Sample + Send + 'static,
    ///     f32: cpal::FromSample<S::Item>,
    /// {
    /// // Build one persistent tap for the source graph.
    /// let (tap_reader, _tap_adapter) = TapReader::<2>::new(source);
    ///
    /// // Build reader that always returns that same tap.
    /// let mut reader = FrameReader::<2>::new({
    ///     let tap_reader = Arc::clone(&tap_reader);
    ///     move || Some(Arc::clone(&tap_reader))
    /// });
    ///
    /// // Drive the reader and process frame batches.
    /// reader.run(|batch, ch, sr| {
    ///     let frames = batch.len();
    ///     let mut sums = vec![0.0f32; ch];
    ///
    ///     for frame in batch {
    ///         for (i, &s) in frame.iter().enumerate() {
    ///             sums[i] += s;
    ///         }
    ///     }
    ///
    ///     let avgs: Vec<f32> = sums.into_iter().map(|s| s / frames as f32).collect();
    ///     println!(
    ///         "[{} Hz] {} frames (~{} ms), avg/ch = {:?}",
    ///         sr,
    ///         frames,
    ///         1000 * frames as u64 / sr as u64,
    ///         avgs
    ///     );
    /// });
    /// }
    /// ```
    pub fn run<F>(&mut self, mut on_batch: F) -> !
    where
        F: FnMut(&[ArrayVec<f32, C>], usize /*channels*/, u32 /*sr*/) + Send + 'static,
    {
        loop {
            if self.active_consumer.is_none() {
                let Some(tap) = (self.tap_fn)() else {
                    std::thread::sleep(self.config.no_tap_sleep);
                    continue;
                };
                if !self.try_attach_or_switch(tap) {
                    std::thread::sleep(self.config.no_tap_sleep);
                    continue;
                }
            }

            let mut made_progress = false;

            // Move the consumer out so we can borrow `self` mutably inside the loop.
            if self.active_consumer.is_some() {
                let mut cons = self.active_consumer.take().unwrap();

                loop {
                    let avail = cons.slots();
                    if avail == 0 {
                        break;
                    }

                    // Prefer reading roughly one callback worth of packets at a time.
                    // Add one packet of slack to accommodate an in-band `Format` packet.
                    let missing_frames = self.batch_len_frames.saturating_sub(self.batch_buf.len());
                    let target_packets = missing_frames.max(1).saturating_add(1);
                    let want = avail.min(target_packets);
                    let Ok(chunk) = cons.read_chunk(want) else {
                        break;
                    };

                    let (a, b) = chunk.as_slices();
                    let total = a.len() + b.len();
                    if total == 0 {
                        break;
                    }

                    for packet in a.iter().chain(b.iter()) {
                        match packet {
                            TapPacket::Format(fmt) => self.handle_format(*fmt, &mut on_batch),
                            TapPacket::Frame(frame) => self.handle_frame(frame, &mut on_batch),
                        }
                        made_progress = true;
                    }

                    // Commit all packets we processed from this chunk.
                    chunk.commit(total);
                }

                // Put the consumer back.
                self.active_consumer = Some(cons);
            }

            // Track boundary: reattach & recompute batch size; drop partial batch.
            if let Some(tap) = (self.tap_fn)() {
                let tap_changed = match &self.active_tap {
                    Some(active) => !Arc::ptr_eq(active, &tap),
                    None => true,
                };
                if tap_changed {
                    #[cfg(feature = "log")]
                    log::trace!("FrameReader switching tap / tracks");

                    self.batch_buf.clear(); // drop partial across taps
                    self.has_format = false;
                    let _ = self.try_attach_or_switch(tap);
                    continue;
                }
            }

            // Pacing: if a batch is in-progress and we didn't fill it this turn, sleep a bit.
            if !self.batch_buf.is_empty()
                && !made_progress
                && let Some(d) = self.sleep_for_missing()
            {
                std::thread::sleep(d);
                continue;
            }

            // Otherwise, be cooperative but eager.
            std::thread::yield_now();
        }
    }
}

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

    fn frame2(a: f32, b: f32) -> ArrayVec<f32, 2> {
        let mut f = ArrayVec::<f32, 2>::new();
        f.push(a);
        f.push(b);
        f
    }

    fn test_reader() -> FrameReader<2> {
        let cfg = FrameReaderConfig {
            frames_per_batch: Some(2),
            time_per_batch: None,
            no_tap_sleep: Duration::from_millis(1),
            sleep_bias: 0.75,
            min_sleep: Duration::from_micros(100),
            max_sleep: Duration::from_millis(5),
        };
        FrameReader::new_with_config(cfg, || None)
    }

    #[test]
    fn ignores_frame_before_first_format() {
        let mut reader = test_reader();
        let mut called = 0usize;
        reader.handle_frame(&frame2(1.0, 2.0), &mut |_, _, _| called += 1);
        assert_eq!(called, 0);
        assert!(reader.batch_buf.is_empty());
    }

    #[test]
    fn emits_full_batch_after_format() {
        let mut reader = test_reader();
        let mut observed = Vec::new();

        reader.handle_format(
            FrameFormat {
                channels: 2,
                sample_rate_hz: 48_000,
            },
            &mut |batch, ch, sr| observed.push((batch.to_vec(), ch, sr)),
        );
        reader.handle_frame(&frame2(1.0, 2.0), &mut |batch, ch, sr| {
            observed.push((batch.to_vec(), ch, sr))
        });
        reader.handle_frame(&frame2(3.0, 4.0), &mut |batch, ch, sr| {
            observed.push((batch.to_vec(), ch, sr))
        });

        assert_eq!(observed.len(), 1);
        assert_eq!(observed[0].1, 2);
        assert_eq!(observed[0].2, 48_000);
        assert_eq!(observed[0].0.len(), 2);
    }

    #[test]
    fn emits_partial_batch_on_format_change() {
        let mut reader = test_reader();
        let mut observed = Vec::new();

        reader.handle_format(
            FrameFormat {
                channels: 2,
                sample_rate_hz: 48_000,
            },
            &mut |batch, ch, sr| observed.push((batch.to_vec(), ch, sr)),
        );
        reader.handle_frame(&frame2(1.0, 2.0), &mut |batch, ch, sr| {
            observed.push((batch.to_vec(), ch, sr))
        });

        reader.handle_format(
            FrameFormat {
                channels: 1,
                sample_rate_hz: 44_100,
            },
            &mut |batch, ch, sr| observed.push((batch.to_vec(), ch, sr)),
        );

        assert_eq!(observed.len(), 1);
        assert_eq!(observed[0].1, 2);
        assert_eq!(observed[0].2, 48_000);
        assert_eq!(observed[0].0.len(), 1);
        assert_eq!(reader.ch, 1);
        assert_eq!(reader.sr, 44_100);
    }

    #[test]
    fn drops_frame_on_channel_mismatch() {
        let mut reader = test_reader();
        reader.handle_format(
            FrameFormat {
                channels: 2,
                sample_rate_hz: 48_000,
            },
            &mut |_, _, _| {},
        );

        let mut bad = ArrayVec::<f32, 2>::new();
        bad.push(1.0);
        reader.handle_frame(&bad, &mut |_, _, _| {});
        assert!(reader.batch_buf.is_empty());
    }
}