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>
25 #include "qemu-common.h"
28 #define AUDIO_CAP "alsa"
29 #include "audio_int.h"
31 typedef struct ALSAVoiceOut
{
37 typedef struct ALSAVoiceIn
{
46 const char *pcm_name_in
;
47 const char *pcm_name_out
;
48 unsigned int buffer_size_in
;
49 unsigned int period_size_in
;
50 unsigned int buffer_size_out
;
51 unsigned int period_size_out
;
52 unsigned int threshold
;
54 int buffer_size_in_overridden
;
55 int period_size_in_overridden
;
57 int buffer_size_out_overridden
;
58 int period_size_out_overridden
;
61 #define DEFAULT_BUFFER_SIZE 1024
62 #define DEFAULT_PERIOD_SIZE 256
65 .size_in_usec_out
= 1,
67 .pcm_name_out
= "default",
68 .pcm_name_in
= "default",
70 .buffer_size_in
= 400000,
71 .period_size_in
= 400000 / 4,
72 .buffer_size_out
= 400000,
73 .period_size_out
= 400000 / 4,
75 .buffer_size_in
= DEFAULT_BUFFER_SIZE
* 4,
76 .period_size_in
= DEFAULT_PERIOD_SIZE
* 4,
77 .buffer_size_out
= DEFAULT_BUFFER_SIZE
,
78 .period_size_out
= DEFAULT_PERIOD_SIZE
,
79 .buffer_size_in_overridden
= 0,
80 .buffer_size_out_overridden
= 0,
81 .period_size_in_overridden
= 0,
82 .period_size_out_overridden
= 0,
88 struct alsa_params_req
{
92 unsigned int buffer_size
;
93 unsigned int period_size
;
96 struct alsa_params_obt
{
100 snd_pcm_uframes_t samples
;
103 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
108 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
111 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
114 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
123 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
126 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
129 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
132 static void alsa_anal_close (snd_pcm_t
**handlep
)
134 int err
= snd_pcm_close (*handlep
);
136 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
141 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
143 return audio_pcm_sw_write (sw
, buf
, len
);
146 static int aud_to_alsafmt (audfmt_e fmt
)
150 return SND_PCM_FORMAT_S8
;
153 return SND_PCM_FORMAT_U8
;
156 return SND_PCM_FORMAT_S16_LE
;
159 return SND_PCM_FORMAT_U16_LE
;
162 return SND_PCM_FORMAT_S32_LE
;
165 return SND_PCM_FORMAT_U32_LE
;
168 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
172 return SND_PCM_FORMAT_U8
;
176 static int alsa_to_audfmt (int alsafmt
, audfmt_e
*fmt
, int *endianness
)
179 case SND_PCM_FORMAT_S8
:
184 case SND_PCM_FORMAT_U8
:
189 case SND_PCM_FORMAT_S16_LE
:
194 case SND_PCM_FORMAT_U16_LE
:
199 case SND_PCM_FORMAT_S16_BE
:
204 case SND_PCM_FORMAT_U16_BE
:
209 case SND_PCM_FORMAT_S32_LE
:
214 case SND_PCM_FORMAT_U32_LE
:
219 case SND_PCM_FORMAT_S32_BE
:
224 case SND_PCM_FORMAT_U32_BE
:
230 dolog ("Unrecognized audio format %d\n", alsafmt
);
237 #if defined DEBUG_MISMATCHES || defined DEBUG
238 static void alsa_dump_info (struct alsa_params_req
*req
,
239 struct alsa_params_obt
*obt
)
241 dolog ("parameter | requested value | obtained value\n");
242 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
243 dolog ("channels | %10d | %10d\n",
244 req
->nchannels
, obt
->nchannels
);
245 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
246 dolog ("============================================\n");
247 dolog ("requested: buffer size %d period size %d\n",
248 req
->buffer_size
, req
->period_size
);
249 dolog ("obtained: samples %ld\n", obt
->samples
);
253 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
256 snd_pcm_sw_params_t
*sw_params
;
258 snd_pcm_sw_params_alloca (&sw_params
);
260 err
= snd_pcm_sw_params_current (handle
, sw_params
);
262 dolog ("Could not fully initialize DAC\n");
263 alsa_logerr (err
, "Failed to get current software parameters\n");
267 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
269 dolog ("Could not fully initialize DAC\n");
270 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
275 err
= snd_pcm_sw_params (handle
, sw_params
);
277 dolog ("Could not fully initialize DAC\n");
278 alsa_logerr (err
, "Failed to set software parameters\n");
283 static int alsa_open (int in
, struct alsa_params_req
*req
,
284 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
287 snd_pcm_hw_params_t
*hw_params
;
288 int err
, freq
, nchannels
;
289 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
290 unsigned int period_size
, buffer_size
;
291 snd_pcm_uframes_t obt_buffer_size
;
292 const char *typ
= in
? "ADC" : "DAC";
295 period_size
= req
->period_size
;
296 buffer_size
= req
->buffer_size
;
297 nchannels
= req
->nchannels
;
299 snd_pcm_hw_params_alloca (&hw_params
);
304 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
308 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
312 err
= snd_pcm_hw_params_any (handle
, hw_params
);
314 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
318 err
= snd_pcm_hw_params_set_access (
321 SND_PCM_ACCESS_RW_INTERLEAVED
324 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
328 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
330 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
334 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
336 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
340 err
= snd_pcm_hw_params_set_channels_near (
346 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
351 if (nchannels
!= 1 && nchannels
!= 2) {
352 alsa_logerr2 (err
, typ
,
353 "Can not handle obtained number of channels %d\n",
358 if (!((in
&& conf
.size_in_usec_in
) || (!in
&& conf
.size_in_usec_out
))) {
360 buffer_size
= DEFAULT_BUFFER_SIZE
;
361 period_size
= DEFAULT_PERIOD_SIZE
;
366 if ((in
&& conf
.size_in_usec_in
) || (!in
&& conf
.size_in_usec_out
)) {
368 err
= snd_pcm_hw_params_set_period_time_near (
375 alsa_logerr2 (err
, typ
,
376 "Failed to set period time %d\n",
382 err
= snd_pcm_hw_params_set_buffer_time_near (
390 alsa_logerr2 (err
, typ
,
391 "Failed to set buffer time %d\n",
398 snd_pcm_uframes_t minval
;
401 minval
= period_size
;
404 err
= snd_pcm_hw_params_get_period_size_min (
412 "Could not get minmal period size for %s\n",
417 if (period_size
< minval
) {
418 if ((in
&& conf
.period_size_in_overridden
)
419 || (!in
&& conf
.period_size_out_overridden
)) {
420 dolog ("%s period size(%d) is less "
421 "than minmal period size(%ld)\n",
426 period_size
= minval
;
430 err
= snd_pcm_hw_params_set_period_size (
437 alsa_logerr2 (err
, typ
, "Failed to set period size %d\n",
443 minval
= buffer_size
;
444 err
= snd_pcm_hw_params_get_buffer_size_min (
449 alsa_logerr (err
, "Could not get minmal buffer size for %s\n",
453 if (buffer_size
< minval
) {
454 if ((in
&& conf
.buffer_size_in_overridden
)
455 || (!in
&& conf
.buffer_size_out_overridden
)) {
457 "%s buffer size(%d) is less "
458 "than minimal buffer size(%ld)\n",
464 buffer_size
= minval
;
468 err
= snd_pcm_hw_params_set_buffer_size (
474 alsa_logerr2 (err
, typ
, "Failed to set buffer size %d\n",
481 dolog ("warning: Buffer size is not set\n");
484 err
= snd_pcm_hw_params (handle
, hw_params
);
486 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
490 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
492 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
496 err
= snd_pcm_prepare (handle
);
498 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
502 if (!in
&& conf
.threshold
) {
503 snd_pcm_uframes_t threshold
;
508 << (req
->fmt
== AUD_FMT_S16
|| req
->fmt
== AUD_FMT_U16
);
510 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
511 alsa_set_threshold (handle
, threshold
);
515 obt
->nchannels
= nchannels
;
517 obt
->samples
= obt_buffer_size
;
520 #if defined DEBUG_MISMATCHES || defined DEBUG
521 if (obt
->fmt
!= req
->fmt
||
522 obt
->nchannels
!= req
->nchannels
||
523 obt
->freq
!= req
->freq
) {
524 dolog ("Audio paramters mismatch for %s\n", typ
);
525 alsa_dump_info (req
, obt
);
530 alsa_dump_info (req
, obt
);
535 alsa_anal_close (&handle
);
539 static int alsa_recover (snd_pcm_t
*handle
)
541 int err
= snd_pcm_prepare (handle
);
543 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
549 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
551 snd_pcm_sframes_t avail
;
553 avail
= snd_pcm_avail_update (handle
);
555 if (avail
== -EPIPE
) {
556 if (!alsa_recover (handle
)) {
557 avail
= snd_pcm_avail_update (handle
);
563 "Could not obtain number of available frames\n");
571 static int alsa_run_out (HWVoiceOut
*hw
)
573 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
574 int rpos
, live
, decr
;
578 snd_pcm_sframes_t avail
;
580 live
= audio_pcm_hw_get_live_out (hw
);
585 avail
= alsa_get_avail (alsa
->handle
);
587 dolog ("Could not get number of available playback frames\n");
591 decr
= audio_MIN (live
, avail
);
595 int left_till_end_samples
= hw
->samples
- rpos
;
596 int len
= audio_MIN (samples
, left_till_end_samples
);
597 snd_pcm_sframes_t written
;
599 src
= hw
->mix_buf
+ rpos
;
600 dst
= advance (alsa
->pcm_buf
, rpos
<< hw
->info
.shift
);
602 hw
->clip (dst
, src
, len
);
605 written
= snd_pcm_writei (alsa
->handle
, dst
, len
);
611 dolog ("Failed to write %d frames (wrote zero)\n", len
);
616 if (alsa_recover (alsa
->handle
)) {
617 alsa_logerr (written
, "Failed to write %d frames\n",
622 dolog ("Recovering from playback xrun\n");
630 alsa_logerr (written
, "Failed to write %d frames to %p\n",
636 rpos
= (rpos
+ written
) % hw
->samples
;
639 dst
= advance (dst
, written
<< hw
->info
.shift
);
649 static void alsa_fini_out (HWVoiceOut
*hw
)
651 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
653 ldebug ("alsa_fini\n");
654 alsa_anal_close (&alsa
->handle
);
657 qemu_free (alsa
->pcm_buf
);
658 alsa
->pcm_buf
= NULL
;
662 static int alsa_init_out (HWVoiceOut
*hw
, audsettings_t
*as
)
664 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
665 struct alsa_params_req req
;
666 struct alsa_params_obt obt
;
667 audfmt_e effective_fmt
;
671 audsettings_t obt_as
;
673 req
.fmt
= aud_to_alsafmt (as
->fmt
);
675 req
.nchannels
= as
->nchannels
;
676 req
.period_size
= conf
.period_size_out
;
677 req
.buffer_size
= conf
.buffer_size_out
;
679 if (alsa_open (0, &req
, &obt
, &handle
)) {
683 err
= alsa_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
685 alsa_anal_close (&handle
);
689 obt_as
.freq
= obt
.freq
;
690 obt_as
.nchannels
= obt
.nchannels
;
691 obt_as
.fmt
= effective_fmt
;
692 obt_as
.endianness
= endianness
;
694 audio_pcm_init_info (&hw
->info
, &obt_as
);
695 hw
->samples
= obt
.samples
;
697 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
698 if (!alsa
->pcm_buf
) {
699 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
700 hw
->samples
, 1 << hw
->info
.shift
);
701 alsa_anal_close (&handle
);
705 alsa
->handle
= handle
;
709 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
714 err
= snd_pcm_drop (handle
);
716 alsa_logerr (err
, "Could not stop %s\n", typ
);
721 err
= snd_pcm_prepare (handle
);
723 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
731 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
733 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
737 ldebug ("enabling voice\n");
738 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
741 ldebug ("disabling voice\n");
742 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
748 static int alsa_init_in (HWVoiceIn
*hw
, audsettings_t
*as
)
750 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
751 struct alsa_params_req req
;
752 struct alsa_params_obt obt
;
755 audfmt_e effective_fmt
;
757 audsettings_t obt_as
;
759 req
.fmt
= aud_to_alsafmt (as
->fmt
);
761 req
.nchannels
= as
->nchannels
;
762 req
.period_size
= conf
.period_size_in
;
763 req
.buffer_size
= conf
.buffer_size_in
;
765 if (alsa_open (1, &req
, &obt
, &handle
)) {
769 err
= alsa_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
771 alsa_anal_close (&handle
);
775 obt_as
.freq
= obt
.freq
;
776 obt_as
.nchannels
= obt
.nchannels
;
777 obt_as
.fmt
= effective_fmt
;
778 obt_as
.endianness
= endianness
;
780 audio_pcm_init_info (&hw
->info
, &obt_as
);
781 hw
->samples
= obt
.samples
;
783 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
784 if (!alsa
->pcm_buf
) {
785 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
786 hw
->samples
, 1 << hw
->info
.shift
);
787 alsa_anal_close (&handle
);
791 alsa
->handle
= handle
;
795 static void alsa_fini_in (HWVoiceIn
*hw
)
797 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
799 alsa_anal_close (&alsa
->handle
);
802 qemu_free (alsa
->pcm_buf
);
803 alsa
->pcm_buf
= NULL
;
807 static int alsa_run_in (HWVoiceIn
*hw
)
809 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
810 int hwshift
= hw
->info
.shift
;
812 int live
= audio_pcm_hw_get_live_in (hw
);
813 int dead
= hw
->samples
- live
;
822 snd_pcm_sframes_t avail
;
823 snd_pcm_uframes_t read_samples
= 0;
829 avail
= alsa_get_avail (alsa
->handle
);
831 dolog ("Could not get number of captured frames\n");
835 if (!avail
&& (snd_pcm_state (alsa
->handle
) == SND_PCM_STATE_PREPARED
)) {
839 decr
= audio_MIN (dead
, avail
);
844 if (hw
->wpos
+ decr
> hw
->samples
) {
845 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
846 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
852 for (i
= 0; i
< 2; ++i
) {
855 snd_pcm_sframes_t nread
;
856 snd_pcm_uframes_t len
;
860 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
861 dst
= hw
->conv_buf
+ bufs
[i
].add
;
864 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
870 dolog ("Failed to read %ld frames (read zero)\n", len
);
875 if (alsa_recover (alsa
->handle
)) {
876 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
880 dolog ("Recovering from capture xrun\n");
890 "Failed to read %ld frames from %p\n",
898 hw
->conv (dst
, src
, nread
, &nominal_volume
);
900 src
= advance (src
, nread
<< hwshift
);
903 read_samples
+= nread
;
909 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
913 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
915 return audio_pcm_sw_read (sw
, buf
, size
);
918 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
920 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
924 ldebug ("enabling voice\n");
925 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
928 ldebug ("disabling voice\n");
929 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
935 static void *alsa_audio_init (void)
940 static void alsa_audio_fini (void *opaque
)
945 static struct audio_option alsa_options
[] = {
946 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_out
,
947 "DAC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
948 {"DAC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_out
,
949 "DAC period size", &conf
.period_size_out_overridden
, 0},
950 {"DAC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_out
,
951 "DAC buffer size", &conf
.buffer_size_out_overridden
, 0},
953 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_in
,
954 "ADC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
955 {"ADC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_in
,
956 "ADC period size", &conf
.period_size_in_overridden
, 0},
957 {"ADC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_in
,
958 "ADC buffer size", &conf
.buffer_size_in_overridden
, 0},
960 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
961 "(undocumented)", NULL
, 0},
963 {"DAC_DEV", AUD_OPT_STR
, &conf
.pcm_name_out
,
964 "DAC device name (for instance dmix)", NULL
, 0},
966 {"ADC_DEV", AUD_OPT_STR
, &conf
.pcm_name_in
,
967 "ADC device name", NULL
, 0},
969 {"VERBOSE", AUD_OPT_BOOL
, &conf
.verbose
,
970 "Behave in a more verbose way", NULL
, 0},
972 {NULL
, 0, NULL
, NULL
, NULL
, 0}
975 static struct audio_pcm_ops alsa_pcm_ops
= {
989 struct audio_driver alsa_audio_driver
= {
990 INIT_FIELD (name
= ) "alsa",
991 INIT_FIELD (descr
= ) "ALSA http://www.alsa-project.org",
992 INIT_FIELD (options
= ) alsa_options
,
993 INIT_FIELD (init
= ) alsa_audio_init
,
994 INIT_FIELD (fini
= ) alsa_audio_fini
,
995 INIT_FIELD (pcm_ops
= ) &alsa_pcm_ops
,
996 INIT_FIELD (can_be_default
= ) 1,
997 INIT_FIELD (max_voices_out
= ) INT_MAX
,
998 INIT_FIELD (max_voices_in
= ) INT_MAX
,
999 INIT_FIELD (voice_size_out
= ) sizeof (ALSAVoiceOut
),
1000 INIT_FIELD (voice_size_in
= ) sizeof (ALSAVoiceIn
)