Imported Upstream version 3.2.2
[debian/gnuradio] / gr-audio-alsa / src / audio_alsa_source.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef INCLUDED_AUDIO_ALSA_SOURCE_H
24 #define INCLUDED_AUDIO_ALSA_SOURCE_H
25
26 // use new ALSA API
27 #define ALSA_PCM_NEW_HW_PARAMS_API
28 #define ALSA_PCM_NEW_SW_PARAMS_API
29
30 #include <gr_sync_block.h>
31 #include <string>
32 #include <alsa/asoundlib.h>
33 #include <stdexcept>
34
35 class audio_alsa_source;
36 typedef boost::shared_ptr<audio_alsa_source> audio_alsa_source_sptr;
37
38 /*!
39  * \brief Make an ALSA audio source.
40  *
41  * \param sampling_rate sampling rate
42  * \param device_name ALSA pcm device name, e.g., "hw:0,0"
43  * \param ok_to_block  (currently ignored)
44  */
45 audio_alsa_source_sptr
46 audio_alsa_make_source (int sampling_rate,
47                         const std::string device_name = "",
48                         bool ok_to_block = true);
49
50 /*!
51  * \brief audio source using ALSA
52  *
53  * The source has between 1 and N input streams of floats, where N is
54  * depends on the hardware characteristics of the selected device.
55  *
56  * Output samples will be in the range [-1,1].
57  */
58 class audio_alsa_source : public gr_sync_block {
59   friend audio_alsa_source_sptr
60   audio_alsa_make_source (int sampling_rate,
61                           const std::string device_name,
62                           bool ok_to_block);
63
64   // typedef for pointer to class work method
65   typedef int (audio_alsa_source::*work_t)(int noutput_items,
66                                            gr_vector_const_void_star &input_items,
67                                            gr_vector_void_star &output_items);
68
69   unsigned int          d_sampling_rate;
70   std::string           d_device_name;
71   snd_pcm_t            *d_pcm_handle;
72   snd_pcm_hw_params_t  *d_hw_params;
73   snd_pcm_sw_params_t  *d_sw_params;
74   snd_pcm_format_t      d_format;
75   unsigned int          d_nperiods;
76   unsigned int          d_period_time_us;       // microseconds
77   snd_pcm_uframes_t     d_period_size;          // in frames
78   unsigned int          d_buffer_size_bytes;    // sizeof of d_buffer
79   char                 *d_buffer;
80   work_t                d_worker;               // the work method to use
81   unsigned int          d_hw_nchan;             // # of configured h/w channels
82   bool                  d_special_case_stereo_to_mono;
83
84   // random stats
85   int                   d_noverruns;            // count of overruns
86   int                   d_nsuspends;            // count of suspends
87
88   void output_error_msg (const char *msg, int err);
89   void bail (const char *msg, int err) throw (std::runtime_error);
90
91  protected:
92   audio_alsa_source (int sampling_rate, const std::string device_name,
93                      bool ok_to_block);
94
95  public:
96   ~audio_alsa_source ();
97   
98   bool check_topology (int ninputs, int noutputs);
99
100   int work (int noutput_items,
101             gr_vector_const_void_star &input_items,
102             gr_vector_void_star &output_items);
103
104 protected:
105   bool read_buffer (void *buffer, unsigned nframes, unsigned sizeof_frame);
106
107   int work_s16 (int noutput_items,
108                 gr_vector_const_void_star &input_items,
109                 gr_vector_void_star &output_items);
110
111   int work_s16_2x1 (int noutput_items,
112                     gr_vector_const_void_star &input_items,
113                     gr_vector_void_star &output_items);
114
115   int work_s32 (int noutput_items,
116                 gr_vector_const_void_star &input_items,
117                 gr_vector_void_star &output_items);
118
119   int work_s32_2x1 (int noutput_items,
120                     gr_vector_const_void_star &input_items,
121                     gr_vector_void_star &output_items);
122 };
123
124 #endif /* INCLUDED_AUDIO_ALSA_SOURCE_H */