From 2877545140059b2d9f95afa83d225515dded803f Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 25 Apr 2025 20:56:36 +0300 Subject: [PATCH] sampler: arrows select slot --- crates/sampler/src/sampler_api.rs | 8 +++---- crates/sampler/src/sampler_model.rs | 36 +++++++++++++++++++++++++---- crates/sampler/src/sampler_view.rs | 15 ++---------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/crates/sampler/src/sampler_api.rs b/crates/sampler/src/sampler_api.rs index b07c887d..295f0cce 100644 --- a/crates/sampler/src/sampler_api.rs +++ b/crates/sampler/src/sampler_api.rs @@ -8,10 +8,10 @@ expose! { [f32] => {} [u7] => {} [usize] => { - ":sample-up" => 0, - ":sample-down" => 0, - ":sample-left" => 0, - ":sample-right" => 0, + ":sample-up" => self.note_pos().min(119) + 8, + ":sample-down" => self.note_pos().max(8) - 8, + ":sample-left" => self.note_pos().min(126) + 1, + ":sample-right" => self.note_pos().max(1) - 1, } } } diff --git a/crates/sampler/src/sampler_model.rs b/crates/sampler/src/sampler_model.rs index 90236a2d..794b3755 100644 --- a/crates/sampler/src/sampler_model.rs +++ b/crates/sampler/src/sampler_model.rs @@ -14,14 +14,16 @@ pub struct Sampler { pub audio_outs: Vec, pub buffer: Vec>, pub output_gain: f32, - pub cursor: (usize, usize), pub editing: Option>>, pub mode: Option, /// Size of actual notes area pub size: Measure, /// Lowest note displayed pub note_lo: AtomicUsize, + /// Selected note pub note_pt: AtomicUsize, + /// Selected note as row/col + pub cursor: (AtomicUsize, AtomicUsize), pub color: ItemPalette } @@ -40,11 +42,11 @@ impl Default for Sampler { output_gain: 1., recording: None, mode: None, - cursor: (0, 0), editing: None, size: Default::default(), note_lo: 0.into(), note_pt: 0.into(), + cursor: (0.into(), 0.into()), color: Default::default(), } } @@ -94,17 +96,43 @@ impl Sampler { /// Immutable reference to sample at cursor. pub fn sample (&self) -> Option<&Arc>> { for (i, sample) in self.mapped.iter().enumerate() { - if i == self.cursor.0 { + if i == self.cursor().0 { return sample.as_ref() } } for (i, sample) in self.unmapped.iter().enumerate() { - if i + self.mapped.len() == self.cursor.0 { + if i + self.mapped.len() == self.cursor().0 { return Some(sample) } } None } + /// Value of cursor + pub fn cursor (&self) -> (usize, usize) { + (self.cursor.0.load(Relaxed), self.cursor.1.load(Relaxed)) + } +} + +impl NoteRange for Sampler { + fn note_lo (&self) -> &AtomicUsize { + &self.note_lo + } + fn note_axis (&self) -> &AtomicUsize { + &self.size.y + } +} + +impl NotePoint for Sampler { + fn note_len (&self) -> usize {0/*TODO*/} + fn set_note_len (&self, x: usize) {} + fn note_pos (&self) -> usize { + self.note_pt.load(Relaxed) + } + fn set_note_pos (&self, x: usize) { + self.note_pt.store(x, Relaxed); + self.cursor.0.store(x % 8, Relaxed); + self.cursor.1.store(x / 8, Relaxed); + } } /// A sound sample. diff --git a/crates/sampler/src/sampler_view.rs b/crates/sampler/src/sampler_view.rs index 926e1a13..1faec29a 100644 --- a/crates/sampler/src/sampler_view.rs +++ b/crates/sampler/src/sampler_view.rs @@ -22,10 +22,11 @@ impl Sampler { pub fn view_grid_cell <'a> ( &'a self, name: &'a str, x: u16, y: u16, w: u16, h: u16 ) -> impl Content + use<'a> { + let cursor = self.cursor(); let hi_fg = Color::Rgb(64, 64, 64); let hi_bg = if y == 0 { Color::Reset } else { Color::Rgb(64, 64, 64) /*prev*/ }; let tx_fg = Color::Rgb(255, 255, 255); - let tx_bg = if x as usize == self.cursor.0 && y as usize == self.cursor.1 { + let tx_bg = if x as usize == cursor.0 && y as usize == cursor.1 { Color::Rgb(96, 96, 96) } else { Color::Rgb(64, 64, 64) @@ -85,18 +86,6 @@ render!(TuiOut: |self: SamplesTui, to| { } }); -impl NoteRange for Sampler { - fn note_lo (&self) -> &AtomicUsize { &self.note_lo } - fn note_axis (&self) -> &AtomicUsize { &self.size.y } -} - -impl NotePoint for Sampler { - fn note_len (&self) -> usize {0/*TODO*/} - fn set_note_len (&self, x: usize) {} - fn note_pos (&self) -> usize { self.note_pt.load(Relaxed) } - fn set_note_pos (&self, x: usize) { self.note_pt.store(x, Relaxed); } -} - impl Sampler { const _EMPTY: &[(f64, f64)] = &[(0., 0.), (1., 1.), (2., 2.), (0., 2.), (2., 0.)]; pub fn list <'a> (&'a self, compact: bool, editor: &MidiEditor) -> impl Content + 'a {