forked from DerArtem/huaweigeneric-ril
-
Notifications
You must be signed in to change notification settings - Fork 2
/
agc.c
142 lines (121 loc) · 4.33 KB
/
agc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* This AGC algorithm was taken from isdn2h323 (http://www.telos.de). It was
* converted from C++ to C, and modified to add control over the recording level.
* Converted to fixed point by Phil Frisbie, Jr. 4/12/2003
*/
#include "agc.h"
#define LOG_NDEBUG 0
#define LOG_TAG "RILAudioAGC"
#include <utils/Log.h>
void agc_init(struct agc_ctx* ctx, short level /* 16384-32767*/ )
{
ALOGD("[%p] Initializing AGC: level: %d",ctx, level);
ctx->sample_max = 1;
ctx->counter = 0;
ctx->igain = 65536;
if(level < 16384) {
level = 16384;
}
ctx->ipeak = (int)(level * 65536);
ctx->silence_counter = 0;
}
void agc_process_16bit(struct agc_ctx *ctx, short *buffer, int len)
{
int i;
for(i=0; i<len; i++) {
long gain_new;
int sample;
/* get the abs of buffer[i] */
sample = buffer[i];
sample = (sample < 0 ? -(sample) : sample);
if(sample > (int)ctx->sample_max)
{
/* update the max */
ctx->sample_max = (unsigned int)sample;
}
ctx->counter ++;
/* Will we get an overflow with the current gain factor? */
if (((sample * ctx->igain) >> 16) > ctx->ipeak)
{
/* Yes: Calculate new gain. */
ctx->igain = ((ctx->ipeak / ctx->sample_max) * 62259) >> 16;
ctx->silence_counter = 0;
buffer[i] = (short) ((buffer[i] * ctx->igain) >> 16);
continue;
}
/* Calculate new gain factor 10x per second */
if (ctx->counter >= 8000/10)
{
if (ctx->sample_max > 800) /* speaking? */
{
gain_new = ((ctx->ipeak / ctx->sample_max) * 62259) >> 16;
if (ctx->silence_counter > 40) /* pause -> speaking */
ctx->igain += (gain_new - ctx->igain) >> 2;
else
ctx->igain += (gain_new - ctx->igain) / 20;
ctx->silence_counter = 0;
}
else /* silence */
{
ctx->silence_counter++;
/* silence > 2 seconds: reduce gain */
if ((ctx->igain > 65536) && (ctx->silence_counter >= 20))
ctx->igain = (ctx->igain * 62259) >> 16;
}
ctx->counter = 0;
ctx->sample_max = 1;
}
buffer[i] = (short) ((buffer[i] * ctx->igain) >> 16);
}
//ALOGD("[%p] Gain: %d",ctx, ctx->igain);
}
void agc_process_8bit(struct agc_ctx *ctx, unsigned char *buffer, int len)
{
int i;
for(i=0; i<len; i++) {
long gain_new;
int sample;
/* get the abs of buffer[i] */
sample = (buffer[i] - 128) << 8;
sample = (sample < 0 ? -(sample) : sample);
if(sample > (int)ctx->sample_max)
{
/* update the max */
ctx->sample_max = (unsigned int)sample;
}
ctx->counter ++;
/* Will we get an overflow with the current gain factor? */
if (((sample * ctx->igain) >> 16) > ctx->ipeak)
{
/* Yes: Calculate new gain. */
ctx->igain = ((ctx->ipeak / ctx->sample_max) * 62259) >> 16;
ctx->silence_counter = 0;
buffer[i] = (unsigned char) (( ((buffer[i] - 128) << 8) * ctx->igain) >> 24) + 128;
continue;
}
/* Calculate new gain factor 10x per second */
if (ctx->counter >= 8000/10)
{
if (ctx->sample_max > 800) /* speaking? */
{
gain_new = ((ctx->ipeak / ctx->sample_max) * 62259) >> 16;
if (ctx->silence_counter > 40) /* pause -> speaking */
ctx->igain += (gain_new - ctx->igain) >> 2;
else
ctx->igain += (gain_new - ctx->igain) / 20;
ctx->silence_counter = 0;
}
else /* silence */
{
ctx->silence_counter++;
/* silence > 2 seconds: reduce gain */
if ((ctx->igain > 65536) && (ctx->silence_counter >= 20))
ctx->igain = (ctx->igain * 62259) >> 16;
}
ctx->counter = 0;
ctx->sample_max = 1;
}
buffer[i] = (unsigned char) (( ((buffer[i] - 128) << 8) * ctx->igain) >> 24) + 128;
}
//ALOGD("[%p] Gain: %d",ctx, ctx->igain);
}