From 0bbc45e0a1fd5f80f9f97c0c81b2d954418b0f66 Mon Sep 17 00:00:00 2001 From: gursi26 Date: Fri, 15 Mar 2024 15:58:06 -0400 Subject: [PATCH] added debug info and cli flag, fixed mix and max height clamp bug --- README.md | 51 +++++++++++++++++++++++++++------------ src/args.rs | 5 ++++ src/main.rs | 38 ++++++++++++++++++++--------- src/systems/update_fft.rs | 6 +++-- 4 files changed, 70 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 33dc6b5..2a1fd13 100644 --- a/README.md +++ b/README.md @@ -43,21 +43,40 @@ Arguments: File path to Audio file Options: - --smoothness Smoothing factor for spatial interpolation between bars - --freq-resolution Number of individual frequencies detected by the FFT - --min-freq Maximum frequency detected by FFT - --max-freq Minimum frequency detected by FFT - --volume Volume - --width Window width - --height Window height - --border-size Border size for each bar - --border-color Border color for each bar (in hex) - --bar-color Color for each bar (in hex) - --track-name Use if you want track name to be printed - --display-gui Use if you want the gui to be open when launched - --text-color Color for currently playing text (in hex) - --font-size Font size of currently playing label + --smoothness + Smoothing factor for spatial interpolation between bars + --freq-resolution + Number of individual frequencies detected by the FFT + --min-freq + Maximum frequency detected by FFT + --max-freq + Minimum frequency detected by FFT + --volume + Volume + --width + Window width + --height + Window height + --border-size + Border size for each bar + --border-color + Border color for each bar (in hex) + --bar-color + Color for each bar (in hex) + --track-name + Use if you want track name to be printed + --display-gui + Use if you want the gui to be open when launched + --debug + Use if you want to display debug information when launching + --text-color + Color for currently playing text (in hex) + --font-size + Font size of currently playing label --background-color - -h, --help Print help - -V, --version Print version + + -h, --help + Print help + -V, --version + Print version ``` diff --git a/src/args.rs b/src/args.rs index 3ae140f..9c8e76f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -58,6 +58,10 @@ pub struct CLIArgs { #[arg(long = "display-gui", action = ArgAction::SetTrue)] pub display_gui: Option, + /// Use if you want to display debug information when launching + #[arg(long = "debug", action = ArgAction::SetTrue)] + pub debug: Option, + /// Color for currently playing text (in hex) #[arg(long = "text-color", default_value = None)] pub text_color: Option, @@ -101,6 +105,7 @@ pub fn cli_args_to_fft_args(mut cli_args: CLIArgs, use_default: bool) -> FFTArgs max_freq: cli_args.max_freq.unwrap(), display_gui: cli_args.display_gui.unwrap(), volume: cli_args.volume.unwrap(), + debug: cli_args.debug.unwrap(), } } diff --git a/src/main.rs b/src/main.rs index 83dd702..1b82a87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,19 +36,20 @@ use std::time::Duration; use std::time::Instant; // TODO: Add to other package managers -// TODO: Remove fft_fps and other deprecated configs from readme -// TODO: Add intensity rescaling and other options to yaml -// Constants +// Timing related constants const RENDERING_FPS: u32 = 60; const TIME_BETWEEN_FRAMES: f64 = 1.0 / RENDERING_FPS as f64; +const FFT_FPS: u32 = 12; +const TIME_BETWEEN_FFT_FRAMES: f64 = 1.0 / FFT_FPS as f64; + +// Normalization constants +const AVERAGING_WINDOW: u32 = 1; const RESCALING_THRESHOLDS: &[f32] = &[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]; const INTENSITY_RESCALING: &[f32] = &[0.4, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.6, 0.5]; const FREQ_RESCALING: &[f32] = &[0.9, 1.2, 1.2, 1.2, 1.0]; -const AVERAGING_WINDOW: u32 = 1; -const FFT_FPS: u32 = 12; -const TIME_BETWEEN_FFT_FRAMES: f64 = 1.0 / FFT_FPS as f64; +// Bar height clamps const MIN_BAR_HEIGHT: f32 = 0.001; const MAX_BAR_HEIGHT: f32 = 0.45; @@ -69,6 +70,7 @@ struct FFTArgs { min_freq: f32, max_freq: f32, display_gui: bool, + debug: bool, volume: u32, } @@ -93,7 +95,7 @@ struct FFTState { } fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec> { - println!("Computing FFT..."); + let now = Instant::now(); let mut fft = compute_fft( fp, FFT_FPS, @@ -102,12 +104,20 @@ fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec> { args.max_freq, ); + if args.debug { + println!("Computed FFT in {:?}", now.elapsed()); + } + + let now = Instant::now(); fft = smooth_fft(fft, AVERAGING_WINDOW); fft = intensity_normalize_fft(fft, RESCALING_THRESHOLDS, INTENSITY_RESCALING); fft = frequency_normalize_fft(fft, FREQ_RESCALING); + if args.debug { + println!("Normalized in {:?}", now.elapsed()); + } + let now = Instant::now(); let mut fft_vec = fft.fft; - // Reverses bar order and prepends for c in fft_vec.iter_mut() { let mut reversed = c.clone(); @@ -119,20 +129,24 @@ fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec> { fft_vec .par_iter_mut() .for_each(|x| space_interpolate(x, args.smoothness)); + if args.debug { + println!("Interpolated in {:?}", now.elapsed()); + } fft_vec } fn main() { - std::env::set_var("RUST_LOG", "none"); - // Parse CLI args let args = parse_cli_args(); let fp = PathBuf::from(OsString::from(&args.file_path)); + if !args.debug { + std::env::set_var("RUST_LOG", "none"); + } + // Compute and preprocess FFT (spatial + temporal interpolation and normalization) let fft_vec = compute_and_preprocess_fft(&fp, &args); - let volume = args.volume; // Initialize Bevy app @@ -180,7 +194,7 @@ fn main() { sink.set_volume(volume as f32 / 100.0); sink.append(source); - // Start stopwatch that keeps fft in sync + // Start timer that keeps fft in sync let fft_timer = Instant::now(); app.insert_resource(AppState { diff --git a/src/systems/update_fft.rs b/src/systems/update_fft.rs index c433d29..3e6b723 100644 --- a/src/systems/update_fft.rs +++ b/src/systems/update_fft.rs @@ -56,8 +56,10 @@ pub fn update_fft( .unwrap() .attribute_mut(Mesh::ATTRIBUTE_POSITION) .unwrap(); - let bar_value_1 = - (new_value.clone() * (h / 2.0) as f32).clamp(h * MIN_BAR_HEIGHT, h * MAX_BAR_HEIGHT); + let bar_value_1 = (new_value.clone() * (h / 2.0) as f32).clamp( + h * MIN_BAR_HEIGHT + args.border_size as f32, + h * MAX_BAR_HEIGHT + args.border_size as f32, + ); match dims { VertexAttributeValues::Float32x3(x) => { x[0][1] = bar_value_1;