Skip to content

Commit

Permalink
switched fully to bevy, fix rendering problems
Browse files Browse the repository at this point in the history
  • Loading branch information
gursi26 committed Mar 5, 2024
1 parent 044f98d commit 0433798
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "fftviz"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
authors = ["Gursimar Singh <gursi26.dev@gmail.com>"]
license = "MIT"
Expand All @@ -15,6 +15,7 @@ categories = ["command-line-utilities"]
bevy = "0.13.0"
bincode = "1.3.3"
clap = { version = "4.5.0", features = ["derive"] }
microfft = "0.5.1"
rayon = "1.9.0"
rodio = "0.17.3"
serde = { version = "1.0.197", features = ["derive"] }
Expand Down
Binary file removed assets/fonts/Roboto-Regular.ttf
Binary file not shown.
31 changes: 23 additions & 8 deletions src/fft.rs
@@ -1,16 +1,15 @@
use crate::*;
use serde::{Deserialize, Serialize};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use bincode::{deserialize, serialize};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rodio::{source::Source, Decoder, OutputStream};
use serde::{Deserialize, Serialize};
use spectrum_analyzer::scaling::divide_by_N_sqrt;
use spectrum_analyzer::windows::hann_window;
use spectrum_analyzer::windows::hamming_window;
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
use std::fs::File;
use std::io::{self, BufReader, Read, Write};
use std::path::PathBuf;


#[derive(Serialize, Deserialize, Debug)]
pub struct FFT {
pub fft: Vec<Vec<f32>>,
Expand All @@ -20,7 +19,6 @@ pub struct FFT {
pub max: f32,
}


pub fn time_interpolate(v1: &Vec<f32>, v2: &Vec<f32>, alpha: f32) -> Vec<f32> {
v1.iter()
.zip(v2.iter())
Expand All @@ -43,6 +41,23 @@ pub fn space_interpolate(v: &mut Vec<f32>, num_new_frames: u32) {
}
}

#[allow(dead_code)]
pub fn smooth_fft(mut fft: FFT, alpha: u32) -> FFT {
let mut new_fft = Vec::new();
for i in (alpha as usize)..(fft.num_frames - alpha as usize) {
let mut new_frame = fft.fft[i].clone();
new_frame.iter_mut().enumerate().for_each(|(j, x)| {
*x = ((i - alpha as usize)..(i + alpha as usize))
.into_iter()
.map(|i| fft.fft[i][j])
.sum::<f32>() as f32 / (2.0 * alpha as f32 + 1.0)
});
new_fft.push(new_frame);
}
fft.fft = new_fft;
fft
}

pub fn normalize_fft(mut fft: FFT, bounds: &[f32], scaling_factor: &[f32]) -> FFT {
let min_max_scale = fft.max - fft.min;
let rescale = |mut x: Vec<f32>| -> Vec<f32> {
Expand Down Expand Up @@ -106,11 +121,12 @@ pub fn compute_fft(audio_path: &PathBuf) -> FFT {
for (i, stereo) in frame.chunks(n_channels as usize).enumerate() {
samples[i] = stereo
.iter()
.map(|x| x.clone() as f32 / n_channels as f32)
.map(|x| x.clone() as f32 * 20.0 / n_channels as f32)
.sum::<f32>();
}

let hann_window = hann_window(&samples);
let hann_window = hamming_window(&samples);

let spectrum_hann_window = samples_fft_to_spectrum(
&hann_window,
sample_rate as u32,
Expand Down Expand Up @@ -142,4 +158,3 @@ pub fn compute_fft(audio_path: &PathBuf) -> FFT {
max,
}
}

7 changes: 3 additions & 4 deletions src/main.rs
Expand Up @@ -27,13 +27,14 @@ const SCREEN_HEIGHT: i32 = 700;

const FREQUENCY_RESOLUTION: u32 = 100;
const FFT_FPS: u32 = 12;
const FREQ_WINDOW_LOW: f32 = 0.0;
const FREQ_WINDOW_LOW: f32 = 50.0;
const FREQ_WINDOW_HIGH: f32 = 5000.0;
const FFT_WINDOW: i32 =
((256 as u64 / 107 as u64) * FREQUENCY_RESOLUTION as u64).next_power_of_two() as i32;
const BAR_INTERPOLATION_FACTOR: 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 RESCALING_FACTOR: &[f32] = &[2.0, 1.7, 1.3, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7];
// const RESCALING_FACTOR: &[f32] = &[1.0; 9];


#[derive(Debug, Parser)]
Expand Down Expand Up @@ -158,7 +159,6 @@ fn startup(
mut materials: ResMut<Assets<ColorMaterial>>,
mut fft_queue: ResMut<FFTQueue>,
args: Res<FFTArgs>,
asset_server: Res<AssetServer>,
) {
commands.spawn(Camera2dBundle {
camera: Camera {
Expand All @@ -174,9 +174,8 @@ fn startup(
let h = window.single_mut().physical_height();

if !args.disable_title {
let font = asset_server.load("fonts/Roboto-Regular.ttf");
let text_style = TextStyle {
font: font.clone(),
font: Default::default(),
font_size: args.font_size as f32,
color: Color::hex(args.text_color.clone()).unwrap(),
};
Expand Down

0 comments on commit 0433798

Please sign in to comment.