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

Pausing #8

Merged
merged 2 commits into from Mar 10, 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
19 changes: 11 additions & 8 deletions README.md
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
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
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
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
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;
}