-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_fft.rs
94 lines (88 loc) · 3.16 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
91
92
93
94
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 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 mut update_i = false;
let interval = RENDERING_FPS / args.fft_fps;
// Get the current frame (either from fft or interpolation)
let clamped_frame_counter = app_state.fft_frame_counter.clamp(0, app_state.fft.len() - 1);
let curr_fft = match app_state.total_frame_counter as u32 % interval {
0 => {
if app_state.fft_frame_counter > app_state.fft.len() {
std::process::exit(0);
}
update_i = true;
app_state.fft[clamped_frame_counter].clone()
}
rem => time_interpolate(
&(app_state.fft[clamped_frame_counter - 1]),
&(app_state.fft[clamped_frame_counter]),
rem as f32 / interval as f32,
),
};
// Iterate through all currently displayed bars to change values
for (handle, new_value) in app_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, h * MAX_BAR_HEIGHT);
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;
}
_ => {}
}
}
// Moves real frame and interpolated frame counters
if !args.paused {
if update_i {
app_state.fft_frame_counter += 1;
}
app_state.total_frame_counter += 1;
}
}