📄 gtagc32.c
字号:
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <malloc.h>
#include "GtAGC32.h"
/*三个电平值参数,以dB计算, */GtAGC* NewGtagc(int peek_level, int dynamic_threshold, int noise_threshold, int inputmode)
{ GtAGC *agc = malloc(sizeof(GtAGC));
if(agc == NULL)
{
return NULL;
}
SetGtagc(agc, peek_level, dynamic_threshold, noise_threshold,inputmode);
return agc;
}
//设置AGC参数int SetGtagc(GtAGC *agc, int peek_level, int dynamic_threshold, int noise_threshold, int inputmode)
{ int peak, dynamic, noise;
if(agc == NULL)
{
return -1;
} agc->sample_max[0]=1; agc->sample_max[1]=1;
agc->pre_max[0] = 1; agc->pre_max[1] = 1; agc->post_max[0] =1; agc->post_max[1] =1; agc->pre_max1[0] = 1; agc->pre_max1[1] = 1; agc->post_max1[0] =1; agc->post_max1[1] =1; agc->pre_sum[0] = 1; agc->pre_sum[1] = 1; agc->post_sum[0] =1; agc->post_sum[1] =1; agc->gain[0] = 1.0f; agc->gain[1] = 1.0f; agc->silence_counter[0] = 0; agc->silence_counter[1] = 0;
agc->counter[0] = 0; agc->counter[1] = 0;
/* if(peek_level>-3)peek_level=-3; else if(peek_level<-20)peek_level=-20; if( dynamic_threshold > peek_level ) dynamic_threshold=peek_level; else if( dynamic_threshold < (peek_level-20) ) dynamic_threshold=peek_level-20; if( noise_threshold > (dynamic_threshold -10) ) noise_threshold= dynamic_threshold -10; */ peak = REF_0dB * pow( 10, ( (float)peek_level / 20.0 ) )/10; dynamic= REF_0dB * pow( 10, ( (float) dynamic_threshold / 20.0) )/10; noise = REF_0dB * pow( 10, ( (float) noise_threshold / 20.0) )/10;
printf("parameter:%d %d,%d,%d .\n",REF_0dB, peek_level, dynamic_threshold, noise_threshold );
agc->peak = peak;
printf("agc->peak:%ld\n",agc->peak);
//第二个门限
agc->active_threshold = dynamic; printf("agc->active_theshold:%ld\n",agc->active_threshold);
agc->silence_threshold = noise; printf("agc->silence_threshold:%ld\n",agc->silence_threshold);
agc->pk=(float)(agc->peak - agc->active_threshold) / (MAX24bit - agc->silence_threshold);
return 1;
}
void DeleteGtagc(GtAGC *agc)
{
free(agc);
}
void DoGtagc(GtAGC *agc, long *buffer, int len, int volume, int bpcount,int adaes3)
{
int i; float gain_new[2];
long sample,sample1; int index;
agc->pre_max[0] =1; agc->pre_max[1] =1; agc->post_max[0] =1; agc->post_max[1] =1; agc->pre_sum[0] =1; agc->pre_sum[1] =1; agc->post_sum[0] =1; agc->post_sum[1] =1; agc->pre_max1[0] =1; agc->pre_max1[1] =1; agc->post_max1[0] =1; agc->post_max1[1] =1;
for(i=0;i<len;i++)
{
index=i&0x00000001;
//由24bit转化为32bit, 特别注意正负符号 buffer[i]=( buffer[i] >= MAX24bit ? (buffer[i] | 0xff000000) : buffer[i]);
sample = buffer[i];
sample = (sample < 0 ? -(sample):sample);
if(sample > (long)agc->sample_max[index])
{
agc->sample_max[index] = (long)sample;
} if(sample > (long)agc->pre_max[index])
{
agc->pre_max[index] = (long)sample;
} agc->pre_sum[index] += sample;//*sample; agc->counter[index] ++;
//attack
if ((float)sample * agc->gain[index] > agc->peak)
{
agc->gain[index] = (float)(agc->peak / (float) agc->sample_max[index]) * 0.95f;
agc->silence_counter[index] = 0;
buffer[i] = (long) (buffer[i] * agc->gain[index]);
continue;
}
if (agc->counter[index] >= 1024*2)
{
//release
if ((long)(agc->sample_max[index]) > agc->silence_threshold) //下限二
{ gain_new[index] = 0.95 * ( (float) (agc->pk) * (agc->sample_max[index] - \ agc->silence_threshold) + agc->active_threshold ) / \ ( (float) (agc->sample_max[index]) );
if (agc->silence_counter[index] > 40)
agc->gain[index] += (gain_new[index] - agc->gain[index]) * 0.5f;
else
agc->gain[index] += (gain_new[index] - agc->gain[index]) * 0.1f;
agc->silence_counter[index] = 0;
}
else
{
agc->silence_counter[index]++;
if ((agc->gain[index] > 1.0f) && (agc->silence_counter >= 20))
agc->gain[index] *= 0.8f;
}
agc->counter[index] = 0;
agc->sample_max[index] = 1;
}//end if
buffer[i] = (long) (buffer[i] * agc->gain[index]);
//sample = buffer[i];
/*buffer[i]=buffer[i]*bpcount/100;*/
sample = buffer[i];
sample = (sample < 0 ? -(sample):sample); if(sample > (long)agc->post_max[index])
{
agc->post_max[index] = (long)sample;
} agc->post_sum[index] += sample;//*sample;
}//end for for(i=0;i<len;i++) { index=i&0x00000001;
//由24bit转化为32bit, 特别注意正负符号 buffer[i]=( buffer[i] >= MAX24bit ? (buffer[i] | 0xff000000) : buffer[i]);
sample1 = buffer[i];
sample1 = (sample1 < 0 ? -(sample1):sample1);
if(sample1 > (long)agc->sample_max[index])
{
agc->sample_max[index] = (long)sample1;
} if(sample1 > (long)agc->pre_max1[index])
{
agc->pre_max1[index] = (long)sample1;
} if(volume==80) buffer[i]=0x00000000; else if(volume== 20) buffer[i]=buffer[i]*0.1;
else if(volume== 1) buffer[i]=buffer[i]*0.9;
else if(volume== 2) buffer[i]=buffer[i]*0.8;
else if(volume== 3) buffer[i]=buffer[i]*0.7;
else if(volume== 4) buffer[i]=buffer[i]*0.63;
else if(volume== 5) buffer[i]=buffer[i]*0.56;
else if(volume== 6) buffer[i]=buffer[i]*0.5;
else if(volume== 7) buffer[i]=buffer[i]*0.45;
else if(volume== 8) buffer[i]=buffer[i]*0.4;
else if(volume== 9) buffer[i]=buffer[i]*0.35;
else if(volume== 10) buffer[i]=buffer[i]*0.31;
else if(volume== 11) buffer[i]=buffer[i]*0.28;
else if(volume== 12) buffer[i]=buffer[i]*0.25;
else if(volume== 13) buffer[i]=buffer[i]*0.22;
else if(volume== 14) buffer[i]=buffer[i]*0.2;
else if(volume== 15) buffer[i]=buffer[i]*0.18;
else if(volume== 16) buffer[i]=buffer[i]*0.16;
else if(volume== 17) buffer[i]=buffer[i]*0.14;
else if(volume== 18) buffer[i]=buffer[i]*0.13;
else if(volume== 19) buffer[i]=buffer[i]*0.11;
else if(volume== 0) buffer[i]=buffer[i];
if(bpcount<10000&adaes3==0) buffer[i]=0x00000000;
sample1 = buffer[i];
sample1 = (sample1 < 0 ? -(sample1):sample1); if(sample1 > (long)agc->post_max1[index])
{
agc->post_max1[index] = (long)sample1;
} }
bpcount++;
if(bpcount<=10000&adaes3==1) buffer[i]=0;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -