/
update_fft.rs
90 lines (85 loc) · 3 KB
/
update_fft.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::fft::time_interpolate;
use crate::*;
use bevy::render::mesh::VertexAttributeValues;
use bevy::sprite::Anchor;
use bevy::{
app::AppExit,
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_egui::egui::{Align2, Color32, Stroke};
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use clap::{ArgAction, Parser};
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use rodio::{source::Source, Decoder, OutputStream};
use std::ffi::OsString;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::time::Duration;
pub fn update_fft(
mut commands: Commands,
mut window: Query<&mut Window>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut fft_state: ResMut<FFTState>,
mut app_state: ResMut<AppState>,
mut args: ResMut<FFTArgs>,
mut clear_color: ResMut<ClearColor>,
mut text_query: Query<&mut Text>,
) {
let h = window.single_mut().height();
let interval = app_state.rendering_fps / app_state.fft_fps;
// Get the current frame (either from fft or interpolation)
let curr_fft = match fft_state.total_frame_counter as u32 % interval {
0 => {
if fft_state.fft_frame_counter >= fft_state.fft.len() - 1 {
std::process::exit(0);
}
fft_state.fft[fft_state.fft_frame_counter].clone()
}
rem => time_interpolate(
&(fft_state.fft[fft_state.fft_frame_counter]),
&(fft_state.fft[fft_state.fft_frame_counter + 1]),
rem as f32 / interval as f32,
),
};
// Iterate through all currently displayed bars to change values
for (handle, new_value) in fft_state.curr_bars.chunks(2).zip(curr_fft.iter()) {
let (handle1, handle2) = (handle[0].0.clone_weak(), handle[1].0.clone_weak());
let dims = meshes
.get_mut(handle1)
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
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;
x[1][1] = bar_value_1;
x[2][1] = -bar_value_1;
x[3][1] = -bar_value_1;
}
_ => {}
}
let dims = meshes
.get_mut(handle2)
.unwrap()
.attribute_mut(Mesh::ATTRIBUTE_POSITION)
.unwrap();
let bar_value_2 = (new_value.clone() * (h / 2.0) as f32 - args.border_size as f32)
.clamp(h * MIN_BAR_HEIGHT, h * MAX_BAR_HEIGHT);
match dims {
VertexAttributeValues::Float32x3(x) => {
x[0][1] = bar_value_2;
x[1][1] = bar_value_2;
x[2][1] = -bar_value_2;
x[3][1] = -bar_value_2;
}
_ => {}
}
}
}