pure sdk for main

This commit is contained in:
divadiow
2025-08-27 09:51:58 +01:00
parent f0d033f1c9
commit 0571416e7c
3283 changed files with 1577720 additions and 1 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,402 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Performs echo control (suppression) with fft routines in fixed-point.
#ifndef MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
#define MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
#include "rtc_base/checks.h"
#include <stdint.h>
#include "ring_buffer.h"
#include "signal_processing_library.h"
#include "aecm_defines.h"
struct RealFFT;
#define ALIGN8_BEG
#define ALIGN8_END __attribute__((aligned(8)))
typedef struct {
int16_t real;
int16_t imag;
} ComplexInt16;
typedef struct {
int farBufWritePos;
int farBufReadPos;
int knownDelay;
int lastKnownDelay;
int firstVAD; // Parameter to control poorly initialized channels
RingBuffer *farFrameBuf;
RingBuffer *nearNoisyFrameBuf;
RingBuffer *nearCleanFrameBuf;
RingBuffer *outFrameBuf;
int16_t farBuf[FAR_BUF_LEN];
int16_t mult;
uint32_t seed;
// Delay estimation variables
void *delay_estimator_farend;
void *delay_estimator;
uint16_t currentDelay;
// Far end history variables
// TODO(bjornv): Replace |far_history| with ring_buffer.
uint16_t far_history[PART_LEN1 * MAX_DELAY];
int far_history_pos;
int far_q_domains[MAX_DELAY];
int16_t nlpFlag;
int16_t fixedDelay;
uint32_t totCount;
int16_t dfaCleanQDomain;
int16_t dfaCleanQDomainOld;
int16_t dfaNoisyQDomain;
int16_t dfaNoisyQDomainOld;
int16_t nearLogEnergy[MAX_BUF_LEN];
int16_t farLogEnergy;
int16_t echoAdaptLogEnergy[MAX_BUF_LEN];
int16_t echoStoredLogEnergy[MAX_BUF_LEN];
// The extra 16 or 32 bytes in the following buffers are for alignment based
// Neon code.
// It's designed this way since the current GCC compiler can't align a
// buffer in 16 or 32 byte boundaries properly.
int16_t channelStored_buf[PART_LEN1 + 8];
int16_t channelAdapt16_buf[PART_LEN1 + 8];
int32_t channelAdapt32_buf[PART_LEN1 + 8];
int16_t xBuf_buf[PART_LEN2 + 16]; // farend
int16_t dBufClean_buf[PART_LEN2 + 16]; // nearend
int16_t dBufNoisy_buf[PART_LEN2 + 16]; // nearend
int16_t outBuf_buf[PART_LEN + 8];
// Pointers to the above buffers
int16_t *channelStored;
int16_t *channelAdapt16;
int32_t *channelAdapt32;
int16_t *xBuf;
int16_t *dBufClean;
int16_t *dBufNoisy;
int16_t *outBuf;
int32_t echoFilt[PART_LEN1];
int16_t nearFilt[PART_LEN1];
int32_t noiseEst[PART_LEN1];
int noiseEstTooLowCtr[PART_LEN1];
int noiseEstTooHighCtr[PART_LEN1];
int16_t noiseEstCtr;
int16_t cngMode;
int32_t mseAdaptOld;
int32_t mseStoredOld;
int32_t mseThreshold;
int16_t farEnergyMin;
int16_t farEnergyMax;
int16_t farEnergyMaxMin;
int16_t farEnergyVAD;
int16_t farEnergyMSE;
int currentVADValue;
int16_t vadUpdateCount;
int16_t startupState;
int16_t mseChannelCount;
int16_t supGain;
int16_t supGainOld;
int16_t supGainErrParamA;
int16_t supGainErrParamD;
int16_t supGainErrParamDiffAB;
int16_t supGainErrParamDiffBD;
struct RealFFT *real_fft;
#ifdef AEC_DEBUG
FILE* farFile;
FILE* nearFile;
FILE* outFile;
#endif
} AecmCore;
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_CreateCore()
//
// Allocates the memory needed by the AECM. The memory needs to be
// initialized separately using the WebRtcAecm_InitCore() function.
// Returns a pointer to the instance and a nullptr at failure.
AecmCore *WebRtcAecm_CreateCore();
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_InitCore(...)
//
// This function initializes the AECM instant created with
// WebRtcAecm_CreateCore()
// Input:
// - aecm : Pointer to the AECM instance
// - samplingFreq : Sampling Frequency
//
// Output:
// - aecm : Initialized instance
//
// Return value : 0 - Ok
// -1 - Error
//
int WebRtcAecm_InitCore(AecmCore *const aecm, int samplingFreq);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_FreeCore(...)
//
// This function releases the memory allocated by WebRtcAecm_CreateCore()
// Input:
// - aecm : Pointer to the AECM instance
//
void WebRtcAecm_FreeCore(AecmCore *aecm);
int WebRtcAecm_Control(AecmCore *aecm, int delay, int nlpFlag);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_InitEchoPathCore(...)
//
// This function resets the echo channel adaptation with the specified channel.
// Input:
// - aecm : Pointer to the AECM instance
// - echo_path : Pointer to the data that should initialize the echo
// path
//
// Output:
// - aecm : Initialized instance
//
void WebRtcAecm_InitEchoPathCore(AecmCore *aecm, const int16_t *echo_path);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_ProcessFrame(...)
//
// This function processes frames and sends blocks to
// WebRtcAecm_ProcessBlock(...)
//
// Inputs:
// - aecm : Pointer to the AECM instance
// - farend : In buffer containing one frame of echo signal
// - nearendNoisy : In buffer containing one frame of nearend+echo signal
// without NS
// - nearendClean : In buffer containing one frame of nearend+echo signal
// with NS
//
// Output:
// - out : Out buffer, one frame of nearend signal :
//
//
int WebRtcAecm_ProcessFrame(AecmCore *aecm,
const int16_t *farend,
const int16_t *nearendNoisy,
const int16_t *nearendClean,
int16_t *out);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_ProcessBlock(...)
//
// This function is called for every block within one frame
// This function is called by WebRtcAecm_ProcessFrame(...)
//
// Inputs:
// - aecm : Pointer to the AECM instance
// - farend : In buffer containing one block of echo signal
// - nearendNoisy : In buffer containing one frame of nearend+echo signal
// without NS
// - nearendClean : In buffer containing one frame of nearend+echo signal
// with NS
//
// Output:
// - out : Out buffer, one block of nearend signal :
//
//
int WebRtcAecm_ProcessBlock(AecmCore *aecm,
const int16_t *farend,
const int16_t *nearendNoisy,
const int16_t *noisyClean,
int16_t *out);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_BufferFarFrame()
//
// Inserts a frame of data into farend buffer.
//
// Inputs:
// - aecm : Pointer to the AECM instance
// - farend : In buffer containing one frame of farend signal
// - farLen : Length of frame
//
void WebRtcAecm_BufferFarFrame(AecmCore *const aecm,
const int16_t *const farend,
const int farLen);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_FetchFarFrame()
//
// Read the farend buffer to account for known delay
//
// Inputs:
// - aecm : Pointer to the AECM instance
// - farend : In buffer containing one frame of farend signal
// - farLen : Length of frame
// - knownDelay : known delay
//
void WebRtcAecm_FetchFarFrame(AecmCore *const aecm,
int16_t *const farend,
const int farLen,
const int knownDelay);
// All the functions below are intended to be private
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_UpdateFarHistory()
//
// Moves the pointer to the next entry and inserts |far_spectrum| and
// corresponding Q-domain in its buffer.
//
// Inputs:
// - self : Pointer to the delay estimation instance
// - far_spectrum : Pointer to the far end spectrum
// - far_q : Q-domain of far end spectrum
//
void WebRtcAecm_UpdateFarHistory(AecmCore *self,
uint16_t *far_spectrum,
int far_q);
////////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_AlignedFarend()
//
// Returns a pointer to the far end spectrum aligned to current near end
// spectrum. The function WebRtc_DelayEstimatorProcessFix(...) should have been
// called before AlignedFarend(...). Otherwise, you get the pointer to the
// previous frame. The memory is only valid until the next call of
// WebRtc_DelayEstimatorProcessFix(...).
//
// Inputs:
// - self : Pointer to the AECM instance.
// - delay : Current delay estimate.
//
// Output:
// - far_q : The Q-domain of the aligned far end spectrum
//
// Return value:
// - far_spectrum : Pointer to the aligned far end spectrum
// NULL - Error
//
const uint16_t *WebRtcAecm_AlignedFarend(AecmCore *self, int *far_q, int delay);
///////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_CalcSuppressionGain()
//
// This function calculates the suppression gain that is used in the
// Wiener filter.
//
// Inputs:
// - aecm : Pointer to the AECM instance.
//
// Return value:
// - supGain : Suppression gain with which to scale the noise
// level (Q14).
//
int16_t WebRtcAecm_CalcSuppressionGain(AecmCore *const aecm);
///////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_CalcEnergies()
//
// This function calculates the log of energies for nearend, farend and
// estimated echoes. There is also an update of energy decision levels,
// i.e. internal VAD.
//
// Inputs:
// - aecm : Pointer to the AECM instance.
// - far_spectrum : Pointer to farend spectrum.
// - far_q : Q-domain of farend spectrum.
// - nearEner : Near end energy for current block in
// Q(aecm->dfaQDomain).
//
// Output:
// - echoEst : Estimated echo in Q(xfa_q+RESOLUTION_CHANNEL16).
//
void WebRtcAecm_CalcEnergies(AecmCore *aecm,
const uint16_t *far_spectrum,
const int16_t far_q,
const uint32_t nearEner,
int32_t *echoEst);
///////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_CalcStepSize()
//
// This function calculates the step size used in channel estimation
//
// Inputs:
// - aecm : Pointer to the AECM instance.
//
// Return value:
// - mu : Stepsize in log2(), i.e. number of shifts.
//
int16_t WebRtcAecm_CalcStepSize(AecmCore *const aecm);
///////////////////////////////////////////////////////////////////////////////
// WebRtcAecm_UpdateChannel(...)
//
// This function performs channel estimation.
// NLMS and decision on channel storage.
//
// Inputs:
// - aecm : Pointer to the AECM instance.
// - far_spectrum : Absolute value of the farend signal in Q(far_q)
// - far_q : Q-domain of the farend signal
// - dfa : Absolute value of the nearend signal
// (Q[aecm->dfaQDomain])
// - mu : NLMS step size.
// Input/Output:
// - echoEst : Estimated echo in Q(far_q+RESOLUTION_CHANNEL16).
//
void WebRtcAecm_UpdateChannel(AecmCore *aecm,
const uint16_t *far_spectrum,
const int16_t far_q,
const uint16_t *const dfa,
const int16_t mu,
int32_t *echoEst);
extern const int16_t WebRtcAecm_kCosTable[];
extern const int16_t WebRtcAecm_kSinTable[];
///////////////////////////////////////////////////////////////////////////////
// Some function pointers, for internal functions shared by ARM NEON and
// generic C code.
//
typedef void (*CalcLinearEnergies)(AecmCore *aecm,
const uint16_t *far_spectrum,
int32_t *echoEst,
uint32_t *far_energy,
uint32_t *echo_energy_adapt,
uint32_t *echo_energy_stored);
extern CalcLinearEnergies WebRtcAecm_CalcLinearEnergies;
typedef void (*StoreAdaptiveChannel)(AecmCore *aecm,
const uint16_t *far_spectrum,
int32_t *echo_est);
extern StoreAdaptiveChannel WebRtcAecm_StoreAdaptiveChannel;
typedef void (*ResetAdaptiveChannel)(AecmCore *aecm);
extern ResetAdaptiveChannel WebRtcAecm_ResetAdaptiveChannel;
#endif
@@ -0,0 +1,662 @@
/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <stddef.h>
#include "aecm_core.h"
#include "real_fft.h"
#include "echo_control_mobile.h"
#include "delay_estimator_wrapper.h"
// Square root of Hanning window in Q14.
static const ALIGN8_BEG int16_t
WebRtcAecm_kSqrtHanning[]
ALIGN8_END = {
0, 399, 798, 1196, 1594, 1990, 2386, 2780, 3172, 3562, 3951,
4337, 4720, 5101, 5478, 5853, 6224, 6591, 6954, 7313, 7668, 8019,
8364, 8705, 9040, 9370, 9695, 10013, 10326, 10633, 10933, 11227, 11514,
11795, 12068, 12335, 12594, 12845, 13089, 13325, 13553, 13773, 13985, 14189,
14384, 14571, 14749, 14918, 15079, 15231, 15373, 15506, 15631, 15746, 15851,
15947, 16034, 16111, 16179, 16237, 16286, 16325, 16354, 16373, 16384};
#ifdef AECM_WITH_ABS_APPROX
// Q15 alpha = 0.99439986968132 const Factor for magnitude approximation
static const uint16_t kAlpha1 = 32584;
// Q15 beta = 0.12967166976970 const Factor for magnitude approximation
static const uint16_t kBeta1 = 4249;
// Q15 alpha = 0.94234827210087 const Factor for magnitude approximation
static const uint16_t kAlpha2 = 30879;
// Q15 beta = 0.33787806009150 const Factor for magnitude approximation
static const uint16_t kBeta2 = 11072;
// Q15 alpha = 0.82247698684306 const Factor for magnitude approximation
static const uint16_t kAlpha3 = 26951;
// Q15 beta = 0.57762063060713 const Factor for magnitude approximation
static const uint16_t kBeta3 = 18927;
#endif
static const int16_t kNoiseEstQDomain = 15;
static const int16_t kNoiseEstIncCount = 5;
static void ComfortNoise(AecmCore *aecm,
const uint16_t *dfa,
ComplexInt16 *out,
const int16_t *lambda) {
int16_t i;
int16_t tmp16;
int32_t tmp32;
int16_t randW16[PART_LEN];
int16_t uReal[PART_LEN1];
int16_t uImag[PART_LEN1];
int32_t outLShift32;
int16_t noiseRShift16[PART_LEN1];
int16_t shiftFromNearToNoise = kNoiseEstQDomain - aecm->dfaCleanQDomain;
int16_t minTrackShift;
RTC_DCHECK_GE(shiftFromNearToNoise, 0);
RTC_DCHECK_LT(shiftFromNearToNoise, 16);
if (aecm->noiseEstCtr < 100) {
// Track the minimum more quickly initially.
aecm->noiseEstCtr++;
minTrackShift = 6;
} else {
minTrackShift = 9;
}
// Estimate noise power.
for (i = 0; i < PART_LEN1; i++) {
// Shift to the noise domain.
tmp32 = (int32_t) dfa[i];
outLShift32 = tmp32 << shiftFromNearToNoise;
if (outLShift32 < aecm->noiseEst[i]) {
// Reset "too low" counter
aecm->noiseEstTooLowCtr[i] = 0;
// Track the minimum.
if (aecm->noiseEst[i] < (1 << minTrackShift)) {
// For small values, decrease noiseEst[i] every
// |kNoiseEstIncCount| block. The regular approach below can not
// go further down due to truncation.
aecm->noiseEstTooHighCtr[i]++;
if (aecm->noiseEstTooHighCtr[i] >= kNoiseEstIncCount) {
aecm->noiseEst[i]--;
aecm->noiseEstTooHighCtr[i] = 0; // Reset the counter
}
} else {
aecm->noiseEst[i] -=
((aecm->noiseEst[i] - outLShift32) >> minTrackShift);
}
} else {
// Reset "too high" counter
aecm->noiseEstTooHighCtr[i] = 0;
// Ramp slowly upwards until we hit the minimum again.
if ((aecm->noiseEst[i] >> 19) > 0) {
// Avoid overflow.
// Multiplication with 2049 will cause wrap around. Scale
// down first and then multiply
aecm->noiseEst[i] >>= 11;
aecm->noiseEst[i] *= 2049;
} else if ((aecm->noiseEst[i] >> 11) > 0) {
// Large enough for relative increase
aecm->noiseEst[i] *= 2049;
aecm->noiseEst[i] >>= 11;
} else {
// Make incremental increases based on size every
// |kNoiseEstIncCount| block
aecm->noiseEstTooLowCtr[i]++;
if (aecm->noiseEstTooLowCtr[i] >= kNoiseEstIncCount) {
aecm->noiseEst[i] += (aecm->noiseEst[i] >> 9) + 1;
aecm->noiseEstTooLowCtr[i] = 0; // Reset counter
}
}
}
}
for (i = 0; i < PART_LEN1; i++) {
tmp32 = aecm->noiseEst[i] >> shiftFromNearToNoise;
if (tmp32 > 32767) {
tmp32 = 32767;
aecm->noiseEst[i] = tmp32 << shiftFromNearToNoise;
}
noiseRShift16[i] = (int16_t)
tmp32;
tmp16 = ONE_Q14 - lambda[i];
noiseRShift16[i] = (int16_t) ((tmp16 * noiseRShift16[i]) >> 14);
}
// Generate a uniform random array on [0 2^15-1].
WebRtcSpl_RandUArray(randW16, PART_LEN, &aecm->seed);
// Generate noise according to estimated energy.
uReal[0] = 0; // Reject LF noise.
uImag[0] = 0;
for (i = 1; i < PART_LEN1; i++) {
// Get a random index for the cos and sin tables over [0 359].
tmp16 = (int16_t) ((359 * randW16[i - 1]) >> 15);
// Tables are in Q13.
uReal[i] =
(int16_t) ((noiseRShift16[i] * WebRtcAecm_kCosTable[tmp16]) >> 13);
uImag[i] =
(int16_t) ((-noiseRShift16[i] * WebRtcAecm_kSinTable[tmp16]) >> 13);
}
uImag[PART_LEN] = 0;
for (i = 0; i < PART_LEN1; i++) {
out[i].real = WebRtcSpl_AddSatW16(out[i].real, uReal[i]);
out[i].imag = WebRtcSpl_AddSatW16(out[i].imag, uImag[i]);
}
}
static void WindowAndFFT(AecmCore *aecm,
int16_t *fft,
const int16_t *time_signal,
ComplexInt16 *freq_signal,
int time_signal_scaling) {
int i = 0;
// FFT of signal
for (i = 0; i < PART_LEN; i++) {
// Window time domain signal and insert into real part of
// transformation array |fft|
int16_t scaled_time_signal = time_signal[i] * (1 << time_signal_scaling);
fft[i] = (int16_t) ((scaled_time_signal * WebRtcAecm_kSqrtHanning[i]) >> 14);
scaled_time_signal = time_signal[i + PART_LEN] * (1 << time_signal_scaling);
fft[PART_LEN + i] = (int16_t) (
(scaled_time_signal * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14);
}
// Do forward FFT, then take only the first PART_LEN complex samples,
// and change signs of the imaginary parts.
WebRtcSpl_RealForwardFFT(aecm->real_fft, fft, (int16_t *)
freq_signal);
for (i = 0; i < PART_LEN; i++) {
freq_signal[i].imag = -freq_signal[i].imag;
}
}
static void InverseFFTAndWindow(AecmCore *aecm,
int16_t *fft,
ComplexInt16 *efw,
int16_t *output,
const int16_t *nearendClean) {
int i, j, outCFFT;
int32_t tmp32no1;
// Reuse |efw| for the inverse FFT output after transferring
// the contents to |fft|.
int16_t *ifft_out = (int16_t *)
efw;
// Synthesis
for (i = 1, j = 2; i < PART_LEN; i += 1, j += 2) {
fft[j] = efw[i].real;
fft[j + 1] = -efw[i].imag;
}
fft[0] = efw[0].real;
fft[1] = -efw[0].imag;
fft[PART_LEN2] = efw[PART_LEN].real;
fft[PART_LEN2 + 1] = -efw[PART_LEN].imag;
// Inverse FFT. Keep outCFFT to scale the samples in the next block.
outCFFT = WebRtcSpl_RealInverseFFT(aecm->real_fft, fft, ifft_out);
for (i = 0; i < PART_LEN; i++) {
ifft_out[i] = (int16_t)
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(
ifft_out[i], WebRtcAecm_kSqrtHanning[i], 14);
tmp32no1 = WEBRTC_SPL_SHIFT_W32((int32_t) ifft_out[i],
outCFFT - aecm->dfaCleanQDomain);
output[i] = (int16_t)
WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX,
tmp32no1 + aecm->outBuf[i],
WEBRTC_SPL_WORD16_MIN);
tmp32no1 =
(ifft_out[PART_LEN + i] * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14;
tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1, outCFFT - aecm->dfaCleanQDomain);
aecm->outBuf[i] = (int16_t)
WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX, tmp32no1,
WEBRTC_SPL_WORD16_MIN);
}
// Copy the current block to the old position
// (aecm->outBuf is shifted elsewhere)
memcpy(aecm->xBuf, aecm->xBuf + PART_LEN, sizeof(int16_t) * PART_LEN);
memcpy(aecm->dBufNoisy, aecm->dBufNoisy + PART_LEN,
sizeof(int16_t) * PART_LEN);
if (nearendClean != NULL) {
memcpy(aecm->dBufClean, aecm->dBufClean + PART_LEN,
sizeof(int16_t) * PART_LEN);
}
}
// Transforms a time domain signal into the frequency domain, outputting the
// complex valued signal, absolute value and sum of absolute values.
//
// time_signal [in] Pointer to time domain signal
// freq_signal_real [out] Pointer to real part of frequency domain array
// freq_signal_imag [out] Pointer to imaginary part of frequency domain
// array
// freq_signal_abs [out] Pointer to absolute value of frequency domain
// array
// freq_signal_sum_abs [out] Pointer to the sum of all absolute values in
// the frequency domain array
// return value The Q-domain of current frequency values
//
static int TimeToFrequencyDomain(AecmCore *aecm,
const int16_t *time_signal,
ComplexInt16 *freq_signal,
uint16_t *freq_signal_abs,
uint32_t *freq_signal_sum_abs) {
int i = 0;
int time_signal_scaling = 0;
int32_t tmp32no1 = 0;
int32_t tmp32no2 = 0;
// In fft_buf, +16 for 32-byte alignment.
int16_t fft_buf[PART_LEN4 + 16];
int16_t *fft = (int16_t *) (((uintptr_t) fft_buf + 31) & ~31);
int16_t tmp16no1;
int16_t tmp16no2;
WindowAndFFT(aecm, fft, time_signal, freq_signal, time_signal_scaling);
// Extract imaginary and real part, calculate the magnitude for
// all frequency bins
freq_signal[0].imag = 0;
freq_signal[PART_LEN].imag = 0;
freq_signal_abs[0] = (uint16_t) WEBRTC_SPL_ABS_W16(freq_signal[0].real);
freq_signal_abs[PART_LEN] =
(uint16_t) WEBRTC_SPL_ABS_W16(freq_signal[PART_LEN].real);
(*freq_signal_sum_abs) =
(uint32_t) (freq_signal_abs[0]) + (uint32_t) (freq_signal_abs[PART_LEN]);
for (i = 1; i < PART_LEN; i++) {
if (freq_signal[i].real == 0) {
freq_signal_abs[i] = (uint16_t) WEBRTC_SPL_ABS_W16(freq_signal[i].imag);
} else if (freq_signal[i].imag == 0) {
freq_signal_abs[i] = (uint16_t) WEBRTC_SPL_ABS_W16(freq_signal[i].real);
} else {
// Approximation for magnitude of complex fft output
// magn = sqrt(real^2 + imag^2)
// magn ~= alpha * max(|imag|,|real|) + beta * min(|imag|,|real|)
//
// The parameters alpha and beta are stored in Q15
tmp16no1 = WEBRTC_SPL_ABS_W16(freq_signal[i].real);
tmp16no2 = WEBRTC_SPL_ABS_W16(freq_signal[i].imag);
tmp32no1 = tmp16no1 * tmp16no1;
tmp32no2 = tmp16no2 * tmp16no2;
tmp32no2 = WebRtcSpl_AddSatW32(tmp32no1, tmp32no2);
tmp32no1 = WebRtcSpl_SqrtFloor(tmp32no2);
freq_signal_abs[i] = (uint16_t) tmp32no1;
}
(*freq_signal_sum_abs) += (uint32_t) freq_signal_abs[i];
}
return time_signal_scaling;
}
// bugs.webrtc.org/8200
int WebRtcAecm_ProcessBlock(AecmCore *aecm, const int16_t *farend, const int16_t *nearendNoisy,
const int16_t *nearendClean, int16_t *output) {
int i;
uint32_t xfaSum;
uint32_t dfaNoisySum;
uint32_t dfaCleanSum;
uint32_t echoEst32Gained;
uint32_t tmpU32;
int32_t tmp32no1;
uint16_t xfa[PART_LEN1];
uint16_t dfaNoisy[PART_LEN1];
uint16_t dfaClean[PART_LEN1];
uint16_t *ptrDfaClean = dfaClean;
const uint16_t *far_spectrum_ptr = NULL;
// 32 byte aligned buffers (with +8 or +16).
// TODO(kma): define fft with ComplexInt16.
int16_t fft_buf[PART_LEN4 + 2 + 16]; // +2 to make a loop safe.
int32_t echoEst32_buf[PART_LEN1 + 8];
int32_t dfw_buf[PART_LEN2 + 8];
int32_t efw_buf[PART_LEN2 + 8];
int16_t *fft = (int16_t *) (((uintptr_t) fft_buf + 31) & ~31);
int32_t *echoEst32 = (int32_t *) (((uintptr_t) echoEst32_buf + 31) & ~31);
ComplexInt16 *dfw = (ComplexInt16 *) (((uintptr_t) dfw_buf + 31) & ~31);
ComplexInt16 *efw = (ComplexInt16 *) (((uintptr_t) efw_buf + 31) & ~31);
int16_t hnl[PART_LEN1];
int16_t numPosCoef = 0;
int16_t nlpGain = ONE_Q14;
int delay;
int16_t tmp16no1;
int16_t tmp16no2;
int16_t mu;
int16_t supGain;
int16_t zeros32, zeros16;
int16_t zerosDBufNoisy, zerosDBufClean, zerosXBuf;
int far_q;
int16_t resolutionDiff, qDomainDiff, dfa_clean_q_domain_diff;
const int kMinPrefBand = 4;
const int kMaxPrefBand = 24;
int32_t avgHnl32 = 0;
// Determine startup state. There are three states:
// (0) the first CONV_LEN blocks
// (1) another CONV_LEN blocks
// (2) the rest
if (aecm->startupState < 2) {
aecm->
startupState =
(aecm->totCount >= CONV_LEN) + (aecm->totCount >= CONV_LEN2);
}
// END: Determine startup state
// Buffer near and far end signals
memcpy(aecm
->xBuf + PART_LEN, farend, sizeof(int16_t) * PART_LEN);
memcpy(aecm
->dBufNoisy + PART_LEN, nearendNoisy, sizeof(int16_t) * PART_LEN);
if (nearendClean != NULL) {
memcpy(aecm
->dBufClean + PART_LEN, nearendClean,
sizeof(int16_t) * PART_LEN);
}
// Transform far end signal from time domain to frequency domain.
far_q = TimeToFrequencyDomain(aecm, aecm->xBuf, dfw, xfa, &xfaSum);
// Transform noisy near end signal from time domain to frequency domain.
zerosDBufNoisy =
TimeToFrequencyDomain(aecm, aecm->dBufNoisy, dfw, dfaNoisy, &dfaNoisySum);
aecm->
dfaNoisyQDomainOld = aecm->dfaNoisyQDomain;
aecm->
dfaNoisyQDomain = (int16_t) zerosDBufNoisy;
if (nearendClean == NULL) {
ptrDfaClean = dfaNoisy;
aecm->
dfaCleanQDomainOld = aecm->dfaNoisyQDomainOld;
aecm->
dfaCleanQDomain = aecm->dfaNoisyQDomain;
dfaCleanSum = dfaNoisySum;
} else {
// Transform clean near end signal from time domain to frequency domain.
zerosDBufClean = TimeToFrequencyDomain(aecm, aecm->dBufClean, dfw, dfaClean,
&dfaCleanSum);
aecm->
dfaCleanQDomainOld = aecm->dfaCleanQDomain;
aecm->
dfaCleanQDomain = (int16_t) zerosDBufClean;
}
// Get the delay
// Save far-end history and estimate delay
WebRtcAecm_UpdateFarHistory(aecm, xfa, far_q
);
if (WebRtc_AddFarSpectrumFix(aecm
->delay_estimator_farend, xfa, PART_LEN1,
far_q) == -1) {
return -1;
}
delay = WebRtc_DelayEstimatorProcessFix(aecm->delay_estimator, dfaNoisy,
PART_LEN1, zerosDBufNoisy);
if (delay == -1) {
return -1;
} else if (delay == -2) {
// If the delay is unknown, we assume zero.
// NOTE: this will have to be adjusted if we ever add lookahead.
delay = 0;
}
if (aecm->fixedDelay >= 0) {
// Use fixed delay
delay = aecm->fixedDelay;
}
// Get aligned far end spectrum
far_spectrum_ptr = WebRtcAecm_AlignedFarend(aecm, &far_q, delay);
zerosXBuf = (int16_t) far_q;
if (far_spectrum_ptr == NULL) {
return -1;
}
// Calculate log(energy) and update energy threshold levels
WebRtcAecm_CalcEnergies(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisySum,
echoEst32
);
// Calculate stepsize
mu = WebRtcAecm_CalcStepSize(aecm);
// Update counters
aecm->totCount++;
// This is the channel estimation algorithm.
// It is base on NLMS but has a variable step length,
// which was calculated above.
WebRtcAecm_UpdateChannel(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisy, mu,
echoEst32
);
supGain = WebRtcAecm_CalcSuppressionGain(aecm);
// Calculate Wiener filter hnl[]
for (
i = 0;
i < PART_LEN1;
i++) {
// Far end signal through channel estimate in Q8
// How much can we shift right to preserve resolution
tmp32no1 = echoEst32[i] - aecm->echoFilt[i];
aecm->echoFilt[i] += (int32_t) (((int64_t) tmp32no1* 50) >> 8);
zeros32 = WebRtcSpl_NormW32(aecm->echoFilt[i]) + 1;
zeros16 = WebRtcSpl_NormW16(supGain) + 1;
if (zeros32 + zeros16 > 16) {
// Multiplication is safe
// Result in
// Q(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN+
// aecm->xfaQDomainBuf[diff])
echoEst32Gained =
WEBRTC_SPL_UMUL_32_16((uint32_t) aecm->echoFilt[i], (uint16_t) supGain);
resolutionDiff = 14 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN;
resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf);
} else {
tmp16no1 = 17 - zeros32 - zeros16;
resolutionDiff =
14 + tmp16no1 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN;
resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf);
if (zeros32 > tmp16no1) {
echoEst32Gained = WEBRTC_SPL_UMUL_32_16((uint32_t) aecm->echoFilt[i],
supGain >> tmp16no1);
} else {
// Result in Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN-16)
echoEst32Gained = (aecm->echoFilt[i] >> tmp16no1) * supGain;
}
}
zeros16 = WebRtcSpl_NormW16(aecm->nearFilt[i]);
RTC_DCHECK_GE(zeros16,
0); // |zeros16| is a norm, hence non-negative.
dfa_clean_q_domain_diff = aecm->dfaCleanQDomain - aecm->dfaCleanQDomainOld;
if (zeros16 < dfa_clean_q_domain_diff && aecm->nearFilt[i]) {
tmp16no1 = aecm->nearFilt[i] * (1 << zeros16);
qDomainDiff = zeros16 - dfa_clean_q_domain_diff;
tmp16no2 = ptrDfaClean[i] >> -qDomainDiff;
} else {
tmp16no1 = dfa_clean_q_domain_diff < 0
? aecm->nearFilt[i] >> -dfa_clean_q_domain_diff
: aecm->nearFilt[i] * (1 << dfa_clean_q_domain_diff);
qDomainDiff = 0;
tmp16no2 = ptrDfaClean[i];
}
tmp32no1 = (int32_t) (tmp16no2 - tmp16no1);
tmp16no2 = (int16_t) (tmp32no1 >> 4);
tmp16no2 +=
tmp16no1;
zeros16 = WebRtcSpl_NormW16(tmp16no2);
if ((tmp16no2) & (-qDomainDiff > zeros16)) {
aecm->nearFilt[i] =
WEBRTC_SPL_WORD16_MAX;
} else {
aecm->nearFilt[i] = qDomainDiff < 0 ? tmp16no2 * (1 << -qDomainDiff)
: tmp16no2 >>
qDomainDiff;
}
// Wiener filter coefficients, resulting hnl in Q14
if (echoEst32Gained == 0) {
hnl[i] =
ONE_Q14;
} else if (aecm->nearFilt[i] == 0) {
hnl[i] = 0;
} else {
// Multiply the suppression gain
// Rounding
echoEst32Gained += (uint32_t) (aecm->nearFilt[i] >> 1);
tmpU32 =
WebRtcSpl_DivU32U16(echoEst32Gained, (uint16_t) aecm->nearFilt[i]);
// Current resolution is
// Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN- max(0,17-zeros16- zeros32))
// Make sure we are in Q14
tmp32no1 = (int32_t) WEBRTC_SPL_SHIFT_W32(tmpU32, resolutionDiff);
if (tmp32no1 > ONE_Q14) {
hnl[i] = 0;
} else if (tmp32no1 < 0) {
hnl[i] =
ONE_Q14;
} else {
// 1-echoEst/dfa
hnl[i] = ONE_Q14 - (int16_t)
tmp32no1;
if (hnl[i] < 0) {
hnl[i] = 0;
}
}
}
if (hnl[i]) {
numPosCoef++;
}
}
// Only in wideband. Prevent the gain in upper band from being larger than
// in lower band.
if (aecm->mult == 2) {
// TODO(bjornv): Investigate if the scaling of hnl[i] below can cause
// speech distortion in double-talk.
for (
i = 0;
i < PART_LEN1;
i++) {
hnl[i] = (int16_t) ((hnl[i] * hnl[i]) >> 14);
}
for (
i = kMinPrefBand;
i <=
kMaxPrefBand;
i++) {
avgHnl32 += (int32_t) hnl[i];
}
RTC_DCHECK_GT(kMaxPrefBand
-kMinPrefBand + 1, 0);
avgHnl32 /= (kMaxPrefBand - kMinPrefBand + 1);
for (
i = kMaxPrefBand;
i < PART_LEN1;
i++) {
if (hnl[i] > (int16_t) avgHnl32) {
hnl[i] = (int16_t)
avgHnl32;
}
}
}
// Calculate NLP gain, result is in Q14
if (aecm->nlpFlag) {
for (
i = 0;
i < PART_LEN1;
i++) {
// Truncate values close to zero and one.
if (hnl[i] > NLP_COMP_HIGH) {
hnl[i] =
ONE_Q14;
} else if (hnl[i] < NLP_COMP_LOW) {
hnl[i] = 0;
}
// Remove outliers
if (numPosCoef < 3) {
nlpGain = 0;
} else {
nlpGain = ONE_Q14;
}
// NLP
if ((hnl[i] == ONE_Q14) && (nlpGain == ONE_Q14)) {
hnl[i] =
ONE_Q14;
} else {
hnl[i] = (int16_t) ((hnl[i] * nlpGain) >> 14);
}
// multiply with Wiener coefficients
efw[i].
real = (int16_t) (
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14));
efw[i].
imag = (int16_t) (
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14));
}
} else {
// multiply with Wiener coefficients
for (
i = 0;
i < PART_LEN1;
i++) {
efw[i].
real = (int16_t) (
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14));
efw[i].
imag = (int16_t) (
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14));
}
}
if (aecm->cngMode == AecmTrue) {
ComfortNoise(aecm, ptrDfaClean, efw, hnl
);
}
InverseFFTAndWindow(aecm, fft, efw, output, nearendClean
);
return 0;
}
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
#define MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
#define AECM_DYNAMIC_Q /* Turn on/off dynamic Q-domain. */
/* Algorithm parameters */
#define FRAME_LEN 80 /* Total frame length, 10 ms. */
#define PART_LEN 64 /* Length of partition. */
#define PART_LEN_SHIFT 7 /* Length of (PART_LEN * 2) in base 2. */
#define PART_LEN1 (PART_LEN + 1) /* Unique fft coefficients. */
#define PART_LEN2 (PART_LEN << 1) /* Length of partition * 2. */
#define PART_LEN4 (PART_LEN << 2) /* Length of partition * 4. */
#define FAR_BUF_LEN PART_LEN4 /* Length of buffers. */
#define MAX_DELAY 50
/* Counter parameters */
#define CONV_LEN 512 /* Convergence length used at startup. */
#define CONV_LEN2 (CONV_LEN << 1) /* Used at startup. */
/* Energy parameters */
#define MAX_BUF_LEN 64 /* History length of energy signals. */
#define FAR_ENERGY_MIN 1025 /* Lowest Far energy level: At least 2 */
/* in energy. */
#define FAR_ENERGY_DIFF 929 /* Allowed difference between max */
/* and min. */
#define ENERGY_DEV_OFFSET 0 /* The energy error offset in Q8. */
#define ENERGY_DEV_TOL 400 /* The energy estimation tolerance (Q8). */
#define FAR_ENERGY_VAD_REGION 230 /* Far VAD tolerance region. */
/* Stepsize parameters */
#define MU_MIN 10 /* Min stepsize 2^-MU_MIN (far end energy */
/* dependent). */
#define MU_MAX 1 /* Max stepsize 2^-MU_MAX (far end energy */
/* dependent). */
#define MU_DIFF 9 /* MU_MIN - MU_MAX */
/* Channel parameters */
#define MIN_MSE_COUNT 20 /* Min number of consecutive blocks with enough */
/* far end energy to compare channel estimates. */
#define MIN_MSE_DIFF 29 /* The ratio between adapted and stored channel to */
/* accept a new storage (0.8 in Q-MSE_RESOLUTION). */
#define MSE_RESOLUTION 5 /* MSE parameter resolution. */
#define RESOLUTION_CHANNEL16 12 /* W16 Channel in Q-RESOLUTION_CHANNEL16. */
#define RESOLUTION_CHANNEL32 28 /* W32 Channel in Q-RESOLUTION_CHANNEL. */
#define CHANNEL_VAD 16 /* Minimum energy in frequency band */
/* to update channel. */
/* Suppression gain parameters: SUPGAIN parameters in Q-(RESOLUTION_SUPGAIN). */
#define RESOLUTION_SUPGAIN 8 /* Channel in Q-(RESOLUTION_SUPGAIN). */
#define SUPGAIN_DEFAULT (1 << RESOLUTION_SUPGAIN) /* Default. */
#define SUPGAIN_ERROR_PARAM_A 3072 /* Estimation error parameter */
/* (Maximum gain) (8 in Q8). */
#define SUPGAIN_ERROR_PARAM_B 1536 /* Estimation error parameter */
/* (Gain before going down). */
#define SUPGAIN_ERROR_PARAM_D SUPGAIN_DEFAULT /* Estimation error parameter */
/* (Should be the same as Default) (1 in Q8). */
#define SUPGAIN_EPC_DT 200 /* SUPGAIN_ERROR_PARAM_C * ENERGY_DEV_TOL */
/* Defines for "check delay estimation" */
#define CORR_WIDTH 31 /* Number of samples to correlate over. */
#define CORR_MAX 16 /* Maximum correlation offset. */
#define CORR_MAX_BUF 63
#define CORR_DEV 4
#define CORR_MAX_LEVEL 20
#define CORR_MAX_LOW 4
#define CORR_BUF_LEN (CORR_MAX << 1) + 1
/* Note that CORR_WIDTH + 2*CORR_MAX <= MAX_BUF_LEN. */
#define ONE_Q14 (1 << 14)
/* NLP defines */
#define NLP_COMP_LOW 3277 /* 0.2 in Q14 */
#define NLP_COMP_HIGH ONE_Q14 /* 1 in Q14 */
#endif
@@ -0,0 +1,594 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "echo_control_mobile.h"
#ifdef AEC_DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "ring_buffer.h"
#include "signal_processing_library.h"
#include "aecm_defines.h"
#include "aecm_core.h"
#define BUF_SIZE_FRAMES 50 // buffer size (frames)
// Maximum length of resampled signal. Must be an integer multiple of frames
// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
// The factor of 2 handles wb, and the + 1 is as a safety margin
#define MAX_RESAMP_LEN (5 * FRAME_LEN)
static const size_t kBufSizeSamp =
BUF_SIZE_FRAMES * FRAME_LEN; // buffer size (samples)
static const int kSampMsNb = 8; // samples per ms in nb
// Target suppression levels for nlp modes
// log{0.001, 0.00001, 0.00000001}
static const int kInitCheck = 42;
typedef struct {
int sampFreq;
int scSampFreq;
short bufSizeStart;
int knownDelay;
// Stores the last frame added to the farend buffer
short farendOld[2][FRAME_LEN];
short initFlag; // indicates if AEC has been initialized
// Variables used for averaging far end buffer size
short counter;
short sum;
short firstVal;
short checkBufSizeCtr;
// Variables used for delay shifts
short msInSndCardBuf;
short filtDelay;
int timeForDelayChange;
int ECstartup;
int checkBuffSize;
int delayChange;
short lastDelayDiff;
int16_t echoMode;
#ifdef AEC_DEBUG
FILE* bufFile;
FILE* delayFile;
FILE* preCompFile;
FILE* postCompFile;
#endif // AEC_DEBUG
// Structures
RingBuffer *farendBuf;
AecmCore *aecmCore;
} AecMobile;
// Estimates delay to set the position of the farend buffer read pointer
// (controlled by knownDelay)
static int WebRtcAecm_EstBufDelay(AecMobile *aecm, short msInSndCardBuf);
// Stuffs the farend buffer if the estimated delay is too large
static int WebRtcAecm_DelayComp(AecMobile *aecm);
void *WebRtcAecm_Create() {
// Allocate zero-filled memory.
AecMobile *aecm = (AecMobile *)(webrtc_calloc(1, sizeof(AecMobile)));
aecm->aecmCore = WebRtcAecm_CreateCore();
if (!aecm->aecmCore) {
WebRtcAecm_Free(aecm);
return NULL;
}
aecm->farendBuf = WebRtc_CreateBuffer(kBufSizeSamp, sizeof(int16_t));
if (!aecm->farendBuf) {
WebRtcAecm_Free(aecm);
return NULL;
}
#ifdef AEC_DEBUG
aecm->aecmCore->farFile = fopen("aecFar.pcm", "wb");
aecm->aecmCore->nearFile = fopen("aecNear.pcm", "wb");
aecm->aecmCore->outFile = fopen("aecOut.pcm", "wb");
// aecm->aecmCore->outLpFile = fopen("aecOutLp.pcm","wb");
aecm->bufFile = fopen("aecBuf.dat", "wb");
aecm->delayFile = fopen("aecDelay.dat", "wb");
aecm->preCompFile = fopen("preComp.pcm", "wb");
aecm->postCompFile = fopen("postComp.pcm", "wb");
#endif // AEC_DEBUG
return aecm;
}
void WebRtcAecm_Free(void *aecmInst) {
AecMobile *aecm = (AecMobile *)(aecmInst);
if (aecm == NULL) {
return;
}
#ifdef AEC_DEBUG
fclose(aecm->aecmCore->farFile);
fclose(aecm->aecmCore->nearFile);
fclose(aecm->aecmCore->outFile);
// fclose(aecm->aecmCore->outLpFile);
fclose(aecm->bufFile);
fclose(aecm->delayFile);
fclose(aecm->preCompFile);
fclose(aecm->postCompFile);
#endif // AEC_DEBUG
WebRtcAecm_FreeCore(aecm->aecmCore);
WebRtc_FreeBuffer(aecm->farendBuf);
webrtc_free(aecm);
}
int32_t WebRtcAecm_Init(void *aecmInst, int32_t sampFreq) {
AecMobile *aecm = (AecMobile *)(aecmInst);
AecmConfig aecConfig;
if (aecm == NULL) {
return -1;
}
if (sampFreq != 8000 && sampFreq != 16000) {
return AECM_BAD_PARAMETER_ERROR;
}
aecm->sampFreq = sampFreq;
// Initialize AECM core
if (WebRtcAecm_InitCore(aecm->aecmCore, aecm->sampFreq) == -1) {
return AECM_UNSPECIFIED_ERROR;
}
// Initialize farend buffer
WebRtc_InitBuffer(aecm->farendBuf);
aecm->initFlag = kInitCheck; // indicates that initialization has been done
aecm->delayChange = 1;
aecm->sum = 0;
aecm->counter = 0;
aecm->checkBuffSize = 1;
aecm->firstVal = 0;
aecm->ECstartup = 1;
aecm->bufSizeStart = 0;
aecm->checkBufSizeCtr = 0;
aecm->filtDelay = 0;
aecm->timeForDelayChange = 0;
aecm->knownDelay = 0;
aecm->lastDelayDiff = 0;
memset(&aecm->farendOld, 0, sizeof(aecm->farendOld));
// Default settings.
aecConfig.cngMode = AecmTrue;
aecConfig.echoMode = 3;
if (WebRtcAecm_set_config(aecm, aecConfig) == -1) {
return AECM_UNSPECIFIED_ERROR;
}
return 0;
}
// Returns any error that is caused when buffering the
// farend signal.
int32_t WebRtcAecm_GetBufferFarendError(void *aecmInst,
const int16_t *farend,
size_t nrOfSamples) {
AecMobile *aecm = (AecMobile *)(aecmInst);
if (aecm == NULL)
return -1;
if (farend == NULL)
return AECM_NULL_POINTER_ERROR;
if (aecm->initFlag != kInitCheck)
return AECM_UNINITIALIZED_ERROR;
if (nrOfSamples != 80 && nrOfSamples != 160)
return AECM_BAD_PARAMETER_ERROR;
return 0;
}
int32_t WebRtcAecm_BufferFarend(void *aecmInst,
const int16_t *farend,
size_t nrOfSamples) {
AecMobile *aecm = (AecMobile *)(aecmInst);
const int32_t err =
WebRtcAecm_GetBufferFarendError(aecmInst, farend, nrOfSamples);
if (err != 0)
return err;
// TODO(unknown): Is this really a good idea?
if (!aecm->ECstartup) {
WebRtcAecm_DelayComp(aecm);
}
WebRtc_WriteBuffer(aecm->farendBuf, farend, nrOfSamples);
return 0;
}
int32_t WebRtcAecm_Process(void *aecmInst,
const int16_t *nearendNoisy,
const int16_t *nearendClean,
int16_t *out,
size_t nrOfSamples,
int16_t msInSndCardBuf) {
AecMobile *aecm = (AecMobile *)(aecmInst);
int32_t retVal = 0;
size_t i;
volatile short nmbrOfFilledBuffers;
size_t nBlocks10ms;
size_t nFrames;
#ifdef AEC_DEBUG
short msInAECBuf;
#endif
if (aecm == NULL) {
return -1;
}
if (nearendNoisy == NULL) {
return AECM_NULL_POINTER_ERROR;
}
if (out == NULL) {
return AECM_NULL_POINTER_ERROR;
}
if (aecm->initFlag != kInitCheck) {
return AECM_UNINITIALIZED_ERROR;
}
if (nrOfSamples != 80 && nrOfSamples != 160) {
return AECM_BAD_PARAMETER_ERROR;
}
if (msInSndCardBuf < 0) {
msInSndCardBuf = 0;
retVal = AECM_BAD_PARAMETER_WARNING;
} else if (msInSndCardBuf > 500) {
msInSndCardBuf = 500;
retVal = AECM_BAD_PARAMETER_WARNING;
}
msInSndCardBuf += 10;
aecm->msInSndCardBuf = msInSndCardBuf;
nFrames = nrOfSamples / FRAME_LEN;
nBlocks10ms = nFrames / aecm->aecmCore->mult;
if (aecm->ECstartup) {
if (nearendClean == NULL) {
if (out != nearendNoisy) {
memcpy(out, nearendNoisy, sizeof(short) * nrOfSamples);
}
} else if (out != nearendClean) {
memcpy(out, nearendClean, sizeof(short) * nrOfSamples);
}
nmbrOfFilledBuffers =
(short) WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
// The AECM is in the start up mode
// AECM is disabled until the soundcard buffer and farend buffers are OK
// Mechanism to ensure that the soundcard buffer is reasonably stable.
if (aecm->checkBuffSize) {
aecm->checkBufSizeCtr++;
// Before we fill up the far end buffer we require the amount of data on
// the sound card to be stable (+/-8 ms) compared to the first value. This
// comparison is made during the following 4 consecutive frames. If it
// seems to be stable then we start to fill up the far end buffer.
if (aecm->counter == 0) {
aecm->firstVal = aecm->msInSndCardBuf;
aecm->sum = 0;
}
if (abs(aecm->firstVal - aecm->msInSndCardBuf) <
WEBRTC_SPL_MAX(0.2 * aecm->msInSndCardBuf, kSampMsNb)) {
aecm->sum += aecm->msInSndCardBuf;
aecm->counter++;
} else {
aecm->counter = 0;
}
if (aecm->counter * nBlocks10ms >= 6) {
// The farend buffer size is determined in blocks of 80 samples
// Use 75% of the average value of the soundcard buffer
aecm->bufSizeStart = WEBRTC_SPL_MIN(
(3 * aecm->sum * aecm->aecmCore->mult) / (aecm->counter * 40),
BUF_SIZE_FRAMES);
// buffersize has now been determined
aecm->checkBuffSize = 0;
}
if (aecm->checkBufSizeCtr * nBlocks10ms > 50) {
// for really bad sound cards, don't disable echocanceller for more than
// 0.5 sec
aecm->bufSizeStart = WEBRTC_SPL_MIN(
(3 * aecm->msInSndCardBuf * aecm->aecmCore->mult) / 40,
BUF_SIZE_FRAMES);
aecm->checkBuffSize = 0;
}
}
// if checkBuffSize changed in the if-statement above
if (!aecm->checkBuffSize) {
// soundcard buffer is now reasonably stable
// When the far end buffer is filled with approximately the same amount of
// data as the amount on the sound card we end the start up phase and
// start to cancel echoes.
if (nmbrOfFilledBuffers == aecm->bufSizeStart) {
aecm->ECstartup = 0; // Enable the AECM
} else if (nmbrOfFilledBuffers > aecm->bufSizeStart) {
WebRtc_MoveReadPtr(aecm->farendBuf,
(int) WebRtc_available_read(aecm->farendBuf) -
(int) aecm->bufSizeStart * FRAME_LEN);
aecm->ECstartup = 0;
}
}
} else {
// AECM is enabled
// Note only 1 block supported for nb and 2 blocks for wb
for (i = 0; i < nFrames; i++) {
int16_t farend[FRAME_LEN];
const int16_t *farend_ptr = NULL;
nmbrOfFilledBuffers =
(short) WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
// Check that there is data in the far end buffer
if (nmbrOfFilledBuffers > 0) {
// Get the next 80 samples from the farend buffer
WebRtc_ReadBuffer(aecm->farendBuf, (void **) &farend_ptr, farend,
FRAME_LEN);
// Always store the last frame for use when we run out of data
memcpy(&(aecm->farendOld[i][0]), farend_ptr, FRAME_LEN * sizeof(short));
} else {
// We have no data so we use the last played frame
memcpy(farend, &(aecm->farendOld[i][0]), FRAME_LEN * sizeof(short));
farend_ptr = farend;
}
// Call buffer delay estimator when all data is extracted,
// i,e. i = 0 for NB and i = 1 for WB
if ((i == 0 && aecm->sampFreq == 8000) ||
(i == 1 && aecm->sampFreq == 16000)) {
WebRtcAecm_EstBufDelay(aecm, aecm->msInSndCardBuf);
}
// Call the AECM
/*WebRtcAecm_ProcessFrame(aecm->aecmCore, farend, &nearend[FRAME_LEN * i],
&out[FRAME_LEN * i], aecm->knownDelay);*/
if (WebRtcAecm_ProcessFrame(
aecm->aecmCore, farend_ptr, &nearendNoisy[FRAME_LEN * i],
(nearendClean ? &nearendClean[FRAME_LEN * i] : NULL),
&out[FRAME_LEN * i]) == -1)
return -1;
}
}
#ifdef AEC_DEBUG
msInAECBuf = (short)WebRtc_available_read(aecm->farendBuf) /
(kSampMsNb * aecm->aecmCore->mult);
fwrite(&msInAECBuf, 2, 1, aecm->bufFile);
fwrite(&(aecm->knownDelay), sizeof(aecm->knownDelay), 1, aecm->delayFile);
#endif
return retVal;
}
int32_t WebRtcAecm_set_config(void *aecmInst, AecmConfig config) {
AecMobile *aecm = (AecMobile *)(aecmInst);
if (aecm == NULL) {
return -1;
}
if (aecm->initFlag != kInitCheck) {
return AECM_UNINITIALIZED_ERROR;
}
if (config.cngMode != AecmFalse && config.cngMode != AecmTrue) {
return AECM_BAD_PARAMETER_ERROR;
}
aecm->aecmCore->cngMode = config.cngMode;
if (config.echoMode < 0 || config.echoMode > 4) {
return AECM_BAD_PARAMETER_ERROR;
}
aecm->echoMode = config.echoMode;
if (aecm->echoMode == 0) {
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 3;
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 3;
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 3;
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 3;
aecm->aecmCore->supGainErrParamDiffAB =
(SUPGAIN_ERROR_PARAM_A >> 3) - (SUPGAIN_ERROR_PARAM_B >> 3);
aecm->aecmCore->supGainErrParamDiffBD =
(SUPGAIN_ERROR_PARAM_B >> 3) - (SUPGAIN_ERROR_PARAM_D >> 3);
} else if (aecm->echoMode == 1) {
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 2;
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 2;
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 2;
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 2;
aecm->aecmCore->supGainErrParamDiffAB =
(SUPGAIN_ERROR_PARAM_A >> 2) - (SUPGAIN_ERROR_PARAM_B >> 2);
aecm->aecmCore->supGainErrParamDiffBD =
(SUPGAIN_ERROR_PARAM_B >> 2) - (SUPGAIN_ERROR_PARAM_D >> 2);
} else if (aecm->echoMode == 2) {
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 1;
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 1;
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 1;
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 1;
aecm->aecmCore->supGainErrParamDiffAB =
(SUPGAIN_ERROR_PARAM_A >> 1) - (SUPGAIN_ERROR_PARAM_B >> 1);
aecm->aecmCore->supGainErrParamDiffBD =
(SUPGAIN_ERROR_PARAM_B >> 1) - (SUPGAIN_ERROR_PARAM_D >> 1);
} else if (aecm->echoMode == 3) {
aecm->aecmCore->supGain = SUPGAIN_DEFAULT;
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT;
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A;
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D;
aecm->aecmCore->supGainErrParamDiffAB =
SUPGAIN_ERROR_PARAM_A - SUPGAIN_ERROR_PARAM_B;
aecm->aecmCore->supGainErrParamDiffBD =
SUPGAIN_ERROR_PARAM_B - SUPGAIN_ERROR_PARAM_D;
} else if (aecm->echoMode == 4) {
aecm->aecmCore->supGain = SUPGAIN_DEFAULT << 1;
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT << 1;
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A << 1;
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D << 1;
aecm->aecmCore->supGainErrParamDiffAB =
(SUPGAIN_ERROR_PARAM_A << 1) - (SUPGAIN_ERROR_PARAM_B << 1);
aecm->aecmCore->supGainErrParamDiffBD =
(SUPGAIN_ERROR_PARAM_B << 1) - (SUPGAIN_ERROR_PARAM_D << 1);
}
return 0;
}
int32_t WebRtcAecm_InitEchoPath(void *aecmInst,
const void *echo_path,
size_t size_bytes) {
AecMobile *aecm = (AecMobile *)(aecmInst);
const int16_t *echo_path_ptr = (const int16_t *)(echo_path);
if (aecmInst == NULL) {
return -1;
}
if (echo_path == NULL) {
return AECM_NULL_POINTER_ERROR;
}
if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
// Input channel size does not match the size of AECM
return AECM_BAD_PARAMETER_ERROR;
}
if (aecm->initFlag != kInitCheck) {
return AECM_UNINITIALIZED_ERROR;
}
WebRtcAecm_InitEchoPathCore(aecm->aecmCore, echo_path_ptr);
return 0;
}
int32_t WebRtcAecm_GetEchoPath(void *aecmInst,
void *echo_path,
size_t size_bytes) {
AecMobile *aecm = (AecMobile *)(aecmInst);
int16_t *echo_path_ptr = (int16_t *)(echo_path);
if (aecmInst == NULL) {
return -1;
}
if (echo_path == NULL) {
return AECM_NULL_POINTER_ERROR;
}
if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
// Input channel size does not match the size of AECM
return AECM_BAD_PARAMETER_ERROR;
}
if (aecm->initFlag != kInitCheck) {
return AECM_UNINITIALIZED_ERROR;
}
memcpy(echo_path_ptr, aecm->aecmCore->channelStored, size_bytes);
return 0;
}
size_t WebRtcAecm_echo_path_size_bytes() {
return (PART_LEN1 * sizeof(int16_t));
}
static int WebRtcAecm_EstBufDelay(AecMobile *aecm, short msInSndCardBuf) {
short delayNew, nSampSndCard;
short nSampFar = (short) WebRtc_available_read(aecm->farendBuf);
short diff;
nSampSndCard = msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
delayNew = nSampSndCard - nSampFar;
if (delayNew < FRAME_LEN) {
WebRtc_MoveReadPtr(aecm->farendBuf, FRAME_LEN);
delayNew += FRAME_LEN;
}
aecm->filtDelay =
WEBRTC_SPL_MAX(0, (8 * aecm->filtDelay + 2 * delayNew) / 10);
diff = aecm->filtDelay - aecm->knownDelay;
if (diff > 224) {
if (aecm->lastDelayDiff < 96) {
aecm->timeForDelayChange = 0;
} else {
aecm->timeForDelayChange++;
}
} else if (diff < 96 && aecm->knownDelay > 0) {
if (aecm->lastDelayDiff > 224) {
aecm->timeForDelayChange = 0;
} else {
aecm->timeForDelayChange++;
}
} else {
aecm->timeForDelayChange = 0;
}
aecm->lastDelayDiff = diff;
if (aecm->timeForDelayChange > 25) {
aecm->knownDelay = WEBRTC_SPL_MAX((int) aecm->filtDelay - 160, 0);
}
return 0;
}
static int WebRtcAecm_DelayComp(AecMobile *aecm) {
int nSampFar = (int) WebRtc_available_read(aecm->farendBuf);
int nSampSndCard, delayNew, nSampAdd;
const int maxStuffSamp = 10 * FRAME_LEN;
nSampSndCard = aecm->msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
delayNew = nSampSndCard - nSampFar;
if (delayNew > FAR_BUF_LEN - FRAME_LEN * aecm->aecmCore->mult) {
// The difference of the buffer sizes is larger than the maximum
// allowed known delay. Compensate by stuffing the buffer.
nSampAdd = (WEBRTC_SPL_MAX(((nSampSndCard >> 1) - nSampFar), FRAME_LEN));
nSampAdd = WEBRTC_SPL_MIN(nSampAdd, maxStuffSamp);
WebRtc_MoveReadPtr(aecm->farendBuf, -nSampAdd);
aecm->delayChange = 1; // the delay needs to be updated
}
return 0;
}
@@ -0,0 +1,201 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
#define MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
#include <stddef.h>
#include "webrtc_typedef.h"
enum {
AecmFalse = 0, AecmTrue
};
// Errors
#define AECM_UNSPECIFIED_ERROR 12000
#define AECM_UNSUPPORTED_FUNCTION_ERROR 12001
#define AECM_UNINITIALIZED_ERROR 12002
#define AECM_NULL_POINTER_ERROR 12003
#define AECM_BAD_PARAMETER_ERROR 12004
// Warnings
#define AECM_BAD_PARAMETER_WARNING 12100
typedef struct {
int16_t cngMode; // AECM_FALSE, AECM_TRUE (default)
int16_t echoMode; // 0, 1, 2, 3 (default), 4
} AecmConfig;
/*
* Allocates the memory needed by the AECM. The memory needs to be
* initialized separately using the WebRtcAecm_Init() function.
* Returns a pointer to the instance and a nullptr at failure.
*/
void *WebRtcAecm_Create();
/*
* This function releases the memory allocated by WebRtcAecm_Create()
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
*/
void WebRtcAecm_Free(void *aecmInst);
/*
* Initializes an AECM instance.
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* int32_t sampFreq Sampling frequency of data
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_Init(void *aecmInst, int32_t sampFreq);
/*
* Inserts an 80 or 160 sample block of data into the farend buffer.
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* int16_t* farend In buffer containing one frame of
* farend signal
* int16_t nrOfSamples Number of samples in farend buffer
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_BufferFarend(void *aecmInst,
const int16_t *farend,
size_t nrOfSamples);
/*
* Reports any errors that would arise when buffering a farend buffer.
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* int16_t* farend In buffer containing one frame of
* farend signal
* int16_t nrOfSamples Number of samples in farend buffer
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_GetBufferFarendError(void *aecmInst,
const int16_t *farend,
size_t nrOfSamples);
/*
* Runs the AECM on an 80 or 160 sample blocks of data.
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* int16_t* nearendNoisy In buffer containing one frame of
* reference nearend+echo signal. If
* noise reduction is active, provide
* the noisy signal here.
* int16_t* nearendClean In buffer containing one frame of
* nearend+echo signal. If noise
* reduction is active, provide the
* clean signal here. Otherwise pass a
* NULL pointer.
* int16_t nrOfSamples Number of samples in nearend buffer
* int16_t msInSndCardBuf Delay estimate for sound card and
* system buffers
*
* Outputs Description
* -------------------------------------------------------------------
* int16_t* out Out buffer, one frame of processed nearend
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_Process(void *aecmInst,
const int16_t *nearendNoisy,
const int16_t *nearendClean,
int16_t *out,
size_t nrOfSamples,
int16_t msInSndCardBuf);
/*
* This function enables the user to set certain parameters on-the-fly
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* AecmConfig config Config instance that contains all
* properties to be set
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_set_config(void *aecmInst, AecmConfig config);
/*
* This function enables the user to set the echo path on-the-fly.
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* void* echo_path Pointer to the echo path to be set
* size_t size_bytes Size in bytes of the echo path
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_InitEchoPath(void *aecmInst,
const void *echo_path,
size_t size_bytes);
/*
* This function enables the user to get the currently used echo path
* on-the-fly
*
* Inputs Description
* -------------------------------------------------------------------
* void* aecmInst Pointer to the AECM instance
* void* echo_path Pointer to echo path
* size_t size_bytes Size in bytes of the echo path
*
* Outputs Description
* -------------------------------------------------------------------
* int32_t return 0: OK
* 1200-12004,12100: error/warning
*/
int32_t WebRtcAecm_GetEchoPath(void *aecmInst,
void *echo_path,
size_t size_bytes);
/*
* This function enables the user to get the echo path size in bytes
*
* Outputs Description
* -------------------------------------------------------------------
* size_t return Size in bytes
*/
size_t WebRtcAecm_echo_path_size_bytes();
#endif // MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,430 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
#define MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
//#define MIC_LEVEL_FEEDBACK
#ifdef WEBRTC_AGC_DEBUG_DUMP
#include <stdio.h>
#endif
#include <stdint.h> // NOLINT(build/include)
#include <string.h>
#ifdef WEBRTC_AGC_DEBUG_DUMP
#include <stdio.h>
#endif
#include <stdint.h> // NOLINT(build/include)
#include <assert.h>
// allpass filter coefficients.
static const uint16_t kResampleAllpass1[3] = {3284, 24441, 49528};
static const uint16_t kResampleAllpass2[3] = {12199, 37471, 60255};
typedef struct {
int32_t downState[8];
int16_t HPstate;
int16_t counter;
int16_t logRatio; // log( P(active) / P(inactive) ) (Q10)
int16_t meanLongTerm; // Q10
int32_t varianceLongTerm; // Q8
int16_t stdLongTerm; // Q10
int16_t meanShortTerm; // Q10
int32_t varianceShortTerm; // Q8
int16_t stdShortTerm; // Q10
} AgcVad; // total = 54 bytes
typedef struct {
int32_t capacitorSlow;
int32_t capacitorFast;
int32_t gain;
int32_t gainTable[32];
int16_t gatePrevious;
int16_t agcMode;
AgcVad vadNearend;
AgcVad vadFarend;
#ifdef WEBRTC_AGC_DEBUG_DUMP
FILE* logFile;
int frameCounter;
#endif
} DigitalAgc;
int32_t WebRtcAgc_InitDigital(DigitalAgc *digitalAgcInst, int16_t agcMode);
int32_t WebRtcAgc_ProcessDigital(DigitalAgc *digitalAgcInst,
const int16_t *const *inNear,
size_t num_bands,
int16_t *const *out,
uint32_t FS,
int16_t lowLevelSignal);
int32_t WebRtcAgc_AddFarendToDigital(DigitalAgc *digitalAgcInst,
const int16_t *inFar,
size_t nrSamples);
void WebRtcAgc_InitVad(AgcVad *vadInst);
int16_t WebRtcAgc_ProcessVad(AgcVad *vadInst, // (i) VAD state
const int16_t *in, // (i) Speech signal
size_t nrSamples); // (i) number of samples
int32_t WebRtcAgc_CalculateGainTable(int32_t *gainTable, // Q16
int16_t compressionGaindB, // Q0 (in dB)
int16_t targetLevelDbfs, // Q0 (in dB)
uint8_t limiterEnable,
int16_t analogTarget);
// Errors
#define AGC_UNSPECIFIED_ERROR 18000
#define AGC_UNSUPPORTED_FUNCTION_ERROR 18001
#define AGC_UNINITIALIZED_ERROR 18002
#define AGC_NULL_POINTER_ERROR 18003
#define AGC_BAD_PARAMETER_ERROR 18004
// Warnings
#define AGC_BAD_PARAMETER_WARNING 18050
enum {
kAgcModeUnchanged,
kAgcModeAdaptiveAnalog,
kAgcModeAdaptiveDigital,
kAgcModeFixedDigital
};
enum {
kAgcFalse = 0, kAgcTrue
};
typedef struct {
int16_t targetLevelDbfs; // default 3 (-3 dBOv)
int16_t compressionGaindB; // default 9 dB
uint8_t limiterEnable; // default kAgcTrue (on)
} WebRtcAgcConfig;
#if defined(__cplusplus)
extern "C" {
#endif
/*
* This function analyses the number of samples passed to
* farend and produces any error code that could arise.
*
* Input:
* - agcInst : AGC instance.
* - samples : Number of samples in input vector.
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error.
*/
int WebRtcAgc_GetAddFarendError(void *state, size_t samples);
/*
* This function processes a 10 ms frame of far-end speech to determine
* if there is active speech. The length of the input speech vector must be
* given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
* FS=48000).
*
* Input:
* - agcInst : AGC instance.
* - inFar : Far-end input speech vector
* - samples : Number of samples in input vector
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_AddFarend(void *agcInst, const int16_t *inFar, size_t samples);
/*
* This function processes a 10 ms frame of microphone speech to determine
* if there is active speech. The length of the input speech vector must be
* given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
* FS=48000). For very low input levels, the input signal is increased in level
* by multiplying and overwriting the samples in inMic[].
*
* This function should be called before any further processing of the
* near-end microphone signal.
*
* Input:
* - agcInst : AGC instance.
* - inMic : Microphone input speech vector for each band
* - num_bands : Number of bands in input vector
* - samples : Number of samples in input vector
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_AddMic(void *agcInst,
int16_t *const *inMic,
size_t num_bands,
size_t samples);
/*
* This function replaces the analog microphone with a virtual one.
* It is a digital gain applied to the input signal and is used in the
* agcAdaptiveDigital mode where no microphone level is adjustable. The length
* of the input speech vector must be given in samples (80 when FS=8000, and 160
* when FS=16000, FS=32000 or FS=48000).
*
* Input:
* - agcInst : AGC instance.
* - inMic : Microphone input speech vector for each band
* - num_bands : Number of bands in input vector
* - samples : Number of samples in input vector
* - micLevelIn : Input level of microphone (static)
*
* Output:
* - inMic : Microphone output after processing (L band)
* - inMic_H : Microphone output after processing (H band)
* - micLevelOut : Adjusted microphone level after processing
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_VirtualMic(void *agcInst,
int16_t *const *inMic,
size_t num_bands,
size_t samples,
int32_t micLevelIn,
int32_t *micLevelOut);
/*
* This function processes a 10 ms frame and adjusts (normalizes) the gain both
* analog and digitally. The gain adjustments are done only during active
* periods of speech. The length of the speech vectors must be given in samples
* (80 when FS=8000, and 160 when FS=16000, FS=32000 or FS=48000). The echo
* parameter can be used to ensure the AGC will not adjust upward in the
* presence of echo.
*
* This function should be called after processing the near-end microphone
* signal, in any case after any echo cancellation.
*
* Input:
* - agcInst : AGC instance
* - inNear : Near-end input speech vector for each band
* - num_bands : Number of bands in input/output vector
* - samples : Number of samples in input/output vector
* - inMicLevel : Current microphone volume level
* - echo : Set to 0 if the signal passed to add_mic is
* almost certainly free of echo; otherwise set
* to 1. If you have no information regarding echo
* set to 0.
*
* Output:
* - outMicLevel : Adjusted microphone volume level
* - out : Gain-adjusted near-end speech vector
* : May be the same vector as the input.
* - saturationWarning : A returned value of 1 indicates a saturation event
* has occurred and the volume cannot be further
* reduced. Otherwise will be set to 0.
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_Process(void *agcInst,
const int16_t *const *inNear,
size_t num_bands,
size_t samples,
int16_t *const *out,
int32_t inMicLevel,
int32_t *outMicLevel,
int16_t echo,
uint8_t *saturationWarning);
/*
* This function sets the config parameters (targetLevelDbfs,
* compressionGaindB and limiterEnable).
*
* Input:
* - agcInst : AGC instance
* - config : config struct
*
* Output:
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_set_config(void *agcInst, WebRtcAgcConfig config);
/*
* This function returns the config parameters (targetLevelDbfs,
* compressionGaindB and limiterEnable).
*
* Input:
* - agcInst : AGC instance
*
* Output:
* - config : config struct
*
* Return value:
* : 0 - Normal operation.
* : -1 - Error
*/
int WebRtcAgc_get_config(void *agcInst, WebRtcAgcConfig *config);
/*
* This function creates and returns an AGC instance, which will contain the
* state information for one (duplex) channel.
*/
void *WebRtcAgc_Create(void);
/*
* This function frees the AGC instance created at the beginning.
*
* Input:
* - agcInst : AGC instance.
*/
void WebRtcAgc_Free(void *agcInst);
/*
* This function initializes an AGC instance.
*
* Input:
* - agcInst : AGC instance.
* - minLevel : Minimum possible mic level
* - maxLevel : Maximum possible mic level
* - agcMode : 0 - Unchanged
* : 1 - Adaptive Analog Automatic Gain Control -3dBOv
* : 2 - Adaptive Digital Automatic Gain Control -3dBOv
* : 3 - Fixed Digital Gain 0dB
* - fs : Sampling frequency
*
* Return value : 0 - Ok
* -1 - Error
*/
int WebRtcAgc_Init(void *agcInst,
int32_t minLevel,
int32_t maxLevel,
int16_t agcMode,
uint32_t fs);
#if defined(__cplusplus)
}
#endif
/* Analog Automatic Gain Control variables:
* Constant declarations (inner limits inside which no changes are done)
* In the beginning the range is narrower to widen as soon as the measure
* 'Rxx160_LP' is inside it. Currently the starting limits are -22.2+/-1dBm0
* and the final limits -22.2+/-2.5dBm0. These levels makes the speech signal
* go towards -25.4dBm0 (-31.4dBov). Tuned with wbfile-31.4dBov.pcm
* The limits are created by running the AGC with a file having the desired
* signal level and thereafter plotting Rxx160_LP in the dBm0-domain defined
* by out=10*log10(in/260537279.7); Set the target level to the average level
* of our measure Rxx160_LP. Remember that the levels are in blocks of 16 in
* Q(-7). (Example matlab code: round(db2pow(-21.2)*16/2^7) )
*/
#define RXX_BUFFER_LEN 10
static const int16_t kMsecSpeechInner = 520;
static const int16_t kMsecSpeechOuter = 340;
static const int16_t kNormalVadThreshold = 400;
static const int16_t kAlphaShortTerm = 6; // 1 >> 6 = 0.0156
static const int16_t kAlphaLongTerm = 10; // 1 >> 10 = 0.000977
typedef struct {
// Configurable parameters/variables
uint32_t fs; // Sampling frequency
int16_t compressionGaindB; // Fixed gain level in dB
int16_t targetLevelDbfs; // Target level in -dBfs of envelope (default -3)
int16_t agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig)
uint8_t limiterEnable; // Enabling limiter (on/off (default off))
WebRtcAgcConfig defaultConfig;
WebRtcAgcConfig usedConfig;
// General variables
int16_t initFlag;
int16_t lastError;
// Target level parameters
// Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
int32_t analogTargetLevel; // = RXX_BUFFER_LEN * 846805; -22 dBfs
int32_t startUpperLimit; // = RXX_BUFFER_LEN * 1066064; -21 dBfs
int32_t startLowerLimit; // = RXX_BUFFER_LEN * 672641; -23 dBfs
int32_t upperPrimaryLimit; // = RXX_BUFFER_LEN * 1342095; -20 dBfs
int32_t lowerPrimaryLimit; // = RXX_BUFFER_LEN * 534298; -24 dBfs
int32_t upperSecondaryLimit; // = RXX_BUFFER_LEN * 2677832; -17 dBfs
int32_t lowerSecondaryLimit; // = RXX_BUFFER_LEN * 267783; -27 dBfs
uint16_t targetIdx; // Table index for corresponding target level
#ifdef MIC_LEVEL_FEEDBACK
uint16_t targetIdxOffset; // Table index offset for level compensation
#endif
int16_t analogTarget; // Digital reference level in ENV scale
// Analog AGC specific variables
int32_t filterState[8]; // For downsampling wb to nb
int32_t upperLimit; // Upper limit for mic energy
int32_t lowerLimit; // Lower limit for mic energy
int32_t Rxx160w32; // Average energy for one frame
int32_t Rxx16_LPw32; // Low pass filtered subframe energies
int32_t Rxx160_LPw32; // Low pass filtered frame energies
int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe
int32_t Rxx16_vectorw32[RXX_BUFFER_LEN]; // Array with subframe energies
int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal
int32_t env[2][10]; // Envelope values of subframes
int16_t Rxx16pos; // Current position in the Rxx16_vectorw32
int16_t envSum; // Filtered scaled envelope in subframes
int16_t vadThreshold; // Threshold for VAD decision
int16_t inActive; // Inactive time in milliseconds
int16_t msTooLow; // Milliseconds of speech at a too low level
int16_t msTooHigh; // Milliseconds of speech at a too high level
int16_t changeToSlowMode; // Change to slow mode after some time at target
int16_t firstCall; // First call to the process-function
int16_t msZero; // Milliseconds of zero input
int16_t msecSpeechOuterChange; // Min ms of speech between volume changes
int16_t msecSpeechInnerChange; // Min ms of speech between volume changes
int16_t activeSpeech; // Milliseconds of active speech
int16_t muteGuardMs; // Counter to prevent mute action
int16_t inQueue; // 10 ms batch indicator
// Microphone level variables
int32_t micRef; // Remember ref. mic level for virtual mic
uint16_t gainTableIdx; // Current position in virtual gain table
int32_t micGainIdx; // Gain index of mic level to increase slowly
int32_t micVol; // Remember volume between frames
int32_t maxLevel; // Max possible vol level, incl dig gain
int32_t maxAnalog; // Maximum possible analog volume level
int32_t maxInit; // Initial value of "max"
int32_t minLevel; // Minimum possible volume level
int32_t minOutput; // Minimum output volume level
int32_t zeroCtrlMax; // Remember max gain => don't amp low input
int32_t lastInMicLevel;
int16_t scale; // Scale factor for internal volume levels
#ifdef MIC_LEVEL_FEEDBACK
int16_t numBlocksMicLvlSat;
uint8_t micLvlSat;
#endif
// Structs for VAD and digital_agc
AgcVad vadMic;
DigitalAgc digitalAgc;
#ifdef WEBRTC_AGC_DEBUG_DUMP
FILE* fpt;
FILE* agcLog;
int32_t fcount;
#endif
int16_t lowLevelSignal;
} LegacyAgc;
#endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_DEFINES_H_
#define WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_DEFINES_H_
//#define PROCESS_FLOW_0 // Use the traditional method.
//#define PROCESS_FLOW_1 // Use traditional with DD estimate of prior SNR.
#define PROCESS_FLOW_2 // Use the new method of speech/noise classification.
#define BLOCKL_MAX 160 // max processing block length: 160
#define ANAL_BLOCKL_MAX 256 // max analysis block length: 256
#define HALF_ANAL_BLOCKL 129 // half max analysis block length + 1
#define QUANTILE (float)0.25
#define SIMULT 3
#define END_STARTUP_LONG 200
#define END_STARTUP_SHORT 50
#define FACTOR (float)40.0
#define WIDTH (float)0.01
#define SMOOTH (float)0.75 // filter smoothing
// Length of fft work arrays.
#define IP_LENGTH (ANAL_BLOCKL_MAX >> 1) // must be at least ceil(2 + sqrt(ANAL_BLOCKL_MAX/2))
#define W_LENGTH (ANAL_BLOCKL_MAX >> 1)
//PARAMETERS FOR NEW METHOD
#define DD_PR_SNR (float)0.98 // DD update of prior SNR
#define LRT_TAVG (float)0.50 // tavg parameter for LRT (previously 0.90)
#define SPECT_FL_TAVG (float)0.30 // tavg parameter for spectral flatness measure
#define SPECT_DIFF_TAVG (float)0.30 // tavg parameter for spectral difference measure
#define PRIOR_UPDATE (float)0.10 // update parameter of prior model
#define NOISE_UPDATE (float)0.90 // update parameter for noise
#define SPEECH_UPDATE (float)0.99 // update parameter when likely speech
#define WIDTH_PR_MAP (float)4.0 // width parameter in sigmoid map for prior model
#define LRT_FEATURE_THR (float)0.5 // default threshold for LRT feature
#define SF_FEATURE_THR (float)0.5 // default threshold for Spectral Flatness feature
#define SD_FEATURE_THR (float)0.5 // default threshold for Spectral Difference feature
#define PROB_RANGE (float)0.20 // probability threshold for noise state in
// speech/noise likelihood
#define HIST_PAR_EST 1000 // histogram size for estimation of parameters
#define GAMMA_PAUSE (float)0.05 // update for conservative noise estimate
//
#define B_LIM (float)0.5 // threshold in final energy gain factor calculation
#endif // WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_DEFINES_H_
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "noise_suppression_x.h"
#include "common_audio/signal_processing/include/real_fft.h"
#include "nsx_core.h"
#include "nsx_defines.h"
//NoiseSuppressionFixedC noise_suppression_ctl;
NsxHandle* WebRtcNsx_Create()
{
NoiseSuppressionFixedC *noise_suppression_ctl = (NoiseSuppressionFixedC*)webrtc_zalloc(sizeof(NoiseSuppressionFixedC));
return (NsxHandle*)noise_suppression_ctl;
}
int WebRtcNsx_Init(NsxHandle* nsxInst, u32 fs)
{
return WebRtcNsx_InitCore((NoiseSuppressionFixedC*) nsxInst);
}
//int WebRtcNsx_set_policy(NsxHandle* nsxInst, int mode)
//{
// return WebRtcNsx_set_policy_core((NoiseSuppressionFixedC*) nsxInst, mode);
//}
void WebRtcNsx_Process(NsxHandle* nsxInst,
short* speechFrame,
int num_bands,
short* outFrame)
{
WebRtcNsx_ProcessCore((NoiseSuppressionFixedC*) nsxInst, speechFrame,
num_bands, outFrame);
}
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSION_X_H_
#define MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSION_X_H_
#include "typedefs.h" // NOLINT(build/include)
typedef struct NsxHandleT NsxHandle;
/*
* This function creates an instance of the fixed point Noise Suppression.
*/
NsxHandle* WebRtcNsx_Create();
/*
* This function initializes a NS instance
*
* Input:
* - nsxInst : Instance that should be initialized
* - fs : sampling frequency
*
* Output:
* - nsxInst : Initialized instance
*
* Return value : 0 - Ok
* -1 - Error
*/
int WebRtcNsx_Init(NsxHandle* nsxInst, u32 fs);
/*
* This changes the aggressiveness of the noise suppression method.
*
* Input:
* - nsxInst : Instance that should be initialized
* - mode : 0: Mild, 1: Medium , 2: Aggressive
*
* Output:
* - nsxInst : Initialized instance
*
* Return value : 0 - Ok
* -1 - Error
*/
int WebRtcNsx_set_policy(NsxHandle* nsxInst, int mode);
/*
* This functions does noise suppression for the inserted speech frame. The
* input and output signals should always be 10ms (80 or 160 samples).
*
* Input
* - nsxInst : NSx instance. Needs to be initiated before call.
* - speechFrame : Pointer to speech frame buffer for each band
* - num_bands : Number of bands
*
* Output:
* - nsxInst : Updated NSx instance
* - outFrame : Pointer to output frame for each band
*/
void WebRtcNsx_Process(NsxHandle* nsxInst,
short* speechFrame,
int num_bands,
short* outFrame);
#endif // MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSION_X_H_
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,210 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
#define MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
#include "common_audio/signal_processing/include/signal_processing_library.h"
#include "nsx_defines.h"
#include "typedefs.h" // NOLINT(build/include)
typedef struct NoiseSuppressionFixedC_ {
//u32 fs;
// const s16* window;
s16 analysisBuffer[ANAL_BLOCKL_MAX];
s16 synthesisBuffer[ANAL_BLOCKL_MAX];
u16 noiseSupFilter[HALF_ANAL_BLOCKL];
// u16 overdrive; /* Q8 */
// u16 denoiseBound; /* Q14 */
// const s16* factor2Table;
s16 noiseEstLogQuantile[SIMULT* HALF_ANAL_BLOCKL];
s16 noiseEstDensity[SIMULT* HALF_ANAL_BLOCKL];
s16 noiseEstCounter[SIMULT];
s16 noiseEstQuantile[HALF_ANAL_BLOCKL];
// uint anaLen;
// uint anaLen2;
// uint magnLen;
// int aggrMode;
// int stages;
// int initFlag;
// int gainMap;
// s32 maxLrt;
// s32 minLrt;
// Log LRT factor with time-smoothing in Q8.
s32 logLrtTimeAvgW32[HALF_ANAL_BLOCKL];
s32 featureLogLrt;
s32 thresholdLogLrt;
s16 weightLogLrt;
u32 featureSpecDiff;
u32 thresholdSpecDiff;
s16 weightSpecDiff;
u32 featureSpecFlat;
u32 thresholdSpecFlat;
s16 weightSpecFlat;
// Conservative estimate of noise spectrum.
s32 avgMagnPause[HALF_ANAL_BLOCKL];
u32 magnEnergy;
u32 sumMagn;
u32 curAvgMagnEnergy;
u32 timeAvgMagnEnergy;
u32 timeAvgMagnEnergyTmp;
u32 whiteNoiseLevel; // Initial noise estimate.
// Initial magnitude spectrum estimate.
u32 initMagnEst[HALF_ANAL_BLOCKL];
// Pink noise parameters:
s32 pinkNoiseNumerator; // Numerator.
s32 pinkNoiseExp; // Power of freq.
int minNorm; // Smallest normalization factor.
int zeroInputSignal; // Zero input signal flag.
// Noise spectrum from previous frame.
u32 prevNoiseU32[HALF_ANAL_BLOCKL];
// Magnitude spectrum from previous frame.
u16 prevMagnU16[HALF_ANAL_BLOCKL];
// Prior speech/noise probability in Q14.
s16 priorNonSpeechProb;
int blockIndex; // Frame index counter.
// Parameter for updating or estimating thresholds/weights for prior model.
// int modelUpdate;
int cntThresUpdate;
// Histograms for parameter estimation.
s16 histLrt[HIST_PAR_EST];
s16 histSpecFlat[HIST_PAR_EST];
s16 histSpecDiff[HIST_PAR_EST];
// Quantities for high band estimate.
// s16 dataBufHBFX[NUM_HIGH_BANDS_MAX][ANAL_BLOCKL_MAX];
int qNoise;
int prevQNoise;
int prevQMagn;
// uint blockLen10ms;
s16 real[ANAL_BLOCKL_MAX];
s16 imag[ANAL_BLOCKL_MAX];
s32 energyIn;
int scaleEnergyIn;
int normData;
struct RealFFT* real_fft;
} NoiseSuppressionFixedC;
/****************************************************************************
* WebRtcNsx_InitCore(...)
*
* This function initializes a noise suppression instance
*
* Input:
* - inst : Instance that should be initialized
* - fs : Sampling frequency
*
* Output:
* - inst : Initialized instance
*
* Return value : 0 - Ok
* -1 - Error
*/
s32 WebRtcNsx_InitCore(NoiseSuppressionFixedC* inst);
/****************************************************************************
* WebRtcNsx_set_policy_core(...)
*
* This changes the aggressiveness of the noise suppression method.
*
* Input:
* - inst : Instance that should be initialized
* - mode : 0: Mild (6 dB), 1: Medium (10 dB), 2: Aggressive (15 dB)
*
* Output:
* - inst : Initialized instance
*
* Return value : 0 - Ok
* -1 - Error
*/
//int WebRtcNsx_set_policy_core(NoiseSuppressionFixedC* inst, int mode);
/****************************************************************************
* WebRtcNsx_ProcessCore
*
* Do noise suppression.
*
* Input:
* - inst : Instance that should be initialized
* - inFrame : Input speech frame for each band
* - num_bands : Number of bands
*
* Output:
* - inst : Updated instance
* - outFrame : Output speech frame for each band
*/
void WebRtcNsx_ProcessCore(NoiseSuppressionFixedC* inst,
short* inFrame,
int num_bands,
short* outFrame);
/****************************************************************************
* Some function pointers, for internal functions shared by ARM NEON and
* generic C code.
*/
// Noise Estimation.
//typedef void (*NoiseEstimation)(NoiseSuppressionFixedC* inst,
// u16* magn,
// u32* noise,
// s16* q_noise);
//extern NoiseEstimation WebRtcNsx_NoiseEstimation;
// Filter the data in the frequency domain, and create spectrum.
//typedef void (*PrepareSpectrum)(NoiseSuppressionFixedC* inst,
// s16 * freq_buff);
//extern PrepareSpectrum WebRtcNsx_PrepareSpectrum;
// For the noise supression process, synthesis, read out fully processed
// segment, and update synthesis buffer.
//typedef void (*SynthesisUpdate)(NoiseSuppressionFixedC* inst,
// s16 * out_frame,
// s16 gain_factor);
//extern SynthesisUpdate WebRtcNsx_SynthesisUpdate;
// Update analysis buffer for lower band, and window data before FFT.
//typedef void (*AnalysisUpdate)(NoiseSuppressionFixedC* inst,
// s16 * out,
// s16 * new_speech);
//extern AnalysisUpdate WebRtcNsx_AnalysisUpdate;
// Denormalize the real-valued signal |in|, the output from inverse FFT.
//typedef void (*Denormalize)(NoiseSuppressionFixedC* inst,
// s16 * in,
// int factor);
//extern Denormalize WebRtcNsx_Denormalize;
// Normalize the real-valued signal |in|, the input to forward FFT.
//typedef void (*NormalizeRealBuffer)(NoiseSuppressionFixedC* inst,
// const s16 * in,
// s16 * out);
//extern NormalizeRealBuffer WebRtcNsx_NormalizeRealBuffer;
// Compute speech/noise probability.
// Intended to be private.
extern void WebRtcNsx_SpeechNoiseProb(NoiseSuppressionFixedC* inst,
u16* nonSpeechProbFinal,
u32* priorLocSnr,
u32* postLocSnr);
#endif // MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
@@ -0,0 +1,258 @@
/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "noise_suppression_x.h"
#include "nsx_core.h"
#include "nsx_defines.h"
static const s16 kIndicatorTable[17] =
{
0, 2017, 3809, 5227, 6258, 6963, 7424, 7718,
7901, 8014, 8084, 8126, 8152, 8168, 8177, 8183, 8187
};
// Compute speech/noise probability
// speech/noise probability is returned in: probSpeechFinal
//snrLocPrior is the prior SNR for each frequency (in Q11)
//snrLocPost is the post SNR for each frequency (in Q11)
void WebRtcNsx_SpeechNoiseProb(NoiseSuppressionFixedC* inst,
u16* nonSpeechProbFinal,
u32* priorLocSnr,
u32* postLocSnr)
{
u32 zeros, num, den, tmpU32no1, tmpU32no2, tmpU32no3;
s32 invLrtFX, indPriorFX, tmp32, tmp32no1, tmp32no2, besselTmpFX32;
s32 frac32, logTmp;
s32 logLrtTimeAvgKsumFX;
s16 indPriorFX16;
s16 tmp16, tmp16no1, tmp16no2, tmpIndFX, tableIndex, frac, intPart;
uint i;
int normTmp, normTmp2, nShifts;
// compute feature based on average LR factor
// this is the average over all frequencies of the smooth log LRT
logLrtTimeAvgKsumFX = 0;
for (i = 0; i < NR_MAGNLEN; i++) {
besselTmpFX32 = (s32) postLocSnr[i]; // Q11
normTmp = WebRtcSpl_NormU32(postLocSnr[i]);
num = postLocSnr[i] << normTmp; // Q(11+normTmp)
if (normTmp > 10) {
den = priorLocSnr[i] << (normTmp - 11); // Q(normTmp)
} else {
den = priorLocSnr[i] >> (11 - normTmp); // Q(normTmp)
}
if (den > 0) {
besselTmpFX32 -= num / den; // Q11
} else {
besselTmpFX32 = 0;
}
// inst->logLrtTimeAvg[i] += LRT_TAVG * (besselTmp - log(snrLocPrior)
// - inst->logLrtTimeAvg[i]);
// Here, LRT_TAVG = 0.5
zeros = WebRtcSpl_NormU32(priorLocSnr[i]);
frac32 = (s32) (((priorLocSnr[i] << zeros) & 0x7FFFFFFF) >> 19);
tmp32 = (frac32 * frac32 * -43) >> 19;
tmp32 += ((s16) frac32 * 5412) >> 12;
frac32 = tmp32 + 37;
// tmp32 = log2(priorLocSnr[i])
tmp32 = (s32) (((31 - zeros) << 12) + frac32) - (11 << 12); // Q12
logTmp = (tmp32 * 178) >> 8; // log2(priorLocSnr[i])*log(2)
// tmp32no1 = LRT_TAVG * (log(snrLocPrior) + inst->logLrtTimeAvg[i]) in Q12.
tmp32no1 = (logTmp + inst->logLrtTimeAvgW32[i]) / 2;
inst->logLrtTimeAvgW32[i] += (besselTmpFX32 - tmp32no1); // Q12
logLrtTimeAvgKsumFX += inst->logLrtTimeAvgW32[i]; // Q12
}
inst->featureLogLrt = (logLrtTimeAvgKsumFX * BIN_SIZE_LRT) >> (NR_STAGES + 11);
// done with computation of LR factor
//
//compute the indicator functions
//
// average LRT feature
// FLOAT code
// indicator0 = 0.5 * (tanh(widthPrior *
// (logLrtTimeAvgKsum - threshPrior0)) + 1.0);
tmpIndFX = 16384; // Q14(1.0)
tmp32no1 = logLrtTimeAvgKsumFX - inst->thresholdLogLrt; // Q12
nShifts = 7 - NR_STAGES; // WIDTH_PR_MAP_SHIFT - inst->stages + 5;
//use larger width in tanh map for pause regions
if (tmp32no1 < 0) {
tmpIndFX = 0;
tmp32no1 = -tmp32no1;
//widthPrior = widthPrior * 2.0;
nShifts++;
}
tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1, nShifts); // Q14
// compute indicator function: sigmoid map
if (tmp32no1 < (16 << 14) && tmp32no1 >= 0) {
tableIndex = (s16) (tmp32no1 >> 14);
tmp16no2 = kIndicatorTable[tableIndex];
tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
frac = (s16) (tmp32no1 & 0x00003fff); // Q14
tmp16no2 += (s16) ((tmp16no1 * frac) >> 14);
if (tmpIndFX == 0) {
tmpIndFX = 8192 - tmp16no2; // Q14
} else {
tmpIndFX = 8192 + tmp16no2; // Q14
}
}
indPriorFX = inst->weightLogLrt * tmpIndFX; // 6*Q14
//spectral flatness feature
if (inst->weightSpecFlat) {
tmpU32no1 = WEBRTC_SPL_UMUL(inst->featureSpecFlat, 400); // Q10
tmpIndFX = 16384; // Q14(1.0)
//use larger width in tanh map for pause regions
tmpU32no2 = inst->thresholdSpecFlat - tmpU32no1; //Q10
nShifts = 4;
if (inst->thresholdSpecFlat < tmpU32no1) {
tmpIndFX = 0;
tmpU32no2 = tmpU32no1 - inst->thresholdSpecFlat;
//widthPrior = widthPrior * 2.0;
nShifts++;
}
tmpU32no1 = WebRtcSpl_DivU32U16(tmpU32no2 << nShifts, 25); // Q14
// compute indicator function: sigmoid map
// FLOAT code
// indicator1 = 0.5 * (tanh(sgnMap * widthPrior *
// (threshPrior1 - tmpFloat1)) + 1.0);
if (tmpU32no1 < (16 << 14)) {
tableIndex = (s16) (tmpU32no1 >> 14);
tmp16no2 = kIndicatorTable[tableIndex];
tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
frac = (s16) (tmpU32no1 & 0x00003fff); // Q14
tmp16no2 += (s16) ((tmp16no1 * frac) >> 14);
if (tmpIndFX) {
tmpIndFX = 8192 + tmp16no2; // Q14
} else {
tmpIndFX = 8192 - tmp16no2; // Q14
}
}
indPriorFX += inst->weightSpecFlat * tmpIndFX; // 6*Q14
}
//for template spectral-difference
if (inst->weightSpecDiff) {
tmpU32no1 = 0;
if (inst->featureSpecDiff) {
normTmp = WEBRTC_SPL_MIN(20 - NR_STAGES,
WebRtcSpl_NormU32(inst->featureSpecDiff));
tmpU32no1 = inst->featureSpecDiff << normTmp; // Q(normTmp-2*stages)
tmpU32no2 = inst->timeAvgMagnEnergy >> (20 - NR_STAGES - normTmp);
if (tmpU32no2 > 0) {
// Q(20 - inst->stages)
tmpU32no1 /= tmpU32no2;
} else {
tmpU32no1 = (u32) (0x7fffffff);
}
}
tmpU32no3 = (inst->thresholdSpecDiff << 17) / 25;
tmpU32no2 = tmpU32no1 - tmpU32no3;
nShifts = 1;
tmpIndFX = 16384; // Q14(1.0)
//use larger width in tanh map for pause regions
if (tmpU32no2 & 0x80000000) {
tmpIndFX = 0;
tmpU32no2 = tmpU32no3 - tmpU32no1;
//widthPrior = widthPrior * 2.0;
nShifts--;
}
tmpU32no1 = tmpU32no2 >> nShifts;
// compute indicator function: sigmoid map
/* FLOAT code
indicator2 = 0.5 * (tanh(widthPrior * (tmpFloat1 - threshPrior2)) + 1.0);
*/
if (tmpU32no1 < (16 << 14)) {
tableIndex = (s16) (tmpU32no1 >> 14);
tmp16no2 = kIndicatorTable[tableIndex];
tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
frac = (s16) (tmpU32no1 & 0x00003fff); // Q14
tmp16no2 += (s16) WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(
tmp16no1, frac, 14);
if (tmpIndFX) {
tmpIndFX = 8192 + tmp16no2;
} else {
tmpIndFX = 8192 - tmp16no2;
}
}
indPriorFX += inst->weightSpecDiff * tmpIndFX; // 6*Q14
}
//combine the indicator function with the feature weights
// FLOAT code
// indPrior = 1 - (weightIndPrior0 * indicator0 + weightIndPrior1 *
// indicator1 + weightIndPrior2 * indicator2);
indPriorFX16 = WebRtcSpl_DivW32W16ResW16(98307 - indPriorFX, 6); // Q14
// done with computing indicator function
//compute the prior probability
// FLOAT code
// inst->priorNonSpeechProb += PRIOR_UPDATE *
// (indPriorNonSpeech - inst->priorNonSpeechProb);
tmp16 = indPriorFX16 - inst->priorNonSpeechProb; // Q14
inst->priorNonSpeechProb += (s16) ((PRIOR_UPDATE_Q14 * tmp16) >> 14);
//final speech probability: combine prior model with LR factor:
memset(nonSpeechProbFinal, 0, sizeof (u16) * NR_MAGNLEN);
if (inst->priorNonSpeechProb > 0) {
for (i = 0; i < NR_MAGNLEN; i++) {
// FLOAT code
// invLrt = exp(inst->logLrtTimeAvg[i]);
// invLrt = inst->priorSpeechProb * invLrt;
// nonSpeechProbFinal[i] = (1.0 - inst->priorSpeechProb) /
// (1.0 - inst->priorSpeechProb + invLrt);
// invLrt = (1.0 - inst->priorNonSpeechProb) * invLrt;
// nonSpeechProbFinal[i] = inst->priorNonSpeechProb /
// (inst->priorNonSpeechProb + invLrt);
if (inst->logLrtTimeAvgW32[i] < 65300) {
tmp32no1 = (inst->logLrtTimeAvgW32[i] * 23637) >> 14; // Q12
intPart = (s16) (tmp32no1 >> 12);
if (intPart < -8) {
intPart = -8;
}
frac = (s16) (tmp32no1 & 0x00000fff); // Q12
// Quadratic approximation of 2^frac
tmp32no2 = (frac * frac * 44) >> 19; // Q12.
tmp32no2 += (frac * 84) >> 7; // Q12
invLrtFX = (1 << (8 + intPart)) +
WEBRTC_SPL_SHIFT_W32(tmp32no2, intPart - 4); // Q8
normTmp = WebRtcSpl_NormW32(invLrtFX);
normTmp2 = WebRtcSpl_NormW16((16384 - inst->priorNonSpeechProb));
if (normTmp + normTmp2 >= 7) {
if (normTmp + normTmp2 < 15) {
invLrtFX >>= 15 - normTmp2 - normTmp;
// Q(normTmp+normTmp2-7)
tmp32no1 = invLrtFX * (16384 - inst->priorNonSpeechProb);
// Q(normTmp+normTmp2+7)
invLrtFX = WEBRTC_SPL_SHIFT_W32(tmp32no1, 7 - normTmp - normTmp2);
// Q14
} else {
tmp32no1 = invLrtFX * (16384 - inst->priorNonSpeechProb);
// Q22
invLrtFX = tmp32no1 >> 8; // Q14.
}
tmp32no1 = (s32) inst->priorNonSpeechProb << 8; // Q22
nonSpeechProbFinal[i] = tmp32no1 /
(inst->priorNonSpeechProb + invLrtFX); // Q8
}
}
}
}
}
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_DEFINES_H_
#define MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_DEFINES_H_
#define ANAL_BLOCKL_MAX 128 /* Max analysis block length,8K采样率只需要128*/
#define HALF_ANAL_BLOCKL (ANAL_BLOCKL_MAX/2+1) /* Half max analysis block length + 1 */
//#define NUM_HIGH_BANDS_MAX 2 /* Max number of high bands */
#define SIMULT 3
#define END_STARTUP_LONG 200
#define END_STARTUP_SHORT 50
#define FACTOR_Q16 2621440 /* 40 in Q16 */
#define FACTOR_Q7 5120 /* 40 in Q7 */
#define FACTOR_Q7_STARTUP 1024 /* 8 in Q7 */
#define WIDTH_Q8 3 /* 0.01 in Q8 (or 25 ) */
/* PARAMETERS FOR NEW METHOD */
#define DD_PR_SNR_Q11 2007 /* ~= Q11(0.98) DD update of prior SNR */
#define ONE_MINUS_DD_PR_SNR_Q11 41 /* DD update of prior SNR */
#define SPECT_FLAT_TAVG_Q14 4915 /* (0.30) tavg parameter for spectral flatness measure */
#define SPECT_DIFF_TAVG_Q8 77 /* (0.30) tavg parameter for spectral flatness measure */
#define PRIOR_UPDATE_Q14 1638 /* Q14(0.1) Update parameter of prior model */
#define NOISE_UPDATE_Q8 26 /* 26 ~= Q8(0.1) Update parameter for noise */
/* Probability threshold for noise state in speech/noise likelihood. */
#define ONE_MINUS_PROB_RANGE_Q8 205 /* 205 ~= Q8(0.8) */
#define HIST_PAR_EST 1000 /* Histogram size for estimation of parameters */
/* FEATURE EXTRACTION CONFIG */
/* Bin size of histogram */
#define BIN_SIZE_LRT 10
/* Scale parameters: multiply dominant peaks of the histograms by scale factor to obtain. */
/* Thresholds for prior model */
#define FACTOR_1_LRT_DIFF 6 /* For LRT and spectral difference (5 times bigger) */
/* For spectral_flatness: used when noise is flatter than speech (10 times bigger). */
#define FACTOR_2_FLAT_Q10 922
/* Peak limit for spectral flatness (varies between 0 and 1) */
#define THRES_PEAK_FLAT 24 /* * 2 * BIN_SIZE_FLAT_FX */
/* Limit on spacing of two highest peaks in histogram: spacing determined by bin size. */
#define LIM_PEAK_SPACE_FLAT_DIFF 4 /* * 2 * BIN_SIZE_DIFF_FX */
/* Limit on relevance of second peak */
#define LIM_PEAK_WEIGHT_FLAT_DIFF 2
#define THRES_FLUCT_LRT 10240 /* = 20 * inst->modelUpdate; fluctuation limit of LRT feat. */
/* Limit on the max and min values for the feature thresholds */
#define MAX_FLAT_Q10 38912 /* * 2 * BIN_SIZE_FLAT_FX */
#define MIN_FLAT_Q10 4096 /* * 2 * BIN_SIZE_FLAT_FX */
#define MAX_DIFF 100 /* * 2 * BIN_SIZE_DIFF_FX */
#define MIN_DIFF 16 /* * 2 * BIN_SIZE_DIFF_FX */
/* Criteria of weight of histogram peak to accept/reject feature */
#define THRES_WEIGHT_FLAT_DIFF 154 /*(int)(0.3*(inst->modelUpdate)) for flatness and difference */
#define STAT_UPDATES 9 /* Update every 512 = 1 << 9 block */
#define ONE_MINUS_GAMMA_PAUSE_Q8 13 /* ~= Q8(0.05) Update for conservative noise estimate */
#define GAMMA_NOISE_TRANS_AND_SPEECH_Q8 3 /* ~= Q8(0.01) Update for transition and noise region */
//使用宏代替一些不会改的变量。只针对8k采样率有用
#define NR_BLOCKLEN10MS 80
#define NR_ANALEN 128
#define NR_ANALEN2 (NR_ANALEN / 2)
#define NR_MAGNLEN (NR_ANALEN2 + 1)
#define NR_STAGES 7
#define NR_MAXLRT 0x0040000
#define NR_MINLRT 52429
#define NR_MODELUPDATE (1 << STAT_UPDATES)
#define NR_HANNING_WINDOW kBlocks80w128x
//凤噪抑制等级参数
#define NSLEVEL 3 //0-3
#if NSLEVEL == 0
#define NR_GAINMAP 0
#define NR_OVERDRIVE 256
#define NR_DENOISEBOUND 8192
#elif NSLEVEL == 1
#define NR_GAINMAP 1
#define NR_OVERDRIVE 256
#define NR_DENOISEBOUND 4096
#define NR_FACTOR2TABLE kFactor2Aggressiveness1
#elif NSLEVEL == 2
#define NR_GAINMAP 1
#define NR_OVERDRIVE 282
#define NR_DENOISEBOUND 2048
#define NR_FACTOR2TABLE kFactor2Aggressiveness2
#elif NSLEVEL == 3
#define NR_GAINMAP 1
#define NR_OVERDRIVE 320
#define NR_DENOISEBOUND 1475
#define NR_FACTOR2TABLE kFactor2Aggressiveness3
#endif
// Skip first frequency bins during estimation. (0 <= value < 64)
#define NR_KSTARTBAND 5
#endif /* MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_DEFINES_H_ */
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This file contains platform-specific typedefs and defines.
// Much of it is derived from Chromium's build/build_config.h.
#ifndef TYPEDEFS_H_
#define TYPEDEFS_H_
#include <stdint.h>
typedef int16_t s16, S16;
typedef uint16_t u16, U16;
typedef int32_t s32, S32;
typedef uint32_t uint, u32, U32;
#define ALIGNED(n) __aligned(n)
#endif // TYPEDEFS_H_
@@ -0,0 +1,723 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "delay_estimator.h"
#include <stdlib.h>
#include <string.h>
#include "signal_processing_library.h"
// Number of right shifts for scaling is linearly depending on number of bits in
// the far-end binary spectrum.
static const int kShiftsAtZero = 13; // Right shifts at zero binary spectrum.
static const int kShiftsLinearSlope = 3;
static const int32_t kProbabilityOffset = 1024; // 2 in Q9.
static const int32_t kProbabilityLowerLimit = 8704; // 17 in Q9.
static const int32_t kProbabilityMinSpread = 2816; // 5.5 in Q9.
// Robust validation settings
static const float kHistogramMax = 3000.f;
static const float kLastHistogramMax = 250.f;
static const float kMinHistogramThreshold = 1.5f;
static const int kMinRequiredHits = 10;
static const int kMaxHitsWhenPossiblyNonCausal = 10;
static const int kMaxHitsWhenPossiblyCausal = 1000;
static const float kQ14Scaling = 1.f / (1 << 14); // Scaling by 2^14 to get Q0.
static const float kFractionSlope = 0.05f;
static const float kMinFractionWhenPossiblyCausal = 0.5f;
static const float kMinFractionWhenPossiblyNonCausal = 0.25f;
// Counts and returns number of bits of a 32-bit word.
static int BitCount(uint32_t u32) {
uint32_t tmp =
u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111);
tmp = ((tmp + (tmp >> 3)) & 030707070707);
tmp = (tmp + (tmp >> 6));
tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077;
return ((int) tmp);
}
// Compares the |binary_vector| with all rows of the |binary_matrix| and counts
// per row the number of times they have the same value.
//
// Inputs:
// - binary_vector : binary "vector" stored in a long
// - binary_matrix : binary "matrix" stored as a vector of long
// - matrix_size : size of binary "matrix"
//
// Output:
// - bit_counts : "Vector" stored as a long, containing for each
// row the number of times the matrix row and the
// input vector have the same value
//
static void BitCountComparison(uint32_t binary_vector,
const uint32_t *binary_matrix,
int matrix_size,
int32_t *bit_counts) {
int n = 0;
// Compare |binary_vector| with all rows of the |binary_matrix|
for (; n < matrix_size; n++) {
bit_counts[n] = (int32_t) BitCount(binary_vector ^ binary_matrix[n]);
}
}
// Collects necessary statistics for the HistogramBasedValidation(). This
// function has to be called prior to calling HistogramBasedValidation(). The
// statistics updated and used by the HistogramBasedValidation() are:
// 1. the number of |candidate_hits|, which states for how long we have had the
// same |candidate_delay|
// 2. the |histogram| of candidate delays over time. This histogram is
// weighted with respect to a reliability measure and time-varying to cope
// with possible delay shifts.
// For further description see commented code.
//
// Inputs:
// - candidate_delay : The delay to validate.
// - valley_depth_q14 : The cost function has a valley/minimum at the
// |candidate_delay| location. |valley_depth_q14| is the
// cost function difference between the minimum and
// maximum locations. The value is in the Q14 domain.
// - valley_level_q14 : Is the cost function value at the minimum, in Q14.
static void UpdateRobustValidationStatistics(BinaryDelayEstimator *self,
int candidate_delay,
int32_t valley_depth_q14,
int32_t valley_level_q14) {
const float valley_depth = valley_depth_q14 * kQ14Scaling;
float decrease_in_last_set = valley_depth;
const int max_hits_for_slow_change = (candidate_delay < self->last_delay)
? kMaxHitsWhenPossiblyNonCausal
: kMaxHitsWhenPossiblyCausal;
int i = 0;
RTC_DCHECK_EQ(self->history_size, self->farend->history_size);
// Reset |candidate_hits| if we have a new candidate.
if (candidate_delay != self->last_candidate_delay) {
self->candidate_hits = 0;
self->last_candidate_delay = candidate_delay;
}
self->candidate_hits++;
// The |histogram| is updated differently across the bins.
// 1. The |candidate_delay| histogram bin is increased with the
// |valley_depth|, which is a simple measure of how reliable the
// |candidate_delay| is. The histogram is not increased above
// |kHistogramMax|.
self->histogram[candidate_delay] += valley_depth;
if (self->histogram[candidate_delay] > kHistogramMax) {
self->histogram[candidate_delay] = kHistogramMax;
}
// 2. The histogram bins in the neighborhood of |candidate_delay| are
// unaffected. The neighborhood is defined as x + {-2, -1, 0, 1}.
// 3. The histogram bins in the neighborhood of |last_delay| are decreased
// with |decrease_in_last_set|. This value equals the difference between
// the cost function values at the locations |candidate_delay| and
// |last_delay| until we reach |max_hits_for_slow_change| consecutive hits
// at the |candidate_delay|. If we exceed this amount of hits the
// |candidate_delay| is a "potential" candidate and we start decreasing
// these histogram bins more rapidly with |valley_depth|.
if (self->candidate_hits < max_hits_for_slow_change) {
decrease_in_last_set =
(self->mean_bit_counts[self->compare_delay] - valley_level_q14) *
kQ14Scaling;
}
// 4. All other bins are decreased with |valley_depth|.
// TODO(bjornv): Investigate how to make this loop more efficient. Split up
// the loop? Remove parts that doesn't add too much.
for (i = 0; i < self->history_size; ++i) {
int is_in_last_set = (i >= self->last_delay - 2) &&
(i <= self->last_delay + 1) && (i != candidate_delay);
int is_in_candidate_set =
(i >= candidate_delay - 2) && (i <= candidate_delay + 1);
self->histogram[i] -=
decrease_in_last_set * is_in_last_set +
valley_depth * (!is_in_last_set && !is_in_candidate_set);
// 5. No histogram bin can go below 0.
if (self->histogram[i] < 0) {
self->histogram[i] = 0;
}
}
}
// Validates the |candidate_delay|, estimated in WebRtc_ProcessBinarySpectrum(),
// based on a mix of counting concurring hits with a modified histogram
// of recent delay estimates. In brief a candidate is valid (returns 1) if it
// is the most likely according to the histogram. There are a couple of
// exceptions that are worth mentioning:
// 1. If the |candidate_delay| < |last_delay| it can be that we are in a
// non-causal state, breaking a possible echo control algorithm. Hence, we
// open up for a quicker change by allowing the change even if the
// |candidate_delay| is not the most likely one according to the histogram.
// 2. There's a minimum number of hits (kMinRequiredHits) and the histogram
// value has to reached a minimum (kMinHistogramThreshold) to be valid.
// 3. The action is also depending on the filter length used for echo control.
// If the delay difference is larger than what the filter can capture, we
// also move quicker towards a change.
// For further description see commented code.
//
// Input:
// - candidate_delay : The delay to validate.
//
// Return value:
// - is_histogram_valid : 1 - The |candidate_delay| is valid.
// 0 - Otherwise.
static int HistogramBasedValidation(const BinaryDelayEstimator *self,
int candidate_delay) {
float fraction = 1.f;
float histogram_threshold = self->histogram[self->compare_delay];
const int delay_difference = candidate_delay - self->last_delay;
int is_histogram_valid = 0;
// The histogram based validation of |candidate_delay| is done by comparing
// the |histogram| at bin |candidate_delay| with a |histogram_threshold|.
// This |histogram_threshold| equals a |fraction| of the |histogram| at bin
// |last_delay|. The |fraction| is a piecewise linear function of the
// |delay_difference| between the |candidate_delay| and the |last_delay|
// allowing for a quicker move if
// i) a potential echo control filter can not handle these large differences.
// ii) keeping |last_delay| instead of updating to |candidate_delay| could
// force an echo control into a non-causal state.
// We further require the histogram to have reached a minimum value of
// |kMinHistogramThreshold|. In addition, we also require the number of
// |candidate_hits| to be more than |kMinRequiredHits| to remove spurious
// values.
// Calculate a comparison histogram value (|histogram_threshold|) that is
// depending on the distance between the |candidate_delay| and |last_delay|.
// TODO(bjornv): How much can we gain by turning the fraction calculation
// into tables?
if (delay_difference > self->allowed_offset) {
fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset);
fraction = (fraction > kMinFractionWhenPossiblyCausal
? fraction
: kMinFractionWhenPossiblyCausal);
} else if (delay_difference < 0) {
fraction =
kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference;
fraction = (fraction > 1.f ? 1.f : fraction);
}
histogram_threshold *= fraction;
histogram_threshold =
(histogram_threshold > kMinHistogramThreshold ? histogram_threshold
: kMinHistogramThreshold);
is_histogram_valid =
(self->histogram[candidate_delay] >= histogram_threshold) &&
(self->candidate_hits > kMinRequiredHits);
return is_histogram_valid;
}
// Performs a robust validation of the |candidate_delay| estimated in
// WebRtc_ProcessBinarySpectrum(). The algorithm takes the
// |is_instantaneous_valid| and the |is_histogram_valid| and combines them
// into a robust validation. The HistogramBasedValidation() has to be called
// prior to this call.
// For further description on how the combination is done, see commented code.
//
// Inputs:
// - candidate_delay : The delay to validate.
// - is_instantaneous_valid : The instantaneous validation performed in
// WebRtc_ProcessBinarySpectrum().
// - is_histogram_valid : The histogram based validation.
//
// Return value:
// - is_robust : 1 - The candidate_delay is valid according to a
// combination of the two inputs.
// : 0 - Otherwise.
static int RobustValidation(const BinaryDelayEstimator *self,
int candidate_delay,
int is_instantaneous_valid,
int is_histogram_valid) {
int is_robust = 0;
// The final robust validation is based on the two algorithms; 1) the
// |is_instantaneous_valid| and 2) the histogram based with result stored in
// |is_histogram_valid|.
// i) Before we actually have a valid estimate (|last_delay| == -2), we say
// a candidate is valid if either algorithm states so
// (|is_instantaneous_valid| OR |is_histogram_valid|).
is_robust =
(self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid);
// ii) Otherwise, we need both algorithms to be certain
// (|is_instantaneous_valid| AND |is_histogram_valid|)
is_robust |= is_instantaneous_valid && is_histogram_valid;
// iii) With one exception, i.e., the histogram based algorithm can overrule
// the instantaneous one if |is_histogram_valid| = 1 and the histogram
// is significantly strong.
is_robust |= is_histogram_valid &&
(self->histogram[candidate_delay] > self->last_delay_histogram);
return is_robust;
}
void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self) {
if (self == NULL) {
return;
}
webrtc_free(self->binary_far_history);
self->binary_far_history = NULL;
webrtc_free(self->far_bit_counts);
self->far_bit_counts = NULL;
webrtc_free(self);
}
BinaryDelayEstimatorFarend *WebRtc_CreateBinaryDelayEstimatorFarend(
int history_size) {
BinaryDelayEstimatorFarend *self = NULL;
if (history_size > 1) {
// Sanity conditions fulfilled.
self = (BinaryDelayEstimatorFarend *)(
webrtc_malloc(sizeof(BinaryDelayEstimatorFarend)));
}
if (self == NULL) {
return NULL;
}
self->history_size = 0;
self->binary_far_history = NULL;
self->far_bit_counts = NULL;
if (WebRtc_AllocateFarendBufferMemory(self, history_size) == 0) {
WebRtc_FreeBinaryDelayEstimatorFarend(self);
self = NULL;
}
return self;
}
int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend *self,
int history_size) {
RTC_DCHECK(self);
// (Re-)Allocate memory for history buffers.
self->binary_far_history = (uint32_t *)(
realloc(self->binary_far_history,
history_size * sizeof(*self->binary_far_history)));
self->far_bit_counts = (int *)(realloc(
self->far_bit_counts, history_size * sizeof(*self->far_bit_counts)));
if ((self->binary_far_history == NULL) || (self->far_bit_counts == NULL)) {
history_size = 0;
}
// Fill with zeros if we have expanded the buffers.
if (history_size > self->history_size) {
int size_diff = history_size - self->history_size;
memset(&self->binary_far_history[self->history_size], 0,
sizeof(*self->binary_far_history) * size_diff);
memset(&self->far_bit_counts[self->history_size], 0,
sizeof(*self->far_bit_counts) * size_diff);
}
self->history_size = history_size;
return self->history_size;
}
void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self) {
RTC_DCHECK(self);
memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
}
void WebRtc_SoftResetBinaryDelayEstimatorFarend(
BinaryDelayEstimatorFarend *self,
int delay_shift) {
int abs_shift = abs(delay_shift);
int shift_size = 0;
int dest_index = 0;
int src_index = 0;
int padding_index = 0;
RTC_DCHECK(self);
shift_size = self->history_size - abs_shift;
RTC_DCHECK_GT(shift_size, 0);
if (delay_shift == 0) {
return;
} else if (delay_shift > 0) {
dest_index = abs_shift;
} else if (delay_shift < 0) {
src_index = abs_shift;
padding_index = shift_size;
}
// Shift and zero pad buffers.
memmove(&self->binary_far_history[dest_index],
&self->binary_far_history[src_index],
sizeof(*self->binary_far_history) * shift_size);
memset(&self->binary_far_history[padding_index], 0,
sizeof(*self->binary_far_history) * abs_shift);
memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
sizeof(*self->far_bit_counts) * shift_size);
memset(&self->far_bit_counts[padding_index], 0,
sizeof(*self->far_bit_counts) * abs_shift);
}
void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend *handle,
uint32_t binary_far_spectrum) {
RTC_DCHECK(handle);
// Shift binary spectrum history and insert current |binary_far_spectrum|.
memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
(handle->history_size - 1) * sizeof(uint32_t));
handle->binary_far_history[0] = binary_far_spectrum;
// Shift history of far-end binary spectrum bit counts and insert bit count
// of current |binary_far_spectrum|.
memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
(handle->history_size - 1) * sizeof(int));
handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
}
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator *self) {
if (self == NULL) {
return;
}
webrtc_free(self->mean_bit_counts);
self->mean_bit_counts = NULL;
webrtc_free(self->bit_counts);
self->bit_counts = NULL;
webrtc_free(self->binary_near_history);
self->binary_near_history = NULL;
webrtc_free(self->histogram);
self->histogram = NULL;
// BinaryDelayEstimator does not have ownership of |farend|, hence we do not
// free the memory here. That should be handled separately by the user.
self->farend = NULL;
webrtc_free(self);
}
BinaryDelayEstimator *WebRtc_CreateBinaryDelayEstimator(
BinaryDelayEstimatorFarend *farend,
int max_lookahead) {
BinaryDelayEstimator *self = NULL;
if ((farend != NULL) && (max_lookahead >= 0)) {
// Sanity conditions fulfilled.
self = (BinaryDelayEstimator *)(
webrtc_malloc(sizeof(BinaryDelayEstimator)));
}
if (self == NULL) {
return NULL;
}
self->farend = farend;
self->near_history_size = max_lookahead + 1;
self->history_size = 0;
self->robust_validation_enabled = 0; // Disabled by default.
self->allowed_offset = 0;
self->lookahead = max_lookahead;
// Allocate memory for spectrum and history buffers.
self->mean_bit_counts = NULL;
self->bit_counts = NULL;
self->histogram = NULL;
self->binary_near_history = (uint32_t *)(
webrtc_malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
if (self->binary_near_history == NULL ||
WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
WebRtc_FreeBinaryDelayEstimator(self);
self = NULL;
}
return self;
}
int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator *self,
int history_size) {
BinaryDelayEstimatorFarend *far = self->farend;
// (Re-)Allocate memory for spectrum and history buffers.
if (history_size != far->history_size) {
// Only update far-end buffers if we need.
history_size = WebRtc_AllocateFarendBufferMemory(far, history_size);
}
// The extra array element in |mean_bit_counts| and |histogram| is a dummy
// element only used while |last_delay| == -2, i.e., before we have a valid
// estimate.
self->mean_bit_counts = (int32_t *)(
realloc(self->mean_bit_counts,
(history_size + 1) * sizeof(*self->mean_bit_counts)));
self->bit_counts = (int32_t *)(
realloc(self->bit_counts, history_size * sizeof(*self->bit_counts)));
self->histogram = (float *)(
realloc(self->histogram, (history_size + 1) * sizeof(*self->histogram)));
if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) ||
(self->histogram == NULL)) {
history_size = 0;
}
// Fill with zeros if we have expanded the buffers.
if (history_size > self->history_size) {
int size_diff = history_size - self->history_size;
memset(&self->mean_bit_counts[self->history_size], 0,
sizeof(*self->mean_bit_counts) * size_diff);
memset(&self->bit_counts[self->history_size], 0,
sizeof(*self->bit_counts) * size_diff);
memset(&self->histogram[self->history_size], 0,
sizeof(*self->histogram) * size_diff);
}
self->history_size = history_size;
return self->history_size;
}
void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator *self) {
int i = 0;
RTC_DCHECK(self);
memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
memset(self->binary_near_history, 0,
sizeof(uint32_t) * self->near_history_size);
for (i = 0; i <= self->history_size; ++i) {
self->mean_bit_counts[i] = (20 << 9); // 20 in Q9.
self->histogram[i] = 0.f;
}
self->minimum_probability = kMaxBitCountsQ9; // 32 in Q9.
self->last_delay_probability = (int) kMaxBitCountsQ9; // 32 in Q9.
// Default return value if we're unable to estimate. -1 is used for errors.
self->last_delay = -2;
self->last_candidate_delay = -2;
self->compare_delay = self->history_size;
self->candidate_hits = 0;
self->last_delay_histogram = 0.f;
}
int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator *self,
int delay_shift) {
int lookahead = 0;
RTC_DCHECK(self);
lookahead = self->lookahead;
self->lookahead -= delay_shift;
if (self->lookahead < 0) {
self->lookahead = 0;
}
if (self->lookahead > self->near_history_size - 1) {
self->lookahead = self->near_history_size - 1;
}
return lookahead - self->lookahead;
}
//模拟c++的std::any_of
#if 1
int any_of(int arr[], int size, int (*predicate)(int)) {
for (int i = 0; i < size; i++) {
if (predicate(arr[i])) {
return 1;
}
}
return 0;
}
#endif
int ret_predicate(int a)
{
return a > 0;
}
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator *self,
uint32_t binary_near_spectrum) {
int i = 0;
int candidate_delay = -1;
int valid_candidate = 0;
int32_t value_best_candidate = kMaxBitCountsQ9;
int32_t value_worst_candidate = 0;
int32_t valley_depth = 0;
RTC_DCHECK(self);
if (self->farend->history_size != self->history_size) {
// Non matching history sizes.
return -1;
}
if (self->near_history_size > 1) {
// If we apply lookahead, shift near-end binary spectrum history. Insert
// current |binary_near_spectrum| and pull out the delayed one.
memmove(&(self->binary_near_history[1]), &(self->binary_near_history[0]),
(self->near_history_size - 1) * sizeof(uint32_t));
self->binary_near_history[0] = binary_near_spectrum;
binary_near_spectrum = self->binary_near_history[self->lookahead];
}
// Compare with delayed spectra and store the |bit_counts| for each delay.
BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
self->history_size, self->bit_counts);
// Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
for (i = 0; i < self->history_size; i++) {
// |bit_counts| is constrained to [0, 32], meaning we can smooth with a
// factor up to 2^26. We use Q9.
int32_t bit_count = (self->bit_counts[i] << 9); // Q9.
// Update |mean_bit_counts| only when far-end signal has something to
// contribute. If |far_bit_counts| is zero the far-end signal is weak and
// we likely have a poor echo condition, hence don't update.
if (self->farend->far_bit_counts[i] > 0) {
// Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
int shifts = kShiftsAtZero;
shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
}
}
// Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
// of |mean_bit_counts|.
for (i = 0; i < self->history_size; i++) {
if (self->mean_bit_counts[i] < value_best_candidate) {
value_best_candidate = self->mean_bit_counts[i];
candidate_delay = i;
}
if (self->mean_bit_counts[i] > value_worst_candidate) {
value_worst_candidate = self->mean_bit_counts[i];
}
}
valley_depth = value_worst_candidate - value_best_candidate;
// The |value_best_candidate| is a good indicator on the probability of
// |candidate_delay| being an accurate delay (a small |value_best_candidate|
// means a good binary match). In the following sections we make a decision
// whether to update |last_delay| or not.
// 1) If the difference bit counts between the best and the worst delay
// candidates is too small we consider the situation to be unreliable and
// don't update |last_delay|.
// 2) If the situation is reliable we update |last_delay| if the value of the
// best candidate delay has a value less than
// i) an adaptive threshold |minimum_probability|, or
// ii) this corresponding value |last_delay_probability|, but updated at
// this time instant.
// Update |minimum_probability|.
if ((self->minimum_probability > kProbabilityLowerLimit) &&
(valley_depth > kProbabilityMinSpread)) {
// The "hard" threshold can't be lower than 17 (in Q9).
// The valley in the curve also has to be distinct, i.e., the
// difference between |value_worst_candidate| and |value_best_candidate| has
// to be large enough.
int32_t threshold = value_best_candidate + kProbabilityOffset;
if (threshold < kProbabilityLowerLimit) {
threshold = kProbabilityLowerLimit;
}
if (self->minimum_probability > threshold) {
self->minimum_probability = threshold;
}
}
// Update |last_delay_probability|.
// We use a Markov type model, i.e., a slowly increasing level over time.
self->last_delay_probability++;
// Validate |candidate_delay|. We have a reliable instantaneous delay
// estimate if
// 1) The valley is distinct enough (|valley_depth| > |kProbabilityOffset|)
// and
// 2) The depth of the valley is deep enough
// (|value_best_candidate| < |minimum_probability|)
// and deeper than the best estimate so far
// (|value_best_candidate| < |last_delay_probability|)
valid_candidate = ((valley_depth > kProbabilityOffset) &&
((value_best_candidate < self->minimum_probability) ||
(value_best_candidate < self->last_delay_probability)));
// Check for nonstationary farend signal.
#if 0
const bool non_stationary_farend =
std::any_of(self->farend->far_bit_counts,
self->farend->far_bit_counts + self->history_size,
[](int a) { return a > 0; });
#else
const int non_stationary_farend = any_of(self->farend->far_bit_counts,self->history_size,ret_predicate);
#endif
if (non_stationary_farend) {
// Only update the validation statistics when the farend is nonstationary
// as the underlying estimates are otherwise frozen.
UpdateRobustValidationStatistics(self, candidate_delay, valley_depth,
value_best_candidate);
}
if (self->robust_validation_enabled) {
int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
is_histogram_valid);
}
// Only update the delay estimate when the farend is nonstationary and when
// a valid delay candidate is available.
if (non_stationary_farend && valid_candidate) {
if (candidate_delay != self->last_delay) {
self->last_delay_histogram =
(self->histogram[candidate_delay] > kLastHistogramMax
? kLastHistogramMax
: self->histogram[candidate_delay]);
// Adjust the histogram if we made a change to |last_delay|, though it was
// not the most likely one according to the histogram.
if (self->histogram[candidate_delay] <
self->histogram[self->compare_delay]) {
self->histogram[self->compare_delay] = self->histogram[candidate_delay];
}
}
self->last_delay = candidate_delay;
if (value_best_candidate < self->last_delay_probability) {
self->last_delay_probability = value_best_candidate;
}
self->compare_delay = self->last_delay;
}
return self->last_delay;
}
int WebRtc_binary_last_delay(BinaryDelayEstimator *self) {
RTC_DCHECK(self);
return self->last_delay;
}
float WebRtc_binary_last_delay_quality(BinaryDelayEstimator *self) {
float quality = 0;
RTC_DCHECK(self);
if (self->robust_validation_enabled) {
// Simply a linear function of the histogram height at delay estimate.
quality = self->histogram[self->compare_delay] / kHistogramMax;
} else {
// Note that |last_delay_probability| states how deep the minimum of the
// cost function is, so it is rather an error probability.
quality = (float) (kMaxBitCountsQ9 - self->last_delay_probability) /
kMaxBitCountsQ9;
if (quality < 0) {
quality = 0;
}
}
return quality;
}
void WebRtc_MeanEstimatorFix(int32_t new_value,
int factor,
int32_t *mean_value) {
int32_t diff = new_value - *mean_value;
// mean_new = mean_value + ((new_value - mean_value) >> factor);
if (diff < 0) {
diff = -((-diff) >> factor);
} else {
diff = (diff >> factor);
}
*mean_value += diff;
}
@@ -0,0 +1,256 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Performs delay estimation on binary converted spectra.
// The return value is 0 - OK and -1 - Error, unless otherwise stated.
#ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
#define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
#include <stdint.h>
#include "rtc_base/checks.h"
static const int32_t kMaxBitCountsQ9 = (32 << 9); // 32 matching bits in Q9.
typedef struct {
// Pointer to bit counts.
int *far_bit_counts;
// Binary history variables.
uint32_t *binary_far_history;
int history_size;
} BinaryDelayEstimatorFarend;
typedef struct {
// Pointer to bit counts.
int32_t *mean_bit_counts;
// Array only used locally in ProcessBinarySpectrum() but whose size is
// determined at run-time.
int32_t *bit_counts;
// Binary history variables.
uint32_t *binary_near_history;
int near_history_size;
int history_size;
// Delay estimation variables.
int32_t minimum_probability;
int last_delay_probability;
// Delay memory.
int last_delay;
// Robust validation
int robust_validation_enabled;
int allowed_offset;
int last_candidate_delay;
int compare_delay;
int candidate_hits;
float *histogram;
float last_delay_histogram;
// For dynamically changing the lookahead when using SoftReset...().
int lookahead;
// Far-end binary spectrum history buffer etc.
BinaryDelayEstimatorFarend *farend;
} BinaryDelayEstimator;
// Releases the memory allocated by
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
// Input:
// - self : Pointer to the binary delay estimation far-end
// instance which is the return value of
// WebRtc_CreateBinaryDelayEstimatorFarend().
//
void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self);
// Allocates the memory needed by the far-end part of the binary delay
// estimation. The memory needs to be initialized separately through
// WebRtc_InitBinaryDelayEstimatorFarend(...).
//
// Inputs:
// - history_size : Size of the far-end binary spectrum history.
//
// Return value:
// - BinaryDelayEstimatorFarend*
// : Created |handle|. If the memory can't be allocated
// or if any of the input parameters are invalid NULL
// is returned.
//
BinaryDelayEstimatorFarend *WebRtc_CreateBinaryDelayEstimatorFarend(
int history_size);
// Re-allocates the buffers.
//
// Inputs:
// - self : Pointer to the binary estimation far-end instance
// which is the return value of
// WebRtc_CreateBinaryDelayEstimatorFarend().
// - history_size : Size of the far-end binary spectrum history.
//
// Return value:
// - history_size : The history size allocated.
int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend *self,
int history_size);
// Initializes the delay estimation far-end instance created with
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
//
// Input:
// - self : Pointer to the delay estimation far-end instance.
//
// Output:
// - self : Initialized far-end instance.
//
void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self);
// Soft resets the delay estimation far-end instance created with
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
//
// Input:
// - delay_shift : The amount of blocks to shift history buffers.
//
void WebRtc_SoftResetBinaryDelayEstimatorFarend(
BinaryDelayEstimatorFarend *self,
int delay_shift);
// Adds the binary far-end spectrum to the internal far-end history buffer. This
// spectrum is used as reference when calculating the delay using
// WebRtc_ProcessBinarySpectrum().
//
// Inputs:
// - self : Pointer to the delay estimation far-end
// instance.
// - binary_far_spectrum : Far-end binary spectrum.
//
// Output:
// - self : Updated far-end instance.
//
void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend *self,
uint32_t binary_far_spectrum);
// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
//
// Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
// not take ownership of it, hence the BinaryDelayEstimator has to be torn down
// before the far-end.
//
// Input:
// - self : Pointer to the binary delay estimation instance
// which is the return value of
// WebRtc_CreateBinaryDelayEstimator().
//
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator *self);
// Allocates the memory needed by the binary delay estimation. The memory needs
// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
//
// See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
// description.
BinaryDelayEstimator *WebRtc_CreateBinaryDelayEstimator(
BinaryDelayEstimatorFarend *farend,
int max_lookahead);
// Re-allocates |history_size| dependent buffers. The far-end buffers will be
// updated at the same time if needed.
//
// Input:
// - self : Pointer to the binary estimation instance which is
// the return value of
// WebRtc_CreateBinaryDelayEstimator().
// - history_size : Size of the history buffers.
//
// Return value:
// - history_size : The history size allocated.
int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator *self,
int history_size);
// Initializes the delay estimation instance created with
// WebRtc_CreateBinaryDelayEstimator(...).
//
// Input:
// - self : Pointer to the delay estimation instance.
//
// Output:
// - self : Initialized instance.
//
void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator *self);
// Soft resets the delay estimation instance created with
// WebRtc_CreateBinaryDelayEstimator(...).
//
// Input:
// - delay_shift : The amount of blocks to shift history buffers.
//
// Return value:
// - actual_shifts : The actual number of shifts performed.
//
int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator *self,
int delay_shift);
// Estimates and returns the delay between the binary far-end and binary near-
// end spectra. It is assumed the binary far-end spectrum has been added using
// WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
// the lookahead (i.e. the lookahead should be subtracted from the returned
// value).
//
// Inputs:
// - self : Pointer to the delay estimation instance.
// - binary_near_spectrum : Near-end binary spectrum of the current block.
//
// Output:
// - self : Updated instance.
//
// Return value:
// - delay : >= 0 - Calculated delay value.
// -2 - Insufficient data for estimation.
//
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator *self,
uint32_t binary_near_spectrum);
// Returns the last calculated delay updated by the function
// WebRtc_ProcessBinarySpectrum(...).
//
// Input:
// - self : Pointer to the delay estimation instance.
//
// Return value:
// - delay : >= 0 - Last calculated delay value
// -2 - Insufficient data for estimation.
//
int WebRtc_binary_last_delay(BinaryDelayEstimator *self);
// Returns the estimation quality of the last calculated delay updated by the
// function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
// in the interval [0, 1]. The higher the value, the better the quality.
//
// Return value:
// - delay_quality : >= 0 - Estimation quality of last calculated
// delay value.
float WebRtc_binary_last_delay_quality(BinaryDelayEstimator *self);
// Updates the |mean_value| recursively with a step size of 2^-|factor|. This
// function is used internally in the Binary Delay Estimator as well as the
// Fixed point wrapper.
//
// Inputs:
// - new_value : The new value the mean should be updated with.
// - factor : The step size, in number of right shifts.
//
// Input/Output:
// - mean_value : Pointer to the mean value.
//
void WebRtc_MeanEstimatorFix(int32_t new_value,
int factor,
int32_t *mean_value);
#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
@@ -0,0 +1,517 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "delay_estimator_wrapper.h"
#include <stdlib.h>
#include <string.h>
#include "delay_estimator.h"
#include "signal_processing_library.h"
typedef union {
float float_;
int32_t int32_;
} SpectrumType;
typedef struct {
// Pointers to mean values of spectrum.
SpectrumType *mean_far_spectrum;
// |mean_far_spectrum| initialization indicator.
int far_spectrum_initialized;
int spectrum_size;
// Far-end part of binary spectrum based delay estimation.
BinaryDelayEstimatorFarend *binary_farend;
} DelayEstimatorFarend;
typedef struct {
// Pointers to mean values of spectrum.
SpectrumType *mean_near_spectrum;
// |mean_near_spectrum| initialization indicator.
int near_spectrum_initialized;
int spectrum_size;
// Binary spectrum based delay estimator
BinaryDelayEstimator *binary_handle;
} DelayEstimator;
// Only bit |kBandFirst| through bit |kBandLast| are processed and
// |kBandFirst| - |kBandLast| must be < 32.
enum {
kBandFirst = 12
};
enum {
kBandLast = 43
};
static __inline uint32_t SetBit(uint32_t in, int pos) {
uint32_t mask = (1 << pos);
uint32_t out = (in | mask);
return out;
}
// Calculates the mean recursively. Same version as WebRtc_MeanEstimatorFix(),
// but for float.
//
// Inputs:
// - new_value : New additional value.
// - scale : Scale for smoothing (should be less than 1.0).
//
// Input/Output:
// - mean_value : Pointer to the mean value for updating.
//
static void MeanEstimatorFloat(float new_value,
float scale,
float *mean_value) {
RTC_DCHECK_LT(scale, 1.0f);
*mean_value += (new_value - *mean_value) * scale;
}
// Computes the binary spectrum by comparing the input |spectrum| with a
// |threshold_spectrum|. Float and fixed point versions.
//
// Inputs:
// - spectrum : Spectrum of which the binary spectrum should be
// calculated.
// - threshold_spectrum : Threshold spectrum with which the input
// spectrum is compared.
// Return:
// - out : Binary spectrum.
//
static uint32_t BinarySpectrumFix(const uint16_t *spectrum,
SpectrumType *threshold_spectrum,
int q_domain,
int *threshold_initialized) {
int i = kBandFirst;
uint32_t out = 0;
RTC_DCHECK_LT(q_domain, 16);
if (!(*threshold_initialized)) {
// Set the |threshold_spectrum| to half the input |spectrum| as starting
// value. This speeds up the convergence.
for (i = kBandFirst; i <= kBandLast; i++) {
if (spectrum[i] > 0) {
// Convert input spectrum from Q(|q_domain|) to Q15.
int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain);
threshold_spectrum[i].int32_ = (spectrum_q15 >> 1);
*threshold_initialized = 1;
}
}
}
for (i = kBandFirst; i <= kBandLast; i++) {
// Convert input spectrum from Q(|q_domain|) to Q15.
int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain);
// Update the |threshold_spectrum|.
WebRtc_MeanEstimatorFix(spectrum_q15, 6, &(threshold_spectrum[i].int32_));
// Convert |spectrum| at current frequency bin to a binary value.
if (spectrum_q15 > threshold_spectrum[i].int32_) {
out = SetBit(out, i - kBandFirst);
}
}
return out;
}
static uint32_t BinarySpectrumFloat(const float *spectrum,
SpectrumType *threshold_spectrum,
int *threshold_initialized) {
int i = kBandFirst;
uint32_t out = 0;
const float kScale = 1 / 64.0;
if (!(*threshold_initialized)) {
// Set the |threshold_spectrum| to half the input |spectrum| as starting
// value. This speeds up the convergence.
for (i = kBandFirst; i <= kBandLast; i++) {
if (spectrum[i] > 0.0f) {
threshold_spectrum[i].float_ = (spectrum[i] / 2);
*threshold_initialized = 1;
}
}
}
for (i = kBandFirst; i <= kBandLast; i++) {
// Update the |threshold_spectrum|.
MeanEstimatorFloat(spectrum[i], kScale, &(threshold_spectrum[i].float_));
// Convert |spectrum| at current frequency bin to a binary value.
if (spectrum[i] > threshold_spectrum[i].float_) {
out = SetBit(out, i - kBandFirst);
}
}
return out;
}
void WebRtc_FreeDelayEstimatorFarend(void *handle) {
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
if (handle == NULL) {
return;
}
webrtc_free(self->mean_far_spectrum);
self->mean_far_spectrum = NULL;
WebRtc_FreeBinaryDelayEstimatorFarend(self->binary_farend);
self->binary_farend = NULL;
webrtc_free(self);
}
void *WebRtc_CreateDelayEstimatorFarend(int spectrum_size, int history_size) {
DelayEstimatorFarend *self = NULL;
// Check if the sub band used in the delay estimation is small enough to fit
// the binary spectra in a uint32_t.
//assert(kBandLast - kBandFirst < 32, "");
if (spectrum_size >= kBandLast) {
self = (DelayEstimatorFarend *)(
webrtc_malloc(sizeof(DelayEstimatorFarend)));
}
if (self != NULL) {
int memory_fail = 0;
// Allocate memory for the binary far-end spectrum handling.
self->binary_farend = WebRtc_CreateBinaryDelayEstimatorFarend(history_size);
memory_fail |= (self->binary_farend == NULL);
// Allocate memory for spectrum buffers.
self->mean_far_spectrum = (SpectrumType *)(
webrtc_malloc(spectrum_size * sizeof(SpectrumType)));
memory_fail |= (self->mean_far_spectrum == NULL);
self->spectrum_size = spectrum_size;
if (memory_fail) {
WebRtc_FreeDelayEstimatorFarend(self);
self = NULL;
}
}
return self;
}
int WebRtc_InitDelayEstimatorFarend(void *handle) {
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
if (self == NULL) {
return -1;
}
// Initialize far-end part of binary delay estimator.
WebRtc_InitBinaryDelayEstimatorFarend(self->binary_farend);
// Set averaged far and near end spectra to zero.
memset(self->mean_far_spectrum, 0,
sizeof(SpectrumType) * self->spectrum_size);
// Reset initialization indicators.
self->far_spectrum_initialized = 0;
return 0;
}
void WebRtc_SoftResetDelayEstimatorFarend(void *handle, int delay_shift) {
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
RTC_DCHECK(self);
WebRtc_SoftResetBinaryDelayEstimatorFarend(self->binary_farend, delay_shift);
}
int WebRtc_AddFarSpectrumFix(void *handle,
const uint16_t *far_spectrum,
int spectrum_size,
int far_q) {
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
uint32_t binary_spectrum = 0;
if (self == NULL) {
return -1;
}
if (far_spectrum == NULL) {
// Empty far end spectrum.
return -1;
}
if (spectrum_size != self->spectrum_size) {
// Data sizes don't match.
return -1;
}
if (far_q > 15) {
// If |far_q| is larger than 15 we cannot guarantee no wrap around.
return -1;
}
// Get binary spectrum.
binary_spectrum = BinarySpectrumFix(far_spectrum, self->mean_far_spectrum,
far_q, &(self->far_spectrum_initialized));
WebRtc_AddBinaryFarSpectrum(self->binary_farend, binary_spectrum);
return 0;
}
int WebRtc_AddFarSpectrumFloat(void *handle,
const float *far_spectrum,
int spectrum_size) {
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
uint32_t binary_spectrum = 0;
if (self == NULL) {
return -1;
}
if (far_spectrum == NULL) {
// Empty far end spectrum.
return -1;
}
if (spectrum_size != self->spectrum_size) {
// Data sizes don't match.
return -1;
}
// Get binary spectrum.
binary_spectrum = BinarySpectrumFloat(far_spectrum, self->mean_far_spectrum,
&(self->far_spectrum_initialized));
WebRtc_AddBinaryFarSpectrum(self->binary_farend, binary_spectrum);
return 0;
}
void WebRtc_FreeDelayEstimator(void *handle) {
DelayEstimator *self = (DelayEstimator *) handle;
if (handle == NULL) {
return;
}
webrtc_free(self->mean_near_spectrum);
self->mean_near_spectrum = NULL;
WebRtc_FreeBinaryDelayEstimator(self->binary_handle);
self->binary_handle = NULL;
webrtc_free(self);
}
void *WebRtc_CreateDelayEstimator(void *farend_handle, int max_lookahead) {
DelayEstimator *self = NULL;
DelayEstimatorFarend *farend = (DelayEstimatorFarend *) farend_handle;
if (farend_handle != NULL) {
self = (DelayEstimator *)(webrtc_malloc(sizeof(DelayEstimator)));
}
if (self != NULL) {
int memory_fail = 0;
// Allocate memory for the farend spectrum handling.
self->binary_handle =
WebRtc_CreateBinaryDelayEstimator(farend->binary_farend, max_lookahead);
memory_fail |= (self->binary_handle == NULL);
// Allocate memory for spectrum buffers.
self->mean_near_spectrum = (SpectrumType *)(
webrtc_malloc(farend->spectrum_size * sizeof(SpectrumType)));
memory_fail |= (self->mean_near_spectrum == NULL);
self->spectrum_size = farend->spectrum_size;
if (memory_fail) {
WebRtc_FreeDelayEstimator(self);
self = NULL;
}
}
return self;
}
int WebRtc_InitDelayEstimator(void *handle) {
DelayEstimator *self = (DelayEstimator *) handle;
if (self == NULL) {
return -1;
}
// Initialize binary delay estimator.
WebRtc_InitBinaryDelayEstimator(self->binary_handle);
// Set averaged far and near end spectra to zero.
memset(self->mean_near_spectrum, 0,
sizeof(SpectrumType) * self->spectrum_size);
// Reset initialization indicators.
self->near_spectrum_initialized = 0;
return 0;
}
int WebRtc_SoftResetDelayEstimator(void *handle, int delay_shift) {
DelayEstimator *self = (DelayEstimator *) handle;
RTC_DCHECK(self);
return WebRtc_SoftResetBinaryDelayEstimator(self->binary_handle, delay_shift);
}
int WebRtc_set_history_size(void *handle, int history_size) {
DelayEstimator *self = (DelayEstimator *)(handle);
if ((self == NULL) || (history_size <= 1)) {
return -1;
}
return WebRtc_AllocateHistoryBufferMemory(self->binary_handle, history_size);
}
int WebRtc_history_size(const void *handle) {
const DelayEstimator *self = (const DelayEstimator *)(handle);
if (self == NULL) {
return -1;
}
if (self->binary_handle->farend->history_size !=
self->binary_handle->history_size) {
// Non matching history sizes.
return -1;
}
return self->binary_handle->history_size;
}
int WebRtc_set_lookahead(void *handle, int lookahead) {
DelayEstimator *self = (DelayEstimator *) handle;
RTC_DCHECK(self);
RTC_DCHECK(self->binary_handle);
if ((lookahead > self->binary_handle->near_history_size - 1) ||
(lookahead < 0)) {
return -1;
}
self->binary_handle->lookahead = lookahead;
return self->binary_handle->lookahead;
}
int WebRtc_lookahead(void *handle) {
DelayEstimator *self = (DelayEstimator *) handle;
RTC_DCHECK(self);
RTC_DCHECK(self->binary_handle);
return self->binary_handle->lookahead;
}
int WebRtc_set_allowed_offset(void *handle, int allowed_offset) {
DelayEstimator *self = (DelayEstimator *) handle;
if ((self == NULL) || (allowed_offset < 0)) {
return -1;
}
self->binary_handle->allowed_offset = allowed_offset;
return 0;
}
int WebRtc_get_allowed_offset(const void *handle) {
const DelayEstimator *self = (const DelayEstimator *) handle;
if (self == NULL) {
return -1;
}
return self->binary_handle->allowed_offset;
}
int WebRtc_enable_robust_validation(void *handle, int enable) {
DelayEstimator *self = (DelayEstimator *) handle;
if (self == NULL) {
return -1;
}
if ((enable < 0) || (enable > 1)) {
return -1;
}
RTC_DCHECK(self->binary_handle);
self->binary_handle->robust_validation_enabled = enable;
return 0;
}
int WebRtc_is_robust_validation_enabled(const void *handle) {
const DelayEstimator *self = (const DelayEstimator *) handle;
if (self == NULL) {
return -1;
}
return self->binary_handle->robust_validation_enabled;
}
int WebRtc_DelayEstimatorProcessFix(void *handle,
const uint16_t *near_spectrum,
int spectrum_size,
int near_q) {
DelayEstimator *self = (DelayEstimator *) handle;
uint32_t binary_spectrum = 0;
if (self == NULL) {
return -1;
}
if (near_spectrum == NULL) {
// Empty near end spectrum.
return -1;
}
if (spectrum_size != self->spectrum_size) {
// Data sizes don't match.
return -1;
}
if (near_q > 15) {
// If |near_q| is larger than 15 we cannot guarantee no wrap around.
return -1;
}
// Get binary spectra.
binary_spectrum =
BinarySpectrumFix(near_spectrum, self->mean_near_spectrum, near_q,
&(self->near_spectrum_initialized));
return WebRtc_ProcessBinarySpectrum(self->binary_handle, binary_spectrum);
}
int WebRtc_DelayEstimatorProcessFloat(void *handle,
const float *near_spectrum,
int spectrum_size) {
DelayEstimator *self = (DelayEstimator *) handle;
uint32_t binary_spectrum = 0;
if (self == NULL) {
return -1;
}
if (near_spectrum == NULL) {
// Empty near end spectrum.
return -1;
}
if (spectrum_size != self->spectrum_size) {
// Data sizes don't match.
return -1;
}
// Get binary spectrum.
binary_spectrum = BinarySpectrumFloat(near_spectrum, self->mean_near_spectrum,
&(self->near_spectrum_initialized));
return WebRtc_ProcessBinarySpectrum(self->binary_handle, binary_spectrum);
}
int WebRtc_last_delay(void *handle) {
DelayEstimator *self = (DelayEstimator *) handle;
if (self == NULL) {
return -1;
}
return WebRtc_binary_last_delay(self->binary_handle);
}
float WebRtc_last_delay_quality(void *handle) {
DelayEstimator *self = (DelayEstimator *) handle;
RTC_DCHECK(self);
return WebRtc_binary_last_delay_quality(self->binary_handle);
}
@@ -0,0 +1,246 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Performs delay estimation on block by block basis.
// The return value is 0 - OK and -1 - Error, unless otherwise stated.
#ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
#define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
#include <stdint.h>
// Releases the memory allocated by WebRtc_CreateDelayEstimatorFarend(...)
void WebRtc_FreeDelayEstimatorFarend(void *handle);
// Allocates the memory needed by the far-end part of the delay estimation. The
// memory needs to be initialized separately through
// WebRtc_InitDelayEstimatorFarend(...).
//
// Inputs:
// - spectrum_size : Size of the spectrum used both in far-end and
// near-end. Used to allocate memory for spectrum
// specific buffers.
// - history_size : The far-end history buffer size. A change in buffer
// size can be forced with WebRtc_set_history_size().
// Note that the maximum delay which can be estimated is
// determined together with WebRtc_set_lookahead().
//
// Return value:
// - void* : Created |handle|. If the memory can't be allocated or
// if any of the input parameters are invalid NULL is
// returned.
void *WebRtc_CreateDelayEstimatorFarend(int spectrum_size, int history_size);
// Initializes the far-end part of the delay estimation instance returned by
// WebRtc_CreateDelayEstimatorFarend(...)
int WebRtc_InitDelayEstimatorFarend(void *handle);
// Soft resets the far-end part of the delay estimation instance returned by
// WebRtc_CreateDelayEstimatorFarend(...).
// Input:
// - delay_shift : The amount of blocks to shift history buffers.
void WebRtc_SoftResetDelayEstimatorFarend(void *handle, int delay_shift);
// Adds the far-end spectrum to the far-end history buffer. This spectrum is
// used as reference when calculating the delay using
// WebRtc_ProcessSpectrum().
//
// Inputs:
// - far_spectrum : Far-end spectrum.
// - spectrum_size : The size of the data arrays (same for both far- and
// near-end).
// - far_q : The Q-domain of the far-end data.
//
// Output:
// - handle : Updated far-end instance.
//
int WebRtc_AddFarSpectrumFix(void *handle,
const uint16_t *far_spectrum,
int spectrum_size,
int far_q);
// See WebRtc_AddFarSpectrumFix() for description.
int WebRtc_AddFarSpectrumFloat(void *handle,
const float *far_spectrum,
int spectrum_size);
// Releases the memory allocated by WebRtc_CreateDelayEstimator(...)
void WebRtc_FreeDelayEstimator(void *handle);
// Allocates the memory needed by the delay estimation. The memory needs to be
// initialized separately through WebRtc_InitDelayEstimator(...).
//
// Inputs:
// - farend_handle : Pointer to the far-end part of the delay estimation
// instance created prior to this call using
// WebRtc_CreateDelayEstimatorFarend().
//
// Note that WebRtc_CreateDelayEstimator does not take
// ownership of |farend_handle|, which has to be torn
// down properly after this instance.
//
// - max_lookahead : Maximum amount of non-causal lookahead allowed. The
// actual amount of lookahead used can be controlled by
// WebRtc_set_lookahead(...). The default |lookahead| is
// set to |max_lookahead| at create time. Use
// WebRtc_set_lookahead(...) before start if a different
// value is desired.
//
// Using lookahead can detect cases in which a near-end
// signal occurs before the corresponding far-end signal.
// It will delay the estimate for the current block by an
// equal amount, and the returned values will be offset
// by it.
//
// A value of zero is the typical no-lookahead case.
// This also represents the minimum delay which can be
// estimated.
//
// Note that the effective range of delay estimates is
// [-|lookahead|,... ,|history_size|-|lookahead|)
// where |history_size| is set through
// WebRtc_set_history_size().
//
// Return value:
// - void* : Created |handle|. If the memory can't be allocated or
// if any of the input parameters are invalid NULL is
// returned.
void *WebRtc_CreateDelayEstimator(void *farend_handle, int max_lookahead);
// Initializes the delay estimation instance returned by
// WebRtc_CreateDelayEstimator(...)
int WebRtc_InitDelayEstimator(void *handle);
// Soft resets the delay estimation instance returned by
// WebRtc_CreateDelayEstimator(...)
// Input:
// - delay_shift : The amount of blocks to shift history buffers.
//
// Return value:
// - actual_shifts : The actual number of shifts performed.
int WebRtc_SoftResetDelayEstimator(void *handle, int delay_shift);
// Sets the effective |history_size| used. Valid values from 2. We simply need
// at least two delays to compare to perform an estimate. If |history_size| is
// changed, buffers are reallocated filling in with zeros if necessary.
// Note that changing the |history_size| affects both buffers in far-end and
// near-end. Hence it is important to change all DelayEstimators that use the
// same reference far-end, to the same |history_size| value.
// Inputs:
// - handle : Pointer to the delay estimation instance.
// - history_size : Effective history size to be used.
// Return value:
// - new_history_size : The new history size used. If the memory was not able
// to be allocated 0 is returned.
int WebRtc_set_history_size(void *handle, int history_size);
// Returns the history_size currently used.
// Input:
// - handle : Pointer to the delay estimation instance.
int WebRtc_history_size(const void *handle);
// Sets the amount of |lookahead| to use. Valid values are [0, max_lookahead]
// where |max_lookahead| was set at create time through
// WebRtc_CreateDelayEstimator(...).
//
// Input:
// - handle : Pointer to the delay estimation instance.
// - lookahead : The amount of lookahead to be used.
//
// Return value:
// - new_lookahead : The actual amount of lookahead set, unless |handle| is
// a NULL pointer or |lookahead| is invalid, for which an
// error is returned.
int WebRtc_set_lookahead(void *handle, int lookahead);
// Returns the amount of lookahead we currently use.
// Input:
// - handle : Pointer to the delay estimation instance.
int WebRtc_lookahead(void *handle);
// Sets the |allowed_offset| used in the robust validation scheme. If the
// delay estimator is used in an echo control component, this parameter is
// related to the filter length. In principle |allowed_offset| should be set to
// the echo control filter length minus the expected echo duration, i.e., the
// delay offset the echo control can handle without quality regression. The
// default value, used if not set manually, is zero. Note that |allowed_offset|
// has to be non-negative.
// Inputs:
// - handle : Pointer to the delay estimation instance.
// - allowed_offset : The amount of delay offset, measured in partitions,
// the echo control filter can handle.
int WebRtc_set_allowed_offset(void *handle, int allowed_offset);
// Returns the |allowed_offset| in number of partitions.
int WebRtc_get_allowed_offset(const void *handle);
// Enables/Disables a robust validation functionality in the delay estimation.
// This is by default set to disabled at create time. The state is preserved
// over a reset.
// Inputs:
// - handle : Pointer to the delay estimation instance.
// - enable : Enable (1) or disable (0) this feature.
int WebRtc_enable_robust_validation(void *handle, int enable);
// Returns 1 if robust validation is enabled and 0 if disabled.
int WebRtc_is_robust_validation_enabled(const void *handle);
// Estimates and returns the delay between the far-end and near-end blocks. The
// value will be offset by the lookahead (i.e. the lookahead should be
// subtracted from the returned value).
// Inputs:
// - handle : Pointer to the delay estimation instance.
// - near_spectrum : Pointer to the near-end spectrum data of the current
// block.
// - spectrum_size : The size of the data arrays (same for both far- and
// near-end).
// - near_q : The Q-domain of the near-end data.
//
// Output:
// - handle : Updated instance.
//
// Return value:
// - delay : >= 0 - Calculated delay value.
// -1 - Error.
// -2 - Insufficient data for estimation.
int WebRtc_DelayEstimatorProcessFix(void *handle,
const uint16_t *near_spectrum,
int spectrum_size,
int near_q);
// See WebRtc_DelayEstimatorProcessFix() for description.
int WebRtc_DelayEstimatorProcessFloat(void *handle,
const float *near_spectrum,
int spectrum_size);
// Returns the last calculated delay updated by the function
// WebRtc_DelayEstimatorProcess(...).
//
// Input:
// - handle : Pointer to the delay estimation instance.
//
// Return value:
// - delay : >= 0 - Last calculated delay value.
// -1 - Error.
// -2 - Insufficient data for estimation.
int WebRtc_last_delay(void *handle);
// Returns the estimation quality/probability of the last calculated delay
// updated by the function WebRtc_DelayEstimatorProcess(...). The estimation
// quality is a value in the interval [0, 1]. The higher the value, the better
// the quality.
//
// Return value:
// - delay_quality : >= 0 - Estimation quality of last calculated delay.
float WebRtc_last_delay_quality(void *handle);
#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,265 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* This header file includes the VAD API calls. Specific function calls are given below.
*/
#ifndef COMMON_AUDIO_VAD_INCLUDE_WEBRTC_VAD_H_ // NOLINT
#define COMMON_AUDIO_VAD_INCLUDE_WEBRTC_VAD_H_
#include <stddef.h>
#include <stdint.h> // NOLINT(build/include)
#include <string.h>
#include <assert.h>
// If you for some reson need to know if DCHECKs are on, test the value of
// RTC_DCHECK_IS_ON. (Test its value, not if it's defined; it'll always be
// defined, to either a true or a false value.)
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define RTC_DCHECK_IS_ON 1
#else
#define RTC_DCHECK_IS_ON 0
#endif
// C version. Lacks many features compared to the C++ version, but usage
// guidelines are the same.
#define RTC_DCHECK(condition) assert(condition)
#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
//int32_t WebRtcSpl_Energy(int16_t *vector,
// size_t vector_length,
// int *scale_factor);
enum
{
kNumChannels = 6
}; // Number of frequency bands (named channels).
enum
{
kNumGaussians = 2
}; // Number of Gaussians per channel in the GMM.
enum
{
kTableSize = kNumChannels * kNumGaussians
};
enum
{
kMinEnergy = 10
}; // Minimum energy required to trigger audio signal.
enum Aggressiveness {
kVadNormal = 0,
kVadLowBitrate = 1,
kVadAggressive = 2,
kVadVeryAggressive = 3
};
typedef struct VadInstT_
{
int vad;
int32_t downsampling_filter_states[4];
int16_t noise_means[kTableSize];
int16_t speech_means[kTableSize];
int16_t noise_stds[kTableSize];
int16_t speech_stds[kTableSize];
// TODO(bjornv): Change to |frame_count|.
int32_t frame_counter;
int16_t over_hang; // Over Hang
int16_t num_of_speech;
// TODO(bjornv): Change to |age_vector|.
int16_t index_vector[16 * kNumChannels];
int16_t low_value_vector[16 * kNumChannels];
// TODO(bjornv): Change to |median|.
int16_t mean_value[kNumChannels];
int16_t upper_state[5];
int16_t lower_state[5];
int16_t hp_filter_state[4];
int16_t over_hang_max_1[3];
int16_t over_hang_max_2[3];
int16_t individual[3];
int16_t total[3];
int init_flag;
} VadInstT;
// Initializes the core VAD component. The default aggressiveness mode is
// controlled by |kDefaultMode| in vad_core.c.
//
// - self [i/o] : Instance that should be initialized
//
// returns : 0 (OK), -1 (null pointer in or if the default mode can't be
// set)
int WebRtcVad_InitCore(VadInstT *self);
/****************************************************************************
* WebRtcVad_set_mode_core(...)
*
* This function changes the VAD settings
*
* Input:
* - inst : VAD instance
* - mode : Aggressiveness degree
* 0 (High quality) - 3 (Highly aggressive)
*
* Output:
* - inst : Changed instance
*
* Return value : 0 - Ok
* -1 - Error
*/
int WebRtcVad_set_mode_core(VadInstT *self, int mode);
/****************************************************************************
* WebRtcVad_CalcVad8khz(...)
*
* Calculate probability for active speech and make VAD decision.
*
* Input:
* - inst : Instance that should be initialized
* - speech_frame : Input speech frame
* - frame_length : Number of input samples
*
* Output:
* - inst : Updated filter states etc.
*
* Return value : VAD decision
* 0 - No active speech
* 1-6 - Active speech
*/
int WebRtcVad_CalcVad8khz(VadInstT *inst, const int16_t *speech_frame,
size_t frame_length);
// Updates and returns the smoothed feature minimum. As minimum we use the
// median of the five smallest feature values in a 100 frames long window.
// As long as |handle->frame_counter| is zero, that is, we haven't received any
// "valid" data, FindMinimum() outputs the default value of 1600.
//
// Inputs:
// - feature_value : New feature value to update with.
// - channel : Channel number.
//
// Input & Output:
// - handle : State information of the VAD.
//
// Returns:
// : Smoothed minimum value for a moving window.
int16_t WebRtcVad_FindMinimum(VadInstT *handle,
int16_t feature_value,
int channel);
// Calculates the probability for |input|, given that |input| comes from a
// normal distribution with mean and standard deviation (|mean|, |std|).
//
// Inputs:
// - input : input sample in Q4.
// - mean : mean input in the statistical model, Q7.
// - std : standard deviation, Q7.
//
// Output:
//
// - delta : input used when updating the model, Q11.
// |delta| = (|input| - |mean|) / |std|^2.
//
// Return:
// (probability for |input|) =
// 1 / |std| * exp(-(|input| - |mean|)^2 / (2 * |std|^2));
int32_t WebRtcVad_GaussianProbability(int16_t input,
int16_t mean,
int16_t std,
int16_t *delta);
// Takes |data_length| samples of |data_in| and calculates the logarithm of the
// energy of each of the |kNumChannels| = 6 frequency bands used by the VAD:
// 80 Hz - 250 Hz
// 250 Hz - 500 Hz
// 500 Hz - 1000 Hz
// 1000 Hz - 2000 Hz
// 2000 Hz - 3000 Hz
// 3000 Hz - 4000 Hz
//
// The values are given in Q4 and written to |features|. Further, an approximate
// overall energy is returned. The return value is used in
// WebRtcVad_GmmProbability() as a signal indicator, hence it is arbitrary above
// the threshold |kMinEnergy|.
//
// - self [i/o] : State information of the VAD.
// - data_in [i] : Input audio data, for feature extraction.
// - data_length [i] : Audio data size, in number of samples.
// - features [o] : 10 * log10(energy in each frequency band), Q4.
// - returns : Total energy of the signal (NOTE! This value is not
// exact. It is only used in a comparison.)
int16_t WebRtcVad_CalculateFeatures(VadInstT *self, const int16_t *data_in,
size_t data_length, int16_t *features);
typedef struct WebRtcVadInst VadInst;
#ifdef __cplusplus
extern "C" {
#endif
// Creates an instance to the VAD structure.
VadInst *WebRtcVad_Create(void);
// Frees the dynamic memory of a specified VAD instance.
//
// - handle [i] : Pointer to VAD instance that should be freed.
void WebRtcVad_Free(VadInst *handle);
// Initializes a VAD instance.
//
// - handle [i/o] : Instance that should be initialized.
//
// returns : 0 - (OK),
// -1 - (null pointer or Default mode could not be set).
int WebRtcVad_Init(VadInst *handle);
// Sets the VAD operating mode. A more aggressive (higher mode) VAD is more
// restrictive in reporting speech. Put in other words the probability of being
// speech when the VAD returns 1 is increased with increasing mode. As a
// consequence also the missed detection rate goes up.
//
// - handle [i/o] : VAD instance.
// - mode [i] : Aggressiveness mode (0, 1, 2, or 3).
//
// returns : 0 - (OK),
// -1 - (null pointer, mode could not be set or the VAD instance
// has not been initialized).
int WebRtcVad_set_mode(VadInst *handle, int mode);
// Calculates a VAD decision for the |audio_frame|. For valid sampling rates
// frame lengths, see the description of WebRtcVad_ValidRatesAndFrameLengths().
//
// - handle [i/o] : VAD Instance. Needs to be initialized by
// WebRtcVad_Init() before call.
// - fs [i] : Sampling frequency (Hz): 8000, 16000, or 32000
// - audio_frame [i] : Audio frame buffer.
// - frame_length [i] : Length of audio frame buffer in number of samples.
// - keep_weight [i] : return active voice weight
//
// returns : 1 - (Active Voice),
// 0 - (Non-active Voice),
// -1 - (Error)
int WebRtcVad_Process(VadInst *handle, int fs, const int16_t *audio_frame,
size_t frame_length, int keep_weight);
#ifdef __cplusplus
}
#endif
#endif // COMMON_AUDIO_VAD_INCLUDE_WEBRTC_VAD_H_ // NOLINT