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 int audio_bug (const char *funcname
, int cond
)
123 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
126 AUD_log (NULL
, "Save all your work and restart without audio\n");
127 AUD_log (NULL
, "Please send bug report to malc@pulsesoft.com\n");
128 AUD_log (NULL
, "I am sorry\n");
130 AUD_log (NULL
, "Context:\n");
132 #if defined AUDIO_BREAKPOINT_ON_BUG
133 # if defined HOST_I386
134 # if defined __GNUC__
136 # elif defined _MSC_VER
151 static inline int audio_bits_to_index (int bits
)
164 audio_bug ("bits_to_index", 1);
165 AUD_log (NULL
, "invalid bits %d\n", bits
);
170 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
176 cond
= !nmemb
|| !size
;
180 if (audio_bug ("audio_calloc", cond
)) {
181 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
183 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
187 return qemu_mallocz (len
);
190 static char *audio_alloc_prefix (const char *s
)
192 const char qemu_prefix
[] = "QEMU_";
201 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
203 u
= r
+ sizeof (qemu_prefix
) - 1;
205 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
206 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
208 for (i
= 0; i
< len
; ++i
) {
209 u
[i
] = qemu_toupper(u
[i
]);
215 static const char *audio_audfmt_to_string (audfmt_e fmt
)
237 dolog ("Bogus audfmt %d returning S16\n", fmt
);
241 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
244 if (!strcasecmp (s
, "u8")) {
248 else if (!strcasecmp (s
, "u16")) {
252 else if (!strcasecmp (s
, "u32")) {
256 else if (!strcasecmp (s
, "s8")) {
260 else if (!strcasecmp (s
, "s16")) {
264 else if (!strcasecmp (s
, "s32")) {
269 dolog ("Bogus audio format `%s' using %s\n",
270 s
, audio_audfmt_to_string (defval
));
276 static audfmt_e
audio_get_conf_fmt (const char *envname
,
280 const char *var
= getenv (envname
);
285 return audio_string_to_audfmt (var
, defval
, defaultp
);
288 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
293 strval
= getenv (key
);
305 static const char *audio_get_conf_str (const char *key
,
309 const char *val
= getenv (key
);
320 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
322 if (conf
.log_to_monitor
) {
324 monitor_printf(cur_mon
, "%s: ", cap
);
327 monitor_vprintf(cur_mon
, fmt
, ap
);
331 fprintf (stderr
, "%s: ", cap
);
334 vfprintf (stderr
, fmt
, ap
);
338 void AUD_log (const char *cap
, const char *fmt
, ...)
343 AUD_vlog (cap
, fmt
, ap
);
347 static void audio_print_options (const char *prefix
,
348 struct audio_option
*opt
)
353 dolog ("No prefix specified\n");
358 dolog ("No options\n");
362 uprefix
= audio_alloc_prefix (prefix
);
364 for (; opt
->name
; opt
++) {
365 const char *state
= "default";
366 printf (" %s_%s: ", uprefix
, opt
->name
);
368 if (opt
->overriddenp
&& *opt
->overriddenp
) {
375 int *intp
= opt
->valp
;
376 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
382 int *intp
= opt
->valp
;
383 printf ("integer, %s = %d\n", state
, *intp
);
389 audfmt_e
*fmtp
= opt
->valp
;
391 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
393 audio_audfmt_to_string (*fmtp
)
400 const char **strp
= opt
->valp
;
401 printf ("string, %s = %s\n",
403 *strp
? *strp
: "(not set)");
409 dolog ("Bad value tag for option %s_%s %d\n",
410 uprefix
, opt
->name
, opt
->tag
);
413 printf (" %s\n", opt
->descr
);
419 static void audio_process_options (const char *prefix
,
420 struct audio_option
*opt
)
423 const char qemu_prefix
[] = "QEMU_";
424 size_t preflen
, optlen
;
426 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
427 dolog ("prefix = NULL\n");
431 if (audio_bug (AUDIO_FUNC
, !opt
)) {
432 dolog ("opt = NULL\n");
436 preflen
= strlen (prefix
);
438 for (; opt
->name
; opt
++) {
443 dolog ("Option value pointer for `%s' is not set\n",
448 len
= strlen (opt
->name
);
449 /* len of opt->name + len of prefix + size of qemu_prefix
450 * (includes trailing zero) + zero + underscore (on behalf of
452 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
453 optname
= qemu_malloc (optlen
);
455 pstrcpy (optname
, optlen
, qemu_prefix
);
457 /* copy while upper-casing, including trailing zero */
458 for (i
= 0; i
<= preflen
; ++i
) {
459 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
461 pstrcat (optname
, optlen
, "_");
462 pstrcat (optname
, optlen
, opt
->name
);
469 int *intp
= opt
->valp
;
470 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
476 audfmt_e
*fmtp
= opt
->valp
;
477 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
483 const char **strp
= opt
->valp
;
484 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
489 dolog ("Bad value tag for option `%s' - %d\n",
494 if (!opt
->overriddenp
) {
495 opt
->overriddenp
= &opt
->overridden
;
497 *opt
->overriddenp
= !def
;
502 static void audio_print_settings (struct audsettings
*as
)
504 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
508 AUD_log (NULL
, "S8");
511 AUD_log (NULL
, "U8");
514 AUD_log (NULL
, "S16");
517 AUD_log (NULL
, "U16");
520 AUD_log (NULL
, "S32");
523 AUD_log (NULL
, "U32");
526 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
530 AUD_log (NULL
, " endianness=");
531 switch (as
->endianness
) {
533 AUD_log (NULL
, "little");
536 AUD_log (NULL
, "big");
539 AUD_log (NULL
, "invalid");
542 AUD_log (NULL
, "\n");
545 static int audio_validate_settings (struct audsettings
*as
)
549 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
550 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
565 invalid
|= as
->freq
<= 0;
566 return invalid
? -1 : 0;
569 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
571 int bits
= 8, sign
= 0;
591 return info
->freq
== as
->freq
592 && info
->nchannels
== as
->nchannels
593 && info
->sign
== sign
594 && info
->bits
== bits
595 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
598 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
600 int bits
= 8, sign
= 0, shift
= 0;
623 info
->freq
= as
->freq
;
626 info
->nchannels
= as
->nchannels
;
627 info
->shift
= (as
->nchannels
== 2) + shift
;
628 info
->align
= (1 << info
->shift
) - 1;
629 info
->bytes_per_second
= info
->freq
<< info
->shift
;
630 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
633 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
640 memset (buf
, 0x00, len
<< info
->shift
);
643 switch (info
->bits
) {
645 memset (buf
, 0x80, len
<< info
->shift
);
652 int shift
= info
->nchannels
- 1;
655 if (info
->swap_endianness
) {
659 for (i
= 0; i
< len
<< shift
; i
++) {
669 int shift
= info
->nchannels
- 1;
670 int32_t s
= INT32_MAX
;
672 if (info
->swap_endianness
) {
676 for (i
= 0; i
< len
<< shift
; i
++) {
683 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
693 static void noop_conv (struct st_sample
*dst
, const void *src
,
694 int samples
, struct mixeng_volume
*vol
)
702 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
703 struct audsettings
*as
706 CaptureVoiceOut
*cap
;
707 AudioState
*s
= &glob_audio_state
;
709 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
710 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
717 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
719 struct capture_callback
*cb
;
722 dolog ("notification %d sent\n", cmd
);
724 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
725 cb
->ops
.notify (cb
->opaque
, cmd
);
729 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
731 if (cap
->hw
.enabled
!= enabled
) {
732 audcnotification_e cmd
;
733 cap
->hw
.enabled
= enabled
;
734 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
735 audio_notify_capture (cap
, cmd
);
739 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
741 HWVoiceOut
*hw
= &cap
->hw
;
745 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
751 audio_capture_maybe_changed (cap
, enabled
);
754 static void audio_detach_capture (HWVoiceOut
*hw
)
756 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
759 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
760 SWVoiceOut
*sw
= &sc
->sw
;
761 CaptureVoiceOut
*cap
= sc
->cap
;
762 int was_active
= sw
->active
;
765 st_rate_stop (sw
->rate
);
769 QLIST_REMOVE (sw
, entries
);
770 QLIST_REMOVE (sc
, entries
);
773 /* We have removed soft voice from the capture:
774 this might have changed the overall status of the capture
775 since this might have been the only active voice */
776 audio_recalc_and_notify_capture (cap
);
782 static int audio_attach_capture (HWVoiceOut
*hw
)
784 AudioState
*s
= &glob_audio_state
;
785 CaptureVoiceOut
*cap
;
787 audio_detach_capture (hw
);
788 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
791 HWVoiceOut
*hw_cap
= &cap
->hw
;
793 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
795 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
805 sw
->active
= hw
->enabled
;
806 sw
->conv
= noop_conv
;
807 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
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 asprintf (&sw
->name
, "for %p %d,%d,%d",
818 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
819 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
822 audio_capture_maybe_changed (cap
, 1);
829 * Hard voice (capture)
831 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
834 int m
= hw
->total_samples_captured
;
836 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
838 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
844 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
846 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
847 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
848 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
855 * Soft voice (capture)
857 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
859 HWVoiceIn
*hw
= sw
->hw
;
860 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
863 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
864 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
868 rpos
= hw
->wpos
- live
;
873 return hw
->samples
+ rpos
;
877 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
879 HWVoiceIn
*hw
= sw
->hw
;
880 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
881 struct st_sample
*src
, *dst
= sw
->buf
;
883 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
885 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
886 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
887 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
891 samples
= size
>> sw
->info
.shift
;
896 swlim
= (live
* sw
->ratio
) >> 32;
897 swlim
= audio_MIN (swlim
, samples
);
900 src
= hw
->conv_buf
+ rpos
;
901 isamp
= hw
->wpos
- rpos
;
904 isamp
= hw
->samples
- rpos
;
912 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
913 dolog ("osamp=%d\n", osamp
);
917 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
919 rpos
= (rpos
+ isamp
) % hw
->samples
;
925 sw
->clip (buf
, sw
->buf
, ret
);
926 sw
->total_hw_samples_acquired
+= total
;
927 return ret
<< sw
->info
.shift
;
931 * Hard voice (playback)
933 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
939 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
940 if (sw
->active
|| !sw
->empty
) {
941 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
950 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
954 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
962 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
963 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
970 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
975 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
976 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
977 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
984 * Soft voice (playback)
986 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
988 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
989 int ret
= 0, pos
= 0, total
= 0;
995 hwsamples
= sw
->hw
->samples
;
997 live
= sw
->total_hw_samples_mixed
;
998 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
999 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1003 if (live
== hwsamples
) {
1005 dolog ("%s is full %d\n", sw
->name
, live
);
1010 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1011 samples
= size
>> sw
->info
.shift
;
1013 dead
= hwsamples
- live
;
1014 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1015 swlim
= audio_MIN (swlim
, samples
);
1017 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1021 dead
= hwsamples
- live
;
1022 left
= hwsamples
- wpos
;
1023 blck
= audio_MIN (dead
, left
);
1032 sw
->hw
->mix_buf
+ wpos
,
1040 wpos
= (wpos
+ osamp
) % hwsamples
;
1044 sw
->total_hw_samples_mixed
+= total
;
1045 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1049 "%s: write size %d ret %d total sw %d\n",
1051 size
>> sw
->info
.shift
,
1053 sw
->total_hw_samples_mixed
1057 return ret
<< sw
->info
.shift
;
1061 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1063 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1064 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1069 #include "audio_template.h"
1071 #include "audio_template.h"
1076 static void audio_timer (void *opaque
)
1078 AudioState
*s
= opaque
;
1080 audio_run ("timer");
1081 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1085 static int audio_is_timer_needed (void)
1087 HWVoiceIn
*hwi
= NULL
;
1088 HWVoiceOut
*hwo
= NULL
;
1090 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1091 if (!hwo
->poll_mode
) return 1;
1093 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1094 if (!hwi
->poll_mode
) return 1;
1099 static void audio_reset_timer (void)
1101 AudioState
*s
= &glob_audio_state
;
1103 if (audio_is_timer_needed ()) {
1104 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + 1);
1107 qemu_del_timer (s
->ts
);
1114 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1119 /* XXX: Consider options */
1123 if (!sw
->hw
->enabled
) {
1124 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1128 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1132 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1137 /* XXX: Consider options */
1141 if (!sw
->hw
->enabled
) {
1142 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1146 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1150 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1152 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1155 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1164 if (sw
->active
!= on
) {
1165 AudioState
*s
= &glob_audio_state
;
1166 SWVoiceOut
*temp_sw
;
1170 hw
->pending_disable
= 0;
1173 if (s
->vm_running
) {
1174 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
, conf
.try_poll_out
);
1175 audio_reset_timer ();
1183 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1184 temp_sw
= temp_sw
->entries
.le_next
) {
1185 nb_active
+= temp_sw
->active
!= 0;
1188 hw
->pending_disable
= nb_active
== 1;
1192 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1193 sc
->sw
.active
= hw
->enabled
;
1195 audio_capture_maybe_changed (sc
->cap
, 1);
1202 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1211 if (sw
->active
!= on
) {
1212 AudioState
*s
= &glob_audio_state
;
1218 if (s
->vm_running
) {
1219 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
, conf
.try_poll_in
);
1222 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1228 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1229 temp_sw
= temp_sw
->entries
.le_next
) {
1230 nb_active
+= temp_sw
->active
!= 0;
1233 if (nb_active
== 1) {
1235 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1243 static int audio_get_avail (SWVoiceIn
*sw
)
1251 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1252 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1253 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1258 "%s: get_avail live %d ret %" PRId64
"\n",
1260 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1263 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1266 static int audio_get_free (SWVoiceOut
*sw
)
1274 live
= sw
->total_hw_samples_mixed
;
1276 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1277 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1281 dead
= sw
->hw
->samples
- live
;
1284 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1286 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1289 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1292 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1299 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1300 SWVoiceOut
*sw
= &sc
->sw
;
1305 int till_end_of_hw
= hw
->samples
- rpos2
;
1306 int to_write
= audio_MIN (till_end_of_hw
, n
);
1307 int bytes
= to_write
<< hw
->info
.shift
;
1310 sw
->buf
= hw
->mix_buf
+ rpos2
;
1311 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1312 if (written
- bytes
) {
1313 dolog ("Could not mix %d bytes into a capture "
1314 "buffer, mixed %d\n",
1319 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1324 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1325 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1326 mixeng_clear (hw
->mix_buf
, samples
- n
);
1329 static void audio_run_out (AudioState
*s
)
1331 HWVoiceOut
*hw
= NULL
;
1334 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1336 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1338 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1343 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1344 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1348 if (hw
->pending_disable
&& !nb_live
) {
1351 dolog ("Disabling voice\n");
1354 hw
->pending_disable
= 0;
1355 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1356 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1358 audio_recalc_and_notify_capture (sc
->cap
);
1364 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1366 free
= audio_get_free (sw
);
1368 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1375 prev_rpos
= hw
->rpos
;
1376 played
= hw
->pcm_ops
->run_out (hw
);
1377 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1378 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1379 hw
->rpos
, hw
->samples
, played
);
1384 dolog ("played=%d\n", played
);
1388 hw
->ts_helper
+= played
;
1389 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1392 cleanup_required
= 0;
1393 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1394 if (!sw
->active
&& sw
->empty
) {
1398 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1399 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1400 played
, sw
->total_hw_samples_mixed
);
1401 played
= sw
->total_hw_samples_mixed
;
1404 sw
->total_hw_samples_mixed
-= played
;
1406 if (!sw
->total_hw_samples_mixed
) {
1408 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1412 free
= audio_get_free (sw
);
1414 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1419 if (cleanup_required
) {
1422 sw
= hw
->sw_head
.lh_first
;
1424 sw1
= sw
->entries
.le_next
;
1425 if (!sw
->active
&& !sw
->callback
.fn
) {
1427 dolog ("Finishing with old voice\n");
1429 audio_close_out (sw
);
1437 static void audio_run_in (AudioState
*s
)
1439 HWVoiceIn
*hw
= NULL
;
1441 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1445 captured
= hw
->pcm_ops
->run_in (hw
);
1447 min
= audio_pcm_hw_find_min_in (hw
);
1448 hw
->total_samples_captured
+= captured
- min
;
1449 hw
->ts_helper
+= captured
;
1451 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1452 sw
->total_hw_samples_acquired
-= min
;
1457 avail
= audio_get_avail (sw
);
1459 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1466 static void audio_run_capture (AudioState
*s
)
1468 CaptureVoiceOut
*cap
;
1470 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1471 int live
, rpos
, captured
;
1472 HWVoiceOut
*hw
= &cap
->hw
;
1475 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1478 int left
= hw
->samples
- rpos
;
1479 int to_capture
= audio_MIN (live
, left
);
1480 struct st_sample
*src
;
1481 struct capture_callback
*cb
;
1483 src
= hw
->mix_buf
+ rpos
;
1484 hw
->clip (cap
->buf
, src
, to_capture
);
1485 mixeng_clear (src
, to_capture
);
1487 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1488 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1489 to_capture
<< hw
->info
.shift
);
1491 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1496 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1497 if (!sw
->active
&& sw
->empty
) {
1501 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1502 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1503 captured
, sw
->total_hw_samples_mixed
);
1504 captured
= sw
->total_hw_samples_mixed
;
1507 sw
->total_hw_samples_mixed
-= captured
;
1508 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1513 void audio_run (const char *msg
)
1515 AudioState
*s
= &glob_audio_state
;
1519 audio_run_capture (s
);
1522 static double prevtime
;
1526 if (gettimeofday (&tv
, NULL
)) {
1527 perror ("audio_run: gettimeofday");
1531 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1532 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1533 prevtime
= currtime
;
1538 static struct audio_option audio_options
[] = {
1541 .name
= "DAC_FIXED_SETTINGS",
1542 .tag
= AUD_OPT_BOOL
,
1543 .valp
= &conf
.fixed_out
.enabled
,
1544 .descr
= "Use fixed settings for host DAC"
1547 .name
= "DAC_FIXED_FREQ",
1549 .valp
= &conf
.fixed_out
.settings
.freq
,
1550 .descr
= "Frequency for fixed host DAC"
1553 .name
= "DAC_FIXED_FMT",
1555 .valp
= &conf
.fixed_out
.settings
.fmt
,
1556 .descr
= "Format for fixed host DAC"
1559 .name
= "DAC_FIXED_CHANNELS",
1561 .valp
= &conf
.fixed_out
.settings
.nchannels
,
1562 .descr
= "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1565 .name
= "DAC_VOICES",
1567 .valp
= &conf
.fixed_out
.nb_voices
,
1568 .descr
= "Number of voices for DAC"
1571 .name
= "DAC_TRY_POLL",
1572 .tag
= AUD_OPT_BOOL
,
1573 .valp
= &conf
.try_poll_out
,
1574 .descr
= "Attempt using poll mode for DAC"
1578 .name
= "ADC_FIXED_SETTINGS",
1579 .tag
= AUD_OPT_BOOL
,
1580 .valp
= &conf
.fixed_in
.enabled
,
1581 .descr
= "Use fixed settings for host ADC"
1584 .name
= "ADC_FIXED_FREQ",
1586 .valp
= &conf
.fixed_in
.settings
.freq
,
1587 .descr
= "Frequency for fixed host ADC"
1590 .name
= "ADC_FIXED_FMT",
1592 .valp
= &conf
.fixed_in
.settings
.fmt
,
1593 .descr
= "Format for fixed host ADC"
1596 .name
= "ADC_FIXED_CHANNELS",
1598 .valp
= &conf
.fixed_in
.settings
.nchannels
,
1599 .descr
= "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1602 .name
= "ADC_VOICES",
1604 .valp
= &conf
.fixed_in
.nb_voices
,
1605 .descr
= "Number of voices for ADC"
1608 .name
= "ADC_TRY_POLL",
1609 .tag
= AUD_OPT_BOOL
,
1610 .valp
= &conf
.try_poll_in
,
1611 .descr
= "Attempt using poll mode for ADC"
1615 .name
= "TIMER_PERIOD",
1617 .valp
= &conf
.period
.hertz
,
1618 .descr
= "Timer period in HZ (0 - use lowest possible)"
1622 .tag
= AUD_OPT_BOOL
,
1623 .valp
= &conf
.plive
,
1624 .descr
= "(undocumented)"
1627 .name
= "LOG_TO_MONITOR",
1628 .tag
= AUD_OPT_BOOL
,
1629 .valp
= &conf
.log_to_monitor
,
1630 .descr
= "Print logging messages to monitor instead of stderr"
1632 { /* End of list */ }
1635 static void audio_pp_nb_voices (const char *typ
, int nb
)
1639 printf ("Does not support %s\n", typ
);
1642 printf ("One %s voice\n", typ
);
1645 printf ("Theoretically supports many %s voices\n", typ
);
1648 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1654 void AUD_help (void)
1658 audio_process_options ("AUDIO", audio_options
);
1659 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1660 struct audio_driver
*d
= drvtab
[i
];
1662 audio_process_options (d
->name
, d
->options
);
1666 printf ("Audio options:\n");
1667 audio_print_options ("AUDIO", audio_options
);
1670 printf ("Available drivers:\n");
1672 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1673 struct audio_driver
*d
= drvtab
[i
];
1675 printf ("Name: %s\n", d
->name
);
1676 printf ("Description: %s\n", d
->descr
);
1678 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1679 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1682 printf ("Options:\n");
1683 audio_print_options (d
->name
, d
->options
);
1686 printf ("No options\n");
1692 "Options are settable through environment variables.\n"
1695 " set QEMU_AUDIO_DRV=wav\n"
1696 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1698 " export QEMU_AUDIO_DRV=wav\n"
1699 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1700 "(for csh replace export with setenv in the above)\n"
1706 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1709 audio_process_options (drv
->name
, drv
->options
);
1711 s
->drv_opaque
= drv
->init ();
1713 if (s
->drv_opaque
) {
1714 audio_init_nb_voices_out (drv
);
1715 audio_init_nb_voices_in (drv
);
1720 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1725 static void audio_vm_change_state_handler (void *opaque
, int running
,
1728 AudioState
*s
= opaque
;
1729 HWVoiceOut
*hwo
= NULL
;
1730 HWVoiceIn
*hwi
= NULL
;
1731 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1733 s
->vm_running
= running
;
1734 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1735 hwo
->pcm_ops
->ctl_out (hwo
, op
, conf
.try_poll_out
);
1738 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1739 hwi
->pcm_ops
->ctl_in (hwi
, op
, conf
.try_poll_in
);
1741 audio_reset_timer ();
1744 static void audio_atexit (void)
1746 AudioState
*s
= &glob_audio_state
;
1747 HWVoiceOut
*hwo
= NULL
;
1748 HWVoiceIn
*hwi
= NULL
;
1750 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1753 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1754 hwo
->pcm_ops
->fini_out (hwo
);
1756 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1757 CaptureVoiceOut
*cap
= sc
->cap
;
1758 struct capture_callback
*cb
;
1760 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1761 cb
->ops
.destroy (cb
->opaque
);
1766 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1767 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1768 hwi
->pcm_ops
->fini_in (hwi
);
1772 s
->drv
->fini (s
->drv_opaque
);
1776 static void audio_save (QEMUFile
*f
, void *opaque
)
1782 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1787 if (version_id
!= 1) {
1794 static void audio_init (void)
1798 const char *drvname
;
1799 VMChangeStateEntry
*e
;
1800 AudioState
*s
= &glob_audio_state
;
1806 QLIST_INIT (&s
->hw_head_out
);
1807 QLIST_INIT (&s
->hw_head_in
);
1808 QLIST_INIT (&s
->cap_head
);
1809 atexit (audio_atexit
);
1811 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1813 hw_error("Could not create audio timer\n");
1816 audio_process_options ("AUDIO", audio_options
);
1818 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1819 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1821 if (s
->nb_hw_voices_out
<= 0) {
1822 dolog ("Bogus number of playback voices %d, setting to 1\n",
1823 s
->nb_hw_voices_out
);
1824 s
->nb_hw_voices_out
= 1;
1827 if (s
->nb_hw_voices_in
<= 0) {
1828 dolog ("Bogus number of capture voices %d, setting to 0\n",
1829 s
->nb_hw_voices_in
);
1830 s
->nb_hw_voices_in
= 0;
1835 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1841 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1842 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1843 done
= !audio_driver_init (s
, drvtab
[i
]);
1850 dolog ("Unknown audio driver `%s'\n", drvname
);
1851 dolog ("Run with -audio-help to list available drivers\n");
1856 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1857 if (drvtab
[i
]->can_be_default
) {
1858 done
= !audio_driver_init (s
, drvtab
[i
]);
1864 done
= !audio_driver_init (s
, &no_audio_driver
);
1866 hw_error("Could not initialize audio subsystem\n");
1869 dolog ("warning: Using timer based audio emulation\n");
1873 if (conf
.period
.hertz
<= 0) {
1874 if (conf
.period
.hertz
< 0) {
1875 dolog ("warning: Timer period is negative - %d "
1876 "treating as zero\n",
1879 conf
.period
.ticks
= 1;
1881 conf
.period
.ticks
= get_ticks_per_sec() / conf
.period
.hertz
;
1884 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1886 dolog ("warning: Could not register change state handler\n"
1887 "(Audio can continue looping even after stopping the VM)\n");
1890 QLIST_INIT (&s
->card_head
);
1891 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1894 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1897 card
->name
= qemu_strdup (name
);
1898 memset (&card
->entries
, 0, sizeof (card
->entries
));
1899 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1902 void AUD_remove_card (QEMUSoundCard
*card
)
1904 QLIST_REMOVE (card
, entries
);
1905 qemu_free (card
->name
);
1909 CaptureVoiceOut
*AUD_add_capture (
1910 struct audsettings
*as
,
1911 struct audio_capture_ops
*ops
,
1915 AudioState
*s
= &glob_audio_state
;
1916 CaptureVoiceOut
*cap
;
1917 struct capture_callback
*cb
;
1919 if (audio_validate_settings (as
)) {
1920 dolog ("Invalid settings were passed when trying to add capture\n");
1921 audio_print_settings (as
);
1925 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1927 dolog ("Could not allocate capture callback information, size %zu\n",
1932 cb
->opaque
= cb_opaque
;
1934 cap
= audio_pcm_capture_find_specific (as
);
1936 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1941 CaptureVoiceOut
*cap
;
1943 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1945 dolog ("Could not allocate capture voice, size %zu\n",
1951 QLIST_INIT (&hw
->sw_head
);
1952 QLIST_INIT (&cap
->cb_head
);
1954 /* XXX find a more elegant way */
1955 hw
->samples
= 4096 * 4;
1956 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1957 sizeof (struct st_sample
));
1959 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1964 audio_pcm_init_info (&hw
->info
, as
);
1966 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1968 dolog ("Could not allocate capture buffer "
1969 "(%d samples, each %d bytes)\n",
1970 hw
->samples
, 1 << hw
->info
.shift
);
1974 hw
->clip
= mixeng_clip
1975 [hw
->info
.nchannels
== 2]
1977 [hw
->info
.swap_endianness
]
1978 [audio_bits_to_index (hw
->info
.bits
)];
1980 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1981 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1984 while ((hw
= audio_pcm_hw_find_any_out (hw
))) {
1985 audio_attach_capture (hw
);
1990 qemu_free (cap
->hw
.mix_buf
);
2000 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
2002 struct capture_callback
*cb
;
2004 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
2005 if (cb
->opaque
== cb_opaque
) {
2006 cb
->ops
.destroy (cb_opaque
);
2007 QLIST_REMOVE (cb
, entries
);
2010 if (!cap
->cb_head
.lh_first
) {
2011 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
2014 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
2015 #ifdef DEBUG_CAPTURE
2016 dolog ("freeing %s\n", sw
->name
);
2019 sw1
= sw
->entries
.le_next
;
2021 st_rate_stop (sw
->rate
);
2024 QLIST_REMOVE (sw
, entries
);
2025 QLIST_REMOVE (sc
, entries
);
2029 QLIST_REMOVE (cap
, entries
);
2037 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2040 sw
->vol
.mute
= mute
;
2041 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2042 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2046 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2049 sw
->vol
.mute
= mute
;
2050 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2051 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;