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
25 #include "qemu/osdep.h"
26 #include <alsa/asoundlib.h>
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
32 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
45 typedef struct ALSAVoiceOut
{
51 struct pollhlp pollhlp
;
55 typedef struct ALSAVoiceIn
{
59 struct pollhlp pollhlp
;
63 struct alsa_params_req
{
69 struct alsa_params_obt
{
74 snd_pcm_uframes_t samples
;
77 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
82 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
85 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
88 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
97 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
100 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
103 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
106 static void alsa_fini_poll (struct pollhlp
*hlp
)
109 struct pollfd
*pfds
= hlp
->pfds
;
112 for (i
= 0; i
< hlp
->count
; ++i
) {
113 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
122 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
124 int err
= snd_pcm_close (*handlep
);
126 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
131 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
133 alsa_fini_poll (hlp
);
134 alsa_anal_close1 (handlep
);
137 static int alsa_recover (snd_pcm_t
*handle
)
139 int err
= snd_pcm_prepare (handle
);
141 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
147 static int alsa_resume (snd_pcm_t
*handle
)
149 int err
= snd_pcm_resume (handle
);
151 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
157 static void alsa_poll_handler (void *opaque
)
160 snd_pcm_state_t state
;
161 struct pollhlp
*hlp
= opaque
;
162 unsigned short revents
;
164 count
= poll (hlp
->pfds
, hlp
->count
, 0);
166 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
174 /* XXX: ALSA example uses initial count, not the one returned by
176 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
177 hlp
->count
, &revents
);
179 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
183 if (!(revents
& hlp
->mask
)) {
184 trace_alsa_revents(revents
);
188 state
= snd_pcm_state (hlp
->handle
);
190 case SND_PCM_STATE_SETUP
:
191 alsa_recover (hlp
->handle
);
194 case SND_PCM_STATE_XRUN
:
195 alsa_recover (hlp
->handle
);
198 case SND_PCM_STATE_SUSPENDED
:
199 alsa_resume (hlp
->handle
);
202 case SND_PCM_STATE_PREPARED
:
203 audio_run(hlp
->s
, "alsa run (prepared)");
206 case SND_PCM_STATE_RUNNING
:
207 audio_run(hlp
->s
, "alsa run (running)");
211 dolog ("Unexpected state %d\n", state
);
215 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
220 count
= snd_pcm_poll_descriptors_count (handle
);
222 dolog ("Could not initialize poll mode\n"
223 "Invalid number of poll descriptors %d\n", count
);
227 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
229 dolog ("Could not initialize poll mode\n");
233 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
235 alsa_logerr (err
, "Could not initialize poll mode\n"
236 "Could not obtain poll descriptors\n");
241 for (i
= 0; i
< count
; ++i
) {
242 if (pfds
[i
].events
& POLLIN
) {
243 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
245 if (pfds
[i
].events
& POLLOUT
) {
246 trace_alsa_pollout(i
, pfds
[i
].fd
);
247 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
249 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
254 hlp
->handle
= handle
;
259 static int alsa_poll_out (HWVoiceOut
*hw
)
261 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
263 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
266 static int alsa_poll_in (HWVoiceIn
*hw
)
268 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
270 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
273 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
275 return audio_pcm_sw_write (sw
, buf
, len
);
278 static snd_pcm_format_t
aud_to_alsafmt (AudioFormat fmt
, int endianness
)
281 case AUDIO_FORMAT_S8
:
282 return SND_PCM_FORMAT_S8
;
284 case AUDIO_FORMAT_U8
:
285 return SND_PCM_FORMAT_U8
;
287 case AUDIO_FORMAT_S16
:
289 return SND_PCM_FORMAT_S16_BE
;
292 return SND_PCM_FORMAT_S16_LE
;
295 case AUDIO_FORMAT_U16
:
297 return SND_PCM_FORMAT_U16_BE
;
300 return SND_PCM_FORMAT_U16_LE
;
303 case AUDIO_FORMAT_S32
:
305 return SND_PCM_FORMAT_S32_BE
;
308 return SND_PCM_FORMAT_S32_LE
;
311 case AUDIO_FORMAT_U32
:
313 return SND_PCM_FORMAT_U32_BE
;
316 return SND_PCM_FORMAT_U32_LE
;
320 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
324 return SND_PCM_FORMAT_U8
;
328 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, AudioFormat
*fmt
,
332 case SND_PCM_FORMAT_S8
:
334 *fmt
= AUDIO_FORMAT_S8
;
337 case SND_PCM_FORMAT_U8
:
339 *fmt
= AUDIO_FORMAT_U8
;
342 case SND_PCM_FORMAT_S16_LE
:
344 *fmt
= AUDIO_FORMAT_S16
;
347 case SND_PCM_FORMAT_U16_LE
:
349 *fmt
= AUDIO_FORMAT_U16
;
352 case SND_PCM_FORMAT_S16_BE
:
354 *fmt
= AUDIO_FORMAT_S16
;
357 case SND_PCM_FORMAT_U16_BE
:
359 *fmt
= AUDIO_FORMAT_U16
;
362 case SND_PCM_FORMAT_S32_LE
:
364 *fmt
= AUDIO_FORMAT_S32
;
367 case SND_PCM_FORMAT_U32_LE
:
369 *fmt
= AUDIO_FORMAT_U32
;
372 case SND_PCM_FORMAT_S32_BE
:
374 *fmt
= AUDIO_FORMAT_S32
;
377 case SND_PCM_FORMAT_U32_BE
:
379 *fmt
= AUDIO_FORMAT_U32
;
383 dolog ("Unrecognized audio format %d\n", alsafmt
);
390 static void alsa_dump_info (struct alsa_params_req
*req
,
391 struct alsa_params_obt
*obt
,
392 snd_pcm_format_t obtfmt
,
393 AudiodevAlsaPerDirectionOptions
*apdo
)
395 dolog("parameter | requested value | obtained value\n");
396 dolog("format | %10d | %10d\n", req
->fmt
, obtfmt
);
397 dolog("channels | %10d | %10d\n",
398 req
->nchannels
, obt
->nchannels
);
399 dolog("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
400 dolog("============================================\n");
401 dolog("requested: buffer len %" PRId32
" period len %" PRId32
"\n",
402 apdo
->buffer_length
, apdo
->period_length
);
403 dolog("obtained: samples %ld\n", obt
->samples
);
406 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
409 snd_pcm_sw_params_t
*sw_params
;
411 snd_pcm_sw_params_alloca (&sw_params
);
413 err
= snd_pcm_sw_params_current (handle
, sw_params
);
415 dolog ("Could not fully initialize DAC\n");
416 alsa_logerr (err
, "Failed to get current software parameters\n");
420 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
422 dolog ("Could not fully initialize DAC\n");
423 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
428 err
= snd_pcm_sw_params (handle
, sw_params
);
430 dolog ("Could not fully initialize DAC\n");
431 alsa_logerr (err
, "Failed to set software parameters\n");
436 static int alsa_open(bool in
, struct alsa_params_req
*req
,
437 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
440 AudiodevAlsaOptions
*aopts
= &dev
->u
.alsa
;
441 AudiodevAlsaPerDirectionOptions
*apdo
= in
? aopts
->in
: aopts
->out
;
443 snd_pcm_hw_params_t
*hw_params
;
445 unsigned int freq
, nchannels
;
446 const char *pcm_name
= apdo
->has_dev
? apdo
->dev
: "default";
447 snd_pcm_uframes_t obt_buffer_size
;
448 const char *typ
= in
? "ADC" : "DAC";
449 snd_pcm_format_t obtfmt
;
452 nchannels
= req
->nchannels
;
454 snd_pcm_hw_params_alloca (&hw_params
);
459 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
463 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
467 err
= snd_pcm_hw_params_any (handle
, hw_params
);
469 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
473 err
= snd_pcm_hw_params_set_access (
476 SND_PCM_ACCESS_RW_INTERLEAVED
479 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
483 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
485 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
488 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
490 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
494 err
= snd_pcm_hw_params_set_channels_near (
500 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
505 if (nchannels
!= 1 && nchannels
!= 2) {
506 alsa_logerr2 (err
, typ
,
507 "Can not handle obtained number of channels %d\n",
512 if (apdo
->buffer_length
) {
514 unsigned int btime
= apdo
->buffer_length
;
516 err
= snd_pcm_hw_params_set_buffer_time_near(
517 handle
, hw_params
, &btime
, &dir
);
520 alsa_logerr2(err
, typ
, "Failed to set buffer time to %" PRId32
"\n",
521 apdo
->buffer_length
);
525 if (apdo
->has_buffer_length
&& btime
!= apdo
->buffer_length
) {
526 dolog("Requested buffer time %" PRId32
527 " was rejected, using %u\n", apdo
->buffer_length
, btime
);
531 if (apdo
->period_length
) {
533 unsigned int ptime
= apdo
->period_length
;
535 err
= snd_pcm_hw_params_set_period_time_near(handle
, hw_params
, &ptime
,
539 alsa_logerr2(err
, typ
, "Failed to set period time to %" PRId32
"\n",
540 apdo
->period_length
);
544 if (apdo
->has_period_length
&& ptime
!= apdo
->period_length
) {
545 dolog("Requested period time %" PRId32
" was rejected, using %d\n",
546 apdo
->period_length
, ptime
);
550 err
= snd_pcm_hw_params (handle
, hw_params
);
552 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
556 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
558 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
562 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
564 alsa_logerr2 (err
, typ
, "Failed to get format\n");
568 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
569 dolog ("Invalid format was returned %d\n", obtfmt
);
573 err
= snd_pcm_prepare (handle
);
575 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
579 if (!in
&& aopts
->has_threshold
&& aopts
->threshold
) {
580 struct audsettings as
= { .freq
= freq
};
583 audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo
),
584 &as
, aopts
->threshold
));
587 obt
->nchannels
= nchannels
;
589 obt
->samples
= obt_buffer_size
;
593 if (obtfmt
!= req
->fmt
||
594 obt
->nchannels
!= req
->nchannels
||
595 obt
->freq
!= req
->freq
) {
596 dolog ("Audio parameters for %s\n", typ
);
597 alsa_dump_info(req
, obt
, obtfmt
, apdo
);
601 alsa_dump_info(req
, obt
, obtfmt
, pdo
);
606 alsa_anal_close1 (&handle
);
610 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
612 snd_pcm_sframes_t avail
;
614 avail
= snd_pcm_avail_update (handle
);
616 if (avail
== -EPIPE
) {
617 if (!alsa_recover (handle
)) {
618 avail
= snd_pcm_avail_update (handle
);
624 "Could not obtain number of available frames\n");
632 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
634 HWVoiceOut
*hw
= &alsa
->hw
;
636 while (alsa
->pending
) {
637 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
638 int len
= MIN (alsa
->pending
, left_till_end_samples
);
639 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
642 snd_pcm_sframes_t written
;
644 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
649 trace_alsa_wrote_zero(len
);
653 if (alsa_recover (alsa
->handle
)) {
654 alsa_logerr (written
, "Failed to write %d frames\n",
658 trace_alsa_xrun_out();
662 /* stream is suspended and waiting for an
663 application recovery */
664 if (alsa_resume (alsa
->handle
)) {
665 alsa_logerr (written
, "Failed to write %d frames\n",
669 trace_alsa_resume_out();
676 alsa_logerr (written
, "Failed to write %d frames from %p\n",
682 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
683 alsa
->pending
-= written
;
689 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
691 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
693 snd_pcm_sframes_t avail
;
695 avail
= alsa_get_avail (alsa
->handle
);
697 dolog ("Could not get number of available playback frames\n");
701 decr
= MIN (live
, avail
);
702 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
703 alsa
->pending
+= decr
;
704 alsa_write_pending (alsa
);
708 static void alsa_fini_out (HWVoiceOut
*hw
)
710 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
712 ldebug ("alsa_fini\n");
713 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
715 g_free(alsa
->pcm_buf
);
716 alsa
->pcm_buf
= NULL
;
719 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
722 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
723 struct alsa_params_req req
;
724 struct alsa_params_obt obt
;
726 struct audsettings obt_as
;
727 Audiodev
*dev
= drv_opaque
;
729 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
731 req
.nchannels
= as
->nchannels
;
733 if (alsa_open(0, &req
, &obt
, &handle
, dev
)) {
737 obt_as
.freq
= obt
.freq
;
738 obt_as
.nchannels
= obt
.nchannels
;
739 obt_as
.fmt
= obt
.fmt
;
740 obt_as
.endianness
= obt
.endianness
;
742 audio_pcm_init_info (&hw
->info
, &obt_as
);
743 hw
->samples
= obt
.samples
;
745 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
746 if (!alsa
->pcm_buf
) {
747 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
748 hw
->samples
, 1 << hw
->info
.shift
);
749 alsa_anal_close1 (&handle
);
753 alsa
->pollhlp
.s
= hw
->s
;
754 alsa
->handle
= handle
;
759 #define VOICE_CTL_PAUSE 0
760 #define VOICE_CTL_PREPARE 1
761 #define VOICE_CTL_START 2
763 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
767 if (ctl
== VOICE_CTL_PAUSE
) {
768 err
= snd_pcm_drop (handle
);
770 alsa_logerr (err
, "Could not stop %s\n", typ
);
775 err
= snd_pcm_prepare (handle
);
777 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
780 if (ctl
== VOICE_CTL_START
) {
781 err
= snd_pcm_start(handle
);
783 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
792 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
794 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
795 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.out
;
800 bool poll_mode
= apdo
->try_poll
;
802 ldebug ("enabling voice\n");
803 if (poll_mode
&& alsa_poll_out (hw
)) {
806 hw
->poll_mode
= poll_mode
;
807 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
811 ldebug ("disabling voice\n");
814 alsa_fini_poll (&alsa
->pollhlp
);
816 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
822 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
824 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
825 struct alsa_params_req req
;
826 struct alsa_params_obt obt
;
828 struct audsettings obt_as
;
829 Audiodev
*dev
= drv_opaque
;
831 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
833 req
.nchannels
= as
->nchannels
;
835 if (alsa_open(1, &req
, &obt
, &handle
, dev
)) {
839 obt_as
.freq
= obt
.freq
;
840 obt_as
.nchannels
= obt
.nchannels
;
841 obt_as
.fmt
= obt
.fmt
;
842 obt_as
.endianness
= obt
.endianness
;
844 audio_pcm_init_info (&hw
->info
, &obt_as
);
845 hw
->samples
= obt
.samples
;
847 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
848 if (!alsa
->pcm_buf
) {
849 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
850 hw
->samples
, 1 << hw
->info
.shift
);
851 alsa_anal_close1 (&handle
);
855 alsa
->pollhlp
.s
= hw
->s
;
856 alsa
->handle
= handle
;
861 static void alsa_fini_in (HWVoiceIn
*hw
)
863 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
865 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
867 g_free(alsa
->pcm_buf
);
868 alsa
->pcm_buf
= NULL
;
871 static int alsa_run_in (HWVoiceIn
*hw
)
873 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
874 int hwshift
= hw
->info
.shift
;
876 int live
= audio_pcm_hw_get_live_in (hw
);
877 int dead
= hw
->samples
- live
;
883 { .add
= hw
->wpos
, .len
= 0 },
884 { .add
= 0, .len
= 0 }
886 snd_pcm_sframes_t avail
;
887 snd_pcm_uframes_t read_samples
= 0;
893 avail
= alsa_get_avail (alsa
->handle
);
895 dolog ("Could not get number of captured frames\n");
900 snd_pcm_state_t state
;
902 state
= snd_pcm_state (alsa
->handle
);
904 case SND_PCM_STATE_PREPARED
:
907 case SND_PCM_STATE_SUSPENDED
:
908 /* stream is suspended and waiting for an application recovery */
909 if (alsa_resume (alsa
->handle
)) {
910 dolog ("Failed to resume suspended input stream\n");
913 trace_alsa_resume_in();
916 trace_alsa_no_frames(state
);
921 decr
= MIN (dead
, avail
);
926 if (hw
->wpos
+ decr
> hw
->samples
) {
927 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
928 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
934 for (i
= 0; i
< 2; ++i
) {
936 struct st_sample
*dst
;
937 snd_pcm_sframes_t nread
;
938 snd_pcm_uframes_t len
;
942 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
943 dst
= hw
->conv_buf
+ bufs
[i
].add
;
946 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
951 trace_alsa_read_zero(len
);
955 if (alsa_recover (alsa
->handle
)) {
956 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
959 trace_alsa_xrun_in();
968 "Failed to read %ld frames from %p\n",
976 hw
->conv (dst
, src
, nread
);
978 src
= advance (src
, nread
<< hwshift
);
981 read_samples
+= nread
;
987 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
991 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
993 return audio_pcm_sw_read (sw
, buf
, size
);
996 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
998 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
999 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.in
;
1004 bool poll_mode
= apdo
->try_poll
;
1006 ldebug ("enabling voice\n");
1007 if (poll_mode
&& alsa_poll_in (hw
)) {
1010 hw
->poll_mode
= poll_mode
;
1012 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1016 ldebug ("disabling voice\n");
1017 if (hw
->poll_mode
) {
1019 alsa_fini_poll (&alsa
->pollhlp
);
1021 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1027 static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions
*apdo
)
1029 if (!apdo
->has_try_poll
) {
1030 apdo
->try_poll
= true;
1031 apdo
->has_try_poll
= true;
1035 static void *alsa_audio_init(Audiodev
*dev
)
1037 AudiodevAlsaOptions
*aopts
;
1038 assert(dev
->driver
== AUDIODEV_DRIVER_ALSA
);
1040 aopts
= &dev
->u
.alsa
;
1041 alsa_init_per_direction(aopts
->in
);
1042 alsa_init_per_direction(aopts
->out
);
1045 * need to define them, as otherwise alsa produces no sound
1046 * doesn't set has_* so alsa_open can identify it wasn't set by the user
1048 if (!dev
->u
.alsa
.out
->has_period_length
) {
1049 /* 1024 frames assuming 44100Hz */
1050 dev
->u
.alsa
.out
->period_length
= 1024 * 1000000 / 44100;
1052 if (!dev
->u
.alsa
.out
->has_buffer_length
) {
1053 /* 4096 frames assuming 44100Hz */
1054 dev
->u
.alsa
.out
->buffer_length
= 4096ll * 1000000 / 44100;
1058 * OptsVisitor sets unspecified optional fields to zero, but do not depend
1061 if (!dev
->u
.alsa
.in
->has_period_length
) {
1062 dev
->u
.alsa
.in
->period_length
= 0;
1064 if (!dev
->u
.alsa
.in
->has_buffer_length
) {
1065 dev
->u
.alsa
.in
->buffer_length
= 0;
1071 static void alsa_audio_fini (void *opaque
)
1075 static struct audio_pcm_ops alsa_pcm_ops
= {
1076 .init_out
= alsa_init_out
,
1077 .fini_out
= alsa_fini_out
,
1078 .run_out
= alsa_run_out
,
1079 .write
= alsa_write
,
1080 .ctl_out
= alsa_ctl_out
,
1082 .init_in
= alsa_init_in
,
1083 .fini_in
= alsa_fini_in
,
1084 .run_in
= alsa_run_in
,
1086 .ctl_in
= alsa_ctl_in
,
1089 static struct audio_driver alsa_audio_driver
= {
1091 .descr
= "ALSA http://www.alsa-project.org",
1092 .init
= alsa_audio_init
,
1093 .fini
= alsa_audio_fini
,
1094 .pcm_ops
= &alsa_pcm_ops
,
1095 .can_be_default
= 1,
1096 .max_voices_out
= INT_MAX
,
1097 .max_voices_in
= INT_MAX
,
1098 .voice_size_out
= sizeof (ALSAVoiceOut
),
1099 .voice_size_in
= sizeof (ALSAVoiceIn
)
1102 static void register_audio_alsa(void)
1104 audio_driver_register(&alsa_audio_driver
);
1106 type_init(register_audio_alsa
);