Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

beat_detect.c

00001 /*****************************************************************************
00002  * beat_detect.c: basic beat detection algorithm
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: beat_detect.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Cyril Deguet <[email protected]>
00008  *          code from projectM http://xmms-projectm.sourceforge.net
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 //
00026 //by Peter Sperl
00027 //
00028 //Takes sound data from wherever and returns beat detection values
00029 //Uses statistical Energy-Based methods. Very simple
00030 //
00031 //Some stuff was taken from Frederic Patin's beat-detection article, you'll find it online
00032 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include "engine_vars.h"
00036 
00037 double beat_buffer[32][80],beat_instant[32],beat_history[32];
00038 double *beat_val,*beat_att,*beat_variance;
00039 int beat_buffer_pos;
00040 
00041 double vol_buffer[80],vol_instant,vol_history;
00042 
00043 void initBeatDetect()
00044 {
00045 
00046   int x,y; 
00047 
00048   vol_instant=0;
00049   vol_history=0;
00050 
00051   for (y=0;y<80;y++)
00052     {
00053       vol_buffer[y]=0;
00054     }
00055 
00056   beat_buffer_pos=0;
00057 
00058   beat_val=(double *)malloc(32*sizeof(double));
00059   beat_att=(double *)malloc(32*sizeof(double));
00060   beat_variance=(double *)malloc(32*sizeof(double));
00061 
00062   for (x=0;x<32;x++)
00063     {
00064       beat_instant[x]=0;
00065       beat_history[x]=0;
00066       beat_val[x]=1.0;
00067       beat_att[x]=1.0;
00068       beat_variance[x]=0;
00069       for (y=0;y<80;y++)
00070         {
00071           beat_buffer[x][y]=0;
00072         }
00073     }
00074 
00075 } 
00076 
00077 void getBeatVals(double *vdataL,double *vdataR, double *vol)
00078 {
00079   int linear=0;
00080   int x,y;
00081 
00082   vol_instant=0;
00083 
00084       for ( x=0;x<16;x++)
00085         {
00086           
00087           beat_instant[x]=0;
00088           for ( y=linear*2;y<(linear+8+x)*2;y++)
00089             {
00090               beat_instant[x]+=((vdataL[y]*vdataL[y])+(vdataR[y]*vdataR[y]))*(1.0/(8+x)); 
00091               vol_instant+=((vdataL[y]*vdataL[y])+(vdataR[y]*vdataR[y]))*(1.0/512.0);
00092 
00093             }
00094           
00095           linear=y/2;
00096           beat_history[x]-=(beat_buffer[x][beat_buffer_pos])*.0125;
00097           beat_buffer[x][beat_buffer_pos]=beat_instant[x];
00098           beat_history[x]+=(beat_instant[x])*.0125;
00099           
00100           beat_val[x]=(beat_instant[x])/(beat_history[x]);
00101           
00102           beat_att[x]+=(beat_instant[x])/(beat_history[x]);
00103 
00104 
00105           
00106         }
00107       
00108       vol_history-=(vol_buffer[beat_buffer_pos])*.0125;
00109       vol_buffer[beat_buffer_pos]=vol_instant;
00110       vol_history+=(vol_instant)*.0125;
00111 
00112       double temp2=0;
00113       mid=0;
00114       for(x=1;x<10;x++)
00115         {
00116          mid+=(beat_instant[x]);
00117           temp2+=(beat_history[x]);
00118          
00119         }
00120         
00121          mid=mid/(1.5*temp2);
00122          temp2=0;
00123          treb=0;
00124           for(x=10;x<16;x++)
00125             { 
00126               treb+=(beat_instant[x]);
00127               temp2+=(beat_history[x]);
00128             }
00129           treb=treb/(1.5*temp2);
00130           *vol=vol_instant/(1.5*vol_history);
00131   
00132           bass=(beat_instant[0])/(1.5*beat_history[0]);
00133 
00134           treb_att=.6 * treb_att + .4 * treb;
00135           mid_att=.6 * mid_att + .4 * mid;
00136           bass_att=.6 * bass_att + .4 * bass;
00137           //printf("%f %f %f %f\n",bass,mid,treb,*vol);
00138            // *vol=(beat_instant[3])/(beat_history[3]);
00139           beat_buffer_pos++;
00140           if( beat_buffer_pos>79)beat_buffer_pos=0;
00141         
00142 }
00143 void freeBeatDetect()
00144 {
00145   free(beat_att);
00146   free(beat_val);
00147   free(beat_variance);
00148 }

Generated on Tue Dec 20 10:14:56 2005 for vlc-0.8.4a by  doxygen 1.4.2