Early support for measurements

This commit is contained in:
Keenan Tims 2023-11-11 13:41:59 -08:00
parent 22a27e4b61
commit 6fdcaa0f5b
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -2,7 +2,7 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::SampleFormat;
use egui::epaint::Hsva;
use egui::Color32;
use egui::{Color32, Label, Widget};
use egui_plot::{Legend, Line, Plot};
use std::mem::swap;
@ -105,8 +105,49 @@ struct MyApp {
ui_selections: MyAppUiSelections,
}
struct Db(f64);
trait MeasurementValueType {
fn label_format(&self) -> String;
}
struct Measurement<T: MeasurementValueType> {
name: &'static str,
values: Vec<T>,
}
impl MeasurementValueType for Db {
fn label_format(&self) -> String {
format!("{:2.2} dB", self.0)
}
}
impl<T: MeasurementValueType> Widget for Measurement<T> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
ui.group(|ui| {
ui.label(self.name);
ui.vertical(|ui| {
for (chan, value) in self.values.iter().enumerate() {
ui.colored_label(CHANNEL_COLOURS[chan], value.label_format());
}
})
}).response
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let measurements = vec![Measurement::<Db> {
name: &"RMS",
values: self
.last_result
.fft_analyses
.lock()
.unwrap()
.iter()
.map(|x| Db(lin_to_db(x.total_power as f64)))
.collect(),
}];
egui::SidePanel::left("left_panel").show(ctx, |ui| {
egui::ComboBox::from_label("Source")
.selected_text(self.audio_devices[self.ui_selections.audio_device].clone())
@ -127,6 +168,11 @@ impl eframe::App for MyApp {
});
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
for meas in measurements {
self.meas_box(ui, meas);
}
});
ui_plot(ui, self.last_result.clone());
});
}
@ -178,6 +224,9 @@ impl MyApp {
self.fft_progress.store(0, Ordering::Relaxed);
}
}
fn meas_box<T: MeasurementValueType>(&mut self, ui: &mut egui::Ui, meas: Measurement<T>) {
ui.add(meas);
}
fn new(cc: &eframe::CreationContext) -> Self {
let host = cpal::default_host();
println!("Audio host: {}", host.id().name());