extract has_sampler

This commit is contained in:
🪞👃🪞 2025-04-04 01:53:44 +03:00
parent 09c8e651d4
commit 432b916823
3 changed files with 34 additions and 30 deletions

View file

@ -0,0 +1,30 @@
use crate::*;
pub trait HasSampler {
fn sampler (&self) -> &Option<Sampler>;
fn sampler_mut (&mut self) -> &mut Option<Sampler>;
fn sample_index (&self) -> usize;
fn view_sample <'a> (&'a self, compact: bool) -> impl Content<TuiOut> + 'a {
self.sampler().as_ref().map(|sampler|Max::y(
if compact { 0u16 } else { 5 }.into(),
Fill::x(sampler.viewer(self.sample_index()))
))
}
fn view_sampler <'a> (&'a self, compact: bool, editor: &Option<MidiEditor>) -> impl Content<TuiOut> + 'a {
self.sampler().as_ref().map(|sampler|Fixed::x(
if compact { 4u16 } else { 40 }.into(),
Push::y(
if compact { 1u16 } else { 0 }.into(),
editor.as_ref().map(|e|Fill::y(sampler.list(compact, e)))
)
))
}
}
#[macro_export] macro_rules! has_sampler {
(|$self:ident:$Struct:ty| { sampler = $e0:expr; index = $e1:expr; }) => {
impl HasSampler for $Struct {
fn sampler (&$self) -> &Option<Sampler> { &$e0 }
fn sampler_mut (&mut $self) -> &mut Option<Sampler> { &mut $e0 }
fn sample_index (&$self) -> usize { $e1 }
}
}
}

View file

@ -25,3 +25,5 @@ pub(crate) use ratatui::{prelude::Rect, widgets::{Widget, canvas::{Canvas, Line}
// TODO!
let sample = Sample::new("test", 0, 0, vec![]);
}
mod has_sampler; pub use self::has_sampler::*;

View file

@ -1,33 +1,4 @@
use crate::*;
pub trait HasSampler {
fn sampler (&self) -> &Option<Sampler>;
fn sampler_mut (&mut self) -> &mut Option<Sampler>;
fn sample_index (&self) -> usize;
fn view_sample <'a> (&'a self, compact: bool) -> impl Content<TuiOut> + 'a {
self.sampler().as_ref().map(|sampler|Max::y(
if compact { 0u16 } else { 5 }.into(),
Fill::x(sampler.viewer(self.sample_index()))
))
}
fn view_sampler <'a> (&'a self, compact: bool, editor: &Option<MidiEditor>) -> impl Content<TuiOut> + 'a {
self.sampler().as_ref().map(|sampler|Fixed::x(
if compact { 4u16 } else { 40 }.into(),
Push::y(
if compact { 1u16 } else { 0 }.into(),
editor.as_ref().map(|e|Fill::y(sampler.list(compact, e)))
)
))
}
}
#[macro_export] macro_rules! has_sampler {
(|$self:ident:$Struct:ty| { sampler = $e0:expr; index = $e1:expr; }) => {
impl HasSampler for $Struct {
fn sampler (&$self) -> &Option<Sampler> { &$e0 }
fn sampler_mut (&mut $self) -> &mut Option<Sampler> { &mut $e0 }
fn sample_index (&$self) -> usize { $e1 }
}
}
}
/// The sampler device plays sounds in response to MIDI notes.
#[derive(Debug)] pub struct Sampler {
pub jack: Jack,
@ -837,7 +808,8 @@ impl Sampler {
let offset = |a|Push::y(i as u16, Align::n(Fixed::y(1, Fill::x(a))));
let mut bg = if note == note_pt { Tui::g(64) } else { Color::Reset };
let mut fg = Tui::g(160);
if self.mapped[note].is_some() {
let mapped: &Option<Arc<RwLock<Sample>>> = &self.mapped[note];
if mapped.is_some() {
fg = Tui::g(224);
bg = Color::Rgb(0, if note == note_pt { 96 } else { 64 }, 0);
}