diff --git a/src/jack.rs b/src/jack.rs index 5173607f..5b3a6978 100644 --- a/src/jack.rs +++ b/src/jack.rs @@ -9,10 +9,6 @@ pub use ::jack::{ Transport, TransportState, MidiIter, MidiWriter, RawMidi, }; -pub mod activate; -pub(crate) use self::activate::*; -pub use self::activate::JackActivate; - pub mod jack_event; pub(crate) use self::jack_event::*; @@ -98,7 +94,7 @@ impl JackConnection { /// /// Needs work. Strange ownership situation between the callback /// and the host object. - pub fn activate ( + fn activate ( self, mut cb: impl FnMut(&Arc>, &Client, &ProcessScope) -> Control + Send + 'static, ) -> Usually>> @@ -114,6 +110,36 @@ impl JackConnection { *state.write().unwrap() = Self::Active(client.activate_async(events, frames)?); Ok(state) } + /// Consume a `JackConnection::Inactive`, activate a client, + /// initialize an app around it with the `init` callback, + /// then return the result of it all. + pub fn activate_with ( + self, + init: impl FnOnce(&Arc>)->Usually + ) + -> Usually>> + { + let client = Arc::new(RwLock::new(self)); + let target = Arc::new(RwLock::new(init(&client)?)); + let event = Box::new(move|_|{/*TODO*/}) as Box; + let events = Notifications(event); + let frame = Box::new({ + let target = target.clone(); + move|c: &_, s: &_|if let Ok(mut target) = target.write() { + target.process(c, s) + } else { + Control::Quit + } + }); + let frames = ClosureProcessHandler::new(frame as BoxedAudioHandler); + let mut buffer = Self::Activating; + std::mem::swap(&mut*client.write().unwrap(), &mut buffer); + *client.write().unwrap() = Self::Active(Client::from(buffer).activate_async( + events, + frames, + )?); + Ok(target) + } pub fn port_by_name (&self, name: &str) -> Option> { self.client().port_by_name(name) } diff --git a/src/jack/activate.rs b/src/jack/activate.rs deleted file mode 100644 index 03a7f2ef..00000000 --- a/src/jack/activate.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::*; - -pub trait JackActivate: Sized { - fn activate_with ( - self, - init: impl FnOnce(&Arc>)->Usually - ) - -> Usually>>; -} - -impl JackActivate for JackConnection { - fn activate_with ( - self, - init: impl FnOnce(&Arc>)->Usually - ) - -> Usually>> - { - let client = Arc::new(RwLock::new(self)); - let target = Arc::new(RwLock::new(init(&client)?)); - let event = Box::new(move|_|{/*TODO*/}) as Box; - let events = Notifications(event); - let frame = Box::new({ - let target = target.clone(); - move|c: &_, s: &_|if let Ok(mut target) = target.write() { - target.process(c, s) - } else { - Control::Quit - } - }); - let frames = ClosureProcessHandler::new(frame as BoxedAudioHandler); - let mut buffer = Self::Activating; - std::mem::swap(&mut*client.write().unwrap(), &mut buffer); - *client.write().unwrap() = Self::Active(Client::from(buffer).activate_async(events, frames)?); - Ok(target) - } -}