Skip to content

Commit

Permalink
draw the time line based on system time
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Jan 4, 2025
1 parent fd79576 commit 29c892d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl View for DelayGraph {
let current_time = params.current_time.load(Ordering::SeqCst);

let target_time_scaling_factor = Self::compute_time_scaling_factor(&params);
let gui_decay_weight = Self::calculate_gui_decay_weight(&params);
let (gui_decay_weight, now_nanos) = Self::calculate_gui_decay_weight(&params);
let available_width = outline_width.mul_add(-0.5, bounds.w - border_width);
let time_scaling_factor = (Self::gui_smooth(
target_time_scaling_factor,
Expand Down Expand Up @@ -618,6 +618,7 @@ impl View for DelayGraph {
outline_width,
time_scaling_factor,
border_width,
now_nanos,
);
}
}
Expand Down Expand Up @@ -756,14 +757,15 @@ impl DelayGraph {
// time out, zoom in but leave a margin
0.11 * max_delay_time
} else {
// zoom out
max_tap_time
};

// Return the total delay
max_delay_time + zoom_tap_samples
}

fn calculate_gui_decay_weight(params: &Arc<Del2Params>) -> f32 {
fn calculate_gui_decay_weight(params: &Arc<Del2Params>) -> (f32, u64) {
// Get current system time in nanoseconds since the UNIX epoch
let now_nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -787,12 +789,13 @@ impl DelayGraph {
// Calculate and return the GUI smoothing decay weight
let decay_ms_per_frame =
(GUI_SMOOTHING_DECAY_MS * 1_000_000.0) / frame_duration_nanos as f64;
0.25f32.powf(decay_ms_per_frame.recip() as f32)
(0.25f32.powf(decay_ms_per_frame.recip() as f32), now_nanos)
}

/// Smoothly updates the value stored within an `f32` based on a target value.
/// If the current value is `NO_GUI_SMOOTHING`, it initializes with the target value.
fn gui_smooth(target_value: f32, atomic_value: &AtomicF32, gui_decay_weight: f32) -> f32 {
// return target_value;
// Define the threshold relative to the target value
let threshold = 0.001 * target_value.abs();
// Load the current value once
Expand Down Expand Up @@ -867,9 +870,17 @@ impl DelayGraph {
line_width: f32,
time_scaling_factor: f32,
border_width: f32,
now_nanos: u64,
) {
// audio time
let current_time = params.current_time.load(Ordering::SeqCst);
let x_offset = current_time.mul_add(time_scaling_factor, border_width * 0.5);
let a_x_pos = bounds.x + x_offset;

// system time
let elapsed_nanos = now_nanos - params.first_note_nanos.load(Ordering::SeqCst);
let seconds = elapsed_nanos as f32 / 1_000_000_000.0;
let x_offset = seconds.mul_add(time_scaling_factor, border_width * 0.5);
let x_pos = bounds.x + x_offset;

// Constants for glow effect
Expand Down Expand Up @@ -930,6 +941,14 @@ impl DelayGraph {
);

canvas.fill_path(&core_path, &vg::Paint::color(color));

// let mut path = vg::Path::new();
// path.move_to(a_x_pos, bounds.y);
// path.line_to(a_x_pos, bounds.y + bounds.h);
// canvas.stroke_path(
// &path,
// &vg::Paint::color(vg::Color::rgb(255, 0, 0)).with_line_width(3.0),
// );
}

fn draw_in_out_meters(
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::sync::atomic::{
AtomicBool, AtomicI32, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering,
};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use synfx_dsp::fh_va::{FilterParams, LadderMode};

mod delay_tap;
Expand Down Expand Up @@ -162,6 +163,7 @@ pub struct Del2Params {
previous_pan_background_lengths: AtomicF32Array,
// for making the gui smoother frame rate independent
last_frame_time: AtomicU64,
first_note_nanos: AtomicU64,
// the rate we are nunning at now
sample_rate: AtomicF32,
host_tempo: AtomicF32,
Expand Down Expand Up @@ -569,6 +571,7 @@ impl Del2Params {
Arc::new(AtomicF32::new(0.0))
})),
last_frame_time: AtomicU64::new(0),
first_note_nanos: AtomicU64::new(0),
sample_rate: 1.0.into(),
host_tempo: (-1.0).into(),
time_sig_numerator: (-1).into(),
Expand Down Expand Up @@ -1216,6 +1219,13 @@ impl Del2 {
// If in TimeOut state, reset and start new counting phase
self.clear_taps(timing, true);
self.params.first_note.store(note, Ordering::SeqCst);
let now_nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("System time should be after UNIX epoch")
.as_nanos() as u64;
self.params
.first_note_nanos
.store(now_nanos, Ordering::SeqCst);
self.params.preset_tempo.store(
self.params.host_tempo.load(Ordering::SeqCst),
Ordering::SeqCst,
Expand Down

0 comments on commit 29c892d

Please sign in to comment.