mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
wip: running interface in separate or combined mode
also disassociating render functions from state structs
This commit is contained in:
parent
f9218e887a
commit
d6bf840a1f
31 changed files with 905 additions and 532 deletions
195
src/sampler.rs
195
src/sampler.rs
|
|
@ -1,18 +1,20 @@
|
|||
mod handle;
|
||||
mod jack;
|
||||
mod render;
|
||||
pub use self::handle::*;
|
||||
pub use self::jack::*;
|
||||
pub use self::render::*;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Sampler {
|
||||
jack: Jack<Notifications>,
|
||||
exit: bool,
|
||||
stdout: Stdout,
|
||||
cols: u16,
|
||||
rows: u16,
|
||||
samples: Vec<Sample>,
|
||||
selected_sample: usize,
|
||||
selected_column: usize,
|
||||
}
|
||||
|
||||
impl Sampler {
|
||||
|
||||
pub fn new () -> Result<Self, Box<dyn Error>> {
|
||||
let (client, status) = Client::new(
|
||||
"bloop-sampler",
|
||||
|
|
@ -28,9 +30,6 @@ impl Sampler {
|
|||
)?;
|
||||
Ok(Self {
|
||||
exit: false,
|
||||
stdout: std::io::stdout(),
|
||||
cols: 0,
|
||||
rows: 0,
|
||||
selected_sample: 0,
|
||||
selected_column: 0,
|
||||
samples: vec![
|
||||
|
|
@ -40,150 +39,6 @@ impl Sampler {
|
|||
jack,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn run_tui () -> Result<(), Box<dyn Error>> {
|
||||
let mut app = Self::new()?;
|
||||
let sleep = std::time::Duration::from_millis(16);
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
let (tx, input) = channel::<crossterm::event::Event>();
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
// Spawn the input thread
|
||||
let exit_input_thread = exited.clone();
|
||||
spawn(move || {
|
||||
loop {
|
||||
// Exit if flag is set
|
||||
if exit_input_thread.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
// Listen for events and send them to the main thread
|
||||
if crossterm::event::poll(Duration::from_millis(100)).is_ok() {
|
||||
if tx.send(crossterm::event::read().unwrap()).is_err() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
loop {
|
||||
app.render()?;
|
||||
app.handle(input.recv()?)?;
|
||||
if app.exit {
|
||||
app.stdout.queue(cursor::Hide)?.flush()?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
break
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle (&mut self, event: crossterm::event::Event) -> Result<(), Box<dyn Error>> {
|
||||
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||
if let Event::Key(event) = event {
|
||||
match event.code {
|
||||
KeyCode::Char('c') => {
|
||||
if event.modifiers == KeyModifiers::CONTROL {
|
||||
self.exit = true;
|
||||
}
|
||||
},
|
||||
KeyCode::Down => {
|
||||
self.selected_sample = (self.selected_sample + 1) % self.samples.len();
|
||||
println!("{}", self.selected_sample);
|
||||
},
|
||||
KeyCode::Up => {
|
||||
if self.selected_sample == 0 {
|
||||
self.selected_sample = self.samples.len() - 1;
|
||||
} else {
|
||||
self.selected_sample = self.selected_sample - 1;
|
||||
}
|
||||
println!("{}", self.selected_sample);
|
||||
},
|
||||
KeyCode::Left => {
|
||||
if self.selected_column == 0 {
|
||||
self.selected_column = 6
|
||||
} else {
|
||||
self.selected_column = self.selected_column - 1;
|
||||
}
|
||||
},
|
||||
KeyCode::Right => {
|
||||
if self.selected_column == 6 {
|
||||
self.selected_column = 0
|
||||
} else {
|
||||
self.selected_column = self.selected_column + 1;
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
println!("{event:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn render (&mut self) -> Result<(), Box<dyn Error>> {
|
||||
let (cols, rows) = terminal::size()?;
|
||||
self.cols = cols;
|
||||
self.rows = rows;
|
||||
self.stdout.queue(terminal::Clear(terminal::ClearType::All))?;
|
||||
self.stdout.queue(cursor::Hide)?;
|
||||
self.render_toolbar()?;
|
||||
self.render_table()?;
|
||||
self.render_meters()?;
|
||||
self.stdout.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn render_toolbar (&mut self) -> Result<(), Box<dyn Error>> {
|
||||
self.stdout
|
||||
.queue(cursor::MoveTo(1, 0))?
|
||||
.queue(PrintStyledContent("[Arrows]".yellow().bold()))?
|
||||
.queue(cursor::MoveTo(1, 1))?
|
||||
.queue(PrintStyledContent("Navigate".yellow()))?
|
||||
|
||||
.queue(cursor::MoveTo(12, 0))?
|
||||
.queue(PrintStyledContent("[Enter]".yellow().bold()))?
|
||||
.queue(cursor::MoveTo(12, 1))?
|
||||
.queue(PrintStyledContent("Play sample".yellow()))?
|
||||
|
||||
.queue(cursor::MoveTo(25, 0))?
|
||||
.queue(PrintStyledContent("[Ins/Del]".yellow().bold()))?
|
||||
.queue(cursor::MoveTo(25, 1))?
|
||||
.queue(PrintStyledContent("Add/remove sample".yellow()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn render_table (&mut self) -> Result<(), Box<dyn Error>> {
|
||||
self.stdout
|
||||
.queue(cursor::MoveTo(0, 3))?
|
||||
.queue(Print(
|
||||
" Name Rate Trigger Route"))?;
|
||||
for (i, sample) in self.samples.iter().enumerate() {
|
||||
let row = 4 + i as u16;
|
||||
for (j, (column, field)) in [
|
||||
(0, format!(" {:7} ", sample.name)),
|
||||
(9, format!(" {:.1}Hz ", sample.rate)),
|
||||
(18, format!(" MIDI C10 36 ")),
|
||||
(33, format!(" {:.1}dB -> Output ", sample.gain)),
|
||||
].into_iter().enumerate() {
|
||||
self.stdout.queue(cursor::MoveTo(column, row))?;
|
||||
if self.selected_sample == i && self.selected_column == j {
|
||||
self.stdout.queue(PrintStyledContent(field.to_string().bold().reverse()))?;
|
||||
} else {
|
||||
self.stdout.queue(PrintStyledContent(field.to_string().bold()))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn render_meters (&mut self) -> Result<(), Box<dyn Error>> {
|
||||
for (i, sample) in self.samples.iter().enumerate() {
|
||||
let row = 4 + i as u16;
|
||||
self.stdout
|
||||
.queue(cursor::MoveTo(32, row))?
|
||||
.queue(PrintStyledContent("▁".green()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct Sample {
|
||||
|
|
@ -207,41 +62,3 @@ impl Sample {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue