Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "beatanalyser.h"
00056 namespace mybeat
00057 {
00058
00059 BeatAnalyser::BeatAnalyser(uint16_t num_bands, uint32_t samplerate, uint16_t recordsize)
00060 : m_maxBandValue(0.0)
00061 {
00062 this->m_bandCount=num_bands;
00063 this->m_sampleRate=samplerate;
00064 this->m_recordSize=recordsize;
00065 m_SubBands = new std::vector<Band*>;
00066 for(uint16_t i=0;i<num_bands;i++)
00067 {
00068
00069 Band *tmp = new Band(4*samplerate/recordsize, pow(0.5,(1/((double)samplerate/recordsize))));
00070 m_SubBands->push_back(tmp);
00071 }
00072
00073 m_beats = new std::vector<bool> (num_bands,false);
00074 }
00075 BeatAnalyser::~BeatAnalyser()
00076 {
00077 delete m_SubBands;
00078 delete m_beats;
00079 }
00080 void BeatAnalyser::processData()
00081 {
00082 uint16_t freq_per_band=m_recordSize/2/m_bandCount/8;
00083
00084 m_beats->assign(m_bandCount, false);
00085 for(uint16_t i=0;i<m_bandCount;i++)
00086 {
00087 double sum=0;
00088 for(uint16_t j=0;j<freq_per_band;j++)
00089 {
00090 sum+=m_FFT->get_magnitude(i*freq_per_band+j);
00091 }
00092 sum/=freq_per_band;
00093 if(sum > m_maxBandValue)
00094 m_maxBandValue = sum;
00095
00096 m_SubBands->at(i)->log(sum);
00097 if(m_SubBands->at(i)->average() < sum && m_SubBands->at(i)->getAllTimeMaximum()*0.8 < sum)
00098 {
00099
00100 (*m_beats)[i] = true;
00101 }
00102 }
00103 }
00104 double BeatAnalyser::getMagSpectrum(uint32_t low, uint32_t high)
00105 {
00106 static double magSpecMax = 0;
00107 double magSpec=0;
00108 uint16_t freq_per_band= m_sampleRate/m_bandCount;
00109 uint32_t low_limit=low/freq_per_band;
00110 uint32_t high_limit=high/freq_per_band;
00111 double avgAllTimeMax = 0;
00112 double avgAverage = 0;
00113
00114 for(uint16_t i=low_limit; i<high_limit;i++)
00115 {
00116 avgAllTimeMax+= m_SubBands->at(i)->getAllTimeMaximumRaw();
00117 if(m_SubBands->at(i)->getNewest() > avgAverage)
00118 avgAverage = m_SubBands->at(i)->getNewest();
00119 }
00120 magSpec = avgAverage / (avgAllTimeMax/(high_limit-low_limit));
00121 if(magSpec > magSpecMax)
00122 magSpecMax = magSpec;
00123 else
00124 magSpecMax *= 0.8;
00125 magSpec = magSpec / magSpecMax;
00126
00127 return magSpec;
00128 }
00129
00130 bool BeatAnalyser::getBeat(uint16_t pos)
00131 {
00132 if(pos < m_bandCount)
00133 return m_beats->at(pos);
00134 else
00135 return false;
00136 }
00137 bool BeatAnalyser::getBeatFrequency(uint32_t frequency)
00138 {
00139 uint16_t freq_per_band=m_sampleRate/m_bandCount;
00140 return m_beats->at((int)frequency/freq_per_band);
00141 }
00142 bool BeatAnalyser::getDrumBeat()
00143 {
00144
00145 bool tmp=false;
00146 uint16_t freq_per_band=m_sampleRate/m_bandCount;
00147 uint32_t low_limit=50/freq_per_band;
00148 uint32_t high_limit=200/freq_per_band;
00149 if(high_limit == 0)
00150 high_limit=1;
00151
00152 for(uint16_t i=low_limit;i<high_limit;i++)
00153 tmp |= m_beats->at(i);
00154 return tmp;
00155 }
00156 bool BeatAnalyser::getSnareBeat()
00157 {
00158
00159 bool tmp=false;
00160 uint16_t freq_per_band=m_sampleRate/m_bandCount;
00161 uint32_t low_limit=1500/freq_per_band;
00162 uint32_t high_limit=2000/freq_per_band;
00163 for(uint16_t i=low_limit;i<high_limit;i++)
00164 tmp |= m_beats->at(i);
00165 return tmp;
00166 }
00167 Band* BeatAnalyser::getBand(uint16_t pos)
00168 {
00169 if(pos < m_bandCount)
00170 return m_SubBands->at(pos);
00171 else
00172 return NULL;
00173 }
00174 }