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"
30 #if QEMU_GNUC_PREREQ(4, 3)
31 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
37 typedef struct ALSAConf
{
40 const char *pcm_name_in
;
41 const char *pcm_name_out
;
42 unsigned int buffer_size_in
;
43 unsigned int period_size_in
;
44 unsigned int buffer_size_out
;
45 unsigned int period_size_out
;
46 unsigned int threshold
;
48 int buffer_size_in_overridden
;
49 int period_size_in_overridden
;
51 int buffer_size_out_overridden
;
52 int period_size_out_overridden
;
63 typedef struct ALSAVoiceOut
{
69 struct pollhlp pollhlp
;
72 typedef struct ALSAVoiceIn
{
76 struct pollhlp pollhlp
;
79 struct alsa_params_req
{
85 unsigned int buffer_size
;
86 unsigned int period_size
;
89 struct alsa_params_obt
{
94 snd_pcm_uframes_t samples
;
97 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
102 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
105 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
108 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
117 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
120 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
123 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
126 static void alsa_fini_poll (struct pollhlp
*hlp
)
129 struct pollfd
*pfds
= hlp
->pfds
;
132 for (i
= 0; i
< hlp
->count
; ++i
) {
133 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
142 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
144 int err
= snd_pcm_close (*handlep
);
146 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
151 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
153 alsa_fini_poll (hlp
);
154 alsa_anal_close1 (handlep
);
157 static int alsa_recover (snd_pcm_t
*handle
)
159 int err
= snd_pcm_prepare (handle
);
161 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
167 static int alsa_resume (snd_pcm_t
*handle
)
169 int err
= snd_pcm_resume (handle
);
171 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
177 static void alsa_poll_handler (void *opaque
)
180 snd_pcm_state_t state
;
181 struct pollhlp
*hlp
= opaque
;
182 unsigned short revents
;
184 count
= poll (hlp
->pfds
, hlp
->count
, 0);
186 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
194 /* XXX: ALSA example uses initial count, not the one returned by
196 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
197 hlp
->count
, &revents
);
199 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
203 if (!(revents
& hlp
->mask
)) {
204 trace_alsa_revents(revents
);
208 state
= snd_pcm_state (hlp
->handle
);
210 case SND_PCM_STATE_SETUP
:
211 alsa_recover (hlp
->handle
);
214 case SND_PCM_STATE_XRUN
:
215 alsa_recover (hlp
->handle
);
218 case SND_PCM_STATE_SUSPENDED
:
219 alsa_resume (hlp
->handle
);
222 case SND_PCM_STATE_PREPARED
:
223 audio_run ("alsa run (prepared)");
226 case SND_PCM_STATE_RUNNING
:
227 audio_run ("alsa run (running)");
231 dolog ("Unexpected state %d\n", state
);
235 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
240 count
= snd_pcm_poll_descriptors_count (handle
);
242 dolog ("Could not initialize poll mode\n"
243 "Invalid number of poll descriptors %d\n", count
);
247 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
249 dolog ("Could not initialize poll mode\n");
253 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
255 alsa_logerr (err
, "Could not initialize poll mode\n"
256 "Could not obtain poll descriptors\n");
261 for (i
= 0; i
< count
; ++i
) {
262 if (pfds
[i
].events
& POLLIN
) {
263 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
265 if (pfds
[i
].events
& POLLOUT
) {
266 trace_alsa_pollout(i
, pfds
[i
].fd
);
267 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
269 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
274 hlp
->handle
= handle
;
279 static int alsa_poll_out (HWVoiceOut
*hw
)
281 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
283 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
286 static int alsa_poll_in (HWVoiceIn
*hw
)
288 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
290 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
293 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
295 return audio_pcm_sw_write (sw
, buf
, len
);
298 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
, int endianness
)
302 return SND_PCM_FORMAT_S8
;
305 return SND_PCM_FORMAT_U8
;
309 return SND_PCM_FORMAT_S16_BE
;
312 return SND_PCM_FORMAT_S16_LE
;
317 return SND_PCM_FORMAT_U16_BE
;
320 return SND_PCM_FORMAT_U16_LE
;
325 return SND_PCM_FORMAT_S32_BE
;
328 return SND_PCM_FORMAT_S32_LE
;
333 return SND_PCM_FORMAT_U32_BE
;
336 return SND_PCM_FORMAT_U32_LE
;
340 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
344 return SND_PCM_FORMAT_U8
;
348 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
352 case SND_PCM_FORMAT_S8
:
357 case SND_PCM_FORMAT_U8
:
362 case SND_PCM_FORMAT_S16_LE
:
367 case SND_PCM_FORMAT_U16_LE
:
372 case SND_PCM_FORMAT_S16_BE
:
377 case SND_PCM_FORMAT_U16_BE
:
382 case SND_PCM_FORMAT_S32_LE
:
387 case SND_PCM_FORMAT_U32_LE
:
392 case SND_PCM_FORMAT_S32_BE
:
397 case SND_PCM_FORMAT_U32_BE
:
403 dolog ("Unrecognized audio format %d\n", alsafmt
);
410 static void alsa_dump_info (struct alsa_params_req
*req
,
411 struct alsa_params_obt
*obt
,
412 snd_pcm_format_t obtfmt
)
414 dolog ("parameter | requested value | obtained value\n");
415 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
416 dolog ("channels | %10d | %10d\n",
417 req
->nchannels
, obt
->nchannels
);
418 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
419 dolog ("============================================\n");
420 dolog ("requested: buffer size %d period size %d\n",
421 req
->buffer_size
, req
->period_size
);
422 dolog ("obtained: samples %ld\n", obt
->samples
);
425 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
428 snd_pcm_sw_params_t
*sw_params
;
430 snd_pcm_sw_params_alloca (&sw_params
);
432 err
= snd_pcm_sw_params_current (handle
, sw_params
);
434 dolog ("Could not fully initialize DAC\n");
435 alsa_logerr (err
, "Failed to get current software parameters\n");
439 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
441 dolog ("Could not fully initialize DAC\n");
442 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
447 err
= snd_pcm_sw_params (handle
, sw_params
);
449 dolog ("Could not fully initialize DAC\n");
450 alsa_logerr (err
, "Failed to set software parameters\n");
455 static int alsa_open (int in
, struct alsa_params_req
*req
,
456 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
460 snd_pcm_hw_params_t
*hw_params
;
463 unsigned int freq
, nchannels
;
464 const char *pcm_name
= in
? conf
->pcm_name_in
: conf
->pcm_name_out
;
465 snd_pcm_uframes_t obt_buffer_size
;
466 const char *typ
= in
? "ADC" : "DAC";
467 snd_pcm_format_t obtfmt
;
470 nchannels
= req
->nchannels
;
471 size_in_usec
= req
->size_in_usec
;
473 snd_pcm_hw_params_alloca (&hw_params
);
478 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
482 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
486 err
= snd_pcm_hw_params_any (handle
, hw_params
);
488 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
492 err
= snd_pcm_hw_params_set_access (
495 SND_PCM_ACCESS_RW_INTERLEAVED
498 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
502 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
504 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
507 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
509 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
513 err
= snd_pcm_hw_params_set_channels_near (
519 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
524 if (nchannels
!= 1 && nchannels
!= 2) {
525 alsa_logerr2 (err
, typ
,
526 "Can not handle obtained number of channels %d\n",
531 if (req
->buffer_size
) {
536 unsigned int btime
= req
->buffer_size
;
538 err
= snd_pcm_hw_params_set_buffer_time_near (
547 snd_pcm_uframes_t bsize
= req
->buffer_size
;
549 err
= snd_pcm_hw_params_set_buffer_size_near (
557 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
558 size_in_usec
? "time" : "size", req
->buffer_size
);
562 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
563 dolog ("Requested buffer %s %u was rejected, using %lu\n",
564 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
567 if (req
->period_size
) {
572 unsigned int ptime
= req
->period_size
;
574 err
= snd_pcm_hw_params_set_period_time_near (
584 snd_pcm_uframes_t psize
= req
->period_size
;
586 err
= snd_pcm_hw_params_set_period_size_near (
596 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
597 size_in_usec
? "time" : "size", req
->period_size
);
601 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
602 dolog ("Requested period %s %u was rejected, using %lu\n",
603 size_in_usec
? "time" : "size", req
->period_size
, obt
);
606 err
= snd_pcm_hw_params (handle
, hw_params
);
608 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
612 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
614 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
618 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
620 alsa_logerr2 (err
, typ
, "Failed to get format\n");
624 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
625 dolog ("Invalid format was returned %d\n", obtfmt
);
629 err
= snd_pcm_prepare (handle
);
631 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
635 if (!in
&& conf
->threshold
) {
636 snd_pcm_uframes_t threshold
;
639 bytes_per_sec
= freq
<< (nchannels
== 2);
657 threshold
= (conf
->threshold
* bytes_per_sec
) / 1000;
658 alsa_set_threshold (handle
, threshold
);
661 obt
->nchannels
= nchannels
;
663 obt
->samples
= obt_buffer_size
;
667 if (obtfmt
!= req
->fmt
||
668 obt
->nchannels
!= req
->nchannels
||
669 obt
->freq
!= req
->freq
) {
670 dolog ("Audio parameters for %s\n", typ
);
671 alsa_dump_info (req
, obt
, obtfmt
);
675 alsa_dump_info (req
, obt
, obtfmt
);
680 alsa_anal_close1 (&handle
);
684 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
686 snd_pcm_sframes_t avail
;
688 avail
= snd_pcm_avail_update (handle
);
690 if (avail
== -EPIPE
) {
691 if (!alsa_recover (handle
)) {
692 avail
= snd_pcm_avail_update (handle
);
698 "Could not obtain number of available frames\n");
706 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
708 HWVoiceOut
*hw
= &alsa
->hw
;
710 while (alsa
->pending
) {
711 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
712 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
713 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
716 snd_pcm_sframes_t written
;
718 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
723 trace_alsa_wrote_zero(len
);
727 if (alsa_recover (alsa
->handle
)) {
728 alsa_logerr (written
, "Failed to write %d frames\n",
732 trace_alsa_xrun_out();
736 /* stream is suspended and waiting for an
737 application recovery */
738 if (alsa_resume (alsa
->handle
)) {
739 alsa_logerr (written
, "Failed to write %d frames\n",
743 trace_alsa_resume_out();
750 alsa_logerr (written
, "Failed to write %d frames from %p\n",
756 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
757 alsa
->pending
-= written
;
763 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
765 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
767 snd_pcm_sframes_t avail
;
769 avail
= alsa_get_avail (alsa
->handle
);
771 dolog ("Could not get number of available playback frames\n");
775 decr
= audio_MIN (live
, avail
);
776 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
777 alsa
->pending
+= decr
;
778 alsa_write_pending (alsa
);
782 static void alsa_fini_out (HWVoiceOut
*hw
)
784 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
786 ldebug ("alsa_fini\n");
787 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
789 g_free(alsa
->pcm_buf
);
790 alsa
->pcm_buf
= NULL
;
793 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
796 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
797 struct alsa_params_req req
;
798 struct alsa_params_obt obt
;
800 struct audsettings obt_as
;
801 ALSAConf
*conf
= drv_opaque
;
803 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
805 req
.nchannels
= as
->nchannels
;
806 req
.period_size
= conf
->period_size_out
;
807 req
.buffer_size
= conf
->buffer_size_out
;
808 req
.size_in_usec
= conf
->size_in_usec_out
;
810 (conf
->period_size_out_overridden
? 1 : 0) |
811 (conf
->buffer_size_out_overridden
? 2 : 0);
813 if (alsa_open (0, &req
, &obt
, &handle
, conf
)) {
817 obt_as
.freq
= obt
.freq
;
818 obt_as
.nchannels
= obt
.nchannels
;
819 obt_as
.fmt
= obt
.fmt
;
820 obt_as
.endianness
= obt
.endianness
;
822 audio_pcm_init_info (&hw
->info
, &obt_as
);
823 hw
->samples
= obt
.samples
;
825 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
826 if (!alsa
->pcm_buf
) {
827 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
828 hw
->samples
, 1 << hw
->info
.shift
);
829 alsa_anal_close1 (&handle
);
833 alsa
->handle
= handle
;
834 alsa
->pollhlp
.conf
= conf
;
838 #define VOICE_CTL_PAUSE 0
839 #define VOICE_CTL_PREPARE 1
840 #define VOICE_CTL_START 2
842 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
846 if (ctl
== VOICE_CTL_PAUSE
) {
847 err
= snd_pcm_drop (handle
);
849 alsa_logerr (err
, "Could not stop %s\n", typ
);
854 err
= snd_pcm_prepare (handle
);
856 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
859 if (ctl
== VOICE_CTL_START
) {
860 err
= snd_pcm_start(handle
);
862 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
871 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
873 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
882 poll_mode
= va_arg (ap
, int);
885 ldebug ("enabling voice\n");
886 if (poll_mode
&& alsa_poll_out (hw
)) {
889 hw
->poll_mode
= poll_mode
;
890 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
894 ldebug ("disabling voice\n");
897 alsa_fini_poll (&alsa
->pollhlp
);
899 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
905 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
907 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
908 struct alsa_params_req req
;
909 struct alsa_params_obt obt
;
911 struct audsettings obt_as
;
912 ALSAConf
*conf
= drv_opaque
;
914 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
916 req
.nchannels
= as
->nchannels
;
917 req
.period_size
= conf
->period_size_in
;
918 req
.buffer_size
= conf
->buffer_size_in
;
919 req
.size_in_usec
= conf
->size_in_usec_in
;
921 (conf
->period_size_in_overridden
? 1 : 0) |
922 (conf
->buffer_size_in_overridden
? 2 : 0);
924 if (alsa_open (1, &req
, &obt
, &handle
, conf
)) {
928 obt_as
.freq
= obt
.freq
;
929 obt_as
.nchannels
= obt
.nchannels
;
930 obt_as
.fmt
= obt
.fmt
;
931 obt_as
.endianness
= obt
.endianness
;
933 audio_pcm_init_info (&hw
->info
, &obt_as
);
934 hw
->samples
= obt
.samples
;
936 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
937 if (!alsa
->pcm_buf
) {
938 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
939 hw
->samples
, 1 << hw
->info
.shift
);
940 alsa_anal_close1 (&handle
);
944 alsa
->handle
= handle
;
945 alsa
->pollhlp
.conf
= conf
;
949 static void alsa_fini_in (HWVoiceIn
*hw
)
951 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
953 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
955 g_free(alsa
->pcm_buf
);
956 alsa
->pcm_buf
= NULL
;
959 static int alsa_run_in (HWVoiceIn
*hw
)
961 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
962 int hwshift
= hw
->info
.shift
;
964 int live
= audio_pcm_hw_get_live_in (hw
);
965 int dead
= hw
->samples
- live
;
971 { .add
= hw
->wpos
, .len
= 0 },
972 { .add
= 0, .len
= 0 }
974 snd_pcm_sframes_t avail
;
975 snd_pcm_uframes_t read_samples
= 0;
981 avail
= alsa_get_avail (alsa
->handle
);
983 dolog ("Could not get number of captured frames\n");
988 snd_pcm_state_t state
;
990 state
= snd_pcm_state (alsa
->handle
);
992 case SND_PCM_STATE_PREPARED
:
995 case SND_PCM_STATE_SUSPENDED
:
996 /* stream is suspended and waiting for an application recovery */
997 if (alsa_resume (alsa
->handle
)) {
998 dolog ("Failed to resume suspended input stream\n");
1001 trace_alsa_resume_in();
1004 trace_alsa_no_frames(state
);
1009 decr
= audio_MIN (dead
, avail
);
1014 if (hw
->wpos
+ decr
> hw
->samples
) {
1015 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1016 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1022 for (i
= 0; i
< 2; ++i
) {
1024 struct st_sample
*dst
;
1025 snd_pcm_sframes_t nread
;
1026 snd_pcm_uframes_t len
;
1030 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1031 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1034 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1039 trace_alsa_read_zero(len
);
1043 if (alsa_recover (alsa
->handle
)) {
1044 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1047 trace_alsa_xrun_in();
1056 "Failed to read %ld frames from %p\n",
1064 hw
->conv (dst
, src
, nread
);
1066 src
= advance (src
, nread
<< hwshift
);
1069 read_samples
+= nread
;
1075 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1076 return read_samples
;
1079 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1081 return audio_pcm_sw_read (sw
, buf
, size
);
1084 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1086 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1095 poll_mode
= va_arg (ap
, int);
1098 ldebug ("enabling voice\n");
1099 if (poll_mode
&& alsa_poll_in (hw
)) {
1102 hw
->poll_mode
= poll_mode
;
1104 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1108 ldebug ("disabling voice\n");
1109 if (hw
->poll_mode
) {
1111 alsa_fini_poll (&alsa
->pollhlp
);
1113 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1119 static ALSAConf glob_conf
= {
1120 .buffer_size_out
= 4096,
1121 .period_size_out
= 1024,
1122 .pcm_name_out
= "default",
1123 .pcm_name_in
= "default",
1126 static void *alsa_audio_init (void)
1128 ALSAConf
*conf
= g_malloc(sizeof(ALSAConf
));
1133 static void alsa_audio_fini (void *opaque
)
1138 static struct audio_option alsa_options
[] = {
1140 .name
= "DAC_SIZE_IN_USEC",
1141 .tag
= AUD_OPT_BOOL
,
1142 .valp
= &glob_conf
.size_in_usec_out
,
1143 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1146 .name
= "DAC_PERIOD_SIZE",
1148 .valp
= &glob_conf
.period_size_out
,
1149 .descr
= "DAC period size (0 to go with system default)",
1150 .overriddenp
= &glob_conf
.period_size_out_overridden
1153 .name
= "DAC_BUFFER_SIZE",
1155 .valp
= &glob_conf
.buffer_size_out
,
1156 .descr
= "DAC buffer size (0 to go with system default)",
1157 .overriddenp
= &glob_conf
.buffer_size_out_overridden
1160 .name
= "ADC_SIZE_IN_USEC",
1161 .tag
= AUD_OPT_BOOL
,
1162 .valp
= &glob_conf
.size_in_usec_in
,
1164 "ADC period/buffer size in microseconds (otherwise in frames)"
1167 .name
= "ADC_PERIOD_SIZE",
1169 .valp
= &glob_conf
.period_size_in
,
1170 .descr
= "ADC period size (0 to go with system default)",
1171 .overriddenp
= &glob_conf
.period_size_in_overridden
1174 .name
= "ADC_BUFFER_SIZE",
1176 .valp
= &glob_conf
.buffer_size_in
,
1177 .descr
= "ADC buffer size (0 to go with system default)",
1178 .overriddenp
= &glob_conf
.buffer_size_in_overridden
1181 .name
= "THRESHOLD",
1183 .valp
= &glob_conf
.threshold
,
1184 .descr
= "(undocumented)"
1189 .valp
= &glob_conf
.pcm_name_out
,
1190 .descr
= "DAC device name (for instance dmix)"
1195 .valp
= &glob_conf
.pcm_name_in
,
1196 .descr
= "ADC device name"
1198 { /* End of list */ }
1201 static struct audio_pcm_ops alsa_pcm_ops
= {
1202 .init_out
= alsa_init_out
,
1203 .fini_out
= alsa_fini_out
,
1204 .run_out
= alsa_run_out
,
1205 .write
= alsa_write
,
1206 .ctl_out
= alsa_ctl_out
,
1208 .init_in
= alsa_init_in
,
1209 .fini_in
= alsa_fini_in
,
1210 .run_in
= alsa_run_in
,
1212 .ctl_in
= alsa_ctl_in
,
1215 struct audio_driver alsa_audio_driver
= {
1217 .descr
= "ALSA http://www.alsa-project.org",
1218 .options
= alsa_options
,
1219 .init
= alsa_audio_init
,
1220 .fini
= alsa_audio_fini
,
1221 .pcm_ops
= &alsa_pcm_ops
,
1222 .can_be_default
= 1,
1223 .max_voices_out
= INT_MAX
,
1224 .max_voices_in
= INT_MAX
,
1225 .voice_size_out
= sizeof (ALSAVoiceOut
),
1226 .voice_size_in
= sizeof (ALSAVoiceIn
)