#[cfg(feature = "alloc")]
use crate::Cow;
#[cfg(feature = "alsa")]
use crate::{_alsa_raw, CStr};
use crate::{_impl_init, ConstInit, impl_trait};
#[doc = crate::_tags!(audio io)]
#[doc = crate::_doc_meta!{location("media/audio")}]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AudioDevice<'a> {
pub id: &'a str,
pub name: Option<&'a str>,
pub desc: Option<&'a str>,
pub dir: AudioDeviceDir,
}
impl ConstInit for AudioDevice<'_> {
const INIT: Self = Self {
id: "",
name: None,
desc: None,
dir: AudioDeviceDir::INIT,
};
}
impl<'a> AudioDevice<'a> {
pub const fn label(self) -> &'a str {
match self.name {
Some(name) => name,
None => self.id,
}
}
pub const fn desc_or_id(self) -> &'a str {
match self.desc {
Some(desc) => desc,
None => self.id,
}
}
}
#[cfg(feature = "alloc")]
#[doc = crate::_tags!(audio io)]
#[doc = crate::_doc_meta!{location("sys/device/audio")}]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AudioDeviceCow<'a> {
pub id: Cow<'a, str>,
pub name: Option<Cow<'a, str>>,
pub desc: Option<Cow<'a, str>>,
pub dir: AudioDeviceDir,
}
#[doc = crate::_tags!(audio dir)]
#[doc = crate::_doc_meta!{location("media/audio")}]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AudioDeviceDir {
Playback,
Capture,
#[default]
Duplex,
Unknown,
}
_impl_init![Self::Duplex => AudioDeviceDir];
impl_trait![fmt::Display for AudioDeviceDir |self, f| f.write_str(self.as_code())];
impl AudioDeviceDir {
pub const fn as_code(self) -> &'static str {
match self {
Self::Playback => "playback",
Self::Capture => "capture",
Self::Duplex => "duplex",
Self::Unknown => "unknown",
}
}
pub const fn has_playback(self) -> bool {
matches!(self, Self::Playback | Self::Duplex)
}
pub const fn has_capture(self) -> bool {
matches!(self, Self::Capture | Self::Duplex)
}
pub const fn single_stream_dir(self) -> Option<AudioStreamDir> {
match self {
Self::Playback => Some(AudioStreamDir::Playback),
Self::Capture => Some(AudioStreamDir::Capture),
Self::Duplex | Self::Unknown => None,
}
}
#[cfg(feature = "alsa")]
#[cfg_attr(not(ffi_alsa··), allow(dead_code))]
pub(crate) const fn from_alsa_ioid(ioid: Option<&CStr>) -> Self {
match ioid {
Some(cstr) => match cstr.to_bytes() {
b"Output" => Self::Playback,
b"Input" => Self::Capture,
_ => Self::Unknown,
},
None => Self::Duplex,
}
}
}
#[doc = crate::_tags!(audio dir)]
#[doc = crate::_doc_meta!{location("media/audio")}]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AudioStreamDir {
#[default]
Playback,
Capture,
}
_impl_init![Self::Playback => AudioStreamDir];
impl_trait![fmt::Display for AudioStreamDir |self, f| f.write_str(self.as_code())];
impl AudioStreamDir {
pub const fn as_code(self) -> &'static str {
match self {
Self::Playback => "playback",
Self::Capture => "capture",
}
}
pub const fn as_name(self) -> &'static str {
match self {
Self::Playback => "Playback",
Self::Capture => "Capture",
}
}
pub const fn is_playback(self) -> bool {
matches!(self, Self::Playback)
}
pub const fn is_capture(self) -> bool {
matches!(self, Self::Capture)
}
#[cfg(feature = "alsa")]
#[cfg_attr(not(ffi_alsa··), allow(dead_code))]
pub(crate) const fn to_alsa(self) -> _alsa_raw::snd_pcm_stream_t {
match self {
Self::Playback => _alsa_raw::SND_PCM_STREAM_PLAYBACK,
Self::Capture => _alsa_raw::SND_PCM_STREAM_CAPTURE,
}
}
}