Skip to content
Snippets Groups Projects
Commit 65088111 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

#5915: use only one input signal for gain control (removed output buffer)

parent 1ad7e284
No related branches found
No related tags found
No related merge requests found
......@@ -123,7 +123,7 @@ AudioRtpRecord::~AudioRtpRecord()
}
AudioRtpRecordHandler::AudioRtpRecordHandler (SIPCall *ca) : _audioRtpRecord (), _ca (ca), echoCanceller(ca->getMemoryPool())
AudioRtpRecordHandler::AudioRtpRecordHandler (SIPCall *ca) : _audioRtpRecord (), _ca (ca), echoCanceller(ca->getMemoryPool()), gainController(8000, 0.0)
{
}
......@@ -328,7 +328,6 @@ int AudioRtpRecordHandler::processDataEncode (void)
// echoCanceller.process(micData, micDataEchoCancelled, nbSample * sizeof(SFLDataFormat));
// echoCanceller.process(micData, micDataEchoCancelled, nbSample * sizeof(SFLDataFormat));
if(Manager::instance().getEchoCancelState() == "enabled") {
_debug("EchoCancel: -------------------------- getData");
echoCanceller.getData(micData);
}
......@@ -372,12 +371,13 @@ void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned
_audioRtpRecord._spkrFadeInComplete = fadeIn (spkrDataDecoded, nbSample, &_audioRtpRecord._micAmplFactor);
}
gainController.process(spkrDataDecoded, nbSample);
// test if resampling is required
if (codecSampleRate != mainBufferSampleRate) {
// Do sample rate conversion
int nbSampleDown = nbSample;
nbSample = _audioRtpRecord._converter->upsampleData (spkrDataDecoded, spkrDataConverted, codecSampleRate, mainBufferSampleRate, nbSampleDown);
if(Manager::instance().getEchoCancelState() == "enabled") {
......@@ -390,8 +390,7 @@ void AudioRtpRecordHandler::processDataDecode (unsigned char *spkrData, unsigned
} else {
if(Manager::instance().getEchoCancelState() == "enabled") {
_debug("EchoCancel: ------------------------ Put Data");
echoCanceller.putData(spkrDataDecoded, expandedSize);
echoCanceller.putData(spkrDataDecoded, expandedSize);
}
// put data in audio layer, size in byte
Manager::instance().getMainBuffer()->putData (spkrDataDecoded, expandedSize, 100, _ca->getCallId());
......
......@@ -37,6 +37,7 @@
#include "audio/noisesuppress.h"
#include "audio/speexechocancel.h"
#include "audio/echosuppress.h"
#include "audio/gaincontrol.h"
#include "managerimpl.h"
#include <ccrtp/rtp.h>
......@@ -67,9 +68,9 @@ timeval2microtimeout (const timeval& t)
class AudioRtpSessionException: public std::exception
{
virtual const char* what() const throw() {
return "AudioRtpSessionException occured";
}
virtual const char* what() const throw() {
return "AudioRtpSessionException occured";
}
};
typedef struct DtmfEvent {
......@@ -115,7 +116,7 @@ class AudioRtpRecord
NoiseSuppress *_noiseSuppress;
ost::Mutex audioProcessMutex;
std::string _callId;
unsigned int _dtmfPayloadType;
unsigned int _dtmfPayloadType;
};
......@@ -228,7 +229,9 @@ class AudioRtpRecordHandler
SIPCall *_ca;
EchoSuppress echoCanceller;
EchoSuppress echoCanceller;
GainControl gainController;
};
}
......
......@@ -11,26 +11,47 @@
#define SFL_GAIN_LIMITER_RATIO 0.1
#define SFL_GAIN_LIMITER_THRESHOLD 0.6
GainControl::GainControl(double sr) : averager(sr, SFL_GAIN_ATTACK_RELEASE_TIME)
, limiter(SFL_GAIN_LIMITER_RATIO, SFL_GAIN_LIMITER_THRESHOLD) {}
#define SFL_GAIN_LOGe10 2.30258509299404568402
GainControl::GainControl(double sr, double target) : averager(sr, SFL_GAIN_ATTACK_RELEASE_TIME)
, limiter(SFL_GAIN_LIMITER_RATIO, SFL_GAIN_LIMITER_THRESHOLD)
, targetGaindB(target)
, targetGainLinear(0.0)
{
targetGainLinear = exp(targetGaindB * 0.05 * SFL_GAIN_LOGe10);
_debug("GainControl: Target gain %d dB (%d linear)", targetGaindB, targetGainLinear);
}
GainControl::~GainControl() {}
std::fstream tmpRms("testrms.raw", std::fstream::out);
#ifdef DUMP_GAIN_CONTROL_SIGNAL
std::fstream tmpRms("gaintestrms.raw", std::fstream::out);
std::fstream tmpIn("gaintestin.raw", std::fstream::out);
std::fstream tmpOut("gaintestout.raw", std::fstream::out);
#endif
void GainControl::process(SFLDataFormat *inBuf, SFLDataFormat *outBuf, int bufLength)
void GainControl::process(SFLDataFormat *buf, int bufLength)
{
double rms, rmsAvg, in, out;
for(int i = 0; i < bufLength; i++) {
in = (double)inBuf[i] / (double)SHRT_MAX;
in = (double)buf[i] / (double)SHRT_MAX;
rms = detector.getRms(in);
rmsAvg = sqrt(averager.getAverage(rms));
#ifdef DUMP_GAIN_CONTROL_SIGNAL
tmpRms.write(reinterpret_cast<char *>(&rmsAvg), sizeof(double));
tmpIn.write(reinterpret_cast<char *>(&in), sizeof(double));
#endif
out = limiter.limit(in);
outBuf[i] = (short)(out * (double)SHRT_MAX);
#ifdef DUMP_GAIN_CONTROL_SIGNAL
tmpRms.write(retinterpret_cast<char *>(&out), sizeof(double));
#endif
buf[i] = (short)(out * (double)SHRT_MAX);
}
}
......
......@@ -8,13 +8,30 @@
class GainControl {
public:
GainControl(double);
/**
* Constructor for the gain controller
* /param Sampling rate
* /param Target gain in dB
*/
GainControl(double, double);
/**
* Destructor for this class
*/
~GainControl(void);
void process(SFLDataFormat *, SFLDataFormat *, int);
/**
* Apply addaptive gain factor on input signal
* /param Input audio buffer
* /param Input buffer length
*/
void process(SFLDataFormat *, int);
private:
/**
* Rms detector
*/
class RmsDetection {
public:
/**
......@@ -24,6 +41,7 @@ private:
/**
* Get rms value
* /param Audio sample
*/
double getRms(double);
......@@ -65,8 +83,16 @@ private:
class Limiter {
public:
/**
* Limiter
* /param Threshold
* /param Ratio
*/
Limiter(double, double);
/**
* Perform compression on input signal
*/
double limit(double);
private:
......@@ -79,6 +105,10 @@ private:
DetectionAverage averager;
Limiter limiter;
double targetGaindB;
double targetGainLinear;
};
#endif // GAINCONTROL_H
......@@ -38,8 +38,7 @@ using namespace std;
void GainControlTest::testGainProcessing()
{
int fileSize;
SFLDataFormat inbuf[SFL_GAIN_BUFFER_LENGTH];
SFLDataFormat outbuf[SFL_GAIN_BUFFER_LENGTH];
SFLDataFormat buf[SFL_GAIN_BUFFER_LENGTH];
GainControl gcontrol(8000);
......@@ -54,7 +53,7 @@ void GainControlTest::testGainProcessing()
while(fileSize > 0) {
inputFile.read(reinterpret_cast<char *>(inbuf), BUFFER_LENGTH * sizeof(SFLDataFormat));
gcontrol.process(inbuf, outbuf, BUFFER_LENGTH);
gcontrol.process(buf, BUFFER_LENGTH);
outputFile.write(reinterpret_cast<char *>(outbuf), BUFFER_LENGTH * sizeof(SFLDataFormat));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment