aboutsummaryrefslogtreecommitdiff
path: root/src/modules/audio_processing/agc/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/audio_processing/agc/main')
-rw-r--r--src/modules/audio_processing/agc/main/interface/gain_control.h273
-rw-r--r--src/modules/audio_processing/agc/main/matlab/getGains.m32
-rw-r--r--src/modules/audio_processing/agc/main/source/Android.mk49
-rw-r--r--src/modules/audio_processing/agc/main/source/agc.gyp43
-rw-r--r--src/modules/audio_processing/agc/main/source/analog_agc.c1700
-rw-r--r--src/modules/audio_processing/agc/main/source/analog_agc.h133
-rw-r--r--src/modules/audio_processing/agc/main/source/digital_agc.c780
-rw-r--r--src/modules/audio_processing/agc/main/source/digital_agc.h76
8 files changed, 3086 insertions, 0 deletions
diff --git a/src/modules/audio_processing/agc/main/interface/gain_control.h b/src/modules/audio_processing/agc/main/interface/gain_control.h
new file mode 100644
index 0000000000..2893331faf
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/interface/gain_control.h
@@ -0,0 +1,273 @@
+/*
+ * 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_AGC_MAIN_INTERFACE_GAIN_CONTROL_H_
+#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_INTERFACE_GAIN_CONTROL_H_
+
+#include "typedefs.h"
+
+// 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
+{
+ WebRtc_Word16 targetLevelDbfs; // default 3 (-3 dBOv)
+ WebRtc_Word16 compressionGaindB; // default 9 dB
+ WebRtc_UWord8 limiterEnable; // default kAgcTrue (on)
+} WebRtcAgc_config_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*
+ * This function processes a 10/20ms frame of far-end speech to determine
+ * if there is active speech. Far-end speech length can be either 10ms or
+ * 20ms. The length of the input speech vector must be given in samples
+ * (80/160 when FS=8000, and 160/320 when FS=16000 or FS=32000).
+ *
+ * Input:
+ * - agcInst : AGC instance.
+ * - inFar : Far-end input speech vector (10 or 20ms)
+ * - samples : Number of samples in input vector
+ *
+ * Return value:
+ * : 0 - Normal operation.
+ * : -1 - Error
+ */
+int WebRtcAgc_AddFarend(void* agcInst,
+ const WebRtc_Word16* inFar,
+ WebRtc_Word16 samples);
+
+/*
+ * This function processes a 10/20ms frame of microphone speech to determine
+ * if there is active speech. Microphone speech length can be either 10ms or
+ * 20ms. The length of the input speech vector must be given in samples
+ * (80/160 when FS=8000, and 160/320 when FS=16000 or FS=32000). 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 (10 or 20 ms) for
+ * L band
+ * - inMic_H : Microphone input speech vector (10 or 20 ms) for
+ * H band
+ * - samples : Number of samples in input vector
+ *
+ * Return value:
+ * : 0 - Normal operation.
+ * : -1 - Error
+ */
+int WebRtcAgc_AddMic(void* agcInst,
+ WebRtc_Word16* inMic,
+ WebRtc_Word16* inMic_H,
+ WebRtc_Word16 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.
+ * Microphone speech length can be either 10ms or 20ms. The length of the
+ * input speech vector must be given in samples (80/160 when FS=8000, and
+ * 160/320 when FS=16000 or FS=32000).
+ *
+ * Input:
+ * - agcInst : AGC instance.
+ * - inMic : Microphone input speech vector for (10 or 20 ms)
+ * L band
+ * - inMic_H : Microphone input speech vector for (10 or 20 ms)
+ * H band
+ * - 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,
+ WebRtc_Word16* inMic,
+ WebRtc_Word16* inMic_H,
+ WebRtc_Word16 samples,
+ WebRtc_Word32 micLevelIn,
+ WebRtc_Word32* micLevelOut);
+
+/*
+ * This function processes a 10/20ms frame and adjusts (normalizes) the gain
+ * both analog and digitally. The gain adjustments are done only during
+ * active periods of speech. The input speech length can be either 10ms or
+ * 20ms and the output is of the same length. The length of the speech
+ * vectors must be given in samples (80/160 when FS=8000, and 160/320 when
+ * FS=16000 or FS=32000). 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 (10 or 20 ms) for
+ * L band
+ * - inNear_H : Near-end input speech vector (10 or 20 ms) for
+ * H band
+ * - 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 (L band)
+ * : May be the same vector as the input.
+ * - out_H : Gain-adjusted near-end speech vector (H band)
+ * - 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 WebRtc_Word16* inNear,
+ const WebRtc_Word16* inNear_H,
+ WebRtc_Word16 samples,
+ WebRtc_Word16* out,
+ WebRtc_Word16* out_H,
+ WebRtc_Word32 inMicLevel,
+ WebRtc_Word32* outMicLevel,
+ WebRtc_Word16 echo,
+ WebRtc_UWord8* 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, WebRtcAgc_config_t 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, WebRtcAgc_config_t* config);
+
+/*
+ * This function creates an AGC instance, which will contain the state
+ * information for one (duplex) channel.
+ *
+ * Return value : AGC instance if successful
+ * : 0 (i.e., a NULL pointer) if unsuccessful
+ */
+int WebRtcAgc_Create(void **agcInst);
+
+/*
+ * This function frees the AGC instance created at the beginning.
+ *
+ * Input:
+ * - agcInst : AGC instance.
+ *
+ * Return value : 0 - Ok
+ * -1 - Error
+ */
+int 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,
+ WebRtc_Word32 minLevel,
+ WebRtc_Word32 maxLevel,
+ WebRtc_Word16 agcMode,
+ WebRtc_UWord32 fs);
+
+/*
+ * This function returns a text string containing the version.
+ *
+ * Input:
+ * - length : Length of the char array pointed to by version
+ * Output:
+ * - version : Pointer to a char array of to which the version
+ * : string will be copied.
+ *
+ * Return value : 0 - OK
+ * -1 - Error
+ */
+int WebRtcAgc_Version(WebRtc_Word8 *versionStr, WebRtc_Word16 length);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_INTERFACE_GAIN_CONTROL_H_
diff --git a/src/modules/audio_processing/agc/main/matlab/getGains.m b/src/modules/audio_processing/agc/main/matlab/getGains.m
new file mode 100644
index 0000000000..e0234b8593
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/matlab/getGains.m
@@ -0,0 +1,32 @@
+% Outputs a file for testing purposes.
+%
+% Adjust the following parameters to suit. Their purpose becomes more clear on
+% viewing the gain plots.
+% MaxGain: Max gain in dB
+% MinGain: Min gain at overload (0 dBov) in dB
+% CompRatio: Compression ratio, essentially determines the slope of the gain
+% function between the max and min gains
+% Knee: The smoothness of the transition to max gain (smaller is smoother)
+MaxGain = 5; MinGain = 0; CompRatio = 3; Knee = 1;
+
+% Compute gains
+zeros = 0:31; lvl = 2.^(1-zeros);
+A = -10*log10(lvl) * (CompRatio - 1) / CompRatio;
+B = MaxGain - MinGain;
+gains = round(2^16*10.^(0.05 * (MinGain + B * ( log(exp(-Knee*A)+exp(-Knee*B)) - log(1+exp(-Knee*B)) ) / log(1/(1+exp(Knee*B))))));
+fprintf(1, '\t%i, %i, %i, %i,\n', gains);
+
+% Save gains to file
+fid = fopen('gains', 'wb');
+if fid == -1
+ error(sprintf('Unable to open file %s', filename));
+ return
+end
+fwrite(fid, gains, 'int32');
+fclose(fid);
+
+% Plotting
+in = 10*log10(lvl); out = 20*log10(gains/65536);
+subplot(121); plot(in, out); axis([-60, 0, -5, 30]); grid on; xlabel('Input (dB)'); ylabel('Gain (dB)');
+subplot(122); plot(in, in+out); axis([-60, 0, -60, 10]); grid on; xlabel('Input (dB)'); ylabel('Output (dB)');
+zoom on;
diff --git a/src/modules/audio_processing/agc/main/source/Android.mk b/src/modules/audio_processing/agc/main/source/Android.mk
new file mode 100644
index 0000000000..e045839147
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/Android.mk
@@ -0,0 +1,49 @@
+# This file is generated by gyp; do not edit. This means you!
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_ARM_MODE := arm
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+LOCAL_MODULE := libwebrtc_agc
+LOCAL_MODULE_TAGS := optional
+LOCAL_GENERATED_SOURCES :=
+LOCAL_SRC_FILES := analog_agc.c \
+ digital_agc.c
+
+# Flags passed to both C and C++ files.
+MY_CFLAGS :=
+MY_CFLAGS_C :=
+MY_DEFS := '-DNO_TCMALLOC' \
+ '-DNO_HEAPCHECKER' \
+ '-DWEBRTC_TARGET_PC' \
+ '-DWEBRTC_LINUX' \
+ '-DWEBRTC_THREAD_RR'
+ifeq ($(TARGET_ARCH),arm)
+MY_DEFS += \
+ '-DWEBRTC_ANDROID' \
+ '-DANDROID'
+endif
+LOCAL_CFLAGS := $(MY_CFLAGS_C) $(MY_CFLAGS) $(MY_DEFS)
+
+# Include paths placed before CFLAGS/CPPFLAGS
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../.. \
+ $(LOCAL_PATH)/../interface \
+ $(LOCAL_PATH)/../../../../../common_audio/signal_processing_library/main/interface
+
+# Flags passed to only C++ (and not C) files.
+LOCAL_CPPFLAGS :=
+LOCAL_LDFLAGS :=
+
+LOCAL_STATIC_LIBRARIES :=
+# Duplicate the static libraries to fix circular references
+LOCAL_STATIC_LIBRARIES += $(LOCAL_STATIC_LIBRARIES)
+
+LOCAL_SHARED_LIBRARIES := libcutils \
+ libdl \
+ libstlport
+LOCAL_ADDITIONAL_DEPENDENCIES :=
+
+include external/stlport/libstlport.mk
+include $(BUILD_STATIC_LIBRARY)
diff --git a/src/modules/audio_processing/agc/main/source/agc.gyp b/src/modules/audio_processing/agc/main/source/agc.gyp
new file mode 100644
index 0000000000..e28a4c8c68
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/agc.gyp
@@ -0,0 +1,43 @@
+# 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.
+
+{
+ 'includes': [
+ '../../../../../common_settings.gypi', # Common settings
+ ],
+ 'targets': [
+ {
+ 'target_name': 'agc',
+ 'type': '<(library)',
+ 'dependencies': [
+ '../../../../../common_audio/signal_processing_library/main/source/spl.gyp:spl',
+ ],
+ 'include_dirs': [
+ '../interface',
+ ],
+ 'direct_dependent_settings': {
+ 'include_dirs': [
+ '../interface',
+ ],
+ },
+ 'sources': [
+ '../interface/gain_control.h',
+ 'analog_agc.c',
+ 'analog_agc.h',
+ 'digital_agc.c',
+ 'digital_agc.h',
+ ],
+ },
+ ],
+}
+
+# Local Variables:
+# tab-width:2
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=2 shiftwidth=2:
diff --git a/src/modules/audio_processing/agc/main/source/analog_agc.c b/src/modules/audio_processing/agc/main/source/analog_agc.c
new file mode 100644
index 0000000000..dfb7adc621
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/analog_agc.c
@@ -0,0 +1,1700 @@
+/*
+ * 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.
+ */
+
+/* analog_agc.c
+ *
+ * Using a feedback system, determines an appropriate analog volume level
+ * given an input signal and current volume level. Targets a conservative
+ * signal level and is intended for use with a digital AGC to apply
+ * additional gain.
+ *
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#ifdef AGC_DEBUG //test log
+#include <stdio.h>
+#endif
+#include "analog_agc.h"
+
+/* The slope of in Q13*/
+static const WebRtc_Word16 kSlope1[8] = {21793, 12517, 7189, 4129, 2372, 1362, 472, 78};
+
+/* The offset in Q14 */
+static const WebRtc_Word16 kOffset1[8] = {25395, 23911, 22206, 20737, 19612, 18805, 17951,
+ 17367};
+
+/* The slope of in Q13*/
+static const WebRtc_Word16 kSlope2[8] = {2063, 1731, 1452, 1218, 1021, 857, 597, 337};
+
+/* The offset in Q14 */
+static const WebRtc_Word16 kOffset2[8] = {18432, 18379, 18290, 18177, 18052, 17920, 17670,
+ 17286};
+
+static const WebRtc_Word16 kMuteGuardTimeMs = 8000;
+static const WebRtc_Word16 kInitCheck = 42;
+
+/* Default settings if config is not used */
+#define AGC_DEFAULT_TARGET_LEVEL 3
+#define AGC_DEFAULT_COMP_GAIN 9
+/* This is the target level for the analog part in ENV scale. To convert to RMS scale you
+ * have to add OFFSET_ENV_TO_RMS.
+ */
+#define ANALOG_TARGET_LEVEL 11
+#define ANALOG_TARGET_LEVEL_2 5 // ANALOG_TARGET_LEVEL / 2
+/* Offset between RMS scale (analog part) and ENV scale (digital part). This value actually
+ * varies with the FIXED_ANALOG_TARGET_LEVEL, hence we should in the future replace it with
+ * a table.
+ */
+#define OFFSET_ENV_TO_RMS 9
+/* The reference input level at which the digital part gives an output of targetLevelDbfs
+ * (desired level) if we have no compression gain. This level should be set high enough not
+ * to compress the peaks due to the dynamics.
+ */
+#define DIGITAL_REF_AT_0_COMP_GAIN 4
+/* Speed of reference level decrease.
+ */
+#define DIFF_REF_TO_ANALOG 5
+
+#ifdef MIC_LEVEL_FEEDBACK
+#define NUM_BLOCKS_IN_SAT_BEFORE_CHANGE_TARGET 7
+#endif
+/* Size of analog gain table */
+#define GAIN_TBL_LEN 32
+/* Matlab code:
+ * fprintf(1, '\t%i, %i, %i, %i,\n', round(10.^(linspace(0,10,32)/20) * 2^12));
+ */
+/* Q12 */
+static const WebRtc_UWord16 kGainTableAnalog[GAIN_TBL_LEN] = {4096, 4251, 4412, 4579, 4752,
+ 4932, 5118, 5312, 5513, 5722, 5938, 6163, 6396, 6638, 6889, 7150, 7420, 7701, 7992,
+ 8295, 8609, 8934, 9273, 9623, 9987, 10365, 10758, 11165, 11587, 12025, 12480, 12953};
+
+/* Gain/Suppression tables for virtual Mic (in Q10) */
+static const WebRtc_UWord16 kGainTableVirtualMic[128] = {1052, 1081, 1110, 1141, 1172, 1204,
+ 1237, 1271, 1305, 1341, 1378, 1416, 1454, 1494, 1535, 1577, 1620, 1664, 1710, 1757,
+ 1805, 1854, 1905, 1957, 2010, 2065, 2122, 2180, 2239, 2301, 2364, 2428, 2495, 2563,
+ 2633, 2705, 2779, 2855, 2933, 3013, 3096, 3180, 3267, 3357, 3449, 3543, 3640, 3739,
+ 3842, 3947, 4055, 4166, 4280, 4397, 4517, 4640, 4767, 4898, 5032, 5169, 5311, 5456,
+ 5605, 5758, 5916, 6078, 6244, 6415, 6590, 6770, 6956, 7146, 7341, 7542, 7748, 7960,
+ 8178, 8402, 8631, 8867, 9110, 9359, 9615, 9878, 10148, 10426, 10711, 11004, 11305,
+ 11614, 11932, 12258, 12593, 12938, 13292, 13655, 14029, 14412, 14807, 15212, 15628,
+ 16055, 16494, 16945, 17409, 17885, 18374, 18877, 19393, 19923, 20468, 21028, 21603,
+ 22194, 22801, 23425, 24065, 24724, 25400, 26095, 26808, 27541, 28295, 29069, 29864,
+ 30681, 31520, 32382};
+static const WebRtc_UWord16 kSuppressionTableVirtualMic[128] = {1024, 1006, 988, 970, 952,
+ 935, 918, 902, 886, 870, 854, 839, 824, 809, 794, 780, 766, 752, 739, 726, 713, 700,
+ 687, 675, 663, 651, 639, 628, 616, 605, 594, 584, 573, 563, 553, 543, 533, 524, 514,
+ 505, 496, 487, 478, 470, 461, 453, 445, 437, 429, 421, 414, 406, 399, 392, 385, 378,
+ 371, 364, 358, 351, 345, 339, 333, 327, 321, 315, 309, 304, 298, 293, 288, 283, 278,
+ 273, 268, 263, 258, 254, 249, 244, 240, 236, 232, 227, 223, 219, 215, 211, 208, 204,
+ 200, 197, 193, 190, 186, 183, 180, 176, 173, 170, 167, 164, 161, 158, 155, 153, 150,
+ 147, 145, 142, 139, 137, 134, 132, 130, 127, 125, 123, 121, 118, 116, 114, 112, 110,
+ 108, 106, 104, 102};
+
+/* Table for target energy levels. Values in Q(-7)
+ * Matlab code
+ * targetLevelTable = fprintf('%d,\t%d,\t%d,\t%d,\n', round((32767*10.^(-(0:63)'/20)).^2*16/2^7) */
+
+static const WebRtc_Word32 kTargetLevelTable[64] = {134209536, 106606424, 84680493, 67264106,
+ 53429779, 42440782, 33711911, 26778323, 21270778, 16895980, 13420954, 10660642,
+ 8468049, 6726411, 5342978, 4244078, 3371191, 2677832, 2127078, 1689598, 1342095,
+ 1066064, 846805, 672641, 534298, 424408, 337119, 267783, 212708, 168960, 134210,
+ 106606, 84680, 67264, 53430, 42441, 33712, 26778, 21271, 16896, 13421, 10661, 8468,
+ 6726, 5343, 4244, 3371, 2678, 2127, 1690, 1342, 1066, 847, 673, 534, 424, 337, 268,
+ 213, 169, 134, 107, 85, 67};
+
+int WebRtcAgc_AddMic(void *state, WebRtc_Word16 *in_mic, WebRtc_Word16 *in_mic_H,
+ WebRtc_Word16 samples)
+{
+ WebRtc_Word32 nrg, max_nrg, sample, tmp32;
+ WebRtc_Word32 *ptr;
+ WebRtc_UWord16 targetGainIdx, gain;
+ WebRtc_Word16 i, n, L, M, subFrames, tmp16, tmp_speech[16];
+ Agc_t *stt;
+ stt = (Agc_t *)state;
+
+ //default/initial values corresponding to 10ms for wb and swb
+ M = 10;
+ L = 16;
+ subFrames = 160;
+
+ if (stt->fs == 8000)
+ {
+ if (samples == 80)
+ {
+ subFrames = 80;
+ M = 10;
+ L = 8;
+ } else if (samples == 160)
+ {
+ subFrames = 80;
+ M = 20;
+ L = 8;
+ } else
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_mic, frame %d: Invalid number of samples\n\n",
+ (stt->fcount + 1));
+#endif
+ return -1;
+ }
+ } else if (stt->fs == 16000)
+ {
+ if (samples == 160)
+ {
+ subFrames = 160;
+ M = 10;
+ L = 16;
+ } else if (samples == 320)
+ {
+ subFrames = 160;
+ M = 20;
+ L = 16;
+ } else
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_mic, frame %d: Invalid number of samples\n\n",
+ (stt->fcount + 1));
+#endif
+ return -1;
+ }
+ } else if (stt->fs == 32000)
+ {
+ /* SWB is processed as 160 sample for L and H bands */
+ if (samples == 160)
+ {
+ subFrames = 160;
+ M = 10;
+ L = 16;
+ } else
+ {
+#ifdef AGC_DEBUG
+ fprintf(stt->fpt,
+ "AGC->add_mic, frame %d: Invalid sample rate\n\n",
+ (stt->fcount + 1));
+#endif
+ return -1;
+ }
+ }
+
+ /* Check for valid pointers based on sampling rate */
+ if ((stt->fs == 32000) && (in_mic_H == NULL))
+ {
+ return -1;
+ }
+ /* Check for valid pointer for low band */
+ if (in_mic == NULL)
+ {
+ return -1;
+ }
+
+ /* apply slowly varying digital gain */
+ if (stt->micVol > stt->maxAnalog)
+ {
+ /* Q1 */
+ tmp16 = (WebRtc_Word16)(stt->micVol - stt->maxAnalog);
+ tmp32 = WEBRTC_SPL_MUL_16_16(GAIN_TBL_LEN - 1, tmp16);
+ tmp16 = (WebRtc_Word16)(stt->maxLevel - stt->maxAnalog);
+ targetGainIdx = (WebRtc_UWord16)WEBRTC_SPL_DIV(tmp32, tmp16);
+ assert(targetGainIdx < GAIN_TBL_LEN);
+
+ /* Increment through the table towards the target gain.
+ * If micVol drops below maxAnalog, we allow the gain
+ * to be dropped immediately. */
+ if (stt->gainTableIdx < targetGainIdx)
+ {
+ stt->gainTableIdx++;
+ } else if (stt->gainTableIdx > targetGainIdx)
+ {
+ stt->gainTableIdx--;
+ }
+
+ /* Q12 */
+ gain = kGainTableAnalog[stt->gainTableIdx];
+
+ for (i = 0; i < samples; i++)
+ {
+ // For lower band
+ tmp32 = WEBRTC_SPL_MUL_16_U16(in_mic[i], gain);
+ sample = WEBRTC_SPL_RSHIFT_W32(tmp32, 12);
+ if (sample > 32767)
+ {
+ in_mic[i] = 32767;
+ } else if (sample < -32768)
+ {
+ in_mic[i] = -32768;
+ } else
+ {
+ in_mic[i] = (WebRtc_Word16)sample;
+ }
+
+ // For higher band
+ if (stt->fs == 32000)
+ {
+ tmp32 = WEBRTC_SPL_MUL_16_U16(in_mic_H[i], gain);
+ sample = WEBRTC_SPL_RSHIFT_W32(tmp32, 12);
+ if (sample > 32767)
+ {
+ in_mic_H[i] = 32767;
+ } else if (sample < -32768)
+ {
+ in_mic_H[i] = -32768;
+ } else
+ {
+ in_mic_H[i] = (WebRtc_Word16)sample;
+ }
+ }
+ }
+ } else
+ {
+ stt->gainTableIdx = 0;
+ }
+
+ /* compute envelope */
+ if ((M == 10) && (stt->inQueue > 0))
+ {
+ ptr = stt->env[1];
+ } else
+ {
+ ptr = stt->env[0];
+ }
+
+ for (i = 0; i < M; i++)
+ {
+ /* iterate over samples */
+ max_nrg = 0;
+ for (n = 0; n < L; n++)
+ {
+ nrg = WEBRTC_SPL_MUL_16_16(in_mic[i * L + n], in_mic[i * L + n]);
+ if (nrg > max_nrg)
+ {
+ max_nrg = nrg;
+ }
+ }
+ ptr[i] = max_nrg;
+ }
+
+ /* compute energy */
+ if ((M == 10) && (stt->inQueue > 0))
+ {
+ ptr = stt->Rxx16w32_array[1];
+ } else
+ {
+ ptr = stt->Rxx16w32_array[0];
+ }
+
+ for (i = 0; i < WEBRTC_SPL_RSHIFT_W16(M, 1); i++)
+ {
+ if (stt->fs == 16000)
+ {
+ WebRtcSpl_DownsampleBy2(&in_mic[i * 32], 32, tmp_speech, stt->filterState);
+ } else
+ {
+ memcpy(tmp_speech, &in_mic[i * 16], 16 * sizeof(short));
+ }
+ /* Compute energy in blocks of 16 samples */
+ ptr[i] = WebRtcSpl_DotProductWithScale(tmp_speech, tmp_speech, 16, 4);
+ }
+
+ /* update queue information */
+ if ((stt->inQueue == 0) && (M == 10))
+ {
+ stt->inQueue = 1;
+ } else
+ {
+ stt->inQueue = 2;
+ }
+
+ /* call VAD (use low band only) */
+ for (i = 0; i < samples; i += subFrames)
+ {
+ WebRtcAgc_ProcessVad(&stt->vadMic, &in_mic[i], subFrames);
+ }
+
+ return 0;
+}
+
+int WebRtcAgc_AddFarend(void *state, const WebRtc_Word16 *in_far, WebRtc_Word16 samples)
+{
+ WebRtc_Word32 errHandle = 0;
+ WebRtc_Word16 i, subFrames;
+ Agc_t *stt;
+ stt = (Agc_t *)state;
+
+ if (stt == NULL)
+ {
+ return -1;
+ }
+
+ if (stt->fs == 8000)
+ {
+ if ((samples != 80) && (samples != 160))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_far_end, frame %d: Invalid number of samples\n\n",
+ stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 80;
+ } else if (stt->fs == 16000)
+ {
+ if ((samples != 160) && (samples != 320))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_far_end, frame %d: Invalid number of samples\n\n",
+ stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 160;
+ } else if (stt->fs == 32000)
+ {
+ if ((samples != 160) && (samples != 320))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_far_end, frame %d: Invalid number of samples\n\n",
+ stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 160;
+ } else
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->add_far_end, frame %d: Invalid sample rate\n\n",
+ stt->fcount + 1);
+#endif
+ return -1;
+ }
+
+ for (i = 0; i < samples; i += subFrames)
+ {
+ errHandle += WebRtcAgc_AddFarendToDigital(&stt->digitalAgc, &in_far[i], subFrames);
+ }
+
+ return errHandle;
+}
+
+int WebRtcAgc_VirtualMic(void *agcInst, WebRtc_Word16 *in_near, WebRtc_Word16 *in_near_H,
+ WebRtc_Word16 samples, WebRtc_Word32 micLevelIn,
+ WebRtc_Word32 *micLevelOut)
+{
+ WebRtc_Word32 tmpFlt, micLevelTmp, gainIdx;
+ WebRtc_UWord16 gain;
+ WebRtc_Word16 ii;
+ Agc_t *stt;
+
+ WebRtc_UWord32 nrg;
+ WebRtc_Word16 sampleCntr;
+ WebRtc_UWord32 frameNrg = 0;
+ WebRtc_UWord32 frameNrgLimit = 5500;
+ WebRtc_Word16 numZeroCrossing = 0;
+ const WebRtc_Word16 kZeroCrossingLowLim = 15;
+ const WebRtc_Word16 kZeroCrossingHighLim = 20;
+
+ stt = (Agc_t *)agcInst;
+
+ /*
+ * Before applying gain decide if this is a low-level signal.
+ * The idea is that digital AGC will not adapt to low-level
+ * signals.
+ */
+ if (stt->fs != 8000)
+ {
+ frameNrgLimit = frameNrgLimit << 1;
+ }
+
+ frameNrg = WEBRTC_SPL_MUL_16_16(in_near[0], in_near[0]);
+ for (sampleCntr = 1; sampleCntr < samples; sampleCntr++)
+ {
+
+ // increment frame energy if it is less than the limit
+ // the correct value of the energy is not important
+ if (frameNrg < frameNrgLimit)
+ {
+ nrg = WEBRTC_SPL_MUL_16_16(in_near[sampleCntr], in_near[sampleCntr]);
+ frameNrg += nrg;
+ }
+
+ // Count the zero crossings
+ numZeroCrossing += ((in_near[sampleCntr] ^ in_near[sampleCntr - 1]) < 0);
+ }
+
+ if ((frameNrg < 500) || (numZeroCrossing <= 5))
+ {
+ stt->lowLevelSignal = 1;
+ } else if (numZeroCrossing <= kZeroCrossingLowLim)
+ {
+ stt->lowLevelSignal = 0;
+ } else if (frameNrg <= frameNrgLimit)
+ {
+ stt->lowLevelSignal = 1;
+ } else if (numZeroCrossing >= kZeroCrossingHighLim)
+ {
+ stt->lowLevelSignal = 1;
+ } else
+ {
+ stt->lowLevelSignal = 0;
+ }
+
+ micLevelTmp = WEBRTC_SPL_LSHIFT_W32(micLevelIn, stt->scale);
+ /* Set desired level */
+ gainIdx = stt->micVol;
+ if (stt->micVol > stt->maxAnalog)
+ {
+ gainIdx = stt->maxAnalog;
+ }
+ if (micLevelTmp != stt->micRef)
+ {
+ /* Something has happened with the physical level, restart. */
+ stt->micRef = micLevelTmp;
+ stt->micVol = 127;
+ *micLevelOut = 127;
+ stt->micGainIdx = 127;
+ gainIdx = 127;
+ }
+ /* Pre-process the signal to emulate the microphone level. */
+ /* Take one step at a time in the gain table. */
+ if (gainIdx > 127)
+ {
+ gain = kGainTableVirtualMic[gainIdx - 128];
+ } else
+ {
+ gain = kSuppressionTableVirtualMic[127 - gainIdx];
+ }
+ for (ii = 0; ii < samples; ii++)
+ {
+ tmpFlt = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_U16(in_near[ii], gain), 10);
+ if (tmpFlt > 32767)
+ {
+ tmpFlt = 32767;
+ gainIdx--;
+ if (gainIdx >= 127)
+ {
+ gain = kGainTableVirtualMic[gainIdx - 127];
+ } else
+ {
+ gain = kSuppressionTableVirtualMic[127 - gainIdx];
+ }
+ }
+ if (tmpFlt < -32768)
+ {
+ tmpFlt = -32768;
+ gainIdx--;
+ if (gainIdx >= 127)
+ {
+ gain = kGainTableVirtualMic[gainIdx - 127];
+ } else
+ {
+ gain = kSuppressionTableVirtualMic[127 - gainIdx];
+ }
+ }
+ in_near[ii] = (WebRtc_Word16)tmpFlt;
+ if (stt->fs == 32000)
+ {
+ tmpFlt = WEBRTC_SPL_MUL_16_U16(in_near_H[ii], gain);
+ tmpFlt = WEBRTC_SPL_RSHIFT_W32(tmpFlt, 10);
+ if (tmpFlt > 32767)
+ {
+ tmpFlt = 32767;
+ }
+ if (tmpFlt < -32768)
+ {
+ tmpFlt = -32768;
+ }
+ in_near_H[ii] = (WebRtc_Word16)tmpFlt;
+ }
+ }
+ /* Set the level we (finally) used */
+ stt->micGainIdx = gainIdx;
+// *micLevelOut = stt->micGainIdx;
+ *micLevelOut = WEBRTC_SPL_RSHIFT_W32(stt->micGainIdx, stt->scale);
+ /* Add to Mic as if it was the output from a true microphone */
+ if (WebRtcAgc_AddMic(agcInst, in_near, in_near_H, samples) != 0)
+ {
+ return -1;
+ }
+ return 0;
+}
+
+void WebRtcAgc_UpdateAgcThresholds(Agc_t *stt)
+{
+
+ WebRtc_Word16 tmp16;
+#ifdef MIC_LEVEL_FEEDBACK
+ int zeros;
+
+ if (stt->micLvlSat)
+ {
+ /* Lower the analog target level since we have reached its maximum */
+ zeros = WebRtcSpl_NormW32(stt->Rxx160_LPw32);
+ stt->targetIdxOffset = WEBRTC_SPL_RSHIFT_W16((3 * zeros) - stt->targetIdx - 2, 2);
+ }
+#endif
+
+ /* Set analog target level in envelope dBOv scale */
+ tmp16 = (DIFF_REF_TO_ANALOG * stt->compressionGaindB) + ANALOG_TARGET_LEVEL_2;
+ tmp16 = WebRtcSpl_DivW32W16ResW16((WebRtc_Word32)tmp16, ANALOG_TARGET_LEVEL);
+ stt->analogTarget = DIGITAL_REF_AT_0_COMP_GAIN + tmp16;
+ if (stt->analogTarget < DIGITAL_REF_AT_0_COMP_GAIN)
+ {
+ stt->analogTarget = DIGITAL_REF_AT_0_COMP_GAIN;
+ }
+ if (stt->agcMode == kAgcModeFixedDigital)
+ {
+ /* Adjust for different parameter interpretation in FixedDigital mode */
+ stt->analogTarget = stt->compressionGaindB;
+ }
+#ifdef MIC_LEVEL_FEEDBACK
+ stt->analogTarget += stt->targetIdxOffset;
+#endif
+ /* Since the offset between RMS and ENV is not constant, we should make this into a
+ * table, but for now, we'll stick with a constant, tuned for the chosen analog
+ * target level.
+ */
+ stt->targetIdx = ANALOG_TARGET_LEVEL + OFFSET_ENV_TO_RMS;
+#ifdef MIC_LEVEL_FEEDBACK
+ stt->targetIdx += stt->targetIdxOffset;
+#endif
+ /* Analog adaptation limits */
+ /* analogTargetLevel = round((32767*10^(-targetIdx/20))^2*16/2^7) */
+ stt->analogTargetLevel = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx]; /* ex. -20 dBov */
+ stt->startUpperLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx - 1];/* -19 dBov */
+ stt->startLowerLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx + 1];/* -21 dBov */
+ stt->upperPrimaryLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx - 2];/* -18 dBov */
+ stt->lowerPrimaryLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx + 2];/* -22 dBov */
+ stt->upperSecondaryLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx - 5];/* -15 dBov */
+ stt->lowerSecondaryLimit = RXX_BUFFER_LEN * kTargetLevelTable[stt->targetIdx + 5];/* -25 dBov */
+ stt->upperLimit = stt->startUpperLimit;
+ stt->lowerLimit = stt->startLowerLimit;
+}
+
+void WebRtcAgc_SaturationCtrl(Agc_t *stt, WebRtc_UWord8 *saturated, WebRtc_Word32 *env)
+{
+ WebRtc_Word16 i, tmpW16;
+
+ /* Check if the signal is saturated */
+ for (i = 0; i < 10; i++)
+ {
+ tmpW16 = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(env[i], 20);
+ if (tmpW16 > 875)
+ {
+ stt->envSum += tmpW16;
+ }
+ }
+
+ if (stt->envSum > 25000)
+ {
+ *saturated = 1;
+ stt->envSum = 0;
+ }
+
+ /* stt->envSum *= 0.99; */
+ stt->envSum = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(stt->envSum,
+ (WebRtc_Word16)32440, 15);
+}
+
+void WebRtcAgc_ZeroCtrl(Agc_t *stt, WebRtc_Word32 *inMicLevel, WebRtc_Word32 *env)
+{
+ WebRtc_Word16 i;
+ WebRtc_Word32 tmp32 = 0;
+ WebRtc_Word32 midVal;
+
+ /* Is the input signal zero? */
+ for (i = 0; i < 10; i++)
+ {
+ tmp32 += env[i];
+ }
+
+ /* Each block is allowed to have a few non-zero
+ * samples.
+ */
+ if (tmp32 < 500)
+ {
+ stt->msZero += 10;
+ } else
+ {
+ stt->msZero = 0;
+ }
+
+ if (stt->muteGuardMs > 0)
+ {
+ stt->muteGuardMs -= 10;
+ }
+
+ if (stt->msZero > 500)
+ {
+ stt->msZero = 0;
+
+ /* Increase microphone level only if it's less than 50% */
+ midVal = WEBRTC_SPL_RSHIFT_W32(stt->maxAnalog + stt->minLevel + 1, 1);
+ if (*inMicLevel < midVal)
+ {
+ /* *inMicLevel *= 1.1; */
+ tmp32 = WEBRTC_SPL_MUL(1126, *inMicLevel);
+ *inMicLevel = WEBRTC_SPL_RSHIFT_W32(tmp32, 10);
+ /* Reduces risk of a muted mic repeatedly triggering excessive levels due
+ * to zero signal detection. */
+ *inMicLevel = WEBRTC_SPL_MIN(*inMicLevel, stt->zeroCtrlMax);
+ stt->micVol = *inMicLevel;
+ }
+
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\t\tAGC->zeroCntrl, frame %d: 500 ms under threshold, micVol:\n",
+ stt->fcount, stt->micVol);
+#endif
+
+ stt->activeSpeech = 0;
+ stt->Rxx16_LPw32Max = 0;
+
+ /* The AGC has a tendency (due to problems with the VAD parameters), to
+ * vastly increase the volume after a muting event. This timer prevents
+ * upwards adaptation for a short period. */
+ stt->muteGuardMs = kMuteGuardTimeMs;
+ }
+}
+
+void WebRtcAgc_SpeakerInactiveCtrl(Agc_t *stt)
+{
+ /* Check if the near end speaker is inactive.
+ * If that is the case the VAD threshold is
+ * increased since the VAD speech model gets
+ * more sensitive to any sound after a long
+ * silence.
+ */
+
+ WebRtc_Word32 tmp32;
+ WebRtc_Word16 vadThresh;
+
+ if (stt->vadMic.stdLongTerm < 2500)
+ {
+ stt->vadThreshold = 1500;
+ } else
+ {
+ vadThresh = kNormalVadThreshold;
+ if (stt->vadMic.stdLongTerm < 4500)
+ {
+ /* Scale between min and max threshold */
+ vadThresh += WEBRTC_SPL_RSHIFT_W16(4500 - stt->vadMic.stdLongTerm, 1);
+ }
+
+ /* stt->vadThreshold = (31 * stt->vadThreshold + vadThresh) / 32; */
+ tmp32 = (WebRtc_Word32)vadThresh;
+ tmp32 += WEBRTC_SPL_MUL_16_16((WebRtc_Word16)31, stt->vadThreshold);
+ stt->vadThreshold = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 5);
+ }
+}
+
+void WebRtcAgc_ExpCurve(WebRtc_Word16 volume, WebRtc_Word16 *index)
+{
+ // volume in Q14
+ // index in [0-7]
+ /* 8 different curves */
+ if (volume > 5243)
+ {
+ if (volume > 7864)
+ {
+ if (volume > 12124)
+ {
+ *index = 7;
+ } else
+ {
+ *index = 6;
+ }
+ } else
+ {
+ if (volume > 6554)
+ {
+ *index = 5;
+ } else
+ {
+ *index = 4;
+ }
+ }
+ } else
+ {
+ if (volume > 2621)
+ {
+ if (volume > 3932)
+ {
+ *index = 3;
+ } else
+ {
+ *index = 2;
+ }
+ } else
+ {
+ if (volume > 1311)
+ {
+ *index = 1;
+ } else
+ {
+ *index = 0;
+ }
+ }
+ }
+}
+
+WebRtc_Word32 WebRtcAgc_ProcessAnalog(void *state, WebRtc_Word32 inMicLevel,
+ WebRtc_Word32 *outMicLevel,
+ WebRtc_Word16 vadLogRatio,
+ WebRtc_Word16 echo, WebRtc_UWord8 *saturationWarning)
+{
+ WebRtc_UWord32 tmpU32;
+ WebRtc_Word32 Rxx16w32, tmp32;
+ WebRtc_Word32 inMicLevelTmp, lastMicVol;
+ WebRtc_Word16 i;
+ WebRtc_UWord8 saturated = 0;
+ Agc_t *stt;
+
+ stt = (Agc_t *)state;
+ inMicLevelTmp = WEBRTC_SPL_LSHIFT_W32(inMicLevel, stt->scale);
+
+ if (inMicLevelTmp > stt->maxAnalog)
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt, "\tAGC->ProcessAnalog, frame %d: micLvl > maxAnalog\n", stt->fcount);
+#endif
+ return -1;
+ } else if (inMicLevelTmp < stt->minLevel)
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt, "\tAGC->ProcessAnalog, frame %d: micLvl < minLevel\n", stt->fcount);
+#endif
+ return -1;
+ }
+
+ if (stt->firstCall == 0)
+ {
+ WebRtc_Word32 tmpVol;
+ stt->firstCall = 1;
+ tmp32 = WEBRTC_SPL_RSHIFT_W32((stt->maxLevel - stt->minLevel) * (WebRtc_Word32)51, 9);
+ tmpVol = (stt->minLevel + tmp32);
+
+ /* If the mic level is very low at start, increase it! */
+ if ((inMicLevelTmp < tmpVol) && (stt->agcMode == kAgcModeAdaptiveAnalog))
+ {
+ inMicLevelTmp = tmpVol;
+ }
+ stt->micVol = inMicLevelTmp;
+ }
+
+ /* Set the mic level to the previous output value if there is digital input gain */
+ if ((inMicLevelTmp == stt->maxAnalog) && (stt->micVol > stt->maxAnalog))
+ {
+ inMicLevelTmp = stt->micVol;
+ }
+
+ /* If the mic level was manually changed to a very low value raise it! */
+ if ((inMicLevelTmp != stt->micVol) && (inMicLevelTmp < stt->minOutput))
+ {
+ tmp32 = WEBRTC_SPL_RSHIFT_W32((stt->maxLevel - stt->minLevel) * (WebRtc_Word32)51, 9);
+ inMicLevelTmp = (stt->minLevel + tmp32);
+ stt->micVol = inMicLevelTmp;
+#ifdef MIC_LEVEL_FEEDBACK
+ //stt->numBlocksMicLvlSat = 0;
+#endif
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: micLvl < minLevel by manual decrease, raise vol\n",
+ stt->fcount);
+#endif
+ }
+
+ if (inMicLevelTmp != stt->micVol)
+ {
+ // Incoming level mismatch; update our level.
+ // This could be the case if the volume is changed manually, or if the
+ // sound device has a low volume resolution.
+ stt->micVol = inMicLevelTmp;
+ }
+
+ if (inMicLevelTmp > stt->maxLevel)
+ {
+ // Always allow the user to raise the volume above the maxLevel.
+ stt->maxLevel = inMicLevelTmp;
+ }
+
+ // Store last value here, after we've taken care of manual updates etc.
+ lastMicVol = stt->micVol;
+
+ /* Checks if the signal is saturated. Also a check if individual samples
+ * are larger than 12000 is done. If they are the counter for increasing
+ * the volume level is set to -100ms
+ */
+ WebRtcAgc_SaturationCtrl(stt, &saturated, stt->env[0]);
+
+ /* The AGC is always allowed to lower the level if the signal is saturated */
+ if (saturated == 1)
+ {
+ /* Lower the recording level
+ * Rxx160_LP is adjusted down because it is so slow it could
+ * cause the AGC to make wrong decisions. */
+ /* stt->Rxx160_LPw32 *= 0.875; */
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(WEBRTC_SPL_RSHIFT_W32(stt->Rxx160_LPw32, 3), 7);
+
+ stt->zeroCtrlMax = stt->micVol;
+
+ /* stt->micVol *= 0.903; */
+ tmp32 = inMicLevelTmp - stt->minLevel;
+ tmpU32 = WEBRTC_SPL_UMUL(29591, (WebRtc_UWord32)(tmp32));
+ stt->micVol = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_U32(tmpU32, 15) + stt->minLevel;
+ if (stt->micVol > lastMicVol - 2)
+ {
+ stt->micVol = lastMicVol - 2;
+ }
+ inMicLevelTmp = stt->micVol;
+
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: saturated, micVol = %d\n",
+ stt->fcount, stt->micVol);
+#endif
+
+ if (stt->micVol < stt->minOutput)
+ {
+ *saturationWarning = 1;
+ }
+
+ /* Reset counter for decrease of volume level to avoid
+ * decreasing too much. The saturation control can still
+ * lower the level if needed. */
+ stt->msTooHigh = -100;
+
+ /* Enable the control mechanism to ensure that our measure,
+ * Rxx160_LP, is in the correct range. This must be done since
+ * the measure is very slow. */
+ stt->activeSpeech = 0;
+ stt->Rxx16_LPw32Max = 0;
+
+ /* Reset to initial values */
+ stt->msecSpeechInnerChange = kMsecSpeechInner;
+ stt->msecSpeechOuterChange = kMsecSpeechOuter;
+ stt->changeToSlowMode = 0;
+
+ stt->muteGuardMs = 0;
+
+ stt->upperLimit = stt->startUpperLimit;
+ stt->lowerLimit = stt->startLowerLimit;
+#ifdef MIC_LEVEL_FEEDBACK
+ //stt->numBlocksMicLvlSat = 0;
+#endif
+ }
+
+ /* Check if the input speech is zero. If so the mic volume
+ * is increased. On some computers the input is zero up as high
+ * level as 17% */
+ WebRtcAgc_ZeroCtrl(stt, &inMicLevelTmp, stt->env[0]);
+
+ /* Check if the near end speaker is inactive.
+ * If that is the case the VAD threshold is
+ * increased since the VAD speech model gets
+ * more sensitive to any sound after a long
+ * silence.
+ */
+ WebRtcAgc_SpeakerInactiveCtrl(stt);
+
+ for (i = 0; i < 5; i++)
+ {
+ /* Computed on blocks of 16 samples */
+
+ Rxx16w32 = stt->Rxx16w32_array[0][i];
+
+ /* Rxx160w32 in Q(-7) */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(Rxx16w32 - stt->Rxx16_vectorw32[stt->Rxx16pos], 3);
+ stt->Rxx160w32 = stt->Rxx160w32 + tmp32;
+ stt->Rxx16_vectorw32[stt->Rxx16pos] = Rxx16w32;
+
+ /* Circular buffer */
+ stt->Rxx16pos = stt->Rxx16pos++;
+ if (stt->Rxx16pos == RXX_BUFFER_LEN)
+ {
+ stt->Rxx16pos = 0;
+ }
+
+ /* Rxx16_LPw32 in Q(-4) */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(Rxx16w32 - stt->Rxx16_LPw32, kAlphaShortTerm);
+ stt->Rxx16_LPw32 = (stt->Rxx16_LPw32) + tmp32;
+
+ if (vadLogRatio > stt->vadThreshold)
+ {
+ /* Speech detected! */
+
+ /* Check if Rxx160_LP is in the correct range. If
+ * it is too high/low then we set it to the maximum of
+ * Rxx16_LPw32 during the first 200ms of speech.
+ */
+ if (stt->activeSpeech < 250)
+ {
+ stt->activeSpeech += 2;
+
+ if (stt->Rxx16_LPw32 > stt->Rxx16_LPw32Max)
+ {
+ stt->Rxx16_LPw32Max = stt->Rxx16_LPw32;
+ }
+ } else if (stt->activeSpeech == 250)
+ {
+ stt->activeSpeech += 2;
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx16_LPw32Max, 3);
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(tmp32, RXX_BUFFER_LEN);
+ }
+
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx160w32 - stt->Rxx160_LPw32, kAlphaLongTerm);
+ stt->Rxx160_LPw32 = stt->Rxx160_LPw32 + tmp32;
+
+ if (stt->Rxx160_LPw32 > stt->upperSecondaryLimit)
+ {
+ stt->msTooHigh += 2;
+ stt->msTooLow = 0;
+ stt->changeToSlowMode = 0;
+
+ if (stt->msTooHigh > stt->msecSpeechOuterChange)
+ {
+ stt->msTooHigh = 0;
+
+ /* Lower the recording level */
+ /* Multiply by 0.828125 which corresponds to decreasing ~0.8dB */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx160_LPw32, 6);
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(tmp32, 53);
+
+ /* Reduce the max gain to avoid excessive oscillation
+ * (but never drop below the maximum analog level).
+ * stt->maxLevel = (15 * stt->maxLevel + stt->micVol) / 16;
+ */
+ tmp32 = (15 * stt->maxLevel) + stt->micVol;
+ stt->maxLevel = WEBRTC_SPL_RSHIFT_W32(tmp32, 4);
+ stt->maxLevel = WEBRTC_SPL_MAX(stt->maxLevel, stt->maxAnalog);
+
+ stt->zeroCtrlMax = stt->micVol;
+
+ /* 0.95 in Q15 */
+ tmp32 = inMicLevelTmp - stt->minLevel;
+ tmpU32 = WEBRTC_SPL_UMUL(31130, (WebRtc_UWord32)(tmp32));
+ stt->micVol = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_U32(tmpU32, 15) + stt->minLevel;
+ if (stt->micVol > lastMicVol - 1)
+ {
+ stt->micVol = lastMicVol - 1;
+ }
+ inMicLevelTmp = stt->micVol;
+
+ /* Enable the control mechanism to ensure that our measure,
+ * Rxx160_LP, is in the correct range.
+ */
+ stt->activeSpeech = 0;
+ stt->Rxx16_LPw32Max = 0;
+#ifdef MIC_LEVEL_FEEDBACK
+ //stt->numBlocksMicLvlSat = 0;
+#endif
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: measure > 2ndUpperLim, micVol = %d, maxLevel = %d\n",
+ stt->fcount, stt->micVol, stt->maxLevel);
+#endif
+ }
+ } else if (stt->Rxx160_LPw32 > stt->upperLimit)
+ {
+ stt->msTooHigh += 2;
+ stt->msTooLow = 0;
+ stt->changeToSlowMode = 0;
+
+ if (stt->msTooHigh > stt->msecSpeechInnerChange)
+ {
+ /* Lower the recording level */
+ stt->msTooHigh = 0;
+ /* Multiply by 0.828125 which corresponds to decreasing ~0.8dB */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx160_LPw32, 6);
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(tmp32, 53);
+
+ /* Reduce the max gain to avoid excessive oscillation
+ * (but never drop below the maximum analog level).
+ * stt->maxLevel = (15 * stt->maxLevel + stt->micVol) / 16;
+ */
+ tmp32 = (15 * stt->maxLevel) + stt->micVol;
+ stt->maxLevel = WEBRTC_SPL_RSHIFT_W32(tmp32, 4);
+ stt->maxLevel = WEBRTC_SPL_MAX(stt->maxLevel, stt->maxAnalog);
+
+ stt->zeroCtrlMax = stt->micVol;
+
+ /* 0.965 in Q15 */
+ tmp32 = inMicLevelTmp - stt->minLevel;
+ tmpU32 = WEBRTC_SPL_UMUL(31621, (WebRtc_UWord32)(inMicLevelTmp - stt->minLevel));
+ stt->micVol = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_U32(tmpU32, 15) + stt->minLevel;
+ if (stt->micVol > lastMicVol - 1)
+ {
+ stt->micVol = lastMicVol - 1;
+ }
+ inMicLevelTmp = stt->micVol;
+
+#ifdef MIC_LEVEL_FEEDBACK
+ //stt->numBlocksMicLvlSat = 0;
+#endif
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: measure > UpperLim, micVol = %d, maxLevel = %d\n",
+ stt->fcount, stt->micVol, stt->maxLevel);
+#endif
+ }
+ } else if (stt->Rxx160_LPw32 < stt->lowerSecondaryLimit)
+ {
+ stt->msTooHigh = 0;
+ stt->changeToSlowMode = 0;
+ stt->msTooLow += 2;
+
+ if (stt->msTooLow > stt->msecSpeechOuterChange)
+ {
+ /* Raise the recording level */
+ WebRtc_Word16 index, weightFIX;
+ WebRtc_Word16 volNormFIX = 16384; // =1 in Q14.
+
+ stt->msTooLow = 0;
+
+ /* Normalize the volume level */
+ tmp32 = WEBRTC_SPL_LSHIFT_W32(inMicLevelTmp - stt->minLevel, 14);
+ if (stt->maxInit != stt->minLevel)
+ {
+ volNormFIX = (WebRtc_Word16)WEBRTC_SPL_DIV(tmp32,
+ (stt->maxInit - stt->minLevel));
+ }
+
+ /* Find correct curve */
+ WebRtcAgc_ExpCurve(volNormFIX, &index);
+
+ /* Compute weighting factor for the volume increase, 32^(-2*X)/2+1.05 */
+ weightFIX = kOffset1[index]
+ - (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(kSlope1[index],
+ volNormFIX, 13);
+
+ /* stt->Rxx160_LPw32 *= 1.047 [~0.2 dB]; */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx160_LPw32, 6);
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(tmp32, 67);
+
+ tmp32 = inMicLevelTmp - stt->minLevel;
+ tmpU32 = ((WebRtc_UWord32)weightFIX * (WebRtc_UWord32)(inMicLevelTmp - stt->minLevel));
+ stt->micVol = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_U32(tmpU32, 14) + stt->minLevel;
+ if (stt->micVol < lastMicVol + 2)
+ {
+ stt->micVol = lastMicVol + 2;
+ }
+
+ inMicLevelTmp = stt->micVol;
+
+#ifdef MIC_LEVEL_FEEDBACK
+ /* Count ms in level saturation */
+ //if (stt->micVol > stt->maxAnalog) {
+ if (stt->micVol > 150)
+ {
+ /* mic level is saturated */
+ stt->numBlocksMicLvlSat++;
+ fprintf(stderr, "Sat mic Level: %d\n", stt->numBlocksMicLvlSat);
+ }
+#endif
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: measure < 2ndLowerLim, micVol = %d\n",
+ stt->fcount, stt->micVol);
+#endif
+ }
+ } else if (stt->Rxx160_LPw32 < stt->lowerLimit)
+ {
+ stt->msTooHigh = 0;
+ stt->changeToSlowMode = 0;
+ stt->msTooLow += 2;
+
+ if (stt->msTooLow > stt->msecSpeechInnerChange)
+ {
+ /* Raise the recording level */
+ WebRtc_Word16 index, weightFIX;
+ WebRtc_Word16 volNormFIX = 16384; // =1 in Q14.
+
+ stt->msTooLow = 0;
+
+ /* Normalize the volume level */
+ tmp32 = WEBRTC_SPL_LSHIFT_W32(inMicLevelTmp - stt->minLevel, 14);
+ if (stt->maxInit != stt->minLevel)
+ {
+ volNormFIX = (WebRtc_Word16)WEBRTC_SPL_DIV(tmp32,
+ (stt->maxInit - stt->minLevel));
+ }
+
+ /* Find correct curve */
+ WebRtcAgc_ExpCurve(volNormFIX, &index);
+
+ /* Compute weighting factor for the volume increase, (3.^(-2.*X))/8+1 */
+ weightFIX = kOffset2[index]
+ - (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(kSlope2[index],
+ volNormFIX, 13);
+
+ /* stt->Rxx160_LPw32 *= 1.047 [~0.2 dB]; */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(stt->Rxx160_LPw32, 6);
+ stt->Rxx160_LPw32 = WEBRTC_SPL_MUL(tmp32, 67);
+
+ tmp32 = inMicLevelTmp - stt->minLevel;
+ tmpU32 = ((WebRtc_UWord32)weightFIX * (WebRtc_UWord32)(inMicLevelTmp - stt->minLevel));
+ stt->micVol = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_U32(tmpU32, 14) + stt->minLevel;
+ if (stt->micVol < lastMicVol + 1)
+ {
+ stt->micVol = lastMicVol + 1;
+ }
+
+ inMicLevelTmp = stt->micVol;
+
+#ifdef MIC_LEVEL_FEEDBACK
+ /* Count ms in level saturation */
+ //if (stt->micVol > stt->maxAnalog) {
+ if (stt->micVol > 150)
+ {
+ /* mic level is saturated */
+ stt->numBlocksMicLvlSat++;
+ fprintf(stderr, "Sat mic Level: %d\n", stt->numBlocksMicLvlSat);
+ }
+#endif
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "\tAGC->ProcessAnalog, frame %d: measure < LowerLim, micVol = %d\n",
+ stt->fcount, stt->micVol);
+#endif
+
+ }
+ } else
+ {
+ /* The signal is inside the desired range which is:
+ * lowerLimit < Rxx160_LP/640 < upperLimit
+ */
+ if (stt->changeToSlowMode > 4000)
+ {
+ stt->msecSpeechInnerChange = 1000;
+ stt->msecSpeechOuterChange = 500;
+ stt->upperLimit = stt->upperPrimaryLimit;
+ stt->lowerLimit = stt->lowerPrimaryLimit;
+ } else
+ {
+ stt->changeToSlowMode += 2; // in milliseconds
+ }
+ stt->msTooLow = 0;
+ stt->msTooHigh = 0;
+
+ stt->micVol = inMicLevelTmp;
+
+ }
+#ifdef MIC_LEVEL_FEEDBACK
+ if (stt->numBlocksMicLvlSat > NUM_BLOCKS_IN_SAT_BEFORE_CHANGE_TARGET)
+ {
+ stt->micLvlSat = 1;
+ fprintf(stderr, "target before = %d (%d)\n", stt->analogTargetLevel, stt->targetIdx);
+ WebRtcAgc_UpdateAgcThresholds(stt);
+ WebRtcAgc_CalculateGainTable(&(stt->digitalAgc.gainTable[0]),
+ stt->compressionGaindB, stt->targetLevelDbfs, stt->limiterEnable,
+ stt->analogTarget);
+ stt->numBlocksMicLvlSat = 0;
+ stt->micLvlSat = 0;
+ fprintf(stderr, "target offset = %d\n", stt->targetIdxOffset);
+ fprintf(stderr, "target after = %d (%d)\n", stt->analogTargetLevel, stt->targetIdx);
+ }
+#endif
+ }
+ }
+
+ /* Ensure gain is not increased in presence of echo or after a mute event
+ * (but allow the zeroCtrl() increase on the frame of a mute detection).
+ */
+ if (echo == 1 || (stt->muteGuardMs > 0 && stt->muteGuardMs < kMuteGuardTimeMs))
+ {
+ if (stt->micVol > lastMicVol)
+ {
+ stt->micVol = lastMicVol;
+ }
+ }
+
+ /* limit the gain */
+ if (stt->micVol > stt->maxLevel)
+ {
+ stt->micVol = stt->maxLevel;
+ } else if (stt->micVol < stt->minOutput)
+ {
+ stt->micVol = stt->minOutput;
+ }
+
+ *outMicLevel = WEBRTC_SPL_RSHIFT_W32(stt->micVol, stt->scale);
+ if (*outMicLevel > WEBRTC_SPL_RSHIFT_W32(stt->maxAnalog, stt->scale))
+ {
+ *outMicLevel = WEBRTC_SPL_RSHIFT_W32(stt->maxAnalog, stt->scale);
+ }
+
+ return 0;
+}
+
+int WebRtcAgc_Process(void *agcInst, const WebRtc_Word16 *in_near,
+ const WebRtc_Word16 *in_near_H, WebRtc_Word16 samples,
+ WebRtc_Word16 *out, WebRtc_Word16 *out_H, WebRtc_Word32 inMicLevel,
+ WebRtc_Word32 *outMicLevel, WebRtc_Word16 echo,
+ WebRtc_UWord8 *saturationWarning)
+{
+ Agc_t *stt;
+ WebRtc_Word32 inMicLevelTmp;
+ WebRtc_Word16 subFrames, i;
+ WebRtc_UWord8 satWarningTmp = 0;
+
+ stt = (Agc_t *)agcInst;
+
+ //
+ if (stt == NULL)
+ {
+ return -1;
+ }
+ //
+
+
+ if (stt->fs == 8000)
+ {
+ if ((samples != 80) && (samples != 160))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->Process, frame %d: Invalid number of samples\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 80;
+ } else if (stt->fs == 16000)
+ {
+ if ((samples != 160) && (samples != 320))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->Process, frame %d: Invalid number of samples\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 160;
+ } else if (stt->fs == 32000)
+ {
+ if ((samples != 160) && (samples != 320))
+ {
+#ifdef AGC_DEBUG //test log
+ fprintf(stt->fpt,
+ "AGC->Process, frame %d: Invalid number of samples\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+ subFrames = 160;
+ } else
+ {
+#ifdef AGC_DEBUG// test log
+ fprintf(stt->fpt,
+ "AGC->Process, frame %d: Invalid sample rate\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+
+ /* Check for valid pointers based on sampling rate */
+ if (stt->fs == 32000 && in_near_H == NULL)
+ {
+ return -1;
+ }
+ /* Check for valid pointers for low band */
+ if (in_near == NULL)
+ {
+ return -1;
+ }
+
+ *saturationWarning = 0;
+ //TODO: PUT IN RANGE CHECKING FOR INPUT LEVELS
+ *outMicLevel = inMicLevel;
+ inMicLevelTmp = inMicLevel;
+
+ memcpy(out, in_near, samples * sizeof(WebRtc_Word16));
+ if (stt->fs == 32000)
+ {
+ memcpy(out_H, in_near_H, samples * sizeof(WebRtc_Word16));
+ }
+
+#ifdef AGC_DEBUG//test log
+ stt->fcount++;
+#endif
+
+ for (i = 0; i < samples; i += subFrames)
+ {
+ if (WebRtcAgc_ProcessDigital(&stt->digitalAgc, &in_near[i], &in_near_H[i], &out[i], &out_H[i],
+ stt->fs, stt->lowLevelSignal) == -1)
+ {
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt, "AGC->Process, frame %d: Error from DigAGC\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+ if ((stt->agcMode < kAgcModeFixedDigital) && ((stt->lowLevelSignal == 0)
+ || (stt->agcMode != kAgcModeAdaptiveDigital)))
+ {
+ if (WebRtcAgc_ProcessAnalog(agcInst, inMicLevelTmp, outMicLevel,
+ stt->vadMic.logRatio, echo, saturationWarning) == -1)
+ {
+ return -1;
+ }
+ }
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->agcLog, "%5d\t%d\t%d\t%d\n", stt->fcount, inMicLevelTmp, *outMicLevel, stt->maxLevel, stt->micVol);
+#endif
+
+ /* update queue */
+ if (stt->inQueue > 1)
+ {
+ memcpy(stt->env[0], stt->env[1], 10 * sizeof(WebRtc_Word32));
+ memcpy(stt->Rxx16w32_array[0], stt->Rxx16w32_array[1], 5 * sizeof(WebRtc_Word32));
+ }
+
+ if (stt->inQueue > 0)
+ {
+ stt->inQueue--;
+ }
+
+ /* If 20ms frames are used the input mic level must be updated so that
+ * the analog AGC does not think that there has been a manual volume
+ * change. */
+ inMicLevelTmp = *outMicLevel;
+
+ /* Store a positive saturation warning. */
+ if (*saturationWarning == 1)
+ {
+ satWarningTmp = 1;
+ }
+ }
+
+ /* Trigger the saturation warning if displayed by any of the frames. */
+ *saturationWarning = satWarningTmp;
+
+ return 0;
+}
+
+int WebRtcAgc_set_config(void *agcInst, WebRtcAgc_config_t agcConfig)
+{
+ Agc_t *stt;
+ stt = (Agc_t *)agcInst;
+
+ if (stt == NULL)
+ {
+ return -1;
+ }
+
+ if (stt->initFlag != kInitCheck)
+ {
+ stt->lastError = AGC_UNINITIALIZED_ERROR;
+ return -1;
+ }
+
+ if (agcConfig.limiterEnable != kAgcFalse && agcConfig.limiterEnable != kAgcTrue)
+ {
+ stt->lastError = AGC_BAD_PARAMETER_ERROR;
+ return -1;
+ }
+ stt->limiterEnable = agcConfig.limiterEnable;
+ stt->compressionGaindB = agcConfig.compressionGaindB;
+ if ((agcConfig.targetLevelDbfs < 0) || (agcConfig.targetLevelDbfs > 31))
+ {
+ stt->lastError = AGC_BAD_PARAMETER_ERROR;
+ return -1;
+ }
+ stt->targetLevelDbfs = agcConfig.targetLevelDbfs;
+
+ if (stt->agcMode == kAgcModeFixedDigital)
+ {
+ /* Adjust for different parameter interpretation in FixedDigital mode */
+ stt->compressionGaindB += agcConfig.targetLevelDbfs;
+ }
+
+ /* Update threshold levels for analog adaptation */
+ WebRtcAgc_UpdateAgcThresholds(stt);
+
+ /* Recalculate gain table */
+ if (WebRtcAgc_CalculateGainTable(&(stt->digitalAgc.gainTable[0]), stt->compressionGaindB,
+ stt->targetLevelDbfs, stt->limiterEnable, stt->analogTarget) == -1)
+ {
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt, "AGC->set_config, frame %d: Error from calcGainTable\n\n", stt->fcount);
+#endif
+ return -1;
+ }
+ /* Store the config in a WebRtcAgc_config_t */
+ stt->usedConfig.compressionGaindB = agcConfig.compressionGaindB;
+ stt->usedConfig.limiterEnable = agcConfig.limiterEnable;
+ stt->usedConfig.targetLevelDbfs = agcConfig.targetLevelDbfs;
+
+ return 0;
+}
+
+int WebRtcAgc_get_config(void *agcInst, WebRtcAgc_config_t *config)
+{
+ Agc_t *stt;
+ stt = (Agc_t *)agcInst;
+
+ if (stt == NULL)
+ {
+ return -1;
+ }
+
+ if (config == NULL)
+ {
+ stt->lastError = AGC_NULL_POINTER_ERROR;
+ return -1;
+ }
+
+ if (stt->initFlag != kInitCheck)
+ {
+ stt->lastError = AGC_UNINITIALIZED_ERROR;
+ return -1;
+ }
+
+ config->limiterEnable = stt->usedConfig.limiterEnable;
+ config->targetLevelDbfs = stt->usedConfig.targetLevelDbfs;
+ config->compressionGaindB = stt->usedConfig.compressionGaindB;
+
+ return 0;
+}
+
+int WebRtcAgc_Create(void **agcInst)
+{
+ Agc_t *stt;
+ if (agcInst == NULL)
+ {
+ return -1;
+ }
+ stt = (Agc_t *)malloc(sizeof(Agc_t));
+
+ *agcInst = stt;
+ if (stt == NULL)
+ {
+ return -1;
+ }
+
+#ifdef AGC_DEBUG
+ stt->fpt = fopen("./agc_test_log.txt", "wt");
+ stt->agcLog = fopen("./agc_debug_log.txt", "wt");
+ stt->digitalAgc.logFile = fopen("./agc_log.txt", "wt");
+#endif
+
+ stt->initFlag = 0;
+ stt->lastError = 0;
+
+ return 0;
+}
+
+int WebRtcAgc_Free(void *state)
+{
+ Agc_t *stt;
+
+ stt = (Agc_t *)state;
+#ifdef AGC_DEBUG
+ fclose(stt->fpt);
+ fclose(stt->agcLog);
+ fclose(stt->digitalAgc.logFile);
+#endif
+ free(stt);
+
+ return 0;
+}
+
+/* minLevel - Minimum volume level
+ * maxLevel - Maximum volume level
+ */
+int WebRtcAgc_Init(void *agcInst, WebRtc_Word32 minLevel, WebRtc_Word32 maxLevel,
+ WebRtc_Word16 agcMode, WebRtc_UWord32 fs)
+{
+ WebRtc_Word32 max_add, tmp32;
+ WebRtc_Word16 i;
+ int tmpNorm;
+ Agc_t *stt;
+
+ /* typecast state pointer */
+ stt = (Agc_t *)agcInst;
+
+ if (WebRtcAgc_InitDigital(&stt->digitalAgc, agcMode) != 0)
+ {
+ stt->lastError = AGC_UNINITIALIZED_ERROR;
+ return -1;
+ }
+
+ /* Analog AGC variables */
+ stt->envSum = 0;
+
+ /* mode = 0 - Only saturation protection
+ * 1 - Analog Automatic Gain Control [-targetLevelDbfs (default -3 dBOv)]
+ * 2 - Digital Automatic Gain Control [-targetLevelDbfs (default -3 dBOv)]
+ * 3 - Fixed Digital Gain [compressionGaindB (default 8 dB)]
+ */
+#ifdef AGC_DEBUG//test log
+ stt->fcount = 0;
+ fprintf(stt->fpt, "AGC->Init\n");
+#endif
+ if (agcMode < kAgcModeUnchanged || agcMode > kAgcModeFixedDigital)
+ {
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt, "AGC->Init: error, incorrect mode\n\n");
+#endif
+ return -1;
+ }
+ stt->agcMode = agcMode;
+ stt->fs = fs;
+
+ /* initialize input VAD */
+ WebRtcAgc_InitVad(&stt->vadMic);
+
+ /* If the volume range is smaller than 0-256 then
+ * the levels are shifted up to Q8-domain */
+ tmpNorm = WebRtcSpl_NormU32((WebRtc_UWord32)maxLevel);
+ stt->scale = tmpNorm - 23;
+ if (stt->scale < 0)
+ {
+ stt->scale = 0;
+ }
+ // TODO(bjornv): Investigate if we really need to scale up a small range now when we have
+ // a guard against zero-increments. For now, we do not support scale up (scale = 0).
+ stt->scale = 0;
+ maxLevel = WEBRTC_SPL_LSHIFT_W32(maxLevel, stt->scale);
+ minLevel = WEBRTC_SPL_LSHIFT_W32(minLevel, stt->scale);
+
+ /* Make minLevel and maxLevel static in AdaptiveDigital */
+ if (stt->agcMode == kAgcModeAdaptiveDigital)
+ {
+ minLevel = 0;
+ maxLevel = 255;
+ stt->scale = 0;
+ }
+ /* The maximum supplemental volume range is based on a vague idea
+ * of how much lower the gain will be than the real analog gain. */
+ max_add = WEBRTC_SPL_RSHIFT_W32(maxLevel - minLevel, 2);
+
+ /* Minimum/maximum volume level that can be set */
+ stt->minLevel = minLevel;
+ stt->maxAnalog = maxLevel;
+ stt->maxLevel = maxLevel + max_add;
+ stt->maxInit = stt->maxLevel;
+
+ stt->zeroCtrlMax = stt->maxAnalog;
+
+ /* Initialize micVol parameter */
+ stt->micVol = stt->maxAnalog;
+ if (stt->agcMode == kAgcModeAdaptiveDigital)
+ {
+ stt->micVol = 127; /* Mid-point of mic level */
+ }
+ stt->micRef = stt->micVol;
+ stt->micGainIdx = 127;
+#ifdef MIC_LEVEL_FEEDBACK
+ stt->numBlocksMicLvlSat = 0;
+ stt->micLvlSat = 0;
+#endif
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt,
+ "AGC->Init: minLevel = %d, maxAnalog = %d, maxLevel = %d\n",
+ stt->minLevel, stt->maxAnalog, stt->maxLevel);
+#endif
+
+ /* Minimum output volume is 4% higher than the available lowest volume level */
+ tmp32 = WEBRTC_SPL_RSHIFT_W32((stt->maxLevel - stt->minLevel) * (WebRtc_Word32)10, 8);
+ stt->minOutput = (stt->minLevel + tmp32);
+
+ stt->msTooLow = 0;
+ stt->msTooHigh = 0;
+ stt->changeToSlowMode = 0;
+ stt->firstCall = 0;
+ stt->msZero = 0;
+ stt->muteGuardMs = 0;
+ stt->gainTableIdx = 0;
+
+ stt->msecSpeechInnerChange = kMsecSpeechInner;
+ stt->msecSpeechOuterChange = kMsecSpeechOuter;
+
+ stt->activeSpeech = 0;
+ stt->Rxx16_LPw32Max = 0;
+
+ stt->vadThreshold = kNormalVadThreshold;
+ stt->inActive = 0;
+
+ for (i = 0; i < RXX_BUFFER_LEN; i++)
+ {
+ stt->Rxx16_vectorw32[i] = (WebRtc_Word32)1000; /* -54dBm0 */
+ }
+ stt->Rxx160w32 = 125 * RXX_BUFFER_LEN; /* (stt->Rxx16_vectorw32[0]>>3) = 125 */
+
+ stt->Rxx16pos = 0;
+ stt->Rxx16_LPw32 = (WebRtc_Word32)16284; /* Q(-4) */
+
+ for (i = 0; i < 5; i++)
+ {
+ stt->Rxx16w32_array[0][i] = 0;
+ }
+ for (i = 0; i < 20; i++)
+ {
+ stt->env[0][i] = 0;
+ }
+ stt->inQueue = 0;
+
+#ifdef MIC_LEVEL_FEEDBACK
+ stt->targetIdxOffset = 0;
+#endif
+
+ WebRtcSpl_MemSetW32(stt->filterState, 0, 8);
+
+ stt->initFlag = kInitCheck;
+ // Default config settings.
+ stt->defaultConfig.limiterEnable = kAgcTrue;
+ stt->defaultConfig.targetLevelDbfs = AGC_DEFAULT_TARGET_LEVEL;
+ stt->defaultConfig.compressionGaindB = AGC_DEFAULT_COMP_GAIN;
+
+ if (WebRtcAgc_set_config(stt, stt->defaultConfig) == -1)
+ {
+ stt->lastError = AGC_UNSPECIFIED_ERROR;
+ return -1;
+ }
+ stt->Rxx160_LPw32 = stt->analogTargetLevel; // Initialize rms value
+
+ stt->lowLevelSignal = 0;
+
+ /* Only positive values are allowed that are not too large */
+ if ((minLevel >= maxLevel) || (maxLevel & 0xFC000000))
+ {
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt, "minLevel, maxLevel value(s) are invalid\n\n");
+#endif
+ return -1;
+ } else
+ {
+#ifdef AGC_DEBUG//test log
+ fprintf(stt->fpt, "\n");
+#endif
+ return 0;
+ }
+}
+
+int WebRtcAgc_Version(WebRtc_Word8 *versionStr, WebRtc_Word16 length)
+{
+ const WebRtc_Word8 version[] = "AGC 1.7.0";
+ const WebRtc_Word16 versionLen = (WebRtc_Word16)strlen(version) + 1;
+
+ if (versionStr == NULL)
+ {
+ return -1;
+ }
+
+ if (versionLen > length)
+ {
+ return -1;
+ }
+
+ strncpy(versionStr, version, versionLen);
+ return 0;
+}
diff --git a/src/modules/audio_processing/agc/main/source/analog_agc.h b/src/modules/audio_processing/agc/main/source/analog_agc.h
new file mode 100644
index 0000000000..b32ac6581e
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/analog_agc.h
@@ -0,0 +1,133 @@
+/*
+ * 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_AGC_MAIN_SOURCE_ANALOG_AGC_H_
+#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_
+
+#include "typedefs.h"
+#include "gain_control.h"
+#include "digital_agc.h"
+
+//#define AGC_DEBUG
+//#define MIC_LEVEL_FEEDBACK
+#ifdef AGC_DEBUG
+#include <stdio.h>
+#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 WebRtc_Word16 kMsecSpeechInner = 520;
+static const WebRtc_Word16 kMsecSpeechOuter = 340;
+
+static const WebRtc_Word16 kNormalVadThreshold = 400;
+
+static const WebRtc_Word16 kAlphaShortTerm = 6; // 1 >> 6 = 0.0156
+static const WebRtc_Word16 kAlphaLongTerm = 10; // 1 >> 10 = 0.000977
+
+typedef struct
+{
+ // Configurable parameters/variables
+ WebRtc_UWord32 fs; // Sampling frequency
+ WebRtc_Word16 compressionGaindB; // Fixed gain level in dB
+ WebRtc_Word16 targetLevelDbfs; // Target level in -dBfs of envelope (default -3)
+ WebRtc_Word16 agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig)
+ WebRtc_UWord8 limiterEnable; // Enabling limiter (on/off (default off))
+ WebRtcAgc_config_t defaultConfig;
+ WebRtcAgc_config_t usedConfig;
+
+ // General variables
+ WebRtc_Word16 initFlag;
+ WebRtc_Word16 lastError;
+
+ // Target level parameters
+ // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
+ WebRtc_Word32 analogTargetLevel; // = RXX_BUFFER_LEN * 846805; -22 dBfs
+ WebRtc_Word32 startUpperLimit; // = RXX_BUFFER_LEN * 1066064; -21 dBfs
+ WebRtc_Word32 startLowerLimit; // = RXX_BUFFER_LEN * 672641; -23 dBfs
+ WebRtc_Word32 upperPrimaryLimit; // = RXX_BUFFER_LEN * 1342095; -20 dBfs
+ WebRtc_Word32 lowerPrimaryLimit; // = RXX_BUFFER_LEN * 534298; -24 dBfs
+ WebRtc_Word32 upperSecondaryLimit;// = RXX_BUFFER_LEN * 2677832; -17 dBfs
+ WebRtc_Word32 lowerSecondaryLimit;// = RXX_BUFFER_LEN * 267783; -27 dBfs
+ WebRtc_UWord16 targetIdx; // Table index for corresponding target level
+#ifdef MIC_LEVEL_FEEDBACK
+ WebRtc_UWord16 targetIdxOffset; // Table index offset for level compensation
+#endif
+ WebRtc_Word16 analogTarget; // Digital reference level in ENV scale
+
+ // Analog AGC specific variables
+ WebRtc_Word32 filterState[8]; // For downsampling wb to nb
+ WebRtc_Word32 upperLimit; // Upper limit for mic energy
+ WebRtc_Word32 lowerLimit; // Lower limit for mic energy
+ WebRtc_Word32 Rxx160w32; // Average energy for one frame
+ WebRtc_Word32 Rxx16_LPw32; // Low pass filtered subframe energies
+ WebRtc_Word32 Rxx160_LPw32; // Low pass filtered frame energies
+ WebRtc_Word32 Rxx16_LPw32Max; // Keeps track of largest energy subframe
+ WebRtc_Word32 Rxx16_vectorw32[RXX_BUFFER_LEN];// Array with subframe energies
+ WebRtc_Word32 Rxx16w32_array[2][5];// Energy values of microphone signal
+ WebRtc_Word32 env[2][10]; // Envelope values of subframes
+
+ WebRtc_Word16 Rxx16pos; // Current position in the Rxx16_vectorw32
+ WebRtc_Word16 envSum; // Filtered scaled envelope in subframes
+ WebRtc_Word16 vadThreshold; // Threshold for VAD decision
+ WebRtc_Word16 inActive; // Inactive time in milliseconds
+ WebRtc_Word16 msTooLow; // Milliseconds of speech at a too low level
+ WebRtc_Word16 msTooHigh; // Milliseconds of speech at a too high level
+ WebRtc_Word16 changeToSlowMode; // Change to slow mode after some time at target
+ WebRtc_Word16 firstCall; // First call to the process-function
+ WebRtc_Word16 msZero; // Milliseconds of zero input
+ WebRtc_Word16 msecSpeechOuterChange;// Min ms of speech between volume changes
+ WebRtc_Word16 msecSpeechInnerChange;// Min ms of speech between volume changes
+ WebRtc_Word16 activeSpeech; // Milliseconds of active speech
+ WebRtc_Word16 muteGuardMs; // Counter to prevent mute action
+ WebRtc_Word16 inQueue; // 10 ms batch indicator
+
+ // Microphone level variables
+ WebRtc_Word32 micRef; // Remember ref. mic level for virtual mic
+ WebRtc_UWord16 gainTableIdx; // Current position in virtual gain table
+ WebRtc_Word32 micGainIdx; // Gain index of mic level to increase slowly
+ WebRtc_Word32 micVol; // Remember volume between frames
+ WebRtc_Word32 maxLevel; // Max possible vol level, incl dig gain
+ WebRtc_Word32 maxAnalog; // Maximum possible analog volume level
+ WebRtc_Word32 maxInit; // Initial value of "max"
+ WebRtc_Word32 minLevel; // Minimum possible volume level
+ WebRtc_Word32 minOutput; // Minimum output volume level
+ WebRtc_Word32 zeroCtrlMax; // Remember max gain => don't amp low input
+
+ WebRtc_Word16 scale; // Scale factor for internal volume levels
+#ifdef MIC_LEVEL_FEEDBACK
+ WebRtc_Word16 numBlocksMicLvlSat;
+ WebRtc_UWord8 micLvlSat;
+#endif
+ // Structs for VAD and digital_agc
+ AgcVad_t vadMic;
+ DigitalAgc_t digitalAgc;
+
+#ifdef AGC_DEBUG
+ FILE* fpt;
+ FILE* agcLog;
+ WebRtc_Word32 fcount;
+#endif
+
+ WebRtc_Word16 lowLevelSignal;
+} Agc_t;
+
+#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_
diff --git a/src/modules/audio_processing/agc/main/source/digital_agc.c b/src/modules/audio_processing/agc/main/source/digital_agc.c
new file mode 100644
index 0000000000..2966586e48
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/digital_agc.c
@@ -0,0 +1,780 @@
+/*
+ * 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.
+ */
+
+/* digital_agc.c
+ *
+ */
+
+#include <string.h>
+#ifdef AGC_DEBUG
+#include <stdio.h>
+#endif
+#include "digital_agc.h"
+#include "gain_control.h"
+
+// To generate the gaintable, copy&paste the following lines to a Matlab window:
+// MaxGain = 6; MinGain = 0; CompRatio = 3; Knee = 1;
+// zeros = 0:31; lvl = 2.^(1-zeros);
+// A = -10*log10(lvl) * (CompRatio - 1) / CompRatio;
+// B = MaxGain - MinGain;
+// gains = round(2^16*10.^(0.05 * (MinGain + B * ( log(exp(-Knee*A)+exp(-Knee*B)) - log(1+exp(-Knee*B)) ) / log(1/(1+exp(Knee*B))))));
+// fprintf(1, '\t%i, %i, %i, %i,\n', gains);
+// % Matlab code for plotting the gain and input/output level characteristic (copy/paste the following 3 lines):
+// in = 10*log10(lvl); out = 20*log10(gains/65536);
+// subplot(121); plot(in, out); axis([-30, 0, -5, 20]); grid on; xlabel('Input (dB)'); ylabel('Gain (dB)');
+// subplot(122); plot(in, in+out); axis([-30, 0, -30, 5]); grid on; xlabel('Input (dB)'); ylabel('Output (dB)');
+// zoom on;
+
+// Generator table for y=log2(1+e^x) in Q8.
+static const WebRtc_UWord16 kGenFuncTable[128] = {
+ 256, 485, 786, 1126, 1484, 1849, 2217, 2586,
+ 2955, 3324, 3693, 4063, 4432, 4801, 5171, 5540,
+ 5909, 6279, 6648, 7017, 7387, 7756, 8125, 8495,
+ 8864, 9233, 9603, 9972, 10341, 10711, 11080, 11449,
+ 11819, 12188, 12557, 12927, 13296, 13665, 14035, 14404,
+ 14773, 15143, 15512, 15881, 16251, 16620, 16989, 17359,
+ 17728, 18097, 18466, 18836, 19205, 19574, 19944, 20313,
+ 20682, 21052, 21421, 21790, 22160, 22529, 22898, 23268,
+ 23637, 24006, 24376, 24745, 25114, 25484, 25853, 26222,
+ 26592, 26961, 27330, 27700, 28069, 28438, 28808, 29177,
+ 29546, 29916, 30285, 30654, 31024, 31393, 31762, 32132,
+ 32501, 32870, 33240, 33609, 33978, 34348, 34717, 35086,
+ 35456, 35825, 36194, 36564, 36933, 37302, 37672, 38041,
+ 38410, 38780, 39149, 39518, 39888, 40257, 40626, 40996,
+ 41365, 41734, 42104, 42473, 42842, 43212, 43581, 43950,
+ 44320, 44689, 45058, 45428, 45797, 46166, 46536, 46905
+};
+
+static const WebRtc_Word16 kAvgDecayTime = 250; // frames; < 3000
+
+WebRtc_Word32 WebRtcAgc_CalculateGainTable(WebRtc_Word32 *gainTable, // Q16
+ WebRtc_Word16 digCompGaindB, // Q0
+ WebRtc_Word16 targetLevelDbfs,// Q0
+ WebRtc_UWord8 limiterEnable,
+ WebRtc_Word16 analogTarget) // Q0
+{
+ // This function generates the compressor gain table used in the fixed digital part.
+ WebRtc_UWord32 tmpU32no1, tmpU32no2, absInLevel, logApprox;
+ WebRtc_Word32 inLevel, limiterLvl;
+ WebRtc_Word32 tmp32, tmp32no1, tmp32no2, numFIX, den, y32;
+ const WebRtc_UWord16 kLog10 = 54426; // log2(10) in Q14
+ const WebRtc_UWord16 kLog10_2 = 49321; // 10*log10(2) in Q14
+ const WebRtc_UWord16 kLogE_1 = 23637; // log2(e) in Q14
+ WebRtc_UWord16 constMaxGain;
+ WebRtc_UWord16 tmpU16, intPart, fracPart;
+ const WebRtc_Word16 kCompRatio = 3;
+ const WebRtc_Word16 kSoftLimiterLeft = 1;
+ WebRtc_Word16 limiterOffset = 0; // Limiter offset
+ WebRtc_Word16 limiterIdx, limiterLvlX;
+ WebRtc_Word16 constLinApprox, zeroGainLvl, maxGain, diffGain;
+ WebRtc_Word16 i, tmp16, tmp16no1;
+ int zeros, zerosScale;
+
+ // Constants
+// kLogE_1 = 23637; // log2(e) in Q14
+// kLog10 = 54426; // log2(10) in Q14
+// kLog10_2 = 49321; // 10*log10(2) in Q14
+
+ // Calculate maximum digital gain and zero gain level
+ tmp32no1 = WEBRTC_SPL_MUL_16_16(digCompGaindB - analogTarget, kCompRatio - 1);
+ tmp16no1 = analogTarget - targetLevelDbfs;
+ tmp16no1 += WebRtcSpl_DivW32W16ResW16(tmp32no1 + (kCompRatio >> 1), kCompRatio);
+ maxGain = WEBRTC_SPL_MAX(tmp16no1, (analogTarget - targetLevelDbfs));
+ tmp32no1 = WEBRTC_SPL_MUL_16_16(maxGain, kCompRatio);
+ zeroGainLvl = digCompGaindB;
+ zeroGainLvl -= WebRtcSpl_DivW32W16ResW16(tmp32no1 + ((kCompRatio - 1) >> 1),
+ kCompRatio - 1);
+ if ((digCompGaindB <= analogTarget) && (limiterEnable))
+ {
+ zeroGainLvl += (analogTarget - digCompGaindB + kSoftLimiterLeft);
+ limiterOffset = 0;
+ }
+
+ // Calculate the difference between maximum gain and gain at 0dB0v:
+ // diffGain = maxGain + (compRatio-1)*zeroGainLvl/compRatio
+ // = (compRatio-1)*digCompGaindB/compRatio
+ tmp32no1 = WEBRTC_SPL_MUL_16_16(digCompGaindB, kCompRatio - 1);
+ diffGain = WebRtcSpl_DivW32W16ResW16(tmp32no1 + (kCompRatio >> 1), kCompRatio);
+ if (diffGain < 0)
+ {
+ return -1;
+ }
+
+ // Calculate the limiter level and index:
+ // limiterLvlX = analogTarget - limiterOffset
+ // limiterLvl = targetLevelDbfs + limiterOffset/compRatio
+ limiterLvlX = analogTarget - limiterOffset;
+ limiterIdx = 2
+ + WebRtcSpl_DivW32W16ResW16(WEBRTC_SPL_LSHIFT_W32((WebRtc_Word32)limiterLvlX, 13),
+ WEBRTC_SPL_RSHIFT_U16(kLog10_2, 1));
+ tmp16no1 = WebRtcSpl_DivW32W16ResW16(limiterOffset + (kCompRatio >> 1), kCompRatio);
+ limiterLvl = targetLevelDbfs + tmp16no1;
+
+ // Calculate (through table lookup):
+ // constMaxGain = log2(1+2^(log2(e)*diffGain)); (in Q8)
+ constMaxGain = kGenFuncTable[diffGain]; // in Q8
+
+ // Calculate a parameter used to approximate the fractional part of 2^x with a
+ // piecewise linear function in Q14:
+ // constLinApprox = round(3/2*(4*(3-2*sqrt(2))/(log(2)^2)-0.5)*2^14);
+ constLinApprox = 22817; // in Q14
+
+ // Calculate a denominator used in the exponential part to convert from dB to linear scale:
+ // den = 20*constMaxGain (in Q8)
+ den = WEBRTC_SPL_MUL_16_U16(20, constMaxGain); // in Q8
+
+ for (i = 0; i < 32; i++)
+ {
+ // Calculate scaled input level (compressor):
+ // inLevel = fix((-constLog10_2*(compRatio-1)*(1-i)+fix(compRatio/2))/compRatio)
+ tmp16 = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16(kCompRatio - 1, i - 1); // Q0
+ tmp32 = WEBRTC_SPL_MUL_16_U16(tmp16, kLog10_2) + 1; // Q14
+ inLevel = WebRtcSpl_DivW32W16(tmp32, kCompRatio); // Q14
+
+ // Calculate diffGain-inLevel, to map using the genFuncTable
+ inLevel = WEBRTC_SPL_LSHIFT_W32((WebRtc_Word32)diffGain, 14) - inLevel; // Q14
+
+ // Make calculations on abs(inLevel) and compensate for the sign afterwards.
+ absInLevel = (WebRtc_UWord32)WEBRTC_SPL_ABS_W32(inLevel); // Q14
+
+ // LUT with interpolation
+ intPart = (WebRtc_UWord16)WEBRTC_SPL_RSHIFT_U32(absInLevel, 14);
+ fracPart = (WebRtc_UWord16)(absInLevel & 0x00003FFF); // extract the fractional part
+ tmpU16 = kGenFuncTable[intPart + 1] - kGenFuncTable[intPart]; // Q8
+ tmpU32no1 = WEBRTC_SPL_UMUL_16_16(tmpU16, fracPart); // Q22
+ tmpU32no1 += WEBRTC_SPL_LSHIFT_U32((WebRtc_UWord32)kGenFuncTable[intPart], 14); // Q22
+ logApprox = WEBRTC_SPL_RSHIFT_U32(tmpU32no1, 8); // Q14
+ // Compensate for negative exponent using the relation:
+ // log2(1 + 2^-x) = log2(1 + 2^x) - x
+ if (inLevel < 0)
+ {
+ zeros = WebRtcSpl_NormU32(absInLevel);
+ zerosScale = 0;
+ if (zeros < 15)
+ {
+ // Not enough space for multiplication
+ tmpU32no2 = WEBRTC_SPL_RSHIFT_U32(absInLevel, 15 - zeros); // Q(zeros-1)
+ tmpU32no2 = WEBRTC_SPL_UMUL_32_16(tmpU32no2, kLogE_1); // Q(zeros+13)
+ if (zeros < 9)
+ {
+ tmpU32no1 = WEBRTC_SPL_RSHIFT_U32(tmpU32no1, 9 - zeros); // Q(zeros+13)
+ zerosScale = 9 - zeros;
+ } else
+ {
+ tmpU32no2 = WEBRTC_SPL_RSHIFT_U32(tmpU32no2, zeros - 9); // Q22
+ }
+ } else
+ {
+ tmpU32no2 = WEBRTC_SPL_UMUL_32_16(absInLevel, kLogE_1); // Q28
+ tmpU32no2 = WEBRTC_SPL_RSHIFT_U32(tmpU32no2, 6); // Q22
+ }
+ logApprox = 0;
+ if (tmpU32no2 < tmpU32no1)
+ {
+ logApprox = WEBRTC_SPL_RSHIFT_U32(tmpU32no1 - tmpU32no2, 8 - zerosScale); //Q14
+ }
+ }
+ numFIX = WEBRTC_SPL_LSHIFT_W32(WEBRTC_SPL_MUL_16_U16(maxGain, constMaxGain), 6); // Q14
+ numFIX -= WEBRTC_SPL_MUL_32_16((WebRtc_Word32)logApprox, diffGain); // Q14
+
+ // Calculate ratio
+ // Shift numFIX as much as possible
+ zeros = WebRtcSpl_NormW32(numFIX);
+ numFIX = WEBRTC_SPL_LSHIFT_W32(numFIX, zeros); // Q(14+zeros)
+
+ // Shift den so we end up in Qy1
+ tmp32no1 = WEBRTC_SPL_SHIFT_W32(den, zeros - 8); // Q(zeros)
+ if (numFIX < 0)
+ {
+ numFIX -= WEBRTC_SPL_RSHIFT_W32(tmp32no1, 1);
+ } else
+ {
+ numFIX += WEBRTC_SPL_RSHIFT_W32(tmp32no1, 1);
+ }
+ y32 = WEBRTC_SPL_DIV(numFIX, tmp32no1); // in Q14
+ if (limiterEnable && (i < limiterIdx))
+ {
+ tmp32 = WEBRTC_SPL_MUL_16_U16(i - 1, kLog10_2); // Q14
+ tmp32 -= WEBRTC_SPL_LSHIFT_W32(limiterLvl, 14); // Q14
+ y32 = WebRtcSpl_DivW32W16(tmp32 + 10, 20);
+ }
+ if (y32 > 39000)
+ {
+ tmp32 = WEBRTC_SPL_MUL(y32 >> 1, kLog10) + 4096; // in Q27
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(tmp32, 13); // in Q14
+ } else
+ {
+ tmp32 = WEBRTC_SPL_MUL(y32, kLog10) + 8192; // in Q28
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(tmp32, 14); // in Q14
+ }
+ tmp32 += WEBRTC_SPL_LSHIFT_W32(16, 14); // in Q14 (Make sure final output is in Q16)
+
+ // Calculate power
+ if (tmp32 > 0)
+ {
+ intPart = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 14);
+ fracPart = (WebRtc_UWord16)(tmp32 & 0x00003FFF); // in Q14
+ if (WEBRTC_SPL_RSHIFT_W32(fracPart, 13))
+ {
+ tmp16 = WEBRTC_SPL_LSHIFT_W16(2, 14) - constLinApprox;
+ tmp32no2 = WEBRTC_SPL_LSHIFT_W32(1, 14) - fracPart;
+ tmp32no2 = WEBRTC_SPL_MUL_32_16(tmp32no2, tmp16);
+ tmp32no2 = WEBRTC_SPL_RSHIFT_W32(tmp32no2, 13);
+ tmp32no2 = WEBRTC_SPL_LSHIFT_W32(1, 14) - tmp32no2;
+ } else
+ {
+ tmp16 = constLinApprox - WEBRTC_SPL_LSHIFT_W16(1, 14);
+ tmp32no2 = WEBRTC_SPL_MUL_32_16(fracPart, tmp16);
+ tmp32no2 = WEBRTC_SPL_RSHIFT_W32(tmp32no2, 13);
+ }
+ fracPart = (WebRtc_UWord16)tmp32no2;
+ gainTable[i] = WEBRTC_SPL_LSHIFT_W32(1, intPart)
+ + WEBRTC_SPL_SHIFT_W32(fracPart, intPart - 14);
+ } else
+ {
+ gainTable[i] = 0;
+ }
+ }
+
+ return 0;
+}
+
+WebRtc_Word32 WebRtcAgc_InitDigital(DigitalAgc_t *stt, WebRtc_Word16 agcMode)
+{
+
+ if (agcMode == kAgcModeFixedDigital)
+ {
+ // start at minimum to find correct gain faster
+ stt->capacitorSlow = 0;
+ } else
+ {
+ // start out with 0 dB gain
+ stt->capacitorSlow = 134217728; // (WebRtc_Word32)(0.125f * 32768.0f * 32768.0f);
+ }
+ stt->capacitorFast = 0;
+ stt->gain = 65536;
+ stt->gatePrevious = 0;
+ stt->agcMode = agcMode;
+#ifdef AGC_DEBUG
+ stt->frameCounter = 0;
+#endif
+
+ // initialize VADs
+ WebRtcAgc_InitVad(&stt->vadNearend);
+ WebRtcAgc_InitVad(&stt->vadFarend);
+
+ return 0;
+}
+
+WebRtc_Word32 WebRtcAgc_AddFarendToDigital(DigitalAgc_t *stt, const WebRtc_Word16 *in_far,
+ WebRtc_Word16 nrSamples)
+{
+ // Check for valid pointer
+ if (&stt->vadFarend == NULL)
+ {
+ return -1;
+ }
+
+ // VAD for far end
+ WebRtcAgc_ProcessVad(&stt->vadFarend, in_far, nrSamples);
+
+ return 0;
+}
+
+WebRtc_Word32 WebRtcAgc_ProcessDigital(DigitalAgc_t *stt, const WebRtc_Word16 *in_near,
+ const WebRtc_Word16 *in_near_H, WebRtc_Word16 *out,
+ WebRtc_Word16 *out_H, WebRtc_UWord32 FS,
+ WebRtc_Word16 lowlevelSignal)
+{
+ // array for gains (one value per ms, incl start & end)
+ WebRtc_Word32 gains[11];
+
+ WebRtc_Word32 out_tmp, tmp32;
+ WebRtc_Word32 env[10];
+ WebRtc_Word32 nrg, max_nrg;
+ WebRtc_Word32 cur_level;
+ WebRtc_Word32 gain32, delta;
+ WebRtc_Word16 logratio;
+ WebRtc_Word16 lower_thr, upper_thr;
+ WebRtc_Word16 zeros, zeros_fast, frac;
+ WebRtc_Word16 decay;
+ WebRtc_Word16 gate, gain_adj;
+ WebRtc_Word16 k, n;
+ WebRtc_Word16 L, L2; // samples/subframe
+
+ // determine number of samples per ms
+ if (FS == 8000)
+ {
+ L = 8;
+ L2 = 3;
+ } else if (FS == 16000)
+ {
+ L = 16;
+ L2 = 4;
+ } else if (FS == 32000)
+ {
+ L = 16;
+ L2 = 4;
+ } else
+ {
+ return -1;
+ }
+
+ memcpy(out, in_near, 10 * L * sizeof(WebRtc_Word16));
+ if (FS == 32000)
+ {
+ memcpy(out_H, in_near_H, 10 * L * sizeof(WebRtc_Word16));
+ }
+ // VAD for near end
+ logratio = WebRtcAgc_ProcessVad(&stt->vadNearend, out, L * 10);
+
+ // Account for far end VAD
+ if (stt->vadFarend.counter > 10)
+ {
+ tmp32 = WEBRTC_SPL_MUL_16_16(3, logratio);
+ logratio = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32 - stt->vadFarend.logRatio, 2);
+ }
+
+ // Determine decay factor depending on VAD
+ // upper_thr = 1.0f;
+ // lower_thr = 0.25f;
+ upper_thr = 1024; // Q10
+ lower_thr = 0; // Q10
+ if (logratio > upper_thr)
+ {
+ // decay = -2^17 / DecayTime; -> -65
+ decay = -65;
+ } else if (logratio < lower_thr)
+ {
+ decay = 0;
+ } else
+ {
+ // decay = (WebRtc_Word16)(((lower_thr - logratio)
+ // * (2^27/(DecayTime*(upper_thr-lower_thr)))) >> 10);
+ // SUBSTITUTED: 2^27/(DecayTime*(upper_thr-lower_thr)) -> 65
+ tmp32 = WEBRTC_SPL_MUL_16_16((lower_thr - logratio), 65);
+ decay = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 10);
+ }
+
+ // adjust decay factor for long silence (detected as low standard deviation)
+ // This is only done in the adaptive modes
+ if (stt->agcMode != kAgcModeFixedDigital)
+ {
+ if (stt->vadNearend.stdLongTerm < 4000)
+ {
+ decay = 0;
+ } else if (stt->vadNearend.stdLongTerm < 8096)
+ {
+ // decay = (WebRtc_Word16)(((stt->vadNearend.stdLongTerm - 4000) * decay) >> 12);
+ tmp32 = WEBRTC_SPL_MUL_16_16((stt->vadNearend.stdLongTerm - 4000), decay);
+ decay = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 12);
+ }
+
+ if (lowlevelSignal != 0)
+ {
+ decay = 0;
+ }
+ }
+#ifdef AGC_DEBUG
+ stt->frameCounter++;
+ fprintf(stt->logFile, "%5.2f\t%d\t%d\t%d\t", (float)(stt->frameCounter) / 100, logratio, decay, stt->vadNearend.stdLongTerm);
+#endif
+ // Find max amplitude per sub frame
+ // iterate over sub frames
+ for (k = 0; k < 10; k++)
+ {
+ // iterate over samples
+ max_nrg = 0;
+ for (n = 0; n < L; n++)
+ {
+ nrg = WEBRTC_SPL_MUL_16_16(out[k * L + n], out[k * L + n]);
+ if (nrg > max_nrg)
+ {
+ max_nrg = nrg;
+ }
+ }
+ env[k] = max_nrg;
+ }
+
+ // Calculate gain per sub frame
+ gains[0] = stt->gain;
+ for (k = 0; k < 10; k++)
+ {
+ // Fast envelope follower
+ // decay time = -131000 / -1000 = 131 (ms)
+ stt->capacitorFast = AGC_SCALEDIFF32(-1000, stt->capacitorFast, stt->capacitorFast);
+ if (env[k] > stt->capacitorFast)
+ {
+ stt->capacitorFast = env[k];
+ }
+ // Slow envelope follower
+ if (env[k] > stt->capacitorSlow)
+ {
+ // increase capacitorSlow
+ stt->capacitorSlow
+ = AGC_SCALEDIFF32(500, (env[k] - stt->capacitorSlow), stt->capacitorSlow);
+ } else
+ {
+ // decrease capacitorSlow
+ stt->capacitorSlow
+ = AGC_SCALEDIFF32(decay, stt->capacitorSlow, stt->capacitorSlow);
+ }
+
+ // use maximum of both capacitors as current level
+ if (stt->capacitorFast > stt->capacitorSlow)
+ {
+ cur_level = stt->capacitorFast;
+ } else
+ {
+ cur_level = stt->capacitorSlow;
+ }
+ // Translate signal level into gain, using a piecewise linear approximation
+ // find number of leading zeros
+ zeros = WebRtcSpl_NormU32((WebRtc_UWord32)cur_level);
+ if (cur_level == 0)
+ {
+ zeros = 31;
+ }
+ tmp32 = (WEBRTC_SPL_LSHIFT_W32(cur_level, zeros) & 0x7FFFFFFF);
+ frac = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 19); // Q12
+ tmp32 = WEBRTC_SPL_MUL((stt->gainTable[zeros-1] - stt->gainTable[zeros]), frac);
+ gains[k + 1] = stt->gainTable[zeros] + WEBRTC_SPL_RSHIFT_W32(tmp32, 12);
+#ifdef AGC_DEBUG
+ if (k == 0)
+ {
+ fprintf(stt->logFile, "%d\t%d\t%d\t%d\t%d\n", env[0], cur_level, stt->capacitorFast, stt->capacitorSlow, zeros);
+ }
+#endif
+ }
+
+ // Gate processing (lower gain during absence of speech)
+ zeros = WEBRTC_SPL_LSHIFT_W16(zeros, 9) - WEBRTC_SPL_RSHIFT_W16(frac, 3);
+ // find number of leading zeros
+ zeros_fast = WebRtcSpl_NormU32((WebRtc_UWord32)stt->capacitorFast);
+ if (stt->capacitorFast == 0)
+ {
+ zeros_fast = 31;
+ }
+ tmp32 = (WEBRTC_SPL_LSHIFT_W32(stt->capacitorFast, zeros_fast) & 0x7FFFFFFF);
+ zeros_fast = WEBRTC_SPL_LSHIFT_W16(zeros_fast, 9);
+ zeros_fast -= (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 22);
+
+ gate = 1000 + zeros_fast - zeros - stt->vadNearend.stdShortTerm;
+
+ if (gate < 0)
+ {
+ stt->gatePrevious = 0;
+ } else
+ {
+ tmp32 = WEBRTC_SPL_MUL_16_16(stt->gatePrevious, 7);
+ gate = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32((WebRtc_Word32)gate + tmp32, 3);
+ stt->gatePrevious = gate;
+ }
+ // gate < 0 -> no gate
+ // gate > 2500 -> max gate
+ if (gate > 0)
+ {
+ if (gate < 2500)
+ {
+ gain_adj = WEBRTC_SPL_RSHIFT_W16(2500 - gate, 5);
+ } else
+ {
+ gain_adj = 0;
+ }
+ for (k = 0; k < 10; k++)
+ {
+ if ((gains[k + 1] - stt->gainTable[0]) > 8388608)
+ {
+ // To prevent wraparound
+ tmp32 = WEBRTC_SPL_RSHIFT_W32((gains[k+1] - stt->gainTable[0]), 8);
+ tmp32 = WEBRTC_SPL_MUL(tmp32, (178 + gain_adj));
+ } else
+ {
+ tmp32 = WEBRTC_SPL_MUL((gains[k+1] - stt->gainTable[0]), (178 + gain_adj));
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(tmp32, 8);
+ }
+ gains[k + 1] = stt->gainTable[0] + tmp32;
+ }
+ }
+
+ // Limit gain to avoid overload distortion
+ for (k = 0; k < 10; k++)
+ {
+ // To prevent wrap around
+ zeros = 10;
+ if (gains[k + 1] > 47453132)
+ {
+ zeros = 16 - WebRtcSpl_NormW32(gains[k + 1]);
+ }
+ gain32 = WEBRTC_SPL_RSHIFT_W32(gains[k+1], zeros) + 1;
+ gain32 = WEBRTC_SPL_MUL(gain32, gain32);
+ // check for overflow
+ while (AGC_MUL32(WEBRTC_SPL_RSHIFT_W32(env[k], 12) + 1, gain32)
+ > WEBRTC_SPL_SHIFT_W32((WebRtc_Word32)32767, 2 * (1 - zeros + 10)))
+ {
+ // multiply by 253/256 ==> -0.1 dB
+ if (gains[k + 1] > 8388607)
+ {
+ // Prevent wrap around
+ gains[k + 1] = WEBRTC_SPL_MUL(WEBRTC_SPL_RSHIFT_W32(gains[k+1], 8), 253);
+ } else
+ {
+ gains[k + 1] = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(gains[k+1], 253), 8);
+ }
+ gain32 = WEBRTC_SPL_RSHIFT_W32(gains[k+1], zeros) + 1;
+ gain32 = WEBRTC_SPL_MUL(gain32, gain32);
+ }
+ }
+ // gain reductions should be done 1 ms earlier than gain increases
+ for (k = 1; k < 10; k++)
+ {
+ if (gains[k] > gains[k + 1])
+ {
+ gains[k] = gains[k + 1];
+ }
+ }
+ // save start gain for next frame
+ stt->gain = gains[10];
+
+ // Apply gain
+ // handle first sub frame separately
+ delta = WEBRTC_SPL_LSHIFT_W32(gains[1] - gains[0], (4 - L2));
+ gain32 = WEBRTC_SPL_LSHIFT_W32(gains[0], 4);
+ // iterate over samples
+ for (n = 0; n < L; n++)
+ {
+ // For lower band
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out[n], WEBRTC_SPL_RSHIFT_W32(gain32 + 127, 7));
+ out_tmp = WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ if (out_tmp > 4095)
+ {
+ out[n] = (WebRtc_Word16)32767;
+ } else if (out_tmp < -4096)
+ {
+ out[n] = (WebRtc_Word16)-32768;
+ } else
+ {
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out[n], WEBRTC_SPL_RSHIFT_W32(gain32, 4));
+ out[n] = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ }
+ // For higher band
+ if (FS == 32000)
+ {
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out_H[n],
+ WEBRTC_SPL_RSHIFT_W32(gain32 + 127, 7));
+ out_tmp = WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ if (out_tmp > 4095)
+ {
+ out_H[n] = (WebRtc_Word16)32767;
+ } else if (out_tmp < -4096)
+ {
+ out_H[n] = (WebRtc_Word16)-32768;
+ } else
+ {
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out_H[n],
+ WEBRTC_SPL_RSHIFT_W32(gain32, 4));
+ out_H[n] = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ }
+ }
+ //
+
+ gain32 += delta;
+ }
+ // iterate over subframes
+ for (k = 1; k < 10; k++)
+ {
+ delta = WEBRTC_SPL_LSHIFT_W32(gains[k+1] - gains[k], (4 - L2));
+ gain32 = WEBRTC_SPL_LSHIFT_W32(gains[k], 4);
+ // iterate over samples
+ for (n = 0; n < L; n++)
+ {
+ // For lower band
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out[k * L + n],
+ WEBRTC_SPL_RSHIFT_W32(gain32, 4));
+ out[k * L + n] = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ // For higher band
+ if (FS == 32000)
+ {
+ tmp32 = WEBRTC_SPL_MUL((WebRtc_Word32)out_H[k * L + n],
+ WEBRTC_SPL_RSHIFT_W32(gain32, 4));
+ out_H[k * L + n] = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32 , 16);
+ }
+ gain32 += delta;
+ }
+ }
+
+ return 0;
+}
+
+void WebRtcAgc_InitVad(AgcVad_t *state)
+{
+ WebRtc_Word16 k;
+
+ state->HPstate = 0; // state of high pass filter
+ state->logRatio = 0; // log( P(active) / P(inactive) )
+ // average input level (Q10)
+ state->meanLongTerm = WEBRTC_SPL_LSHIFT_W16(15, 10);
+
+ // variance of input level (Q8)
+ state->varianceLongTerm = WEBRTC_SPL_LSHIFT_W32(500, 8);
+
+ state->stdLongTerm = 0; // standard deviation of input level in dB
+ // short-term average input level (Q10)
+ state->meanShortTerm = WEBRTC_SPL_LSHIFT_W16(15, 10);
+
+ // short-term variance of input level (Q8)
+ state->varianceShortTerm = WEBRTC_SPL_LSHIFT_W32(500, 8);
+
+ state->stdShortTerm = 0; // short-term standard deviation of input level in dB
+ state->counter = 3; // counts updates
+ for (k = 0; k < 8; k++)
+ {
+ // downsampling filter
+ state->downState[k] = 0;
+ }
+}
+
+WebRtc_Word16 WebRtcAgc_ProcessVad(AgcVad_t *state, // (i) VAD state
+ const WebRtc_Word16 *in, // (i) Speech signal
+ WebRtc_Word16 nrSamples) // (i) number of samples
+{
+ WebRtc_Word32 out, nrg, tmp32, tmp32b;
+ WebRtc_UWord16 tmpU16;
+ WebRtc_Word16 k, subfr, tmp16;
+ WebRtc_Word16 buf1[8];
+ WebRtc_Word16 buf2[4];
+ WebRtc_Word16 HPstate;
+ WebRtc_Word16 zeros, dB;
+ WebRtc_Word16 *buf1_ptr;
+
+ // process in 10 sub frames of 1 ms (to save on memory)
+ nrg = 0;
+ buf1_ptr = &buf1[0];
+ HPstate = state->HPstate;
+ for (subfr = 0; subfr < 10; subfr++)
+ {
+ // downsample to 4 kHz
+ if (nrSamples == 160)
+ {
+ for (k = 0; k < 8; k++)
+ {
+ tmp32 = (WebRtc_Word32)in[2 * k] + (WebRtc_Word32)in[2 * k + 1];
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(tmp32, 1);
+ buf1[k] = (WebRtc_Word16)tmp32;
+ }
+ in += 16;
+
+ WebRtcSpl_DownsampleBy2(buf1, 8, buf2, state->downState);
+ } else
+ {
+ WebRtcSpl_DownsampleBy2(in, 8, buf2, state->downState);
+ in += 8;
+ }
+
+ // high pass filter and compute energy
+ for (k = 0; k < 4; k++)
+ {
+ out = buf2[k] + HPstate;
+ tmp32 = WEBRTC_SPL_MUL(600, out);
+ HPstate = (WebRtc_Word16)(WEBRTC_SPL_RSHIFT_W32(tmp32, 10) - buf2[k]);
+ tmp32 = WEBRTC_SPL_MUL(out, out);
+ nrg += WEBRTC_SPL_RSHIFT_W32(tmp32, 6);
+ }
+ }
+ state->HPstate = HPstate;
+
+ // find number of leading zeros
+ if (!(0xFFFF0000 & nrg))
+ {
+ zeros = 16;
+ } else
+ {
+ zeros = 0;
+ }
+ if (!(0xFF000000 & (nrg << zeros)))
+ {
+ zeros += 8;
+ }
+ if (!(0xF0000000 & (nrg << zeros)))
+ {
+ zeros += 4;
+ }
+ if (!(0xC0000000 & (nrg << zeros)))
+ {
+ zeros += 2;
+ }
+ if (!(0x80000000 & (nrg << zeros)))
+ {
+ zeros += 1;
+ }
+
+ // energy level (range {-32..30}) (Q10)
+ dB = WEBRTC_SPL_LSHIFT_W16(15 - zeros, 11);
+
+ // Update statistics
+
+ if (state->counter < kAvgDecayTime)
+ {
+ // decay time = AvgDecTime * 10 ms
+ state->counter++;
+ }
+
+ // update short-term estimate of mean energy level (Q10)
+ tmp32 = (WEBRTC_SPL_MUL_16_16(state->meanShortTerm, 15) + (WebRtc_Word32)dB);
+ state->meanShortTerm = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 4);
+
+ // update short-term estimate of variance in energy level (Q8)
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(dB, dB), 12);
+ tmp32 += WEBRTC_SPL_MUL(state->varianceShortTerm, 15);
+ state->varianceShortTerm = WEBRTC_SPL_RSHIFT_W32(tmp32, 4);
+
+ // update short-term estimate of standard deviation in energy level (Q10)
+ tmp32 = WEBRTC_SPL_MUL_16_16(state->meanShortTerm, state->meanShortTerm);
+ tmp32 = WEBRTC_SPL_LSHIFT_W32(state->varianceShortTerm, 12) - tmp32;
+ state->stdShortTerm = (WebRtc_Word16)WebRtcSpl_Sqrt(tmp32);
+
+ // update long-term estimate of mean energy level (Q10)
+ tmp32 = WEBRTC_SPL_MUL_16_16(state->meanLongTerm, state->counter) + (WebRtc_Word32)dB;
+ state->meanLongTerm = WebRtcSpl_DivW32W16ResW16(tmp32,
+ WEBRTC_SPL_ADD_SAT_W16(state->counter, 1));
+
+ // update long-term estimate of variance in energy level (Q8)
+ tmp32 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(dB, dB), 12);
+ tmp32 += WEBRTC_SPL_MUL(state->varianceLongTerm, state->counter);
+ state->varianceLongTerm = WebRtcSpl_DivW32W16(tmp32,
+ WEBRTC_SPL_ADD_SAT_W16(state->counter, 1));
+
+ // update long-term estimate of standard deviation in energy level (Q10)
+ tmp32 = WEBRTC_SPL_MUL_16_16(state->meanLongTerm, state->meanLongTerm);
+ tmp32 = WEBRTC_SPL_LSHIFT_W32(state->varianceLongTerm, 12) - tmp32;
+ state->stdLongTerm = (WebRtc_Word16)WebRtcSpl_Sqrt(tmp32);
+
+ // update voice activity measure (Q10)
+ tmp16 = WEBRTC_SPL_LSHIFT_W16(3, 12);
+ tmp32 = WEBRTC_SPL_MUL_16_16(tmp16, (dB - state->meanLongTerm));
+ tmp32 = WebRtcSpl_DivW32W16(tmp32, state->stdLongTerm);
+ tmpU16 = WEBRTC_SPL_LSHIFT_U16((WebRtc_UWord16)13, 12);
+ tmp32b = WEBRTC_SPL_MUL_16_U16(state->logRatio, tmpU16);
+ tmp32 += WEBRTC_SPL_RSHIFT_W32(tmp32b, 10);
+
+ state->logRatio = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(tmp32, 6);
+
+ // limit
+ if (state->logRatio > 2048)
+ {
+ state->logRatio = 2048;
+ }
+ if (state->logRatio < -2048)
+ {
+ state->logRatio = -2048;
+ }
+
+ return state->logRatio; // Q10
+}
diff --git a/src/modules/audio_processing/agc/main/source/digital_agc.h b/src/modules/audio_processing/agc/main/source/digital_agc.h
new file mode 100644
index 0000000000..240b220661
--- /dev/null
+++ b/src/modules/audio_processing/agc/main/source/digital_agc.h
@@ -0,0 +1,76 @@
+/*
+ * 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_AGC_MAIN_SOURCE_DIGITAL_AGC_H_
+#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_DIGITAL_AGC_H_
+
+#ifdef AGC_DEBUG
+#include <stdio.h>
+#endif
+#include "typedefs.h"
+#include "signal_processing_library.h"
+
+// the 32 most significant bits of A(19) * B(26) >> 13
+#define AGC_MUL32(A, B) (((B)>>13)*(A) + ( ((0x00001FFF & (B))*(A)) >> 13 ))
+// C + the 32 most significant bits of A * B
+#define AGC_SCALEDIFF32(A, B, C) ((C) + ((B)>>16)*(A) + ( ((0x0000FFFF & (B))*(A)) >> 16 ))
+
+typedef struct
+{
+ WebRtc_Word32 downState[8];
+ WebRtc_Word16 HPstate;
+ WebRtc_Word16 counter;
+ WebRtc_Word16 logRatio; // log( P(active) / P(inactive) ) (Q10)
+ WebRtc_Word16 meanLongTerm; // Q10
+ WebRtc_Word32 varianceLongTerm; // Q8
+ WebRtc_Word16 stdLongTerm; // Q10
+ WebRtc_Word16 meanShortTerm; // Q10
+ WebRtc_Word32 varianceShortTerm; // Q8
+ WebRtc_Word16 stdShortTerm; // Q10
+} AgcVad_t; // total = 54 bytes
+
+typedef struct
+{
+ WebRtc_Word32 capacitorSlow;
+ WebRtc_Word32 capacitorFast;
+ WebRtc_Word32 gain;
+ WebRtc_Word32 gainTable[32];
+ WebRtc_Word16 gatePrevious;
+ WebRtc_Word16 agcMode;
+ AgcVad_t vadNearend;
+ AgcVad_t vadFarend;
+#ifdef AGC_DEBUG
+ FILE* logFile;
+ int frameCounter;
+#endif
+} DigitalAgc_t;
+
+WebRtc_Word32 WebRtcAgc_InitDigital(DigitalAgc_t *digitalAgcInst, WebRtc_Word16 agcMode);
+
+WebRtc_Word32 WebRtcAgc_ProcessDigital(DigitalAgc_t *digitalAgcInst, const WebRtc_Word16 *inNear,
+ const WebRtc_Word16 *inNear_H, WebRtc_Word16 *out,
+ WebRtc_Word16 *out_H, WebRtc_UWord32 FS,
+ WebRtc_Word16 lowLevelSignal);
+
+WebRtc_Word32 WebRtcAgc_AddFarendToDigital(DigitalAgc_t *digitalAgcInst, const WebRtc_Word16 *inFar,
+ WebRtc_Word16 nrSamples);
+
+void WebRtcAgc_InitVad(AgcVad_t *vadInst);
+
+WebRtc_Word16 WebRtcAgc_ProcessVad(AgcVad_t *vadInst, // (i) VAD state
+ const WebRtc_Word16 *in, // (i) Speech signal
+ WebRtc_Word16 nrSamples); // (i) number of samples
+
+WebRtc_Word32 WebRtcAgc_CalculateGainTable(WebRtc_Word32 *gainTable, // Q16
+ WebRtc_Word16 compressionGaindB, // Q0 (in dB)
+ WebRtc_Word16 targetLevelDbfs,// Q0 (in dB)
+ WebRtc_UWord8 limiterEnable, WebRtc_Word16 analogTarget);
+
+#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_