use crate::{
DefaultStreamConfigError, DeviceNameError, SupportedStreamConfig, SupportedStreamConfigsError,
};
use cpal::traits::DeviceTrait;
use std::ops::Deref;
pub struct Device {
pub(crate) device: cpal::Device,
}
pub struct Devices {
pub(crate) devices: cpal::Devices,
}
pub type SupportedInputConfigs = cpal::SupportedInputConfigs;
pub type SupportedOutputConfigs = cpal::SupportedOutputConfigs;
impl Device {
pub fn name(&self) -> Result<String, DeviceNameError> {
self.device.name()
}
pub fn supported_input_configs(
&self,
) -> Result<SupportedInputConfigs, SupportedStreamConfigsError> {
self.device.supported_input_configs()
}
pub fn supported_output_configs(
&self,
) -> Result<SupportedOutputConfigs, SupportedStreamConfigsError> {
self.device.supported_output_configs()
}
pub fn default_input_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> {
self.device.default_input_config()
}
pub fn default_output_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> {
self.device.default_output_config()
}
pub fn max_supported_output_channels(&self) -> usize {
self.supported_output_configs()
.expect("failed to get supported output audio stream formats")
.map(|fmt| fmt.channels() as usize)
.max()
.unwrap_or(0)
}
pub fn max_supported_input_channels(&self) -> usize {
self.supported_input_configs()
.expect("failed to get supported input audio stream formats")
.map(|fmt| fmt.channels() as usize)
.max()
.unwrap_or(0)
}
}
impl Deref for Device {
type Target = cpal::Device;
fn deref(&self) -> &Self::Target {
&self.device
}
}
impl Iterator for Devices {
type Item = Device;
fn next(&mut self) -> Option<Self::Item> {
self.devices.next().map(|device| Device { device })
}
}