2026-01-06 23:32:58 +05:00
|
|
|
pub mod noise_suppression;
|
|
|
|
|
pub mod vad;
|
|
|
|
|
pub mod gain_normalizer;
|
|
|
|
|
|
|
|
|
|
use once_cell::sync::OnceCell;
|
2026-02-18 21:08:48 +05:00
|
|
|
use parking_lot::Mutex;
|
2026-01-06 23:32:58 +05:00
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
use crate::config::structs::NoiseSuppressionBackend;
|
2026-01-06 23:32:58 +05:00
|
|
|
use crate::DB;
|
|
|
|
|
|
|
|
|
|
static PROCESSOR: OnceCell<Mutex<AudioProcessor>> = OnceCell::new();
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ProcessedAudio {
|
|
|
|
|
pub samples: Vec<i16>,
|
|
|
|
|
pub is_voice: bool,
|
|
|
|
|
pub vad_confidence: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct AudioProcessor {
|
2026-02-18 21:08:48 +05:00
|
|
|
has_gain: bool,
|
|
|
|
|
has_ns: bool,
|
2026-01-06 23:32:58 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AudioProcessor {
|
2026-02-18 21:08:48 +05:00
|
|
|
fn new(ns: NoiseSuppressionBackend, gain: bool) -> Self {
|
2026-01-06 23:32:58 +05:00
|
|
|
noise_suppression::init(ns);
|
2026-02-18 21:08:48 +05:00
|
|
|
vad::init();
|
2026-01-06 23:32:58 +05:00
|
|
|
if gain {
|
|
|
|
|
gain_normalizer::init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self {
|
2026-02-18 21:08:48 +05:00
|
|
|
has_gain: gain,
|
|
|
|
|
has_ns: !matches!(ns, NoiseSuppressionBackend::None),
|
2026-01-06 23:32:58 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process(&mut self, input: &[i16]) -> ProcessedAudio {
|
2026-02-18 21:08:48 +05:00
|
|
|
let gained: Vec<i16>;
|
|
|
|
|
let after_gain: &[i16] = if self.has_gain {
|
|
|
|
|
gained = gain_normalizer::normalize(input);
|
|
|
|
|
&gained
|
|
|
|
|
} else {
|
|
|
|
|
input
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let suppressed: Vec<i16>;
|
|
|
|
|
let after_ns: &[i16] = if self.has_ns {
|
|
|
|
|
suppressed = noise_suppression::process(after_gain);
|
|
|
|
|
&suppressed
|
|
|
|
|
} else {
|
|
|
|
|
after_gain
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (is_voice, confidence) = vad::detect(after_ns);
|
2026-01-06 23:32:58 +05:00
|
|
|
|
|
|
|
|
ProcessedAudio {
|
2026-02-18 21:08:48 +05:00
|
|
|
samples: after_ns.to_vec(),
|
2026-01-06 23:32:58 +05:00
|
|
|
is_voice,
|
|
|
|
|
vad_confidence: confidence,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
|
noise_suppression::reset();
|
|
|
|
|
vad::reset();
|
|
|
|
|
gain_normalizer::reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Result<(), String> {
|
|
|
|
|
if PROCESSOR.get().is_some() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
let (ns, gain) = get_settings();
|
|
|
|
|
info!("Initializing audio processing: NS={:?}, Gain={}", ns, gain);
|
2026-01-06 23:32:58 +05:00
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
let processor = AudioProcessor::new(ns, gain);
|
2026-01-06 23:32:58 +05:00
|
|
|
PROCESSOR
|
|
|
|
|
.set(Mutex::new(processor))
|
2026-02-18 21:08:48 +05:00
|
|
|
.map_err(|_| "Audio processor already initialized".to_string())?;
|
2026-01-06 23:32:58 +05:00
|
|
|
|
|
|
|
|
info!("Audio processing initialized.");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn process(input: &[i16]) -> ProcessedAudio {
|
|
|
|
|
match PROCESSOR.get() {
|
2026-02-18 21:08:48 +05:00
|
|
|
Some(p) => p.lock().process(input),
|
2026-01-06 23:32:58 +05:00
|
|
|
None => ProcessedAudio {
|
|
|
|
|
samples: input.to_vec(),
|
|
|
|
|
is_voice: true,
|
|
|
|
|
vad_confidence: 1.0,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn reset() {
|
|
|
|
|
if let Some(p) = PROCESSOR.get() {
|
2026-02-18 21:08:48 +05:00
|
|
|
p.lock().reset();
|
2026-01-06 23:32:58 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
fn get_settings() -> (NoiseSuppressionBackend, bool) {
|
2026-01-06 23:32:58 +05:00
|
|
|
match DB.get() {
|
|
|
|
|
Some(db) => {
|
|
|
|
|
let settings = db.read();
|
2026-02-18 21:08:48 +05:00
|
|
|
(settings.noise_suppression, settings.gain_normalizer)
|
2026-01-06 23:32:58 +05:00
|
|
|
}
|
|
|
|
|
None => (
|
|
|
|
|
crate::config::DEFAULT_NOISE_SUPPRESSION,
|
|
|
|
|
crate::config::DEFAULT_GAIN_NORMALIZER,
|
|
|
|
|
),
|
|
|
|
|
}
|
2026-02-18 21:08:48 +05:00
|
|
|
}
|