beatcontroller.cpp
Go to the documentation of this file.
00001 
00002 /****************************************************************
00003  *
00004  * Copyright (c) 2010
00005  *
00006  * Fraunhofer Institute for Manufacturing Engineering   
00007  * and Automation (IPA)
00008  *
00009  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00010  *
00011  * Project name: care-o-bot
00012  * ROS stack name: cob_driver
00013  * ROS package name: cob_light
00014  * Description: Switch robots led color by sending data to
00015  * the led-µC over serial connection.
00016  *                              
00017  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00018  *          
00019  * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00020  * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00021  *
00022  * Date of creation: August 2012
00023  * ToDo:
00024  *
00025  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00026  *
00027  * Redistribution and use in source and binary forms, with or without
00028  * modification, are permitted provided that the following conditions are met:
00029  *
00030  *     * Redistributions of source code must retain the above copyright
00031  *       notice, this list of conditions and the following disclaimer.
00032  *     * Redistributions in binary form must reproduce the above copyright
00033  *       notice, this list of conditions and the following disclaimer in the
00034  *       documentation and/or other materials provided with the distribution.
00035  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00036  *       Engineering and Automation (IPA) nor the names of its
00037  *       contributors may be used to endorse or promote products derived from
00038  *       this software without specific prior written permission.
00039  *
00040  * This program is free software: you can redistribute it and/or modify
00041  * it under the terms of the GNU Lesser General Public License LGPL as 
00042  * published by the Free Software Foundation, either version 3 of the 
00043  * License, or (at your option) any later version.
00044  * 
00045  * This program is distributed in the hope that it will be useful,
00046  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00047  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00048  * GNU Lesser General Public License LGPL for more details.
00049  * 
00050  * You should have received a copy of the GNU Lesser General Public 
00051  * License LGPL along with this program. 
00052  * If not, see <http://www.gnu.org/licenses/>.
00053  *
00054  ****************************************************************/
00055 
00056 #include "beatcontroller.h"
00057 
00058 namespace mybeat
00059 {
00060 
00061 //                                          2048,               44100,              192
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         //Check for a beat for every frequency in our list
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 } //namespace libbeat
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


cob_lightmode
Author(s): Benjamin Maidel
autogenerated on Thu Jan 17 2013 13:39:37