GNU Radio 3.6.5.1 C++ API
audio_alsa_sink.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004-2011 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_audio_sink.h>
31 #include <string>
32 #include <alsa/asoundlib.h>
33 #include <stdexcept>
34 
35 /*!
36  * \brief audio sink using ALSA
37  * \ingroup audio_blk
38  *
39  * The sink has N input streams of floats, where N depends
40  * on the hardware characteristics of the selected device.
41  *
42  * Input samples must be in the range [-1,1].
43  */
44 class audio_alsa_sink : public audio_sink {
45  // typedef for pointer to class work method
46  typedef int (audio_alsa_sink::*work_t)(int noutput_items,
47  gr_vector_const_void_star &input_items,
48  gr_vector_void_star &output_items);
49 
50  unsigned int d_sampling_rate;
51  std::string d_device_name;
52  snd_pcm_t *d_pcm_handle;
53  snd_pcm_hw_params_t *d_hw_params;
54  snd_pcm_sw_params_t *d_sw_params;
55  snd_pcm_format_t d_format;
56  unsigned int d_nperiods;
57  unsigned int d_period_time_us; // microseconds
58  snd_pcm_uframes_t d_period_size; // in frames
59  unsigned int d_buffer_size_bytes; // sizeof of d_buffer
60  char *d_buffer;
61  work_t d_worker; // the work method to use
62  bool d_special_case_mono_to_stereo;
63 
64  // random stats
65  int d_nunderuns; // count of underruns
66  int d_nsuspends; // count of suspends
67  bool d_ok_to_block; // defaults to "true", controls blocking/non-block I/O
68 
69  void output_error_msg (const char *msg, int err);
70  void bail (const char *msg, int err) throw (std::runtime_error);
71 
72 public:
73  audio_alsa_sink (int sampling_rate, const std::string device_name,
74  bool ok_to_block);
75 
77 
78  bool check_topology (int ninputs, int noutputs);
79 
80  int work (int noutput_items,
81  gr_vector_const_void_star &input_items,
82  gr_vector_void_star &output_items);
83 
84 
85 protected:
86  bool write_buffer (const void *buffer, unsigned nframes, unsigned sizeof_frame);
87 
88  int work_s16 (int noutput_items,
89  gr_vector_const_void_star &input_items,
90  gr_vector_void_star &output_items);
91 
92  int work_s16_1x2 (int noutput_items,
93  gr_vector_const_void_star &input_items,
94  gr_vector_void_star &output_items);
95 
96  int work_s32 (int noutput_items,
97  gr_vector_const_void_star &input_items,
98  gr_vector_void_star &output_items);
99 
100  int work_s32_1x2 (int noutput_items,
101  gr_vector_const_void_star &input_items,
102  gr_vector_void_star &output_items);
103 };
104 
105 #endif /* INCLUDED_AUDIO_ALSA_SINK_H */