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 {
38 typedef struct ALSAVoiceIn {
48 const char *pcm_name_in;
49 const char *pcm_name_out;
50 unsigned int buffer_size_in;
51 unsigned int period_size_in;
52 unsigned int buffer_size_out;
53 unsigned int period_size_out;
54 unsigned int threshold;
56 int buffer_size_in_overriden;
57 int period_size_in_overriden;
59 int buffer_size_out_overriden;
60 int period_size_out_overriden;
64 .size_in_usec_out = 1,
66 .pcm_name_out = "hw:0,0",
67 .pcm_name_in = "hw:0,0",
69 .buffer_size_in = 400000,
70 .period_size_in = 400000 / 4,
71 .buffer_size_out = 400000,
72 .period_size_out = 400000 / 4,
74 #define DEFAULT_BUFFER_SIZE 1024
75 #define DEFAULT_PERIOD_SIZE 256
76 .buffer_size_in = DEFAULT_BUFFER_SIZE,
77 .period_size_in = DEFAULT_PERIOD_SIZE,
78 .buffer_size_out = DEFAULT_BUFFER_SIZE,
79 .period_size_out = DEFAULT_PERIOD_SIZE,
80 .buffer_size_in_overriden = 0,
81 .buffer_size_out_overriden = 0,
82 .period_size_in_overriden = 0,
83 .period_size_out_overriden = 0,
88 struct alsa_params_req {
92 unsigned int buffer_size;
93 unsigned int period_size;
96 struct alsa_params_obt {
101 snd_pcm_uframes_t samples;
104 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
109 AUD_vlog (AUDIO_CAP, fmt, ap);
112 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
115 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
124 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
127 AUD_vlog (AUDIO_CAP, fmt, ap);
130 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
133 static void alsa_anal_close (snd_pcm_t **handlep)
135 int err = snd_pcm_close (*handlep);
137 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
142 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
144 return audio_pcm_sw_write (sw, buf, len);
147 static int aud_to_alsafmt (audfmt_e fmt)
151 return SND_PCM_FORMAT_S8;
154 return SND_PCM_FORMAT_U8;
157 return SND_PCM_FORMAT_S16_LE;
160 return SND_PCM_FORMAT_U16_LE;
163 dolog ("Internal logic error: Bad audio format %d\n", fmt);
167 return SND_PCM_FORMAT_U8;
171 static int alsa_to_audfmt (int alsafmt, audfmt_e *fmt, int *endianness)
174 case SND_PCM_FORMAT_S8:
179 case SND_PCM_FORMAT_U8:
184 case SND_PCM_FORMAT_S16_LE:
189 case SND_PCM_FORMAT_U16_LE:
194 case SND_PCM_FORMAT_S16_BE:
199 case SND_PCM_FORMAT_U16_BE:
205 dolog ("Unrecognized audio format %d\n", alsafmt);
212 #if defined DEBUG_MISMATCHES || defined DEBUG
213 static void alsa_dump_info (struct alsa_params_req *req,
214 struct alsa_params_obt *obt)
216 dolog ("parameter | requested value | obtained value\n");
217 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
218 dolog ("channels | %10d | %10d\n",
219 req->nchannels, obt->nchannels);
220 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
221 dolog ("============================================\n");
222 dolog ("requested: buffer size %d period size %d\n",
223 req->buffer_size, req->period_size);
224 dolog ("obtained: samples %ld\n", obt->samples);
228 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
231 snd_pcm_sw_params_t *sw_params;
233 snd_pcm_sw_params_alloca (&sw_params);
235 err = snd_pcm_sw_params_current (handle, sw_params);
237 dolog ("Could not fully initialize DAC\n");
238 alsa_logerr (err, "Failed to get current software parameters\n");
242 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
244 dolog ("Could not fully initialize DAC\n");
245 alsa_logerr (err, "Failed to set software threshold to %ld\n",
250 err = snd_pcm_sw_params (handle, sw_params);
252 dolog ("Could not fully initialize DAC\n");
253 alsa_logerr (err, "Failed to set software parameters\n");
258 static int alsa_open (int in, struct alsa_params_req *req,
259 struct alsa_params_obt *obt, snd_pcm_t **handlep)
262 snd_pcm_hw_params_t *hw_params;
263 int err, freq, nchannels;
264 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
265 unsigned int period_size, buffer_size;
266 snd_pcm_uframes_t obt_buffer_size;
267 const char *typ = in ? "ADC" : "DAC";
270 period_size = req->period_size;
271 buffer_size = req->buffer_size;
272 nchannels = req->nchannels;
274 snd_pcm_hw_params_alloca (&hw_params);
279 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
283 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
287 err = snd_pcm_hw_params_any (handle, hw_params);
289 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
293 err = snd_pcm_hw_params_set_access (
296 SND_PCM_ACCESS_RW_INTERLEAVED
299 alsa_logerr2 (err, typ, "Failed to set access type\n");
303 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
305 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
309 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
311 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
315 err = snd_pcm_hw_params_set_channels_near (
321 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
326 if (nchannels != 1 && nchannels != 2) {
327 alsa_logerr2 (err, typ,
328 "Can not handle obtained number of channels %d\n",
333 if (!((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out))) {
335 buffer_size = DEFAULT_BUFFER_SIZE;
336 period_size= DEFAULT_PERIOD_SIZE;
341 if ((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out)) {
343 err = snd_pcm_hw_params_set_period_time_near (
350 alsa_logerr2 (err, typ,
351 "Failed to set period time %d\n",
357 err = snd_pcm_hw_params_set_buffer_time_near (
365 alsa_logerr2 (err, typ,
366 "Failed to set buffer time %d\n",
373 snd_pcm_uframes_t minval;
376 minval = period_size;
379 err = snd_pcm_hw_params_get_period_size_min (
387 "Could not get minmal period size for %s\n",
392 if (period_size < minval) {
393 if ((in && conf.period_size_in_overriden)
394 || (!in && conf.period_size_out_overriden)) {
395 dolog ("%s period size(%d) is less "
396 "than minmal period size(%ld)\n",
401 period_size = minval;
405 err = snd_pcm_hw_params_set_period_size (
412 alsa_logerr2 (err, typ, "Failed to set period size %d\n",
418 minval = buffer_size;
419 err = snd_pcm_hw_params_get_buffer_size_min (
424 alsa_logerr (err, "Could not get minmal buffer size for %s\n",
428 if (buffer_size < minval) {
429 if ((in && conf.buffer_size_in_overriden)
430 || (!in && conf.buffer_size_out_overriden)) {
432 "%s buffer size(%d) is less "
433 "than minimal buffer size(%ld)\n",
439 buffer_size = minval;
443 err = snd_pcm_hw_params_set_buffer_size (
449 alsa_logerr2 (err, typ, "Failed to set buffer size %d\n",
456 dolog ("warning: Buffer size is not set\n");
459 err = snd_pcm_hw_params (handle, hw_params);
461 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
465 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
467 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
471 err = snd_pcm_prepare (handle);
473 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
477 obt->can_pause = snd_pcm_hw_params_can_pause (hw_params);
478 if (obt->can_pause < 0) {
479 alsa_logerr (err, "Could not get pause capability for %s\n", typ);
483 if (!in && conf.threshold) {
484 snd_pcm_uframes_t threshold;
489 << (req->fmt == AUD_FMT_S16 || req->fmt == AUD_FMT_U16);
491 threshold = (conf.threshold * bytes_per_sec) / 1000;
492 alsa_set_threshold (handle, threshold);
496 obt->nchannels = nchannels;
498 obt->samples = obt_buffer_size;
501 #if defined DEBUG_MISMATCHES || defined DEBUG
502 if (obt->fmt != req->fmt ||
503 obt->nchannels != req->nchannels ||
504 obt->freq != req->freq) {
505 dolog ("Audio paramters mismatch for %s\n", typ);
506 alsa_dump_info (req, obt);
511 alsa_dump_info (req, obt);
516 alsa_anal_close (&handle);
520 static int alsa_recover (snd_pcm_t *handle)
522 int err = snd_pcm_prepare (handle);
524 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
530 static int alsa_run_out (HWVoiceOut *hw)
532 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
533 int rpos, live, decr;
537 snd_pcm_sframes_t avail;
539 live = audio_pcm_hw_get_live_out (hw);
544 avail = snd_pcm_avail_update (alsa->handle);
546 if (avail == -EPIPE) {
547 if (!alsa_recover (alsa->handle)) {
548 avail = snd_pcm_avail_update (alsa->handle);
555 alsa_logerr (avail, "Could not get amount free space\n");
560 decr = audio_MIN (live, avail);
564 int left_till_end_samples = hw->samples - rpos;
565 int convert_samples = audio_MIN (samples, left_till_end_samples);
566 snd_pcm_sframes_t written;
568 src = hw->mix_buf + rpos;
569 dst = advance (alsa->pcm_buf, rpos << hw->info.shift);
571 hw->clip (dst, src, convert_samples);
574 written = snd_pcm_writei (alsa->handle, dst, convert_samples);
579 if (!alsa_recover (alsa->handle)) {
583 "Failed to write %d frames to %p, handle %p not prepared\n",
594 alsa_logerr (written, "Failed to write %d frames to %p\n",
595 convert_samples, dst);
600 mixeng_clear (src, written);
601 rpos = (rpos + written) % hw->samples;
610 static void alsa_fini_out (HWVoiceOut *hw)
612 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
614 ldebug ("alsa_fini\n");
615 alsa_anal_close (&alsa->handle);
618 qemu_free (alsa->pcm_buf);
619 alsa->pcm_buf = NULL;
623 static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as)
625 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
626 struct alsa_params_req req;
627 struct alsa_params_obt obt;
628 audfmt_e effective_fmt;
632 audsettings_t obt_as;
634 req.fmt = aud_to_alsafmt (as->fmt);
636 req.nchannels = as->nchannels;
637 req.period_size = conf.period_size_out;
638 req.buffer_size = conf.buffer_size_out;
640 if (alsa_open (0, &req, &obt, &handle)) {
644 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
646 alsa_anal_close (&handle);
650 obt_as.freq = obt.freq;
651 obt_as.nchannels = obt.nchannels;
652 obt_as.fmt = effective_fmt;
654 audio_pcm_init_info (
657 audio_need_to_swap_endian (endianness)
659 alsa->can_pause = obt.can_pause;
660 hw->samples = obt.samples;
662 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
663 if (!alsa->pcm_buf) {
664 dolog ("Could not allocate DAC buffer (%d bytes)\n",
665 hw->samples << hw->info.shift);
666 alsa_anal_close (&handle);
670 alsa->handle = handle;
671 alsa->was_enabled = 0;
675 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
678 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
682 ldebug ("enabling voice\n");
683 audio_pcm_info_clear_buf (&hw->info, alsa->pcm_buf, hw->samples);
684 if (alsa->can_pause) {
685 /* Why this was_enabled madness is needed at all?? */
686 if (alsa->was_enabled) {
687 err = snd_pcm_pause (alsa->handle, 0);
689 alsa_logerr (err, "Failed to resume playing\n");
690 /* not fatal really */
694 alsa->was_enabled = 1;
700 ldebug ("disabling voice\n");
701 if (alsa->can_pause) {
702 err = snd_pcm_pause (alsa->handle, 1);
704 alsa_logerr (err, "Failed to stop playing\n");
705 /* not fatal really */
713 static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as)
715 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
716 struct alsa_params_req req;
717 struct alsa_params_obt obt;
720 audfmt_e effective_fmt;
722 audsettings_t obt_as;
724 req.fmt = aud_to_alsafmt (as->fmt);
726 req.nchannels = as->nchannels;
727 req.period_size = conf.period_size_in;
728 req.buffer_size = conf.buffer_size_in;
730 if (alsa_open (1, &req, &obt, &handle)) {
734 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
736 alsa_anal_close (&handle);
740 obt_as.freq = obt.freq;
741 obt_as.nchannels = obt.nchannels;
742 obt_as.fmt = effective_fmt;
744 audio_pcm_init_info (
747 audio_need_to_swap_endian (endianness)
749 alsa->can_pause = obt.can_pause;
750 hw->samples = obt.samples;
752 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
753 if (!alsa->pcm_buf) {
754 dolog ("Could not allocate ADC buffer (%d bytes)\n",
755 hw->samples << hw->info.shift);
756 alsa_anal_close (&handle);
760 alsa->handle = handle;
764 static void alsa_fini_in (HWVoiceIn *hw)
766 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
768 alsa_anal_close (&alsa->handle);
771 qemu_free (alsa->pcm_buf);
772 alsa->pcm_buf = NULL;
776 static int alsa_run_in (HWVoiceIn *hw)
778 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
779 int hwshift = hw->info.shift;
781 int live = audio_pcm_hw_get_live_in (hw);
782 int dead = hw->samples - live;
791 snd_pcm_uframes_t read_samples = 0;
797 if (hw->wpos + dead > hw->samples) {
798 bufs[0].len = (hw->samples - hw->wpos);
799 bufs[1].len = (dead - (hw->samples - hw->wpos));
806 for (i = 0; i < 2; ++i) {
809 snd_pcm_sframes_t nread;
810 snd_pcm_uframes_t len;
814 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
815 dst = hw->conv_buf + bufs[i].add;
818 nread = snd_pcm_readi (alsa->handle, src, len);
823 if (!alsa_recover (alsa->handle)) {
827 "Failed to read %ld frames from %p, "
828 "handle %p not prepared\n",
841 "Failed to read %ld frames from %p\n",
849 hw->conv (dst, src, nread, &nominal_volume);
851 src = advance (src, nread << hwshift);
854 read_samples += nread;
860 hw->wpos = (hw->wpos + read_samples) % hw->samples;
864 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
866 return audio_pcm_sw_read (sw, buf, size);
869 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
876 static void *alsa_audio_init (void)
881 static void alsa_audio_fini (void *opaque)
886 static struct audio_option alsa_options[] = {
887 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out,
888 "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
889 {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out,
890 "DAC period size", &conf.period_size_out_overriden, 0},
891 {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out,
892 "DAC buffer size", &conf.buffer_size_out_overriden, 0},
894 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in,
895 "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
896 {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in,
897 "ADC period size", &conf.period_size_in_overriden, 0},
898 {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in,
899 "ADC buffer size", &conf.buffer_size_in_overriden, 0},
901 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
902 "(undocumented)", NULL, 0},
904 {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out,
905 "DAC device name (for instance dmix)", NULL, 0},
907 {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in,
908 "ADC device name", NULL, 0},
909 {NULL, 0, NULL, NULL, NULL, 0}
912 static struct audio_pcm_ops alsa_pcm_ops = {
926 struct audio_driver alsa_audio_driver = {
927 INIT_FIELD (name = ) "alsa",
928 INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org",
929 INIT_FIELD (options = ) alsa_options,
930 INIT_FIELD (init = ) alsa_audio_init,
931 INIT_FIELD (fini = ) alsa_audio_fini,
932 INIT_FIELD (pcm_ops = ) &alsa_pcm_ops,
933 INIT_FIELD (can_be_default = ) 1,
934 INIT_FIELD (max_voices_out = ) INT_MAX,
935 INIT_FIELD (max_voices_in = ) INT_MAX,
936 INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut),
937 INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn)