Skip to content

Commit

Permalink
add example label, wip trigger logic
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Oct 23, 2024
1 parent f6b2ae7 commit 6a2f4be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
15 changes: 12 additions & 3 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};
use nih_plug::prelude::{AtomicF32, Editor, Param};
use nih_plug_vizia::vizia::prelude::*;
use nih_plug_vizia::vizia::vg;
use nih_plug_vizia::widgets::*;
Expand Down Expand Up @@ -109,6 +109,16 @@ 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 @@ -579,7 +589,6 @@ impl ActionTrigger {
}

pub fn is_playing(&self) -> bool {
// self.last_played_notes.print_notes();
self.last_played_notes
.is_playing(self.learned_notes.load(self.own_index))
}
Expand Down Expand Up @@ -629,7 +638,7 @@ impl View for ActionTrigger {
Some("action-trigger")
}

fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
// We don't need special double and triple click handling
WindowEvent::MouseDown(MouseButton::Left)
Expand Down
60 changes: 45 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const VELOCITY_LOW_NAME_PREFIX: &str = "low velocity";
const VELOCITY_HIGH_NAME_PREFIX: &str = "high velocity";
// this seems to be the number JUCE is using
const MAX_BLOCK_LEN: usize = 32768;

const PEAK_METER_DECAY_MS: f64 = 150.0;
const MAX_LEARNED_NOTES: usize = 8;

struct Del2 {
params: Arc<Del2Params>,
Expand Down Expand Up @@ -559,7 +559,6 @@ impl Plugin for Del2 {
if self.is_learning.load(Ordering::SeqCst) {
self.counting_state = CountingState::MidiLearn;
}

self.process_midi_events(context);
self.prepare_for_delay(buffer.samples());

Expand Down Expand Up @@ -603,21 +602,23 @@ impl Del2 {
} => {
let should_record_tap = self.delay_data.current_tap < MAX_NR_TAPS;

let is_delay_note = !self.learned_notes.contains(note);
self.last_played_notes.note_on(note);

match self.counting_state {
CountingState::TimeOut => {
// If in TimeOut state, reset and start new counting phase
for tap in 0..MAX_NR_TAPS {
self.releasings[tap] = true;
self.amp_envelopes[tap].style = SmoothingStyle::Exponential(
self.params.global.release_ms.value(),
);
self.amp_envelopes[tap].set_target(self.sample_rate, 0.0);
if is_delay_note {
// If in TimeOut state, reset and start new counting phase
for tap in 0..MAX_NR_TAPS {
self.releasings[tap] = true;
self.amp_envelopes[tap].style = SmoothingStyle::Exponential(
self.params.global.release_ms.value(),
);
self.amp_envelopes[tap].set_target(self.sample_rate, 0.0);
}
self.delay_data.current_tap = 0;
self.timing_last_event = timing;
self.counting_state = CountingState::CountingInBuffer;
}
self.delay_data.current_tap = 0;
self.timing_last_event = timing;
self.counting_state = CountingState::CountingInBuffer;
}
CountingState::CountingInBuffer => {
// Validate and record a new tap within the buffer
Expand Down Expand Up @@ -664,6 +665,7 @@ impl Del2 {
)
&& self.samples_since_last_event > 0
&& velocity > 0.0
&& is_delay_note
{
// Update tap information with timing and velocity
let current_tap = self.delay_data.current_tap;
Expand All @@ -685,7 +687,7 @@ impl Del2 {
// TODO: instead of 1.0, use a combination of gain params
self.amp_envelopes[current_tap].set_target(self.sample_rate, 1.0);
self.delay_data.current_tap += 1;
self.delay_data.current_tap = self.delay_data.current_tap;
// self.delay_data.current_tap = self.delay_data.current_tap;

// Indicate filter update needed
self.should_update_filter.store(true, Ordering::Release);
Expand Down Expand Up @@ -979,6 +981,22 @@ impl Del2 {
}
}
}

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 {
for i in 0..MAX_LEARNED_NOTES {
if self
.last_played_notes
.is_playing(self.learned_notes.load(i))
{
return false;
}
}
true
}
}

impl ClapPlugin for Del2 {
Expand Down Expand Up @@ -1112,6 +1130,18 @@ impl AtomicByteArray {
let new_value = (self.data.load(Ordering::SeqCst) & mask) | ((byte as u64) << (index * 8));
self.data.store(new_value, Ordering::SeqCst);
}
fn contains(&self, byte: u8) -> bool {
let value = self.data.load(Ordering::SeqCst);
let byte_u64 = byte as u64;

for i in 0..8 {
let shifted_byte = (value >> (i * 8)) & 0xFF;
if shifted_byte == byte_u64 {
return true;
}
}
false
}
}

struct LastPlayedNotes {
Expand Down Expand Up @@ -1244,7 +1274,7 @@ impl LastPlayedNotes {
}

/// Print the notes for testing purposes.
fn print_notes(&self) {
fn _print_notes(&self) {
// Width used for formatting alignment
const WIDTH: usize = 4;

Expand Down

0 comments on commit 6a2f4be

Please sign in to comment.