Hi,
I created a class to handle aec using WebRTC library but somehow it is not working.
When using my class implementation I can clearly hear my voice in the farend signal.
Please let me know if I am doing something obviously wrong or point me to an example that works...
Please help me, I spent the last 30 days trying to figure it out, but no luck...
My code is pasted below.
This is my last resort...
Thanks a lot,
Daniel
bool WebRTCAEC::init(uint32_t aecDspFreq, uint32_t aecDspChannels, uint32_t soundCardFreq)
{
if (m_aecCtx || m_nsCtx)
{
uninit();
}
m_nsCtx = WebRtcNs_Create();
if (WebRtcNs_Init(m_nsCtx, aecDspFreq) < 0)
{
LOG_ERROR("WebRTCAEC", "init : WebRtcWrapperNs_Init failed");
return false;
}
if (WebRtcNs_set_policy(m_nsCtx, 2) < 0)
{
LOG_ERROR("WebRTCAEC", "init : WebRtcWrapperNs_set_policy failed");
return false;
}
m_aecCtx = WebRtcAec_Create();
if (!m_aecCtx)
{
LOG_ERROR("WebRTCAEC", "init : WebRtcAec_Create failed");
return false;
}
m_auxBuf = new Buffer();
m_auxBuf2 = new Buffer();
void * aecCore = WebRtcAec_aec_core(m_aecCtx);
if (!aecCore)
{
LOG_ERROR("WebRTCAEC", "init : WebRtcAec_aec_core failed (aecDspFreq: %u, soundCardFreq: %u)", aecDspFreq, soundCardFreq);
return false;
}
WebRtcAec_enable_delay_agnostic(aecCore, true);
WebRtcAec_enable_refined_adaptive_filter(aecCore, true);
WebRtcAec_enable_extended_filter(aecCore, true);
if (WebRtcAec_Init(m_aecCtx, aecDspFreq, soundCardFreq) < 0)
{
LOG_ERROR("WebRTCAEC", "init : could not initialize aec engine (aecDspFreq: %u, soundCardFreq: %u)",
aecDspFreq, soundCardFreq);
return false;
}
webrtc::AecConfig aec_config;
aec_config.nlpMode = webrtc::kAecNlpAggressive;
aec_config.skewMode = 0;
aec_config.metricsMode = 0;
aec_config.delay_logging = 0;
if (WebRtcAec_set_config(m_aecCtx, &aec_config) != 0)
{
LOG_ERROR("WebRTCAEC", "init : WebRtcWrapperAec_set_config failed",
aecDspFreq, soundCardFreq);
return false;
}
m_fmt.SetFormat(sizeof(float), aecDspFreq, aecDspChannels);
m_fmt.SetSampleFormat(AUDIO_SAMPLE_FMT_FLT);
return true;
}
bool WebRTCAEC::cancelEcho(const float * nearend, const float * farend, float * result, uint32_t samples)
{
if (!m_aecCtx)
{
LOG_ERROR("WebRTCAEC", "cancelEcho : not initialized");
return false;
}
if (!nearend || !farend || !result || !samples)
{
LOG_ERROR("WebRTCAEC", "cancelEcho : nearend/farend/samples not set");
return false;
}
int32_t ret = WebRtcAec_BufferFarend(m_aecCtx, farend, samples);
if (ret != 0)
{
LOG_ERROR("WebRTCAEC", "cancelEcho : WebRtcWrapperAec_BufferFarend failed (ret: %d)", ret);
return false;
}
uint32_t floatAudioChunkSize = m_fmt.FramesToBytes(samples);
m_auxBuf->alloc(floatAudioChunkSize);
m_auxBuf2->alloc(floatAudioChunkSize);
float * aecFreePtr = (float *)m_auxBuf->getDataPtr();
uint32_t soundCardDelayMs = 0;
uint32_t skew = 0;
ret = WebRtcAec_Process(m_aecCtx, &nearend, 1, &aecFreePtr, samples, soundCardDelayMs, skew);
if (ret != 0)
{
LOG_ERROR("WebRTCAEC", "cancelEcho : WebRtcWrapperAec_Process failed (ret: %d)", ret);
return false;
}
float * noiseFreePtr = (float *)m_auxBuf2->getDataPtr();
WebRtcNs_Process(m_nsCtx, &aecFreePtr, 1, &noiseFreePtr);
memcpy(result, noiseFreePtr, floatAudioChunkSize);
return true;
}