transport is rolling

This commit is contained in:
🪞👃🪞 2024-05-31 23:43:12 +03:00
parent 1f928fba9d
commit 4fd208d53f
18 changed files with 540 additions and 498 deletions

View file

@ -1,2 +1,322 @@
//pub mod jack;
//pub mod tui;
use crate::prelude::*;
use std::sync::mpsc;
use crossterm::event;
pub trait Exitable {
fn exit (&mut self);
fn exited (&self) -> bool;
}
#[derive(Debug)]
pub enum Event {
/// An input event that must be handled.
Input(::crossterm::event::Event),
/// Update values but not the whole form.
Update,
/// Update the whole form.
Redraw,
}
pub struct Engine {
stdout: Stdout,
exited: Arc<AtomicBool>,
sender: Sender<Event>,
receiver: Receiver<Event>,
input_thread: JoinHandle<()>,
pub jack_client: Jack<Notifications>,
}
impl Engine {
pub fn new (name: Option<&str>) -> Result<Self, Box<dyn Error>> {
let (sender, receiver) = mpsc::channel::<self::Event>();
let exited = Arc::new(AtomicBool::new(false));
let jack_client = {
let sender = sender.clone();
let exited = exited.clone();
let (client, _status) = Client::new(
name.unwrap_or("blinkenlive"),
ClientOptions::NO_START_SERVER
)?;
let handler = ClosureProcessHandler::new(
Box::new(move |_client: &Client, _ps: &ProcessScope| -> Control {
if exited.fetch_and(true, Ordering::Relaxed) {
Control::Quit
} else {
sender.send(Event::Update);
Control::Continue
}
}) as BoxedProcessHandler
);
client.activate_async(Notifications, handler)?
};
let input_thread = {
let sender = sender.clone();
let exited = exited.clone();
let poll = std::time::Duration::from_millis(100);
spawn(move || {
loop {
// Exit if flag is set
if exited.fetch_and(true, Ordering::Relaxed) {
break
}
// Listen for events and send them to the main thread
if event::poll(poll).is_ok() {
let event = event::read().unwrap();
if sender.send(Event::Input(event)).is_err() {
break
}
}
}
})
};
Ok(Self {
stdout: stdout(),
exited,
sender,
receiver,
jack_client,
input_thread,
})
}
pub fn run <T: Exitable> (
&mut self,
state: &mut T,
mut render: impl FnMut(&mut T, &mut Stdout, (u16, u16)) -> Result<(), Box<dyn Error>>,
mut handle: impl FnMut(&mut T, &Event) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
// JACK thread:
// Input thread:
// Render (main) thread:
crossterm::terminal::enable_raw_mode()?;
let stdout = &mut self.stdout;
stdout
.queue(crossterm::terminal::EnterAlternateScreen)?
.flush()?;
let sleep = std::time::Duration::from_millis(16);
loop {
stdout
.queue(crossterm::terminal::BeginSynchronizedUpdate)?
.queue(Clear(ClearType::All))?;
render(state, stdout, (0, 0))?;
stdout
.queue(crossterm::terminal::EndSynchronizedUpdate)?
.flush()?;
// Handle event if present (`None` redraws)
if let event = self.receiver.recv()? {
handle(state, &event)?;
}
if state.exited() {
self.exited.store(true, Ordering::Relaxed);
stdout
.queue(crossterm::terminal::LeaveAlternateScreen)?
.flush()?;
crossterm::terminal::disable_raw_mode()?;
break
}
std::thread::sleep(sleep);
}
Ok(())
}
}
pub struct Notifications;
impl NotificationHandler for Notifications {
fn thread_init (&self, _: &Client) {
}
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
}
fn freewheel (&mut self, _: &Client, is_enabled: bool) {
}
fn sample_rate (&mut self, _: &Client, _: Frames) -> Control {
Control::Quit
}
fn client_registration (&mut self, _: &Client, name: &str, is_reg: bool) {
}
fn port_registration (&mut self, _: &Client, port_id: PortId, is_reg: bool) {
}
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
Control::Continue
}
fn ports_connected (&mut self, _: &Client, id_a: PortId, id_b: PortId, are: bool) {
}
fn graph_reorder (&mut self, _: &Client) -> Control {
Control::Continue
}
fn xrun (&mut self, _: &Client) -> Control {
Control::Continue
}
}
//pub struct Jack {
//client: Option<AsyncClient<
//Notifications,
//ClosureProcessHandler<Box<dyn FnMut(&Client, &ProcessScope)->Control + Send>>
//>>,
//pub transport: Option<Transport>,
//audio_ins: BTreeMap<String, Port<AudioIn>>,
//audio_outs: BTreeMap<String, Port<AudioOut>>,
//midi_ins: BTreeMap<String, Port<MidiIn>>,
//midi_outs: BTreeMap<String, Port<MidiOut>>,
//}
//impl Jack {
//pub fn init_from_cli (options: &Cli)
//-> Result<Arc<Mutex<Self>>, Box<dyn Error>>
//{
//let jack = Self::init(&options.jack_client_name)?;
//{
//let jack = jack.clone();
//let mut jack = jack.lock().unwrap();
//for port in options.jack_audio_ins.iter() {
//jack.add_audio_in(port)?;
//}
//for port in options.jack_audio_outs.iter() {
//jack.add_audio_out(port)?;
//}
//for port in options.jack_midi_ins.iter() {
//jack.add_midi_in(port)?;
//}
//for port in options.jack_midi_outs.iter() {
//jack.add_midi_out(port)?;
//}
//}
//Ok(jack.clone())
//}
//fn init (name: &str)
//-> Result<Arc<Mutex<Self>>, Box<dyn Error>>
//{
//let jack = Arc::new(Mutex::new(Self {
//client: None,
//transport: None,
//audio_ins: BTreeMap::new(),
//audio_outs: BTreeMap::new(),
//midi_ins: BTreeMap::new(),
//midi_outs: BTreeMap::new(),
//}));
//let (client, status) = Client::new(name, ClientOptions::NO_START_SERVER)?;
//println!("Client status: {status:?}");
//let jack1 = jack.clone();
//let mut jack1 = jack1.lock().unwrap();
//let jack2 = jack.clone();
//jack1.transport = Some(client.transport());
//jack1.client = Some(client.activate_async(
//Notifications, ClosureProcessHandler::new(Box::new(
//move |_client: &Client, _ps: &ProcessScope| -> Control {
//let jack = jack2.lock().expect("Failed to lock jack mutex");
//jack.read_inputs();
//jack.write_outputs();
//Control::Continue
//}
//) as Box<dyn FnMut(&Client, &ProcessScope)->Control + Send>)
//)?);
//Ok(jack)
//}
//fn start (&self) {
//}
//fn process (&self, _: &Client, ps: &ProcessScope) -> Control {
//Control::Continue
//}
//fn add_audio_in (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
//let port = self.client
//.as_ref()
//.expect("Not initialized.")
//.as_client()
//.register_port(name, AudioIn::default())?;
//self.audio_ins.insert(name.into(), port);
//Ok(self)
//}
//fn add_audio_out (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
//let port = self.client
//.as_ref()
//.expect("Not initialized.")
//.as_client()
//.register_port(name, AudioOut::default())?;
//self.audio_outs.insert(name.into(), port);
//Ok(self)
//}
//fn add_midi_in (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
//let port = self.client
//.as_ref()
//.expect("Not initialized.")
//.as_client()
//.register_port(name, MidiIn::default())?;
//self.midi_ins.insert(name.into(), port);
//Ok(self)
//}
//fn add_midi_out (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
//let port = self.client
//.as_ref()
//.expect("Not initialized.")
//.as_client()
//.register_port(name, MidiOut::default())?;
//self.midi_outs.insert(name.into(), port);
//Ok(self)
//}
//fn read_inputs (&self) {
//// read input buffers
////println!("read");
//}
//fn write_outputs (&self) {
//// clear output buffers
//// write output buffers
////println!("write");
//}
//}
//struct Notifications;
//impl NotificationHandler for Notifications {
//fn thread_init (&self, _: &Client) {
//}
//fn shutdown (&mut self, status: ClientStatus, reason: &str) {
//}
//fn freewheel (&mut self, _: &Client, is_enabled: bool) {
//}
//fn sample_rate (&mut self, _: &Client, _: Frames) -> Control {
//Control::Quit
//}
//fn client_registration (&mut self, _: &Client, name: &str, is_reg: bool) {
//}
//fn port_registration (&mut self, _: &Client, port_id: PortId, is_reg: bool) {
//}
//fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
//Control::Continue
//}
//fn ports_connected (&mut self, _: &Client, id_a: PortId, id_b: PortId, are: bool) {
//}
//fn graph_reorder (&mut self, _: &Client) -> Control {
//Control::Continue
//}
//fn xrun (&mut self, _: &Client) -> Control {
//Control::Continue
//}
//}