alsarecorder.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 <alsarecorder.h>
00056 
00057 namespace mybeat
00058 {
00059 
00060 AlsaRecorder::AlsaRecorder(uint32_t samplerate, uint8_t channels, std::vector<SoundBuffer*> mySoundBuffers,uint16_t recordsize)
00061  :m_started(false)
00062 {
00063     this->m_sampleSize=samplerate;
00064     this->m_SoundBuffers=mySoundBuffers;
00065     this->m_channels=channels;
00066     this->m_recordSize=recordsize;
00067     m_captureEnabled=false;
00068     m_signal = new int16_t[recordsize*channels];
00069 }
00070 
00071 AlsaRecorder::~AlsaRecorder()
00072 {
00073     delete[] m_signal;
00074     m_captureEnabled=false;
00075     //Wait until run(); has finished
00076     this->join();
00077 }
00078 void AlsaRecorder::stop()
00079 {
00080     m_captureEnabled=false;
00081 }
00082 bool AlsaRecorder::initSound()
00083 {
00084     snd_pcm_hw_params_t *hw_params;
00085     char pcm_name[]="default";
00086     //char pcm_name[]="plughw:0,0";
00087     int err;
00088 
00089 
00090     if ((err = snd_pcm_open (&m_captureHandle, pcm_name, SND_PCM_STREAM_CAPTURE, 0)) < 0)
00091     {
00092         printf("cannot open audio device (%s)\n",
00093                  snd_strerror (err));
00094         return false;
00095     }
00096     if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
00097     {
00098         printf("cannot allocate hardware parameter structure (%s)\n",
00099                  snd_strerror (err));
00100         return false;
00101     }
00102     if ((err = snd_pcm_hw_params_any (m_captureHandle, hw_params)) < 0)
00103     {
00104         printf("cannot initialize hardware parameter structure (%s)\n",
00105                  snd_strerror (err));
00106         return false;
00107     }
00108     if ((err = snd_pcm_hw_params_set_access (m_captureHandle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
00109     {
00110         printf("cannot set access type (%s)\n",
00111                  snd_strerror (err));
00112         return false;
00113     }
00114     if ((err = snd_pcm_hw_params_set_format (m_captureHandle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
00115     {
00116         printf("cannot set sample format (%s)\n",
00117                  snd_strerror (err));
00118         return false;
00119     }
00120     if ((err = snd_pcm_hw_params_set_rate_near (m_captureHandle, hw_params, (unsigned int *) &m_sampleSize, 0)) < 0)
00121     {
00122         printf("cannot set sample rate (%s)\n",
00123                  snd_strerror (err));
00124         return false;
00125     }
00126     if ((err = snd_pcm_hw_params_set_channels (m_captureHandle, hw_params, m_channels)) < 0)
00127     {
00128         printf("cannot set channel count (%s)\n",
00129                  snd_strerror (err));
00130         return false;
00131     }
00132     if ((err = snd_pcm_hw_params (m_captureHandle, hw_params)) < 0)
00133     {
00134         printf("cannot set parameters (%s)\n",
00135                  snd_strerror (err));
00136         return false;
00137     }
00138     snd_pcm_hw_params_free (hw_params);
00139     return true;
00140 }
00141 void AlsaRecorder::closeSound()
00142 {
00143     snd_pcm_close (m_captureHandle);
00144 }
00145 void AlsaRecorder::run()
00146 {
00147     int err;
00148     //stop will set this to false
00149     m_captureEnabled=true;
00150     if(initSound())
00151     {
00152         m_started = true;
00153         while(m_captureEnabled)
00154         {
00155             if ((err = snd_pcm_readi (m_captureHandle, m_signal, m_recordSize)) != m_recordSize)
00156             {
00157                 if ((err = snd_pcm_prepare (m_captureHandle)) < 0)
00158                 {
00159                     printf("cannot prepare audio interface for use (%s)\n",
00160                              snd_strerror (err));
00161 
00162                 }
00163             }
00164             //Write data to Buffer
00165             for(uint16_t i=0;i<m_recordSize*m_channels;i+=m_channels)
00166             {
00167                 int32_t sum=0;
00168 
00169                 for(uint8_t j=0;j<m_channels;j++)
00170                     m_SoundBuffers.at(j)->write(i/m_channels,(int16_t)m_signal[i+j]);
00171 
00172                 //m_SoundBuffer->write(i/m_channels,(int16_t)sum/m_channels);
00173             }
00174             //Emit signal and notify connected modules that new data is ready for processing
00175             m_sigNewDataReady();
00176         }
00177     }
00178     closeSound();
00179 }
00180 } //namespace mybeat
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


cob_lightmode
Author(s): Benjamin Maidel
autogenerated on Thu Jan 17 2013 12:16:04