Skip to content

Commit

Permalink
improve bars and beats time
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 22, 2024
1 parent 9b6af5e commit 67a2b80
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 67a2b80

Please sign in to comment.