Merge pull request 'Update JACK to 0.13' (#1) from wmedrano/tek:main into main

Reviewed-on: https://codeberg.org/unspeaker/tek/pulls/1
This commit is contained in:
unspeaker 2024-09-22 21:44:00 +00:00
commit 0646e0fe0b
4 changed files with 253 additions and 213 deletions

11
Cargo.lock generated
View file

@ -1094,11 +1094,11 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "jack"
version = "0.10.0"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce722655a29b13bb98ec7e8ba9dc65d670b9b37c7b1c09775c7f7516811c5a36"
checksum = "78a4ae24f4ee29676aef8330fed1104e72f314cab16643dbeb61bfd99b4a8273"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.6.0",
"jack-sys",
"lazy_static",
"libc",
@ -1107,14 +1107,15 @@ dependencies = [
[[package]]
name = "jack-sys"
version = "0.4.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9d70559ff166d148ccb750ddd77702af760718f3a752c731add168c22c16a9f"
checksum = "6013b7619b95a22b576dfb43296faa4ecbe40abbdb97dfd22ead520775fc86ab"
dependencies = [
"bitflags 1.3.2",
"lazy_static",
"libc",
"libloading 0.7.4",
"log",
"pkg-config",
]

View file

@ -1,7 +1,7 @@
use crate::*;
use tek_core::Direction;
use tek_sequencer::{TransportToolbar, Arranger};
use tek_mixer::Mixer;
use tek_sequencer::{Arranger, TransportToolbar};
/// Root of application state.
pub struct App {
@ -16,7 +16,7 @@ pub struct App {
/// Mixer (contains tracks)
pub mixer: Mixer,
/// Main JACK client.
pub jack: Option<JackClient>,
pub jack: Option<JackClientt>,
/// Map of external MIDI outs in the jack graph
/// to internal MIDI ins of this app.
pub midi_in: Option<Arc<Port<MidiIn>>>,
@ -36,12 +36,9 @@ impl App {
pub fn new() -> Usually<Self> {
let xdg = Arc::new(XdgApp::new("tek")?);
let first_run = AppPaths::new(&xdg)?.should_create();
let jack = JackClient::Inactive(
Client::new("tek", ClientOptions::NO_START_SERVER)?.0
);
*MODAL.lock().unwrap() = first_run.then(||{
ExitableComponent::boxed(SetupModal(Some(xdg.clone()), false))
});
let jack = JackClient::Inactive(Client::new("tek", ClientOptions::NO_START_SERVER)?.0);
*MODAL.lock().unwrap() =
first_run.then(|| ExitableComponent::boxed(SetupModal(Some(xdg.clone()), false)));
Ok(Self {
entered: true,
section: AppFocus::default(),
@ -71,10 +68,12 @@ impl App {
let client = self.client();
self.audio_outs = names
.iter()
.map(|name|client
.map(|name| {
client
.ports(Some(name), None, PortFlags::empty())
.get(0)
.map(|name|client.port_by_name(name)))
.map(|name| client.port_by_name(name))
})
.flatten()
.filter_map(|x| x)
.map(Arc::new)
@ -82,7 +81,8 @@ impl App {
Ok(self)
}
pub fn activate(
mut self, init: Option<impl FnOnce(&Arc<RwLock<Self>>)->Usually<()>>
mut self,
init: Option<impl FnOnce(&Arc<RwLock<Self>>) -> Usually<()>>,
) -> Usually<Arc<RwLock<Self>>> {
let jack = self.jack.take().expect("no jack client");
let app = Arc::new(RwLock::new(self));
@ -96,30 +96,38 @@ impl App {
}
}
render!(App |self, buf, area| {
render!(
App | self,
buf,
area | {
Split::down()
.add_ref(&self.transport)
.add_ref(&self.arranger)
.add(If(self.arranger.selected.is_clip(), &Split::right()
.add(If(
self.arranger.selected.is_clip(),
&Split::right()
.add(tek_mixer::TrackView {
direction: Direction::Down,
entered: self.entered,
focused: self.section == AppFocus::Chain,
chain: self.mixer.track()
chain: self.mixer.track(),
})
.add_ref(&self.arranger.sequencer())
.add_ref(&self.arranger.sequencer()),
))
.render(buf, area)?;
if let Some(ref modal) = *MODAL.lock().unwrap() {
modal.render(buf, area)?;
}
Ok(area)
});
}
);
process!(App |self, _client, scope| {
let (
reset, current_frames, chunk_size, current_usecs, next_usecs, period_usecs
) = self.transport.update(&scope);
process!(
App | self,
_client,
scope | {
let (reset, current_frames, chunk_size, current_usecs, next_usecs, period_usecs) =
self.transport.update(&scope);
self.chunk_size = chunk_size;
for track in self.arranger.tracks.iter_mut() {
track.process(
@ -131,9 +139,13 @@ process!(App |self, _client, scope| {
reset,
&scope,
(current_frames as usize, self.chunk_size),
(current_usecs as usize, next_usecs.saturating_sub(current_usecs) as usize),
period_usecs as f64
(
current_usecs as usize,
next_usecs.saturating_sub(current_usecs) as usize,
),
period_usecs as f64,
);
}
Control::Continue
});
}
);

View file

@ -10,7 +10,7 @@ better-panic = "0.3.0"
clap = { version = "4.5.4", features = [ "derive" ] }
clojure-reader = "0.1.0"
crossterm = "0.27"
jack = "0.10"
jack = "0.13"
midly = "0.5"
once_cell = "1.19.0"
ratatui = { version = "0.26.3", features = [ "unstable-widget-ref", "underline-color" ] }

View file

@ -1,5 +1,5 @@
use jack::*;
use crate::*;
use jack::*;
/// Trait for things that have a JACK process callback.
pub trait Audio {
@ -27,7 +27,10 @@ pub trait Ports {
/// A UI component that may be associated with a JACK client by the `Jack` factory.
pub trait AudioComponent<E: Engine>: Component<E> + Audio {
/// Perform type erasure for collecting heterogeneous devices.
fn boxed (self) -> Box<dyn AudioComponent<E>> where Self: Sized + 'static {
fn boxed(self) -> Box<dyn AudioComponent<E>>
where
Self: Sized + 'static,
{
Box::new(self)
}
}
@ -44,10 +47,8 @@ pub enum JackClient {
impl JackClient {
pub fn client(&self) -> &Client {
match self {
Self::Inactive(ref client) =>
client,
Self::Active(ref client) =>
client.as_client(),
Self::Inactive(ref client) => client,
Self::Active(ref client) => client.as_client(),
}
}
pub fn transport(&self) -> Transport {
@ -62,34 +63,33 @@ impl JackClient {
pub fn activate<T: Send + Sync + 'static>(
self,
state: &Arc<RwLock<T>>,
mut process: impl FnMut(&Arc<RwLock<T>>, &Client, &ProcessScope)->Control + Send + 'static
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({
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)
)?)
}) as BoxedAudioHandler),
)?),
})
}
}
pub type DynamicAsyncClient =
AsyncClient<DynamicNotifications, DynamicAudioHandler>;
pub type DynamicAsyncClient = AsyncClient<DynamicNotifications, DynamicAudioHandler>;
type DynamicAudioHandler =
ClosureProcessHandler<BoxedAudioHandler>;
type DynamicAudioHandler = contrib::ClosureProcessHandler<(), BoxedAudioHandler>;
pub type BoxedAudioHandler =
Box<dyn FnMut(&Client, &ProcessScope)-> Control + Send>;
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
where
T: Handle<E> + Audio + Send + Sync + 'static,
{
let options = ClientOptions::NO_START_SERVER;
let (client, _status) = Client::new(name, options)?;
@ -101,13 +101,13 @@ pub fn jack_run <T, E: Engine> (name: &str, app: &Arc<RwLock<T>>) -> Usually<Dyn
//app.lock().unwrap().handle(&event).unwrap();
}
}) as Box<dyn Fn(JackEvent) + Send + Sync>),
ClosureProcessHandler::new(Box::new({
contrib::ClosureProcessHandler::new(Box::new({
let app = app.clone();
move |c: &Client, s: &ProcessScope| {
app.write().unwrap().process(c, s)
//Control::Continue
}
}) as BoxedAudioHandler)
}) as BoxedAudioHandler),
)?)
}
@ -128,32 +128,43 @@ impl Jack {
audio_ins: vec![],
midi_outs: vec![],
audio_outs: vec![],
client: Client::new(
name,
ClientOptions::NO_START_SERVER
)?.0,
client: Client::new(name, ClientOptions::NO_START_SERVER)?.0,
})
}
pub fn run<'a: 'static, D, E>(
self, state: impl FnOnce(JackPorts)->Box<D>
self,
state: impl FnOnce(JackPorts) -> Box<D>,
) -> Usually<JackDevice<E>>
where D: AudioComponent<E> + Sized + 'static,
where
D: AudioComponent<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)?,
audio_ins: register_ports(&self.client, self.audio_ins, AudioIn::default())?,
audio_outs: register_ports(&self.client, self.audio_outs, AudioOut::default())?,
midi_ins: register_ports(&self.client, self.midi_ins, MidiIn::default())?,
midi_outs: register_ports(&self.client, self.midi_outs, MidiOut::default())?,
};
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 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 AudioComponent<E>>));
let client = self.client.activate_async(
Notifications(Box::new({
@ -163,12 +174,10 @@ impl Jack {
//state.lock().unwrap().handle(&event).unwrap();
}
}) as Box<dyn Fn(JackEvent) + Send + Sync>),
ClosureProcessHandler::new(Box::new({
contrib::ClosureProcessHandler::new(Box::new({
let state = state.clone();
move|c: &Client, s: &ProcessScope|{
state.write().unwrap().process(c, s)
}
}) as BoxedAudioHandler)
move |c: &Client, s: &ProcessScope| state.write().unwrap().process(c, s)
}) as BoxedAudioHandler),
)?;
Ok(JackDevice {
ports: UnownedJackPorts {
@ -200,18 +209,20 @@ impl Jack {
}
fn register_ports<T: PortSpec + Copy>(
client: &Client, names: Vec<String>, spec: T
client: &Client,
names: Vec<String>,
spec: T,
) -> Usually<BTreeMap<String, Port<T>>> {
names.into_iter().try_fold(BTreeMap::new(), |mut ports, name|{
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>> {
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);
@ -221,8 +232,7 @@ fn query_ports (
/// Notification handler used by the [Jack] factory
/// when constructing [JackDevice]s.
pub type DynamicNotifications =
Notifications<Box<dyn Fn(JackEvent) + Send + Sync>>;
pub type DynamicNotifications = Notifications<Box<dyn Fn(JackEvent) + Send + Sync>>;
#[derive(Debug)]
/// Event enum for JACK events.
@ -247,7 +257,7 @@ impl<T: Fn(JackEvent) + Send> NotificationHandler for Notifications<T> {
self.0(JackEvent::ThreadInit);
}
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
unsafe fn shutdown(&mut self, status: ClientStatus, reason: &str) {
self.0(JackEvent::Shutdown(status, reason.into()));
}
@ -301,7 +311,9 @@ pub struct JackDevice<E: Engine> {
impl<E: Engine> std::fmt::Debug for JackDevice<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JackDevice").field("ports", &self.ports).finish()
f.debug_struct("JackDevice")
.field("ports", &self.ports)
.finish()
}
}
@ -346,16 +358,28 @@ impl<E: Engine> JackDevice<E> {
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])?)
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)?)
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])?)
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)?)
Ok(self
.client
.as_client()
.connect_ports(self.audio_outs()?[index], port)?)
}
}
@ -390,14 +414,17 @@ impl JackPorts {
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
.audio_outs
.insert(name.clone(), port.clone_unowned());
}
unowned
}
}
/// Implement the `Ports` trait.
#[macro_export] macro_rules! ports {
#[macro_export]
macro_rules! ports {
($T:ty $({ $(audio: {
$(ins: |$ai_arg:ident|$ai_impl:expr,)?
$(outs: |$ao_arg:ident|$ao_impl:expr,)?