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_overridden
;
54 int period_size_in_overridden
;
56 int buffer_size_out_overridden
;
57 int period_size_out_overridden
;
60 #define DEFAULT_BUFFER_SIZE 1024
61 #define DEFAULT_PERIOD_SIZE 256
64 .size_in_usec_out
= 1,
66 .pcm_name_out
= "default",
67 .pcm_name_in
= "default",
69 .buffer_size_in
= 400000,
70 .period_size_in
= 400000 / 4,
71 .buffer_size_out
= 400000,
72 .period_size_out
= 400000 / 4,
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_overridden
= 0,
79 .buffer_size_out_overridden
= 0,
80 .period_size_in_overridden
= 0,
81 .period_size_out_overridden
= 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 return SND_PCM_FORMAT_S32_LE
;
164 return SND_PCM_FORMAT_U32_LE
;
167 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
171 return SND_PCM_FORMAT_U8
;
175 static int alsa_to_audfmt (int alsafmt
, audfmt_e
*fmt
, int *endianness
)
178 case SND_PCM_FORMAT_S8
:
183 case SND_PCM_FORMAT_U8
:
188 case SND_PCM_FORMAT_S16_LE
:
193 case SND_PCM_FORMAT_U16_LE
:
198 case SND_PCM_FORMAT_S16_BE
:
203 case SND_PCM_FORMAT_U16_BE
:
208 case SND_PCM_FORMAT_S32_LE
:
213 case SND_PCM_FORMAT_U32_LE
:
218 case SND_PCM_FORMAT_S32_BE
:
223 case SND_PCM_FORMAT_U32_BE
:
229 dolog ("Unrecognized audio format %d\n", alsafmt
);
236 #if defined DEBUG_MISMATCHES || defined DEBUG
237 static void alsa_dump_info (struct alsa_params_req
*req
,
238 struct alsa_params_obt
*obt
)
240 dolog ("parameter | requested value | obtained value\n");
241 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
242 dolog ("channels | %10d | %10d\n",
243 req
->nchannels
, obt
->nchannels
);
244 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
245 dolog ("============================================\n");
246 dolog ("requested: buffer size %d period size %d\n",
247 req
->buffer_size
, req
->period_size
);
248 dolog ("obtained: samples %ld\n", obt
->samples
);
252 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
255 snd_pcm_sw_params_t
*sw_params
;
257 snd_pcm_sw_params_alloca (&sw_params
);
259 err
= snd_pcm_sw_params_current (handle
, sw_params
);
261 dolog ("Could not fully initialize DAC\n");
262 alsa_logerr (err
, "Failed to get current software parameters\n");
266 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
268 dolog ("Could not fully initialize DAC\n");
269 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
274 err
= snd_pcm_sw_params (handle
, sw_params
);
276 dolog ("Could not fully initialize DAC\n");
277 alsa_logerr (err
, "Failed to set software parameters\n");
282 static int alsa_open (int in
, struct alsa_params_req
*req
,
283 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
286 snd_pcm_hw_params_t
*hw_params
;
287 int err
, freq
, nchannels
;
288 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
289 unsigned int period_size
, buffer_size
;
290 snd_pcm_uframes_t obt_buffer_size
;
291 const char *typ
= in
? "ADC" : "DAC";
294 period_size
= req
->period_size
;
295 buffer_size
= req
->buffer_size
;
296 nchannels
= req
->nchannels
;
298 snd_pcm_hw_params_alloca (&hw_params
);
303 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
307 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
311 err
= snd_pcm_hw_params_any (handle
, hw_params
);
313 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
317 err
= snd_pcm_hw_params_set_access (
320 SND_PCM_ACCESS_RW_INTERLEAVED
323 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
327 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
329 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
333 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
335 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
339 err
= snd_pcm_hw_params_set_channels_near (
345 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
350 if (nchannels
!= 1 && nchannels
!= 2) {
351 alsa_logerr2 (err
, typ
,
352 "Can not handle obtained number of channels %d\n",
357 if (!((in
&& conf
.size_in_usec_in
) || (!in
&& conf
.size_in_usec_out
))) {
359 buffer_size
= DEFAULT_BUFFER_SIZE
;
360 period_size
= DEFAULT_PERIOD_SIZE
;
365 if ((in
&& conf
.size_in_usec_in
) || (!in
&& conf
.size_in_usec_out
)) {
367 err
= snd_pcm_hw_params_set_period_time_near (
374 alsa_logerr2 (err
, typ
,
375 "Failed to set period time %d\n",
381 err
= snd_pcm_hw_params_set_buffer_time_near (
389 alsa_logerr2 (err
, typ
,
390 "Failed to set buffer time %d\n",
397 snd_pcm_uframes_t minval
;
400 minval
= period_size
;
403 err
= snd_pcm_hw_params_get_period_size_min (
411 "Could not get minmal period size for %s\n",
416 if (period_size
< minval
) {
417 if ((in
&& conf
.period_size_in_overridden
)
418 || (!in
&& conf
.period_size_out_overridden
)) {
419 dolog ("%s period size(%d) is less "
420 "than minmal period size(%ld)\n",
425 period_size
= minval
;
429 err
= snd_pcm_hw_params_set_period_size (
436 alsa_logerr2 (err
, typ
, "Failed to set period size %d\n",
442 minval
= buffer_size
;
443 err
= snd_pcm_hw_params_get_buffer_size_min (
448 alsa_logerr (err
, "Could not get minmal buffer size for %s\n",
452 if (buffer_size
< minval
) {
453 if ((in
&& conf
.buffer_size_in_overridden
)
454 || (!in
&& conf
.buffer_size_out_overridden
)) {
456 "%s buffer size(%d) is less "
457 "than minimal buffer size(%ld)\n",
463 buffer_size
= minval
;
467 err
= snd_pcm_hw_params_set_buffer_size (
473 alsa_logerr2 (err
, typ
, "Failed to set buffer size %d\n",
480 dolog ("warning: Buffer size is not set\n");
483 err
= snd_pcm_hw_params (handle
, hw_params
);
485 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
489 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
491 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
495 err
= snd_pcm_prepare (handle
);
497 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
501 if (!in
&& conf
.threshold
) {
502 snd_pcm_uframes_t threshold
;
507 << (req
->fmt
== AUD_FMT_S16
|| req
->fmt
== AUD_FMT_U16
);
509 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
510 alsa_set_threshold (handle
, threshold
);
514 obt
->nchannels
= nchannels
;
516 obt
->samples
= obt_buffer_size
;
519 #if defined DEBUG_MISMATCHES || defined DEBUG
520 if (obt
->fmt
!= req
->fmt
||
521 obt
->nchannels
!= req
->nchannels
||
522 obt
->freq
!= req
->freq
) {
523 dolog ("Audio paramters mismatch for %s\n", typ
);
524 alsa_dump_info (req
, obt
);
529 alsa_dump_info (req
, obt
);
534 alsa_anal_close (&handle
);
538 static int alsa_recover (snd_pcm_t
*handle
)
540 int err
= snd_pcm_prepare (handle
);
542 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
548 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
550 snd_pcm_sframes_t avail
;
552 avail
= snd_pcm_avail_update (handle
);
554 if (avail
== -EPIPE
) {
555 if (!alsa_recover (handle
)) {
556 avail
= snd_pcm_avail_update (handle
);
562 "Could not obtain number of available frames\n");
570 static int alsa_run_out (HWVoiceOut
*hw
)
572 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
573 int rpos
, live
, decr
;
577 snd_pcm_sframes_t avail
;
579 live
= audio_pcm_hw_get_live_out (hw
);
584 avail
= alsa_get_avail (alsa
->handle
);
586 dolog ("Could not get number of available playback frames\n");
590 decr
= audio_MIN (live
, avail
);
594 int left_till_end_samples
= hw
->samples
- rpos
;
595 int len
= audio_MIN (samples
, left_till_end_samples
);
596 snd_pcm_sframes_t written
;
598 src
= hw
->mix_buf
+ rpos
;
599 dst
= advance (alsa
->pcm_buf
, rpos
<< hw
->info
.shift
);
601 hw
->clip (dst
, src
, len
);
604 written
= snd_pcm_writei (alsa
->handle
, dst
, len
);
610 dolog ("Failed to write %d frames (wrote zero)\n", len
);
615 if (alsa_recover (alsa
->handle
)) {
616 alsa_logerr (written
, "Failed to write %d frames\n",
621 dolog ("Recovering from playback xrun\n");
629 alsa_logerr (written
, "Failed to write %d frames to %p\n",
635 rpos
= (rpos
+ written
) % hw
->samples
;
638 dst
= advance (dst
, written
<< hw
->info
.shift
);
648 static void alsa_fini_out (HWVoiceOut
*hw
)
650 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
652 ldebug ("alsa_fini\n");
653 alsa_anal_close (&alsa
->handle
);
656 qemu_free (alsa
->pcm_buf
);
657 alsa
->pcm_buf
= NULL
;
661 static int alsa_init_out (HWVoiceOut
*hw
, audsettings_t
*as
)
663 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
664 struct alsa_params_req req
;
665 struct alsa_params_obt obt
;
666 audfmt_e effective_fmt
;
670 audsettings_t obt_as
;
672 req
.fmt
= aud_to_alsafmt (as
->fmt
);
674 req
.nchannels
= as
->nchannels
;
675 req
.period_size
= conf
.period_size_out
;
676 req
.buffer_size
= conf
.buffer_size_out
;
678 if (alsa_open (0, &req
, &obt
, &handle
)) {
682 err
= alsa_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
684 alsa_anal_close (&handle
);
688 obt_as
.freq
= obt
.freq
;
689 obt_as
.nchannels
= obt
.nchannels
;
690 obt_as
.fmt
= effective_fmt
;
691 obt_as
.endianness
= endianness
;
693 audio_pcm_init_info (&hw
->info
, &obt_as
);
694 hw
->samples
= obt
.samples
;
696 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
697 if (!alsa
->pcm_buf
) {
698 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
699 hw
->samples
, 1 << hw
->info
.shift
);
700 alsa_anal_close (&handle
);
704 alsa
->handle
= handle
;
708 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
713 err
= snd_pcm_drop (handle
);
715 alsa_logerr (err
, "Could not stop %s\n", typ
);
720 err
= snd_pcm_prepare (handle
);
722 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
730 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
732 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
736 ldebug ("enabling voice\n");
737 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
740 ldebug ("disabling voice\n");
741 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
747 static int alsa_init_in (HWVoiceIn
*hw
, audsettings_t
*as
)
749 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
750 struct alsa_params_req req
;
751 struct alsa_params_obt obt
;
754 audfmt_e effective_fmt
;
756 audsettings_t obt_as
;
758 req
.fmt
= aud_to_alsafmt (as
->fmt
);
760 req
.nchannels
= as
->nchannels
;
761 req
.period_size
= conf
.period_size_in
;
762 req
.buffer_size
= conf
.buffer_size_in
;
764 if (alsa_open (1, &req
, &obt
, &handle
)) {
768 err
= alsa_to_audfmt (obt
.fmt
, &effective_fmt
, &endianness
);
770 alsa_anal_close (&handle
);
774 obt_as
.freq
= obt
.freq
;
775 obt_as
.nchannels
= obt
.nchannels
;
776 obt_as
.fmt
= effective_fmt
;
777 obt_as
.endianness
= endianness
;
779 audio_pcm_init_info (&hw
->info
, &obt_as
);
780 hw
->samples
= obt
.samples
;
782 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
783 if (!alsa
->pcm_buf
) {
784 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
785 hw
->samples
, 1 << hw
->info
.shift
);
786 alsa_anal_close (&handle
);
790 alsa
->handle
= handle
;
794 static void alsa_fini_in (HWVoiceIn
*hw
)
796 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
798 alsa_anal_close (&alsa
->handle
);
801 qemu_free (alsa
->pcm_buf
);
802 alsa
->pcm_buf
= NULL
;
806 static int alsa_run_in (HWVoiceIn
*hw
)
808 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
809 int hwshift
= hw
->info
.shift
;
811 int live
= audio_pcm_hw_get_live_in (hw
);
812 int dead
= hw
->samples
- live
;
821 snd_pcm_sframes_t avail
;
822 snd_pcm_uframes_t read_samples
= 0;
828 avail
= alsa_get_avail (alsa
->handle
);
830 dolog ("Could not get number of captured frames\n");
834 if (!avail
&& (snd_pcm_state (alsa
->handle
) == SND_PCM_STATE_PREPARED
)) {
838 decr
= audio_MIN (dead
, avail
);
843 if (hw
->wpos
+ decr
> hw
->samples
) {
844 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
845 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
851 for (i
= 0; i
< 2; ++i
) {
854 snd_pcm_sframes_t nread
;
855 snd_pcm_uframes_t len
;
859 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
860 dst
= hw
->conv_buf
+ bufs
[i
].add
;
863 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
869 dolog ("Failed to read %ld frames (read zero)\n", len
);
874 if (alsa_recover (alsa
->handle
)) {
875 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
879 dolog ("Recovering from capture xrun\n");
889 "Failed to read %ld frames from %p\n",
897 hw
->conv (dst
, src
, nread
, &nominal_volume
);
899 src
= advance (src
, nread
<< hwshift
);
902 read_samples
+= nread
;
908 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
912 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
914 return audio_pcm_sw_read (sw
, buf
, size
);
917 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
919 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
923 ldebug ("enabling voice\n");
924 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
927 ldebug ("disabling voice\n");
928 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
934 static void *alsa_audio_init (void)
939 static void alsa_audio_fini (void *opaque
)
944 static struct audio_option alsa_options
[] = {
945 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_out
,
946 "DAC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
947 {"DAC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_out
,
948 "DAC period size", &conf
.period_size_out_overridden
, 0},
949 {"DAC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_out
,
950 "DAC buffer size", &conf
.buffer_size_out_overridden
, 0},
952 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_in
,
953 "ADC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
954 {"ADC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_in
,
955 "ADC period size", &conf
.period_size_in_overridden
, 0},
956 {"ADC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_in
,
957 "ADC buffer size", &conf
.buffer_size_in_overridden
, 0},
959 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
960 "(undocumented)", NULL
, 0},
962 {"DAC_DEV", AUD_OPT_STR
, &conf
.pcm_name_out
,
963 "DAC device name (for instance dmix)", NULL
, 0},
965 {"ADC_DEV", AUD_OPT_STR
, &conf
.pcm_name_in
,
966 "ADC device name", NULL
, 0},
968 {"VERBOSE", AUD_OPT_BOOL
, &conf
.verbose
,
969 "Behave in a more verbose way", NULL
, 0},
971 {NULL
, 0, NULL
, NULL
, NULL
, 0}
974 static struct audio_pcm_ops alsa_pcm_ops
= {
988 struct audio_driver alsa_audio_driver
= {
989 INIT_FIELD (name
= ) "alsa",
990 INIT_FIELD (descr
= ) "ALSA http://www.alsa-project.org",
991 INIT_FIELD (options
= ) alsa_options
,
992 INIT_FIELD (init
= ) alsa_audio_init
,
993 INIT_FIELD (fini
= ) alsa_audio_fini
,
994 INIT_FIELD (pcm_ops
= ) &alsa_pcm_ops
,
995 INIT_FIELD (can_be_default
= ) 1,
996 INIT_FIELD (max_voices_out
= ) INT_MAX
,
997 INIT_FIELD (max_voices_in
= ) INT_MAX
,
998 INIT_FIELD (voice_size_out
= ) sizeof (ALSAVoiceOut
),
999 INIT_FIELD (voice_size_in
= ) sizeof (ALSAVoiceIn
)