mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-09 05:06:43 +01:00
clone ports as unowned and pass outwards
This commit is contained in:
parent
394355331d
commit
ddaf870271
9 changed files with 123 additions and 112 deletions
101
src/core/jack.rs
101
src/core/jack.rs
|
|
@ -1,24 +1,22 @@
|
|||
use crate::core::*;
|
||||
|
||||
pub struct Jack {
|
||||
pub client: JackClient,
|
||||
pub client: Client,
|
||||
pub ports: JackPorts,
|
||||
}
|
||||
|
||||
pub struct JackDevice {
|
||||
pub client: DynamicAsyncClient,
|
||||
pub state: Arc<Mutex<Box<dyn Device>>>,
|
||||
pub ports: UnownedJackPorts,
|
||||
}
|
||||
|
||||
pub enum JackClient {
|
||||
Active(DynamicAsyncClient),
|
||||
Inactive(Client),
|
||||
}
|
||||
|
||||
#[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>>,
|
||||
}
|
||||
|
||||
ports!(Jack {
|
||||
ports!(JackDevice {
|
||||
audio: {
|
||||
ins: |s|Ok(s.ports.audio_ins.values().collect()),
|
||||
outs: |s|Ok(s.ports.audio_outs.values().collect()),
|
||||
|
|
@ -32,7 +30,33 @@ ports!(Jack {
|
|||
impl Jack {
|
||||
pub fn new (name: &str) -> Usually<Self> {
|
||||
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
Ok(Self { client: JackClient::Inactive(client), ports: JackPorts::default(), })
|
||||
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();
|
||||
|
|
@ -52,61 +76,25 @@ impl Jack {
|
|||
Ok(jack)
|
||||
}
|
||||
pub fn register_midi_out (mut self, name: &str) -> Usually<Self> {
|
||||
self.ports.midi_outs.insert(name.to_string(), match &self.client {
|
||||
JackClient::Inactive(c) => c.register_port(name, MidiOut::default())?,
|
||||
JackClient::Active(c) => c.as_client().register_port(name, MidiOut::default())?,
|
||||
});
|
||||
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> {
|
||||
self.ports.midi_ins.insert(name.to_string(), match &self.client {
|
||||
JackClient::Inactive(c) => c.register_port(name, MidiIn::default())?,
|
||||
JackClient::Active(c) => c.as_client().register_port(name, MidiIn::default())?,
|
||||
});
|
||||
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> {
|
||||
self.ports.audio_outs.insert(name.to_string(), match &self.client {
|
||||
JackClient::Inactive(c) => c.register_port(name, AudioOut::default())?,
|
||||
JackClient::Active(c) => c.as_client().register_port(name, AudioOut::default())?,
|
||||
});
|
||||
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> {
|
||||
self.ports.audio_ins.insert(name.to_string(), match &self.client {
|
||||
JackClient::Inactive(c) => c.register_port(name, AudioIn::default())?,
|
||||
JackClient::Active(c) => c.as_client().register_port(name, AudioIn::default())?,
|
||||
});
|
||||
let port = self.client.register_port(name, AudioIn::default())?;
|
||||
self.ports.audio_ins.insert(name.to_string(), port);
|
||||
Ok(self)
|
||||
}
|
||||
pub fn run <T> (
|
||||
mut self, state: impl FnOnce(JackPorts)->Box<T>
|
||||
) -> Usually<Arc<Mutex<Box<dyn Device>>>> where
|
||||
T: Device + Process + Sized + 'static
|
||||
{
|
||||
let mut ports = JackPorts::default();
|
||||
std::mem::swap(&mut self.ports, &mut ports);
|
||||
let state = Arc::new(Mutex::new(state(ports) as Box<dyn Device>));
|
||||
self.client = match self.client {
|
||||
JackClient::Active(_) => panic!("already running"),
|
||||
JackClient::Inactive(client) => JackClient::Active(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(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn jack_run <T> (name: &str, app: &Arc<Mutex<T>>) -> Usually<DynamicAsyncClient>
|
||||
|
|
@ -192,7 +180,8 @@ pub use ::jack::{
|
|||
RawMidi,
|
||||
Transport,
|
||||
TransportState,
|
||||
TransportStatePosition
|
||||
TransportStatePosition,
|
||||
Unowned
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,60 @@
|
|||
use crate::core::*;
|
||||
|
||||
#[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<AudioIn>>> {
|
||||
fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<AudioOut>>> {
|
||||
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<MidiIn>>> {
|
||||
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<MidiOut>>> {
|
||||
fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
|
@ -26,26 +69,26 @@ pub trait Ports {
|
|||
})?})?) => {
|
||||
impl Ports for $T {$(
|
||||
$(
|
||||
$(fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<AudioIn>>> {
|
||||
$(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<AudioOut>>> {
|
||||
$(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<MidiIn>>> {
|
||||
$(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<MidiOut>>> {
|
||||
$(fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
|
||||
let cb = (|$mo_arg:&'a Self|$mo_impl);
|
||||
cb(self)
|
||||
})?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue