From 67a2b80ff9a4a51e62364e40b831d08c4c600749 Mon Sep 17 00:00:00 2001 From: Bart Brouns Date: Fri, 22 Nov 2024 17:15:23 +0100 Subject: [PATCH] improve bars and beats time --- src/editor.rs | 70 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index c8534e7..06fb0de 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -441,53 +441,73 @@ impl DelayGraph { cx, params.map(move |params| { let tap_counter = params.tap_counter.load(Ordering::SeqCst); + let current_time = params.current_time.load(Ordering::SeqCst); match tap_counter { - // 0 => "tap a rhythm!".to_string(), - 0 => String::new(), - 1 => "1 tap".to_string(), + 0 => { + if current_time > 0 { + String::from("no taps") + } else { + String::new() + } + } + 1 => String::from("1 tap"), tap_nr => format!("{tap_nr} taps"), } }), ) .class("tap-nr-label"); + Label::new( cx, params.map(move |params| { const TOTAL_DIGITS: usize = 3; + let tap_counter = params.tap_counter.load(Ordering::SeqCst); + let current_time = params.current_time.load(Ordering::SeqCst); let tempo = params.tempo.load(Ordering::SeqCst); let time_sig_numerator = params.time_sig_numerator.load(Ordering::SeqCst); - let tap_counter = params.tap_counter.load(Ordering::SeqCst); - // Determine the max delay time - let seconds = if tap_counter > 0 { + let seconds = if current_time > 0 { + current_time as f32 / params.sample_rate.load(Ordering::SeqCst) + } else if tap_counter > 0 { params.delay_times[tap_counter - 1].load(Ordering::SeqCst) as f32 / params.sample_rate.load(Ordering::SeqCst) } else { - 0.0 + 0.0 // Default value in case both conditions fail }; - - if tempo >= 0.0 && time_sig_numerator > 0 { + if current_time == 0 && tap_counter == 0 { + // String::from("tap a rhythm!") + String::new() + } else if tempo >= 0.0 && time_sig_numerator > 0 { let seconds_per_beat = 60.0 / tempo; let seconds_per_measure = seconds_per_beat * time_sig_numerator as f32; - let full_bars = (seconds / seconds_per_measure).floor() as i32; let remaining_seconds = seconds % seconds_per_measure; let additional_beats = - (remaining_seconds / seconds_per_beat).round() as i32; - - if full_bars > 1 { - format!("{full_bars} bars") - } else if full_bars > 0 { - format!("{full_bars} bar") - } else if additional_beats > 1 { - format!("{additional_beats} beats") - } else if additional_beats > 0 { - format!("{additional_beats} beat") + (remaining_seconds / seconds_per_beat).floor() as i32; + if full_bars > 0 { + let bar_str = if current_time > 0 || full_bars > 1 { + "bars" + } else { + "bar" + }; + if current_time > 0 || additional_beats > 0 { + let beat_str = if current_time > 0 || additional_beats != 1 { + "beats" + } else { + "beat" + }; + format!("{full_bars} {bar_str}, {additional_beats} {beat_str}") + } else { + format!("{full_bars} {bar_str}") + } } else { - String::new() + let beat_str = if current_time > 0 || additional_beats != 1 { + "beats" + } else { + "beat" + }; + format!("{additional_beats} {beat_str}") } - } else if seconds < 0.001 { - String::new() } else { format_time(seconds * 1000.0, TOTAL_DIGITS) } @@ -645,10 +665,6 @@ impl DelayGraph { ) { // Load the values once let current_time = params.current_time.load(Ordering::SeqCst); - let tap_counter = params.tap_counter.load(Ordering::SeqCst); - if tap_counter == NUM_TAPS { - return; - }; // Compute offset using mul_add for precision and performance let x_offset = (current_time as f32).mul_add(time_scaling_factor, border_width * 0.5);