From 3e04fd2974f5ca2cc154a130ec3d35d837138568 Mon Sep 17 00:00:00 2001 From: Bart Brouns Date: Wed, 23 Oct 2024 11:53:14 +0200 Subject: [PATCH] add label to ActionTrigger --- src/editor.rs | 52 +++++++++++++++++++++++++++------------------------ src/lib.rs | 12 +++++++++--- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index feb2650..83217da 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,5 +1,5 @@ use crate::util; -use nih_plug::prelude::{AtomicF32, Editor, Param}; +use nih_plug::prelude::{AtomicF32, Editor}; use nih_plug_vizia::vizia::prelude::*; use nih_plug_vizia::vizia::vg; use nih_plug_vizia::widgets::*; @@ -12,6 +12,8 @@ use crate::Del2Params; use crate::DelayData; use crate::DelayDataOutput; use crate::LastPlayedNotes; +use crate::LEARNING; +use crate::NO_LEARNED_NOTE; #[derive(Lens, Clone)] pub(crate) struct Data { @@ -109,16 +111,6 @@ pub fn create(editor_data: Data, editor_state: Arc) -> Option String { - let note_name = util::NOTES[(note_nr % 12) as usize]; - let octave = (note_nr / 12) - 1; - format!("{note_name}{octave}") - } - fn draw_bounding_outline( &self, canvas: &mut Canvas, @@ -548,7 +534,6 @@ pub struct ActionTrigger { impl ActionTrigger { pub fn new( cx: &mut Context, - is_learning: IsLearningL, learning_index: LearningIndexL, own_index: usize, @@ -568,15 +553,23 @@ impl ActionTrigger { learned_notes: learned_notes.get(cx), last_played_notes: last_played_notes.get(cx), } - .build(cx, |_cx| { - // Label::new(cx, "XXX").class("global-title"); - // put other widgets here + .build(cx, move |cx| { + Label::new( + cx, + learned_notes.clone().map(move |notes| { + let note_nr = notes.load(own_index); + ActionTrigger::get_note_name(note_nr) + }), + ) + .class("action-label"); }) } pub fn start_learning(&self) { self.is_learning.store(true, Ordering::SeqCst); - self.learning_index.store(self.own_index, Ordering::SeqCst); + let index = self.own_index; + self.learned_notes.store(index, LEARNING); + self.learning_index.store(index, Ordering::SeqCst); } pub fn stop_learning(&self) { self.is_learning.store(false, Ordering::SeqCst); @@ -584,8 +577,7 @@ impl ActionTrigger { // Checks if learning is active for this trigger pub fn is_learning(&self) -> bool { - self.is_learning.load(Ordering::SeqCst) - && self.learning_index.load(Ordering::SeqCst) == self.own_index + self.learned_notes.load(self.own_index) == LEARNING } pub fn is_playing(&self) -> bool { @@ -593,6 +585,18 @@ impl ActionTrigger { .is_playing(self.learned_notes.load(self.own_index)) } + fn get_note_name(note_nr: u8) -> String { + if note_nr == LEARNING { + "learning".to_string() + } else if note_nr == NO_LEARNED_NOTE { + "click to learn".to_string() + } else { + let note_name = util::NOTES[(note_nr % 12) as usize]; + let octave = (note_nr / 12) - 1; + format!("{note_name}{octave}") + } + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // for drawing ///////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/lib.rs b/src/lib.rs index e0f3cbc..eda595b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,9 @@ const VELOCITY_HIGH_NAME_PREFIX: &str = "high velocity"; const MAX_BLOCK_LEN: usize = 32768; const PEAK_METER_DECAY_MS: f64 = 150.0; const MAX_LEARNED_NOTES: usize = 8; +// abuse the difference in range between u8 and midi notes for special meaning +const NO_LEARNED_NOTE: u8 = 128; +const LEARNING: u8 = 129; struct Del2 { params: Arc, @@ -538,6 +541,10 @@ impl Plugin for Del2 { // Initialize filter parameters for each tap self.initialize_filter_parameters(); + for i in 0..MAX_LEARNED_NOTES { + self.learned_notes.store(i, NO_LEARNED_NOTE); + } + true } @@ -570,7 +577,6 @@ impl Plugin for Del2 { self.update_peak_meter(buffer, &self.input_meter); self.process_audio_blocks(buffer); - // self.apply_fade_out(buffer); self.update_peak_meter(buffer, &self.output_meter); ProcessStatus::Normal } @@ -982,11 +988,11 @@ impl Del2 { } } - pub fn is_playing(&self, index: usize) -> bool { + pub fn _is_playing(&self, index: usize) -> bool { self.last_played_notes .is_playing(self.learned_notes.load(index)) } - pub fn no_learned_note_are_playing(&self) -> bool { + pub fn _no_learned_note_are_playing(&self) -> bool { for i in 0..MAX_LEARNED_NOTES { if self .last_played_notes