import re with open("src/core/mod.rs", "r") as f: text = f.read() # 1. Add noise_gate_threshold to state text = text.replace( "let ptt_active = Arc::new(AtomicBool::new(false));", "let ptt_active = Arc::new(AtomicBool::new(false));\n let noise_gate_threshold = Arc::new(std::sync::atomic::AtomicU32::new(0.01f32.to_bits()));" ) # 2. Add noise_gate_threshold to capture thread closure text = text.replace( "let ptt_active_clone = ptt_active.clone();", "let ptt_active_clone = ptt_active.clone();\n let noise_gate_threshold_clone = noise_gate_threshold.clone();" ) # 3. Add noise gate logic to capture loop old_capture_loop = """ while let Ok(pcm) = capture_rx.recv() { if is_muted_clone.load(Ordering::Relaxed) { continue; } if ptt_mode_clone.load(Ordering::Relaxed) && !ptt_active_clone.load(Ordering::Relaxed) { continue; } if let Ok(encoded) = encoder.encode(&pcm) {""" new_capture_loop = """ while let Ok(pcm) = capture_rx.recv() { if is_muted_clone.load(Ordering::Relaxed) { continue; } if ptt_mode_clone.load(Ordering::Relaxed) && !ptt_active_clone.load(Ordering::Relaxed) { continue; } let ng_bits = noise_gate_threshold_clone.load(Ordering::Relaxed); let ng_thresh = f32::from_bits(ng_bits); if ng_thresh > 0.0001 { let mut sum_sq = 0.0f32; for &sample in &pcm { let normalized = (sample as f32) / 32768.0; sum_sq += normalized * normalized; } let rms = (sum_sq / pcm.len() as f32).sqrt(); if rms < ng_thresh { continue; } } if let Ok(encoded) = encoder.encode(&pcm) {""" text = text.replace(old_capture_loop, new_capture_loop) # 4. Handle CoreCommand::SetNoiseGateThreshold old_match_end = """ CoreCommand::SetPeerVolume(peer_id, vol) => { let mut guard = peer_volumes.lock().await; guard.insert(peer_id, vol); } } } """ new_match_end = """ CoreCommand::SetPeerVolume(peer_id, vol) => { let mut guard = peer_volumes.lock().await; guard.insert(peer_id, vol); } CoreCommand::SetNoiseGateThreshold(threshold) => { noise_gate_threshold.store(threshold.to_bits(), Ordering::Relaxed); } } } """ text = text.replace(old_match_end, new_match_end) with open("src/core/mod.rs", "w") as f: f.write(text)