pulserecorder.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering   
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_driver
00012  * ROS package name: cob_light
00013  * Description: Switch robots led color by sending data to
00014  * the led-µC over serial connection.
00015  *                              
00016  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00017  *          
00018  * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00019  * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00020  *
00021  * Date of creation: August 2012
00022  * ToDo:
00023  *
00024  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00025  *
00026  * Redistribution and use in source and binary forms, with or without
00027  * modification, are permitted provided that the following conditions are met:
00028  *
00029  *     * Redistributions of source code must retain the above copyright
00030  *       notice, this list of conditions and the following disclaimer.
00031  *     * Redistributions in binary form must reproduce the above copyright
00032  *       notice, this list of conditions and the following disclaimer in the
00033  *       documentation and/or other materials provided with the distribution.
00034  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00035  *       Engineering and Automation (IPA) nor the names of its
00036  *       contributors may be used to endorse or promote products derived from
00037  *       this software without specific prior written permission.
00038  *
00039  * This program is free software: you can redistribute it and/or modify
00040  * it under the terms of the GNU Lesser General Public License LGPL as 
00041  * published by the Free Software Foundation, either version 3 of the 
00042  * License, or (at your option) any later version.
00043  * 
00044  * This program is distributed in the hope that it will be useful,
00045  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00046  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00047  * GNU Lesser General Public License LGPL for more details.
00048  * 
00049  * You should have received a copy of the GNU Lesser General Public 
00050  * License LGPL along with this program. 
00051  * If not, see <http://www.gnu.org/licenses/>.
00052  *
00053  ****************************************************************/
00054 
00055 #include "pulserecorder.h"
00056 #include <stdio.h>
00057 namespace mybeat
00058 {
00059 
00060 
00061 PulseRecorder::PulseRecorder(uint32_t samplerate,uint8_t channels, std::vector<SoundBuffer*> mySoundBuffers,uint16_t recordsize)
00062  :m_started(false)
00063 {
00064     this->m_sampleRate=samplerate;
00065     this->m_SoundBuffers=mySoundBuffers;
00066     this->m_channels=channels;
00067     this->m_recordSize=recordsize;
00068     m_captureEnabled=false;
00069     m_signal = new int16_t[recordsize*channels];
00070 }
00071 PulseRecorder::~PulseRecorder()
00072 {
00073     delete[] m_signal;
00074     m_captureEnabled=false;
00075     //Wait until run(); has finished
00076     wait();
00077 }
00078 void PulseRecorder::stop()
00079 {
00080     m_captureEnabled=false;
00081 }
00082 bool PulseRecorder::initSound()
00083 {
00084     m_ss.format= PA_SAMPLE_S16LE;
00085     m_ss.channels=m_channels;
00086     m_ss.rate=m_sampleRate;
00087     m_s = pa_simple_new(NULL, "mybeat",PA_STREAM_RECORD,NULL,"Sound Processing",&m_ss,NULL,NULL,NULL);
00088     if (m_s != NULL)
00089         return true; //No Error
00090     else
00091         return false; //Error
00092 }
00093 void PulseRecorder::closeSound()
00094 {
00095     pa_simple_free(m_s);
00096 }
00097 void PulseRecorder::run()
00098 {
00099     if(initSound())
00100     {
00101         m_started = true;
00102         m_captureEnabled=true;
00103         int error;
00104         while(m_captureEnabled)
00105         {
00106             if (pa_simple_read(m_s,m_signal,m_recordSize*m_channels*2,&error) < 0)
00107             {
00108                 printf("pa_simple_read() failed: %s\n", pa_strerror(error));
00109                 m_captureEnabled=false;
00110             }
00111             //Write data to Buffer
00112             for(uint16_t i=0;i<m_recordSize*m_channels;i+=m_channels)
00113             {
00114                 int32_t sum=0;
00115                 for(uint8_t j=0;j<m_channels;j++)
00116                     m_SoundBuffers.at(j)->write(i/m_channels,(int16_t)m_signal[i+j]);
00117             }
00118             //Emit signal and notify connected modules that new data is ready for processing
00119             m_sigNewDataReady();
00120         }
00121     }
00122     m_captureEnabled=false;
00123     closeSound();
00124 }
00125 } //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