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
);
514 AUD_log (NULL
, " endianness=");
515 switch (as
->endianness
) {
517 AUD_log (NULL
, "little");
520 AUD_log (NULL
, "big");
523 AUD_log (NULL
, "invalid");
526 AUD_log (NULL
, "\n");
529 static int audio_validate_settings (audsettings_t
*as
)
533 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
534 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
547 invalid
|= as
->freq
<= 0;
548 return invalid
? -1 : 0;
551 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, audsettings_t
*as
)
553 int bits
= 8, sign
= 0;
567 return info
->freq
== as
->freq
568 && info
->nchannels
== as
->nchannels
569 && info
->sign
== sign
570 && info
->bits
== bits
571 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
574 void audio_pcm_init_info (struct audio_pcm_info
*info
, audsettings_t
*as
)
576 int bits
= 8, sign
= 0;
591 info
->freq
= as
->freq
;
594 info
->nchannels
= as
->nchannels
;
595 info
->shift
= (as
->nchannels
== 2) + (bits
== 16);
596 info
->align
= (1 << info
->shift
) - 1;
597 info
->bytes_per_second
= info
->freq
<< info
->shift
;
598 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
601 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
608 memset (buf
, 0x00, len
<< info
->shift
);
611 if (info
->bits
== 8) {
612 memset (buf
, 0x80, len
<< info
->shift
);
617 int shift
= info
->nchannels
- 1;
620 if (info
->swap_endianness
) {
624 for (i
= 0; i
< len
<< shift
; i
++) {
634 static void noop_conv (st_sample_t
*dst
, const void *src
,
635 int samples
, volume_t
*vol
)
643 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
648 CaptureVoiceOut
*cap
;
650 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
651 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
658 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
660 struct capture_callback
*cb
;
663 dolog ("notification %d sent\n", cmd
);
665 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
666 cb
->ops
.notify (cb
->opaque
, cmd
);
670 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
672 if (cap
->hw
.enabled
!= enabled
) {
673 audcnotification_e cmd
;
674 cap
->hw
.enabled
= enabled
;
675 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
676 audio_notify_capture (cap
, cmd
);
680 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
682 HWVoiceOut
*hw
= &cap
->hw
;
686 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
692 audio_capture_maybe_changed (cap
, enabled
);
695 static void audio_detach_capture (HWVoiceOut
*hw
)
697 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
700 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
701 SWVoiceOut
*sw
= &sc
->sw
;
702 CaptureVoiceOut
*cap
= sc
->cap
;
703 int was_active
= sw
->active
;
706 st_rate_stop (sw
->rate
);
710 LIST_REMOVE (sw
, entries
);
711 LIST_REMOVE (sc
, entries
);
714 /* We have removed soft voice from the capture:
715 this might have changed the overall status of the capture
716 since this might have been the only active voice */
717 audio_recalc_and_notify_capture (cap
);
723 static int audio_attach_capture (AudioState
*s
, HWVoiceOut
*hw
)
725 CaptureVoiceOut
*cap
;
727 audio_detach_capture (hw
);
728 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
731 HWVoiceOut
*hw_cap
= &cap
->hw
;
733 sc
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sc
));
735 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
745 sw
->active
= hw
->enabled
;
746 sw
->conv
= noop_conv
;
747 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
748 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
750 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
754 LIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
755 LIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
757 asprintf (&sw
->name
, "for %p %d,%d,%d",
758 hw
, sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
759 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
762 audio_capture_maybe_changed (cap
, 1);
769 * Hard voice (capture)
771 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
774 int m
= hw
->total_samples_captured
;
776 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
778 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
784 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
786 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
787 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
788 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
795 * Soft voice (capture)
797 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
799 HWVoiceIn
*hw
= sw
->hw
;
800 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
803 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
804 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
808 rpos
= hw
->wpos
- live
;
813 return hw
->samples
+ rpos
;
817 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
819 HWVoiceIn
*hw
= sw
->hw
;
820 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
821 st_sample_t
*src
, *dst
= sw
->buf
;
823 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
825 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
826 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
827 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
831 samples
= size
>> sw
->info
.shift
;
836 swlim
= (live
* sw
->ratio
) >> 32;
837 swlim
= audio_MIN (swlim
, samples
);
840 src
= hw
->conv_buf
+ rpos
;
841 isamp
= hw
->wpos
- rpos
;
844 isamp
= hw
->samples
- rpos
;
852 if (audio_bug (AUDIO_FUNC
, osamp
< 0)) {
853 dolog ("osamp=%d\n", osamp
);
857 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
859 rpos
= (rpos
+ isamp
) % hw
->samples
;
865 sw
->clip (buf
, sw
->buf
, ret
);
866 sw
->total_hw_samples_acquired
+= total
;
867 return ret
<< sw
->info
.shift
;
871 * Hard voice (playback)
873 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
879 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
880 if (sw
->active
|| !sw
->empty
) {
881 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
890 int audio_pcm_hw_get_live_out2 (HWVoiceOut
*hw
, int *nb_live
)
894 smin
= audio_pcm_hw_find_min_out (hw
, nb_live
);
902 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
903 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
910 int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
)
915 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
916 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
917 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
924 * Soft voice (playback)
926 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
928 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
929 int ret
= 0, pos
= 0, total
= 0;
935 hwsamples
= sw
->hw
->samples
;
937 live
= sw
->total_hw_samples_mixed
;
938 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hwsamples
)){
939 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
943 if (live
== hwsamples
) {
945 dolog ("%s is full %d\n", sw
->name
, live
);
950 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
951 samples
= size
>> sw
->info
.shift
;
953 dead
= hwsamples
- live
;
954 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
955 swlim
= audio_MIN (swlim
, samples
);
957 sw
->conv (sw
->buf
, buf
, swlim
, &sw
->vol
);
961 dead
= hwsamples
- live
;
962 left
= hwsamples
- wpos
;
963 blck
= audio_MIN (dead
, left
);
972 sw
->hw
->mix_buf
+ wpos
,
980 wpos
= (wpos
+ osamp
) % hwsamples
;
984 sw
->total_hw_samples_mixed
+= total
;
985 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
989 "%s: write size %d ret %d total sw %d\n",
991 size
>> sw
->info
.shift
,
993 sw
->total_hw_samples_mixed
997 return ret
<< sw
->info
.shift
;
1001 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1003 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1004 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1009 #include "audio_template.h"
1011 #include "audio_template.h"
1013 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1018 /* XXX: Consider options */
1022 if (!sw
->hw
->enabled
) {
1023 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1027 bytes
= sw
->hw
->pcm_ops
->write (sw
, buf
, size
);
1031 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1036 /* XXX: Consider options */
1040 if (!sw
->hw
->enabled
) {
1041 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1045 bytes
= sw
->hw
->pcm_ops
->read (sw
, buf
, size
);
1049 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1051 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1054 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1063 if (sw
->active
!= on
) {
1064 SWVoiceOut
*temp_sw
;
1068 hw
->pending_disable
= 0;
1071 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
);
1078 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1079 temp_sw
= temp_sw
->entries
.le_next
) {
1080 nb_active
+= temp_sw
->active
!= 0;
1083 hw
->pending_disable
= nb_active
== 1;
1087 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1088 sc
->sw
.active
= hw
->enabled
;
1090 audio_capture_maybe_changed (sc
->cap
, 1);
1097 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1106 if (sw
->active
!= on
) {
1112 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
);
1114 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1120 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1121 temp_sw
= temp_sw
->entries
.le_next
) {
1122 nb_active
+= temp_sw
->active
!= 0;
1125 if (nb_active
== 1) {
1127 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1135 static int audio_get_avail (SWVoiceIn
*sw
)
1143 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1144 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1145 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1150 "%s: get_avail live %d ret %" PRId64
"\n",
1152 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1155 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1158 static int audio_get_free (SWVoiceOut
*sw
)
1166 live
= sw
->total_hw_samples_mixed
;
1168 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> sw
->hw
->samples
)) {
1169 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1173 dead
= sw
->hw
->samples
- live
;
1176 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1178 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1181 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1184 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1191 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1192 SWVoiceOut
*sw
= &sc
->sw
;
1197 int till_end_of_hw
= hw
->samples
- rpos2
;
1198 int to_write
= audio_MIN (till_end_of_hw
, n
);
1199 int bytes
= to_write
<< hw
->info
.shift
;
1202 sw
->buf
= hw
->mix_buf
+ rpos2
;
1203 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1204 if (written
- bytes
) {
1205 dolog ("Could not mix %d bytes into a capture "
1206 "buffer, mixed %d\n",
1211 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1216 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1217 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1218 mixeng_clear (hw
->mix_buf
, samples
- n
);
1221 static void audio_run_out (AudioState
*s
)
1223 HWVoiceOut
*hw
= NULL
;
1226 while ((hw
= audio_pcm_hw_find_any_enabled_out (s
, hw
))) {
1228 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1230 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
1235 if (audio_bug (AUDIO_FUNC
, live
< 0 || live
> hw
->samples
)) {
1236 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1240 if (hw
->pending_disable
&& !nb_live
) {
1243 dolog ("Disabling voice\n");
1246 hw
->pending_disable
= 0;
1247 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1248 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1250 audio_recalc_and_notify_capture (sc
->cap
);
1256 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1258 free
= audio_get_free (sw
);
1260 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1267 prev_rpos
= hw
->rpos
;
1268 played
= hw
->pcm_ops
->run_out (hw
);
1269 if (audio_bug (AUDIO_FUNC
, hw
->rpos
>= hw
->samples
)) {
1270 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1271 hw
->rpos
, hw
->samples
, played
);
1276 dolog ("played=%d\n", played
);
1280 hw
->ts_helper
+= played
;
1281 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1284 cleanup_required
= 0;
1285 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1286 if (!sw
->active
&& sw
->empty
) {
1290 if (audio_bug (AUDIO_FUNC
, played
> sw
->total_hw_samples_mixed
)) {
1291 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1292 played
, sw
->total_hw_samples_mixed
);
1293 played
= sw
->total_hw_samples_mixed
;
1296 sw
->total_hw_samples_mixed
-= played
;
1298 if (!sw
->total_hw_samples_mixed
) {
1300 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1304 free
= audio_get_free (sw
);
1306 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1311 if (cleanup_required
) {
1314 sw
= hw
->sw_head
.lh_first
;
1316 sw1
= sw
->entries
.le_next
;
1317 if (!sw
->active
&& !sw
->callback
.fn
) {
1319 dolog ("Finishing with old voice\n");
1321 audio_close_out (s
, sw
);
1329 static void audio_run_in (AudioState
*s
)
1331 HWVoiceIn
*hw
= NULL
;
1333 while ((hw
= audio_pcm_hw_find_any_enabled_in (s
, hw
))) {
1337 captured
= hw
->pcm_ops
->run_in (hw
);
1339 min
= audio_pcm_hw_find_min_in (hw
);
1340 hw
->total_samples_captured
+= captured
- min
;
1341 hw
->ts_helper
+= captured
;
1343 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1344 sw
->total_hw_samples_acquired
-= min
;
1349 avail
= audio_get_avail (sw
);
1351 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1358 static void audio_run_capture (AudioState
*s
)
1360 CaptureVoiceOut
*cap
;
1362 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1363 int live
, rpos
, captured
;
1364 HWVoiceOut
*hw
= &cap
->hw
;
1367 captured
= live
= audio_pcm_hw_get_live_out (hw
);
1370 int left
= hw
->samples
- rpos
;
1371 int to_capture
= audio_MIN (live
, left
);
1373 struct capture_callback
*cb
;
1375 src
= hw
->mix_buf
+ rpos
;
1376 hw
->clip (cap
->buf
, src
, to_capture
);
1377 mixeng_clear (src
, to_capture
);
1379 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1380 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1381 to_capture
<< hw
->info
.shift
);
1383 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1388 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1389 if (!sw
->active
&& sw
->empty
) {
1393 if (audio_bug (AUDIO_FUNC
, captured
> sw
->total_hw_samples_mixed
)) {
1394 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1395 captured
, sw
->total_hw_samples_mixed
);
1396 captured
= sw
->total_hw_samples_mixed
;
1399 sw
->total_hw_samples_mixed
-= captured
;
1400 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1405 static void audio_timer (void *opaque
)
1407 AudioState
*s
= opaque
;
1411 audio_run_capture (s
);
1413 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1416 static struct audio_option audio_options
[] = {
1418 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_out
.enabled
,
1419 "Use fixed settings for host DAC", NULL
, 0},
1421 {"DAC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_out
.settings
.freq
,
1422 "Frequency for fixed host DAC", NULL
, 0},
1424 {"DAC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_out
.settings
.fmt
,
1425 "Format for fixed host DAC", NULL
, 0},
1427 {"DAC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_out
.settings
.nchannels
,
1428 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL
, 0},
1430 {"DAC_VOICES", AUD_OPT_INT
, &conf
.fixed_out
.nb_voices
,
1431 "Number of voices for DAC", NULL
, 0},
1434 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL
, &conf
.fixed_in
.enabled
,
1435 "Use fixed settings for host ADC", NULL
, 0},
1437 {"ADC_FIXED_FREQ", AUD_OPT_INT
, &conf
.fixed_in
.settings
.freq
,
1438 "Frequency for fixed host ADC", NULL
, 0},
1440 {"ADC_FIXED_FMT", AUD_OPT_FMT
, &conf
.fixed_in
.settings
.fmt
,
1441 "Format for fixed host ADC", NULL
, 0},
1443 {"ADC_FIXED_CHANNELS", AUD_OPT_INT
, &conf
.fixed_in
.settings
.nchannels
,
1444 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL
, 0},
1446 {"ADC_VOICES", AUD_OPT_INT
, &conf
.fixed_in
.nb_voices
,
1447 "Number of voices for ADC", NULL
, 0},
1450 {"TIMER_PERIOD", AUD_OPT_INT
, &conf
.period
.hz
,
1451 "Timer period in HZ (0 - use lowest possible)", NULL
, 0},
1453 {"PLIVE", AUD_OPT_BOOL
, &conf
.plive
,
1454 "(undocumented)", NULL
, 0},
1456 {"LOG_TO_MONITOR", AUD_OPT_BOOL
, &conf
.log_to_monitor
,
1457 "print logging messages to montior instead of stderr", NULL
, 0},
1459 {NULL
, 0, NULL
, NULL
, NULL
, 0}
1462 static void audio_pp_nb_voices (const char *typ
, int nb
)
1466 printf ("Does not support %s\n", typ
);
1469 printf ("One %s voice\n", typ
);
1472 printf ("Theoretically supports many %s voices\n", typ
);
1475 printf ("Theoretically supports upto %d %s voices\n", nb
, typ
);
1481 void AUD_help (void)
1485 audio_process_options ("AUDIO", audio_options
);
1486 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1487 struct audio_driver
*d
= drvtab
[i
];
1489 audio_process_options (d
->name
, d
->options
);
1493 printf ("Audio options:\n");
1494 audio_print_options ("AUDIO", audio_options
);
1497 printf ("Available drivers:\n");
1499 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1500 struct audio_driver
*d
= drvtab
[i
];
1502 printf ("Name: %s\n", d
->name
);
1503 printf ("Description: %s\n", d
->descr
);
1505 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1506 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1509 printf ("Options:\n");
1510 audio_print_options (d
->name
, d
->options
);
1513 printf ("No options\n");
1519 "Options are settable through environment variables.\n"
1522 " set QEMU_AUDIO_DRV=wav\n"
1523 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1525 " export QEMU_AUDIO_DRV=wav\n"
1526 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1527 "(for csh replace export with setenv in the above)\n"
1533 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1536 audio_process_options (drv
->name
, drv
->options
);
1538 s
->drv_opaque
= drv
->init ();
1540 if (s
->drv_opaque
) {
1541 audio_init_nb_voices_out (s
, drv
);
1542 audio_init_nb_voices_in (s
, drv
);
1547 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1552 static void audio_vm_change_state_handler (void *opaque
, int running
)
1554 AudioState
*s
= opaque
;
1555 HWVoiceOut
*hwo
= NULL
;
1556 HWVoiceIn
*hwi
= NULL
;
1557 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1559 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1560 hwo
->pcm_ops
->ctl_out (hwo
, op
);
1563 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1564 hwi
->pcm_ops
->ctl_in (hwi
, op
);
1568 static void audio_atexit (void)
1570 AudioState
*s
= &glob_audio_state
;
1571 HWVoiceOut
*hwo
= NULL
;
1572 HWVoiceIn
*hwi
= NULL
;
1574 while ((hwo
= audio_pcm_hw_find_any_enabled_out (s
, hwo
))) {
1577 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1578 hwo
->pcm_ops
->fini_out (hwo
);
1580 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1581 CaptureVoiceOut
*cap
= sc
->cap
;
1582 struct capture_callback
*cb
;
1584 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1585 cb
->ops
.destroy (cb
->opaque
);
1590 while ((hwi
= audio_pcm_hw_find_any_enabled_in (s
, hwi
))) {
1591 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1592 hwi
->pcm_ops
->fini_in (hwi
);
1596 s
->drv
->fini (s
->drv_opaque
);
1600 static void audio_save (QEMUFile
*f
, void *opaque
)
1606 static int audio_load (QEMUFile
*f
, void *opaque
, int version_id
)
1611 if (version_id
!= 1) {
1618 void AUD_register_card (AudioState
*s
, const char *name
, QEMUSoundCard
*card
)
1621 card
->name
= qemu_strdup (name
);
1622 memset (&card
->entries
, 0, sizeof (card
->entries
));
1623 LIST_INSERT_HEAD (&s
->card_head
, card
, entries
);
1626 void AUD_remove_card (QEMUSoundCard
*card
)
1628 LIST_REMOVE (card
, entries
);
1630 qemu_free (card
->name
);
1633 AudioState
*AUD_init (void)
1637 const char *drvname
;
1638 AudioState
*s
= &glob_audio_state
;
1640 LIST_INIT (&s
->hw_head_out
);
1641 LIST_INIT (&s
->hw_head_in
);
1642 LIST_INIT (&s
->cap_head
);
1643 atexit (audio_atexit
);
1645 s
->ts
= qemu_new_timer (vm_clock
, audio_timer
, s
);
1647 dolog ("Could not create audio timer\n");
1651 audio_process_options ("AUDIO", audio_options
);
1653 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1654 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1656 if (s
->nb_hw_voices_out
<= 0) {
1657 dolog ("Bogus number of playback voices %d, setting to 1\n",
1658 s
->nb_hw_voices_out
);
1659 s
->nb_hw_voices_out
= 1;
1662 if (s
->nb_hw_voices_in
<= 0) {
1663 dolog ("Bogus number of capture voices %d, setting to 0\n",
1664 s
->nb_hw_voices_in
);
1665 s
->nb_hw_voices_in
= 0;
1670 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1676 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1677 if (!strcmp (drvname
, drvtab
[i
]->name
)) {
1678 done
= !audio_driver_init (s
, drvtab
[i
]);
1685 dolog ("Unknown audio driver `%s'\n", drvname
);
1686 dolog ("Run with -audio-help to list available drivers\n");
1691 for (i
= 0; !done
&& i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
1692 if (drvtab
[i
]->can_be_default
) {
1693 done
= !audio_driver_init (s
, drvtab
[i
]);
1699 done
= !audio_driver_init (s
, &no_audio_driver
);
1701 dolog ("Could not initialize audio subsystem\n");
1704 dolog ("warning: Using timer based audio emulation\n");
1709 VMChangeStateEntry
*e
;
1711 if (conf
.period
.hz
<= 0) {
1712 if (conf
.period
.hz
< 0) {
1713 dolog ("warning: Timer period is negative - %d "
1714 "treating as zero\n",
1717 conf
.period
.ticks
= 1;
1720 conf
.period
.ticks
= ticks_per_sec
/ conf
.period
.hz
;
1723 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1725 dolog ("warning: Could not register change state handler\n"
1726 "(Audio can continue looping even after stopping the VM)\n");
1730 qemu_del_timer (s
->ts
);
1734 LIST_INIT (&s
->card_head
);
1735 register_savevm ("audio", 0, 1, audio_save
, audio_load
, s
);
1736 qemu_mod_timer (s
->ts
, qemu_get_clock (vm_clock
) + conf
.period
.ticks
);
1740 CaptureVoiceOut
*AUD_add_capture (
1743 struct audio_capture_ops
*ops
,
1747 CaptureVoiceOut
*cap
;
1748 struct capture_callback
*cb
;
1752 s
= &glob_audio_state
;
1755 if (audio_validate_settings (as
)) {
1756 dolog ("Invalid settings were passed when trying to add capture\n");
1757 audio_print_settings (as
);
1761 cb
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cb
));
1763 dolog ("Could not allocate capture callback information, size %zu\n",
1768 cb
->opaque
= cb_opaque
;
1770 cap
= audio_pcm_capture_find_specific (s
, as
);
1772 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1777 CaptureVoiceOut
*cap
;
1779 cap
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*cap
));
1781 dolog ("Could not allocate capture voice, size %zu\n",
1787 LIST_INIT (&hw
->sw_head
);
1788 LIST_INIT (&cap
->cb_head
);
1790 /* XXX find a more elegant way */
1791 hw
->samples
= 4096 * 4;
1792 hw
->mix_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
,
1793 sizeof (st_sample_t
));
1795 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1800 audio_pcm_init_info (&hw
->info
, as
);
1802 cap
->buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
1804 dolog ("Could not allocate capture buffer "
1805 "(%d samples, each %d bytes)\n",
1806 hw
->samples
, 1 << hw
->info
.shift
);
1810 hw
->clip
= mixeng_clip
1811 [hw
->info
.nchannels
== 2]
1813 [hw
->info
.swap_endianness
]
1814 [hw
->info
.bits
== 16];
1816 LIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1817 LIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1820 while ((hw
= audio_pcm_hw_find_any_out (s
, hw
))) {
1821 audio_attach_capture (s
, hw
);
1826 qemu_free (cap
->hw
.mix_buf
);
1836 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1838 struct capture_callback
*cb
;
1840 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1841 if (cb
->opaque
== cb_opaque
) {
1842 cb
->ops
.destroy (cb_opaque
);
1843 LIST_REMOVE (cb
, entries
);
1846 if (!cap
->cb_head
.lh_first
) {
1847 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1850 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1851 #ifdef DEBUG_CAPTURE
1852 dolog ("freeing %s\n", sw
->name
);
1855 sw1
= sw
->entries
.le_next
;
1857 st_rate_stop (sw
->rate
);
1860 LIST_REMOVE (sw
, entries
);
1861 LIST_REMOVE (sc
, entries
);
1865 LIST_REMOVE (cap
, entries
);