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 (AudioFormat fmt
, int endianness
)
300 case AUDIO_FORMAT_S8
:
301 return SND_PCM_FORMAT_S8
;
303 case AUDIO_FORMAT_U8
:
304 return SND_PCM_FORMAT_U8
;
306 case AUDIO_FORMAT_S16
:
308 return SND_PCM_FORMAT_S16_BE
;
311 return SND_PCM_FORMAT_S16_LE
;
314 case AUDIO_FORMAT_U16
:
316 return SND_PCM_FORMAT_U16_BE
;
319 return SND_PCM_FORMAT_U16_LE
;
322 case AUDIO_FORMAT_S32
:
324 return SND_PCM_FORMAT_S32_BE
;
327 return SND_PCM_FORMAT_S32_LE
;
330 case AUDIO_FORMAT_U32
:
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
, AudioFormat
*fmt
,
351 case SND_PCM_FORMAT_S8
:
353 *fmt
= AUDIO_FORMAT_S8
;
356 case SND_PCM_FORMAT_U8
:
358 *fmt
= AUDIO_FORMAT_U8
;
361 case SND_PCM_FORMAT_S16_LE
:
363 *fmt
= AUDIO_FORMAT_S16
;
366 case SND_PCM_FORMAT_U16_LE
:
368 *fmt
= AUDIO_FORMAT_U16
;
371 case SND_PCM_FORMAT_S16_BE
:
373 *fmt
= AUDIO_FORMAT_S16
;
376 case SND_PCM_FORMAT_U16_BE
:
378 *fmt
= AUDIO_FORMAT_U16
;
381 case SND_PCM_FORMAT_S32_LE
:
383 *fmt
= AUDIO_FORMAT_S32
;
386 case SND_PCM_FORMAT_U32_LE
:
388 *fmt
= AUDIO_FORMAT_U32
;
391 case SND_PCM_FORMAT_S32_BE
:
393 *fmt
= AUDIO_FORMAT_S32
;
396 case SND_PCM_FORMAT_U32_BE
:
398 *fmt
= AUDIO_FORMAT_U32
;
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);
641 case AUDIO_FORMAT_S8
:
642 case AUDIO_FORMAT_U8
:
645 case AUDIO_FORMAT_S16
:
646 case AUDIO_FORMAT_U16
:
650 case AUDIO_FORMAT_S32
:
651 case AUDIO_FORMAT_U32
:
659 threshold
= (conf
->threshold
* bytes_per_sec
) / 1000;
660 alsa_set_threshold (handle
, threshold
);
663 obt
->nchannels
= nchannels
;
665 obt
->samples
= obt_buffer_size
;
669 if (obtfmt
!= req
->fmt
||
670 obt
->nchannels
!= req
->nchannels
||
671 obt
->freq
!= req
->freq
) {
672 dolog ("Audio parameters for %s\n", typ
);
673 alsa_dump_info (req
, obt
, obtfmt
);
677 alsa_dump_info (req
, obt
, obtfmt
);
682 alsa_anal_close1 (&handle
);
686 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
688 snd_pcm_sframes_t avail
;
690 avail
= snd_pcm_avail_update (handle
);
692 if (avail
== -EPIPE
) {
693 if (!alsa_recover (handle
)) {
694 avail
= snd_pcm_avail_update (handle
);
700 "Could not obtain number of available frames\n");
708 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
710 HWVoiceOut
*hw
= &alsa
->hw
;
712 while (alsa
->pending
) {
713 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
714 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
715 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
718 snd_pcm_sframes_t written
;
720 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
725 trace_alsa_wrote_zero(len
);
729 if (alsa_recover (alsa
->handle
)) {
730 alsa_logerr (written
, "Failed to write %d frames\n",
734 trace_alsa_xrun_out();
738 /* stream is suspended and waiting for an
739 application recovery */
740 if (alsa_resume (alsa
->handle
)) {
741 alsa_logerr (written
, "Failed to write %d frames\n",
745 trace_alsa_resume_out();
752 alsa_logerr (written
, "Failed to write %d frames from %p\n",
758 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
759 alsa
->pending
-= written
;
765 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
767 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
769 snd_pcm_sframes_t avail
;
771 avail
= alsa_get_avail (alsa
->handle
);
773 dolog ("Could not get number of available playback frames\n");
777 decr
= audio_MIN (live
, avail
);
778 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
779 alsa
->pending
+= decr
;
780 alsa_write_pending (alsa
);
784 static void alsa_fini_out (HWVoiceOut
*hw
)
786 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
788 ldebug ("alsa_fini\n");
789 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
791 g_free(alsa
->pcm_buf
);
792 alsa
->pcm_buf
= NULL
;
795 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
798 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
799 struct alsa_params_req req
;
800 struct alsa_params_obt obt
;
802 struct audsettings obt_as
;
803 ALSAConf
*conf
= drv_opaque
;
805 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
807 req
.nchannels
= as
->nchannels
;
808 req
.period_size
= conf
->period_size_out
;
809 req
.buffer_size
= conf
->buffer_size_out
;
810 req
.size_in_usec
= conf
->size_in_usec_out
;
812 (conf
->period_size_out_overridden
? 1 : 0) |
813 (conf
->buffer_size_out_overridden
? 2 : 0);
815 if (alsa_open (0, &req
, &obt
, &handle
, conf
)) {
819 obt_as
.freq
= obt
.freq
;
820 obt_as
.nchannels
= obt
.nchannels
;
821 obt_as
.fmt
= obt
.fmt
;
822 obt_as
.endianness
= obt
.endianness
;
824 audio_pcm_init_info (&hw
->info
, &obt_as
);
825 hw
->samples
= obt
.samples
;
827 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
828 if (!alsa
->pcm_buf
) {
829 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
830 hw
->samples
, 1 << hw
->info
.shift
);
831 alsa_anal_close1 (&handle
);
835 alsa
->handle
= handle
;
836 alsa
->pollhlp
.conf
= conf
;
840 #define VOICE_CTL_PAUSE 0
841 #define VOICE_CTL_PREPARE 1
842 #define VOICE_CTL_START 2
844 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
848 if (ctl
== VOICE_CTL_PAUSE
) {
849 err
= snd_pcm_drop (handle
);
851 alsa_logerr (err
, "Could not stop %s\n", typ
);
856 err
= snd_pcm_prepare (handle
);
858 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
861 if (ctl
== VOICE_CTL_START
) {
862 err
= snd_pcm_start(handle
);
864 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
873 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
875 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
884 poll_mode
= va_arg (ap
, int);
887 ldebug ("enabling voice\n");
888 if (poll_mode
&& alsa_poll_out (hw
)) {
891 hw
->poll_mode
= poll_mode
;
892 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
896 ldebug ("disabling voice\n");
899 alsa_fini_poll (&alsa
->pollhlp
);
901 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
907 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
909 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
910 struct alsa_params_req req
;
911 struct alsa_params_obt obt
;
913 struct audsettings obt_as
;
914 ALSAConf
*conf
= drv_opaque
;
916 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
918 req
.nchannels
= as
->nchannels
;
919 req
.period_size
= conf
->period_size_in
;
920 req
.buffer_size
= conf
->buffer_size_in
;
921 req
.size_in_usec
= conf
->size_in_usec_in
;
923 (conf
->period_size_in_overridden
? 1 : 0) |
924 (conf
->buffer_size_in_overridden
? 2 : 0);
926 if (alsa_open (1, &req
, &obt
, &handle
, conf
)) {
930 obt_as
.freq
= obt
.freq
;
931 obt_as
.nchannels
= obt
.nchannels
;
932 obt_as
.fmt
= obt
.fmt
;
933 obt_as
.endianness
= obt
.endianness
;
935 audio_pcm_init_info (&hw
->info
, &obt_as
);
936 hw
->samples
= obt
.samples
;
938 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
939 if (!alsa
->pcm_buf
) {
940 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
941 hw
->samples
, 1 << hw
->info
.shift
);
942 alsa_anal_close1 (&handle
);
946 alsa
->handle
= handle
;
947 alsa
->pollhlp
.conf
= conf
;
951 static void alsa_fini_in (HWVoiceIn
*hw
)
953 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
955 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
957 g_free(alsa
->pcm_buf
);
958 alsa
->pcm_buf
= NULL
;
961 static int alsa_run_in (HWVoiceIn
*hw
)
963 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
964 int hwshift
= hw
->info
.shift
;
966 int live
= audio_pcm_hw_get_live_in (hw
);
967 int dead
= hw
->samples
- live
;
973 { .add
= hw
->wpos
, .len
= 0 },
974 { .add
= 0, .len
= 0 }
976 snd_pcm_sframes_t avail
;
977 snd_pcm_uframes_t read_samples
= 0;
983 avail
= alsa_get_avail (alsa
->handle
);
985 dolog ("Could not get number of captured frames\n");
990 snd_pcm_state_t state
;
992 state
= snd_pcm_state (alsa
->handle
);
994 case SND_PCM_STATE_PREPARED
:
997 case SND_PCM_STATE_SUSPENDED
:
998 /* stream is suspended and waiting for an application recovery */
999 if (alsa_resume (alsa
->handle
)) {
1000 dolog ("Failed to resume suspended input stream\n");
1003 trace_alsa_resume_in();
1006 trace_alsa_no_frames(state
);
1011 decr
= audio_MIN (dead
, avail
);
1016 if (hw
->wpos
+ decr
> hw
->samples
) {
1017 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1018 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1024 for (i
= 0; i
< 2; ++i
) {
1026 struct st_sample
*dst
;
1027 snd_pcm_sframes_t nread
;
1028 snd_pcm_uframes_t len
;
1032 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1033 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1036 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1041 trace_alsa_read_zero(len
);
1045 if (alsa_recover (alsa
->handle
)) {
1046 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1049 trace_alsa_xrun_in();
1058 "Failed to read %ld frames from %p\n",
1066 hw
->conv (dst
, src
, nread
);
1068 src
= advance (src
, nread
<< hwshift
);
1071 read_samples
+= nread
;
1077 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1078 return read_samples
;
1081 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1083 return audio_pcm_sw_read (sw
, buf
, size
);
1086 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1088 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1097 poll_mode
= va_arg (ap
, int);
1100 ldebug ("enabling voice\n");
1101 if (poll_mode
&& alsa_poll_in (hw
)) {
1104 hw
->poll_mode
= poll_mode
;
1106 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1110 ldebug ("disabling voice\n");
1111 if (hw
->poll_mode
) {
1113 alsa_fini_poll (&alsa
->pollhlp
);
1115 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1121 static ALSAConf glob_conf
= {
1122 .buffer_size_out
= 4096,
1123 .period_size_out
= 1024,
1124 .pcm_name_out
= "default",
1125 .pcm_name_in
= "default",
1128 static void *alsa_audio_init (void)
1130 ALSAConf
*conf
= g_malloc(sizeof(ALSAConf
));
1135 static void alsa_audio_fini (void *opaque
)
1140 static struct audio_option alsa_options
[] = {
1142 .name
= "DAC_SIZE_IN_USEC",
1143 .tag
= AUD_OPT_BOOL
,
1144 .valp
= &glob_conf
.size_in_usec_out
,
1145 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1148 .name
= "DAC_PERIOD_SIZE",
1150 .valp
= &glob_conf
.period_size_out
,
1151 .descr
= "DAC period size (0 to go with system default)",
1152 .overriddenp
= &glob_conf
.period_size_out_overridden
1155 .name
= "DAC_BUFFER_SIZE",
1157 .valp
= &glob_conf
.buffer_size_out
,
1158 .descr
= "DAC buffer size (0 to go with system default)",
1159 .overriddenp
= &glob_conf
.buffer_size_out_overridden
1162 .name
= "ADC_SIZE_IN_USEC",
1163 .tag
= AUD_OPT_BOOL
,
1164 .valp
= &glob_conf
.size_in_usec_in
,
1166 "ADC period/buffer size in microseconds (otherwise in frames)"
1169 .name
= "ADC_PERIOD_SIZE",
1171 .valp
= &glob_conf
.period_size_in
,
1172 .descr
= "ADC period size (0 to go with system default)",
1173 .overriddenp
= &glob_conf
.period_size_in_overridden
1176 .name
= "ADC_BUFFER_SIZE",
1178 .valp
= &glob_conf
.buffer_size_in
,
1179 .descr
= "ADC buffer size (0 to go with system default)",
1180 .overriddenp
= &glob_conf
.buffer_size_in_overridden
1183 .name
= "THRESHOLD",
1185 .valp
= &glob_conf
.threshold
,
1186 .descr
= "(undocumented)"
1191 .valp
= &glob_conf
.pcm_name_out
,
1192 .descr
= "DAC device name (for instance dmix)"
1197 .valp
= &glob_conf
.pcm_name_in
,
1198 .descr
= "ADC device name"
1200 { /* End of list */ }
1203 static struct audio_pcm_ops alsa_pcm_ops
= {
1204 .init_out
= alsa_init_out
,
1205 .fini_out
= alsa_fini_out
,
1206 .run_out
= alsa_run_out
,
1207 .write
= alsa_write
,
1208 .ctl_out
= alsa_ctl_out
,
1210 .init_in
= alsa_init_in
,
1211 .fini_in
= alsa_fini_in
,
1212 .run_in
= alsa_run_in
,
1214 .ctl_in
= alsa_ctl_in
,
1217 static struct audio_driver alsa_audio_driver
= {
1219 .descr
= "ALSA http://www.alsa-project.org",
1220 .options
= alsa_options
,
1221 .init
= alsa_audio_init
,
1222 .fini
= alsa_audio_fini
,
1223 .pcm_ops
= &alsa_pcm_ops
,
1224 .can_be_default
= 1,
1225 .max_voices_out
= INT_MAX
,
1226 .max_voices_in
= INT_MAX
,
1227 .voice_size_out
= sizeof (ALSAVoiceOut
),
1228 .voice_size_in
= sizeof (ALSAVoiceIn
)
1231 static void register_audio_alsa(void)
1233 audio_driver_register(&alsa_audio_driver
);
1235 type_init(register_audio_alsa
);