GNU Radio 3.3.0 C++ API
audio_alsa_sink.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004 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_SINK_H
00024 #define INCLUDED_AUDIO_ALSA_SINK_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_sync_block.h>
00031 #include <string>
00032 #include <alsa/asoundlib.h>
00033 #include <stdexcept>
00034 
00035 
00036 class audio_alsa_sink;
00037 typedef boost::shared_ptr<audio_alsa_sink> audio_alsa_sink_sptr;
00038 
00039 /*!
00040  * \brief make an alsa audio sink.
00041  *
00042  * \param sampling_rate sampling rate in Hz
00043  * \param device_name ALSA pcm device name, e.g., "hw:0,0"
00044  * \param ok_to_block (currently ignored)
00045  */
00046 audio_alsa_sink_sptr
00047 audio_alsa_make_sink (int sampling_rate,
00048                       const std::string device_name = "",
00049                       bool ok_to_block = true);
00050 
00051 /*!
00052  * \brief audio sink using ALSA
00053  *
00054  * The sink has N input streams of floats, where N depends
00055  * on the hardware characteristics of the selected device.
00056  *
00057  * Input samples must be in the range [-1,1].
00058  */
00059 class audio_alsa_sink : public gr_sync_block {
00060   friend audio_alsa_sink_sptr
00061   audio_alsa_make_sink (int sampling_rate, const std::string device_name,
00062                         bool ok_to_block);
00063 
00064   // typedef for pointer to class work method
00065   typedef int (audio_alsa_sink::*work_t)(int noutput_items,
00066                                          gr_vector_const_void_star &input_items,
00067                                          gr_vector_void_star &output_items);
00068 
00069   unsigned int          d_sampling_rate;
00070   std::string           d_device_name;
00071   snd_pcm_t            *d_pcm_handle;
00072   snd_pcm_hw_params_t  *d_hw_params;
00073   snd_pcm_sw_params_t  *d_sw_params;
00074   snd_pcm_format_t      d_format;
00075   unsigned int          d_nperiods;
00076   unsigned int          d_period_time_us;       // microseconds
00077   snd_pcm_uframes_t     d_period_size;          // in frames
00078   unsigned int          d_buffer_size_bytes;    // sizeof of d_buffer
00079   char                 *d_buffer;
00080   work_t                d_worker;               // the work method to use
00081   bool                  d_special_case_mono_to_stereo;
00082 
00083   // random stats
00084   int                   d_nunderuns;            // count of underruns
00085   int                   d_nsuspends;            // count of suspends
00086 
00087   void output_error_msg (const char *msg, int err);
00088   void bail (const char *msg, int err) throw (std::runtime_error);
00089 
00090  protected:
00091   audio_alsa_sink (int sampling_rate, const std::string device_name,
00092                    bool ok_to_block);
00093 
00094  public:
00095   ~audio_alsa_sink ();
00096   
00097   bool check_topology (int ninputs, int noutputs);
00098 
00099   int work (int noutput_items,
00100             gr_vector_const_void_star &input_items,
00101             gr_vector_void_star &output_items);
00102 
00103 
00104 protected:
00105   bool write_buffer (const void *buffer, unsigned nframes, unsigned sizeof_frame);
00106 
00107   int work_s16 (int noutput_items,
00108                 gr_vector_const_void_star &input_items,
00109                 gr_vector_void_star &output_items);
00110 
00111   int work_s16_1x2 (int noutput_items,
00112                     gr_vector_const_void_star &input_items,
00113                     gr_vector_void_star &output_items);
00114 
00115   int work_s32 (int noutput_items,
00116                 gr_vector_const_void_star &input_items,
00117                 gr_vector_void_star &output_items);
00118 
00119   int work_s32_1x2 (int noutput_items,
00120                     gr_vector_const_void_star &input_items,
00121                     gr_vector_void_star &output_items);
00122 };
00123 
00124 #endif /* INCLUDED_AUDIO_ALSA_SINK_H */