Skip to content

Commit

Permalink
add label to ActionTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Oct 23, 2024
1 parent 6a2f4be commit 3e04fd2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
52 changes: 28 additions & 24 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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 {
Expand Down Expand Up @@ -109,16 +111,6 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
Label::new(cx, "release").class("slider-label");
ParamSlider::new(cx, Data::params, |params| &params.global.release_ms)
.class("widget");
Label::new(
cx,
Data::params.map(|params| {
params.global.release_ms.normalized_value_to_string(
params.global.release_ms.modulated_normalized_value(),
true,
)
}),
)
.class("slider-label");
})
.class("row");
}) // TODO: make into a class
Expand Down Expand Up @@ -496,12 +488,6 @@ impl DelayGraph {
}
// TODO: .overflow(Overflow::Visible);

fn u8_note_to_name(note_nr: u8) -> 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,
Expand Down Expand Up @@ -548,7 +534,6 @@ pub struct ActionTrigger {
impl ActionTrigger {
pub fn new<IsLearningL, LearningIndexL, LearnedNotesL, LastPlayedNotesL>(
cx: &mut Context,

is_learning: IsLearningL,
learning_index: LearningIndexL,
own_index: usize,
Expand All @@ -568,31 +553,50 @@ 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);
}

// 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 {
self.last_played_notes
.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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Del2Params>,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e04fd2

Please sign in to comment.