wip: connect devices

This commit is contained in:
🪞👃🪞 2024-07-04 00:02:22 +03:00
parent 7f3425fe04
commit 394355331d
10 changed files with 235 additions and 145 deletions

View file

@ -1,5 +1,114 @@
use crate::core::*;
pub struct Jack {
pub client: JackClient,
pub ports: JackPorts,
}
pub enum JackClient {
Active(DynamicAsyncClient),
Inactive(Client),
}
#[derive(Default)]
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>>,
}
ports!(Jack {
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 Jack {
pub fn new (name: &str) -> Usually<Self> {
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
Ok(Self { client: JackClient::Inactive(client), ports: JackPorts::default(), })
}
pub fn ports_from_lv2 (self, plugin: &::livi::Plugin) -> Usually<Self> {
let counts = plugin.port_counts();
let mut jack = self;
for i in 0..counts.atom_sequence_inputs {
jack = jack.register_midi_in(&format!("midi-in-{i}"))?
}
for i in 0..counts.atom_sequence_outputs {
jack = jack.register_midi_out(&format!("midi-out-{i}"))?;
}
for i in 0..counts.audio_inputs {
jack = jack.register_audio_in(&format!("audio-in-{i}"))?
}
for i in 0..counts.audio_outputs {
jack = jack.register_audio_out(&format!("audio-out-{i}"))?;
}
Ok(jack)
}
pub fn register_midi_out (mut self, name: &str) -> Usually<Self> {
self.ports.midi_outs.insert(name.to_string(), match &self.client {
JackClient::Inactive(c) => c.register_port(name, MidiOut::default())?,
JackClient::Active(c) => c.as_client().register_port(name, MidiOut::default())?,
});
Ok(self)
}
pub fn register_midi_in (mut self, name: &str) -> Usually<Self> {
self.ports.midi_ins.insert(name.to_string(), match &self.client {
JackClient::Inactive(c) => c.register_port(name, MidiIn::default())?,
JackClient::Active(c) => c.as_client().register_port(name, MidiIn::default())?,
});
Ok(self)
}
pub fn register_audio_out (mut self, name: &str) -> Usually<Self> {
self.ports.audio_outs.insert(name.to_string(), match &self.client {
JackClient::Inactive(c) => c.register_port(name, AudioOut::default())?,
JackClient::Active(c) => c.as_client().register_port(name, AudioOut::default())?,
});
Ok(self)
}
pub fn register_audio_in (mut self, name: &str) -> Usually<Self> {
self.ports.audio_ins.insert(name.to_string(), match &self.client {
JackClient::Inactive(c) => c.register_port(name, AudioIn::default())?,
JackClient::Active(c) => c.as_client().register_port(name, AudioIn::default())?,
});
Ok(self)
}
pub fn run <T> (
mut self, state: impl FnOnce(JackPorts)->Box<T>
) -> Usually<Arc<Mutex<Box<dyn Device>>>> where
T: Device + Process + Sized + 'static
{
let mut ports = JackPorts::default();
std::mem::swap(&mut self.ports, &mut ports);
let state = Arc::new(Mutex::new(state(ports) as Box<dyn Device>));
self.client = match self.client {
JackClient::Active(_) => panic!("already running"),
JackClient::Inactive(client) => JackClient::Active(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(AppEvent) + Send + Sync>),
ClosureProcessHandler::new(Box::new({
let state = state.clone();
move|c: &Client, s: &ProcessScope|{
state.lock().unwrap().process(c, s)
}
}) as BoxedProcessHandler)
)?)
};
Ok(state)
}
}
pub fn jack_run <T> (name: &str, app: &Arc<Mutex<T>>) -> Usually<DynamicAsyncClient>
where T: Handle + Process + Send + 'static
{

View file

@ -2,10 +2,10 @@ use crate::core::*;
/// Trait for things that may expose JACK ports.
pub trait Ports {
fn audio_ins (&self) -> Usually<Vec<&Port<AudioIn>>> {
fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<AudioIn>>> {
Ok(vec![])
}
fn audio_outs (&self) -> Usually<Vec<&Port<AudioOut>>> {
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<AudioOut>>> {
Ok(vec![])
}
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<MidiIn>>> {

View file

@ -1,6 +1,19 @@
use crate::core::*;
use ratatui::widgets::WidgetRef;
pub trait Blit {
// Render something to X, Y coordinates in a buffer, ignoring width/height.
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
}
/// Trait for things that render to the display.
pub trait Render: Send {
// Render something to an area of the buffer.
@ -43,6 +56,12 @@ impl Render for Box<dyn Device> {
}
}
impl<T: Render> Render for Arc<Mutex<T>> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
self.lock().unwrap().render(b, a)
}
}
impl WidgetRef for &dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(*self, buf, area).expect("Failed to render device.");
@ -54,16 +73,3 @@ impl WidgetRef for dyn Render {
Render::render(self, buf, area).expect("Failed to render device.");
}
}
pub trait Blit {
// Render something to X, Y coordinates in a buffer, ignoring width/height.
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
}