3 * Copyright 2004,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_sink.h>
28 #include <gr_io_signature.h>
36 static bool CHATTY_DEBUG = false;
39 static snd_pcm_format_t acceptable_formats[] = {
40 // these are in our preferred order...
45 #define NELEMS(x) (sizeof(x)/sizeof(x[0]))
49 default_device_name ()
51 return gr_prefs::singleton()->get_string("audio_alsa", "default_output_device", "hw:0,0");
55 default_period_time ()
57 return std::max(0.001, gr_prefs::singleton()->get_double("audio_alsa", "period_time", 0.010));
63 return std::max(2L, gr_prefs::singleton()->get_long("audio_alsa", "nperiods", 4));
66 // ----------------------------------------------------------------
69 audio_alsa_make_sink (int sampling_rate,
70 const std::string dev,
73 return gnuradio::get_initial_sptr(new audio_alsa_sink (sampling_rate, dev,
77 audio_alsa_sink::audio_alsa_sink (int sampling_rate,
78 const std::string device_name,
80 : gr_sync_block ("audio_alsa_sink",
81 gr_make_io_signature (0, 0, 0),
82 gr_make_io_signature (0, 0, 0)),
83 d_sampling_rate (sampling_rate),
84 d_device_name (device_name.empty() ? default_device_name() : device_name),
86 d_hw_params ((snd_pcm_hw_params_t *)(new char[snd_pcm_hw_params_sizeof()])),
87 d_sw_params ((snd_pcm_sw_params_t *)(new char[snd_pcm_sw_params_sizeof()])),
88 d_nperiods (default_nperiods()),
89 d_period_time_us ((unsigned int) (default_period_time() * 1e6)),
91 d_buffer_size_bytes (0), d_buffer (0),
92 d_worker (0), d_special_case_mono_to_stereo (false),
93 d_nunderuns (0), d_nsuspends (0), d_ok_to_block(ok_to_block)
95 CHATTY_DEBUG = gr_prefs::singleton()->get_bool("audio_alsa", "verbose", false);
100 // open the device for playback
101 error = snd_pcm_open(&d_pcm_handle, d_device_name.c_str (),
102 SND_PCM_STREAM_PLAYBACK, 0);
103 if (ok_to_block == false)
104 snd_pcm_nonblock(d_pcm_handle, !ok_to_block);
106 fprintf (stderr, "audio_alsa_sink[%s]: %s\n",
107 d_device_name.c_str(), snd_strerror(error));
108 throw std::runtime_error ("audio_alsa_sink");
111 // Fill params with a full configuration space for a PCM.
112 error = snd_pcm_hw_params_any(d_pcm_handle, d_hw_params);
114 bail ("broken configuration for playback", error);
118 gri_alsa_dump_hw_params (d_pcm_handle, d_hw_params, stdout);
121 // now that we know how many channels the h/w can handle, set input signature
122 unsigned int umin_chan, umax_chan;
123 snd_pcm_hw_params_get_channels_min (d_hw_params, &umin_chan);
124 snd_pcm_hw_params_get_channels_max (d_hw_params, &umax_chan);
125 int min_chan = std::min (umin_chan, 1000U);
126 int max_chan = std::min (umax_chan, 1000U);
128 // As a special case, if the hw's min_chan is two, we'll accept
129 // a single input and handle the duplication ourselves.
133 d_special_case_mono_to_stereo = true;
135 set_input_signature (gr_make_io_signature (min_chan, max_chan,
138 // fill in portions of the d_hw_params that we know now...
140 // Specify the access methods we implement
141 // For now, we only handle RW_INTERLEAVED...
142 snd_pcm_access_mask_t *access_mask;
143 snd_pcm_access_mask_t **access_mask_ptr = &access_mask; // FIXME: workaround for compiler warning
144 snd_pcm_access_mask_alloca (access_mask_ptr);
145 snd_pcm_access_mask_none (access_mask);
146 snd_pcm_access_mask_set (access_mask, SND_PCM_ACCESS_RW_INTERLEAVED);
147 // snd_pcm_access_mask_set (access_mask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
149 if ((error = snd_pcm_hw_params_set_access_mask (d_pcm_handle,
150 d_hw_params, access_mask)) < 0)
151 bail ("failed to set access mask", error);
155 if (!gri_alsa_pick_acceptable_format (d_pcm_handle, d_hw_params,
157 NELEMS (acceptable_formats),
161 throw std::runtime_error ("audio_alsa_sink");
165 unsigned int orig_sampling_rate = d_sampling_rate;
166 if ((error = snd_pcm_hw_params_set_rate_near (d_pcm_handle, d_hw_params,
167 &d_sampling_rate, 0)) < 0)
168 bail ("failed to set rate near", error);
170 if (orig_sampling_rate != d_sampling_rate){
171 fprintf (stderr, "audio_alsa_sink[%s]: unable to support sampling rate %d\n",
172 snd_pcm_name (d_pcm_handle), orig_sampling_rate);
173 fprintf (stderr, " card requested %d instead.\n", d_sampling_rate);
177 * ALSA transfers data in units of "periods".
178 * We indirectly determine the underlying buffersize by specifying
179 * the number of periods we want (typically 4) and the length of each
180 * period in units of time (typically 1ms).
182 unsigned int min_nperiods, max_nperiods;
183 snd_pcm_hw_params_get_periods_min (d_hw_params, &min_nperiods, &dir);
184 snd_pcm_hw_params_get_periods_max (d_hw_params, &max_nperiods, &dir);
185 //fprintf (stderr, "alsa_sink: min_nperiods = %d, max_nperiods = %d\n",
186 // min_nperiods, max_nperiods);
188 unsigned int orig_nperiods = d_nperiods;
189 d_nperiods = std::min (std::max (min_nperiods, d_nperiods), max_nperiods);
191 // adjust period time so that total buffering remains more-or-less constant
192 d_period_time_us = (d_period_time_us * orig_nperiods) / d_nperiods;
194 error = snd_pcm_hw_params_set_periods (d_pcm_handle, d_hw_params,
197 bail ("set_periods failed", error);
200 error = snd_pcm_hw_params_set_period_time_near (d_pcm_handle, d_hw_params,
201 &d_period_time_us, &dir);
203 bail ("set_period_time_near failed", error);
206 error = snd_pcm_hw_params_get_period_size (d_hw_params,
207 &d_period_size, &dir);
209 bail ("get_period_size failed", error);
211 set_output_multiple (d_period_size);
216 audio_alsa_sink::check_topology (int ninputs, int noutputs)
218 // ninputs is how many channels the user has connected.
219 // Now we can finish up setting up the hw params...
224 // Check the state of the stream
225 // Ensure that the pcm is in a state where we can still mess with the hw_params
226 snd_pcm_state_t state;
227 state=snd_pcm_state(d_pcm_handle);
228 if ( state== SND_PCM_STATE_RUNNING)
229 return true; // If stream is running, don't change any parameters
230 else if(state == SND_PCM_STATE_XRUN )
231 snd_pcm_prepare ( d_pcm_handle ); // Prepare stream on underrun, and we can set parameters;
233 bool special_case = nchan == 1 && d_special_case_mono_to_stereo;
237 err = snd_pcm_hw_params_set_channels (d_pcm_handle, d_hw_params, nchan);
240 output_error_msg ("set_channels failed", err);
244 // set the parameters into the driver...
245 err = snd_pcm_hw_params(d_pcm_handle, d_hw_params);
247 output_error_msg ("snd_pcm_hw_params failed", err);
251 // get current s/w params
252 err = snd_pcm_sw_params_current (d_pcm_handle, d_sw_params);
254 bail ("snd_pcm_sw_params_current", err);
256 // Tell the PCM device to wait to start until we've filled
257 // it's buffers half way full. This helps avoid audio underruns.
259 err = snd_pcm_sw_params_set_start_threshold(d_pcm_handle,
261 d_nperiods * d_period_size / 2);
263 bail ("snd_pcm_sw_params_set_start_threshold", err);
265 // store the s/w params
266 err = snd_pcm_sw_params (d_pcm_handle, d_sw_params);
268 bail ("snd_pcm_sw_params", err);
270 d_buffer_size_bytes =
271 d_period_size * nchan * snd_pcm_format_size (d_format, 1);
273 d_buffer = new char [d_buffer_size_bytes];
276 fprintf (stdout, "audio_alsa_sink[%s]: sample resolution = %d bits\n",
277 snd_pcm_name (d_pcm_handle),
278 snd_pcm_hw_params_get_sbits (d_hw_params));
281 case SND_PCM_FORMAT_S16:
283 d_worker = &audio_alsa_sink::work_s16_1x2;
285 d_worker = &audio_alsa_sink::work_s16;
288 case SND_PCM_FORMAT_S32:
290 d_worker = &audio_alsa_sink::work_s32_1x2;
292 d_worker = &audio_alsa_sink::work_s32;
301 audio_alsa_sink::~audio_alsa_sink ()
303 if (snd_pcm_state (d_pcm_handle) == SND_PCM_STATE_RUNNING)
304 snd_pcm_drop (d_pcm_handle);
306 snd_pcm_close(d_pcm_handle);
307 delete [] ((char *) d_hw_params);
308 delete [] ((char *) d_sw_params);
313 audio_alsa_sink::work (int noutput_items,
314 gr_vector_const_void_star &input_items,
315 gr_vector_void_star &output_items)
317 assert ((noutput_items % d_period_size) == 0);
319 // this is a call through a pointer to a method...
320 return (this->*d_worker)(noutput_items, input_items, output_items);
324 * Work function that deals with float to S16 conversion
327 audio_alsa_sink::work_s16 (int noutput_items,
328 gr_vector_const_void_star &input_items,
329 gr_vector_void_star &output_items)
331 typedef gr_int16 sample_t; // the type of samples we're creating
332 static const int NBITS = 16; // # of bits in a sample
334 unsigned int nchan = input_items.size ();
335 const float **in = (const float **) &input_items[0];
336 sample_t *buf = (sample_t *) d_buffer;
340 unsigned int sizeof_frame = nchan * sizeof (sample_t);
341 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
343 for (n = 0; n < noutput_items; n += d_period_size){
345 // process one period of data
347 for (unsigned int i = 0; i < d_period_size; i++){
348 for (unsigned int chan = 0; chan < nchan; chan++){
349 buf[bi++] = (sample_t) (in[chan][i] * (float) ((1L << (NBITS-1)) - 1));
353 // update src pointers
354 for (unsigned int chan = 0; chan < nchan; chan++)
355 in[chan] += d_period_size;
357 if (!write_buffer (buf, d_period_size, sizeof_frame))
358 return -1; // No fixing this problem. Say we're done.
366 * Work function that deals with float to S32 conversion
369 audio_alsa_sink::work_s32 (int noutput_items,
370 gr_vector_const_void_star &input_items,
371 gr_vector_void_star &output_items)
373 typedef gr_int32 sample_t; // the type of samples we're creating
374 static const int NBITS = 32; // # of bits in a sample
376 unsigned int nchan = input_items.size ();
377 const float **in = (const float **) &input_items[0];
378 sample_t *buf = (sample_t *) d_buffer;
382 unsigned int sizeof_frame = nchan * sizeof (sample_t);
383 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
385 for (n = 0; n < noutput_items; n += d_period_size){
387 // process one period of data
389 for (unsigned int i = 0; i < d_period_size; i++){
390 for (unsigned int chan = 0; chan < nchan; chan++){
391 buf[bi++] = (sample_t) (in[chan][i] * (float) ((1L << (NBITS-1)) - 1));
395 // update src pointers
396 for (unsigned int chan = 0; chan < nchan; chan++)
397 in[chan] += d_period_size;
399 if (!write_buffer (buf, d_period_size, sizeof_frame))
400 return -1; // No fixing this problem. Say we're done.
407 * Work function that deals with float to S16 conversion and
408 * mono to stereo kludge.
411 audio_alsa_sink::work_s16_1x2 (int noutput_items,
412 gr_vector_const_void_star &input_items,
413 gr_vector_void_star &output_items)
415 typedef gr_int16 sample_t; // the type of samples we're creating
416 static const int NBITS = 16; // # of bits in a sample
418 assert (input_items.size () == 1);
419 static const unsigned int nchan = 2;
420 const float **in = (const float **) &input_items[0];
421 sample_t *buf = (sample_t *) d_buffer;
425 unsigned int sizeof_frame = nchan * sizeof (sample_t);
426 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
428 for (n = 0; n < noutput_items; n += d_period_size){
430 // process one period of data
432 for (unsigned int i = 0; i < d_period_size; i++){
433 sample_t t = (sample_t) (in[0][i] * (float) ((1L << (NBITS-1)) - 1));
438 // update src pointers
439 in[0] += d_period_size;
441 if (!write_buffer (buf, d_period_size, sizeof_frame))
442 return -1; // No fixing this problem. Say we're done.
449 * Work function that deals with float to S32 conversion and
450 * mono to stereo kludge.
453 audio_alsa_sink::work_s32_1x2 (int noutput_items,
454 gr_vector_const_void_star &input_items,
455 gr_vector_void_star &output_items)
457 typedef gr_int32 sample_t; // the type of samples we're creating
458 static const int NBITS = 32; // # of bits in a sample
460 assert (input_items.size () == 1);
461 static unsigned int nchan = 2;
462 const float **in = (const float **) &input_items[0];
463 sample_t *buf = (sample_t *) d_buffer;
467 unsigned int sizeof_frame = nchan * sizeof (sample_t);
468 assert (d_buffer_size_bytes == d_period_size * sizeof_frame);
470 for (n = 0; n < noutput_items; n += d_period_size){
472 // process one period of data
474 for (unsigned int i = 0; i < d_period_size; i++){
475 sample_t t = (sample_t) (in[0][i] * (float) ((1L << (NBITS-1)) - 1));
480 // update src pointers
481 in[0] += d_period_size;
483 if (!write_buffer (buf, d_period_size, sizeof_frame))
484 return -1; // No fixing this problem. Say we're done.
491 audio_alsa_sink::write_buffer (const void *vbuffer,
492 unsigned nframes, unsigned sizeof_frame)
494 const unsigned char *buffer = (const unsigned char *) vbuffer;
497 int r = snd_pcm_writei (d_pcm_handle, buffer, nframes);
500 if (d_ok_to_block == true)
501 continue; // try again
506 else if (r == -EPIPE){ // underrun
508 fputs ("aU", stderr);
509 if ((r = snd_pcm_prepare (d_pcm_handle)) < 0){
510 output_error_msg ("snd_pcm_prepare failed. Can't recover from underrun", r);
513 continue; // try again
516 else if (r == -ESTRPIPE){ // h/w is suspended (whatever that means)
517 // This is apparently related to power management
519 if ((r = snd_pcm_resume (d_pcm_handle)) < 0){
520 output_error_msg ("failed to resume from suspend", r);
523 continue; // try again
527 output_error_msg ("snd_pcm_writei failed", r);
532 buffer += r * sizeof_frame;
540 audio_alsa_sink::output_error_msg (const char *msg, int err)
542 fprintf (stderr, "audio_alsa_sink[%s]: %s: %s\n",
543 snd_pcm_name (d_pcm_handle), msg, snd_strerror (err));
547 audio_alsa_sink::bail (const char *msg, int err) throw (std::runtime_error)
549 output_error_msg (msg, err);
550 throw std::runtime_error ("audio_alsa_sink");