diff --git a/src/editor.rs b/src/editor.rs index 7aa1f11..a8eae19 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -26,6 +26,11 @@ pub struct Data { pub is_learning: Arc, pub learning_index: Arc, pub learned_notes: Arc, + // 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, pub last_played_notes: Arc, pub enabled_actions: Arc, } @@ -159,6 +164,7 @@ pub fn create(editor_data: Data, editor_state: Arc) -> Option) -> Option) -> Option) -> Option, learning_index: Arc, learned_notes: Arc, + last_learned_notes: Arc, last_played_notes: Arc, enabled_actions: Arc, 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< @@ -764,6 +771,7 @@ impl ActionTrigger { IsLearningL, LearningIndexL, LearnedNotesL, + LastLearnedNotesL, LastPlayedNotesL, EnabledActionsL, >( @@ -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, @@ -781,6 +790,7 @@ impl ActionTrigger { IsLearningL: Lens>, LearningIndexL: Lens>, LearnedNotesL: Lens>, + LastLearnedNotesL: Lens>, LastPlayedNotesL: Lens>, EnabledActionsL: Lens>, { @@ -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( @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 8715c4d..152ef66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,6 +108,7 @@ struct Del2 { // for which control are we learning? learning_index: Arc, learned_notes: Arc, + last_learned_notes: Arc, last_played_notes: Arc, samples_since_last_event: u32, timing_last_event: u32, @@ -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())); @@ -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, @@ -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(), }, @@ -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); } @@ -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)) } } }