1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: simple utility that runs audio through the psychoacoustics
15 last mod: $Id: psytune.c,v 1.17 2001/08/21 19:05:06 giles Exp $
17 ********************************************************************/
24 #include "vorbis/codec.h"
25 #include "codec_internal.h"
37 static vorbis_info_psy_global _psy_set0G={
39 8, /* lines per eighth octave */
41 /* thresh sample period, preecho clamp trigger threshhold, range, minenergy */
42 256, {26.f,26.f,26.f,30.f}, {-90.f,-90.f,-90.f,-90.f}, -90.f,
51 static vp_part _vp_part0[]={
52 { 1,9e10f, 9e10f, 1.f,9999.f},
53 { 9999, .75f, 9e10f, .5f,9999.f},
54 /*{ 9999, 1.5f, 9e10f, .5f,9999.f},*/
55 { 18,9e10f, 9e10f, .5f, 30.f},
56 { 9999,9e10f, 9e10f, .5f, 30.f}
59 static vp_couple _vp_couple0[]={
60 { 1, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
61 { 18, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
62 { 9999, {9e10f,9e10f,0}, { 0.f, 9e10f,0}, { 0.f,22.f,1}, {0.f,0.f,0}}
65 static vorbis_info_psy _psy_set0={
66 ATH_Bark_dB_lineaggressive,
70 6.f, /* floor master att */
72 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
73 /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
74 /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
76 0.f, /* tone master att */
77 /* 0 10 20 30 40 50 60 70 80 90 100 */
79 {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*63*/
80 {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*88*/
81 {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*125*/
83 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*175*/
84 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*250*/
85 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*350*/
86 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*500*/
87 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*700*/
88 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1000*/
89 {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1400*/
90 {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2000*/
91 {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2800*/
92 {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*4000*/
94 {-30.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*5600*/
96 {-30.f,-30.f,-33.f,-35.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*8000*/
97 {-30.f,-30.f,-33.f,-35.f,-40.f,-45.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*11500*/
98 {-24.f,-24.f,-26.f,-32.f,-32.f,-42.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*16000*/
103 {{-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*63*/
104 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*88*/
105 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*125*/
106 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*175*/
107 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*250*/
108 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*350*/
109 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*500*/
110 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*700*/
111 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1000*/
112 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1400*/
113 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2000*/
114 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2800*/
115 {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*4000*/
116 {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*5600*/
117 {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*8000*/
118 {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*11500*/
119 {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*16000*/
123 -10.f, /* suppress any noise curve over maxspec+n */
124 .5f, /* low window */
125 .5f, /* high window */
144 .900f, 0.f, /*11500*/
145 .900f, 1.f, /*16000*/
148 95.f, /* even decade + 5 is important; saves an rint() later in a
153 _vp_part0,_vp_couple0
156 static vorbis_info_floor1 _floor_set0={1,
171 6,3,10, 22,18,26, 41,36,47,
172 69,61,78, 112,99,126, 185,162,211,
173 329,282,387, 672,553,825
182 static vorbis_info_mapping0 mapping_info={1,{0,1},{0},{0},{0},0, 1, {0},{1}};
183 static codec_setup_info codec_setup0={ {0,0},
195 void analysis(char *base,int i,float *v,int n,int bark,int dB){
200 sprintf(buffer,"%s_%d.m",base,i);
201 of=fopen(buffer,"w");
208 fprintf(of,"%g ",toBARK(22050.f*j/n));
210 fprintf(of,"%g ",(float)j);
213 fprintf(of,"%g\n",todB(v+j));
215 fprintf(of,"%g\n",v[j]);
225 /****************************************************************/
227 int main(int argc,char *argv[]){
232 float ampmax=-9999,newmax;
233 float local_ampmax[2];
236 float ampmax_att_per_sec=-6.;
238 float *pcm[2],*out[2],*window,*flr[2],*mask[2],*work[2];
239 signed char *buffer,*buffer2;
242 vorbis_look_psy p_look;
243 vorbis_look_psy_global *pg_look;
244 vorbis_look_floor *floor_look;
265 framesize=atoi(argv[0]);
270 vi.codec_setup=&codec_setup0;
272 pcm[0]=_ogg_malloc(framesize*sizeof(float));
273 pcm[1]=_ogg_malloc(framesize*sizeof(float));
274 out[0]=_ogg_calloc(framesize/2,sizeof(float));
275 out[1]=_ogg_calloc(framesize/2,sizeof(float));
276 work[0]=_ogg_calloc(framesize,sizeof(float));
277 work[1]=_ogg_calloc(framesize,sizeof(float));
278 flr[0]=_ogg_calloc(framesize/2,sizeof(float));
279 flr[1]=_ogg_calloc(framesize/2,sizeof(float));
280 buffer=_ogg_malloc(framesize*4);
281 buffer2=buffer+framesize*2;
282 window=_vorbis_window(0,framesize,framesize/2,framesize/2);
283 mdct_init(&m_look,framesize);
284 drft_init(&f_look,framesize);
285 _vp_psy_init(&p_look,&_psy_set0,&_psy_set0G,framesize/2,44100);
286 pg_look=_vp_global_look(&vi);
287 floor_look=_floor_P[1]->look(NULL,NULL,&_floor_set0);
289 /* we cheat on the WAV header; we just bypass 44 bytes and never
290 verify that it matches 16bit/stereo/44.1kHz. */
292 fread(buffer,1,44,stdin);
293 fwrite(buffer,1,44,stdout);
294 memset(buffer,0,framesize*2);
296 analysis("window",0,window,framesize,0,0);
298 fprintf(stderr,"Processing for frame size %d...\n",framesize);
301 long bytes=fread(buffer2,1,framesize*2,stdin);
302 if(bytes<framesize*2)
303 memset(buffer2+bytes,0,framesize*2-bytes);
308 /* uninterleave samples */
309 for(i=0;i<framesize;i++){
310 pcm[0][i]=((buffer[i*4+1]<<8)|
311 (0x00ff&(int)buffer[i*4]))/32768.f;
312 pcm[1][i]=((buffer[i*4+3]<<8)|
313 (0x00ff&(int)buffer[i*4+2]))/32768.f;
317 float secs=framesize/44100.;
319 ampmax+=secs*ampmax_att_per_sec;
320 if(ampmax<-9999)ampmax=-9999;
324 float scale=4.f/framesize;
327 float *logmdct=mdct+framesize/2;
329 analysis("pre",frameno+i,pcm[i],framesize,0,0);
331 /* fft and mdct transforms */
332 for(j=0;j<framesize;j++)
333 fft[j]=pcm[i][j]*=window[j];
335 drft_forward(&f_look,fft);
337 local_ampmax[i]=-9999.f;
340 for(j=1;j<framesize-1;j+=2){
341 float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
342 temp=fft[(j+1)>>1]=todB(&temp);
343 if(temp>local_ampmax[i])local_ampmax[i]=temp;
345 if(local_ampmax[i]>ampmax)ampmax=local_ampmax[i];
347 mdct_forward(&m_look,pcm[i],mdct);
348 for(j=0;j<framesize/2;j++)
349 logmdct[j]=todB(mdct+j);
351 analysis("mdct",frameno+i,logmdct,framesize/2,1,0);
352 analysis("fft",frameno+i,fft,framesize/2,1,0);
360 float *logmdct=mdct+framesize/2;
361 float *mask=fft+framesize/2;
363 /* floor psychoacoustics */
364 _vp_compute_mask(&p_look,
374 analysis("mask",frameno+i,mask,framesize/2,1,0);
379 memset(&vd,0,sizeof(vd));
384 /* floor quantization/application */
385 nonzero[i]=_floor_P[1]->forward(&vb,floor_look,
394 _vp_remove_floor(&p_look,
402 for(j=0;j<framesize/2;j++)
403 if(fabs(pcm[i][j])>1500)
404 fprintf(stderr,"%ld ",frameno+i);
406 analysis("res",frameno+i,pcm[i],framesize/2,1,0);
407 analysis("codedflr",frameno+i,flr[i],framesize/2,1,1);
410 /* residue prequantization */
411 _vp_partition_prequant(&p_look,
417 analysis("quant",frameno+i,pcm[i],framesize/2,1,0);
419 /* channel coupling / stereo quantization */
427 analysis("coupled",frameno+i,pcm[i],framesize/2,1,0);
430 for(i=mapping_info.coupling_steps-1;i>=0;i--){
431 float *pcmM=pcm[mapping_info.coupling_mag[i]];
432 float *pcmA=pcm[mapping_info.coupling_ang[i]];
434 for(j=0;j<framesize/2;j++){
458 analysis("decoupled",frameno+i,pcm[i],framesize/2,1,0);
463 for(j=0;j<framesize/2;j++)
464 pcm[i][j]*=flr[i][j];
466 analysis("final",frameno+i,pcm[i],framesize/2,1,1);
468 /* take it back to time */
469 mdct_backward(&m_look,pcm[i],pcm[i]);
471 for(j=0;j<framesize/2;j++)
472 out[i][j]+=pcm[i][j]*window[j];
474 analysis("out",frameno+i,out[i],framesize/2,0,0);
479 /* write data. Use the part of buffer we're about to shift out */
481 char *ptr=buffer+i*2;
484 for(j=0;j<framesize/2;j++){
485 int val=mono[j]*32767.;
486 /* might as well guard against clipping */
488 if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
493 if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
498 ptr[1]=(val>>8)&0xff;
504 fwrite(buffer,1,framesize*2,stdout);
505 memmove(buffer,buffer2,framesize*2);
508 for(j=0,k=framesize/2;j<framesize/2;j++,k++)
509 out[i][j]=pcm[i][k]*window[k];
515 fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
516 fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
518 fprintf(stderr,"Done\n\n");