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
25 #include "qemu/osdep.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qapi-visit-audio.h"
33 #include "sysemu/sysemu.h"
34 #include "qemu/cutils.h"
35 #include "qemu/module.h"
36 #include "sysemu/replay.h"
39 #define AUDIO_CAP "audio"
40 #include "audio_int.h"
42 /* #define DEBUG_LIVE */
43 /* #define DEBUG_OUT */
44 /* #define DEBUG_CAPTURE */
45 /* #define DEBUG_POLL */
47 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
50 /* Order of CONFIG_AUDIO_DRIVERS is import.
51 The 1st one is the one used by default, that is the reason
52 that we generate the list.
54 const char *audio_prio_list
[] = {
62 static QLIST_HEAD(, audio_driver
) audio_drivers
;
63 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
65 void audio_driver_register(audio_driver
*drv
)
67 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
70 audio_driver
*audio_driver_lookup(const char *name
)
72 struct audio_driver
*d
;
74 QLIST_FOREACH(d
, &audio_drivers
, next
) {
75 if (strcmp(name
, d
->name
) == 0) {
80 audio_module_load_one(name
);
81 QLIST_FOREACH(d
, &audio_drivers
, next
) {
82 if (strcmp(name
, d
->name
) == 0) {
90 static AudioState glob_audio_state
;
92 const struct mixeng_volume nominal_volume
= {
103 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
106 int audio_bug (const char *funcname
, int cond
)
111 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
114 AUD_log (NULL
, "Save all your work and restart without audio\n");
115 AUD_log (NULL
, "I am sorry\n");
117 AUD_log (NULL
, "Context:\n");
119 #if defined AUDIO_BREAKPOINT_ON_BUG
120 # if defined HOST_I386
121 # if defined __GNUC__
123 # elif defined _MSC_VER
138 static inline int audio_bits_to_index (int bits
)
151 audio_bug ("bits_to_index", 1);
152 AUD_log (NULL
, "invalid bits %d\n", bits
);
157 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
163 cond
= !nmemb
|| !size
;
167 if (audio_bug ("audio_calloc", cond
)) {
168 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
170 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
174 return g_malloc0 (len
);
177 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
180 fprintf(stderr
, "%s: ", cap
);
183 vfprintf(stderr
, fmt
, ap
);
186 void AUD_log (const char *cap
, const char *fmt
, ...)
191 AUD_vlog (cap
, fmt
, ap
);
195 static void audio_print_settings (struct audsettings
*as
)
197 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
200 case AUDIO_FORMAT_S8
:
201 AUD_log (NULL
, "S8");
203 case AUDIO_FORMAT_U8
:
204 AUD_log (NULL
, "U8");
206 case AUDIO_FORMAT_S16
:
207 AUD_log (NULL
, "S16");
209 case AUDIO_FORMAT_U16
:
210 AUD_log (NULL
, "U16");
212 case AUDIO_FORMAT_S32
:
213 AUD_log (NULL
, "S32");
215 case AUDIO_FORMAT_U32
:
216 AUD_log (NULL
, "U32");
219 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
223 AUD_log (NULL
, " endianness=");
224 switch (as
->endianness
) {
226 AUD_log (NULL
, "little");
229 AUD_log (NULL
, "big");
232 AUD_log (NULL
, "invalid");
235 AUD_log (NULL
, "\n");
238 static int audio_validate_settings (struct audsettings
*as
)
242 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
243 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
246 case AUDIO_FORMAT_S8
:
247 case AUDIO_FORMAT_U8
:
248 case AUDIO_FORMAT_S16
:
249 case AUDIO_FORMAT_U16
:
250 case AUDIO_FORMAT_S32
:
251 case AUDIO_FORMAT_U32
:
258 invalid
|= as
->freq
<= 0;
259 return invalid
? -1 : 0;
262 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
264 int bits
= 8, sign
= 0;
267 case AUDIO_FORMAT_S8
:
270 case AUDIO_FORMAT_U8
:
273 case AUDIO_FORMAT_S16
:
276 case AUDIO_FORMAT_U16
:
280 case AUDIO_FORMAT_S32
:
283 case AUDIO_FORMAT_U32
:
290 return info
->freq
== as
->freq
291 && info
->nchannels
== as
->nchannels
292 && info
->sign
== sign
293 && info
->bits
== bits
294 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
297 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
299 int bits
= 8, sign
= 0, shift
= 0;
302 case AUDIO_FORMAT_S8
:
304 case AUDIO_FORMAT_U8
:
307 case AUDIO_FORMAT_S16
:
309 case AUDIO_FORMAT_U16
:
314 case AUDIO_FORMAT_S32
:
316 case AUDIO_FORMAT_U32
:
325 info
->freq
= as
->freq
;
328 info
->nchannels
= as
->nchannels
;
329 info
->shift
= (as
->nchannels
== 2) + shift
;
330 info
->align
= (1 << info
->shift
) - 1;
331 info
->bytes_per_second
= info
->freq
<< info
->shift
;
332 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
335 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
342 memset (buf
, 0x00, len
<< info
->shift
);
345 switch (info
->bits
) {
347 memset (buf
, 0x80, len
<< info
->shift
);
354 int shift
= info
->nchannels
- 1;
357 if (info
->swap_endianness
) {
361 for (i
= 0; i
< len
<< shift
; i
++) {
371 int shift
= info
->nchannels
- 1;
372 int32_t s
= INT32_MAX
;
374 if (info
->swap_endianness
) {
378 for (i
= 0; i
< len
<< shift
; i
++) {
385 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
395 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
402 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
403 struct audsettings
*as
406 CaptureVoiceOut
*cap
;
407 AudioState
*s
= &glob_audio_state
;
409 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
410 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
417 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
419 struct capture_callback
*cb
;
422 dolog ("notification %d sent\n", cmd
);
424 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
425 cb
->ops
.notify (cb
->opaque
, cmd
);
429 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
431 if (cap
->hw
.enabled
!= enabled
) {
432 audcnotification_e cmd
;
433 cap
->hw
.enabled
= enabled
;
434 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
435 audio_notify_capture (cap
, cmd
);
439 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
441 HWVoiceOut
*hw
= &cap
->hw
;
445 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
451 audio_capture_maybe_changed (cap
, enabled
);
454 static void audio_detach_capture (HWVoiceOut
*hw
)
456 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
459 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
460 SWVoiceOut
*sw
= &sc
->sw
;
461 CaptureVoiceOut
*cap
= sc
->cap
;
462 int was_active
= sw
->active
;
465 st_rate_stop (sw
->rate
);
469 QLIST_REMOVE (sw
, entries
);
470 QLIST_REMOVE (sc
, entries
);
473 /* We have removed soft voice from the capture:
474 this might have changed the overall status of the capture
475 since this might have been the only active voice */
476 audio_recalc_and_notify_capture (cap
);
482 static int audio_attach_capture (HWVoiceOut
*hw
)
484 AudioState
*s
= &glob_audio_state
;
485 CaptureVoiceOut
*cap
;
487 audio_detach_capture (hw
);
488 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
491 HWVoiceOut
*hw_cap
= &cap
->hw
;
493 sc
= g_malloc0(sizeof(*sc
));
500 sw
->active
= hw
->enabled
;
501 sw
->conv
= noop_conv
;
502 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
503 sw
->vol
= nominal_volume
;
504 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
506 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
510 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
511 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
513 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
514 hw
, sw
->info
.freq
, sw
->info
.bits
,
516 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
519 audio_capture_maybe_changed (cap
, 1);
526 * Hard voice (capture)
528 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
531 int m
= hw
->total_samples_captured
;
533 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
535 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
541 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
543 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
544 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
545 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
551 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
552 int live
, int pending
)
554 int left
= hw
->samples
- pending
;
555 int len
= audio_MIN (left
, live
);
559 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
560 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
561 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
562 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
564 hw
->clip (dst
, src
, samples_to_clip
);
566 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
567 len
-= samples_to_clip
;
568 clipped
+= samples_to_clip
;
574 * Soft voice (capture)
576 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
578 HWVoiceIn
*hw
= sw
->hw
;
579 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
582 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
583 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
587 rpos
= hw
->wpos
- live
;
592 return hw
->samples
+ rpos
;
596 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
598 HWVoiceIn
*hw
= sw
->hw
;
599 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
600 struct st_sample
*src
, *dst
= sw
->buf
;
602 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
604 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
605 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
606 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
610 samples
= size
>> sw
->info
.shift
;
615 swlim
= (live
* sw
->ratio
) >> 32;
616 swlim
= audio_MIN (swlim
, samples
);
619 src
= hw
->conv_buf
+ rpos
;
620 isamp
= hw
->wpos
- rpos
;
623 isamp
= hw
->samples
- rpos
;
631 if (audio_bug(__func__
, osamp
< 0)) {
632 dolog ("osamp=%d\n", osamp
);
636 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
638 rpos
= (rpos
+ isamp
) % hw
->samples
;
644 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
645 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
648 sw
->clip (buf
, sw
->buf
, ret
);
649 sw
->total_hw_samples_acquired
+= total
;
650 return ret
<< sw
->info
.shift
;
654 * Hard voice (playback)
656 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
662 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
663 if (sw
->active
|| !sw
->empty
) {
664 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
673 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
678 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
686 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
687 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
696 * Soft voice (playback)
698 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
700 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
701 int ret
= 0, pos
= 0, total
= 0;
707 hwsamples
= sw
->hw
->samples
;
709 live
= sw
->total_hw_samples_mixed
;
710 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
711 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
715 if (live
== hwsamples
) {
717 dolog ("%s is full %d\n", sw
->name
, live
);
722 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
723 samples
= size
>> sw
->info
.shift
;
725 dead
= hwsamples
- live
;
726 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
727 swlim
= audio_MIN (swlim
, samples
);
729 sw
->conv (sw
->buf
, buf
, swlim
);
731 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
732 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
737 dead
= hwsamples
- live
;
738 left
= hwsamples
- wpos
;
739 blck
= audio_MIN (dead
, left
);
748 sw
->hw
->mix_buf
+ wpos
,
756 wpos
= (wpos
+ osamp
) % hwsamples
;
760 sw
->total_hw_samples_mixed
+= total
;
761 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
765 "%s: write size %d ret %d total sw %d\n",
767 size
>> sw
->info
.shift
,
769 sw
->total_hw_samples_mixed
773 return ret
<< sw
->info
.shift
;
777 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
779 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
780 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
785 #include "audio_template.h"
787 #include "audio_template.h"
793 static bool audio_timer_running
;
794 static uint64_t audio_timer_last
;
796 static int audio_is_timer_needed (void)
798 HWVoiceIn
*hwi
= NULL
;
799 HWVoiceOut
*hwo
= NULL
;
801 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
802 if (!hwo
->poll_mode
) return 1;
804 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
805 if (!hwi
->poll_mode
) return 1;
810 static void audio_reset_timer (AudioState
*s
)
812 if (audio_is_timer_needed ()) {
813 timer_mod_anticipate_ns(s
->ts
,
814 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
815 if (!audio_timer_running
) {
816 audio_timer_running
= true;
817 audio_timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
818 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
822 if (audio_timer_running
) {
823 audio_timer_running
= false;
824 trace_audio_timer_stop();
829 static void audio_timer (void *opaque
)
832 AudioState
*s
= opaque
;
834 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
835 diff
= now
- audio_timer_last
;
836 if (diff
> s
->period_ticks
* 3 / 2) {
837 trace_audio_timer_delayed(diff
/ SCALE_MS
);
839 audio_timer_last
= now
;
842 audio_reset_timer(s
);
848 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
851 /* XXX: Consider options */
855 if (!sw
->hw
->enabled
) {
856 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
860 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
863 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
866 /* XXX: Consider options */
870 if (!sw
->hw
->enabled
) {
871 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
875 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
878 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
880 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
883 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
892 if (sw
->active
!= on
) {
893 AudioState
*s
= &glob_audio_state
;
898 hw
->pending_disable
= 0;
902 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
903 audio_reset_timer (s
);
911 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
912 temp_sw
= temp_sw
->entries
.le_next
) {
913 nb_active
+= temp_sw
->active
!= 0;
916 hw
->pending_disable
= nb_active
== 1;
920 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
921 sc
->sw
.active
= hw
->enabled
;
923 audio_capture_maybe_changed (sc
->cap
, 1);
930 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
939 if (sw
->active
!= on
) {
940 AudioState
*s
= &glob_audio_state
;
947 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
948 audio_reset_timer (s
);
951 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
957 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
958 temp_sw
= temp_sw
->entries
.le_next
) {
959 nb_active
+= temp_sw
->active
!= 0;
962 if (nb_active
== 1) {
964 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
972 static int audio_get_avail (SWVoiceIn
*sw
)
980 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
981 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
982 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
987 "%s: get_avail live %d ret %" PRId64
"\n",
989 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
992 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
995 static int audio_get_free (SWVoiceOut
*sw
)
1003 live
= sw
->total_hw_samples_mixed
;
1005 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1006 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1010 dead
= sw
->hw
->samples
- live
;
1013 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1015 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1018 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1021 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1028 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1029 SWVoiceOut
*sw
= &sc
->sw
;
1034 int till_end_of_hw
= hw
->samples
- rpos2
;
1035 int to_write
= audio_MIN (till_end_of_hw
, n
);
1036 int bytes
= to_write
<< hw
->info
.shift
;
1039 sw
->buf
= hw
->mix_buf
+ rpos2
;
1040 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1041 if (written
- bytes
) {
1042 dolog ("Could not mix %d bytes into a capture "
1043 "buffer, mixed %d\n",
1048 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1053 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1054 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1055 mixeng_clear (hw
->mix_buf
, samples
- n
);
1058 static void audio_run_out (AudioState
*s
)
1060 HWVoiceOut
*hw
= NULL
;
1063 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1065 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1067 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1072 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1073 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1077 if (hw
->pending_disable
&& !nb_live
) {
1080 dolog ("Disabling voice\n");
1083 hw
->pending_disable
= 0;
1084 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1085 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1087 audio_recalc_and_notify_capture (sc
->cap
);
1093 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1095 free
= audio_get_free (sw
);
1097 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1104 prev_rpos
= hw
->rpos
;
1105 played
= hw
->pcm_ops
->run_out (hw
, live
);
1106 replay_audio_out(&played
);
1107 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1108 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1109 hw
->rpos
, hw
->samples
, played
);
1114 dolog ("played=%d\n", played
);
1118 hw
->ts_helper
+= played
;
1119 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1122 cleanup_required
= 0;
1123 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1124 if (!sw
->active
&& sw
->empty
) {
1128 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1129 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1130 played
, sw
->total_hw_samples_mixed
);
1131 played
= sw
->total_hw_samples_mixed
;
1134 sw
->total_hw_samples_mixed
-= played
;
1136 if (!sw
->total_hw_samples_mixed
) {
1138 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1142 free
= audio_get_free (sw
);
1144 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1149 if (cleanup_required
) {
1152 sw
= hw
->sw_head
.lh_first
;
1154 sw1
= sw
->entries
.le_next
;
1155 if (!sw
->active
&& !sw
->callback
.fn
) {
1156 audio_close_out (sw
);
1164 static void audio_run_in (AudioState
*s
)
1166 HWVoiceIn
*hw
= NULL
;
1168 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1170 int captured
= 0, min
;
1172 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1173 captured
= hw
->pcm_ops
->run_in(hw
);
1175 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1177 min
= audio_pcm_hw_find_min_in (hw
);
1178 hw
->total_samples_captured
+= captured
- min
;
1179 hw
->ts_helper
+= captured
;
1181 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1182 sw
->total_hw_samples_acquired
-= min
;
1187 avail
= audio_get_avail (sw
);
1189 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1196 static void audio_run_capture (AudioState
*s
)
1198 CaptureVoiceOut
*cap
;
1200 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1201 int live
, rpos
, captured
;
1202 HWVoiceOut
*hw
= &cap
->hw
;
1205 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1208 int left
= hw
->samples
- rpos
;
1209 int to_capture
= audio_MIN (live
, left
);
1210 struct st_sample
*src
;
1211 struct capture_callback
*cb
;
1213 src
= hw
->mix_buf
+ rpos
;
1214 hw
->clip (cap
->buf
, src
, to_capture
);
1215 mixeng_clear (src
, to_capture
);
1217 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1218 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1219 to_capture
<< hw
->info
.shift
);
1221 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1226 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1227 if (!sw
->active
&& sw
->empty
) {
1231 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1232 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1233 captured
, sw
->total_hw_samples_mixed
);
1234 captured
= sw
->total_hw_samples_mixed
;
1237 sw
->total_hw_samples_mixed
-= captured
;
1238 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1243 void audio_run (const char *msg
)
1245 AudioState
*s
= &glob_audio_state
;
1249 audio_run_capture (s
);
1252 static double prevtime
;
1256 if (gettimeofday (&tv
, NULL
)) {
1257 perror ("audio_run: gettimeofday");
1261 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1262 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1263 prevtime
= currtime
;
1268 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1269 bool msg
, Audiodev
*dev
)
1271 s
->drv_opaque
= drv
->init(dev
);
1273 if (s
->drv_opaque
) {
1274 audio_init_nb_voices_out (drv
);
1275 audio_init_nb_voices_in (drv
);
1281 dolog("Could not init `%s' audio driver\n", drv
->name
);
1287 static void audio_vm_change_state_handler (void *opaque
, int running
,
1290 AudioState
*s
= opaque
;
1291 HWVoiceOut
*hwo
= NULL
;
1292 HWVoiceIn
*hwi
= NULL
;
1293 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1295 s
->vm_running
= running
;
1296 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1297 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1300 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1301 hwi
->pcm_ops
->ctl_in(hwi
, op
);
1303 audio_reset_timer (s
);
1306 static bool is_cleaning_up
;
1308 bool audio_is_cleaning_up(void)
1310 return is_cleaning_up
;
1313 void audio_cleanup(void)
1315 AudioState
*s
= &glob_audio_state
;
1316 HWVoiceOut
*hwo
, *hwon
;
1317 HWVoiceIn
*hwi
, *hwin
;
1319 is_cleaning_up
= true;
1320 QLIST_FOREACH_SAFE(hwo
, &glob_audio_state
.hw_head_out
, entries
, hwon
) {
1324 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1326 hwo
->pcm_ops
->fini_out (hwo
);
1328 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1329 CaptureVoiceOut
*cap
= sc
->cap
;
1330 struct capture_callback
*cb
;
1332 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1333 cb
->ops
.destroy (cb
->opaque
);
1336 QLIST_REMOVE(hwo
, entries
);
1339 QLIST_FOREACH_SAFE(hwi
, &glob_audio_state
.hw_head_in
, entries
, hwin
) {
1341 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1343 hwi
->pcm_ops
->fini_in (hwi
);
1344 QLIST_REMOVE(hwi
, entries
);
1348 s
->drv
->fini (s
->drv_opaque
);
1353 qapi_free_Audiodev(s
->dev
);
1358 static const VMStateDescription vmstate_audio
= {
1361 .minimum_version_id
= 1,
1362 .fields
= (VMStateField
[]) {
1363 VMSTATE_END_OF_LIST()
1367 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1369 static AudiodevListEntry
*audiodev_find(
1370 AudiodevListHead
*head
, const char *drvname
)
1372 AudiodevListEntry
*e
;
1373 QSIMPLEQ_FOREACH(e
, head
, next
) {
1374 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1382 static int audio_init(Audiodev
*dev
)
1386 const char *drvname
= NULL
;
1387 VMChangeStateEntry
*e
;
1388 AudioState
*s
= &glob_audio_state
;
1389 struct audio_driver
*driver
;
1390 /* silence gcc warning about uninitialized variable */
1391 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1395 dolog("Cannot create more than one audio backend, sorry\n");
1396 qapi_free_Audiodev(dev
);
1402 /* -audiodev option */
1403 drvname
= AudiodevDriver_str(dev
->driver
);
1405 /* legacy implicit initialization */
1406 head
= audio_handle_legacy_opts();
1408 * In case of legacy initialization, all Audiodevs in the list will have
1409 * the same configuration (except the driver), so it does't matter which
1410 * one we chose. We need an Audiodev to set up AudioState before we can
1411 * init a driver. Also note that dev at this point is still in the
1414 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1415 audio_validate_opts(dev
, &error_abort
);
1419 QLIST_INIT (&s
->hw_head_out
);
1420 QLIST_INIT (&s
->hw_head_in
);
1421 QLIST_INIT (&s
->cap_head
);
1422 atexit(audio_cleanup
);
1424 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1426 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1427 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1429 if (s
->nb_hw_voices_out
<= 0) {
1430 dolog ("Bogus number of playback voices %d, setting to 1\n",
1431 s
->nb_hw_voices_out
);
1432 s
->nb_hw_voices_out
= 1;
1435 if (s
->nb_hw_voices_in
<= 0) {
1436 dolog ("Bogus number of capture voices %d, setting to 0\n",
1437 s
->nb_hw_voices_in
);
1438 s
->nb_hw_voices_in
= 0;
1442 driver
= audio_driver_lookup(drvname
);
1444 done
= !audio_driver_init(s
, driver
, true, dev
);
1446 dolog ("Unknown audio driver `%s'\n", drvname
);
1449 for (i
= 0; audio_prio_list
[i
]; i
++) {
1450 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1451 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1454 s
->dev
= dev
= e
->dev
;
1455 audio_validate_opts(dev
, &error_abort
);
1456 done
= !audio_driver_init(s
, driver
, false, dev
);
1464 audio_free_audiodev_list(&head
);
1467 driver
= audio_driver_lookup("none");
1468 done
= !audio_driver_init(s
, driver
, false, dev
);
1470 dolog("warning: Using timer based audio emulation\n");
1473 if (dev
->timer_period
<= 0) {
1474 s
->period_ticks
= 1;
1476 s
->period_ticks
= dev
->timer_period
* SCALE_US
;
1479 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1481 dolog ("warning: Could not register change state handler\n"
1482 "(Audio can continue looping even after stopping the VM)\n");
1485 QLIST_INIT (&s
->card_head
);
1486 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1490 void audio_free_audiodev_list(AudiodevListHead
*head
)
1492 AudiodevListEntry
*e
;
1493 while ((e
= QSIMPLEQ_FIRST(head
))) {
1494 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1495 qapi_free_Audiodev(e
->dev
);
1500 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1503 card
->name
= g_strdup (name
);
1504 memset (&card
->entries
, 0, sizeof (card
->entries
));
1505 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1508 void AUD_remove_card (QEMUSoundCard
*card
)
1510 QLIST_REMOVE (card
, entries
);
1511 g_free (card
->name
);
1515 CaptureVoiceOut
*AUD_add_capture (
1516 struct audsettings
*as
,
1517 struct audio_capture_ops
*ops
,
1521 AudioState
*s
= &glob_audio_state
;
1522 CaptureVoiceOut
*cap
;
1523 struct capture_callback
*cb
;
1525 if (audio_validate_settings (as
)) {
1526 dolog ("Invalid settings were passed when trying to add capture\n");
1527 audio_print_settings (as
);
1531 cb
= g_malloc0(sizeof(*cb
));
1533 cb
->opaque
= cb_opaque
;
1535 cap
= audio_pcm_capture_find_specific (as
);
1537 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1542 CaptureVoiceOut
*cap
;
1544 cap
= g_malloc0(sizeof(*cap
));
1547 QLIST_INIT (&hw
->sw_head
);
1548 QLIST_INIT (&cap
->cb_head
);
1550 /* XXX find a more elegant way */
1551 hw
->samples
= 4096 * 4;
1552 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1554 audio_pcm_init_info (&hw
->info
, as
);
1556 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1558 hw
->clip
= mixeng_clip
1559 [hw
->info
.nchannels
== 2]
1561 [hw
->info
.swap_endianness
]
1562 [audio_bits_to_index (hw
->info
.bits
)];
1564 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1565 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1567 QLIST_FOREACH(hw
, &glob_audio_state
.hw_head_out
, entries
) {
1568 audio_attach_capture (hw
);
1574 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1576 struct capture_callback
*cb
;
1578 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1579 if (cb
->opaque
== cb_opaque
) {
1580 cb
->ops
.destroy (cb_opaque
);
1581 QLIST_REMOVE (cb
, entries
);
1584 if (!cap
->cb_head
.lh_first
) {
1585 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1588 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1589 #ifdef DEBUG_CAPTURE
1590 dolog ("freeing %s\n", sw
->name
);
1593 sw1
= sw
->entries
.le_next
;
1595 st_rate_stop (sw
->rate
);
1598 QLIST_REMOVE (sw
, entries
);
1599 QLIST_REMOVE (sc
, entries
);
1603 QLIST_REMOVE (cap
, entries
);
1604 g_free (cap
->hw
.mix_buf
);
1613 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1616 HWVoiceOut
*hw
= sw
->hw
;
1618 sw
->vol
.mute
= mute
;
1619 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1620 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1622 if (hw
->pcm_ops
->ctl_out
) {
1623 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1628 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1631 HWVoiceIn
*hw
= sw
->hw
;
1633 sw
->vol
.mute
= mute
;
1634 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1635 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1637 if (hw
->pcm_ops
->ctl_in
) {
1638 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1643 void audio_create_pdos(Audiodev
*dev
)
1645 switch (dev
->driver
) {
1646 #define CASE(DRIVER, driver, pdo_name) \
1647 case AUDIODEV_DRIVER_##DRIVER: \
1648 if (!dev->u.driver.has_in) { \
1649 dev->u.driver.in = g_malloc0( \
1650 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1651 dev->u.driver.has_in = true; \
1653 if (!dev->u.driver.has_out) { \
1654 dev->u.driver.out = g_malloc0( \
1655 sizeof(AudiodevAlsaPerDirectionOptions)); \
1656 dev->u.driver.has_out = true; \
1661 CASE(ALSA
, alsa
, Alsa
);
1662 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1663 CASE(DSOUND
, dsound
, );
1664 CASE(OSS
, oss
, Oss
);
1667 CASE(SPICE
, spice
, );
1670 case AUDIODEV_DRIVER__MAX
:
1675 static void audio_validate_per_direction_opts(
1676 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1678 if (!pdo
->has_fixed_settings
) {
1679 pdo
->has_fixed_settings
= true;
1680 pdo
->fixed_settings
= true;
1682 if (!pdo
->fixed_settings
&&
1683 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1685 "You can't use frequency, channels or format with fixed-settings=off");
1689 if (!pdo
->has_frequency
) {
1690 pdo
->has_frequency
= true;
1691 pdo
->frequency
= 44100;
1693 if (!pdo
->has_channels
) {
1694 pdo
->has_channels
= true;
1697 if (!pdo
->has_voices
) {
1698 pdo
->has_voices
= true;
1701 if (!pdo
->has_format
) {
1702 pdo
->has_format
= true;
1703 pdo
->format
= AUDIO_FORMAT_S16
;
1707 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1711 audio_create_pdos(dev
);
1713 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1715 error_propagate(errp
, err
);
1719 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1721 error_propagate(errp
, err
);
1725 if (!dev
->has_timer_period
) {
1726 dev
->has_timer_period
= true;
1727 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1731 void audio_parse_option(const char *opt
)
1733 AudiodevListEntry
*e
;
1734 Audiodev
*dev
= NULL
;
1736 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1737 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1740 audio_validate_opts(dev
, &error_fatal
);
1742 e
= g_malloc0(sizeof(AudiodevListEntry
));
1744 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1747 void audio_init_audiodevs(void)
1749 AudiodevListEntry
*e
;
1751 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1756 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1758 return (audsettings
) {
1759 .freq
= pdo
->frequency
,
1760 .nchannels
= pdo
->channels
,
1762 .endianness
= AUDIO_HOST_ENDIANNESS
,
1766 int audioformat_bytes_per_sample(AudioFormat fmt
)
1769 case AUDIO_FORMAT_U8
:
1770 case AUDIO_FORMAT_S8
:
1773 case AUDIO_FORMAT_U16
:
1774 case AUDIO_FORMAT_S16
:
1777 case AUDIO_FORMAT_U32
:
1778 case AUDIO_FORMAT_S32
:
1781 case AUDIO_FORMAT__MAX
:
1788 /* frames = freq * usec / 1e6 */
1789 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1790 audsettings
*as
, int def_usecs
)
1792 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1793 return (as
->freq
* usecs
+ 500000) / 1000000;
1796 /* samples = channels * frames = channels * freq * usec / 1e6 */
1797 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1798 audsettings
*as
, int def_usecs
)
1800 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1804 * bytes = bytes_per_sample * samples =
1805 * bytes_per_sample * channels * freq * usec / 1e6
1807 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1808 audsettings
*as
, int def_usecs
)
1810 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1811 audioformat_bytes_per_sample(as
->fmt
);