GNU Radio 3.5.1 C++ API
audio_alsa_source.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004-2011 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_AUDIO_ALSA_SOURCE_H
00024 #define INCLUDED_AUDIO_ALSA_SOURCE_H
00025 
00026 // use new ALSA API
00027 #define ALSA_PCM_NEW_HW_PARAMS_API
00028 #define ALSA_PCM_NEW_SW_PARAMS_API
00029 
00030 #include <gr_audio_source.h>
00031 #include <string>
00032 #include <alsa/asoundlib.h>
00033 #include <stdexcept>
00034 
00035 class audio_alsa_source;
00036 typedef boost::shared_ptr<audio_alsa_source> audio_alsa_source_sptr;
00037 
00038 /*!
00039  * \brief audio source using ALSA
00040  * \ingroup audio_blk
00041  *
00042  * The source has between 1 and N input streams of floats, where N is
00043  * depends on the hardware characteristics of the selected device.
00044  *
00045  * Output samples will be in the range [-1,1].
00046  */
00047 class audio_alsa_source : public audio_source {
00048   // typedef for pointer to class work method
00049   typedef int (audio_alsa_source::*work_t)(int noutput_items,
00050                                            gr_vector_const_void_star &input_items,
00051                                            gr_vector_void_star &output_items);
00052 
00053   unsigned int          d_sampling_rate;
00054   std::string           d_device_name;
00055   snd_pcm_t            *d_pcm_handle;
00056   snd_pcm_hw_params_t  *d_hw_params;
00057   snd_pcm_sw_params_t  *d_sw_params;
00058   snd_pcm_format_t      d_format;
00059   unsigned int          d_nperiods;
00060   unsigned int          d_period_time_us;       // microseconds
00061   snd_pcm_uframes_t     d_period_size;          // in frames
00062   unsigned int          d_buffer_size_bytes;    // sizeof of d_buffer
00063   char                 *d_buffer;
00064   work_t                d_worker;               // the work method to use
00065   unsigned int          d_hw_nchan;             // # of configured h/w channels
00066   bool                  d_special_case_stereo_to_mono;
00067 
00068   // random stats
00069   int                   d_noverruns;            // count of overruns
00070   int                   d_nsuspends;            // count of suspends
00071 
00072   void output_error_msg (const char *msg, int err);
00073   void bail (const char *msg, int err) throw (std::runtime_error);
00074 
00075 public:
00076   audio_alsa_source (int sampling_rate, const std::string device_name,
00077                      bool ok_to_block);
00078 
00079   ~audio_alsa_source ();
00080   
00081   bool check_topology (int ninputs, int noutputs);
00082 
00083   int work (int noutput_items,
00084             gr_vector_const_void_star &input_items,
00085             gr_vector_void_star &output_items);
00086 
00087 protected:
00088   bool read_buffer (void *buffer, unsigned nframes, unsigned sizeof_frame);
00089 
00090   int work_s16 (int noutput_items,
00091                 gr_vector_const_void_star &input_items,
00092                 gr_vector_void_star &output_items);
00093 
00094   int work_s16_2x1 (int noutput_items,
00095                     gr_vector_const_void_star &input_items,
00096                     gr_vector_void_star &output_items);
00097 
00098   int work_s32 (int noutput_items,
00099                 gr_vector_const_void_star &input_items,
00100                 gr_vector_void_star &output_items);
00101 
00102   int work_s32_2x1 (int noutput_items,
00103                     gr_vector_const_void_star &input_items,
00104                     gr_vector_void_star &output_items);
00105 };
00106 
00107 #endif /* INCLUDED_AUDIO_ALSA_SOURCE_H */