decibri 5.1.0

Cross-platform audio capture, playback, and voice activity detection for Rust
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
//! Internal audio-backend seam.
//!
//! [`AudioBackend`] is the single place the crate reaches the platform audio
//! library. Device enumeration, selector resolution, and stream opening all go
//! through this trait, and [`CpalBackend`] is its sole implementation: the only
//! code in the crate that names a platform-audio (cpal) type. Containing the
//! platform library behind one trait keeps a backend version change a
//! single-file, single-implementation swap, and keeps the rest of the crate in
//! decibri-owned types ([`DecibriError`], [`DeviceSelector`], [`MicrophoneInfo`],
//! [`SpeakerInfo`], and the small support types defined below).

use std::sync::Mutex;

use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};

use crate::device::{compute_is_default, ComputedRow, DeviceSelector, MicrophoneInfo, SpeakerInfo};
use crate::error::DecibriError;

// ── Decibri-owned support types ────────────────────────────────────────────

/// A resolved platform audio device: an opaque, decibri-owned handle around the
/// backend's native device so the platform type does not cross the seam.
pub(crate) struct BackendDevice {
    inner: cpal::Device,
}

/// A live platform audio stream, owned so that dropping it (on
/// [`stop`](Self::stop) or on drop) releases the OS device.
///
/// The native stream is held behind a `Mutex<Option<...>>` so
/// [`stop`](Self::stop) can take and drop it under a shared `&self` while the
/// type stays `Send + Sync` (the bindings require `Sync`: a `#[pyclass]` and
/// `py.detach` both need it). The lock is uncontended: only `stop()` and drop
/// ever touch it. The `Option` also lets the test seam build a handle with no
/// real stream.
pub(crate) struct BackendStream {
    inner: Mutex<Option<cpal::Stream>>,
}

impl BackendStream {
    /// Drop the held stream now, releasing the OS device, rather than only on
    /// drop. Idempotent and poison-tolerant: a poisoned lock still clears the
    /// slot rather than panicking.
    pub(crate) fn stop(&self) {
        if let Ok(mut guard) = self.inner.lock() {
            *guard = None;
        }
    }

    /// Whether the stream is still held (not yet dropped by [`stop`](Self::stop)).
    #[cfg(test)]
    pub(crate) fn is_active(&self) -> bool {
        self.inner.lock().map(|g| g.is_some()).unwrap_or(false)
    }

    /// A handle holding no native stream, for unit tests that exercise the
    /// stream-reading paths without opening a real device.
    #[cfg(test)]
    pub(crate) fn empty() -> Self {
        Self {
            inner: Mutex::new(None),
        }
    }
}

/// Parameters for opening a stream: the decibri-owned mirror of the fields the
/// open path sets on the platform stream config.
pub(crate) struct StreamParams {
    pub channels: u16,
    pub sample_rate: u32,
    /// Fixed buffer size in frames, or `None` for the backend default. Capture
    /// pins a fixed size; playback uses the backend default.
    pub frames_per_buffer: Option<u32>,
}

/// Realtime capture callback: invoked with one interleaved `f32` input buffer.
#[cfg(feature = "capture")]
pub(crate) type InputDataCallback = Box<dyn FnMut(&[f32]) + Send + 'static>;

/// Realtime render callback: invoked to fill one interleaved `f32` output buffer.
#[cfg(feature = "playback")]
pub(crate) type OutputDataCallback = Box<dyn FnMut(&mut [f32]) + Send + 'static>;

/// Stream error callback: invoked with a typed [`DecibriError::DeviceFailed`]
/// when the device or driver fails while streaming.
pub(crate) type StreamErrorCallback = Box<dyn FnMut(DecibriError) + Send + 'static>;

// ── The seam ───────────────────────────────────────────────────────────────

/// The crate's audio-backend seam. The implementor owns every interaction with
/// the platform audio library; callers see only decibri-owned types.
pub(crate) trait AudioBackend: Send + Sync {
    /// List the available input devices.
    fn input_devices(&self) -> Result<Vec<MicrophoneInfo>, DecibriError>;

    /// List the available output devices.
    fn output_devices(&self) -> Result<Vec<SpeakerInfo>, DecibriError>;

    /// Resolve a selector to an input device handle.
    #[cfg(feature = "capture")]
    fn resolve_input_device(
        &self,
        selector: &DeviceSelector,
    ) -> Result<BackendDevice, DecibriError>;

    /// Report the device's native input sample rate: the rate of its default
    /// supported format, which the capture stream is opened at before the
    /// capture chain resamples to the requested target rate.
    #[cfg(feature = "capture")]
    fn native_input_rate(&self, device: &BackendDevice) -> Result<u32, DecibriError>;

    /// Resolve a selector to an output device handle.
    #[cfg(feature = "playback")]
    fn resolve_output_device(
        &self,
        selector: &DeviceSelector,
    ) -> Result<BackendDevice, DecibriError>;

    /// Open and start an input stream on `device`, driving `on_data` with each
    /// captured buffer and `on_error` with a typed device failure.
    #[cfg(feature = "capture")]
    fn open_input_stream(
        &self,
        device: &BackendDevice,
        params: &StreamParams,
        on_data: InputDataCallback,
        on_error: StreamErrorCallback,
    ) -> Result<BackendStream, DecibriError>;

    /// Open and start an output stream on `device`, driving `on_data` to fill
    /// each playback buffer and `on_error` with a typed device failure.
    #[cfg(feature = "playback")]
    fn open_output_stream(
        &self,
        device: &BackendDevice,
        params: &StreamParams,
        on_data: OutputDataCallback,
        on_error: StreamErrorCallback,
    ) -> Result<BackendStream, DecibriError>;
}

/// The cpal-backed [`AudioBackend`]: the crate's sole backend implementation and
/// the only place a cpal type is named.
pub(crate) struct CpalBackend;

impl AudioBackend for CpalBackend {
    fn input_devices(&self) -> Result<Vec<MicrophoneInfo>, DecibriError> {
        enumerate_devices::<Input>()
    }

    fn output_devices(&self) -> Result<Vec<SpeakerInfo>, DecibriError> {
        enumerate_devices::<Output>()
    }

    #[cfg(feature = "capture")]
    fn resolve_input_device(
        &self,
        selector: &DeviceSelector,
    ) -> Result<BackendDevice, DecibriError> {
        Ok(BackendDevice {
            inner: resolve_device_generic::<Input>(selector)?,
        })
    }

    #[cfg(feature = "capture")]
    fn native_input_rate(&self, device: &BackendDevice) -> Result<u32, DecibriError> {
        // The default input config is the device's native supported format, so
        // opening at this rate is always accepted; the capture chain resamples
        // it to the requested target. A query failure maps to the same
        // enumeration error the rest of the seam uses.
        device
            .inner
            .default_input_config()
            .map(|config| config.sample_rate())
            .map_err(|e| DecibriError::DeviceEnumerationFailed(e.to_string()))
    }

    #[cfg(feature = "playback")]
    fn resolve_output_device(
        &self,
        selector: &DeviceSelector,
    ) -> Result<BackendDevice, DecibriError> {
        Ok(BackendDevice {
            inner: resolve_device_generic::<Output>(selector)?,
        })
    }

    #[cfg(feature = "capture")]
    fn open_input_stream(
        &self,
        device: &BackendDevice,
        params: &StreamParams,
        mut on_data: InputDataCallback,
        mut on_error: StreamErrorCallback,
    ) -> Result<BackendStream, DecibriError> {
        let stream = device
            .inner
            .build_input_stream(
                &stream_config(params),
                move |data: &[f32], _: &cpal::InputCallbackInfo| {
                    on_data(data);
                },
                move |err| {
                    // Convert the platform error to the typed cause at the
                    // boundary, exactly as before; the decibri callback records
                    // it and marks the stream closed.
                    on_error(DecibriError::DeviceFailed {
                        source: Box::new(err),
                    });
                },
                None,
            )
            .map_err(|e| DecibriError::StreamOpenFailed(e.to_string()))?;

        stream
            .play()
            .map_err(|e| DecibriError::StreamStartFailed(e.to_string()))?;

        Ok(BackendStream {
            inner: Mutex::new(Some(stream)),
        })
    }

    #[cfg(feature = "playback")]
    fn open_output_stream(
        &self,
        device: &BackendDevice,
        params: &StreamParams,
        mut on_data: OutputDataCallback,
        mut on_error: StreamErrorCallback,
    ) -> Result<BackendStream, DecibriError> {
        let stream = device
            .inner
            .build_output_stream(
                &stream_config(params),
                move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
                    on_data(data);
                },
                move |err| {
                    on_error(DecibriError::DeviceFailed {
                        source: Box::new(err),
                    });
                },
                None,
            )
            .map_err(|e| DecibriError::StreamOpenFailed(e.to_string()))?;

        stream
            .play()
            .map_err(|e| DecibriError::StreamStartFailed(e.to_string()))?;

        Ok(BackendStream {
            inner: Mutex::new(Some(stream)),
        })
    }
}

/// Build a cpal stream config from decibri-owned [`StreamParams`].
fn stream_config(params: &StreamParams) -> cpal::StreamConfig {
    cpal::StreamConfig {
        channels: params.channels,
        sample_rate: params.sample_rate,
        buffer_size: match params.frames_per_buffer {
            Some(frames) => cpal::BufferSize::Fixed(frames),
            None => cpal::BufferSize::Default,
        },
    }
}

// ── cpal device enumeration and resolution ─────────────────────────────────
//
// Direction-generic over the input vs output differences in cpal's enumeration
// and resolution APIs, so one implementation of `enumerate_devices` and
// `resolve_device_generic` covers both. The pure `is_default` matching and the
// `ComputedRow` row type are cpal-free and live in `crate::device`.

/// Internal trait abstracting the input vs output differences in cpal's device
/// enumeration and resolution APIs. Each `impl` is a small table of function
/// pointers into cpal.
trait DeviceDirection {
    type Info;

    fn default_device(host: &cpal::Host) -> Option<cpal::Device>;
    fn list_devices(host: &cpal::Host) -> Result<Vec<cpal::Device>, DecibriError>;
    /// Returns `(channels, sample_rate)`; `(0, 0)` on config failure (matches
    /// the historical behaviour of surfacing an "unknown" device rather than
    /// propagating the error).
    fn default_config(device: &cpal::Device) -> (u16, u32);
    fn build_info(row: ComputedRow) -> Self::Info;
    fn no_device_error() -> DecibriError;
    /// Direction-specific "not found" error for a `Name` / `Id` lookup miss.
    /// Input returns [`DecibriError::MicrophoneNotFound`]; Output returns
    /// [`DecibriError::SpeakerNotFound`].
    fn not_found_error(query: String) -> DecibriError;
}

struct Input;

struct Output;

impl DeviceDirection for Input {
    type Info = MicrophoneInfo;

    fn default_device(host: &cpal::Host) -> Option<cpal::Device> {
        host.default_input_device()
    }

    fn list_devices(host: &cpal::Host) -> Result<Vec<cpal::Device>, DecibriError> {
        host.input_devices()
            .map(|it| it.collect())
            .map_err(|e| DecibriError::DeviceEnumerationFailed(e.to_string()))
    }

    fn default_config(device: &cpal::Device) -> (u16, u32) {
        match device.default_input_config() {
            Ok(config) => (config.channels(), config.sample_rate()),
            Err(_) => (0, 0),
        }
    }

    fn build_info(row: ComputedRow) -> Self::Info {
        MicrophoneInfo {
            index: row.index,
            name: row.name,
            id: row.id,
            max_input_channels: row.channels,
            default_sample_rate: row.sample_rate,
            is_default: row.is_default,
        }
    }

    fn no_device_error() -> DecibriError {
        DecibriError::NoMicrophoneFound
    }

    fn not_found_error(query: String) -> DecibriError {
        DecibriError::MicrophoneNotFound(query)
    }
}

impl DeviceDirection for Output {
    type Info = SpeakerInfo;

    fn default_device(host: &cpal::Host) -> Option<cpal::Device> {
        host.default_output_device()
    }

    fn list_devices(host: &cpal::Host) -> Result<Vec<cpal::Device>, DecibriError> {
        host.output_devices()
            .map(|it| it.collect())
            .map_err(|e| DecibriError::DeviceEnumerationFailed(e.to_string()))
    }

    fn default_config(device: &cpal::Device) -> (u16, u32) {
        match device.default_output_config() {
            Ok(config) => (config.channels(), config.sample_rate()),
            Err(_) => (0, 0),
        }
    }

    fn build_info(row: ComputedRow) -> Self::Info {
        SpeakerInfo {
            index: row.index,
            name: row.name,
            id: row.id,
            max_output_channels: row.channels,
            default_sample_rate: row.sample_rate,
            is_default: row.is_default,
        }
    }

    fn no_device_error() -> DecibriError {
        DecibriError::NoSpeakerFound
    }

    fn not_found_error(query: String) -> DecibriError {
        DecibriError::SpeakerNotFound(query)
    }
}

/// Shared implementation behind [`AudioBackend::input_devices`] /
/// [`AudioBackend::output_devices`]. Direction-generic via [`DeviceDirection`].
fn enumerate_devices<D: DeviceDirection>() -> Result<Vec<D::Info>, DecibriError> {
    let host = cpal::default_host();

    // Stable per-host id (WASAPI endpoint ID / CoreAudio UID / ALSA pcm_id)
    // for the OS default device. `.id()` is fallible on rare host backends;
    // treat a failure as "no default" rather than propagating.
    let default_id = D::default_device(&host).and_then(|d| d.id().ok());

    let devices = D::list_devices(&host)?;

    let rows: Vec<(Option<cpal::DeviceId>, String, u16, u32)> = devices
        .into_iter()
        .enumerate()
        .map(|(index, device)| {
            let id = device.id().ok();
            let name = device
                .description()
                .map(|d| d.name().to_string())
                .unwrap_or_else(|_| format!("Unknown Device {index}"));
            let (channels, sample_rate) = D::default_config(&device);
            (id, name, channels, sample_rate)
        })
        .collect();

    Ok(compute_is_default(rows, default_id)
        .into_iter()
        .map(D::build_info)
        .collect())
}

/// Shared implementation behind [`AudioBackend::resolve_input_device`] /
/// [`AudioBackend::resolve_output_device`]. Direction-generic via
/// [`DeviceDirection`].
fn resolve_device_generic<D: DeviceDirection>(
    selector: &DeviceSelector,
) -> Result<cpal::Device, DecibriError> {
    let host = cpal::default_host();

    match selector {
        DeviceSelector::Default => D::default_device(&host).ok_or_else(D::no_device_error),

        DeviceSelector::Index(idx) => D::list_devices(&host)?
            .into_iter()
            .nth(*idx)
            .ok_or(DecibriError::DeviceIndexOutOfRange),

        DeviceSelector::Name(query) => {
            let query_lower = query.to_lowercase();
            let devices = D::list_devices(&host)?;

            let mut matches: Vec<(usize, String, cpal::Device)> = Vec::new();
            for (index, device) in devices.into_iter().enumerate() {
                let name = device
                    .description()
                    .map(|d| d.name().to_string())
                    .unwrap_or_default();
                if name.to_lowercase().contains(&query_lower) {
                    matches.push((index, name, device));
                }
            }

            match matches.len() {
                0 => Err(D::not_found_error(query.clone())),
                1 => Ok(matches.into_iter().next().unwrap().2),
                _ => {
                    let match_list = matches
                        .iter()
                        .map(|(idx, name, _)| format!("  [{idx}] {name}"))
                        .collect::<Vec<_>>()
                        .join("\n");
                    Err(DecibriError::MultipleDevicesMatch {
                        name: query.clone(),
                        matches: format!(
                            "{match_list}\nUse a more specific name or pass the device index directly."
                        ),
                    })
                }
            }
        }

        DeviceSelector::Id(query) => {
            // Exact match on cpal::DeviceId's `Display` representation. Device
            // IDs are unique per host, so at most one device matches. A device
            // whose `id()` call fails (rare) is silently skipped; the scenario
            // is treated as "not found", matching `enumerate_devices`, which
            // assigns such a device an empty `id` string that no query matches.
            let devices = D::list_devices(&host)?;
            for device in devices {
                if let Ok(id) = device.id() {
                    if id.to_string() == *query {
                        return Ok(device);
                    }
                }
            }
            Err(D::not_found_error(query.clone()))
        }
    }
}

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

    /// The cpal backend satisfies the [`AudioBackend`] seam contract, including
    /// its `Send + Sync` supertrait bound (the bindings depend on it).
    #[test]
    fn cpal_backend_satisfies_the_seam() {
        fn assert_backend<T: AudioBackend>() {}
        fn assert_send_sync<T: Send + Sync>() {}
        assert_backend::<CpalBackend>();
        assert_send_sync::<CpalBackend>();
    }

    /// The stream handle is `Send + Sync` so the stream types that hold it stay
    /// `Send + Sync` (the bindings require `Sync`).
    #[test]
    fn backend_stream_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<BackendStream>();
    }

    /// `stop()` drops the held stream and is idempotent. The test seam holds no
    /// real device, so this asserts the slot is empty after stop and that a
    /// second stop does not panic; the real `Some -> None` drop (device release)
    /// is exercised by the binding suites and integration capture/playback.
    #[test]
    fn backend_stream_stop_empties_and_is_idempotent() {
        let stream = BackendStream::empty();
        assert!(!stream.is_active(), "an empty handle holds no stream");
        stream.stop();
        assert!(!stream.is_active(), "stop() leaves the slot empty");
        stream.stop(); // idempotent: an already-empty slot must not panic
    }

    /// Enumeration through the seam reaches cpal and returns without panicking
    /// on any host, including headless CI runners with no audio devices (the
    /// result may be an empty list or an enumeration error, both acceptable).
    #[test]
    fn backend_enumeration_runs() {
        let _ = CpalBackend.input_devices();
        let _ = CpalBackend.output_devices();
    }

    /// The direction-specific `not_found_error` produces the correct
    /// `DecibriError` variant and display string for each direction: input
    /// lookups return `MicrophoneNotFound`, output lookups `SpeakerNotFound`.
    #[test]
    fn not_found_error_produces_direction_correct_variant() {
        let input_err = Input::not_found_error("nonexistent-input".to_string());
        let output_err = Output::not_found_error("nonexistent-output".to_string());

        assert!(
            matches!(input_err, DecibriError::MicrophoneNotFound(ref s) if s == "nonexistent-input"),
            "Input direction must return DecibriError::MicrophoneNotFound"
        );
        assert!(
            matches!(output_err, DecibriError::SpeakerNotFound(ref s) if s == "nonexistent-output"),
            "Output direction must return DecibriError::SpeakerNotFound"
        );

        assert_eq!(
            input_err.to_string(),
            "No microphone found matching \"nonexistent-input\"",
            "Input variant's Display must name the microphone"
        );
        assert_eq!(
            output_err.to_string(),
            "No speaker found matching \"nonexistent-output\"",
            "Output variant's Display must name the speaker"
        );
    }
}