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 debug info and cli flag, fixed mix and max height clamp bug #14

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
51 changes: 35 additions & 16 deletions README.md
Expand Up @@ -43,21 +43,40 @@ Arguments:
<FILE_PATH> File path to Audio file

Options:
--smoothness <SMOOTHNESS> Smoothing factor for spatial interpolation between bars
--freq-resolution <FREQ_RESOLUTION> Number of individual frequencies detected by the FFT
--min-freq <MIN_FREQ> Maximum frequency detected by FFT
--max-freq <MAX_FREQ> Minimum frequency detected by FFT
--volume <VOLUME> Volume
--width <WINDOW_WIDTH> Window width
--height <WINDOW_HEIGHT> Window height
--border-size <BORDER_SIZE> Border size for each bar
--border-color <BORDER_COLOR> Border color for each bar (in hex)
--bar-color <BAR_COLOR> Color for each bar (in hex)
--track-name Use if you want track name to be printed
--display-gui Use if you want the gui to be open when launched
--text-color <TEXT_COLOR> Color for currently playing text (in hex)
--font-size <FONT_SIZE> Font size of currently playing label
--smoothness <SMOOTHNESS>
Smoothing factor for spatial interpolation between bars
--freq-resolution <FREQ_RESOLUTION>
Number of individual frequencies detected by the FFT
--min-freq <MIN_FREQ>
Maximum frequency detected by FFT
--max-freq <MAX_FREQ>
Minimum frequency detected by FFT
--volume <VOLUME>
Volume
--width <WINDOW_WIDTH>
Window width
--height <WINDOW_HEIGHT>
Window height
--border-size <BORDER_SIZE>
Border size for each bar
--border-color <BORDER_COLOR>
Border color for each bar (in hex)
--bar-color <BAR_COLOR>
Color for each bar (in hex)
--track-name
Use if you want track name to be printed
--display-gui
Use if you want the gui to be open when launched
--debug
Use if you want to display debug information when launching
--text-color <TEXT_COLOR>
Color for currently playing text (in hex)
--font-size <FONT_SIZE>
Font size of currently playing label
--background-color <BACKGROUND_COLOR>
-h, --help Print help
-V, --version Print version

-h, --help
Print help
-V, --version
Print version
```
5 changes: 5 additions & 0 deletions src/args.rs
Expand Up @@ -58,6 +58,10 @@ pub struct CLIArgs {
#[arg(long = "display-gui", action = ArgAction::SetTrue)]
pub display_gui: Option<bool>,

/// Use if you want to display debug information when launching
#[arg(long = "debug", action = ArgAction::SetTrue)]
pub debug: 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 @@ -101,6 +105,7 @@ pub fn cli_args_to_fft_args(mut cli_args: CLIArgs, use_default: bool) -> FFTArgs
max_freq: cli_args.max_freq.unwrap(),
display_gui: cli_args.display_gui.unwrap(),
volume: cli_args.volume.unwrap(),
debug: cli_args.debug.unwrap(),
}
}

Expand Down
38 changes: 26 additions & 12 deletions src/main.rs
Expand Up @@ -36,19 +36,20 @@ use std::time::Duration;
use std::time::Instant;

// TODO: Add to other package managers
// TODO: Remove fft_fps and other deprecated configs from readme
// TODO: Add intensity rescaling and other options to yaml

// Constants
// Timing related constants
const RENDERING_FPS: u32 = 60;
const TIME_BETWEEN_FRAMES: f64 = 1.0 / RENDERING_FPS as f64;
const FFT_FPS: u32 = 12;
const TIME_BETWEEN_FFT_FRAMES: f64 = 1.0 / FFT_FPS as f64;

// Normalization constants
const AVERAGING_WINDOW: u32 = 1;
const RESCALING_THRESHOLDS: &[f32] = &[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];
const INTENSITY_RESCALING: &[f32] = &[0.4, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.6, 0.5];
const FREQ_RESCALING: &[f32] = &[0.9, 1.2, 1.2, 1.2, 1.0];
const AVERAGING_WINDOW: u32 = 1;
const FFT_FPS: u32 = 12;
const TIME_BETWEEN_FFT_FRAMES: f64 = 1.0 / FFT_FPS as f64;

// Bar height clamps
const MIN_BAR_HEIGHT: f32 = 0.001;
const MAX_BAR_HEIGHT: f32 = 0.45;

Expand All @@ -69,6 +70,7 @@ struct FFTArgs {
min_freq: f32,
max_freq: f32,
display_gui: bool,
debug: bool,
volume: u32,
}

Expand All @@ -93,7 +95,7 @@ struct FFTState {
}

fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec<Vec<f32>> {
println!("Computing FFT...");
let now = Instant::now();
let mut fft = compute_fft(
fp,
FFT_FPS,
Expand All @@ -102,12 +104,20 @@ fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec<Vec<f32>> {
args.max_freq,
);

if args.debug {
println!("Computed FFT in {:?}", now.elapsed());
}

let now = Instant::now();
fft = smooth_fft(fft, AVERAGING_WINDOW);
fft = intensity_normalize_fft(fft, RESCALING_THRESHOLDS, INTENSITY_RESCALING);
fft = frequency_normalize_fft(fft, FREQ_RESCALING);
if args.debug {
println!("Normalized in {:?}", now.elapsed());
}

let now = Instant::now();
let mut fft_vec = fft.fft;

// Reverses bar order and prepends
for c in fft_vec.iter_mut() {
let mut reversed = c.clone();
Expand All @@ -119,20 +129,24 @@ fn compute_and_preprocess_fft(fp: &PathBuf, args: &FFTArgs) -> Vec<Vec<f32>> {
fft_vec
.par_iter_mut()
.for_each(|x| space_interpolate(x, args.smoothness));
if args.debug {
println!("Interpolated in {:?}", now.elapsed());
}

fft_vec
}

fn main() {
std::env::set_var("RUST_LOG", "none");

// Parse CLI args
let args = parse_cli_args();
let fp = PathBuf::from(OsString::from(&args.file_path));

if !args.debug {
std::env::set_var("RUST_LOG", "none");
}

// 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
Expand Down Expand Up @@ -180,7 +194,7 @@ fn main() {
sink.set_volume(volume as f32 / 100.0);
sink.append(source);

// Start stopwatch that keeps fft in sync
// Start timer that keeps fft in sync
let fft_timer = Instant::now();

app.insert_resource(AppState {
Expand Down
6 changes: 4 additions & 2 deletions src/systems/update_fft.rs
Expand Up @@ -56,8 +56,10 @@ pub fn update_fft(
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
let bar_value_1 =
(new_value.clone() * (h / 2.0) as f32).clamp(h * MIN_BAR_HEIGHT, h * MAX_BAR_HEIGHT);
let bar_value_1 = (new_value.clone() * (h / 2.0) as f32).clamp(
h * MIN_BAR_HEIGHT + args.border_size as f32,
h * MAX_BAR_HEIGHT + args.border_size as f32,
);
match dims {
VertexAttributeValues::Float32x3(x) => {
x[0][1] = bar_value_1;
Expand Down