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 const char *audio_audfmt_to_string (audfmt_e fmt
)
263 dolog ("Bogus audfmt %d returning S16\n", fmt
);
267 audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
, int *defaultp
)
269 if (!strcasecmp (s
, "u8")) {
273 else if (!strcasecmp (s
, "u16")) {
277 else if (!strcasecmp (s
, "u32")) {
281 else if (!strcasecmp (s
, "s8")) {
285 else if (!strcasecmp (s
, "s16")) {
289 else if (!strcasecmp (s
, "s32")) {
294 dolog ("Bogus audio format `%s' using %s\n",
295 s
, audio_audfmt_to_string (defval
));
301 static audfmt_e
audio_get_conf_fmt (const char *envname
,
305 const char *var
= getenv (envname
);
310 return audio_string_to_audfmt (var
, defval
, defaultp
);
313 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
318 strval
= getenv (key
);
330 static const char *audio_get_conf_str (const char *key
,
334 const char *val
= getenv (key
);
345 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
347 if (conf
.log_to_monitor
) {
349 term_printf ("%s: ", cap
);
352 term_vprintf (fmt
, ap
);
356 fprintf (stderr
, "%s: ", cap
);
359 vfprintf (stderr
, fmt
, ap
);
363 void AUD_log (const char *cap
, const char *fmt
, ...)
368 AUD_vlog (cap
, fmt
, ap
);
372 static void audio_print_options (const char *prefix
,
373 struct audio_option
*opt
)
378 dolog ("No prefix specified\n");
383 dolog ("No options\n");
387 uprefix
= audio_alloc_prefix (prefix
);
389 for (; opt
->name
; opt
++) {
390 const char *state
= "default";
391 printf (" %s_%s: ", uprefix
, opt
->name
);
393 if (opt
->overriddenp
&& *opt
->overriddenp
) {
400 int *intp
= opt
->valp
;
401 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
407 int *intp
= opt
->valp
;
408 printf ("integer, %s = %d\n", state
, *intp
);
414 audfmt_e
*fmtp
= opt
->valp
;
416 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
418 audio_audfmt_to_string (*fmtp
)
425 const char **strp
= opt
->valp
;
426 printf ("string, %s = %s\n",
428 *strp
? *strp
: "(not set)");
434 dolog ("Bad value tag for option %s_%s %d\n",
435 uprefix
, opt
->name
, opt
->tag
);
438 printf (" %s\n", opt
->descr
);
444 static void audio_process_options (const char *prefix
,
445 struct audio_option
*opt
)
448 const char qemu_prefix
[] = "QEMU_";
451 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
452 dolog ("prefix = NULL\n");
456 if (audio_bug (AUDIO_FUNC
, !opt
)) {
457 dolog ("opt = NULL\n");
461 preflen
= strlen (prefix
);
463 for (; opt
->name
; opt
++) {
468 dolog ("Option value pointer for `%s' is not set\n",
473 len
= strlen (opt
->name
);
474 /* len of opt->name + len of prefix + size of qemu_prefix
475 * (includes trailing zero) + zero + underscore (on behalf of
477 optname
= qemu_malloc (len
+ preflen
+ sizeof (qemu_prefix
) + 1);
479 dolog ("Could not allocate memory for option name `%s'\n",
484 strcpy (optname
, qemu_prefix
);
486 /* copy while upper-casing, including trailing zero */
487 for (i
= 0; i
<= preflen
; ++i
) {
488 optname
[i
+ sizeof (qemu_prefix
) - 1] = toupper (prefix
[i
]);
490 strcat (optname
, "_");
491 strcat (optname
, opt
->name
);
498 int *intp
= opt
->valp
;
499 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
505 audfmt_e
*fmtp
= opt
->valp
;
506 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
512 const char **strp
= opt
->valp
;
513 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
518 dolog ("Bad value tag for option `%s' - %d\n",
523 if (!opt
->overriddenp
) {
524 opt
->overriddenp
= &opt
->overridden
;
526 *opt
->overriddenp
= !def
;
531 static void audio_print_settings (audsettings_t
*as
)
533 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
537 AUD_log (NULL
, "S8");
540 AUD_log (NULL
, "U8");
543 AUD_log (NULL
, "S16");
546 AUD_log (NULL
, "U16");
549 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
553 AUD_log (NULL
, " endianness=");
554 switch (as
->endianness
) {
556 AUD_log (NULL
, "little");
559 AUD_log (NULL
, "big");
562 AUD_log (NULL
, "invalid");
565 AUD_log (NULL
, "\n");
568 static int audio_validate_settings (audsettings_t
*as
)
572 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
573 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
588 invalid
|= as
->freq
<= 0;
589 return invalid
? -1 : 0;
592 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, audsettings_t
*as
)
594 int bits
= 8, sign
= 0;
614 return info
->freq
== as
->freq
615 && info
->nchannels
== as
->nchannels
616 && info
->sign
== sign
617 && info
->bits
== bits
618 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
621 void audio_pcm_init_info (struct audio_pcm_info
*info
, audsettings_t
*as
)
623 int bits
= 8, sign
= 0, shift
= 0;
646 info
->freq
= as
->freq
;
649 info
->nchannels
= as
->nchannels
;
650 info
->shift
= (as
->nchannels
== 2) + shift
;
651 info
->align
= (1 << info
->shift
) - 1;
652 info
->bytes_per_second
= info
->freq
<< info
->shift
;
653 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
656 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
663 memset (buf
, 0x00, len
<< info
->shift
);
666 switch (info
->bits
) {
668 memset (buf
, 0x80, len
<< info
->shift
);
675 int shift
= info
->nchannels
- 1;
678 if (info
->swap_endianness
) {
682 for (i
= 0; i
< len
<< shift
; i
++) {
692 int shift
= info
->nchannels
- 1;
693 int32_t s
= INT32_MAX
;
695 if (info
->swap_endianness
) {
699 for (i
= 0; i
< len
<< shift
; i
++) {
706 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
716 static void noop_conv (st_sample_t
*dst
, const void *src
,
717 int samples
, volume_t
*vol
)
725 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
730 CaptureVoiceOut
*cap
;
732 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
733 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
740 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
742 struct capture_callback
*cb
;
745 dolog ("notification %d sent\n", cmd
);
747 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
748 cb
->ops
.notify (cb
->opaque
, cmd
);
752 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
754 if (cap
->hw
.enabled
!= enabled
) {
755 audcnotification_e cmd
;
756 cap
->hw
.enabled
= enabled
;
757 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
758 audio_notify_capture (cap
, cmd
);
762 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
764 HWVoiceOut
*hw
= &cap
->hw
;
768 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
774 audio_capture_maybe_changed (cap
, enabled
);
777 static void audio_detach_capture (HWVoiceOut
*hw
)
779 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
782 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
783 SWVoiceOut
*sw
= &sc
->sw
;
784 CaptureVoiceOut
*cap
= sc
->cap
;
785 int was_active
= sw
->active
;
788 st_rate_stop (sw
->rate
);
792 LIST_REMOVE (sw
, entries
);
793 LIST_REMOVE (sc
, entries
);
796 /* We have removed soft voice from the capture:
797 this might have changed the overall status of the capture
798 since this might have been the only active voice */
799 audio_recalc_and_notify_capture (cap
);
805 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
807 CaptureVoiceOut
*cap
;
809 audio_detach_capture (hw
);
810 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
813 HWVoiceOut
*hw_cap
= &cap
->hw
;
815 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
817 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
827 sw
->active
= hw
->enabled
;
828 sw
->conv
= noop_conv
;
829 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
830 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
832 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
836 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
837 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
839 asprintf (&sw
->name
, "for %p %d,%d,%d",
840 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
841 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
844 audio_capture_maybe_changed (cap
, 1);
851 * Hard voice (capture)
853 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
856 int m
= hw
->total_samples_captured
;
858 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
860 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
866 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
868 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
869 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
870 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
877 * Soft voice (capture)
879 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
881 HWVoiceIn
*hw
= sw
->hw
;
882 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
885 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
886 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
890 rpos
= hw
->wpos
- live
;
895 return hw
->samples
+ rpos
;
899 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
901 HWVoiceIn
*hw
= sw
->hw
;
902 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
903 st_sample_t
*src
, *dst
= sw
->buf
;
905 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
907 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
908 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
909 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
913 samples
= size
>> sw
->info
.shift
;
918 swlim
= (live
* sw
->ratio
) >> 32;
919 swlim
= audio_MIN (swlim
, samples
);
922 src
= hw
->conv_buf
+ rpos
;
923 isamp
= hw
->wpos
- rpos
;
926 isamp
= hw
->samples
- rpos
;
934 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
935 dolog ("osamp=%d\n", osamp
);
939 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
941 rpos
= (rpos
+ isamp
) % hw
->samples
;
947 sw
->clip (buf
, sw
->buf
, ret
);
948 sw
->total_hw_samples_acquired
+= total
;
949 return ret
<< sw
->info
.shift
;
953 * Hard voice (playback)
955 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
961 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
962 if (sw
->active
|| !sw
->empty
) {
963 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
972 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
976 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
984 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
985 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
992 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
997 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
998 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
999 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1006 * Soft voice (playback)
1008 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1010 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1011 int ret
= 0, pos
= 0, total
= 0;
1017 hwsamples
= sw
->hw
->samples
;
1019 live
= sw
->total_hw_samples_mixed
;
1020 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
1021 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1025 if (live
== hwsamples
) {
1027 dolog ("%s is full %d\n", sw
->name
, live
);
1032 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1033 samples
= size
>> sw
->info
.shift
;
1035 dead
= hwsamples
- live
;
1036 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1037 swlim
= audio_MIN (swlim
, samples
);
1039 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
1043 dead
= hwsamples
- live
;
1044 left
= hwsamples
- wpos
;
1045 blck
= audio_MIN (dead
, left
);
1054 sw
->hw
->mix_buf
+ wpos
,
1062 wpos
= (wpos
+ osamp
) % hwsamples
;
1066 sw
->total_hw_samples_mixed
+= total
;
1067 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1071 "%s: write size %d ret %d total sw %d\n",
1073 size
>> sw
->info
.shift
,
1075 sw
->total_hw_samples_mixed
1079 return ret
<< sw
->info
.shift
;
1083 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1085 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1086 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1091 #include "audio_template.h"
1093 #include "audio_template.h"
1095 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1100 /* XXX: Consider options */
1104 if (!sw
->hw
->enabled
) {
1105 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1109 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1113 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1118 /* XXX: Consider options */
1122 if (!sw
->hw
->enabled
) {
1123 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1127 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1131 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1133 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1136 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1145 if (sw
->active
!= on
) {
1146 SWVoiceOut
*temp_sw
;
1150 hw
->pending_disable
= 0;
1153 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1160 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1161 temp_sw
= temp_sw
->entries
.le_next
) {
1162 nb_active
+= temp_sw
->active
!= 0;
1165 hw
->pending_disable
= nb_active
== 1;
1169 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1170 sc
->sw
.active
= hw
->enabled
;
1172 audio_capture_maybe_changed (sc
->cap
, 1);
1179 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1188 if (sw
->active
!= on
) {
1194 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1196 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1202 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1203 temp_sw
= temp_sw
->entries
.le_next
) {
1204 nb_active
+= temp_sw
->active
!= 0;
1207 if (nb_active
== 1) {
1209 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1217 static int audio_get_avail (SWVoiceIn
*sw
)
1225 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1226 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1227 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1232 "%s: get_avail live %d ret %" PRId64
"\n",
1234 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1237 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1240 static int audio_get_free (SWVoiceOut
*sw
)
1248 live
= sw
->total_hw_samples_mixed
;
1250 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1251 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1255 dead
= sw
->hw
->samples
- live
;
1258 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1260 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1263 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1266 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1273 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1274 SWVoiceOut
*sw
= &sc
->sw
;
1279 int till_end_of_hw
= hw
->samples
- rpos2
;
1280 int to_write
= audio_MIN (till_end_of_hw
, n
);
1281 int bytes
= to_write
<< hw
->info
.shift
;
1284 sw
->buf
= hw
->mix_buf
+ rpos2
;
1285 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1286 if (written
- bytes
) {
1287 dolog ("Could not mix %d bytes into a capture "
1288 "buffer, mixed %d\n",
1293 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1298 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1299 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1300 mixeng_clear (hw
->mix_buf
, samples
- n
);
1303 static void audio_run_out (AudioState
*s
)
1305 HWVoiceOut
*hw
= NULL
;
1308 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1310 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1312 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1317 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1318 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1322 if (hw
->pending_disable
&& !nb_live
) {
1325 dolog ("Disabling voice\n");
1328 hw
->pending_disable
= 0;
1329 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1330 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1332 audio_recalc_and_notify_capture (sc
->cap
);
1338 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1340 free
= audio_get_free (sw
);
1342 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1349 prev_rpos
= hw
->rpos
;
1350 played
= hw
->pcm_ops
->run_out (hw
);
1351 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1352 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1353 hw
->rpos
, hw
->samples
, played
);
1358 dolog ("played=%d\n", played
);
1362 hw
->ts_helper
+= played
;
1363 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1366 cleanup_required
= 0;
1367 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1368 if (!sw
->active
&& sw
->empty
) {
1372 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1373 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1374 played
, sw
->total_hw_samples_mixed
);
1375 played
= sw
->total_hw_samples_mixed
;
1378 sw
->total_hw_samples_mixed
-= played
;
1380 if (!sw
->total_hw_samples_mixed
) {
1382 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1386 free
= audio_get_free (sw
);
1388 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1393 if (cleanup_required
) {
1396 sw
= hw
->sw_head
.lh_first
;
1398 sw1
= sw
->entries
.le_next
;
1399 if (!sw
->active
&& !sw
->callback
.fn
) {
1401 dolog ("Finishing with old voice\n");
1403 audio_close_out (s
, sw
);
1411 static void audio_run_in (AudioState
*s
)
1413 HWVoiceIn
*hw
= NULL
;
1415 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1419 captured
= hw
->pcm_ops
->run_in (hw
);
1421 min
= audio_pcm_hw_find_min_in (hw
);
1422 hw
->total_samples_captured
+= captured
- min
;
1423 hw
->ts_helper
+= captured
;
1425 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1426 sw
->total_hw_samples_acquired
-= min
;
1431 avail
= audio_get_avail (sw
);
1433 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1440 static void audio_run_capture (AudioState
*s
)
1442 CaptureVoiceOut
*cap
;
1444 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1445 int live
, rpos
, captured
;
1446 HWVoiceOut
*hw
= &cap
->hw
;
1449 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1452 int left
= hw
->samples
- rpos
;
1453 int to_capture
= audio_MIN (live
, left
);
1455 struct capture_callback
*cb
;
1457 src
= hw
->mix_buf
+ rpos
;
1458 hw
->clip (cap
->buf
, src
, to_capture
);
1459 mixeng_clear (src
, to_capture
);
1461 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1462 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1463 to_capture
<< hw
->info
.shift
);
1465 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1470 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1471 if (!sw
->active
&& sw
->empty
) {
1475 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1476 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1477 captured
, sw
->total_hw_samples_mixed
);
1478 captured
= sw
->total_hw_samples_mixed
;
1481 sw
->total_hw_samples_mixed
-= captured
;
1482 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1487 static void audio_timer (void *opaque
)
1489 AudioState
*s
= opaque
;
1493 audio_run_capture (s
);
1495 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1498 static struct audio_option audio_options
[] = {
1500 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1501 "Use fixed settings for host DAC", NULL
, 0},
1503 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1504 "Frequency for fixed host DAC", NULL
, 0},
1506 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1507 "Format for fixed host DAC", NULL
, 0},
1509 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1510 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1512 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1513 "Number of voices for DAC", NULL
, 0},
1516 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1517 "Use fixed settings for host ADC", NULL
, 0},
1519 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1520 "Frequency for fixed host ADC", NULL
, 0},
1522 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1523 "Format for fixed host ADC", NULL
, 0},
1525 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1526 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1528 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1529 "Number of voices for ADC", NULL
, 0},
1532 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hz
,
1533 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1535 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1536 "(undocumented)", NULL
, 0},
1538 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1539 "print logging messages to montior instead of stderr", NULL
, 0},
1541 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1544 static void audio_pp_nb_voices (const char *typ
, int nb
)
1548 printf ("Does not support %s\n", typ
);
1551 printf ("One %s voice\n", typ
);
1554 printf ("Theoretically supports many %s voices\n", typ
);
1557 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1563 void AUD_help (void)
1567 audio_process_options ("AUDIO", audio_options
);
1568 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1569 struct audio_driver
*d
= drvtab
[i
];
1571 audio_process_options (d
->name
, d
->options
);
1575 printf ("Audio options:\n");
1576 audio_print_options ("AUDIO", audio_options
);
1579 printf ("Available drivers:\n");
1581 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1582 struct audio_driver
*d
= drvtab
[i
];
1584 printf ("Name: %s\n", d
->name
);
1585 printf ("Description: %s\n", d
->descr
);
1587 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1588 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1591 printf ("Options:\n");
1592 audio_print_options (d
->name
, d
->options
);
1595 printf ("No options\n");
1601 "Options are settable through environment variables.\n"
1604 " set QEMU_AUDIO_DRV=wav\n"
1605 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1607 " export QEMU_AUDIO_DRV=wav\n"
1608 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1609 "(for csh replace export with setenv in the above)\n"
1615 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1618 audio_process_options (drv
->name
, drv
->options
);
1620 s
->drv_opaque
= drv
->init ();
1622 if (s
->drv_opaque
) {
1623 audio_init_nb_voices_out (s
, drv
);
1624 audio_init_nb_voices_in (s
, drv
);
1629 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1634 static void audio_vm_change_state_handler (void *opaque
, int running
)
1636 AudioState
*s
= opaque
;
1637 HWVoiceOut
*hwo
= NULL
;
1638 HWVoiceIn
*hwi
= NULL
;
1639 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1641 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1642 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1645 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1646 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1650 static void audio_atexit (void)
1652 AudioState
*s
= &glob_audio_state
;
1653 HWVoiceOut
*hwo
= NULL
;
1654 HWVoiceIn
*hwi
= NULL
;
1656 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1659 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1660 hwo
->pcm_ops
->fini_out (hwo
);
1662 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1663 CaptureVoiceOut
*cap
= sc
->cap
;
1664 struct capture_callback
*cb
;
1666 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1667 cb
->ops
.destroy (cb
->opaque
);
1672 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1673 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1674 hwi
->pcm_ops
->fini_in (hwi
);
1678 s
->drv
->fini (s
->drv_opaque
);
1682 static void audio_save (QEMUFile
*f
, void *opaque
)
1688 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1693 if (version_id
!= 1) {
1700 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1703 card
->name
= qemu_strdup (name
);
1704 memset (&card
->entries
, 0, sizeof (card
->entries
));
1705 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1708 void AUD_remove_card (QEMUSoundCard
*card
)
1710 LIST_REMOVE (card
, entries
);
1712 qemu_free (card
->name
);
1715 AudioState
*AUD_init (void)
1719 const char *drvname
;
1720 AudioState
*s
= &glob_audio_state
;
1722 LIST_INIT (&s
->hw_head_out
);
1723 LIST_INIT (&s
->hw_head_in
);
1724 LIST_INIT (&s
->cap_head
);
1725 atexit (audio_atexit
);
1727 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1729 dolog ("Could not create audio timer\n");
1733 audio_process_options ("AUDIO", audio_options
);
1735 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1736 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1738 if (s
->nb_hw_voices_out
<= 0) {
1739 dolog ("Bogus number of playback voices %d, setting to 1\n",
1740 s
->nb_hw_voices_out
);
1741 s
->nb_hw_voices_out
= 1;
1744 if (s
->nb_hw_voices_in
<= 0) {
1745 dolog ("Bogus number of capture voices %d, setting to 0\n",
1746 s
->nb_hw_voices_in
);
1747 s
->nb_hw_voices_in
= 0;
1752 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1758 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1759 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1760 done
= !audio_driver_init (s
, drvtab
[i
]);
1767 dolog ("Unknown audio driver `%s'\n", drvname
);
1768 dolog ("Run with -audio-help to list available drivers\n");
1773 for (i
= 0; !done
&& i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1774 if (drvtab
[i
]->can_be_default
) {
1775 done
= !audio_driver_init (s
, drvtab
[i
]);
1781 done
= !audio_driver_init (s
, &no_audio_driver
);
1783 dolog ("Could not initialize audio subsystem\n");
1786 dolog ("warning: Using timer based audio emulation\n");
1791 VMChangeStateEntry
*e
;
1793 if (conf
.period
.hz
<= 0) {
1794 if (conf
.period
.hz
< 0) {
1795 dolog ("warning: Timer period is negative - %d "
1796 "treating as zero\n",
1799 conf
.period
.ticks
= 1;
1802 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hz
;
1805 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1807 dolog ("warning: Could not register change state handler\n"
1808 "(Audio can continue looping even after stopping the VM)\n");
1812 qemu_del_timer (s
->ts
);
1816 LIST_INIT (&s
->card_head
);
1817 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1818 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1822 CaptureVoiceOut
*AUD_add_capture (
1825 struct audio_capture_ops
*ops
,
1829 CaptureVoiceOut
*cap
;
1830 struct capture_callback
*cb
;
1834 s
= &glob_audio_state
;
1837 if (audio_validate_settings (as
)) {
1838 dolog ("Invalid settings were passed when trying to add capture\n");
1839 audio_print_settings (as
);
1843 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1845 dolog ("Could not allocate capture callback information, size %zu\n",
1850 cb
->opaque
= cb_opaque
;
1852 cap
= audio_pcm_capture_find_specific (s
, as
);
1854 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1859 CaptureVoiceOut
*cap
;
1861 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1863 dolog ("Could not allocate capture voice, size %zu\n",
1869 LIST_INIT (&hw
->sw_head
);
1870 LIST_INIT (&cap
->cb_head
);
1872 /* XXX find a more elegant way */
1873 hw
->samples
= 4096 * 4;
1874 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1875 sizeof (st_sample_t
));
1877 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1882 audio_pcm_init_info (&hw
->info
, as
);
1884 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1886 dolog ("Could not allocate capture buffer "
1887 "(%d samples, each %d bytes)\n",
1888 hw
->samples
, 1 << hw
->info
.shift
);
1892 hw
->clip
= mixeng_clip
1893 [hw
->info
.nchannels
== 2]
1895 [hw
->info
.swap_endianness
]
1896 [audio_bits_to_index (hw
->info
.bits
)];
1898 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1899 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1902 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1903 audio_attach_capture (s
, hw
);
1908 qemu_free (cap
->hw
.mix_buf
);
1918 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1920 struct capture_callback
*cb
;
1922 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1923 if (cb
->opaque
== cb_opaque
) {
1924 cb
->ops
.destroy (cb_opaque
);
1925 LIST_REMOVE (cb
, entries
);
1928 if (!cap
->cb_head
.lh_first
) {
1929 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1932 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1933 #ifdef DEBUG_CAPTURE
1934 dolog ("freeing %s\n", sw
->name
);
1937 sw1
= sw
->entries
.le_next
;
1939 st_rate_stop (sw
->rate
);
1942 LIST_REMOVE (sw
, entries
);
1943 LIST_REMOVE (sc
, entries
);
1947 LIST_REMOVE (cap
, entries
);