use rodio::cpal::traits::DeviceTrait;
use crate::Backend;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Device {
pub(crate) backend: Backend,
pub(crate) id: Option<rodio::cpal::DeviceId>,
pub(crate) description: rodio::cpal::DeviceDescription,
pub(crate) is_default: bool,
}
impl std::fmt::Display for Device {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}", self.description)?;
Ok(())
}
}
impl Device {
pub fn backend(&self) -> Backend {
self.backend
}
pub fn id(&self) -> Option<&rodio::cpal::DeviceId> {
self.id.as_ref()
}
pub fn description(&self) -> &rodio::cpal::DeviceDescription {
&self.description
}
pub fn name(&self) -> &str {
self.description.name()
}
pub fn is_default(&self) -> bool {
self.is_default
}
pub fn device_type(&self) -> rodio::cpal::DeviceType {
self.description.device_type()
}
pub fn interface_type(&self) -> rodio::cpal::InterfaceType {
self.description.interface_type()
}
pub(crate) fn from_cpal_device(
backend: Backend,
device: &rodio::cpal::Device,
is_default: bool,
) -> Option<Self> {
let description = device.description().ok()?;
let id = device.id().ok();
Some(Self {
backend,
id,
description,
is_default,
})
}
pub(crate) fn matches_cpal_device(&self, device: &rodio::cpal::Device) -> bool {
if let (Some(wanted), Ok(actual)) = (self.id.as_ref(), device.id())
&& wanted == &actual
{
return true;
}
device
.description()
.is_ok_and(|description| description.name() == self.name())
}
}