psytune.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
  9. * by the Xiph.Org Foundation https://xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: simple utility that runs audio through the psychoacoustics
  13. without encoding
  14. ********************************************************************/
  15. /* NB: this is dead code, retained purely for doc and reference value
  16. don't try to compile it */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include "vorbis/codec.h"
  22. #include "codec_internal.h"
  23. #include "os.h"
  24. #include "misc.h"
  25. #include "psy.h"
  26. #include "mdct.h"
  27. #include "smallft.h"
  28. #include "window.h"
  29. #include "scales.h"
  30. #include "lpc.h"
  31. #include "lsp.h"
  32. #include "masking.h"
  33. #include "registry.h"
  34. static vorbis_info_psy_global _psy_set0G={
  35. 0, /* decaydBpms */
  36. 8, /* lines per eighth octave */
  37. /* thresh sample period, preecho clamp trigger threshhold, range, minenergy */
  38. 256, {26.f,26.f,26.f,30.f}, {-90.f,-90.f,-90.f,-90.f}, -90.f,
  39. -6.f,
  40. 0,
  41. 0.,
  42. 0.,
  43. };
  44. static vp_part _vp_part0[]={
  45. { 1,9e10f, 9e10f, 1.f,9999.f},
  46. { 9999, .75f, 9e10f, .5f,9999.f},
  47. /*{ 9999, 1.5f, 9e10f, .5f,9999.f},*/
  48. { 18,9e10f, 9e10f, .5f, 30.f},
  49. { 9999,9e10f, 9e10f, .5f, 30.f}
  50. };
  51. static vp_couple _vp_couple0[]={
  52. { 1, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
  53. { 18, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
  54. { 9999, {9e10f,9e10f,0}, { 0.f, 9e10f,0}, { 0.f,22.f,1}, {0.f,0.f,0}}
  55. };
  56. static vorbis_info_psy _psy_set0={
  57. ATH_Bark_dB_lineaggressive,
  58. -100.f,
  59. -140.f,
  60. 6.f, /* floor master att */
  61. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
  62. /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
  63. /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
  64. 1, /* tonemaskp */
  65. 0.f, /* tone master att */
  66. /* 0 10 20 30 40 50 60 70 80 90 100 */
  67. {
  68. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*63*/
  69. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*88*/
  70. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*125*/
  71. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*175*/
  72. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*250*/
  73. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*350*/
  74. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*500*/
  75. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*700*/
  76. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1000*/
  77. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1400*/
  78. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2000*/
  79. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2800*/
  80. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*4000*/
  81. {-30.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*5600*/
  82. {-30.f,-30.f,-33.f,-35.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*8000*/
  83. {-30.f,-30.f,-33.f,-35.f,-40.f,-45.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*11500*/
  84. {-24.f,-24.f,-26.f,-32.f,-32.f,-42.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*16000*/
  85. },
  86. 1,/* peakattp */
  87. {{-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*63*/
  88. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*88*/
  89. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*125*/
  90. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*175*/
  91. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*250*/
  92. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*350*/
  93. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*500*/
  94. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*700*/
  95. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1000*/
  96. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1400*/
  97. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2000*/
  98. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2800*/
  99. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*4000*/
  100. {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*5600*/
  101. {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*8000*/
  102. {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*11500*/
  103. {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*16000*/
  104. },
  105. 1,/*noisemaskp */
  106. -10.f, /* suppress any noise curve over maxspec+n */
  107. .5f, /* low window */
  108. .5f, /* high window */
  109. 10,
  110. 10,
  111. 25,
  112. {.000f, 0.f, /*63*/
  113. .000f, 0.f, /*88*/
  114. .000f, 0.f, /*125*/
  115. .000f, 0.f, /*175*/
  116. .000f, 0.f, /*250*/
  117. .000f, 0.f, /*350*/
  118. .000f, 0.f, /*500*/
  119. .000f, 0.f, /*700*/
  120. .000f, 0.f, /*1000*/
  121. .300f, 0.f, /*1400*/
  122. .300f, 0.f, /*2000*/
  123. .300f, 0.f, /*2800*/
  124. .500f, 0.f, /*4000*/
  125. .700f, 0.f, /*5600*/
  126. .850f, 0.f, /*8000*/
  127. .900f, 0.f, /*11500*/
  128. .900f, 1.f, /*16000*/
  129. },
  130. 95.f, /* even decade + 5 is important; saves an rint() later in a
  131. tight loop) */
  132. -44.,
  133. 32,
  134. _vp_part0,_vp_couple0
  135. };
  136. static vorbis_info_floor1 _floor_set0={1,
  137. {0},
  138. {32},
  139. {0},
  140. {0},
  141. {{-1}},
  142. 2,
  143. {0,1024,
  144. 88,31,243,
  145. 14,54,143,460,
  146. 6,3,10, 22,18,26, 41,36,47,
  147. 69,61,78, 112,99,126, 185,162,211,
  148. 329,282,387, 672,553,825
  149. },
  150. 60,30,400,
  151. 20,8,1,18.,
  152. 20,600,
  153. 960};
  154. static vorbis_info_mapping0 mapping_info={1,{0,1},{0},{0},{0},0, 1, {0},{1}};
  155. static codec_setup_info codec_setup0={ {0,0},
  156. 1,1,1,1,1,0,1,
  157. {NULL},
  158. {0},{&mapping_info},
  159. {0},{NULL},
  160. {1},{&_floor_set0},
  161. {2},{NULL},
  162. {NULL},
  163. {&_psy_set0},
  164. &_psy_set0G};
  165. static int noisy=0;
  166. void analysis(char *base,int i,float *v,int n,int bark,int dB){
  167. if(noisy){
  168. int j;
  169. FILE *of;
  170. char buffer[80];
  171. sprintf(buffer,"%s_%d.m",base,i);
  172. of=fopen(buffer,"w");
  173. for(j=0;j<n;j++){
  174. if(dB && v[j]==0)
  175. fprintf(of,"\n\n");
  176. else{
  177. if(bark)
  178. fprintf(of,"%g ",toBARK(22050.f*j/n));
  179. else
  180. fprintf(of,"%g ",(float)j);
  181. if(dB){
  182. fprintf(of,"%g\n",todB(v+j));
  183. }else{
  184. fprintf(of,"%g\n",v[j]);
  185. }
  186. }
  187. }
  188. fclose(of);
  189. }
  190. }
  191. long frameno=0;
  192. /****************************************************************/
  193. int main(int argc,char *argv[]){
  194. int eos=0;
  195. float nonz=0.f;
  196. float acc=0.f;
  197. float tot=0.f;
  198. float ampmax=-9999,newmax;
  199. float local_ampmax[2];
  200. int framesize=2048;
  201. float ampmax_att_per_sec=-6.;
  202. float *pcm[2],*out[2],*window,*flr[2],*mask[2],*work[2];
  203. signed char *buffer,*buffer2;
  204. mdct_lookup m_look;
  205. drft_lookup f_look;
  206. vorbis_look_psy p_look;
  207. vorbis_look_psy_global *pg_look;
  208. vorbis_look_floor *floor_look;
  209. vorbis_info vi;
  210. long i,j,k;
  211. int ath=0;
  212. int decayp=0;
  213. argv++;
  214. while(*argv){
  215. if(*argv[0]=='-'){
  216. /* option */
  217. if(argv[0][1]=='v'){
  218. noisy=0;
  219. }
  220. }else
  221. if(*argv[0]=='+'){
  222. /* option */
  223. if(argv[0][1]=='v'){
  224. noisy=1;
  225. }
  226. }else
  227. framesize=atoi(argv[0]);
  228. argv++;
  229. }
  230. vi.channels=2;
  231. vi.codec_setup=&codec_setup0;
  232. pcm[0]=_ogg_malloc(framesize*sizeof(float));
  233. pcm[1]=_ogg_malloc(framesize*sizeof(float));
  234. out[0]=_ogg_calloc(framesize/2,sizeof(float));
  235. out[1]=_ogg_calloc(framesize/2,sizeof(float));
  236. work[0]=_ogg_calloc(framesize,sizeof(float));
  237. work[1]=_ogg_calloc(framesize,sizeof(float));
  238. flr[0]=_ogg_calloc(framesize/2,sizeof(float));
  239. flr[1]=_ogg_calloc(framesize/2,sizeof(float));
  240. buffer=_ogg_malloc(framesize*4);
  241. buffer2=buffer+framesize*2;
  242. window=_vorbis_window_create(0,framesize,framesize/2,framesize/2);
  243. mdct_init(&m_look,framesize);
  244. drft_init(&f_look,framesize);
  245. _vp_psy_init(&p_look,&_psy_set0,&_psy_set0G,framesize/2,44100);
  246. pg_look=_vp_global_look(&vi);
  247. floor_look=_floor_P[1]->look(NULL,NULL,&_floor_set0);
  248. /* we cheat on the WAV header; we just bypass 44 bytes and never
  249. verify that it matches 16bit/stereo/44.1kHz. */
  250. fread(buffer,1,44,stdin);
  251. fwrite(buffer,1,44,stdout);
  252. memset(buffer,0,framesize*2);
  253. analysis("window",0,window,framesize,0,0);
  254. fprintf(stderr,"Processing for frame size %d...\n",framesize);
  255. while(!eos){
  256. long bytes=fread(buffer2,1,framesize*2,stdin);
  257. if(bytes<framesize*2)
  258. memset(buffer2+bytes,0,framesize*2-bytes);
  259. if(bytes!=0){
  260. int nonzero[2];
  261. /* uninterleave samples */
  262. for(i=0;i<framesize;i++){
  263. pcm[0][i]=((buffer[i*4+1]<<8)|
  264. (0x00ff&(int)buffer[i*4]))/32768.f;
  265. pcm[1][i]=((buffer[i*4+3]<<8)|
  266. (0x00ff&(int)buffer[i*4+2]))/32768.f;
  267. }
  268. {
  269. float secs=framesize/44100.;
  270. ampmax+=secs*ampmax_att_per_sec;
  271. if(ampmax<-9999)ampmax=-9999;
  272. }
  273. for(i=0;i<2;i++){
  274. float scale=4.f/framesize;
  275. float *fft=work[i];
  276. float *mdct=pcm[i];
  277. float *logmdct=mdct+framesize/2;
  278. analysis("pre",frameno+i,pcm[i],framesize,0,0);
  279. /* fft and mdct transforms */
  280. for(j=0;j<framesize;j++)
  281. fft[j]=pcm[i][j]*=window[j];
  282. drft_forward(&f_look,fft);
  283. local_ampmax[i]=-9999.f;
  284. fft[0]*=scale;
  285. fft[0]=todB(fft);
  286. for(j=1;j<framesize-1;j+=2){
  287. float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
  288. temp=fft[(j+1)>>1]=todB(&temp);
  289. if(temp>local_ampmax[i])local_ampmax[i]=temp;
  290. }
  291. if(local_ampmax[i]>ampmax)ampmax=local_ampmax[i];
  292. mdct_forward(&m_look,pcm[i],mdct);
  293. for(j=0;j<framesize/2;j++)
  294. logmdct[j]=todB(mdct+j);
  295. analysis("mdct",frameno+i,logmdct,framesize/2,1,0);
  296. analysis("fft",frameno+i,fft,framesize/2,1,0);
  297. }
  298. for(i=0;i<2;i++){
  299. float amp;
  300. float *fft=work[i];
  301. float *logmax=fft;
  302. float *mdct=pcm[i];
  303. float *logmdct=mdct+framesize/2;
  304. float *mask=fft+framesize/2;
  305. /* floor psychoacoustics */
  306. _vp_compute_mask(&p_look,
  307. pg_look,
  308. i,
  309. fft,
  310. logmdct,
  311. mask,
  312. ampmax,
  313. local_ampmax[i],
  314. framesize/2);
  315. analysis("mask",frameno+i,mask,framesize/2,1,0);
  316. {
  317. vorbis_block vb;
  318. vorbis_dsp_state vd;
  319. memset(&vd,0,sizeof(vd));
  320. vd.vi=&vi;
  321. vb.vd=&vd;
  322. vb.pcmend=framesize;
  323. /* floor quantization/application */
  324. nonzero[i]=_floor_P[1]->forward(&vb,floor_look,
  325. mdct,
  326. logmdct,
  327. mask,
  328. logmax,
  329. flr[i]);
  330. }
  331. _vp_remove_floor(&p_look,
  332. pg_look,
  333. logmdct,
  334. mdct,
  335. flr[i],
  336. pcm[i],
  337. local_ampmax[i]);
  338. for(j=0;j<framesize/2;j++)
  339. if(fabs(pcm[i][j])>1500)
  340. fprintf(stderr,"%ld ",frameno+i);
  341. analysis("res",frameno+i,pcm[i],framesize/2,1,0);
  342. analysis("codedflr",frameno+i,flr[i],framesize/2,1,1);
  343. }
  344. /* residue prequantization */
  345. _vp_partition_prequant(&p_look,
  346. &vi,
  347. pcm,
  348. nonzero);
  349. for(i=0;i<2;i++)
  350. analysis("quant",frameno+i,pcm[i],framesize/2,1,0);
  351. /* channel coupling / stereo quantization */
  352. _vp_couple(&p_look,
  353. &mapping_info,
  354. pcm,
  355. nonzero);
  356. for(i=0;i<2;i++)
  357. analysis("coupled",frameno+i,pcm[i],framesize/2,1,0);
  358. /* decoupling */
  359. for(i=mapping_info.coupling_steps-1;i>=0;i--){
  360. float *pcmM=pcm[mapping_info.coupling_mag[i]];
  361. float *pcmA=pcm[mapping_info.coupling_ang[i]];
  362. for(j=0;j<framesize/2;j++){
  363. float mag=pcmM[j];
  364. float ang=pcmA[j];
  365. if(mag>0)
  366. if(ang>0){
  367. pcmM[j]=mag;
  368. pcmA[j]=mag-ang;
  369. }else{
  370. pcmA[j]=mag;
  371. pcmM[j]=mag+ang;
  372. }
  373. else
  374. if(ang>0){
  375. pcmM[j]=mag;
  376. pcmA[j]=mag+ang;
  377. }else{
  378. pcmA[j]=mag;
  379. pcmM[j]=mag-ang;
  380. }
  381. }
  382. }
  383. for(i=0;i<2;i++)
  384. analysis("decoupled",frameno+i,pcm[i],framesize/2,1,0);
  385. for(i=0;i<2;i++){
  386. float amp;
  387. for(j=0;j<framesize/2;j++)
  388. pcm[i][j]*=flr[i][j];
  389. analysis("final",frameno+i,pcm[i],framesize/2,1,1);
  390. /* take it back to time */
  391. mdct_backward(&m_look,pcm[i],pcm[i]);
  392. for(j=0;j<framesize/2;j++)
  393. out[i][j]+=pcm[i][j]*window[j];
  394. analysis("out",frameno+i,out[i],framesize/2,0,0);
  395. }
  396. /* write data. Use the part of buffer we're about to shift out */
  397. for(i=0;i<2;i++){
  398. char *ptr=buffer+i*2;
  399. float *mono=out[i];
  400. int flag=0;
  401. for(j=0;j<framesize/2;j++){
  402. int val=mono[j]*32767.;
  403. /* might as well guard against clipping */
  404. if(val>32767){
  405. if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
  406. flag=1;
  407. val=32767;
  408. }
  409. if(val<-32768){
  410. if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
  411. flag=1;
  412. val=-32768;
  413. }
  414. ptr[0]=val&0xff;
  415. ptr[1]=(val>>8)&0xff;
  416. ptr+=4;
  417. }
  418. }
  419. fprintf(stderr,"*");
  420. fwrite(buffer,1,framesize*2,stdout);
  421. memmove(buffer,buffer2,framesize*2);
  422. for(i=0;i<2;i++){
  423. for(j=0,k=framesize/2;j<framesize/2;j++,k++)
  424. out[i][j]=pcm[i][k]*window[k];
  425. }
  426. frameno+=2;
  427. }else
  428. eos=1;
  429. }
  430. fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
  431. fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
  432. framesize/2);
  433. fprintf(stderr,"Done\n\n");
  434. return 0;
  435. }