interflow/backends/
mod.rs

1//! # Backends
2//!
3//! Home of the various backends supported by the library.
4//!
5//! Each backend is provided in its own submodule. Types should be public so that the user isn't
6//! limited to going through the main API if they want to choose a specific backend.
7
8use crate::{AudioDriver, AudioInputDevice, AudioOutputDevice, DeviceType};
9
10#[cfg(unsupported)]
11compile_error!("Unsupported platform (supports ALSA, CoreAudio, and WASAPI)");
12
13#[cfg(os_alsa)]
14pub mod alsa;
15
16#[cfg(os_coreaudio)]
17pub mod coreaudio;
18
19#[cfg(os_wasapi)]
20pub mod wasapi;
21
22#[cfg(all(os_pipewire, feature = "pipewire"))]
23pub mod pipewire;
24
25/// Returns the default driver.
26///
27/// "Default" here means that it is a supported driver that is available on the platform.
28///
29/// The signature makes it unfortunately impossible to do runtime selection, and could change in
30/// the future to make it possible. Until now, the "default" driver is the lowest common
31/// denominator.
32///
33/// Selects the following driver depending on platform:
34///
35/// | **Platform** |           **Driver**        |
36/// |:------------:|:---------------------------:|
37/// |     Linux    | Pipewire (if enabled), ALSA |
38/// |     macOS    |          CoreAudio          |
39/// |    Windows   |           WASAPI            |
40#[cfg(any(os_alsa, os_coreaudio, os_wasapi))]
41#[allow(clippy::needless_return)]
42pub fn default_driver() -> impl AudioDriver {
43    #[cfg(all(os_pipewire, feature = "pipewire"))]
44    return pipewire::driver::PipewireDriver::new().unwrap();
45    #[cfg(all(not(all(os_pipewire, feature = "pipewire")), os_alsa))]
46    return alsa::AlsaDriver;
47    #[cfg(os_coreaudio)]
48    return coreaudio::CoreAudioDriver;
49    #[cfg(os_wasapi)]
50    return wasapi::WasapiDriver;
51}
52
53/// Returns the default input device for the given audio driver.
54///
55/// The default device is usually the one the user has selected in its system settings.
56pub fn default_input_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
57where
58    Driver::Device: AudioInputDevice,
59{
60    driver
61        .default_device(DeviceType::PHYSICAL | DeviceType::INPUT)
62        .expect("Audio driver error")
63        .expect("No default device found")
64}
65
66/// Default input device from the default driver for this platform.
67///
68/// "Default" here means both in terms of platform support but also can include runtime selection.
69/// Therefore, it is better to use this method directly rather than first getting the default
70/// driver from [`default_driver`].
71#[cfg(any(feature = "pipewire", os_alsa, os_coreaudio, os_wasapi))]
72#[allow(clippy::needless_return)]
73pub fn default_input_device() -> impl AudioInputDevice {
74    #[cfg(all(os_pipewire, feature = "pipewire"))]
75    return default_input_device_from(&pipewire::driver::PipewireDriver::new().unwrap());
76    #[cfg(all(not(all(os_pipewire, feature = "pipewire")), os_alsa))]
77    return default_input_device_from(&alsa::AlsaDriver);
78    #[cfg(os_coreaudio)]
79    return default_input_device_from(&coreaudio::CoreAudioDriver);
80    #[cfg(os_wasapi)]
81    return default_input_device_from(&wasapi::WasapiDriver);
82}
83
84/// Returns the default input device for the given audio driver.
85///
86/// The default device is usually the one the user has selected in its system settings.
87pub fn default_output_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
88where
89    Driver::Device: AudioOutputDevice,
90{
91    driver
92        .default_device(DeviceType::PHYSICAL | DeviceType::OUTPUT)
93        .expect("Audio driver error")
94        .expect("No default device found")
95}
96
97/// Default output device from the default driver for this platform.
98///
99/// "Default" here means both in terms of platform support but also can include runtime selection.
100/// Therefore, it is better to use this method directly rather than first getting the default
101/// driver from [`default_driver`].
102#[cfg(any(os_alsa, os_coreaudio, os_wasapi, feature = "pipewire"))]
103#[allow(clippy::needless_return)]
104pub fn default_output_device() -> impl AudioOutputDevice {
105    #[cfg(all(os_pipewire, feature = "pipewire"))]
106    return default_output_device_from(&pipewire::driver::PipewireDriver::new().unwrap());
107    #[cfg(all(not(all(os_pipewire, feature = "pipewire")), os_alsa))]
108    return default_output_device_from(&alsa::AlsaDriver);
109    #[cfg(os_coreaudio)]
110    return default_output_device_from(&coreaudio::CoreAudioDriver);
111    #[cfg(os_wasapi)]
112    return default_output_device_from(&wasapi::WasapiDriver);
113}