AI models shared registry + Code cleanup + Better async handling + Some fixes, etc
This commit is contained in:
parent
a8ff3442ff
commit
520b98143f
62 changed files with 1683 additions and 1239 deletions
|
|
@ -1,7 +1,7 @@
|
|||
mod simple;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::Mutex;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
static NORMALIZER: OnceCell<Mutex<simple::GainNormalizer>> = OnceCell::new();
|
||||
|
||||
|
|
@ -16,13 +16,13 @@ pub fn init() {
|
|||
|
||||
pub fn normalize(input: &[i16]) -> Vec<i16> {
|
||||
match NORMALIZER.get() {
|
||||
Some(n) => n.lock().unwrap().normalize(input),
|
||||
Some(n) => n.lock().normalize(input),
|
||||
None => input.to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset() {
|
||||
if let Some(n) = NORMALIZER.get() {
|
||||
n.lock().unwrap().reset();
|
||||
n.lock().reset();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +1,27 @@
|
|||
mod none;
|
||||
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
mod nnnoiseless;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::Mutex;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use crate::config::structs::NoiseSuppressionBackend;
|
||||
|
||||
static BACKEND: OnceCell<NoiseSuppressionBackend> = OnceCell::new();
|
||||
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
static NNNOISELESS_STATE: OnceCell<Mutex<nnnoiseless::NnnoiselessNS>> = OnceCell::new();
|
||||
static NNNOISELESS_STATE: OnceCell<Mutex<crate::models::nnnoiseless::NnnoiselessNS>> = OnceCell::new();
|
||||
|
||||
pub fn init(backend: NoiseSuppressionBackend) {
|
||||
if BACKEND.get().is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
// fallback if nnnoiseless not compiled in
|
||||
#[cfg(not(feature = "nnnoiseless"))]
|
||||
if matches!(backend, NoiseSuppressionBackend::Nnnoiseless) {
|
||||
warn!("Nnnoiseless not compiled in, falling back to None");
|
||||
backend = NoiseSuppressionBackend::None;
|
||||
}
|
||||
|
||||
BACKEND.set(backend).ok();
|
||||
|
||||
match backend {
|
||||
|
|
@ -26,30 +30,25 @@ pub fn init(backend: NoiseSuppressionBackend) {
|
|||
}
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
NoiseSuppressionBackend::Nnnoiseless => {
|
||||
NNNOISELESS_STATE.set(Mutex::new(nnnoiseless::NnnoiselessNS::new())).ok();
|
||||
NNNOISELESS_STATE.set(Mutex::new(crate::models::nnnoiseless::NnnoiselessNS::new())).ok();
|
||||
info!("Noise suppression: Nnnoiseless");
|
||||
}
|
||||
#[cfg(not(feature = "nnnoiseless"))]
|
||||
NoiseSuppressionBackend::Nnnoiseless => {
|
||||
warn!("Nnnoiseless not compiled in, falling back to None");
|
||||
BACKEND.set(NoiseSuppressionBackend::None).ok();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process(input: &[i16]) -> Vec<i16> {
|
||||
match BACKEND.get() {
|
||||
Some(NoiseSuppressionBackend::None) | None => none::process(input),
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
Some(NoiseSuppressionBackend::Nnnoiseless) => {
|
||||
if let Some(state) = NNNOISELESS_STATE.get() {
|
||||
state.lock().unwrap().process(input)
|
||||
state.lock().process(input)
|
||||
} else {
|
||||
none::process(input)
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "nnnoiseless"))]
|
||||
Some(NoiseSuppressionBackend::Nnnoiseless) => none::process(input),
|
||||
_ => none::process(input),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,9 +57,9 @@ pub fn reset() {
|
|||
#[cfg(feature = "nnnoiseless")]
|
||||
Some(NoiseSuppressionBackend::Nnnoiseless) => {
|
||||
if let Some(state) = NNNOISELESS_STATE.get() {
|
||||
state.lock().unwrap().reset();
|
||||
state.lock().reset();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
use nnnoiseless::DenoiseState;
|
||||
use crate::config;
|
||||
|
||||
pub struct NnnoiselessNS {
|
||||
state: Box<DenoiseState<'static>>,
|
||||
buffer: Vec<f32>,
|
||||
}
|
||||
|
||||
impl NnnoiselessNS {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
state: DenoiseState::new(),
|
||||
buffer: Vec::with_capacity(config::NNNOISELESS_FRAME_SIZE * 2),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process(&mut self, input: &[i16]) -> Vec<i16> {
|
||||
for &sample in input {
|
||||
self.buffer.push(sample as f32);
|
||||
}
|
||||
|
||||
let mut output: Vec<i16> = Vec::with_capacity(input.len());
|
||||
|
||||
while self.buffer.len() >= config::NNNOISELESS_FRAME_SIZE {
|
||||
let mut input_frame = [0.0f32; 480];
|
||||
let mut output_frame = [0.0f32; 480];
|
||||
|
||||
input_frame.copy_from_slice(&self.buffer[..config::NNNOISELESS_FRAME_SIZE]);
|
||||
self.buffer.drain(..config::NNNOISELESS_FRAME_SIZE);
|
||||
|
||||
// process: input -> output (denoised)
|
||||
let _ = self.state.process_frame(&mut output_frame, &input_frame);
|
||||
|
||||
for &sample in &output_frame {
|
||||
let clamped = sample.clamp(i16::MIN as f32, i16::MAX as f32);
|
||||
output.push(clamped as i16);
|
||||
}
|
||||
}
|
||||
|
||||
if output.is_empty() {
|
||||
return input.to_vec();
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
// self.state = DenoiseState::new();
|
||||
// self.buffer.clear();
|
||||
|
||||
self.buffer.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,72 +1,72 @@
|
|||
mod none;
|
||||
mod energy;
|
||||
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
mod nnnoiseless;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::Mutex;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use crate::config::structs::VadBackend;
|
||||
use crate::DB;
|
||||
|
||||
static BACKEND: OnceCell<VadBackend> = OnceCell::new();
|
||||
static BACKEND: OnceCell<String> = OnceCell::new();
|
||||
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
static NNNOISELESS_STATE: OnceCell<Mutex<nnnoiseless::NnnoiselessVAD>> = OnceCell::new();
|
||||
static NNNOISELESS_STATE: OnceCell<Mutex<crate::models::nnnoiseless::NnnoiselessVAD>> = OnceCell::new();
|
||||
|
||||
pub fn init(backend: VadBackend) {
|
||||
pub fn init() {
|
||||
if BACKEND.get().is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
BACKEND.set(backend).ok();
|
||||
let backend = DB.get()
|
||||
.map(|db| db.read().vad_backend.clone())
|
||||
.unwrap_or_else(|| "energy".to_string());
|
||||
|
||||
match backend {
|
||||
VadBackend::None => {
|
||||
BACKEND.set(backend.clone()).ok();
|
||||
|
||||
match backend.as_str() {
|
||||
"none" => {
|
||||
info!("VAD: disabled");
|
||||
}
|
||||
VadBackend::Energy => {
|
||||
"energy" => {
|
||||
info!("VAD: Energy-based");
|
||||
}
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
VadBackend::Nnnoiseless => {
|
||||
NNNOISELESS_STATE.set(Mutex::new(nnnoiseless::NnnoiselessVAD::new())).ok();
|
||||
"nnnoiseless" => {
|
||||
NNNOISELESS_STATE.set(Mutex::new(crate::models::nnnoiseless::NnnoiselessVAD::new())).ok();
|
||||
info!("VAD: Nnnoiseless");
|
||||
}
|
||||
#[cfg(not(feature = "nnnoiseless"))]
|
||||
VadBackend::Nnnoiseless => {
|
||||
warn!("Nnnoiseless not compiled in, falling back to Energy");
|
||||
BACKEND.set(VadBackend::Energy).ok();
|
||||
other => {
|
||||
warn!("Unknown VAD backend '{}', falling back to energy", other);
|
||||
// overwrite with energy
|
||||
// (BACKEND already set, so energy::detect will be used via fallthrough)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns (is_voice, confidence)
|
||||
// returns (is_voice, confidence)
|
||||
pub fn detect(input: &[i16]) -> (bool, f32) {
|
||||
match BACKEND.get() {
|
||||
Some(VadBackend::None) | None => none::detect(input),
|
||||
Some(VadBackend::Energy) => energy::detect(input),
|
||||
match BACKEND.get().map(|s| s.as_str()) {
|
||||
Some("none") | None => none::detect(input),
|
||||
Some("energy") => energy::detect(input),
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
Some(VadBackend::Nnnoiseless) => {
|
||||
Some("nnnoiseless") => {
|
||||
if let Some(state) = NNNOISELESS_STATE.get() {
|
||||
state.lock().unwrap().detect(input)
|
||||
state.lock().detect(input)
|
||||
} else {
|
||||
energy::detect(input)
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "nnnoiseless"))]
|
||||
Some(VadBackend::Nnnoiseless) => energy::detect(input),
|
||||
_ => energy::detect(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset() {
|
||||
match BACKEND.get() {
|
||||
match BACKEND.get().map(|s| s.as_str()) {
|
||||
#[cfg(feature = "nnnoiseless")]
|
||||
Some(VadBackend::Nnnoiseless) => {
|
||||
Some("nnnoiseless") => {
|
||||
if let Some(state) = NNNOISELESS_STATE.get() {
|
||||
state.lock().unwrap().reset();
|
||||
state.lock().reset();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
use nnnoiseless::DenoiseState;
|
||||
use crate::config;
|
||||
|
||||
pub struct NnnoiselessVAD {
|
||||
state: Box<DenoiseState<'static>>,
|
||||
buffer: Vec<f32>,
|
||||
}
|
||||
|
||||
impl NnnoiselessVAD {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
state: DenoiseState::new(),
|
||||
buffer: Vec::with_capacity(config::NNNOISELESS_FRAME_SIZE * 2),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn detect(&mut self, input: &[i16]) -> (bool, f32) {
|
||||
for &sample in input {
|
||||
self.buffer.push(sample as f32);
|
||||
}
|
||||
|
||||
let mut total_vad = 0.0f32;
|
||||
let mut frame_count = 0u32;
|
||||
|
||||
while self.buffer.len() >= config::NNNOISELESS_FRAME_SIZE {
|
||||
let mut input_frame = [0.0f32; 480];
|
||||
let mut output_frame = [0.0f32; 480];
|
||||
|
||||
input_frame.copy_from_slice(&self.buffer[..config::NNNOISELESS_FRAME_SIZE]);
|
||||
self.buffer.drain(..config::NNNOISELESS_FRAME_SIZE);
|
||||
|
||||
let vad_prob = self.state.process_frame(&mut output_frame, &input_frame);
|
||||
total_vad += vad_prob;
|
||||
frame_count += 1;
|
||||
}
|
||||
|
||||
if frame_count == 0 {
|
||||
return (true, 0.5);
|
||||
}
|
||||
|
||||
let avg_vad = total_vad / frame_count as f32;
|
||||
let is_voice = avg_vad >= config::VAD_NNNOISELESS_THRESHOLD;
|
||||
|
||||
(is_voice, avg_vad)
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.state = DenoiseState::new();
|
||||
self.buffer.clear();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue