Skip to content

Commit

Permalink
Merge pull request #34 from subalterngames/octave_key_down
Browse files Browse the repository at this point in the history
Fixed: Changing octaves "erases" note-off events
  • Loading branch information
subalterngames authored May 29, 2024
2 parents 91cc116 + 304acca commit 08f10dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.2.4

- Fixed: If note is played via a qwerty key press, and then an octave is changed via a qwerty key press, there won't be a note-off event.
- Fixed: Cacophony can't found files (saves, soundfonts, etc.) if the file extension contains uppercase characters.
- Fixed clippy warnings for Rust 1.78
- The GitHub workflow for building Cacophony now uses the latest version of Rust.
Expand Down
14 changes: 12 additions & 2 deletions input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub struct Input {
events: Vec<InputEvent>,
/// The MIDI connection.
midi_conn: Option<MidiConn>,
// Note-on MIDI messages. These will be sent immediately to the synthesizer to be played.
/// Note-on MIDI messages. These will be sent immediately to the synthesizer to be played.
pub note_on_messages: Vec<[u8; 3]>,
// Note-off MIDI messages. These will be sent immediately to the synthesizer.
/// Note-off MIDI messages. These will be sent immediately to the synthesizer.
pub note_off_keys: Vec<u8>,
/// Note-on events that don't have corresponding off events.
note_on_events: Vec<NoteOn>,
Expand Down Expand Up @@ -206,10 +206,12 @@ impl Input {
}
// Octave up.
if events.contains(&InputEvent::OctaveUp) && self.qwerty_octave < MAX_OCTAVE {
self.clear_notes_on_qwerty_octave();
self.qwerty_octave += 1;
}
// Octave down.
if events.contains(&InputEvent::OctaveDown) && self.qwerty_octave > 0 {
self.clear_notes_on_qwerty_octave();
self.qwerty_octave -= 1;
}
// Qwerty note-off.
Expand Down Expand Up @@ -420,6 +422,14 @@ impl Input {
(9 - self.qwerty_octave) * 12 + note
}

/// When a qwerty note is pressed, followed by an octave change, clear all note-on events.
fn clear_notes_on_qwerty_octave(&mut self) {
// Qwerty note-off.
for (_, qwerty_note_off) in QWERTY_NOTE_EVENTS.iter() {
self.note_off_keys.push(self.get_pitch(*qwerty_note_off));
}
}

#[cfg(debug_assertions)]
fn listen_for_note_offs(&mut self) {
if self.happened(&InputEvent::NotesOff) {
Expand Down

0 comments on commit 08f10dc

Please sign in to comment.