scal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include "config.h"
  34. #include "speex/speex_echo.h"
  35. #include "vorbis_psy.h"
  36. #include "arch.h"
  37. #include "os_support.h"
  38. #include "smallft.h"
  39. #include <math.h>
  40. #include <stdlib.h>
  41. #define ALLPASS_ORDER 20
  42. struct SpeexDecorrState_ {
  43. int rate;
  44. int channels;
  45. int frame_size;
  46. #ifdef VORBIS_PSYCHO
  47. VorbisPsy *psy;
  48. struct drft_lookup lookup;
  49. float *wola_mem;
  50. float *curve;
  51. #endif
  52. float *vorbis_win;
  53. int seed;
  54. float *y;
  55. /* Per-channel stuff */
  56. float *buff;
  57. float (*ring)[ALLPASS_ORDER];
  58. int *ringID;
  59. int *order;
  60. float *alpha;
  61. };
  62. EXPORT SpeexDecorrState *speex_decorrelate_new(int rate, int channels, int frame_size)
  63. {
  64. int i, ch;
  65. SpeexDecorrState *st = speex_alloc(sizeof(SpeexDecorrState));
  66. st->rate = rate;
  67. st->channels = channels;
  68. st->frame_size = frame_size;
  69. #ifdef VORBIS_PSYCHO
  70. st->psy = vorbis_psy_init(rate, 2*frame_size);
  71. spx_drft_init(&st->lookup, 2*frame_size);
  72. st->wola_mem = speex_alloc(frame_size*sizeof(float));
  73. st->curve = speex_alloc(frame_size*sizeof(float));
  74. #endif
  75. st->y = speex_alloc(frame_size*sizeof(float));
  76. st->buff = speex_alloc(channels*2*frame_size*sizeof(float));
  77. st->ringID = speex_alloc(channels*sizeof(int));
  78. st->order = speex_alloc(channels*sizeof(int));
  79. st->alpha = speex_alloc(channels*sizeof(float));
  80. st->ring = speex_alloc(channels*ALLPASS_ORDER*sizeof(float));
  81. /*FIXME: The +20 is there only as a kludge for ALL_PASS_OLA*/
  82. st->vorbis_win = speex_alloc((2*frame_size+20)*sizeof(float));
  83. for (i=0;i<2*frame_size;i++)
  84. st->vorbis_win[i] = sin(.5*SPEEX_PI* sin(SPEEX_PI*i/(2*frame_size))*sin(SPEEX_PI*i/(2*frame_size)) );
  85. st->seed = rand();
  86. for (ch=0;ch<channels;ch++)
  87. {
  88. for (i=0;i<ALLPASS_ORDER;i++)
  89. st->ring[ch][i] = 0;
  90. st->ringID[ch] = 0;
  91. st->alpha[ch] = 0;
  92. st->order[ch] = 10;
  93. }
  94. return st;
  95. }
  96. static float uni_rand(int *seed)
  97. {
  98. const unsigned int jflone = 0x3f800000;
  99. const unsigned int jflmsk = 0x007fffff;
  100. union {int i; float f;} ran;
  101. *seed = 1664525 * *seed + 1013904223;
  102. ran.i = jflone | (jflmsk & *seed);
  103. ran.f -= 1.5;
  104. return 2*ran.f;
  105. }
  106. static unsigned int irand(int *seed)
  107. {
  108. *seed = 1664525 * *seed + 1013904223;
  109. return ((unsigned int)*seed)>>16;
  110. }
  111. EXPORT void speex_decorrelate(SpeexDecorrState *st, const spx_int16_t *in, spx_int16_t *out, int strength)
  112. {
  113. int ch;
  114. float amount;
  115. if (strength<0)
  116. strength = 0;
  117. if (strength>100)
  118. strength = 100;
  119. amount = .01*strength;
  120. for (ch=0;ch<st->channels;ch++)
  121. {
  122. int i;
  123. int N=2*st->frame_size;
  124. float beta, beta2;
  125. float *x;
  126. float max_alpha = 0;
  127. float *buff;
  128. float *ring;
  129. int ringID;
  130. int order;
  131. float alpha;
  132. buff = st->buff+ch*2*st->frame_size;
  133. ring = st->ring[ch];
  134. ringID = st->ringID[ch];
  135. order = st->order[ch];
  136. alpha = st->alpha[ch];
  137. for (i=0;i<st->frame_size;i++)
  138. buff[i] = buff[i+st->frame_size];
  139. for (i=0;i<st->frame_size;i++)
  140. buff[i+st->frame_size] = in[i*st->channels+ch];
  141. x = buff+st->frame_size;
  142. beta = 1.-.3*amount*amount;
  143. if (amount>1)
  144. beta = 1-sqrt(.4*amount);
  145. else
  146. beta = 1-0.63246*amount;
  147. if (beta<0)
  148. beta = 0;
  149. beta2 = beta;
  150. for (i=0;i<st->frame_size;i++)
  151. {
  152. st->y[i] = alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[st->frame_size+i+order]
  153. + x[i-ALLPASS_ORDER]*st->vorbis_win[st->frame_size+i]
  154. - alpha*(ring[ringID]
  155. - beta*ring[ringID+1>=order?0:ringID+1]);
  156. ring[ringID++]=st->y[i];
  157. st->y[i] *= st->vorbis_win[st->frame_size+i];
  158. if (ringID>=order)
  159. ringID=0;
  160. }
  161. order = order+(irand(&st->seed)%3)-1;
  162. if (order < 5)
  163. order = 5;
  164. if (order > 10)
  165. order = 10;
  166. /*order = 5+(irand(&st->seed)%6);*/
  167. max_alpha = pow(.96+.04*(amount-1),order);
  168. if (max_alpha > .98/(1.+beta2))
  169. max_alpha = .98/(1.+beta2);
  170. alpha = alpha + .4*uni_rand(&st->seed);
  171. if (alpha > max_alpha)
  172. alpha = max_alpha;
  173. if (alpha < -max_alpha)
  174. alpha = -max_alpha;
  175. for (i=0;i<ALLPASS_ORDER;i++)
  176. ring[i] = 0;
  177. ringID = 0;
  178. for (i=0;i<st->frame_size;i++)
  179. {
  180. float tmp = alpha*(x[i-ALLPASS_ORDER+order]-beta*x[i-ALLPASS_ORDER+order-1])*st->vorbis_win[i+order]
  181. + x[i-ALLPASS_ORDER]*st->vorbis_win[i]
  182. - alpha*(ring[ringID]
  183. - beta*ring[ringID+1>=order?0:ringID+1]);
  184. ring[ringID++]=tmp;
  185. tmp *= st->vorbis_win[i];
  186. if (ringID>=order)
  187. ringID=0;
  188. st->y[i] += tmp;
  189. }
  190. #ifdef VORBIS_PSYCHO
  191. float frame[N];
  192. float scale = 1./N;
  193. for (i=0;i<2*st->frame_size;i++)
  194. frame[i] = buff[i];
  195. //float coef = .5*0.78130;
  196. float coef = SPEEX_PI*0.075063 * 0.93763 * amount * .8 * 0.707;
  197. compute_curve(st->psy, buff, st->curve);
  198. for (i=1;i<st->frame_size;i++)
  199. {
  200. float x1,x2;
  201. float gain;
  202. do {
  203. x1 = uni_rand(&st->seed);
  204. x2 = uni_rand(&st->seed);
  205. } while (x1*x1+x2*x2 > 1.);
  206. gain = coef*sqrt(.1+st->curve[i]);
  207. frame[2*i-1] = gain*x1;
  208. frame[2*i] = gain*x2;
  209. }
  210. frame[0] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[0]);
  211. frame[2*st->frame_size-1] = coef*uni_rand(&st->seed)*sqrt(.1+st->curve[st->frame_size-1]);
  212. spx_drft_backward(&st->lookup,frame);
  213. for (i=0;i<2*st->frame_size;i++)
  214. frame[i] *= st->vorbis_win[i];
  215. #endif
  216. for (i=0;i<st->frame_size;i++)
  217. {
  218. #ifdef VORBIS_PSYCHO
  219. float tmp = st->y[i] + frame[i] + st->wola_mem[i];
  220. st->wola_mem[i] = frame[i+st->frame_size];
  221. #else
  222. float tmp = st->y[i];
  223. #endif
  224. if (tmp>32767)
  225. tmp = 32767;
  226. if (tmp < -32767)
  227. tmp = -32767;
  228. out[i*st->channels+ch] = tmp;
  229. }
  230. st->ringID[ch] = ringID;
  231. st->order[ch] = order;
  232. st->alpha[ch] = alpha;
  233. }
  234. }
  235. EXPORT void speex_decorrelate_destroy(SpeexDecorrState *st)
  236. {
  237. #ifdef VORBIS_PSYCHO
  238. vorbis_psy_destroy(st->psy);
  239. speex_free(st->wola_mem);
  240. speex_free(st->curve);
  241. #endif
  242. speex_free(st->buff);
  243. speex_free(st->ring);
  244. speex_free(st->ringID);
  245. speex_free(st->alpha);
  246. speex_free(st->vorbis_win);
  247. speex_free(st->order);
  248. speex_free(st->y);
  249. speex_free(st);
  250. }