refactor sampler, flatten arranger

This commit is contained in:
🪞👃🪞 2025-04-24 19:33:22 +03:00
parent a9d22bd26f
commit 9f70441627
28 changed files with 1816 additions and 1836 deletions

View file

@ -1,13 +1,7 @@
use crate::*;
mod view_clock; pub use self::view_clock::*;
mod view_memo; pub use self::view_memo::*;
mod view_meter; pub use self::view_meter::*;
mod view_ports; pub use self::view_ports::*;
mod view_layout; pub use self::view_layout::*;
pub(crate) use std::fmt::Write;
pub(crate) use ::tengri::tui::ratatui::prelude::Position;
pub(crate) trait ScenesColors<'a> = Iterator<Item=SceneWithColor<'a>>;
pub(crate) type SceneWithColor<'a> = (usize, &'a Scene, usize, usize, Option<ItemPalette>);
pub(crate) struct ArrangerView<'a> {
app: &'a Tek,
@ -88,6 +82,143 @@ impl<'a> ArrangerView<'a> {
}
}
/// Render input matrix.
pub(crate) fn inputs (&'a self) -> impl Content<TuiOut> + 'a {
Tui::bg(Color::Reset,
Bsp::s(Bsp::s(self.input_routes(), self.input_ports()), self.input_intos()))
}
fn input_routes (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(self.inputs_height)
.left(self.width_side,
io_ports(Tui::g(224), Tui::g(32), ||self.app.inputs_with_sizes()))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.app.tracks_with_sizes(),
move|_, &Track { color, .. }|{
io_conns(color.dark.rgb, color.darker.rgb, ||self.app.inputs_with_sizes())
}
))
}
fn input_ports (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(1)
.left(self.width_side,
button_3("i", "midi ins", format!("{}", self.inputs_count), self.is_editing))
.right(self.width_side,
button_2("I", "add midi in", self.is_editing))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.app.tracks_with_sizes(),
move|t, track|{
let rec = track.player.recording;
let mon = track.player.monitoring;
let rec = if rec { White } else { track.color.darkest.rgb };
let mon = if mon { White } else { track.color.darkest.rgb };
let bg = if self.track_selected == Some(t) {
track.color.light.rgb
} else {
track.color.base.rgb
};
//let bg2 = if t > 0 { track.color.base.rgb } else { Reset };
wrap(bg, Tui::g(224), Tui::bold(true, Fill::x(Bsp::e(
Tui::fg_bg(rec, bg, "Rec "),
Tui::fg_bg(mon, bg, "Mon "),
))))
}))
}
fn input_intos (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(2)
.left(self.width_side,
Bsp::s(Align::e("Input:"), Align::e("Into:")))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.app.tracks_with_sizes(),
|_, _|{
Tui::bg(Reset, Align::c(Bsp::s(OctaveVertical::default(), " ------ ")))
}))
}
/// Render output matrix.
pub(crate) fn outputs (&'a self) -> impl Content<TuiOut> + 'a {
Tui::bg(Color::Reset, Align::n(Bsp::s(
Bsp::s(
self.output_nexts(),
self.output_froms(),
),
Bsp::s(
self.output_ports(),
self.output_conns(),
)
)))
}
fn output_nexts (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(2)
.left(self.width_side, Align::ne("From:"))
.middle(self.width_mid, per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
|_, _|{
Tui::bg(Reset, Align::c(Bsp::s(" ------ ", OctaveVertical::default())))
}))
}
fn output_froms (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(2)
.left(self.width_side, Align::ne("Next:"))
.middle(self.width_mid, per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
|t, track|Either(
track.player.next_clip.is_some(),
Thunk::new(||Tui::bg(Reset, format!("{:?}",
track.player.next_clip.as_ref()
.map(|(moment, clip)|clip.as_ref()
.map(|clip|clip.read().unwrap().name.clone()))
.flatten().as_ref()))),
Thunk::new(||Tui::bg(Reset, " ------ "))
)))
}
fn output_ports (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(1)
.left(self.width_side,
button_3("o", "midi outs", format!("{}", self.outputs_count), self.is_editing))
.right(self.width_side,
button_2("O", "add midi out", self.is_editing))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
move|i, t|{
let mute = false;
let solo = false;
let mute = if mute { White } else { t.color.darkest.rgb };
let solo = if solo { White } else { t.color.darkest.rgb };
let bg_1 = if self.track_selected == Some(i) { t.color.light.rgb } else { t.color.base.rgb };
let bg_2 = if i > 0 { t.color.base.rgb } else { Reset };
let mute = Tui::fg_bg(mute, bg_1, "Play ");
let solo = Tui::fg_bg(solo, bg_1, "Solo ");
wrap(bg_1, Tui::g(224), Tui::bold(true, Fill::x(Bsp::e(mute, solo))))
}))
}
fn output_conns (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(self.outputs_height)
.left(self.width_side,
io_ports(Tui::g(224), Tui::g(32), ||self.app.outputs_with_sizes()))
.middle(self.width_mid, per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
|_, t|io_conns(
t.color.dark.rgb,
t.color.darker.rgb,
||self.app.outputs_with_sizes()
)))
}
/// Render track headers
pub(crate) fn tracks (&'a self) -> impl Content<TuiOut> + 'a {
let Self { width_side, width_mid, track_count, track_selected, is_editing, .. } = self;
@ -238,6 +369,10 @@ impl<'a> ArrangerView<'a> {
}
trait ScenesColors<'a> = Iterator<Item=SceneWithColor<'a>>;
type SceneWithColor<'a> = (usize, &'a Scene, usize, usize, Option<ItemPalette>);
impl Tek {
/// Spacing between tracks.
pub(crate) const TRACK_SPACING: usize = 0;
@ -345,36 +480,356 @@ impl Tek {
})
}
fn update_clock (&self) {
ViewCache::update_clock(&self.view_cache, self.clock(), self.size.w() > 80)
}
pub fn view_transport (&self) -> impl Content<TuiOut> + use<'_> {
self.update_clock();
let cache = self.view_cache.read().unwrap();
view_transport(
self.clock.is_rolling(),
cache.bpm.view.clone(),
cache.beat.view.clone(),
cache.time.view.clone(),
)
}
pub fn view_status (&self) -> impl Content<TuiOut> + use<'_> {
self.update_clock();
let cache = self.view_cache.read().unwrap();
view_status(
self.selected.describe(&self.tracks, &self.scenes),
cache.sr.view.clone(),
cache.buf.view.clone(),
cache.lat.view.clone(),
)
}
}
/// Define a type alias for iterators of sized items (columns).
macro_rules! def_sizes_iter {
($Type:ident => $($Item:ty),+) => {
pub(crate) trait $Type<'a> =
Iterator<Item=(usize, $(&'a $Item,)+ usize, usize)> + Send + Sync + 'a;}}
Iterator<Item=(usize, $(&'a $Item,)+ usize, usize)> + Send + Sync + 'a;
}
}
def_sizes_iter!(ScenesSizes => Scene);
def_sizes_iter!(TracksSizes => Track);
def_sizes_iter!(InputsSizes => JackMidiIn);
def_sizes_iter!(OutputsSizes => JackMidiOut);
def_sizes_iter!(PortsSizes => Arc<str>, [PortConnect]);
#[cfg(test)] #[test] fn test_view_iter () {
let mut tek = Tek::default();
tek.editor = Some(Default::default());
let _: Vec<_> = tek.inputs_with_sizes().collect();
let _: Vec<_> = tek.outputs_with_sizes().collect();
let _: Vec<_> = tek.tracks_with_sizes().collect();
let _: Vec<_> = tek.scenes_with_sizes(true, 10, 10).collect();
//let _: Vec<_> = tek.scenes_with_colors(true, 10).collect();
//let _: Vec<_> = tek.scenes_with_track_colors(true, 10, 10).collect();
fn view_transport (
play: bool,
bpm: Arc<RwLock<String>>,
beat: Arc<RwLock<String>>,
time: Arc<RwLock<String>>,
) -> impl Content<TuiOut> {
let theme = ItemPalette::G[96];
Tui::bg(Black, row!(Bsp::a(
Fill::xy(Align::w(button_play_pause(play))),
Fill::xy(Align::e(row!(
FieldH(theme, "BPM", bpm),
FieldH(theme, "Beat", beat),
FieldH(theme, "Time", time),
)))
)))
}
#[cfg(test)] #[test] fn test_view_sizes () {
let app = Tek::default();
let _ = app.w();
let _ = app.w_sidebar();
let _ = app.w_tracks_area();
let _ = app.h();
let _ = app.h_tracks_area();
let _ = app.h_inputs();
let _ = app.h_outputs();
let _ = app.h_scenes();
fn view_status (
sel: Arc<str>,
sr: Arc<RwLock<String>>,
buf: Arc<RwLock<String>>,
lat: Arc<RwLock<String>>,
) -> impl Content<TuiOut> {
let theme = ItemPalette::G[96];
Tui::bg(Black, row!(Bsp::a(
Fill::xy(Align::w(FieldH(theme, "Selected", sel))),
Fill::xy(Align::e(row!(
FieldH(theme, "SR", sr),
FieldH(theme, "Buf", buf),
FieldH(theme, "Lat", lat),
)))
)))
}
fn button_play_pause (playing: bool) -> impl Content<TuiOut> {
let compact = true;//self.is_editing();
Tui::bg(
if playing{Rgb(0,128,0)}else{Rgb(128,64,0)},
Either::new(compact,
Thunk::new(move||Fixed::x(9, Either::new(playing,
Tui::fg(Rgb(0, 255, 0), " PLAYING "),
Tui::fg(Rgb(255, 128, 0), " STOPPED ")))
),
Thunk::new(move||Fixed::x(5, Either::new(playing,
Tui::fg(Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)),
Tui::fg(Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",))))
)
)
)
}
fn view_meter <'a> (label: &'a str, value: f32) -> impl Content<TuiOut> + 'a {
col!(
FieldH(ItemPalette::G[128], label, format!("{:>+9.3}", value)),
Fixed::xy(if value >= 0.0 { 13 }
else if value >= -1.0 { 12 }
else if value >= -2.0 { 11 }
else if value >= -3.0 { 10 }
else if value >= -4.0 { 9 }
else if value >= -6.0 { 8 }
else if value >= -9.0 { 7 }
else if value >= -12.0 { 6 }
else if value >= -15.0 { 5 }
else if value >= -20.0 { 4 }
else if value >= -25.0 { 3 }
else if value >= -30.0 { 2 }
else if value >= -40.0 { 1 }
else { 0 }, 1, Tui::bg(if value >= 0.0 { Red }
else if value >= -3.0 { Yellow }
else { Green }, ())))
}
fn view_meters (values: &[f32;2]) -> impl Content<TuiOut> + use<'_> {
Bsp::s(
format!("L/{:>+9.3}", values[0]),
format!("R/{:>+9.3}", values[1]),
)
}
pub(crate) fn wrap (
bg: Color, fg: Color, content: impl Content<TuiOut>
) -> impl Content<TuiOut> {
Bsp::e(Tui::fg_bg(bg, Reset, ""),
Bsp::w(Tui::fg_bg(bg, Reset, ""),
Tui::fg_bg(fg, bg, content)))
}
pub(crate) fn button_2 <'a, K, L> (
key: K, label: L, editing: bool,
) -> impl Content<TuiOut> + 'a where
K: Content<TuiOut> + 'a,
L: Content<TuiOut> + 'a,
{
let key = Tui::fg_bg(Tui::g(0), Tui::orange(), Bsp::e(
Tui::fg_bg(Tui::orange(), Reset, ""),
Bsp::e(key, Tui::fg(Tui::g(96), ""))
));
let label = When::new(!editing, Tui::fg_bg(Tui::g(255), Tui::g(96), label));
Tui::bold(true, Bsp::e(key, label))
}
pub(crate) fn button_3 <'a, K, L, V> (
key: K,
label: L,
value: V,
editing: bool,
) -> impl Content<TuiOut> + 'a where
K: Content<TuiOut> + 'a,
L: Content<TuiOut> + 'a,
V: Content<TuiOut> + 'a,
{
let key = Tui::fg_bg(Tui::g(0), Tui::orange(),
Bsp::e(Tui::fg_bg(Tui::orange(), Reset, ""), Bsp::e(key, Tui::fg(if editing {
Tui::g(128)
} else {
Tui::g(96)
}, ""))));
let label = Bsp::e(
When::new(!editing, Bsp::e(
Tui::fg_bg(Tui::g(255), Tui::g(96), label),
Tui::fg_bg(Tui::g(128), Tui::g(96), ""),
)),
Bsp::e(
Tui::fg_bg(Tui::g(224), Tui::g(128), value),
Tui::fg_bg(Tui::g(128), Reset, ""),
));
Tui::bold(true, Bsp::e(key, label))
}
pub(crate) fn heading <'a> (
key: &'a str,
label: &'a str,
count: usize,
content: impl Content<TuiOut> + Send + Sync + 'a,
editing: bool,
) -> impl Content<TuiOut> + 'a {
let count = format!("{count}");
Fill::xy(Align::w(Bsp::s(Fill::x(Align::w(button_3(key, label, count, editing))), content)))
}
pub(crate) fn io_ports <'a, T: PortsSizes<'a>> (
fg: Color, bg: Color, iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter,
move|(index, name, connections, y, y2): (usize, &'a Arc<str>, &'a [PortConnect], usize, usize), _|
map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, Tui::fg_bg(fg, bg, Align::w(Bsp::e(" 󰣲 ", name))))),
Map::new(||connections.iter(), move|connect: &'a PortConnect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, Tui::fg_bg(fg, bg,
&connect.info)))))))))
}
pub(crate) fn io_conns <'a, T: PortsSizes<'a>> (
fg: Color, bg: Color, iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter,
move|(index, name, connections, y, y2): (usize, &'a Arc<str>, &'a [PortConnect], usize, usize), _|
map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, wrap(bg, fg, Fill::x(Align::w("▞▞▞▞ ▞▞▞▞"))))),
Map::new(||connections.iter(), move|connect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, wrap(bg, fg, Fill::x(""))))))))))
}
pub(crate) fn per_track_top <'a, T: Content<TuiOut> + 'a, U: TracksSizes<'a>> (
width: u16,
tracks: impl Fn() -> U + Send + Sync + 'a,
callback: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Align::x(Tui::bg(Reset, Map::new(tracks,
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|{
let width = (x2 - x1) as u16;
map_east(x1 as u16, width, Fixed::x(width, Tui::fg_bg(
track.color.lightest.rgb,
track.color.base.rgb,
callback(index, track))))})))
}
pub(crate) fn per_track <'a, T: Content<TuiOut> + 'a, U: TracksSizes<'a>> (
width: u16,
tracks: impl Fn() -> U + Send + Sync + 'a,
callback: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
per_track_top(
width,
tracks,
move|index, track|Fill::y(Align::y(callback(index, track)))
)
}
/// Clear a pre-allocated buffer, then write into it.
#[macro_export] macro_rules! rewrite {
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
}
#[derive(Debug, Default)] pub(crate) struct ViewMemo<T, U> {
pub(crate) value: T,
pub(crate) view: Arc<RwLock<U>>
}
impl<T: PartialEq, U> ViewMemo<T, U> {
fn new (value: T, view: U) -> Self {
Self { value, view: Arc::new(view.into()) }
}
pub(crate) fn update <R> (
&mut self,
newval: T,
render: impl Fn(&mut U, &T, &T)->R
) -> Option<R> {
if newval != self.value {
let result = render(&mut*self.view.write().unwrap(), &newval, &self.value);
self.value = newval;
return Some(result);
}
None
}
}
#[derive(Debug)] pub struct ViewCache {
pub(crate) sr: ViewMemo<Option<(bool, f64)>, String>,
pub(crate) buf: ViewMemo<Option<f64>, String>,
pub(crate) lat: ViewMemo<Option<f64>, String>,
pub(crate) bpm: ViewMemo<Option<f64>, String>,
pub(crate) beat: ViewMemo<Option<f64>, String>,
pub(crate) time: ViewMemo<Option<f64>, String>,
pub(crate) scns: ViewMemo<Option<(usize, usize)>, String>,
pub(crate) trks: ViewMemo<Option<(usize, usize)>, String>,
pub(crate) stop: Arc<str>,
pub(crate) edit: Arc<str>,
}
impl Default for ViewCache {
fn default () -> Self {
let mut beat = String::with_capacity(16);
write!(beat, "{}", Self::BEAT_EMPTY);
let mut time = String::with_capacity(16);
write!(time, "{}", Self::TIME_EMPTY);
let mut bpm = String::with_capacity(16);
write!(bpm, "{}", Self::BPM_EMPTY);
Self {
beat: ViewMemo::new(None, beat),
time: ViewMemo::new(None, time),
bpm: ViewMemo::new(None, bpm),
sr: ViewMemo::new(None, String::with_capacity(16)),
buf: ViewMemo::new(None, String::with_capacity(16)),
lat: ViewMemo::new(None, String::with_capacity(16)),
scns: ViewMemo::new(None, String::with_capacity(16)),
trks: ViewMemo::new(None, String::with_capacity(16)),
stop: "".into(),
edit: "edit".into(),
}
}
}
impl ViewCache {
pub const BEAT_EMPTY: &'static str = "-.-.--";
pub const TIME_EMPTY: &'static str = "-.---s";
pub const BPM_EMPTY: &'static str = "---.---";
pub(crate) fn track_counter (cache: &Arc<RwLock<Self>>, track: usize, tracks: usize)
-> Arc<RwLock<String>>
{
let data = (track, tracks);
cache.write().unwrap().trks.update(Some(data), rewrite!(buf, "{}/{}", data.0, data.1));
cache.read().unwrap().trks.view.clone()
}
pub(crate) fn scene_add (cache: &Arc<RwLock<Self>>, scene: usize, scenes: usize, is_editing: bool)
-> impl Content<TuiOut>
{
let data = (scene, scenes);
cache.write().unwrap().scns.update(Some(data), rewrite!(buf, "({}/{})", data.0, data.1));
button_3("S", "add scene", cache.read().unwrap().scns.view.clone(), is_editing)
}
pub(crate) fn update_clock (cache: &Arc<RwLock<Self>>, clock: &Clock, compact: bool) {
let rate = clock.timebase.sr.get();
let chunk = clock.chunk.load(Relaxed) as f64;
let lat = chunk / rate * 1000.;
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
let mut cache = cache.write().unwrap();
cache.buf.update(Some(chunk), rewrite!(buf, "{chunk}"));
cache.lat.update(Some(lat), rewrite!(buf, "{lat:.1}ms"));
cache.sr.update(Some((compact, rate)), |buf,_,_|{
buf.clear();
if compact {
write!(buf, "{:.1}kHz", rate / 1000.)
} else {
write!(buf, "{:.0}Hz", rate)
}
});
if let Some(now) = clock.started.read().unwrap().as_ref().map(delta) {
let pulse = clock.timebase.usecs_to_pulse(now);
let time = now/1000000.;
let bpm = clock.timebase.bpm.get();
cache.beat.update(Some(pulse), |buf, _, _|{
buf.clear();
clock.timebase.format_beats_1_to(buf, pulse)
});
cache.time.update(Some(time), rewrite!(buf, "{:.3}s", time));
cache.bpm.update(Some(bpm), rewrite!(buf, "{:.3}", bpm));
} else {
cache.beat.update(None, rewrite!(buf, "{}", ViewCache::BEAT_EMPTY));
cache.time.update(None, rewrite!(buf, "{}", ViewCache::TIME_EMPTY));
cache.bpm.update(None, rewrite!(buf, "{}", ViewCache::BPM_EMPTY));
}
}
}