vorbis_psy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Copyright (C) 2005 Jean-Marc Valin, CSIRO, Christopher Montgomery
  2. File: vorbis_psy.c
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of the Xiph.org Foundation nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #ifdef VORBIS_PSYCHO
  30. #include "arch.h"
  31. #include "smallft.h"
  32. #include "lpc.h"
  33. #include "vorbis_psy.h"
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include <string.h>
  37. /* psychoacoustic setup ********************************************/
  38. static VorbisPsyInfo example_tuning = {
  39. .5,.5,
  40. 3,3,25,
  41. /*63 125 250 500 1k 2k 4k 8k 16k*/
  42. // vorbis mode 4 style
  43. //{-32,-32,-32,-32,-28,-24,-22,-20,-20, -20, -20, -8, -6, -6, -6, -6, -6},
  44. { -4, -6, -6, -6, -6, -6, -6, -6, -8, -8,-10,-10, -8, -6, -4, -4, -2},
  45. {
  46. 0, 1, 2, 3, 4, 5, 5, 5, /* 7dB */
  47. 6, 6, 6, 5, 4, 4, 4, 4, /* 15dB */
  48. 4, 4, 5, 5, 5, 6, 6, 6, /* 23dB */
  49. 7, 7, 7, 8, 8, 8, 9, 10, /* 31dB */
  50. 11,12,13,14,15,16,17, 18, /* 39dB */
  51. }
  52. };
  53. /* there was no great place to put this.... */
  54. #include <stdio.h>
  55. static void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
  56. int j;
  57. FILE *of;
  58. char buffer[80];
  59. sprintf(buffer,"%s_%d.m",base,i);
  60. of=fopen(buffer,"w");
  61. if(!of)perror("failed to open data dump file");
  62. for(j=0;j<n;j++){
  63. if(bark){
  64. float b=toBARK((4000.f*j/n)+.25);
  65. fprintf(of,"%f ",b);
  66. }else
  67. fprintf(of,"%f ",(double)j);
  68. if(dB){
  69. float val;
  70. if(v[j]==0.)
  71. val=-140.;
  72. else
  73. val=todB(v[j]);
  74. fprintf(of,"%f\n",val);
  75. }else{
  76. fprintf(of,"%f\n",v[j]);
  77. }
  78. }
  79. fclose(of);
  80. }
  81. static void bark_noise_hybridmp(int n,const long *b,
  82. const float *f,
  83. float *noise,
  84. const float offset,
  85. const int fixed){
  86. float *N=alloca(n*sizeof(*N));
  87. float *X=alloca(n*sizeof(*N));
  88. float *XX=alloca(n*sizeof(*N));
  89. float *Y=alloca(n*sizeof(*N));
  90. float *XY=alloca(n*sizeof(*N));
  91. float tN, tX, tXX, tY, tXY;
  92. int i;
  93. int lo, hi;
  94. float R, A, B, D;
  95. float w, x, y;
  96. tN = tX = tXX = tY = tXY = 0.f;
  97. y = f[0] + offset;
  98. if (y < 1.f) y = 1.f;
  99. w = y * y * .5;
  100. tN += w;
  101. tX += w;
  102. tY += w * y;
  103. N[0] = tN;
  104. X[0] = tX;
  105. XX[0] = tXX;
  106. Y[0] = tY;
  107. XY[0] = tXY;
  108. for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
  109. y = f[i] + offset;
  110. if (y < 1.f) y = 1.f;
  111. w = y * y;
  112. tN += w;
  113. tX += w * x;
  114. tXX += w * x * x;
  115. tY += w * y;
  116. tXY += w * x * y;
  117. N[i] = tN;
  118. X[i] = tX;
  119. XX[i] = tXX;
  120. Y[i] = tY;
  121. XY[i] = tXY;
  122. }
  123. for (i = 0, x = 0.f;; i++, x += 1.f) {
  124. lo = b[i] >> 16;
  125. if( lo>=0 ) break;
  126. hi = b[i] & 0xffff;
  127. tN = N[hi] + N[-lo];
  128. tX = X[hi] - X[-lo];
  129. tXX = XX[hi] + XX[-lo];
  130. tY = Y[hi] + Y[-lo];
  131. tXY = XY[hi] - XY[-lo];
  132. A = tY * tXX - tX * tXY;
  133. B = tN * tXY - tX * tY;
  134. D = tN * tXX - tX * tX;
  135. R = (A + x * B) / D;
  136. if (R < 0.f)
  137. R = 0.f;
  138. noise[i] = R - offset;
  139. }
  140. for ( ;; i++, x += 1.f) {
  141. lo = b[i] >> 16;
  142. hi = b[i] & 0xffff;
  143. if(hi>=n)break;
  144. tN = N[hi] - N[lo];
  145. tX = X[hi] - X[lo];
  146. tXX = XX[hi] - XX[lo];
  147. tY = Y[hi] - Y[lo];
  148. tXY = XY[hi] - XY[lo];
  149. A = tY * tXX - tX * tXY;
  150. B = tN * tXY - tX * tY;
  151. D = tN * tXX - tX * tX;
  152. R = (A + x * B) / D;
  153. if (R < 0.f) R = 0.f;
  154. noise[i] = R - offset;
  155. }
  156. for ( ; i < n; i++, x += 1.f) {
  157. R = (A + x * B) / D;
  158. if (R < 0.f) R = 0.f;
  159. noise[i] = R - offset;
  160. }
  161. if (fixed <= 0) return;
  162. for (i = 0, x = 0.f;; i++, x += 1.f) {
  163. hi = i + fixed / 2;
  164. lo = hi - fixed;
  165. if(lo>=0)break;
  166. tN = N[hi] + N[-lo];
  167. tX = X[hi] - X[-lo];
  168. tXX = XX[hi] + XX[-lo];
  169. tY = Y[hi] + Y[-lo];
  170. tXY = XY[hi] - XY[-lo];
  171. A = tY * tXX - tX * tXY;
  172. B = tN * tXY - tX * tY;
  173. D = tN * tXX - tX * tX;
  174. R = (A + x * B) / D;
  175. if (R - offset < noise[i]) noise[i] = R - offset;
  176. }
  177. for ( ;; i++, x += 1.f) {
  178. hi = i + fixed / 2;
  179. lo = hi - fixed;
  180. if(hi>=n)break;
  181. tN = N[hi] - N[lo];
  182. tX = X[hi] - X[lo];
  183. tXX = XX[hi] - XX[lo];
  184. tY = Y[hi] - Y[lo];
  185. tXY = XY[hi] - XY[lo];
  186. A = tY * tXX - tX * tXY;
  187. B = tN * tXY - tX * tY;
  188. D = tN * tXX - tX * tX;
  189. R = (A + x * B) / D;
  190. if (R - offset < noise[i]) noise[i] = R - offset;
  191. }
  192. for ( ; i < n; i++, x += 1.f) {
  193. R = (A + x * B) / D;
  194. if (R - offset < noise[i]) noise[i] = R - offset;
  195. }
  196. }
  197. static void _vp_noisemask(VorbisPsy *p,
  198. float *logfreq,
  199. float *logmask){
  200. int i,n=p->n/2;
  201. float *work=alloca(n*sizeof(*work));
  202. bark_noise_hybridmp(n,p->bark,logfreq,logmask,
  203. 140.,-1);
  204. for(i=0;i<n;i++)work[i]=logfreq[i]-logmask[i];
  205. bark_noise_hybridmp(n,p->bark,work,logmask,0.,
  206. p->vi->noisewindowfixed);
  207. for(i=0;i<n;i++)work[i]=logfreq[i]-work[i];
  208. {
  209. static int seq=0;
  210. float work2[n];
  211. for(i=0;i<n;i++){
  212. work2[i]=logmask[i]+work[i];
  213. }
  214. //_analysis_output("logfreq",seq,logfreq,n,0,0);
  215. //_analysis_output("median",seq,work,n,0,0);
  216. //_analysis_output("envelope",seq,work2,n,0,0);
  217. seq++;
  218. }
  219. for(i=0;i<n;i++){
  220. int dB=logmask[i]+.5;
  221. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  222. if(dB<0)dB=0;
  223. logmask[i]= work[i]+p->vi->noisecompand[dB]+p->noiseoffset[i];
  224. }
  225. }
  226. VorbisPsy *vorbis_psy_init(int rate, int n)
  227. {
  228. long i,j,lo=-99,hi=1;
  229. VorbisPsy *p = speex_alloc(sizeof(VorbisPsy));
  230. memset(p,0,sizeof(*p));
  231. p->n = n;
  232. spx_drft_init(&p->lookup, n);
  233. p->bark = speex_alloc(n*sizeof(*p->bark));
  234. p->rate=rate;
  235. p->vi = &example_tuning;
  236. /* BH4 window */
  237. p->window = speex_alloc(sizeof(*p->window)*n);
  238. float a0 = .35875f;
  239. float a1 = .48829f;
  240. float a2 = .14128f;
  241. float a3 = .01168f;
  242. for(i=0;i<n;i++)
  243. p->window[i] = //a0 - a1*cos(2.*M_PI/n*(i+.5)) + a2*cos(4.*M_PI/n*(i+.5)) - a3*cos(6.*M_PI/n*(i+.5));
  244. sin((i+.5)/n * M_PI)*sin((i+.5)/n * M_PI);
  245. /* bark scale lookups */
  246. for(i=0;i<n;i++){
  247. float bark=toBARK(rate/(2*n)*i);
  248. for(;lo+p->vi->noisewindowlomin<i &&
  249. toBARK(rate/(2*n)*lo)<(bark-p->vi->noisewindowlo);lo++);
  250. for(;hi<=n && (hi<i+p->vi->noisewindowhimin ||
  251. toBARK(rate/(2*n)*hi)<(bark+p->vi->noisewindowhi));hi++);
  252. p->bark[i]=((lo-1)<<16)+(hi-1);
  253. }
  254. /* set up rolling noise median */
  255. p->noiseoffset=speex_alloc(n*sizeof(*p->noiseoffset));
  256. for(i=0;i<n;i++){
  257. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  258. int inthalfoc;
  259. float del;
  260. if(halfoc<0)halfoc=0;
  261. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  262. inthalfoc=(int)halfoc;
  263. del=halfoc-inthalfoc;
  264. p->noiseoffset[i]=
  265. p->vi->noiseoff[inthalfoc]*(1.-del) +
  266. p->vi->noiseoff[inthalfoc+1]*del;
  267. }
  268. #if 0
  269. _analysis_output_always("noiseoff0",ls,p->noiseoffset,n,1,0,0);
  270. #endif
  271. return p;
  272. }
  273. void vorbis_psy_destroy(VorbisPsy *p)
  274. {
  275. if(p){
  276. spx_drft_clear(&p->lookup);
  277. if(p->bark)
  278. speex_free(p->bark);
  279. if(p->noiseoffset)
  280. speex_free(p->noiseoffset);
  281. if(p->window)
  282. speex_free(p->window);
  283. memset(p,0,sizeof(*p));
  284. speex_free(p);
  285. }
  286. }
  287. void compute_curve(VorbisPsy *psy, float *audio, float *curve)
  288. {
  289. int i;
  290. float work[psy->n];
  291. float scale=4.f/psy->n;
  292. float scale_dB;
  293. scale_dB=todB(scale);
  294. /* window the PCM data; use a BH4 window, not vorbis */
  295. for(i=0;i<psy->n;i++)
  296. work[i]=audio[i] * psy->window[i];
  297. {
  298. static int seq=0;
  299. //_analysis_output("win",seq,work,psy->n,0,0);
  300. seq++;
  301. }
  302. /* FFT yields more accurate tonal estimation (not phase sensitive) */
  303. spx_drft_forward(&psy->lookup,work);
  304. /* magnitudes */
  305. work[0]=scale_dB+todB(work[0]);
  306. for(i=1;i<psy->n-1;i+=2){
  307. float temp = work[i]*work[i] + work[i+1]*work[i+1];
  308. work[(i+1)>>1] = scale_dB+.5f * todB(temp);
  309. }
  310. /* derive a noise curve */
  311. _vp_noisemask(psy,work,curve);
  312. #define SIDEL 12
  313. for (i=0;i<SIDEL;i++)
  314. {
  315. curve[i]=curve[SIDEL];
  316. }
  317. #define SIDEH 12
  318. for (i=0;i<SIDEH;i++)
  319. {
  320. curve[(psy->n>>1)-i-1]=curve[(psy->n>>1)-SIDEH];
  321. }
  322. for(i=0;i<((psy->n)>>1);i++)
  323. curve[i] = fromdB(1.2*curve[i]+.2*i);
  324. //curve[i] = fromdB(0.8*curve[i]+.35*i);
  325. //curve[i] = fromdB(0.9*curve[i])*pow(1.0*i+45,1.3);
  326. }
  327. /* Transform a masking curve (power spectrum) into a pole-zero filter */
  328. void curve_to_lpc(VorbisPsy *psy, float *curve, float *awk1, float *awk2, int ord)
  329. {
  330. int i;
  331. float ac[psy->n];
  332. float tmp;
  333. int len = psy->n >> 1;
  334. for (i=0;i<2*len;i++)
  335. ac[i] = 0;
  336. for (i=1;i<len;i++)
  337. ac[2*i-1] = curve[i];
  338. ac[0] = curve[0];
  339. ac[2*len-1] = curve[len-1];
  340. spx_drft_backward(&psy->lookup, ac);
  341. _spx_lpc(awk1, ac, ord);
  342. tmp = 1.;
  343. for (i=0;i<ord;i++)
  344. {
  345. tmp *= .99;
  346. awk1[i] *= tmp;
  347. }
  348. #if 0
  349. for (i=0;i<ord;i++)
  350. awk2[i] = 0;
  351. #else
  352. /* Use the second (awk2) filter to correct the first one */
  353. for (i=0;i<2*len;i++)
  354. ac[i] = 0;
  355. for (i=0;i<ord;i++)
  356. ac[i+1] = awk1[i];
  357. ac[0] = 1;
  358. spx_drft_forward(&psy->lookup, ac);
  359. /* Compute (power) response of awk1 (all zero) */
  360. ac[0] *= ac[0];
  361. for (i=1;i<len;i++)
  362. ac[i] = ac[2*i-1]*ac[2*i-1] + ac[2*i]*ac[2*i];
  363. ac[len] = ac[2*len-1]*ac[2*len-1];
  364. /* Compute correction required */
  365. for (i=0;i<len;i++)
  366. curve[i] = 1. / (1e-6f+curve[i]*ac[i]);
  367. for (i=0;i<2*len;i++)
  368. ac[i] = 0;
  369. for (i=1;i<len;i++)
  370. ac[2*i-1] = curve[i];
  371. ac[0] = curve[0];
  372. ac[2*len-1] = curve[len-1];
  373. spx_drft_backward(&psy->lookup, ac);
  374. _spx_lpc(awk2, ac, ord);
  375. tmp = 1;
  376. for (i=0;i<ord;i++)
  377. {
  378. tmp *= .99;
  379. awk2[i] *= tmp;
  380. }
  381. #endif
  382. }
  383. #if 0
  384. #include <stdio.h>
  385. #include <math.h>
  386. #define ORDER 10
  387. #define CURVE_SIZE 24
  388. int main()
  389. {
  390. int i;
  391. float curve[CURVE_SIZE];
  392. float awk1[ORDER], awk2[ORDER];
  393. for (i=0;i<CURVE_SIZE;i++)
  394. scanf("%f ", &curve[i]);
  395. for (i=0;i<CURVE_SIZE;i++)
  396. curve[i] = pow(10.f, .1*curve[i]);
  397. curve_to_lpc(curve, CURVE_SIZE, awk1, awk2, ORDER);
  398. for (i=0;i<ORDER;i++)
  399. printf("%f ", awk1[i]);
  400. printf ("\n");
  401. for (i=0;i<ORDER;i++)
  402. printf("%f ", awk2[i]);
  403. printf ("\n");
  404. return 0;
  405. }
  406. #endif
  407. #endif