1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH 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.3 2000/05/08 20:49:49 xiphmont Exp $
18 ********************************************************************/
25 #include "vorbis/codec.h"
33 static vorbis_info_psy _psy_set0={
42 {-35.,-40.,-60.,-80.,-80.}, /* remember that el 4 is an 80 dB curve, not 100 */
43 {-35.,-40.,-60.,-80.,-95.},
44 {-35.,-40.,-60.,-80.,-95.},
45 {-35.,-40.,-60.,-80.,-95.},
46 {-35.,-40.,-60.,-80.,-95.},
47 {-65.,-60.,-60.,-80.,-90.}, /* remember that el 1 is a 60 dB curve, not 40 */
50 {-100.,-100.,-100.,-200.,-200.}, /* this is the 500 Hz curve, which
51 is too wrong to work */
52 {-60.,-60.,-60.,-80.,-80.},
53 {-60.,-60.,-60.,-80.,-80.},
54 {-60.,-60.,-60.,-80.,-80.},
55 {-60.,-60.,-60.,-80.,-80.},
56 {-50.,-55.,-60.,-80.,-80.},
60 .9998, .9997 /* attack/decay control */
64 void analysis(char *base,int i,double *v,int n,int bark,int dB){
69 sprintf(buffer,"%s_%d.m",base,i);
77 fprintf(of,"%g ",toBARK(22050.*j/n));
79 fprintf(of,"%g ",(double)j);
82 fprintf(of,"%g\n",todB(fabs(v[j])));
84 fprintf(of,"%g\n",v[j]);
98 vorbis_info_floor0 *vi;
100 } vorbis_look_floor0;
102 extern double _curve_to_lpc(double *curve,double *lpc,vorbis_look_floor0 *l,
104 extern void _lpc_to_curve(double *curve,double *lpc,double amp,
105 vorbis_look_floor0 *l,char *name,long frameno);
109 /* hacked from floor0.c */
110 static void floorinit(vorbis_look_floor0 *look,int n,int m,int ln){
116 lpc_init(&look->lpclook,look->ln,look->m);
118 scale=look->ln/toBARK(22050.);
120 look->linearmap=malloc(look->n*sizeof(int));
121 for(j=0;j<look->n;j++){
122 int val=floor( toBARK(22050./n*j) *scale);
123 if(val>look->ln)val=look->ln;
124 look->linearmap[j]=val;
128 int main(int argc,char *argv[]){
137 double *pcm[2],*out[2],*window,*decay[2],*lpc,*floor,*mask;
138 signed char *buffer,*buffer2;
140 vorbis_look_psy p_look;
143 vorbis_look_floor0 floorlook;
182 framesize=atoi(argv[0]);
186 pcm[0]=malloc(framesize*sizeof(double));
187 pcm[1]=malloc(framesize*sizeof(double));
188 out[0]=calloc(framesize/2,sizeof(double));
189 out[1]=calloc(framesize/2,sizeof(double));
190 decay[0]=calloc(framesize/2,sizeof(double));
191 decay[1]=calloc(framesize/2,sizeof(double));
192 floor=malloc(framesize*sizeof(double));
193 mask=malloc(framesize*sizeof(double));
194 lpc=malloc(order*sizeof(double));
195 buffer=malloc(framesize*4);
196 buffer2=buffer+framesize*2;
197 window=_vorbis_window(0,framesize,framesize/2,framesize/2);
198 mdct_init(&m_look,framesize);
199 _vp_psy_init(&p_look,&_psy_set0,framesize/2,44100);
200 floorinit(&floorlook,framesize/2,order,framesize/8);
204 analysis("Ptonecurve",i*10+j,p_look.tonecurves[i][j],EHMER_MAX,0,1);
207 analysis("Pnoisecurve",i*10+j,p_look.noisecurves[i][j],EHMER_MAX,0,1);
209 /* we cheat on the WAV header; we just bypass 44 bytes and never
210 verify that it matches 16bit/stereo/44.1kHz. */
212 fread(buffer,1,44,stdin);
213 fwrite(buffer,1,44,stdout);
214 memset(buffer,0,framesize*2);
216 analysis("window",0,window,framesize,0,0);
218 fprintf(stderr,"Processing for frame size %d...\n",framesize);
221 long bytes=fread(buffer2,1,framesize*2,stdin);
222 if(bytes<framesize*2)
223 memset(buffer2+bytes,0,framesize*2-bytes);
227 /* uninterleave samples */
228 for(i=0;i<framesize;i++){
229 pcm[0][i]=((buffer[i*4+1]<<8)|
230 (0x00ff&(int)buffer[i*4]))/32768.;
231 pcm[1][i]=((buffer[i*4+3]<<8)|
232 (0x00ff&(int)buffer[i*4+2]))/32768.;
238 analysis("pre",frameno,pcm[i],framesize,0,0);
240 /* do the psychacoustics */
241 for(j=0;j<framesize;j++)
242 pcm[i][j]*=window[j];
244 mdct_forward(&m_look,pcm[i],pcm[i]);
246 analysis("mdct",frameno,pcm[i],framesize/2,1,1);
248 _vp_compute_mask(&p_look,pcm[i],floor,mask,decay[i]);
250 analysis("prefloor",frameno,floor,framesize/2,1,1);
251 analysis("mask",frameno,mask,framesize/2,1,1);
252 analysis("decay",frameno,decay[i],framesize/2,1,1);
254 amp=_curve_to_lpc(floor,lpc,&floorlook,frameno);
255 _lpc_to_curve(floor,lpc,sqrt(amp),&floorlook,"Ffloor",frameno);
256 analysis("floor",frameno,floor,framesize/2,1,1);
258 _vp_apply_floor(&p_look,pcm[i],floor,mask);
259 analysis("quant",frameno,pcm[i],framesize/2,1,1);
262 for(j=0;j<framesize/2;j++){
263 double val=rint(pcm[i][j]);
267 acc+=log(fabs(val)*2.+1.)/log(2);
268 pcm[i][j]=val*floor[j];
274 analysis("final",frameno,pcm[i],framesize/2,1,1);
276 /* take it back to time */
277 mdct_backward(&m_look,pcm[i],pcm[i]);
278 for(j=0;j<framesize/2;j++)
279 out[i][j]+=pcm[i][j]*window[j];
284 /* write data. Use the part of buffer we're about to shift out */
286 char *ptr=buffer+i*2;
288 for(j=0;j<framesize/2;j++){
289 int val=mono[j]*32767.;
290 /* might as well guard against clipping */
291 if(val>32767)val=32767;
292 if(val<-32768)val=-32768;
294 ptr[1]=(val>>8)&0xff;
299 fwrite(buffer,1,framesize*2,stdout);
300 memmove(buffer,buffer2,framesize*2);
303 for(j=0,k=framesize/2;j<framesize/2;j++,k++)
304 out[i][j]=pcm[i][k]*window[k];
309 fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
310 fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
312 fprintf(stderr,"Done\n\n");