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
[] = {
47 #ifdef CONFIG_COREAUDIO
48 &coreaudio_audio_driver
,
63 struct fixed_settings
{
67 audsettings_t settings
;
71 struct fixed_settings fixed_out
;
72 struct fixed_settings fixed_in
;
80 { /* DAC fixed settings */
87 AUD_FMT_S16
, /* fmt */
92 { /* ADC fixed settings */
99 AUD_FMT_S16
, /* fmt */
100 AUDIO_HOST_ENDIANNESS
106 0 /* log_to_monitor */
109 static AudioState glob_audio_state
;
111 volume_t nominal_volume
= {
122 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
123 /* http://www.multi-platforms.com/Tips/PopCount.htm */
124 uint32_t popcount (uint32_t u
)
126 u
= ((u
&0x55555555) + ((u
>>1)&0x55555555));
127 u
= ((u
&0x33333333) + ((u
>>2)&0x33333333));
128 u
= ((u
&0x0f0f0f0f) + ((u
>>4)&0x0f0f0f0f));
129 u
= ((u
&0x00ff00ff) + ((u
>>8)&0x00ff00ff));
130 u
= ( u
&0x0000ffff) + (u
>>16);
134 inline uint32_t lsbindex (uint32_t u
)
136 return popcount ((u
&-u
)-1);
139 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
142 int audio_bug (const char *funcname
, int cond
)
147 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
150 AUD_log (NULL
, "Save all your work and restart without audio\n");
151 AUD_log (NULL
, "Please send bug report to malc@pulsesoft.com\n");
152 AUD_log (NULL
, "I am sorry\n");
154 AUD_log (NULL
, "Context:\n");
156 #if defined AUDIO_BREAKPOINT_ON_BUG
157 # if defined HOST_I386
158 # if defined __GNUC__
160 # elif defined _MSC_VER
175 static inline int audio_bits_to_index (int bits
)
188 audio_bug ("bits_to_index", 1);
189 AUD_log (NULL
, "invalid bits %d\n", bits
);
194 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
200 cond
= !nmemb
|| !size
;
204 if (audio_bug ("audio_calloc", cond
)) {
205 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
207 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
211 return qemu_mallocz (len
);
214 static char *audio_alloc_prefix (const char *s
)
216 const char qemu_prefix
[] = "QEMU_";
225 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
229 char *u
= r
+ sizeof (qemu_prefix
) - 1;
231 strcpy (r
, qemu_prefix
);
234 for (i
= 0; i
< len
; ++i
) {
235 u
[i
] = toupper (u
[i
]);
241 static const char *audio_audfmt_to_string (audfmt_e fmt
)
263 dolog ("Bogus audfmt %d returning S16\n", fmt
);
267 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
270 if (!strcasecmp (s
, "u8")) {
274 else if (!strcasecmp (s
, "u16")) {
278 else if (!strcasecmp (s
, "u32")) {
282 else if (!strcasecmp (s
, "s8")) {
286 else if (!strcasecmp (s
, "s16")) {
290 else if (!strcasecmp (s
, "s32")) {
295 dolog ("Bogus audio format `%s' using %s\n",
296 s
, audio_audfmt_to_string (defval
));
302 static audfmt_e
audio_get_conf_fmt (const char *envname
,
306 const char *var
= getenv (envname
);
311 return audio_string_to_audfmt (var
, defval
, defaultp
);
314 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
319 strval
= getenv (key
);
331 static const char *audio_get_conf_str (const char *key
,
335 const char *val
= getenv (key
);
346 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
348 if (conf
.log_to_monitor
) {
350 term_printf ("%s: ", cap
);
353 term_vprintf (fmt
, ap
);
357 fprintf (stderr
, "%s: ", cap
);
360 vfprintf (stderr
, fmt
, ap
);
364 void AUD_log (const char *cap
, const char *fmt
, ...)
369 AUD_vlog (cap
, fmt
, ap
);
373 static void audio_print_options (const char *prefix
,
374 struct audio_option
*opt
)
379 dolog ("No prefix specified\n");
384 dolog ("No options\n");
388 uprefix
= audio_alloc_prefix (prefix
);
390 for (; opt
->name
; opt
++) {
391 const char *state
= "default";
392 printf (" %s_%s: ", uprefix
, opt
->name
);
394 if (opt
->overriddenp
&& *opt
->overriddenp
) {
401 int *intp
= opt
->valp
;
402 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
408 int *intp
= opt
->valp
;
409 printf ("integer, %s = %d\n", state
, *intp
);
415 audfmt_e
*fmtp
= opt
->valp
;
417 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
419 audio_audfmt_to_string (*fmtp
)
426 const char **strp
= opt
->valp
;
427 printf ("string, %s = %s\n",
429 *strp
? *strp
: "(not set)");
435 dolog ("Bad value tag for option %s_%s %d\n",
436 uprefix
, opt
->name
, opt
->tag
);
439 printf (" %s\n", opt
->descr
);
445 static void audio_process_options (const char *prefix
,
446 struct audio_option
*opt
)
449 const char qemu_prefix
[] = "QEMU_";
452 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
453 dolog ("prefix = NULL\n");
457 if (audio_bug (AUDIO_FUNC
, !opt
)) {
458 dolog ("opt = NULL\n");
462 preflen
= strlen (prefix
);
464 for (; opt
->name
; opt
++) {
469 dolog ("Option value pointer for `%s' is not set\n",
474 len
= strlen (opt
->name
);
475 /* len of opt->name + len of prefix + size of qemu_prefix
476 * (includes trailing zero) + zero + underscore (on behalf of
478 optname
= qemu_malloc (len
+ preflen
+ sizeof (qemu_prefix
) + 1);
480 dolog ("Could not allocate memory for option name `%s'\n",
485 strcpy (optname
, qemu_prefix
);
487 /* copy while upper-casing, including trailing zero */
488 for (i
= 0; i
<= preflen
; ++i
) {
489 optname
[i
+ sizeof (qemu_prefix
) - 1] = toupper (prefix
[i
]);
491 strcat (optname
, "_");
492 strcat (optname
, opt
->name
);
499 int *intp
= opt
->valp
;
500 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
506 audfmt_e
*fmtp
= opt
->valp
;
507 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
513 const char **strp
= opt
->valp
;
514 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
519 dolog ("Bad value tag for option `%s' - %d\n",
524 if (!opt
->overriddenp
) {
525 opt
->overriddenp
= &opt
->overridden
;
527 *opt
->overriddenp
= !def
;
532 static void audio_print_settings (audsettings_t
*as
)
534 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
538 AUD_log (NULL
, "S8");
541 AUD_log (NULL
, "U8");
544 AUD_log (NULL
, "S16");
547 AUD_log (NULL
, "U16");
550 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
554 AUD_log (NULL
, " endianness=");
555 switch (as
->endianness
) {
557 AUD_log (NULL
, "little");
560 AUD_log (NULL
, "big");
563 AUD_log (NULL
, "invalid");
566 AUD_log (NULL
, "\n");
569 static int audio_validate_settings (audsettings_t
*as
)
573 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
574 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
589 invalid
|= as
->freq
<= 0;
590 return invalid
? -1 : 0;
593 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, audsettings_t
*as
)
595 int bits
= 8, sign
= 0;
615 return info
->freq
== as
->freq
616 && info
->nchannels
== as
->nchannels
617 && info
->sign
== sign
618 && info
->bits
== bits
619 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
622 void audio_pcm_init_info (struct audio_pcm_info
*info
, audsettings_t
*as
)
624 int bits
= 8, sign
= 0, shift
= 0;
647 info
->freq
= as
->freq
;
650 info
->nchannels
= as
->nchannels
;
651 info
->shift
= (as
->nchannels
== 2) + shift
;
652 info
->align
= (1 << info
->shift
) - 1;
653 info
->bytes_per_second
= info
->freq
<< info
->shift
;
654 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
657 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
664 memset (buf
, 0x00, len
<< info
->shift
);
667 switch (info
->bits
) {
669 memset (buf
, 0x80, len
<< info
->shift
);
676 int shift
= info
->nchannels
- 1;
679 if (info
->swap_endianness
) {
683 for (i
= 0; i
< len
<< shift
; i
++) {
693 int shift
= info
->nchannels
- 1;
694 int32_t s
= INT32_MAX
;
696 if (info
->swap_endianness
) {
700 for (i
= 0; i
< len
<< shift
; i
++) {
707 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
717 static void noop_conv (st_sample_t
*dst
, const void *src
,
718 int samples
, volume_t
*vol
)
726 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
731 CaptureVoiceOut
*cap
;
733 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
734 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
741 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
743 struct capture_callback
*cb
;
746 dolog ("notification %d sent\n", cmd
);
748 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
749 cb
->ops
.notify (cb
->opaque
, cmd
);
753 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
755 if (cap
->hw
.enabled
!= enabled
) {
756 audcnotification_e cmd
;
757 cap
->hw
.enabled
= enabled
;
758 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
759 audio_notify_capture (cap
, cmd
);
763 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
765 HWVoiceOut
*hw
= &cap
->hw
;
769 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
775 audio_capture_maybe_changed (cap
, enabled
);
778 static void audio_detach_capture (HWVoiceOut
*hw
)
780 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
783 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
784 SWVoiceOut
*sw
= &sc
->sw
;
785 CaptureVoiceOut
*cap
= sc
->cap
;
786 int was_active
= sw
->active
;
789 st_rate_stop (sw
->rate
);
793 LIST_REMOVE (sw
, entries
);
794 LIST_REMOVE (sc
, entries
);
797 /* We have removed soft voice from the capture:
798 this might have changed the overall status of the capture
799 since this might have been the only active voice */
800 audio_recalc_and_notify_capture (cap
);
806 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
808 CaptureVoiceOut
*cap
;
810 audio_detach_capture (hw
);
811 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
814 HWVoiceOut
*hw_cap
= &cap
->hw
;
816 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
818 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
828 sw
->active
= hw
->enabled
;
829 sw
->conv
= noop_conv
;
830 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
831 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
833 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
837 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
838 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
840 asprintf (&sw
->name
, "for %p %d,%d,%d",
841 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
842 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
845 audio_capture_maybe_changed (cap
, 1);
852 * Hard voice (capture)
854 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
857 int m
= hw
->total_samples_captured
;
859 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
861 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
867 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
869 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
870 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
871 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
878 * Soft voice (capture)
880 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
882 HWVoiceIn
*hw
= sw
->hw
;
883 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
886 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
887 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
891 rpos
= hw
->wpos
- live
;
896 return hw
->samples
+ rpos
;
900 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
902 HWVoiceIn
*hw
= sw
->hw
;
903 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
904 st_sample_t
*src
, *dst
= sw
->buf
;
906 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
908 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
909 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
910 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
914 samples
= size
>> sw
->info
.shift
;
919 swlim
= (live
* sw
->ratio
) >> 32;
920 swlim
= audio_MIN (swlim
, samples
);
923 src
= hw
->conv_buf
+ rpos
;
924 isamp
= hw
->wpos
- rpos
;
927 isamp
= hw
->samples
- rpos
;
935 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
936 dolog ("osamp=%d\n", osamp
);
940 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
942 rpos
= (rpos
+ isamp
) % hw
->samples
;
948 sw
->clip (buf
, sw
->buf
, ret
);
949 sw
->total_hw_samples_acquired
+= total
;
950 return ret
<< sw
->info
.shift
;
954 * Hard voice (playback)
956 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
962 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
963 if (sw
->active
|| !sw
->empty
) {
964 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
973 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
977 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
985 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
986 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
993 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
998 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
999 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1000 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1007 * Soft voice (playback)
1009 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1011 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1012 int ret
= 0, pos
= 0, total
= 0;
1018 hwsamples
= sw
->hw
->samples
;
1020 live
= sw
->total_hw_samples_mixed
;
1021 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1022 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1026 if (live
== hwsamples
) {
1028 dolog ("%s is full %d\n", sw
->name
, live
);
1033 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1034 samples
= size
>> sw
->info
.shift
;
1036 dead
= hwsamples
- live
;
1037 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1038 swlim
= audio_MIN (swlim
, samples
);
1040 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1044 dead
= hwsamples
- live
;
1045 left
= hwsamples
- wpos
;
1046 blck
= audio_MIN (dead
, left
);
1055 sw
->hw
->mix_buf
+ wpos
,
1063 wpos
= (wpos
+ osamp
) % hwsamples
;
1067 sw
->total_hw_samples_mixed
+= total
;
1068 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1072 "%s: write size %d ret %d total sw %d\n",
1074 size
>> sw
->info
.shift
,
1076 sw
->total_hw_samples_mixed
1080 return ret
<< sw
->info
.shift
;
1084 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1086 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1087 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1092 #include "audio_template.h"
1094 #include "audio_template.h"
1096 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1101 /* XXX: Consider options */
1105 if (!sw
->hw
->enabled
) {
1106 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1110 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1114 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1119 /* XXX: Consider options */
1123 if (!sw
->hw
->enabled
) {
1124 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1128 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1132 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1134 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1137 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1146 if (sw
->active
!= on
) {
1147 SWVoiceOut
*temp_sw
;
1151 hw
->pending_disable
= 0;
1154 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1161 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1162 temp_sw
= temp_sw
->entries
.le_next
) {
1163 nb_active
+= temp_sw
->active
!= 0;
1166 hw
->pending_disable
= nb_active
== 1;
1170 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1171 sc
->sw
.active
= hw
->enabled
;
1173 audio_capture_maybe_changed (sc
->cap
, 1);
1180 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1189 if (sw
->active
!= on
) {
1195 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1197 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1203 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1204 temp_sw
= temp_sw
->entries
.le_next
) {
1205 nb_active
+= temp_sw
->active
!= 0;
1208 if (nb_active
== 1) {
1210 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1218 static int audio_get_avail (SWVoiceIn
*sw
)
1226 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1227 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1228 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1233 "%s: get_avail live %d ret %" PRId64
"\n",
1235 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1238 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1241 static int audio_get_free (SWVoiceOut
*sw
)
1249 live
= sw
->total_hw_samples_mixed
;
1251 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1252 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1256 dead
= sw
->hw
->samples
- live
;
1259 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1261 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1264 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1267 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1274 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1275 SWVoiceOut
*sw
= &sc
->sw
;
1280 int till_end_of_hw
= hw
->samples
- rpos2
;
1281 int to_write
= audio_MIN (till_end_of_hw
, n
);
1282 int bytes
= to_write
<< hw
->info
.shift
;
1285 sw
->buf
= hw
->mix_buf
+ rpos2
;
1286 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1287 if (written
- bytes
) {
1288 dolog ("Could not mix %d bytes into a capture "
1289 "buffer, mixed %d\n",
1294 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1299 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1300 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1301 mixeng_clear (hw
->mix_buf
, samples
- n
);
1304 static void audio_run_out (AudioState
*s
)
1306 HWVoiceOut
*hw
= NULL
;
1309 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1311 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1313 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1318 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1319 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1323 if (hw
->pending_disable
&& !nb_live
) {
1326 dolog ("Disabling voice\n");
1329 hw
->pending_disable
= 0;
1330 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1331 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1333 audio_recalc_and_notify_capture (sc
->cap
);
1339 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1341 free
= audio_get_free (sw
);
1343 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1350 prev_rpos
= hw
->rpos
;
1351 played
= hw
->pcm_ops
->run_out (hw
);
1352 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1353 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1354 hw
->rpos
, hw
->samples
, played
);
1359 dolog ("played=%d\n", played
);
1363 hw
->ts_helper
+= played
;
1364 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1367 cleanup_required
= 0;
1368 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1369 if (!sw
->active
&& sw
->empty
) {
1373 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1374 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1375 played
, sw
->total_hw_samples_mixed
);
1376 played
= sw
->total_hw_samples_mixed
;
1379 sw
->total_hw_samples_mixed
-= played
;
1381 if (!sw
->total_hw_samples_mixed
) {
1383 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1387 free
= audio_get_free (sw
);
1389 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1394 if (cleanup_required
) {
1397 sw
= hw
->sw_head
.lh_first
;
1399 sw1
= sw
->entries
.le_next
;
1400 if (!sw
->active
&& !sw
->callback
.fn
) {
1402 dolog ("Finishing with old voice\n");
1404 audio_close_out (s
, sw
);
1412 static void audio_run_in (AudioState
*s
)
1414 HWVoiceIn
*hw
= NULL
;
1416 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1420 captured
= hw
->pcm_ops
->run_in (hw
);
1422 min
= audio_pcm_hw_find_min_in (hw
);
1423 hw
->total_samples_captured
+= captured
- min
;
1424 hw
->ts_helper
+= captured
;
1426 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1427 sw
->total_hw_samples_acquired
-= min
;
1432 avail
= audio_get_avail (sw
);
1434 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1441 static void audio_run_capture (AudioState
*s
)
1443 CaptureVoiceOut
*cap
;
1445 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1446 int live
, rpos
, captured
;
1447 HWVoiceOut
*hw
= &cap
->hw
;
1450 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1453 int left
= hw
->samples
- rpos
;
1454 int to_capture
= audio_MIN (live
, left
);
1456 struct capture_callback
*cb
;
1458 src
= hw
->mix_buf
+ rpos
;
1459 hw
->clip (cap
->buf
, src
, to_capture
);
1460 mixeng_clear (src
, to_capture
);
1462 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1463 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1464 to_capture
<< hw
->info
.shift
);
1466 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1471 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1472 if (!sw
->active
&& sw
->empty
) {
1476 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1477 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1478 captured
, sw
->total_hw_samples_mixed
);
1479 captured
= sw
->total_hw_samples_mixed
;
1482 sw
->total_hw_samples_mixed
-= captured
;
1483 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1488 static void audio_timer (void *opaque
)
1490 AudioState
*s
= opaque
;
1494 audio_run_capture (s
);
1496 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1499 static struct audio_option audio_options
[] = {
1501 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1502 "Use fixed settings for host DAC", NULL
, 0},
1504 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1505 "Frequency for fixed host DAC", NULL
, 0},
1507 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1508 "Format for fixed host DAC", NULL
, 0},
1510 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1511 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1513 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1514 "Number of voices for DAC", NULL
, 0},
1517 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1518 "Use fixed settings for host ADC", NULL
, 0},
1520 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1521 "Frequency for fixed host ADC", NULL
, 0},
1523 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1524 "Format for fixed host ADC", NULL
, 0},
1526 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1527 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1529 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1530 "Number of voices for ADC", NULL
, 0},
1533 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hz
,
1534 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1536 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1537 "(undocumented)", NULL
, 0},
1539 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1540 "print logging messages to montior instead of stderr", NULL
, 0},
1542 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1545 static void audio_pp_nb_voices (const char *typ
, int nb
)
1549 printf ("Does not support %s\n", typ
);
1552 printf ("One %s voice\n", typ
);
1555 printf ("Theoretically supports many %s voices\n", typ
);
1558 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1564 void AUD_help (void)
1568 audio_process_options ("AUDIO", audio_options
);
1569 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1570 struct audio_driver
*d
= drvtab
[i
];
1572 audio_process_options (d
->name
, d
->options
);
1576 printf ("Audio options:\n");
1577 audio_print_options ("AUDIO", audio_options
);
1580 printf ("Available drivers:\n");
1582 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1583 struct audio_driver
*d
= drvtab
[i
];
1585 printf ("Name: %s\n", d
->name
);
1586 printf ("Description: %s\n", d
->descr
);
1588 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1589 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1592 printf ("Options:\n");
1593 audio_print_options (d
->name
, d
->options
);
1596 printf ("No options\n");
1602 "Options are settable through environment variables.\n"
1605 " set QEMU_AUDIO_DRV=wav\n"
1606 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1608 " export QEMU_AUDIO_DRV=wav\n"
1609 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1610 "(for csh replace export with setenv in the above)\n"
1616 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1619 audio_process_options (drv
->name
, drv
->options
);
1621 s
->drv_opaque
= drv
->init ();
1623 if (s
->drv_opaque
) {
1624 audio_init_nb_voices_out (s
, drv
);
1625 audio_init_nb_voices_in (s
, drv
);
1630 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1635 static void audio_vm_change_state_handler (void *opaque
, int running
)
1637 AudioState
*s
= opaque
;
1638 HWVoiceOut
*hwo
= NULL
;
1639 HWVoiceIn
*hwi
= NULL
;
1640 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1642 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1643 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1646 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1647 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1651 static void audio_atexit (void)
1653 AudioState
*s
= &glob_audio_state
;
1654 HWVoiceOut
*hwo
= NULL
;
1655 HWVoiceIn
*hwi
= NULL
;
1657 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1660 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1661 hwo
->pcm_ops
->fini_out (hwo
);
1663 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1664 CaptureVoiceOut
*cap
= sc
->cap
;
1665 struct capture_callback
*cb
;
1667 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1668 cb
->ops
.destroy (cb
->opaque
);
1673 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1674 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1675 hwi
->pcm_ops
->fini_in (hwi
);
1679 s
->drv
->fini (s
->drv_opaque
);
1683 static void audio_save (QEMUFile
*f
, void *opaque
)
1689 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1694 if (version_id
!= 1) {
1701 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1704 card
->name
= qemu_strdup (name
);
1705 memset (&card
->entries
, 0, sizeof (card
->entries
));
1706 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1709 void AUD_remove_card (QEMUSoundCard
*card
)
1711 LIST_REMOVE (card
, entries
);
1713 qemu_free (card
->name
);
1716 AudioState
*AUD_init (void)
1720 const char *drvname
;
1721 AudioState
*s
= &glob_audio_state
;
1723 LIST_INIT (&s
->hw_head_out
);
1724 LIST_INIT (&s
->hw_head_in
);
1725 LIST_INIT (&s
->cap_head
);
1726 atexit (audio_atexit
);
1728 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1730 dolog ("Could not create audio timer\n");
1734 audio_process_options ("AUDIO", audio_options
);
1736 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1737 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1739 if (s
->nb_hw_voices_out
<= 0) {
1740 dolog ("Bogus number of playback voices %d, setting to 1\n",
1741 s
->nb_hw_voices_out
);
1742 s
->nb_hw_voices_out
= 1;
1745 if (s
->nb_hw_voices_in
<= 0) {
1746 dolog ("Bogus number of capture voices %d, setting to 0\n",
1747 s
->nb_hw_voices_in
);
1748 s
->nb_hw_voices_in
= 0;
1753 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1759 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1760 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1761 done
= !audio_driver_init (s
, drvtab
[i
]);
1768 dolog ("Unknown audio driver `%s'\n", drvname
);
1769 dolog ("Run with -audio-help to list available drivers\n");
1774 for (i
= 0; !done
&& i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1775 if (drvtab
[i
]->can_be_default
) {
1776 done
= !audio_driver_init (s
, drvtab
[i
]);
1782 done
= !audio_driver_init (s
, &no_audio_driver
);
1784 dolog ("Could not initialize audio subsystem\n");
1787 dolog ("warning: Using timer based audio emulation\n");
1792 VMChangeStateEntry
*e
;
1794 if (conf
.period
.hz
<= 0) {
1795 if (conf
.period
.hz
< 0) {
1796 dolog ("warning: Timer period is negative - %d "
1797 "treating as zero\n",
1800 conf
.period
.ticks
= 1;
1803 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hz
;
1806 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1808 dolog ("warning: Could not register change state handler\n"
1809 "(Audio can continue looping even after stopping the VM)\n");
1813 qemu_del_timer (s
->ts
);
1817 LIST_INIT (&s
->card_head
);
1818 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1819 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1823 CaptureVoiceOut
*AUD_add_capture (
1826 struct audio_capture_ops
*ops
,
1830 CaptureVoiceOut
*cap
;
1831 struct capture_callback
*cb
;
1835 s
= &glob_audio_state
;
1838 if (audio_validate_settings (as
)) {
1839 dolog ("Invalid settings were passed when trying to add capture\n");
1840 audio_print_settings (as
);
1844 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1846 dolog ("Could not allocate capture callback information, size %zu\n",
1851 cb
->opaque
= cb_opaque
;
1853 cap
= audio_pcm_capture_find_specific (s
, as
);
1855 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1860 CaptureVoiceOut
*cap
;
1862 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1864 dolog ("Could not allocate capture voice, size %zu\n",
1870 LIST_INIT (&hw
->sw_head
);
1871 LIST_INIT (&cap
->cb_head
);
1873 /* XXX find a more elegant way */
1874 hw
->samples
= 4096 * 4;
1875 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1876 sizeof (st_sample_t
));
1878 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1883 audio_pcm_init_info (&hw
->info
, as
);
1885 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1887 dolog ("Could not allocate capture buffer "
1888 "(%d samples, each %d bytes)\n",
1889 hw
->samples
, 1 << hw
->info
.shift
);
1893 hw
->clip
= mixeng_clip
1894 [hw
->info
.nchannels
== 2]
1896 [hw
->info
.swap_endianness
]
1897 [audio_bits_to_index (hw
->info
.bits
)];
1899 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1900 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1903 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1904 audio_attach_capture (s
, hw
);
1909 qemu_free (cap
->hw
.mix_buf
);
1919 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1921 struct capture_callback
*cb
;
1923 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1924 if (cb
->opaque
== cb_opaque
) {
1925 cb
->ops
.destroy (cb_opaque
);
1926 LIST_REMOVE (cb
, entries
);
1929 if (!cap
->cb_head
.lh_first
) {
1930 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1933 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1934 #ifdef DEBUG_CAPTURE
1935 dolog ("freeing %s\n", sw
->name
);
1938 sw1
= sw
->entries
.le_next
;
1940 st_rate_stop (sw
->rate
);
1943 LIST_REMOVE (sw
, entries
);
1944 LIST_REMOVE (sc
, entries
);
1948 LIST_REMOVE (cap
, entries
);