From e26c236f14bc4f8865bcb488277260e1df7085bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Mary=C5=84czak?= Date: Fri, 23 Jun 2023 14:30:03 +0200 Subject: [PATCH] More play along improvements (#46) * Don't clear user pressed keys on playback pause * Ignore drums and keys out of range in play along --- neothesia-core/src/render/keyboard/mod.rs | 1 - .../src/scene/playing_scene/midi_player.rs | 24 ++++++++++++++++--- neothesia/src/scene/playing_scene/mod.rs | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/neothesia-core/src/render/keyboard/mod.rs b/neothesia-core/src/render/keyboard/mod.rs index e28a4dfd..260a2a59 100644 --- a/neothesia-core/src/render/keyboard/mod.rs +++ b/neothesia-core/src/render/keyboard/mod.rs @@ -47,7 +47,6 @@ impl KeyboardRenderer { pub fn reset_notes(&mut self) { for key in self.key_states.iter_mut() { key.pressed_by_file_off(); - key.set_pressed_by_user(false); } self.queue_reupload(); } diff --git a/neothesia/src/scene/playing_scene/midi_player.rs b/neothesia/src/scene/playing_scene/midi_player.rs index dee8a87b..643f3e20 100644 --- a/neothesia/src/scene/playing_scene/midi_player.rs +++ b/neothesia/src/scene/playing_scene/midi_player.rs @@ -22,7 +22,7 @@ pub struct MidiPlayer { } impl MidiPlayer { - pub fn new(target: &mut Target) -> Self { + pub fn new(target: &mut Target, user_keyboard_range: piano_math::KeyboardRange) -> Self { let midi_file = target.midi_file.as_ref().unwrap(); let mut player = Self { @@ -33,7 +33,7 @@ impl MidiPlayer { rewind_controller: RewindController::None, output_manager: target.output_manager.clone(), midi_file: midi_file.clone(), - play_along: PlayAlong::default(), + play_along: PlayAlong::new(user_keyboard_range), }; player.update(target, Duration::ZERO); @@ -58,6 +58,10 @@ impl MidiPlayer { events.iter().for_each(|event| { self.output_manager.borrow_mut().midi_event(event); + if event.channel == 9 { + return; + } + use midi_file::midly::MidiMessage; match event.message { MidiMessage::NoteOn { key, .. } => { @@ -192,8 +196,10 @@ struct UserPress { note_id: u8, } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct PlayAlong { + user_keyboard_range: piano_math::KeyboardRange, + required_notes: HashSet, // List of user key press events that happened in last 500ms, @@ -202,6 +208,14 @@ pub struct PlayAlong { } impl PlayAlong { + fn new(user_keyboard_range: piano_math::KeyboardRange) -> Self { + Self { + user_keyboard_range, + required_notes: Default::default(), + user_pressed_recently: Default::default(), + } + } + fn update(&mut self) { // Instead of calling .elapsed() per item let's fetch `now` once, and substract it ourselfs let now = Instant::now(); @@ -247,6 +261,10 @@ impl PlayAlong { } pub fn press_key(&mut self, src: KeyPressSource, note_id: u8, active: bool) { + if !self.user_keyboard_range.contains(note_id) { + return; + } + match src { KeyPressSource::User => self.user_press_key(note_id, active), KeyPressSource::File => self.file_press_key(note_id, active), diff --git a/neothesia/src/scene/playing_scene/mod.rs b/neothesia/src/scene/playing_scene/mod.rs index ceee1c2e..d649f247 100644 --- a/neothesia/src/scene/playing_scene/mod.rs +++ b/neothesia/src/scene/playing_scene/mod.rs @@ -61,7 +61,7 @@ impl PlayingScene { keyboard_layout.clone(), ); - let player = MidiPlayer::new(target); + let player = MidiPlayer::new(target, keyboard_layout.range.clone()); notes.update(&target.gpu.queue, player.time_without_lead_in()); Self {