psy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: psychoacoustics not including preecho
  14. last mod: $Id: psy.c,v 1.26.2.3 2000/09/02 05:19:25 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include "vorbis/codec.h"
  20. #include "masking.h"
  21. #include "psy.h"
  22. #include "os.h"
  23. #include "lpc.h"
  24. #include "smallft.h"
  25. #include "scales.h"
  26. #include "misc.h"
  27. /* Why Bark scale for encoding but not masking computation? Because
  28. masking has a strong harmonic dependancy */
  29. /* the beginnings of real psychoacoustic infrastructure. This is
  30. still not tightly tuned */
  31. void _vi_psy_free(vorbis_info_psy *i){
  32. if(i){
  33. memset(i,0,sizeof(vorbis_info_psy));
  34. free(i);
  35. }
  36. }
  37. /* Set up decibel threshhold slopes on a Bark frequency scale */
  38. /* ATH is the only bit left on a Bark scale. No reason to change it
  39. right now */
  40. static void set_curve(float *ref,float *c,int n, float crate){
  41. int i,j=0;
  42. for(i=0;i<MAX_BARK-1;i++){
  43. int endpos=rint(fromBARK(i+1)*2*n/crate);
  44. float base=ref[i];
  45. if(j<endpos){
  46. float delta=(ref[i+1]-base)/(endpos-j);
  47. for(;j<endpos && j<n;j++){
  48. c[j]=base;
  49. base+=delta;
  50. }
  51. }
  52. }
  53. }
  54. static void min_curve(float *c,
  55. float *c2){
  56. int i;
  57. for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
  58. }
  59. static void max_curve(float *c,
  60. float *c2){
  61. int i;
  62. for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
  63. }
  64. static void attenuate_curve(float *c,float att){
  65. int i;
  66. for(i=0;i<EHMER_MAX;i++)
  67. c[i]+=att;
  68. }
  69. static void linear_curve(float *c){
  70. int i;
  71. for(i=0;i<EHMER_MAX;i++)
  72. if(c[i]<=-200.)
  73. c[i]=0.;
  74. else
  75. c[i]=fromdB(c[i]);
  76. }
  77. static void interp_curve(float *c,float *c1,float *c2,float del){
  78. int i;
  79. for(i=0;i<EHMER_MAX;i++)
  80. c[i]=c2[i]*del+c1[i]*(1.-del);
  81. }
  82. static void setup_curve(float **c,
  83. int band,
  84. float *curveatt_dB){
  85. int i,j;
  86. float ath[EHMER_MAX];
  87. float tempc[P_LEVELS][EHMER_MAX];
  88. memcpy(c[0],c[4],sizeof(float)*EHMER_MAX);
  89. memcpy(c[2],c[4],sizeof(float)*EHMER_MAX);
  90. /* we add back in the ATH to avoid low level curves falling off to
  91. -infinity and unneccessarily cutting off high level curves in the
  92. curve limiting (last step). But again, remember... a half-band's
  93. settings must be valid over the whole band, and it's better to
  94. mask too little than too much, so be pessimal. */
  95. for(i=0;i<EHMER_MAX;i++){
  96. float oc_min=band*.5-1+(i-EHMER_OFFSET)*.125;
  97. float oc_max=band*.5-1+(i-EHMER_OFFSET+1)*.125;
  98. float bark=toBARK(fromOC(oc_min));
  99. int ibark=floor(bark);
  100. float del=bark-ibark;
  101. float ath_min,ath_max;
  102. if(ibark<26)
  103. ath_min=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  104. else
  105. ath_min=200.;
  106. bark=toBARK(fromOC(oc_max));
  107. ibark=floor(bark);
  108. del=bark-ibark;
  109. if(ibark<26)
  110. ath_max=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
  111. else
  112. ath_max=200.;
  113. ath[i]=min(ath_min,ath_max);
  114. }
  115. /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
  116. interpolate intermediate dB curves */
  117. for(i=1;i<P_LEVELS;i+=2){
  118. interp_curve(c[i],c[i-1],c[i+1],.5);
  119. }
  120. /* normalize curves so the driving amplitude is 0dB */
  121. /* make temp curves with the ATH overlayed */
  122. for(i=0;i<P_LEVELS;i++){
  123. attenuate_curve(c[i],curveatt_dB[i]);
  124. memcpy(tempc[i],ath,EHMER_MAX*sizeof(float));
  125. attenuate_curve(tempc[i],-i*10.);
  126. max_curve(tempc[i],c[i]);
  127. }
  128. /* Now limit the louder curves.
  129. the idea is this: We don't know what the playback attenuation
  130. will be; 0dB SL moves every time the user twiddles the volume
  131. knob. So that means we have to use a single 'most pessimal' curve
  132. for all masking amplitudes, right? Wrong. The *loudest* sound
  133. can be in (we assume) a range of ...+100dB] SL. However, sounds
  134. 20dB down will be in a range ...+80], 40dB down is from ...+60],
  135. etc... */
  136. for(i=P_LEVELS-1;i>0;i--){
  137. for(j=0;j<i;j++)
  138. min_curve(c[i],tempc[j]);
  139. }
  140. /* take things out of dB domain into linear amplitude */
  141. for(i=0;i<P_LEVELS;i++)
  142. linear_curve(c[i]);
  143. }
  144. void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
  145. long i,j;
  146. memset(p,0,sizeof(vorbis_look_psy));
  147. p->ath=malloc(n*sizeof(float));
  148. p->octave=malloc(n*sizeof(int));
  149. p->bark=malloc(n*sizeof(float));
  150. p->vi=vi;
  151. p->n=n;
  152. /* set up the lookups for a given blocksize and sample rate */
  153. /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
  154. set_curve(ATH_Bark_dB, p->ath,n,rate);
  155. for(i=0;i<n;i++)
  156. p->ath[i]=fromdB(p->ath[i]);
  157. for(i=0;i<n;i++)
  158. p->bark[i]=toBARK(rate/(2*n)*i);
  159. for(i=0;i<n;i++){
  160. int oc=toOC((i+.5)*rate/(2*n))*2.+2; /* half octaves, actually */
  161. if(oc<0)oc=0;
  162. if(oc>=P_BANDS)oc=P_BANDS-1;
  163. p->octave[i]=oc;
  164. }
  165. p->tonecurves=malloc(P_BANDS*sizeof(float **));
  166. p->noiseatt=malloc(P_BANDS*sizeof(float **));
  167. p->peakatt=malloc(P_BANDS*sizeof(float *));
  168. for(i=0;i<P_BANDS;i++){
  169. p->tonecurves[i]=malloc(P_LEVELS*sizeof(float *));
  170. p->noiseatt[i]=malloc(P_LEVELS*sizeof(float));
  171. p->peakatt[i]=malloc(P_LEVELS*sizeof(float));
  172. }
  173. for(i=0;i<P_BANDS;i++)
  174. for(j=0;j<P_LEVELS;j++){
  175. p->tonecurves[i][j]=malloc(EHMER_MAX*sizeof(float));
  176. }
  177. /* OK, yeah, this was a silly way to do it */
  178. memcpy(p->tonecurves[0][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  179. memcpy(p->tonecurves[0][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  180. memcpy(p->tonecurves[0][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  181. memcpy(p->tonecurves[0][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  182. memcpy(p->tonecurves[2][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
  183. memcpy(p->tonecurves[2][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
  184. memcpy(p->tonecurves[2][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
  185. memcpy(p->tonecurves[2][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
  186. memcpy(p->tonecurves[4][4],tone_250_40dB_SL,sizeof(float)*EHMER_MAX);
  187. memcpy(p->tonecurves[4][6],tone_250_60dB_SL,sizeof(float)*EHMER_MAX);
  188. memcpy(p->tonecurves[4][8],tone_250_80dB_SL,sizeof(float)*EHMER_MAX);
  189. memcpy(p->tonecurves[4][10],tone_250_100dB_SL,sizeof(float)*EHMER_MAX);
  190. memcpy(p->tonecurves[6][4],tone_500_40dB_SL,sizeof(float)*EHMER_MAX);
  191. memcpy(p->tonecurves[6][6],tone_500_60dB_SL,sizeof(float)*EHMER_MAX);
  192. memcpy(p->tonecurves[6][8],tone_500_80dB_SL,sizeof(float)*EHMER_MAX);
  193. memcpy(p->tonecurves[6][10],tone_500_100dB_SL,sizeof(float)*EHMER_MAX);
  194. memcpy(p->tonecurves[8][4],tone_1000_40dB_SL,sizeof(float)*EHMER_MAX);
  195. memcpy(p->tonecurves[8][6],tone_1000_60dB_SL,sizeof(float)*EHMER_MAX);
  196. memcpy(p->tonecurves[8][8],tone_1000_80dB_SL,sizeof(float)*EHMER_MAX);
  197. memcpy(p->tonecurves[8][10],tone_1000_100dB_SL,sizeof(float)*EHMER_MAX);
  198. memcpy(p->tonecurves[10][4],tone_2000_40dB_SL,sizeof(float)*EHMER_MAX);
  199. memcpy(p->tonecurves[10][6],tone_2000_60dB_SL,sizeof(float)*EHMER_MAX);
  200. memcpy(p->tonecurves[10][8],tone_2000_80dB_SL,sizeof(float)*EHMER_MAX);
  201. memcpy(p->tonecurves[10][10],tone_2000_100dB_SL,sizeof(float)*EHMER_MAX);
  202. memcpy(p->tonecurves[12][4],tone_4000_40dB_SL,sizeof(float)*EHMER_MAX);
  203. memcpy(p->tonecurves[12][6],tone_4000_60dB_SL,sizeof(float)*EHMER_MAX);
  204. memcpy(p->tonecurves[12][8],tone_4000_80dB_SL,sizeof(float)*EHMER_MAX);
  205. memcpy(p->tonecurves[12][10],tone_4000_100dB_SL,sizeof(float)*EHMER_MAX);
  206. memcpy(p->tonecurves[14][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  207. memcpy(p->tonecurves[14][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  208. memcpy(p->tonecurves[14][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  209. memcpy(p->tonecurves[14][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  210. memcpy(p->tonecurves[16][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
  211. memcpy(p->tonecurves[16][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
  212. memcpy(p->tonecurves[16][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
  213. memcpy(p->tonecurves[16][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
  214. /* interpolate curves between */
  215. for(i=1;i<P_BANDS;i+=2)
  216. for(j=4;j<P_LEVELS;j+=2){
  217. memcpy(p->tonecurves[i][j],p->tonecurves[i-1][j],EHMER_MAX*sizeof(float));
  218. /*interp_curve(p->tonecurves[i][j],
  219. p->tonecurves[i-1][j],
  220. p->tonecurves[i+1][j],.5);*/
  221. min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);
  222. /*min_curve(p->tonecurves[i][j],p->tonecurves[i-1][j]);*/
  223. }
  224. /*for(i=0;i<P_BANDS-1;i++)
  225. for(j=4;j<P_LEVELS;j+=2)
  226. min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);*/
  227. /* set up the final curves */
  228. for(i=0;i<P_BANDS;i++)
  229. setup_curve(p->tonecurves[i],i,vi->toneatt[i]);
  230. /* set up attenuation levels */
  231. for(i=0;i<P_BANDS;i++)
  232. for(j=0;j<P_LEVELS;j++){
  233. p->peakatt[i][j]=fromdB(p->vi->peakatt[i][j]);
  234. p->noiseatt[i][j]=fromdB(p->vi->noiseatt[i][j]);
  235. }
  236. }
  237. void _vp_psy_clear(vorbis_look_psy *p){
  238. int i,j;
  239. if(p){
  240. if(p->ath)free(p->ath);
  241. if(p->octave)free(p->octave);
  242. if(p->tonecurves){
  243. for(i=0;i<P_BANDS;i++){
  244. for(j=0;j<P_LEVELS;j++){
  245. free(p->tonecurves[i][j]);
  246. }
  247. free(p->noiseatt[i]);
  248. free(p->tonecurves[i]);
  249. free(p->peakatt[i]);
  250. }
  251. free(p->tonecurves);
  252. free(p->noiseatt);
  253. free(p->peakatt);
  254. }
  255. memset(p,0,sizeof(vorbis_look_psy));
  256. }
  257. }
  258. static void compute_decay_fixed(vorbis_look_psy *p,float *f, float *decay, int n){
  259. /* handle decay */
  260. int i;
  261. float decscale=fromdB(p->vi->decay_coeff*n);
  262. float attscale=1./fromdB(p->vi->attack_coeff);
  263. for(i=10;i<n;i++){
  264. float pre=decay[i];
  265. if(decay[i]){
  266. float val=decay[i]*decscale;
  267. float att=fabs(f[i]/val);
  268. if(att>attscale)
  269. decay[i]=fabs(f[i]/attscale);
  270. else
  271. decay[i]=val;
  272. }else{
  273. decay[i]=fabs(f[i]/attscale);
  274. }
  275. if(pre>f[i])f[i]=pre;
  276. }
  277. }
  278. static long _eights[EHMER_MAX+1]={
  279. 981,1069,1166,1272,
  280. 1387,1512,1649,1798,
  281. 1961,2139,2332,2543,
  282. 2774,3025,3298,3597,
  283. 3922,4277,4664,5087,
  284. 5547,6049,6597,7194,
  285. 7845,8555,9329,10173,
  286. 11094,12098,13193,14387,
  287. 15689,17109,18658,20347,
  288. 22188,24196,26386,28774,
  289. 31379,34219,37316,40693,
  290. 44376,48393,52772,57549,
  291. 62757,68437,74631,81386,
  292. 88752,96785,105545,115097,
  293. 125515};
  294. static int seed_curve(float *flr,
  295. float **curves,
  296. float amp,float specmax,
  297. int x,int n,float specatt,
  298. int maxEH){
  299. int i;
  300. float *curve;
  301. /* make this attenuation adjustable */
  302. int choice=(int)((todB(amp)-specmax+specatt)/10.+.5);
  303. choice=max(choice,0);
  304. choice=min(choice,P_LEVELS-1);
  305. for(i=maxEH;i>=0;i--)
  306. if(((x*_eights[i])>>12)<n)break;
  307. maxEH=i;
  308. curve=curves[choice];
  309. for(;i>=0;i--)
  310. if(curve[i]>0.)break;
  311. for(;i>=0;i--){
  312. float lin=curve[i];
  313. if(lin>0.){
  314. float *fp=flr+((x*_eights[i])>>12);
  315. lin*=amp;
  316. if(*fp<lin)*fp=lin;
  317. }else break;
  318. }
  319. return(maxEH);
  320. }
  321. static void seed_peak(float *flr,
  322. float *att,
  323. float amp,float specmax,
  324. int x,int n,float specatt){
  325. int prevx=(x*_eights[16])>>12;
  326. /* make this attenuation adjustable */
  327. int choice=rint((todB(amp)-specmax+specatt)/10.+.5);
  328. if(choice<0)choice=0;
  329. if(choice>=P_LEVELS)choice=P_LEVELS-1;
  330. if(prevx<n){
  331. float lin=att[choice];
  332. if(lin){
  333. lin*=amp;
  334. if(flr[prevx]<lin)flr[prevx]=lin;
  335. }
  336. }
  337. }
  338. static void seed_generic(vorbis_look_psy *p,
  339. float ***curves,
  340. float *f,
  341. float *flr,
  342. float *seeds,
  343. float specmax){
  344. vorbis_info_psy *vi=p->vi;
  345. long n=p->n,i;
  346. int maxEH=EHMER_MAX-1;
  347. /* prime the working vector with peak values */
  348. /* Use the 125 Hz curve up to 125 Hz and 8kHz curve after 8kHz. */
  349. for(i=0;i<n;i++)
  350. if(f[i]>flr[i])
  351. maxEH=seed_curve(seeds,curves[p->octave[i]],
  352. f[i],specmax,i,n,vi->max_curve_dB,maxEH);
  353. }
  354. static void seed_att(vorbis_look_psy *p,
  355. float **att,
  356. float *f,
  357. float *flr,
  358. float specmax){
  359. vorbis_info_psy *vi=p->vi;
  360. long n=p->n,i;
  361. for(i=0;i<n;i++)
  362. if(f[i]>flr[i])
  363. seed_peak(flr,att[p->octave[i]],f[i],
  364. specmax,i,n,vi->max_curve_dB);
  365. }
  366. static void seed_point(vorbis_look_psy *p,
  367. float **att,
  368. float *f,
  369. float *flr,
  370. float specmax){
  371. vorbis_info_psy *vi=p->vi;
  372. long n=p->n,i;
  373. for(i=0;i<n;i++){
  374. /* make this attenuation adjustable */
  375. int choice=rint((todB(f[i])-specmax+vi->max_curve_dB)/10.+.5);
  376. float lin;
  377. if(choice<0)choice=0;
  378. if(choice>=P_LEVELS)choice=P_LEVELS-1;
  379. lin=att[p->octave[i]][choice]*f[i];
  380. if(flr[i]<lin)flr[i]=lin;
  381. }
  382. }
  383. /* bleaugh, this is more complicated than it needs to be */
  384. static void max_seeds(vorbis_look_psy *p,float *seeds,float *flr){
  385. long n=p->n,i,j;
  386. long *posstack=alloca(n*sizeof(long));
  387. float *ampstack=alloca(n*sizeof(float));
  388. long stack=0;
  389. for(i=0;i<n;i++){
  390. if(stack<2){
  391. posstack[stack]=i;
  392. ampstack[stack++]=seeds[i];
  393. }else{
  394. while(1){
  395. if(seeds[i]<ampstack[stack-1]){
  396. posstack[stack]=i;
  397. ampstack[stack++]=seeds[i];
  398. break;
  399. }else{
  400. if(i<posstack[stack-1]*1.0905077080){
  401. if(stack>1 && ampstack[stack-1]<ampstack[stack-2] &&
  402. i<posstack[stack-2]*1.0905077080){
  403. /* we completely overlap, making stack-1 irrelevant. pop it */
  404. stack--;
  405. continue;
  406. }
  407. }
  408. posstack[stack]=i;
  409. ampstack[stack++]=seeds[i];
  410. break;
  411. }
  412. }
  413. }
  414. }
  415. /* the stack now contains only the positions that are relevant. Scan
  416. 'em straight through */
  417. {
  418. long pos=0;
  419. for(i=0;i<stack;i++){
  420. long endpos;
  421. if(i<stack-1 && ampstack[i+1]>ampstack[i]){
  422. endpos=posstack[i+1];
  423. }else{
  424. endpos=posstack[i]*1.0905077080+1; /* +1 is important, else bin 0 is
  425. discarded in short frames */
  426. }
  427. if(endpos>n)endpos=n;
  428. for(j=pos;j<endpos;j++)
  429. if(flr[j]<ampstack[i])
  430. flr[j]=ampstack[i];
  431. pos=endpos;
  432. }
  433. }
  434. /* there. Linear time. I now remember this was on a problem set I
  435. had in Grad Skool... I didn't solve it at the time ;-) */
  436. }
  437. static void bark_noise(long n,float *b,float *f,float *noise){
  438. long i=1,lo=0,hi=2;
  439. float acc=0.,val,del=0.;
  440. float *norm=alloca(n*sizeof(float));
  441. memset(noise,0,n*sizeof(float));
  442. memset(norm,0,n*sizeof(float));
  443. while(hi<n){
  444. val=todB_nn(f[i]*f[i])+400.;
  445. del=1./(i-lo);
  446. noise[lo]+=val*del;
  447. noise[i]-=val*del;
  448. norm[lo]+=del;
  449. norm[i]-=del;
  450. del=1./(hi-i);
  451. noise[i]-=val*del;
  452. noise[hi]+=val*del;
  453. norm[hi]+=del;
  454. norm[i]-=del;
  455. i++;
  456. for(;hi<n && b[hi]-.3<b[i];hi++);
  457. for(;lo<i-1 && b[lo]+.3<b[i];lo++);
  458. if(i==hi)hi++;
  459. }
  460. {
  461. long ilo=i-lo;
  462. long hii=hi-i;
  463. for(;i<n;i++){
  464. val=todB_nn(f[i]*f[i])+400.;
  465. del=1./(hii);
  466. noise[i]-=val*del;
  467. norm[i]-=del;
  468. del=1./(ilo);
  469. noise[i-ilo]+=val*del;
  470. noise[i]-=val*del;
  471. norm[i-ilo]+=del;
  472. norm[i]-=del;
  473. }
  474. for(i=1,lo=n-ilo;lo<n;lo++,i++){
  475. val=todB_nn(f[n-i]*f[n-i])+400.;
  476. del=1./ilo;
  477. noise[lo]+=val*del;
  478. norm[lo]+=del;
  479. }
  480. }
  481. acc=0;
  482. val=0;
  483. for(i=0;i<n;i++){
  484. val+=norm[i];
  485. norm[i]=val;
  486. acc+=noise[i];
  487. noise[i]=acc;
  488. }
  489. val=0;
  490. acc=0;
  491. for(i=0;i<n;i++){
  492. val+=norm[i];
  493. acc+=noise[i];
  494. if(val==0){
  495. noise[i]=0.;
  496. norm[i]=0;
  497. }else{
  498. float v=acc/val-400;
  499. noise[i]=sqrt(fromdB(v));
  500. }
  501. }
  502. }
  503. void _vp_compute_mask(vorbis_look_psy *p,float *f,
  504. float *flr,
  505. float *decay){
  506. float *smooth=alloca(sizeof(float)*p->n);
  507. int i,n=p->n;
  508. float specmax=0.;
  509. float *seed=alloca(sizeof(float)*p->n);
  510. float *seed2=alloca(sizeof(float)*p->n);
  511. memset(flr,0,n*sizeof(float));
  512. /* noise masking */
  513. if(p->vi->noisemaskp){
  514. memset(seed,0,n*sizeof(float));
  515. bark_noise(n,p->bark,f,seed);
  516. seed_point(p,p->noiseatt,seed,flr,specmax);
  517. }
  518. /* smooth the data is that's called for ********************************/
  519. for(i=0;i<n;i++)smooth[i]=fabs(f[i]);
  520. if(p->vi->smoothp){
  521. /* compute power^.5 of three neighboring bins to smooth for peaks
  522. that get split twixt bins/peaks that nail the bin. This evens
  523. out treatment as we're not doing additive masking any longer. */
  524. float acc=smooth[0]*smooth[0]+smooth[1]*smooth[1];
  525. float prev=smooth[0];
  526. smooth[0]=sqrt(acc);
  527. for(i=1;i<n-1;i++){
  528. float this=smooth[i];
  529. acc+=smooth[i+1]*smooth[i+1];
  530. if(acc<0)acc=0; /* it can happen due to finite precision */
  531. smooth[i]=sqrt(acc);
  532. acc-=prev*prev;
  533. prev=this;
  534. }
  535. smooth[n-1]=sqrt(acc);
  536. }
  537. /* find the highest peak so we know the limits *************************/
  538. for(i=0;i<n;i++){
  539. if(smooth[i]>specmax)specmax=smooth[i];
  540. }
  541. specmax=todB(specmax);
  542. /* set the ATH (floating below specmax by a specified att) */
  543. if(p->vi->athp){
  544. float att=specmax+p->vi->ath_adjatt;
  545. if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
  546. att=fromdB(att);
  547. for(i=0;i<n;i++){
  548. float av=p->ath[i]*att;
  549. if(av>flr[i])flr[i]=av;
  550. }
  551. }
  552. /* peak attenuation ******/
  553. if(p->vi->peakattp){
  554. memset(seed,0,n*sizeof(float));
  555. seed_att(p,p->peakatt,smooth,seed,specmax);
  556. max_seeds(p,seed,flr);
  557. }
  558. /* tone masking */
  559. if(p->vi->tonemaskp){
  560. memset(seed,0,n*sizeof(float));
  561. memset(seed2,0,n*sizeof(float));
  562. seed_generic(p,p->tonecurves,smooth,flr,seed2,specmax);
  563. max_seeds(p,seed2,seed2);
  564. for(i=0;i<n;i++)if(seed2[i]<flr[i])seed2[i]=flr[i];
  565. for(i=0;i<n;i++)if(seed2[i]<decay[i])seed2[i]=decay[i];
  566. seed_generic(p,p->tonecurves,smooth,seed2,seed,specmax);
  567. max_seeds(p,seed,seed);
  568. if(p->vi->decayp)
  569. compute_decay_fixed(p,seed,decay,n);
  570. for(i=0;i<n;i++)if(flr[i]<seed[i])flr[i]=seed[i];
  571. }
  572. /* doing this here is clean, but we need to find a faster way to do
  573. it than to just tack it on */
  574. for(i=0;i<n;i++)if(2.*f[i]>flr[i] || -2.*f[i]>flr[i])break;
  575. if(i==n)memset(flr,0,sizeof(float)*n);
  576. }
  577. /* this applies the floor and (optionally) tries to preserve noise
  578. energy in low resolution portions of the spectrum */
  579. /* f and flr are *linear* scale, not dB */
  580. void _vp_apply_floor(vorbis_look_psy *p,float *f, float *flr){
  581. float *work=alloca(p->n*sizeof(float));
  582. int j;
  583. /* subtract the floor */
  584. for(j=0;j<p->n;j++){
  585. if(flr[j]<=0)
  586. work[j]=0.;
  587. else
  588. work[j]=f[j]/flr[j];
  589. }
  590. memcpy(f,work,p->n*sizeof(float));
  591. }