Skip to content

Commit

Permalink
Playback::Update optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Sep 17, 2023
1 parent a525b9c commit 174ca01
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 38 deletions.
42 changes: 7 additions & 35 deletions midi-file/src/playback.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{collections::HashSet, time::Duration};

use midly::MidiMessage;
use std::time::Duration;

use crate::{MidiEvent, MidiTrack};

Expand All @@ -19,8 +17,6 @@ pub struct PlaybackState {

first_note_start: Duration,
last_note_end: Duration,

active_notes: HashSet<ActiveNote>,
}

impl PlaybackState {
Expand All @@ -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<MidiEvent> {
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 {
Expand Down Expand Up @@ -107,10 +84,6 @@ impl PlaybackState {
self.running.as_secs_f32() / self.lenght().as_secs_f32()
}

pub fn active_notes(&self) -> &HashSet<ActiveNote> {
&self.active_notes
}

pub fn leed_in(&self) -> &Duration {
&self.leed_in
}
Expand All @@ -130,6 +103,5 @@ impl PlaybackState {
pub fn reset(&mut self) {
self.running = Duration::ZERO;
self.seen_events = 0;
self.active_notes.clear();
}
}
2 changes: 1 addition & 1 deletion neothesia-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion neothesia/src/scene/playing_scene/keyboard_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion neothesia/src/scene/playing_scene/midi_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl MidiPlayer {
&mut self,
target: &mut Target,
delta: Duration,
) -> Option<Vec<midi_file::MidiEvent>> {
) -> Option<Vec<&midi_file::MidiEvent>> {
self.play_along.update();

let elapsed = (delta / 10) * (target.config.speed_multiplier * 10.0) as u32;
Expand Down

0 comments on commit 174ca01

Please sign in to comment.