Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / audio / alsaaudio.c
blob74ead97d87a0b736e92b0a5ffe10b4165c955ab5
1 /*
2 * QEMU ALSA audio driver
4 * Copyright (c) 2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <alsa/asoundlib.h>
25 #include "qemu-common.h"
26 #include "qemu/main-loop.h"
27 #include "audio.h"
29 #if QEMU_GNUC_PREREQ(4, 3)
30 #pragma GCC diagnostic ignored "-Waddress"
31 #endif
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
36 struct pollhlp {
37 snd_pcm_t *handle;
38 struct pollfd *pfds;
39 int count;
40 int mask;
43 typedef struct ALSAVoiceOut {
44 HWVoiceOut hw;
45 int wpos;
46 int pending;
47 void *pcm_buf;
48 snd_pcm_t *handle;
49 struct pollhlp pollhlp;
50 } ALSAVoiceOut;
52 typedef struct ALSAVoiceIn {
53 HWVoiceIn hw;
54 snd_pcm_t *handle;
55 void *pcm_buf;
56 struct pollhlp pollhlp;
57 } ALSAVoiceIn;
59 static struct {
60 int size_in_usec_in;
61 int size_in_usec_out;
62 const char *pcm_name_in;
63 const char *pcm_name_out;
64 unsigned int buffer_size_in;
65 unsigned int period_size_in;
66 unsigned int buffer_size_out;
67 unsigned int period_size_out;
68 unsigned int threshold;
70 int buffer_size_in_overridden;
71 int period_size_in_overridden;
73 int buffer_size_out_overridden;
74 int period_size_out_overridden;
75 int verbose;
76 } conf = {
77 .buffer_size_out = 4096,
78 .period_size_out = 1024,
79 .pcm_name_out = "default",
80 .pcm_name_in = "default",
83 struct alsa_params_req {
84 int freq;
85 snd_pcm_format_t fmt;
86 int nchannels;
87 int size_in_usec;
88 int override_mask;
89 unsigned int buffer_size;
90 unsigned int period_size;
93 struct alsa_params_obt {
94 int freq;
95 audfmt_e fmt;
96 int endianness;
97 int nchannels;
98 snd_pcm_uframes_t samples;
101 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
103 va_list ap;
105 va_start (ap, fmt);
106 AUD_vlog (AUDIO_CAP, fmt, ap);
107 va_end (ap);
109 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
112 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
113 int err,
114 const char *typ,
115 const char *fmt,
119 va_list ap;
121 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
123 va_start (ap, fmt);
124 AUD_vlog (AUDIO_CAP, fmt, ap);
125 va_end (ap);
127 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
130 static void alsa_fini_poll (struct pollhlp *hlp)
132 int i;
133 struct pollfd *pfds = hlp->pfds;
135 if (pfds) {
136 for (i = 0; i < hlp->count; ++i) {
137 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
139 g_free (pfds);
141 hlp->pfds = NULL;
142 hlp->count = 0;
143 hlp->handle = NULL;
146 static void alsa_anal_close1 (snd_pcm_t **handlep)
148 int err = snd_pcm_close (*handlep);
149 if (err) {
150 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
152 *handlep = NULL;
155 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
157 alsa_fini_poll (hlp);
158 alsa_anal_close1 (handlep);
161 static int alsa_recover (snd_pcm_t *handle)
163 int err = snd_pcm_prepare (handle);
164 if (err < 0) {
165 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
166 return -1;
168 return 0;
171 static int alsa_resume (snd_pcm_t *handle)
173 int err = snd_pcm_resume (handle);
174 if (err < 0) {
175 alsa_logerr (err, "Failed to resume handle %p\n", handle);
176 return -1;
178 return 0;
181 static void alsa_poll_handler (void *opaque)
183 int err, count;
184 snd_pcm_state_t state;
185 struct pollhlp *hlp = opaque;
186 unsigned short revents;
188 count = poll (hlp->pfds, hlp->count, 0);
189 if (count < 0) {
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
191 return;
194 if (!count) {
195 return;
198 /* XXX: ALSA example uses initial count, not the one returned by
199 poll, correct? */
200 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
201 hlp->count, &revents);
202 if (err < 0) {
203 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
204 return;
207 if (!(revents & hlp->mask)) {
208 if (conf.verbose) {
209 dolog ("revents = %d\n", revents);
211 return;
214 state = snd_pcm_state (hlp->handle);
215 switch (state) {
216 case SND_PCM_STATE_SETUP:
217 alsa_recover (hlp->handle);
218 break;
220 case SND_PCM_STATE_XRUN:
221 alsa_recover (hlp->handle);
222 break;
224 case SND_PCM_STATE_SUSPENDED:
225 alsa_resume (hlp->handle);
226 break;
228 case SND_PCM_STATE_PREPARED:
229 audio_run ("alsa run (prepared)");
230 break;
232 case SND_PCM_STATE_RUNNING:
233 audio_run ("alsa run (running)");
234 break;
236 default:
237 dolog ("Unexpected state %d\n", state);
241 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
243 int i, count, err;
244 struct pollfd *pfds;
246 count = snd_pcm_poll_descriptors_count (handle);
247 if (count <= 0) {
248 dolog ("Could not initialize poll mode\n"
249 "Invalid number of poll descriptors %d\n", count);
250 return -1;
253 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
254 if (!pfds) {
255 dolog ("Could not initialize poll mode\n");
256 return -1;
259 err = snd_pcm_poll_descriptors (handle, pfds, count);
260 if (err < 0) {
261 alsa_logerr (err, "Could not initialize poll mode\n"
262 "Could not obtain poll descriptors\n");
263 g_free (pfds);
264 return -1;
267 for (i = 0; i < count; ++i) {
268 if (pfds[i].events & POLLIN) {
269 err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
270 NULL, hlp);
272 if (pfds[i].events & POLLOUT) {
273 if (conf.verbose) {
274 dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
276 err = qemu_set_fd_handler (pfds[i].fd, NULL,
277 alsa_poll_handler, hlp);
279 if (conf.verbose) {
280 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
281 pfds[i].events, i, pfds[i].fd, err);
284 if (err) {
285 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
286 pfds[i].events, i, pfds[i].fd, err);
288 while (i--) {
289 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
291 g_free (pfds);
292 return -1;
295 hlp->pfds = pfds;
296 hlp->count = count;
297 hlp->handle = handle;
298 hlp->mask = mask;
299 return 0;
302 static int alsa_poll_out (HWVoiceOut *hw)
304 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
306 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
309 static int alsa_poll_in (HWVoiceIn *hw)
311 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
313 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
316 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
318 return audio_pcm_sw_write (sw, buf, len);
321 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt, int endianness)
323 switch (fmt) {
324 case AUD_FMT_S8:
325 return SND_PCM_FORMAT_S8;
327 case AUD_FMT_U8:
328 return SND_PCM_FORMAT_U8;
330 case AUD_FMT_S16:
331 if (endianness) {
332 return SND_PCM_FORMAT_S16_BE;
334 else {
335 return SND_PCM_FORMAT_S16_LE;
338 case AUD_FMT_U16:
339 if (endianness) {
340 return SND_PCM_FORMAT_U16_BE;
342 else {
343 return SND_PCM_FORMAT_U16_LE;
346 case AUD_FMT_S32:
347 if (endianness) {
348 return SND_PCM_FORMAT_S32_BE;
350 else {
351 return SND_PCM_FORMAT_S32_LE;
354 case AUD_FMT_U32:
355 if (endianness) {
356 return SND_PCM_FORMAT_U32_BE;
358 else {
359 return SND_PCM_FORMAT_U32_LE;
362 default:
363 dolog ("Internal logic error: Bad audio format %d\n", fmt);
364 #ifdef DEBUG_AUDIO
365 abort ();
366 #endif
367 return SND_PCM_FORMAT_U8;
371 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
372 int *endianness)
374 switch (alsafmt) {
375 case SND_PCM_FORMAT_S8:
376 *endianness = 0;
377 *fmt = AUD_FMT_S8;
378 break;
380 case SND_PCM_FORMAT_U8:
381 *endianness = 0;
382 *fmt = AUD_FMT_U8;
383 break;
385 case SND_PCM_FORMAT_S16_LE:
386 *endianness = 0;
387 *fmt = AUD_FMT_S16;
388 break;
390 case SND_PCM_FORMAT_U16_LE:
391 *endianness = 0;
392 *fmt = AUD_FMT_U16;
393 break;
395 case SND_PCM_FORMAT_S16_BE:
396 *endianness = 1;
397 *fmt = AUD_FMT_S16;
398 break;
400 case SND_PCM_FORMAT_U16_BE:
401 *endianness = 1;
402 *fmt = AUD_FMT_U16;
403 break;
405 case SND_PCM_FORMAT_S32_LE:
406 *endianness = 0;
407 *fmt = AUD_FMT_S32;
408 break;
410 case SND_PCM_FORMAT_U32_LE:
411 *endianness = 0;
412 *fmt = AUD_FMT_U32;
413 break;
415 case SND_PCM_FORMAT_S32_BE:
416 *endianness = 1;
417 *fmt = AUD_FMT_S32;
418 break;
420 case SND_PCM_FORMAT_U32_BE:
421 *endianness = 1;
422 *fmt = AUD_FMT_U32;
423 break;
425 default:
426 dolog ("Unrecognized audio format %d\n", alsafmt);
427 return -1;
430 return 0;
433 static void alsa_dump_info (struct alsa_params_req *req,
434 struct alsa_params_obt *obt,
435 snd_pcm_format_t obtfmt)
437 dolog ("parameter | requested value | obtained value\n");
438 dolog ("format | %10d | %10d\n", req->fmt, obtfmt);
439 dolog ("channels | %10d | %10d\n",
440 req->nchannels, obt->nchannels);
441 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
442 dolog ("============================================\n");
443 dolog ("requested: buffer size %d period size %d\n",
444 req->buffer_size, req->period_size);
445 dolog ("obtained: samples %ld\n", obt->samples);
448 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
450 int err;
451 snd_pcm_sw_params_t *sw_params;
453 snd_pcm_sw_params_alloca (&sw_params);
455 err = snd_pcm_sw_params_current (handle, sw_params);
456 if (err < 0) {
457 dolog ("Could not fully initialize DAC\n");
458 alsa_logerr (err, "Failed to get current software parameters\n");
459 return;
462 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
463 if (err < 0) {
464 dolog ("Could not fully initialize DAC\n");
465 alsa_logerr (err, "Failed to set software threshold to %ld\n",
466 threshold);
467 return;
470 err = snd_pcm_sw_params (handle, sw_params);
471 if (err < 0) {
472 dolog ("Could not fully initialize DAC\n");
473 alsa_logerr (err, "Failed to set software parameters\n");
474 return;
478 static int alsa_open (int in, struct alsa_params_req *req,
479 struct alsa_params_obt *obt, snd_pcm_t **handlep)
481 snd_pcm_t *handle;
482 snd_pcm_hw_params_t *hw_params;
483 int err;
484 int size_in_usec;
485 unsigned int freq, nchannels;
486 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
487 snd_pcm_uframes_t obt_buffer_size;
488 const char *typ = in ? "ADC" : "DAC";
489 snd_pcm_format_t obtfmt;
491 freq = req->freq;
492 nchannels = req->nchannels;
493 size_in_usec = req->size_in_usec;
495 snd_pcm_hw_params_alloca (&hw_params);
497 err = snd_pcm_open (
498 &handle,
499 pcm_name,
500 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
501 SND_PCM_NONBLOCK
503 if (err < 0) {
504 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
505 return -1;
508 err = snd_pcm_hw_params_any (handle, hw_params);
509 if (err < 0) {
510 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
511 goto err;
514 err = snd_pcm_hw_params_set_access (
515 handle,
516 hw_params,
517 SND_PCM_ACCESS_RW_INTERLEAVED
519 if (err < 0) {
520 alsa_logerr2 (err, typ, "Failed to set access type\n");
521 goto err;
524 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
525 if (err < 0 && conf.verbose) {
526 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
529 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
530 if (err < 0) {
531 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
532 goto err;
535 err = snd_pcm_hw_params_set_channels_near (
536 handle,
537 hw_params,
538 &nchannels
540 if (err < 0) {
541 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
542 req->nchannels);
543 goto err;
546 if (nchannels != 1 && nchannels != 2) {
547 alsa_logerr2 (err, typ,
548 "Can not handle obtained number of channels %d\n",
549 nchannels);
550 goto err;
553 if (req->buffer_size) {
554 unsigned long obt;
556 if (size_in_usec) {
557 int dir = 0;
558 unsigned int btime = req->buffer_size;
560 err = snd_pcm_hw_params_set_buffer_time_near (
561 handle,
562 hw_params,
563 &btime,
564 &dir
566 obt = btime;
568 else {
569 snd_pcm_uframes_t bsize = req->buffer_size;
571 err = snd_pcm_hw_params_set_buffer_size_near (
572 handle,
573 hw_params,
574 &bsize
576 obt = bsize;
578 if (err < 0) {
579 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
580 size_in_usec ? "time" : "size", req->buffer_size);
581 goto err;
584 if ((req->override_mask & 2) && (obt - req->buffer_size))
585 dolog ("Requested buffer %s %u was rejected, using %lu\n",
586 size_in_usec ? "time" : "size", req->buffer_size, obt);
589 if (req->period_size) {
590 unsigned long obt;
592 if (size_in_usec) {
593 int dir = 0;
594 unsigned int ptime = req->period_size;
596 err = snd_pcm_hw_params_set_period_time_near (
597 handle,
598 hw_params,
599 &ptime,
600 &dir
602 obt = ptime;
604 else {
605 int dir = 0;
606 snd_pcm_uframes_t psize = req->period_size;
608 err = snd_pcm_hw_params_set_period_size_near (
609 handle,
610 hw_params,
611 &psize,
612 &dir
614 obt = psize;
617 if (err < 0) {
618 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
619 size_in_usec ? "time" : "size", req->period_size);
620 goto err;
623 if (((req->override_mask & 1) && (obt - req->period_size)))
624 dolog ("Requested period %s %u was rejected, using %lu\n",
625 size_in_usec ? "time" : "size", req->period_size, obt);
628 err = snd_pcm_hw_params (handle, hw_params);
629 if (err < 0) {
630 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
631 goto err;
634 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
635 if (err < 0) {
636 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
637 goto err;
640 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
641 if (err < 0) {
642 alsa_logerr2 (err, typ, "Failed to get format\n");
643 goto err;
646 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
647 dolog ("Invalid format was returned %d\n", obtfmt);
648 goto err;
651 err = snd_pcm_prepare (handle);
652 if (err < 0) {
653 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
654 goto err;
657 if (!in && conf.threshold) {
658 snd_pcm_uframes_t threshold;
659 int bytes_per_sec;
661 bytes_per_sec = freq << (nchannels == 2);
663 switch (obt->fmt) {
664 case AUD_FMT_S8:
665 case AUD_FMT_U8:
666 break;
668 case AUD_FMT_S16:
669 case AUD_FMT_U16:
670 bytes_per_sec <<= 1;
671 break;
673 case AUD_FMT_S32:
674 case AUD_FMT_U32:
675 bytes_per_sec <<= 2;
676 break;
679 threshold = (conf.threshold * bytes_per_sec) / 1000;
680 alsa_set_threshold (handle, threshold);
683 obt->nchannels = nchannels;
684 obt->freq = freq;
685 obt->samples = obt_buffer_size;
687 *handlep = handle;
689 if (conf.verbose &&
690 (obtfmt != req->fmt ||
691 obt->nchannels != req->nchannels ||
692 obt->freq != req->freq)) {
693 dolog ("Audio parameters for %s\n", typ);
694 alsa_dump_info (req, obt, obtfmt);
697 #ifdef DEBUG
698 alsa_dump_info (req, obt, obtfmt);
699 #endif
700 return 0;
702 err:
703 alsa_anal_close1 (&handle);
704 return -1;
707 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
709 snd_pcm_sframes_t avail;
711 avail = snd_pcm_avail_update (handle);
712 if (avail < 0) {
713 if (avail == -EPIPE) {
714 if (!alsa_recover (handle)) {
715 avail = snd_pcm_avail_update (handle);
719 if (avail < 0) {
720 alsa_logerr (avail,
721 "Could not obtain number of available frames\n");
722 return -1;
726 return avail;
729 static void alsa_write_pending (ALSAVoiceOut *alsa)
731 HWVoiceOut *hw = &alsa->hw;
733 while (alsa->pending) {
734 int left_till_end_samples = hw->samples - alsa->wpos;
735 int len = audio_MIN (alsa->pending, left_till_end_samples);
736 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
738 while (len) {
739 snd_pcm_sframes_t written;
741 written = snd_pcm_writei (alsa->handle, src, len);
743 if (written <= 0) {
744 switch (written) {
745 case 0:
746 if (conf.verbose) {
747 dolog ("Failed to write %d frames (wrote zero)\n", len);
749 return;
751 case -EPIPE:
752 if (alsa_recover (alsa->handle)) {
753 alsa_logerr (written, "Failed to write %d frames\n",
754 len);
755 return;
757 if (conf.verbose) {
758 dolog ("Recovering from playback xrun\n");
760 continue;
762 case -ESTRPIPE:
763 /* stream is suspended and waiting for an
764 application recovery */
765 if (alsa_resume (alsa->handle)) {
766 alsa_logerr (written, "Failed to write %d frames\n",
767 len);
768 return;
770 if (conf.verbose) {
771 dolog ("Resuming suspended output stream\n");
773 continue;
775 case -EAGAIN:
776 return;
778 default:
779 alsa_logerr (written, "Failed to write %d frames from %p\n",
780 len, src);
781 return;
785 alsa->wpos = (alsa->wpos + written) % hw->samples;
786 alsa->pending -= written;
787 len -= written;
792 static int alsa_run_out (HWVoiceOut *hw, int live)
794 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
795 int decr;
796 snd_pcm_sframes_t avail;
798 avail = alsa_get_avail (alsa->handle);
799 if (avail < 0) {
800 dolog ("Could not get number of available playback frames\n");
801 return 0;
804 decr = audio_MIN (live, avail);
805 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
806 alsa->pending += decr;
807 alsa_write_pending (alsa);
808 return decr;
811 static void alsa_fini_out (HWVoiceOut *hw)
813 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
815 ldebug ("alsa_fini\n");
816 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
818 g_free(alsa->pcm_buf);
819 alsa->pcm_buf = NULL;
822 static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
824 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
825 struct alsa_params_req req;
826 struct alsa_params_obt obt;
827 snd_pcm_t *handle;
828 struct audsettings obt_as;
830 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
831 req.freq = as->freq;
832 req.nchannels = as->nchannels;
833 req.period_size = conf.period_size_out;
834 req.buffer_size = conf.buffer_size_out;
835 req.size_in_usec = conf.size_in_usec_out;
836 req.override_mask =
837 (conf.period_size_out_overridden ? 1 : 0) |
838 (conf.buffer_size_out_overridden ? 2 : 0);
840 if (alsa_open (0, &req, &obt, &handle)) {
841 return -1;
844 obt_as.freq = obt.freq;
845 obt_as.nchannels = obt.nchannels;
846 obt_as.fmt = obt.fmt;
847 obt_as.endianness = obt.endianness;
849 audio_pcm_init_info (&hw->info, &obt_as);
850 hw->samples = obt.samples;
852 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
853 if (!alsa->pcm_buf) {
854 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
855 hw->samples, 1 << hw->info.shift);
856 alsa_anal_close1 (&handle);
857 return -1;
860 alsa->handle = handle;
861 return 0;
864 #define VOICE_CTL_PAUSE 0
865 #define VOICE_CTL_PREPARE 1
866 #define VOICE_CTL_START 2
868 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
870 int err;
872 if (ctl == VOICE_CTL_PAUSE) {
873 err = snd_pcm_drop (handle);
874 if (err < 0) {
875 alsa_logerr (err, "Could not stop %s\n", typ);
876 return -1;
879 else {
880 err = snd_pcm_prepare (handle);
881 if (err < 0) {
882 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
883 return -1;
885 if (ctl == VOICE_CTL_START) {
886 err = snd_pcm_start(handle);
887 if (err < 0) {
888 alsa_logerr (err, "Could not start handle for %s\n", typ);
889 return -1;
894 return 0;
897 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
899 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
901 switch (cmd) {
902 case VOICE_ENABLE:
904 va_list ap;
905 int poll_mode;
907 va_start (ap, cmd);
908 poll_mode = va_arg (ap, int);
909 va_end (ap);
911 ldebug ("enabling voice\n");
912 if (poll_mode && alsa_poll_out (hw)) {
913 poll_mode = 0;
915 hw->poll_mode = poll_mode;
916 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PREPARE);
919 case VOICE_DISABLE:
920 ldebug ("disabling voice\n");
921 if (hw->poll_mode) {
922 hw->poll_mode = 0;
923 alsa_fini_poll (&alsa->pollhlp);
925 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PAUSE);
928 return -1;
931 static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
933 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
934 struct alsa_params_req req;
935 struct alsa_params_obt obt;
936 snd_pcm_t *handle;
937 struct audsettings obt_as;
939 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
940 req.freq = as->freq;
941 req.nchannels = as->nchannels;
942 req.period_size = conf.period_size_in;
943 req.buffer_size = conf.buffer_size_in;
944 req.size_in_usec = conf.size_in_usec_in;
945 req.override_mask =
946 (conf.period_size_in_overridden ? 1 : 0) |
947 (conf.buffer_size_in_overridden ? 2 : 0);
949 if (alsa_open (1, &req, &obt, &handle)) {
950 return -1;
953 obt_as.freq = obt.freq;
954 obt_as.nchannels = obt.nchannels;
955 obt_as.fmt = obt.fmt;
956 obt_as.endianness = obt.endianness;
958 audio_pcm_init_info (&hw->info, &obt_as);
959 hw->samples = obt.samples;
961 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
962 if (!alsa->pcm_buf) {
963 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
964 hw->samples, 1 << hw->info.shift);
965 alsa_anal_close1 (&handle);
966 return -1;
969 alsa->handle = handle;
970 return 0;
973 static void alsa_fini_in (HWVoiceIn *hw)
975 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
977 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
979 g_free(alsa->pcm_buf);
980 alsa->pcm_buf = NULL;
983 static int alsa_run_in (HWVoiceIn *hw)
985 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
986 int hwshift = hw->info.shift;
987 int i;
988 int live = audio_pcm_hw_get_live_in (hw);
989 int dead = hw->samples - live;
990 int decr;
991 struct {
992 int add;
993 int len;
994 } bufs[2] = {
995 { .add = hw->wpos, .len = 0 },
996 { .add = 0, .len = 0 }
998 snd_pcm_sframes_t avail;
999 snd_pcm_uframes_t read_samples = 0;
1001 if (!dead) {
1002 return 0;
1005 avail = alsa_get_avail (alsa->handle);
1006 if (avail < 0) {
1007 dolog ("Could not get number of captured frames\n");
1008 return 0;
1011 if (!avail) {
1012 snd_pcm_state_t state;
1014 state = snd_pcm_state (alsa->handle);
1015 switch (state) {
1016 case SND_PCM_STATE_PREPARED:
1017 avail = hw->samples;
1018 break;
1019 case SND_PCM_STATE_SUSPENDED:
1020 /* stream is suspended and waiting for an application recovery */
1021 if (alsa_resume (alsa->handle)) {
1022 dolog ("Failed to resume suspended input stream\n");
1023 return 0;
1025 if (conf.verbose) {
1026 dolog ("Resuming suspended input stream\n");
1028 break;
1029 default:
1030 if (conf.verbose) {
1031 dolog ("No frames available and ALSA state is %d\n", state);
1033 return 0;
1037 decr = audio_MIN (dead, avail);
1038 if (!decr) {
1039 return 0;
1042 if (hw->wpos + decr > hw->samples) {
1043 bufs[0].len = (hw->samples - hw->wpos);
1044 bufs[1].len = (decr - (hw->samples - hw->wpos));
1046 else {
1047 bufs[0].len = decr;
1050 for (i = 0; i < 2; ++i) {
1051 void *src;
1052 struct st_sample *dst;
1053 snd_pcm_sframes_t nread;
1054 snd_pcm_uframes_t len;
1056 len = bufs[i].len;
1058 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1059 dst = hw->conv_buf + bufs[i].add;
1061 while (len) {
1062 nread = snd_pcm_readi (alsa->handle, src, len);
1064 if (nread <= 0) {
1065 switch (nread) {
1066 case 0:
1067 if (conf.verbose) {
1068 dolog ("Failed to read %ld frames (read zero)\n", len);
1070 goto exit;
1072 case -EPIPE:
1073 if (alsa_recover (alsa->handle)) {
1074 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1075 goto exit;
1077 if (conf.verbose) {
1078 dolog ("Recovering from capture xrun\n");
1080 continue;
1082 case -EAGAIN:
1083 goto exit;
1085 default:
1086 alsa_logerr (
1087 nread,
1088 "Failed to read %ld frames from %p\n",
1089 len,
1092 goto exit;
1096 hw->conv (dst, src, nread);
1098 src = advance (src, nread << hwshift);
1099 dst += nread;
1101 read_samples += nread;
1102 len -= nread;
1106 exit:
1107 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1108 return read_samples;
1111 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1113 return audio_pcm_sw_read (sw, buf, size);
1116 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1118 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1120 switch (cmd) {
1121 case VOICE_ENABLE:
1123 va_list ap;
1124 int poll_mode;
1126 va_start (ap, cmd);
1127 poll_mode = va_arg (ap, int);
1128 va_end (ap);
1130 ldebug ("enabling voice\n");
1131 if (poll_mode && alsa_poll_in (hw)) {
1132 poll_mode = 0;
1134 hw->poll_mode = poll_mode;
1136 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_START);
1139 case VOICE_DISABLE:
1140 ldebug ("disabling voice\n");
1141 if (hw->poll_mode) {
1142 hw->poll_mode = 0;
1143 alsa_fini_poll (&alsa->pollhlp);
1145 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_PAUSE);
1148 return -1;
1151 static void *alsa_audio_init (void)
1153 return &conf;
1156 static void alsa_audio_fini (void *opaque)
1158 (void) opaque;
1161 static struct audio_option alsa_options[] = {
1163 .name = "DAC_SIZE_IN_USEC",
1164 .tag = AUD_OPT_BOOL,
1165 .valp = &conf.size_in_usec_out,
1166 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1169 .name = "DAC_PERIOD_SIZE",
1170 .tag = AUD_OPT_INT,
1171 .valp = &conf.period_size_out,
1172 .descr = "DAC period size (0 to go with system default)",
1173 .overriddenp = &conf.period_size_out_overridden
1176 .name = "DAC_BUFFER_SIZE",
1177 .tag = AUD_OPT_INT,
1178 .valp = &conf.buffer_size_out,
1179 .descr = "DAC buffer size (0 to go with system default)",
1180 .overriddenp = &conf.buffer_size_out_overridden
1183 .name = "ADC_SIZE_IN_USEC",
1184 .tag = AUD_OPT_BOOL,
1185 .valp = &conf.size_in_usec_in,
1186 .descr =
1187 "ADC period/buffer size in microseconds (otherwise in frames)"
1190 .name = "ADC_PERIOD_SIZE",
1191 .tag = AUD_OPT_INT,
1192 .valp = &conf.period_size_in,
1193 .descr = "ADC period size (0 to go with system default)",
1194 .overriddenp = &conf.period_size_in_overridden
1197 .name = "ADC_BUFFER_SIZE",
1198 .tag = AUD_OPT_INT,
1199 .valp = &conf.buffer_size_in,
1200 .descr = "ADC buffer size (0 to go with system default)",
1201 .overriddenp = &conf.buffer_size_in_overridden
1204 .name = "THRESHOLD",
1205 .tag = AUD_OPT_INT,
1206 .valp = &conf.threshold,
1207 .descr = "(undocumented)"
1210 .name = "DAC_DEV",
1211 .tag = AUD_OPT_STR,
1212 .valp = &conf.pcm_name_out,
1213 .descr = "DAC device name (for instance dmix)"
1216 .name = "ADC_DEV",
1217 .tag = AUD_OPT_STR,
1218 .valp = &conf.pcm_name_in,
1219 .descr = "ADC device name"
1222 .name = "VERBOSE",
1223 .tag = AUD_OPT_BOOL,
1224 .valp = &conf.verbose,
1225 .descr = "Behave in a more verbose way"
1227 { /* End of list */ }
1230 static struct audio_pcm_ops alsa_pcm_ops = {
1231 .init_out = alsa_init_out,
1232 .fini_out = alsa_fini_out,
1233 .run_out = alsa_run_out,
1234 .write = alsa_write,
1235 .ctl_out = alsa_ctl_out,
1237 .init_in = alsa_init_in,
1238 .fini_in = alsa_fini_in,
1239 .run_in = alsa_run_in,
1240 .read = alsa_read,
1241 .ctl_in = alsa_ctl_in,
1244 struct audio_driver alsa_audio_driver = {
1245 .name = "alsa",
1246 .descr = "ALSA http://www.alsa-project.org",
1247 .options = alsa_options,
1248 .init = alsa_audio_init,
1249 .fini = alsa_audio_fini,
1250 .pcm_ops = &alsa_pcm_ops,
1251 .can_be_default = 1,
1252 .max_voices_out = INT_MAX,
1253 .max_voices_in = INT_MAX,
1254 .voice_size_out = sizeof (ALSAVoiceOut),
1255 .voice_size_in = sizeof (ALSAVoiceIn)