Skip to content

Commit

Permalink
show when a trigger is played
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Oct 22, 2024
1 parent 2b824f8 commit f6b2ae7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
61 changes: 40 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,6 @@ impl Del2 {
let should_record_tap = self.delay_data.current_tap < MAX_NR_TAPS;

self.last_played_notes.note_on(note);
println!("ooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnn");
self.last_played_notes.print_notes();

match self.counting_state {
CountingState::TimeOut => {
Expand Down Expand Up @@ -697,9 +695,7 @@ impl Del2 {
NoteEvent::NoteOff {
timing: _, note, ..
} => {
println!("OOOOOOOOOOOOOOOOOOOOOFFFFFFFFFFFFFFFFFFFFFFFFF");
self.last_played_notes.note_off(note);
self.last_played_notes.print_notes();
}
_ => {} // Handle other types of events if necessary
}
Expand Down Expand Up @@ -1126,7 +1122,9 @@ struct LastPlayedNotes {
}

impl LastPlayedNotes {
/// Constructs a new instance of `LastPlayedNotes`.
fn new() -> Self {
// Initializes the state, notes, sequence, and current_sequence fields.
Self {
state: AtomicU8::new(0),
notes: AtomicByteArray::new(),
Expand All @@ -1135,19 +1133,41 @@ impl LastPlayedNotes {
}
}

/// Handles the 'note on' event.
fn note_on(&self, note: u8) {
let mut current_state = self.state.load(Ordering::SeqCst);

// Check if the note is already in the table
// Check if the note is already in the table and reactivate if so.
if let Some(index) = (0..8).find(|&i| self.notes.load(i) == note) {
// Update sequence and reactivate the note.
self.sequence
.store(index, self.current_sequence.fetch_add(1, Ordering::SeqCst));
// Ensure it's marked as active in the state.
loop {
let new_state = current_state | (1 << index); // Set this index as active
if self
.state
.compare_exchange_weak(
current_state,
new_state,
Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
break;
} else {
current_state = self.state.load(Ordering::SeqCst);
}
}
return;
}

// Loop until space is found in the notes array.
loop {
// Find first available spot in the notes array.
if let Some(index) = (0..8).find(|i| (current_state & (1 << i)) == 0) {
// Occupy an empty spot
// Attempt to occupy this empty spot.
let new_state = current_state | (1 << index);
if self
.state
Expand All @@ -1159,11 +1179,13 @@ impl LastPlayedNotes {
)
.is_ok()
{
// Store the note and its sequence once the position is successfully claimed.
self.notes.store(index, note);
self.sequence
.store(index, self.current_sequence.fetch_add(1, Ordering::SeqCst));
break;
} else {
// Reload state as previous compare_exchange was not successful.
current_state = self.state.load(Ordering::SeqCst);
}
} else {
Expand All @@ -1179,10 +1201,13 @@ impl LastPlayedNotes {
}
}

/// Handles the 'note off' event.
fn note_off(&self, note: u8) {
let mut current_state = self.state.load(Ordering::SeqCst);
loop {
// Check if the note exists among the recorded notes.
if let Some(index) = (0..8).find(|&i| self.notes.load(i) == note) {
// Calculate new state after disabling the note at the found index.
let new_state = current_state & !(1 << index);
if self
.state
Expand All @@ -1194,9 +1219,11 @@ impl LastPlayedNotes {
)
.is_ok()
{
// Zero out the sequence to signify note is turned off.
self.sequence.store(index, 0);
break;
} else {
// Reload state as previous compare_exchange was not successful.
current_state = self.state.load(Ordering::SeqCst);
}
} else {
Expand All @@ -1205,41 +1232,33 @@ impl LastPlayedNotes {
}
}

/// Checks if a note is currently being played.
fn is_playing(&self, note: u8) -> bool {
// Find the index of the note and check if its spot in state is occupied.
if let Some(index) = (0..8).find(|&i| self.notes.load(i) == note) {
let current_state = self.state.load(Ordering::SeqCst);
(current_state & (1 << index)) != 0
} else {
false
}
}
/// for testing

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

// print!("{:^25} | ", action);
for i in 0..8 {
let note = self.notes.load(i);
if self.is_playing(note) {
// Print active notes
print!("{:>WIDTH$}", note);
} else {
// Print placeholder for inactive notes
print!("{:>WIDTH$}", "_");
}
}

println!();

// print!("{:^25} | ", "Sequence");
// for i in 0..8 {
// let seq = self.sequence.load(i);
// if self.is_playing(self.notes.load(i)) {
// print!("{:>WIDTH$}", seq);
// } else {
// print!("{:>WIDTH$}", "_");
// }
// }
// println!();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ action-trigger {
height: 30px;
top: 100px;
background-color: #4e4e4e;
border-color: #fabdf0;
selection-color: #5879af;
border-color: #5879af;
selection-color: #fabdf0;
border-width: 1px;
transition: background-color 300ms;
}
Expand Down

0 comments on commit f6b2ae7

Please sign in to comment.