refactor: jack proto-lib

This commit is contained in:
🪞👃🪞 2024-07-04 15:32:41 +03:00
parent 4aadc712b8
commit fe6ffea5df
12 changed files with 467 additions and 441 deletions

29
src/jack/device.rs Normal file
View file

@ -0,0 +1,29 @@
use super::*;
/// A Device bound to a JACK client and a set of ports.
pub struct JackDevice {
/// The active JACK client of this device.
pub client: DynamicAsyncClient,
/// The device state, encapsulated for sharing between threads.
pub state: Arc<Mutex<Box<dyn Device>>>,
/// 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,
}
ports!(JackDevice {
audio: {
ins: |s|Ok(s.ports.audio_ins.values().collect()),
outs: |s|Ok(s.ports.audio_outs.values().collect()),
}
midi: {
ins: |s|Ok(s.ports.midi_ins.values().collect()),
outs: |s|Ok(s.ports.midi_outs.values().collect()),
}
});
impl JackDevice {
/// Returns a locked mutex of the state's contents.
pub fn state (&self) -> MutexGuard<Box<dyn Device>> {
self.state.lock().unwrap()
}
}

64
src/jack/event.rs Normal file
View file

@ -0,0 +1,64 @@
use super::*;
#[derive(Debug)]
pub enum JackEvent {
ThreadInit,
Shutdown(ClientStatus, String),
Freewheel(bool),
SampleRate(Frames),
ClientRegistration(String, bool),
PortRegistration(PortId, bool),
PortRename(PortId, String, String),
PortsConnected(PortId, PortId, bool),
GraphReorder,
XRun,
}
pub struct Notifications<T: Fn(AppEvent) + Send>(pub T);
impl<T: Fn(AppEvent) + Send> NotificationHandler for Notifications<T> {
fn thread_init (&self, _: &Client) {
self.0(AppEvent::Jack(JackEvent::ThreadInit));
}
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
self.0(AppEvent::Jack(JackEvent::Shutdown(status, reason.into())));
}
fn freewheel (&mut self, _: &Client, enabled: bool) {
self.0(AppEvent::Jack(JackEvent::Freewheel(enabled)));
}
fn sample_rate (&mut self, _: &Client, frames: Frames) -> Control {
self.0(AppEvent::Jack(JackEvent::SampleRate(frames)));
Control::Quit
}
fn client_registration (&mut self, _: &Client, name: &str, reg: bool) {
self.0(AppEvent::Jack(JackEvent::ClientRegistration(name.into(), reg)));
}
fn port_registration (&mut self, _: &Client, id: PortId, reg: bool) {
self.0(AppEvent::Jack(JackEvent::PortRegistration(id, reg)));
}
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
self.0(AppEvent::Jack(JackEvent::PortRename(id, old.into(), new.into())));
Control::Continue
}
fn ports_connected (&mut self, _: &Client, a: PortId, b: PortId, are: bool) {
self.0(AppEvent::Jack(JackEvent::PortsConnected(a, b, are)));
}
fn graph_reorder (&mut self, _: &Client) -> Control {
self.0(AppEvent::Jack(JackEvent::GraphReorder));
Control::Continue
}
fn xrun (&mut self, _: &Client) -> Control {
self.0(AppEvent::Jack(JackEvent::XRun));
Control::Continue
}
}

78
src/jack/factory.rs Normal file
View file

@ -0,0 +1,78 @@
use super::*;
/// `JackDevice` factory. Creates JACK `Client`s, performs port registration
/// and activation, and encapsulates a `Device` into a `JackDevice`.
pub struct Jack {
pub client: Client,
pub ports: JackPorts,
}
impl Jack {
pub fn new (name: &str) -> Usually<Self> {
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
Ok(Self { client, ports: JackPorts::default() })
}
pub fn run <T: Device + Process + Sized + 'static> (
mut self, state: impl FnOnce(JackPorts)->Box<T>
)
-> Usually<JackDevice>
{
let mut owned_ports = JackPorts::default();
std::mem::swap(&mut self.ports, &mut owned_ports);
let unowned_ports = owned_ports.clone_unowned(&self.client);
let state = Arc::new(Mutex::new(state(owned_ports) as Box<dyn Device>));
let client = self.client.activate_async(
Notifications(Box::new({
let _state = state.clone();
move|_event|{
// FIXME: this deadlocks
//state.lock().unwrap().handle(&event).unwrap();
}
}) as Box<dyn Fn(AppEvent) + Send + Sync>),
ClosureProcessHandler::new(Box::new({
let state = state.clone();
move|c: &Client, s: &ProcessScope|{
state.lock().unwrap().process(c, s)
}
}) as BoxedProcessHandler)
)?;
Ok(JackDevice { client, state, ports: unowned_ports })
}
pub fn ports_from_lv2 (self, plugin: &::livi::Plugin) -> Usually<Self> {
let counts = plugin.port_counts();
let mut jack = self;
for i in 0..counts.atom_sequence_inputs {
jack = jack.register_midi_in(&format!("midi-in-{i}"))?
}
for i in 0..counts.atom_sequence_outputs {
jack = jack.register_midi_out(&format!("midi-out-{i}"))?;
}
for i in 0..counts.audio_inputs {
jack = jack.register_audio_in(&format!("audio-in-{i}"))?
}
for i in 0..counts.audio_outputs {
jack = jack.register_audio_out(&format!("audio-out-{i}"))?;
}
Ok(jack)
}
pub fn register_midi_out (mut self, name: &str) -> Usually<Self> {
let port = self.client.register_port(name, MidiOut::default())?;
self.ports.midi_outs.insert(name.to_string(), port);
Ok(self)
}
pub fn register_midi_in (mut self, name: &str) -> Usually<Self> {
let port = self.client.register_port(name, MidiIn::default())?;
self.ports.midi_ins.insert(name.to_string(), port);
Ok(self)
}
pub fn register_audio_out (mut self, name: &str) -> Usually<Self> {
let port = self.client.register_port(name, AudioOut::default())?;
self.ports.audio_outs.insert(name.to_string(), port);
Ok(self)
}
pub fn register_audio_in (mut self, name: &str) -> Usually<Self> {
let port = self.client.register_port(name, AudioIn::default())?;
self.ports.audio_ins.insert(name.to_string(), port);
Ok(self)
}
}

98
src/jack/ports.rs Normal file
View file

@ -0,0 +1,98 @@
use super::*;
#[derive(Default)]
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>>,
}
#[derive(Default)]
pub struct UnownedJackPorts {
pub audio_ins: BTreeMap<String, Port<::jack::Unowned>>,
pub midi_ins: BTreeMap<String, Port<::jack::Unowned>>,
pub audio_outs: BTreeMap<String, Port<::jack::Unowned>>,
pub midi_outs: BTreeMap<String, Port<::jack::Unowned>>,
}
impl JackPorts {
pub fn clone_unowned (&self, client: &Client) -> UnownedJackPorts {
let mut unowned = UnownedJackPorts::default();
for (name, port) in self.midi_ins.iter() {
unowned.midi_ins.insert(name.clone(), unsafe {
Port::from_raw(::jack::Unowned, client.raw(), port.raw(), Arc::downgrade(&Arc::default()))
});
}
for (name, port) in self.midi_outs.iter() {
unowned.midi_outs.insert(name.clone(), unsafe {
Port::from_raw(::jack::Unowned, client.raw(), port.raw(), Arc::downgrade(&Arc::default()))
});
}
for (name, port) in self.audio_ins.iter() {
unowned.audio_ins.insert(name.clone(), unsafe {
Port::from_raw(::jack::Unowned, client.raw(), port.raw(), Arc::downgrade(&Arc::default()))
});
}
for (name, port) in self.audio_outs.iter() {
unowned.audio_outs.insert(name.clone(), unsafe {
Port::from_raw(::jack::Unowned, client.raw(), port.raw(), Arc::downgrade(&Arc::default()))
});
}
unowned
}
}
/// Trait for things that may expose JACK ports.
pub trait Ports {
fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
}
#[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)
})?
)?
)?}
};
}

30
src/jack/process.rs Normal file
View file

@ -0,0 +1,30 @@
use super::*;
/// Trait for things that have a JACK process callback.
pub trait Process {
fn process (&mut self, _: &Client, _: &ProcessScope) -> Control {
Control::Continue
}
}
/// Define the JACK process callback associated with a struct.
#[macro_export] macro_rules! process {
($T:ty) => {
impl Process for $T {}
};
($T:ty |$self:ident, $c:ident, $s:ident|$block:tt) => {
impl Process for $T {
fn process (&mut $self, $c: &Client, $s: &ProcessScope) -> Control {
$block
}
}
};
($T:ty = $process:path) => {
impl Process for $T {
fn process (&mut self, c: &Client, s: &ProcessScope) -> Control {
$process(self, c, s)
}
}
}
}