3 * Copyright 2004,2006,2010 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.
27 #include <audio_alsa_source.h>
28 #include <gr_io_signature.h>
36 static bool CHATTY_DEBUG = false;
38 static snd_pcm_format_t acceptable_formats[] = {
39 // these are in our preferred order...
44 #define NELEMS(x) (sizeof(x)/sizeof(x[0]))
48 default_device_name ()
50 return gr_prefs::singleton()->get_string("audio_alsa", "default_input_device", "hw:0,0");
54 default_period_time ()
56 return std::max(0.001, gr_prefs::singleton()->get_double("audio_alsa", "period_time", 0.010));
62 return std::max(2L, gr_prefs::singleton()->get_long("audio_alsa", "nperiods", 4));
65 // ----------------------------------------------------------------
67 audio_alsa_source_sptr
68 audio_alsa_make_source (int sampling_rate, const std::string dev,
71 return gnuradio::get_initial_sptr(new audio_alsa_source (sampling_rate, dev,
75 audio_alsa_source::audio_alsa_source (int sampling_rate,
76 const std::string device_name,
78 : gr_sync_block ("audio_alsa_source",
79 gr_make_io_signature (0, 0, 0),
80 gr_make_io_signature (0, 0, 0)),
81 d_sampling_rate (sampling_rate),
82 d_device_name (device_name.empty() ? default_device_name() : device_name),
84 d_hw_params ((snd_pcm_hw_params_t *)(new char[snd_pcm_hw_params_sizeof()])),
85 d_sw_params ((snd_pcm_sw_params_t *)(new char[snd_pcm_sw_params_sizeof()])),
86 d_nperiods (default_nperiods()),
87 d_period_time_us ((unsigned int) (default_period_time() * 1e6)),
89 d_buffer_size_bytes (0), d_buffer (0),
90 d_worker (0), d_hw_nchan (0),
91 d_special_case_stereo_to_mono (false),
92 d_noverruns (0), d_nsuspends (0)
95 CHATTY_DEBUG = gr_prefs::singleton()->get_bool("audio_alsa", "verbose", false);
100 // open the device for capture
101 error = snd_pcm_open(&d_pcm_handle, d_device_name.c_str (),
102 SND_PCM_STREAM_CAPTURE, 0);
104 fprintf (stderr, "audio_alsa_source[%s]: %s\n",
105 d_device_name.c_str(), snd_strerror(error));
106 throw std::runtime_error ("audio_alsa_source");
109 // Fill params with a full configuration space for a PCM.
110 error = snd_pcm_hw_params_any(d_pcm_handle, d_hw_params);
112 bail ("broken configuration for playback", error);
115 gri_alsa_dump_hw_params (d_pcm_handle, d_hw_params, stdout);
117 // now that we know how many channels the h/w can handle, set output signature
118 unsigned int umax_chan;
119 unsigned int umin_chan;
120 snd_pcm_hw_params_get_channels_min (d_hw_params, &umin_chan);
121 snd_pcm_hw_params_get_channels_max (d_hw_params, &umax_chan);
122 int min_chan = std::min (umin_chan, 1000U);
123 int max_chan = std::min (umax_chan, 1000U);
125 // As a special case, if the hw's min_chan is two, we'll accept
126 // a single output and handle the demux ourselves.
130 d_special_case_stereo_to_mono = true;
133 set_output_signature (gr_make_io_signature (min_chan, max_chan,
136 // fill in portions of the d_hw_params that we know now...
138 // Specify the access methods we implement
139 // For now, we only handle RW_INTERLEAVED...
140 snd_pcm_access_mask_t *access_mask;
141 snd_pcm_access_mask_t **access_mask_ptr = &access_mask; // FIXME: workaround for compiler warning
142 snd_pcm_access_mask_alloca (access_mask_ptr);
143 snd_pcm_access_mask_none (access_mask);
144 snd_pcm_access_mask_set (access_mask, SND_PCM_ACCESS_RW_INTERLEAVED);
145 // snd_pcm_access_mask_set (access_mask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
147 if ((error = snd_pcm_hw_params_set_access_mask (d_pcm_handle,
148 d_hw_params, access_mask)) < 0)
149 bail ("failed to set access mask", error);
153 if (!gri_alsa_pick_acceptable_format (d_pcm_handle, d_hw_params,
155 NELEMS (acceptable_formats),
159 throw std::runtime_error ("audio_alsa_source");
163 unsigned int orig_sampling_rate = d_sampling_rate;
164 if ((error = snd_pcm_hw_params_set_rate_near (d_pcm_handle, d_hw_params,
165 &d_sampling_rate, 0)) < 0)
166 bail ("failed to set rate near", error);
168 if (orig_sampling_rate != d_sampling_rate){
169 fprintf (stderr, "audio_alsa_source[%s]: unable to support sampling rate %d\n",
170 snd_pcm_name (d_pcm_handle), orig_sampling_rate);
171 fprintf (stderr, " card requested %d instead.\n", d_sampling_rate);
175 * ALSA transfers data in units of "periods".
176 * We indirectly determine the underlying buffersize by specifying
177 * the number of periods we want (typically 4) and the length of each
178 * period in units of time (typically 1ms).
180 unsigned int min_nperiods, max_nperiods;
181 snd_pcm_hw_params_get_periods_min (d_hw_params, &min_nperiods, &dir);
182 snd_pcm_hw_params_get_periods_max (d_hw_params, &max_nperiods, &dir);
183 //fprintf (stderr, "alsa_source: min_nperiods = %d, max_nperiods = %d\n",
184 // min_nperiods, max_nperiods);
187 unsigned int orig_nperiods = d_nperiods;
188 d_nperiods = std::min (std::max (min_nperiods, d_nperiods), max_nperiods);
190 // adjust period time so that total buffering remains more-or-less constant
191 d_period_time_us = (d_period_time_us * orig_nperiods) / d_nperiods;
193 error = snd_pcm_hw_params_set_periods (d_pcm_handle, d_hw_params,
196 bail ("set_periods failed", error);
199 error = snd_pcm_hw_params_set_period_time_near (d_pcm_handle, d_hw_params,
200 &d_period_time_us, &dir);
202 bail ("set_period_time_near failed", error);
205 error = snd_pcm_hw_params_get_period_size (d_hw_params,
206 &d_period_size, &dir);
208 bail ("get_period_size failed", error);
210 set_output_multiple (d_period_size);
214 audio_alsa_source::check_topology (int ninputs, int noutputs)
216 // noutputs is how many channels the user has connected.
217 // Now we can finish up setting up the hw params...
219 unsigned int nchan = noutputs;
222 // FIXME check_topology may be called more than once.
223 // Ensure that the pcm is in a state where we can still mess with the hw_params
225 bool special_case = nchan == 1 && d_special_case_stereo_to_mono;
230 err = snd_pcm_hw_params_set_channels (d_pcm_handle, d_hw_params, d_hw_nchan);
232 output_error_msg ("set_channels failed", err);
236 // set the parameters into the driver...
237 err = snd_pcm_hw_params(d_pcm_handle, d_hw_params);
239 output_error_msg ("snd_pcm_hw_params failed", err);
243 d_buffer_size_bytes =
244 d_period_size * d_hw_nchan * snd_pcm_format_size (d_format, 1);
246 d_buffer = new char [d_buffer_size_bytes];
249 fprintf (stdout, "audio_alsa_source[%s]: sample resolution = %d bits\n",
250 snd_pcm_name (d_pcm_handle),
251 snd_pcm_hw_params_get_sbits (d_hw_params));
254 case SND_PCM_FORMAT_S16:
256 d_worker = &audio_alsa_source::work_s16_2x1;
258 d_worker = &audio_alsa_source::work_s16;
261 case SND_PCM_FORMAT_S32:
263 d_worker = &audio_alsa_source::work_s32_2x1;
265 d_worker = &audio_alsa_source::work_s32;
275 audio_alsa_source::~audio_alsa_source ()
277 if (snd_pcm_state (d_pcm_handle) == SND_PCM_STATE_RUNNING)
278 snd_pcm_drop (d_pcm_handle);
280 snd_pcm_close(d_pcm_handle);
281 delete [] ((char *) d_hw_params);
282 delete [] ((char *) d_sw_params);
287 audio_alsa_source::work (int noutput_items,
288 gr_vector_const_void_star &input_items,
289 gr_vector_void_star &output_items)
291 assert ((noutput_items % d_period_size) == 0);
292 assert (noutput_items != 0);
294 // this is a call through a pointer to a method...
295 return (this->*d_worker)(noutput_items, input_items, output_items);
299 * Work function that deals with float to S16 conversion
302 audio_alsa_source::work_s16 (int noutput_items,
303 gr_vector_const_void_star &input_items,
304 gr_vector_void_star &output_items)
306 typedef gr_int16 sample_t; // the type of samples we're creating
307 static const int NBITS = 16; // # of bits in a sample
309 unsigned int nchan = output_items.size ();
310 float **out = (float **) &output_items[0];
311 sample_t *buf = (sample_t *) d_buffer;
314 unsigned int sizeof_frame = d_hw_nchan * sizeof (sample_t);
315 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
317 // To minimize latency, return at most a single period's worth of samples.
318 // [We could also read the first one in a blocking mode and subsequent
319 // ones in non-blocking mode, but we'll leave that for later (or never).]
321 if (!read_buffer (buf, d_period_size, sizeof_frame))
322 return -1; // No fixing this problem. Say we're done.
324 // process one period of data
326 for (unsigned int i = 0; i < d_period_size; i++){
327 for (unsigned int chan = 0; chan < nchan; chan++){
328 out[chan][i] = (float) buf[bi++] * (1.0 / (float) ((1L << (NBITS-1)) - 1));
332 return d_period_size;
336 * Work function that deals with float to S16 conversion
337 * and stereo to mono kludge...
340 audio_alsa_source::work_s16_2x1 (int noutput_items,
341 gr_vector_const_void_star &input_items,
342 gr_vector_void_star &output_items)
344 typedef gr_int16 sample_t; // the type of samples we're creating
345 static const int NBITS = 16; // # of bits in a sample
347 unsigned int nchan = output_items.size ();
348 float **out = (float **) &output_items[0];
349 sample_t *buf = (sample_t *) d_buffer;
354 unsigned int sizeof_frame = d_hw_nchan * sizeof (sample_t);
355 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
357 // To minimize latency, return at most a single period's worth of samples.
358 // [We could also read the first one in a blocking mode and subsequent
359 // ones in non-blocking mode, but we'll leave that for later (or never).]
361 if (!read_buffer (buf, d_period_size, sizeof_frame))
362 return -1; // No fixing this problem. Say we're done.
364 // process one period of data
366 for (unsigned int i = 0; i < d_period_size; i++){
367 int t = (buf[bi] + buf[bi+1]) / 2;
369 out[0][i] = (float) t * (1.0 / (float) ((1L << (NBITS-1)) - 1));
372 return d_period_size;
376 * Work function that deals with float to S32 conversion
379 audio_alsa_source::work_s32 (int noutput_items,
380 gr_vector_const_void_star &input_items,
381 gr_vector_void_star &output_items)
383 typedef gr_int32 sample_t; // the type of samples we're creating
384 static const int NBITS = 32; // # of bits in a sample
386 unsigned int nchan = output_items.size ();
387 float **out = (float **) &output_items[0];
388 sample_t *buf = (sample_t *) d_buffer;
391 unsigned int sizeof_frame = d_hw_nchan * sizeof (sample_t);
392 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
394 // To minimize latency, return at most a single period's worth of samples.
395 // [We could also read the first one in a blocking mode and subsequent
396 // ones in non-blocking mode, but we'll leave that for later (or never).]
398 if (!read_buffer (buf, d_period_size, sizeof_frame))
399 return -1; // No fixing this problem. Say we're done.
401 // process one period of data
403 for (unsigned int i = 0; i < d_period_size; i++){
404 for (unsigned int chan = 0; chan < nchan; chan++){
405 out[chan][i] = (float) buf[bi++] * (1.0 / (float) ((1L << (NBITS-1)) - 1));
409 return d_period_size;
413 * Work function that deals with float to S32 conversion
414 * and stereo to mono kludge...
417 audio_alsa_source::work_s32_2x1 (int noutput_items,
418 gr_vector_const_void_star &input_items,
419 gr_vector_void_star &output_items)
421 typedef gr_int32 sample_t; // the type of samples we're creating
422 static const int NBITS = 32; // # of bits in a sample
424 unsigned int nchan = output_items.size ();
425 float **out = (float **) &output_items[0];
426 sample_t *buf = (sample_t *) d_buffer;
431 unsigned int sizeof_frame = d_hw_nchan * sizeof (sample_t);
432 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
434 // To minimize latency, return at most a single period's worth of samples.
435 // [We could also read the first one in a blocking mode and subsequent
436 // ones in non-blocking mode, but we'll leave that for later (or never).]
438 if (!read_buffer (buf, d_period_size, sizeof_frame))
439 return -1; // No fixing this problem. Say we're done.
441 // process one period of data
443 for (unsigned int i = 0; i < d_period_size; i++){
444 int t = (buf[bi] + buf[bi+1]) / 2;
446 out[0][i] = (float) t * (1.0 / (float) ((1L << (NBITS-1)) - 1));
449 return d_period_size;
453 audio_alsa_source::read_buffer (void *vbuffer, unsigned nframes, unsigned sizeof_frame)
455 unsigned char *buffer = (unsigned char *) vbuffer;
458 int r = snd_pcm_readi (d_pcm_handle, buffer, nframes);
460 continue; // try again
462 else if (r == -EPIPE){ // overrun
464 fputs ("aO", stderr);
465 if ((r = snd_pcm_prepare (d_pcm_handle)) < 0){
466 output_error_msg ("snd_pcm_prepare failed. Can't recover from overrun", r);
469 continue; // try again
472 else if (r == -ESTRPIPE){ // h/w is suspended (whatever that means)
473 // This is apparently related to power management
475 if ((r = snd_pcm_resume (d_pcm_handle)) < 0){
476 output_error_msg ("failed to resume from suspend", r);
479 continue; // try again
483 output_error_msg ("snd_pcm_readi failed", r);
488 buffer += r * sizeof_frame;
496 audio_alsa_source::output_error_msg (const char *msg, int err)
498 fprintf (stderr, "audio_alsa_source[%s]: %s: %s\n",
499 snd_pcm_name (d_pcm_handle), msg, snd_strerror (err));
503 audio_alsa_source::bail (const char *msg, int err) throw (std::runtime_error)
505 output_error_msg (msg, err);
506 throw std::runtime_error ("audio_alsa_source");