source: mediastreamer2/src/speexec.c @ 1002:980eb537ba69

Last change on this file since 1002:980eb537ba69 was 1002:980eb537ba69, checked in by Simon Morlat <simon.morlat@…>, 3 years ago

make speex encoder use a generic interface

File size: 6.1 KB
Line 
1/*
2mediastreamer2 library - modular sound and video processing and streaming
3Copyright (C) 2010  Belledonne Communications SARL
4Author: Simon Morlat <simon.morlat@linphone.org>
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; either version 2
9of the License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19*/
20
21#include "mediastreamer2/msfilter.h"
22#include <speex/speex_echo.h>
23#include <speex/speex_preprocess.h>
24
25#ifdef HAVE_CONFIG_H
26#include "mediastreamer-config.h"
27#endif
28
29#ifdef WIN32
30#include <malloc.h> /* for alloca */
31#endif
32
33static const int framesize=128;
34
35typedef struct SpeexECState{
36        SpeexEchoState *ecstate;
37        SpeexPreprocessState *den;
38        MSBufferizer ref;
39        MSBufferizer delayed_ref;
40        MSBufferizer echo;
41        int ref_bytes_limit;
42        int framesize;
43        int filterlength;
44        int samplerate;
45        int delay_ms;
46        int tail_length_ms;
47}SpeexECState;
48
49static void speex_ec_init(MSFilter *f){
50        SpeexECState *s=(SpeexECState *)ms_new(SpeexECState,1);
51
52        s->samplerate=8000;
53        ms_bufferizer_init(&s->ref);
54        ms_bufferizer_init(&s->delayed_ref);
55        ms_bufferizer_init(&s->echo);
56        s->delay_ms=0;
57        s->tail_length_ms=250;
58        s->ecstate=NULL;
59        s->framesize=framesize;
60        s->den = NULL;
61
62        f->data=s;
63}
64
65static void speex_ec_uninit(MSFilter *f){
66        SpeexECState *s=(SpeexECState*)f->data;
67        ms_bufferizer_uninit(&s->ref);
68        ms_bufferizer_uninit(&s->delayed_ref);
69        ms_bufferizer_uninit(&s->delayed_ref);
70
71        ms_free(s);
72}
73
74
75static void speex_ec_preprocess(MSFilter *f){
76        SpeexECState *s=(SpeexECState*)f->data;
77        int delay_samples=0;
78        mblk_t *m;
79
80        s->filterlength=(s->tail_length_ms*s->samplerate)/1000;
81        delay_samples=s->delay_ms*s->samplerate/1000;
82        ms_message("Initializing speex echo canceler with framesize=%i, filterlength=%i, delay_samples=%i",
83                s->framesize,s->filterlength,delay_samples);
84        s->ref_bytes_limit=3*s->framesize;
85        s->ecstate=speex_echo_state_init(s->framesize,s->filterlength);
86        s->den = speex_preprocess_state_init(s->framesize, s->samplerate);
87        speex_echo_ctl(s->ecstate, SPEEX_ECHO_SET_SAMPLING_RATE, &s->samplerate);
88        speex_preprocess_ctl(s->den, SPEEX_PREPROCESS_SET_ECHO_STATE, s->ecstate);
89        /* fill with zeroes for the time of the delay*/
90        m=allocb(delay_samples*2,0);
91        ms_bufferizer_put (&s->delayed_ref,m);
92}
93
94/*      inputs[0]= reference signal (sent to soundcard)
95 *      inputs[1]= near speech & echo signal    (read from soundcard)
96 *      outputs[1]=  near end speech, echo removed - towards far end
97*/
98static void speex_ec_process(MSFilter *f){
99        SpeexECState *s=(SpeexECState*)f->data;
100        int nbytes=s->framesize*2;
101        mblk_t *refm;
102        int ref_samples=0;
103        uint8_t *ref,*echo;
104       
105        if (f->inputs[0]!=NULL){
106                while((refm=ms_queue_get(f->inputs[0]))!=NULL){
107                        mblk_t *cp=copymsg(refm);
108                        ref_samples+=msgdsize(refm)/2;
109                        ms_bufferizer_put(&s->ref,refm);
110                        ms_bufferizer_put(&s->delayed_ref,cp);
111                }
112        }
113        if (f->inputs[1]!=NULL){
114                ms_bufferizer_put_from_queue (&s->echo,f->inputs[1]);
115        }
116       
117        ref=(uint8_t*)alloca(nbytes);
118        echo=(uint8_t*)alloca(nbytes);
119        while (ms_bufferizer_read(&s->echo,echo,nbytes)>=nbytes){
120                mblk_t *oref=allocb(nbytes,0);
121                mblk_t *oecho=allocb(nbytes,0);
122                if (ms_bufferizer_read(&s->ref,oref->b_wptr,nbytes)==0){
123                        memset(ref,0,nbytes);
124                        memset(oref->b_wptr,0,nbytes);
125                        /*missing data, use silence instead*/
126                        ms_warning("No ref samples, using silence instead");
127                }else{
128                        ms_bufferizer_read(&s->delayed_ref,ref,nbytes);
129                }
130                oref->b_wptr+=nbytes;
131                ms_queue_put(f->outputs[0],oref);
132                speex_echo_cancellation(s->ecstate,(short*)echo,(short*)ref,(short*)oecho->b_wptr);
133                speex_preprocess_run(s->den, (short*)oecho->b_wptr);
134                oecho->b_wptr+=nbytes;
135                ms_queue_put(f->outputs[1],oecho);
136        }
137        /* do not accumulate too much reference signal */
138        if (ms_bufferizer_get_avail(&s->ref)> s->ref_bytes_limit) {
139                /* reset evrything */
140                ms_warning("purging ref signal");
141                ms_bufferizer_flush(&s->ref);
142                ms_bufferizer_flush(&s->delayed_ref);
143        }
144}
145
146static void speex_ec_postprocess(MSFilter *f){
147        SpeexECState *s=(SpeexECState*)f->data;
148        ms_bufferizer_flush (&s->ref);
149        ms_bufferizer_flush (&s->delayed_ref);
150        ms_bufferizer_flush (&s->echo);
151        if (s->ecstate!=NULL){
152                speex_echo_state_destroy(s->ecstate);
153                s->ecstate=NULL;
154        }
155        if (s->den!=NULL){
156                speex_preprocess_state_destroy(s->den);
157                s->den=NULL;
158        }
159}
160
161static int speex_ec_set_sr(MSFilter *f, void *arg){
162        SpeexECState *s=(SpeexECState*)f->data;
163        s->samplerate = *(int*)arg;
164        return 0;
165}
166
167static int speex_ec_set_framesize(MSFilter *f, void *arg){
168        SpeexECState *s=(SpeexECState*)f->data;
169        s->framesize = *(int*)arg;
170        return 0;
171}
172
173static int speex_ec_set_delay(MSFilter *f, void *arg){
174        SpeexECState *s=(SpeexECState*)f->data;
175        s->delay_ms = *(int*)arg;
176        return 0;
177}
178
179static int speex_ec_set_tail_length(MSFilter *f, void *arg){
180        SpeexECState *s=(SpeexECState*)f->data;
181        s->tail_length_ms=*(int*)arg;
182        return 0;
183}
184
185
186static MSFilterMethod speex_ec_methods[]={
187        {       MS_FILTER_SET_SAMPLE_RATE, speex_ec_set_sr },
188        {       MS_ECHO_CANCELLER_SET_TAIL_LENGTH       ,       speex_ec_set_tail_length        },
189        {       MS_ECHO_CANCELLER_SET_DELAY             ,       speex_ec_set_delay              },
190        {       MS_ECHO_CANCELLER_SET_FRAMESIZE ,       speex_ec_set_framesize          },
191};
192
193#ifdef _MSC_VER
194
195MSFilterDesc ms_speex_ec_desc={
196        MS_SPEEX_EC_ID,
197        "MSSpeexEC",
198        N_("Echo canceller using speex library"),
199        MS_FILTER_OTHER,
200        NULL,
201        2,
202        2,
203        speex_ec_init,
204        speex_ec_preprocess,
205        speex_ec_process,
206        speex_ec_postprocess,
207        speex_ec_uninit,
208        speex_ec_methods
209};
210
211#else
212
213MSFilterDesc ms_speex_ec_desc={
214        .id=MS_SPEEX_EC_ID,
215        .name="MSSpeexEC",
216        .text=N_("Echo canceller using speex library"),
217        .category=MS_FILTER_OTHER,
218        .ninputs=2,
219        .noutputs=2,
220        .init=speex_ec_init,
221        .preprocess=speex_ec_preprocess,
222        .process=speex_ec_process,
223        .postprocess=speex_ec_postprocess,
224        .uninit=speex_ec_uninit,
225        .methods=speex_ec_methods
226};
227
228#endif
229
230MS_FILTER_DESC_EXPORT(ms_speex_ec_desc)
Note: See TracBrowser for help on using the repository browser.