mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: AudioEngine (new jack init)
This commit is contained in:
parent
98cf8da424
commit
2303b258f6
5 changed files with 108 additions and 80 deletions
|
|
@ -1,6 +1,79 @@
|
|||
use crate::*;
|
||||
use jack::*;
|
||||
|
||||
/// Just run thing with JACK. Returns the activated client.
|
||||
pub fn jack_run<T, E: Engine>(name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicAsyncClient>
|
||||
where
|
||||
T: Handle<E> + Audio + Send + Sync + 'static,
|
||||
{
|
||||
let options = ClientOptions::NO_START_SERVER;
|
||||
let (client, _status) = Client::new(name, options)?;
|
||||
let on_event = Notifications(Box::new({
|
||||
let _app = app.clone();
|
||||
move|_event|{/* FIXME: this deadlocks: app.lock().unwrap().handle(&event).unwrap(); */}
|
||||
}) as Box<dyn Fn(JackEvent) + Send + Sync>);
|
||||
let on_chunk = contrib::ClosureProcessHandler::new(Box::new({
|
||||
let app = app.clone();
|
||||
move|c: &Client, s: &ProcessScope|app.write().unwrap().process(c, s)
|
||||
}) as BoxedAudioHandler);
|
||||
Ok(client.activate_async(on_event, on_chunk)?)
|
||||
}
|
||||
/// Trait for things that wrap a JACK client.
|
||||
pub trait AudioEngine {
|
||||
fn activate(
|
||||
self,
|
||||
process: impl FnMut(&Arc<RwLock<Self>>, &Client, &ProcessScope) -> Control + Send + 'static
|
||||
) -> Usually<Arc<RwLock<Self>>> where Self: Send + Sync + 'static;
|
||||
fn client(&self) -> &Client;
|
||||
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)?)
|
||||
}
|
||||
}
|
||||
/// Wraps [Client] or [DynamicAsyncClient] in place.
|
||||
pub enum JackClient {
|
||||
Inactive(Client),
|
||||
Activating,
|
||||
Active(DynamicAsyncClient),
|
||||
}
|
||||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
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 process: 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: &_|process(&state, c, s)});
|
||||
let frames = contrib::ClosureProcessHandler::new(frame as BoxedAudioHandler);
|
||||
*state.write().unwrap() = Self::Active(client.activate_async(events, frames)?);
|
||||
Ok(state)
|
||||
}
|
||||
}
|
||||
/// Trait for things that have a JACK process callback.
|
||||
pub trait Audio {
|
||||
fn process(&mut self, _: &Client, _: &ProcessScope) -> Control {
|
||||
|
|
@ -47,79 +120,12 @@ pub trait AudioComponent<E: Engine>: Component<E> + Audio {
|
|||
/// All things that implement the required traits can be treated as `AudioComponent`.
|
||||
impl<E: Engine, W: Component<E> + Audio> AudioComponent<E> for W {}
|
||||
|
||||
/// Wraps [Client] or [DynamicAsyncClient] in place.
|
||||
pub enum JackClient {
|
||||
Inactive(Client),
|
||||
Active(DynamicAsyncClient),
|
||||
}
|
||||
|
||||
impl JackClient {
|
||||
pub fn client(&self) -> &Client {
|
||||
match self {
|
||||
Self::Inactive(ref client) => client,
|
||||
Self::Active(ref client) => client.as_client(),
|
||||
}
|
||||
}
|
||||
pub fn transport(&self) -> Transport {
|
||||
self.client().transport()
|
||||
}
|
||||
pub fn port_by_name(&self, name: &str) -> Option<Port<Unowned>> {
|
||||
self.client().port_by_name(name)
|
||||
}
|
||||
pub fn register_port<PS: PortSpec>(&self, name: &str, spec: PS) -> Usually<Port<PS>> {
|
||||
Ok(self.client().register_port(name, spec)?)
|
||||
}
|
||||
pub fn activate<T: Send + Sync + 'static>(
|
||||
self,
|
||||
state: &Arc<RwLock<T>>,
|
||||
mut process: impl FnMut(&Arc<RwLock<T>>, &Client, &ProcessScope) -> Control + Send + 'static,
|
||||
) -> Usually<Self> {
|
||||
Ok(match self {
|
||||
Self::Active(_) => self,
|
||||
Self::Inactive(client) => Self::Active(client.activate_async(
|
||||
Notifications(
|
||||
Box::new(move |_| { /*TODO*/ }) as Box<dyn Fn(JackEvent) + Send + Sync>
|
||||
),
|
||||
contrib::ClosureProcessHandler::new(Box::new({
|
||||
let state = state.clone();
|
||||
move |c: &Client, s: &ProcessScope| process(&state, c, s)
|
||||
}) as BoxedAudioHandler),
|
||||
)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DynamicAsyncClient = AsyncClient<DynamicNotifications, DynamicAudioHandler>;
|
||||
|
||||
type DynamicAudioHandler = contrib::ClosureProcessHandler<(), BoxedAudioHandler>;
|
||||
pub type DynamicAudioHandler = contrib::ClosureProcessHandler<(), BoxedAudioHandler>;
|
||||
|
||||
pub type BoxedAudioHandler = Box<dyn FnMut(&Client, &ProcessScope) -> Control + Send>;
|
||||
|
||||
/// Just run thing with JACK. Returns the activated client.
|
||||
pub fn jack_run<T, E: Engine>(name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicAsyncClient>
|
||||
where
|
||||
T: Handle<E> + Audio + Send + Sync + 'static,
|
||||
{
|
||||
let options = ClientOptions::NO_START_SERVER;
|
||||
let (client, _status) = Client::new(name, options)?;
|
||||
Ok(client.activate_async(
|
||||
Notifications(Box::new({
|
||||
let _app = app.clone();
|
||||
move |_event| {
|
||||
// FIXME: this deadlocks
|
||||
//app.lock().unwrap().handle(&event).unwrap();
|
||||
}
|
||||
}) as Box<dyn Fn(JackEvent) + Send + Sync>),
|
||||
contrib::ClosureProcessHandler::new(Box::new({
|
||||
let app = app.clone();
|
||||
move |c: &Client, s: &ProcessScope| {
|
||||
app.write().unwrap().process(c, s)
|
||||
//Control::Continue
|
||||
}
|
||||
}) as BoxedAudioHandler),
|
||||
)?)
|
||||
}
|
||||
|
||||
/// `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