mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
refactor jack ports again
This commit is contained in:
parent
c13eff95ca
commit
6c8f85ab84
16 changed files with 558 additions and 609 deletions
150
jack/src/jack_client.rs
Normal file
150
jack/src/jack_client.rs
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
use crate::*;
|
||||
use self::JackClientState::*;
|
||||
|
||||
/// Wraps [JackClientState] and through it [jack::Client].
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct JackClient {
|
||||
state: Arc<RwLock<JackClientState>>
|
||||
}
|
||||
impl JackClient {
|
||||
pub fn new (name: &str) -> Usually<Self> {
|
||||
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
Ok(Self { state: Arc::new(RwLock::new(Inactive(client))) })
|
||||
}
|
||||
/// Return the internal [Client] handle that lets you call the JACK API.
|
||||
pub fn inner (&self) -> Client {
|
||||
self.state.read().unwrap().inner()
|
||||
}
|
||||
/// Activate a connection with an application.
|
||||
///
|
||||
/// Consume a `JackClient::Inactive`, binding a process callback and returning a `JackClient::Active`.
|
||||
///
|
||||
/// * [ ] TODO: Needs work. Strange ownership situation between the callback and the host object.
|
||||
fn activate <'a: 'static> (
|
||||
&'a self, mut cb: impl FnMut(JackClient, &Client, &ProcessScope) -> Control + Send + 'a
|
||||
) -> Usually<Self> where Self: Send + Sync + 'a {
|
||||
let client = self.inner();
|
||||
let state = Arc::new(RwLock::new(Activating));
|
||||
let event = Box::new(move|_|{/*TODO*/}) as Box<dyn Fn(JackEvent) + Send + Sync>;
|
||||
let events = Notifications(event);
|
||||
let frame = Box::new(move|c: &_, s: &_|cb(self.clone(), c, s));
|
||||
let frames = ClosureProcessHandler::new(frame as BoxedAudioHandler<'a>);
|
||||
*state.write().unwrap() = Active(client.activate_async(events, frames)?);
|
||||
Ok(Self { state })
|
||||
}
|
||||
/// Activate a connection with an application.
|
||||
///
|
||||
/// * Wrap a [JackClient::Inactive] into [Arc<RwLock<_>>].
|
||||
/// * Pass it to the `init` callback
|
||||
/// * This allows user code to connect to JACK
|
||||
/// * While user code retains clone of the
|
||||
/// [Arc<RwLock<JackClient>>] that is
|
||||
/// passed to `init`, the audio engine is running.
|
||||
pub fn activate_with <'a: 'static, T> (
|
||||
&self, init: impl FnOnce(&JackClient)->Usually<T>
|
||||
) -> Usually<Arc<RwLock<T>>> where T: Audio + 'a {
|
||||
// Run init callback. Return value is target. Target must retain clone of `connection`.
|
||||
let target = Arc::new(RwLock::new(init(&self)?));
|
||||
// Swap the `client` from the `JackClient::Inactive`
|
||||
// for a `JackClient::Activating`.
|
||||
let mut client = Activating;
|
||||
std::mem::swap(&mut*self.state.write().unwrap(), &mut client);
|
||||
// Replace the `JackClient::Activating` with a
|
||||
// `JackClient::Active` wrapping the [AsyncClient]
|
||||
// returned by the activation.
|
||||
*self.state.write().unwrap() = Active(client.inner().activate_async(
|
||||
// This is the misc notifications handler. It's a struct that wraps a [Box]
|
||||
// which performs type erasure on a callback that takes [JackEvent], which is
|
||||
// one of the available misc notifications.
|
||||
Notifications(Box::new(move|_|{/*TODO*/}) as BoxedJackEventHandler),
|
||||
// This is the main processing handler. It's a struct that wraps a [Box]
|
||||
// which performs type erasure on a callback that takes [Client] and [ProcessScope]
|
||||
// and passes them down to the `target`'s `process` callback, which in turn
|
||||
// implements audio and MIDI input and output on a realtime basis.
|
||||
ClosureProcessHandler::new(Box::new({
|
||||
let target = target.clone();
|
||||
move|c: &_, s: &_|if let Ok(mut target) = target.write() {
|
||||
target.process(c, s)
|
||||
} else {
|
||||
Control::Quit
|
||||
}
|
||||
}) as BoxedAudioHandler),
|
||||
)?);
|
||||
Ok(target)
|
||||
}
|
||||
pub fn port_by_name (&self, name: &str) -> Option<Port<Unowned>> {
|
||||
self.inner().port_by_name(name)
|
||||
}
|
||||
pub fn register_port <PS: PortSpec> (&self, name: &str, spec: PS) -> Usually<Port<PS>> {
|
||||
Ok(self.inner().register_port(name, spec)?)
|
||||
}
|
||||
}
|
||||
/// This is a connection which may be [Inactive], [Activating], or [Active].
|
||||
/// In the [Active] and [Inactive] states, [JackClientState::client] returns a
|
||||
/// [jack::Client], which you can use to talk to the JACK API.
|
||||
#[derive(Debug, Default)] enum JackClientState {
|
||||
/// Unused
|
||||
#[default] Inert,
|
||||
/// Before activation.
|
||||
Inactive(Client),
|
||||
/// During activation.
|
||||
Activating,
|
||||
/// After activation. Must not be dropped for JACK thread to persist.
|
||||
Active(DynamicAsyncClient<'static>),
|
||||
}
|
||||
impl JackClientState {
|
||||
pub fn inner (&self) -> Client {
|
||||
match self {
|
||||
Inert => panic!("jack client not activated"),
|
||||
Inactive(ref client) => unsafe { Client::from_raw(client.raw()) },
|
||||
Activating => panic!("jack client has not finished activation"),
|
||||
Active(ref client) => unsafe { Client::from_raw(client.as_client().raw()) },
|
||||
}
|
||||
}
|
||||
}
|
||||
/// This is a boxed realtime callback.
|
||||
pub type BoxedAudioHandler<'j> =
|
||||
Box<dyn FnMut(&Client, &ProcessScope) -> Control + Send + 'j>;
|
||||
/// This is the notification handler wrapper for a boxed realtime callback.
|
||||
pub type DynamicAudioHandler<'j> =
|
||||
ClosureProcessHandler<(), BoxedAudioHandler<'j>>;
|
||||
/// This is a boxed [JackEvent] callback.
|
||||
pub type BoxedJackEventHandler<'j> =
|
||||
Box<dyn Fn(JackEvent) + Send + Sync + 'j>;
|
||||
/// This is the notification handler wrapper for a boxed [JackEvent] callback.
|
||||
pub type DynamicNotifications<'j> =
|
||||
Notifications<BoxedJackEventHandler<'j>>;
|
||||
/// This is a running JACK [AsyncClient] with maximum type erasure.
|
||||
/// It has one [Box] containing a function that handles [JackEvent]s,
|
||||
/// and another [Box] containing a function that handles realtime IO,
|
||||
/// and that's all it knows about them.
|
||||
pub type DynamicAsyncClient<'j>
|
||||
= AsyncClient<DynamicNotifications<'j>, DynamicAudioHandler<'j>>;
|
||||
|
||||
impl RegisterPort for JackClient {
|
||||
fn midi_in (&self, name: impl AsRef<str>) -> Usually<Port<MidiIn>> {
|
||||
Ok(self.inner().register_port(name.as_ref(), MidiIn::default())?)
|
||||
}
|
||||
fn midi_out (&self, name: impl AsRef<str>) -> Usually<Port<MidiOut>> {
|
||||
Ok(self.inner().register_port(name.as_ref(), MidiOut::default())?)
|
||||
}
|
||||
fn audio_in (&self, name: impl AsRef<str>) -> Usually<Port<AudioIn>> {
|
||||
Ok(self.inner().register_port(name.as_ref(), AudioIn::default())?)
|
||||
}
|
||||
fn audio_out (&self, name: impl AsRef<str>) -> Usually<Port<AudioOut>> {
|
||||
Ok(self.inner().register_port(name.as_ref(), AudioOut::default())?)
|
||||
}
|
||||
}
|
||||
impl ConnectPort for JackClient {
|
||||
fn ports (&self, re_name: Option<&str>, re_type: Option<&str>, flags: PortFlags) -> Vec<String> {
|
||||
self.inner().ports(re_name, re_type, flags)
|
||||
}
|
||||
fn port_by_name (&self, name: impl AsRef<str>) -> Option<Port<Unowned>> {
|
||||
self.inner().port_by_name(name.as_ref())
|
||||
}
|
||||
fn connect_ports <A: PortSpec, B: PortSpec> (&self, source: &Port<A>, target: &Port<B>)
|
||||
-> Usually<()>
|
||||
{
|
||||
Ok(self.inner().connect_ports(source, target)?)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue