diff --git a/Cargo.toml b/Cargo.toml index 09798b1..3fe1206 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fftviz" -version = "0.3.1" +version = "0.3.2" edition = "2021" authors = ["Gursimar Singh "] license = "MIT" @@ -23,6 +23,7 @@ serde = { version = "1.0.197", features = ["derive"] } dirs = "5.0.1" serde_yaml = "0.9.32" spectrum-analyzer = "1.5.0" +stopwatch = "0.0.7" [profile.release] strip = true diff --git a/README.md b/README.md index 2a1fd13..0138492 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Options: Use if you want the gui to be open when launched --debug Use if you want to display debug information when launching + --no-title-bar + Use to disable the title bar --text-color Color for currently playing text (in hex) --font-size diff --git a/src/args.rs b/src/args.rs index 9c8e76f..14a634f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -62,6 +62,10 @@ pub struct CLIArgs { #[arg(long = "debug", action = ArgAction::SetTrue)] pub debug: Option, + /// Use to disable the title bar + #[arg(long = "no-title-bar", action = ArgAction::SetFalse)] + pub title_bar: Option, + /// Color for currently playing text (in hex) #[arg(long = "text-color", default_value = None)] pub text_color: Option, @@ -104,6 +108,7 @@ pub fn cli_args_to_fft_args(mut cli_args: CLIArgs, use_default: bool) -> FFTArgs min_freq: cli_args.min_freq.unwrap(), max_freq: cli_args.max_freq.unwrap(), display_gui: cli_args.display_gui.unwrap(), + title_bar: cli_args.title_bar.unwrap(), volume: cli_args.volume.unwrap(), debug: cli_args.debug.unwrap(), } diff --git a/src/config.rs b/src/config.rs index 2614537..52ab55f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,6 +29,7 @@ pub struct ConfigFFTArgs { pub max_freq: Option, pub display_gui: Option, pub volume: Option, + pub title_bar: Option, } impl Default for ConfigFFTArgs { @@ -49,6 +50,7 @@ impl Default for ConfigFFTArgs { max_freq: Some(5000.0), display_gui: Some(false), volume: Some(50), + title_bar: Some(true), } } } @@ -85,6 +87,16 @@ macro_rules! update_cli_arg { }; } +macro_rules! update_boolean_cli_arg { + ($cli_arg: expr, $default_arg: expr, $config_arg: expr) => { + if $cli_arg.unwrap() == $default_arg.unwrap() { + if let Some(x) = $config_arg { + *$cli_arg = Some(x); + } + } + }; +} + macro_rules! overwrite_non_default_args { ($user_config_arg: expr, $fft_arg: expr) => { *$user_config_arg = Some($fft_arg); @@ -134,6 +146,7 @@ pub fn write_fftargs_to_config(args: &FFTArgs) { overwrite_non_default_args!(&mut default_args.min_freq, args.min_freq); overwrite_non_default_args!(&mut default_args.max_freq, args.max_freq); overwrite_non_default_args!(&mut default_args.volume, args.volume); + overwrite_non_default_args!(&mut default_args.title_bar, args.title_bar); let cfg_path = config_path(); create_dir_all(cfg_path.as_path().parent().unwrap()).unwrap(); @@ -146,10 +159,10 @@ pub fn write_fftargs_to_config(args: &FFTArgs) { serde_yaml::to_writer(config_file, &default_args).unwrap(); let mut cfg_yaml: Vec = read_to_string(&cfg_path) - .unwrap() // panic on possible file-reading errors - .lines() // split the string into an iterator of string slices - .map(String::from) // make each slice into a string - .collect(); // gather them together into a vector + .unwrap() + .lines() + .map(String::from) + .collect(); cfg_yaml.retain(|x| x.contains(":")); @@ -240,6 +253,11 @@ pub fn merge_config_with_cli_args(args: &mut CLIArgs, use_default: bool) { None::, default_user_config.window_height ); + update_cli_arg!( + &mut args.title_bar, + None::, + default_user_config.title_bar + ); return; } @@ -250,12 +268,9 @@ pub fn merge_config_with_cli_args(args: &mut CLIArgs, use_default: bool) { user_config_yaml = read_config(); } - if let Some(x) = user_config_yaml.display_track_name { - args.track_name = Some(x); - } - if let Some(x) = user_config_yaml.display_gui { - args.display_gui = Some(x); - } + update_boolean_cli_arg!(&mut args.track_name, default_user_config.display_track_name, user_config_yaml.display_track_name); + update_boolean_cli_arg!(&mut args.display_gui, default_user_config.display_gui, user_config_yaml.display_gui); + update_boolean_cli_arg!(&mut args.title_bar, default_user_config.title_bar, user_config_yaml.title_bar); update_cli_arg!( &mut args.background_color, diff --git a/src/main.rs b/src/main.rs index b2db0f6..c793e5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use std::path::PathBuf; use std::time::Instant; // TODO: Add to other package managers +// TODO: Clean up config and cli arg handling // Timing related constants const RENDERING_FPS: u32 = 60; @@ -58,6 +59,7 @@ struct FFTArgs { min_freq: f32, max_freq: f32, display_gui: bool, + title_bar: bool, debug: bool, volume: u32, } @@ -79,7 +81,7 @@ struct FFTState { despawn_handles: Vec, total_frame_counter: usize, fft_frame_counter: usize, - fft_timer: Instant, + fft_timer: stopwatch::Stopwatch, } fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec> { @@ -150,6 +152,7 @@ fn main() { ) .into(), name: Some("fftviz".into()), + decorations: args.title_bar, resolution: (args.window_width as f32, args.window_height as f32).into(), prevent_default_event_handling: false, enabled_buttons: bevy::window::EnabledButtons { @@ -183,7 +186,7 @@ fn main() { sink.append(source); // Start timer that keeps fft in sync - let fft_timer = Instant::now(); + let fft_timer = stopwatch::Stopwatch::start_new(); app.insert_resource(AppState { sink, diff --git a/src/systems/get_keyboard_input.rs b/src/systems/get_keyboard_input.rs index 1a6d707..5b19fdc 100644 --- a/src/systems/get_keyboard_input.rs +++ b/src/systems/get_keyboard_input.rs @@ -1,4 +1,4 @@ -use crate::{AppState, FFTArgs}; +use crate::{AppState, FFTArgs, FFTState}; use bevy::{ app::AppExit, prelude::*, @@ -8,6 +8,7 @@ pub fn get_keyboard_input( keyboard_input: Res>, mut exit: EventWriter, mut app_state: ResMut, + mut fft_state: ResMut, mut args: ResMut, ) { if keyboard_input.just_pressed(KeyCode::KeyQ) { @@ -20,8 +21,10 @@ pub fn get_keyboard_input( app_state.paused = !app_state.paused; if app_state.sink.is_paused() { app_state.sink.play(); + fft_state.fft_timer.start(); } else { app_state.sink.pause(); + fft_state.fft_timer.stop(); } } if keyboard_input.just_pressed(KeyCode::ArrowUp) {