preprocess.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /* Copyright (C) 2003 Epic Games
  2. Written by Jean-Marc Valin
  3. File: preprocess.c
  4. Preprocessor with denoising based on the algorithm by Ephraim and Malah
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <math.h>
  31. #include "speex/speex_preprocess.h"
  32. #include "misc.h"
  33. #include "smallft.h"
  34. #define max(a,b) ((a) > (b) ? (a) : (b))
  35. #define min(a,b) ((a) < (b) ? (a) : (b))
  36. #ifndef M_PI
  37. #define M_PI 3.14159263
  38. #endif
  39. #define SQRT_M_PI_2 0.88623
  40. #define LOUDNESS_EXP 2.5
  41. #define NB_BANDS 8
  42. #define SPEEX_PROB_START_DEFAULT 0.35f
  43. #define SPEEX_PROB_CONTINUE_DEFAULT 0.20f
  44. #define ZMIN .1
  45. #define ZMAX .316
  46. #define ZMIN_1 10
  47. #define LOG_MIN_MAX_1 0.86859
  48. static void conj_window(float *w, int len)
  49. {
  50. int i;
  51. for (i=0;i<len;i++)
  52. {
  53. float x=4*((float)i)/len;
  54. int inv=0;
  55. if (x<1)
  56. {
  57. } else if (x<2)
  58. {
  59. x=2-x;
  60. inv=1;
  61. } else if (x<3)
  62. {
  63. x=x-2;
  64. inv=1;
  65. } else {
  66. x=4-x;
  67. }
  68. x*=1.9979;
  69. w[i]=(.5-.5*cos(x))*(.5-.5*cos(x));
  70. if (inv)
  71. w[i]=1-w[i];
  72. w[i]=sqrt(w[i]);
  73. }
  74. }
  75. /* This function approximates the gain function
  76. y = gamma(1.25)^2 * M(-.25;1;-x) / sqrt(x)
  77. which multiplied by xi/(1+xi) is the optimal gain
  78. in the loudness domain ( sqrt[amplitude] )
  79. */
  80. static inline float hypergeom_gain(float x)
  81. {
  82. int ind;
  83. float integer, frac;
  84. static const float table[21] = {
  85. 0.82157f, 1.02017f, 1.20461f, 1.37534f, 1.53363f, 1.68092f, 1.81865f,
  86. 1.94811f, 2.07038f, 2.18638f, 2.29688f, 2.40255f, 2.50391f, 2.60144f,
  87. 2.69551f, 2.78647f, 2.87458f, 2.96015f, 3.04333f, 3.12431f, 3.20326f};
  88. if (x>9.5)
  89. return 1+.1296/x;
  90. integer = floor(2*x);
  91. frac = 2*x-integer;
  92. ind = (int)integer;
  93. return ((1-frac)*table[ind] + frac*table[ind+1])/sqrt(x+.0001f);
  94. }
  95. static inline float qcurve(float x)
  96. {
  97. return 1.f/(1.f+.1f/(x*x));
  98. }
  99. SpeexPreprocessState *speex_preprocess_state_init(int frame_size, int sampling_rate)
  100. {
  101. int i;
  102. int N, N3, N4;
  103. SpeexPreprocessState *st = (SpeexPreprocessState *)speex_alloc(sizeof(SpeexPreprocessState));
  104. st->frame_size = frame_size;
  105. /* Round ps_size down to the nearest power of two */
  106. #if 0
  107. i=1;
  108. st->ps_size = st->frame_size;
  109. while(1)
  110. {
  111. if (st->ps_size & ~i)
  112. {
  113. st->ps_size &= ~i;
  114. i<<=1;
  115. } else {
  116. break;
  117. }
  118. }
  119. if (st->ps_size < 3*st->frame_size/4)
  120. st->ps_size = st->ps_size * 3 / 2;
  121. #else
  122. st->ps_size = st->frame_size;
  123. #endif
  124. N = st->ps_size;
  125. N3 = 2*N - st->frame_size;
  126. N4 = st->frame_size - N3;
  127. st->sampling_rate = sampling_rate;
  128. st->denoise_enabled = 1;
  129. st->agc_enabled = 0;
  130. st->agc_level = 8000;
  131. st->vad_enabled = 0;
  132. st->dereverb_enabled = 0;
  133. st->reverb_decay = .5;
  134. st->reverb_level = .2;
  135. st->speech_prob_start = SPEEX_PROB_START_DEFAULT;
  136. st->speech_prob_continue = SPEEX_PROB_CONTINUE_DEFAULT;
  137. st->frame = (float*)speex_alloc(2*N*sizeof(float));
  138. st->ps = (float*)speex_alloc(N*sizeof(float));
  139. st->gain2 = (float*)speex_alloc(N*sizeof(float));
  140. st->window = (float*)speex_alloc(2*N*sizeof(float));
  141. st->noise = (float*)speex_alloc(N*sizeof(float));
  142. st->reverb_estimate = (float*)speex_alloc(N*sizeof(float));
  143. st->old_ps = (float*)speex_alloc(N*sizeof(float));
  144. st->gain = (float*)speex_alloc(N*sizeof(float));
  145. st->prior = (float*)speex_alloc(N*sizeof(float));
  146. st->post = (float*)speex_alloc(N*sizeof(float));
  147. st->loudness_weight = (float*)speex_alloc(N*sizeof(float));
  148. st->inbuf = (float*)speex_alloc(N3*sizeof(float));
  149. st->outbuf = (float*)speex_alloc(N3*sizeof(float));
  150. st->echo_noise = (float*)speex_alloc(N*sizeof(float));
  151. st->S = (float*)speex_alloc(N*sizeof(float));
  152. st->Smin = (float*)speex_alloc(N*sizeof(float));
  153. st->Stmp = (float*)speex_alloc(N*sizeof(float));
  154. st->update_prob = (float*)speex_alloc(N*sizeof(float));
  155. st->zeta = (float*)speex_alloc(N*sizeof(float));
  156. st->Zpeak = 0;
  157. st->Zlast = 0;
  158. st->noise_bands = (float*)speex_alloc(NB_BANDS*sizeof(float));
  159. st->noise_bands2 = (float*)speex_alloc(NB_BANDS*sizeof(float));
  160. st->speech_bands = (float*)speex_alloc(NB_BANDS*sizeof(float));
  161. st->speech_bands2 = (float*)speex_alloc(NB_BANDS*sizeof(float));
  162. st->noise_bandsN = st->speech_bandsN = 1;
  163. conj_window(st->window, 2*N3);
  164. for (i=2*N3;i<2*st->ps_size;i++)
  165. st->window[i]=1;
  166. if (N4>0)
  167. {
  168. for (i=N3-1;i>=0;i--)
  169. {
  170. st->window[i+N3+N4]=st->window[i+N3];
  171. st->window[i+N3]=1;
  172. }
  173. }
  174. for (i=0;i<N;i++)
  175. {
  176. st->noise[i]=1e4;
  177. st->reverb_estimate[i]=0.;
  178. st->old_ps[i]=1e4;
  179. st->gain[i]=1;
  180. st->post[i]=1;
  181. st->prior[i]=1;
  182. }
  183. for (i=0;i<N3;i++)
  184. {
  185. st->inbuf[i]=0;
  186. st->outbuf[i]=0;
  187. }
  188. for (i=0;i<N;i++)
  189. {
  190. float ff=((float)i)*.5*sampling_rate/((float)N);
  191. st->loudness_weight[i] = .35f-.35f*ff/16000.f+.73f*exp(-.5f*(ff-3800)*(ff-3800)/9e5f);
  192. if (st->loudness_weight[i]<.01f)
  193. st->loudness_weight[i]=.01f;
  194. st->loudness_weight[i] *= st->loudness_weight[i];
  195. }
  196. st->speech_prob = 0;
  197. st->last_speech = 1000;
  198. st->loudness = pow(6000,LOUDNESS_EXP);
  199. st->loudness2 = 6000;
  200. st->nb_loudness_adapt = 0;
  201. st->fft_lookup = (struct drft_lookup*)speex_alloc(sizeof(struct drft_lookup));
  202. spx_drft_init(st->fft_lookup,2*N);
  203. st->nb_adapt=0;
  204. st->consec_noise=0;
  205. st->nb_preprocess=0;
  206. return st;
  207. }
  208. void speex_preprocess_state_destroy(SpeexPreprocessState *st)
  209. {
  210. speex_free(st->frame);
  211. speex_free(st->ps);
  212. speex_free(st->gain2);
  213. speex_free(st->window);
  214. speex_free(st->noise);
  215. speex_free(st->reverb_estimate);
  216. speex_free(st->old_ps);
  217. speex_free(st->gain);
  218. speex_free(st->prior);
  219. speex_free(st->post);
  220. speex_free(st->loudness_weight);
  221. speex_free(st->echo_noise);
  222. speex_free(st->S);
  223. speex_free(st->Smin);
  224. speex_free(st->Stmp);
  225. speex_free(st->update_prob);
  226. speex_free(st->zeta);
  227. speex_free(st->noise_bands);
  228. speex_free(st->noise_bands2);
  229. speex_free(st->speech_bands);
  230. speex_free(st->speech_bands2);
  231. speex_free(st->inbuf);
  232. speex_free(st->outbuf);
  233. spx_drft_clear(st->fft_lookup);
  234. speex_free(st->fft_lookup);
  235. speex_free(st);
  236. }
  237. static void update_noise(SpeexPreprocessState *st, float *ps, spx_int32_t *echo)
  238. {
  239. int i;
  240. float beta;
  241. st->nb_adapt++;
  242. beta=1.0f/st->nb_adapt;
  243. if (beta < .05f)
  244. beta=.05f;
  245. if (!echo)
  246. {
  247. for (i=0;i<st->ps_size;i++)
  248. st->noise[i] = (1.f-beta)*st->noise[i] + beta*ps[i];
  249. } else {
  250. for (i=0;i<st->ps_size;i++)
  251. st->noise[i] = (1.f-beta)*st->noise[i] + beta*max(1.f,ps[i]-st->frame_size*st->frame_size*4.0*echo[i]);
  252. #if 0
  253. for (i=0;i<st->ps_size;i++)
  254. st->noise[i] = 0;
  255. #endif
  256. }
  257. }
  258. static int speex_compute_vad(SpeexPreprocessState *st, float *ps, float mean_prior, float mean_post)
  259. {
  260. int i, is_speech=0;
  261. int N = st->ps_size;
  262. float scale=.5f/N;
  263. /* FIXME: Clean this up a bit */
  264. {
  265. float bands[NB_BANDS];
  266. int j;
  267. float p0, p1;
  268. float tot_loudness=0;
  269. float x = sqrt(mean_post);
  270. for (i=5;i<N-10;i++)
  271. {
  272. tot_loudness += scale*st->ps[i] * st->loudness_weight[i];
  273. }
  274. for (i=0;i<NB_BANDS;i++)
  275. {
  276. bands[i]=1e4f;
  277. for (j=i*N/NB_BANDS;j<(i+1)*N/NB_BANDS;j++)
  278. {
  279. bands[i] += ps[j];
  280. }
  281. bands[i]=log(bands[i]);
  282. }
  283. /*p1 = .0005+.6*exp(-.5*(x-.4)*(x-.4)*11)+.1*exp(-1.2*x);
  284. if (x<1.5)
  285. p0=.1*exp(2*(x-1.5));
  286. else
  287. p0=.02+.1*exp(-.2*(x-1.5));
  288. */
  289. p0=1.f/(1.f+exp(3.f*(1.5f-x)));
  290. p1=1.f-p0;
  291. /*fprintf (stderr, "%f %f ", p0, p1);*/
  292. /*p0 *= .99*st->speech_prob + .01*(1-st->speech_prob);
  293. p1 *= .01*st->speech_prob + .99*(1-st->speech_prob);
  294. st->speech_prob = p0/(p1+p0);
  295. */
  296. if (st->noise_bandsN < 50 || st->speech_bandsN < 50)
  297. {
  298. if (mean_post > 5.f)
  299. {
  300. float adapt = 1./st->speech_bandsN++;
  301. if (adapt<.005f)
  302. adapt = .005f;
  303. for (i=0;i<NB_BANDS;i++)
  304. {
  305. st->speech_bands[i] = (1.f-adapt)*st->speech_bands[i] + adapt*bands[i];
  306. /*st->speech_bands2[i] = (1-adapt)*st->speech_bands2[i] + adapt*bands[i]*bands[i];*/
  307. st->speech_bands2[i] = (1.f-adapt)*st->speech_bands2[i] + adapt*(bands[i]-st->speech_bands[i])*(bands[i]-st->speech_bands[i]);
  308. }
  309. } else {
  310. float adapt = 1./st->noise_bandsN++;
  311. if (adapt<.005f)
  312. adapt = .005f;
  313. for (i=0;i<NB_BANDS;i++)
  314. {
  315. st->noise_bands[i] = (1.f-adapt)*st->noise_bands[i] + adapt*bands[i];
  316. /*st->noise_bands2[i] = (1-adapt)*st->noise_bands2[i] + adapt*bands[i]*bands[i];*/
  317. st->noise_bands2[i] = (1.f-adapt)*st->noise_bands2[i] + adapt*(bands[i]-st->noise_bands[i])*(bands[i]-st->noise_bands[i]);
  318. }
  319. }
  320. }
  321. p0=p1=1;
  322. for (i=0;i<NB_BANDS;i++)
  323. {
  324. float noise_var, speech_var;
  325. float noise_mean, speech_mean;
  326. float tmp1, tmp2, pr;
  327. /*noise_var = 1.01*st->noise_bands2[i] - st->noise_bands[i]*st->noise_bands[i];
  328. speech_var = 1.01*st->speech_bands2[i] - st->speech_bands[i]*st->speech_bands[i];*/
  329. noise_var = st->noise_bands2[i];
  330. speech_var = st->speech_bands2[i];
  331. if (noise_var < .1f)
  332. noise_var = .1f;
  333. if (speech_var < .1f)
  334. speech_var = .1f;
  335. /*speech_var = sqrt(speech_var*noise_var);
  336. noise_var = speech_var;*/
  337. if (speech_var < .05f*speech_var)
  338. noise_var = .05f*speech_var;
  339. if (speech_var < .05f*noise_var)
  340. speech_var = .05f*noise_var;
  341. if (bands[i] < st->noise_bands[i])
  342. speech_var = noise_var;
  343. if (bands[i] > st->speech_bands[i])
  344. noise_var = speech_var;
  345. speech_mean = st->speech_bands[i];
  346. noise_mean = st->noise_bands[i];
  347. if (noise_mean < speech_mean - 5.f)
  348. noise_mean = speech_mean - 5.f;
  349. tmp1 = exp(-.5f*(bands[i]-speech_mean)*(bands[i]-speech_mean)/speech_var)/sqrt(2.f*M_PI*speech_var);
  350. tmp2 = exp(-.5f*(bands[i]-noise_mean)*(bands[i]-noise_mean)/noise_var)/sqrt(2.f*M_PI*noise_var);
  351. /*fprintf (stderr, "%f ", (float)(p0/(.01+p0+p1)));*/
  352. /*fprintf (stderr, "%f ", (float)(bands[i]));*/
  353. pr = tmp1/(1e-25+tmp1+tmp2);
  354. /*if (bands[i] < st->noise_bands[i])
  355. pr=.01;
  356. if (bands[i] > st->speech_bands[i] && pr < .995)
  357. pr=.995;*/
  358. if (pr>.999f)
  359. pr=.999f;
  360. if (pr<.001f)
  361. pr=.001f;
  362. /*fprintf (stderr, "%f ", pr);*/
  363. p0 *= pr;
  364. p1 *= (1-pr);
  365. }
  366. p0 = pow(p0,.2);
  367. p1 = pow(p1,.2);
  368. #if 1
  369. p0 *= 2.f;
  370. p0=p0/(p1+p0);
  371. if (st->last_speech>20)
  372. {
  373. float tmp = sqrt(tot_loudness)/st->loudness2;
  374. tmp = 1.f-exp(-10.f*tmp);
  375. if (p0>tmp)
  376. p0=tmp;
  377. }
  378. p1=1-p0;
  379. #else
  380. if (sqrt(tot_loudness) < .6f*st->loudness2 && p0>15.f*p1)
  381. p0=15.f*p1;
  382. if (sqrt(tot_loudness) < .45f*st->loudness2 && p0>7.f*p1)
  383. p0=7.f*p1;
  384. if (sqrt(tot_loudness) < .3f*st->loudness2 && p0>3.f*p1)
  385. p0=3.f*p1;
  386. if (sqrt(tot_loudness) < .15f*st->loudness2 && p0>p1)
  387. p0=p1;
  388. /*fprintf (stderr, "%f %f ", (float)(sqrt(tot_loudness) /( .25*st->loudness2)), p0/(p1+p0));*/
  389. #endif
  390. p0 *= .99f*st->speech_prob + .01f*(1-st->speech_prob);
  391. p1 *= .01f*st->speech_prob + .99f*(1-st->speech_prob);
  392. st->speech_prob = p0/(1e-25f+p1+p0);
  393. /*fprintf (stderr, "%f %f %f ", tot_loudness, st->loudness2, st->speech_prob);*/
  394. if (st->speech_prob > st->speech_prob_start
  395. || (st->last_speech < 20 && st->speech_prob > st->speech_prob_continue))
  396. {
  397. is_speech = 1;
  398. st->last_speech = 0;
  399. } else {
  400. st->last_speech++;
  401. if (st->last_speech<20)
  402. is_speech = 1;
  403. }
  404. if (st->noise_bandsN > 50 && st->speech_bandsN > 50)
  405. {
  406. if (mean_post > 5)
  407. {
  408. float adapt = 1./st->speech_bandsN++;
  409. if (adapt<.005f)
  410. adapt = .005f;
  411. for (i=0;i<NB_BANDS;i++)
  412. {
  413. st->speech_bands[i] = (1-adapt)*st->speech_bands[i] + adapt*bands[i];
  414. /*st->speech_bands2[i] = (1-adapt)*st->speech_bands2[i] + adapt*bands[i]*bands[i];*/
  415. st->speech_bands2[i] = (1-adapt)*st->speech_bands2[i] + adapt*(bands[i]-st->speech_bands[i])*(bands[i]-st->speech_bands[i]);
  416. }
  417. } else {
  418. float adapt = 1./st->noise_bandsN++;
  419. if (adapt<.005f)
  420. adapt = .005f;
  421. for (i=0;i<NB_BANDS;i++)
  422. {
  423. st->noise_bands[i] = (1-adapt)*st->noise_bands[i] + adapt*bands[i];
  424. /*st->noise_bands2[i] = (1-adapt)*st->noise_bands2[i] + adapt*bands[i]*bands[i];*/
  425. st->noise_bands2[i] = (1-adapt)*st->noise_bands2[i] + adapt*(bands[i]-st->noise_bands[i])*(bands[i]-st->noise_bands[i]);
  426. }
  427. }
  428. }
  429. }
  430. return is_speech;
  431. }
  432. static void speex_compute_agc(SpeexPreprocessState *st, float mean_prior)
  433. {
  434. int i;
  435. int N = st->ps_size;
  436. float scale=.5f/N;
  437. float agc_gain;
  438. int freq_start, freq_end;
  439. float active_bands = 0;
  440. freq_start = (int)(300.0f*2*N/st->sampling_rate);
  441. freq_end = (int)(2000.0f*2*N/st->sampling_rate);
  442. for (i=freq_start;i<freq_end;i++)
  443. {
  444. if (st->S[i] > 20.f*st->Smin[i]+1000.f)
  445. active_bands+=1;
  446. }
  447. active_bands /= (freq_end-freq_start+1);
  448. if (active_bands > .2f)
  449. {
  450. float loudness=0.f;
  451. float rate, rate2=.2f;
  452. st->nb_loudness_adapt++;
  453. rate=2.0f/(1+st->nb_loudness_adapt);
  454. if (rate < .05f)
  455. rate = .05f;
  456. if (rate < .1f && pow(loudness, LOUDNESS_EXP) > st->loudness)
  457. rate = .1f;
  458. if (rate < .2f && pow(loudness, LOUDNESS_EXP) > 3.f*st->loudness)
  459. rate = .2f;
  460. if (rate < .4f && pow(loudness, LOUDNESS_EXP) > 10.f*st->loudness)
  461. rate = .4f;
  462. for (i=2;i<N;i++)
  463. {
  464. loudness += scale*st->ps[i] * st->gain2[i] * st->gain2[i] * st->loudness_weight[i];
  465. }
  466. loudness=sqrt(loudness);
  467. /*if (loudness < 2*pow(st->loudness, 1.0/LOUDNESS_EXP) &&
  468. loudness*2 > pow(st->loudness, 1.0/LOUDNESS_EXP))*/
  469. st->loudness = (1-rate)*st->loudness + (rate)*pow(loudness, LOUDNESS_EXP);
  470. st->loudness2 = (1-rate2)*st->loudness2 + rate2*pow(st->loudness, 1.0f/LOUDNESS_EXP);
  471. loudness = pow(st->loudness, 1.0f/LOUDNESS_EXP);
  472. /*fprintf (stderr, "%f %f %f\n", loudness, st->loudness2, rate);*/
  473. }
  474. agc_gain = st->agc_level/st->loudness2;
  475. /*fprintf (stderr, "%f %f %f %f\n", active_bands, st->loudness, st->loudness2, agc_gain);*/
  476. if (agc_gain>200)
  477. agc_gain = 200;
  478. for (i=0;i<N;i++)
  479. st->gain2[i] *= agc_gain;
  480. }
  481. static void preprocess_analysis(SpeexPreprocessState *st, spx_int16_t *x)
  482. {
  483. int i;
  484. int N = st->ps_size;
  485. int N3 = 2*N - st->frame_size;
  486. int N4 = st->frame_size - N3;
  487. float *ps=st->ps;
  488. /* 'Build' input frame */
  489. for (i=0;i<N3;i++)
  490. st->frame[i]=st->inbuf[i];
  491. for (i=0;i<st->frame_size;i++)
  492. st->frame[N3+i]=x[i];
  493. /* Update inbuf */
  494. for (i=0;i<N3;i++)
  495. st->inbuf[i]=x[N4+i];
  496. /* Windowing */
  497. for (i=0;i<2*N;i++)
  498. st->frame[i] *= st->window[i];
  499. /* Perform FFT */
  500. spx_drft_forward(st->fft_lookup, st->frame);
  501. /* Power spectrum */
  502. ps[0]=1;
  503. for (i=1;i<N;i++)
  504. ps[i]=1+st->frame[2*i-1]*st->frame[2*i-1] + st->frame[2*i]*st->frame[2*i];
  505. }
  506. static void update_noise_prob(SpeexPreprocessState *st)
  507. {
  508. int i;
  509. int N = st->ps_size;
  510. for (i=1;i<N-1;i++)
  511. st->S[i] = 100.f+ .8f*st->S[i] + .05f*st->ps[i-1]+.1f*st->ps[i]+.05f*st->ps[i+1];
  512. if (st->nb_preprocess<1)
  513. {
  514. for (i=1;i<N-1;i++)
  515. st->Smin[i] = st->Stmp[i] = st->S[i]+100.f;
  516. }
  517. if (st->nb_preprocess%200==0)
  518. {
  519. for (i=1;i<N-1;i++)
  520. {
  521. st->Smin[i] = min(st->Stmp[i], st->S[i]);
  522. st->Stmp[i] = st->S[i];
  523. }
  524. } else {
  525. for (i=1;i<N-1;i++)
  526. {
  527. st->Smin[i] = min(st->Smin[i], st->S[i]);
  528. st->Stmp[i] = min(st->Stmp[i], st->S[i]);
  529. }
  530. }
  531. for (i=1;i<N-1;i++)
  532. {
  533. st->update_prob[i] *= .2f;
  534. if (st->S[i] > 2.5*st->Smin[i])
  535. st->update_prob[i] += .8f;
  536. /*fprintf (stderr, "%f ", st->S[i]/st->Smin[i]);*/
  537. /*fprintf (stderr, "%f ", st->update_prob[i]);*/
  538. }
  539. }
  540. #define NOISE_OVERCOMPENS 1.4
  541. int speex_preprocess(SpeexPreprocessState *st, spx_int16_t *x, spx_int32_t *echo)
  542. {
  543. int i;
  544. int is_speech=1;
  545. float mean_post=0;
  546. float mean_prior=0;
  547. int N = st->ps_size;
  548. int N3 = 2*N - st->frame_size;
  549. int N4 = st->frame_size - N3;
  550. float scale=.5f/N;
  551. float *ps=st->ps;
  552. float Zframe=0, Pframe;
  553. preprocess_analysis(st, x);
  554. update_noise_prob(st);
  555. st->nb_preprocess++;
  556. /* Noise estimation always updated for the 20 first times */
  557. if (st->nb_adapt<10)
  558. {
  559. update_noise(st, ps, echo);
  560. }
  561. /* Deal with residual echo if provided */
  562. if (echo)
  563. for (i=1;i<N;i++)
  564. st->echo_noise[i] = (.3f*st->echo_noise[i] + st->frame_size*st->frame_size*4.0*echo[i]);
  565. /* Compute a posteriori SNR */
  566. for (i=1;i<N;i++)
  567. {
  568. float tot_noise = 1.f+ NOISE_OVERCOMPENS*st->noise[i] + st->echo_noise[i] + st->reverb_estimate[i];
  569. st->post[i] = ps[i]/tot_noise - 1.f;
  570. if (st->post[i]>100.f)
  571. st->post[i]=100.f;
  572. /*if (st->post[i]<0)
  573. st->post[i]=0;*/
  574. mean_post+=st->post[i];
  575. }
  576. mean_post /= N;
  577. if (mean_post<0.f)
  578. mean_post=0.f;
  579. /* Special case for first frame */
  580. if (st->nb_adapt==1)
  581. for (i=1;i<N;i++)
  582. st->old_ps[i] = ps[i];
  583. /* Compute a priori SNR */
  584. {
  585. /* A priori update rate */
  586. for (i=1;i<N;i++)
  587. {
  588. float gamma = .1+.9*st->prior[i]*st->prior[i]/((1+st->prior[i])*(1+st->prior[i]));
  589. float tot_noise = 1.f+ NOISE_OVERCOMPENS*st->noise[i] + st->echo_noise[i] + st->reverb_estimate[i];
  590. /* A priori SNR update */
  591. st->prior[i] = gamma*max(0.0f,st->post[i]) +
  592. (1.f-gamma)* (.8*st->gain[i]*st->gain[i]*st->old_ps[i]/tot_noise + .2*st->prior[i]);
  593. if (st->prior[i]>100.f)
  594. st->prior[i]=100.f;
  595. mean_prior+=st->prior[i];
  596. }
  597. }
  598. mean_prior /= N;
  599. #if 0
  600. for (i=0;i<N;i++)
  601. {
  602. fprintf (stderr, "%f ", st->prior[i]);
  603. }
  604. fprintf (stderr, "\n");
  605. #endif
  606. /*fprintf (stderr, "%f %f\n", mean_prior,mean_post);*/
  607. if (st->nb_preprocess>=20)
  608. {
  609. int do_update = 0;
  610. float noise_ener=0, sig_ener=0;
  611. /* If SNR is low (both a priori and a posteriori), update the noise estimate*/
  612. /*if (mean_prior<.23 && mean_post < .5)*/
  613. if (mean_prior<.23f && mean_post < .5f)
  614. do_update = 1;
  615. for (i=1;i<N;i++)
  616. {
  617. noise_ener += st->noise[i];
  618. sig_ener += ps[i];
  619. }
  620. if (noise_ener > 3.f*sig_ener)
  621. do_update = 1;
  622. /*do_update = 0;*/
  623. if (do_update)
  624. {
  625. st->consec_noise++;
  626. } else {
  627. st->consec_noise=0;
  628. }
  629. }
  630. if (st->vad_enabled)
  631. is_speech = speex_compute_vad(st, ps, mean_prior, mean_post);
  632. if (st->consec_noise>=3)
  633. {
  634. update_noise(st, st->old_ps, echo);
  635. } else {
  636. for (i=1;i<N-1;i++)
  637. {
  638. if (st->update_prob[i]<.5f/* || st->ps[i] < st->noise[i]*/)
  639. {
  640. if (echo)
  641. st->noise[i] = .95f*st->noise[i] + .05f*max(1.0f,st->ps[i]-st->frame_size*st->frame_size*4.0*echo[i]);
  642. else
  643. st->noise[i] = .95f*st->noise[i] + .05f*st->ps[i];
  644. }
  645. }
  646. }
  647. for (i=1;i<N;i++)
  648. {
  649. st->zeta[i] = .7f*st->zeta[i] + .3f*st->prior[i];
  650. }
  651. {
  652. int freq_start = (int)(300.0f*2.f*N/st->sampling_rate);
  653. int freq_end = (int)(2000.0f*2.f*N/st->sampling_rate);
  654. for (i=freq_start;i<freq_end;i++)
  655. {
  656. Zframe += st->zeta[i];
  657. }
  658. Zframe /= (freq_end-freq_start);
  659. }
  660. st->Zlast = Zframe;
  661. Pframe = qcurve(Zframe);
  662. /*fprintf (stderr, "%f\n", Pframe);*/
  663. /* Compute gain according to the Ephraim-Malah algorithm */
  664. for (i=1;i<N;i++)
  665. {
  666. float MM;
  667. float theta;
  668. float prior_ratio;
  669. float p, q;
  670. float zeta1;
  671. float P1;
  672. prior_ratio = st->prior[i]/(1.0001f+st->prior[i]);
  673. theta = (1.f+st->post[i])*prior_ratio;
  674. if (i==1 || i==N-1)
  675. zeta1 = st->zeta[i];
  676. else
  677. zeta1 = .25f*st->zeta[i-1] + .5f*st->zeta[i] + .25f*st->zeta[i+1];
  678. P1 = qcurve (zeta1);
  679. /* FIXME: add global prob (P2) */
  680. q = 1-Pframe*P1;
  681. q = 1-P1;
  682. if (q>.95f)
  683. q=.95f;
  684. p=1.f/(1.f + (q/(1.f-q))*(1.f+st->prior[i])*exp(-theta));
  685. /*p=1;*/
  686. /* Optimal estimator for loudness domain */
  687. MM = hypergeom_gain(theta);
  688. st->gain[i] = prior_ratio * MM;
  689. /*Put some (very arbitraty) limit on the gain*/
  690. if (st->gain[i]>2.f)
  691. {
  692. st->gain[i]=2.f;
  693. }
  694. st->reverb_estimate[i] = st->reverb_decay*st->reverb_estimate[i] + st->reverb_decay*st->reverb_level*st->gain[i]*st->gain[i]*st->ps[i];
  695. if (st->denoise_enabled)
  696. {
  697. st->gain2[i] = p*p*st->gain[i];
  698. /*st->gain2[i]=(p*sqrt(st->gain[i])+.05*(1-p))*(p*sqrt(st->gain[i])+.05*(1-p));*/
  699. /*st->gain2[i] = pow(st->gain[i], p) * pow(.2f,1.f-p);*/
  700. } else {
  701. st->gain2[i]=1.f;
  702. }
  703. }
  704. st->gain2[0]=st->gain[0]=0.f;
  705. st->gain2[N-1]=st->gain[N-1]=0.f;
  706. /*
  707. for (i=30;i<N-2;i++)
  708. {
  709. st->gain[i] = st->gain2[i]*st->gain2[i] + (1-st->gain2[i])*.333*(.6*st->gain2[i-1]+st->gain2[i]+.6*st->gain2[i+1]+.4*st->gain2[i-2]+.4*st->gain2[i+2]);
  710. }
  711. for (i=30;i<N-2;i++)
  712. st->gain2[i] = st->gain[i];
  713. */
  714. if (st->agc_enabled)
  715. speex_compute_agc(st, mean_prior);
  716. #if 0
  717. if (!is_speech)
  718. {
  719. for (i=0;i<N;i++)
  720. st->gain2[i] = 0;
  721. }
  722. #if 0
  723. else {
  724. for (i=0;i<N;i++)
  725. st->gain2[i] = 1;
  726. }
  727. #endif
  728. #endif
  729. /* Apply computed gain */
  730. for (i=1;i<N;i++)
  731. {
  732. st->frame[2*i-1] *= st->gain2[i];
  733. st->frame[2*i] *= st->gain2[i];
  734. }
  735. /* Get rid of the DC and very low frequencies */
  736. st->frame[0]=0;
  737. st->frame[1]=0;
  738. st->frame[2]=0;
  739. /* Nyquist frequency is mostly useless too */
  740. st->frame[2*N-1]=0;
  741. /* Inverse FFT with 1/N scaling */
  742. spx_drft_backward(st->fft_lookup, st->frame);
  743. for (i=0;i<2*N;i++)
  744. st->frame[i] *= scale;
  745. {
  746. float max_sample=0;
  747. for (i=0;i<2*N;i++)
  748. if (fabs(st->frame[i])>max_sample)
  749. max_sample = fabs(st->frame[i]);
  750. if (max_sample>28000.f)
  751. {
  752. float damp = 28000.f/max_sample;
  753. for (i=0;i<2*N;i++)
  754. st->frame[i] *= damp;
  755. }
  756. }
  757. for (i=0;i<2*N;i++)
  758. st->frame[i] *= st->window[i];
  759. /* Perform overlap and add */
  760. for (i=0;i<N3;i++)
  761. x[i] = st->outbuf[i] + st->frame[i];
  762. for (i=0;i<N4;i++)
  763. x[N3+i] = st->frame[N3+i];
  764. /* Update outbuf */
  765. for (i=0;i<N3;i++)
  766. st->outbuf[i] = st->frame[st->frame_size+i];
  767. /* Save old power spectrum */
  768. for (i=1;i<N;i++)
  769. st->old_ps[i] = ps[i];
  770. return is_speech;
  771. }
  772. void speex_preprocess_estimate_update(SpeexPreprocessState *st, spx_int16_t *x, spx_int32_t *echo)
  773. {
  774. int i;
  775. int N = st->ps_size;
  776. int N3 = 2*N - st->frame_size;
  777. float *ps=st->ps;
  778. preprocess_analysis(st, x);
  779. update_noise_prob(st);
  780. st->nb_preprocess++;
  781. for (i=1;i<N-1;i++)
  782. {
  783. if (st->update_prob[i]<.5f || st->ps[i] < st->noise[i])
  784. {
  785. if (echo)
  786. st->noise[i] = .95f*st->noise[i] + .1f*max(1.0f,st->ps[i]-st->frame_size*st->frame_size*4.0*echo[i]);
  787. else
  788. st->noise[i] = .95f*st->noise[i] + .1f*st->ps[i];
  789. }
  790. }
  791. for (i=0;i<N3;i++)
  792. st->outbuf[i] = x[st->frame_size-N3+i]*st->window[st->frame_size+i];
  793. /* Save old power spectrum */
  794. for (i=1;i<N;i++)
  795. st->old_ps[i] = ps[i];
  796. for (i=1;i<N;i++)
  797. st->reverb_estimate[i] *= st->reverb_decay;
  798. }
  799. int speex_preprocess_ctl(SpeexPreprocessState *state, int request, void *ptr)
  800. {
  801. int i;
  802. SpeexPreprocessState *st;
  803. st=(SpeexPreprocessState*)state;
  804. switch(request)
  805. {
  806. case SPEEX_PREPROCESS_SET_DENOISE:
  807. st->denoise_enabled = (*(int*)ptr);
  808. break;
  809. case SPEEX_PREPROCESS_GET_DENOISE:
  810. (*(int*)ptr) = st->denoise_enabled;
  811. break;
  812. case SPEEX_PREPROCESS_SET_AGC:
  813. st->agc_enabled = (*(int*)ptr);
  814. break;
  815. case SPEEX_PREPROCESS_GET_AGC:
  816. (*(int*)ptr) = st->agc_enabled;
  817. break;
  818. case SPEEX_PREPROCESS_SET_AGC_LEVEL:
  819. st->agc_level = (*(float*)ptr);
  820. if (st->agc_level<1)
  821. st->agc_level=1;
  822. if (st->agc_level>32768)
  823. st->agc_level=32768;
  824. break;
  825. case SPEEX_PREPROCESS_GET_AGC_LEVEL:
  826. (*(float*)ptr) = st->agc_level;
  827. break;
  828. case SPEEX_PREPROCESS_SET_VAD:
  829. st->vad_enabled = (*(int*)ptr);
  830. break;
  831. case SPEEX_PREPROCESS_GET_VAD:
  832. (*(int*)ptr) = st->vad_enabled;
  833. break;
  834. case SPEEX_PREPROCESS_SET_DEREVERB:
  835. st->dereverb_enabled = (*(int*)ptr);
  836. for (i=0;i<st->ps_size;i++)
  837. st->reverb_estimate[i]=0;
  838. break;
  839. case SPEEX_PREPROCESS_GET_DEREVERB:
  840. (*(int*)ptr) = st->dereverb_enabled;
  841. break;
  842. case SPEEX_PREPROCESS_SET_DEREVERB_LEVEL:
  843. st->reverb_level = (*(float*)ptr);
  844. break;
  845. case SPEEX_PREPROCESS_GET_DEREVERB_LEVEL:
  846. (*(float*)ptr) = st->reverb_level;
  847. break;
  848. case SPEEX_PREPROCESS_SET_DEREVERB_DECAY:
  849. st->reverb_decay = (*(float*)ptr);
  850. break;
  851. case SPEEX_PREPROCESS_GET_DEREVERB_DECAY:
  852. (*(float*)ptr) = st->reverb_decay;
  853. break;
  854. case SPEEX_PREPROCESS_SET_PROB_START:
  855. st->speech_prob_start = (*(int*)ptr) / 100.0;
  856. if ( st->speech_prob_start > 1 || st->speech_prob_start < 0 )
  857. st->speech_prob_start = SPEEX_PROB_START_DEFAULT;
  858. break;
  859. case SPEEX_PREPROCESS_GET_PROB_START:
  860. (*(int*)ptr) = st->speech_prob_start * 100;
  861. break;
  862. case SPEEX_PREPROCESS_SET_PROB_CONTINUE:
  863. st->speech_prob_continue = (*(int*)ptr) / 100.0;
  864. if ( st->speech_prob_continue > 1 || st->speech_prob_continue < 0 )
  865. st->speech_prob_continue = SPEEX_PROB_CONTINUE_DEFAULT;
  866. break;
  867. case SPEEX_PREPROCESS_GET_PROB_CONTINUE:
  868. (*(int*)ptr) = st->speech_prob_continue * 100;
  869. break;
  870. default:
  871. speex_warning_int("Unknown speex_preprocess_ctl request: ", request);
  872. return -1;
  873. }
  874. return 0;
  875. }
粤ICP备19079148号