Skip to content

Commit

Permalink
unify format_time logic
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 20, 2024
1 parent c8befd7 commit edc1ff5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
25 changes: 9 additions & 16 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use nih_plug_vizia::{
};

use crate::{
util, AtomicBoolArray, AtomicByteArray, AtomicF32, AtomicF32Array, Del2Params, LastPlayedNotes,
CLEAR_TAPS, LEARNING, LOCK_TAPS, MUTE_IN, MUTE_OUT, NO_LEARNED_NOTE, NUM_TAPS,
format_time, util, AtomicBoolArray, AtomicByteArray, AtomicF32, AtomicF32Array, Del2Params,
LastPlayedNotes, CLEAR_TAPS, LEARNING, LOCK_TAPS, MUTE_IN, MUTE_OUT, NO_LEARNED_NOTE, NUM_TAPS,
};

const GUI_SMOOTHING_DECAY_MS: f64 = 240.0;
Expand Down Expand Up @@ -437,6 +437,7 @@ impl DelayGraph {
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 {
params.delay_times[tap_counter - 1].load(Ordering::SeqCst) as f32
Expand All @@ -446,7 +447,6 @@ impl DelayGraph {
};

if tempo >= 0.0 && time_sig_numerator > 0 {
// Only proceed if tempo and time signature are valid (non-negative)
let seconds_per_beat = 60.0 / tempo;
let seconds_per_measure = seconds_per_beat * time_sig_numerator as f32;

Expand All @@ -464,21 +464,14 @@ impl DelayGraph {
} else if additional_beats > 0 {
format!("{additional_beats} beat")
} else {
return String::new();
String::new()
}
} else if seconds < 0.001 {
String::new()
} else if seconds < 1.0 {
// Calculate the number of digits after the decimal to maintain a total of three digits
let ms = seconds * 1000.0;
let digits_after_decimal = (TOTAL_DIGITS - ms.trunc().to_string().len())
.clamp(0, TOTAL_DIGITS - 1); // Ensure it's between 0 and 2
format!("{ms:.digits_after_decimal$} ms")
} else {
// Same logic for seconds
let digits_after_decimal =
(TOTAL_DIGITS - seconds.trunc().to_string().len()).max(0);
format!("{seconds:.digits_after_decimal$} s")
if seconds < 0.001 {
String::new()
} else {
format_time(seconds * 1000.0, TOTAL_DIGITS)
}
}
}),
)
Expand Down
21 changes: 15 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/*
TODO:
- improve voice management:
- re-use the voice if the delay time and the note are the same
if the velocity is different, we smooth to it, using the attack and release times
- the params should have the delay_times, notes and velocities, so nih-plug can persist them
- the delay tap should also have delay_times and notes, so it can decide if a new voice should be started
- smooth all dsp params (at the end of the interpolation?)
- evaluate filters:
Expand Down Expand Up @@ -1235,6 +1229,9 @@ impl Del2 {
}

fn v2s_f32_ms_then_s(total_digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
Arc::new(move |value| format_time(value, total_digits))
}
fn _v2s_f32_ms_then_s(total_digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
Arc::new(move |value| {
if value < 1000.0 {
// Calculate the number of digits after the decimal to maintain a total of three digits
Expand Down Expand Up @@ -1905,5 +1902,17 @@ impl LastPlayedNotes {
}
}

fn format_time(value: f32, total_digits: usize) -> String {
if value < 1000.0 {
let digits_after_decimal = (total_digits - value.trunc().to_string().len())
.max(0)
.min(total_digits - 1);
format!("{value:.digits_after_decimal$} ms")
} else {
let seconds = value / 1000.0;
let digits_after_decimal = (total_digits - seconds.trunc().to_string().len()).max(0);
format!("{seconds:.digits_after_decimal$} s")
}
}
nih_export_clap!(Del2);
nih_export_vst3!(Del2);

0 comments on commit edc1ff5

Please sign in to comment.