Skip to content

Commit 24e5e79

Browse files
authored
Merge pull request #94 from wiiznokes/speex_denoise
Speex denoise
2 parents 97071b2 + 4dff6e3 commit 24e5e79

File tree

18 files changed

+609
-268
lines changed

18 files changed

+609
-268
lines changed

RustApp/Cargo.lock

Lines changed: 111 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RustApp/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ fix-path-env = { git = "https://github.com/wiiznokes/fix-path-env-rs", rev = "86
9494
env_logger = "0.11"
9595
nnnoiseless = "0.5"
9696
open = "5"
97+
speexdsp = { git = "https://github.com/wiiznokes/speexdsp-rs-fork", rev = "367a12d11ac2fea30ef712c779b7e5f5707d33fe", features = [
98+
"sys",
99+
"vendored-sys",
100+
] }
97101

98102
[build-dependencies]
99103
prost-build = "0.14"
@@ -107,3 +111,6 @@ winres = "0.1"
107111

108112
# [patch."https://github.com/pop-os/libcosmic.git"]
109113
# libcosmic = { path = "../../libcosmic" }
114+
115+
# [patch."https://github.com/wiiznokes/speexdsp-rs-fork"]
116+
# speexdsp = { path = "../../speexdsp-rs-fork" }

RustApp/i18n/en/android_mic.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ waiting = Waiting...
1111
sample_rate = Sample rate
1212
channel_count = Channel count
1313
audio_format = Audio format
14-
denoise = Use audio noise reduction
14+
denoise = Noise reduction
1515
1616
start_at_login = Start at login
1717
auto_connect = Auto connect

RustApp/i18n/fr/android_mic.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ waiting = Attente...
1111
sample_rate = Fréquence d'échantillonnage
1212
channel_count = Nombre de canaux
1313
audio_format = Format audio
14-
denoise = Activer la réduction du bruit audio
14+
denoise = Réduction du bruit
1515
1616
start_at_login = Démarrer à la connexion
1717
auto_connect = Connexion automatique

RustApp/justfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,3 @@ metainfo-check:
6262

6363
install_nsis: build-release nsis
6464
Start-Process (Get-ChildItem .\target\release\android-mic_*_x64-setup.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
65-

RustApp/src/audio/denoise_speex.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use speexdsp::preprocess::*;
2+
3+
// xxx: do we really need to change the sample rate ?
4+
// apparently, speexdsp is optimized for low sample rate (8000, 16000), according to chatgpt,
5+
// but 16000 just doesn't work on my end
6+
pub const DENOISE_SPEEX_SAMPLE_RATE: u32 = 48000;
7+
8+
pub struct DenoiseSpeexCache {
9+
denoisers: Vec<SpeexPreprocess>,
10+
}
11+
12+
// safe because packets are processed in order, and not concurrently
13+
unsafe impl Send for DenoiseSpeexCache {}
14+
15+
pub fn denoise_speex_f32_stream(
16+
data: &mut [Vec<i16>],
17+
cache: &mut Option<DenoiseSpeexCache>,
18+
noise_suppress: i32,
19+
) -> anyhow::Result<()> {
20+
const FRAME_SIZE: usize = (DENOISE_SPEEX_SAMPLE_RATE as f32 * 0.02) as usize; // 20 ms frame
21+
22+
if cache.is_none() {
23+
*cache = Some(DenoiseSpeexCache {
24+
denoisers: data
25+
.iter()
26+
.map(|_| {
27+
let mut st =
28+
SpeexPreprocess::new(FRAME_SIZE, DENOISE_SPEEX_SAMPLE_RATE as usize)
29+
.unwrap();
30+
st.set_denoise(true);
31+
st.set_noise_suppress(noise_suppress);
32+
st
33+
})
34+
.collect(),
35+
});
36+
}
37+
38+
for (channel, st) in data
39+
.iter_mut()
40+
.zip(cache.as_mut().unwrap().denoisers.iter_mut())
41+
{
42+
for frame in channel.chunks_exact_mut(FRAME_SIZE) {
43+
match st.preprocess_run(frame) {
44+
0 => {
45+
frame.fill(0);
46+
}
47+
1 => {}
48+
_ => panic!(),
49+
}
50+
}
51+
}
52+
53+
Ok(())
54+
}

RustApp/src/audio/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use crate::{
88
};
99

1010
mod denoise;
11+
mod denoise_speex;
1112
mod player;
1213
pub mod process;
1314
mod resampler;
15+
pub use denoise_speex::DenoiseSpeexCache;
1416

1517
impl AppState {
1618
pub fn start_audio_stream(

0 commit comments

Comments
 (0)