merge tek_jack into tek_core

This commit is contained in:
🪞👃🪞 2024-08-11 09:48:45 +03:00
parent a659062dbc
commit 278b3caad3
23 changed files with 285 additions and 456 deletions

View file

@ -0,0 +1,229 @@
use crate::{*, jack::*};
/// A UI component that may be associated with a JACK client by the `Jack` factory.
pub trait Device: Render + Handle + Process + Send + Sync {
/// Perform type erasure for collecting heterogeneous devices.
fn boxed (self) -> Box<dyn Device> where Self: Sized + 'static {
Box::new(self)
}
}
/// All things that implement the required traits can be treated as `Device`.
impl<T: Render + Handle + Process + Send + Sync> Device for T {}
impl Render for Box<dyn Device> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
(**self).render(b, a)
}
}
/// 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: Sync> (name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicAsyncClient>
where T: Handle + Process + Send + '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 <T: Device + Process + Sized + 'static> (
self, state: impl FnOnce(JackPorts)->Box<T>
)
-> Usually<JackDevice>
{
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>));
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
})
}

View file

@ -0,0 +1,52 @@
use crate::{*, jack::*};
/// A [Device] bound to a JACK client and a set of ports.
pub struct JackDevice {
/// The active JACK client of this device.
pub client: DynamicAsyncClient,
/// The device state, encapsulated for sharing between threads.
pub state: Arc<RwLock<Box<dyn Device>>>,
/// Unowned copies of the device's JACK ports, for connecting to the device.
/// The "real" readable/writable `Port`s are owned by the `state`.
pub ports: UnownedJackPorts,
}
impl std::fmt::Debug for JackDevice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JackDevice").field("ports", &self.ports).finish()
}
}
render!(JackDevice |self, buf, area| self.state.read().unwrap().render(buf, area));
handle!(JackDevice |self, event| self.state.write().unwrap().handle(event));
ports!(JackDevice {
audio: {
ins: |s|Ok(s.ports.audio_ins.values().collect()),
outs: |s|Ok(s.ports.audio_outs.values().collect()),
}
midi: {
ins: |s|Ok(s.ports.midi_ins.values().collect()),
outs: |s|Ok(s.ports.midi_outs.values().collect()),
}
});
impl JackDevice {
/// Returns a locked mutex of the state's contents.
pub fn state (&self) -> LockResult<RwLockReadGuard<Box<dyn Device>>> {
self.state.read()
}
/// Returns a locked mutex of the state's contents.
pub fn state_mut (&self) -> LockResult<RwLockWriteGuard<Box<dyn Device>>> {
self.state.write()
}
pub fn connect_midi_in (&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
Ok(self.client.as_client().connect_ports(port, self.midi_ins()?[index])?)
}
pub fn connect_midi_out (&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
Ok(self.client.as_client().connect_ports(self.midi_outs()?[index], port)?)
}
pub fn connect_audio_in (&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
Ok(self.client.as_client().connect_ports(port, self.audio_ins()?[index])?)
}
pub fn connect_audio_out (&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
Ok(self.client.as_client().connect_ports(self.audio_outs()?[index], port)?)
}
}

View file

@ -0,0 +1,71 @@
use crate::{*, jack::*};
/// Notification handler used by the [Jack] factory
/// when constructing [JackDevice]s.
pub type DynamicNotifications =
Notifications<Box<dyn Fn(JackEvent) + Send + Sync>>;
#[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,
}
/// 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);
}
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
}
}

View file

@ -0,0 +1,94 @@
use crate::{*, jack::*};
/// Collection of JACK ports as [AudioIn]/[AudioOut]/[MidiIn]/[MidiOut].
#[derive(Default, Debug)]
pub struct JackPorts {
pub audio_ins: BTreeMap<String, Port<AudioIn>>,
pub midi_ins: BTreeMap<String, Port<MidiIn>>,
pub audio_outs: BTreeMap<String, Port<AudioOut>>,
pub midi_outs: BTreeMap<String, Port<MidiOut>>,
}
/// Collection of JACK ports as [Unowned].
#[derive(Default, Debug)]
pub struct UnownedJackPorts {
pub audio_ins: BTreeMap<String, Port<Unowned>>,
pub midi_ins: BTreeMap<String, Port<Unowned>>,
pub audio_outs: BTreeMap<String, Port<Unowned>>,
pub midi_outs: BTreeMap<String, Port<Unowned>>,
}
impl JackPorts {
pub fn clone_unowned (&self) -> UnownedJackPorts {
let mut unowned = UnownedJackPorts::default();
for (name, port) in self.midi_ins.iter() {
unowned.midi_ins.insert(name.clone(), port.clone_unowned());
}
for (name, port) in self.midi_outs.iter() {
unowned.midi_outs.insert(name.clone(), port.clone_unowned());
}
for (name, port) in self.audio_ins.iter() {
unowned.audio_ins.insert(name.clone(), port.clone_unowned());
}
for (name, port) in self.audio_outs.iter() {
unowned.audio_outs.insert(name.clone(), port.clone_unowned());
}
unowned
}
}
/// Trait for things that may expose JACK ports.
pub trait Ports {
fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
Ok(vec![])
}
}
/// Implement the `Ports` trait.
#[macro_export] macro_rules! ports {
($T:ty $({ $(audio: {
$(ins: |$ai_arg:ident|$ai_impl:expr,)?
$(outs: |$ao_arg:ident|$ao_impl:expr,)?
})? $(midi: {
$(ins: |$mi_arg:ident|$mi_impl:expr,)?
$(outs: |$mo_arg:ident|$mo_impl:expr,)?
})?})?) => {
impl Ports for $T {$(
$(
$(fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
let cb = |$ai_arg:&'a Self|$ai_impl;
cb(self)
})?
)?
$(
$(fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
let cb = (|$ao_arg:&'a Self|$ao_impl);
cb(self)
})?
)?
)? $(
$(
$(fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
let cb = (|$mi_arg:&'a Self|$mi_impl);
cb(self)
})?
)?
$(
$(fn midi_outs <'a> (&'a self) -> Usually<Vec<&'a Port<Unowned>>> {
let cb = (|$mo_arg:&'a Self|$mo_impl);
cb(self)
})?
)?
)?}
};
}

View file

@ -1,8 +1,9 @@
pub use ratatui;
pub use crossterm;
pub use jack;
pub use midly;
pub use clap;
pub use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use std::sync::{Arc, Mutex, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use std::collections::BTreeMap;
pub use crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers};
pub use ratatui::prelude::{Rect, Style, Color, Buffer};
@ -16,7 +17,6 @@ pub(crate) use std::io::{stdout};
pub(crate) use std::thread::{spawn, JoinHandle};
pub(crate) use std::time::Duration;
pub(crate) use atomic_float::*;
//, LockResult, RwLockReadGuard, RwLockWriteGuard};
//pub(crate) use std::path::PathBuf;
//pub(crate) use std::fs::read_dir;
//pub(crate) use std::ffi::OsString;
@ -48,6 +48,10 @@ submod! {
time_base
time_note
time_tick
jack_core
jack_device
jack_event
jack_ports
}
/// EDN parsing helper.

View file

@ -145,7 +145,7 @@ impl Render for () {
}
}
impl<T: Render> Render for Option<T> {
impl<T: Render + Send + Sync> Render for Option<&T> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
match self {
Some(widget) => widget.render(b, a),

View file

@ -46,63 +46,3 @@ mod test {
}
}
/// (pulses, name)
pub const NOTE_DURATIONS: [(usize, &str);26] = [
(1, "1/384"),
(2, "1/192"),
(3, "1/128"),
(4, "1/96"),
(6, "1/64"),
(8, "1/48"),
(12, "1/32"),
(16, "1/24"),
(24, "1/16"),
(32, "1/12"),
(48, "1/8"),
(64, "1/6"),
(96, "1/4"),
(128, "1/3"),
(192, "1/2"),
(256, "2/3"),
(384, "1/1"),
(512, "4/3"),
(576, "3/2"),
(768, "2/1"),
(1152, "3/1"),
(1536, "4/1"),
(2304, "6/1"),
(3072, "8/1"),
(3456, "9/1"),
(6144, "16/1"),
];
/// Returns the next shorter length
pub fn prev_note_length (ppq: usize) -> usize {
for i in 1..=16 {
let length = NOTE_DURATIONS[16-i].0;
if length < ppq {
return length
}
}
ppq
}
/// Returns the next longer length
pub fn next_note_length (ppq: usize) -> usize {
for (length, _) in &NOTE_DURATIONS {
if *length > ppq {
return *length
}
}
ppq
}
pub fn ppq_to_name (ppq: usize) -> &'static str {
for (length, name) in &NOTE_DURATIONS {
if *length == ppq {
return name
}
}
""
}