Skip to content

Commit

Permalink
Remove MidiPlayer dependence on Target (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored Jan 10, 2024
1 parent ddc16dd commit e7f5ed8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 5 additions & 8 deletions neothesia/src/scene/playing_scene/midi_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use midi_file::midly::{num::u4, MidiMessage};
use crate::{
output_manager::OutputConnection,
song::{PlayerConfig, Song},
target::Target,
};
use std::{
collections::{HashSet, VecDeque},
Expand All @@ -19,7 +18,7 @@ pub struct MidiPlayer {

impl MidiPlayer {
pub fn new(
target: &Target,
output: OutputConnection,
song: Song,
user_keyboard_range: piano_math::KeyboardRange,
) -> Self {
Expand All @@ -28,28 +27,26 @@ impl MidiPlayer {
Duration::from_secs(3),
song.file.tracks.clone(),
),
output: target.output_manager.connection().clone(),
output,
play_along: PlayAlong::new(user_keyboard_range),
song,
};
// Let's reset programs,
// for timestamp 0 most likely all programs will be 0, so this should clean any leftovers
// from previous songs
player.send_midi_programs_for_timestamp(&player.playback.time());
player.update(target, Duration::ZERO);
player.update(Duration::ZERO);

player
}

/// When playing: returns midi events
///
/// When paused: returns None
pub fn update(&mut self, target: &Target, delta: Duration) -> Vec<&midi_file::MidiEvent> {
pub fn update(&mut self, delta: Duration) -> Vec<&midi_file::MidiEvent> {
self.play_along.update();

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

let events = self.playback.update(elapsed);
let events = self.playback.update(delta);

events.iter().for_each(|event| {
let config = &self.song.config.tracks[event.track_id];
Expand Down
9 changes: 7 additions & 2 deletions neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ impl PlayingScene {
keyboard_layout.clone(),
);

let player = MidiPlayer::new(target, song, keyboard_layout.range.clone());
let player = MidiPlayer::new(
target.output_manager.connection().clone(),
song,
keyboard_layout.range.clone(),
);
notes.update(&target.gpu.queue, player.time_without_lead_in());

Self {
Expand Down Expand Up @@ -94,7 +98,8 @@ impl Scene for PlayingScene {
self.rewind_controler.update(&mut self.player, target);

if self.player.play_along().are_required_keys_pressed() {
let midi_events = self.player.update(target, delta);
let delta = (delta / 10) * (target.config.speed_multiplier * 10.0) as u32;
let midi_events = self.player.update(delta);
self.keyboard.file_midi_events(&target.config, &midi_events);
}

Expand Down

0 comments on commit e7f5ed8

Please sign in to comment.