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 <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
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
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
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
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
00173 }
00174
00175 m_sigNewDataReady();
00176 }
00177 }
00178 closeSound();
00179 }
00180 }