Skip to content

Commit 3e04fd2

Browse files
committed
add label to ActionTrigger
1 parent 6a2f4be commit 3e04fd2

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/editor.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::util;
2-
use nih_plug::prelude::{AtomicF32, Editor, Param};
2+
use nih_plug::prelude::{AtomicF32, Editor};
33
use nih_plug_vizia::vizia::prelude::*;
44
use nih_plug_vizia::vizia::vg;
55
use nih_plug_vizia::widgets::*;
@@ -12,6 +12,8 @@ use crate::Del2Params;
1212
use crate::DelayData;
1313
use crate::DelayDataOutput;
1414
use crate::LastPlayedNotes;
15+
use crate::LEARNING;
16+
use crate::NO_LEARNED_NOTE;
1517

1618
#[derive(Lens, Clone)]
1719
pub(crate) struct Data {
@@ -109,16 +111,6 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
109111
Label::new(cx, "release").class("slider-label");
110112
ParamSlider::new(cx, Data::params, |params| &params.global.release_ms)
111113
.class("widget");
112-
Label::new(
113-
cx,
114-
Data::params.map(|params| {
115-
params.global.release_ms.normalized_value_to_string(
116-
params.global.release_ms.modulated_normalized_value(),
117-
true,
118-
)
119-
}),
120-
)
121-
.class("slider-label");
122114
})
123115
.class("row");
124116
}) // TODO: make into a class
@@ -496,12 +488,6 @@ impl DelayGraph {
496488
}
497489
// TODO: .overflow(Overflow::Visible);
498490

499-
fn u8_note_to_name(note_nr: u8) -> String {
500-
let note_name = util::NOTES[(note_nr % 12) as usize];
501-
let octave = (note_nr / 12) - 1;
502-
format!("{note_name}{octave}")
503-
}
504-
505491
fn draw_bounding_outline(
506492
&self,
507493
canvas: &mut Canvas,
@@ -548,7 +534,6 @@ pub struct ActionTrigger {
548534
impl ActionTrigger {
549535
pub fn new<IsLearningL, LearningIndexL, LearnedNotesL, LastPlayedNotesL>(
550536
cx: &mut Context,
551-
552537
is_learning: IsLearningL,
553538
learning_index: LearningIndexL,
554539
own_index: usize,
@@ -568,31 +553,50 @@ impl ActionTrigger {
568553
learned_notes: learned_notes.get(cx),
569554
last_played_notes: last_played_notes.get(cx),
570555
}
571-
.build(cx, |_cx| {
572-
// Label::new(cx, "XXX").class("global-title");
573-
// put other widgets here
556+
.build(cx, move |cx| {
557+
Label::new(
558+
cx,
559+
learned_notes.clone().map(move |notes| {
560+
let note_nr = notes.load(own_index);
561+
ActionTrigger::get_note_name(note_nr)
562+
}),
563+
)
564+
.class("action-label");
574565
})
575566
}
576567

577568
pub fn start_learning(&self) {
578569
self.is_learning.store(true, Ordering::SeqCst);
579-
self.learning_index.store(self.own_index, Ordering::SeqCst);
570+
let index = self.own_index;
571+
self.learned_notes.store(index, LEARNING);
572+
self.learning_index.store(index, Ordering::SeqCst);
580573
}
581574
pub fn stop_learning(&self) {
582575
self.is_learning.store(false, Ordering::SeqCst);
583576
}
584577

585578
// Checks if learning is active for this trigger
586579
pub fn is_learning(&self) -> bool {
587-
self.is_learning.load(Ordering::SeqCst)
588-
&& self.learning_index.load(Ordering::SeqCst) == self.own_index
580+
self.learned_notes.load(self.own_index) == LEARNING
589581
}
590582

591583
pub fn is_playing(&self) -> bool {
592584
self.last_played_notes
593585
.is_playing(self.learned_notes.load(self.own_index))
594586
}
595587

588+
fn get_note_name(note_nr: u8) -> String {
589+
if note_nr == LEARNING {
590+
"learning".to_string()
591+
} else if note_nr == NO_LEARNED_NOTE {
592+
"click to learn".to_string()
593+
} else {
594+
let note_name = util::NOTES[(note_nr % 12) as usize];
595+
let octave = (note_nr / 12) - 1;
596+
format!("{note_name}{octave}")
597+
}
598+
}
599+
596600
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
597601
// for drawing
598602
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const VELOCITY_HIGH_NAME_PREFIX: &str = "high velocity";
4747
const MAX_BLOCK_LEN: usize = 32768;
4848
const PEAK_METER_DECAY_MS: f64 = 150.0;
4949
const MAX_LEARNED_NOTES: usize = 8;
50+
// abuse the difference in range between u8 and midi notes for special meaning
51+
const NO_LEARNED_NOTE: u8 = 128;
52+
const LEARNING: u8 = 129;
5053

5154
struct Del2 {
5255
params: Arc<Del2Params>,
@@ -538,6 +541,10 @@ impl Plugin for Del2 {
538541
// Initialize filter parameters for each tap
539542
self.initialize_filter_parameters();
540543

544+
for i in 0..MAX_LEARNED_NOTES {
545+
self.learned_notes.store(i, NO_LEARNED_NOTE);
546+
}
547+
541548
true
542549
}
543550

@@ -570,7 +577,6 @@ impl Plugin for Del2 {
570577

571578
self.update_peak_meter(buffer, &self.input_meter);
572579
self.process_audio_blocks(buffer);
573-
// self.apply_fade_out(buffer);
574580
self.update_peak_meter(buffer, &self.output_meter);
575581
ProcessStatus::Normal
576582
}
@@ -982,11 +988,11 @@ impl Del2 {
982988
}
983989
}
984990

985-
pub fn is_playing(&self, index: usize) -> bool {
991+
pub fn _is_playing(&self, index: usize) -> bool {
986992
self.last_played_notes
987993
.is_playing(self.learned_notes.load(index))
988994
}
989-
pub fn no_learned_note_are_playing(&self) -> bool {
995+
pub fn _no_learned_note_are_playing(&self) -> bool {
990996
for i in 0..MAX_LEARNED_NOTES {
991997
if self
992998
.last_played_notes

0 commit comments

Comments
 (0)