From 022f414acbe55895078c129489b74605bfea1e83 Mon Sep 17 00:00:00 2001 From: Emerald Date: Tue, 10 Oct 2023 14:27:24 -0400 Subject: [PATCH] mic level bar --- assets/style.ess | 39 +++- src/audio.rs | 42 +++-- src/main.rs | 8 +- src/tube.rs | 13 +- src/ui/levels.rs | 460 +++++++++++++++++++++++------------------------ src/ui/mod.rs | 27 ++- 6 files changed, 325 insertions(+), 264 deletions(-) diff --git a/assets/style.ess b/assets/style.ess index 3716a8f..81adc5a 100644 --- a/assets/style.ess +++ b/assets/style.ess @@ -3,13 +3,17 @@ left: 0px; } +.right { + position-type: absolute; + right: 0px; +} + .frames-container { width: 150px; height: 100%; display: flex; flex-direction: column; justify-content: center; - } .frame { @@ -33,3 +37,36 @@ height: auto; max-height: 75%; } + +.levels-container { + height: 100%; +} + +.levels-container div { + height: 100%; + align-items: center; + justify-content: center; +} + +.levels-container range { + padding: 0px; + margin: 10px; +} + +.range-back { + display: none; +} + +.mic-level { + padding: 0px; +} + +.levels-container .active .range-low { + background-color: green +} + +.levels-container .range-low { + background-color: blue +} + + diff --git a/src/audio.rs b/src/audio.rs index 142a682..3f276d5 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -10,39 +10,57 @@ use anyhow::{anyhow, Result}; pub struct MicMonitor(Stream); -pub type Level = Arc>; -#[derive(Resource, Deref)] -pub struct MicLevel(Level); +pub type Level = Mutex; + +pub static MIC_LEVEL_RAW: Level = Mutex::new(0.); +pub static MIC_SENS_RAW: Level = Mutex::new(1.); + +#[derive(Resource, Deref, Default)] +pub struct MicLevel(f32); + +impl MicLevel { + pub fn value(&self) -> f32 { + **self + } +} #[derive(Resource, Deref)] -pub struct MicSens(Level); +pub struct MicSens(f32); pub struct AudioPlugin; impl Plugin for AudioPlugin { fn build(&self, app: &mut App) { - app.add_startup_system(monitor); + app.add_startup_system(monitor) + .add_systems((update_mic_level, update_mic_sens).in_base_set(CoreSet::PreUpdate)) + .insert_resource(MicSens(1.)) + .init_resource::(); } } +pub fn update_mic_level(mut level: ResMut) { + level.0 = *MIC_LEVEL_RAW.lock().expect("Poisoned mutex"); +} + +pub fn update_mic_sens(mut sens: ResMut) { + sens.0 = *MIC_SENS_RAW.lock().expect("Poisoned mutex"); +} + pub fn monitor(world: &mut World) { let device = initialize().expect("Unable to init audio"); info!("Using device {}", device.name().unwrap()); let config = device.default_input_config().unwrap(); - let level = Arc::new(Mutex::new(0.)); - world.insert_resource(MicLevel(level.clone())); - let sens = Arc::new(Mutex::new(1.)); - world.insert_resource(MicSens(sens.clone())); + *MIC_SENS_RAW.lock().expect("Poisoned sens mutex") = 100.0; let stream = device .build_input_stream( &config.config(), move |data: &[f32], _: &InputCallbackInfo| { - let cur_level = { *level.lock().unwrap() }; - *level.lock().unwrap() = (data + let cur_level = { *MIC_LEVEL_RAW.lock().unwrap() }; + *MIC_LEVEL_RAW.lock().unwrap() = (data .iter() - .map(|e| e.abs() * *sens.lock().unwrap()) + .map(|e| e.abs() * *MIC_SENS_RAW.lock().unwrap()) .max_by(|a, b| a.total_cmp(b)) .unwrap() * 0.5) diff --git a/src/main.rs b/src/main.rs index 8dbf18f..a86aa9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,14 +64,12 @@ fn main() { } fn logging(level: Res, threshold: Res) { - info!("Audio in: {}", level.lock().unwrap()); + info!("Audio in: {}", **level); info!("Mic thresh: {}", **threshold); } -fn setup(mut commands: Commands, sens: ResMut) { +fn setup(mut commands: Commands) { commands.spawn(Camera2dBundle::default()); - - *sens.lock().expect("Poisoned sens mutex") = 100.0; } // window systems @@ -87,7 +85,7 @@ fn react_to_focus( } if event.focused { - commands.insert_resource(ClearColor(Color::ALICE_BLUE)); + commands.insert_resource(ClearColor(Color::PURPLE)); ui_events.send(UiMessage::Show); } else { commands.insert_resource(ClearColor(Color::NONE)); diff --git a/src/tube.rs b/src/tube.rs index 5aff2c5..428d6ae 100644 --- a/src/tube.rs +++ b/src/tube.rs @@ -3,7 +3,7 @@ use bevy::prelude::*; use bevy_tweening::{lens::TransformPositionLens, Animator, EaseMethod, Tween}; use std::time::Duration; -use crate::audio::MicLevel; +use crate::audio::{update_mic_level, MicLevel}; #[derive(Resource, Clone, Copy)] pub enum MouthState { @@ -42,7 +42,11 @@ pub struct TubePlugin; impl Plugin for TubePlugin { fn build(&self, app: &mut App) { app.add_startup_system(setup) - .add_system(audio_trigger.in_base_set(CoreSet::PreUpdate)) + .add_system( + audio_trigger + .in_base_set(CoreSet::PreUpdate) + .after(update_mic_level), + ) .add_system(run_animation.run_if(resource_changed::())) .add_system(sync_images) .insert_resource(MouthState::Closed) @@ -87,15 +91,14 @@ fn setup(mut commands: Commands, ass: Res) { const MAX_THRESHOLD: f32 = 10.0; fn audio_trigger( mut mouth_state: ResMut, - mic_level: Res, + level: Res, threshold: Res, mut buffer: ResMut, mut was_zero: Local, time: Res