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 audsettings_t 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 volume_t 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
));
212 char *u
= r
+ sizeof (qemu_prefix
) - 1;
214 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
217 for (i
= 0; i
< len
; ++i
) {
218 u
[i
] = 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 term_printf ("%s: ", cap
);
336 term_vprintf (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 dolog ("Could not allocate memory for option name `%s'\n",
469 pstrcpy (optname
, optlen
, qemu_prefix
);
472 /* copy while upper-casing, including trailing zero */
473 for (i
= 0; i
<= preflen
; ++i
) {
474 optname
[i
+ sizeof (qemu_prefix
) - 1] = toupper (prefix
[i
]);
476 pstrcat (optname
, optlen
, "_");
478 pstrcat (optname
, optlen
, opt
->name
);
486 int *intp
= opt
->valp
;
487 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
493 audfmt_e
*fmtp
= opt
->valp
;
494 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
500 const char **strp
= opt
->valp
;
501 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
506 dolog ("Bad value tag for option `%s' - %d\n",
511 if (!opt
->overriddenp
) {
512 opt
->overriddenp
= &opt
->overridden
;
514 *opt
->overriddenp
= !def
;
519 static void audio_print_settings (audsettings_t
*as
)
521 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
525 AUD_log (NULL
, "S8");
528 AUD_log (NULL
, "U8");
531 AUD_log (NULL
, "S16");
534 AUD_log (NULL
, "U16");
537 AUD_log (NULL
, "S32");
540 AUD_log (NULL
, "U32");
543 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
547 AUD_log (NULL
, " endianness=");
548 switch (as
->endianness
) {
550 AUD_log (NULL
, "little");
553 AUD_log (NULL
, "big");
556 AUD_log (NULL
, "invalid");
559 AUD_log (NULL
, "\n");
562 static int audio_validate_settings (audsettings_t
*as
)
566 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
567 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
582 invalid
|= as
->freq
<= 0;
583 return invalid
? -1 : 0;
586 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, audsettings_t
*as
)
588 int bits
= 8, sign
= 0;
608 return info
->freq
== as
->freq
609 && info
->nchannels
== as
->nchannels
610 && info
->sign
== sign
611 && info
->bits
== bits
612 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
615 void audio_pcm_init_info (struct audio_pcm_info
*info
, audsettings_t
*as
)
617 int bits
= 8, sign
= 0, shift
= 0;
640 info
->freq
= as
->freq
;
643 info
->nchannels
= as
->nchannels
;
644 info
->shift
= (as
->nchannels
== 2) + shift
;
645 info
->align
= (1 << info
->shift
) - 1;
646 info
->bytes_per_second
= info
->freq
<< info
->shift
;
647 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
650 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
657 memset (buf
, 0x00, len
<< info
->shift
);
660 switch (info
->bits
) {
662 memset (buf
, 0x80, len
<< info
->shift
);
669 int shift
= info
->nchannels
- 1;
672 if (info
->swap_endianness
) {
676 for (i
= 0; i
< len
<< shift
; i
++) {
686 int shift
= info
->nchannels
- 1;
687 int32_t s
= INT32_MAX
;
689 if (info
->swap_endianness
) {
693 for (i
= 0; i
< len
<< shift
; i
++) {
700 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
710 static void noop_conv (st_sample_t
*dst
, const void *src
,
711 int samples
, volume_t
*vol
)
719 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
724 CaptureVoiceOut
*cap
;
726 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
727 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
734 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
736 struct capture_callback
*cb
;
739 dolog ("notification %d sent\n", cmd
);
741 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
742 cb
->ops
.notify (cb
->opaque
, cmd
);
746 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
748 if (cap
->hw
.enabled
!= enabled
) {
749 audcnotification_e cmd
;
750 cap
->hw
.enabled
= enabled
;
751 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
752 audio_notify_capture (cap
, cmd
);
756 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
758 HWVoiceOut
*hw
= &cap
->hw
;
762 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
768 audio_capture_maybe_changed (cap
, enabled
);
771 static void audio_detach_capture (HWVoiceOut
*hw
)
773 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
776 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
777 SWVoiceOut
*sw
= &sc
->sw
;
778 CaptureVoiceOut
*cap
= sc
->cap
;
779 int was_active
= sw
->active
;
782 st_rate_stop (sw
->rate
);
786 LIST_REMOVE (sw
, entries
);
787 LIST_REMOVE (sc
, entries
);
790 /* We have removed soft voice from the capture:
791 this might have changed the overall status of the capture
792 since this might have been the only active voice */
793 audio_recalc_and_notify_capture (cap
);
799 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
801 CaptureVoiceOut
*cap
;
803 audio_detach_capture (hw
);
804 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
807 HWVoiceOut
*hw_cap
= &cap
->hw
;
809 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
811 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
821 sw
->active
= hw
->enabled
;
822 sw
->conv
= noop_conv
;
823 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
824 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
826 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
830 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
831 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
833 asprintf (&sw
->name
, "for %p %d,%d,%d",
834 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
835 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
838 audio_capture_maybe_changed (cap
, 1);
845 * Hard voice (capture)
847 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
850 int m
= hw
->total_samples_captured
;
852 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
854 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
860 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
862 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
863 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
864 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
871 * Soft voice (capture)
873 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
875 HWVoiceIn
*hw
= sw
->hw
;
876 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
879 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
880 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
884 rpos
= hw
->wpos
- live
;
889 return hw
->samples
+ rpos
;
893 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
895 HWVoiceIn
*hw
= sw
->hw
;
896 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
897 st_sample_t
*src
, *dst
= sw
->buf
;
899 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
901 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
902 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
903 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
907 samples
= size
>> sw
->info
.shift
;
912 swlim
= (live
* sw
->ratio
) >> 32;
913 swlim
= audio_MIN (swlim
, samples
);
916 src
= hw
->conv_buf
+ rpos
;
917 isamp
= hw
->wpos
- rpos
;
920 isamp
= hw
->samples
- rpos
;
928 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
929 dolog ("osamp=%d\n", osamp
);
933 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
935 rpos
= (rpos
+ isamp
) % hw
->samples
;
941 sw
->clip (buf
, sw
->buf
, ret
);
942 sw
->total_hw_samples_acquired
+= total
;
943 return ret
<< sw
->info
.shift
;
947 * Hard voice (playback)
949 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
955 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
956 if (sw
->active
|| !sw
->empty
) {
957 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
966 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
970 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
978 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
979 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
986 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
991 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
992 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
993 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1000 * Soft voice (playback)
1002 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1004 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1005 int ret
= 0, pos
= 0, total
= 0;
1011 hwsamples
= sw
->hw
->samples
;
1013 live
= sw
->total_hw_samples_mixed
;
1014 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1015 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1019 if (live
== hwsamples
) {
1021 dolog ("%s is full %d\n", sw
->name
, live
);
1026 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1027 samples
= size
>> sw
->info
.shift
;
1029 dead
= hwsamples
- live
;
1030 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1031 swlim
= audio_MIN (swlim
, samples
);
1033 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1037 dead
= hwsamples
- live
;
1038 left
= hwsamples
- wpos
;
1039 blck
= audio_MIN (dead
, left
);
1048 sw
->hw
->mix_buf
+ wpos
,
1056 wpos
= (wpos
+ osamp
) % hwsamples
;
1060 sw
->total_hw_samples_mixed
+= total
;
1061 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1065 "%s: write size %d ret %d total sw %d\n",
1067 size
>> sw
->info
.shift
,
1069 sw
->total_hw_samples_mixed
1073 return ret
<< sw
->info
.shift
;
1077 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1079 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1080 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1085 #include "audio_template.h"
1087 #include "audio_template.h"
1089 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1094 /* XXX: Consider options */
1098 if (!sw
->hw
->enabled
) {
1099 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1103 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1107 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1112 /* XXX: Consider options */
1116 if (!sw
->hw
->enabled
) {
1117 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1121 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1125 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1127 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1130 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1139 if (sw
->active
!= on
) {
1140 SWVoiceOut
*temp_sw
;
1144 hw
->pending_disable
= 0;
1147 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1154 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1155 temp_sw
= temp_sw
->entries
.le_next
) {
1156 nb_active
+= temp_sw
->active
!= 0;
1159 hw
->pending_disable
= nb_active
== 1;
1163 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1164 sc
->sw
.active
= hw
->enabled
;
1166 audio_capture_maybe_changed (sc
->cap
, 1);
1173 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1182 if (sw
->active
!= on
) {
1188 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1190 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1196 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1197 temp_sw
= temp_sw
->entries
.le_next
) {
1198 nb_active
+= temp_sw
->active
!= 0;
1201 if (nb_active
== 1) {
1203 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1211 static int audio_get_avail (SWVoiceIn
*sw
)
1219 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1220 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1221 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1226 "%s: get_avail live %d ret %" PRId64
"\n",
1228 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1231 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1234 static int audio_get_free (SWVoiceOut
*sw
)
1242 live
= sw
->total_hw_samples_mixed
;
1244 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1245 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1249 dead
= sw
->hw
->samples
- live
;
1252 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1254 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1257 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1260 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1267 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1268 SWVoiceOut
*sw
= &sc
->sw
;
1273 int till_end_of_hw
= hw
->samples
- rpos2
;
1274 int to_write
= audio_MIN (till_end_of_hw
, n
);
1275 int bytes
= to_write
<< hw
->info
.shift
;
1278 sw
->buf
= hw
->mix_buf
+ rpos2
;
1279 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1280 if (written
- bytes
) {
1281 dolog ("Could not mix %d bytes into a capture "
1282 "buffer, mixed %d\n",
1287 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1292 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1293 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1294 mixeng_clear (hw
->mix_buf
, samples
- n
);
1297 static void audio_run_out (AudioState
*s
)
1299 HWVoiceOut
*hw
= NULL
;
1302 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1304 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1306 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1311 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1312 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1316 if (hw
->pending_disable
&& !nb_live
) {
1319 dolog ("Disabling voice\n");
1322 hw
->pending_disable
= 0;
1323 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1324 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1326 audio_recalc_and_notify_capture (sc
->cap
);
1332 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1334 free
= audio_get_free (sw
);
1336 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1343 prev_rpos
= hw
->rpos
;
1344 played
= hw
->pcm_ops
->run_out (hw
);
1345 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1346 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1347 hw
->rpos
, hw
->samples
, played
);
1352 dolog ("played=%d\n", played
);
1356 hw
->ts_helper
+= played
;
1357 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1360 cleanup_required
= 0;
1361 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1362 if (!sw
->active
&& sw
->empty
) {
1366 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1367 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1368 played
, sw
->total_hw_samples_mixed
);
1369 played
= sw
->total_hw_samples_mixed
;
1372 sw
->total_hw_samples_mixed
-= played
;
1374 if (!sw
->total_hw_samples_mixed
) {
1376 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1380 free
= audio_get_free (sw
);
1382 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1387 if (cleanup_required
) {
1390 sw
= hw
->sw_head
.lh_first
;
1392 sw1
= sw
->entries
.le_next
;
1393 if (!sw
->active
&& !sw
->callback
.fn
) {
1395 dolog ("Finishing with old voice\n");
1397 audio_close_out (s
, sw
);
1405 static void audio_run_in (AudioState
*s
)
1407 HWVoiceIn
*hw
= NULL
;
1409 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1413 captured
= hw
->pcm_ops
->run_in (hw
);
1415 min
= audio_pcm_hw_find_min_in (hw
);
1416 hw
->total_samples_captured
+= captured
- min
;
1417 hw
->ts_helper
+= captured
;
1419 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1420 sw
->total_hw_samples_acquired
-= min
;
1425 avail
= audio_get_avail (sw
);
1427 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1434 static void audio_run_capture (AudioState
*s
)
1436 CaptureVoiceOut
*cap
;
1438 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1439 int live
, rpos
, captured
;
1440 HWVoiceOut
*hw
= &cap
->hw
;
1443 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1446 int left
= hw
->samples
- rpos
;
1447 int to_capture
= audio_MIN (live
, left
);
1449 struct capture_callback
*cb
;
1451 src
= hw
->mix_buf
+ rpos
;
1452 hw
->clip (cap
->buf
, src
, to_capture
);
1453 mixeng_clear (src
, to_capture
);
1455 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1456 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1457 to_capture
<< hw
->info
.shift
);
1459 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1464 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1465 if (!sw
->active
&& sw
->empty
) {
1469 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1470 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1471 captured
, sw
->total_hw_samples_mixed
);
1472 captured
= sw
->total_hw_samples_mixed
;
1475 sw
->total_hw_samples_mixed
-= captured
;
1476 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1481 static void audio_timer (void *opaque
)
1483 AudioState
*s
= opaque
;
1487 audio_run_capture (s
);
1489 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1492 static struct audio_option audio_options
[] = {
1494 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1495 "Use fixed settings for host DAC", NULL
, 0},
1497 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1498 "Frequency for fixed host DAC", NULL
, 0},
1500 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1501 "Format for fixed host DAC", NULL
, 0},
1503 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1504 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1506 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1507 "Number of voices for DAC", NULL
, 0},
1510 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1511 "Use fixed settings for host ADC", NULL
, 0},
1513 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1514 "Frequency for fixed host ADC", NULL
, 0},
1516 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1517 "Format for fixed host ADC", NULL
, 0},
1519 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1520 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1522 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1523 "Number of voices for ADC", NULL
, 0},
1526 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hz
,
1527 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1529 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1530 "(undocumented)", NULL
, 0},
1532 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1533 "print logging messages to monitor instead of stderr", NULL
, 0},
1535 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1538 static void audio_pp_nb_voices (const char *typ
, int nb
)
1542 printf ("Does not support %s\n", typ
);
1545 printf ("One %s voice\n", typ
);
1548 printf ("Theoretically supports many %s voices\n", typ
);
1551 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1557 void AUD_help (void)
1561 audio_process_options ("AUDIO", audio_options
);
1562 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1563 struct audio_driver
*d
= drvtab
[i
];
1565 audio_process_options (d
->name
, d
->options
);
1569 printf ("Audio options:\n");
1570 audio_print_options ("AUDIO", audio_options
);
1573 printf ("Available drivers:\n");
1575 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1576 struct audio_driver
*d
= drvtab
[i
];
1578 printf ("Name: %s\n", d
->name
);
1579 printf ("Description: %s\n", d
->descr
);
1581 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1582 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1585 printf ("Options:\n");
1586 audio_print_options (d
->name
, d
->options
);
1589 printf ("No options\n");
1595 "Options are settable through environment variables.\n"
1598 " set QEMU_AUDIO_DRV=wav\n"
1599 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1601 " export QEMU_AUDIO_DRV=wav\n"
1602 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1603 "(for csh replace export with setenv in the above)\n"
1609 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1612 audio_process_options (drv
->name
, drv
->options
);
1614 s
->drv_opaque
= drv
->init ();
1616 if (s
->drv_opaque
) {
1617 audio_init_nb_voices_out (s
, drv
);
1618 audio_init_nb_voices_in (s
, drv
);
1623 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1628 static void audio_vm_change_state_handler (void *opaque
, int running
)
1630 AudioState
*s
= opaque
;
1631 HWVoiceOut
*hwo
= NULL
;
1632 HWVoiceIn
*hwi
= NULL
;
1633 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1635 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1636 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1639 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1640 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1644 static void audio_atexit (void)
1646 AudioState
*s
= &glob_audio_state
;
1647 HWVoiceOut
*hwo
= NULL
;
1648 HWVoiceIn
*hwi
= NULL
;
1650 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1653 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1654 hwo
->pcm_ops
->fini_out (hwo
);
1656 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1657 CaptureVoiceOut
*cap
= sc
->cap
;
1658 struct capture_callback
*cb
;
1660 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1661 cb
->ops
.destroy (cb
->opaque
);
1666 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1667 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1668 hwi
->pcm_ops
->fini_in (hwi
);
1672 s
->drv
->fini (s
->drv_opaque
);
1676 static void audio_save (QEMUFile
*f
, void *opaque
)
1682 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1687 if (version_id
!= 1) {
1694 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1697 card
->name
= qemu_strdup (name
);
1698 memset (&card
->entries
, 0, sizeof (card
->entries
));
1699 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1702 void AUD_remove_card (QEMUSoundCard
*card
)
1704 LIST_REMOVE (card
, entries
);
1706 qemu_free (card
->name
);
1709 AudioState
*AUD_init (void)
1713 const char *drvname
;
1714 AudioState
*s
= &glob_audio_state
;
1716 LIST_INIT (&s
->hw_head_out
);
1717 LIST_INIT (&s
->hw_head_in
);
1718 LIST_INIT (&s
->cap_head
);
1719 atexit (audio_atexit
);
1721 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1723 dolog ("Could not create audio timer\n");
1727 audio_process_options ("AUDIO", audio_options
);
1729 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1730 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1732 if (s
->nb_hw_voices_out
<= 0) {
1733 dolog ("Bogus number of playback voices %d, setting to 1\n",
1734 s
->nb_hw_voices_out
);
1735 s
->nb_hw_voices_out
= 1;
1738 if (s
->nb_hw_voices_in
<= 0) {
1739 dolog ("Bogus number of capture voices %d, setting to 0\n",
1740 s
->nb_hw_voices_in
);
1741 s
->nb_hw_voices_in
= 0;
1746 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1752 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1753 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1754 done
= !audio_driver_init (s
, drvtab
[i
]);
1761 dolog ("Unknown audio driver `%s'\n", drvname
);
1762 dolog ("Run with -audio-help to list available drivers\n");
1767 for (i
= 0; !done
&& i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1768 if (drvtab
[i
]->can_be_default
) {
1769 done
= !audio_driver_init (s
, drvtab
[i
]);
1775 done
= !audio_driver_init (s
, &no_audio_driver
);
1777 dolog ("Could not initialize audio subsystem\n");
1780 dolog ("warning: Using timer based audio emulation\n");
1785 VMChangeStateEntry
*e
;
1787 if (conf
.period
.hz
<= 0) {
1788 if (conf
.period
.hz
< 0) {
1789 dolog ("warning: Timer period is negative - %d "
1790 "treating as zero\n",
1793 conf
.period
.ticks
= 1;
1796 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hz
;
1799 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1801 dolog ("warning: Could not register change state handler\n"
1802 "(Audio can continue looping even after stopping the VM)\n");
1806 qemu_del_timer (s
->ts
);
1810 LIST_INIT (&s
->card_head
);
1811 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1812 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1816 CaptureVoiceOut
*AUD_add_capture (
1819 struct audio_capture_ops
*ops
,
1823 CaptureVoiceOut
*cap
;
1824 struct capture_callback
*cb
;
1828 s
= &glob_audio_state
;
1831 if (audio_validate_settings (as
)) {
1832 dolog ("Invalid settings were passed when trying to add capture\n");
1833 audio_print_settings (as
);
1837 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1839 dolog ("Could not allocate capture callback information, size %zu\n",
1844 cb
->opaque
= cb_opaque
;
1846 cap
= audio_pcm_capture_find_specific (s
, as
);
1848 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1853 CaptureVoiceOut
*cap
;
1855 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1857 dolog ("Could not allocate capture voice, size %zu\n",
1863 LIST_INIT (&hw
->sw_head
);
1864 LIST_INIT (&cap
->cb_head
);
1866 /* XXX find a more elegant way */
1867 hw
->samples
= 4096 * 4;
1868 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1869 sizeof (st_sample_t
));
1871 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1876 audio_pcm_init_info (&hw
->info
, as
);
1878 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1880 dolog ("Could not allocate capture buffer "
1881 "(%d samples, each %d bytes)\n",
1882 hw
->samples
, 1 << hw
->info
.shift
);
1886 hw
->clip
= mixeng_clip
1887 [hw
->info
.nchannels
== 2]
1889 [hw
->info
.swap_endianness
]
1890 [audio_bits_to_index (hw
->info
.bits
)];
1892 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1893 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1896 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1897 audio_attach_capture (s
, hw
);
1902 qemu_free (cap
->hw
.mix_buf
);
1912 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1914 struct capture_callback
*cb
;
1916 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1917 if (cb
->opaque
== cb_opaque
) {
1918 cb
->ops
.destroy (cb_opaque
);
1919 LIST_REMOVE (cb
, entries
);
1922 if (!cap
->cb_head
.lh_first
) {
1923 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1926 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1927 #ifdef DEBUG_CAPTURE
1928 dolog ("freeing %s\n", sw
->name
);
1931 sw1
= sw
->entries
.le_next
;
1933 st_rate_stop (sw
->rate
);
1936 LIST_REMOVE (sw
, entries
);
1937 LIST_REMOVE (sc
, entries
);
1941 LIST_REMOVE (cap
, entries
);
1949 void AUD_set_volume_out (SWVoiceOut
*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;
1958 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1961 sw
->vol
.mute
= mute
;
1962 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1963 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;