Skip to content

Commit

Permalink
More play along improvements (#46)
Browse files Browse the repository at this point in the history
* Don't clear user pressed keys on playback pause

* Ignore drums and keys out of range in play along
  • Loading branch information
PolyMeilex authored Jun 23, 2023
1 parent 45ef0e0 commit e26c236
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 0 additions & 1 deletion neothesia-core/src/render/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
24 changes: 21 additions & 3 deletions neothesia/src/scene/playing_scene/midi_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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, .. } => {
Expand Down Expand Up @@ -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<u8>,

// List of user key press events that happened in last 500ms,
Expand All @@ -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();
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e26c236

Please sign in to comment.