oximedia-audio 0.2.1

Audio codec implementations for OxiMedia
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
639
640
641
642
643
644
//! Lock-free ring-queue stream buffer for audio frame pipelines.
//!
//! This module provides two buffer implementations:
//!
//! - [`StreamBuffer`]: A simple FIFO queue suitable for single-threaded use.
//! - [`LockFreeRingBuffer`]: A single-producer single-consumer (SPSC) lock-free
//!   ring buffer designed for real-time audio threading.  Uses `AtomicUsize`
//!   sequence numbers to coordinate access without a mutex.
//!
//! # Lock-free design
//!
//! [`LockFreeRingBuffer`] can safely be shared between exactly **one writer
//! thread** (audio callback or capture thread) and **one reader thread**
//! (processing or playback thread) without any locking.  The implementation
//! follows the classic SPSC ring-buffer pattern:
//!
//! 1. `head` is updated only by the **producer** (writer).
//! 2. `tail` is updated only by the **consumer** (reader).
//! 3. Both indices are `AtomicUsize` accessed with `Acquire`/`Release`
//!    ordering to ensure the data written by the producer is visible to the
//!    consumer.
//!
//! The effective capacity is `capacity - 1` samples to distinguish between
//! full and empty states without an extra flag.
//!
//! # Example
//!
//! ```
//! use oximedia_audio::stream_buffer::LockFreeRingBuffer;
//! use std::sync::Arc;
//!
//! let buf = Arc::new(LockFreeRingBuffer::new(1024));
//!
//! // Producer side (audio callback thread)
//! let samples = vec![0.0_f32; 256];
//! buf.write_samples(&samples);
//!
//! // Consumer side (processing thread)
//! let mut out = vec![0.0_f32; 256];
//! let n = buf.read_samples(&mut out);
//! assert_eq!(n, 256);
//! ```
#![allow(dead_code)]

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};

/// Configuration for a [`StreamBuffer`].
#[derive(Debug, Clone, Copy)]
pub struct StreamBufferConfig {
    /// Maximum number of frames the buffer may hold.
    pub max_frames: usize,
    /// Sample rate in Hz (used for duration calculations).
    pub sample_rate: u32,
    /// Number of channels per frame.
    pub channels: u16,
}

impl StreamBufferConfig {
    /// Create a new configuration.
    #[must_use]
    pub fn new(max_frames: usize, sample_rate: u32, channels: u16) -> Self {
        Self {
            max_frames,
            sample_rate,
            channels,
        }
    }

    /// Maximum queue depth expressed in frames.
    #[must_use]
    pub fn max_frames(&self) -> usize {
        self.max_frames
    }
}

impl Default for StreamBufferConfig {
    fn default() -> Self {
        Self {
            max_frames: 64,
            sample_rate: 48_000,
            channels: 2,
        }
    }
}

/// A single audio frame inside the stream buffer.
#[derive(Debug, Clone)]
pub struct StreamFrame {
    /// Interleaved PCM samples (f32).
    pub samples: Vec<f32>,
    /// Presentation timestamp in samples since stream start.
    pub pts_samples: u64,
    /// Number of channels in this frame.
    pub channels: u16,
    /// Sample rate of the frame (Hz).
    pub sample_rate: u32,
}

impl StreamFrame {
    /// Create a new frame.
    #[must_use]
    pub fn new(samples: Vec<f32>, pts_samples: u64, channels: u16, sample_rate: u32) -> Self {
        Self {
            samples,
            pts_samples,
            channels,
            sample_rate,
        }
    }

    /// Number of multi-channel audio samples (frames) in this buffer.
    ///
    /// That is, the length of `samples` divided by the channel count.
    #[must_use]
    pub fn sample_count(&self) -> usize {
        if self.channels == 0 {
            return 0;
        }
        self.samples.len() / self.channels as usize
    }

    /// Duration of this frame in milliseconds.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn duration_ms(&self) -> f64 {
        if self.sample_rate == 0 {
            return 0.0;
        }
        self.sample_count() as f64 / self.sample_rate as f64 * 1_000.0
    }
}

/// FIFO queue of [`StreamFrame`]s with a configurable capacity.
#[derive(Debug)]
pub struct StreamBuffer {
    queue: VecDeque<StreamFrame>,
    config: StreamBufferConfig,
    /// Total number of frames ever pushed (monotonically increasing).
    total_pushed: u64,
    /// Total number of frames ever popped.
    total_popped: u64,
}

impl StreamBuffer {
    /// Create a new stream buffer with the given configuration.
    #[must_use]
    pub fn new(config: StreamBufferConfig) -> Self {
        Self {
            queue: VecDeque::with_capacity(config.max_frames),
            config,
            total_pushed: 0,
            total_popped: 0,
        }
    }

    /// Push a frame into the buffer.
    ///
    /// Returns `false` (and discards the frame) when the buffer is full.
    pub fn push_frame(&mut self, frame: StreamFrame) -> bool {
        if self.queue.len() >= self.config.max_frames {
            return false;
        }
        self.queue.push_back(frame);
        self.total_pushed += 1;
        true
    }

    /// Pop the oldest frame from the buffer, or `None` if empty.
    pub fn pop_frame(&mut self) -> Option<StreamFrame> {
        let frame = self.queue.pop_front();
        if frame.is_some() {
            self.total_popped += 1;
        }
        frame
    }

    /// Approximate total buffered audio in milliseconds.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn duration_ms(&self) -> f64 {
        self.queue.iter().map(|f| f.duration_ms()).sum()
    }

    /// Number of frames currently queued.
    #[must_use]
    pub fn len(&self) -> usize {
        self.queue.len()
    }

    /// Returns `true` when no frames are queued.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    /// Returns `true` when the queue has reached its configured maximum.
    #[must_use]
    pub fn is_full(&self) -> bool {
        self.queue.len() >= self.config.max_frames
    }

    /// Total frames pushed since creation.
    #[must_use]
    pub fn total_pushed(&self) -> u64 {
        self.total_pushed
    }

    /// Total frames popped since creation.
    #[must_use]
    pub fn total_popped(&self) -> u64 {
        self.total_popped
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Lock-free SPSC ring buffer
// ─────────────────────────────────────────────────────────────────────────────

/// Single-producer single-consumer lock-free ring buffer for `f32` samples.
///
/// Designed for real-time audio pipelines where one thread writes audio data
/// (the audio callback or capture thread) and another thread reads it (the
/// processing or playback thread).
///
/// Samples are stored as their `u32` bit patterns in `AtomicU32` cells so
/// that the entire structure is `Send + Sync` without any `unsafe` code.
/// The SPSC ring-buffer protocol using `AtomicUsize` head/tail indices
/// ensures correct ordering between producer and consumer.
///
/// ## Capacity
///
/// The actual number of samples that can be buffered is `capacity - 1`.
/// Choose a power-of-two capacity (e.g., 2048, 4096) for best performance.
///
/// ## Thread safety
///
/// Only one writer and one reader are supported.  Using more than one writer
/// or more than one reader concurrently results in incorrect data ordering.
pub struct LockFreeRingBuffer {
    /// Internal sample storage as atomic u32 (f32 bit patterns).
    data: Vec<AtomicU32>,
    /// Write index (producer-owned).
    head: AtomicUsize,
    /// Read index (consumer-owned).
    tail: AtomicUsize,
    /// Capacity (length of `data`).
    cap: usize,
}

// `Vec<AtomicU32>` is already `Send + Sync`, so `LockFreeRingBuffer` is too.
// No `unsafe impl` blocks are required.

impl LockFreeRingBuffer {
    /// Create a new ring buffer that can hold up to `capacity - 1` samples.
    ///
    /// `capacity` should be a power of two for best performance.  A minimum
    /// capacity of 2 is enforced.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        let cap = capacity.max(2);
        let data = (0..cap).map(|_| AtomicU32::new(0)).collect();
        Self {
            data,
            head: AtomicUsize::new(0),
            tail: AtomicUsize::new(0),
            cap,
        }
    }

    /// Returns the total capacity of the buffer (number of samples that can
    /// ever be stored).  The usable capacity is `capacity() - 1`.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.cap
    }

    /// Number of samples currently available for reading.
    #[must_use]
    pub fn available(&self) -> usize {
        let head = self.head.load(Ordering::Acquire);
        let tail = self.tail.load(Ordering::Acquire);
        if head >= tail {
            head - tail
        } else {
            self.cap - tail + head
        }
    }

    /// Number of free slots available for writing.
    #[must_use]
    pub fn free(&self) -> usize {
        self.cap - 1 - self.available()
    }

    /// Returns `true` when the buffer contains no readable samples.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.head.load(Ordering::Acquire) == self.tail.load(Ordering::Acquire)
    }

    /// Returns `true` when the buffer is full (cannot accept more writes).
    #[must_use]
    pub fn is_full(&self) -> bool {
        let head = self.head.load(Ordering::Acquire);
        let tail = self.tail.load(Ordering::Acquire);
        (head + 1) % self.cap == tail
    }

    /// Write a single sample.
    ///
    /// Returns `true` when successful, `false` when the buffer is full.
    ///
    /// **Must only be called from the producer thread.**
    pub fn write(&self, sample: f32) -> bool {
        let head = self.head.load(Ordering::Relaxed);
        let next_head = (head + 1) % self.cap;
        if next_head == self.tail.load(Ordering::Acquire) {
            return false; // full
        }
        // Store the f32 bit pattern atomically.  Only the producer writes to
        // data[head], and head < cap, so the index is always in bounds.
        self.data[head].store(sample.to_bits(), Ordering::Relaxed);
        self.head.store(next_head, Ordering::Release);
        true
    }

    /// Read a single sample.
    ///
    /// Returns `Some(sample)` when data is available, `None` when empty.
    ///
    /// **Must only be called from the consumer thread.**
    pub fn read(&self) -> Option<f32> {
        let tail = self.tail.load(Ordering::Relaxed);
        if tail == self.head.load(Ordering::Acquire) {
            return None; // empty
        }
        let bits = self.data[tail].load(Ordering::Relaxed);
        self.tail.store((tail + 1) % self.cap, Ordering::Release);
        Some(f32::from_bits(bits))
    }

    /// Write a block of samples.
    ///
    /// Returns the number of samples actually written (may be less than
    /// `samples.len()` when the buffer does not have enough free space).
    ///
    /// **Must only be called from the producer thread.**
    pub fn write_samples(&self, samples: &[f32]) -> usize {
        let mut written = 0;
        for &s in samples {
            if !self.write(s) {
                break;
            }
            written += 1;
        }
        written
    }

    /// Read samples into `dst`.
    ///
    /// Returns the number of samples actually read (may be less than
    /// `dst.len()` when the buffer does not have enough data).
    ///
    /// **Must only be called from the consumer thread.**
    pub fn read_samples(&self, dst: &mut [f32]) -> usize {
        let mut read = 0;
        for slot in dst.iter_mut() {
            match self.read() {
                Some(s) => {
                    *slot = s;
                    read += 1;
                }
                None => break,
            }
        }
        read
    }

    /// Clear all samples from the buffer.
    ///
    /// **Must only be called when no concurrent reads/writes are in progress.**
    pub fn clear(&self) {
        let head = self.head.load(Ordering::Relaxed);
        self.tail.store(head, Ordering::Release);
    }
}

impl std::fmt::Debug for LockFreeRingBuffer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LockFreeRingBuffer")
            .field("cap", &self.cap)
            .field("available", &self.available())
            .field("free", &self.free())
            .finish()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn make_frame(n_samples: usize, pts: u64) -> StreamFrame {
        StreamFrame::new(
            vec![0.0_f32; n_samples * 2], // stereo
            pts,
            2,
            48_000,
        )
    }

    #[test]
    fn test_config_max_frames() {
        let cfg = StreamBufferConfig::new(32, 48_000, 2);
        assert_eq!(cfg.max_frames(), 32);
    }

    #[test]
    fn test_config_default() {
        let cfg = StreamBufferConfig::default();
        assert_eq!(cfg.max_frames, 64);
        assert_eq!(cfg.sample_rate, 48_000);
    }

    #[test]
    fn test_frame_sample_count() {
        let frame = make_frame(480, 0);
        assert_eq!(frame.sample_count(), 480);
    }

    #[test]
    fn test_frame_duration_ms() {
        let frame = make_frame(480, 0); // 480/48000 = 10ms
        let dur = frame.duration_ms();
        assert!((dur - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_frame_duration_zero_rate() {
        let frame = StreamFrame::new(vec![0.0; 4], 0, 2, 0);
        assert_eq!(frame.duration_ms(), 0.0);
    }

    #[test]
    fn test_buffer_push_and_pop() {
        let cfg = StreamBufferConfig::default();
        let mut buf = StreamBuffer::new(cfg);
        let f = make_frame(480, 0);
        assert!(buf.push_frame(f));
        assert_eq!(buf.len(), 1);
        let popped = buf.pop_frame();
        assert!(popped.is_some());
        assert!(buf.is_empty());
    }

    #[test]
    fn test_buffer_fifo_order() {
        let cfg = StreamBufferConfig::default();
        let mut buf = StreamBuffer::new(cfg);
        buf.push_frame(make_frame(480, 0));
        buf.push_frame(make_frame(480, 480));
        let first = buf.pop_frame().expect("should succeed");
        assert_eq!(first.pts_samples, 0);
        let second = buf.pop_frame().expect("should succeed");
        assert_eq!(second.pts_samples, 480);
    }

    #[test]
    fn test_buffer_full_rejects_push() {
        let cfg = StreamBufferConfig::new(2, 48_000, 2);
        let mut buf = StreamBuffer::new(cfg);
        assert!(buf.push_frame(make_frame(480, 0)));
        assert!(buf.push_frame(make_frame(480, 480)));
        assert!(buf.is_full());
        assert!(!buf.push_frame(make_frame(480, 960)));
    }

    #[test]
    fn test_buffer_is_empty_initially() {
        let buf = StreamBuffer::new(StreamBufferConfig::default());
        assert!(buf.is_empty());
    }

    #[test]
    fn test_buffer_pop_empty_returns_none() {
        let mut buf = StreamBuffer::new(StreamBufferConfig::default());
        assert!(buf.pop_frame().is_none());
    }

    #[test]
    fn test_buffer_duration_ms() {
        let cfg = StreamBufferConfig::default();
        let mut buf = StreamBuffer::new(cfg);
        buf.push_frame(make_frame(480, 0)); // 10ms
        buf.push_frame(make_frame(480, 480)); // 10ms
        let dur = buf.duration_ms();
        assert!((dur - 20.0).abs() < 0.01);
    }

    #[test]
    fn test_buffer_total_counters() {
        let cfg = StreamBufferConfig::default();
        let mut buf = StreamBuffer::new(cfg);
        buf.push_frame(make_frame(480, 0));
        buf.push_frame(make_frame(480, 480));
        buf.pop_frame();
        assert_eq!(buf.total_pushed(), 2);
        assert_eq!(buf.total_popped(), 1);
    }

    #[test]
    fn test_frame_zero_channels() {
        let frame = StreamFrame::new(vec![0.0; 10], 0, 0, 48_000);
        assert_eq!(frame.sample_count(), 0);
    }

    // ── LockFreeRingBuffer tests ───────────────────────────────────────────────

    #[test]
    fn test_ringbuf_initially_empty() {
        let rb = LockFreeRingBuffer::new(16);
        assert!(rb.is_empty());
        assert_eq!(rb.available(), 0);
    }

    #[test]
    fn test_ringbuf_write_and_read_single() {
        let rb = LockFreeRingBuffer::new(16);
        assert!(rb.write(0.5));
        assert!(!rb.is_empty());
        let s = rb.read().expect("should have data");
        assert!((s - 0.5).abs() < 1e-7);
        assert!(rb.is_empty());
    }

    #[test]
    fn test_ringbuf_write_block_read_block() {
        let rb = LockFreeRingBuffer::new(64);
        let data: Vec<f32> = (0..32).map(|i| i as f32 * 0.1).collect();
        let written = rb.write_samples(&data);
        assert_eq!(written, 32);
        let mut dst = vec![0.0_f32; 32];
        let read = rb.read_samples(&mut dst);
        assert_eq!(read, 32);
        for (a, b) in data.iter().zip(dst.iter()) {
            assert!((a - b).abs() < 1e-6);
        }
    }

    #[test]
    fn test_ringbuf_full_rejects_write() {
        let rb = LockFreeRingBuffer::new(4); // usable cap = 3
        assert!(rb.write(1.0));
        assert!(rb.write(2.0));
        assert!(rb.write(3.0));
        assert!(rb.is_full());
        assert!(!rb.write(4.0)); // should fail
    }

    #[test]
    fn test_ringbuf_read_empty_returns_none() {
        let rb = LockFreeRingBuffer::new(16);
        assert!(rb.read().is_none());
    }

    #[test]
    fn test_ringbuf_wrap_around() {
        let rb = LockFreeRingBuffer::new(8); // usable cap = 7
                                             // Fill then partially drain, then fill again — exercises wrap-around
        for i in 0..7 {
            rb.write(i as f32);
        }
        for _ in 0..4 {
            rb.read();
        }
        for i in 0..4 {
            assert!(rb.write(i as f32 + 10.0));
        }
        // Remaining: 4 from original + 4 new = 7; but usable cap = 7, so last write should fail
        assert_eq!(rb.available(), 7);
    }

    #[test]
    fn test_ringbuf_clear() {
        let rb = LockFreeRingBuffer::new(16);
        rb.write_samples(&[1.0, 2.0, 3.0]);
        assert_eq!(rb.available(), 3);
        rb.clear();
        assert!(rb.is_empty());
    }

    #[test]
    fn test_ringbuf_capacity() {
        let rb = LockFreeRingBuffer::new(32);
        assert_eq!(rb.capacity(), 32);
    }

    #[test]
    fn test_ringbuf_free() {
        let rb = LockFreeRingBuffer::new(16); // usable = 15
        rb.write(0.5);
        assert_eq!(rb.free(), 14);
    }

    #[test]
    fn test_ringbuf_fifo_order() {
        let rb = LockFreeRingBuffer::new(16);
        rb.write(1.0);
        rb.write(2.0);
        rb.write(3.0);
        assert_eq!(rb.read().expect("1"), 1.0);
        assert_eq!(rb.read().expect("2"), 2.0);
        assert_eq!(rb.read().expect("3"), 3.0);
    }

    #[test]
    fn test_ringbuf_arc_shared() {
        let rb = Arc::new(LockFreeRingBuffer::new(64));
        let rb2 = Arc::clone(&rb);
        // Simulate producer/consumer in the same thread for determinism
        rb.write_samples(&[0.1, 0.2, 0.3]);
        let mut out = vec![0.0_f32; 3];
        rb2.read_samples(&mut out);
        assert!((out[0] - 0.1).abs() < 1e-6);
    }

    #[test]
    fn test_ringbuf_minimum_capacity_enforced() {
        let rb = LockFreeRingBuffer::new(0); // should be clamped to 2
        assert_eq!(rb.capacity(), 2);
    }

    #[test]
    fn test_ringbuf_debug_format() {
        let rb = LockFreeRingBuffer::new(16);
        let s = format!("{rb:?}");
        assert!(s.contains("LockFreeRingBuffer"));
    }
}