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: random psychoacoustics (not including preecho)
15 last mod: $Id: psy.c,v 1.8 1999/12/31 12:35:16 xiphmont Exp $
17 ********************************************************************/
28 /* Set up decibel threshhold 'curves'. Actually, just set a level at
29 log frequency intervals, interpolate, and call it a curve. */
31 static void set_curve(double *points,
32 double *ref,int rn,double rrate,
33 double *c,int n, double crate){
37 int endpos=points[i+1]*n*rrate/crate;
39 double delta=(ref[i+1]-base)/(endpos-j);
40 for(;j<endpos && j<n;j++){
47 void _vp_psy_init(psy_lookup *p,vorbis_info *vi,int n){
48 memset(p,0,sizeof(psy_lookup));
49 p->noisethresh=malloc(n*sizeof(double));
50 p->maskthresh=malloc(n*sizeof(double));
54 /* set up the curves for a given blocksize and sample rate */
56 set_curve(vi->threshhold_points,
57 vi->noisethresh,THRESH_POINTS,48000,p->noisethresh,n,vi->rate);
58 set_curve(vi->threshhold_points,
59 vi->maskthresh, THRESH_POINTS,48000,p->maskthresh, n,vi->rate);
67 sprintf(buffer,"noise_threshhold_%d.m",n);
68 out=fopen(buffer,"w+");
70 fprintf(out,"%g\n",p->noisethresh[j]);
72 sprintf(buffer,"mask_threshhold_%d.m",n);
73 out=fopen(buffer,"w+");
75 fprintf(out,"%g\n",p->maskthresh[j]);
82 void _vp_psy_clear(psy_lookup *p){
84 if(p->noisethresh)free(p->noisethresh);
85 if(p->maskthresh)free(p->maskthresh);
86 memset(p,0,sizeof(psy_lookup));
90 /* Find the mean log energy of a given 'band'; used to evaluate tones
91 against background noise */
93 /* This is faster than a real convolution, gives us roughly the log f
94 scale we seek, and gives OK results. So, that means it's a good
97 void _vp_noise_floor(psy_lookup *p, double *f, double *m){
99 vorbis_info *vi=p->vi;
106 double bias=n*vi->noisebias;
109 long newlo=i*vi->lnoise-bias;
110 long newhi=i*vi->hnoise+bias;
116 for(j=hi;j<newhi;j++){
120 for(j=lo;j<newlo;j++){
128 /* attenuate by the noise threshhold curve */
129 temp=fromdB(acc/div+p->noisethresh[i]);
130 if(m[i]<temp)m[i]=temp;
134 /* Masking curve: linear rolloff on a dB scale, adjusted by octave,
135 attenuated by maskthresh */
137 void _vp_mask_floor(psy_lookup *p,double *f, double *m){
139 double hroll=p->vi->hroll;
140 double lroll=p->vi->lroll;
141 double ocSCALE=1./log(2);
142 double curmask=-9.e40;
143 double maskbias=n*p->vi->maskbias;
144 double curoc=log(maskbias)*ocSCALE;
147 /* run mask forward then backward */
149 double newmask=todB(f[i])+p->maskthresh[i];
150 double newoc=log(i+maskbias)*ocSCALE;
151 double roll=curmask-(newoc-curoc)*hroll;
154 roll=curmask=newmask;
158 if(m[i]<troll)m[i]=troll;
162 curoc=log(n+maskbias)*ocSCALE;
164 double newmask=todB(f[i])+p->maskthresh[i];
165 double newoc=log(i+maskbias)*ocSCALE;
166 double roll=curmask-(curoc-newoc)*lroll;
169 roll=curmask=newmask;
173 if(m[i]<troll)m[i]=troll;
177 /* s must be padded at the end with m-1 zeroes */
178 static void time_convolve(double *s,double *r,int n,int m){
186 acc+=s[i+j]*r[m-j-1];