It's all coming back together slowly. Incremental update.
[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.14 2000/01/22 13:28:18 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 #include "vorbis/codec.h"
26
27 #include "os.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);
58   
59   for(j=0;j<n;j++){
60     double acc=0.;
61  
62     memcpy(out,pcm+j*winsize,winsize);
63     for(i=0;i<winsize;i++)
64       out[i]*=window[i];
65    
66     mdct_forward(m,out,out);
67     for(i=winsize/10;i<winsize/2;i++)
68       acc+=fabs(out[i]);
69     if(deltas[j]<acc)deltas[j]=acc;
70   }
71 }
72
73 void _ve_envelope_deltas(vorbis_dsp_state *v){
74   vorbis_info *vi=v->vi;
75   int step=vi->envelopesa;
76   
77   int dtotal=v->pcm_current/vi->envelopesa;
78   int dcurr=v->envelope_current;
79   int pch;
80   
81   if(dtotal>dcurr){
82     double *mult=v->multipliers+dcurr;
83     memset(mult,0,sizeof(double)*(dtotal-dcurr));
84       
85     for(pch=0;pch<vi->channels;pch++){
86       double *pcm=v->pcm[pch]+dcurr*step;
87       _ve_deltas(mult,pcm,dtotal-dcurr,v->ve.window,v->ve.winlen,&v->ve.mdct);
88     }
89     v->envelope_current=dtotal;
90
91 #ifdef ANALYSIS
92     {
93       static int frameno=0;
94       FILE *out;
95       char buffer[80];
96       int j,k;
97       
98       sprintf(buffer,"delt%d.m",frameno);
99       out=fopen(buffer,"w+");
100       for(j=0;j<v->envelope_current;j++)
101         fprintf(out,"%d %g\n",j*vi->envelopesa+vi->envelopesa/2,v->multipliers[j]);
102       fclose(out);
103
104       sprintf(buffer,"deltpcm%d.m",frameno++);
105       out=fopen(buffer,"w+");
106       for(k=0;k<vi->channels;k++){
107         for(j=0;j<v->pcm_current;j++)
108           fprintf(out,"%d %g\n",j,v->pcm[k][j]);
109         fprintf(out,"\n");
110       }
111       fclose(out);
112     }
113 #endif
114
115   }
116 }
117
118
119
120
121
122