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