1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_DIGITAL_AGC_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_DIGITAL_AGC_H_
13
14#ifdef AGC_DEBUG
15#include <stdio.h>
16#endif
17#include "typedefs.h"
18#include "signal_processing_library.h"
19
20// the 32 most significant bits of A(19) * B(26) >> 13
21#define AGC_MUL32(A, B)             (((B)>>13)*(A) + ( ((0x00001FFF & (B))*(A)) >> 13 ))
22// C + the 32 most significant bits of A * B
23#define AGC_SCALEDIFF32(A, B, C)    ((C) + ((B)>>16)*(A) + ( ((0x0000FFFF & (B))*(A)) >> 16 ))
24
25typedef struct
26{
27    WebRtc_Word32 downState[8];
28    WebRtc_Word16 HPstate;
29    WebRtc_Word16 counter;
30    WebRtc_Word16 logRatio; // log( P(active) / P(inactive) ) (Q10)
31    WebRtc_Word16 meanLongTerm; // Q10
32    WebRtc_Word32 varianceLongTerm; // Q8
33    WebRtc_Word16 stdLongTerm; // Q10
34    WebRtc_Word16 meanShortTerm; // Q10
35    WebRtc_Word32 varianceShortTerm; // Q8
36    WebRtc_Word16 stdShortTerm; // Q10
37} AgcVad_t; // total = 54 bytes
38
39typedef struct
40{
41    WebRtc_Word32 capacitorSlow;
42    WebRtc_Word32 capacitorFast;
43    WebRtc_Word32 gain;
44    WebRtc_Word32 gainTable[32];
45    WebRtc_Word16 gatePrevious;
46    WebRtc_Word16 agcMode;
47    AgcVad_t      vadNearend;
48    AgcVad_t      vadFarend;
49#ifdef AGC_DEBUG
50    FILE*         logFile;
51    int           frameCounter;
52#endif
53} DigitalAgc_t;
54
55WebRtc_Word32 WebRtcAgc_InitDigital(DigitalAgc_t *digitalAgcInst, WebRtc_Word16 agcMode);
56
57WebRtc_Word32 WebRtcAgc_ProcessDigital(DigitalAgc_t *digitalAgcInst, const WebRtc_Word16 *inNear,
58                             const WebRtc_Word16 *inNear_H, WebRtc_Word16 *out,
59                             WebRtc_Word16 *out_H, WebRtc_UWord32 FS,
60                             WebRtc_Word16 lowLevelSignal);
61
62WebRtc_Word32 WebRtcAgc_AddFarendToDigital(DigitalAgc_t *digitalAgcInst, const WebRtc_Word16 *inFar,
63                                 WebRtc_Word16 nrSamples);
64
65void WebRtcAgc_InitVad(AgcVad_t *vadInst);
66
67WebRtc_Word16 WebRtcAgc_ProcessVad(AgcVad_t *vadInst, // (i) VAD state
68                            const WebRtc_Word16 *in, // (i) Speech signal
69                            WebRtc_Word16 nrSamples); // (i) number of samples
70
71WebRtc_Word32 WebRtcAgc_CalculateGainTable(WebRtc_Word32 *gainTable, // Q16
72                                 WebRtc_Word16 compressionGaindB, // Q0 (in dB)
73                                 WebRtc_Word16 targetLevelDbfs,// Q0 (in dB)
74                                 WebRtc_UWord8 limiterEnable, WebRtc_Word16 analogTarget);
75
76#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_
77