2017-02-02 17:24:40 +01:00
|
|
|
//
|
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
|
|
|
//
|
|
|
|
|
2018-11-21 19:22:31 +01:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
|
|
|
#include "webrtc_dsp/modules/audio_processing/include/audio_processing.h"
|
|
|
|
#include "webrtc_dsp/api/audio/audio_frame.h"
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 17:24:40 +01:00
|
|
|
#include "EchoCanceller.h"
|
|
|
|
#include "audio/AudioOutput.h"
|
2017-05-14 15:28:17 +02:00
|
|
|
#include "audio/AudioInput.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
#include "logging.h"
|
2018-12-19 03:10:11 +01:00
|
|
|
#include "VoIPServerConfig.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
#include <string.h>
|
2017-03-30 16:06:59 +02:00
|
|
|
#include <stdio.h>
|
2019-02-10 13:45:30 +01:00
|
|
|
#include <math.h>
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
using namespace tgvoip;
|
|
|
|
|
|
|
|
EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2017-03-30 16:06:59 +02:00
|
|
|
this->enableAEC=enableAEC;
|
|
|
|
this->enableAGC=enableAGC;
|
|
|
|
this->enableNS=enableNS;
|
2018-05-28 01:17:22 +02:00
|
|
|
isOn=true;
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2018-11-21 19:22:31 +01:00
|
|
|
webrtc::Config extraConfig;
|
|
|
|
#ifdef TGVOIP_USE_DESKTOP_DSP
|
|
|
|
extraConfig.Set(new webrtc::DelayAgnostic(true));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
apm=webrtc::AudioProcessingBuilder().Create(extraConfig);
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2018-11-21 19:22:31 +01:00
|
|
|
webrtc::AudioProcessing::Config config;
|
|
|
|
config.echo_canceller.enabled = enableAEC;
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef TGVOIP_USE_DESKTOP_DSP
|
2018-11-21 19:22:31 +01:00
|
|
|
config.echo_canceller.mobile_mode = true;
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2018-11-21 19:22:31 +01:00
|
|
|
config.echo_canceller.mobile_mode = false;
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2018-11-21 19:22:31 +01:00
|
|
|
config.high_pass_filter.enabled = enableAEC;
|
|
|
|
config.gain_controller2.enabled = enableAGC;
|
|
|
|
apm->ApplyConfig(config);
|
2020-01-21 03:25:18 +01:00
|
|
|
|
2018-12-19 03:10:11 +01:00
|
|
|
webrtc::NoiseSuppression::Level nsLevel;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
switch(ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level_vpio", 0)){
|
|
|
|
#else
|
|
|
|
switch(ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level", 2)){
|
|
|
|
#endif
|
|
|
|
case 0:
|
|
|
|
nsLevel=webrtc::NoiseSuppression::Level::kLow;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
nsLevel=webrtc::NoiseSuppression::Level::kModerate;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
nsLevel=webrtc::NoiseSuppression::Level::kVeryHigh;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
default:
|
|
|
|
nsLevel=webrtc::NoiseSuppression::Level::kHigh;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
apm->noise_suppression()->set_level(nsLevel);
|
2018-11-21 19:22:31 +01:00
|
|
|
apm->noise_suppression()->Enable(enableNS);
|
|
|
|
if(enableAGC){
|
|
|
|
apm->gain_control()->set_mode(webrtc::GainControl::Mode::kAdaptiveDigital);
|
2018-12-19 03:10:11 +01:00
|
|
|
apm->gain_control()->set_target_level_dbfs(ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_target_level", 9));
|
|
|
|
apm->gain_control()->enable_limiter(ServerConfig::GetSharedInstance()->GetBoolean("webrtc_agc_enable_limiter", true));
|
|
|
|
apm->gain_control()->set_compression_gain_db(ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_compression_gain", 20));
|
2017-03-30 16:06:59 +02:00
|
|
|
}
|
2018-11-30 13:39:31 +01:00
|
|
|
apm->voice_detection()->set_likelihood(webrtc::VoiceDetection::Likelihood::kVeryLowLikelihood);
|
2017-03-30 16:06:59 +02:00
|
|
|
|
2018-11-21 19:22:31 +01:00
|
|
|
audioFrame=new webrtc::AudioFrame();
|
|
|
|
audioFrame->samples_per_channel_=480;
|
|
|
|
audioFrame->sample_rate_hz_=48000;
|
|
|
|
audioFrame->num_channels_=1;
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
farendQueue=new BlockingQueue<Buffer>(11);
|
2018-11-21 19:22:31 +01:00
|
|
|
running=true;
|
|
|
|
bufferFarendThread=new Thread(std::bind(&EchoCanceller::RunBufferFarendThread, this));
|
2019-04-15 01:43:10 +02:00
|
|
|
bufferFarendThread->SetName("VoipECBufferFarEnd");
|
2018-11-21 19:22:31 +01:00
|
|
|
bufferFarendThread->Start();
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
#else
|
|
|
|
this->enableAEC=this->enableAGC=enableAGC=this->enableNS=enableNS=false;
|
|
|
|
isOn=true;
|
2017-04-09 18:14:33 +02:00
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
EchoCanceller::~EchoCanceller(){
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2018-11-21 19:22:31 +01:00
|
|
|
delete apm;
|
|
|
|
delete audioFrame;
|
2019-04-15 01:43:10 +02:00
|
|
|
farendQueue->Put(Buffer());
|
|
|
|
bufferFarendThread->Join();
|
|
|
|
delete bufferFarendThread;
|
2018-07-17 18:48:21 +02:00
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void EchoCanceller::Start(){
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void EchoCanceller::Stop(){
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void EchoCanceller::SpeakerOutCallback(unsigned char* data, size_t len){
|
2018-05-28 01:17:22 +02:00
|
|
|
if(len!=960*2 || !enableAEC || !isOn)
|
2017-02-02 17:24:40 +01:00
|
|
|
return;
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2019-04-15 01:43:10 +02:00
|
|
|
try{
|
|
|
|
Buffer buf=farendBufferPool.Get();
|
|
|
|
buf.CopyFrom(data, 0, 960*2);
|
|
|
|
farendQueue->Put(std::move(buf));
|
|
|
|
}catch(std::bad_alloc& x){
|
|
|
|
LOGW("Echo canceller can't keep up with real time");
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
2018-07-17 18:48:21 +02:00
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2018-11-09 16:44:01 +01:00
|
|
|
void EchoCanceller::RunBufferFarendThread(){
|
2018-11-21 19:22:31 +01:00
|
|
|
webrtc::AudioFrame frame;
|
|
|
|
frame.num_channels_=1;
|
|
|
|
frame.sample_rate_hz_=48000;
|
|
|
|
frame.samples_per_channel_=480;
|
2017-02-02 17:24:40 +01:00
|
|
|
while(running){
|
2019-04-15 01:43:10 +02:00
|
|
|
Buffer buf=farendQueue->GetBlocking();
|
|
|
|
if(buf.IsEmpty()){
|
|
|
|
LOGI("Echo canceller buffer farend thread exiting");
|
|
|
|
return;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
2019-04-15 01:43:10 +02:00
|
|
|
int16_t* samplesIn=(int16_t*)*buf;
|
|
|
|
memcpy(frame.mutable_data(), samplesIn, 480*2);
|
|
|
|
apm->ProcessReverseStream(&frame);
|
|
|
|
memcpy(frame.mutable_data(), samplesIn+480, 480*2);
|
|
|
|
apm->ProcessReverseStream(&frame);
|
|
|
|
didBufferFarend=true;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 18:48:21 +02:00
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void EchoCanceller::Enable(bool enabled){
|
2018-05-28 01:17:22 +02:00
|
|
|
isOn=enabled;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:39:31 +01:00
|
|
|
void EchoCanceller::ProcessInput(int16_t* inOut, size_t numSamples, bool& hasVoice){
|
2019-02-10 13:45:30 +01:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2018-05-28 01:17:22 +02:00
|
|
|
if(!isOn || (!enableAEC && !enableAGC && !enableNS)){
|
2017-02-02 17:24:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-21 19:22:31 +01:00
|
|
|
int delay=audio::AudioInput::GetEstimatedDelay()+audio::AudioOutput::GetEstimatedDelay();
|
|
|
|
assert(numSamples==960);
|
|
|
|
|
|
|
|
memcpy(audioFrame->mutable_data(), inOut, 480*2);
|
2018-11-30 13:39:31 +01:00
|
|
|
if(enableAEC)
|
|
|
|
apm->set_stream_delay_ms(delay);
|
2018-11-21 19:22:31 +01:00
|
|
|
apm->ProcessStream(audioFrame);
|
2018-11-30 13:39:31 +01:00
|
|
|
if(enableVAD)
|
|
|
|
hasVoice=apm->voice_detection()->stream_has_voice();
|
2018-11-21 19:22:31 +01:00
|
|
|
memcpy(inOut, audioFrame->data(), 480*2);
|
|
|
|
memcpy(audioFrame->mutable_data(), inOut+480, 480*2);
|
2018-11-30 13:39:31 +01:00
|
|
|
if(enableAEC)
|
|
|
|
apm->set_stream_delay_ms(delay);
|
2018-11-21 19:22:31 +01:00
|
|
|
apm->ProcessStream(audioFrame);
|
2018-11-30 13:39:31 +01:00
|
|
|
if(enableVAD){
|
|
|
|
hasVoice=hasVoice || apm->voice_detection()->stream_has_voice();
|
|
|
|
}
|
2018-11-21 19:22:31 +01:00
|
|
|
memcpy(inOut+480, audioFrame->data(), 480*2);
|
2019-02-10 13:45:30 +01:00
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 18:25:47 +01:00
|
|
|
void EchoCanceller::SetAECStrength(int strength){
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2018-11-21 19:22:31 +01:00
|
|
|
/*if(aec){
|
2017-12-27 18:25:47 +01:00
|
|
|
#ifndef TGVOIP_USE_DESKTOP_DSP
|
|
|
|
AecmConfig cfg;
|
|
|
|
cfg.cngMode=AecmFalse;
|
|
|
|
cfg.echoMode=(int16_t) strength;
|
|
|
|
WebRtcAecm_set_config(aec, cfg);
|
|
|
|
#endif
|
2018-11-21 19:22:31 +01:00
|
|
|
}*/
|
2018-07-17 18:48:21 +02:00
|
|
|
#endif
|
2017-12-27 18:25:47 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:39:31 +01:00
|
|
|
void EchoCanceller::SetVoiceDetectionEnabled(bool enabled){
|
|
|
|
enableVAD=enabled;
|
2019-02-10 13:45:30 +01:00
|
|
|
#ifndef TGVOIP_NO_DSP
|
2018-11-30 13:39:31 +01:00
|
|
|
apm->voice_detection()->Enable(enabled);
|
2019-02-10 13:45:30 +01:00
|
|
|
#endif
|
2018-11-30 13:39:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
using namespace tgvoip::effects;
|
|
|
|
|
2017-09-07 07:39:33 +02:00
|
|
|
AudioEffect::~AudioEffect(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffect::SetPassThrough(bool passThrough){
|
|
|
|
this->passThrough=passThrough;
|
|
|
|
}
|
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
Volume::Volume(){
|
2017-09-07 07:39:33 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
Volume::~Volume(){
|
2017-09-07 07:39:33 +02:00
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
}
|
2017-09-07 07:39:33 +02:00
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
void Volume::Process(int16_t* inOut, size_t numSamples){
|
|
|
|
if(level==1.0f || passThrough){
|
|
|
|
return;
|
2017-09-07 07:39:33 +02:00
|
|
|
}
|
2018-12-30 00:24:55 +01:00
|
|
|
for(size_t i=0;i<numSamples;i++){
|
|
|
|
float sample=(float)inOut[i]*multiplier;
|
|
|
|
if(sample>32767.0f)
|
|
|
|
inOut[i]=INT16_MAX;
|
|
|
|
else if(sample<-32768.0f)
|
|
|
|
inOut[i]=INT16_MIN;
|
|
|
|
else
|
|
|
|
inOut[i]=(int16_t)sample;
|
2017-09-07 07:39:33 +02:00
|
|
|
}
|
2018-12-30 00:24:55 +01:00
|
|
|
}
|
2017-09-07 07:39:33 +02:00
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
void Volume::SetLevel(float level){
|
|
|
|
this->level=level;
|
|
|
|
float db;
|
|
|
|
if(level<1.0f)
|
|
|
|
db=-50.0f*(1.0f-level);
|
|
|
|
else if(level>1.0f && level<=2.0f)
|
|
|
|
db=10.0f*(level-1.0f);
|
|
|
|
else
|
|
|
|
db=0.0f;
|
|
|
|
multiplier=expf(db/20.0f * logf(10.0f));
|
2017-09-07 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:24:55 +01:00
|
|
|
float Volume::GetLevel(){
|
|
|
|
return level;
|
|
|
|
}
|