diff --git a/interface/Cargo.lock b/interface/Cargo.lock index 059abb6..0b8b141 100644 --- a/interface/Cargo.lock +++ b/interface/Cargo.lock @@ -434,6 +434,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atomic_float" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628d228f918ac3b82fe590352cc719d30664a0c13ca3a60266fe02c7132d480a" + [[package]] name = "atspi" version = "0.22.0" @@ -1883,6 +1889,7 @@ name = "interface" version = "0.1.0" dependencies = [ "anyhow", + "atomic_float", "common", "cpal", "eframe", diff --git a/interface/Cargo.toml b/interface/Cargo.toml index 02b9d7c..8ebdd93 100644 --- a/interface/Cargo.toml +++ b/interface/Cargo.toml @@ -18,3 +18,4 @@ eframe = { version = "0.30", features = ["persistence"] } egui_extras = { version = "0.30", features = ["default", "image"] } egui-toast = "0.16.0" home = "0.5.11" +atomic_float = "1.1.0" diff --git a/interface/src/gui.rs b/interface/src/gui.rs index 81d0f9d..0ec5318 100644 --- a/interface/src/gui.rs +++ b/interface/src/gui.rs @@ -1,12 +1,12 @@ -use std::{cell::OnceCell, path::PathBuf, time::Duration}; +use std::{cell::OnceCell, path::PathBuf, sync::atomic::Ordering, time::Duration}; use common::{ControlPacket, TelemetryPacket}; use eframe::{egui::{self, containers, Align2, Checkbox, Context, Id, Label}, Storage}; use tokio::{runtime::Runtime, sync::{mpsc, watch::Receiver}}; use egui_toast::{Toast, Toasts}; -use crate::storage_dir::storage_dir; +use crate::{storage_dir::storage_dir, POWER_THRESHOLD}; pub const GUI: OnceCell = OnceCell::new(); @@ -36,9 +36,9 @@ pub struct GUIData { } #[cfg(target_os = "linux")] -const DEFAULT_VOLUME_THRESHOLD: f32 = 5.0; +pub const DEFAULT_VOLUME_THRESHOLD: f32 = 5.0; #[cfg(target_os = "windows")] -const DEFAULT_VOLUME_THRESHOLD: f32 = 1.0; +pub const DEFAULT_VOLUME_THRESHOLD: f32 = 1.0; const DEFAULT_TURN_GAIN: f32 = 0.3; const DEFAULT_FIRE_DISTANCE: f32 = 55.0; @@ -96,6 +96,8 @@ impl eframe::App for GUI { storage.set_string("auto_turn_gain", format!("{}",self.auto_turn_gain)); storage.set_string("auto_fire_distance", format!("{}",self.auto_fire_distance)); storage.set_string("selected_auto", format!("{}",self.selected_auto)); + + POWER_THRESHOLD.store(self.volume_threshold, Ordering::Relaxed); }); egui::CentralPanel::default().show(ctx, |ui| { diff --git a/interface/src/main.rs b/interface/src/main.rs index ca1c616..a799877 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -1,11 +1,12 @@ #![feature(iter_collect_into)] -use std::{fmt::format, ops::ControlFlow, result, sync::Arc, thread::{self, sleep}, time::Duration}; +use std::{fmt::format, ops::ControlFlow, result, sync::{atomic::Ordering, Arc}, thread::{self, sleep}, time::Duration}; use anyhow::{Context, Ok, Result}; +use atomic_float::AtomicF32; use common::{ControlPacket, TelemetryPacket}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use egui_toast::{Toast, ToastKind}; -use gui::{gui, GUIData, GUI}; +use gui::{gui, GUIData, DEFAULT_VOLUME_THRESHOLD, GUI}; use pitch_detection::{detector::{mcleod::McLeodDetector, PitchDetector}, utils}; use rust_music_theory::note::{Note, NoteLetter, Pitch, Tuning}; use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter, WriteHalf}, net::{tcp::{OwnedReadHalf, OwnedWriteHalf}, TcpStream}, spawn, sync::{self, broadcast, mpsc, watch, RwLock}, time::timeout}; @@ -13,6 +14,8 @@ use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter, WriteHalf}, net::{tcp:: mod gui; mod storage_dir; +pub const POWER_THRESHOLD: AtomicF32 = AtomicF32::new(DEFAULT_VOLUME_THRESHOLD); + fn main() -> Result<()> { // assumes pulseaudio system with f32 samples and 2204 sample packets dbg!(cpal::available_hosts()); @@ -32,10 +35,6 @@ fn main() -> Result<()> { let (sender, notes) = broadcast::channel(2); - #[cfg(target_os = "linux")] - const POWER_THRESHOLD: f32 = 5.0; - #[cfg(target_os = "windows")] - const POWER_THRESHOLD: f32 = 1.0; const CLARITY_THRESHOLD: f32 = 0.85; #[cfg(target_os = "linux")] const PACKET_LEN: usize = 2204; @@ -66,7 +65,7 @@ fn main() -> Result<()> { // reinitialized every packet as it is not thread safe let mut detctor = McLeodDetector::new(PACKET_LEN, PACKET_LEN/2); let vol = utils::buffer::square_sum(data); - if let Err(e) = sender.send((detctor.get_pitch(data, rate.0 as usize, POWER_THRESHOLD, CLARITY_THRESHOLD).map(|p|p.frequency),vol)) { + if let Err(e) = sender.send((detctor.get_pitch(data, rate.0 as usize, POWER_THRESHOLD.load(Ordering::Relaxed), CLARITY_THRESHOLD).map(|p|p.frequency),vol)) { println!("channel: {e}"); }