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
26 #define AUDIO_CAP "audio"
27 #include "audio_int.h"
29 /* #define DEBUG_PLIVE */
30 /* #define DEBUG_LIVE */
31 /* #define DEBUG_OUT */
32 /* #define DEBUG_CAPTURE */
34 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
36 static struct audio_driver
*drvtab
[] = {
43 #ifdef CONFIG_COREAUDIO
44 &coreaudio_audio_driver
,
59 struct fixed_settings
{
63 audsettings_t settings
;
67 struct fixed_settings fixed_out
;
68 struct fixed_settings fixed_in
;
76 { /* DAC fixed settings */
87 { /* ADC fixed settings */
100 0 /* log_to_monitor */
103 static AudioState glob_audio_state
;
105 volume_t nominal_volume
= {
116 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
117 /* http://www.multi-platforms.com/Tips/PopCount.htm */
118 uint32_t popcount (uint32_t u
)
120 u
= ((u
&0x55555555) + ((u
>>1)&0x55555555));
121 u
= ((u
&0x33333333) + ((u
>>2)&0x33333333));
122 u
= ((u
&0x0f0f0f0f) + ((u
>>4)&0x0f0f0f0f));
123 u
= ((u
&0x00ff00ff) + ((u
>>8)&0x00ff00ff));
124 u
= ( u
&0x0000ffff) + (u
>>16);
128 inline uint32_t lsbindex (uint32_t u
)
130 return popcount ((u
&-u
)-1);
133 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
136 int audio_bug (const char *funcname
, int cond
)
141 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
144 AUD_log (NULL
, "Save all your work and restart without audio\n");
145 AUD_log (NULL
, "Please send bug report to malc@pulsesoft.com\n");
146 AUD_log (NULL
, "I am sorry\n");
148 AUD_log (NULL
, "Context:\n");
150 #if defined AUDIO_BREAKPOINT_ON_BUG
151 # if defined HOST_I386
152 # if defined __GNUC__
154 # elif defined _MSC_VER
169 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
175 cond
= !nmemb
|| !size
;
179 if (audio_bug ("audio_calloc", cond
)) {
180 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
182 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
186 return qemu_mallocz (len
);
189 static char *audio_alloc_prefix (const char *s
)
191 const char qemu_prefix
[] = "QEMU_";
200 r
= qemu_malloc (len
+ sizeof (qemu_prefix
));
204 char *u
= r
+ sizeof (qemu_prefix
) - 1;
206 strcpy (r
, qemu_prefix
);
209 for (i
= 0; i
< len
; ++i
) {
210 u
[i
] = toupper (u
[i
]);
216 const char *audio_audfmt_to_string (audfmt_e fmt
)
232 dolog ("Bogus audfmt %d returning S16\n", fmt
);
236 audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
, int *defaultp
)
238 if (!strcasecmp (s
, "u8")) {
242 else if (!strcasecmp (s
, "u16")) {
246 else if (!strcasecmp (s
, "s8")) {
250 else if (!strcasecmp (s
, "s16")) {
255 dolog ("Bogus audio format `%s' using %s\n",
256 s
, audio_audfmt_to_string (defval
));
262 static audfmt_e
audio_get_conf_fmt (const char *envname
,
266 const char *var
= getenv (envname
);
271 return audio_string_to_audfmt (var
, defval
, defaultp
);
274 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
279 strval
= getenv (key
);
291 static const char *audio_get_conf_str (const char *key
,
295 const char *val
= getenv (key
);
306 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
308 if (conf
.log_to_monitor
) {
310 term_printf ("%s: ", cap
);
313 term_vprintf (fmt
, ap
);
317 fprintf (stderr
, "%s: ", cap
);
320 vfprintf (stderr
, fmt
, ap
);
324 void AUD_log (const char *cap
, const char *fmt
, ...)
329 AUD_vlog (cap
, fmt
, ap
);
333 static void audio_print_options (const char *prefix
,
334 struct audio_option
*opt
)
339 dolog ("No prefix specified\n");
344 dolog ("No options\n");
348 uprefix
= audio_alloc_prefix (prefix
);
350 for (; opt
->name
; opt
++) {
351 const char *state
= "default";
352 printf (" %s_%s: ", uprefix
, opt
->name
);
354 if (opt
->overridenp
&& *opt
->overridenp
) {
361 int *intp
= opt
->valp
;
362 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
368 int *intp
= opt
->valp
;
369 printf ("integer, %s = %d\n", state
, *intp
);
375 audfmt_e
*fmtp
= opt
->valp
;
377 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
379 audio_audfmt_to_string (*fmtp
)
386 const char **strp
= opt
->valp
;
387 printf ("string, %s = %s\n",
389 *strp
? *strp
: "(not set)");
395 dolog ("Bad value tag for option %s_%s %d\n",
396 uprefix
, opt
->name
, opt
->tag
);
399 printf (" %s\n", opt
->descr
);
405 static void audio_process_options (const char *prefix
,
406 struct audio_option
*opt
)
409 const char qemu_prefix
[] = "QEMU_";
412 if (audio_bug (AUDIO_FUNC
, !prefix
)) {
413 dolog ("prefix = NULL\n");
417 if (audio_bug (AUDIO_FUNC
, !opt
)) {
418 dolog ("opt = NULL\n");
422 preflen
= strlen (prefix
);
424 for (; opt
->name
; opt
++) {
429 dolog ("Option value pointer for `%s' is not set\n",
434 len
= strlen (opt
->name
);
435 /* len of opt->name + len of prefix + size of qemu_prefix
436 * (includes trailing zero) + zero + underscore (on behalf of
438 optname
= qemu_malloc (len
+ preflen
+ sizeof (qemu_prefix
) + 1);
440 dolog ("Could not allocate memory for option name `%s'\n",
445 strcpy (optname
, qemu_prefix
);
447 /* copy while upper-casing, including trailing zero */
448 for (i
= 0; i
<= preflen
; ++i
) {
449 optname
[i
+ sizeof (qemu_prefix
) - 1] = toupper (prefix
[i
]);
451 strcat (optname
, "_");
452 strcat (optname
, opt
->name
);
459 int *intp
= opt
->valp
;
460 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
466 audfmt_e
*fmtp
= opt
->valp
;
467 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
473 const char **strp
= opt
->valp
;
474 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
479 dolog ("Bad value tag for option `%s' - %d\n",
484 if (!opt
->overridenp
) {
485 opt
->overridenp
= &opt
->overriden
;
487 *opt
->overridenp
= !def
;
492 static void audio_print_settings (audsettings_t
*as
)
494 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
498 AUD_log (NULL
, "S8");
501 AUD_log (NULL
, "U8");
504 AUD_log (NULL
, "S16");
507 AUD_log (NULL
, "U16");
510 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
513 AUD_log (NULL
, "endianness=");
514 switch (as
->endianness
) {
516 AUD_log (NULL
, "little");
519 AUD_log (NULL
, "big");
522 AUD_log (NULL
, "invalid");
525 AUD_log (NULL
, "\n");
528 static int audio_validate_settigs (audsettings_t
*as
)
532 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
533 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
546 invalid
|= as
->freq
<= 0;
547 return invalid
? -1 : 0;
550 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, audsettings_t
*as
)
552 int bits
= 8, sign
= 0;
566 return info
->freq
== as
->freq
567 && info
->nchannels
== as
->nchannels
568 && info
->sign
== sign
569 && info
->bits
== bits
570 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
573 void audio_pcm_init_info (struct audio_pcm_info
*info
, audsettings_t
*as
)
575 int bits
= 8, sign
= 0;
590 info
->freq
= as
->freq
;
593 info
->nchannels
= as
->nchannels
;
594 info
->shift
= (as
->nchannels
== 2) + (bits
== 16);
595 info
->align
= (1 << info
->shift
) - 1;
596 info
->bytes_per_second
= info
->freq
<< info
->shift
;
597 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
600 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
607 memset (buf
, len
<< info
->shift
, 0x00);
610 if (info
->bits
== 8) {
611 memset (buf
, len
<< info
->shift
, 0x80);
616 int shift
= info
->nchannels
- 1;
619 if (info
->swap_endianness
) {
623 for (i
= 0; i
< len
<< shift
; i
++) {
633 static void noop_conv (st_sample_t
*dst
, const void *src
,
634 int samples
, volume_t
*vol
)
642 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
647 CaptureVoiceOut
*cap
;
649 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
650 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
657 static void audio_notify_capture (CaptureVoiceOut
*cap
, int enabled
)
659 if (cap
->hw
.enabled
!= enabled
) {
660 struct capture_callback
*cb
;
662 cap
->hw
.enabled
= enabled
;
663 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
664 cb
->ops
.state (cb
->opaque
, enabled
);
669 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
671 HWVoiceOut
*hw
= &cap
->hw
;
675 for (sw
= hw
->sw_cap_head
.lh_first
; sw
; sw
= sw
->cap_entries
.le_next
) {
681 audio_notify_capture (cap
, enabled
);
684 static void audio_detach_capture (HWVoiceOut
*hw
)
688 for (sw
= hw
->sw_cap_head
.lh_first
; sw
; sw
= sw
->cap_entries
.le_next
) {
690 st_rate_stop (sw
->rate
);
694 LIST_REMOVE (sw
, entries
);
695 LIST_REMOVE (sw
, cap_entries
);
697 audio_recalc_and_notify_capture ((CaptureVoiceOut
*) sw
->hw
);
701 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
703 CaptureVoiceOut
*cap
;
705 audio_detach_capture (hw
);
706 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
711 sw
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sw
));
713 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
721 sw
->active
= hw
->enabled
;
722 sw
->conv
= noop_conv
;
723 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
724 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
726 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
730 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
731 LIST_INSERT_HEAD (&hw
->sw_cap_head
, sw
, cap_entries
);
733 audio_notify_capture (cap
, 1);
736 audio_recalc_and_notify_capture (cap
);
743 * Hard voice (capture)
745 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
748 int m
= hw
->total_samples_captured
;
750 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
752 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
758 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
760 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
761 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
762 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
769 * Soft voice (capture)
771 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
773 HWVoiceIn
*hw
= sw
->hw
;
774 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
777 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
778 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
782 rpos
= hw
->wpos
- live
;
787 return hw
->samples
+ rpos
;
791 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
793 HWVoiceIn
*hw
= sw
->hw
;
794 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
795 st_sample_t
*src
, *dst
= sw
->buf
;
797 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
799 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
800 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
801 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
805 samples
= size
>> sw
->info
.shift
;
810 swlim
= (live
* sw
->ratio
) >> 32;
811 swlim
= audio_MIN (swlim
, samples
);
814 src
= hw
->conv_buf
+ rpos
;
815 isamp
= hw
->wpos
- rpos
;
818 isamp
= hw
->samples
- rpos
;
826 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
827 dolog ("osamp=%d\n", osamp
);
831 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
833 rpos
= (rpos
+ isamp
) % hw
->samples
;
839 sw
->clip (buf
, sw
->buf
, ret
);
840 sw
->total_hw_samples_acquired
+= total
;
841 return ret
<< sw
->info
.shift
;
845 * Hard voice (playback)
847 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
853 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
854 if (sw
->active
|| !sw
->empty
) {
855 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
864 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
868 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
876 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
877 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
884 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
889 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
890 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
891 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
898 * Soft voice (playback)
900 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
902 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
903 int ret
= 0, pos
= 0, total
= 0;
909 hwsamples
= sw
->hw
->samples
;
911 live
= sw
->total_hw_samples_mixed
;
912 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
913 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
917 if (live
== hwsamples
) {
921 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
922 samples
= size
>> sw
->info
.shift
;
924 dead
= hwsamples
- live
;
925 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
926 swlim
= audio_MIN (swlim
, samples
);
928 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
932 dead
= hwsamples
- live
;
933 left
= hwsamples
- wpos
;
934 blck
= audio_MIN (dead
, left
);
943 sw
->hw
->mix_buf
+ wpos
,
951 wpos
= (wpos
+ osamp
) % hwsamples
;
955 sw
->total_hw_samples_mixed
+= total
;
956 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
960 "%s: write size %d ret %d total sw %d\n",
962 size
>> sw
->info
.shift
,
964 sw
->total_hw_samples_mixed
968 return ret
<< sw
->info
.shift
;
972 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
974 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
975 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
980 #include "audio_template.h"
982 #include "audio_template.h"
984 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
989 /* XXX: Consider options */
993 if (!sw
->hw
->enabled
) {
994 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
998 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1002 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1007 /* XXX: Consider options */
1011 if (!sw
->hw
->enabled
) {
1012 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1016 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1020 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1022 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1025 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1034 if (sw
->active
!= on
) {
1035 SWVoiceOut
*temp_sw
;
1038 hw
->pending_disable
= 0;
1041 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1048 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1049 temp_sw
= temp_sw
->entries
.le_next
) {
1050 nb_active
+= temp_sw
->active
!= 0;
1053 hw
->pending_disable
= nb_active
== 1;
1056 for (temp_sw
= hw
->sw_cap_head
.lh_first
; temp_sw
;
1057 temp_sw
= temp_sw
->entries
.le_next
) {
1058 temp_sw
->active
= hw
->enabled
;
1060 audio_notify_capture ((CaptureVoiceOut
*) temp_sw
->hw
, 1);
1067 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1076 if (sw
->active
!= on
) {
1082 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1084 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1090 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1091 temp_sw
= temp_sw
->entries
.le_next
) {
1092 nb_active
+= temp_sw
->active
!= 0;
1095 if (nb_active
== 1) {
1097 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1105 static int audio_get_avail (SWVoiceIn
*sw
)
1113 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1114 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1115 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1120 "%s: get_avail live %d ret %" PRId64
"\n",
1122 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1125 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1128 static int audio_get_free (SWVoiceOut
*sw
)
1136 live
= sw
->total_hw_samples_mixed
;
1138 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1139 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1143 dead
= sw
->hw
->samples
- live
;
1146 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1148 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1151 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1154 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1161 for (sw
= hw
->sw_cap_head
.lh_first
; sw
; sw
= sw
->cap_entries
.le_next
) {
1166 int till_end_of_hw
= hw
->samples
- rpos2
;
1167 int to_write
= audio_MIN (till_end_of_hw
, n
);
1168 int bytes
= to_write
<< hw
->info
.shift
;
1171 sw
->buf
= hw
->mix_buf
+ rpos2
;
1172 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1173 if (written
- bytes
) {
1174 dolog ("Could not mix %d bytes into a capture buffer",
1179 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1184 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1185 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1186 mixeng_clear (hw
->mix_buf
, samples
- n
);
1189 static void audio_run_out (AudioState
*s
)
1191 HWVoiceOut
*hw
= NULL
;
1194 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1196 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1198 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1203 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1204 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1208 if (hw
->pending_disable
&& !nb_live
) {
1210 dolog ("Disabling voice\n");
1213 hw
->pending_disable
= 0;
1214 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1215 for (sw
= hw
->sw_cap_head
.lh_first
; sw
;
1216 sw
= sw
->cap_entries
.le_next
) {
1218 audio_recalc_and_notify_capture ((CaptureVoiceOut
*) sw
->hw
);
1224 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1226 free
= audio_get_free (sw
);
1228 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1235 prev_rpos
= hw
->rpos
;
1236 played
= hw
->pcm_ops
->run_out (hw
);
1237 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1238 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1239 hw
->rpos
, hw
->samples
, played
);
1244 dolog ("played=%d\n", played
);
1248 hw
->ts_helper
+= played
;
1249 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1252 cleanup_required
= 0;
1253 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1254 if (!sw
->active
&& sw
->empty
) {
1258 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1259 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1260 played
, sw
->total_hw_samples_mixed
);
1261 played
= sw
->total_hw_samples_mixed
;
1264 sw
->total_hw_samples_mixed
-= played
;
1266 if (!sw
->total_hw_samples_mixed
) {
1268 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1272 free
= audio_get_free (sw
);
1274 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1279 if (cleanup_required
) {
1281 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1282 if (!sw
->active
&& !sw
->callback
.fn
) {
1284 dolog ("Finishing with old voice\n");
1286 audio_close_out (s
, sw
);
1287 goto restart
; /* play it safe */
1294 static void audio_run_in (AudioState
*s
)
1296 HWVoiceIn
*hw
= NULL
;
1298 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1302 captured
= hw
->pcm_ops
->run_in (hw
);
1304 min
= audio_pcm_hw_find_min_in (hw
);
1305 hw
->total_samples_captured
+= captured
- min
;
1306 hw
->ts_helper
+= captured
;
1308 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1309 sw
->total_hw_samples_acquired
-= min
;
1314 avail
= audio_get_avail (sw
);
1316 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1323 static void audio_run_capture (AudioState
*s
)
1325 CaptureVoiceOut
*cap
;
1327 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1328 int live
, rpos
, captured
;
1329 HWVoiceOut
*hw
= &cap
->hw
;
1332 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1335 int left
= hw
->samples
- rpos
;
1336 int to_capture
= audio_MIN (live
, left
);
1338 struct capture_callback
*cb
;
1340 src
= hw
->mix_buf
+ rpos
;
1341 hw
->clip (cap
->buf
, src
, to_capture
);
1342 mixeng_clear (src
, to_capture
);
1344 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1345 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1346 to_capture
<< hw
->info
.shift
);
1348 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1353 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1354 if (!sw
->active
&& sw
->empty
) {
1358 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1359 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1360 captured
, sw
->total_hw_samples_mixed
);
1361 captured
= sw
->total_hw_samples_mixed
;
1364 sw
->total_hw_samples_mixed
-= captured
;
1365 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1370 static void audio_timer (void *opaque
)
1372 AudioState
*s
= opaque
;
1376 audio_run_capture (s
);
1378 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1381 static struct audio_option audio_options
[] = {
1383 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1384 "Use fixed settings for host DAC", NULL
, 0},
1386 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1387 "Frequency for fixed host DAC", NULL
, 0},
1389 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1390 "Format for fixed host DAC", NULL
, 0},
1392 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1393 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1395 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1396 "Number of voices for DAC", NULL
, 0},
1399 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1400 "Use fixed settings for host ADC", NULL
, 0},
1402 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1403 "Frequency for fixed host ADC", NULL
, 0},
1405 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1406 "Format for fixed host ADC", NULL
, 0},
1408 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1409 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1411 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1412 "Number of voices for ADC", NULL
, 0},
1415 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hz
,
1416 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1418 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1419 "(undocumented)", NULL
, 0},
1421 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1422 "print logging messages to montior instead of stderr", NULL
, 0},
1424 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1427 static void audio_pp_nb_voices (const char *typ
, int nb
)
1431 printf ("Does not support %s\n", typ
);
1434 printf ("One %s voice\n", typ
);
1437 printf ("Theoretically supports many %s voices\n", typ
);
1440 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1446 void AUD_help (void)
1450 audio_process_options ("AUDIO", audio_options
);
1451 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1452 struct audio_driver
*d
= drvtab
[i
];
1454 audio_process_options (d
->name
, d
->options
);
1458 printf ("Audio options:\n");
1459 audio_print_options ("AUDIO", audio_options
);
1462 printf ("Available drivers:\n");
1464 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1465 struct audio_driver
*d
= drvtab
[i
];
1467 printf ("Name: %s\n", d
->name
);
1468 printf ("Description: %s\n", d
->descr
);
1470 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1471 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1474 printf ("Options:\n");
1475 audio_print_options (d
->name
, d
->options
);
1478 printf ("No options\n");
1484 "Options are settable through environment variables.\n"
1487 " set QEMU_AUDIO_DRV=wav\n"
1488 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1490 " export QEMU_AUDIO_DRV=wav\n"
1491 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1492 "(for csh replace export with setenv in the above)\n"
1498 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1501 audio_process_options (drv
->name
, drv
->options
);
1503 s
->drv_opaque
= drv
->init ();
1505 if (s
->drv_opaque
) {
1506 audio_init_nb_voices_out (s
, drv
);
1507 audio_init_nb_voices_in (s
, drv
);
1512 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1517 static void audio_vm_change_state_handler (void *opaque
, int running
)
1519 AudioState
*s
= opaque
;
1520 HWVoiceOut
*hwo
= NULL
;
1521 HWVoiceIn
*hwi
= NULL
;
1522 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1524 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1525 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1528 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1529 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1533 static void audio_atexit (void)
1535 AudioState
*s
= &glob_audio_state
;
1536 HWVoiceOut
*hwo
= NULL
;
1537 HWVoiceIn
*hwi
= NULL
;
1539 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1542 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1543 hwo
->pcm_ops
->fini_out (hwo
);
1545 for (sw
= hwo
->sw_cap_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1546 audio_notify_capture ((CaptureVoiceOut
*) sw
->hw
, 0);
1550 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1551 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1552 hwi
->pcm_ops
->fini_in (hwi
);
1556 s
->drv
->fini (s
->drv_opaque
);
1560 static void audio_save (QEMUFile
*f
, void *opaque
)
1566 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1571 if (version_id
!= 1) {
1578 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1581 card
->name
= qemu_strdup (name
);
1582 memset (&card
->entries
, 0, sizeof (card
->entries
));
1583 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1586 void AUD_remove_card (QEMUSoundCard
*card
)
1588 LIST_REMOVE (card
, entries
);
1590 qemu_free (card
->name
);
1593 AudioState
*AUD_init (void)
1597 const char *drvname
;
1598 AudioState
*s
= &glob_audio_state
;
1600 LIST_INIT (&s
->hw_head_out
);
1601 LIST_INIT (&s
->hw_head_in
);
1602 LIST_INIT (&s
->cap_head
);
1603 atexit (audio_atexit
);
1605 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1607 dolog ("Could not create audio timer\n");
1611 audio_process_options ("AUDIO", audio_options
);
1613 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1614 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1616 if (s
->nb_hw_voices_out
<= 0) {
1617 dolog ("Bogus number of playback voices %d, setting to 1\n",
1618 s
->nb_hw_voices_out
);
1619 s
->nb_hw_voices_out
= 1;
1622 if (s
->nb_hw_voices_in
<= 0) {
1623 dolog ("Bogus number of capture voices %d, setting to 0\n",
1624 s
->nb_hw_voices_in
);
1625 s
->nb_hw_voices_in
= 0;
1630 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1636 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1637 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1638 done
= !audio_driver_init (s
, drvtab
[i
]);
1645 dolog ("Unknown audio driver `%s'\n", drvname
);
1646 dolog ("Run with -audio-help to list available drivers\n");
1651 for (i
= 0; !done
&& i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1652 if (drvtab
[i
]->can_be_default
) {
1653 done
= !audio_driver_init (s
, drvtab
[i
]);
1659 done
= !audio_driver_init (s
, &no_audio_driver
);
1661 dolog ("Could not initialize audio subsystem\n");
1664 dolog ("warning: Using timer based audio emulation\n");
1669 VMChangeStateEntry
*e
;
1671 if (conf
.period
.hz
<= 0) {
1672 if (conf
.period
.hz
< 0) {
1673 dolog ("warning: Timer period is negative - %d "
1674 "treating as zero\n",
1677 conf
.period
.ticks
= 1;
1680 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hz
;
1683 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1685 dolog ("warning: Could not register change state handler\n"
1686 "(Audio can continue looping even after stopping the VM)\n");
1690 qemu_del_timer (s
->ts
);
1694 LIST_INIT (&s
->card_head
);
1695 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1696 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1700 int AUD_add_capture (
1703 struct audio_capture_ops
*ops
,
1707 CaptureVoiceOut
*cap
;
1708 struct capture_callback
*cb
;
1712 s
= &glob_audio_state
;
1715 if (audio_validate_settigs (as
)) {
1716 dolog ("Invalid settings were passed when trying to add capture\n");
1717 audio_print_settings (as
);
1721 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1723 dolog ("Could not allocate capture callback information, size %zu\n",
1728 cb
->opaque
= cb_opaque
;
1730 cap
= audio_pcm_capture_find_specific (s
, as
);
1732 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1737 CaptureVoiceOut
*cap
;
1739 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1741 dolog ("Could not allocate capture voice, size %zu\n",
1747 LIST_INIT (&hw
->sw_head
);
1748 LIST_INIT (&cap
->cb_head
);
1750 /* XXX find a more elegant way */
1751 hw
->samples
= 4096 * 4;
1752 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1753 sizeof (st_sample_t
));
1755 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1760 audio_pcm_init_info (&hw
->info
, as
);
1762 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1764 dolog ("Could not allocate capture buffer "
1765 "(%d samples, each %d bytes)\n",
1766 hw
->samples
, 1 << hw
->info
.shift
);
1770 hw
->clip
= mixeng_clip
1771 [hw
->info
.nchannels
== 2]
1773 [hw
->info
.swap_endianness
]
1774 [hw
->info
.bits
== 16];
1776 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1777 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1780 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1781 audio_attach_capture (s
, hw
);
1786 qemu_free (cap
->hw
.mix_buf
);