4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu-timer.h"
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
33 /* #define DEBUG_PLIVE */
34 /* #define DEBUG_LIVE */
35 /* #define DEBUG_OUT */
36 /* #define DEBUG_CAPTURE */
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
40 static struct audio_driver
*drvtab
[] = {
46 struct fixed_settings
{
50 struct audsettings settings
;
54 struct fixed_settings fixed_out
;
55 struct fixed_settings fixed_in
;
63 { /* DAC fixed settings */
70 AUD_FMT_S16
, /* fmt */
75 { /* ADC fixed settings */
82 AUD_FMT_S16
, /* fmt */
89 0 /* log_to_monitor */
92 static AudioState glob_audio_state
;
94 struct mixeng_volume nominal_volume
= {
105 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
106 /* http://www.multi-platforms.com/Tips/PopCount.htm */
107 uint32_t popcount (uint32_t u
)
109 u
= ((u
&0x55555555) + ((u
>>1)&0x55555555));
110 u
= ((u
&0x33333333) + ((u
>>2)&0x33333333));
111 u
= ((u
&0x0f0f0f0f) + ((u
>>4)&0x0f0f0f0f));
112 u
= ((u
&0x00ff00ff) + ((u
>>8)&0x00ff00ff));
113 u
= ( u
&0x0000ffff) + (u
>>16);
117 inline uint32_t lsbindex (uint32_t u
)
119 return popcount ((u
&-u
)-1);
122 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
125 int audio_bug (const char *funcname
, int cond
)
130 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
133 AUD_log (NULL
, "Save all your work and restart without audio\n");
134 AUD_log (NULL
, "Please send bug report to malc@pulsesoft.com\n");
135 AUD_log (NULL
, "I am sorry\n");
137 AUD_log (NULL
, "Context:\n");
139 #if defined AUDIO_BREAKPOINT_ON_BUG
140 # if defined HOST_I386
141 # if defined __GNUC__
143 # elif defined _MSC_VER
158 static inline int audio_bits_to_index (int bits
)
171 audio_bug ("bits_to_index", 1);
172 AUD_log (NULL
, "invalid bits %d\n", bits
);
177 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
183 cond
= !nmemb
|| !size
;
187 if (audio_bug ("audio_calloc", cond
)) {
188 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
190 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
194 return qemu_mallocz (len
);
197 static char *audio_alloc_prefix (const char *s
)
199 const char qemu_prefix
[] = "QEMU_";
208 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
210 u
= r
+ sizeof (qemu_prefix
) - 1;
212 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
213 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
215 for (i
= 0; i
< len
; ++i
) {
216 u
[i
] = qemu_toupper(u
[i
]);
222 static const char *audio_audfmt_to_string (audfmt_e fmt
)
244 dolog ("Bogus audfmt %d returning S16\n", fmt
);
248 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
251 if (!strcasecmp (s
, "u8")) {
255 else if (!strcasecmp (s
, "u16")) {
259 else if (!strcasecmp (s
, "u32")) {
263 else if (!strcasecmp (s
, "s8")) {
267 else if (!strcasecmp (s
, "s16")) {
271 else if (!strcasecmp (s
, "s32")) {
276 dolog ("Bogus audio format `%s' using %s\n",
277 s
, audio_audfmt_to_string (defval
));
283 static audfmt_e
audio_get_conf_fmt (const char *envname
,
287 const char *var
= getenv (envname
);
292 return audio_string_to_audfmt (var
, defval
, defaultp
);
295 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
300 strval
= getenv (key
);
312 static const char *audio_get_conf_str (const char *key
,
316 const char *val
= getenv (key
);
327 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
329 if (conf
.log_to_monitor
) {
331 term_printf ("%s: ", cap
);
334 term_vprintf (fmt
, ap
);
338 fprintf (stderr
, "%s: ", cap
);
341 vfprintf (stderr
, fmt
, ap
);
345 void AUD_log (const char *cap
, const char *fmt
, ...)
350 AUD_vlog (cap
, fmt
, ap
);
354 static void audio_print_options (const char *prefix
,
355 struct audio_option
*opt
)
360 dolog ("No prefix specified\n");
365 dolog ("No options\n");
369 uprefix
= audio_alloc_prefix (prefix
);
371 for (; opt
->name
; opt
++) {
372 const char *state
= "default";
373 printf (" %s_%s: ", uprefix
, opt
->name
);
375 if (opt
->overriddenp
&& *opt
->overriddenp
) {
382 int *intp
= opt
->valp
;
383 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
389 int *intp
= opt
->valp
;
390 printf ("integer, %s = %d\n", state
, *intp
);
396 audfmt_e
*fmtp
= opt
->valp
;
398 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
400 audio_audfmt_to_string (*fmtp
)
407 const char **strp
= opt
->valp
;
408 printf ("string, %s = %s\n",
410 *strp
? *strp
: "(not set)");
416 dolog ("Bad value tag for option %s_%s %d\n",
417 uprefix
, opt
->name
, opt
->tag
);
420 printf (" %s\n", opt
->descr
);
426 static void audio_process_options (const char *prefix
,
427 struct audio_option
*opt
)
430 const char qemu_prefix
[] = "QEMU_";
431 size_t preflen
, optlen
;
433 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
434 dolog ("prefix = NULL\n");
438 if (audio_bug (AUDIO_FUNC
, !opt
)) {
439 dolog ("opt = NULL\n");
443 preflen
= strlen (prefix
);
445 for (; opt
->name
; opt
++) {
450 dolog ("Option value pointer for `%s' is not set\n",
455 len
= strlen (opt
->name
);
456 /* len of opt->name + len of prefix + size of qemu_prefix
457 * (includes trailing zero) + zero + underscore (on behalf of
459 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
460 optname
= qemu_malloc (optlen
);
462 pstrcpy (optname
, optlen
, qemu_prefix
);
464 /* copy while upper-casing, including trailing zero */
465 for (i
= 0; i
<= preflen
; ++i
) {
466 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
468 pstrcat (optname
, optlen
, "_");
469 pstrcat (optname
, optlen
, opt
->name
);
476 int *intp
= opt
->valp
;
477 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
483 audfmt_e
*fmtp
= opt
->valp
;
484 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
490 const char **strp
= opt
->valp
;
491 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
496 dolog ("Bad value tag for option `%s' - %d\n",
501 if (!opt
->overriddenp
) {
502 opt
->overriddenp
= &opt
->overridden
;
504 *opt
->overriddenp
= !def
;
509 static void audio_print_settings (struct audsettings
*as
)
511 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
515 AUD_log (NULL
, "S8");
518 AUD_log (NULL
, "U8");
521 AUD_log (NULL
, "S16");
524 AUD_log (NULL
, "U16");
527 AUD_log (NULL
, "S32");
530 AUD_log (NULL
, "U32");
533 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
537 AUD_log (NULL
, " endianness=");
538 switch (as
->endianness
) {
540 AUD_log (NULL
, "little");
543 AUD_log (NULL
, "big");
546 AUD_log (NULL
, "invalid");
549 AUD_log (NULL
, "\n");
552 static int audio_validate_settings (struct audsettings
*as
)
556 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
557 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
572 invalid
|= as
->freq
<= 0;
573 return invalid
? -1 : 0;
576 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
578 int bits
= 8, sign
= 0;
598 return info
->freq
== as
->freq
599 && info
->nchannels
== as
->nchannels
600 && info
->sign
== sign
601 && info
->bits
== bits
602 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
605 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
607 int bits
= 8, sign
= 0, shift
= 0;
630 info
->freq
= as
->freq
;
633 info
->nchannels
= as
->nchannels
;
634 info
->shift
= (as
->nchannels
== 2) + shift
;
635 info
->align
= (1 << info
->shift
) - 1;
636 info
->bytes_per_second
= info
->freq
<< info
->shift
;
637 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
640 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
647 memset (buf
, 0x00, len
<< info
->shift
);
650 switch (info
->bits
) {
652 memset (buf
, 0x80, len
<< info
->shift
);
659 int shift
= info
->nchannels
- 1;
662 if (info
->swap_endianness
) {
666 for (i
= 0; i
< len
<< shift
; i
++) {
676 int shift
= info
->nchannels
- 1;
677 int32_t s
= INT32_MAX
;
679 if (info
->swap_endianness
) {
683 for (i
= 0; i
< len
<< shift
; i
++) {
690 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
700 static void noop_conv (struct st_sample
*dst
, const void *src
,
701 int samples
, struct mixeng_volume
*vol
)
709 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
711 struct audsettings
*as
714 CaptureVoiceOut
*cap
;
716 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
717 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
724 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
726 struct capture_callback
*cb
;
729 dolog ("notification %d sent\n", cmd
);
731 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
732 cb
->ops
.notify (cb
->opaque
, cmd
);
736 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
738 if (cap
->hw
.enabled
!= enabled
) {
739 audcnotification_e cmd
;
740 cap
->hw
.enabled
= enabled
;
741 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
742 audio_notify_capture (cap
, cmd
);
746 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
748 HWVoiceOut
*hw
= &cap
->hw
;
752 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
758 audio_capture_maybe_changed (cap
, enabled
);
761 static void audio_detach_capture (HWVoiceOut
*hw
)
763 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
766 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
767 SWVoiceOut
*sw
= &sc
->sw
;
768 CaptureVoiceOut
*cap
= sc
->cap
;
769 int was_active
= sw
->active
;
772 st_rate_stop (sw
->rate
);
776 LIST_REMOVE (sw
, entries
);
777 LIST_REMOVE (sc
, entries
);
780 /* We have removed soft voice from the capture:
781 this might have changed the overall status of the capture
782 since this might have been the only active voice */
783 audio_recalc_and_notify_capture (cap
);
789 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
791 CaptureVoiceOut
*cap
;
793 audio_detach_capture (hw
);
794 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
797 HWVoiceOut
*hw_cap
= &cap
->hw
;
799 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
801 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
811 sw
->active
= hw
->enabled
;
812 sw
->conv
= noop_conv
;
813 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
814 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
816 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
820 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
821 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
823 asprintf (&sw
->name
, "for %p %d,%d,%d",
824 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
825 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
828 audio_capture_maybe_changed (cap
, 1);
835 * Hard voice (capture)
837 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
840 int m
= hw
->total_samples_captured
;
842 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
844 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
850 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
852 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
853 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
854 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
861 * Soft voice (capture)
863 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
865 HWVoiceIn
*hw
= sw
->hw
;
866 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
869 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
870 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
874 rpos
= hw
->wpos
- live
;
879 return hw
->samples
+ rpos
;
883 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
885 HWVoiceIn
*hw
= sw
->hw
;
886 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
887 struct st_sample
*src
, *dst
= sw
->buf
;
889 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
891 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
892 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
893 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
897 samples
= size
>> sw
->info
.shift
;
902 swlim
= (live
* sw
->ratio
) >> 32;
903 swlim
= audio_MIN (swlim
, samples
);
906 src
= hw
->conv_buf
+ rpos
;
907 isamp
= hw
->wpos
- rpos
;
910 isamp
= hw
->samples
- rpos
;
918 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
919 dolog ("osamp=%d\n", osamp
);
923 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
925 rpos
= (rpos
+ isamp
) % hw
->samples
;
931 sw
->clip (buf
, sw
->buf
, ret
);
932 sw
->total_hw_samples_acquired
+= total
;
933 return ret
<< sw
->info
.shift
;
937 * Hard voice (playback)
939 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
945 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
946 if (sw
->active
|| !sw
->empty
) {
947 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
956 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
960 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
968 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
969 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
976 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
981 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
982 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
983 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
990 * Soft voice (playback)
992 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
994 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
995 int ret
= 0, pos
= 0, total
= 0;
1001 hwsamples
= sw
->hw
->samples
;
1003 live
= sw
->total_hw_samples_mixed
;
1004 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1005 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1009 if (live
== hwsamples
) {
1011 dolog ("%s is full %d\n", sw
->name
, live
);
1016 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1017 samples
= size
>> sw
->info
.shift
;
1019 dead
= hwsamples
- live
;
1020 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1021 swlim
= audio_MIN (swlim
, samples
);
1023 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1027 dead
= hwsamples
- live
;
1028 left
= hwsamples
- wpos
;
1029 blck
= audio_MIN (dead
, left
);
1038 sw
->hw
->mix_buf
+ wpos
,
1046 wpos
= (wpos
+ osamp
) % hwsamples
;
1050 sw
->total_hw_samples_mixed
+= total
;
1051 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1055 "%s: write size %d ret %d total sw %d\n",
1057 size
>> sw
->info
.shift
,
1059 sw
->total_hw_samples_mixed
1063 return ret
<< sw
->info
.shift
;
1067 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1069 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1070 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1075 #include "audio_template.h"
1077 #include "audio_template.h"
1079 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1084 /* XXX: Consider options */
1088 if (!sw
->hw
->enabled
) {
1089 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1093 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1097 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1102 /* XXX: Consider options */
1106 if (!sw
->hw
->enabled
) {
1107 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1111 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1115 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1117 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1120 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1129 if (sw
->active
!= on
) {
1130 SWVoiceOut
*temp_sw
;
1134 hw
->pending_disable
= 0;
1137 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1144 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1145 temp_sw
= temp_sw
->entries
.le_next
) {
1146 nb_active
+= temp_sw
->active
!= 0;
1149 hw
->pending_disable
= nb_active
== 1;
1153 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1154 sc
->sw
.active
= hw
->enabled
;
1156 audio_capture_maybe_changed (sc
->cap
, 1);
1163 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1172 if (sw
->active
!= on
) {
1178 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1180 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1186 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1187 temp_sw
= temp_sw
->entries
.le_next
) {
1188 nb_active
+= temp_sw
->active
!= 0;
1191 if (nb_active
== 1) {
1193 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1201 static int audio_get_avail (SWVoiceIn
*sw
)
1209 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1210 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1211 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1216 "%s: get_avail live %d ret %" PRId64
"\n",
1218 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1221 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1224 static int audio_get_free (SWVoiceOut
*sw
)
1232 live
= sw
->total_hw_samples_mixed
;
1234 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1235 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1239 dead
= sw
->hw
->samples
- live
;
1242 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1244 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1247 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1250 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1257 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1258 SWVoiceOut
*sw
= &sc
->sw
;
1263 int till_end_of_hw
= hw
->samples
- rpos2
;
1264 int to_write
= audio_MIN (till_end_of_hw
, n
);
1265 int bytes
= to_write
<< hw
->info
.shift
;
1268 sw
->buf
= hw
->mix_buf
+ rpos2
;
1269 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1270 if (written
- bytes
) {
1271 dolog ("Could not mix %d bytes into a capture "
1272 "buffer, mixed %d\n",
1277 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1282 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1283 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1284 mixeng_clear (hw
->mix_buf
, samples
- n
);
1287 static void audio_run_out (AudioState
*s
)
1289 HWVoiceOut
*hw
= NULL
;
1292 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1294 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1296 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1301 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1302 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1306 if (hw
->pending_disable
&& !nb_live
) {
1309 dolog ("Disabling voice\n");
1312 hw
->pending_disable
= 0;
1313 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1314 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1316 audio_recalc_and_notify_capture (sc
->cap
);
1322 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1324 free
= audio_get_free (sw
);
1326 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1333 prev_rpos
= hw
->rpos
;
1334 played
= hw
->pcm_ops
->run_out (hw
);
1335 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1336 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1337 hw
->rpos
, hw
->samples
, played
);
1342 dolog ("played=%d\n", played
);
1346 hw
->ts_helper
+= played
;
1347 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1350 cleanup_required
= 0;
1351 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1352 if (!sw
->active
&& sw
->empty
) {
1356 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1357 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1358 played
, sw
->total_hw_samples_mixed
);
1359 played
= sw
->total_hw_samples_mixed
;
1362 sw
->total_hw_samples_mixed
-= played
;
1364 if (!sw
->total_hw_samples_mixed
) {
1366 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1370 free
= audio_get_free (sw
);
1372 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1377 if (cleanup_required
) {
1380 sw
= hw
->sw_head
.lh_first
;
1382 sw1
= sw
->entries
.le_next
;
1383 if (!sw
->active
&& !sw
->callback
.fn
) {
1385 dolog ("Finishing with old voice\n");
1387 audio_close_out (s
, sw
);
1395 static void audio_run_in (AudioState
*s
)
1397 HWVoiceIn
*hw
= NULL
;
1399 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1403 captured
= hw
->pcm_ops
->run_in (hw
);
1405 min
= audio_pcm_hw_find_min_in (hw
);
1406 hw
->total_samples_captured
+= captured
- min
;
1407 hw
->ts_helper
+= captured
;
1409 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1410 sw
->total_hw_samples_acquired
-= min
;
1415 avail
= audio_get_avail (sw
);
1417 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1424 static void audio_run_capture (AudioState
*s
)
1426 CaptureVoiceOut
*cap
;
1428 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1429 int live
, rpos
, captured
;
1430 HWVoiceOut
*hw
= &cap
->hw
;
1433 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1436 int left
= hw
->samples
- rpos
;
1437 int to_capture
= audio_MIN (live
, left
);
1438 struct st_sample
*src
;
1439 struct capture_callback
*cb
;
1441 src
= hw
->mix_buf
+ rpos
;
1442 hw
->clip (cap
->buf
, src
, to_capture
);
1443 mixeng_clear (src
, to_capture
);
1445 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1446 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1447 to_capture
<< hw
->info
.shift
);
1449 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1454 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1455 if (!sw
->active
&& sw
->empty
) {
1459 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1460 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1461 captured
, sw
->total_hw_samples_mixed
);
1462 captured
= sw
->total_hw_samples_mixed
;
1465 sw
->total_hw_samples_mixed
-= captured
;
1466 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1471 static void audio_timer (void *opaque
)
1473 AudioState
*s
= opaque
;
1477 audio_run_capture (s
);
1479 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1482 static struct audio_option audio_options
[] = {
1484 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1485 "Use fixed settings for host DAC", NULL
, 0},
1487 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1488 "Frequency for fixed host DAC", NULL
, 0},
1490 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1491 "Format for fixed host DAC", NULL
, 0},
1493 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1494 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1496 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1497 "Number of voices for DAC", NULL
, 0},
1500 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1501 "Use fixed settings for host ADC", NULL
, 0},
1503 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1504 "Frequency for fixed host ADC", NULL
, 0},
1506 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1507 "Format for fixed host ADC", NULL
, 0},
1509 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1510 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1512 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1513 "Number of voices for ADC", NULL
, 0},
1516 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hertz
,
1517 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1519 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1520 "(undocumented)", NULL
, 0},
1522 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1523 "print logging messages to monitor instead of stderr", NULL
, 0},
1525 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1528 static void audio_pp_nb_voices (const char *typ
, int nb
)
1532 printf ("Does not support %s\n", typ
);
1535 printf ("One %s voice\n", typ
);
1538 printf ("Theoretically supports many %s voices\n", typ
);
1541 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1547 void AUD_help (void)
1551 audio_process_options ("AUDIO", audio_options
);
1552 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1553 struct audio_driver
*d
= drvtab
[i
];
1555 audio_process_options (d
->name
, d
->options
);
1559 printf ("Audio options:\n");
1560 audio_print_options ("AUDIO", audio_options
);
1563 printf ("Available drivers:\n");
1565 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1566 struct audio_driver
*d
= drvtab
[i
];
1568 printf ("Name: %s\n", d
->name
);
1569 printf ("Description: %s\n", d
->descr
);
1571 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1572 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1575 printf ("Options:\n");
1576 audio_print_options (d
->name
, d
->options
);
1579 printf ("No options\n");
1585 "Options are settable through environment variables.\n"
1588 " set QEMU_AUDIO_DRV=wav\n"
1589 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1591 " export QEMU_AUDIO_DRV=wav\n"
1592 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1593 "(for csh replace export with setenv in the above)\n"
1599 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1602 audio_process_options (drv
->name
, drv
->options
);
1604 s
->drv_opaque
= drv
->init ();
1606 if (s
->drv_opaque
) {
1607 audio_init_nb_voices_out (s
, drv
);
1608 audio_init_nb_voices_in (s
, drv
);
1613 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1618 static void audio_vm_change_state_handler (void *opaque
, int running
,
1621 AudioState
*s
= opaque
;
1622 HWVoiceOut
*hwo
= NULL
;
1623 HWVoiceIn
*hwi
= NULL
;
1624 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1626 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1627 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1630 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1631 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1635 static void audio_atexit (void)
1637 AudioState
*s
= &glob_audio_state
;
1638 HWVoiceOut
*hwo
= NULL
;
1639 HWVoiceIn
*hwi
= NULL
;
1641 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1644 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1645 hwo
->pcm_ops
->fini_out (hwo
);
1647 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1648 CaptureVoiceOut
*cap
= sc
->cap
;
1649 struct capture_callback
*cb
;
1651 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1652 cb
->ops
.destroy (cb
->opaque
);
1657 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1658 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1659 hwi
->pcm_ops
->fini_in (hwi
);
1663 s
->drv
->fini (s
->drv_opaque
);
1667 static void audio_save (QEMUFile
*f
, void *opaque
)
1673 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1678 if (version_id
!= 1) {
1685 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1688 card
->name
= qemu_strdup (name
);
1689 memset (&card
->entries
, 0, sizeof (card
->entries
));
1690 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1693 void AUD_remove_card (QEMUSoundCard
*card
)
1695 LIST_REMOVE (card
, entries
);
1697 qemu_free (card
->name
);
1700 AudioState
*AUD_init (void)
1704 const char *drvname
;
1705 AudioState
*s
= &glob_audio_state
;
1707 LIST_INIT (&s
->hw_head_out
);
1708 LIST_INIT (&s
->hw_head_in
);
1709 LIST_INIT (&s
->cap_head
);
1710 atexit (audio_atexit
);
1712 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1714 dolog ("Could not create audio timer\n");
1718 audio_process_options ("AUDIO", audio_options
);
1720 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1721 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1723 if (s
->nb_hw_voices_out
<= 0) {
1724 dolog ("Bogus number of playback voices %d, setting to 1\n",
1725 s
->nb_hw_voices_out
);
1726 s
->nb_hw_voices_out
= 1;
1729 if (s
->nb_hw_voices_in
<= 0) {
1730 dolog ("Bogus number of capture voices %d, setting to 0\n",
1731 s
->nb_hw_voices_in
);
1732 s
->nb_hw_voices_in
= 0;
1737 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1743 for (i
= 0; i
< ARRAY_SIZE (drvtab
); i
++) {
1744 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1745 done
= !audio_driver_init (s
, drvtab
[i
]);
1752 dolog ("Unknown audio driver `%s'\n", drvname
);
1753 dolog ("Run with -audio-help to list available drivers\n");
1758 for (i
= 0; !done
&& i
< ARRAY_SIZE (drvtab
); i
++) {
1759 if (drvtab
[i
]->can_be_default
) {
1760 done
= !audio_driver_init (s
, drvtab
[i
]);
1766 done
= !audio_driver_init (s
, &no_audio_driver
);
1768 dolog ("Could not initialize audio subsystem\n");
1771 dolog ("warning: Using timer based audio emulation\n");
1776 VMChangeStateEntry
*e
;
1778 if (conf
.period
.hertz
<= 0) {
1779 if (conf
.period
.hertz
< 0) {
1780 dolog ("warning: Timer period is negative - %d "
1781 "treating as zero\n",
1784 conf
.period
.ticks
= 1;
1787 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hertz
;
1790 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1792 dolog ("warning: Could not register change state handler\n"
1793 "(Audio can continue looping even after stopping the VM)\n");
1797 qemu_del_timer (s
->ts
);
1801 LIST_INIT (&s
->card_head
);
1802 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1803 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1807 CaptureVoiceOut
*AUD_add_capture (
1809 struct audsettings
*as
,
1810 struct audio_capture_ops
*ops
,
1814 CaptureVoiceOut
*cap
;
1815 struct capture_callback
*cb
;
1819 s
= &glob_audio_state
;
1822 if (audio_validate_settings (as
)) {
1823 dolog ("Invalid settings were passed when trying to add capture\n");
1824 audio_print_settings (as
);
1828 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1830 dolog ("Could not allocate capture callback information, size %zu\n",
1835 cb
->opaque
= cb_opaque
;
1837 cap
= audio_pcm_capture_find_specific (s
, as
);
1839 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1844 CaptureVoiceOut
*cap
;
1846 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1848 dolog ("Could not allocate capture voice, size %zu\n",
1854 LIST_INIT (&hw
->sw_head
);
1855 LIST_INIT (&cap
->cb_head
);
1857 /* XXX find a more elegant way */
1858 hw
->samples
= 4096 * 4;
1859 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1860 sizeof (struct st_sample
));
1862 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1867 audio_pcm_init_info (&hw
->info
, as
);
1869 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1871 dolog ("Could not allocate capture buffer "
1872 "(%d samples, each %d bytes)\n",
1873 hw
->samples
, 1 << hw
->info
.shift
);
1877 hw
->clip
= mixeng_clip
1878 [hw
->info
.nchannels
== 2]
1880 [hw
->info
.swap_endianness
]
1881 [audio_bits_to_index (hw
->info
.bits
)];
1883 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1884 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1887 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1888 audio_attach_capture (s
, hw
);
1893 qemu_free (cap
->hw
.mix_buf
);
1903 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1905 struct capture_callback
*cb
;
1907 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1908 if (cb
->opaque
== cb_opaque
) {
1909 cb
->ops
.destroy (cb_opaque
);
1910 LIST_REMOVE (cb
, entries
);
1913 if (!cap
->cb_head
.lh_first
) {
1914 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1917 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1918 #ifdef DEBUG_CAPTURE
1919 dolog ("freeing %s\n", sw
->name
);
1922 sw1
= sw
->entries
.le_next
;
1924 st_rate_stop (sw
->rate
);
1927 LIST_REMOVE (sw
, entries
);
1928 LIST_REMOVE (sc
, entries
);
1932 LIST_REMOVE (cap
, entries
);
1940 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1943 sw
->vol
.mute
= mute
;
1944 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1945 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1949 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1952 sw
->vol
.mute
= mute
;
1953 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1954 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;