From a9cad1889184214f95c8fb0ee713c9d2fd4f1dd8 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 11 Jan 2025 19:13:59 +0100 Subject: [PATCH] remove Samplerviewer, SamplerStatus --- sampler/src/sampler_tui.rs | 118 ++++++++++++++++++------------------- tek/src/groovebox.rs | 2 +- 2 files changed, 59 insertions(+), 61 deletions(-) diff --git a/sampler/src/sampler_tui.rs b/sampler/src/sampler_tui.rs index ca569c70..9ea94988 100644 --- a/sampler/src/sampler_tui.rs +++ b/sampler/src/sampler_tui.rs @@ -124,68 +124,66 @@ render!(TuiOut: (self: SampleList<'a>) => { })) }); impl Sampler { + const EMPTY: &[(f64, f64)] = &[(0., 0.), (1., 1.), (2., 2.), (0., 2.), (2., 0.)]; pub fn viewer (&self, note_pt: usize) -> impl Content { - if let Some((_, sample)) = &self.recording { - SampleViewer(Some(sample.clone())) + let sample = if let Some((_, sample)) = &self.recording { + Some(sample.clone()) } else if let Some(sample) = &self.mapped[note_pt] { - SampleViewer(Some(sample.clone())) + Some(sample.clone()) } else { - SampleViewer(None) - } + None + }; + let min_db = -40.0; + RenderThunk::new(move|to: &mut TuiOut|{ + let [x, y, width, height] = to.area(); + let area = Rect { x, y, width, height }; + let (x_bounds, y_bounds, lines): ([f64;2], [f64;2], Vec) = + if let Some(sample) = &sample { + let sample = sample.read().unwrap(); + let start = sample.start as f64; + let end = sample.end as f64; + let length = end - start; + let step = length / width as f64; + let mut t = start; + let mut lines = vec![]; + while t < end { + let chunk = &sample.channels[0][t as usize..((t + step) as usize).min(sample.end)]; + let total: f32 = chunk.iter().map(|x|x.abs()).sum(); + let count = chunk.len() as f32; + let meter = 10. * (total / count).log10(); + let x = t as f64; + let y = meter as f64; + lines.push(Line::new(x, min_db, x, y, Color::Green)); + t += step / 2.; + } + ( + [sample.start as f64, sample.end as f64], + [min_db, 0.], + lines + ) + } else { + ( + [0.0, width as f64], + [0.0, height as f64], + vec![ + Line::new(0.0, 0.0, width as f64, height as f64, Color::Red), + Line::new(width as f64, 0.0, 0.0, height as f64, Color::Red), + ] + ) + }; + + Canvas::default() + .x_bounds(x_bounds) + .y_bounds(y_bounds) + .paint(|ctx| { for line in lines.iter() { ctx.draw(line) } }) + .render(area, &mut to.buffer); + }) + } + pub fn status (&self, index: usize) -> impl Content { + Tui::bold(true, Tui::fg(TuiTheme::g(224), self.mapped[index].as_ref().map(|sample|format!( + "Sample {}-{}", + sample.read().unwrap().start, + sample.read().unwrap().end, + )).unwrap_or_else(||"No sample".to_string()))) } } -const EMPTY: &[(f64, f64)] = &[(0., 0.), (1., 1.), (2., 2.), (0., 2.), (2., 0.)]; -pub struct SampleViewer(pub Option>>); -render!(TuiOut: |self: SampleViewer, to|{ - let [x, y, width, height] = to.area(); - let area = Rect { x, y, width, height }; - let min_db = -40.0; - let (x_bounds, y_bounds, lines): ([f64;2], [f64;2], Vec) = - if let Some(sample) = &self.0 { - let sample = sample.read().unwrap(); - let start = sample.start as f64; - let end = sample.end as f64; - let length = end - start; - let step = length / width as f64; - let mut t = start; - let mut lines = vec![]; - while t < end { - let chunk = &sample.channels[0][t as usize..((t + step) as usize).min(sample.end)]; - let total: f32 = chunk.iter().map(|x|x.abs()).sum(); - let count = chunk.len() as f32; - let meter = 10. * (total / count).log10(); - let x = t as f64; - let y = meter as f64; - lines.push(Line::new(x, min_db, x, y, Color::Green)); - t += step / 2.; - } - ( - [sample.start as f64, sample.end as f64], - [min_db, 0.], - lines - ) - } else { - ( - [0.0, width as f64], - [0.0, height as f64], - vec![ - Line::new(0.0, 0.0, width as f64, height as f64, Color::Red), - Line::new(width as f64, 0.0, 0.0, height as f64, Color::Red), - ] - ) - }; - - Canvas::default() - .x_bounds(x_bounds) - .y_bounds(y_bounds) - .paint(|ctx| { for line in lines.iter() { ctx.draw(line) } }) - .render(area, &mut to.buffer); -}); - -pub struct SamplerStatus<'a>(pub &'a Sampler, pub usize); - -render!(TuiOut: (self: SamplerStatus<'a>) => Tui::bold(true, Tui::fg(TuiTheme::g(224), self.0.mapped[self.1].as_ref().map(|sample|format!( - "Sample {}-{}", - sample.read().unwrap().start, - sample.read().unwrap().end, -)).unwrap_or_else(||"No sample".to_string())))); diff --git a/tek/src/groovebox.rs b/tek/src/groovebox.rs index ca424941..6e09b0e0 100644 --- a/tek/src/groovebox.rs +++ b/tek/src/groovebox.rs @@ -80,7 +80,7 @@ impl Groovebox { let sample_h = if self.compact { 0 } else { 5 }; Max::y(sample_h, Fill::xy( Bsp::a( - Fill::x(Align::w(Fixed::y(1, SamplerStatus(&self.sampler, note_pt)))), + Fill::x(Align::w(Fixed::y(1, self.sampler.status(note_pt)))), self.sampler.viewer(note_pt)))) } fn pool_view (&self) -> impl Content + use<'_> {