mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
wip: enabling autoconnecting ports
This commit is contained in:
parent
c23f52c87b
commit
fe70b57dc1
10 changed files with 375 additions and 424 deletions
|
|
@ -1,169 +1,151 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct JackPort<T: PortSpec> {
|
||||
/// 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<PortConnect>
|
||||
pub connect: Vec<PortConnection>
|
||||
}
|
||||
pub struct PortConnect {
|
||||
pub name: PortConnectName,
|
||||
pub order: PortConnectScope,
|
||||
pub status: Vec<(Port<Unowned>, PortConnectStatus)>,
|
||||
}
|
||||
impl PortConnect {
|
||||
/// Connect to this exact port
|
||||
pub fn exact (name: impl AsRef<str>) -> Self {
|
||||
let name = PortConnectName::Exact(name.as_ref().into());
|
||||
Self { name, order: PortConnectScope::One, status: vec![] }
|
||||
impl<T: PortSpec> JackPort<T> {
|
||||
pub fn connect_to_matching (&mut self) {
|
||||
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);
|
||||
status.push((port, 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);
|
||||
status.push((port, port_status));
|
||||
if port_status == Connected && connect.scope == One {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
connect.status = status
|
||||
}
|
||||
}
|
||||
pub fn wildcard (name: impl AsRef<str>) -> Self {
|
||||
let name = PortConnectName::Wildcard(name.as_ref().into());
|
||||
Self { name, order: PortConnectScope::One, status: vec![] }
|
||||
}
|
||||
pub fn wildcard_all (name: impl AsRef<str>) -> Self {
|
||||
let name = PortConnectName::Wildcard(name.as_ref().into());
|
||||
Self { name, order: PortConnectScope::All, status: vec![] }
|
||||
}
|
||||
pub fn regexp (name: impl AsRef<str>) -> Self {
|
||||
let name = PortConnectName::RegExp(name.as_ref().into());
|
||||
Self { name, order: PortConnectScope::One, status: vec![] }
|
||||
}
|
||||
pub fn regexp_all (name: impl AsRef<str>) -> Self {
|
||||
let name = PortConnectName::RegExp(name.as_ref().into());
|
||||
Self { name, order: PortConnectScope::All, status: vec![] }
|
||||
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 enum PortConnectName {
|
||||
/// Exact match
|
||||
Exact(Arc<str>),
|
||||
/// Match wildcard
|
||||
Wildcard(Arc<str>),
|
||||
/// Match regular expression
|
||||
RegExp(Arc<str>),
|
||||
pub struct PortConnection {
|
||||
pub name: PortConnectionName,
|
||||
pub scope: PortConnectionScope,
|
||||
pub status: Vec<(Port<Unowned>, PortConnectionStatus)>,
|
||||
}
|
||||
impl PortConnection {
|
||||
/// 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![] }
|
||||
}
|
||||
}
|
||||
#[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 PortConnectScope { One, All }
|
||||
pub enum PortConnectionScope { One, All }
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum PortConnectStatus { Missing, Disconnected, Connected, Mismatch, }
|
||||
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<T: PortSpec> JackPort<T> {
|
||||
pub fn midi_in (
|
||||
jack: Arc<RwLock<JackConnection>>, name: impl AsRef<str>, connect: &[PortConnect]
|
||||
) -> Usually<JackPort<MidiIn>> {
|
||||
let input = jack.midi_in(name)?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(output) = jack.port_by_name(port).as_ref() {
|
||||
jack.connect_ports(output, &input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(JackPort { jack: jack.clone(), port: input, connect: connect.clone() })
|
||||
}
|
||||
|
||||
pub fn midi_out (
|
||||
jack: Arc<RwLock<JackConnection>>,
|
||||
name: impl AsRef<str>,
|
||||
connect: &[impl AsRef<str>]
|
||||
) -> Usually<JackPort<MidiOut>> {
|
||||
let output = jack.midi_out(name)?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(input) = jack.port_by_name(port).as_ref() {
|
||||
jack.connect_ports(&output, input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI input: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(JackPort { jack: jack.clone(), port: output, connect: connect.into() })
|
||||
}
|
||||
|
||||
pub fn audio_in (
|
||||
jack: Arc<RwLock<JackConnection>>,
|
||||
name: impl AsRef<str>,
|
||||
connect: &[impl AsRef<str>]
|
||||
) -> Usually<JackPort<AudioIn>> {
|
||||
let input = jack.audio_in(name)?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(output) = jack.port_by_name(port).as_ref() {
|
||||
jack.connect_ports(output, &input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(JackPort { jack: jack.clone(), port: input, connect: connect.into() })
|
||||
}
|
||||
|
||||
pub fn audio_out (
|
||||
jack: Arc<RwLock<JackConnection>>,
|
||||
name: impl AsRef<str>,
|
||||
connect: &[impl AsRef<str>]
|
||||
) -> Usually<JackPort<AudioOut>> {
|
||||
let output = jack.audio_out(name)?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(input) = jack.port_by_name(port).as_ref() {
|
||||
jack.client().connect_ports(&output, input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI input: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(JackPort { jack: jack.clone(), port: output, connect: connect.into() })
|
||||
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)?,
|
||||
connect: connect.to_vec()
|
||||
};
|
||||
port.connect_to_matching();
|
||||
Ok(port)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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 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)?,
|
||||
connect: connect.to_vec()
|
||||
};
|
||||
port.connect_to_matching();
|
||||
Ok(port)
|
||||
}
|
||||
}
|
||||
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)
|
||||
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)?,
|
||||
connect: connect.to_vec()
|
||||
};
|
||||
port.connect_to_matching();
|
||||
Ok(port)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)?,
|
||||
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<()>;
|
||||
|
|
@ -213,6 +195,9 @@ pub trait ConnectPort {
|
|||
}
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
|
@ -223,6 +208,9 @@ impl ConnectPort for JackConnection {
|
|||
}
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
|
@ -233,6 +221,45 @@ impl<T: ConnectPort> ConnectPort for Arc<RwLock<T>> {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ pub(crate) use ::jack::{
|
|||
contrib::ClosureProcessHandler, NotificationHandler,
|
||||
Client, AsyncClient, ClientOptions, ClientStatus,
|
||||
ProcessScope, Control, Frames,
|
||||
Port, PortId, PortSpec, Unowned, MidiIn, MidiOut, AudioIn, AudioOut,
|
||||
Port, PortId, PortSpec, PortFlags,
|
||||
Unowned, MidiIn, MidiOut, AudioIn, AudioOut,
|
||||
};
|
||||
|
||||
mod from_jack; pub use self::from_jack::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue