From 174ca01a3c5172d43850ae8847c439d8182a3df2 Mon Sep 17 00:00:00 2001 From: PolyMeilex Date: Sun, 17 Sep 2023 21:42:15 +0200 Subject: [PATCH] Playback::Update optimizations --- midi-file/src/playback.rs | 42 ++++--------------- neothesia-cli/src/main.rs | 2 +- .../scene/playing_scene/keyboard_events.rs | 2 +- .../src/scene/playing_scene/midi_player.rs | 2 +- 4 files changed, 10 insertions(+), 38 deletions(-) diff --git a/midi-file/src/playback.rs b/midi-file/src/playback.rs index 4f4ebc1d..e60d404d 100644 --- a/midi-file/src/playback.rs +++ b/midi-file/src/playback.rs @@ -1,6 +1,4 @@ -use std::{collections::HashSet, time::Duration}; - -use midly::MidiMessage; +use std::time::Duration; use crate::{MidiEvent, MidiTrack}; @@ -19,8 +17,6 @@ pub struct PlaybackState { first_note_start: Duration, last_note_end: Duration, - - active_notes: HashSet, } impl PlaybackState { @@ -44,42 +40,23 @@ impl PlaybackState { first_note_start, last_note_end, - - active_notes: Default::default(), } } - pub fn update(&mut self, track: &MidiTrack, delta: Duration) -> Vec { + pub fn update<'a>(&mut self, track: &'a MidiTrack, delta: Duration) -> Vec<&'a MidiEvent> { if !self.is_paused { self.running += delta; } - track + let events: Vec<_> = track .events .iter() .skip(self.seen_events) .filter(|event| event.timestamp + self.leed_in <= self.running) - .map(|event| { - let event = event.clone(); - self.seen_events += 1; - event - }) - .inspect(|event| match event.message { - MidiMessage::NoteOn { key, .. } => { - self.active_notes.insert(ActiveNote { - key: key.as_int(), - channel: event.channel, - }); - } - MidiMessage::NoteOff { key, .. } => { - self.active_notes.remove(&ActiveNote { - key: key.as_int(), - channel: event.channel, - }); - } - _ => {} - }) - .collect() + .collect(); + + self.seen_events += events.len(); + events } pub fn is_paused(&self) -> bool { @@ -107,10 +84,6 @@ impl PlaybackState { self.running.as_secs_f32() / self.lenght().as_secs_f32() } - pub fn active_notes(&self) -> &HashSet { - &self.active_notes - } - pub fn leed_in(&self) -> &Duration { &self.leed_in } @@ -130,6 +103,5 @@ impl PlaybackState { pub fn reset(&mut self) { self.running = Duration::ZERO; self.seen_events = 0; - self.active_notes.clear(); } } diff --git a/neothesia-cli/src/main.rs b/neothesia-cli/src/main.rs index 208202d7..851f87db 100644 --- a/neothesia-cli/src/main.rs +++ b/neothesia-cli/src/main.rs @@ -269,7 +269,7 @@ fn main() { fn file_midi_events( keyboard: &mut KeyboardRenderer, config: &Config, - events: &[midi_file::MidiEvent], + events: &[&midi_file::MidiEvent], ) { use midi_file::midly::MidiMessage; diff --git a/neothesia/src/scene/playing_scene/keyboard_events.rs b/neothesia/src/scene/playing_scene/keyboard_events.rs index c589a6bf..a9a57889 100644 --- a/neothesia/src/scene/playing_scene/keyboard_events.rs +++ b/neothesia/src/scene/playing_scene/keyboard_events.rs @@ -22,7 +22,7 @@ pub fn user_midi_event(keyboard: &mut KeyboardRenderer, event: &crate::midi_even pub fn file_midi_events( keyboard: &mut KeyboardRenderer, config: &Config, - events: &[midi_file::MidiEvent], + events: &[&midi_file::MidiEvent], ) { use midi_file::midly::MidiMessage; diff --git a/neothesia/src/scene/playing_scene/midi_player.rs b/neothesia/src/scene/playing_scene/midi_player.rs index cd51f0bc..d64a251f 100644 --- a/neothesia/src/scene/playing_scene/midi_player.rs +++ b/neothesia/src/scene/playing_scene/midi_player.rs @@ -42,7 +42,7 @@ impl MidiPlayer { &mut self, target: &mut Target, delta: Duration, - ) -> Option> { + ) -> Option> { self.play_along.update(); let elapsed = (delta / 10) * (target.config.speed_multiplier * 10.0) as u32;