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 */
37 /* #define DEBUG_POLL */
39 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42 /* Order of CONFIG_AUDIO_DRIVERS is import.
43 The 1st one is the one used by default, that is the reason
44 that we generate the list.
46 static struct audio_driver
*drvtab
[] = {
55 struct fixed_settings
{
59 struct audsettings settings
;
63 struct fixed_settings fixed_out
;
64 struct fixed_settings fixed_in
;
74 .fixed_out
= { /* DAC fixed settings */
82 .endianness
= AUDIO_HOST_ENDIANNESS
,
86 .fixed_in
= { /* ADC fixed settings */
94 .endianness
= AUDIO_HOST_ENDIANNESS
,
98 .period
= { .hertz
= 250 },
105 static AudioState glob_audio_state
;
107 const struct mixeng_volume nominal_volume
= {
118 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
121 static void audio_print_options (const char *prefix
,
122 struct audio_option
*opt
);
124 int audio_bug (const char *funcname
, int cond
)
129 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
131 struct audio_driver
*d
;
134 AUD_log (NULL
, "Save all your work and restart without audio\n");
135 AUD_log (NULL
, "Please send bug report to av1474@comtv.ru\n");
136 AUD_log (NULL
, "I am sorry\n");
137 d
= glob_audio_state
.drv
;
139 audio_print_options (d
->name
, d
->options
);
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 g_malloc0 (len
);
202 static char *audio_alloc_prefix (const char *s
)
204 const char qemu_prefix
[] = "QEMU_";
213 r
= g_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(default_mon
, "%s: ", cap
);
339 monitor_vprintf(default_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
= g_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;
606 return info
->freq
== as
->freq
607 && info
->nchannels
== as
->nchannels
608 && info
->sign
== sign
609 && info
->bits
== bits
610 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
613 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
615 int bits
= 8, sign
= 0, shift
= 0;
638 info
->freq
= as
->freq
;
641 info
->nchannels
= as
->nchannels
;
642 info
->shift
= (as
->nchannels
== 2) + shift
;
643 info
->align
= (1 << info
->shift
) - 1;
644 info
->bytes_per_second
= info
->freq
<< info
->shift
;
645 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
648 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
655 memset (buf
, 0x00, len
<< info
->shift
);
658 switch (info
->bits
) {
660 memset (buf
, 0x80, len
<< info
->shift
);
667 int shift
= info
->nchannels
- 1;
670 if (info
->swap_endianness
) {
674 for (i
= 0; i
< len
<< shift
; i
++) {
684 int shift
= info
->nchannels
- 1;
685 int32_t s
= INT32_MAX
;
687 if (info
->swap_endianness
) {
691 for (i
= 0; i
< len
<< shift
; i
++) {
698 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
708 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
715 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
716 struct audsettings
*as
719 CaptureVoiceOut
*cap
;
720 AudioState
*s
= &glob_audio_state
;
722 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
723 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
730 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
732 struct capture_callback
*cb
;
735 dolog ("notification %d sent\n", cmd
);
737 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
738 cb
->ops
.notify (cb
->opaque
, cmd
);
742 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
744 if (cap
->hw
.enabled
!= enabled
) {
745 audcnotification_e cmd
;
746 cap
->hw
.enabled
= enabled
;
747 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
748 audio_notify_capture (cap
, cmd
);
752 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
754 HWVoiceOut
*hw
= &cap
->hw
;
758 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
764 audio_capture_maybe_changed (cap
, enabled
);
767 static void audio_detach_capture (HWVoiceOut
*hw
)
769 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
772 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
773 SWVoiceOut
*sw
= &sc
->sw
;
774 CaptureVoiceOut
*cap
= sc
->cap
;
775 int was_active
= sw
->active
;
778 st_rate_stop (sw
->rate
);
782 QLIST_REMOVE (sw
, entries
);
783 QLIST_REMOVE (sc
, entries
);
786 /* We have removed soft voice from the capture:
787 this might have changed the overall status of the capture
788 since this might have been the only active voice */
789 audio_recalc_and_notify_capture (cap
);
795 static int audio_attach_capture (HWVoiceOut
*hw
)
797 AudioState
*s
= &glob_audio_state
;
798 CaptureVoiceOut
*cap
;
800 audio_detach_capture (hw
);
801 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
804 HWVoiceOut
*hw_cap
= &cap
->hw
;
806 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
808 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
818 sw
->active
= hw
->enabled
;
819 sw
->conv
= noop_conv
;
820 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
821 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
823 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
827 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
828 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
830 asprintf (&sw
->name
, "for %p %d,%d,%d",
831 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
832 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
835 audio_capture_maybe_changed (cap
, 1);
842 * Hard voice (capture)
844 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
847 int m
= hw
->total_samples_captured
;
849 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
851 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
857 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
859 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
860 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
861 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
867 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
868 int live
, int pending
)
870 int left
= hw
->samples
- pending
;
871 int len
= audio_MIN (left
, live
);
875 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
876 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
877 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
878 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
880 hw
->clip (dst
, src
, samples_to_clip
);
882 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
883 len
-= samples_to_clip
;
884 clipped
+= samples_to_clip
;
890 * Soft voice (capture)
892 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
894 HWVoiceIn
*hw
= sw
->hw
;
895 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
898 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
899 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
903 rpos
= hw
->wpos
- live
;
908 return hw
->samples
+ rpos
;
912 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
914 HWVoiceIn
*hw
= sw
->hw
;
915 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
916 struct st_sample
*src
, *dst
= sw
->buf
;
918 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
920 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
921 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
922 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
926 samples
= size
>> sw
->info
.shift
;
931 swlim
= (live
* sw
->ratio
) >> 32;
932 swlim
= audio_MIN (swlim
, samples
);
935 src
= hw
->conv_buf
+ rpos
;
936 isamp
= hw
->wpos
- rpos
;
939 isamp
= hw
->samples
- rpos
;
947 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
948 dolog ("osamp=%d\n", osamp
);
952 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
954 rpos
= (rpos
+ isamp
) % hw
->samples
;
960 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
961 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
964 sw
->clip (buf
, sw
->buf
, ret
);
965 sw
->total_hw_samples_acquired
+= total
;
966 return ret
<< sw
->info
.shift
;
970 * Hard voice (playback)
972 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
978 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
979 if (sw
->active
|| !sw
->empty
) {
980 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
989 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
994 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
1002 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1003 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1012 * Soft voice (playback)
1014 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1016 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1017 int ret
= 0, pos
= 0, total
= 0;
1023 hwsamples
= sw
->hw
->samples
;
1025 live
= sw
->total_hw_samples_mixed
;
1026 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1027 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1031 if (live
== hwsamples
) {
1033 dolog ("%s is full %d\n", sw
->name
, live
);
1038 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1039 samples
= size
>> sw
->info
.shift
;
1041 dead
= hwsamples
- live
;
1042 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1043 swlim
= audio_MIN (swlim
, samples
);
1045 sw
->conv (sw
->buf
, buf
, swlim
);
1047 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
1048 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
1053 dead
= hwsamples
- live
;
1054 left
= hwsamples
- wpos
;
1055 blck
= audio_MIN (dead
, left
);
1064 sw
->hw
->mix_buf
+ wpos
,
1072 wpos
= (wpos
+ osamp
) % hwsamples
;
1076 sw
->total_hw_samples_mixed
+= total
;
1077 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1081 "%s: write size %d ret %d total sw %d\n",
1083 size
>> sw
->info
.shift
,
1085 sw
->total_hw_samples_mixed
1089 return ret
<< sw
->info
.shift
;
1093 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1095 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1096 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1101 #include "audio_template.h"
1103 #include "audio_template.h"
1108 static int audio_is_timer_needed (void)
1110 HWVoiceIn
*hwi
= NULL
;
1111 HWVoiceOut
*hwo
= NULL
;
1113 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1114 if (!hwo
->poll_mode
) return 1;
1116 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1117 if (!hwi
->poll_mode
) return 1;
1122 static void audio_reset_timer (AudioState
*s
)
1124 if (audio_is_timer_needed ()) {
1125 qemu_mod_timer (s
->ts
, qemu_get_clock_ns (vm_clock
) + 1);
1128 qemu_del_timer (s
->ts
);
1132 static void audio_timer (void *opaque
)
1134 audio_run ("timer");
1135 audio_reset_timer (opaque
);
1141 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1146 /* XXX: Consider options */
1150 if (!sw
->hw
->enabled
) {
1151 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1155 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1159 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1164 /* XXX: Consider options */
1168 if (!sw
->hw
->enabled
) {
1169 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1173 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1177 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1179 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1182 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1191 if (sw
->active
!= on
) {
1192 AudioState
*s
= &glob_audio_state
;
1193 SWVoiceOut
*temp_sw
;
1197 hw
->pending_disable
= 0;
1200 if (s
->vm_running
) {
1201 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
, conf
.try_poll_out
);
1202 audio_reset_timer (s
);
1210 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1211 temp_sw
= temp_sw
->entries
.le_next
) {
1212 nb_active
+= temp_sw
->active
!= 0;
1215 hw
->pending_disable
= nb_active
== 1;
1219 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1220 sc
->sw
.active
= hw
->enabled
;
1222 audio_capture_maybe_changed (sc
->cap
, 1);
1229 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1238 if (sw
->active
!= on
) {
1239 AudioState
*s
= &glob_audio_state
;
1245 if (s
->vm_running
) {
1246 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
, conf
.try_poll_in
);
1247 audio_reset_timer (s
);
1250 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1256 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1257 temp_sw
= temp_sw
->entries
.le_next
) {
1258 nb_active
+= temp_sw
->active
!= 0;
1261 if (nb_active
== 1) {
1263 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1271 static int audio_get_avail (SWVoiceIn
*sw
)
1279 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1280 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1281 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1286 "%s: get_avail live %d ret %" PRId64
"\n",
1288 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1291 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1294 static int audio_get_free (SWVoiceOut
*sw
)
1302 live
= sw
->total_hw_samples_mixed
;
1304 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1305 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1309 dead
= sw
->hw
->samples
- live
;
1312 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1314 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1317 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1320 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1327 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1328 SWVoiceOut
*sw
= &sc
->sw
;
1333 int till_end_of_hw
= hw
->samples
- rpos2
;
1334 int to_write
= audio_MIN (till_end_of_hw
, n
);
1335 int bytes
= to_write
<< hw
->info
.shift
;
1338 sw
->buf
= hw
->mix_buf
+ rpos2
;
1339 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1340 if (written
- bytes
) {
1341 dolog ("Could not mix %d bytes into a capture "
1342 "buffer, mixed %d\n",
1347 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1352 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1353 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1354 mixeng_clear (hw
->mix_buf
, samples
- n
);
1357 static void audio_run_out (AudioState
*s
)
1359 HWVoiceOut
*hw
= NULL
;
1362 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1364 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1366 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1371 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1372 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1376 if (hw
->pending_disable
&& !nb_live
) {
1379 dolog ("Disabling voice\n");
1382 hw
->pending_disable
= 0;
1383 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1384 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1386 audio_recalc_and_notify_capture (sc
->cap
);
1392 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1394 free
= audio_get_free (sw
);
1396 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1403 prev_rpos
= hw
->rpos
;
1404 played
= hw
->pcm_ops
->run_out (hw
, live
);
1405 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1406 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1407 hw
->rpos
, hw
->samples
, played
);
1412 dolog ("played=%d\n", played
);
1416 hw
->ts_helper
+= played
;
1417 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1420 cleanup_required
= 0;
1421 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1422 if (!sw
->active
&& sw
->empty
) {
1426 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1427 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1428 played
, sw
->total_hw_samples_mixed
);
1429 played
= sw
->total_hw_samples_mixed
;
1432 sw
->total_hw_samples_mixed
-= played
;
1434 if (!sw
->total_hw_samples_mixed
) {
1436 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1440 free
= audio_get_free (sw
);
1442 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1447 if (cleanup_required
) {
1450 sw
= hw
->sw_head
.lh_first
;
1452 sw1
= sw
->entries
.le_next
;
1453 if (!sw
->active
&& !sw
->callback
.fn
) {
1455 dolog ("Finishing with old voice\n");
1457 audio_close_out (sw
);
1465 static void audio_run_in (AudioState
*s
)
1467 HWVoiceIn
*hw
= NULL
;
1469 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1473 captured
= hw
->pcm_ops
->run_in (hw
);
1475 min
= audio_pcm_hw_find_min_in (hw
);
1476 hw
->total_samples_captured
+= captured
- min
;
1477 hw
->ts_helper
+= captured
;
1479 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1480 sw
->total_hw_samples_acquired
-= min
;
1485 avail
= audio_get_avail (sw
);
1487 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1494 static void audio_run_capture (AudioState
*s
)
1496 CaptureVoiceOut
*cap
;
1498 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1499 int live
, rpos
, captured
;
1500 HWVoiceOut
*hw
= &cap
->hw
;
1503 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1506 int left
= hw
->samples
- rpos
;
1507 int to_capture
= audio_MIN (live
, left
);
1508 struct st_sample
*src
;
1509 struct capture_callback
*cb
;
1511 src
= hw
->mix_buf
+ rpos
;
1512 hw
->clip (cap
->buf
, src
, to_capture
);
1513 mixeng_clear (src
, to_capture
);
1515 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1516 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1517 to_capture
<< hw
->info
.shift
);
1519 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1524 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1525 if (!sw
->active
&& sw
->empty
) {
1529 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1530 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1531 captured
, sw
->total_hw_samples_mixed
);
1532 captured
= sw
->total_hw_samples_mixed
;
1535 sw
->total_hw_samples_mixed
-= captured
;
1536 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1541 void audio_run (const char *msg
)
1543 AudioState
*s
= &glob_audio_state
;
1547 audio_run_capture (s
);
1550 static double prevtime
;
1554 if (gettimeofday (&tv
, NULL
)) {
1555 perror ("audio_run: gettimeofday");
1559 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1560 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1561 prevtime
= currtime
;
1566 static struct audio_option audio_options
[] = {
1569 .name
= "DAC_FIXED_SETTINGS",
1570 .tag
= AUD_OPT_BOOL
,
1571 .valp
= &conf
.fixed_out
.enabled
,
1572 .descr
= "Use fixed settings for host DAC"
1575 .name
= "DAC_FIXED_FREQ",
1577 .valp
= &conf
.fixed_out
.settings
.freq
,
1578 .descr
= "Frequency for fixed host DAC"
1581 .name
= "DAC_FIXED_FMT",
1583 .valp
= &conf
.fixed_out
.settings
.fmt
,
1584 .descr
= "Format for fixed host DAC"
1587 .name
= "DAC_FIXED_CHANNELS",
1589 .valp
= &conf
.fixed_out
.settings
.nchannels
,
1590 .descr
= "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1593 .name
= "DAC_VOICES",
1595 .valp
= &conf
.fixed_out
.nb_voices
,
1596 .descr
= "Number of voices for DAC"
1599 .name
= "DAC_TRY_POLL",
1600 .tag
= AUD_OPT_BOOL
,
1601 .valp
= &conf
.try_poll_out
,
1602 .descr
= "Attempt using poll mode for DAC"
1606 .name
= "ADC_FIXED_SETTINGS",
1607 .tag
= AUD_OPT_BOOL
,
1608 .valp
= &conf
.fixed_in
.enabled
,
1609 .descr
= "Use fixed settings for host ADC"
1612 .name
= "ADC_FIXED_FREQ",
1614 .valp
= &conf
.fixed_in
.settings
.freq
,
1615 .descr
= "Frequency for fixed host ADC"
1618 .name
= "ADC_FIXED_FMT",
1620 .valp
= &conf
.fixed_in
.settings
.fmt
,
1621 .descr
= "Format for fixed host ADC"
1624 .name
= "ADC_FIXED_CHANNELS",
1626 .valp
= &conf
.fixed_in
.settings
.nchannels
,
1627 .descr
= "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1630 .name
= "ADC_VOICES",
1632 .valp
= &conf
.fixed_in
.nb_voices
,
1633 .descr
= "Number of voices for ADC"
1636 .name
= "ADC_TRY_POLL",
1637 .tag
= AUD_OPT_BOOL
,
1638 .valp
= &conf
.try_poll_in
,
1639 .descr
= "Attempt using poll mode for ADC"
1643 .name
= "TIMER_PERIOD",
1645 .valp
= &conf
.period
.hertz
,
1646 .descr
= "Timer period in HZ (0 - use lowest possible)"
1650 .tag
= AUD_OPT_BOOL
,
1651 .valp
= &conf
.plive
,
1652 .descr
= "(undocumented)"
1655 .name
= "LOG_TO_MONITOR",
1656 .tag
= AUD_OPT_BOOL
,
1657 .valp
= &conf
.log_to_monitor
,
1658 .descr
= "Print logging messages to monitor instead of stderr"
1660 { /* End of list */ }
1663 static void audio_pp_nb_voices (const char *typ
, int nb
)
1667 printf ("Does not support %s\n", typ
);
1670 printf ("One %s voice\n", typ
);
1673 printf ("Theoretically supports many %s voices\n", typ
);
1676 printf ("Theoretically supports up to %d %s voices\n", nb
, typ
);
1682 void AUD_help (void)
1686 audio_process_options ("AUDIO", audio_options
);
1687 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1688 struct audio_driver
*d
= drvtab
[i
];
1690 audio_process_options (d
->name
, d
->options
);
1694 printf ("Audio options:\n");
1695 audio_print_options ("AUDIO", audio_options
);
1698 printf ("Available drivers:\n");
1700 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1701 struct audio_driver
*d
= drvtab
[i
];
1703 printf ("Name: %s\n", d
->name
);
1704 printf ("Description: %s\n", d
->descr
);
1706 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1707 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1710 printf ("Options:\n");
1711 audio_print_options (d
->name
, d
->options
);
1714 printf ("No options\n");
1720 "Options are settable through environment variables.\n"
1723 " set QEMU_AUDIO_DRV=wav\n"
1724 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1726 " export QEMU_AUDIO_DRV=wav\n"
1727 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1728 "(for csh replace export with setenv in the above)\n"
1734 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1737 audio_process_options (drv
->name
, drv
->options
);
1739 s
->drv_opaque
= drv
->init ();
1741 if (s
->drv_opaque
) {
1742 audio_init_nb_voices_out (drv
);
1743 audio_init_nb_voices_in (drv
);
1748 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1753 static void audio_vm_change_state_handler (void *opaque
, int running
,
1756 AudioState
*s
= opaque
;
1757 HWVoiceOut
*hwo
= NULL
;
1758 HWVoiceIn
*hwi
= NULL
;
1759 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1761 s
->vm_running
= running
;
1762 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1763 hwo
->pcm_ops
->ctl_out (hwo
, op
, conf
.try_poll_out
);
1766 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1767 hwi
->pcm_ops
->ctl_in (hwi
, op
, conf
.try_poll_in
);
1769 audio_reset_timer (s
);
1772 static void audio_atexit (void)
1774 AudioState
*s
= &glob_audio_state
;
1775 HWVoiceOut
*hwo
= NULL
;
1776 HWVoiceIn
*hwi
= NULL
;
1778 while ((hwo
= audio_pcm_hw_find_any_out (hwo
))) {
1782 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1784 hwo
->pcm_ops
->fini_out (hwo
);
1786 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1787 CaptureVoiceOut
*cap
= sc
->cap
;
1788 struct capture_callback
*cb
;
1790 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1791 cb
->ops
.destroy (cb
->opaque
);
1796 while ((hwi
= audio_pcm_hw_find_any_in (hwi
))) {
1798 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1800 hwi
->pcm_ops
->fini_in (hwi
);
1804 s
->drv
->fini (s
->drv_opaque
);
1808 static const VMStateDescription vmstate_audio
= {
1811 .minimum_version_id
= 1,
1812 .minimum_version_id_old
= 1,
1813 .fields
= (VMStateField
[]) {
1814 VMSTATE_END_OF_LIST()
1818 static void audio_init (void)
1822 const char *drvname
;
1823 VMChangeStateEntry
*e
;
1824 AudioState
*s
= &glob_audio_state
;
1830 QLIST_INIT (&s
->hw_head_out
);
1831 QLIST_INIT (&s
->hw_head_in
);
1832 QLIST_INIT (&s
->cap_head
);
1833 atexit (audio_atexit
);
1835 s
->ts
= qemu_new_timer_ns (vm_clock
, audio_timer
, s
);
1837 hw_error("Could not create audio timer\n");
1840 audio_process_options ("AUDIO", audio_options
);
1842 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1843 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1845 if (s
->nb_hw_voices_out
<= 0) {
1846 dolog ("Bogus number of playback voices %d, setting to 1\n",
1847 s
->nb_hw_voices_out
);
1848 s
->nb_hw_voices_out
= 1;
1851 if (s
->nb_hw_voices_in
<= 0) {
1852 dolog ("Bogus number of capture voices %d, setting to 0\n",
1853 s
->nb_hw_voices_in
);
1854 s
->nb_hw_voices_in
= 0;
1859 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1865 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1866 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1867 done
= !audio_driver_init (s
, drvtab
[i
]);
1874 dolog ("Unknown audio driver `%s'\n", drvname
);
1875 dolog ("Run with -audio-help to list available drivers\n");
1880 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1881 if (drvtab
[i
]->can_be_default
) {
1882 done
= !audio_driver_init (s
, drvtab
[i
]);
1888 done
= !audio_driver_init (s
, &no_audio_driver
);
1890 hw_error("Could not initialize audio subsystem\n");
1893 dolog ("warning: Using timer based audio emulation\n");
1897 if (conf
.period
.hertz
<= 0) {
1898 if (conf
.period
.hertz
< 0) {
1899 dolog ("warning: Timer period is negative - %d "
1900 "treating as zero\n",
1903 conf
.period
.ticks
= 1;
1906 muldiv64 (1, get_ticks_per_sec (), conf
.period
.hertz
);
1909 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1911 dolog ("warning: Could not register change state handler\n"
1912 "(Audio can continue looping even after stopping the VM)\n");
1915 QLIST_INIT (&s
->card_head
);
1916 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1919 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1922 card
->name
= g_strdup (name
);
1923 memset (&card
->entries
, 0, sizeof (card
->entries
));
1924 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1927 void AUD_remove_card (QEMUSoundCard
*card
)
1929 QLIST_REMOVE (card
, entries
);
1930 g_free (card
->name
);
1934 CaptureVoiceOut
*AUD_add_capture (
1935 struct audsettings
*as
,
1936 struct audio_capture_ops
*ops
,
1940 AudioState
*s
= &glob_audio_state
;
1941 CaptureVoiceOut
*cap
;
1942 struct capture_callback
*cb
;
1944 if (audio_validate_settings (as
)) {
1945 dolog ("Invalid settings were passed when trying to add capture\n");
1946 audio_print_settings (as
);
1950 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1952 dolog ("Could not allocate capture callback information, size %zu\n",
1957 cb
->opaque
= cb_opaque
;
1959 cap
= audio_pcm_capture_find_specific (as
);
1961 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1966 CaptureVoiceOut
*cap
;
1968 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1970 dolog ("Could not allocate capture voice, size %zu\n",
1976 QLIST_INIT (&hw
->sw_head
);
1977 QLIST_INIT (&cap
->cb_head
);
1979 /* XXX find a more elegant way */
1980 hw
->samples
= 4096 * 4;
1981 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1982 sizeof (struct st_sample
));
1984 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1989 audio_pcm_init_info (&hw
->info
, as
);
1991 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1993 dolog ("Could not allocate capture buffer "
1994 "(%d samples, each %d bytes)\n",
1995 hw
->samples
, 1 << hw
->info
.shift
);
1999 hw
->clip
= mixeng_clip
2000 [hw
->info
.nchannels
== 2]
2002 [hw
->info
.swap_endianness
]
2003 [audio_bits_to_index (hw
->info
.bits
)];
2005 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
2006 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
2009 while ((hw
= audio_pcm_hw_find_any_out (hw
))) {
2010 audio_attach_capture (hw
);
2015 g_free (cap
->hw
.mix_buf
);
2025 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
2027 struct capture_callback
*cb
;
2029 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
2030 if (cb
->opaque
== cb_opaque
) {
2031 cb
->ops
.destroy (cb_opaque
);
2032 QLIST_REMOVE (cb
, entries
);
2035 if (!cap
->cb_head
.lh_first
) {
2036 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
2039 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
2040 #ifdef DEBUG_CAPTURE
2041 dolog ("freeing %s\n", sw
->name
);
2044 sw1
= sw
->entries
.le_next
;
2046 st_rate_stop (sw
->rate
);
2049 QLIST_REMOVE (sw
, entries
);
2050 QLIST_REMOVE (sc
, entries
);
2054 QLIST_REMOVE (cap
, entries
);
2062 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2065 HWVoiceOut
*hw
= sw
->hw
;
2067 sw
->vol
.mute
= mute
;
2068 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2069 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2071 if (hw
->pcm_ops
->ctl_out
) {
2072 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
2077 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2080 HWVoiceIn
*hw
= sw
->hw
;
2082 sw
->vol
.mute
= mute
;
2083 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2084 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2086 if (hw
->pcm_ops
->ctl_in
) {
2087 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);