feat(audio): real noise suppression via vendored RNNoise (send + receive)
The two-sided NR plumbing (RemoteStream::recv_ns + the per-listener vc_set_remote_stream noise_reduction toggle) was wired but inert: ApmProcessor::create() returned a no-op passthrough, because the originally-planned webrtc-audio-processing has no working Windows/macOS build. Drop in RNNoise as the real backend behind the same ApmProcessor interface, lighting up both NR paths. - Vendor RNNoise (BSD-3 + CC0) at third_party/rnnoise/ — the vcpkg port is !windows !arm, so it can't cover our primary targets. Shrunk int8 model (78MB -> 11.7MB via upstream scripts/shrink_model.sh), built as a standalone C static lib with no RTCD (portable scalar path on x86, auto-NEON on arm64) under -DDISABLE_DEBUG_FLOAT. Model is baked in (rnnoise_create(NULL)); no runtime file. - New RnnoiseProcessor (core/src/audio/apm_processor.cpp) selected by ApmProcessor::create() when VOICECAT_HAS_NS. Mono/48kHz/480-sample; our clock is fixed 48kHz and Opus frame sizes are multiples of 480, so no resampling. RT-safe: allocates at construction, lock-free in the capture/playback callbacks. - Receive-side: lit up via the factory; gated to mono streams (a stereo stream is a screen-audio share, not voice). - Send-side (new): vc_set_input_noise_reduction(client, enable) ABI + vc_client::mic_ns_, run before input gain/VAD in on_capture_frame. A stereo mic is downmixed to mono ONLY when NR is on — with NR off a stereo mic keeps full stereo (never collapse mic quality unasked). - Enable C as a project language for the vendored lib. - New noise_suppression test: white noise through ApmProcessor::create() drops ~99.9% RMS. ctest --preset dev green, 28/28. windows-client DLL builds clean with vc_set_input_noise_reduction exported, system-only deps. - Docs synced: voice.md §10, tech-stack.md §1/§5, third_party/README.md, vcpkg.json note, PROGRESS.md, CLAUDE.md. Client on/off UI toggles (Windows/macOS/iOS) are the remaining follow-up. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
#include "audio/apm_processor.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
#ifdef VOICECAT_HAS_NS
|
||||
#include "rnnoise.h"
|
||||
#endif
|
||||
|
||||
namespace voicecat::audio {
|
||||
|
||||
namespace {
|
||||
@@ -57,12 +62,55 @@ class EnergyVadProcessor final : public ApmProcessor {
|
||||
int64_t last_voice_ms_ = 0; // epoch start -> gate begins closed until first loud frame
|
||||
};
|
||||
|
||||
#ifdef VOICECAT_HAS_NS
|
||||
// ── RnnoiseProcessor ─────────────────────────────────────────────────────────
|
||||
// Real noise suppression via vendored RNNoise (third_party/rnnoise; docs/voice.md §10-11).
|
||||
// RNNoise is a mono, 48 kHz, fixed 480-sample (10 ms) speech denoiser; our engine clock is fixed
|
||||
// at 48 kHz and every Opus frame size (480/960/1920/2880) is a multiple of 480, so we process
|
||||
// whole 480-sample chunks with no resampling and no cross-call carry. Mono only — callers gate
|
||||
// on a single channel (a stereo screen-audio share is never voice and isn't denoised).
|
||||
//
|
||||
// RT-safety (docs/architecture.md §3): the DenoiseState and the float scratch are allocated in the
|
||||
// ctor; process_capture() does no allocation/locking. The state is owned per-stream (recv) or
|
||||
// per-mic (send) so it persists across calls, which is exactly what RNNoise's overlap needs.
|
||||
class RnnoiseProcessor final : public ApmProcessor {
|
||||
public:
|
||||
RnnoiseProcessor() : st_(rnnoise_create(nullptr)) {}
|
||||
~RnnoiseProcessor() override {
|
||||
if (st_) rnnoise_destroy(st_);
|
||||
}
|
||||
|
||||
void process_render(const int16_t*, int, int) override {} // NS needs no AEC reference
|
||||
|
||||
bool process_capture(int16_t* pcm, int samples, int sample_rate) override {
|
||||
// RNNoise is 48 kHz only; anything else passes through untouched (our clock is 48 kHz, so
|
||||
// this guard never trips in practice — it's just a correctness backstop).
|
||||
if (!st_ || sample_rate != 48000) return true;
|
||||
for (int off = 0; off + kFrame <= samples; off += kFrame) {
|
||||
for (int i = 0; i < kFrame; ++i) in_[i] = static_cast<float>(pcm[off + i]);
|
||||
rnnoise_process_frame(st_, out_, in_);
|
||||
for (int i = 0; i < kFrame; ++i) {
|
||||
int32_t v = static_cast<int32_t>(std::lround(out_[i]));
|
||||
pcm[off + i] = static_cast<int16_t>(std::clamp(v, -32768, 32767));
|
||||
}
|
||||
}
|
||||
return true; // NS doesn't gate; the send path's VAD stays a separate stage
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr int kFrame = 480; // rnnoise_get_frame_size()
|
||||
DenoiseState* st_;
|
||||
float in_[kFrame];
|
||||
float out_[kFrame];
|
||||
};
|
||||
#endif // VOICECAT_HAS_NS
|
||||
|
||||
std::unique_ptr<ApmProcessor> ApmProcessor::create() {
|
||||
#ifdef VOICECAT_HAS_APM
|
||||
// TODO: return std::make_unique<WebrtcApmProcessor>(); — see create_vad()'s doc comment for
|
||||
// why this isn't wired up yet (no working Windows/MSVC build upstream).
|
||||
#endif
|
||||
#ifdef VOICECAT_HAS_NS
|
||||
return std::make_unique<RnnoiseProcessor>();
|
||||
#else
|
||||
return std::make_unique<ApmPassthrough>();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::unique_ptr<ApmProcessor> ApmProcessor::create_vad(float rms_threshold,
|
||||
|
||||
@@ -28,10 +28,12 @@ class ApmProcessor {
|
||||
// Safe to call from any thread — EnergyVadProcessor stores it atomically.
|
||||
virtual void set_threshold(float) {}
|
||||
|
||||
// Factory: returns a real APM if VOICECAT_HAS_APM is defined, else a passthrough. Used for
|
||||
// recv-side per-stream noise reduction (docs/voice.md §10) — gating doesn't apply there, so
|
||||
// this stays a passthrough until a real APM/NS backend exists (still inert; see
|
||||
// PROGRESS.md). Do not use this for the send-side VAD gate — see create_vad() below.
|
||||
// Factory for the noise-suppression backend (docs/voice.md §10-11): a real RNNoise denoiser
|
||||
// when VOICECAT_HAS_NS is defined (third_party/rnnoise), else a no-op passthrough. Used for
|
||||
// BOTH recv-side per-stream NR (RemoteStream::recv_ns) and send-side mic NR (vc_client's
|
||||
// mic_ns_). The RNNoise backend is mono/48 kHz only, so callers gate it on a single channel.
|
||||
// NS never gates (process_capture always returns true) — the send-side VAD is separate, see
|
||||
// create_vad() below.
|
||||
static std::unique_ptr<ApmProcessor> create();
|
||||
|
||||
// Factory for the send-side input gate (docs/voice.md §11): a lightweight, dependency-free
|
||||
|
||||
@@ -788,7 +788,9 @@ void AudioEngine::on_playback(int16_t* out, ma_uint32 frames) {
|
||||
|
||||
// `n` is samples-per-channel (matches the frame_samples convention used by
|
||||
// OpusEncoder::encode elsewhere in the codebase).
|
||||
if (stream.recv_ns)
|
||||
// RNNoise is mono-only; a stereo stream (screen-audio share) is never voice, so skip
|
||||
// NR there rather than denoise a garbled deinterleave (docs/voice.md §10).
|
||||
if (stream.recv_ns && dec_channels == 1)
|
||||
stream.recv_ns->process_capture(stream.decode_scratch.data(), n,
|
||||
static_cast<int>(params_.sample_rate));
|
||||
|
||||
|
||||
@@ -1009,6 +1009,24 @@ void vc_client::on_capture_frame(int kind, const int16_t* pcm, int samples, int
|
||||
(self_mic_muted_.load(std::memory_order_acquire) ||
|
||||
server_muted_.load(std::memory_order_acquire))) return;
|
||||
|
||||
// Send-side mic noise suppression (vc_set_input_noise_reduction) — MIC only. Runs first, on
|
||||
// the raw mic, so the gain boost and the VAD gate below both see the cleaned signal. mic_ns_
|
||||
// exists for the lifetime of the MIC stream; the atomic flip gates it without touching the
|
||||
// pointer on this RT callback. RNNoise is a mono 48 kHz denoiser, so a stereo mic is downmixed
|
||||
// to mono IN PLACE here — but only when NS is enabled. With NS off this block is skipped
|
||||
// entirely, so a stereo mic keeps full stereo: we never collapse mic quality unless asked.
|
||||
if (kind == static_cast<int>(VC_STREAM_MIC) && mic_ns_ &&
|
||||
input_noise_reduction_.load(std::memory_order_relaxed)) {
|
||||
int16_t* w = const_cast<int16_t*>(pcm);
|
||||
if (channels == 2) {
|
||||
for (int i = 0; i < samples; ++i)
|
||||
w[i] = static_cast<int16_t>(
|
||||
(static_cast<int32_t>(w[2 * i]) + static_cast<int32_t>(w[2 * i + 1])) / 2);
|
||||
channels = 1; // rest of the pipeline (gain, gate, encode) now sees a mono frame
|
||||
}
|
||||
if (channels == 1) mic_ns_->process_capture(w, samples, 48000);
|
||||
}
|
||||
|
||||
// Send-side mic input gain (vc_set_input_gain) — MIC only. Applied in place before the gate
|
||||
// so a boosted quiet mic also helps cross the VAD threshold. EnergyVadProcessor never writes
|
||||
// through its pointer, so the const_cast (same as the VAD path below) is safe; no allocation.
|
||||
@@ -1329,6 +1347,11 @@ void vc_client::handle_stream_announce_result(uint64_t req_id,
|
||||
mic_vad_ = voicecat::audio::ApmProcessor::create_vad(
|
||||
vad_threshold_.load(std::memory_order_relaxed));
|
||||
}
|
||||
// Mic noise suppressor, built once here (not on the RT capture callback). Always created
|
||||
// so the toggle is a pure atomic flip — see client.h's comment on mic_ns_.
|
||||
if (kind == static_cast<int>(VC_STREAM_MIC) && !mic_ns_) {
|
||||
mic_ns_ = voicecat::audio::ApmProcessor::create();
|
||||
}
|
||||
|
||||
// SCREEN_AUDIO loopback opens the WASAPI device in the channel's mode: stereo capture
|
||||
// when the channel is stereo (real L/R, no downmix), mono otherwise. Captured under
|
||||
@@ -1473,6 +1496,13 @@ vc_result vc_client::set_input_gain(float gain) {
|
||||
return VC_OK;
|
||||
}
|
||||
|
||||
vc_result vc_client::set_input_noise_reduction(bool enable) {
|
||||
// Pure local toggle (like set_input_gain): mic_ns_ is created with the MIC stream, this only
|
||||
// flips whether the capture callback runs it. Safe to call before a MIC stream exists.
|
||||
input_noise_reduction_.store(enable, std::memory_order_relaxed);
|
||||
return VC_OK;
|
||||
}
|
||||
|
||||
vc_result vc_client::set_remote_stream(uint32_t user_id, uint32_t stream_id, float gain,
|
||||
bool muted, bool noise_reduction) {
|
||||
if (state_net_.load(std::memory_order_acquire) != VC_STATE_CONNECTED) return VC_ERR_NOT_CONNECTED;
|
||||
@@ -1990,6 +2020,7 @@ vc_result vc_client::set_capture_channels(uint32_t, uint32_t) { return VC_ERR_NO
|
||||
vc_result vc_client::set_input_mode(vc_input_mode) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_vad_threshold(float) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_input_gain(float) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_input_noise_reduction(bool) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_push_to_talk(bool) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_self_mute(bool, bool) { return VC_ERR_NOT_IMPLEMENTED; }
|
||||
vc_result vc_client::set_remote_stream(uint32_t, uint32_t, float, bool, bool) {
|
||||
|
||||
@@ -56,6 +56,7 @@ struct vc_client {
|
||||
vc_result set_self_mute(bool mic_muted, bool deafened);
|
||||
vc_result set_output_volume(float gain);
|
||||
vc_result set_input_gain(float gain);
|
||||
vc_result set_input_noise_reduction(bool enable);
|
||||
vc_result set_remote_stream(uint32_t user_id, uint32_t stream_id, float gain, bool muted,
|
||||
bool noise_reduction);
|
||||
vc_result get_remote_stream(uint32_t user_id, uint32_t stream_id,
|
||||
@@ -313,12 +314,17 @@ struct vc_client {
|
||||
std::atomic<bool> ptt_active_{false};
|
||||
std::atomic<float> vad_threshold_{0.025f}; // remembered across mode switches
|
||||
std::atomic<float> input_gain_{1.0f}; // send-side MIC gain (vc_set_input_gain)
|
||||
std::atomic<bool> input_noise_reduction_{false}; // send-side MIC NS (vc_set_input_noise_reduction)
|
||||
|
||||
// External-playback mode (iOS VPIO): when true, ensure_audio_running() configures the
|
||||
// AudioEngine to skip its hardware playback device and drive the mixer on a timer instead,
|
||||
// delivering the final mix to the mixed-output sink. Set via vc_set_external_playback.
|
||||
std::atomic<bool> external_playback_{false};
|
||||
std::unique_ptr<voicecat::audio::ApmProcessor> mic_vad_;
|
||||
// Send-side mic noise suppressor (RNNoise). Constructed once with the MIC stream alongside
|
||||
// mic_vad_ (off the RT capture callback); toggling only flips input_noise_reduction_, so the
|
||||
// capture callback never allocates or races this pointer.
|
||||
std::unique_ptr<voicecat::audio::ApmProcessor> mic_ns_;
|
||||
|
||||
// teardown_voice() is called both from run_io()'s own cleanup (on the io_thread_, when
|
||||
// the read loop exits) and from disconnect() (on the caller's thread) -- without
|
||||
|
||||
@@ -127,6 +127,11 @@ vc_result vc_set_input_gain(vc_client* c, float gain) {
|
||||
return c->set_input_gain(gain);
|
||||
}
|
||||
|
||||
vc_result vc_set_input_noise_reduction(vc_client* c, int enable) {
|
||||
if (c == nullptr) return VC_ERR_INVALID_ARG;
|
||||
return c->set_input_noise_reduction(enable != 0);
|
||||
}
|
||||
|
||||
vc_result vc_set_remote_stream(vc_client* c, uint32_t user_id, uint32_t stream_id, float gain,
|
||||
int muted, int noise_reduction) {
|
||||
if (c == nullptr) return VC_ERR_INVALID_ARG;
|
||||
|
||||
Reference in New Issue
Block a user