]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/beat_detect.c
Simplify configure for win32
[vlc] / modules / visualization / galaktos / beat_detect.c
1 /*****************************************************************************
2  * beat_detect.c: basic beat detection algorithm
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          code from projectM http://xmms-projectm.sourceforge.net
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 //
26 //by Peter Sperl
27 //
28 //Takes sound data from wherever and returns beat detection values
29 //Uses statistical Energy-Based methods. Very simple
30 //
31 //Some stuff was taken from Frederic Patin's beat-detection article, you'll find it online
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include "engine_vars.h"
36
37 double beat_buffer[32][80],beat_instant[32],beat_history[32];
38 double *beat_val,*beat_att,*beat_variance;
39 int beat_buffer_pos;
40
41 double vol_buffer[80],vol_instant,vol_history;
42
43 void initBeatDetect()
44 {
45
46   int x,y; 
47
48   vol_instant=0;
49   vol_history=0;
50
51   for (y=0;y<80;y++)
52     {
53       vol_buffer[y]=0;
54     }
55
56   beat_buffer_pos=0;
57
58   beat_val=(double *)malloc(32*sizeof(double));
59   beat_att=(double *)malloc(32*sizeof(double));
60   beat_variance=(double *)malloc(32*sizeof(double));
61
62   for (x=0;x<32;x++)
63     {
64       beat_instant[x]=0;
65       beat_history[x]=0;
66       beat_val[x]=1.0;
67       beat_att[x]=1.0;
68       beat_variance[x]=0;
69       for (y=0;y<80;y++)
70         {
71           beat_buffer[x][y]=0;
72         }
73     }
74
75
76
77 void getBeatVals(double *vdataL,double *vdataR, double *vol)
78 {
79   int linear=0;
80   int x,y;
81
82   vol_instant=0;
83
84       for ( x=0;x<16;x++)
85         {
86           
87           beat_instant[x]=0;
88           for ( y=linear*2;y<(linear+8+x)*2;y++)
89             {
90               beat_instant[x]+=((vdataL[y]*vdataL[y])+(vdataR[y]*vdataR[y]))*(1.0/(8+x)); 
91               vol_instant+=((vdataL[y]*vdataL[y])+(vdataR[y]*vdataR[y]))*(1.0/512.0);
92
93             }
94           
95           linear=y/2;
96           beat_history[x]-=(beat_buffer[x][beat_buffer_pos])*.0125;
97           beat_buffer[x][beat_buffer_pos]=beat_instant[x];
98           beat_history[x]+=(beat_instant[x])*.0125;
99           
100           beat_val[x]=(beat_instant[x])/(beat_history[x]);
101           
102           beat_att[x]+=(beat_instant[x])/(beat_history[x]);
103
104
105           
106         }
107       
108       vol_history-=(vol_buffer[beat_buffer_pos])*.0125;
109       vol_buffer[beat_buffer_pos]=vol_instant;
110       vol_history+=(vol_instant)*.0125;
111
112       double temp2=0;
113       mid=0;
114       for(x=1;x<10;x++)
115         {
116          mid+=(beat_instant[x]);
117           temp2+=(beat_history[x]);
118          
119         }
120         
121          mid=mid/(1.5*temp2);
122          temp2=0;
123          treb=0;
124           for(x=10;x<16;x++)
125             { 
126               treb+=(beat_instant[x]);
127               temp2+=(beat_history[x]);
128             }
129           treb=treb/(1.5*temp2);
130           *vol=vol_instant/(1.5*vol_history);
131   
132           bass=(beat_instant[0])/(1.5*beat_history[0]);
133
134           treb_att=.6 * treb_att + .4 * treb;
135           mid_att=.6 * mid_att + .4 * mid;
136           bass_att=.6 * bass_att + .4 * bass;
137           //printf("%f %f %f %f\n",bass,mid,treb,*vol);
138            // *vol=(beat_instant[3])/(beat_history[3]);
139           beat_buffer_pos++;
140           if( beat_buffer_pos>79)beat_buffer_pos=0;
141         
142 }
143 void freeBeatDetect()
144 {
145   free(beat_att);
146   free(beat_val);
147   free(beat_variance);
148 }