Skip to content

Commit

Permalink
Add 2 second start delay (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackMIDIScore authored Sep 24, 2024
1 parent 9d12ac4 commit d701e2c
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 48 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ num_enum = "0.7.0"
rfd = { version = "0.12.0", default-features = false, features = [
'xdg-portal',
] }
time = "0.3.36"

[profile.dev]
opt-level = 2
Expand Down
6 changes: 3 additions & 3 deletions src/gui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod xsynth_settings;
use std::{
path::PathBuf,
sync::{Arc, RwLock},
time::Duration,
};
use time::Duration;

use egui::{style::Margin, Frame, Visuals};

Expand Down Expand Up @@ -130,7 +130,7 @@ impl GuiWasabiWindow {
.show_separator_line(false)
.show(&ctx, |ui| {
if let Some(midi_file) = self.midi_file.as_mut() {
let one_sec = Duration::from_secs(1);
let one_sec = Duration::seconds(1);
let time = midi_file.timer().get_time();

ui.input(|events| {
Expand All @@ -144,7 +144,7 @@ impl GuiWasabiWindow {
egui::Key::ArrowLeft => {
if midi_file.allows_seeking_backward() {
midi_file.timer_mut().seek(if time <= one_sec {
Duration::from_secs(0)
Duration::seconds(0)
} else {
time - one_sec
})
Expand Down
6 changes: 3 additions & 3 deletions src/gui/window/scene/cake_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl CakeRenderer {
}
}

let midi_time = midi_file.current_time().as_secs_f64();
let midi_time = midi_file.current_time().as_seconds_f64();
let screen_start = (midi_time * midi_file.ticks_per_second() as f64) as i32;
let screen_end = ((midi_time + view_range) * midi_file.ticks_per_second() as f64) as i32;

Expand Down Expand Up @@ -372,8 +372,8 @@ impl CakeRenderer {
.key_blocks()
.iter()
.map(|block| {
let passed = block.get_notes_passed_at(screen_end as u32)
- block.get_notes_passed_at(screen_start as u32);
let passed =
block.get_notes_passed_at(screen_end) - block.get_notes_passed_at(screen_start);

if block.get_note_at(screen_start as u32).is_some() {
passed as u64 + 1
Expand Down
27 changes: 16 additions & 11 deletions src/gui/window/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub fn draw_stats(win: &mut GuiWasabiWindow, ctx: &Context, pos: Pos2, mut stats
.fixed_pos(pos)
.fixed_size(egui::Vec2::new(200.0, 128.0))
.show(ctx, |ui| {
let mut time_millis: u64 = 0;
let mut time_sec: u64 = 0;
let mut time_min: u64 = 0;
let mut time_millis: i64 = 0;
let mut time_sec: i64 = 0;
let mut time_min: i64 = 0;
let mut length_millis: u64 = 0;
let mut length_sec: u64 = 0;
let mut length_min: u64 = 0;
Expand All @@ -61,7 +61,7 @@ pub fn draw_stats(win: &mut GuiWasabiWindow, ctx: &Context, pos: Pos2, mut stats

if let Some(midi_file) = win.midi_file.as_mut() {
stats.time_total = midi_file.midi_length().unwrap_or(0.0);
let time = midi_file.timer().get_time().as_secs_f64();
let time = midi_file.timer().get_time().as_seconds_f64();

length_millis = (stats.time_total * 10.0) as u64 % 10;
length_sec = stats.time_total as u64 % 60;
Expand All @@ -73,9 +73,9 @@ pub fn draw_stats(win: &mut GuiWasabiWindow, ctx: &Context, pos: Pos2, mut stats
stats.time_passed = time;
}

time_millis = (stats.time_passed * 10.0) as u64 % 10;
time_sec = stats.time_passed as u64 % 60;
time_min = stats.time_passed as u64 / 60;
time_millis = (stats.time_passed * 10.0) as i64 % 10;
time_sec = stats.time_passed as i64 % 60;
time_min = stats.time_passed as i64 / 60;

note_stats = midi_file.stats();
}
Expand All @@ -84,10 +84,15 @@ pub fn draw_stats(win: &mut GuiWasabiWindow, ctx: &Context, pos: Pos2, mut stats
ui.monospace("Time:");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.monospace(format!(
"{:0width$}:{:0width$}.{} / {:0width$}:{:0width$}.{}",
time_min,
time_sec,
time_millis,
"{}{:0width$}:{:0width$}.{} / {:0width$}:{:0width$}.{}",
if time_sec + time_millis < 0 {
'-'
} else {
'\0'
},
time_min.abs(),
time_sec.abs(),
time_millis.abs(),
length_min,
length_sec,
length_millis,
Expand Down
9 changes: 5 additions & 4 deletions src/gui/window/top_panel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use egui::{Context, Frame};

use std::time::Duration;
use time::Duration;

use crate::{
gui::window::GuiWasabiWindow, midi::MIDIFileBase, settings::WasabiSettings, state::WasabiState,
Expand Down Expand Up @@ -71,14 +71,15 @@ pub fn draw_panel(
|| ui.add(egui::Slider::new(&mut 0.0, 0.0..=1.0).show_value(false));
if let Some(midi_file) = win.midi_file.as_mut() {
if let Some(length) = midi_file.midi_length() {
let mut time = midi_file.timer().get_time().as_secs_f64();
let mut time = midi_file.timer().get_time().as_seconds_f64();
let time_prev = time;

ui.add(egui::Slider::new(&mut time, 0.0..=length).show_value(false));
let start_delay = crate::midi::START_DELAY.as_seconds_f64();
ui.add(egui::Slider::new(&mut time, -start_delay..=length).show_value(false));
if (time_prev != time)
&& (midi_file.allows_seeking_backward() || time_prev < time)
{
midi_file.timer_mut().seek(Duration::from_secs_f64(time));
midi_file.timer_mut().seek(Duration::seconds_f64(time));
}
} else {
empty_slider();
Expand Down
10 changes: 5 additions & 5 deletions src/midi/audio/live.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
sync::{Arc, RwLock},
thread::{self, JoinHandle},
time::Duration,
};
use time::Duration;

use crossbeam_channel::Receiver;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl LiveAudioPlayer {
match self.timer.wait_until_unpause() {
UnpauseWaitResult::Unpaused => push_cc(&event),
UnpauseWaitResult::UnpausedAndSeeked(time) => {
if time.as_secs_f64() - event.time > max_fall_time {
if time.as_seconds_f64() - event.time > max_fall_time {
seek_catching_up = true;
}
continue;
Expand All @@ -67,7 +67,7 @@ impl LiveAudioPlayer {
}

if seek_catching_up {
let time = self.timer.get_time().as_secs_f64();
let time = self.timer.get_time().as_seconds_f64();
if time - event.time > max_fall_time {
push_cc(&event);
continue;
Expand All @@ -76,15 +76,15 @@ impl LiveAudioPlayer {
}
}

let time = Duration::from_secs_f64(event.time);
let time = Duration::seconds_f64(event.time);
match self.timer.wait_until(time) {
WaitResult::Ok => {}
WaitResult::Paused => {
continue;
}
WaitResult::Seeked(time) => {
reset();
if time.as_secs_f64() - event.time > max_fall_time {
if time.as_seconds_f64() - event.time > max_fall_time {
seek_catching_up = true;
}
continue;
Expand Down
16 changes: 10 additions & 6 deletions src/midi/audio/ram.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
sync::{Arc, RwLock},
thread::{self, JoinHandle},
time::Duration,
};
use time::Duration;

use crate::{
audio_playback::SimpleTemporaryPlayer,
Expand Down Expand Up @@ -45,11 +45,11 @@ impl InRamAudioPlayer {
reset();
match self.timer.wait_until_unpause() {
UnpauseWaitResult::Unpaused => {
self.seek_to_time(self.timer.get_time().as_secs_f64());
self.seek_to_time(self.timer.get_time().as_seconds_f64());
continue;
}
UnpauseWaitResult::UnpausedAndSeeked(time) => {
self.seek_to_time(time.as_secs_f64());
self.seek_to_time(time.as_seconds_f64());
continue;
}
UnpauseWaitResult::Killed => break,
Expand All @@ -59,7 +59,7 @@ impl InRamAudioPlayer {
if self.index >= self.events.len() {
match self.timer.wait_until_seeked() {
SeekWaitResult::UnpausedAndSeeked(time) => {
self.seek_to_time(time.as_secs_f64());
self.seek_to_time(time.as_seconds_f64());
continue;
}
SeekWaitResult::Killed => break,
Expand All @@ -68,15 +68,15 @@ impl InRamAudioPlayer {

let event = &self.events[self.index];

let time = Duration::from_secs_f64(event.time);
let time = Duration::seconds_f64(event.time);
match self.timer.wait_until(time) {
WaitResult::Ok => {}
WaitResult::Paused => {
continue;
}
WaitResult::Seeked(time) => {
reset();
self.seek_to_time(time.as_secs_f64());
self.seek_to_time(time.as_seconds_f64());
continue;
}
WaitResult::Killed => {
Expand All @@ -93,6 +93,10 @@ impl InRamAudioPlayer {
}

fn find_time_index(&self, time: f64) -> usize {
if time < 0.0 {
return 0;
}

let events = &self.events;

// Binary search to find the right time segment
Expand Down
4 changes: 2 additions & 2 deletions src/midi/cake/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ impl CakeBlock {
})
}
}
pub fn get_notes_passed_at(&self, time: u32) -> u32 {
pub fn get_notes_passed_at(&self, time: i32) -> u32 {
let mut last_notes_passed;
let mut next_index = self.tree[0].length_marker_len();

loop {
let node = self.tree[next_index];

let offset = if time < node.leaf_cutoff() as u32 {
let offset = if time < node.leaf_cutoff() {
node.leaf_left()
} else {
node.leaf_right()
Expand Down
6 changes: 3 additions & 3 deletions src/midi/cake/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
sync::{Arc, RwLock},
thread,
time::Duration,
};
use time::Duration;

use midi_toolkit::{
events::{Event, MIDIEventEnum},
Expand Down Expand Up @@ -225,8 +225,8 @@ impl MIDIFileBase for CakeMIDIFile {
}

fn stats(&self) -> MIDIFileStats {
let time = self.timer.get_time().as_secs_f64();
let time_int = (time * self.ticks_per_second as f64) as u32;
let time = self.timer.get_time().as_seconds_f64();
let time_int = (time * self.ticks_per_second as f64) as i32;

let passed_notes = self
.key_blocks()
Expand Down
2 changes: 1 addition & 1 deletion src/midi/live/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl MIDIFile for LiveLoadMIDIFile {
type ColumnsViews<'a> = LiveCurrentNoteViews<'a> where Self: 'a;

fn get_current_column_views(&mut self, range: f64) -> Self::ColumnsViews<'_> {
let time = self.timer.get_time().as_secs_f64();
let time = self.timer.get_time().as_seconds_f64();
let new_range = MIDIViewRange::new(time, time + range);
self.view_data.shift_view_range(new_range);

Expand Down
4 changes: 2 additions & 2 deletions src/midi/live/parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
sync::{atomic::Ordering, Arc, RwLock},
thread::{self, JoinHandle},
time::Duration,
};
use time::Duration;

use atomic_float::AtomicF64;
use crossbeam_channel::Receiver;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl LiveMidiParser {
}

let playback_time = (time - 10.0).max(0.0); // 10 seconds offset
let waited = parser_timer.wait_until(Duration::from_secs_f64(playback_time));
let waited = parser_timer.wait_until(Duration::seconds_f64(playback_time));
if let WaitResult::Killed = waited {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/midi/live/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl LiveNoteViewData {
parser,
columns,
view_range: MIDIViewRange {
start: 0.0,
end: 0.0,
start: f64::NEG_INFINITY,
end: f64::NEG_INFINITY,
},
default_track_colors: if random_colors {
MIDIColor::new_random_vec_for_tracks(track_count)
Expand Down
Loading

0 comments on commit d701e2c

Please sign in to comment.