Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added no-title-bar option, switched to stopwatch to fix pause not wor… #17

Merged
merged 1 commit into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "fftviz"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Gursimar Singh <gursi26.dev@gmail.com>"]
license = "MIT"
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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 <TEXT_COLOR>
Color for currently playing text (in hex)
--font-size <FONT_SIZE>
Expand Down
5 changes: 5 additions & 0 deletions src/args.rs
Expand Up @@ -62,6 +62,10 @@ pub struct CLIArgs {
#[arg(long = "debug", action = ArgAction::SetTrue)]
pub debug: Option<bool>,

/// Use to disable the title bar
#[arg(long = "no-title-bar", action = ArgAction::SetFalse)]
pub title_bar: Option<bool>,

/// Color for currently playing text (in hex)
#[arg(long = "text-color", default_value = None)]
pub text_color: Option<String>,
Expand Down Expand Up @@ -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(),
}
Expand Down
35 changes: 25 additions & 10 deletions src/config.rs
Expand Up @@ -29,6 +29,7 @@ pub struct ConfigFFTArgs {
pub max_freq: Option<f32>,
pub display_gui: Option<bool>,
pub volume: Option<u32>,
pub title_bar: Option<bool>,
}

impl Default for ConfigFFTArgs {
Expand All @@ -49,6 +50,7 @@ impl Default for ConfigFFTArgs {
max_freq: Some(5000.0),
display_gui: Some(false),
volume: Some(50),
title_bar: Some(true),
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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<String> = 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(":"));

Expand Down Expand Up @@ -240,6 +253,11 @@ pub fn merge_config_with_cli_args(args: &mut CLIArgs, use_default: bool) {
None::<f32>,
default_user_config.window_height
);
update_cli_arg!(
&mut args.title_bar,
None::<bool>,
default_user_config.title_bar
);
return;
}

Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,7 @@ struct FFTArgs {
min_freq: f32,
max_freq: f32,
display_gui: bool,
title_bar: bool,
debug: bool,
volume: u32,
}
Expand All @@ -79,7 +81,7 @@ struct FFTState {
despawn_handles: Vec<Entity>,
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<Vec<f32>> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/systems/get_keyboard_input.rs
@@ -1,4 +1,4 @@
use crate::{AppState, FFTArgs};
use crate::{AppState, FFTArgs, FFTState};
use bevy::{
app::AppExit,
prelude::*,
Expand All @@ -8,6 +8,7 @@ pub fn get_keyboard_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut exit: EventWriter<AppExit>,
mut app_state: ResMut<AppState>,
mut fft_state: ResMut<FFTState>,
mut args: ResMut<FFTArgs>,
) {
if keyboard_input.just_pressed(KeyCode::KeyQ) {
Expand All @@ -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) {
Expand Down