use cpal::Device;
use crate::input;
use crate::output::{self, device_name, OutputType};
use crate::plugins::Plugin;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
Input,
Output,
}
#[derive(Clone)]
pub struct AtomeDevice {
device: Device,
direction: Direction,
host: OutputType,
plugins: Vec<Plugin>,
routing: Option<Vec<String>>,
}
impl AtomeDevice {
pub fn input(device: Device, host: OutputType) -> Self {
AtomeDevice::new(device, Direction::Input, host)
}
pub fn output(device: Device, host: OutputType) -> Self {
AtomeDevice::new(device, Direction::Output, host)
}
pub fn default_input(host: OutputType) -> Option<Self> {
input::default_device().map(|device| AtomeDevice::input(device, host))
}
pub fn default_output(host: OutputType) -> Option<Self> {
output::default_device().map(|device| AtomeDevice::output(device, host))
}
fn new(device: Device, direction: Direction, host: OutputType) -> Self {
AtomeDevice {
device,
direction,
host,
plugins: Vec::new(),
routing: None,
}
}
pub fn with_plugin(mut self, plugin: Plugin) -> Self {
self.plugins.push(plugin);
self
}
pub fn with_plugins(mut self, plugins: impl IntoIterator<Item = Plugin>) -> Self {
self.plugins.extend(plugins);
self
}
pub fn route_to(mut self, outputs: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.routing = Some(outputs.into_iter().map(Into::into).collect());
self
}
pub fn direction(&self) -> Direction {
self.direction
}
pub fn host(&self) -> OutputType {
self.host
}
pub fn device(&self) -> &Device {
&self.device
}
pub fn plugins(&self) -> &[Plugin] {
&self.plugins
}
pub fn plugins_mut(&mut self) -> &mut Vec<Plugin> {
&mut self.plugins
}
pub fn routing(&self) -> Option<&[String]> {
self.routing.as_deref()
}
pub fn name(&self) -> String {
device_name(&self.device)
}
}
impl std::fmt::Debug for AtomeDevice {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("AtomeDevice")
.field("name", &self.name())
.field("direction", &self.direction)
.field("host", &self.host)
.field("plugins", &self.plugins.len())
.field("routing", &self.routing)
.finish()
}
}