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
26 #include "monitor/monitor.h"
27 #include "qemu/timer.h"
28 #include "sysemu/sysemu.h"
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
33 /* #define DEBUG_LIVE */
34 /* #define DEBUG_OUT */
35 /* #define DEBUG_CAPTURE */
36 /* #define DEBUG_POLL */
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
[] = {
54 struct fixed_settings
{
58 struct audsettings settings
;
62 struct fixed_settings fixed_out
;
63 struct fixed_settings fixed_in
;
71 .fixed_out
= { /* DAC fixed settings */
79 .endianness
= AUDIO_HOST_ENDIANNESS
,
83 .fixed_in
= { /* ADC fixed settings */
91 .endianness
= AUDIO_HOST_ENDIANNESS
,
95 .period
= { .hertz
= 100 },
100 static AudioState glob_audio_state
;
102 const struct mixeng_volume nominal_volume
= {
113 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
116 static void audio_print_options (const char *prefix
,
117 struct audio_option
*opt
);
119 int audio_bug (const char *funcname
, int cond
)
124 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
126 struct audio_driver
*d
;
129 AUD_log (NULL
, "Save all your work and restart without audio\n");
130 AUD_log (NULL
, "Please send bug report to av1474@comtv.ru\n");
131 AUD_log (NULL
, "I am sorry\n");
132 d
= glob_audio_state
.drv
;
134 audio_print_options (d
->name
, d
->options
);
137 AUD_log (NULL
, "Context:\n");
139 #if defined AUDIO_BREAKPOINT_ON_BUG
140 # if defined HOST_I386
141 # if defined __GNUC__
143 # elif defined _MSC_VER
158 static inline int audio_bits_to_index (int bits
)
171 audio_bug ("bits_to_index", 1);
172 AUD_log (NULL
, "invalid bits %d\n", bits
);
177 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
183 cond
= !nmemb
|| !size
;
187 if (audio_bug ("audio_calloc", cond
)) {
188 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
190 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
194 return g_malloc0 (len
);
197 static char *audio_alloc_prefix (const char *s
)
199 const char qemu_prefix
[] = "QEMU_";
208 r
= g_malloc (len
+ sizeof (qemu_prefix
));
210 u
= r
+ sizeof (qemu_prefix
) - 1;
212 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
213 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
215 for (i
= 0; i
< len
; ++i
) {
216 u
[i
] = qemu_toupper(u
[i
]);
222 static const char *audio_audfmt_to_string (audfmt_e fmt
)
244 dolog ("Bogus audfmt %d returning S16\n", fmt
);
248 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
251 if (!strcasecmp (s
, "u8")) {
255 else if (!strcasecmp (s
, "u16")) {
259 else if (!strcasecmp (s
, "u32")) {
263 else if (!strcasecmp (s
, "s8")) {
267 else if (!strcasecmp (s
, "s16")) {
271 else if (!strcasecmp (s
, "s32")) {
276 dolog ("Bogus audio format `%s' using %s\n",
277 s
, audio_audfmt_to_string (defval
));
283 static audfmt_e
audio_get_conf_fmt (const char *envname
,
287 const char *var
= getenv (envname
);
292 return audio_string_to_audfmt (var
, defval
, defaultp
);
295 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
300 strval
= getenv (key
);
312 static const char *audio_get_conf_str (const char *key
,
316 const char *val
= getenv (key
);
327 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
330 fprintf(stderr
, "%s: ", cap
);
333 vfprintf(stderr
, fmt
, ap
);
336 void AUD_log (const char *cap
, const char *fmt
, ...)
341 AUD_vlog (cap
, fmt
, ap
);
345 static void audio_print_options (const char *prefix
,
346 struct audio_option
*opt
)
351 dolog ("No prefix specified\n");
356 dolog ("No options\n");
360 uprefix
= audio_alloc_prefix (prefix
);
362 for (; opt
->name
; opt
++) {
363 const char *state
= "default";
364 printf (" %s_%s: ", uprefix
, opt
->name
);
366 if (opt
->overriddenp
&& *opt
->overriddenp
) {
373 int *intp
= opt
->valp
;
374 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
380 int *intp
= opt
->valp
;
381 printf ("integer, %s = %d\n", state
, *intp
);
387 audfmt_e
*fmtp
= opt
->valp
;
389 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
391 audio_audfmt_to_string (*fmtp
)
398 const char **strp
= opt
->valp
;
399 printf ("string, %s = %s\n",
401 *strp
? *strp
: "(not set)");
407 dolog ("Bad value tag for option %s_%s %d\n",
408 uprefix
, opt
->name
, opt
->tag
);
411 printf (" %s\n", opt
->descr
);
417 static void audio_process_options (const char *prefix
,
418 struct audio_option
*opt
)
421 const char qemu_prefix
[] = "QEMU_";
422 size_t preflen
, optlen
;
424 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
425 dolog ("prefix = NULL\n");
429 if (audio_bug (AUDIO_FUNC
, !opt
)) {
430 dolog ("opt = NULL\n");
434 preflen
= strlen (prefix
);
436 for (; opt
->name
; opt
++) {
441 dolog ("Option value pointer for `%s' is not set\n",
446 len
= strlen (opt
->name
);
447 /* len of opt->name + len of prefix + size of qemu_prefix
448 * (includes trailing zero) + zero + underscore (on behalf of
450 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
451 optname
= g_malloc (optlen
);
453 pstrcpy (optname
, optlen
, qemu_prefix
);
455 /* copy while upper-casing, including trailing zero */
456 for (i
= 0; i
<= preflen
; ++i
) {
457 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
459 pstrcat (optname
, optlen
, "_");
460 pstrcat (optname
, optlen
, opt
->name
);
467 int *intp
= opt
->valp
;
468 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
474 audfmt_e
*fmtp
= opt
->valp
;
475 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
481 const char **strp
= opt
->valp
;
482 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
487 dolog ("Bad value tag for option `%s' - %d\n",
492 if (!opt
->overriddenp
) {
493 opt
->overriddenp
= &opt
->overridden
;
495 *opt
->overriddenp
= !def
;
500 static void audio_print_settings (struct audsettings
*as
)
502 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
506 AUD_log (NULL
, "S8");
509 AUD_log (NULL
, "U8");
512 AUD_log (NULL
, "S16");
515 AUD_log (NULL
, "U16");
518 AUD_log (NULL
, "S32");
521 AUD_log (NULL
, "U32");
524 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
528 AUD_log (NULL
, " endianness=");
529 switch (as
->endianness
) {
531 AUD_log (NULL
, "little");
534 AUD_log (NULL
, "big");
537 AUD_log (NULL
, "invalid");
540 AUD_log (NULL
, "\n");
543 static int audio_validate_settings (struct audsettings
*as
)
547 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
548 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
563 invalid
|= as
->freq
<= 0;
564 return invalid
? -1 : 0;
567 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
569 int bits
= 8, sign
= 0;
592 return info
->freq
== as
->freq
593 && info
->nchannels
== as
->nchannels
594 && info
->sign
== sign
595 && info
->bits
== bits
596 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
599 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
601 int bits
= 8, sign
= 0, shift
= 0;
624 info
->freq
= as
->freq
;
627 info
->nchannels
= as
->nchannels
;
628 info
->shift
= (as
->nchannels
== 2) + shift
;
629 info
->align
= (1 << info
->shift
) - 1;
630 info
->bytes_per_second
= info
->freq
<< info
->shift
;
631 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
634 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
641 memset (buf
, 0x00, len
<< info
->shift
);
644 switch (info
->bits
) {
646 memset (buf
, 0x80, len
<< info
->shift
);
653 int shift
= info
->nchannels
- 1;
656 if (info
->swap_endianness
) {
660 for (i
= 0; i
< len
<< shift
; i
++) {
670 int shift
= info
->nchannels
- 1;
671 int32_t s
= INT32_MAX
;
673 if (info
->swap_endianness
) {
677 for (i
= 0; i
< len
<< shift
; i
++) {
684 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
694 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
701 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
702 struct audsettings
*as
705 CaptureVoiceOut
*cap
;
706 AudioState
*s
= &glob_audio_state
;
708 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
709 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
716 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
718 struct capture_callback
*cb
;
721 dolog ("notification %d sent\n", cmd
);
723 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
724 cb
->ops
.notify (cb
->opaque
, cmd
);
728 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
730 if (cap
->hw
.enabled
!= enabled
) {
731 audcnotification_e cmd
;
732 cap
->hw
.enabled
= enabled
;
733 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
734 audio_notify_capture (cap
, cmd
);
738 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
740 HWVoiceOut
*hw
= &cap
->hw
;
744 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
750 audio_capture_maybe_changed (cap
, enabled
);
753 static void audio_detach_capture (HWVoiceOut
*hw
)
755 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
758 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
759 SWVoiceOut
*sw
= &sc
->sw
;
760 CaptureVoiceOut
*cap
= sc
->cap
;
761 int was_active
= sw
->active
;
764 st_rate_stop (sw
->rate
);
768 QLIST_REMOVE (sw
, entries
);
769 QLIST_REMOVE (sc
, entries
);
772 /* We have removed soft voice from the capture:
773 this might have changed the overall status of the capture
774 since this might have been the only active voice */
775 audio_recalc_and_notify_capture (cap
);
781 static int audio_attach_capture (HWVoiceOut
*hw
)
783 AudioState
*s
= &glob_audio_state
;
784 CaptureVoiceOut
*cap
;
786 audio_detach_capture (hw
);
787 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
790 HWVoiceOut
*hw_cap
= &cap
->hw
;
792 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
794 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
804 sw
->active
= hw
->enabled
;
805 sw
->conv
= noop_conv
;
806 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
807 sw
->vol
= nominal_volume
;
808 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
810 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
814 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
815 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
817 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
818 hw
, sw
->info
.freq
, sw
->info
.bits
,
820 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
823 audio_capture_maybe_changed (cap
, 1);
830 * Hard voice (capture)
832 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
835 int m
= hw
->total_samples_captured
;
837 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
839 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
845 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
847 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
848 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
849 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
855 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
856 int live
, int pending
)
858 int left
= hw
->samples
- pending
;
859 int len
= audio_MIN (left
, live
);
863 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
864 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
865 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
866 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
868 hw
->clip (dst
, src
, samples_to_clip
);
870 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
871 len
-= samples_to_clip
;
872 clipped
+= samples_to_clip
;
878 * Soft voice (capture)
880 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
882 HWVoiceIn
*hw
= sw
->hw
;
883 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
886 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
887 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
891 rpos
= hw
->wpos
- live
;
896 return hw
->samples
+ rpos
;
900 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
902 HWVoiceIn
*hw
= sw
->hw
;
903 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
904 struct st_sample
*src
, *dst
= sw
->buf
;
906 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
908 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
909 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
910 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
914 samples
= size
>> sw
->info
.shift
;
919 swlim
= (live
* sw
->ratio
) >> 32;
920 swlim
= audio_MIN (swlim
, samples
);
923 src
= hw
->conv_buf
+ rpos
;
924 isamp
= hw
->wpos
- rpos
;
927 isamp
= hw
->samples
- rpos
;
935 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
936 dolog ("osamp=%d\n", osamp
);
940 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
942 rpos
= (rpos
+ isamp
) % hw
->samples
;
948 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
949 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
952 sw
->clip (buf
, sw
->buf
, ret
);
953 sw
->total_hw_samples_acquired
+= total
;
954 return ret
<< sw
->info
.shift
;
958 * Hard voice (playback)
960 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
966 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
967 if (sw
->active
|| !sw
->empty
) {
968 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
977 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
982 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
990 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
991 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1000 * Soft voice (playback)
1002 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1004 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1005 int ret
= 0, pos
= 0, total
= 0;
1011 hwsamples
= sw
->hw
->samples
;
1013 live
= sw
->total_hw_samples_mixed
;
1014 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1015 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1019 if (live
== hwsamples
) {
1021 dolog ("%s is full %d\n", sw
->name
, live
);
1026 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1027 samples
= size
>> sw
->info
.shift
;
1029 dead
= hwsamples
- live
;
1030 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1031 swlim
= audio_MIN (swlim
, samples
);
1033 sw
->conv (sw
->buf
, buf
, swlim
);
1035 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
1036 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
1041 dead
= hwsamples
- live
;
1042 left
= hwsamples
- wpos
;
1043 blck
= audio_MIN (dead
, left
);
1052 sw
->hw
->mix_buf
+ wpos
,
1060 wpos
= (wpos
+ osamp
) % hwsamples
;
1064 sw
->total_hw_samples_mixed
+= total
;
1065 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1069 "%s: write size %d ret %d total sw %d\n",
1071 size
>> sw
->info
.shift
,
1073 sw
->total_hw_samples_mixed
1077 return ret
<< sw
->info
.shift
;
1081 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1083 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1084 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1089 #include "audio_template.h"
1091 #include "audio_template.h"
1096 static int audio_is_timer_needed (void)
1098 HWVoiceIn
*hwi
= NULL
;
1099 HWVoiceOut
*hwo
= NULL
;
1101 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1102 if (!hwo
->poll_mode
) return 1;
1104 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1105 if (!hwi
->poll_mode
) return 1;
1110 static void audio_reset_timer (AudioState
*s
)
1112 if (audio_is_timer_needed ()) {
1114 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + conf
.period
.ticks
);
1121 static void audio_timer (void *opaque
)
1123 audio_run ("timer");
1124 audio_reset_timer (opaque
);
1130 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1135 /* XXX: Consider options */
1139 if (!sw
->hw
->enabled
) {
1140 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1144 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1148 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1153 /* XXX: Consider options */
1157 if (!sw
->hw
->enabled
) {
1158 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1162 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1166 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1168 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1171 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1180 if (sw
->active
!= on
) {
1181 AudioState
*s
= &glob_audio_state
;
1182 SWVoiceOut
*temp_sw
;
1186 hw
->pending_disable
= 0;
1189 if (s
->vm_running
) {
1190 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
, conf
.try_poll_out
);
1191 audio_reset_timer (s
);
1199 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1200 temp_sw
= temp_sw
->entries
.le_next
) {
1201 nb_active
+= temp_sw
->active
!= 0;
1204 hw
->pending_disable
= nb_active
== 1;
1208 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1209 sc
->sw
.active
= hw
->enabled
;
1211 audio_capture_maybe_changed (sc
->cap
, 1);
1218 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1227 if (sw
->active
!= on
) {
1228 AudioState
*s
= &glob_audio_state
;
1234 if (s
->vm_running
) {
1235 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
, conf
.try_poll_in
);
1236 audio_reset_timer (s
);
1239 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1245 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1246 temp_sw
= temp_sw
->entries
.le_next
) {
1247 nb_active
+= temp_sw
->active
!= 0;
1250 if (nb_active
== 1) {
1252 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1260 static int audio_get_avail (SWVoiceIn
*sw
)
1268 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1269 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1270 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1275 "%s: get_avail live %d ret %" PRId64
"\n",
1277 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1280 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1283 static int audio_get_free (SWVoiceOut
*sw
)
1291 live
= sw
->total_hw_samples_mixed
;
1293 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1294 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1298 dead
= sw
->hw
->samples
- live
;
1301 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1303 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1306 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1309 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1316 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1317 SWVoiceOut
*sw
= &sc
->sw
;
1322 int till_end_of_hw
= hw
->samples
- rpos2
;
1323 int to_write
= audio_MIN (till_end_of_hw
, n
);
1324 int bytes
= to_write
<< hw
->info
.shift
;
1327 sw
->buf
= hw
->mix_buf
+ rpos2
;
1328 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1329 if (written
- bytes
) {
1330 dolog ("Could not mix %d bytes into a capture "
1331 "buffer, mixed %d\n",
1336 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1341 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1342 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1343 mixeng_clear (hw
->mix_buf
, samples
- n
);
1346 static void audio_run_out (AudioState
*s
)
1348 HWVoiceOut
*hw
= NULL
;
1351 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1353 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1355 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1360 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1361 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1365 if (hw
->pending_disable
&& !nb_live
) {
1368 dolog ("Disabling voice\n");
1371 hw
->pending_disable
= 0;
1372 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1373 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1375 audio_recalc_and_notify_capture (sc
->cap
);
1381 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1383 free
= audio_get_free (sw
);
1385 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1392 prev_rpos
= hw
->rpos
;
1393 played
= hw
->pcm_ops
->run_out (hw
, live
);
1394 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1395 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1396 hw
->rpos
, hw
->samples
, played
);
1401 dolog ("played=%d\n", played
);
1405 hw
->ts_helper
+= played
;
1406 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1409 cleanup_required
= 0;
1410 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1411 if (!sw
->active
&& sw
->empty
) {
1415 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1416 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1417 played
, sw
->total_hw_samples_mixed
);
1418 played
= sw
->total_hw_samples_mixed
;
1421 sw
->total_hw_samples_mixed
-= played
;
1423 if (!sw
->total_hw_samples_mixed
) {
1425 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1429 free
= audio_get_free (sw
);
1431 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1436 if (cleanup_required
) {
1439 sw
= hw
->sw_head
.lh_first
;
1441 sw1
= sw
->entries
.le_next
;
1442 if (!sw
->active
&& !sw
->callback
.fn
) {
1443 audio_close_out (sw
);
1451 static void audio_run_in (AudioState
*s
)
1453 HWVoiceIn
*hw
= NULL
;
1455 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1459 captured
= hw
->pcm_ops
->run_in (hw
);
1461 min
= audio_pcm_hw_find_min_in (hw
);
1462 hw
->total_samples_captured
+= captured
- min
;
1463 hw
->ts_helper
+= captured
;
1465 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1466 sw
->total_hw_samples_acquired
-= min
;
1471 avail
= audio_get_avail (sw
);
1473 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1480 static void audio_run_capture (AudioState
*s
)
1482 CaptureVoiceOut
*cap
;
1484 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1485 int live
, rpos
, captured
;
1486 HWVoiceOut
*hw
= &cap
->hw
;
1489 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1492 int left
= hw
->samples
- rpos
;
1493 int to_capture
= audio_MIN (live
, left
);
1494 struct st_sample
*src
;
1495 struct capture_callback
*cb
;
1497 src
= hw
->mix_buf
+ rpos
;
1498 hw
->clip (cap
->buf
, src
, to_capture
);
1499 mixeng_clear (src
, to_capture
);
1501 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1502 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1503 to_capture
<< hw
->info
.shift
);
1505 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1510 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1511 if (!sw
->active
&& sw
->empty
) {
1515 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1516 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1517 captured
, sw
->total_hw_samples_mixed
);
1518 captured
= sw
->total_hw_samples_mixed
;
1521 sw
->total_hw_samples_mixed
-= captured
;
1522 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1527 void audio_run (const char *msg
)
1529 AudioState
*s
= &glob_audio_state
;
1533 audio_run_capture (s
);
1536 static double prevtime
;
1540 if (gettimeofday (&tv
, NULL
)) {
1541 perror ("audio_run: gettimeofday");
1545 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1546 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1547 prevtime
= currtime
;
1552 static struct audio_option audio_options
[] = {
1555 .name
= "DAC_FIXED_SETTINGS",
1556 .tag
= AUD_OPT_BOOL
,
1557 .valp
= &conf
.fixed_out
.enabled
,
1558 .descr
= "Use fixed settings for host DAC"
1561 .name
= "DAC_FIXED_FREQ",
1563 .valp
= &conf
.fixed_out
.settings
.freq
,
1564 .descr
= "Frequency for fixed host DAC"
1567 .name
= "DAC_FIXED_FMT",
1569 .valp
= &conf
.fixed_out
.settings
.fmt
,
1570 .descr
= "Format for fixed host DAC"
1573 .name
= "DAC_FIXED_CHANNELS",
1575 .valp
= &conf
.fixed_out
.settings
.nchannels
,
1576 .descr
= "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1579 .name
= "DAC_VOICES",
1581 .valp
= &conf
.fixed_out
.nb_voices
,
1582 .descr
= "Number of voices for DAC"
1585 .name
= "DAC_TRY_POLL",
1586 .tag
= AUD_OPT_BOOL
,
1587 .valp
= &conf
.try_poll_out
,
1588 .descr
= "Attempt using poll mode for DAC"
1592 .name
= "ADC_FIXED_SETTINGS",
1593 .tag
= AUD_OPT_BOOL
,
1594 .valp
= &conf
.fixed_in
.enabled
,
1595 .descr
= "Use fixed settings for host ADC"
1598 .name
= "ADC_FIXED_FREQ",
1600 .valp
= &conf
.fixed_in
.settings
.freq
,
1601 .descr
= "Frequency for fixed host ADC"
1604 .name
= "ADC_FIXED_FMT",
1606 .valp
= &conf
.fixed_in
.settings
.fmt
,
1607 .descr
= "Format for fixed host ADC"
1610 .name
= "ADC_FIXED_CHANNELS",
1612 .valp
= &conf
.fixed_in
.settings
.nchannels
,
1613 .descr
= "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1616 .name
= "ADC_VOICES",
1618 .valp
= &conf
.fixed_in
.nb_voices
,
1619 .descr
= "Number of voices for ADC"
1622 .name
= "ADC_TRY_POLL",
1623 .tag
= AUD_OPT_BOOL
,
1624 .valp
= &conf
.try_poll_in
,
1625 .descr
= "Attempt using poll mode for ADC"
1629 .name
= "TIMER_PERIOD",
1631 .valp
= &conf
.period
.hertz
,
1632 .descr
= "Timer period in HZ (0 - use lowest possible)"
1634 { /* End of list */ }
1637 static void audio_pp_nb_voices (const char *typ
, int nb
)
1641 printf ("Does not support %s\n", typ
);
1644 printf ("One %s voice\n", typ
);
1647 printf ("Theoretically supports many %s voices\n", typ
);
1650 printf ("Theoretically supports up to %d %s voices\n", nb
, typ
);
1656 void AUD_help (void)
1660 audio_process_options ("AUDIO", audio_options
);
1661 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1662 struct audio_driver
*d
= drvtab
[i
];
1664 audio_process_options (d
->name
, d
->options
);
1668 printf ("Audio options:\n");
1669 audio_print_options ("AUDIO", audio_options
);
1672 printf ("Available drivers:\n");
1674 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1675 struct audio_driver
*d
= drvtab
[i
];
1677 printf ("Name: %s\n", d
->name
);
1678 printf ("Description: %s\n", d
->descr
);
1680 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1681 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1684 printf ("Options:\n");
1685 audio_print_options (d
->name
, d
->options
);
1688 printf ("No options\n");
1694 "Options are settable through environment variables.\n"
1697 " set QEMU_AUDIO_DRV=wav\n"
1698 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1700 " export QEMU_AUDIO_DRV=wav\n"
1701 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1702 "(for csh replace export with setenv in the above)\n"
1708 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1711 audio_process_options (drv
->name
, drv
->options
);
1713 s
->drv_opaque
= drv
->init ();
1715 if (s
->drv_opaque
) {
1716 audio_init_nb_voices_out (drv
);
1717 audio_init_nb_voices_in (drv
);
1722 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1727 static void audio_vm_change_state_handler (void *opaque
, int running
,
1730 AudioState
*s
= opaque
;
1731 HWVoiceOut
*hwo
= NULL
;
1732 HWVoiceIn
*hwi
= NULL
;
1733 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1735 s
->vm_running
= running
;
1736 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1737 hwo
->pcm_ops
->ctl_out (hwo
, op
, conf
.try_poll_out
);
1740 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1741 hwi
->pcm_ops
->ctl_in (hwi
, op
, conf
.try_poll_in
);
1743 audio_reset_timer (s
);
1746 static void audio_atexit (void)
1748 AudioState
*s
= &glob_audio_state
;
1749 HWVoiceOut
*hwo
= NULL
;
1750 HWVoiceIn
*hwi
= NULL
;
1752 while ((hwo
= audio_pcm_hw_find_any_out (hwo
))) {
1756 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1758 hwo
->pcm_ops
->fini_out (hwo
);
1760 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1761 CaptureVoiceOut
*cap
= sc
->cap
;
1762 struct capture_callback
*cb
;
1764 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1765 cb
->ops
.destroy (cb
->opaque
);
1770 while ((hwi
= audio_pcm_hw_find_any_in (hwi
))) {
1772 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1774 hwi
->pcm_ops
->fini_in (hwi
);
1778 s
->drv
->fini (s
->drv_opaque
);
1782 static const VMStateDescription vmstate_audio
= {
1785 .minimum_version_id
= 1,
1786 .fields
= (VMStateField
[]) {
1787 VMSTATE_END_OF_LIST()
1791 static void audio_init (void)
1795 const char *drvname
;
1796 VMChangeStateEntry
*e
;
1797 AudioState
*s
= &glob_audio_state
;
1803 QLIST_INIT (&s
->hw_head_out
);
1804 QLIST_INIT (&s
->hw_head_in
);
1805 QLIST_INIT (&s
->cap_head
);
1806 atexit (audio_atexit
);
1808 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1810 hw_error("Could not create audio timer\n");
1813 audio_process_options ("AUDIO", audio_options
);
1815 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1816 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1818 if (s
->nb_hw_voices_out
<= 0) {
1819 dolog ("Bogus number of playback voices %d, setting to 1\n",
1820 s
->nb_hw_voices_out
);
1821 s
->nb_hw_voices_out
= 1;
1824 if (s
->nb_hw_voices_in
<= 0) {
1825 dolog ("Bogus number of capture voices %d, setting to 0\n",
1826 s
->nb_hw_voices_in
);
1827 s
->nb_hw_voices_in
= 0;
1832 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1838 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1839 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1840 done
= !audio_driver_init (s
, drvtab
[i
]);
1847 dolog ("Unknown audio driver `%s'\n", drvname
);
1848 dolog ("Run with -audio-help to list available drivers\n");
1853 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1854 if (drvtab
[i
]->can_be_default
) {
1855 done
= !audio_driver_init (s
, drvtab
[i
]);
1861 done
= !audio_driver_init (s
, &no_audio_driver
);
1863 hw_error("Could not initialize audio subsystem\n");
1866 dolog ("warning: Using timer based audio emulation\n");
1870 if (conf
.period
.hertz
<= 0) {
1871 if (conf
.period
.hertz
< 0) {
1872 dolog ("warning: Timer period is negative - %d "
1873 "treating as zero\n",
1876 conf
.period
.ticks
= 1;
1879 muldiv64 (1, get_ticks_per_sec (), conf
.period
.hertz
);
1882 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1884 dolog ("warning: Could not register change state handler\n"
1885 "(Audio can continue looping even after stopping the VM)\n");
1888 QLIST_INIT (&s
->card_head
);
1889 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1892 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1895 card
->name
= g_strdup (name
);
1896 memset (&card
->entries
, 0, sizeof (card
->entries
));
1897 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1900 void AUD_remove_card (QEMUSoundCard
*card
)
1902 QLIST_REMOVE (card
, entries
);
1903 g_free (card
->name
);
1907 CaptureVoiceOut
*AUD_add_capture (
1908 struct audsettings
*as
,
1909 struct audio_capture_ops
*ops
,
1913 AudioState
*s
= &glob_audio_state
;
1914 CaptureVoiceOut
*cap
;
1915 struct capture_callback
*cb
;
1917 if (audio_validate_settings (as
)) {
1918 dolog ("Invalid settings were passed when trying to add capture\n");
1919 audio_print_settings (as
);
1923 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1925 dolog ("Could not allocate capture callback information, size %zu\n",
1930 cb
->opaque
= cb_opaque
;
1932 cap
= audio_pcm_capture_find_specific (as
);
1934 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1939 CaptureVoiceOut
*cap
;
1941 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1943 dolog ("Could not allocate capture voice, size %zu\n",
1949 QLIST_INIT (&hw
->sw_head
);
1950 QLIST_INIT (&cap
->cb_head
);
1952 /* XXX find a more elegant way */
1953 hw
->samples
= 4096 * 4;
1954 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1955 sizeof (struct st_sample
));
1957 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1962 audio_pcm_init_info (&hw
->info
, as
);
1964 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1966 dolog ("Could not allocate capture buffer "
1967 "(%d samples, each %d bytes)\n",
1968 hw
->samples
, 1 << hw
->info
.shift
);
1972 hw
->clip
= mixeng_clip
1973 [hw
->info
.nchannels
== 2]
1975 [hw
->info
.swap_endianness
]
1976 [audio_bits_to_index (hw
->info
.bits
)];
1978 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1979 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1982 while ((hw
= audio_pcm_hw_find_any_out (hw
))) {
1983 audio_attach_capture (hw
);
1988 g_free (cap
->hw
.mix_buf
);
1998 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
2000 struct capture_callback
*cb
;
2002 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
2003 if (cb
->opaque
== cb_opaque
) {
2004 cb
->ops
.destroy (cb_opaque
);
2005 QLIST_REMOVE (cb
, entries
);
2008 if (!cap
->cb_head
.lh_first
) {
2009 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
2012 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
2013 #ifdef DEBUG_CAPTURE
2014 dolog ("freeing %s\n", sw
->name
);
2017 sw1
= sw
->entries
.le_next
;
2019 st_rate_stop (sw
->rate
);
2022 QLIST_REMOVE (sw
, entries
);
2023 QLIST_REMOVE (sc
, entries
);
2027 QLIST_REMOVE (cap
, entries
);
2035 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2038 HWVoiceOut
*hw
= sw
->hw
;
2040 sw
->vol
.mute
= mute
;
2041 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2042 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2044 if (hw
->pcm_ops
->ctl_out
) {
2045 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
2050 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2053 HWVoiceIn
*hw
= sw
->hw
;
2055 sw
->vol
.mute
= mute
;
2056 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2057 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2059 if (hw
->pcm_ops
->ctl_in
) {
2060 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);