Add a default noise floor.

Pretty simple, if the average amplitude is under a certain value clear
out the buffer! The value I chose (.001) was an arbitrary value I got
from printf debugging. I was able to show that this worked pretty well
on the desktop session. Hopefully we'll add this to the settings page at
some point.
This commit is contained in:
2026-01-10 17:11:15 -07:00
parent c8119d0efa
commit 2cb03208b4
+15
View File
@@ -10,6 +10,8 @@ use tracing::{error, info};
use crate::imp;
static DF_MODEL: Asset = asset!("/assets/DeepFilterNet3_ll_onnx.tar.gz");
// TODO: make this user configurable.
static DEFAULT_NOISE_FLOOR: f32 = 0.001;
enum DenoisingModelState {
Nothing,
@@ -76,6 +78,7 @@ pub struct AudioProcessor {
denoise: bool,
spawn: imp::SpawnHandle,
buffer: Vec<f32>,
noise_floor: f32,
}
impl AudioProcessor {
@@ -84,6 +87,7 @@ impl AudioProcessor {
denoise: false,
spawn: imp::SpawnHandle::current(),
buffer: Vec::new(),
noise_floor: DEFAULT_NOISE_FLOOR,
}
}
@@ -92,6 +96,7 @@ impl AudioProcessor {
denoise: true,
spawn: imp::SpawnHandle::current(),
buffer: Vec::new(),
noise_floor: DEFAULT_NOISE_FLOOR,
}
}
}
@@ -132,6 +137,16 @@ impl AudioProcessor {
if include_raw {
output.extend(audio.iter().step_by(channels).copied());
}
// Adds threshoulding to prevent sending audio when things are really quiet.
let avg: f32 = if output.is_empty() {
0.0
} else {
output.iter().map(|x| x.abs()).sum::<f32>() / output.len() as f32
};
if avg < self.noise_floor {
output.clear();
}
}
}