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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use arc_swap::ArcSwapOption;
use arrayvec::ArrayVec;
use cpal::Sample;
use rodio::SampleRate;
use rodio::source::SeekError;
use rtrb::{Consumer, Producer, RingBuffer};
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;

/// Runtime audio format metadata for tapped packets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameFormat {
    pub channels: u8,
    pub sample_rate_hz: u32,
}

/// Packet emitted by the low-level tap ring. Generic C over the maximum number of channels supported.
///
/// Invariants:
/// - The first packet for a stream is always `Format`.
/// - Whenever format changes, a new `Format` packet is emitted before the first `Frame`.
/// - `Frame.len()` is always `<= C`, and equals the active format channels for this tap.
#[derive(Debug, Clone, PartialEq)]
pub enum TapPacket<const C: usize = 2> {
    /// Format packet announces the current `{channels, sample_rate_hz}`.
    Format(FrameFormat),
    /// Frame packet carries one interleaved frame (up to `C` channels).
    /// For example, the frame is interleaved `[L, R, L, R, ...]` for stereo.
    Frame(ArrayVec<f32, C>),
}

impl<const C: usize> Default for TapPacket<C> {
    fn default() -> Self {
        if C == 0 {
            panic!("TapPacket requires generic C > 0");
        }
        let channels = C.max(1) as u8;
        TapPacket::Format(FrameFormat {
            channels,
            sample_rate_hz: 48_000,
        })
    }
}

/// Adapts a `rodio::Source` and taps packets into a lock-free ring buffer.
///
/// This is the write side of the low-level tap API. It forwards all samples to the
/// playback pipeline while mirroring packets into an `rtrb` ring:
/// - `TapPacket::Format` announces the active format.
/// - `TapPacket::Frame` carries one interleaved frame (up to `C` channels).
///
/// TapAdapter is generic over C: the maximum number of channels supported.
/// If the source reports more channels than `C`, extra channels are dropped.
pub struct TapAdapter<S: rodio::Source, const C: usize = 2> {
    inner: S,
    prod: Producer<TapPacket<C>>,
    /// Publish-on-first-sample hook (sets Player.current_tap once audio starts)
    on_start: Option<OnFirstSample<C>>,
    /// Build one output frame before packet emission.
    frame_buf: ArrayVec<f32, C>,
    /// Source frame position in current source channel layout.
    src_frame_pos: usize,
    /// Active source channel count.
    active_src_channels: usize,
    /// Active output channel count (`min(source_channels, C)`).
    active_out_channels: usize,
    /// Active source sample-rate.
    active_sample_rate_hz: u32,
    /// Remaining samples in current span.
    span_remaining: Option<usize>,
    /// True until a `Format` packet is emitted for current params.
    format_dirty: bool,
}

impl<S: rodio::Source, const C: usize> TapAdapter<S, C> {
    pub fn new(inner: S, prod: Producer<TapPacket<C>>, on_start: Option<OnFirstSample<C>>) -> Self {
        assert!(C > 0, "TapAdapter requires generic C > 0");

        let src_channels = inner.channels().get() as usize;
        let out_channels = src_channels.min(C).max(1);
        let sample_rate_hz: u32 = inner.sample_rate().into();
        let span_remaining = inner.current_span_len();

        Self {
            inner,
            prod,
            on_start,
            frame_buf: ArrayVec::new(),
            src_frame_pos: 0,
            active_src_channels: src_channels,
            active_out_channels: out_channels,
            active_sample_rate_hz: sample_rate_hz,
            span_remaining,
            format_dirty: true,
        }
    }

    #[inline]
    fn current_output_format(&self) -> FrameFormat {
        FrameFormat {
            channels: self.active_out_channels as u8,
            sample_rate_hz: self.active_sample_rate_hz,
        }
    }

    #[inline]
    fn push_packet(&mut self, packet: TapPacket<C>) -> bool {
        if self.prod.push(packet).is_err() {
            #[cfg(all(debug_assertions, feature = "log"))]
            log::debug!("TapAdapter: No space in ring buffer, dropped packet.");
            false
        } else {
            true
        }
    }

    #[inline]
    fn emit_format_if_needed(&mut self) -> bool {
        if !self.format_dirty {
            return true;
        }
        if self.push_packet(TapPacket::Format(self.current_output_format())) {
            self.format_dirty = false;
            true
        } else {
            false
        }
    }

    #[inline]
    fn emit_frame_packet(&mut self) {
        if self.active_out_channels == 0 {
            self.frame_buf.clear();
            return;
        }

        if self.frame_buf.len() != self.active_out_channels {
            self.frame_buf.clear();
            return;
        }

        // Never emit a frame before its matching format packet is accepted.
        let format_ok = self.emit_format_if_needed();
        if !format_ok {
            // Drop this frame and retry format on the next frame boundary.
            self.frame_buf.clear();
            return;
        }

        let frame = std::mem::take(&mut self.frame_buf);
        debug_assert_eq!(frame.len(), self.active_out_channels);
        let _ = self.push_packet(TapPacket::Frame(frame));
    }

    #[inline]
    fn note_span_sample(&mut self) {
        if let Some(rem) = self.span_remaining.as_mut() {
            if *rem > 0 {
                *rem -= 1;
            }
            if *rem == 0 {
                self.handle_span_boundary();
            }
        }
    }

    #[inline]
    fn handle_span_boundary(&mut self) {
        // Discard partial frame to avoid any cross-boundary mixing.
        self.frame_buf.clear();
        self.src_frame_pos = 0;

        let new_src_channels = self.inner.channels().get() as usize;
        let new_out_channels = new_src_channels.min(C).max(1);
        let new_sample_rate_hz: u32 = self.inner.sample_rate().into();

        if new_out_channels != self.active_out_channels
            || new_sample_rate_hz != self.active_sample_rate_hz
        {
            self.format_dirty = true;
        }

        self.active_src_channels = new_src_channels;
        self.active_out_channels = new_out_channels;
        self.active_sample_rate_hz = new_sample_rate_hz;
        self.span_remaining = self.inner.current_span_len();
    }
}

impl<S: rodio::Source, const C: usize> Iterator for TapAdapter<S, C>
where
    S::Item: cpal::Sample + Send + 'static,
    f32: cpal::FromSample<S::Item>,
{
    type Item = S::Item;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        // `Some(0)` means current span is exhausted, so refresh format/span before
        // associating the next emitted sample with metadata.
        if self.span_remaining == Some(0) {
            self.handle_span_boundary();
        }

        let s = self.inner.next()?;

        if let Some(cb) = &self.on_start {
            cb.maybe_publish();
        }

        let f: f32 = f32::from_sample(s);
        if self.src_frame_pos < self.active_out_channels {
            let _ = self.frame_buf.try_push(f);
        }
        self.src_frame_pos += 1;

        if self.src_frame_pos == self.active_src_channels {
            self.src_frame_pos = 0;
            self.emit_frame_packet();
        }

        self.note_span_sample();
        Some(s)
    }
}

impl<S: rodio::Source, const C: usize> rodio::Source for TapAdapter<S, C>
where
    S::Item: cpal::Sample + Send + 'static,
    f32: cpal::FromSample<S::Item>,
{
    #[inline]
    fn current_span_len(&self) -> Option<usize> {
        self.inner.current_span_len()
    }

    #[inline]
    fn channels(&self) -> std::num::NonZero<u16> {
        self.inner.channels()
    }

    #[inline]
    fn sample_rate(&self) -> SampleRate {
        self.inner.sample_rate()
    }

    #[inline]
    fn total_duration(&self) -> Option<Duration> {
        self.inner.total_duration()
    }

    #[inline]
    fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
        self.inner.try_seek(pos)?;
        self.frame_buf.clear();
        self.src_frame_pos = 0;
        self.active_src_channels = self.inner.channels().get() as usize;
        self.active_out_channels = self.active_src_channels.min(C).max(1);
        self.active_sample_rate_hz = self.inner.sample_rate().into();
        self.span_remaining = self.inner.current_span_len();
        self.format_dirty = true;
        Ok(())
    }
}

impl<S: rodio::Source, const C: usize> Drop for TapAdapter<S, C> {
    fn drop(&mut self) {
        self.frame_buf.clear();
    }
}

/// Read side for packets produced by [`TapAdapter`].
///
/// # Low-level API warning
///
/// `TapReader`/`TapAdapter` are intentionally low-level building blocks:
///
/// - You are responsible for manually taking and polling the ring-buffer consumer.
/// - You must process `TapPacket::Format` boundaries before consuming `TapPacket::Frame`.
/// - You should decide your own pacing strategy to avoid busy waiting.
///
/// If you want a higher-level API with batching and built-in pacing, use
/// [`crate::FrameReader`] instead. For Tokio/async runtimes, enable the `async`
/// feature and use [`crate::AsyncFrameReader`].
///
/// # Example
///
/// ```no_run
/// use rodio_tap::{TapPacket, TapReader};
///
/// fn run<S>(decoder: S)
/// where
///     S: rodio::Source + Send + 'static,
///     S::Item: cpal::Sample + Send + 'static,
///     f32: cpal::FromSample<S::Item>,
/// {
/// let (tap_reader, adapter) = TapReader::<2>::new(decoder);
/// let _ = adapter;
///
/// let mut consumer = tap_reader
///     .consumer
///     .lock()
///     .expect("tap consumer lock poisoned")
///     .take()
///     .expect("tap consumer already taken");
///
/// let mut current_format = None;
///
/// loop {
///     let avail = consumer.slots();
///     if avail == 0 {
///         std::thread::sleep(std::time::Duration::from_millis(1));
///         continue;
///     }
///
///     let chunk = consumer.read_chunk(avail).expect("read_chunk failed");
///     let (a_len, b_len) = {
///         let (a, b) = chunk.as_slices();
///
///         for packet in a.iter().chain(b.iter()) {
///             match packet {
///                 TapPacket::<2>::Format(fmt) => current_format = Some(*fmt),
///                 TapPacket::<2>::Frame(frame) => {
///                     let _ = (frame, current_format);
///                     // Process one interleaved frame for the active format.
///                 }
///             }
///         }
///         (a.len(), b.len())
///     };
///
///     chunk.commit(a_len + b_len);
/// }
/// # }
/// ```
pub struct TapReader<const C: usize = 2> {
    /// Single-use ring-buffer consumer.
    ///
    /// This is wrapped in `Mutex<Option<_>>` so one thread/task can take ownership once
    /// and then poll it directly.
    pub consumer: Mutex<Option<Consumer<TapPacket<C>>>>,
}

impl<const C: usize> TapReader<C> {
    /// Build a `TapReader` + `TapAdapter` pair.
    pub fn new<S>(decoder: S) -> (Arc<TapReader<C>>, TapAdapter<S, C>)
    where
        S: rodio::Source + Send + 'static,
        S::Item: cpal::Sample + Send + 'static,
        f32: cpal::FromSample<S::Item>,
    {
        Self::new_with_publish_target_inner(None, decoder)
    }

    /// Build a TapReader + TapAdapter pair and wire publish-on-first-sample.
    pub fn new_with_publish_target<S>(
        publish_target: &Arc<ArcSwapOption<TapReader<C>>>,
        decoder: S,
    ) -> (Arc<TapReader<C>>, TapAdapter<S, C>)
    where
        S: rodio::Source + Send + 'static,
        S::Item: cpal::Sample + Send + 'static,
        f32: cpal::FromSample<S::Item>,
    {
        Self::new_with_publish_target_inner(Some(publish_target), decoder)
    }

    fn new_with_publish_target_inner<S>(
        publish_target: Option<&Arc<ArcSwapOption<TapReader<C>>>>,
        decoder: S,
    ) -> (Arc<TapReader<C>>, TapAdapter<S, C>)
    where
        S: rodio::Source + Send + 'static,
        S::Item: cpal::Sample + Send + 'static,
        f32: cpal::FromSample<S::Item>,
    {
        assert!(C > 0, "TapReader requires C > 0");

        let cap = 65_536;
        let (prod, cons) = RingBuffer::<TapPacket<C>>::new(cap);

        let reader = Arc::new(TapReader {
            consumer: Mutex::new(Some(cons)),
        });

        let on_start = publish_target.map(|target| OnFirstSample {
            fired: AtomicBool::new(false),
            target: Arc::clone(target),
            tap: Arc::clone(&reader),
        });

        let adapter = TapAdapter::new(decoder, prod, on_start);
        (reader, adapter)
    }
}

pub struct OnFirstSample<const C: usize = 2> {
    fired: AtomicBool,
    target: Arc<ArcSwapOption<TapReader<C>>>,
    tap: Arc<TapReader<C>>,
}

impl<const C: usize> OnFirstSample<C> {
    #[inline]
    fn maybe_publish(&self) {
        if self
            .fired
            .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
            .is_ok()
        {
            self.target.store(Some(self.tap.clone()));

            #[cfg(feature = "log")]
            log::trace!("OnFirstSample published tap");
        }
    }

    pub fn is_fired(&self, order: Ordering) -> bool {
        self.fired.load(order)
    }

    pub fn get_tap(&self) -> Arc<TapReader<C>> {
        self.tap.clone()
    }

    pub fn get_target(&self) -> Arc<ArcSwapOption<TapReader<C>>> {
        self.target.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rodio::Source;
    use std::collections::VecDeque;
    use std::num::NonZero;

    #[derive(Clone)]
    struct SpanChunk {
        channels: u16,
        sample_rate_hz: u32,
        samples: Vec<f32>,
    }

    struct MockSpanSource {
        chunks: VecDeque<SpanChunk>,
        current: Option<SpanChunk>,
        idx: usize,
    }

    impl MockSpanSource {
        fn new(chunks: Vec<SpanChunk>) -> Self {
            let mut q = VecDeque::from(chunks);
            let current = q.pop_front();
            Self {
                chunks: q,
                current,
                idx: 0,
            }
        }
    }

    impl Iterator for MockSpanSource {
        type Item = f32;

        fn next(&mut self) -> Option<Self::Item> {
            loop {
                let cur = self.current.as_ref()?;
                if self.idx < cur.samples.len() {
                    let s = cur.samples[self.idx];
                    self.idx += 1;
                    if self.idx == cur.samples.len() {
                        self.current = self.chunks.pop_front();
                        self.idx = 0;
                    }
                    return Some(s);
                }
                self.current = self.chunks.pop_front();
                self.idx = 0;
            }
        }
    }

    impl rodio::Source for MockSpanSource {
        fn current_span_len(&self) -> Option<usize> {
            self.current.as_ref().map(|c| c.samples.len())
        }

        fn channels(&self) -> NonZero<u16> {
            let c = self.current.as_ref().map(|c| c.channels).unwrap_or(2);
            NonZero::new(c).unwrap()
        }

        fn sample_rate(&self) -> SampleRate {
            NonZero::new(
                self.current
                    .as_ref()
                    .map(|c| c.sample_rate_hz)
                    .unwrap_or(48_000),
            )
            .unwrap()
        }

        fn total_duration(&self) -> Option<Duration> {
            None
        }
    }

    fn drain_packets<const C: usize>(consumer: &mut Consumer<TapPacket<C>>) -> Vec<TapPacket<C>> {
        let avail = consumer.slots();
        if avail == 0 {
            return Vec::new();
        }
        let chunk = consumer.read_chunk(avail).expect("read_chunk");
        let (a_len, b_len, out) = {
            let (a, b) = chunk.as_slices();
            let mut out = Vec::with_capacity(a.len() + b.len());
            out.extend(a.iter().cloned());
            out.extend(b.iter().cloned());
            (a.len(), b.len(), out)
        };
        chunk.commit(a_len + b_len);
        out
    }

    #[test]
    fn emits_initial_format_before_frames() {
        let src = MockSpanSource::new(vec![SpanChunk {
            channels: 2,
            sample_rate_hz: 48_000,
            samples: vec![0.1, 0.2, 0.3, 0.4],
        }]);

        let (reader, mut adapter) = TapReader::<2>::new(src);
        while adapter.next().is_some() {}
        drop(adapter);

        let mut cons = reader.consumer.lock().unwrap().take().unwrap();
        let packets = drain_packets(&mut cons);
        assert!(!packets.is_empty());
        assert!(matches!(
            packets[0],
            TapPacket::Format(FrameFormat {
                channels: 2,
                sample_rate_hz: 48_000
            })
        ));
    }

    #[test]
    fn emits_format_on_change() {
        let src = MockSpanSource::new(vec![
            SpanChunk {
                channels: 2,
                sample_rate_hz: 44_100,
                samples: vec![0.0, 0.1, 0.2, 0.3],
            },
            SpanChunk {
                channels: 1,
                sample_rate_hz: 48_000,
                samples: vec![0.4, 0.5],
            },
        ]);

        let (reader, mut adapter) = TapReader::<2>::new(src);
        while adapter.next().is_some() {}
        drop(adapter);

        let mut cons = reader.consumer.lock().unwrap().take().unwrap();
        let packets = drain_packets(&mut cons);
        let formats: Vec<_> = packets
            .iter()
            .filter_map(|p| match p {
                TapPacket::Format(f) => Some(*f),
                TapPacket::Frame(_) => None,
            })
            .collect();

        assert!(formats.len() >= 2);
        assert_eq!(
            formats[0],
            FrameFormat {
                channels: 2,
                sample_rate_hz: 44_100
            }
        );
        assert_eq!(
            formats[1],
            FrameFormat {
                channels: 1,
                sample_rate_hz: 48_000
            }
        );
    }

    #[test]
    fn frame_lengths_match_output_channels() {
        let src = MockSpanSource::new(vec![SpanChunk {
            channels: 4,
            sample_rate_hz: 48_000,
            samples: vec![1.0, 2.0, 3.0, 4.0],
        }]);

        let (reader, mut adapter) = TapReader::<2>::new(src);
        while adapter.next().is_some() {}
        drop(adapter);

        let mut cons = reader.consumer.lock().unwrap().take().unwrap();
        let packets = drain_packets(&mut cons);
        for packet in packets {
            if let TapPacket::Frame(frame) = packet {
                assert_eq!(frame.len(), 2);
            }
        }
    }

    #[test]
    fn seek_resets_format_and_partial_state() {
        let src = MockSpanSource::new(vec![SpanChunk {
            channels: 2,
            sample_rate_hz: 44_100,
            samples: vec![0.0, 0.1, 0.2, 0.3],
        }]);

        let (reader, mut adapter) = TapReader::<2>::new(src);
        let _ = adapter.next();
        let _ = adapter.try_seek(Duration::from_secs(0));
        while adapter.next().is_some() {}
        drop(adapter);

        let mut cons = reader.consumer.lock().unwrap().take().unwrap();
        let packets = drain_packets(&mut cons);
        assert!(packets.iter().any(|p| matches!(p, TapPacket::Format(_))));
    }
}