psytune.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: simple utility that runs audio through the psychoacoustics
  12. without encoding
  13. last mod: $Id: psytune.c,v 1.15 2001/05/27 06:44:00 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "vorbis/codec.h"
  20. #include "os.h"
  21. #include "psy.h"
  22. #include "mdct.h"
  23. #include "smallft.h"
  24. #include "window.h"
  25. #include "scales.h"
  26. #include "lpc.h"
  27. #include "lsp.h"
  28. static vorbis_info_psy _psy_set0={
  29. 1,/*athp*/
  30. 1,/*decayp*/
  31. -100.f,
  32. -140.f,
  33. 8,
  34. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
  35. /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
  36. /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
  37. 1,/* tonemaskp */
  38. /* 0 10 20 30 40 50 60 70 80 90 100 */
  39. {
  40. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*63*/
  41. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*88*/
  42. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*125*/
  43. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*175*/
  44. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*250*/
  45. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*350*/
  46. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*500*/
  47. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*700*/
  48. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1000*/
  49. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1400*/
  50. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2000*/
  51. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2800*/
  52. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*4000*/
  53. {-35.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*5600*/
  54. {-30.f,-30.f,-33.f,-35.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*8000*/
  55. {-30.f,-30.f,-33.f,-35.f,-35.f,-45.f,-50.f,-60.f,-70.f,-90.f,-100.f}, /*11500*/
  56. {-24.f,-24.f,-26.f,-32.f,-32.f,-42.f,-50.f,-60.f,-70.f,-90.f,-100.f}, /*16000*/
  57. },
  58. 1,/* peakattp */
  59. {{-14.f,-16.f,-18.f,-19.f,-20.f,-21.f,-22.f,-22.f,-24.f,-24.f,-24.f},/*63*/
  60. {-14.f,-16.f,-18.f,-19.f,-20.f,-21.f,-22.f,-22.f,-24.f,-24.f,-24.f},/*88*/
  61. {-14.f,-16.f,-18.f,-19.f,-20.f,-21.f,-22.f,-22.f,-24.f,-24.f,-24.f},/*125*/
  62. {-10.f,-10.f,-10.f,-10.f,-16.f,-16.f,-18.f,-20.f,-24.f,-24.f,-24.f},/*175*/
  63. {-10.f,-10.f,-10.f,-10.f,-16.f,-16.f,-18.f,-20.f,-24.f,-24.f,-24.f},/*250*/
  64. {-10.f,-10.f,-10.f,-10.f,-16.f,-16.f,-18.f,-20.f,-22.f,-24.f,-24.f},/*350*/
  65. {-10.f,-10.f,-10.f,-10.f,-16.f,-16.f,-18.f,-20.f,-22.f,-24.f,-24.f},/*500*/
  66. {-10.f,-10.f,-10.f,-10.f,-14.f,-14.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*700*/
  67. {-10.f,-10.f,-10.f,-10.f,-14.f,-14.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*1000*/
  68. {-10.f,-10.f,-10.f,-10.f,-14.f,-14.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*1400*/
  69. {-10.f,-10.f,-10.f,-10.f,-14.f,-14.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*2000*/
  70. {-10.f,-10.f,-10.f,-12.f,-16.f,-16.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*2400*/
  71. {-10.f,-10.f,-10.f,-12.f,-16.f,-16.f,-16.f,-20.f,-22.f,-24.f,-24.f},/*4000*/
  72. {-10.f,-10.f,-10.f,-12.f,-12.f,-14.f,-16.f,-18.f,-22.f,-24.f,-24.f},/*5600*/
  73. {-10.f,-10.f,-10.f,-10.f,-10.f,-14.f,-16.f,-18.f,-22.f,-24.f,-24.f},/*8000*/
  74. {-10.f,-10.f,-10.f,-10.f,-10.f,-14.f,-16.f,-18.f,-22.f,-24.f,-24.f},/*11500*/
  75. {-10.f,-10.f,-10.f,-10.f,-10.f,-12.f,-16.f,-18.f,-22.f,-24.f,-24.f},/*16000*/
  76. },
  77. 1,/*noisemaskp */
  78. -24.f, /* suppress any noise curve over maxspec+n */
  79. .5f, /* low window */
  80. .5f, /* high window */
  81. 25,
  82. 25,
  83. {.000f, 0.f, /*63*/
  84. .000f, 0.f, /*88*/
  85. .000f, 0.f, /*125*/
  86. .000f, 0.f, /*175*/
  87. .000f, 0.f, /*250*/
  88. .000f, 0.f, /*350*/
  89. .000f, 0.f, /*500*/
  90. .200f, 0.f, /*700*/
  91. .300f, 0.f, /*1000*/
  92. .400f, 0.f, /*1400*/
  93. .400f, 0.f, /*2000*/
  94. .400f, 0.f, /*2800*/
  95. .700f, 0.f, /*4000*/
  96. .850f, 0.f, /*5600*/
  97. .900f, 0.f, /*8000*/
  98. .900f, 0.f, /*11500*/
  99. .900f, 1.f, /*16000*/
  100. },
  101. 95.f, /* even decade + 5 is important; saves an rint() later in a
  102. tight loop) */
  103. -28.,
  104. };
  105. static int noisy=1;
  106. void analysis(char *base,int i,float *v,int n,int bark,int dB){
  107. if(noisy){
  108. int j;
  109. FILE *of;
  110. char buffer[80];
  111. sprintf(buffer,"%s_%d.m",base,i);
  112. of=fopen(buffer,"w");
  113. for(j=0;j<n;j++){
  114. if(dB && v[j]==0)
  115. fprintf(of,"\n\n");
  116. else{
  117. if(bark)
  118. fprintf(of,"%g ",toBARK(22050.f*j/n));
  119. else
  120. fprintf(of,"%g ",(float)j);
  121. if(dB){
  122. fprintf(of,"%g\n",todB(fabs(v+j)));
  123. }else{
  124. fprintf(of,"%g\n",v[j]);
  125. }
  126. }
  127. }
  128. fclose(of);
  129. }
  130. }
  131. long frameno=0;
  132. /****************************************************************/
  133. int main(int argc,char *argv[]){
  134. int eos=0;
  135. float nonz=0.f;
  136. float acc=0.f;
  137. float tot=0.f;
  138. float ampmax=-9999,newmax;
  139. int framesize=2048;
  140. int order=30;
  141. int map=256;
  142. float ampmax_att_per_sec=-10.;
  143. float *pcm[2],*out[2],*window,*lpc,*flr,*mask;
  144. signed char *buffer,*buffer2;
  145. mdct_lookup m_look;
  146. drft_lookup f_look;
  147. drft_lookup f_look2;
  148. vorbis_look_psy p_look;
  149. long i,j,k;
  150. int ath=0;
  151. int decayp=0;
  152. argv++;
  153. while(*argv){
  154. if(*argv[0]=='-'){
  155. /* option */
  156. if(argv[0][1]=='v'){
  157. noisy=0;
  158. }
  159. if(argv[0][1]=='o'){
  160. order=atoi(argv[0]+2);
  161. }
  162. if(argv[0][1]=='m'){
  163. map=atoi(argv[0]+2);
  164. }
  165. }else
  166. if(*argv[0]=='+'){
  167. /* option */
  168. if(argv[0][1]=='v'){
  169. noisy=1;
  170. }
  171. if(argv[0][1]=='o'){
  172. order=atoi(argv[0]+2);
  173. }
  174. if(argv[0][1]=='m'){
  175. map=atoi(argv[0]+2);
  176. }
  177. }else
  178. framesize=atoi(argv[0]);
  179. argv++;
  180. }
  181. mask=_ogg_malloc(framesize*sizeof(float));
  182. pcm[0]=_ogg_malloc(framesize*sizeof(float));
  183. pcm[1]=_ogg_malloc(framesize*sizeof(float));
  184. out[0]=_ogg_calloc(framesize/2,sizeof(float));
  185. out[1]=_ogg_calloc(framesize/2,sizeof(float));
  186. flr=_ogg_malloc(framesize*sizeof(float));
  187. lpc=_ogg_malloc(order*sizeof(float));
  188. buffer=_ogg_malloc(framesize*4);
  189. buffer2=buffer+framesize*2;
  190. window=_vorbis_window(0,framesize,framesize/2,framesize/2);
  191. mdct_init(&m_look,framesize);
  192. drft_init(&f_look,framesize);
  193. drft_init(&f_look2,framesize/2);
  194. _vp_psy_init(&p_look,&_psy_set0,framesize/2,44100);
  195. for(i=0;i<P_BANDS;i++)
  196. for(j=0;j<P_LEVELS;j++)
  197. analysis("Ptonecurve",i*100+j,p_look.tonecurves[i][j],EHMER_MAX,0,0);
  198. /* we cheat on the WAV header; we just bypass 44 bytes and never
  199. verify that it matches 16bit/stereo/44.1kHz. */
  200. fread(buffer,1,44,stdin);
  201. fwrite(buffer,1,44,stdout);
  202. memset(buffer,0,framesize*2);
  203. analysis("window",0,window,framesize,0,0);
  204. fprintf(stderr,"Processing for frame size %d...\n",framesize);
  205. while(!eos){
  206. long bytes=fread(buffer2,1,framesize*2,stdin);
  207. if(bytes<framesize*2)
  208. memset(buffer2+bytes,0,framesize*2-bytes);
  209. if(bytes!=0){
  210. /* uninterleave samples */
  211. for(i=0;i<framesize;i++){
  212. pcm[0][i]=((buffer[i*4+1]<<8)|
  213. (0x00ff&(int)buffer[i*4]))/32768.f;
  214. pcm[1][i]=((buffer[i*4+3]<<8)|
  215. (0x00ff&(int)buffer[i*4+2]))/32768.f;
  216. }
  217. {
  218. float secs=framesize/44100.;
  219. ampmax+=secs*ampmax_att_per_sec;
  220. if(ampmax<-9999)ampmax=-9999;
  221. }
  222. newmax=ampmax;
  223. for(i=0;i<2;i++){
  224. float amp;
  225. analysis("pre",frameno,pcm[i],framesize,0,0);
  226. memcpy(mask,pcm[i],sizeof(float)*framesize);
  227. /* do the psychacoustics */
  228. for(j=0;j<framesize;j++)
  229. mask[j]=pcm[i][j]*=window[j];
  230. drft_forward(&f_look,mask);
  231. mask[0]/=(framesize/4.);
  232. for(j=1;j<framesize-1;j+=2)
  233. mask[(j+1)>>1]=4*hypot(mask[j],mask[j+1])/framesize;
  234. mdct_forward(&m_look,pcm[i],pcm[i]);
  235. memcpy(mask+framesize/2,pcm[i],sizeof(float)*framesize/2);
  236. analysis("mdct",frameno,pcm[i],framesize/2,0,1);
  237. analysis("fft",frameno,mask,framesize/2,0,1);
  238. {
  239. float ret;
  240. ret=_vp_compute_mask(&p_look,mask,mask+framesize/2,flr,NULL,ampmax);
  241. if(ret>newmax)newmax=ret;
  242. }
  243. analysis("mask",frameno,flr,framesize/2,0,0);
  244. mask[framesize-1]=0.;
  245. mask[0]=0.;
  246. for(j=1;j<framesize-1;j+=2){
  247. mask[j]=todB(pcm[i]+((j+1)>>1));
  248. mask[j+1]=0;
  249. }
  250. analysis("lfft",frameno,mask,framesize,0,0);
  251. drft_backward(&f_look,mask);
  252. analysis("cep",frameno,mask,framesize,0,0);
  253. analysis("logcep",frameno,mask,framesize,0,1);
  254. /*for(j=0;j<framesize/2;j++){
  255. float val=fromdB(flr[j]);
  256. int p=rint(pcm[i][j]/val);
  257. pcm[i][j]=p*val;
  258. }*/
  259. /*for(j=0;j<framesize/2;j++){
  260. float val=todB(pcm[i]+j);
  261. if(val+6.<flr[j])
  262. pcm[i][j]=0.;
  263. }*/
  264. for(j=0;j<framesize/2;j++){
  265. float val=rint(todB(pcm[i]+j)/6);
  266. if(pcm[i][j]>0)
  267. pcm[i][j]=fromdB(val*6);
  268. else
  269. pcm[i][j]=-fromdB(val*6);
  270. }
  271. analysis("final",frameno,pcm[i],framesize/2,0,1);
  272. /* take it back to time */
  273. mdct_backward(&m_look,pcm[i],pcm[i]);
  274. for(j=0;j<framesize/2;j++)
  275. out[i][j]+=pcm[i][j]*window[j];
  276. frameno++;
  277. }
  278. ampmax=newmax;
  279. /* write data. Use the part of buffer we're about to shift out */
  280. for(i=0;i<2;i++){
  281. char *ptr=buffer+i*2;
  282. float *mono=out[i];
  283. for(j=0;j<framesize/2;j++){
  284. int val=mono[j]*32767.;
  285. /* might as well guard against clipping */
  286. if(val>32767)val=32767;
  287. if(val<-32768)val=-32768;
  288. ptr[0]=val&0xff;
  289. ptr[1]=(val>>8)&0xff;
  290. ptr+=4;
  291. }
  292. }
  293. fprintf(stderr,"*");
  294. fwrite(buffer,1,framesize*2,stdout);
  295. memmove(buffer,buffer2,framesize*2);
  296. for(i=0;i<2;i++){
  297. for(j=0,k=framesize/2;j<framesize/2;j++,k++)
  298. out[i][j]=pcm[i][k]*window[k];
  299. }
  300. }else
  301. eos=1;
  302. }
  303. fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
  304. fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
  305. framesize/2);
  306. fprintf(stderr,"Done\n\n");
  307. return 0;
  308. }