mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
181 lines
5.6 KiB
Rust
181 lines
5.6 KiB
Rust
use crate::*;
|
|
use jack::*;
|
|
|
|
#[derive(Debug)]
|
|
/// Event enum for JACK events.
|
|
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,
|
|
}
|
|
|
|
/// Wraps [Client] or [DynamicAsyncClient] in place.
|
|
#[derive(Debug)]
|
|
pub enum JackClient {
|
|
/// Before activation.
|
|
Inactive(Client),
|
|
/// During activation.
|
|
Activating,
|
|
/// After activation. Must not be dropped for JACK thread to persist.
|
|
Active(DynamicAsyncClient),
|
|
}
|
|
|
|
/// Trait for things that wrap a JACK client.
|
|
pub trait AudioEngine {
|
|
|
|
fn transport (&self) -> Transport {
|
|
self.client().transport()
|
|
}
|
|
|
|
fn port_by_name (&self, name: &str) -> Option<Port<Unowned>> {
|
|
self.client().port_by_name(name)
|
|
}
|
|
|
|
fn register_port <PS: PortSpec> (&self, name: &str, spec: PS) -> Usually<Port<PS>> {
|
|
Ok(self.client().register_port(name, spec)?)
|
|
}
|
|
|
|
fn client (&self) -> &Client;
|
|
|
|
fn activate (
|
|
self,
|
|
process: impl FnMut(&Arc<RwLock<Self>>, &Client, &ProcessScope) -> Control + Send + 'static
|
|
) -> Usually<Arc<RwLock<Self>>> where Self: Send + Sync + 'static;
|
|
|
|
fn thread_init (&self, _: &Client) {}
|
|
|
|
unsafe fn shutdown (&mut self, status: ClientStatus, reason: &str) {}
|
|
|
|
fn freewheel (&mut self, _: &Client, enabled: bool) {}
|
|
|
|
fn client_registration (&mut self, _: &Client, name: &str, reg: bool) {}
|
|
|
|
fn port_registration (&mut self, _: &Client, id: PortId, reg: bool) {}
|
|
|
|
fn ports_connected (&mut self, _: &Client, a: PortId, b: PortId, are: bool) {}
|
|
|
|
fn sample_rate (&mut self, _: &Client, frames: Frames) -> Control {
|
|
Control::Continue
|
|
}
|
|
|
|
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
|
Control::Continue
|
|
}
|
|
|
|
fn graph_reorder (&mut self, _: &Client) -> Control {
|
|
Control::Continue
|
|
}
|
|
|
|
fn xrun (&mut self, _: &Client) -> Control {
|
|
Control::Continue
|
|
}
|
|
}
|
|
|
|
impl AudioEngine for JackClient {
|
|
fn client(&self) -> &Client {
|
|
match self {
|
|
Self::Inactive(ref client) => client,
|
|
Self::Activating => panic!("jack client has not finished activation"),
|
|
Self::Active(ref client) => client.as_client(),
|
|
}
|
|
}
|
|
fn activate(
|
|
self,
|
|
mut cb: impl FnMut(&Arc<RwLock<Self>>, &Client, &ProcessScope) -> Control + Send + 'static,
|
|
) -> Usually<Arc<RwLock<Self>>>
|
|
where
|
|
Self: Send + Sync + 'static
|
|
{
|
|
let client = Client::from(self);
|
|
let state = Arc::new(RwLock::new(Self::Activating));
|
|
let event = Box::new(move|_|{/*TODO*/}) as Box<dyn Fn(JackEvent) + Send + Sync>;
|
|
let events = Notifications(event);
|
|
let frame = Box::new({let state = state.clone(); move|c: &_, s: &_|cb(&state, c, s)});
|
|
let frames = contrib::ClosureProcessHandler::new(frame as BoxedAudioHandler);
|
|
*state.write().unwrap() = Self::Active(client.activate_async(events, frames)?);
|
|
Ok(state)
|
|
}
|
|
}
|
|
|
|
pub type DynamicAsyncClient = AsyncClient<DynamicNotifications, DynamicAudioHandler>;
|
|
|
|
pub type DynamicAudioHandler = contrib::ClosureProcessHandler<(), BoxedAudioHandler>;
|
|
|
|
pub type BoxedAudioHandler = Box<dyn FnMut(&Client, &ProcessScope) -> Control + Send>;
|
|
|
|
impl JackClient {
|
|
pub fn new (name: &str) -> Usually<Self> {
|
|
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
|
Ok(Self::Inactive(client))
|
|
}
|
|
}
|
|
|
|
impl From<JackClient> for Client {
|
|
fn from (jack: JackClient) -> Client {
|
|
match jack {
|
|
JackClient::Inactive(client) => client,
|
|
JackClient::Activating => panic!("jack client still activating"),
|
|
JackClient::Active(_) => panic!("jack client already activated"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Notification handler used by the [Jack] factory
|
|
/// when constructing [JackDevice]s.
|
|
pub type DynamicNotifications = Notifications<Box<dyn Fn(JackEvent) + Send + Sync>>;
|
|
|
|
/// Generic notification handler that emits [JackEvent]
|
|
pub struct Notifications<T: Fn(JackEvent) + Send>(pub T);
|
|
|
|
impl<T: Fn(JackEvent) + Send> NotificationHandler for Notifications<T> {
|
|
fn thread_init(&self, _: &Client) {
|
|
self.0(JackEvent::ThreadInit);
|
|
}
|
|
|
|
unsafe fn shutdown(&mut self, status: ClientStatus, reason: &str) {
|
|
self.0(JackEvent::Shutdown(status, reason.into()));
|
|
}
|
|
|
|
fn freewheel(&mut self, _: &Client, enabled: bool) {
|
|
self.0(JackEvent::Freewheel(enabled));
|
|
}
|
|
|
|
fn sample_rate(&mut self, _: &Client, frames: Frames) -> Control {
|
|
self.0(JackEvent::SampleRate(frames));
|
|
Control::Quit
|
|
}
|
|
|
|
fn client_registration(&mut self, _: &Client, name: &str, reg: bool) {
|
|
self.0(JackEvent::ClientRegistration(name.into(), reg));
|
|
}
|
|
|
|
fn port_registration(&mut self, _: &Client, id: PortId, reg: bool) {
|
|
self.0(JackEvent::PortRegistration(id, reg));
|
|
}
|
|
|
|
fn port_rename(&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
|
self.0(JackEvent::PortRename(id, old.into(), new.into()));
|
|
Control::Continue
|
|
}
|
|
|
|
fn ports_connected(&mut self, _: &Client, a: PortId, b: PortId, are: bool) {
|
|
self.0(JackEvent::PortsConnected(a, b, are));
|
|
}
|
|
|
|
fn graph_reorder(&mut self, _: &Client) -> Control {
|
|
self.0(JackEvent::GraphReorder);
|
|
Control::Continue
|
|
}
|
|
|
|
fn xrun(&mut self, _: &Client) -> Control {
|
|
self.0(JackEvent::XRun);
|
|
Control::Continue
|
|
}
|
|
}
|