1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: simple utility that runs audio through the psychoacoustics
16 last mod: $Id: psytune.c,v 1.10 2000/11/14 00:05:31 xiphmont Exp $
18 ********************************************************************/
25 #include "vorbis/codec.h"
34 static vorbis_info_psy _psy_set0={
43 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
44 /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
45 /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
48 /* 0 10 20 30 40 50 60 70 80 90 100 */
49 {{-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*63*/
50 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*88*/
51 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*125*/
52 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*175*/
53 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*250*/
54 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*350*/
55 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*500*/
56 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*700*/
57 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*1000*/
58 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*1400*/
59 {-40.,-40.,-40.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*2000*/
60 {-40.,-40.,-40.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*2800*/
61 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*4000*/
62 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*5600*/
63 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*8000*/
64 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*11500*/
65 {-30.,-35.,-35.,-40.,-40.,-50.,-60.,-70.,-80.,-90.,-100.}, /*16000*/
69 {{-14.,-16.,-18.,-19.,-20.,-21.,-22.,-22.,-22.,-22.,-22.}, /*63*/
70 {-14.,-16.,-18.,-19.,-20.,-21.,-22.,-22.,-22.,-22.,-22.}, /*88*/
71 {-14.,-16.,-18.,-19.,-20.,-21.,-22.,-22.,-22.,-22.,-22.}, /*125*/
72 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
73 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
74 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
75 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
76 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
77 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
78 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
79 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
80 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
81 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
82 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-18.,-20.}, /*8000*/
83 { -7., -8., -9.,-10.,-10.,-11.,-12.,-13.,-15.,-16.,-17.}, /*8000*/
84 { -6., -7., -9., -9., -9., -9.,-10.,-11.,-12.,-13.,-14.}, /*11500*/
85 { -6., -6., -9., -9., -9., -9., -9., -9.,-10.,-11.,-12.}, /*16000*/
90 /* 0 10 20 30 40 50 60 70 80 90 100 */
91 {{-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*63*/
92 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*88*/
93 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*125*/
94 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*175*/
95 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*250*/
96 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*350*/
97 {-99.,-99.,-99.,-99.,-99.,-99.,-99.,-199.,-199.,-199.,-199.}, /*500*/
98 { 6., 6., 6., 2., 2., 2., 2., 2., 2., 1., 0.}, /*700*/
100 { 6., 6., 6., 5., 5., 5., 5., 5., 5., 4., 3.}, /*1000*/
101 { 6., 6., 6., 5., 5., 5., 5., 5., 5., 4., 3.}, /*1400*/
102 { 6., 6., 6., 5., 5., 5., 5., 5., 5., 4., 3.}, /*2000*/
103 { 6., 6., 6., 5., 5., 5., 5., 5., 5., 4., 3.}, /*2800*/
104 { 6., 6., 6., 5., 5., 5., 5., 5., 5., 4., 3.}, /*4000*/
105 { 10., 10., 10., 10., 10., 10., 8., 8., 6., 6., 6.}, /*5600*/
106 { 10., 10., 10., 10., 10., 10., 8., 8., 8., 8., 8.}, /*8000*/
107 { 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.}, /*11500*/
108 { 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.}, /*16000*/
113 -0., -.004 /* attack/decay control */
117 void analysis(char *base,int i,float *v,int n,int bark,int dB){
122 sprintf(buffer,"%s_%d.m",base,i);
123 of=fopen(buffer,"w");
130 fprintf(of,"%g ",toBARK(22050.*j/n));
132 fprintf(of,"%g ",(float)j);
135 fprintf(of,"%g\n",todB(fabs(v[j])));
137 fprintf(of,"%g\n",v[j]);
152 } vorbis_look_floor0;
156 /* hacked from floor0.c */
157 static void floorinit(vorbis_look_floor0 *look,int n,int m,int ln){
163 lpc_init(&look->lpclook,look->ln,look->m);
165 scale=look->ln/toBARK(22050.);
167 look->linearmap=_ogg_malloc(look->n*sizeof(int));
168 for(j=0;j<look->n;j++){
169 int val=floor( toBARK(22050./n*j) *scale);
170 if(val>look->ln)val=look->ln;
171 look->linearmap[j]=val;
175 int main(int argc,char *argv[]){
185 float *pcm[2],*out[2],*window,*decay[2],*lpc,*floor,*mask;
186 signed char *buffer,*buffer2;
188 vorbis_look_psy p_look;
191 vorbis_look_floor0 floorlook;
230 framesize=atoi(argv[0]);
234 mask=_ogg_malloc(framesize*sizeof(float));
235 pcm[0]=_ogg_malloc(framesize*sizeof(float));
236 pcm[1]=_ogg_malloc(framesize*sizeof(float));
237 out[0]=_ogg_calloc(framesize/2,sizeof(float));
238 out[1]=_ogg_calloc(framesize/2,sizeof(float));
239 decay[0]=_ogg_calloc(framesize/2,sizeof(float));
240 decay[1]=_ogg_calloc(framesize/2,sizeof(float));
241 floor=_ogg_malloc(framesize*sizeof(float));
242 lpc=_ogg_malloc(order*sizeof(float));
243 buffer=_ogg_malloc(framesize*4);
244 buffer2=buffer+framesize*2;
245 window=_vorbis_window(0,framesize,framesize/2,framesize/2);
246 mdct_init(&m_look,framesize);
247 _vp_psy_init(&p_look,&_psy_set0,framesize/2,44100);
248 floorinit(&floorlook,framesize/2,order,map);
250 for(i=0;i<P_BANDS;i++)
251 for(j=0;j<P_LEVELS;j++)
252 analysis("Ptonecurve",i*100+j,p_look.tonecurves[i][j],EHMER_MAX,0,1);
254 /* we cheat on the WAV header; we just bypass 44 bytes and never
255 verify that it matches 16bit/stereo/44.1kHz. */
257 fread(buffer,1,44,stdin);
258 fwrite(buffer,1,44,stdout);
259 memset(buffer,0,framesize*2);
261 analysis("window",0,window,framesize,0,0);
263 fprintf(stderr,"Processing for frame size %d...\n",framesize);
266 long bytes=fread(buffer2,1,framesize*2,stdin);
267 if(bytes<framesize*2)
268 memset(buffer2+bytes,0,framesize*2-bytes);
272 /* uninterleave samples */
273 for(i=0;i<framesize;i++){
274 pcm[0][i]=((buffer[i*4+1]<<8)|
275 (0x00ff&(int)buffer[i*4]))/32768.;
276 pcm[1][i]=((buffer[i*4+3]<<8)|
277 (0x00ff&(int)buffer[i*4+2]))/32768.;
283 analysis("pre",frameno,pcm[i],framesize,0,0);
285 /* do the psychacoustics */
286 for(j=0;j<framesize;j++)
287 pcm[i][j]*=window[j];
289 memcpy(mask,pcm[i],sizeof(float)*framesize);
291 mdct_forward(&m_look,pcm[i],pcm[i]);
293 analysis("mdct",frameno,pcm[i],framesize/2,1,1);
295 _vp_compute_mask(&p_look,pcm[i],floor,decay[i]);
297 analysis("floor",frameno,floor,framesize/2,1,1);
298 analysis("decay",frameno,decay[i],framesize/2,1,1);
300 /*for(j=0;j<framesize/2;){
307 if(fabs(v[k]/floor[j+k])>.5)acc+=v[k]*v[k];
310 if(acc>0.)fprintf(stderr,"culling\n");
311 for(k=0;k<32;k++)v[k]=0;
316 _vp_apply_floor(&p_look,pcm[i],floor);
319 analysis("quant",frameno,pcm[i],framesize/2,1,1);
322 for(j=0;j<framesize/2;j++){
323 float val=rint(pcm[i][j]);
327 acc+=log(fabs(val)*2.+1.)/log(2);
328 pcm[i][j]=val*floor[j];
334 analysis("final",frameno,pcm[i],framesize/2,1,1);
336 /* take it back to time */
337 mdct_backward(&m_look,pcm[i],pcm[i]);
338 for(j=0;j<framesize/2;j++)
339 out[i][j]+=pcm[i][j]*window[j];
344 /* write data. Use the part of buffer we're about to shift out */
346 char *ptr=buffer+i*2;
348 for(j=0;j<framesize/2;j++){
349 int val=mono[j]*32767.;
350 /* might as well guard against clipping */
351 if(val>32767)val=32767;
352 if(val<-32768)val=-32768;
354 ptr[1]=(val>>8)&0xff;
360 fwrite(buffer,1,framesize*2,stdout);
361 memmove(buffer,buffer2,framesize*2);
364 for(j=0,k=framesize/2;j<framesize/2;j++,k++)
365 out[i][j]=pcm[i][k]*window[k];
370 fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
371 fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
373 fprintf(stderr,"Done\n\n");