From 5d070cc3a9cc3c1614a8ff823452b12ba92bf7da Mon Sep 17 00:00:00 2001 From: gursi26 Date: Sun, 10 Mar 2024 17:55:12 -0400 Subject: [PATCH 1/2] added pause, volume control keybinds and volume cli arg --- README.md | 19 +++++++++++-------- src/args.rs | 8 +++++++- src/main.rs | 23 +++++++++++++++-------- src/systems/get_keyboard_input.rs | 18 ++++++++++++++++++ src/systems/update_fft.rs | 8 +++++--- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 95e0bfb..6461d5c 100644 --- a/README.md +++ b/README.md @@ -18,20 +18,23 @@ brew tap gursi26/fftviz brew install fftviz ``` +# Keybinds: +- `q` to close window. +- `e` to open config gui in player window. +- `Space` to pause/play. +- `↑` to increase volume. +- `↓` to decrease volume. + # Usage - Run fftviz with a path to an audio file. ``` fftviz "path/to/audio/file.mp3" ``` -- Keybinds: - - `q` to quit - - `e` for config gui - - Run with `-h` flag for configuration options ``` fftviz -h -A lightweight, customizable FFT visualizer for audio files. +A lightweight, customizable FFT visualizer for audio files Usage: fftviz [OPTIONS] @@ -41,7 +44,7 @@ Arguments: Options: --fft-fps Temporal resolution for FFT calculation (rendering always occurs at 60 fps with interpolation) [default: 12] - --bar-smoothness + --smoothness Smoothing factor for spatial interpolation between bars [default: 1] --freq-resolution Number of individual frequencies detected by the FFT [default: 90] @@ -49,8 +52,8 @@ Options: Maximum frequency detected by FFT [default: 0] --max-freq Minimum frequency detected by FFT [default: 5000] - --averaging-window - Size of averaging window (larger = less movement) [default: 1] + --volume + Volume [default: 0.7] --width Window width [default: 1000] --height diff --git a/src/args.rs b/src/args.rs index 9c5e056..c33a80f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -30,6 +30,10 @@ pub struct CLIArgs { #[arg(long = "max-freq", default_value_t = 5000.0)] max_freq: f32, + /// Volume + #[arg(long = "volume", default_value_t = 0.7)] + volume: f32, + /// Window width #[arg(long = "width", default_value_t = 1000.0)] window_width: f32, @@ -97,7 +101,9 @@ pub fn cli_args_to_fft_args(cli_args: CLIArgs) -> FFTArgs { window_width: cli_args.window_width, min_freq: cli_args.min_freq, max_freq: cli_args.max_freq, - display_gui: cli_args.display_gui + display_gui: cli_args.display_gui, + volume: cli_args.volume, + paused: false, } } diff --git a/src/main.rs b/src/main.rs index b84b593..fa46360 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,10 +63,13 @@ struct FFTArgs { min_freq: f32, max_freq: f32, display_gui: bool, + volume: f32, + paused: bool, } #[derive(Resource)] struct AppState { + sink: rodio::Sink, fft: Vec>, curr_bars: Vec<(Handle, Handle)>, despawn_handles: Vec, @@ -116,6 +119,8 @@ fn main() { // 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 let mut binding = App::new(); let app = binding @@ -146,13 +151,6 @@ fn main() { // Insert resources .insert_resource(ClearColor(args.background_color)) - .insert_resource(AppState { - fft: fft_vec, - curr_bars: Vec::new(), - despawn_handles: Vec::new(), - fft_frame_counter: 0, - total_frame_counter: 0, - }) .insert_resource(args) // Insert systems @@ -172,9 +170,18 @@ fn main() { let source = Decoder::new(file).unwrap(); let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = rodio::Sink::try_new(&stream_handle).unwrap(); - sink.set_volume(0.7); + sink.set_volume(volume); sink.append(source); + app.insert_resource(AppState { + sink, + fft: fft_vec, + curr_bars: Vec::new(), + despawn_handles: Vec::new(), + fft_frame_counter: 0, + total_frame_counter: 0, + }); + app.run(); } diff --git a/src/systems/get_keyboard_input.rs b/src/systems/get_keyboard_input.rs index 4973541..d73cc8c 100644 --- a/src/systems/get_keyboard_input.rs +++ b/src/systems/get_keyboard_input.rs @@ -30,4 +30,22 @@ pub fn get_keyboard_input( if keyboard_input.just_pressed(KeyCode::KeyE) { args.display_gui = !args.display_gui; } + if keyboard_input.just_pressed(KeyCode::Space) { + args.paused = !args.paused; + if app_state.sink.is_paused() { + app_state.sink.play(); + } else { + app_state.sink.pause(); + } + } + if keyboard_input.just_pressed(KeyCode::ArrowUp) { + args.volume += 0.1; + args.volume = args.volume.min(1.0); + app_state.sink.set_volume(args.volume); + } + if keyboard_input.just_pressed(KeyCode::ArrowDown) { + args.volume -= 0.1; + args.volume = args.volume.max(0.0); + app_state.sink.set_volume(args.volume); + } } diff --git a/src/systems/update_fft.rs b/src/systems/update_fft.rs index 8a35d30..5e570f2 100644 --- a/src/systems/update_fft.rs +++ b/src/systems/update_fft.rs @@ -85,8 +85,10 @@ pub fn update_fft( } // Moves real frame and interpolated frame counters - if update_i { - app_state.fft_frame_counter += 1; + if !args.paused { + if update_i { + app_state.fft_frame_counter += 1; + } + app_state.total_frame_counter += 1; } - app_state.total_frame_counter += 1; } From aa6b930880706ec99d159b7541710d5ecc77a00a Mon Sep 17 00:00:00 2001 From: gursi26 Date: Sun, 10 Mar 2024 17:55:57 -0400 Subject: [PATCH 2/2] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6461d5c..9f3f38b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ brew tap gursi26/fftviz brew install fftviz ``` -# Keybinds: +# Keybinds - `q` to close window. - `e` to open config gui in player window. - `Space` to pause/play.