refactors and cleanups

This commit is contained in:
🪞👃🪞 2024-07-10 21:53:38 +03:00
parent 78afaf9693
commit 6979fd67ec
8 changed files with 126 additions and 173 deletions

View file

@ -20,14 +20,15 @@ pub fn fill_bg (buf: &mut Buffer, area: Rect, color: Color) {
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>);
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Usually<Rect>;
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Usually<Rect> {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()));
}
Ok(Rect { x, y, width: self.as_ref().len() as u16, height: 1 })
}
}

View file

@ -124,26 +124,28 @@ impl Track {
_ => {}
});
let (left, right) = (app.audio_out(0), app.audio_out(1));
app.add_track_with_cb(Some(name.as_str()), move|_, track|{
for phrase in phrases {
track.phrases.push(phrase);
let track = app.add_track(Some(name.as_str()))?;
for phrase in phrases {
track.phrases.push(phrase);
}
for device in devices {
track.add_device(device)?;
}
if let Some(device) = track.devices.get(0) {
device.client.as_client().connect_ports(
&track.midi_out,
&device.midi_ins()?[0],
)?;
}
if let Some(device) = track.devices.get(track.devices.len() - 1) {
if let Some(ref left) = left {
device.connect_audio_out(0, left)?;
}
for device in devices {
track.add_device(device)?;
if let Some(ref right) = right {
device.connect_audio_out(1, right)?;
}
if let Some(device) = track.devices.get(0) {
//device.connect_midi_in(0, &track.midi_out.clone_unowned())?;
}
if let Some(device) = track.devices.get(track.devices.len() - 1) {
if let Some(ref left) = left {
device.connect_audio_out(0, left)?;
}
if let Some(ref right) = right {
device.connect_audio_out(1, right)?;
}
}
Ok(())
})
}
Ok(track)
}
}

View file

@ -139,41 +139,3 @@ pub fn jack_run <T: Sync> (name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicA
}) as BoxedProcessHandler)
)?)
}
pub trait ProcessSplit {
fn process_in (&mut self, _: &Client, _: &ProcessScope) -> Usually<()>;
fn process_out (&self, _: &Client, _: &ProcessScope) -> Usually<()>;
}
impl<T: ProcessSplit> Process for T {
fn process (&mut self, c: &Client, s: &ProcessScope) -> Control {
self.process_in(c, s).unwrap();
self.process_out(c, s).unwrap();
Control::Continue
}
}
/// Just run thing with JACK. Returns the activated client.
pub fn jack_run_split <T: Sync> (name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicAsyncClient>
where T: Handle + ProcessSplit + Send + 'static
{
let options = ClientOptions::NO_START_SERVER;
let (client, _status) = Client::new(name, options)?;
Ok(client.activate_async(
Notifications(Box::new({
let _app = app.clone();
move|_event|{
// FIXME: this deadlocks
//app.lock().unwrap().handle(&event).unwrap();
}
}) as Box<dyn Fn(AppEvent) + Send + Sync>),
ClosureProcessHandler::new(Box::new({
let app = app.clone();
move|c: &Client, s: &ProcessScope|{
app.write().unwrap().process_in(c, s).unwrap();
app.read().unwrap().process_out(c, s).unwrap();
Control::Continue
}
}) as BoxedProcessHandler)
)?)
}

View file

@ -14,9 +14,8 @@ use crate::{core::*, model::*};
/// Application entrypoint.
pub fn main () -> Usually<()> {
let mut init = App::new()?;
init.connect_to_midi_ins(&["nanoKEY Studio.*capture.*"])?
.connect_to_audio_outs(&["Komplete.+:playback_FL", "Komplete.+:playback_FR"])?
.load_edn(include_str!("../demos/project.edn"))?;
init.activate().map(|app|run(app)).map(|_|())
let mut app = App::new(include_str!("../demos/project.edn"))?;
app.connect_to_midi_ins(&["nanoKEY Studio.*capture.*"])?;
app.connect_to_audio_outs(&["Komplete.+:playback_FL", "Komplete.+:playback_FR"])?;
run(app.activate()?).map(|_|())
}

View file

@ -73,12 +73,12 @@ pub struct App {
}
impl App {
pub fn new () -> Usually<Self> {
pub fn new (project: &str) -> Usually<Self> {
let xdg = Arc::new(microxdg::XdgApp::new("tek")?);
let first_run = crate::config::AppPaths::new(&xdg)?.should_create();
let jack = JackClient::Inactive(Client::new("tek", ClientOptions::NO_START_SERVER)?.0);
let transport = jack.transport();
Ok(Self {
let mut app = Self {
arranger_mode: false,
audio_outs: vec![],
chain_mode: false,
@ -106,7 +106,9 @@ impl App {
tracks: vec![],
transport: Some(transport),
xdg: Some(xdg),
})
};
app.load_edn(project)?;
Ok(app)
}
}

View file

@ -19,13 +19,11 @@ use crate::{render, App, core::*};
render!(App |self, buf, area| {
Split::down([
&TransportView::new(self),
&Split::down([
&ArrangerView::new(&self, !self.arranger_mode),
&If(self.track_cursor > 0, &Split::right([
&ChainView::vertical(&self),
&SequencerView::new(&self),
]))
])
&ArrangerView::new(&self, !self.arranger_mode),
&If(self.track_cursor > 0, &Split::right([
&ChainView::vertical(&self),
&SequencerView::new(&self),
]))
]).render(buf, area)?;
if let Some(ref modal) = self.modal {
modal.render(buf, area)?;

View file

@ -114,7 +114,7 @@ impl<'a> SequencerView<'a> {
let step = (time0 + (x-offset) as usize) * time_z;
let next_step = (time0 + (x-offset) as usize + 1) * time_z;
let style = Self::style_timer_step(now, step, next_step);
"-".blit(buf, x, area.y, Some(style))
"-".blit(buf, x, area.y, Some(style));
}
}

View file

@ -1,118 +1,107 @@
use crate::{core::*, model::App};
pub struct TransportView<'a> {
pub timebase: &'a Arc<Timebase>,
use crate::{core::*, view::Split, model::App};
pub struct TransportView {
pub playing: TransportState,
pub record: bool,
pub overdub: bool,
pub monitor: bool,
pub frame: usize,
pub quant: usize,
pub ppq: usize,
pub bpm: usize,
pub pulse: usize,
pub usecs: usize,
}
impl<'a> TransportView<'a> {
pub fn new (app: &'a App) -> Self {
impl TransportView {
pub fn new (app: &App) -> Self {
Self {
timebase: &app.timebase,
playing: *app.playing.as_ref().unwrap_or(&TransportState::Stopped),
monitor: app.track().map(|t|t.1.monitoring).unwrap_or(false),
record: app.track().map(|t|t.1.recording).unwrap_or(false),
overdub: app.track().map(|t|t.1.overdub).unwrap_or(false),
frame: app.playhead,
quant: app.quant,
ppq: app.timebase.ppq() as usize,
bpm: app.timebase.bpm() as usize,
pulse: app.timebase.frame_to_pulse(app.playhead as f64) as usize,
usecs: app.timebase.frame_to_usec(app.playhead as f64) as usize,
}
}
}
render!(TransportView |self, buf, area| {
let Self { ppq, frame, bpm, quant, pulse, usecs, .. } = self;
let frame = *frame as f64;
let Rect { x, y, width, .. } = area;
fill_bg(buf, Rect {
x, y, width, height: if width > 100 { 1 } else { 2 }
}, Color::Rgb(20, 45, 5));
let mut area = Split::right([
impl<'a> Render for TransportView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
fill_bg(buf, area, Color::Rgb(20, 45, 5));
let Rect { x, y, width, .. } = area;
draw_play_stop(buf, x + 1, y, &self.playing);
if width > 100 {
draw_rec(buf, x + 12, y, self.record);
draw_dub(buf, x + 19, y, self.overdub);
draw_mon(buf, x + 26, y, self.monitor);
draw_bpm(buf, x + 33, y, self.timebase.bpm() as usize, self.quant);
draw_timer(buf, x + width - 1, y,
self.timebase.ppq() as usize,
self.timebase.frame_to_pulse(self.frame as f64) as usize,
self.timebase.frame_to_usec(self.frame as f64) as usize,
);
Ok(Rect { x, y, width, height: 1 })
} else {
draw_bpm(buf, x + 12, y, self.timebase.bpm() as usize, self.quant);
draw_timer(buf, x + width - 1, y,
self.timebase.ppq() as usize,
self.timebase.frame_to_pulse(self.frame as f64) as usize,
self.timebase.frame_to_usec(self.frame as f64) as usize,
);
draw_rec(buf, x + 1, y + 1, self.record);
draw_dub(buf, x + 8, y + 1, self.overdub);
draw_mon(buf, x + 15, y + 1, self.monitor);
Ok(Rect { x, y, width, height: 2 })
// Play/Pause button
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
let style = Style::default().gray();
let style = Some(match &self.playing {
TransportState::Stopped => style.dim().bold(),
TransportState::Starting => style.not_dim().bold(),
TransportState::Rolling => style.not_dim().white().bold()
});
let label = match &self.playing {
TransportState::Rolling => "▶ PLAYING",
TransportState::Starting => "READY ...",
TransportState::Stopped => "⏹ STOPPED",
};
label.blit(buf, x, y, style)
},
// Record button/indicator
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
"⏺ REC".blit(buf, x, y, Some(if self.record {
Style::default().bold().red()
} else {
Style::default().bold().dim()
}))
},
// Overdub button/indicator
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
"⏺ DUB".blit(buf, x, y, Some(if self.overdub {
Style::default().bold().yellow()
} else {
Style::default().bold().dim()
}))
},
// Monitor button/indicator
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
"⏺ MON".blit(buf, x, y, Some(if self.monitor {
Style::default().bold().green()
} else {
Style::default().bold().dim()
}))
},
// Time settings
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
let style = Style::default().not_dim();
"BPM".blit(buf, x, y, Some(style))?;
format!("{}.{:03}", bpm, bpm % 1).blit(buf, x + 4, y, Some(style.bold()))?;
"SYNC".blit(buf, x + 13, y, Some(style))?;
"4/4".blit(buf, x + 18, y, Some(style.bold()))?;
"QUANT".blit(buf, x + 23, y, Some(style))?;
ppq_to_name(*quant).blit(buf, x + 29, y, Some(style.bold()))?;
Ok(Rect { x, y, width: 40, height: 1 })
},
// Clock
&|buf: &mut Buffer, Rect { x, y, .. }: Rect|{
let (beats, pulses) = (pulse / ppq, pulse % ppq);
let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1);
let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000);
let (minutes, seconds) = (seconds / 60, seconds % 60);
let timer = format!("{minutes}:{seconds:02}:{msecs:03} {bars}.{beats}.{pulses:02}");
timer.blit(buf, x - timer.len() as u16, y, Some(Style::default().not_dim()))
}
}
}
pub fn draw_timer (buf: &mut Buffer, x: u16, y: u16, ppq: usize, pulse: usize, usecs: usize) {
let (beats, pulses) = (pulse / ppq, pulse % ppq);
let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1);
let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000);
let (minutes, seconds) = (seconds / 60, seconds % 60);
let timer = format!("{minutes}:{seconds:02}:{msecs:03} {bars}.{beats}.{pulses:02}");
timer.blit(buf, x - timer.len() as u16, y, Some(Style::default().not_dim()));
}
pub fn draw_play_stop (buf: &mut Buffer, x: u16, y: u16, state: &TransportState) {
let style = Style::default().gray();
match state {
TransportState::Rolling => "▶ PLAYING",
TransportState::Starting => "READY ...",
TransportState::Stopped => "⏹ STOPPED",
}.blit(buf, x, y, Some(match state {
TransportState::Stopped => style.dim().bold(),
TransportState::Starting => style.not_dim().bold(),
TransportState::Rolling => style.not_dim().white().bold()
}));
}
pub fn draw_rec (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ REC".blit(buf, x, y, Some(if on {
Style::default().bold().red()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_dub (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ DUB".blit(buf, x, y, Some(if on {
Style::default().bold().yellow()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_mon (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ MON".blit(buf, x, y, Some(if on {
Style::default().bold().green()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_bpm (buf: &mut Buffer, x: u16, y: u16, bpm: usize, quant: usize) {
let style = Style::default().not_dim();
"BPM"
.blit(buf, x, y, Some(style));
format!("{}.{:03}", bpm as usize, bpm % 1)
.blit(buf, x + 4, y, Some(style.bold()));
"SYNC"
.blit(buf, x + 13, y, Some(style));
"4/4"
.blit(buf, x + 18, y, Some(style.bold()));
"QUANT"
.blit(buf, x + 23, y, Some(style));
ppq_to_name(quant)
.blit(buf, x + 29, y, Some(style.bold()));
}
]).render(buf, area)?;
area.height = 1;
Ok(area)
});