source: mediastreamer2/linphone/mediastreamer2/src/msvolume.c @ 356:4e1430487bce

Last change on this file since 356:4e1430487bce was 356:4e1430487bce, checked in by smorlat <smorlat@…>, 4 years ago

tunning again

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@359 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 5.3 KB
Line 
1/*
2mediastreamer2 library - modular sound and video processing and streaming
3Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*/
19
20#include "mediastreamer2/msvolume.h"
21#include <math.h>
22
23static const float max_e=32767*32767;
24static const float coef=0.1;
25static const float gain_k=0.02;
26static const float en_weight=4.0;
27static const float noise_thres=0.1;
28
29
30typedef struct Volume{
31        float energy;
32        float norm_en;
33        float gain;
34        float static_gain;
35        float gain_k;
36        float thres;
37        MSFilter *peer;
38        bool_t ea_active;
39}Volume;
40
41static void volume_init(MSFilter *f){
42        Volume *v=(Volume*)ms_new(Volume,1);
43        v->energy=0;
44        v->norm_en=0;
45        v->static_gain=v->gain=1;
46        v->ea_active=FALSE;
47        v->gain_k=gain_k;
48        v->thres=noise_thres;
49        v->peer=NULL;
50        f->data=v;
51}
52
53static void volume_uninit(MSFilter *f){
54        ms_free(f->data);
55}
56
57static int volume_get(MSFilter *f, void *arg){
58        float *farg=(float*)arg;
59        Volume *v=(Volume*)f->data;
60        *farg=10*log10f((v->energy+1)/max_e);
61        return 0;
62}
63
64static int volume_get_linear(MSFilter *f, void *arg){
65        float *farg=(float*)arg;
66        Volume *v=(Volume*)f->data;
67        *farg=(v->energy+1)/max_e;
68        return 0;
69}
70
71static inline float compute_gain(float static_gain, float energy, float weight){
72        float ret=static_gain*(1 - (energy*weight));
73        if (ret<0) ret=0;
74        return ret;
75}
76
77static void volume_echo_avoider_process(Volume *v){
78        float peer_e;
79        float gain;
80        float gain_k2;
81        ms_filter_call_method(v->peer,MS_VOLUME_GET_LINEAR,&peer_e);
82        peer_e=sqrt(peer_e);
83        if (v->ea_active){
84                if (peer_e>v->thres){
85                        /*lower our output*/
86                        gain=compute_gain(v->static_gain,peer_e,en_weight);
87                }else {
88                        gain=v->static_gain;
89                        v->ea_active=FALSE;
90                }
91        }else{
92                int peer_active=FALSE;
93                ms_filter_call_method(v->peer,MS_VOLUME_GET_EA_STATE,&peer_active);
94                if (peer_e>v->thres && ! peer_active){
95                        /*lower our output*/
96                        gain=compute_gain(v->static_gain,peer_e,en_weight);
97                        v->ea_active=TRUE;
98                }else gain=v->static_gain;
99        }
100        if (v->ea_active){
101                gain_k2=5*v->gain_k;
102        }else gain_k2=v->gain_k;
103        v->gain=(v->gain*(1-gain_k2)) + (gain_k2*gain);
104        ms_message("ea_active=%i, peer_e=%f gain=%f gain_k=%f",v->ea_active,peer_e,v->gain, v->gain_k);
105}
106
107static int volume_set_gain(MSFilter *f, void *arg){
108        float *farg=(float*)arg;
109        Volume *v=(Volume*)f->data;
110        v->gain=v->static_gain=*farg;
111        return 0;
112}
113
114
115static int volume_get_ea_state(MSFilter *f, void *arg){
116        int *barg=(int*)arg;
117        Volume *v=(Volume*)f->data;
118        *barg=v->ea_active;
119        return 0;
120}
121
122static int volume_set_peer(MSFilter *f, void *arg){
123        MSFilter *p=(MSFilter*)arg;
124        Volume *v=(Volume*)f->data;
125        v->peer=p;
126        return 0;
127}
128
129static int volume_set_ea_threshold(MSFilter *f, void*arg){
130        Volume *v=(Volume*)f->data;
131        float val=*(float*)arg;
132        if (val<0 || val>1) {
133                ms_error("Error: threshold must be in range [0..1]");
134                return -1;
135        }
136        v->thres=val;
137        return 0;
138}
139
140static int volume_set_ea_speed(MSFilter *f, void*arg){
141        Volume *v=(Volume*)f->data;
142        float val=*(float*)arg;
143        if (val<0 || val>1) {
144                ms_error("Error: speed must be in range [0..1]");
145                return -1;
146        }
147        v->gain_k=val;
148        return 0;
149}
150
151static inline int16_t saturate(float val){
152        return (val>32767) ? 32767 : ( (val<-32767) ? -32767 : val);
153}
154
155static void volume_process(MSFilter *f){
156        mblk_t *m;
157        int16_t *sample;
158        Volume *v=(Volume*)f->data;
159        float en=v->energy;
160        while((m=ms_queue_get(f->inputs[0]))!=NULL){
161                for (   sample=(int16_t*)m->b_rptr;
162                        sample<(int16_t*)m->b_wptr;
163                        ++sample){
164                        float s=*sample;
165                        en=(s*s*coef) + (1.0-coef)*en;
166                }
167                if (v->peer){
168                        volume_echo_avoider_process(v); 
169                }
170                if (v->gain!=1){
171                        for (   sample=(int16_t*)m->b_rptr;
172                                sample<(int16_t*)m->b_wptr;
173                                ++sample){
174                                float s=*sample;
175                                *sample=saturate(s*v->gain);
176                        }
177                }
178                ms_queue_put(f->outputs[0],m);
179        }
180        v->energy=en;
181}
182
183static MSFilterMethod methods[]={
184        {       MS_VOLUME_GET           ,       volume_get              },
185        {       MS_VOLUME_GET_LINEAR    ,       volume_get_linear       },
186        {       MS_VOLUME_SET_GAIN      ,       volume_set_gain         },
187        {       MS_VOLUME_GET_EA_STATE  ,       volume_get_ea_state     },
188        {       MS_VOLUME_SET_PEER      ,       volume_set_peer         },
189        {       MS_VOLUME_SET_EA_THRESHOLD ,    volume_set_ea_threshold },
190        {       MS_VOLUME_SET_EA_SPEED  ,       volume_set_ea_speed     },
191        {       0                       ,       NULL                    }
192};
193
194#ifndef _MSC_VER
195MSFilterDesc ms_volume_desc={
196        .name="MSVolume",
197        .text=N_("A filter to make level measurements on 16 bits pcm audio stream"),
198        .id=MS_VOLUME_ID,
199        .category=MS_FILTER_OTHER,
200        .ninputs=1,
201        .noutputs=1,
202        .init=volume_init,
203        .uninit=volume_uninit,
204        .process=volume_process,
205        .methods=methods
206};
207#else
208MSFilterDesc ms_volume_desc={
209        MS_VOLUME_ID,
210        "MSVolume",
211        N_("A filter to make level measurements on 16 bits pcm audio stream"),
212        MS_FILTER_OTHER,
213        NULL,
214        1,
215        1,
216        volume_init,
217        NULL,
218        volume_process,
219        NULL,
220        volume_uninit,
221        methods
222};
223#endif
224
225MS_FILTER_DESC_EXPORT(ms_volume_desc)
226
227
Note: See TracBrowser for help on using the repository browser.