mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
refactor: device abstraction, layout components
This commit is contained in:
parent
d330d31ce4
commit
788dc1ccde
21 changed files with 1144 additions and 1166 deletions
360
src/device.rs
360
src/device.rs
|
|
@ -1,44 +1,338 @@
|
|||
pub mod transport;
|
||||
pub mod sequencer;
|
||||
pub mod sampler;
|
||||
pub mod mixer;
|
||||
pub mod looper;
|
||||
use crate::prelude::*;
|
||||
|
||||
mod transport;
|
||||
mod chain;
|
||||
mod sequencer;
|
||||
mod sampler;
|
||||
mod mixer;
|
||||
mod looper;
|
||||
mod plugin;
|
||||
|
||||
pub use self::transport::Transport;
|
||||
pub use self::chain::Chain;
|
||||
pub use self::sequencer::Sequencer;
|
||||
pub use self::sampler::Sampler;
|
||||
pub use self::mixer::Mixer;
|
||||
pub use self::looper::Looper;
|
||||
pub use self::plugin::Plugin;
|
||||
|
||||
use crate::prelude::*;
|
||||
use self::transport::*;
|
||||
use self::sequencer::*;
|
||||
use self::sampler::*;
|
||||
use self::mixer::*;
|
||||
use self::looper::*;
|
||||
use std::sync::mpsc;
|
||||
use crossterm::event;
|
||||
|
||||
pub enum Device {
|
||||
Transport(self::transport::Transport),
|
||||
Sequencer(self::sequencer::Sequencer),
|
||||
Sampler(self::sampler::Sampler),
|
||||
Mixer(self::mixer::Mixer),
|
||||
Looper(self::looper::Looper),
|
||||
pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn Error>> {
|
||||
let device = Arc::new(Mutex::new(device));
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
let input_thread = {
|
||||
let poll = std::time::Duration::from_millis(100);
|
||||
let exited = exited.clone();
|
||||
let device = device.clone();
|
||||
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();
|
||||
match event {
|
||||
crossterm::event::Event::Key(KeyEvent {
|
||||
code: KeyCode::Char('c'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..
|
||||
}) => {
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
},
|
||||
_ => if device.lock().unwrap().handle(&EngineEvent::Input(event)).is_err() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
stdout().execute(EnterAlternateScreen)?;
|
||||
enable_raw_mode()?;
|
||||
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
let sleep = std::time::Duration::from_millis(16);
|
||||
//better_panic::install();
|
||||
let better_panic_handler = better_panic::Settings::auto()
|
||||
.verbosity(better_panic::Verbosity::Full)
|
||||
.create_panic_handler();
|
||||
std::panic::set_hook(Box::new(move |info: &std::panic::PanicInfo|{
|
||||
stdout()
|
||||
.execute(crossterm::terminal::LeaveAlternateScreen)
|
||||
.unwrap();
|
||||
crossterm::terminal::disable_raw_mode()
|
||||
.unwrap();
|
||||
better_panic_handler(info);
|
||||
//writeln!(std::io::stderr(), "{}", info)
|
||||
//.unwrap();
|
||||
//writeln!(std::io::stderr(), "{:?}", ::backtrace::Backtrace::new())
|
||||
//.unwrap();
|
||||
}));
|
||||
loop {
|
||||
terminal.draw(|frame|{
|
||||
let area = frame.size();
|
||||
let buffer = frame.buffer_mut();
|
||||
device.lock().unwrap().render(buffer, area)
|
||||
}).expect("Failed to render frame");
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
std::thread::sleep(sleep);
|
||||
};
|
||||
//render_thread.join().expect("Failed to join render thread");
|
||||
stdout()
|
||||
.queue(crossterm::terminal::LeaveAlternateScreen)?
|
||||
.flush()?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl WidgetRef for Device {
|
||||
pub trait Device: Send + Sync {
|
||||
fn handle (&mut self, _event: &EngineEvent) -> Result<(), Box<dyn Error>> { Ok(()) }
|
||||
fn render (&self, _buffer: &mut Buffer, _area: Rect) {}
|
||||
fn process (&mut self, _client: Client, _scope: ProcessScope) {}
|
||||
}
|
||||
|
||||
pub struct DynamicDevice<T> {
|
||||
pub state: Mutex<T>,
|
||||
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect) + Send>>,
|
||||
pub handle: Mutex<Box<dyn FnMut(&mut T, &EngineEvent)->Result<(), Box<dyn Error>> + Send>>,
|
||||
pub process: Mutex<Box<dyn FnMut(&mut T) + Send>>
|
||||
}
|
||||
|
||||
impl<T> DynamicDevice<T> {
|
||||
fn new <'a, R, H, P> (
|
||||
render: R,
|
||||
handle: H,
|
||||
process: P,
|
||||
state: T
|
||||
) -> Self where
|
||||
R: FnMut(&T, &mut Buffer, Rect) + Send + 'static,
|
||||
H: FnMut(&mut T, &EngineEvent) -> Result<(), Box<dyn Error>> + Send + 'static,
|
||||
P: FnMut(&mut T) + Send + 'static
|
||||
{
|
||||
Self {
|
||||
render: Mutex::new(Box::new(render)),
|
||||
handle: Mutex::new(Box::new(handle)),
|
||||
process: Mutex::new(Box::new(process)),
|
||||
state: Mutex::new(state),
|
||||
}
|
||||
}
|
||||
fn state (&self) -> std::sync::MutexGuard<'_, T> {
|
||||
self.state.lock().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Send + Sync> Device for DynamicDevice<T> {
|
||||
fn handle (&mut self, event: &EngineEvent) -> Result<(), Box<dyn Error>> {
|
||||
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
|
||||
}
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) {
|
||||
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for &dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
match self {
|
||||
Self::Transport(device) => device.render(area, buf),
|
||||
Self::Sequencer(device) => device.render(area, buf),
|
||||
Self::Sampler(device) => device.render(area, buf),
|
||||
Self::Mixer(device) => device.render(area, buf),
|
||||
Self::Looper(device) => device.render(area, buf),
|
||||
}
|
||||
Device::render(*self, buf, area)
|
||||
}
|
||||
}
|
||||
|
||||
impl HandleInput for Device {
|
||||
fn handle (&mut self, event: &crate::engine::Event) -> Result<(), Box<dyn Error>> {
|
||||
match self {
|
||||
Self::Transport(device) => device.handle(event),
|
||||
Self::Sequencer(device) => device.handle(event),
|
||||
Self::Sampler(device) => device.handle(event),
|
||||
Self::Mixer(device) => device.handle(event),
|
||||
Self::Looper(device) => device.handle(event),
|
||||
}
|
||||
impl WidgetRef for dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Device::render(self, buf, area)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
exited: Arc<AtomicBool>,
|
||||
sender: Sender<Event>,
|
||||
receiver: Receiver<Event>,
|
||||
pub jack_client: Jack<Notifications>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EngineEvent {
|
||||
/// 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 fn activate_jack_client <N: NotificationHandler + Sync + 'static> (
|
||||
client: Client,
|
||||
notifications: N,
|
||||
handler: BoxedProcessHandler
|
||||
) -> Result<Jack<N>, Box<dyn Error>> {
|
||||
Ok(client.activate_async(notifications, ClosureProcessHandler::new(handler))?)
|
||||
}
|
||||
|
||||
fn panic_hook (info: &std::panic::PanicInfo) {
|
||||
stdout()
|
||||
.execute(crossterm::terminal::LeaveAlternateScreen)
|
||||
.unwrap();
|
||||
crossterm::terminal::disable_raw_mode()
|
||||
.unwrap();
|
||||
writeln!(std::io::stderr(), "{}", info)
|
||||
.unwrap();
|
||||
writeln!(std::io::stderr(), "{:?}", ::backtrace::Backtrace::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub struct Notifications;//(mpsc::Sender<self::Event>);
|
||||
|
||||
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");
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue