mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
408 lines
15 KiB
Rust
408 lines
15 KiB
Rust
use crate::*;
|
|
|
|
#[derive(Debug)]
|
|
pub struct JackPort<T: PortSpec> {
|
|
/// Port name
|
|
pub name: Arc<str>,
|
|
/// Handle to JACK client, for receiving reconnect events.
|
|
pub jack: Arc<RwLock<JackConnection>>,
|
|
/// Port handle.
|
|
pub port: Port<T>,
|
|
/// List of ports to connect to.
|
|
pub connect: Vec<PortConnection>
|
|
}
|
|
impl<T: PortSpec> JackPort<T> {
|
|
pub fn connect_to_matching (&mut self) -> Usually<()> {
|
|
use PortConnectionName::*;
|
|
use PortConnectionScope::*;
|
|
use PortConnectionStatus::*;
|
|
for connect in self.connect.iter_mut() {
|
|
let mut status = vec![];
|
|
match &connect.name {
|
|
Exact(name) => for port in self.jack.ports(None, None, PortFlags::empty()).iter() {
|
|
if port.as_str() == &**name {
|
|
if let Some(port) = self.jack.port_by_name(port.as_str()) {
|
|
let port_status = Self::try_both_ways(&self.jack, &port, &self.port);
|
|
let name = port.name()?.into();
|
|
status.push((port, name, port_status));
|
|
if port_status == Connected {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
},
|
|
RegExp(re) => for port in self.jack.ports(Some(&re), None, PortFlags::empty()).iter() {
|
|
if let Some(port) = self.jack.port_by_name(port.as_str()) {
|
|
let port_status = Self::try_both_ways(&self.jack, &port, &self.port);
|
|
let name = port.name()?.into();
|
|
status.push((port, name, port_status));
|
|
if port_status == Connected && connect.scope == One {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
connect.status = status
|
|
}
|
|
Ok(())
|
|
}
|
|
fn try_both_ways <A: PortSpec, B: PortSpec> (
|
|
jack: &impl ConnectPort, port_a: &Port<A>, port_b: &Port<B>
|
|
)
|
|
-> PortConnectionStatus
|
|
{
|
|
if let Ok(_) = jack.connect_ports(port_a, port_b) {
|
|
PortConnectionStatus::Connected
|
|
} else if let Ok(_) = jack.connect_ports(port_b, port_a) {
|
|
PortConnectionStatus::Connected
|
|
} else {
|
|
PortConnectionStatus::Mismatch
|
|
}
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct PortConnection {
|
|
pub name: PortConnectionName,
|
|
pub scope: PortConnectionScope,
|
|
pub status: Vec<(Port<Unowned>, Arc<str>, PortConnectionStatus)>,
|
|
}
|
|
impl PortConnection {
|
|
pub fn collect (exact: &[impl AsRef<str>], re: &[impl AsRef<str>], re_all: &[impl AsRef<str>])
|
|
-> Vec<Self>
|
|
{
|
|
let mut connections = vec![];
|
|
for port in exact.iter() { connections.push(Self::exact(port)) }
|
|
for port in re.iter() { connections.push(Self::regexp(port)) }
|
|
for port in re_all.iter() { connections.push(Self::regexp_all(port)) }
|
|
connections
|
|
}
|
|
/// Connect to this exact port
|
|
pub fn exact (name: impl AsRef<str>) -> Self {
|
|
let name = PortConnectionName::Exact(name.as_ref().into());
|
|
Self { name, scope: PortConnectionScope::One, status: vec![] }
|
|
}
|
|
pub fn regexp (name: impl AsRef<str>) -> Self {
|
|
let name = PortConnectionName::RegExp(name.as_ref().into());
|
|
Self { name, scope: PortConnectionScope::One, status: vec![] }
|
|
}
|
|
pub fn regexp_all (name: impl AsRef<str>) -> Self {
|
|
let name = PortConnectionName::RegExp(name.as_ref().into());
|
|
Self { name, scope: PortConnectionScope::All, status: vec![] }
|
|
}
|
|
pub fn info (&self) -> Arc<str> {
|
|
format!("{} {} {}", match self.scope {
|
|
PortConnectionScope::One => " ",
|
|
PortConnectionScope::All => "*",
|
|
}, match &self.name {
|
|
PortConnectionName::Exact(name) => format!("= {name}"),
|
|
PortConnectionName::RegExp(name) => format!("~ {name}"),
|
|
}, self.status.len()).into()
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum PortConnectionName {
|
|
/** Exact match */
|
|
Exact(Arc<str>),
|
|
/** Match regular expression */
|
|
RegExp(Arc<str>),
|
|
}
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub enum PortConnectionScope { One, All }
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub enum PortConnectionStatus { Missing, Disconnected, Connected, Mismatch, }
|
|
|
|
impl<T: PortSpec> AsRef<Port<T>> for JackPort<T> {
|
|
fn as_ref (&self) -> &Port<T> {
|
|
&self.port
|
|
}
|
|
}
|
|
impl JackPort<MidiIn> {
|
|
pub fn new (
|
|
jack: &Arc<RwLock<JackConnection>>, name: impl AsRef<str>, connect: &[PortConnection]
|
|
) -> Usually<Self> {
|
|
let mut port = JackPort {
|
|
jack: jack.clone(),
|
|
port: jack.midi_in(name.as_ref())?,
|
|
name: name.as_ref().into(),
|
|
connect: connect.to_vec()
|
|
};
|
|
port.connect_to_matching()?;
|
|
Ok(port)
|
|
}
|
|
}
|
|
impl JackPort<MidiOut> {
|
|
pub fn new (
|
|
jack: &Arc<RwLock<JackConnection>>, name: impl AsRef<str>, connect: &[PortConnection]
|
|
) -> Usually<Self> {
|
|
let mut port = Self {
|
|
jack: jack.clone(),
|
|
port: jack.midi_out(name.as_ref())?,
|
|
name: name.as_ref().into(),
|
|
connect: connect.to_vec()
|
|
};
|
|
port.connect_to_matching()?;
|
|
Ok(port)
|
|
}
|
|
}
|
|
impl JackPort<AudioIn> {
|
|
pub fn new (
|
|
jack: &Arc<RwLock<JackConnection>>, name: impl AsRef<str>, connect: &[PortConnection]
|
|
) -> Usually<Self> {
|
|
let mut port = Self {
|
|
jack: jack.clone(),
|
|
port: jack.audio_in(name.as_ref())?,
|
|
name: name.as_ref().into(),
|
|
connect: connect.to_vec()
|
|
};
|
|
port.connect_to_matching()?;
|
|
Ok(port)
|
|
}
|
|
}
|
|
impl JackPort<AudioOut> {
|
|
pub fn new (
|
|
jack: &Arc<RwLock<JackConnection>>, name: impl AsRef<str>, connect: &[PortConnection]
|
|
) -> Usually<Self> {
|
|
let mut port = Self {
|
|
jack: jack.clone(),
|
|
port: jack.audio_out(name.as_ref())?,
|
|
name: name.as_ref().into(),
|
|
connect: connect.to_vec()
|
|
};
|
|
port.connect_to_matching()?;
|
|
Ok(port)
|
|
}
|
|
}
|
|
|
|
pub trait ConnectPort {
|
|
fn ports (&self, re_name: Option<&str>, re_type: Option<&str>, flags: PortFlags) -> Vec<String>;
|
|
fn port_by_name (&self, name: impl AsRef<str>) -> Option<Port<Unowned>>;
|
|
fn connect_ports <A: PortSpec, B: PortSpec> (&self, source: &Port<A>, target: &Port<B>)
|
|
-> Usually<()>;
|
|
fn connect_midi_from (&self, input: &Port<MidiIn>, ports: &[impl AsRef<str>]) -> Usually<()> {
|
|
for port in ports.iter() {
|
|
let port = port.as_ref();
|
|
if let Some(port) = self.port_by_name(port).as_ref() {
|
|
self.connect_ports(port, input)?;
|
|
} else {
|
|
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
fn connect_midi_to (&self, output: &Port<MidiOut>, ports: &[impl AsRef<str>]) -> Usually<()> {
|
|
for port in ports.iter() {
|
|
let port = port.as_ref();
|
|
if let Some(port) = self.port_by_name(port).as_ref() {
|
|
self.connect_ports(output, port)?;
|
|
} else {
|
|
panic!("Missing MIDI input: {port}. Use jack_lsp to list all port names.");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
fn connect_audio_from (&self, input: &Port<AudioIn>, ports: &[impl AsRef<str>]) -> Usually<()> {
|
|
for port in ports.iter() {
|
|
let port = port.as_ref();
|
|
if let Some(port) = self.port_by_name(port).as_ref() {
|
|
self.connect_ports(port, input)?;
|
|
} else {
|
|
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
fn connect_audio_to (&self, output: &Port<AudioOut>, ports: &[impl AsRef<str>]) -> Usually<()> {
|
|
for port in ports.iter() {
|
|
let port = port.as_ref();
|
|
if let Some(port) = self.port_by_name(port).as_ref() {
|
|
self.connect_ports(output, port)?;
|
|
} else {
|
|
panic!("Missing MIDI input: {port}. Use jack_lsp to list all port names.");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
impl ConnectPort for JackConnection {
|
|
fn ports (&self, re_name: Option<&str>, re_type: Option<&str>, flags: PortFlags) -> Vec<String> {
|
|
self.client().ports(re_name, re_type, flags)
|
|
}
|
|
fn port_by_name (&self, name: impl AsRef<str>) -> Option<Port<Unowned>> {
|
|
self.client().port_by_name(name.as_ref())
|
|
}
|
|
fn connect_ports <A: PortSpec, B: PortSpec> (&self, source: &Port<A>, target: &Port<B>)
|
|
-> Usually<()>
|
|
{
|
|
Ok(self.client().connect_ports(source, target)?)
|
|
}
|
|
}
|
|
impl<T: ConnectPort> ConnectPort for Arc<RwLock<T>> {
|
|
fn ports (&self, re_name: Option<&str>, re_type: Option<&str>, flags: PortFlags) -> Vec<String> {
|
|
self.read().unwrap().ports(re_name, re_type, flags)
|
|
}
|
|
fn port_by_name (&self, name: impl AsRef<str>) -> Option<Port<Unowned>> {
|
|
self.read().unwrap().port_by_name(name.as_ref())
|
|
}
|
|
fn connect_ports <A: PortSpec, B: PortSpec> (&self, source: &Port<A>, target: &Port<B>)
|
|
-> Usually<()>
|
|
{
|
|
Ok(self.read().unwrap().connect_ports(source, target)?)
|
|
}
|
|
}
|
|
|
|
/// This is a utility trait for things that may register or connect [Port]s.
|
|
/// It contains shorthand methods to this purpose. It's implemented for
|
|
/// `Arc<RwLock<JackConnection>>` for terse port registration in the
|
|
/// `init` callback of [JackClient::activate_with].
|
|
pub trait RegisterPort {
|
|
fn midi_in (&self, name: impl AsRef<str>) -> Usually<Port<MidiIn>>;
|
|
fn midi_out (&self, name: impl AsRef<str>) -> Usually<Port<MidiOut>>;
|
|
fn audio_in (&self, name: impl AsRef<str>) -> Usually<Port<AudioIn>>;
|
|
fn audio_out (&self, name: impl AsRef<str>) -> Usually<Port<AudioOut>>;
|
|
}
|
|
impl RegisterPort for JackConnection {
|
|
fn midi_in (&self, name: impl AsRef<str>) -> Usually<Port<MidiIn>> {
|
|
Ok(self.client().register_port(name.as_ref(), MidiIn::default())?)
|
|
}
|
|
fn midi_out (&self, name: impl AsRef<str>) -> Usually<Port<MidiOut>> {
|
|
Ok(self.client().register_port(name.as_ref(), MidiOut::default())?)
|
|
}
|
|
fn audio_in (&self, name: impl AsRef<str>) -> Usually<Port<AudioIn>> {
|
|
Ok(self.client().register_port(name.as_ref(), AudioIn::default())?)
|
|
}
|
|
fn audio_out (&self, name: impl AsRef<str>) -> Usually<Port<AudioOut>> {
|
|
Ok(self.client().register_port(name.as_ref(), AudioOut::default())?)
|
|
}
|
|
}
|
|
impl<T: RegisterPort> RegisterPort for Arc<RwLock<T>> {
|
|
fn midi_in (&self, name: impl AsRef<str>) -> Usually<Port<MidiIn>> {
|
|
self.read().unwrap().midi_in(name)
|
|
}
|
|
fn midi_out (&self, name: impl AsRef<str>) -> Usually<Port<MidiOut>> {
|
|
self.read().unwrap().midi_out(name)
|
|
}
|
|
fn audio_in (&self, name: impl AsRef<str>) -> Usually<Port<AudioIn>> {
|
|
self.read().unwrap().audio_in(name)
|
|
}
|
|
fn audio_out (&self, name: impl AsRef<str>) -> Usually<Port<AudioOut>> {
|
|
self.read().unwrap().audio_out(name)
|
|
}
|
|
}
|
|
|
|
///// Collection of JACK ports as [AudioIn]/[AudioOut]/[MidiIn]/[MidiOut].
|
|
//#[derive(Default, Debug)]
|
|
//pub struct JackPorts {
|
|
//pub audio_ins: BTreeMap<String, Port<AudioIn>>,
|
|
//pub midi_ins: BTreeMap<String, Port<MidiIn>>,
|
|
//pub audio_outs: BTreeMap<String, Port<AudioOut>>,
|
|
//pub midi_outs: BTreeMap<String, Port<MidiOut>>,
|
|
//}
|
|
|
|
///// Collection of JACK ports as [Unowned].
|
|
//#[derive(Default, Debug)]
|
|
//pub struct UnownedJackPorts {
|
|
//pub audio_ins: BTreeMap<String, Port<Unowned>>,
|
|
//pub midi_ins: BTreeMap<String, Port<Unowned>>,
|
|
//pub audio_outs: BTreeMap<String, Port<Unowned>>,
|
|
//pub midi_outs: BTreeMap<String, Port<Unowned>>,
|
|
//}
|
|
|
|
//impl JackPorts {
|
|
//pub fn clone_unowned(&self) -> UnownedJackPorts {
|
|
//let mut unowned = UnownedJackPorts::default();
|
|
//for (name, port) in self.midi_ins.iter() {
|
|
//unowned.midi_ins.insert(name.clone(), port.clone_unowned());
|
|
//}
|
|
//for (name, port) in self.midi_outs.iter() {
|
|
//unowned.midi_outs.insert(name.clone(), port.clone_unowned());
|
|
//}
|
|
//for (name, port) in self.audio_ins.iter() {
|
|
//unowned.audio_ins.insert(name.clone(), port.clone_unowned());
|
|
//}
|
|
//for (name, port) in self.audio_outs.iter() {
|
|
//unowned
|
|
//.audio_outs
|
|
//.insert(name.clone(), port.clone_unowned());
|
|
//}
|
|
//unowned
|
|
//}
|
|
//}
|
|
|
|
///// Implement the `Ports` trait.
|
|
//#[macro_export]
|
|
//macro_rules! ports {
|
|
//($T:ty $({ $(audio: {
|
|
//$(ins: |$ai_arg:ident|$ai_impl:expr,)?
|
|
//$(outs: |$ao_arg:ident|$ao_impl:expr,)?
|
|
//})? $(midi: {
|
|
//$(ins: |$mi_arg:ident|$mi_impl:expr,)?
|
|
//$(outs: |$mo_arg:ident|$mo_impl:expr,)?
|
|
//})?})?) => {
|
|
//impl Ports for $T {$(
|
|
//$(
|
|
//$(fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
|
//let cb = |$ai_arg:&'a Self|$ai_impl;
|
|
//cb(self)
|
|
//})?
|
|
//)?
|
|
//$(
|
|
//$(fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
|
//let cb = (|$ao_arg:&'a Self|$ao_impl);
|
|
//cb(self)
|
|
//})?
|
|
//)?
|
|
//)? $(
|
|
//$(
|
|
//$(fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
|
//let cb = (|$mi_arg:&'a Self|$mi_impl);
|
|
//cb(self)
|
|
//})?
|
|
//)?
|
|
//$(
|
|
//$(fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
|
//let cb = (|$mo_arg:&'a Self|$mo_impl);
|
|
//cb(self)
|
|
//})?
|
|
//)?
|
|
//)?}
|
|
//};
|
|
//}
|
|
|
|
/// Trait for things that may expose JACK ports.
|
|
pub trait Ports {
|
|
fn audio_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
|
Ok(vec![])
|
|
}
|
|
fn audio_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
|
Ok(vec![])
|
|
}
|
|
fn midi_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
|
Ok(vec![])
|
|
}
|
|
fn midi_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
|
Ok(vec![])
|
|
}
|
|
}
|
|
|
|
fn register_ports<T: PortSpec + Copy>(
|
|
client: &Client,
|
|
names: Vec<String>,
|
|
spec: T,
|
|
) -> Usually<BTreeMap<String, Port<T>>> {
|
|
names
|
|
.into_iter()
|
|
.try_fold(BTreeMap::new(), |mut ports, name| {
|
|
let port = client.register_port(&name, spec)?;
|
|
ports.insert(name, port);
|
|
Ok(ports)
|
|
})
|
|
}
|
|
|
|
fn query_ports(client: &Client, names: Vec<String>) -> BTreeMap<String, Port<Unowned>> {
|
|
names.into_iter().fold(BTreeMap::new(), |mut ports, name| {
|
|
let port = client.port_by_name(&name).unwrap();
|
|
ports.insert(name, port);
|
|
ports
|
|
})
|
|
}
|
|
|