mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
refactor jack ports again
This commit is contained in:
parent
c13eff95ca
commit
6c8f85ab84
16 changed files with 558 additions and 609 deletions
|
|
@ -1,279 +1,56 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct JackPort<T: PortSpec> {
|
||||
/// Handle to JACK client, for receiving reconnect events.
|
||||
pub jack: Arc<RwLock<JackConnection>>,
|
||||
/// Port name
|
||||
pub name: Arc<str>,
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
macro_rules! impl_port {
|
||||
($Name:ident $Spec:ident |$jack:ident, $name:ident|$port:expr) => {
|
||||
#[derive(Debug)] pub struct $Name {
|
||||
/// Handle to JACK client, for receiving reconnect events.
|
||||
pub jack: JackClient,
|
||||
/// Port name
|
||||
pub name: Arc<str>,
|
||||
/// Port handle.
|
||||
pub port: Port<$Spec>,
|
||||
/// List of ports to connect to.
|
||||
pub conn: Vec<PortConnection>
|
||||
}
|
||||
impl AsRef<Port<$Spec>> for $Name { fn as_ref (&self) -> &Port<$Spec> { &self.port } }
|
||||
impl $Name {
|
||||
pub fn new ($jack: &JackClient, name: impl AsRef<str>, connect: &[PortConnection])
|
||||
-> Usually<Self>
|
||||
{
|
||||
let $name = name.as_ref();
|
||||
let mut port = Self {
|
||||
jack: $jack.clone(),
|
||||
port: $port?,
|
||||
name: $name.into(),
|
||||
conn: connect.to_vec()
|
||||
};
|
||||
port.connect_to_matching()?;
|
||||
Ok(port)
|
||||
}
|
||||
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
|
||||
impl JackPortConnect<$Spec> for $Name {
|
||||
fn jack (&self) -> &JackClient { &self.jack }
|
||||
fn port (&self) -> &Port<$Spec> { &self.port }
|
||||
fn conn (&self) -> &[PortConnection] { self.conn.as_slice() }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
#[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_port! { JackAudioIn AudioIn |jack, name|jack.audio_in(name) }
|
||||
impl_port! { JackAudioOut AudioOut |jack, name|jack.audio_out(name) }
|
||||
impl_port! { JackMidiIn MidiIn |jack, name|jack.midi_in(name) }
|
||||
impl_port! { JackMidiOut MidiOut |jack, name|jack.midi_out(name) }
|
||||
|
||||
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].
|
||||
/// `Arc<RwLock<JackClient>>` for terse port registration in the
|
||||
/// `init` callback of [jack::Client::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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue