tek/crates/engine/src/audio/audio_out.rs
unspeaker 447638ee71
Some checks are pending
/ build (push) Waiting to run
wip: general overhaul of core and ports
2025-05-20 22:05:09 +03:00

77 lines
2.4 KiB
Rust

use crate::*;
//impl_port!(AudioOutput: AudioOut -> AudioIn |j, n|j.register_port::<AudioOut>(n));
#[derive(Debug)] pub struct AudioOutput<'j> {
/// Handle to JACK client, for receiving reconnect events.
jack: Jack<'j>,
/// Port name
name: Arc<str>,
/// Port handle.
port: Port<AudioOut>,
/// List of ports to connect to.
connections: Vec<PortConnect>
}
impl<'j> AsRef<Port<AudioOut>> for AudioOutput<'j> {
fn as_ref (&self) -> &Port<AudioOut> { &self.port }
}
impl<'j> AudioOutput<'j> {
pub fn new (jack: &Jack, name: impl AsRef<str>, connect: &[PortConnect])
-> Usually<Self>
{
let port = Self {
port: jack.register_port::<AudioOut>(name.as_ref())?,
jack,
name: name.into(),
connections: connect.to_vec()
};
port.connect_to_matching()?;
Ok(port)
}
pub fn name (&self) -> &Arc<str> { &self.name }
pub fn port (&self) -> &Port<AudioOut> { &self.port }
pub fn port_mut (&mut self) -> &mut Port<AudioOut> { &mut self.port }
pub fn into_port (self) -> Port<AudioOut> { self.port }
pub fn close (self) -> Usually<()> {
let Self { jack, port, .. } = self;
Ok(jack.with_client(|client|client.unregister_port(port))?)
}
}
impl<'j> HasJack<'j> for AudioOutput<'j> {
fn jack (&self) -> &'j Jack<'j> { &self.jack }
}
impl<'j> JackPort<'j> for AudioOutput<'j> {
type Port = AudioOut;
type Pair = AudioIn;
fn port (&self) -> &Port<AudioOut> { &self.port }
}
connect_to!(<'j>|self: AudioOutput<'j>, port: &str|{
self.with_client(|c|if let Some(ref port) = c.port_by_name(port.as_ref()) {
self.connect_to(port)
} else {
Ok(Missing)
})
});
connect_to!(<'j>|self: AudioOutput<'j>, port: Port<AudioIn>|{
self.with_client(|c|Ok(if let Ok(_) = c.connect_ports(&self.port, port) {
Connected
} else if let Ok(_) = c.connect_ports(port, &self.port) {
Connected
} else {
Mismatch
}))
});
connect_to!(<'j>|self: AudioOutput<'j>, port: Port<Unowned>|{
self.with_client(|c|Ok(if let Ok(_) = c.connect_ports(&self.port, port) {
Connected
} else if let Ok(_) = c.connect_ports(port, &self.port) {
Connected
} else {
Mismatch
}))
});
impl<'j> ConnectAuto<'j> for AudioOutput<'j> {
fn connections (&self) -> &[PortConnect] {
&self.connections
}
}