Skip to content

Commit

Permalink
Fixed: It's possible to add notes while music is playing
Browse files Browse the repository at this point in the history
  • Loading branch information
subalterngames committed May 29, 2024
1 parent 5b57e07 commit 4383170
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- 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: ChildPaths sometimes doesn't set the correct directory when moving up a directory.
- Fixed: It's possible to add notes while the music is playing.
- (Backend) Fixed clippy warnings for Rust 1.78
- (Backend) The GitHub workflow for building Cacophony now uses the latest version of Rust.
- (Backend) Added tests for ChildPaths.
Expand Down
4 changes: 4 additions & 0 deletions common/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct InputState {
pub use_volume: bool,
/// The input beat in PPQ.
pub beat: U64orF32,
/// If true, music is playing or exporting.
#[serde(skip)]
pub is_playing: bool,
}

impl Default for InputState {
Expand All @@ -24,6 +27,7 @@ impl Default for InputState {
volume: Index::new(MAX_VOLUME, MAX_VOLUME + 1),
use_volume: true,
beat: U64orF32::from(PPQ_U),
is_playing: false,
}
}
}
22 changes: 13 additions & 9 deletions input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,18 @@ impl Input {
*midi
};
// Remember the note-on for piano roll input.
if state.input.armed {
self.note_on_events.push(NoteOn::new(&midi));
if !state.input.is_playing {
if state.input.armed {
self.note_on_events.push(NoteOn::new(&midi));
}
// Copy this note to the immediate note-on array.
self.note_on_messages.push(midi);
}
// Copy this note to the immediate note-on array.
self.note_on_messages.push(midi);
}
// Note-off.
if midi[0] >= 128 && midi[0] <= 143 {
self.note_off_keys.push(midi[1]);
if state.input.armed {
if state.input.armed && !state.input.is_playing {
// Find the corresponding note.
for note_on in self.note_on_events.iter_mut() {
// Same key. Note-off.
Expand Down Expand Up @@ -410,11 +412,13 @@ impl Input {

/// Push a new note from qwerty input.
fn qwerty_note(&mut self, note: u8, state: &State) {
let note: [u8; 3] = [144, self.get_pitch(note), state.input.volume.get()];
if state.input.armed {
self.new_notes.push(note);
if !state.input.is_playing {
let note: [u8; 3] = [144, self.get_pitch(note), state.input.volume.get()];
if state.input.armed {
self.new_notes.push(note);
}
self.note_on_messages.push(note);
}
self.note_on_messages.push(note);
}

/// Converts the note index to a MIDI note value.
Expand Down
7 changes: 7 additions & 0 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! Each panel implements the `Panel` trait.
use audio::export::ExportState;
use audio::play_state::PlayState;
use audio::Conn;
use common::{InputState, Music, PanelType, Paths, PathsState, SelectMode, State};
use edit::edit_file;
Expand Down Expand Up @@ -432,6 +433,12 @@ impl IO {
.iter()
.any(|t| !t.get_playback_notes(state.time.playback).is_empty())
{
// Toggle whether music is playing.
state.input.is_playing = matches!(
*conn.play_state.lock(),
PlayState::NotPlaying | PlayState::Decaying
);
// Start to play music.
conn.set_music(state);
}
// We're not done yet.
Expand Down

0 comments on commit 4383170

Please sign in to comment.