scal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Copyright (C) 2006-2008 CSIRO, Jean-Marc Valin, Xiph.Org Foundation
  2. File: scal.c
  3. Shaped comb-allpass filter for channel decorrelation
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The name of the author may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  18. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  23. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. The algorithm implemented here is described in:
  28. * J.-M. Valin, Perceptually-Motivated Nonlinear Channel Decorrelation For
  29. Stereo Acoustic Echo Cancellation, Accepted for Joint Workshop on
  30. Hands­free Speech Communication and Microphone Arrays (HSCMA), 2008.
  31. http://people.xiph.org/~jm/papers/valin_hscma2008.pdf
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "speex/speex_echo.h"
  37. #include "vorbis_psy.h"
  38. #include "arch.h"
  39. #include "os_support.h"
  40. #include "smallft.h"
  41. #include <math.h>
  42. #include <stdlib.h>
  43. #ifndef M_PI
  44. #define M_PI 3.14159265358979323846 /* pi */
  45. #endif
  46. #define ALLPASS_ORDER 20
  47. struct SpeexDecorrState_ {
  48. int rate;
  49. int channels;
  50. int frame_size;
  51. #ifdef VORBIS_PSYCHO
  52. VorbisPsy *psy;
  53. struct drft_lookup lookup;
  54. float *wola_mem;
  55. float *curve;
  56. #endif
  57. float *vorbis_win;
  58. int seed;
  59. float *y;
  60. /* Per-channel stuff */
  61. float *buff;
  62. float (*ring)[ALLPASS_ORDER];
  63. int *ringID;
  64. int *order;
  65. float *alpha;
  66. };
  67. EXPORT SpeexDecorrState *speex_decorrelate_new(int rate, int channels, int frame_size)
  68. {
  69. int i, ch;
  70. SpeexDecorrState *st = speex_alloc(sizeof(SpeexDecorrState));
  71. st->rate = rate;
  72. st->channels = channels;
  73. st->frame_size = frame_size;
  74. #ifdef VORBIS_PSYCHO
  75. st->psy = vorbis_psy_init(rate, 2*frame_size);
  76. spx_drft_init(&st->lookup, 2*frame_size);
  77. st->wola_mem = speex_alloc(frame_size*sizeof(float));
  78. st->curve = speex_alloc(frame_size*sizeof(float));
  79. #endif
  80. st->y = speex_alloc(frame_size*sizeof(float));
  81. st->buff = speex_alloc(channels*2*frame_size*sizeof(float));
  82. st->ringID = speex_alloc(channels*sizeof(int));
  83. st->order = speex_alloc(channels*sizeof(int));
  84. st->alpha = speex_alloc(channels*sizeof(float));
  85. st->ring = speex_alloc(channels*ALLPASS_ORDER*sizeof(float));
  86. /*FIXME: The +20 is there only as a kludge for ALL_PASS_OLA*/
  87. st->vorbis_win = speex_alloc((2*frame_size+20)*sizeof(float));
  88. for (i=0;i<2*frame_size;i++)
  89. st->vorbis_win[i] = sin(.5*M_PI* sin(M_PI*i/(2*frame_size))*sin(M_PI*i/(2*frame_size)) );
  90. st->seed = rand();
  91. for (ch=0;ch<channels;ch++)
  92. {
  93. for (i=0;i<ALLPASS_ORDER;i++)
  94. st->ring[ch][i] = 0;
  95. st->ringID[ch] = 0;
  96. st->alpha[ch] = 0;
  97. st->order[ch] = 10;
  98. }
  99. return st;
  100. }
  101. static float uni_rand(int *seed)
  102. {
  103. const unsigned int jflone = 0x3f800000;
  104. const unsigned int jflmsk = 0x007fffff;
  105. union {int i; float f;} ran;
  106. *seed = 1664525 * *seed + 1013904223;
  107. ran.i = jflone | (jflmsk & *seed);
  108. ran.f -= 1.5;
  109. return 2*ran.f;
  110. }
  111. static unsigned int irand(int *seed)
  112. {
  113. *seed = 1664525 * *seed + 1013904223;
  114. return ((unsigned int)*seed)>>16;
  115. }
  116. EXPORT void speex_decorrelate(SpeexDecorrState *st, const spx_int16_t *in, spx_int16_t *out, int strength)
  117. {
  118. int ch;
  119. float amount;
  120. if (strength<0)
  121. strength = 0;
  122. if (strength>100)
  123. strength = 100;
  124. amount = .01*strength;
  125. for (ch=0;ch<st->channels;ch++)
  126. {
  127. int i;
  128. //int N=2*st->frame_size;
  129. float beta, beta2;
  130. float *x;
  131. float max_alpha = 0;
  132. float *buff;
  133. float *ring;
  134. int ringID;
  135. int order;
  136. float alpha;
  137. buff = st->buff+ch*2*st->frame_size;
  138. ring = st->ring[ch];
  139. ringID = st->ringID[ch];
  140. order = st->order[ch];
  141. alpha = st->alpha[ch];
  142. for (i=0;i<st->frame_size;i++)
  143. buff[i] = buff[i+st->frame_size];
  144. for (i=0;i<st->frame_size;i++)
  145. buff[i+st->frame_size] = in[i*st->channels+ch];
  146. x = buff+st->frame_size;
  147. if (amount>1)
  148. beta = 1-sqrt(.4*amount);
  149. else
  150. beta = 1-0.63246*amount;
  151. if (beta<0)
  152. beta = 0;
  153. beta2 = beta;
  154. for (i=0;i<st->frame_size;i++)
  155. {
  156. st->y[i] = alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[st->frame_size+i+order]
  157. + x[i-ALLPASS_ORDER]*st->vorbis_win[st->frame_size+i]
  158. - alpha*(ring[ringID]
  159. - beta*ring[ringID+1>=order?0:ringID+1]);
  160. ring[ringID++]=st->y[i];
  161. st->y[i] *= st->vorbis_win[st->frame_size+i];
  162. if (ringID>=order)
  163. ringID=0;
  164. }
  165. order = order+(irand(&st->seed)%3)-1;
  166. if (order < 5)
  167. order = 5;
  168. if (order > 10)
  169. order = 10;
  170. /*order = 5+(irand(&st->seed)%6);*/
  171. max_alpha = pow(.96+.04*(amount-1),order);
  172. if (max_alpha > .98/(1.+beta2))
  173. max_alpha = .98/(1.+beta2);
  174. alpha = alpha + .4*uni_rand(&st->seed);
  175. if (alpha > max_alpha)
  176. alpha = max_alpha;
  177. if (alpha < -max_alpha)
  178. alpha = -max_alpha;
  179. for (i=0;i<ALLPASS_ORDER;i++)
  180. ring[i] = 0;
  181. ringID = 0;
  182. for (i=0;i<st->frame_size;i++)
  183. {
  184. float tmp = alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[i+order]
  185. + x[i-ALLPASS_ORDER]*st->vorbis_win[i]
  186. - alpha*(ring[ringID]
  187. - beta*ring[ringID+1>=order?0:ringID+1]);
  188. ring[ringID++]=tmp;
  189. tmp *= st->vorbis_win[i];
  190. if (ringID>=order)
  191. ringID=0;
  192. st->y[i] += tmp;
  193. }
  194. #ifdef VORBIS_PSYCHO
  195. float frame[N];
  196. float scale = 1./N;
  197. for (i=0;i<2*st->frame_size;i++)
  198. frame[i] = buff[i];
  199. //float coef = .5*0.78130;
  200. float coef = M_PI*0.075063 * 0.93763 * amount * .8 * 0.707;
  201. compute_curve(st->psy, buff, st->curve);
  202. for (i=1;i<st->frame_size;i++)
  203. {
  204. float x1,x2;
  205. float gain;
  206. do {
  207. x1 = uni_rand(&st->seed);
  208. x2 = uni_rand(&st->seed);
  209. } while (x1*x1+x2*x2 > 1.);
  210. gain = coef*sqrt(.1+st->curve[i]);
  211. frame[2*i-1] = gain*x1;
  212. frame[2*i] = gain*x2;
  213. }
  214. frame[0] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[0]);
  215. frame[2*st->frame_size-1] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[st->frame_size-1]);
  216. spx_drft_backward(&st->lookup,frame);
  217. for (i=0;i<2*st->frame_size;i++)
  218. frame[i] *= st->vorbis_win[i];
  219. #endif
  220. for (i=0;i<st->frame_size;i++)
  221. {
  222. #ifdef VORBIS_PSYCHO
  223. float tmp = st->y[i] + frame[i] + st->wola_mem[i];
  224. st->wola_mem[i] = frame[i+st->frame_size];
  225. #else
  226. float tmp = st->y[i];
  227. #endif
  228. if (tmp>32767)
  229. tmp = 32767;
  230. if (tmp < -32767)
  231. tmp = -32767;
  232. out[i*st->channels+ch] = tmp;
  233. }
  234. st->ringID[ch] = ringID;
  235. st->order[ch] = order;
  236. st->alpha[ch] = alpha;
  237. }
  238. }
  239. EXPORT void speex_decorrelate_destroy(SpeexDecorrState *st)
  240. {
  241. #ifdef VORBIS_PSYCHO
  242. vorbis_psy_destroy(st->psy);
  243. speex_free(st->wola_mem);
  244. speex_free(st->curve);
  245. #endif
  246. speex_free(st->buff);
  247. speex_free(st->ring);
  248. speex_free(st->ringID);
  249. speex_free(st->alpha);
  250. speex_free(st->vorbis_win);
  251. speex_free(st->order);
  252. speex_free(st->y);
  253. speex_free(st);
  254. }