GNU Radio 3.5.3.2 C++ API
audio_alsa_source.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_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_audio_source.h>
31 #include <string>
32 #include <alsa/asoundlib.h>
33 #include <stdexcept>
34 
35 class audio_alsa_source;
37 
38 /*!
39  * \brief audio source using ALSA
40  * \ingroup audio_blk
41  *
42  * The source has between 1 and N input streams of floats, where N is
43  * depends on the hardware characteristics of the selected device.
44  *
45  * Output samples will be in the range [-1,1].
46  */
48  // typedef for pointer to class work method
49  typedef int (audio_alsa_source::*work_t)(int noutput_items,
50  gr_vector_const_void_star &input_items,
51  gr_vector_void_star &output_items);
52 
53  unsigned int d_sampling_rate;
54  std::string d_device_name;
55  snd_pcm_t *d_pcm_handle;
56  snd_pcm_hw_params_t *d_hw_params;
57  snd_pcm_sw_params_t *d_sw_params;
58  snd_pcm_format_t d_format;
59  unsigned int d_nperiods;
60  unsigned int d_period_time_us; // microseconds
61  snd_pcm_uframes_t d_period_size; // in frames
62  unsigned int d_buffer_size_bytes; // sizeof of d_buffer
63  char *d_buffer;
64  work_t d_worker; // the work method to use
65  unsigned int d_hw_nchan; // # of configured h/w channels
66  bool d_special_case_stereo_to_mono;
67 
68  // random stats
69  int d_noverruns; // count of overruns
70  int d_nsuspends; // count of suspends
71 
72  void output_error_msg (const char *msg, int err);
73  void bail (const char *msg, int err) throw (std::runtime_error);
74 
75 public:
76  audio_alsa_source (int sampling_rate, const std::string device_name,
77  bool ok_to_block);
78 
80 
81  bool check_topology (int ninputs, int noutputs);
82 
83  int work (int noutput_items,
84  gr_vector_const_void_star &input_items,
85  gr_vector_void_star &output_items);
86 
87 protected:
88  bool read_buffer (void *buffer, unsigned nframes, unsigned sizeof_frame);
89 
90  int work_s16 (int noutput_items,
91  gr_vector_const_void_star &input_items,
92  gr_vector_void_star &output_items);
93 
94  int work_s16_2x1 (int noutput_items,
95  gr_vector_const_void_star &input_items,
96  gr_vector_void_star &output_items);
97 
98  int work_s32 (int noutput_items,
99  gr_vector_const_void_star &input_items,
100  gr_vector_void_star &output_items);
101 
102  int work_s32_2x1 (int noutput_items,
103  gr_vector_const_void_star &input_items,
104  gr_vector_void_star &output_items);
105 };
106 
107 #endif /* INCLUDED_AUDIO_ALSA_SOURCE_H */