Skip to content

Commit

Permalink
Merge pull request #8 from gursi26/pausing
Browse files Browse the repository at this point in the history
Pausing
  • Loading branch information
gursi26 committed Mar 10, 2024
2 parents 661c977 + aa6b930 commit 39bd7d1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] <FILE_PATH>
Expand All @@ -41,16 +44,16 @@ Arguments:
Options:
--fft-fps <FFT_FPS>
Temporal resolution for FFT calculation (rendering always occurs at 60 fps with interpolation) [default: 12]
--bar-smoothness <BAR_SMOOTHNESS>
--smoothness <SMOOTHNESS>
Smoothing factor for spatial interpolation between bars [default: 1]
--freq-resolution <FREQ_RESOLUTION>
Number of individual frequencies detected by the FFT [default: 90]
--min-freq <MIN_FREQ>
Maximum frequency detected by FFT [default: 0]
--max-freq <MAX_FREQ>
Minimum frequency detected by FFT [default: 5000]
--averaging-window <AVERAGING_WINDOW>
Size of averaging window (larger = less movement) [default: 1]
--volume <VOLUME>
Volume [default: 0.7]
--width <WINDOW_WIDTH>
Window width [default: 1000]
--height <WINDOW_HEIGHT>
Expand Down
8 changes: 7 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<f32>>,
curr_bars: Vec<(Handle<Mesh>, Handle<ColorMaterial>)>,
despawn_handles: Vec<Entity>,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
}

18 changes: 18 additions & 0 deletions src/systems/get_keyboard_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 5 additions & 3 deletions src/systems/update_fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 39bd7d1

Please sign in to comment.