mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 12:46:42 +01:00
some more core refactor before fixing the mess
This commit is contained in:
parent
fe09536a45
commit
0bbf74e915
12 changed files with 31 additions and 28 deletions
236
crates/tek_core/src/audio.rs
Normal file
236
crates/tek_core/src/audio.rs
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
use jack::*;
|
||||
use crate::*;
|
||||
|
||||
submod! {
|
||||
device
|
||||
event
|
||||
ports
|
||||
}
|
||||
|
||||
/// A UI component that may be associated with a JACK client by the `Jack` factory.
|
||||
pub trait Device<E: Engine>: Component<E> + Process {
|
||||
/// Perform type erasure for collecting heterogeneous devices.
|
||||
fn boxed (self) -> Box<dyn Device<E>> where Self: Sized + 'static {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// All things that implement the required traits can be treated as `Device`.
|
||||
impl<D, E: Engine> Device<E> for D where D: Component<E> + Process {}
|
||||
|
||||
impl<E: Engine> Render<E> for Box<dyn Device<E>> {
|
||||
fn render (&self, to: &mut E::RenderInput) -> Perhaps<E::Rendered> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>),
|
||||
ClosureProcessHandler::new(Box::new({
|
||||
let state = state.clone();
|
||||
move|c: &Client, s: &ProcessScope|process(&state, c, s)
|
||||
}) as BoxedProcessHandler)
|
||||
)?)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type DynamicAsyncClient =
|
||||
AsyncClient<DynamicNotifications, DynamicProcessHandler>;
|
||||
|
||||
type DynamicProcessHandler =
|
||||
ClosureProcessHandler<BoxedProcessHandler>;
|
||||
|
||||
pub type BoxedProcessHandler =
|
||||
Box<dyn FnMut(&Client, &ProcessScope)-> Control + Send>;
|
||||
|
||||
/// Trait for things that have a JACK process callback.
|
||||
pub trait Process {
|
||||
fn process (&mut self, _: &Client, _: &ProcessScope) -> Control {
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
||||
/// Define the JACK process callback associated with a struct.
|
||||
#[macro_export] macro_rules! process {
|
||||
($T:ty) => {
|
||||
impl Process for $T {}
|
||||
};
|
||||
($T:ty |$self:ident, $c:ident, $s:ident|$block:tt) => {
|
||||
impl Process for $T {
|
||||
fn process (&mut $self, $c: &Client, $s: &ProcessScope) -> Control {
|
||||
$block
|
||||
}
|
||||
}
|
||||
};
|
||||
($T:ty = $process:path) => {
|
||||
impl Process for $T {
|
||||
fn process (&mut self, c: &Client, s: &ProcessScope) -> Control {
|
||||
$process(self, c, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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> + Process + 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>),
|
||||
ClosureProcessHandler::new(Box::new({
|
||||
let app = app.clone();
|
||||
move|c: &Client, s: &ProcessScope|{
|
||||
app.write().unwrap().process(c, s)
|
||||
//Control::Continue
|
||||
}
|
||||
}) as BoxedProcessHandler)
|
||||
)?)
|
||||
}
|
||||
|
||||
/// `JackDevice` factory. Creates JACK `Client`s, performs port registration
|
||||
/// and activation, and encapsulates a `Device` into a `JackDevice`.
|
||||
pub struct Jack {
|
||||
pub client: Client,
|
||||
pub midi_ins: Vec<String>,
|
||||
pub audio_ins: Vec<String>,
|
||||
pub midi_outs: Vec<String>,
|
||||
pub audio_outs: Vec<String>,
|
||||
}
|
||||
impl Jack {
|
||||
pub fn new (name: &str) -> Usually<Self> {
|
||||
Ok(Self {
|
||||
midi_ins: vec![],
|
||||
audio_ins: vec![],
|
||||
midi_outs: vec![],
|
||||
audio_outs: vec![],
|
||||
client: Client::new(
|
||||
name,
|
||||
ClientOptions::NO_START_SERVER
|
||||
)?.0,
|
||||
})
|
||||
}
|
||||
pub fn run <'a: 'static, D, E> (
|
||||
self, state: impl FnOnce(JackPorts)->Box<D>
|
||||
) -> Usually<JackDevice<E>>
|
||||
where D: Device<E> + Sized + 'static,
|
||||
E: Engine + 'static,
|
||||
{
|
||||
let owned_ports = JackPorts {
|
||||
audio_ins: register_ports(&self.client, self.audio_ins, AudioIn)?,
|
||||
audio_outs: register_ports(&self.client, self.audio_outs, AudioOut)?,
|
||||
midi_ins: register_ports(&self.client, self.midi_ins, MidiIn)?,
|
||||
midi_outs: register_ports(&self.client, self.midi_outs, MidiOut)?,
|
||||
};
|
||||
let midi_outs = owned_ports.midi_outs.values()
|
||||
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
|
||||
let midi_ins = owned_ports.midi_ins.values()
|
||||
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
|
||||
let audio_outs = owned_ports.audio_outs.values()
|
||||
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
|
||||
let audio_ins = owned_ports.audio_ins.values()
|
||||
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
|
||||
let state = Arc::new(RwLock::new(state(owned_ports) as Box<dyn Device<E>>));
|
||||
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(JackEvent) + Send + Sync>),
|
||||
ClosureProcessHandler::new(Box::new({
|
||||
let state = state.clone();
|
||||
move|c: &Client, s: &ProcessScope|{
|
||||
state.write().unwrap().process(c, s)
|
||||
}
|
||||
}) as BoxedProcessHandler)
|
||||
)?;
|
||||
Ok(JackDevice {
|
||||
ports: UnownedJackPorts {
|
||||
audio_ins: query_ports(&client.as_client(), audio_ins),
|
||||
audio_outs: query_ports(&client.as_client(), audio_outs),
|
||||
midi_ins: query_ports(&client.as_client(), midi_ins),
|
||||
midi_outs: query_ports(&client.as_client(), midi_outs),
|
||||
},
|
||||
client,
|
||||
state,
|
||||
})
|
||||
}
|
||||
pub fn audio_in (mut self, name: &str) -> Self {
|
||||
self.audio_ins.push(name.to_string());
|
||||
self
|
||||
}
|
||||
pub fn audio_out (mut self, name: &str) -> Self {
|
||||
self.audio_outs.push(name.to_string());
|
||||
self
|
||||
}
|
||||
pub fn midi_in (mut self, name: &str) -> Self {
|
||||
self.midi_ins.push(name.to_string());
|
||||
self
|
||||
}
|
||||
pub fn midi_out (mut self, name: &str) -> Self {
|
||||
self.midi_outs.push(name.to_string());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn register_ports <T: PortSpec + Copy> (
|
||||
client: &Client, names: Vec<String>, spec: T
|
||||
) -> Usually<BTreeMap<String, Port<T>>> {
|
||||
names.into_iter().try_fold(BTreeMap::new(), |mut ports, name|{
|
||||
let port = client.register_port(&name, spec)?;
|
||||
ports.insert(name, port);
|
||||
Ok(ports)
|
||||
})
|
||||
}
|
||||
|
||||
fn query_ports (
|
||||
client: &Client, names: Vec<String>
|
||||
) -> BTreeMap<String, Port<Unowned>> {
|
||||
names.into_iter().fold(BTreeMap::new(), |mut ports, name|{
|
||||
let port = client.port_by_name(&name).unwrap();
|
||||
ports.insert(name, port);
|
||||
ports
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue