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 
00056 #include "beatcontroller.h"
00057 
00058 namespace mybeat
00059 {
00060 
00061 
00062 BeatController::BeatController(uint16_t recordSize, uint32_t sampleRate, uint16_t m_bandCount, uint16_t channels)
00063 {
00064     for(int i = 0; i < channels; i++)
00065     {
00066         m_Buffers.push_back(new SoundBuffer(recordSize));
00067         m_FFTs.push_back(new FFT(recordSize));
00068         m_FFTs.back()->setSoundBuffer(m_Buffers.back());
00069         m_Analysers.push_back(new BeatAnalyser(m_bandCount,sampleRate,recordSize));
00070         m_Analysers.back()->setFFT(m_FFTs.back());
00071     }
00072     
00073 #ifdef USE_ALSA
00074     dynamic_cast<AlsaRecorder*>(m_Recorder);
00075     m_Recorder = new AlsaRecorder(sampleRate, channels, m_Buffers, recordSize);
00076 #endif
00077 #ifdef USE_PULSE
00078     dynamic_cast<PulseRecorder*>(m_Recorder);
00079     m_Recorder = new PulseRecorder(sampleRate, channels, m_Buffers, recordSize);
00080 #endif
00081 
00082     m_enabled=false;
00083 }
00084 BeatController::~BeatController()
00085 {
00086     m_Recorder->stop();
00087     m_FFTs.clear();
00088     delete m_Recorder;
00089     m_Buffers.clear();
00090     m_Analysers.clear();
00091 }
00092 void BeatController::start()
00093 {
00094     if(!m_enabled)
00095     {
00096         if(!m_Recorder->isStarted())
00097             m_Recorder->start();
00098         m_enabled=true;
00099         m_connectionProcessingDone = m_Recorder->signalNewDataIsReady()->connect(boost::bind(&BeatController::processNewData,this));
00100     }
00101 }
00102 
00103 void BeatController::stop()
00104 {
00105     if(m_enabled)
00106     {
00107         m_enabled=false;
00108         m_connectionProcessingDone.disconnect();
00109     }
00110 }
00111 bool BeatController::getEnabled()
00112 {
00113     return m_enabled;
00114 }
00115 
00116 void BeatController::processNewData()
00117 {
00118     if(m_enabled)
00119     {
00120         for(int i=0; i<m_FFTs.size(); i++)
00121             m_FFTs.at(i)->process_data();
00122 
00123         for(int i=0; i<m_Analysers.size(); i++)
00124             m_Analysers.at(i)->processData();
00125         m_sigProcessingDone();
00126 
00127         if(m_Analysers.at(0)->getDrumBeat())
00128             m_sigBeatDrum();
00129         if(m_Analysers.at(0)->getSnareBeat())
00130             m_sigBeatSnare();
00131         
00132         std::tr1::unordered_set<uint16_t> myBeats;
00133         
00134         for(std::tr1::unordered_set<uint16_t>::iterator i = m_customBeats.begin();
00135             i != m_customBeats.end(); ++i)
00136         {
00137             if(m_Analysers.at(0)->getBeatFrequency(*i))
00138                 myBeats.insert(*i);
00139         }
00140         
00141         if(!myBeats.empty())
00142             m_sigCustom(myBeats);
00143     }
00144 }
00145 }