mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
wip: implementing jack port autoconnection
This commit is contained in:
parent
b995f81a26
commit
c23f52c87b
4 changed files with 246 additions and 155 deletions
86
jack/src/jack_device.rs
Normal file
86
jack/src/jack_device.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
use crate::*
|
||||
|
||||
/// A [AudioComponent] bound to a JACK client and a set of ports.
|
||||
pub struct JackDevice<E: Engine> {
|
||||
/// The active JACK client of this device.
|
||||
pub client: DynamicAsyncClient,
|
||||
/// The device state, encapsulated for sharing between threads.
|
||||
pub state: Arc<RwLock<Box<dyn AudioComponent<E>>>>,
|
||||
/// Unowned copies of the device's JACK ports, for connecting to the device.
|
||||
/// The "real" readable/writable `Port`s are owned by the `state`.
|
||||
pub ports: UnownedJackPorts,
|
||||
}
|
||||
|
||||
impl<E: Engine> std::fmt::Debug for JackDevice<E> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("JackDevice")
|
||||
.field("ports", &self.ports)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Render for JackDevice<E> {
|
||||
type Engine = E;
|
||||
fn min_size(&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.state.read().unwrap().layout(to)
|
||||
}
|
||||
fn render(&self, to: &mut E::Output) -> Usually<()> {
|
||||
self.state.read().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Handle<E> for JackDevice<E> {
|
||||
fn handle(&mut self, from: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.state.write().unwrap().handle(from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Ports for JackDevice<E> {
|
||||
fn audio_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
Ok(self.ports.audio_ins.values().collect())
|
||||
}
|
||||
fn audio_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
Ok(self.ports.audio_outs.values().collect())
|
||||
}
|
||||
fn midi_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
Ok(self.ports.midi_ins.values().collect())
|
||||
}
|
||||
fn midi_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
Ok(self.ports.midi_outs.values().collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> JackDevice<E> {
|
||||
/// Returns a locked mutex of the state's contents.
|
||||
pub fn state(&self) -> LockResult<RwLockReadGuard<Box<dyn AudioComponent<E>>>> {
|
||||
self.state.read()
|
||||
}
|
||||
/// Returns a locked mutex of the state's contents.
|
||||
pub fn state_mut(&self) -> LockResult<RwLockWriteGuard<Box<dyn AudioComponent<E>>>> {
|
||||
self.state.write()
|
||||
}
|
||||
pub fn connect_midi_in(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
Ok(self
|
||||
.client
|
||||
.as_client()
|
||||
.connect_ports(port, self.midi_ins()?[index])?)
|
||||
}
|
||||
pub fn connect_midi_out(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
Ok(self
|
||||
.client
|
||||
.as_client()
|
||||
.connect_ports(self.midi_outs()?[index], port)?)
|
||||
}
|
||||
pub fn connect_audio_in(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
Ok(self
|
||||
.client
|
||||
.as_client()
|
||||
.connect_ports(port, self.audio_ins()?[index])?)
|
||||
}
|
||||
pub fn connect_audio_out(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
Ok(self
|
||||
.client
|
||||
.as_client()
|
||||
.connect_ports(self.audio_outs()?[index], port)?)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,84 +1,165 @@
|
|||
use crate::*;
|
||||
|
||||
pub struct JackPort<T: PortSpec> {
|
||||
pub port: Port<T>,
|
||||
/// 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>
|
||||
}
|
||||
|
||||
impl<T: PortSpec> JackPort<T> {
|
||||
//pub fn new (jack: &impl RegisterPort
|
||||
pub struct PortConnect {
|
||||
pub name: PortConnectName,
|
||||
pub order: PortConnectScope,
|
||||
pub status: Vec<(Port<Unowned>, PortConnectStatus)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum PortConnect {
|
||||
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![] }
|
||||
}
|
||||
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![] }
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum PortConnectName {
|
||||
/// Exact match
|
||||
Exact(Arc<str>),
|
||||
/// Match wildcard
|
||||
Wildcard(Arc<str>),
|
||||
/// Match regular expression
|
||||
RegExp(Arc<str>),
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum PortConnectScope { One, All }
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum PortConnectStatus { 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() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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>, connect: &[impl AsRef<str>]) -> Usually<Port<MidiIn>>;
|
||||
fn midi_out (&self, name: impl AsRef<str>, connect: &[impl AsRef<str>]) -> Usually<Port<MidiOut>>;
|
||||
fn audio_in (&self, name: impl AsRef<str>, connect: &[impl AsRef<str>]) -> Usually<Port<AudioIn>>;
|
||||
fn audio_out (&self, name: impl AsRef<str>, connect: &[impl AsRef<str>]) -> Usually<Port<AudioOut>>;
|
||||
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 Arc<RwLock<JackConnection>> {
|
||||
fn midi_in (&self, name: impl AsRef<str>, connect: &[impl AsRef<str>]) -> Usually<Port<MidiIn>> {
|
||||
let jack = self.read().unwrap();
|
||||
let input = jack.client().register_port(name.as_ref(), MidiIn::default())?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(output) = jack.port_by_name(port).as_ref() {
|
||||
jack.client().connect_ports(output, &input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(input)
|
||||
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>, connect: &[impl AsRef<str>]) -> Usually<Port<MidiOut>> {
|
||||
let jack = self.read().unwrap();
|
||||
let output = jack.client().register_port(name.as_ref(), MidiOut::default())?;
|
||||
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(output)
|
||||
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>, connect: &[impl AsRef<str>]) -> Usually<Port<AudioIn>> {
|
||||
let jack = self.read().unwrap();
|
||||
let input = jack.client().register_port(name.as_ref(), AudioIn::default())?;
|
||||
for port in connect.iter() {
|
||||
let port = port.as_ref();
|
||||
if let Some(output) = jack.port_by_name(port).as_ref() {
|
||||
jack.client().connect_ports(output, &input)?;
|
||||
} else {
|
||||
panic!("Missing MIDI output: {port}. Use jack_lsp to list all port names.");
|
||||
}
|
||||
}
|
||||
Ok(input)
|
||||
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>, connect: &[impl AsRef<str>]) -> Usually<Port<AudioOut>> {
|
||||
let jack = self.read().unwrap();
|
||||
let output = jack.client().register_port(name.as_ref(), AudioOut::default())?;
|
||||
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(output)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -131,15 +212,24 @@ pub trait ConnectPort {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectPort for Arc<RwLock<JackConnection>> {
|
||||
impl ConnectPort for JackConnection {
|
||||
fn port_by_name (&self, name: impl AsRef<str>) -> Option<Port<Unowned>> {
|
||||
self.read().unwrap().client().port_by_name(name.as_ref())
|
||||
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.read().unwrap().client().connect_ports(source, target)?)
|
||||
Ok(self.client().connect_ports(source, target)?)
|
||||
}
|
||||
}
|
||||
impl<T: ConnectPort> ConnectPort for Arc<RwLock<T>> {
|
||||
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)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,101 +9,16 @@ pub(crate) use ::jack::{
|
|||
Port, PortId, PortSpec, Unowned, MidiIn, MidiOut, AudioIn, AudioOut,
|
||||
};
|
||||
|
||||
mod from_jack; pub use self::from_jack::*;
|
||||
mod jack_audio; pub use self::jack_audio::*;
|
||||
mod jack_connection; pub use self::jack_connection::*;
|
||||
mod jack_event; pub use self::jack_event::*;
|
||||
mod jack_port; pub use self::jack_port::*;
|
||||
mod from_jack; pub use self::from_jack::*;
|
||||
mod jack_audio; pub use self::jack_audio::*;
|
||||
mod jack_connect; pub use self::jack_connect::*;
|
||||
mod jack_event; pub use self::jack_event::*;
|
||||
mod jack_port; pub use self::jack_port::*;
|
||||
|
||||
pub(crate) type Usually<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///// A [AudioComponent] bound to a JACK client and a set of ports.
|
||||
//pub struct JackDevice<E: Engine> {
|
||||
///// The active JACK client of this device.
|
||||
//pub client: DynamicAsyncClient,
|
||||
///// The device state, encapsulated for sharing between threads.
|
||||
//pub state: Arc<RwLock<Box<dyn AudioComponent<E>>>>,
|
||||
///// Unowned copies of the device's JACK ports, for connecting to the device.
|
||||
///// The "real" readable/writable `Port`s are owned by the `state`.
|
||||
//pub ports: UnownedJackPorts,
|
||||
//}
|
||||
|
||||
//impl<E: Engine> std::fmt::Debug for JackDevice<E> {
|
||||
//fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
//f.debug_struct("JackDevice")
|
||||
//.field("ports", &self.ports)
|
||||
//.finish()
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<E: Engine> Render for JackDevice<E> {
|
||||
//type Engine = E;
|
||||
//fn min_size(&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
//self.state.read().unwrap().layout(to)
|
||||
//}
|
||||
//fn render(&self, to: &mut E::Output) -> Usually<()> {
|
||||
//self.state.read().unwrap().render(to)
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<E: Engine> Handle<E> for JackDevice<E> {
|
||||
//fn handle(&mut self, from: &E::Input) -> Perhaps<E::Handled> {
|
||||
//self.state.write().unwrap().handle(from)
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<E: Engine> Ports for JackDevice<E> {
|
||||
//fn audio_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
//Ok(self.ports.audio_ins.values().collect())
|
||||
//}
|
||||
//fn audio_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
//Ok(self.ports.audio_outs.values().collect())
|
||||
//}
|
||||
//fn midi_ins(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
//Ok(self.ports.midi_ins.values().collect())
|
||||
//}
|
||||
//fn midi_outs(&self) -> Usually<Vec<&Port<Unowned>>> {
|
||||
//Ok(self.ports.midi_outs.values().collect())
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<E: Engine> JackDevice<E> {
|
||||
///// Returns a locked mutex of the state's contents.
|
||||
//pub fn state(&self) -> LockResult<RwLockReadGuard<Box<dyn AudioComponent<E>>>> {
|
||||
//self.state.read()
|
||||
//}
|
||||
///// Returns a locked mutex of the state's contents.
|
||||
//pub fn state_mut(&self) -> LockResult<RwLockWriteGuard<Box<dyn AudioComponent<E>>>> {
|
||||
//self.state.write()
|
||||
//}
|
||||
//pub fn connect_midi_in(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
//Ok(self
|
||||
//.client
|
||||
//.as_client()
|
||||
//.connect_ports(port, self.midi_ins()?[index])?)
|
||||
//}
|
||||
//pub fn connect_midi_out(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
//Ok(self
|
||||
//.client
|
||||
//.as_client()
|
||||
//.connect_ports(self.midi_outs()?[index], port)?)
|
||||
//}
|
||||
//pub fn connect_audio_in(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
//Ok(self
|
||||
//.client
|
||||
//.as_client()
|
||||
//.connect_ports(port, self.audio_ins()?[index])?)
|
||||
//}
|
||||
//pub fn connect_audio_out(&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
|
||||
//Ok(self
|
||||
//.client
|
||||
//.as_client()
|
||||
//.connect_ports(self.audio_outs()?[index], port)?)
|
||||
//}
|
||||
//}
|
||||
|
||||
///// `JackDevice` factory. Creates JACK `Client`s, performs port registration
|
||||
///// and activation, and encapsulates a `AudioComponent` into a `JackDevice`.
|
||||
//pub struct Jack {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue