3 * Copyright 2004,2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
23 #ifndef INCLUDED_AUDIO_ALSA_SOURCE_H
24 #define INCLUDED_AUDIO_ALSA_SOURCE_H
27 #define ALSA_PCM_NEW_HW_PARAMS_API
28 #define ALSA_PCM_NEW_SW_PARAMS_API
30 #include <gr_sync_block.h>
32 #include <alsa/asoundlib.h>
35 class audio_alsa_source;
36 typedef boost::shared_ptr<audio_alsa_source> audio_alsa_source_sptr;
39 * \brief Make an ALSA audio source.
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)
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);
51 * \brief audio source using ALSA
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.
56 * Output samples will be in the range [-1,1].
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,
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);
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
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;
85 int d_noverruns; // count of overruns
86 int d_nsuspends; // count of suspends
88 void output_error_msg (const char *msg, int err);
89 void bail (const char *msg, int err) throw (std::runtime_error);
92 audio_alsa_source (int sampling_rate, const std::string device_name,
96 ~audio_alsa_source ();
98 bool check_topology (int ninputs, int noutputs);
100 int work (int noutput_items,
101 gr_vector_const_void_star &input_items,
102 gr_vector_void_star &output_items);
105 bool read_buffer (void *buffer, unsigned nframes, unsigned sizeof_frame);
107 int work_s16 (int noutput_items,
108 gr_vector_const_void_star &input_items,
109 gr_vector_void_star &output_items);
111 int work_s16_2x1 (int noutput_items,
112 gr_vector_const_void_star &input_items,
113 gr_vector_void_star &output_items);
115 int work_s32 (int noutput_items,
116 gr_vector_const_void_star &input_items,
117 gr_vector_void_star &output_items);
119 int work_s32_2x1 (int noutput_items,
120 gr_vector_const_void_star &input_items,
121 gr_vector_void_star &output_items);
124 #endif /* INCLUDED_AUDIO_ALSA_SOURCE_H */