Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MidiPlayer dependence on Target #129

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading