add ns code.

This commit is contained in:
luocai 2024-09-06 23:06:54 +08:00
parent 2bed1dacf2
commit 5c892bec39
2 changed files with 25 additions and 3 deletions

View File

@ -6,6 +6,7 @@
#include "api/audio/echo_canceller3_factory.h"
#include "main.h"
#include "modules/audio_processing/aec3/echo_canceller3.h"
#include "modules/audio_processing/ns/noise_suppressor.h"
#include <boost/asio/post.hpp>
#include <sstream>
@ -19,6 +20,10 @@ public:
linearOutputBuffer = std::make_unique<webrtc::AudioBuffer>(sampleRate, channels, sampleRate, channels, sampleRate, channels);
// RKAP_3A_Init(&m_vqe, AEC_TX_TYPE);
webrtc::NsConfig config;
config.target_level = webrtc::NsConfig::SuppressionLevel::k12dB;
noiseSuppressor = std::make_unique<webrtc::NoiseSuppressor>(config, sampleRate, channels);
noiseSuppressorBuffer = std::make_unique<webrtc::AudioBuffer>(sampleRate, channels, sampleRate, channels, sampleRate, channels);
}
std::unique_ptr<webrtc::EchoControl> echoCanceller;
@ -26,6 +31,9 @@ public:
std::unique_ptr<webrtc::AudioBuffer> farendBuffer;
std::unique_ptr<webrtc::AudioBuffer> linearOutputBuffer;
std::unique_ptr<webrtc::NoiseSuppressor> noiseSuppressor;
std::unique_ptr<webrtc::AudioBuffer> noiseSuppressorBuffer;
// RKAP_AEC_State m_vqe;
};
@ -54,6 +62,10 @@ void ProcessFileTask::run() {
m_outFilename = oss.str();
m_ofs = std::make_shared<std::ofstream>(m_outFilename, std::ofstream::binary);
oss.str("");
oss << DumpPath << "/ns_" << dspToString(m_dsp) << "_16k.pcm";
m_nsOfs = std::make_shared<std::ofstream>(oss.str(), std::ofstream::binary);
// m_speakerIfs = std::make_shared<std::ifstream>("/sdcard/speaker_8k.pcm", std::ifstream::binary);
// m_micIfs = std::make_shared<std::ifstream>("/sdcard/micin_8k.pcm", std::ifstream::binary);
// m_ofs = std::make_shared<std::ofstream>("/sdcard/out_8k.pcm", std::ofstream::binary);
@ -75,23 +87,32 @@ void ProcessFileTask::process() {
if (m_speakerIfs && m_micIfs && *m_speakerIfs && *m_micIfs) {
char farendBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
char nearendBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
char noiseSuppressorOutBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
char outBuffer[sizeof(int16_t) * 16000 / 1000 * 10] = {0};
webrtc::StreamConfig config(16000, 1); // 单声道
// char farendBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
// char nearendBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
// char outBuffer[sizeof(int16_t) * 8000 / 1000 * 10] = {0};
m_speakerIfs->read(farendBuffer, sizeof(farendBuffer));
m_micIfs->read(nearendBuffer, sizeof(nearendBuffer));
m_d->nearendBuffer->CopyFrom(reinterpret_cast<const int16_t *>(nearendBuffer), config);
if (m_dsp == Speex) {
m_speex->echoPlayback(reinterpret_cast<const int16_t *>(farendBuffer));
m_speex->echoCapture(reinterpret_cast<const int16_t *>(nearendBuffer), reinterpret_cast<int16_t *>(outBuffer));
} else if (m_dsp == AecMobile) {
m_d->noiseSuppressor->Analyze(*m_d->nearendBuffer);
m_d->noiseSuppressor->Process(m_d->nearendBuffer.get());
m_d->nearendBuffer->CopyTo(config, reinterpret_cast<int16_t *>(noiseSuppressorOutBuffer));
m_nsOfs->write(noiseSuppressorOutBuffer, sizeof(noiseSuppressorOutBuffer));
// LOG(info) << " " << m_d->noiseSuppressorBuffer->num_frames() << " " << m_d->nearendBuffer->num_frames();
m_webRtcAecm->echoPlayback(reinterpret_cast<const int16_t *>(farendBuffer), sizeof(farendBuffer) / 2);
m_webRtcAecm->echoCancellation(reinterpret_cast<int16_t *>(nearendBuffer), nullptr, reinterpret_cast<int16_t *>(outBuffer),
m_webRtcAecm->echoCancellation(reinterpret_cast<int16_t *>(nearendBuffer),
reinterpret_cast<int16_t *>(noiseSuppressorOutBuffer), reinterpret_cast<int16_t *>(outBuffer),
sizeof(farendBuffer) / 2);
} else if (m_dsp == Aec3) {
webrtc::StreamConfig config(16000, 1); // 单声道
m_d->nearendBuffer->CopyFrom(reinterpret_cast<const int16_t *>(nearendBuffer), config);
m_d->farendBuffer->CopyFrom(reinterpret_cast<const int16_t *>(farendBuffer), config);

View File

@ -96,6 +96,7 @@ private:
std::shared_ptr<std::ifstream> m_speakerIfs;
std::shared_ptr<std::ifstream> m_micIfs;
std::shared_ptr<std::ofstream> m_ofs;
std::shared_ptr<std::ofstream> m_nsOfs; // 降噪后的文件
std::string m_outFilename;
std::chrono::system_clock::time_point m_begin;
};