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: psychoacoustics not including preecho
15 last mod: $Id: psy.c,v 1.17 2000/03/10 13:21:18 xiphmont Exp $
17 ********************************************************************/
23 #include "vorbis/codec.h"
31 /* Set up decibel threshhold slopes on a Bark frequency scale */
33 static void set_curve(double *ref,double *c,int n, double crate){
36 for(i=0;i<MAX_BARK-1;i++){
37 int endpos=rint(fromBARK(i+1)*2*n/crate);
39 double delta=(ref[i+1]-base)/(endpos-j);
40 for(;j<endpos && j<n;j++){
47 void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
49 memset(p,0,sizeof(vorbis_look_psy));
50 p->maskthresh=malloc(n*sizeof(double));
51 p->barknum=malloc(n*sizeof(double));
55 /* set up the lookups for a given blocksize and sample rate */
56 /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
57 set_curve(vi->maskthresh, p->maskthresh, n,rate);
60 p->barknum[i]=toBARK(rate/2.*i/n);
68 sprintf(buffer,"mask_threshhold_%d.m",n);
69 out=fopen(buffer,"w+");
71 fprintf(out,"%g\n",p->maskthresh[j]);
78 void _vp_psy_clear(vorbis_look_psy *p){
80 if(p->maskthresh)free(p->maskthresh);
81 if(p->barknum)free(p->barknum);
82 memset(p,0,sizeof(vorbis_look_psy));
86 /* Masking curve: linear rolloff on a Bark/dB scale, attenuated by
89 void _vp_mask_floor(vorbis_look_psy *p,double *f, double *floor,int attp){
91 double hroll=p->vi->hrolldB;
92 double lroll=p->vi->lrolldB;
93 double curmask=todB(f[0])+(attp?p->maskthresh[0]:0);
97 /* run mask forward then backward */
99 double newmask=todB(f[i])+(attp?p->maskthresh[i]:0);
100 double newoc=p->barknum[i];
101 double roll=curmask-(newoc-curoc)*hroll;
104 roll=curmask=newmask;
108 if(floor[i]<troll)floor[i]=troll;
111 curmask=todB(f[n-1])+(attp?p->maskthresh[n-1]:0);
112 curoc=p->barknum[n-1];
114 double newmask=todB(f[i])+(attp?p->maskthresh[i]:0);
115 double newoc=p->barknum[i];
116 double roll=curmask-(curoc-newoc)*lroll;
119 roll=curmask=newmask;
123 if(floor[i]<troll)floor[i]=troll;
127 /* s must be padded at the end with m-1 zeroes */
128 static void time_convolve(double *s,double *r,int n,int m){
136 acc+=s[i+j]*r[m-j-1];
142 void _vi_psy_free(vorbis_info_psy *i){
144 memset(i,0,sizeof(vorbis_info_psy));