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 "qemu/osdep.h"
25 #include <alsa/asoundlib.h>
26 #include "qemu-common.h"
27 #include "qemu/main-loop.h"
31 #pragma GCC diagnostic ignored "-Waddress"
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
36 typedef struct ALSAConf
{
39 const char *pcm_name_in
;
40 const char *pcm_name_out
;
41 unsigned int buffer_size_in
;
42 unsigned int period_size_in
;
43 unsigned int buffer_size_out
;
44 unsigned int period_size_out
;
45 unsigned int threshold
;
47 int buffer_size_in_overridden
;
48 int period_size_in_overridden
;
50 int buffer_size_out_overridden
;
51 int period_size_out_overridden
;
62 typedef struct ALSAVoiceOut
{
68 struct pollhlp pollhlp
;
71 typedef struct ALSAVoiceIn
{
75 struct pollhlp pollhlp
;
78 struct alsa_params_req
{
84 unsigned int buffer_size
;
85 unsigned int period_size
;
88 struct alsa_params_obt
{
93 snd_pcm_uframes_t samples
;
96 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
101 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
104 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
107 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
116 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
119 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
122 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
125 static void alsa_fini_poll (struct pollhlp
*hlp
)
128 struct pollfd
*pfds
= hlp
->pfds
;
131 for (i
= 0; i
< hlp
->count
; ++i
) {
132 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
141 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
143 int err
= snd_pcm_close (*handlep
);
145 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
150 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
152 alsa_fini_poll (hlp
);
153 alsa_anal_close1 (handlep
);
156 static int alsa_recover (snd_pcm_t
*handle
)
158 int err
= snd_pcm_prepare (handle
);
160 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
166 static int alsa_resume (snd_pcm_t
*handle
)
168 int err
= snd_pcm_resume (handle
);
170 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
176 static void alsa_poll_handler (void *opaque
)
179 snd_pcm_state_t state
;
180 struct pollhlp
*hlp
= opaque
;
181 unsigned short revents
;
183 count
= poll (hlp
->pfds
, hlp
->count
, 0);
185 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
193 /* XXX: ALSA example uses initial count, not the one returned by
195 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
196 hlp
->count
, &revents
);
198 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
202 if (!(revents
& hlp
->mask
)) {
203 trace_alsa_revents(revents
);
207 state
= snd_pcm_state (hlp
->handle
);
209 case SND_PCM_STATE_SETUP
:
210 alsa_recover (hlp
->handle
);
213 case SND_PCM_STATE_XRUN
:
214 alsa_recover (hlp
->handle
);
217 case SND_PCM_STATE_SUSPENDED
:
218 alsa_resume (hlp
->handle
);
221 case SND_PCM_STATE_PREPARED
:
222 audio_run ("alsa run (prepared)");
225 case SND_PCM_STATE_RUNNING
:
226 audio_run ("alsa run (running)");
230 dolog ("Unexpected state %d\n", state
);
234 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
239 count
= snd_pcm_poll_descriptors_count (handle
);
241 dolog ("Could not initialize poll mode\n"
242 "Invalid number of poll descriptors %d\n", count
);
246 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
248 dolog ("Could not initialize poll mode\n");
252 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
254 alsa_logerr (err
, "Could not initialize poll mode\n"
255 "Could not obtain poll descriptors\n");
260 for (i
= 0; i
< count
; ++i
) {
261 if (pfds
[i
].events
& POLLIN
) {
262 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
264 if (pfds
[i
].events
& POLLOUT
) {
265 trace_alsa_pollout(i
, pfds
[i
].fd
);
266 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
268 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
273 hlp
->handle
= handle
;
278 static int alsa_poll_out (HWVoiceOut
*hw
)
280 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
282 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
285 static int alsa_poll_in (HWVoiceIn
*hw
)
287 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
289 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
292 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
294 return audio_pcm_sw_write (sw
, buf
, len
);
297 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
, int endianness
)
301 return SND_PCM_FORMAT_S8
;
304 return SND_PCM_FORMAT_U8
;
308 return SND_PCM_FORMAT_S16_BE
;
311 return SND_PCM_FORMAT_S16_LE
;
316 return SND_PCM_FORMAT_U16_BE
;
319 return SND_PCM_FORMAT_U16_LE
;
324 return SND_PCM_FORMAT_S32_BE
;
327 return SND_PCM_FORMAT_S32_LE
;
332 return SND_PCM_FORMAT_U32_BE
;
335 return SND_PCM_FORMAT_U32_LE
;
339 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
343 return SND_PCM_FORMAT_U8
;
347 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
351 case SND_PCM_FORMAT_S8
:
356 case SND_PCM_FORMAT_U8
:
361 case SND_PCM_FORMAT_S16_LE
:
366 case SND_PCM_FORMAT_U16_LE
:
371 case SND_PCM_FORMAT_S16_BE
:
376 case SND_PCM_FORMAT_U16_BE
:
381 case SND_PCM_FORMAT_S32_LE
:
386 case SND_PCM_FORMAT_U32_LE
:
391 case SND_PCM_FORMAT_S32_BE
:
396 case SND_PCM_FORMAT_U32_BE
:
402 dolog ("Unrecognized audio format %d\n", alsafmt
);
409 static void alsa_dump_info (struct alsa_params_req
*req
,
410 struct alsa_params_obt
*obt
,
411 snd_pcm_format_t obtfmt
)
413 dolog ("parameter | requested value | obtained value\n");
414 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
415 dolog ("channels | %10d | %10d\n",
416 req
->nchannels
, obt
->nchannels
);
417 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
418 dolog ("============================================\n");
419 dolog ("requested: buffer size %d period size %d\n",
420 req
->buffer_size
, req
->period_size
);
421 dolog ("obtained: samples %ld\n", obt
->samples
);
424 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
427 snd_pcm_sw_params_t
*sw_params
;
429 snd_pcm_sw_params_alloca (&sw_params
);
431 err
= snd_pcm_sw_params_current (handle
, sw_params
);
433 dolog ("Could not fully initialize DAC\n");
434 alsa_logerr (err
, "Failed to get current software parameters\n");
438 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
440 dolog ("Could not fully initialize DAC\n");
441 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
446 err
= snd_pcm_sw_params (handle
, sw_params
);
448 dolog ("Could not fully initialize DAC\n");
449 alsa_logerr (err
, "Failed to set software parameters\n");
454 static int alsa_open (int in
, struct alsa_params_req
*req
,
455 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
459 snd_pcm_hw_params_t
*hw_params
;
462 unsigned int freq
, nchannels
;
463 const char *pcm_name
= in
? conf
->pcm_name_in
: conf
->pcm_name_out
;
464 snd_pcm_uframes_t obt_buffer_size
;
465 const char *typ
= in
? "ADC" : "DAC";
466 snd_pcm_format_t obtfmt
;
469 nchannels
= req
->nchannels
;
470 size_in_usec
= req
->size_in_usec
;
472 snd_pcm_hw_params_alloca (&hw_params
);
477 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
481 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
485 err
= snd_pcm_hw_params_any (handle
, hw_params
);
487 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
491 err
= snd_pcm_hw_params_set_access (
494 SND_PCM_ACCESS_RW_INTERLEAVED
497 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
501 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
503 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
506 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
508 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
512 err
= snd_pcm_hw_params_set_channels_near (
518 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
523 if (nchannels
!= 1 && nchannels
!= 2) {
524 alsa_logerr2 (err
, typ
,
525 "Can not handle obtained number of channels %d\n",
530 if (req
->buffer_size
) {
535 unsigned int btime
= req
->buffer_size
;
537 err
= snd_pcm_hw_params_set_buffer_time_near (
546 snd_pcm_uframes_t bsize
= req
->buffer_size
;
548 err
= snd_pcm_hw_params_set_buffer_size_near (
556 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
557 size_in_usec
? "time" : "size", req
->buffer_size
);
561 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
562 dolog ("Requested buffer %s %u was rejected, using %lu\n",
563 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
566 if (req
->period_size
) {
571 unsigned int ptime
= req
->period_size
;
573 err
= snd_pcm_hw_params_set_period_time_near (
583 snd_pcm_uframes_t psize
= req
->period_size
;
585 err
= snd_pcm_hw_params_set_period_size_near (
595 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
596 size_in_usec
? "time" : "size", req
->period_size
);
600 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
601 dolog ("Requested period %s %u was rejected, using %lu\n",
602 size_in_usec
? "time" : "size", req
->period_size
, obt
);
605 err
= snd_pcm_hw_params (handle
, hw_params
);
607 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
611 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
613 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
617 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
619 alsa_logerr2 (err
, typ
, "Failed to get format\n");
623 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
624 dolog ("Invalid format was returned %d\n", obtfmt
);
628 err
= snd_pcm_prepare (handle
);
630 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
634 if (!in
&& conf
->threshold
) {
635 snd_pcm_uframes_t threshold
;
638 bytes_per_sec
= freq
<< (nchannels
== 2);
656 threshold
= (conf
->threshold
* bytes_per_sec
) / 1000;
657 alsa_set_threshold (handle
, threshold
);
660 obt
->nchannels
= nchannels
;
662 obt
->samples
= obt_buffer_size
;
666 if (obtfmt
!= req
->fmt
||
667 obt
->nchannels
!= req
->nchannels
||
668 obt
->freq
!= req
->freq
) {
669 dolog ("Audio parameters for %s\n", typ
);
670 alsa_dump_info (req
, obt
, obtfmt
);
674 alsa_dump_info (req
, obt
, obtfmt
);
679 alsa_anal_close1 (&handle
);
683 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
685 snd_pcm_sframes_t avail
;
687 avail
= snd_pcm_avail_update (handle
);
689 if (avail
== -EPIPE
) {
690 if (!alsa_recover (handle
)) {
691 avail
= snd_pcm_avail_update (handle
);
697 "Could not obtain number of available frames\n");
705 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
707 HWVoiceOut
*hw
= &alsa
->hw
;
709 while (alsa
->pending
) {
710 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
711 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
712 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
715 snd_pcm_sframes_t written
;
717 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
722 trace_alsa_wrote_zero(len
);
726 if (alsa_recover (alsa
->handle
)) {
727 alsa_logerr (written
, "Failed to write %d frames\n",
731 trace_alsa_xrun_out();
735 /* stream is suspended and waiting for an
736 application recovery */
737 if (alsa_resume (alsa
->handle
)) {
738 alsa_logerr (written
, "Failed to write %d frames\n",
742 trace_alsa_resume_out();
749 alsa_logerr (written
, "Failed to write %d frames from %p\n",
755 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
756 alsa
->pending
-= written
;
762 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
764 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
766 snd_pcm_sframes_t avail
;
768 avail
= alsa_get_avail (alsa
->handle
);
770 dolog ("Could not get number of available playback frames\n");
774 decr
= audio_MIN (live
, avail
);
775 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
776 alsa
->pending
+= decr
;
777 alsa_write_pending (alsa
);
781 static void alsa_fini_out (HWVoiceOut
*hw
)
783 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
785 ldebug ("alsa_fini\n");
786 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
788 g_free(alsa
->pcm_buf
);
789 alsa
->pcm_buf
= NULL
;
792 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
795 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
796 struct alsa_params_req req
;
797 struct alsa_params_obt obt
;
799 struct audsettings obt_as
;
800 ALSAConf
*conf
= drv_opaque
;
802 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
804 req
.nchannels
= as
->nchannels
;
805 req
.period_size
= conf
->period_size_out
;
806 req
.buffer_size
= conf
->buffer_size_out
;
807 req
.size_in_usec
= conf
->size_in_usec_out
;
809 (conf
->period_size_out_overridden
? 1 : 0) |
810 (conf
->buffer_size_out_overridden
? 2 : 0);
812 if (alsa_open (0, &req
, &obt
, &handle
, conf
)) {
816 obt_as
.freq
= obt
.freq
;
817 obt_as
.nchannels
= obt
.nchannels
;
818 obt_as
.fmt
= obt
.fmt
;
819 obt_as
.endianness
= obt
.endianness
;
821 audio_pcm_init_info (&hw
->info
, &obt_as
);
822 hw
->samples
= obt
.samples
;
824 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
825 if (!alsa
->pcm_buf
) {
826 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
827 hw
->samples
, 1 << hw
->info
.shift
);
828 alsa_anal_close1 (&handle
);
832 alsa
->handle
= handle
;
833 alsa
->pollhlp
.conf
= conf
;
837 #define VOICE_CTL_PAUSE 0
838 #define VOICE_CTL_PREPARE 1
839 #define VOICE_CTL_START 2
841 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
845 if (ctl
== VOICE_CTL_PAUSE
) {
846 err
= snd_pcm_drop (handle
);
848 alsa_logerr (err
, "Could not stop %s\n", typ
);
853 err
= snd_pcm_prepare (handle
);
855 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
858 if (ctl
== VOICE_CTL_START
) {
859 err
= snd_pcm_start(handle
);
861 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
870 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
872 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
881 poll_mode
= va_arg (ap
, int);
884 ldebug ("enabling voice\n");
885 if (poll_mode
&& alsa_poll_out (hw
)) {
888 hw
->poll_mode
= poll_mode
;
889 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
893 ldebug ("disabling voice\n");
896 alsa_fini_poll (&alsa
->pollhlp
);
898 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
904 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
906 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
907 struct alsa_params_req req
;
908 struct alsa_params_obt obt
;
910 struct audsettings obt_as
;
911 ALSAConf
*conf
= drv_opaque
;
913 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
915 req
.nchannels
= as
->nchannels
;
916 req
.period_size
= conf
->period_size_in
;
917 req
.buffer_size
= conf
->buffer_size_in
;
918 req
.size_in_usec
= conf
->size_in_usec_in
;
920 (conf
->period_size_in_overridden
? 1 : 0) |
921 (conf
->buffer_size_in_overridden
? 2 : 0);
923 if (alsa_open (1, &req
, &obt
, &handle
, conf
)) {
927 obt_as
.freq
= obt
.freq
;
928 obt_as
.nchannels
= obt
.nchannels
;
929 obt_as
.fmt
= obt
.fmt
;
930 obt_as
.endianness
= obt
.endianness
;
932 audio_pcm_init_info (&hw
->info
, &obt_as
);
933 hw
->samples
= obt
.samples
;
935 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
936 if (!alsa
->pcm_buf
) {
937 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
938 hw
->samples
, 1 << hw
->info
.shift
);
939 alsa_anal_close1 (&handle
);
943 alsa
->handle
= handle
;
944 alsa
->pollhlp
.conf
= conf
;
948 static void alsa_fini_in (HWVoiceIn
*hw
)
950 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
952 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
954 g_free(alsa
->pcm_buf
);
955 alsa
->pcm_buf
= NULL
;
958 static int alsa_run_in (HWVoiceIn
*hw
)
960 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
961 int hwshift
= hw
->info
.shift
;
963 int live
= audio_pcm_hw_get_live_in (hw
);
964 int dead
= hw
->samples
- live
;
970 { .add
= hw
->wpos
, .len
= 0 },
971 { .add
= 0, .len
= 0 }
973 snd_pcm_sframes_t avail
;
974 snd_pcm_uframes_t read_samples
= 0;
980 avail
= alsa_get_avail (alsa
->handle
);
982 dolog ("Could not get number of captured frames\n");
987 snd_pcm_state_t state
;
989 state
= snd_pcm_state (alsa
->handle
);
991 case SND_PCM_STATE_PREPARED
:
994 case SND_PCM_STATE_SUSPENDED
:
995 /* stream is suspended and waiting for an application recovery */
996 if (alsa_resume (alsa
->handle
)) {
997 dolog ("Failed to resume suspended input stream\n");
1000 trace_alsa_resume_in();
1003 trace_alsa_no_frames(state
);
1008 decr
= audio_MIN (dead
, avail
);
1013 if (hw
->wpos
+ decr
> hw
->samples
) {
1014 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1015 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1021 for (i
= 0; i
< 2; ++i
) {
1023 struct st_sample
*dst
;
1024 snd_pcm_sframes_t nread
;
1025 snd_pcm_uframes_t len
;
1029 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1030 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1033 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1038 trace_alsa_read_zero(len
);
1042 if (alsa_recover (alsa
->handle
)) {
1043 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1046 trace_alsa_xrun_in();
1055 "Failed to read %ld frames from %p\n",
1063 hw
->conv (dst
, src
, nread
);
1065 src
= advance (src
, nread
<< hwshift
);
1068 read_samples
+= nread
;
1074 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1075 return read_samples
;
1078 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1080 return audio_pcm_sw_read (sw
, buf
, size
);
1083 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1085 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1094 poll_mode
= va_arg (ap
, int);
1097 ldebug ("enabling voice\n");
1098 if (poll_mode
&& alsa_poll_in (hw
)) {
1101 hw
->poll_mode
= poll_mode
;
1103 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1107 ldebug ("disabling voice\n");
1108 if (hw
->poll_mode
) {
1110 alsa_fini_poll (&alsa
->pollhlp
);
1112 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1118 static ALSAConf glob_conf
= {
1119 .buffer_size_out
= 4096,
1120 .period_size_out
= 1024,
1121 .pcm_name_out
= "default",
1122 .pcm_name_in
= "default",
1125 static void *alsa_audio_init (void)
1127 ALSAConf
*conf
= g_malloc(sizeof(ALSAConf
));
1132 static void alsa_audio_fini (void *opaque
)
1137 static struct audio_option alsa_options
[] = {
1139 .name
= "DAC_SIZE_IN_USEC",
1140 .tag
= AUD_OPT_BOOL
,
1141 .valp
= &glob_conf
.size_in_usec_out
,
1142 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1145 .name
= "DAC_PERIOD_SIZE",
1147 .valp
= &glob_conf
.period_size_out
,
1148 .descr
= "DAC period size (0 to go with system default)",
1149 .overriddenp
= &glob_conf
.period_size_out_overridden
1152 .name
= "DAC_BUFFER_SIZE",
1154 .valp
= &glob_conf
.buffer_size_out
,
1155 .descr
= "DAC buffer size (0 to go with system default)",
1156 .overriddenp
= &glob_conf
.buffer_size_out_overridden
1159 .name
= "ADC_SIZE_IN_USEC",
1160 .tag
= AUD_OPT_BOOL
,
1161 .valp
= &glob_conf
.size_in_usec_in
,
1163 "ADC period/buffer size in microseconds (otherwise in frames)"
1166 .name
= "ADC_PERIOD_SIZE",
1168 .valp
= &glob_conf
.period_size_in
,
1169 .descr
= "ADC period size (0 to go with system default)",
1170 .overriddenp
= &glob_conf
.period_size_in_overridden
1173 .name
= "ADC_BUFFER_SIZE",
1175 .valp
= &glob_conf
.buffer_size_in
,
1176 .descr
= "ADC buffer size (0 to go with system default)",
1177 .overriddenp
= &glob_conf
.buffer_size_in_overridden
1180 .name
= "THRESHOLD",
1182 .valp
= &glob_conf
.threshold
,
1183 .descr
= "(undocumented)"
1188 .valp
= &glob_conf
.pcm_name_out
,
1189 .descr
= "DAC device name (for instance dmix)"
1194 .valp
= &glob_conf
.pcm_name_in
,
1195 .descr
= "ADC device name"
1197 { /* End of list */ }
1200 static struct audio_pcm_ops alsa_pcm_ops
= {
1201 .init_out
= alsa_init_out
,
1202 .fini_out
= alsa_fini_out
,
1203 .run_out
= alsa_run_out
,
1204 .write
= alsa_write
,
1205 .ctl_out
= alsa_ctl_out
,
1207 .init_in
= alsa_init_in
,
1208 .fini_in
= alsa_fini_in
,
1209 .run_in
= alsa_run_in
,
1211 .ctl_in
= alsa_ctl_in
,
1214 static struct audio_driver alsa_audio_driver
= {
1216 .descr
= "ALSA http://www.alsa-project.org",
1217 .options
= alsa_options
,
1218 .init
= alsa_audio_init
,
1219 .fini
= alsa_audio_fini
,
1220 .pcm_ops
= &alsa_pcm_ops
,
1221 .can_be_default
= 1,
1222 .max_voices_out
= INT_MAX
,
1223 .max_voices_in
= INT_MAX
,
1224 .voice_size_out
= sizeof (ALSAVoiceOut
),
1225 .voice_size_in
= sizeof (ALSAVoiceIn
)
1228 static void register_audio_alsa(void)
1230 audio_driver_register(&alsa_audio_driver
);
1232 type_init(register_audio_alsa
);