c#项目使用webrtc的降噪模块方法

本文介绍了如何从webrtc中分离出语音降噪模块,使用cmake生成VS2017工程,并进行编译。接着在C#中调用编译得到的ns.exe进行语音降噪处理,提升语音识别性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分离webrtc的降噪(Noise Suppression)部分

webrtc是Google开源的优秀音视频处理及传输代码库,其中包含了audio processing、video processing算法。语音降噪模块就包含在audio processing中。本文的降噪算法引用自https://github.com/cpuimage/WebRTC_NS,用c语言编写,需要编译成exe可执行程序供c#程序调用。

使用cmake生成vs2017工程

cmake是一款生成c/c++项目的工具,下载地址:https://cmake.org/download/。
https://github.com/cpuimage/WebRTC_NS源码中包含了cmake所需的CMakeLists.txt.使用cmake工具生成vs工程的时候有两个坑需要注意:

  • CMakeLists.txt的最后一句“target_link_libraries(ns m)”需要注释掉,否则会报link-error.
  • cmake生成的工程main.c编码为uft.因为源文件里边有汉字,需改变编码为GBK。改变编码的方法为在vs2017打开的工程中,点击“文件”下面的“高级保存选项”。如果没有“高级保存选项”的请百度如何调出。

编译c源程序

在vs2017中按f5编译源程序。生成完之后会在debug文件下出现:

/* * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef COMMON_AUDIO_VAD_INCLUDE_VAD_H_ #define COMMON_AUDIO_VAD_INCLUDE_VAD_H_ #include <memory> #include "common_audio/vad/include/webrtc_vad.h" #include "rtc_base/checks.h" namespace webrtc { class Vad { public: enum Aggressiveness { kVadNormal = 0, kVadLowBitrate = 1, kVadAggressive = 2, kVadVeryAggressive = 3 }; enum Activity { kPassive = 0, kActive = 1, kError = -1 }; virtual ~Vad() = default; // Calculates a VAD decision for the given audio frame. Valid sample rates // are 8000, 16000, and 32000 Hz; the number of samples must be such that the // frame is 10, 20, or 30 ms long. virtual Activity VoiceActivity(const int16_t* audio, size_t num_samples, int sample_rate_hz) = 0; // Resets VAD state. virtual void Reset() = 0; }; // Returns a Vad instance that's implemented on top of WebRtcVad. std::unique_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness); } // namespace webrtc #endif // COMMON_AUDIO_VAD_INCLUDE_VAD_H_ /* * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include "common_audio/vad/include/vad.h" #include <memory> #include "common_audio/vad/include/webrtc_vad.h" #include "rtc_base/checks.h" namespace webrtc { namespace { class VadImpl final : public Vad { public: explicit VadImpl(Aggressiveness aggressiveness) : handle_(nullptr), aggressiveness_(aggressiveness) { Reset(); } ~VadImpl() override { WebRtcVad_Free(handle_); } Activity VoiceActivity(const int16_t* audio, size_t num_samples, int sample_rate_hz) override { int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples); switch (ret) { case 0: return kPassive; case 1: return kActive; default: RTC_DCHECK_NOTREACHED() << "WebRtcVad_Process returned an error."; return kError; } } void Reset() override { if (handle_) WebRtcVad_Free(handle_); handle_ = WebRtcVad_Create(); RTC_CHECK(handle_); RTC_CHECK_EQ(WebRtcVad_Init(handle_), 0); RTC_CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0); } private: VadInst* handle_; Aggressiveness aggressiveness_; }; } // namespace std::unique_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness) { return std::unique_ptr<Vad>(new VadImpl(aggressiveness)); } } // namespace webrtc 学习这段代码
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值