Merge "RTP: Fix non-zero DC in EchoSuppressor caused while aggregating samples." into gingerbread
This commit is contained in:
@@ -768,7 +768,7 @@ bool AudioGroup::DeviceThread::threadLoop()
|
|||||||
LOGD("latency: output %d, input %d", track.latency(), record.latency());
|
LOGD("latency: output %d, input %d", track.latency(), record.latency());
|
||||||
|
|
||||||
// Initialize echo canceler.
|
// Initialize echo canceler.
|
||||||
EchoSuppressor echo(sampleRate, sampleCount, sampleCount * 2 +
|
EchoSuppressor echo(sampleCount,
|
||||||
(track.latency() + record.latency()) * sampleRate / 1000);
|
(track.latency() + record.latency()) * sampleRate / 1000);
|
||||||
|
|
||||||
// Give device socket a reasonable buffer size.
|
// Give device socket a reasonable buffer size.
|
||||||
|
|||||||
@@ -24,146 +24,163 @@
|
|||||||
|
|
||||||
#include "EchoSuppressor.h"
|
#include "EchoSuppressor.h"
|
||||||
|
|
||||||
EchoSuppressor::EchoSuppressor(int sampleRate, int sampleCount, int tailLength)
|
// It is very difficult to do echo cancellation at this level due to the lack of
|
||||||
|
// the timing information of the samples being played and recorded. Therefore,
|
||||||
|
// for the first release only echo suppression is implemented.
|
||||||
|
|
||||||
|
// The algorithm is derived from the "previous works" summarized in
|
||||||
|
// A new class of doubletalk detectors based on cross-correlation,
|
||||||
|
// J Benesty, DR Morgan, JH Cho, IEEE Trans. on Speech and Audio Processing.
|
||||||
|
// The method proposed in that paper is not used because of its high complexity.
|
||||||
|
|
||||||
|
// It is well known that cross-correlation can be computed using convolution,
|
||||||
|
// but unfortunately not every mobile processor has a (fast enough) FPU. Thus
|
||||||
|
// we use integer arithmetic as much as possible and do lots of bookkeeping.
|
||||||
|
// Again, parameters and thresholds are chosen by experiments.
|
||||||
|
|
||||||
|
EchoSuppressor::EchoSuppressor(int sampleCount, int tailLength)
|
||||||
{
|
{
|
||||||
int scale = 1;
|
tailLength += sampleCount * 4;
|
||||||
while (tailLength > 200 * scale) {
|
|
||||||
scale <<= 1;
|
int shift = 0;
|
||||||
}
|
while ((sampleCount >> shift) > 1 && (tailLength >> shift) > 256) {
|
||||||
if (scale > sampleCount) {
|
++shift;
|
||||||
scale = sampleCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mScale = scale;
|
mShift = shift + 4;
|
||||||
|
mScale = 1 << shift;
|
||||||
mSampleCount = sampleCount;
|
mSampleCount = sampleCount;
|
||||||
mWindowSize = sampleCount / scale;
|
mWindowSize = sampleCount >> shift;
|
||||||
mTailLength = (tailLength + scale - 1) / scale;
|
mTailLength = tailLength >> shift;
|
||||||
mRecordLength = (sampleRate + sampleCount - 1) / sampleCount;
|
mRecordLength = tailLength * 2 / sampleCount;
|
||||||
mRecordOffset = 0;
|
mRecordOffset = 0;
|
||||||
|
|
||||||
mXs = new float[mTailLength + mWindowSize];
|
mXs = new uint16_t[mTailLength + mWindowSize];
|
||||||
memset(mXs, 0, sizeof(float) * (mTailLength + mWindowSize));
|
memset(mXs, 0, sizeof(*mXs) * (mTailLength + mWindowSize));
|
||||||
mXYs = new float[mTailLength];
|
mXSums = new uint32_t[mTailLength];
|
||||||
memset(mXYs, 0, sizeof(float) * mTailLength);
|
memset(mXSums, 0, sizeof(*mXSums) * mTailLength);
|
||||||
mXXs = new float[mTailLength];
|
mX2Sums = new uint32_t[mTailLength];
|
||||||
memset(mXYs, 0, sizeof(float) * mTailLength);
|
memset(mX2Sums, 0, sizeof(*mX2Sums) * mTailLength);
|
||||||
mYY = 0;
|
mXRecords = new uint16_t[mRecordLength * mWindowSize];
|
||||||
|
memset(mXRecords, 0, sizeof(*mXRecords) * mRecordLength * mWindowSize);
|
||||||
|
|
||||||
mXYRecords = new float[mRecordLength * mTailLength];
|
mYSum = 0;
|
||||||
memset(mXYRecords, 0, sizeof(float) * mRecordLength * mTailLength);
|
mY2Sum = 0;
|
||||||
mXXRecords = new float[mRecordLength * mWindowSize];
|
mYRecords = new uint32_t[mRecordLength];
|
||||||
memset(mXXRecords, 0, sizeof(float) * mRecordLength * mWindowSize);
|
memset(mYRecords, 0, sizeof(*mYRecords) * mRecordLength);
|
||||||
mYYRecords = new float[mRecordLength];
|
mY2Records = new uint32_t[mRecordLength];
|
||||||
memset(mYYRecords, 0, sizeof(float) * mRecordLength);
|
memset(mY2Records, 0, sizeof(*mY2Records) * mRecordLength);
|
||||||
|
|
||||||
|
mXYSums = new uint32_t[mTailLength];
|
||||||
|
memset(mXYSums, 0, sizeof(*mXYSums) * mTailLength);
|
||||||
|
mXYRecords = new uint32_t[mRecordLength * mTailLength];
|
||||||
|
memset(mXYRecords, 0, sizeof(*mXYRecords) * mRecordLength * mTailLength);
|
||||||
|
|
||||||
mLastX = 0;
|
mLastX = 0;
|
||||||
mLastY = 0;
|
mLastY = 0;
|
||||||
|
mWeight = 1.0f / (mRecordLength * mWindowSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
EchoSuppressor::~EchoSuppressor()
|
EchoSuppressor::~EchoSuppressor()
|
||||||
{
|
{
|
||||||
delete [] mXs;
|
delete [] mXs;
|
||||||
delete [] mXYs;
|
delete [] mXSums;
|
||||||
delete [] mXXs;
|
delete [] mX2Sums;
|
||||||
|
delete [] mXRecords;
|
||||||
|
delete [] mYRecords;
|
||||||
|
delete [] mY2Records;
|
||||||
|
delete [] mXYSums;
|
||||||
delete [] mXYRecords;
|
delete [] mXYRecords;
|
||||||
delete [] mXXRecords;
|
|
||||||
delete [] mYYRecords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EchoSuppressor::run(int16_t *playbacked, int16_t *recorded)
|
void EchoSuppressor::run(int16_t *playbacked, int16_t *recorded)
|
||||||
{
|
{
|
||||||
float *records;
|
|
||||||
|
|
||||||
// Update Xs.
|
// Update Xs.
|
||||||
for (int i = 0; i < mTailLength; ++i) {
|
for (int i = mTailLength - 1; i >= 0; --i) {
|
||||||
mXs[i] = mXs[mWindowSize + i];
|
mXs[i + mWindowSize] = mXs[i];
|
||||||
}
|
}
|
||||||
for (int i = 0, j = 0; i < mWindowSize; ++i, j += mScale) {
|
for (int i = mWindowSize - 1, j = 0; i >= 0; --i, j += mScale) {
|
||||||
float sum = 0;
|
uint32_t sum = 0;
|
||||||
for (int k = 0; k < mScale; ++k) {
|
for (int k = 0; k < mScale; ++k) {
|
||||||
float x = playbacked[j + k] >> 8;
|
int32_t x = playbacked[j + k] << 15;
|
||||||
mLastX += x;
|
mLastX += x;
|
||||||
sum += (mLastX >= 0) ? mLastX : -mLastX;
|
sum += ((mLastX >= 0) ? mLastX : -mLastX) >> 15;
|
||||||
mLastX = 0.005f * mLastX - x;
|
mLastX -= (mLastX >> 10) + x;
|
||||||
}
|
}
|
||||||
mXs[mTailLength - 1 + i] = sum;
|
mXs[i] = sum >> mShift;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update XXs and XXRecords.
|
// Update XSums, X2Sums, and XRecords.
|
||||||
for (int i = 0; i < mTailLength - mWindowSize; ++i) {
|
for (int i = mTailLength - mWindowSize - 1; i >= 0; --i) {
|
||||||
mXXs[i] = mXXs[mWindowSize + i];
|
mXSums[i + mWindowSize] = mXSums[i];
|
||||||
|
mX2Sums[i + mWindowSize] = mX2Sums[i];
|
||||||
}
|
}
|
||||||
records = &mXXRecords[mRecordOffset * mWindowSize];
|
uint16_t *xRecords = &mXRecords[mRecordOffset * mWindowSize];
|
||||||
for (int i = 0, j = mTailLength - mWindowSize; i < mWindowSize; ++i, ++j) {
|
for (int i = mWindowSize - 1; i >= 0; --i) {
|
||||||
float xx = mXs[mTailLength - 1 + i] * mXs[mTailLength - 1 + i];
|
uint16_t x = mXs[i];
|
||||||
mXXs[j] = mXXs[j - 1] + xx - records[i];
|
mXSums[i] = mXSums[i + 1] + x - xRecords[i];
|
||||||
records[i] = xx;
|
mX2Sums[i] = mX2Sums[i + 1] + x * x - xRecords[i] * xRecords[i];
|
||||||
if (mXXs[j] < 0) {
|
xRecords[i] = x;
|
||||||
mXXs[j] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute Ys.
|
// Compute Ys.
|
||||||
float ys[mWindowSize];
|
uint16_t ys[mWindowSize];
|
||||||
for (int i = 0, j = 0; i < mWindowSize; ++i, j += mScale) {
|
for (int i = mWindowSize - 1, j = 0; i >= 0; --i, j += mScale) {
|
||||||
float sum = 0;
|
uint32_t sum = 0;
|
||||||
for (int k = 0; k < mScale; ++k) {
|
for (int k = 0; k < mScale; ++k) {
|
||||||
float y = recorded[j + k] >> 8;
|
int32_t y = recorded[j + k] << 15;
|
||||||
mLastY += y;
|
mLastY += y;
|
||||||
sum += (mLastY >= 0) ? mLastY : -mLastY;
|
sum += ((mLastY >= 0) ? mLastY : -mLastY) >> 15;
|
||||||
mLastY = 0.005f * mLastY - y;
|
mLastY -= (mLastY >> 10) + y;
|
||||||
}
|
}
|
||||||
ys[i] = sum;
|
ys[i] = sum >> mShift;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update YY and YYRecords.
|
// Update YSum, Y2Sum, YRecords, and Y2Records.
|
||||||
float yy = 0;
|
uint32_t ySum = 0;
|
||||||
for (int i = 0; i < mWindowSize; ++i) {
|
uint32_t y2Sum = 0;
|
||||||
yy += ys[i] * ys[i];
|
for (int i = mWindowSize - 1; i >= 0; --i) {
|
||||||
|
ySum += ys[i];
|
||||||
|
y2Sum += ys[i] * ys[i];
|
||||||
}
|
}
|
||||||
mYY += yy - mYYRecords[mRecordOffset];
|
mYSum += ySum - mYRecords[mRecordOffset];
|
||||||
mYYRecords[mRecordOffset] = yy;
|
mY2Sum += y2Sum - mY2Records[mRecordOffset];
|
||||||
if (mYY < 0) {
|
mYRecords[mRecordOffset] = ySum;
|
||||||
mYY = 0;
|
mY2Records[mRecordOffset] = y2Sum;
|
||||||
|
|
||||||
|
// Update XYSums and XYRecords.
|
||||||
|
uint32_t *xyRecords = &mXYRecords[mRecordOffset * mTailLength];
|
||||||
|
for (int i = mTailLength - 1; i >= 0; --i) {
|
||||||
|
uint32_t xySum = 0;
|
||||||
|
for (int j = mWindowSize - 1; j >= 0; --j) {
|
||||||
|
xySum += mXs[i + j] * ys[j];
|
||||||
|
}
|
||||||
|
mXYSums[i] += xySum - xyRecords[i];
|
||||||
|
xyRecords[i] = xySum;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update XYs and XYRecords.
|
// Compute correlations.
|
||||||
records = &mXYRecords[mRecordOffset * mTailLength];
|
float corr2 = 0.0f;
|
||||||
for (int i = 0; i < mTailLength; ++i) {
|
|
||||||
float xy = 0;
|
|
||||||
for (int j = 0;j < mWindowSize; ++j) {
|
|
||||||
xy += mXs[i + j] * ys[j];
|
|
||||||
}
|
|
||||||
mXYs[i] += xy - records[i];
|
|
||||||
records[i] = xy;
|
|
||||||
if (mXYs[i] < 0) {
|
|
||||||
mXYs[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Computes correlations from XYs, XXs, and YY.
|
|
||||||
float weight = 1.0f / (mYY + 1);
|
|
||||||
float correlation = 0;
|
|
||||||
int latency = 0;
|
int latency = 0;
|
||||||
for (int i = 0; i < mTailLength; ++i) {
|
float varY = mY2Sum - mWeight * mYSum * mYSum;
|
||||||
float c = mXYs[i] * mXYs[i] * weight / (mXXs[i] + 1);
|
for (int i = mTailLength - 1; i >= 0; --i) {
|
||||||
if (c > correlation) {
|
float varX = mX2Sums[i] - mWeight * mXSums[i] * mXSums[i];
|
||||||
correlation = c;
|
float cov = mXYSums[i] - mWeight * mXSums[i] * mYSum;
|
||||||
|
float c2 = cov * cov / (varX * varY + 1);
|
||||||
|
if (c2 > corr2) {
|
||||||
|
corr2 = c2;
|
||||||
latency = i;
|
latency = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//LOGI("correlation^2 = %.10f, latency = %d", corr2, latency * mScale);
|
||||||
|
|
||||||
correlation = sqrtf(correlation);
|
// Do echo suppression.
|
||||||
if (correlation > 0.3f) {
|
if (corr2 > 0.1f) {
|
||||||
float factor = 1.0f - correlation;
|
int factor = (corr2 > 1.0f) ? 0 : (1.0f - sqrtf(corr2)) * 4096;
|
||||||
factor *= factor;
|
|
||||||
factor /= 2.0; // suppress harder
|
|
||||||
for (int i = 0; i < mSampleCount; ++i) {
|
for (int i = 0; i < mSampleCount; ++i) {
|
||||||
recorded[i] *= factor;
|
recorded[i] = recorded[i] * factor >> 16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//LOGI("latency %5d, correlation %.10f", latency, correlation);
|
|
||||||
|
|
||||||
|
|
||||||
// Increase RecordOffset.
|
// Increase RecordOffset.
|
||||||
++mRecordOffset;
|
++mRecordOffset;
|
||||||
|
|||||||
@@ -23,11 +23,12 @@ class EchoSuppressor
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// The sampleCount must be power of 2.
|
// The sampleCount must be power of 2.
|
||||||
EchoSuppressor(int sampleRate, int sampleCount, int tailLength);
|
EchoSuppressor(int sampleCount, int tailLength);
|
||||||
~EchoSuppressor();
|
~EchoSuppressor();
|
||||||
void run(int16_t *playbacked, int16_t *recorded);
|
void run(int16_t *playbacked, int16_t *recorded);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int mShift;
|
||||||
int mScale;
|
int mScale;
|
||||||
int mSampleCount;
|
int mSampleCount;
|
||||||
int mWindowSize;
|
int mWindowSize;
|
||||||
@@ -35,17 +36,23 @@ private:
|
|||||||
int mRecordLength;
|
int mRecordLength;
|
||||||
int mRecordOffset;
|
int mRecordOffset;
|
||||||
|
|
||||||
float *mXs;
|
uint16_t *mXs;
|
||||||
float *mXYs;
|
uint32_t *mXSums;
|
||||||
float *mXXs;
|
uint32_t *mX2Sums;
|
||||||
float mYY;
|
uint16_t *mXRecords;
|
||||||
|
|
||||||
float *mXYRecords;
|
uint32_t mYSum;
|
||||||
float *mXXRecords;
|
uint32_t mY2Sum;
|
||||||
float *mYYRecords;
|
uint32_t *mYRecords;
|
||||||
|
uint32_t *mY2Records;
|
||||||
|
|
||||||
float mLastX;
|
uint32_t *mXYSums;
|
||||||
float mLastY;
|
uint32_t *mXYRecords;
|
||||||
|
|
||||||
|
int32_t mLastX;
|
||||||
|
int32_t mLastY;
|
||||||
|
|
||||||
|
float mWeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user