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
24 #include <alsa/asoundlib.h>
27 #define AUDIO_CAP "alsa"
28 #include "audio_int.h"
30 typedef struct ALSAVoiceOut {
36 typedef struct ALSAVoiceIn {
45 const char *pcm_name_in;
46 const char *pcm_name_out;
47 unsigned int buffer_size_in;
48 unsigned int period_size_in;
49 unsigned int buffer_size_out;
50 unsigned int period_size_out;
51 unsigned int threshold;
53 int buffer_size_in_overriden;
54 int period_size_in_overriden;
56 int buffer_size_out_overriden;
57 int period_size_out_overriden;
62 .size_in_usec_out = 1,
64 .pcm_name_out = "hw:0,0",
65 .pcm_name_in = "hw:0,0",
67 .buffer_size_in = 400000,
68 .period_size_in = 400000 / 4,
69 .buffer_size_out = 400000,
70 .period_size_out = 400000 / 4,
72 #define DEFAULT_BUFFER_SIZE 1024
73 #define DEFAULT_PERIOD_SIZE 256
74 .buffer_size_in = DEFAULT_BUFFER_SIZE * 4,
75 .period_size_in = DEFAULT_PERIOD_SIZE * 4,
76 .buffer_size_out = DEFAULT_BUFFER_SIZE,
77 .period_size_out = DEFAULT_PERIOD_SIZE,
78 .buffer_size_in_overriden = 0,
79 .buffer_size_out_overriden = 0,
80 .period_size_in_overriden = 0,
81 .period_size_out_overriden = 0,
87 struct alsa_params_req {
91 unsigned int buffer_size;
92 unsigned int period_size;
95 struct alsa_params_obt {
99 snd_pcm_uframes_t samples;
102 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
107 AUD_vlog (AUDIO_CAP, fmt, ap);
110 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
113 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
122 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
125 AUD_vlog (AUDIO_CAP, fmt, ap);
128 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
131 static void alsa_anal_close (snd_pcm_t **handlep)
133 int err = snd_pcm_close (*handlep);
135 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
140 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
142 return audio_pcm_sw_write (sw, buf, len);
145 static int aud_to_alsafmt (audfmt_e fmt)
149 return SND_PCM_FORMAT_S8;
152 return SND_PCM_FORMAT_U8;
155 return SND_PCM_FORMAT_S16_LE;
158 return SND_PCM_FORMAT_U16_LE;
161 dolog ("Internal logic error: Bad audio format %d\n", fmt);
165 return SND_PCM_FORMAT_U8;
169 static int alsa_to_audfmt (int alsafmt, audfmt_e *fmt, int *endianness)
172 case SND_PCM_FORMAT_S8:
177 case SND_PCM_FORMAT_U8:
182 case SND_PCM_FORMAT_S16_LE:
187 case SND_PCM_FORMAT_U16_LE:
192 case SND_PCM_FORMAT_S16_BE:
197 case SND_PCM_FORMAT_U16_BE:
203 dolog ("Unrecognized audio format %d\n", alsafmt);
210 #if defined DEBUG_MISMATCHES || defined DEBUG
211 static void alsa_dump_info (struct alsa_params_req *req,
212 struct alsa_params_obt *obt)
214 dolog ("parameter | requested value | obtained value\n");
215 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
216 dolog ("channels | %10d | %10d\n",
217 req->nchannels, obt->nchannels);
218 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
219 dolog ("============================================\n");
220 dolog ("requested: buffer size %d period size %d\n",
221 req->buffer_size, req->period_size);
222 dolog ("obtained: samples %ld\n", obt->samples);
226 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
229 snd_pcm_sw_params_t *sw_params;
231 snd_pcm_sw_params_alloca (&sw_params);
233 err = snd_pcm_sw_params_current (handle, sw_params);
235 dolog ("Could not fully initialize DAC\n");
236 alsa_logerr (err, "Failed to get current software parameters\n");
240 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
242 dolog ("Could not fully initialize DAC\n");
243 alsa_logerr (err, "Failed to set software threshold to %ld\n",
248 err = snd_pcm_sw_params (handle, sw_params);
250 dolog ("Could not fully initialize DAC\n");
251 alsa_logerr (err, "Failed to set software parameters\n");
256 static int alsa_open (int in, struct alsa_params_req *req,
257 struct alsa_params_obt *obt, snd_pcm_t **handlep)
260 snd_pcm_hw_params_t *hw_params;
261 int err, freq, nchannels;
262 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
263 unsigned int period_size, buffer_size;
264 snd_pcm_uframes_t obt_buffer_size;
265 const char *typ = in ? "ADC" : "DAC";
268 period_size = req->period_size;
269 buffer_size = req->buffer_size;
270 nchannels = req->nchannels;
272 snd_pcm_hw_params_alloca (&hw_params);
277 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
281 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
285 err = snd_pcm_hw_params_any (handle, hw_params);
287 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
291 err = snd_pcm_hw_params_set_access (
294 SND_PCM_ACCESS_RW_INTERLEAVED
297 alsa_logerr2 (err, typ, "Failed to set access type\n");
301 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
303 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
307 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
309 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
313 err = snd_pcm_hw_params_set_channels_near (
319 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
324 if (nchannels != 1 && nchannels != 2) {
325 alsa_logerr2 (err, typ,
326 "Can not handle obtained number of channels %d\n",
331 if (!((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out))) {
333 buffer_size = DEFAULT_BUFFER_SIZE;
334 period_size= DEFAULT_PERIOD_SIZE;
339 if ((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out)) {
341 err = snd_pcm_hw_params_set_period_time_near (
348 alsa_logerr2 (err, typ,
349 "Failed to set period time %d\n",
355 err = snd_pcm_hw_params_set_buffer_time_near (
363 alsa_logerr2 (err, typ,
364 "Failed to set buffer time %d\n",
371 snd_pcm_uframes_t minval;
374 minval = period_size;
377 err = snd_pcm_hw_params_get_period_size_min (
385 "Could not get minmal period size for %s\n",
390 if (period_size < minval) {
391 if ((in && conf.period_size_in_overriden)
392 || (!in && conf.period_size_out_overriden)) {
393 dolog ("%s period size(%d) is less "
394 "than minmal period size(%ld)\n",
399 period_size = minval;
403 err = snd_pcm_hw_params_set_period_size (
410 alsa_logerr2 (err, typ, "Failed to set period size %d\n",
416 minval = buffer_size;
417 err = snd_pcm_hw_params_get_buffer_size_min (
422 alsa_logerr (err, "Could not get minmal buffer size for %s\n",
426 if (buffer_size < minval) {
427 if ((in && conf.buffer_size_in_overriden)
428 || (!in && conf.buffer_size_out_overriden)) {
430 "%s buffer size(%d) is less "
431 "than minimal buffer size(%ld)\n",
437 buffer_size = minval;
441 err = snd_pcm_hw_params_set_buffer_size (
447 alsa_logerr2 (err, typ, "Failed to set buffer size %d\n",
454 dolog ("warning: Buffer size is not set\n");
457 err = snd_pcm_hw_params (handle, hw_params);
459 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
463 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
465 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
469 err = snd_pcm_prepare (handle);
471 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
475 if (!in && conf.threshold) {
476 snd_pcm_uframes_t threshold;
481 << (req->fmt == AUD_FMT_S16 || req->fmt == AUD_FMT_U16);
483 threshold = (conf.threshold * bytes_per_sec) / 1000;
484 alsa_set_threshold (handle, threshold);
488 obt->nchannels = nchannels;
490 obt->samples = obt_buffer_size;
493 #if defined DEBUG_MISMATCHES || defined DEBUG
494 if (obt->fmt != req->fmt ||
495 obt->nchannels != req->nchannels ||
496 obt->freq != req->freq) {
497 dolog ("Audio paramters mismatch for %s\n", typ);
498 alsa_dump_info (req, obt);
503 alsa_dump_info (req, obt);
508 alsa_anal_close (&handle);
512 static int alsa_recover (snd_pcm_t *handle)
514 int err = snd_pcm_prepare (handle);
516 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
522 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
524 snd_pcm_sframes_t avail;
526 avail = snd_pcm_avail_update (handle);
528 if (avail == -EPIPE) {
529 if (!alsa_recover (handle)) {
530 avail = snd_pcm_avail_update (handle);
536 "Could not obtain number of available frames\n");
544 static int alsa_run_out (HWVoiceOut *hw)
546 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
547 int rpos, live, decr;
551 snd_pcm_sframes_t avail;
553 live = audio_pcm_hw_get_live_out (hw);
558 avail = alsa_get_avail (alsa->handle);
560 dolog ("Could not get number of available playback frames\n");
564 decr = audio_MIN (live, avail);
568 int left_till_end_samples = hw->samples - rpos;
569 int len = audio_MIN (samples, left_till_end_samples);
570 snd_pcm_sframes_t written;
572 src = hw->mix_buf + rpos;
573 dst = advance (alsa->pcm_buf, rpos << hw->info.shift);
575 hw->clip (dst, src, len);
578 written = snd_pcm_writei (alsa->handle, dst, len);
584 dolog ("Failed to write %d frames (wrote zero)\n", len);
589 if (alsa_recover (alsa->handle)) {
590 alsa_logerr (written, "Failed to write %d frames\n",
595 dolog ("Recovering from playback xrun\n");
603 alsa_logerr (written, "Failed to write %d frames to %p\n",
609 mixeng_clear (src, written);
610 rpos = (rpos + written) % hw->samples;
613 dst = advance (dst, written << hw->info.shift);
623 static void alsa_fini_out (HWVoiceOut *hw)
625 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
627 ldebug ("alsa_fini\n");
628 alsa_anal_close (&alsa->handle);
631 qemu_free (alsa->pcm_buf);
632 alsa->pcm_buf = NULL;
636 static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as)
638 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
639 struct alsa_params_req req;
640 struct alsa_params_obt obt;
641 audfmt_e effective_fmt;
645 audsettings_t obt_as;
647 req.fmt = aud_to_alsafmt (as->fmt);
649 req.nchannels = as->nchannels;
650 req.period_size = conf.period_size_out;
651 req.buffer_size = conf.buffer_size_out;
653 if (alsa_open (0, &req, &obt, &handle)) {
657 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
659 alsa_anal_close (&handle);
663 obt_as.freq = obt.freq;
664 obt_as.nchannels = obt.nchannels;
665 obt_as.fmt = effective_fmt;
667 audio_pcm_init_info (
670 audio_need_to_swap_endian (endianness)
672 hw->samples = obt.samples;
674 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
675 if (!alsa->pcm_buf) {
676 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
677 hw->samples, 1 << hw->info.shift);
678 alsa_anal_close (&handle);
682 alsa->handle = handle;
686 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int pause)
691 err = snd_pcm_drop (handle);
693 alsa_logerr (err, "Could not stop %s\n", typ);
698 err = snd_pcm_prepare (handle);
700 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
708 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
710 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
714 ldebug ("enabling voice\n");
715 return alsa_voice_ctl (alsa->handle, "playback", 0);
718 ldebug ("disabling voice\n");
719 return alsa_voice_ctl (alsa->handle, "playback", 1);
725 static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as)
727 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
728 struct alsa_params_req req;
729 struct alsa_params_obt obt;
732 audfmt_e effective_fmt;
734 audsettings_t obt_as;
736 req.fmt = aud_to_alsafmt (as->fmt);
738 req.nchannels = as->nchannels;
739 req.period_size = conf.period_size_in;
740 req.buffer_size = conf.buffer_size_in;
742 if (alsa_open (1, &req, &obt, &handle)) {
746 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
748 alsa_anal_close (&handle);
752 obt_as.freq = obt.freq;
753 obt_as.nchannels = obt.nchannels;
754 obt_as.fmt = effective_fmt;
756 audio_pcm_init_info (
759 audio_need_to_swap_endian (endianness)
761 hw->samples = obt.samples;
763 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
764 if (!alsa->pcm_buf) {
765 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
766 hw->samples, 1 << hw->info.shift);
767 alsa_anal_close (&handle);
771 alsa->handle = handle;
775 static void alsa_fini_in (HWVoiceIn *hw)
777 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
779 alsa_anal_close (&alsa->handle);
782 qemu_free (alsa->pcm_buf);
783 alsa->pcm_buf = NULL;
787 static int alsa_run_in (HWVoiceIn *hw)
789 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
790 int hwshift = hw->info.shift;
792 int live = audio_pcm_hw_get_live_in (hw);
793 int dead = hw->samples - live;
802 snd_pcm_sframes_t avail;
803 snd_pcm_uframes_t read_samples = 0;
809 avail = alsa_get_avail (alsa->handle);
811 dolog ("Could not get number of captured frames\n");
815 if (!avail && (snd_pcm_state (alsa->handle) == SND_PCM_STATE_PREPARED)) {
819 decr = audio_MIN (dead, avail);
824 if (hw->wpos + decr > hw->samples) {
825 bufs[0].len = (hw->samples - hw->wpos);
826 bufs[1].len = (decr - (hw->samples - hw->wpos));
832 for (i = 0; i < 2; ++i) {
835 snd_pcm_sframes_t nread;
836 snd_pcm_uframes_t len;
840 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
841 dst = hw->conv_buf + bufs[i].add;
844 nread = snd_pcm_readi (alsa->handle, src, len);
850 dolog ("Failed to read %ld frames (read zero)\n", len);
855 if (alsa_recover (alsa->handle)) {
856 alsa_logerr (nread, "Failed to read %ld frames\n", len);
860 dolog ("Recovering from capture xrun\n");
870 "Failed to read %ld frames from %p\n",
878 hw->conv (dst, src, nread, &nominal_volume);
880 src = advance (src, nread << hwshift);
883 read_samples += nread;
889 hw->wpos = (hw->wpos + read_samples) % hw->samples;
893 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
895 return audio_pcm_sw_read (sw, buf, size);
898 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
900 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
904 ldebug ("enabling voice\n");
905 return alsa_voice_ctl (alsa->handle, "capture", 0);
908 ldebug ("disabling voice\n");
909 return alsa_voice_ctl (alsa->handle, "capture", 1);
915 static void *alsa_audio_init (void)
920 static void alsa_audio_fini (void *opaque)
925 static struct audio_option alsa_options[] = {
926 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out,
927 "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
928 {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out,
929 "DAC period size", &conf.period_size_out_overriden, 0},
930 {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out,
931 "DAC buffer size", &conf.buffer_size_out_overriden, 0},
933 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in,
934 "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
935 {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in,
936 "ADC period size", &conf.period_size_in_overriden, 0},
937 {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in,
938 "ADC buffer size", &conf.buffer_size_in_overriden, 0},
940 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
941 "(undocumented)", NULL, 0},
943 {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out,
944 "DAC device name (for instance dmix)", NULL, 0},
946 {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in,
947 "ADC device name", NULL, 0},
949 {"VERBOSE", AUD_OPT_BOOL, &conf.verbose,
950 "Behave in a more verbose way", NULL, 0},
952 {NULL, 0, NULL, NULL, NULL, 0}
955 static struct audio_pcm_ops alsa_pcm_ops = {
969 struct audio_driver alsa_audio_driver = {
970 INIT_FIELD (name = ) "alsa",
971 INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org",
972 INIT_FIELD (options = ) alsa_options,
973 INIT_FIELD (init = ) alsa_audio_init,
974 INIT_FIELD (fini = ) alsa_audio_fini,
975 INIT_FIELD (pcm_ops = ) &alsa_pcm_ops,
976 INIT_FIELD (can_be_default = ) 1,
977 INIT_FIELD (max_voices_out = ) INT_MAX,
978 INIT_FIELD (max_voices_in = ) INT_MAX,
979 INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut),
980 INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn)