Imported Upstream version 3.2.2
[debian/gnuradio] / gr-audio-alsa / src / audio_alsa_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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_SINK_H
24 #define INCLUDED_AUDIO_ALSA_SINK_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
36 class audio_alsa_sink;
37 typedef boost::shared_ptr<audio_alsa_sink> audio_alsa_sink_sptr;
38
39 /*!
40  * \brief make an alsa audio sink.
41  *
42  * \param sampling_rate sampling rate in Hz
43  * \param device_name ALSA pcm device name, e.g., "hw:0,0"
44  * \param ok_to_block (currently ignored)
45  */
46 audio_alsa_sink_sptr
47 audio_alsa_make_sink (int sampling_rate,
48                       const std::string device_name = "",
49                       bool ok_to_block = true);
50
51 /*!
52  * \brief audio sink using ALSA
53  *
54  * The sink has N input streams of floats, where N depends
55  * on the hardware characteristics of the selected device.
56  *
57  * Input samples must be in the range [-1,1].
58  */
59 class audio_alsa_sink : public gr_sync_block {
60   friend audio_alsa_sink_sptr
61   audio_alsa_make_sink (int sampling_rate, const std::string device_name,
62                         bool ok_to_block);
63
64   // typedef for pointer to class work method
65   typedef int (audio_alsa_sink::*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   bool                  d_special_case_mono_to_stereo;
82
83   // random stats
84   int                   d_nunderuns;            // count of underruns
85   int                   d_nsuspends;            // count of suspends
86
87   void output_error_msg (const char *msg, int err);
88   void bail (const char *msg, int err) throw (std::runtime_error);
89
90  protected:
91   audio_alsa_sink (int sampling_rate, const std::string device_name,
92                    bool ok_to_block);
93
94  public:
95   ~audio_alsa_sink ();
96   
97   bool check_topology (int ninputs, int noutputs);
98
99   int work (int noutput_items,
100             gr_vector_const_void_star &input_items,
101             gr_vector_void_star &output_items);
102
103
104 protected:
105   bool write_buffer (const 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_1x2 (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_1x2 (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_SINK_H */