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"
26 #include "qemu/main-loop.h"
29 #if QEMU_GNUC_PREREQ(4, 3)
30 #pragma GCC diagnostic ignored "-Waddress"
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
43 typedef struct ALSAVoiceOut
{
49 struct pollhlp pollhlp
;
52 typedef struct ALSAVoiceIn
{
56 struct pollhlp pollhlp
;
62 const char *pcm_name_in
;
63 const char *pcm_name_out
;
64 unsigned int buffer_size_in
;
65 unsigned int period_size_in
;
66 unsigned int buffer_size_out
;
67 unsigned int period_size_out
;
68 unsigned int threshold
;
70 int buffer_size_in_overridden
;
71 int period_size_in_overridden
;
73 int buffer_size_out_overridden
;
74 int period_size_out_overridden
;
77 .buffer_size_out
= 4096,
78 .period_size_out
= 1024,
79 .pcm_name_out
= "default",
80 .pcm_name_in
= "default",
83 struct alsa_params_req
{
89 unsigned int buffer_size
;
90 unsigned int period_size
;
93 struct alsa_params_obt
{
98 snd_pcm_uframes_t samples
;
101 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
106 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
109 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
112 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
121 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
124 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
127 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
130 static void alsa_fini_poll (struct pollhlp
*hlp
)
133 struct pollfd
*pfds
= hlp
->pfds
;
136 for (i
= 0; i
< hlp
->count
; ++i
) {
137 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
146 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
148 int err
= snd_pcm_close (*handlep
);
150 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
155 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
157 alsa_fini_poll (hlp
);
158 alsa_anal_close1 (handlep
);
161 static int alsa_recover (snd_pcm_t
*handle
)
163 int err
= snd_pcm_prepare (handle
);
165 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
171 static int alsa_resume (snd_pcm_t
*handle
)
173 int err
= snd_pcm_resume (handle
);
175 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
181 static void alsa_poll_handler (void *opaque
)
184 snd_pcm_state_t state
;
185 struct pollhlp
*hlp
= opaque
;
186 unsigned short revents
;
188 count
= poll (hlp
->pfds
, hlp
->count
, 0);
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
198 /* XXX: ALSA example uses initial count, not the one returned by
200 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
201 hlp
->count
, &revents
);
203 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
207 if (!(revents
& hlp
->mask
)) {
209 dolog ("revents = %d\n", revents
);
214 state
= snd_pcm_state (hlp
->handle
);
216 case SND_PCM_STATE_SETUP
:
217 alsa_recover (hlp
->handle
);
220 case SND_PCM_STATE_XRUN
:
221 alsa_recover (hlp
->handle
);
224 case SND_PCM_STATE_SUSPENDED
:
225 alsa_resume (hlp
->handle
);
228 case SND_PCM_STATE_PREPARED
:
229 audio_run ("alsa run (prepared)");
232 case SND_PCM_STATE_RUNNING
:
233 audio_run ("alsa run (running)");
237 dolog ("Unexpected state %d\n", state
);
241 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
246 count
= snd_pcm_poll_descriptors_count (handle
);
248 dolog ("Could not initialize poll mode\n"
249 "Invalid number of poll descriptors %d\n", count
);
253 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
255 dolog ("Could not initialize poll mode\n");
259 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
261 alsa_logerr (err
, "Could not initialize poll mode\n"
262 "Could not obtain poll descriptors\n");
267 for (i
= 0; i
< count
; ++i
) {
268 if (pfds
[i
].events
& POLLIN
) {
269 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
271 if (pfds
[i
].events
& POLLOUT
) {
273 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
275 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
278 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
279 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
285 hlp
->handle
= handle
;
290 static int alsa_poll_out (HWVoiceOut
*hw
)
292 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
294 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
297 static int alsa_poll_in (HWVoiceIn
*hw
)
299 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
301 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
304 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
306 return audio_pcm_sw_write (sw
, buf
, len
);
309 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
, int endianness
)
313 return SND_PCM_FORMAT_S8
;
316 return SND_PCM_FORMAT_U8
;
320 return SND_PCM_FORMAT_S16_BE
;
323 return SND_PCM_FORMAT_S16_LE
;
328 return SND_PCM_FORMAT_U16_BE
;
331 return SND_PCM_FORMAT_U16_LE
;
336 return SND_PCM_FORMAT_S32_BE
;
339 return SND_PCM_FORMAT_S32_LE
;
344 return SND_PCM_FORMAT_U32_BE
;
347 return SND_PCM_FORMAT_U32_LE
;
351 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
355 return SND_PCM_FORMAT_U8
;
359 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
363 case SND_PCM_FORMAT_S8
:
368 case SND_PCM_FORMAT_U8
:
373 case SND_PCM_FORMAT_S16_LE
:
378 case SND_PCM_FORMAT_U16_LE
:
383 case SND_PCM_FORMAT_S16_BE
:
388 case SND_PCM_FORMAT_U16_BE
:
393 case SND_PCM_FORMAT_S32_LE
:
398 case SND_PCM_FORMAT_U32_LE
:
403 case SND_PCM_FORMAT_S32_BE
:
408 case SND_PCM_FORMAT_U32_BE
:
414 dolog ("Unrecognized audio format %d\n", alsafmt
);
421 static void alsa_dump_info (struct alsa_params_req
*req
,
422 struct alsa_params_obt
*obt
,
423 snd_pcm_format_t obtfmt
)
425 dolog ("parameter | requested value | obtained value\n");
426 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
427 dolog ("channels | %10d | %10d\n",
428 req
->nchannels
, obt
->nchannels
);
429 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
430 dolog ("============================================\n");
431 dolog ("requested: buffer size %d period size %d\n",
432 req
->buffer_size
, req
->period_size
);
433 dolog ("obtained: samples %ld\n", obt
->samples
);
436 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
439 snd_pcm_sw_params_t
*sw_params
;
441 snd_pcm_sw_params_alloca (&sw_params
);
443 err
= snd_pcm_sw_params_current (handle
, sw_params
);
445 dolog ("Could not fully initialize DAC\n");
446 alsa_logerr (err
, "Failed to get current software parameters\n");
450 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
452 dolog ("Could not fully initialize DAC\n");
453 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
458 err
= snd_pcm_sw_params (handle
, sw_params
);
460 dolog ("Could not fully initialize DAC\n");
461 alsa_logerr (err
, "Failed to set software parameters\n");
466 static int alsa_open (int in
, struct alsa_params_req
*req
,
467 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
470 snd_pcm_hw_params_t
*hw_params
;
473 unsigned int freq
, nchannels
;
474 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
475 snd_pcm_uframes_t obt_buffer_size
;
476 const char *typ
= in
? "ADC" : "DAC";
477 snd_pcm_format_t obtfmt
;
480 nchannels
= req
->nchannels
;
481 size_in_usec
= req
->size_in_usec
;
483 snd_pcm_hw_params_alloca (&hw_params
);
488 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
492 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
496 err
= snd_pcm_hw_params_any (handle
, hw_params
);
498 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
502 err
= snd_pcm_hw_params_set_access (
505 SND_PCM_ACCESS_RW_INTERLEAVED
508 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
512 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
513 if (err
< 0 && conf
.verbose
) {
514 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
517 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
519 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
523 err
= snd_pcm_hw_params_set_channels_near (
529 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
534 if (nchannels
!= 1 && nchannels
!= 2) {
535 alsa_logerr2 (err
, typ
,
536 "Can not handle obtained number of channels %d\n",
541 if (req
->buffer_size
) {
546 unsigned int btime
= req
->buffer_size
;
548 err
= snd_pcm_hw_params_set_buffer_time_near (
557 snd_pcm_uframes_t bsize
= req
->buffer_size
;
559 err
= snd_pcm_hw_params_set_buffer_size_near (
567 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
568 size_in_usec
? "time" : "size", req
->buffer_size
);
572 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
573 dolog ("Requested buffer %s %u was rejected, using %lu\n",
574 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
577 if (req
->period_size
) {
582 unsigned int ptime
= req
->period_size
;
584 err
= snd_pcm_hw_params_set_period_time_near (
594 snd_pcm_uframes_t psize
= req
->period_size
;
596 err
= snd_pcm_hw_params_set_period_size_near (
606 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
607 size_in_usec
? "time" : "size", req
->period_size
);
611 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
612 dolog ("Requested period %s %u was rejected, using %lu\n",
613 size_in_usec
? "time" : "size", req
->period_size
, obt
);
616 err
= snd_pcm_hw_params (handle
, hw_params
);
618 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
622 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
624 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
628 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
630 alsa_logerr2 (err
, typ
, "Failed to get format\n");
634 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
635 dolog ("Invalid format was returned %d\n", obtfmt
);
639 err
= snd_pcm_prepare (handle
);
641 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
645 if (!in
&& conf
.threshold
) {
646 snd_pcm_uframes_t threshold
;
649 bytes_per_sec
= freq
<< (nchannels
== 2);
667 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
668 alsa_set_threshold (handle
, threshold
);
671 obt
->nchannels
= nchannels
;
673 obt
->samples
= obt_buffer_size
;
678 (obtfmt
!= req
->fmt
||
679 obt
->nchannels
!= req
->nchannels
||
680 obt
->freq
!= req
->freq
)) {
681 dolog ("Audio parameters for %s\n", typ
);
682 alsa_dump_info (req
, obt
, obtfmt
);
686 alsa_dump_info (req
, obt
, obtfmt
);
691 alsa_anal_close1 (&handle
);
695 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
697 snd_pcm_sframes_t avail
;
699 avail
= snd_pcm_avail_update (handle
);
701 if (avail
== -EPIPE
) {
702 if (!alsa_recover (handle
)) {
703 avail
= snd_pcm_avail_update (handle
);
709 "Could not obtain number of available frames\n");
717 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
719 HWVoiceOut
*hw
= &alsa
->hw
;
721 while (alsa
->pending
) {
722 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
723 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
724 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
727 snd_pcm_sframes_t written
;
729 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
735 dolog ("Failed to write %d frames (wrote zero)\n", len
);
740 if (alsa_recover (alsa
->handle
)) {
741 alsa_logerr (written
, "Failed to write %d frames\n",
746 dolog ("Recovering from playback xrun\n");
751 /* stream is suspended and waiting for an
752 application recovery */
753 if (alsa_resume (alsa
->handle
)) {
754 alsa_logerr (written
, "Failed to write %d frames\n",
759 dolog ("Resuming suspended output stream\n");
767 alsa_logerr (written
, "Failed to write %d frames from %p\n",
773 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
774 alsa
->pending
-= written
;
780 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
782 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
784 snd_pcm_sframes_t avail
;
786 avail
= alsa_get_avail (alsa
->handle
);
788 dolog ("Could not get number of available playback frames\n");
792 decr
= audio_MIN (live
, avail
);
793 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
794 alsa
->pending
+= decr
;
795 alsa_write_pending (alsa
);
799 static void alsa_fini_out (HWVoiceOut
*hw
)
801 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
803 ldebug ("alsa_fini\n");
804 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
806 g_free(alsa
->pcm_buf
);
807 alsa
->pcm_buf
= NULL
;
810 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
812 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
813 struct alsa_params_req req
;
814 struct alsa_params_obt obt
;
816 struct audsettings obt_as
;
818 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
820 req
.nchannels
= as
->nchannels
;
821 req
.period_size
= conf
.period_size_out
;
822 req
.buffer_size
= conf
.buffer_size_out
;
823 req
.size_in_usec
= conf
.size_in_usec_out
;
825 (conf
.period_size_out_overridden
? 1 : 0) |
826 (conf
.buffer_size_out_overridden
? 2 : 0);
828 if (alsa_open (0, &req
, &obt
, &handle
)) {
832 obt_as
.freq
= obt
.freq
;
833 obt_as
.nchannels
= obt
.nchannels
;
834 obt_as
.fmt
= obt
.fmt
;
835 obt_as
.endianness
= obt
.endianness
;
837 audio_pcm_init_info (&hw
->info
, &obt_as
);
838 hw
->samples
= obt
.samples
;
840 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
841 if (!alsa
->pcm_buf
) {
842 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
843 hw
->samples
, 1 << hw
->info
.shift
);
844 alsa_anal_close1 (&handle
);
848 alsa
->handle
= handle
;
852 #define VOICE_CTL_PAUSE 0
853 #define VOICE_CTL_PREPARE 1
854 #define VOICE_CTL_START 2
856 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
860 if (ctl
== VOICE_CTL_PAUSE
) {
861 err
= snd_pcm_drop (handle
);
863 alsa_logerr (err
, "Could not stop %s\n", typ
);
868 err
= snd_pcm_prepare (handle
);
870 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
873 if (ctl
== VOICE_CTL_START
) {
874 err
= snd_pcm_start(handle
);
876 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
885 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
887 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
896 poll_mode
= va_arg (ap
, int);
899 ldebug ("enabling voice\n");
900 if (poll_mode
&& alsa_poll_out (hw
)) {
903 hw
->poll_mode
= poll_mode
;
904 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
908 ldebug ("disabling voice\n");
911 alsa_fini_poll (&alsa
->pollhlp
);
913 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
919 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
921 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
922 struct alsa_params_req req
;
923 struct alsa_params_obt obt
;
925 struct audsettings obt_as
;
927 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
929 req
.nchannels
= as
->nchannels
;
930 req
.period_size
= conf
.period_size_in
;
931 req
.buffer_size
= conf
.buffer_size_in
;
932 req
.size_in_usec
= conf
.size_in_usec_in
;
934 (conf
.period_size_in_overridden
? 1 : 0) |
935 (conf
.buffer_size_in_overridden
? 2 : 0);
937 if (alsa_open (1, &req
, &obt
, &handle
)) {
941 obt_as
.freq
= obt
.freq
;
942 obt_as
.nchannels
= obt
.nchannels
;
943 obt_as
.fmt
= obt
.fmt
;
944 obt_as
.endianness
= obt
.endianness
;
946 audio_pcm_init_info (&hw
->info
, &obt_as
);
947 hw
->samples
= obt
.samples
;
949 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
950 if (!alsa
->pcm_buf
) {
951 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
952 hw
->samples
, 1 << hw
->info
.shift
);
953 alsa_anal_close1 (&handle
);
957 alsa
->handle
= handle
;
961 static void alsa_fini_in (HWVoiceIn
*hw
)
963 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
965 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
967 g_free(alsa
->pcm_buf
);
968 alsa
->pcm_buf
= NULL
;
971 static int alsa_run_in (HWVoiceIn
*hw
)
973 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
974 int hwshift
= hw
->info
.shift
;
976 int live
= audio_pcm_hw_get_live_in (hw
);
977 int dead
= hw
->samples
- live
;
983 { .add
= hw
->wpos
, .len
= 0 },
984 { .add
= 0, .len
= 0 }
986 snd_pcm_sframes_t avail
;
987 snd_pcm_uframes_t read_samples
= 0;
993 avail
= alsa_get_avail (alsa
->handle
);
995 dolog ("Could not get number of captured frames\n");
1000 snd_pcm_state_t state
;
1002 state
= snd_pcm_state (alsa
->handle
);
1004 case SND_PCM_STATE_PREPARED
:
1005 avail
= hw
->samples
;
1007 case SND_PCM_STATE_SUSPENDED
:
1008 /* stream is suspended and waiting for an application recovery */
1009 if (alsa_resume (alsa
->handle
)) {
1010 dolog ("Failed to resume suspended input stream\n");
1014 dolog ("Resuming suspended input stream\n");
1019 dolog ("No frames available and ALSA state is %d\n", state
);
1025 decr
= audio_MIN (dead
, avail
);
1030 if (hw
->wpos
+ decr
> hw
->samples
) {
1031 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1032 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1038 for (i
= 0; i
< 2; ++i
) {
1040 struct st_sample
*dst
;
1041 snd_pcm_sframes_t nread
;
1042 snd_pcm_uframes_t len
;
1046 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1047 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1050 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1056 dolog ("Failed to read %ld frames (read zero)\n", len
);
1061 if (alsa_recover (alsa
->handle
)) {
1062 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1066 dolog ("Recovering from capture xrun\n");
1076 "Failed to read %ld frames from %p\n",
1084 hw
->conv (dst
, src
, nread
);
1086 src
= advance (src
, nread
<< hwshift
);
1089 read_samples
+= nread
;
1095 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1096 return read_samples
;
1099 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1101 return audio_pcm_sw_read (sw
, buf
, size
);
1104 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1106 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1115 poll_mode
= va_arg (ap
, int);
1118 ldebug ("enabling voice\n");
1119 if (poll_mode
&& alsa_poll_in (hw
)) {
1122 hw
->poll_mode
= poll_mode
;
1124 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1128 ldebug ("disabling voice\n");
1129 if (hw
->poll_mode
) {
1131 alsa_fini_poll (&alsa
->pollhlp
);
1133 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1139 static void *alsa_audio_init (void)
1144 static void alsa_audio_fini (void *opaque
)
1149 static struct audio_option alsa_options
[] = {
1151 .name
= "DAC_SIZE_IN_USEC",
1152 .tag
= AUD_OPT_BOOL
,
1153 .valp
= &conf
.size_in_usec_out
,
1154 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1157 .name
= "DAC_PERIOD_SIZE",
1159 .valp
= &conf
.period_size_out
,
1160 .descr
= "DAC period size (0 to go with system default)",
1161 .overriddenp
= &conf
.period_size_out_overridden
1164 .name
= "DAC_BUFFER_SIZE",
1166 .valp
= &conf
.buffer_size_out
,
1167 .descr
= "DAC buffer size (0 to go with system default)",
1168 .overriddenp
= &conf
.buffer_size_out_overridden
1171 .name
= "ADC_SIZE_IN_USEC",
1172 .tag
= AUD_OPT_BOOL
,
1173 .valp
= &conf
.size_in_usec_in
,
1175 "ADC period/buffer size in microseconds (otherwise in frames)"
1178 .name
= "ADC_PERIOD_SIZE",
1180 .valp
= &conf
.period_size_in
,
1181 .descr
= "ADC period size (0 to go with system default)",
1182 .overriddenp
= &conf
.period_size_in_overridden
1185 .name
= "ADC_BUFFER_SIZE",
1187 .valp
= &conf
.buffer_size_in
,
1188 .descr
= "ADC buffer size (0 to go with system default)",
1189 .overriddenp
= &conf
.buffer_size_in_overridden
1192 .name
= "THRESHOLD",
1194 .valp
= &conf
.threshold
,
1195 .descr
= "(undocumented)"
1200 .valp
= &conf
.pcm_name_out
,
1201 .descr
= "DAC device name (for instance dmix)"
1206 .valp
= &conf
.pcm_name_in
,
1207 .descr
= "ADC device name"
1211 .tag
= AUD_OPT_BOOL
,
1212 .valp
= &conf
.verbose
,
1213 .descr
= "Behave in a more verbose way"
1215 { /* End of list */ }
1218 static struct audio_pcm_ops alsa_pcm_ops
= {
1219 .init_out
= alsa_init_out
,
1220 .fini_out
= alsa_fini_out
,
1221 .run_out
= alsa_run_out
,
1222 .write
= alsa_write
,
1223 .ctl_out
= alsa_ctl_out
,
1225 .init_in
= alsa_init_in
,
1226 .fini_in
= alsa_fini_in
,
1227 .run_in
= alsa_run_in
,
1229 .ctl_in
= alsa_ctl_in
,
1232 struct audio_driver alsa_audio_driver
= {
1234 .descr
= "ALSA http://www.alsa-project.org",
1235 .options
= alsa_options
,
1236 .init
= alsa_audio_init
,
1237 .fini
= alsa_audio_fini
,
1238 .pcm_ops
= &alsa_pcm_ops
,
1239 .can_be_default
= 1,
1240 .max_voices_out
= INT_MAX
,
1241 .max_voices_in
= INT_MAX
,
1242 .voice_size_out
= sizeof (ALSAVoiceOut
),
1243 .voice_size_in
= sizeof (ALSAVoiceIn
)