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
[] = {
52 struct fixed_settings
{
56 struct audsettings settings
;
60 struct fixed_settings fixed_out
;
61 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
= 250 },
102 static AudioState glob_audio_state
;
104 struct mixeng_volume nominal_volume
= {
115 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
118 static void audio_print_options (const char *prefix
,
119 struct audio_option
*opt
);
121 int audio_bug (const char *funcname
, int cond
)
126 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
128 struct audio_driver
*d
;
131 AUD_log (NULL
, "Save all your work and restart without audio\n");
132 AUD_log (NULL
, "Please send bug report to av1474@comtv.ru\n");
133 AUD_log (NULL
, "I am sorry\n");
134 d
= glob_audio_state
.drv
;
136 audio_print_options (d
->name
, d
->options
);
139 AUD_log (NULL
, "Context:\n");
141 #if defined AUDIO_BREAKPOINT_ON_BUG
142 # if defined HOST_I386
143 # if defined __GNUC__
145 # elif defined _MSC_VER
160 static inline int audio_bits_to_index (int bits
)
173 audio_bug ("bits_to_index", 1);
174 AUD_log (NULL
, "invalid bits %d\n", bits
);
179 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
185 cond
= !nmemb
|| !size
;
189 if (audio_bug ("audio_calloc", cond
)) {
190 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
192 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
196 return qemu_mallocz (len
);
199 static char *audio_alloc_prefix (const char *s
)
201 const char qemu_prefix
[] = "QEMU_";
210 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
212 u
= r
+ sizeof (qemu_prefix
) - 1;
214 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
215 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
217 for (i
= 0; i
< len
; ++i
) {
218 u
[i
] = qemu_toupper(u
[i
]);
224 static const char *audio_audfmt_to_string (audfmt_e fmt
)
246 dolog ("Bogus audfmt %d returning S16\n", fmt
);
250 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
253 if (!strcasecmp (s
, "u8")) {
257 else if (!strcasecmp (s
, "u16")) {
261 else if (!strcasecmp (s
, "u32")) {
265 else if (!strcasecmp (s
, "s8")) {
269 else if (!strcasecmp (s
, "s16")) {
273 else if (!strcasecmp (s
, "s32")) {
278 dolog ("Bogus audio format `%s' using %s\n",
279 s
, audio_audfmt_to_string (defval
));
285 static audfmt_e
audio_get_conf_fmt (const char *envname
,
289 const char *var
= getenv (envname
);
294 return audio_string_to_audfmt (var
, defval
, defaultp
);
297 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
302 strval
= getenv (key
);
314 static const char *audio_get_conf_str (const char *key
,
318 const char *val
= getenv (key
);
329 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
331 if (conf
.log_to_monitor
) {
333 monitor_printf(default_mon
, "%s: ", cap
);
336 monitor_vprintf(default_mon
, fmt
, ap
);
340 fprintf (stderr
, "%s: ", cap
);
343 vfprintf (stderr
, fmt
, ap
);
347 void AUD_log (const char *cap
, const char *fmt
, ...)
352 AUD_vlog (cap
, fmt
, ap
);
356 static void audio_print_options (const char *prefix
,
357 struct audio_option
*opt
)
362 dolog ("No prefix specified\n");
367 dolog ("No options\n");
371 uprefix
= audio_alloc_prefix (prefix
);
373 for (; opt
->name
; opt
++) {
374 const char *state
= "default";
375 printf (" %s_%s: ", uprefix
, opt
->name
);
377 if (opt
->overriddenp
&& *opt
->overriddenp
) {
384 int *intp
= opt
->valp
;
385 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
391 int *intp
= opt
->valp
;
392 printf ("integer, %s = %d\n", state
, *intp
);
398 audfmt_e
*fmtp
= opt
->valp
;
400 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
402 audio_audfmt_to_string (*fmtp
)
409 const char **strp
= opt
->valp
;
410 printf ("string, %s = %s\n",
412 *strp
? *strp
: "(not set)");
418 dolog ("Bad value tag for option %s_%s %d\n",
419 uprefix
, opt
->name
, opt
->tag
);
422 printf (" %s\n", opt
->descr
);
428 static void audio_process_options (const char *prefix
,
429 struct audio_option
*opt
)
432 const char qemu_prefix
[] = "QEMU_";
433 size_t preflen
, optlen
;
435 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
436 dolog ("prefix = NULL\n");
440 if (audio_bug (AUDIO_FUNC
, !opt
)) {
441 dolog ("opt = NULL\n");
445 preflen
= strlen (prefix
);
447 for (; opt
->name
; opt
++) {
452 dolog ("Option value pointer for `%s' is not set\n",
457 len
= strlen (opt
->name
);
458 /* len of opt->name + len of prefix + size of qemu_prefix
459 * (includes trailing zero) + zero + underscore (on behalf of
461 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
462 optname
= qemu_malloc (optlen
);
464 pstrcpy (optname
, optlen
, qemu_prefix
);
466 /* copy while upper-casing, including trailing zero */
467 for (i
= 0; i
<= preflen
; ++i
) {
468 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
470 pstrcat (optname
, optlen
, "_");
471 pstrcat (optname
, optlen
, opt
->name
);
478 int *intp
= opt
->valp
;
479 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
485 audfmt_e
*fmtp
= opt
->valp
;
486 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
492 const char **strp
= opt
->valp
;
493 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
498 dolog ("Bad value tag for option `%s' - %d\n",
503 if (!opt
->overriddenp
) {
504 opt
->overriddenp
= &opt
->overridden
;
506 *opt
->overriddenp
= !def
;
511 static void audio_print_settings (struct audsettings
*as
)
513 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
517 AUD_log (NULL
, "S8");
520 AUD_log (NULL
, "U8");
523 AUD_log (NULL
, "S16");
526 AUD_log (NULL
, "U16");
529 AUD_log (NULL
, "S32");
532 AUD_log (NULL
, "U32");
535 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
539 AUD_log (NULL
, " endianness=");
540 switch (as
->endianness
) {
542 AUD_log (NULL
, "little");
545 AUD_log (NULL
, "big");
548 AUD_log (NULL
, "invalid");
551 AUD_log (NULL
, "\n");
554 static int audio_validate_settings (struct audsettings
*as
)
558 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
559 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
574 invalid
|= as
->freq
<= 0;
575 return invalid
? -1 : 0;
578 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
580 int bits
= 8, sign
= 0;
600 return info
->freq
== as
->freq
601 && info
->nchannels
== as
->nchannels
602 && info
->sign
== sign
603 && info
->bits
== bits
604 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
607 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
609 int bits
= 8, sign
= 0, shift
= 0;
632 info
->freq
= as
->freq
;
635 info
->nchannels
= as
->nchannels
;
636 info
->shift
= (as
->nchannels
== 2) + shift
;
637 info
->align
= (1 << info
->shift
) - 1;
638 info
->bytes_per_second
= info
->freq
<< info
->shift
;
639 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
642 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
649 memset (buf
, 0x00, len
<< info
->shift
);
652 switch (info
->bits
) {
654 memset (buf
, 0x80, len
<< info
->shift
);
661 int shift
= info
->nchannels
- 1;
664 if (info
->swap_endianness
) {
668 for (i
= 0; i
< len
<< shift
; i
++) {
678 int shift
= info
->nchannels
- 1;
679 int32_t s
= INT32_MAX
;
681 if (info
->swap_endianness
) {
685 for (i
= 0; i
< len
<< shift
; i
++) {
692 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
702 static void noop_conv (struct st_sample
*dst
, const void *src
,
703 int samples
, struct mixeng_volume
*vol
)
711 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
712 struct audsettings
*as
715 CaptureVoiceOut
*cap
;
716 AudioState
*s
= &glob_audio_state
;
718 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
719 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
726 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
728 struct capture_callback
*cb
;
731 dolog ("notification %d sent\n", cmd
);
733 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
734 cb
->ops
.notify (cb
->opaque
, cmd
);
738 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
740 if (cap
->hw
.enabled
!= enabled
) {
741 audcnotification_e cmd
;
742 cap
->hw
.enabled
= enabled
;
743 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
744 audio_notify_capture (cap
, cmd
);
748 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
750 HWVoiceOut
*hw
= &cap
->hw
;
754 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
760 audio_capture_maybe_changed (cap
, enabled
);
763 static void audio_detach_capture (HWVoiceOut
*hw
)
765 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
768 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
769 SWVoiceOut
*sw
= &sc
->sw
;
770 CaptureVoiceOut
*cap
= sc
->cap
;
771 int was_active
= sw
->active
;
774 st_rate_stop (sw
->rate
);
778 QLIST_REMOVE (sw
, entries
);
779 QLIST_REMOVE (sc
, entries
);
782 /* We have removed soft voice from the capture:
783 this might have changed the overall status of the capture
784 since this might have been the only active voice */
785 audio_recalc_and_notify_capture (cap
);
791 static int audio_attach_capture (HWVoiceOut
*hw
)
793 AudioState
*s
= &glob_audio_state
;
794 CaptureVoiceOut
*cap
;
796 audio_detach_capture (hw
);
797 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
800 HWVoiceOut
*hw_cap
= &cap
->hw
;
802 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
804 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
814 sw
->active
= hw
->enabled
;
815 sw
->conv
= noop_conv
;
816 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
817 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
819 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
823 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
824 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
826 asprintf (&sw
->name
, "for %p %d,%d,%d",
827 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
828 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
831 audio_capture_maybe_changed (cap
, 1);
838 * Hard voice (capture)
840 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
843 int m
= hw
->total_samples_captured
;
845 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
847 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
853 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
855 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
856 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
857 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
863 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
864 int live
, int pending
)
866 int left
= hw
->samples
- pending
;
867 int len
= audio_MIN (left
, live
);
871 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
872 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
873 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
874 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
876 hw
->clip (dst
, src
, samples_to_clip
);
878 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
879 len
-= samples_to_clip
;
880 clipped
+= samples_to_clip
;
886 * Soft voice (capture)
888 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
890 HWVoiceIn
*hw
= sw
->hw
;
891 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
894 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
895 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
899 rpos
= hw
->wpos
- live
;
904 return hw
->samples
+ rpos
;
908 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
910 HWVoiceIn
*hw
= sw
->hw
;
911 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
912 struct st_sample
*src
, *dst
= sw
->buf
;
914 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
916 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
917 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
918 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
922 samples
= size
>> sw
->info
.shift
;
927 swlim
= (live
* sw
->ratio
) >> 32;
928 swlim
= audio_MIN (swlim
, samples
);
931 src
= hw
->conv_buf
+ rpos
;
932 isamp
= hw
->wpos
- rpos
;
935 isamp
= hw
->samples
- rpos
;
943 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
944 dolog ("osamp=%d\n", osamp
);
948 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
950 rpos
= (rpos
+ isamp
) % hw
->samples
;
956 sw
->clip (buf
, sw
->buf
, ret
);
957 sw
->total_hw_samples_acquired
+= total
;
958 return ret
<< sw
->info
.shift
;
962 * Hard voice (playback)
964 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
970 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
971 if (sw
->active
|| !sw
->empty
) {
972 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
981 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
986 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
994 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
995 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1004 * Soft voice (playback)
1006 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1008 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1009 int ret
= 0, pos
= 0, total
= 0;
1015 hwsamples
= sw
->hw
->samples
;
1017 live
= sw
->total_hw_samples_mixed
;
1018 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1019 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1023 if (live
== hwsamples
) {
1025 dolog ("%s is full %d\n", sw
->name
, live
);
1030 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1031 samples
= size
>> sw
->info
.shift
;
1033 dead
= hwsamples
- live
;
1034 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1035 swlim
= audio_MIN (swlim
, samples
);
1037 sw
->conv (sw
->buf
, 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 void audio_timer (void *opaque
)
1098 AudioState
*s
= opaque
;
1100 audio_run ("timer");
1101 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1105 static int audio_is_timer_needed (void)
1107 HWVoiceIn
*hwi
= NULL
;
1108 HWVoiceOut
*hwo
= NULL
;
1110 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1111 if (!hwo
->poll_mode
) return 1;
1113 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1114 if (!hwi
->poll_mode
) return 1;
1119 static void audio_reset_timer (void)
1121 AudioState
*s
= &glob_audio_state
;
1123 if (audio_is_timer_needed ()) {
1124 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + 1);
1127 qemu_del_timer (s
->ts
);
1134 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1139 /* XXX: Consider options */
1143 if (!sw
->hw
->enabled
) {
1144 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1148 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1152 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1157 /* XXX: Consider options */
1161 if (!sw
->hw
->enabled
) {
1162 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1166 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1170 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1172 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1175 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1184 if (sw
->active
!= on
) {
1185 AudioState
*s
= &glob_audio_state
;
1186 SWVoiceOut
*temp_sw
;
1190 hw
->pending_disable
= 0;
1193 if (s
->vm_running
) {
1194 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
, conf
.try_poll_out
);
1195 audio_reset_timer ();
1203 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1204 temp_sw
= temp_sw
->entries
.le_next
) {
1205 nb_active
+= temp_sw
->active
!= 0;
1208 hw
->pending_disable
= nb_active
== 1;
1212 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1213 sc
->sw
.active
= hw
->enabled
;
1215 audio_capture_maybe_changed (sc
->cap
, 1);
1222 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1231 if (sw
->active
!= on
) {
1232 AudioState
*s
= &glob_audio_state
;
1238 if (s
->vm_running
) {
1239 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
, conf
.try_poll_in
);
1242 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1248 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1249 temp_sw
= temp_sw
->entries
.le_next
) {
1250 nb_active
+= temp_sw
->active
!= 0;
1253 if (nb_active
== 1) {
1255 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1263 static int audio_get_avail (SWVoiceIn
*sw
)
1271 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1272 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1273 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1278 "%s: get_avail live %d ret %" PRId64
"\n",
1280 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1283 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1286 static int audio_get_free (SWVoiceOut
*sw
)
1294 live
= sw
->total_hw_samples_mixed
;
1296 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1297 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1301 dead
= sw
->hw
->samples
- live
;
1304 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1306 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1309 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1312 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1319 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1320 SWVoiceOut
*sw
= &sc
->sw
;
1325 int till_end_of_hw
= hw
->samples
- rpos2
;
1326 int to_write
= audio_MIN (till_end_of_hw
, n
);
1327 int bytes
= to_write
<< hw
->info
.shift
;
1330 sw
->buf
= hw
->mix_buf
+ rpos2
;
1331 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1332 if (written
- bytes
) {
1333 dolog ("Could not mix %d bytes into a capture "
1334 "buffer, mixed %d\n",
1339 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1344 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1345 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1346 mixeng_clear (hw
->mix_buf
, samples
- n
);
1349 static void audio_run_out (AudioState
*s
)
1351 HWVoiceOut
*hw
= NULL
;
1354 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1356 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1358 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1363 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1364 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1368 if (hw
->pending_disable
&& !nb_live
) {
1371 dolog ("Disabling voice\n");
1374 hw
->pending_disable
= 0;
1375 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1376 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1378 audio_recalc_and_notify_capture (sc
->cap
);
1384 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1386 free
= audio_get_free (sw
);
1388 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1395 prev_rpos
= hw
->rpos
;
1396 played
= hw
->pcm_ops
->run_out (hw
, live
);
1397 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1398 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1399 hw
->rpos
, hw
->samples
, played
);
1404 dolog ("played=%d\n", played
);
1408 hw
->ts_helper
+= played
;
1409 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1412 cleanup_required
= 0;
1413 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1414 if (!sw
->active
&& sw
->empty
) {
1418 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1419 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1420 played
, sw
->total_hw_samples_mixed
);
1421 played
= sw
->total_hw_samples_mixed
;
1424 sw
->total_hw_samples_mixed
-= played
;
1426 if (!sw
->total_hw_samples_mixed
) {
1428 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1432 free
= audio_get_free (sw
);
1434 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1439 if (cleanup_required
) {
1442 sw
= hw
->sw_head
.lh_first
;
1444 sw1
= sw
->entries
.le_next
;
1445 if (!sw
->active
&& !sw
->callback
.fn
) {
1447 dolog ("Finishing with old voice\n");
1449 audio_close_out (sw
);
1457 static void audio_run_in (AudioState
*s
)
1459 HWVoiceIn
*hw
= NULL
;
1461 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1465 captured
= hw
->pcm_ops
->run_in (hw
);
1467 min
= audio_pcm_hw_find_min_in (hw
);
1468 hw
->total_samples_captured
+= captured
- min
;
1469 hw
->ts_helper
+= captured
;
1471 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1472 sw
->total_hw_samples_acquired
-= min
;
1477 avail
= audio_get_avail (sw
);
1479 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1486 static void audio_run_capture (AudioState
*s
)
1488 CaptureVoiceOut
*cap
;
1490 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1491 int live
, rpos
, captured
;
1492 HWVoiceOut
*hw
= &cap
->hw
;
1495 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1498 int left
= hw
->samples
- rpos
;
1499 int to_capture
= audio_MIN (live
, left
);
1500 struct st_sample
*src
;
1501 struct capture_callback
*cb
;
1503 src
= hw
->mix_buf
+ rpos
;
1504 hw
->clip (cap
->buf
, src
, to_capture
);
1505 mixeng_clear (src
, to_capture
);
1507 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1508 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1509 to_capture
<< hw
->info
.shift
);
1511 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1516 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1517 if (!sw
->active
&& sw
->empty
) {
1521 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1522 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1523 captured
, sw
->total_hw_samples_mixed
);
1524 captured
= sw
->total_hw_samples_mixed
;
1527 sw
->total_hw_samples_mixed
-= captured
;
1528 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1533 void audio_run (const char *msg
)
1535 AudioState
*s
= &glob_audio_state
;
1539 audio_run_capture (s
);
1542 static double prevtime
;
1546 if (gettimeofday (&tv
, NULL
)) {
1547 perror ("audio_run: gettimeofday");
1551 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1552 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1553 prevtime
= currtime
;
1558 static struct audio_option audio_options
[] = {
1561 .name
= "DAC_FIXED_SETTINGS",
1562 .tag
= AUD_OPT_BOOL
,
1563 .valp
= &conf
.fixed_out
.enabled
,
1564 .descr
= "Use fixed settings for host DAC"
1567 .name
= "DAC_FIXED_FREQ",
1569 .valp
= &conf
.fixed_out
.settings
.freq
,
1570 .descr
= "Frequency for fixed host DAC"
1573 .name
= "DAC_FIXED_FMT",
1575 .valp
= &conf
.fixed_out
.settings
.fmt
,
1576 .descr
= "Format for fixed host DAC"
1579 .name
= "DAC_FIXED_CHANNELS",
1581 .valp
= &conf
.fixed_out
.settings
.nchannels
,
1582 .descr
= "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1585 .name
= "DAC_VOICES",
1587 .valp
= &conf
.fixed_out
.nb_voices
,
1588 .descr
= "Number of voices for DAC"
1591 .name
= "DAC_TRY_POLL",
1592 .tag
= AUD_OPT_BOOL
,
1593 .valp
= &conf
.try_poll_out
,
1594 .descr
= "Attempt using poll mode for DAC"
1598 .name
= "ADC_FIXED_SETTINGS",
1599 .tag
= AUD_OPT_BOOL
,
1600 .valp
= &conf
.fixed_in
.enabled
,
1601 .descr
= "Use fixed settings for host ADC"
1604 .name
= "ADC_FIXED_FREQ",
1606 .valp
= &conf
.fixed_in
.settings
.freq
,
1607 .descr
= "Frequency for fixed host ADC"
1610 .name
= "ADC_FIXED_FMT",
1612 .valp
= &conf
.fixed_in
.settings
.fmt
,
1613 .descr
= "Format for fixed host ADC"
1616 .name
= "ADC_FIXED_CHANNELS",
1618 .valp
= &conf
.fixed_in
.settings
.nchannels
,
1619 .descr
= "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1622 .name
= "ADC_VOICES",
1624 .valp
= &conf
.fixed_in
.nb_voices
,
1625 .descr
= "Number of voices for ADC"
1628 .name
= "ADC_TRY_POLL",
1629 .tag
= AUD_OPT_BOOL
,
1630 .valp
= &conf
.try_poll_in
,
1631 .descr
= "Attempt using poll mode for ADC"
1635 .name
= "TIMER_PERIOD",
1637 .valp
= &conf
.period
.hertz
,
1638 .descr
= "Timer period in HZ (0 - use lowest possible)"
1642 .tag
= AUD_OPT_BOOL
,
1643 .valp
= &conf
.plive
,
1644 .descr
= "(undocumented)"
1647 .name
= "LOG_TO_MONITOR",
1648 .tag
= AUD_OPT_BOOL
,
1649 .valp
= &conf
.log_to_monitor
,
1650 .descr
= "Print logging messages to monitor instead of stderr"
1652 { /* End of list */ }
1655 static void audio_pp_nb_voices (const char *typ
, int nb
)
1659 printf ("Does not support %s\n", typ
);
1662 printf ("One %s voice\n", typ
);
1665 printf ("Theoretically supports many %s voices\n", typ
);
1668 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1674 void AUD_help (void)
1678 audio_process_options ("AUDIO", audio_options
);
1679 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1680 struct audio_driver
*d
= drvtab
[i
];
1682 audio_process_options (d
->name
, d
->options
);
1686 printf ("Audio options:\n");
1687 audio_print_options ("AUDIO", audio_options
);
1690 printf ("Available drivers:\n");
1692 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1693 struct audio_driver
*d
= drvtab
[i
];
1695 printf ("Name: %s\n", d
->name
);
1696 printf ("Description: %s\n", d
->descr
);
1698 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1699 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1702 printf ("Options:\n");
1703 audio_print_options (d
->name
, d
->options
);
1706 printf ("No options\n");
1712 "Options are settable through environment variables.\n"
1715 " set QEMU_AUDIO_DRV=wav\n"
1716 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1718 " export QEMU_AUDIO_DRV=wav\n"
1719 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1720 "(for csh replace export with setenv in the above)\n"
1726 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1729 audio_process_options (drv
->name
, drv
->options
);
1731 s
->drv_opaque
= drv
->init ();
1733 if (s
->drv_opaque
) {
1734 audio_init_nb_voices_out (drv
);
1735 audio_init_nb_voices_in (drv
);
1740 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1745 static void audio_vm_change_state_handler (void *opaque
, int running
,
1748 AudioState
*s
= opaque
;
1749 HWVoiceOut
*hwo
= NULL
;
1750 HWVoiceIn
*hwi
= NULL
;
1751 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1753 s
->vm_running
= running
;
1754 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1755 hwo
->pcm_ops
->ctl_out (hwo
, op
, conf
.try_poll_out
);
1758 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1759 hwi
->pcm_ops
->ctl_in (hwi
, op
, conf
.try_poll_in
);
1761 audio_reset_timer ();
1764 static void audio_atexit (void)
1766 AudioState
*s
= &glob_audio_state
;
1767 HWVoiceOut
*hwo
= NULL
;
1768 HWVoiceIn
*hwi
= NULL
;
1770 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1773 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1774 hwo
->pcm_ops
->fini_out (hwo
);
1776 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1777 CaptureVoiceOut
*cap
= sc
->cap
;
1778 struct capture_callback
*cb
;
1780 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1781 cb
->ops
.destroy (cb
->opaque
);
1786 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1787 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1788 hwi
->pcm_ops
->fini_in (hwi
);
1792 s
->drv
->fini (s
->drv_opaque
);
1796 static const VMStateDescription vmstate_audio
= {
1799 .minimum_version_id
= 1,
1800 .minimum_version_id_old
= 1,
1801 .fields
= (VMStateField
[]) {
1802 VMSTATE_END_OF_LIST()
1806 static void audio_init (void)
1810 const char *drvname
;
1811 VMChangeStateEntry
*e
;
1812 AudioState
*s
= &glob_audio_state
;
1818 QLIST_INIT (&s
->hw_head_out
);
1819 QLIST_INIT (&s
->hw_head_in
);
1820 QLIST_INIT (&s
->cap_head
);
1821 atexit (audio_atexit
);
1823 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1825 hw_error("Could not create audio timer\n");
1828 audio_process_options ("AUDIO", audio_options
);
1830 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1831 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1833 if (s
->nb_hw_voices_out
<= 0) {
1834 dolog ("Bogus number of playback voices %d, setting to 1\n",
1835 s
->nb_hw_voices_out
);
1836 s
->nb_hw_voices_out
= 1;
1839 if (s
->nb_hw_voices_in
<= 0) {
1840 dolog ("Bogus number of capture voices %d, setting to 0\n",
1841 s
->nb_hw_voices_in
);
1842 s
->nb_hw_voices_in
= 0;
1847 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1853 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1854 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1855 done
= !audio_driver_init (s
, drvtab
[i
]);
1862 dolog ("Unknown audio driver `%s'\n", drvname
);
1863 dolog ("Run with -audio-help to list available drivers\n");
1868 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1869 if (drvtab
[i
]->can_be_default
) {
1870 done
= !audio_driver_init (s
, drvtab
[i
]);
1876 done
= !audio_driver_init (s
, &no_audio_driver
);
1878 hw_error("Could not initialize audio subsystem\n");
1881 dolog ("warning: Using timer based audio emulation\n");
1885 if (conf
.period
.hertz
<= 0) {
1886 if (conf
.period
.hertz
< 0) {
1887 dolog ("warning: Timer period is negative - %d "
1888 "treating as zero\n",
1891 conf
.period
.ticks
= 1;
1894 muldiv64 (1, get_ticks_per_sec (), conf
.period
.hertz
);
1897 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1899 dolog ("warning: Could not register change state handler\n"
1900 "(Audio can continue looping even after stopping the VM)\n");
1903 QLIST_INIT (&s
->card_head
);
1904 vmstate_register (0, &vmstate_audio
, s
);
1907 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1910 card
->name
= qemu_strdup (name
);
1911 memset (&card
->entries
, 0, sizeof (card
->entries
));
1912 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1915 void AUD_remove_card (QEMUSoundCard
*card
)
1917 QLIST_REMOVE (card
, entries
);
1918 qemu_free (card
->name
);
1922 CaptureVoiceOut
*AUD_add_capture (
1923 struct audsettings
*as
,
1924 struct audio_capture_ops
*ops
,
1928 AudioState
*s
= &glob_audio_state
;
1929 CaptureVoiceOut
*cap
;
1930 struct capture_callback
*cb
;
1932 if (audio_validate_settings (as
)) {
1933 dolog ("Invalid settings were passed when trying to add capture\n");
1934 audio_print_settings (as
);
1938 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1940 dolog ("Could not allocate capture callback information, size %zu\n",
1945 cb
->opaque
= cb_opaque
;
1947 cap
= audio_pcm_capture_find_specific (as
);
1949 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1954 CaptureVoiceOut
*cap
;
1956 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1958 dolog ("Could not allocate capture voice, size %zu\n",
1964 QLIST_INIT (&hw
->sw_head
);
1965 QLIST_INIT (&cap
->cb_head
);
1967 /* XXX find a more elegant way */
1968 hw
->samples
= 4096 * 4;
1969 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1970 sizeof (struct st_sample
));
1972 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1977 audio_pcm_init_info (&hw
->info
, as
);
1979 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1981 dolog ("Could not allocate capture buffer "
1982 "(%d samples, each %d bytes)\n",
1983 hw
->samples
, 1 << hw
->info
.shift
);
1987 hw
->clip
= mixeng_clip
1988 [hw
->info
.nchannels
== 2]
1990 [hw
->info
.swap_endianness
]
1991 [audio_bits_to_index (hw
->info
.bits
)];
1993 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1994 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1997 while ((hw
= audio_pcm_hw_find_any_out (hw
))) {
1998 audio_attach_capture (hw
);
2003 qemu_free (cap
->hw
.mix_buf
);
2013 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
2015 struct capture_callback
*cb
;
2017 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
2018 if (cb
->opaque
== cb_opaque
) {
2019 cb
->ops
.destroy (cb_opaque
);
2020 QLIST_REMOVE (cb
, entries
);
2023 if (!cap
->cb_head
.lh_first
) {
2024 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
2027 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
2028 #ifdef DEBUG_CAPTURE
2029 dolog ("freeing %s\n", sw
->name
);
2032 sw1
= sw
->entries
.le_next
;
2034 st_rate_stop (sw
->rate
);
2037 QLIST_REMOVE (sw
, entries
);
2038 QLIST_REMOVE (sc
, entries
);
2042 QLIST_REMOVE (cap
, entries
);
2050 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2053 sw
->vol
.mute
= mute
;
2054 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2055 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2059 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2062 sw
->vol
.mute
= mute
;
2063 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2064 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;