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 "main-loop.h"
27 #include "qemu-char.h"
30 #if QEMU_GNUC_PREREQ(4, 3)
31 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
44 typedef struct ALSAVoiceOut
{
50 struct pollhlp pollhlp
;
53 typedef struct ALSAVoiceIn
{
57 struct pollhlp pollhlp
;
63 const char *pcm_name_in
;
64 const char *pcm_name_out
;
65 unsigned int buffer_size_in
;
66 unsigned int period_size_in
;
67 unsigned int buffer_size_out
;
68 unsigned int period_size_out
;
69 unsigned int threshold
;
71 int buffer_size_in_overridden
;
72 int period_size_in_overridden
;
74 int buffer_size_out_overridden
;
75 int period_size_out_overridden
;
78 .buffer_size_out
= 4096,
79 .period_size_out
= 1024,
80 .pcm_name_out
= "default",
81 .pcm_name_in
= "default",
84 struct alsa_params_req
{
90 unsigned int buffer_size
;
91 unsigned int period_size
;
94 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_fini_poll (struct pollhlp
*hlp
)
134 struct pollfd
*pfds
= hlp
->pfds
;
137 for (i
= 0; i
< hlp
->count
; ++i
) {
138 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
147 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
149 int err
= snd_pcm_close (*handlep
);
151 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
156 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
158 alsa_fini_poll (hlp
);
159 alsa_anal_close1 (handlep
);
162 static int alsa_recover (snd_pcm_t
*handle
)
164 int err
= snd_pcm_prepare (handle
);
166 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
172 static int alsa_resume (snd_pcm_t
*handle
)
174 int err
= snd_pcm_resume (handle
);
176 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
182 static void alsa_poll_handler (void *opaque
)
185 snd_pcm_state_t state
;
186 struct pollhlp
*hlp
= opaque
;
187 unsigned short revents
;
189 count
= poll (hlp
->pfds
, hlp
->count
, 0);
191 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
199 /* XXX: ALSA example uses initial count, not the one returned by
201 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
202 hlp
->count
, &revents
);
204 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
208 if (!(revents
& hlp
->mask
)) {
210 dolog ("revents = %d\n", revents
);
215 state
= snd_pcm_state (hlp
->handle
);
217 case SND_PCM_STATE_SETUP
:
218 alsa_recover (hlp
->handle
);
221 case SND_PCM_STATE_XRUN
:
222 alsa_recover (hlp
->handle
);
225 case SND_PCM_STATE_SUSPENDED
:
226 alsa_resume (hlp
->handle
);
229 case SND_PCM_STATE_PREPARED
:
230 audio_run ("alsa run (prepared)");
233 case SND_PCM_STATE_RUNNING
:
234 audio_run ("alsa run (running)");
238 dolog ("Unexpected state %d\n", state
);
242 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
247 count
= snd_pcm_poll_descriptors_count (handle
);
249 dolog ("Could not initialize poll mode\n"
250 "Invalid number of poll descriptors %d\n", count
);
254 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
256 dolog ("Could not initialize poll mode\n");
260 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
262 alsa_logerr (err
, "Could not initialize poll mode\n"
263 "Could not obtain poll descriptors\n");
268 for (i
= 0; i
< count
; ++i
) {
269 if (pfds
[i
].events
& POLLIN
) {
270 err
= qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
,
273 if (pfds
[i
].events
& POLLOUT
) {
275 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
277 err
= qemu_set_fd_handler (pfds
[i
].fd
, NULL
,
278 alsa_poll_handler
, hlp
);
281 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
282 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
286 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
287 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
290 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
298 hlp
->handle
= handle
;
303 static int alsa_poll_out (HWVoiceOut
*hw
)
305 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
307 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
310 static int alsa_poll_in (HWVoiceIn
*hw
)
312 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
314 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
317 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
319 return audio_pcm_sw_write (sw
, buf
, len
);
322 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
, int endianness
)
326 return SND_PCM_FORMAT_S8
;
329 return SND_PCM_FORMAT_U8
;
333 return SND_PCM_FORMAT_S16_BE
;
336 return SND_PCM_FORMAT_S16_LE
;
341 return SND_PCM_FORMAT_U16_BE
;
344 return SND_PCM_FORMAT_U16_LE
;
349 return SND_PCM_FORMAT_S32_BE
;
352 return SND_PCM_FORMAT_S32_LE
;
357 return SND_PCM_FORMAT_U32_BE
;
360 return SND_PCM_FORMAT_U32_LE
;
364 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
368 return SND_PCM_FORMAT_U8
;
372 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
376 case SND_PCM_FORMAT_S8
:
381 case SND_PCM_FORMAT_U8
:
386 case SND_PCM_FORMAT_S16_LE
:
391 case SND_PCM_FORMAT_U16_LE
:
396 case SND_PCM_FORMAT_S16_BE
:
401 case SND_PCM_FORMAT_U16_BE
:
406 case SND_PCM_FORMAT_S32_LE
:
411 case SND_PCM_FORMAT_U32_LE
:
416 case SND_PCM_FORMAT_S32_BE
:
421 case SND_PCM_FORMAT_U32_BE
:
427 dolog ("Unrecognized audio format %d\n", alsafmt
);
434 static void alsa_dump_info (struct alsa_params_req
*req
,
435 struct alsa_params_obt
*obt
,
436 snd_pcm_format_t obtfmt
)
438 dolog ("parameter | requested value | obtained value\n");
439 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
440 dolog ("channels | %10d | %10d\n",
441 req
->nchannels
, obt
->nchannels
);
442 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
443 dolog ("============================================\n");
444 dolog ("requested: buffer size %d period size %d\n",
445 req
->buffer_size
, req
->period_size
);
446 dolog ("obtained: samples %ld\n", obt
->samples
);
449 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
452 snd_pcm_sw_params_t
*sw_params
;
454 snd_pcm_sw_params_alloca (&sw_params
);
456 err
= snd_pcm_sw_params_current (handle
, sw_params
);
458 dolog ("Could not fully initialize DAC\n");
459 alsa_logerr (err
, "Failed to get current software parameters\n");
463 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
465 dolog ("Could not fully initialize DAC\n");
466 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
471 err
= snd_pcm_sw_params (handle
, sw_params
);
473 dolog ("Could not fully initialize DAC\n");
474 alsa_logerr (err
, "Failed to set software parameters\n");
479 static int alsa_open (int in
, struct alsa_params_req
*req
,
480 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
483 snd_pcm_hw_params_t
*hw_params
;
486 unsigned int freq
, nchannels
;
487 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
488 snd_pcm_uframes_t obt_buffer_size
;
489 const char *typ
= in
? "ADC" : "DAC";
490 snd_pcm_format_t obtfmt
;
493 nchannels
= req
->nchannels
;
494 size_in_usec
= req
->size_in_usec
;
496 snd_pcm_hw_params_alloca (&hw_params
);
501 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
505 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
509 err
= snd_pcm_hw_params_any (handle
, hw_params
);
511 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
515 err
= snd_pcm_hw_params_set_access (
518 SND_PCM_ACCESS_RW_INTERLEAVED
521 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
525 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
526 if (err
< 0 && conf
.verbose
) {
527 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
530 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
532 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
536 err
= snd_pcm_hw_params_set_channels_near (
542 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
547 if (nchannels
!= 1 && nchannels
!= 2) {
548 alsa_logerr2 (err
, typ
,
549 "Can not handle obtained number of channels %d\n",
554 if (req
->buffer_size
) {
559 unsigned int btime
= req
->buffer_size
;
561 err
= snd_pcm_hw_params_set_buffer_time_near (
570 snd_pcm_uframes_t bsize
= req
->buffer_size
;
572 err
= snd_pcm_hw_params_set_buffer_size_near (
580 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
581 size_in_usec
? "time" : "size", req
->buffer_size
);
585 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
586 dolog ("Requested buffer %s %u was rejected, using %lu\n",
587 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
590 if (req
->period_size
) {
595 unsigned int ptime
= req
->period_size
;
597 err
= snd_pcm_hw_params_set_period_time_near (
607 snd_pcm_uframes_t psize
= req
->period_size
;
609 err
= snd_pcm_hw_params_set_period_size_near (
619 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
620 size_in_usec
? "time" : "size", req
->period_size
);
624 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
625 dolog ("Requested period %s %u was rejected, using %lu\n",
626 size_in_usec
? "time" : "size", req
->period_size
, obt
);
629 err
= snd_pcm_hw_params (handle
, hw_params
);
631 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
635 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
637 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
641 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
643 alsa_logerr2 (err
, typ
, "Failed to get format\n");
647 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
648 dolog ("Invalid format was returned %d\n", obtfmt
);
652 err
= snd_pcm_prepare (handle
);
654 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
658 if (!in
&& conf
.threshold
) {
659 snd_pcm_uframes_t threshold
;
662 bytes_per_sec
= freq
<< (nchannels
== 2);
680 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
681 alsa_set_threshold (handle
, threshold
);
684 obt
->nchannels
= nchannels
;
686 obt
->samples
= obt_buffer_size
;
691 (obtfmt
!= req
->fmt
||
692 obt
->nchannels
!= req
->nchannels
||
693 obt
->freq
!= req
->freq
)) {
694 dolog ("Audio parameters for %s\n", typ
);
695 alsa_dump_info (req
, obt
, obtfmt
);
699 alsa_dump_info (req
, obt
, obtfmt
);
704 alsa_anal_close1 (&handle
);
708 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
710 snd_pcm_sframes_t avail
;
712 avail
= snd_pcm_avail_update (handle
);
714 if (avail
== -EPIPE
) {
715 if (!alsa_recover (handle
)) {
716 avail
= snd_pcm_avail_update (handle
);
722 "Could not obtain number of available frames\n");
730 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
732 HWVoiceOut
*hw
= &alsa
->hw
;
734 while (alsa
->pending
) {
735 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
736 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
737 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
740 snd_pcm_sframes_t written
;
742 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
748 dolog ("Failed to write %d frames (wrote zero)\n", len
);
753 if (alsa_recover (alsa
->handle
)) {
754 alsa_logerr (written
, "Failed to write %d frames\n",
759 dolog ("Recovering from playback xrun\n");
764 /* stream is suspended and waiting for an
765 application recovery */
766 if (alsa_resume (alsa
->handle
)) {
767 alsa_logerr (written
, "Failed to write %d frames\n",
772 dolog ("Resuming suspended output stream\n");
780 alsa_logerr (written
, "Failed to write %d frames from %p\n",
786 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
787 alsa
->pending
-= written
;
793 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
795 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
797 snd_pcm_sframes_t avail
;
799 avail
= alsa_get_avail (alsa
->handle
);
801 dolog ("Could not get number of available playback frames\n");
805 decr
= audio_MIN (live
, avail
);
806 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
807 alsa
->pending
+= decr
;
808 alsa_write_pending (alsa
);
812 static void alsa_fini_out (HWVoiceOut
*hw
)
814 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
816 ldebug ("alsa_fini\n");
817 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
820 g_free (alsa
->pcm_buf
);
821 alsa
->pcm_buf
= NULL
;
825 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
827 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
828 struct alsa_params_req req
;
829 struct alsa_params_obt obt
;
831 struct audsettings obt_as
;
833 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
835 req
.nchannels
= as
->nchannels
;
836 req
.period_size
= conf
.period_size_out
;
837 req
.buffer_size
= conf
.buffer_size_out
;
838 req
.size_in_usec
= conf
.size_in_usec_out
;
840 (conf
.period_size_out_overridden
? 1 : 0) |
841 (conf
.buffer_size_out_overridden
? 2 : 0);
843 if (alsa_open (0, &req
, &obt
, &handle
)) {
847 obt_as
.freq
= obt
.freq
;
848 obt_as
.nchannels
= obt
.nchannels
;
849 obt_as
.fmt
= obt
.fmt
;
850 obt_as
.endianness
= obt
.endianness
;
852 audio_pcm_init_info (&hw
->info
, &obt_as
);
853 hw
->samples
= obt
.samples
;
855 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
856 if (!alsa
->pcm_buf
) {
857 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
858 hw
->samples
, 1 << hw
->info
.shift
);
859 alsa_anal_close1 (&handle
);
863 alsa
->handle
= handle
;
867 #define VOICE_CTL_PAUSE 0
868 #define VOICE_CTL_PREPARE 1
869 #define VOICE_CTL_START 2
871 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
875 if (ctl
== VOICE_CTL_PAUSE
) {
876 err
= snd_pcm_drop (handle
);
878 alsa_logerr (err
, "Could not stop %s\n", typ
);
883 err
= snd_pcm_prepare (handle
);
885 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
888 if (ctl
== VOICE_CTL_START
) {
889 err
= snd_pcm_start(handle
);
891 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
900 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
902 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
911 poll_mode
= va_arg (ap
, int);
914 ldebug ("enabling voice\n");
915 if (poll_mode
&& alsa_poll_out (hw
)) {
918 hw
->poll_mode
= poll_mode
;
919 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
923 ldebug ("disabling voice\n");
926 alsa_fini_poll (&alsa
->pollhlp
);
928 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
934 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
936 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
937 struct alsa_params_req req
;
938 struct alsa_params_obt obt
;
940 struct audsettings obt_as
;
942 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
944 req
.nchannels
= as
->nchannels
;
945 req
.period_size
= conf
.period_size_in
;
946 req
.buffer_size
= conf
.buffer_size_in
;
947 req
.size_in_usec
= conf
.size_in_usec_in
;
949 (conf
.period_size_in_overridden
? 1 : 0) |
950 (conf
.buffer_size_in_overridden
? 2 : 0);
952 if (alsa_open (1, &req
, &obt
, &handle
)) {
956 obt_as
.freq
= obt
.freq
;
957 obt_as
.nchannels
= obt
.nchannels
;
958 obt_as
.fmt
= obt
.fmt
;
959 obt_as
.endianness
= obt
.endianness
;
961 audio_pcm_init_info (&hw
->info
, &obt_as
);
962 hw
->samples
= obt
.samples
;
964 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
965 if (!alsa
->pcm_buf
) {
966 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
967 hw
->samples
, 1 << hw
->info
.shift
);
968 alsa_anal_close1 (&handle
);
972 alsa
->handle
= handle
;
976 static void alsa_fini_in (HWVoiceIn
*hw
)
978 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
980 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
983 g_free (alsa
->pcm_buf
);
984 alsa
->pcm_buf
= NULL
;
988 static int alsa_run_in (HWVoiceIn
*hw
)
990 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
991 int hwshift
= hw
->info
.shift
;
993 int live
= audio_pcm_hw_get_live_in (hw
);
994 int dead
= hw
->samples
- live
;
1000 { .add
= hw
->wpos
, .len
= 0 },
1001 { .add
= 0, .len
= 0 }
1003 snd_pcm_sframes_t avail
;
1004 snd_pcm_uframes_t read_samples
= 0;
1010 avail
= alsa_get_avail (alsa
->handle
);
1012 dolog ("Could not get number of captured frames\n");
1017 snd_pcm_state_t state
;
1019 state
= snd_pcm_state (alsa
->handle
);
1021 case SND_PCM_STATE_PREPARED
:
1022 avail
= hw
->samples
;
1024 case SND_PCM_STATE_SUSPENDED
:
1025 /* stream is suspended and waiting for an application recovery */
1026 if (alsa_resume (alsa
->handle
)) {
1027 dolog ("Failed to resume suspended input stream\n");
1031 dolog ("Resuming suspended input stream\n");
1036 dolog ("No frames available and ALSA state is %d\n", state
);
1042 decr
= audio_MIN (dead
, avail
);
1047 if (hw
->wpos
+ decr
> hw
->samples
) {
1048 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1049 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1055 for (i
= 0; i
< 2; ++i
) {
1057 struct st_sample
*dst
;
1058 snd_pcm_sframes_t nread
;
1059 snd_pcm_uframes_t len
;
1063 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1064 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1067 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1073 dolog ("Failed to read %ld frames (read zero)\n", len
);
1078 if (alsa_recover (alsa
->handle
)) {
1079 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1083 dolog ("Recovering from capture xrun\n");
1093 "Failed to read %ld frames from %p\n",
1101 hw
->conv (dst
, src
, nread
);
1103 src
= advance (src
, nread
<< hwshift
);
1106 read_samples
+= nread
;
1112 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1113 return read_samples
;
1116 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1118 return audio_pcm_sw_read (sw
, buf
, size
);
1121 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1123 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1132 poll_mode
= va_arg (ap
, int);
1135 ldebug ("enabling voice\n");
1136 if (poll_mode
&& alsa_poll_in (hw
)) {
1139 hw
->poll_mode
= poll_mode
;
1141 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1145 ldebug ("disabling voice\n");
1146 if (hw
->poll_mode
) {
1148 alsa_fini_poll (&alsa
->pollhlp
);
1150 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1156 static void *alsa_audio_init (void)
1161 static void alsa_audio_fini (void *opaque
)
1166 static struct audio_option alsa_options
[] = {
1168 .name
= "DAC_SIZE_IN_USEC",
1169 .tag
= AUD_OPT_BOOL
,
1170 .valp
= &conf
.size_in_usec_out
,
1171 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1174 .name
= "DAC_PERIOD_SIZE",
1176 .valp
= &conf
.period_size_out
,
1177 .descr
= "DAC period size (0 to go with system default)",
1178 .overriddenp
= &conf
.period_size_out_overridden
1181 .name
= "DAC_BUFFER_SIZE",
1183 .valp
= &conf
.buffer_size_out
,
1184 .descr
= "DAC buffer size (0 to go with system default)",
1185 .overriddenp
= &conf
.buffer_size_out_overridden
1188 .name
= "ADC_SIZE_IN_USEC",
1189 .tag
= AUD_OPT_BOOL
,
1190 .valp
= &conf
.size_in_usec_in
,
1192 "ADC period/buffer size in microseconds (otherwise in frames)"
1195 .name
= "ADC_PERIOD_SIZE",
1197 .valp
= &conf
.period_size_in
,
1198 .descr
= "ADC period size (0 to go with system default)",
1199 .overriddenp
= &conf
.period_size_in_overridden
1202 .name
= "ADC_BUFFER_SIZE",
1204 .valp
= &conf
.buffer_size_in
,
1205 .descr
= "ADC buffer size (0 to go with system default)",
1206 .overriddenp
= &conf
.buffer_size_in_overridden
1209 .name
= "THRESHOLD",
1211 .valp
= &conf
.threshold
,
1212 .descr
= "(undocumented)"
1217 .valp
= &conf
.pcm_name_out
,
1218 .descr
= "DAC device name (for instance dmix)"
1223 .valp
= &conf
.pcm_name_in
,
1224 .descr
= "ADC device name"
1228 .tag
= AUD_OPT_BOOL
,
1229 .valp
= &conf
.verbose
,
1230 .descr
= "Behave in a more verbose way"
1232 { /* End of list */ }
1235 static struct audio_pcm_ops alsa_pcm_ops
= {
1236 .init_out
= alsa_init_out
,
1237 .fini_out
= alsa_fini_out
,
1238 .run_out
= alsa_run_out
,
1239 .write
= alsa_write
,
1240 .ctl_out
= alsa_ctl_out
,
1242 .init_in
= alsa_init_in
,
1243 .fini_in
= alsa_fini_in
,
1244 .run_in
= alsa_run_in
,
1246 .ctl_in
= alsa_ctl_in
,
1249 struct audio_driver alsa_audio_driver
= {
1251 .descr
= "ALSA http://www.alsa-project.org",
1252 .options
= alsa_options
,
1253 .init
= alsa_audio_init
,
1254 .fini
= alsa_audio_fini
,
1255 .pcm_ops
= &alsa_pcm_ops
,
1256 .can_be_default
= 1,
1257 .max_voices_out
= INT_MAX
,
1258 .max_voices_in
= INT_MAX
,
1259 .voice_size_out
= sizeof (ALSAVoiceOut
),
1260 .voice_size_in
= sizeof (ALSAVoiceIn
)