Minor build fixes, integrate XMMS into the autoconfed stuff
[platform/upstream/libvorbis.git] / lib / envelope.c
1 /********************************************************************
2  *                                                                  *
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.                            *
7  *                                                                  *
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/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: PCM data envelope analysis and manipulation
15  last mod: $Id: envelope.c,v 1.12 1999/12/30 07:26:36 xiphmont Exp $
16
17  Preecho calculation.
18
19  ********************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <math.h>
25
26 #include "os.h"
27 #include "codec.h"
28 #include "mdct.h"
29 #include "envelope.h"
30 #include "bitwise.h"
31 #include "window.h"
32
33 void _ve_envelope_init(envelope_lookup *e,int samples_per){
34   int i;
35
36   e->winlen=samples_per;
37   e->window=malloc(e->winlen*sizeof(double));
38   mdct_init(&e->mdct,e->winlen);
39
40   /* We just use a straight sin(x) window for this */
41   for(i=0;i<e->winlen;i++)
42     e->window[i]=sin((i+.5)/e->winlen*M_PI);
43
44 }
45
46 void _ve_envelope_clear(envelope_lookup *e){
47   if(e->window)free(e->window);
48   mdct_clear(&e->mdct);
49   memset(e,0,sizeof(envelope_lookup));
50 }
51
52 /* use MDCT for spectral power estimation */
53
54 static void _ve_deltas(double *deltas,double *pcm,int n,double *window,
55                        int winsize,mdct_lookup *m){
56   int i,j;
57   double *out=alloca(sizeof(double)*winsize/2);
58   
59   for(j=0;j<n;j++){
60     double acc=0.;
61     
62     mdct_forward(m,pcm+j*winsize,out,window);
63     for(i=winsize/10;i<winsize/2;i++)
64       acc+=fabs(out[i]);
65     if(deltas[j]<acc)deltas[j]=acc;
66   }
67 }
68
69 void _ve_envelope_deltas(vorbis_dsp_state *v){
70   vorbis_info *vi=v->vi;
71   int step=vi->envelopesa;
72   
73   int dtotal=v->pcm_current/vi->envelopesa;
74   int dcurr=v->envelope_current;
75   int pch;
76   
77   if(dtotal>dcurr){
78     double *mult=v->multipliers+dcurr;
79     memset(mult,0,sizeof(double)*(dtotal-dcurr));
80       
81     for(pch=0;pch<vi->channels;pch++){
82       double *pcm=v->pcm[pch]+dcurr*step;
83       _ve_deltas(mult,pcm,dtotal-dcurr,v->ve.window,v->ve.winlen,&v->ve.mdct);
84     }
85     v->envelope_current=dtotal;
86
87 #ifdef ANALYSIS
88     {
89       static int frameno=0;
90       FILE *out;
91       char buffer[80];
92       int j,k;
93       
94       sprintf(buffer,"delt%d.m",frameno);
95       out=fopen(buffer,"w+");
96       for(j=0;j<v->envelope_current;j++)
97         fprintf(out,"%d %g\n",j*vi->envelopesa+vi->envelopesa/2,v->multipliers[j]);
98       fclose(out);
99
100       sprintf(buffer,"deltpcm%d.m",frameno++);
101       out=fopen(buffer,"w+");
102       for(k=0;k<vi->channels;k++){
103         for(j=0;j<v->pcm_current;j++)
104           fprintf(out,"%d %g\n",j,v->pcm[k][j]);
105         fprintf(out,"\n");
106       }
107       fclose(out);
108     }
109 #endif
110
111   }
112 }
113
114
115
116
117
118