Skip to content

Commit

Permalink
only one trigger at a time can be learning
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 15, 2024
1 parent b04c0f7 commit 70e2b0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
26 changes: 20 additions & 6 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub struct Data {
pub is_learning: Arc<AtomicBool>,
pub learning_index: Arc<AtomicUsize>,
pub learned_notes: Arc<AtomicByteArray>,
// to temp store the note we had during learning
// so we can keep abusing the notes above 127 to signify the states
// LEARNING and NO_LEARNED_NOTE but we can still go back
// to the last properly learned note before we started learning
pub last_learned_notes: Arc<AtomicByteArray>,
pub last_played_notes: Arc<LastPlayedNotes>,
pub enabled_actions: Arc<AtomicBoolArray>,
}
Expand Down Expand Up @@ -159,6 +164,7 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
Data::is_learning,
Data::learning_index,
Data::learned_notes,
Data::last_learned_notes,
Data::last_played_notes,
Data::enabled_actions,
MUTE_IN,
Expand All @@ -176,6 +182,7 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
Data::is_learning,
Data::learning_index,
Data::learned_notes,
Data::last_learned_notes,
Data::last_played_notes,
Data::enabled_actions,
CLEAR_TAPS,
Expand All @@ -197,6 +204,7 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
Data::is_learning,
Data::learning_index,
Data::learned_notes,
Data::last_learned_notes,
Data::last_played_notes,
Data::enabled_actions,
MUTE_OUT,
Expand All @@ -214,6 +222,7 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
Data::is_learning,
Data::learning_index,
Data::learned_notes,
Data::last_learned_notes,
Data::last_played_notes,
Data::enabled_actions,
LOCK_TAPS,
Expand Down Expand Up @@ -751,19 +760,18 @@ pub struct ActionTrigger {
is_learning: Arc<AtomicBool>,
learning_index: Arc<AtomicUsize>,
learned_notes: Arc<AtomicByteArray>,
last_learned_notes: Arc<AtomicByteArray>,
last_played_notes: Arc<LastPlayedNotes>,
enabled_actions: Arc<AtomicBoolArray>,
own_index: usize,
// to temp store the note we had during learning
// so we can keep abusing the notes above 127 to signify other things
last_learned_note: u8,
}
impl ActionTrigger {
pub fn new<
ParamsL,
IsLearningL,
LearningIndexL,
LearnedNotesL,
LastLearnedNotesL,
LastPlayedNotesL,
EnabledActionsL,
>(
Expand All @@ -772,6 +780,7 @@ impl ActionTrigger {
is_learning: IsLearningL,
learning_index: LearningIndexL,
learned_notes: LearnedNotesL,
last_learned_notes: LastLearnedNotesL,
last_played_notes: LastPlayedNotesL,
enabled_actions: EnabledActionsL,
own_index: usize,
Expand All @@ -781,6 +790,7 @@ impl ActionTrigger {
IsLearningL: Lens<Target = Arc<AtomicBool>>,
LearningIndexL: Lens<Target = Arc<AtomicUsize>>,
LearnedNotesL: Lens<Target = Arc<AtomicByteArray>>,
LastLearnedNotesL: Lens<Target = Arc<AtomicByteArray>>,
LastPlayedNotesL: Lens<Target = Arc<LastPlayedNotes>>,
EnabledActionsL: Lens<Target = Arc<AtomicBoolArray>>,
{
Expand All @@ -789,10 +799,10 @@ impl ActionTrigger {
is_learning: is_learning.get(cx),
learning_index: learning_index.get(cx),
learned_notes: learned_notes.get(cx),
last_learned_notes: last_learned_notes.get(cx),
last_played_notes: last_played_notes.get(cx),
enabled_actions: enabled_actions.get(cx),
own_index,
last_learned_note: NO_LEARNED_NOTE,
}
.build(cx, move |cx| {
Label::new(
Expand All @@ -807,16 +817,20 @@ impl ActionTrigger {
}

pub fn start_learning(&mut self) {
let index = self.learning_index.load(Ordering::SeqCst);
self.learned_notes
.store(index, self.last_learned_notes.load(index));
self.is_learning.store(true, Ordering::SeqCst);
let index = self.own_index;
self.last_learned_note = self.learned_notes.load(index);
self.last_learned_notes
.store(index, self.learned_notes.load(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);
self.learned_notes
.store(self.own_index, self.last_learned_note);
.store(self.own_index, self.last_learned_notes.load(self.own_index));
}

// Checks if learning is active for this trigger
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct Del2 {
// for which control are we learning?
learning_index: Arc<AtomicUsize>,
learned_notes: Arc<AtomicByteArray>,
last_learned_notes: Arc<AtomicByteArray>,
last_played_notes: Arc<LastPlayedNotes>,
samples_since_last_event: u32,
timing_last_event: u32,
Expand Down Expand Up @@ -436,6 +437,7 @@ impl Default for Del2 {
let filter_params = array_init(|_| Arc::new(FilterParams::new()));
let should_update_filter = Arc::new(AtomicBool::new(false));
let learned_notes = Arc::new(AtomicByteArray::new(NO_LEARNED_NOTE));
let last_learned_notes = Arc::new(AtomicByteArray::new(NO_LEARNED_NOTE));
let enabled_actions = Arc::new(AtomicBoolArray::new());
let ladders: [LadderFilter; NUM_TAPS] =
array_init(|i| LadderFilter::new(filter_params[i].clone()));
Expand Down Expand Up @@ -485,6 +487,7 @@ impl Default for Del2 {
is_learning: Arc::new(AtomicBool::new(false)),
learning_index: Arc::new(AtomicUsize::new(0)),
learned_notes,
last_learned_notes,
last_played_notes: Arc::new(LastPlayedNotes::new()),
samples_since_last_event: 0,
timing_last_event: 0,
Expand Down Expand Up @@ -590,6 +593,7 @@ impl Plugin for Del2 {
is_learning: self.is_learning.clone(),
learning_index: self.learning_index.clone(),
learned_notes: self.learned_notes.clone(),
last_learned_notes: self.last_learned_notes.clone(),
last_played_notes: self.last_played_notes.clone(),
enabled_actions: self.enabled_actions.clone(),
},
Expand Down Expand Up @@ -1118,8 +1122,9 @@ impl Del2 {
}
if is_learning {
self.is_learning.store(false, Ordering::SeqCst);
self.learned_notes
.store(self.learning_index.load(Ordering::SeqCst), note);
let index = self.learning_index.load(Ordering::SeqCst);
self.learned_notes.store(index, note);
self.last_learned_notes.store(index, note);
self.last_played_notes.note_off(note);
}

Expand Down Expand Up @@ -1192,7 +1197,6 @@ impl Del2 {
}
if self.is_playing_action(CLEAR_TAPS) {
self.clear_taps(timing, false);
// self.last_played_notes.note_off(self.learned_notes.load(CLEAR_TAPS))
}
}
}
Expand Down

0 comments on commit 70e2b0c

Please sign in to comment.