diff --git a/src/main.rs b/src/main.rs index cf01d6b..925cdf8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { + name: &'static str, + values: Vec, +} + +impl MeasurementValueType for Db { + fn label_format(&self) -> String { + format!("{:2.2} dB", self.0) + } +} + +impl Widget for Measurement { + 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:: { + 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(&mut self, ui: &mut egui::Ui, meas: Measurement) { + ui.add(meas); + } fn new(cc: &eframe::CreationContext) -> Self { let host = cpal::default_host(); println!("Audio host: {}", host.id().name());