4 * Copyright (c) 2003-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
27 #include "qemu-timer.h"
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
33 /* #define DEBUG_PLIVE */
34 /* #define DEBUG_LIVE */
35 /* #define DEBUG_OUT */
36 /* #define DEBUG_CAPTURE */
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
41 /* Order of CONFIG_AUDIO_DRIVERS is import.
42 The 1st one is the one used by default, that is the reason
43 that we generate the list.
45 static struct audio_driver
*drvtab
[] = {
51 struct fixed_settings
{
55 struct audsettings settings
;
59 struct fixed_settings fixed_out
;
60 struct fixed_settings fixed_in
;
68 .fixed_out
= { /* DAC fixed settings */
76 .endianness
= AUDIO_HOST_ENDIANNESS
,
80 .fixed_in
= { /* ADC fixed settings */
88 .endianness
= AUDIO_HOST_ENDIANNESS
,
92 .period
= { .hertz
= 250 },
97 static AudioState glob_audio_state
;
99 struct mixeng_volume nominal_volume
= {
110 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
111 /* http://www.multi-platforms.com/Tips/PopCount.htm */
112 uint32_t popcount (uint32_t u
)
114 u
= ((u
&0x55555555) + ((u
>>1)&0x55555555));
115 u
= ((u
&0x33333333) + ((u
>>2)&0x33333333));
116 u
= ((u
&0x0f0f0f0f) + ((u
>>4)&0x0f0f0f0f));
117 u
= ((u
&0x00ff00ff) + ((u
>>8)&0x00ff00ff));
118 u
= ( u
&0x0000ffff) + (u
>>16);
122 inline uint32_t lsbindex (uint32_t u
)
124 return popcount ((u
&-u
)-1);
127 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
130 int audio_bug (const char *funcname
, int cond
)
135 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
138 AUD_log (NULL
, "Save all your work and restart without audio\n");
139 AUD_log (NULL
, "Please send bug report to malc@pulsesoft.com\n");
140 AUD_log (NULL
, "I am sorry\n");
142 AUD_log (NULL
, "Context:\n");
144 #if defined AUDIO_BREAKPOINT_ON_BUG
145 # if defined HOST_I386
146 # if defined __GNUC__
148 # elif defined _MSC_VER
163 static inline int audio_bits_to_index (int bits
)
176 audio_bug ("bits_to_index", 1);
177 AUD_log (NULL
, "invalid bits %d\n", bits
);
182 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
188 cond
= !nmemb
|| !size
;
192 if (audio_bug ("audio_calloc", cond
)) {
193 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
195 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
199 return qemu_mallocz (len
);
202 static char *audio_alloc_prefix (const char *s
)
204 const char qemu_prefix
[] = "QEMU_";
213 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
215 u
= r
+ sizeof (qemu_prefix
) - 1;
217 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
218 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
220 for (i
= 0; i
< len
; ++i
) {
221 u
[i
] = qemu_toupper(u
[i
]);
227 static const char *audio_audfmt_to_string (audfmt_e fmt
)
249 dolog ("Bogus audfmt %d returning S16\n", fmt
);
253 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
256 if (!strcasecmp (s
, "u8")) {
260 else if (!strcasecmp (s
, "u16")) {
264 else if (!strcasecmp (s
, "u32")) {
268 else if (!strcasecmp (s
, "s8")) {
272 else if (!strcasecmp (s
, "s16")) {
276 else if (!strcasecmp (s
, "s32")) {
281 dolog ("Bogus audio format `%s' using %s\n",
282 s
, audio_audfmt_to_string (defval
));
288 static audfmt_e
audio_get_conf_fmt (const char *envname
,
292 const char *var
= getenv (envname
);
297 return audio_string_to_audfmt (var
, defval
, defaultp
);
300 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
305 strval
= getenv (key
);
317 static const char *audio_get_conf_str (const char *key
,
321 const char *val
= getenv (key
);
332 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
334 if (conf
.log_to_monitor
) {
336 monitor_printf(cur_mon
, "%s: ", cap
);
339 monitor_vprintf(cur_mon
, fmt
, ap
);
343 fprintf (stderr
, "%s: ", cap
);
346 vfprintf (stderr
, fmt
, ap
);
350 void AUD_log (const char *cap
, const char *fmt
, ...)
355 AUD_vlog (cap
, fmt
, ap
);
359 static void audio_print_options (const char *prefix
,
360 struct audio_option
*opt
)
365 dolog ("No prefix specified\n");
370 dolog ("No options\n");
374 uprefix
= audio_alloc_prefix (prefix
);
376 for (; opt
->name
; opt
++) {
377 const char *state
= "default";
378 printf (" %s_%s: ", uprefix
, opt
->name
);
380 if (opt
->overriddenp
&& *opt
->overriddenp
) {
387 int *intp
= opt
->valp
;
388 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
394 int *intp
= opt
->valp
;
395 printf ("integer, %s = %d\n", state
, *intp
);
401 audfmt_e
*fmtp
= opt
->valp
;
403 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
405 audio_audfmt_to_string (*fmtp
)
412 const char **strp
= opt
->valp
;
413 printf ("string, %s = %s\n",
415 *strp
? *strp
: "(not set)");
421 dolog ("Bad value tag for option %s_%s %d\n",
422 uprefix
, opt
->name
, opt
->tag
);
425 printf (" %s\n", opt
->descr
);
431 static void audio_process_options (const char *prefix
,
432 struct audio_option
*opt
)
435 const char qemu_prefix
[] = "QEMU_";
436 size_t preflen
, optlen
;
438 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
439 dolog ("prefix = NULL\n");
443 if (audio_bug (AUDIO_FUNC
, !opt
)) {
444 dolog ("opt = NULL\n");
448 preflen
= strlen (prefix
);
450 for (; opt
->name
; opt
++) {
455 dolog ("Option value pointer for `%s' is not set\n",
460 len
= strlen (opt
->name
);
461 /* len of opt->name + len of prefix + size of qemu_prefix
462 * (includes trailing zero) + zero + underscore (on behalf of
464 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
465 optname
= qemu_malloc (optlen
);
467 pstrcpy (optname
, optlen
, qemu_prefix
);
469 /* copy while upper-casing, including trailing zero */
470 for (i
= 0; i
<= preflen
; ++i
) {
471 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
473 pstrcat (optname
, optlen
, "_");
474 pstrcat (optname
, optlen
, opt
->name
);
481 int *intp
= opt
->valp
;
482 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
488 audfmt_e
*fmtp
= opt
->valp
;
489 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
495 const char **strp
= opt
->valp
;
496 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
501 dolog ("Bad value tag for option `%s' - %d\n",
506 if (!opt
->overriddenp
) {
507 opt
->overriddenp
= &opt
->overridden
;
509 *opt
->overriddenp
= !def
;
514 static void audio_print_settings (struct audsettings
*as
)
516 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
520 AUD_log (NULL
, "S8");
523 AUD_log (NULL
, "U8");
526 AUD_log (NULL
, "S16");
529 AUD_log (NULL
, "U16");
532 AUD_log (NULL
, "S32");
535 AUD_log (NULL
, "U32");
538 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
542 AUD_log (NULL
, " endianness=");
543 switch (as
->endianness
) {
545 AUD_log (NULL
, "little");
548 AUD_log (NULL
, "big");
551 AUD_log (NULL
, "invalid");
554 AUD_log (NULL
, "\n");
557 static int audio_validate_settings (struct audsettings
*as
)
561 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
562 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
577 invalid
|= as
->freq
<= 0;
578 return invalid
? -1 : 0;
581 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
583 int bits
= 8, sign
= 0;
603 return info
->freq
== as
->freq
604 && info
->nchannels
== as
->nchannels
605 && info
->sign
== sign
606 && info
->bits
== bits
607 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
610 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
612 int bits
= 8, sign
= 0, shift
= 0;
635 info
->freq
= as
->freq
;
638 info
->nchannels
= as
->nchannels
;
639 info
->shift
= (as
->nchannels
== 2) + shift
;
640 info
->align
= (1 << info
->shift
) - 1;
641 info
->bytes_per_second
= info
->freq
<< info
->shift
;
642 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
645 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
652 memset (buf
, 0x00, len
<< info
->shift
);
655 switch (info
->bits
) {
657 memset (buf
, 0x80, len
<< info
->shift
);
664 int shift
= info
->nchannels
- 1;
667 if (info
->swap_endianness
) {
671 for (i
= 0; i
< len
<< shift
; i
++) {
681 int shift
= info
->nchannels
- 1;
682 int32_t s
= INT32_MAX
;
684 if (info
->swap_endianness
) {
688 for (i
= 0; i
< len
<< shift
; i
++) {
695 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
705 static void noop_conv (struct st_sample
*dst
, const void *src
,
706 int samples
, struct mixeng_volume
*vol
)
714 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
715 struct audsettings
*as
718 CaptureVoiceOut
*cap
;
719 AudioState
*s
= &glob_audio_state
;
721 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
722 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
729 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
731 struct capture_callback
*cb
;
734 dolog ("notification %d sent\n", cmd
);
736 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
737 cb
->ops
.notify (cb
->opaque
, cmd
);
741 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
743 if (cap
->hw
.enabled
!= enabled
) {
744 audcnotification_e cmd
;
745 cap
->hw
.enabled
= enabled
;
746 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
747 audio_notify_capture (cap
, cmd
);
751 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
753 HWVoiceOut
*hw
= &cap
->hw
;
757 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
763 audio_capture_maybe_changed (cap
, enabled
);
766 static void audio_detach_capture (HWVoiceOut
*hw
)
768 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
771 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
772 SWVoiceOut
*sw
= &sc
->sw
;
773 CaptureVoiceOut
*cap
= sc
->cap
;
774 int was_active
= sw
->active
;
777 st_rate_stop (sw
->rate
);
781 LIST_REMOVE (sw
, entries
);
782 LIST_REMOVE (sc
, entries
);
785 /* We have removed soft voice from the capture:
786 this might have changed the overall status of the capture
787 since this might have been the only active voice */
788 audio_recalc_and_notify_capture (cap
);
794 static int audio_attach_capture (HWVoiceOut
*hw
)
796 AudioState
*s
= &glob_audio_state
;
797 CaptureVoiceOut
*cap
;
799 audio_detach_capture (hw
);
800 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
803 HWVoiceOut
*hw_cap
= &cap
->hw
;
805 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
807 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
817 sw
->active
= hw
->enabled
;
818 sw
->conv
= noop_conv
;
819 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
820 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
822 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
826 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
827 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
829 asprintf (&sw
->name
, "for %p %d,%d,%d",
830 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
831 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
834 audio_capture_maybe_changed (cap
, 1);
841 * Hard voice (capture)
843 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
846 int m
= hw
->total_samples_captured
;
848 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
850 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
856 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
858 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
859 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
860 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
867 * Soft voice (capture)
869 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
871 HWVoiceIn
*hw
= sw
->hw
;
872 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
875 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
876 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
880 rpos
= hw
->wpos
- live
;
885 return hw
->samples
+ rpos
;
889 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
891 HWVoiceIn
*hw
= sw
->hw
;
892 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
893 struct st_sample
*src
, *dst
= sw
->buf
;
895 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
897 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
898 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
899 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
903 samples
= size
>> sw
->info
.shift
;
908 swlim
= (live
* sw
->ratio
) >> 32;
909 swlim
= audio_MIN (swlim
, samples
);
912 src
= hw
->conv_buf
+ rpos
;
913 isamp
= hw
->wpos
- rpos
;
916 isamp
= hw
->samples
- rpos
;
924 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
925 dolog ("osamp=%d\n", osamp
);
929 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
931 rpos
= (rpos
+ isamp
) % hw
->samples
;
937 sw
->clip (buf
, sw
->buf
, ret
);
938 sw
->total_hw_samples_acquired
+= total
;
939 return ret
<< sw
->info
.shift
;
943 * Hard voice (playback)
945 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
951 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
952 if (sw
->active
|| !sw
->empty
) {
953 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
962 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
966 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
974 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
975 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
982 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
987 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
988 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
989 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
996 * Soft voice (playback)
998 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1000 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1001 int ret
= 0, pos
= 0, total
= 0;
1007 hwsamples
= sw
->hw
->samples
;
1009 live
= sw
->total_hw_samples_mixed
;
1010 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1011 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1015 if (live
== hwsamples
) {
1017 dolog ("%s is full %d\n", sw
->name
, live
);
1022 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1023 samples
= size
>> sw
->info
.shift
;
1025 dead
= hwsamples
- live
;
1026 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1027 swlim
= audio_MIN (swlim
, samples
);
1029 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1033 dead
= hwsamples
- live
;
1034 left
= hwsamples
- wpos
;
1035 blck
= audio_MIN (dead
, left
);
1044 sw
->hw
->mix_buf
+ wpos
,
1052 wpos
= (wpos
+ osamp
) % hwsamples
;
1056 sw
->total_hw_samples_mixed
+= total
;
1057 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1061 "%s: write size %d ret %d total sw %d\n",
1063 size
>> sw
->info
.shift
,
1065 sw
->total_hw_samples_mixed
1069 return ret
<< sw
->info
.shift
;
1073 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1075 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1076 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1081 #include "audio_template.h"
1083 #include "audio_template.h"
1085 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1090 /* XXX: Consider options */
1094 if (!sw
->hw
->enabled
) {
1095 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1099 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1103 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1108 /* XXX: Consider options */
1112 if (!sw
->hw
->enabled
) {
1113 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1117 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1121 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1123 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1126 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1135 if (sw
->active
!= on
) {
1136 AudioState
*s
= &glob_audio_state
;
1137 SWVoiceOut
*temp_sw
;
1141 hw
->pending_disable
= 0;
1144 if (s
->vm_running
) {
1145 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1153 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1154 temp_sw
= temp_sw
->entries
.le_next
) {
1155 nb_active
+= temp_sw
->active
!= 0;
1158 hw
->pending_disable
= nb_active
== 1;
1162 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1163 sc
->sw
.active
= hw
->enabled
;
1165 audio_capture_maybe_changed (sc
->cap
, 1);
1172 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1181 if (sw
->active
!= on
) {
1182 AudioState
*s
= &glob_audio_state
;
1188 if (s
->vm_running
) {
1189 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1192 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1198 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1199 temp_sw
= temp_sw
->entries
.le_next
) {
1200 nb_active
+= temp_sw
->active
!= 0;
1203 if (nb_active
== 1) {
1205 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1213 static int audio_get_avail (SWVoiceIn
*sw
)
1221 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1222 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1223 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1228 "%s: get_avail live %d ret %" PRId64
"\n",
1230 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1233 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1236 static int audio_get_free (SWVoiceOut
*sw
)
1244 live
= sw
->total_hw_samples_mixed
;
1246 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1247 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1251 dead
= sw
->hw
->samples
- live
;
1254 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1256 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1259 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1262 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1269 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1270 SWVoiceOut
*sw
= &sc
->sw
;
1275 int till_end_of_hw
= hw
->samples
- rpos2
;
1276 int to_write
= audio_MIN (till_end_of_hw
, n
);
1277 int bytes
= to_write
<< hw
->info
.shift
;
1280 sw
->buf
= hw
->mix_buf
+ rpos2
;
1281 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1282 if (written
- bytes
) {
1283 dolog ("Could not mix %d bytes into a capture "
1284 "buffer, mixed %d\n",
1289 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1294 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1295 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1296 mixeng_clear (hw
->mix_buf
, samples
- n
);
1299 static void audio_run_out (AudioState
*s
)
1301 HWVoiceOut
*hw
= NULL
;
1304 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1306 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1308 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1313 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1314 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1318 if (hw
->pending_disable
&& !nb_live
) {
1321 dolog ("Disabling voice\n");
1324 hw
->pending_disable
= 0;
1325 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1326 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1328 audio_recalc_and_notify_capture (sc
->cap
);
1334 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1336 free
= audio_get_free (sw
);
1338 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1345 prev_rpos
= hw
->rpos
;
1346 played
= hw
->pcm_ops
->run_out (hw
);
1347 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1348 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1349 hw
->rpos
, hw
->samples
, played
);
1354 dolog ("played=%d\n", played
);
1358 hw
->ts_helper
+= played
;
1359 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1362 cleanup_required
= 0;
1363 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1364 if (!sw
->active
&& sw
->empty
) {
1368 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1369 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1370 played
, sw
->total_hw_samples_mixed
);
1371 played
= sw
->total_hw_samples_mixed
;
1374 sw
->total_hw_samples_mixed
-= played
;
1376 if (!sw
->total_hw_samples_mixed
) {
1378 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1382 free
= audio_get_free (sw
);
1384 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1389 if (cleanup_required
) {
1392 sw
= hw
->sw_head
.lh_first
;
1394 sw1
= sw
->entries
.le_next
;
1395 if (!sw
->active
&& !sw
->callback
.fn
) {
1397 dolog ("Finishing with old voice\n");
1399 audio_close_out (sw
);
1407 static void audio_run_in (AudioState
*s
)
1409 HWVoiceIn
*hw
= NULL
;
1411 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1415 captured
= hw
->pcm_ops
->run_in (hw
);
1417 min
= audio_pcm_hw_find_min_in (hw
);
1418 hw
->total_samples_captured
+= captured
- min
;
1419 hw
->ts_helper
+= captured
;
1421 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1422 sw
->total_hw_samples_acquired
-= min
;
1427 avail
= audio_get_avail (sw
);
1429 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1436 static void audio_run_capture (AudioState
*s
)
1438 CaptureVoiceOut
*cap
;
1440 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1441 int live
, rpos
, captured
;
1442 HWVoiceOut
*hw
= &cap
->hw
;
1445 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1448 int left
= hw
->samples
- rpos
;
1449 int to_capture
= audio_MIN (live
, left
);
1450 struct st_sample
*src
;
1451 struct capture_callback
*cb
;
1453 src
= hw
->mix_buf
+ rpos
;
1454 hw
->clip (cap
->buf
, src
, to_capture
);
1455 mixeng_clear (src
, to_capture
);
1457 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1458 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1459 to_capture
<< hw
->info
.shift
);
1461 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1466 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1467 if (!sw
->active
&& sw
->empty
) {
1471 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1472 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1473 captured
, sw
->total_hw_samples_mixed
);
1474 captured
= sw
->total_hw_samples_mixed
;
1477 sw
->total_hw_samples_mixed
-= captured
;
1478 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1483 static void audio_timer (void *opaque
)
1485 AudioState
*s
= opaque
;
1489 audio_run_capture (s
);
1491 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1494 static struct audio_option audio_options
[] = {
1496 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1497 "Use fixed settings for host DAC", NULL
, 0},
1499 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1500 "Frequency for fixed host DAC", NULL
, 0},
1502 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1503 "Format for fixed host DAC", NULL
, 0},
1505 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1506 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1508 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1509 "Number of voices for DAC", NULL
, 0},
1512 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1513 "Use fixed settings for host ADC", NULL
, 0},
1515 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1516 "Frequency for fixed host ADC", NULL
, 0},
1518 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1519 "Format for fixed host ADC", NULL
, 0},
1521 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1522 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1524 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1525 "Number of voices for ADC", NULL
, 0},
1528 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hertz
,
1529 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1531 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1532 "(undocumented)", NULL
, 0},
1534 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1535 "print logging messages to monitor instead of stderr", NULL
, 0},
1537 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1540 static void audio_pp_nb_voices (const char *typ
, int nb
)
1544 printf ("Does not support %s\n", typ
);
1547 printf ("One %s voice\n", typ
);
1550 printf ("Theoretically supports many %s voices\n", typ
);
1553 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1559 void AUD_help (void)
1563 audio_process_options ("AUDIO", audio_options
);
1564 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1565 struct audio_driver
*d
= drvtab
[i
];
1567 audio_process_options (d
->name
, d
->options
);
1571 printf ("Audio options:\n");
1572 audio_print_options ("AUDIO", audio_options
);
1575 printf ("Available drivers:\n");
1577 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1578 struct audio_driver
*d
= drvtab
[i
];
1580 printf ("Name: %s\n", d
->name
);
1581 printf ("Description: %s\n", d
->descr
);
1583 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1584 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1587 printf ("Options:\n");
1588 audio_print_options (d
->name
, d
->options
);
1591 printf ("No options\n");
1597 "Options are settable through environment variables.\n"
1600 " set QEMU_AUDIO_DRV=wav\n"
1601 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1603 " export QEMU_AUDIO_DRV=wav\n"
1604 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1605 "(for csh replace export with setenv in the above)\n"
1611 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1614 audio_process_options (drv
->name
, drv
->options
);
1616 s
->drv_opaque
= drv
->init ();
1618 if (s
->drv_opaque
) {
1619 audio_init_nb_voices_out (drv
);
1620 audio_init_nb_voices_in (drv
);
1625 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1630 static void audio_vm_change_state_handler (void *opaque
, int running
,
1633 AudioState
*s
= opaque
;
1634 HWVoiceOut
*hwo
= NULL
;
1635 HWVoiceIn
*hwi
= NULL
;
1636 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1638 s
->vm_running
= running
;
1639 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1640 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1643 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1644 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1648 static void audio_atexit (void)
1650 AudioState
*s
= &glob_audio_state
;
1651 HWVoiceOut
*hwo
= NULL
;
1652 HWVoiceIn
*hwi
= NULL
;
1654 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1657 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1658 hwo
->pcm_ops
->fini_out (hwo
);
1660 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1661 CaptureVoiceOut
*cap
= sc
->cap
;
1662 struct capture_callback
*cb
;
1664 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1665 cb
->ops
.destroy (cb
->opaque
);
1670 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1671 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1672 hwi
->pcm_ops
->fini_in (hwi
);
1676 s
->drv
->fini (s
->drv_opaque
);
1680 static void audio_save (QEMUFile
*f
, void *opaque
)
1686 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1691 if (version_id
!= 1) {
1698 static void audio_init (void)
1702 const char *drvname
;
1703 AudioState
*s
= &glob_audio_state
;
1709 LIST_INIT (&s
->hw_head_out
);
1710 LIST_INIT (&s
->hw_head_in
);
1711 LIST_INIT (&s
->cap_head
);
1712 atexit (audio_atexit
);
1714 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1716 hw_error("Could not create audio timer\n");
1719 audio_process_options ("AUDIO", audio_options
);
1721 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1722 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1724 if (s
->nb_hw_voices_out
<= 0) {
1725 dolog ("Bogus number of playback voices %d, setting to 1\n",
1726 s
->nb_hw_voices_out
);
1727 s
->nb_hw_voices_out
= 1;
1730 if (s
->nb_hw_voices_in
<= 0) {
1731 dolog ("Bogus number of capture voices %d, setting to 0\n",
1732 s
->nb_hw_voices_in
);
1733 s
->nb_hw_voices_in
= 0;
1738 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1744 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1745 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1746 done
= !audio_driver_init (s
, drvtab
[i
]);
1753 dolog ("Unknown audio driver `%s'\n", drvname
);
1754 dolog ("Run with -audio-help to list available drivers\n");
1759 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1760 if (drvtab
[i
]->can_be_default
) {
1761 done
= !audio_driver_init (s
, drvtab
[i
]);
1767 done
= !audio_driver_init (s
, &no_audio_driver
);
1769 hw_error("Could not initialize audio subsystem\n");
1772 dolog ("warning: Using timer based audio emulation\n");
1776 VMChangeStateEntry
*e
;
1778 if (conf
.period
.hertz
<= 0) {
1779 if (conf
.period
.hertz
< 0) {
1780 dolog ("warning: Timer period is negative - %d "
1781 "treating as zero\n",
1784 conf
.period
.ticks
= 1;
1786 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hertz
;
1789 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1791 dolog ("warning: Could not register change state handler\n"
1792 "(Audio can continue looping even after stopping the VM)\n");
1795 LIST_INIT (&s
->card_head
);
1796 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1797 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1800 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1803 card
->name
= qemu_strdup (name
);
1804 memset (&card
->entries
, 0, sizeof (card
->entries
));
1805 LIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1808 void AUD_remove_card (QEMUSoundCard
*card
)
1810 LIST_REMOVE (card
, entries
);
1811 qemu_free (card
->name
);
1815 CaptureVoiceOut
*AUD_add_capture (
1816 struct audsettings
*as
,
1817 struct audio_capture_ops
*ops
,
1821 AudioState
*s
= &glob_audio_state
;
1822 CaptureVoiceOut
*cap
;
1823 struct capture_callback
*cb
;
1825 if (audio_validate_settings (as
)) {
1826 dolog ("Invalid settings were passed when trying to add capture\n");
1827 audio_print_settings (as
);
1831 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1833 dolog ("Could not allocate capture callback information, size %zu\n",
1838 cb
->opaque
= cb_opaque
;
1840 cap
= audio_pcm_capture_find_specific (as
);
1842 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1847 CaptureVoiceOut
*cap
;
1849 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1851 dolog ("Could not allocate capture voice, size %zu\n",
1857 LIST_INIT (&hw
->sw_head
);
1858 LIST_INIT (&cap
->cb_head
);
1860 /* XXX find a more elegant way */
1861 hw
->samples
= 4096 * 4;
1862 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1863 sizeof (struct st_sample
));
1865 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1870 audio_pcm_init_info (&hw
->info
, as
);
1872 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1874 dolog ("Could not allocate capture buffer "
1875 "(%d samples, each %d bytes)\n",
1876 hw
->samples
, 1 << hw
->info
.shift
);
1880 hw
->clip
= mixeng_clip
1881 [hw
->info
.nchannels
== 2]
1883 [hw
->info
.swap_endianness
]
1884 [audio_bits_to_index (hw
->info
.bits
)];
1886 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1887 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1890 while ((hw
= audio_pcm_hw_find_any_out (hw
))) {
1891 audio_attach_capture (hw
);
1896 qemu_free (cap
->hw
.mix_buf
);
1906 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1908 struct capture_callback
*cb
;
1910 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1911 if (cb
->opaque
== cb_opaque
) {
1912 cb
->ops
.destroy (cb_opaque
);
1913 LIST_REMOVE (cb
, entries
);
1916 if (!cap
->cb_head
.lh_first
) {
1917 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1920 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1921 #ifdef DEBUG_CAPTURE
1922 dolog ("freeing %s\n", sw
->name
);
1925 sw1
= sw
->entries
.le_next
;
1927 st_rate_stop (sw
->rate
);
1930 LIST_REMOVE (sw
, entries
);
1931 LIST_REMOVE (sc
, entries
);
1935 LIST_REMOVE (cap
, entries
);
1943 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1946 sw
->vol
.mute
= mute
;
1947 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1948 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1952 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1955 sw
->vol
.mute
= mute
;
1956 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1957 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;