summaryrefslogtreecommitdiff
path: root/drivers/speex/scal.c
blob: 056aa62b56458021d390a63cf65d441ddb9793fa (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* Copyright (C) 2006-2008 CSIRO, Jean-Marc Valin, Xiph.Org Foundation

   File: scal.c
   Shaped comb-allpass filter for channel decorrelation

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/

/*
The algorithm implemented here is described in:

* J.-M. Valin, Perceptually-Motivated Nonlinear Channel Decorrelation For 
  Stereo Acoustic Echo Cancellation, Accepted for Joint Workshop on 
  Hands­free Speech Communication and Microphone Arrays (HSCMA), 2008.
  http://people.xiph.org/~jm/papers/valin_hscma2008.pdf

*/


#include "config.h"


#include "speex/speex_echo.h"
#include "vorbis_psy.h"
#include "arch.h"
#include "os_support.h"
#include "smallft.h"
#include <math.h>
#include <stdlib.h>

#define ALLPASS_ORDER 20

struct SpeexDecorrState_ {
   int rate;
   int channels;
   int frame_size;
#ifdef VORBIS_PSYCHO
   VorbisPsy *psy;
   struct drft_lookup lookup;
   float *wola_mem;
   float *curve;
#endif
   float *vorbis_win;
   int    seed;
   float *y;
   
   /* Per-channel stuff */
   float *buff;
   float (*ring)[ALLPASS_ORDER];
   int *ringID;
   int *order;
   float *alpha;
};



EXPORT SpeexDecorrState *speex_decorrelate_new(int rate, int channels, int frame_size)
{
   int i, ch;
   SpeexDecorrState *st = speex_alloc(sizeof(SpeexDecorrState));
   st->rate = rate;
   st->channels = channels;
   st->frame_size = frame_size;
#ifdef VORBIS_PSYCHO
   st->psy = vorbis_psy_init(rate, 2*frame_size);
   spx_drft_init(&st->lookup, 2*frame_size);
   st->wola_mem = speex_alloc(frame_size*sizeof(float));
   st->curve = speex_alloc(frame_size*sizeof(float));
#endif
   st->y = speex_alloc(frame_size*sizeof(float));

   st->buff = speex_alloc(channels*2*frame_size*sizeof(float));
   st->ringID = speex_alloc(channels*sizeof(int));
   st->order = speex_alloc(channels*sizeof(int));
   st->alpha = speex_alloc(channels*sizeof(float));
   st->ring = speex_alloc(channels*ALLPASS_ORDER*sizeof(float));
   
   /*FIXME: The +20 is there only as a kludge for ALL_PASS_OLA*/
   st->vorbis_win = speex_alloc((2*frame_size+20)*sizeof(float));
   for (i=0;i<2*frame_size;i++)
      st->vorbis_win[i] = sin(.5*SPEEX_PI* sin(SPEEX_PI*i/(2*frame_size))*sin(SPEEX_PI*i/(2*frame_size)) );
   st->seed = rand();
   
   for (ch=0;ch<channels;ch++)
   {
      for (i=0;i<ALLPASS_ORDER;i++)
         st->ring[ch][i] = 0;
      st->ringID[ch] = 0;
      st->alpha[ch] = 0;
      st->order[ch] = 10;
   }
   return st;
}

static float uni_rand(int *seed)
{
   const unsigned int jflone = 0x3f800000;
   const unsigned int jflmsk = 0x007fffff;
   union {int i; float f;} ran;
   *seed = 1664525 * *seed + 1013904223;
   ran.i = jflone | (jflmsk & *seed);
   ran.f -= 1.5;
   return 2*ran.f;
}

static unsigned int irand(int *seed)
{
   *seed = 1664525 * *seed + 1013904223;
   return ((unsigned int)*seed)>>16;
}


EXPORT void speex_decorrelate(SpeexDecorrState *st, const spx_int16_t *in, spx_int16_t *out, int strength)
{
   int ch;
   float amount;
   
   if (strength<0)
      strength = 0;
   if (strength>100)
      strength = 100;
   
   amount = .01*strength;
   for (ch=0;ch<st->channels;ch++)
   {
      int i;
      int N=2*st->frame_size;
      float beta, beta2;
      float *x;
      float max_alpha = 0;
      
      float *buff;
      float *ring;
      int ringID;
      int order;
      float alpha;

      buff = st->buff+ch*2*st->frame_size;
      ring = st->ring[ch];
      ringID = st->ringID[ch];
      order = st->order[ch];
      alpha = st->alpha[ch];
      
      for (i=0;i<st->frame_size;i++)
         buff[i] = buff[i+st->frame_size];
      for (i=0;i<st->frame_size;i++)
         buff[i+st->frame_size] = in[i*st->channels+ch];

      x = buff+st->frame_size;
      beta = 1.-.3*amount*amount;
      if (amount>1)
         beta = 1-sqrt(.4*amount);
      else
         beta = 1-0.63246*amount;
      if (beta<0)
         beta = 0;
   
      beta2 = beta;
      for (i=0;i<st->frame_size;i++)
      {
         st->y[i] = alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[st->frame_size+i+order] 
               + x[i-ALLPASS_ORDER]*st->vorbis_win[st->frame_size+i] 
               - alpha*(ring[ringID]
               - beta*ring[ringID+1>=order?0:ringID+1]);
         ring[ringID++]=st->y[i];
         st->y[i] *= st->vorbis_win[st->frame_size+i];
         if (ringID>=order)
            ringID=0;
      }
      order = order+(irand(&st->seed)%3)-1;
      if (order < 5)
         order = 5;
      if (order > 10)
         order = 10;
      /*order = 5+(irand(&st->seed)%6);*/
      max_alpha = pow(.96+.04*(amount-1),order);
      if (max_alpha > .98/(1.+beta2))
         max_alpha = .98/(1.+beta2);
   
      alpha = alpha + .4*uni_rand(&st->seed);
      if (alpha > max_alpha)
         alpha = max_alpha;
      if (alpha < -max_alpha)
         alpha = -max_alpha;
      for (i=0;i<ALLPASS_ORDER;i++)
         ring[i] = 0;
      ringID = 0;
      for (i=0;i<st->frame_size;i++)
      {
         float tmp =  alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[i+order] 
               + x[i-ALLPASS_ORDER]*st->vorbis_win[i] 
               - alpha*(ring[ringID]
               - beta*ring[ringID+1>=order?0:ringID+1]);
         ring[ringID++]=tmp;
         tmp *= st->vorbis_win[i];
         if (ringID>=order)
            ringID=0;
         st->y[i] += tmp;
      }
   
#ifdef VORBIS_PSYCHO
      float frame[N];
      float scale = 1./N;
      for (i=0;i<2*st->frame_size;i++)
         frame[i] = buff[i];
   //float coef = .5*0.78130;
      float coef = SPEEX_PI*0.075063 * 0.93763 * amount * .8 * 0.707;
      compute_curve(st->psy, buff, st->curve);
      for (i=1;i<st->frame_size;i++)
      {
         float x1,x2;
         float gain;
         do {
            x1 = uni_rand(&st->seed);
            x2 = uni_rand(&st->seed);
         } while (x1*x1+x2*x2 > 1.);
         gain = coef*sqrt(.1+st->curve[i]);
         frame[2*i-1] = gain*x1;
         frame[2*i] = gain*x2;
      }
      frame[0] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[0]);
      frame[2*st->frame_size-1] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[st->frame_size-1]);
      spx_drft_backward(&st->lookup,frame);
      for (i=0;i<2*st->frame_size;i++)
         frame[i] *= st->vorbis_win[i];
#endif
   
      for (i=0;i<st->frame_size;i++)
      {
#ifdef VORBIS_PSYCHO
         float tmp = st->y[i] + frame[i] + st->wola_mem[i];
         st->wola_mem[i] = frame[i+st->frame_size];
#else
         float tmp = st->y[i];
#endif
         if (tmp>32767)
            tmp = 32767;
         if (tmp < -32767)
            tmp = -32767;
         out[i*st->channels+ch] = tmp;
      }
      
      st->ringID[ch] = ringID;
      st->order[ch] = order;
      st->alpha[ch] = alpha;

   }
}

EXPORT void speex_decorrelate_destroy(SpeexDecorrState *st)
{
#ifdef VORBIS_PSYCHO
   vorbis_psy_destroy(st->psy);
   speex_free(st->wola_mem);
   speex_free(st->curve);
#endif
   speex_free(st->buff);
   speex_free(st->ring);
   speex_free(st->ringID);
   speex_free(st->alpha);
   speex_free(st->vorbis_win);
   speex_free(st->order);
   speex_free(st->y);
   speex_free(st);
}