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"
27 #include "migration/vmstate.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 "qemu/cutils.h"
34 #include "qemu/module.h"
35 #include "sysemu/replay.h"
36 #include "sysemu/runstate.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 QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
91 QTAILQ_HEAD_INITIALIZER(audio_states
);
93 const struct mixeng_volume nominal_volume
= {
104 static bool legacy_config
= true;
106 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
109 int audio_bug (const char *funcname
, int cond
)
114 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
117 AUD_log (NULL
, "Save all your work and restart without audio\n");
118 AUD_log (NULL
, "I am sorry\n");
120 AUD_log (NULL
, "Context:\n");
122 #if defined AUDIO_BREAKPOINT_ON_BUG
123 # if defined HOST_I386
124 # if defined __GNUC__
126 # elif defined _MSC_VER
141 static inline int audio_bits_to_index (int bits
)
154 audio_bug ("bits_to_index", 1);
155 AUD_log (NULL
, "invalid bits %d\n", bits
);
160 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
166 cond
= !nmemb
|| !size
;
170 if (audio_bug ("audio_calloc", cond
)) {
171 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
173 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
177 return g_malloc0 (len
);
180 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
183 fprintf(stderr
, "%s: ", cap
);
186 vfprintf(stderr
, fmt
, ap
);
189 void AUD_log (const char *cap
, const char *fmt
, ...)
194 AUD_vlog (cap
, fmt
, ap
);
198 static void audio_print_settings (struct audsettings
*as
)
200 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
203 case AUDIO_FORMAT_S8
:
204 AUD_log (NULL
, "S8");
206 case AUDIO_FORMAT_U8
:
207 AUD_log (NULL
, "U8");
209 case AUDIO_FORMAT_S16
:
210 AUD_log (NULL
, "S16");
212 case AUDIO_FORMAT_U16
:
213 AUD_log (NULL
, "U16");
215 case AUDIO_FORMAT_S32
:
216 AUD_log (NULL
, "S32");
218 case AUDIO_FORMAT_U32
:
219 AUD_log (NULL
, "U32");
222 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
226 AUD_log (NULL
, " endianness=");
227 switch (as
->endianness
) {
229 AUD_log (NULL
, "little");
232 AUD_log (NULL
, "big");
235 AUD_log (NULL
, "invalid");
238 AUD_log (NULL
, "\n");
241 static int audio_validate_settings (struct audsettings
*as
)
245 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
246 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
249 case AUDIO_FORMAT_S8
:
250 case AUDIO_FORMAT_U8
:
251 case AUDIO_FORMAT_S16
:
252 case AUDIO_FORMAT_U16
:
253 case AUDIO_FORMAT_S32
:
254 case AUDIO_FORMAT_U32
:
261 invalid
|= as
->freq
<= 0;
262 return invalid
? -1 : 0;
265 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
267 int bits
= 8, sign
= 0;
270 case AUDIO_FORMAT_S8
:
273 case AUDIO_FORMAT_U8
:
276 case AUDIO_FORMAT_S16
:
279 case AUDIO_FORMAT_U16
:
283 case AUDIO_FORMAT_S32
:
286 case AUDIO_FORMAT_U32
:
293 return info
->freq
== as
->freq
294 && info
->nchannels
== as
->nchannels
295 && info
->sign
== sign
296 && info
->bits
== bits
297 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
300 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
302 int bits
= 8, sign
= 0, shift
= 0;
305 case AUDIO_FORMAT_S8
:
307 case AUDIO_FORMAT_U8
:
310 case AUDIO_FORMAT_S16
:
313 case AUDIO_FORMAT_U16
:
318 case AUDIO_FORMAT_S32
:
321 case AUDIO_FORMAT_U32
:
330 info
->freq
= as
->freq
;
333 info
->nchannels
= as
->nchannels
;
334 info
->shift
= (as
->nchannels
== 2) + shift
;
335 info
->align
= (1 << info
->shift
) - 1;
336 info
->bytes_per_second
= info
->freq
<< info
->shift
;
337 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
340 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
347 memset (buf
, 0x00, len
<< info
->shift
);
350 switch (info
->bits
) {
352 memset (buf
, 0x80, len
<< info
->shift
);
359 int shift
= info
->nchannels
- 1;
362 if (info
->swap_endianness
) {
366 for (i
= 0; i
< len
<< shift
; i
++) {
376 int shift
= info
->nchannels
- 1;
377 int32_t s
= INT32_MAX
;
379 if (info
->swap_endianness
) {
383 for (i
= 0; i
< len
<< shift
; i
++) {
390 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
400 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
407 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
408 struct audsettings
*as
)
410 CaptureVoiceOut
*cap
;
412 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
413 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
420 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
422 struct capture_callback
*cb
;
425 dolog ("notification %d sent\n", cmd
);
427 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
428 cb
->ops
.notify (cb
->opaque
, cmd
);
432 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
434 if (cap
->hw
.enabled
!= enabled
) {
435 audcnotification_e cmd
;
436 cap
->hw
.enabled
= enabled
;
437 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
438 audio_notify_capture (cap
, cmd
);
442 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
444 HWVoiceOut
*hw
= &cap
->hw
;
448 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
454 audio_capture_maybe_changed (cap
, enabled
);
457 static void audio_detach_capture (HWVoiceOut
*hw
)
459 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
462 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
463 SWVoiceOut
*sw
= &sc
->sw
;
464 CaptureVoiceOut
*cap
= sc
->cap
;
465 int was_active
= sw
->active
;
468 st_rate_stop (sw
->rate
);
472 QLIST_REMOVE (sw
, entries
);
473 QLIST_REMOVE (sc
, entries
);
476 /* We have removed soft voice from the capture:
477 this might have changed the overall status of the capture
478 since this might have been the only active voice */
479 audio_recalc_and_notify_capture (cap
);
485 static int audio_attach_capture (HWVoiceOut
*hw
)
487 AudioState
*s
= hw
->s
;
488 CaptureVoiceOut
*cap
;
490 audio_detach_capture (hw
);
491 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
494 HWVoiceOut
*hw_cap
= &cap
->hw
;
496 sc
= g_malloc0(sizeof(*sc
));
503 sw
->active
= hw
->enabled
;
504 sw
->conv
= noop_conv
;
505 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
506 sw
->vol
= nominal_volume
;
507 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
509 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
513 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
514 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
516 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
517 hw
, sw
->info
.freq
, sw
->info
.bits
,
519 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
522 audio_capture_maybe_changed (cap
, 1);
529 * Hard voice (capture)
531 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
534 size_t m
= hw
->total_samples_captured
;
536 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
538 m
= MIN (m
, sw
->total_hw_samples_acquired
);
544 size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
546 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
547 if (audio_bug(__func__
, live
> hw
->samples
)) {
548 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
554 size_t audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
,
555 size_t live
, size_t pending
)
557 size_t left
= hw
->samples
- pending
;
558 size_t len
= MIN (left
, live
);
562 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
563 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
564 size_t samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
565 size_t samples_to_clip
= MIN (len
, samples_till_end_of_buf
);
567 hw
->clip (dst
, src
, samples_to_clip
);
569 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
570 len
-= samples_to_clip
;
571 clipped
+= samples_to_clip
;
577 * Soft voice (capture)
579 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
581 HWVoiceIn
*hw
= sw
->hw
;
582 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
585 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
586 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
590 rpos
= hw
->wpos
- live
;
595 return hw
->samples
+ rpos
;
599 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
601 HWVoiceIn
*hw
= sw
->hw
;
602 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
603 struct st_sample
*src
, *dst
= sw
->buf
;
605 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
607 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
608 if (audio_bug(__func__
, live
> hw
->samples
)) {
609 dolog("live_in=%zu hw->samples=%zu\n", live
, hw
->samples
);
613 samples
= size
>> sw
->info
.shift
;
618 swlim
= (live
* sw
->ratio
) >> 32;
619 swlim
= MIN (swlim
, samples
);
622 src
= hw
->conv_buf
+ rpos
;
623 if (hw
->wpos
> rpos
) {
624 isamp
= hw
->wpos
- rpos
;
626 isamp
= hw
->samples
- rpos
;
634 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
636 rpos
= (rpos
+ isamp
) % hw
->samples
;
642 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
643 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
646 sw
->clip (buf
, sw
->buf
, ret
);
647 sw
->total_hw_samples_acquired
+= total
;
648 return ret
<< sw
->info
.shift
;
652 * Hard voice (playback)
654 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
660 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
661 if (sw
->active
|| !sw
->empty
) {
662 m
= MIN (m
, sw
->total_hw_samples_mixed
);
671 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
676 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
684 if (audio_bug(__func__
, live
> hw
->samples
)) {
685 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
694 * Soft voice (playback)
696 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
698 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
699 size_t ret
= 0, pos
= 0, total
= 0;
705 hwsamples
= sw
->hw
->samples
;
707 live
= sw
->total_hw_samples_mixed
;
708 if (audio_bug(__func__
, live
> hwsamples
)) {
709 dolog("live=%zu hw->samples=%zu\n", live
, hwsamples
);
713 if (live
== hwsamples
) {
715 dolog ("%s is full %d\n", sw
->name
, live
);
720 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
721 samples
= size
>> sw
->info
.shift
;
723 dead
= hwsamples
- live
;
724 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
725 swlim
= MIN (swlim
, samples
);
727 sw
->conv (sw
->buf
, buf
, swlim
);
729 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
730 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
735 dead
= hwsamples
- live
;
736 left
= hwsamples
- wpos
;
737 blck
= MIN (dead
, left
);
746 sw
->hw
->mix_buf
+ wpos
,
754 wpos
= (wpos
+ osamp
) % hwsamples
;
758 sw
->total_hw_samples_mixed
+= total
;
759 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
763 "%s: write size %zu ret %zu total sw %zu\n",
765 size
>> sw
->info
.shift
,
767 sw
->total_hw_samples_mixed
771 return ret
<< sw
->info
.shift
;
775 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
777 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
778 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
783 #include "audio_template.h"
785 #include "audio_template.h"
790 static int audio_is_timer_needed(AudioState
*s
)
792 HWVoiceIn
*hwi
= NULL
;
793 HWVoiceOut
*hwo
= NULL
;
795 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
796 if (!hwo
->poll_mode
) return 1;
798 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
799 if (!hwi
->poll_mode
) return 1;
804 static void audio_reset_timer (AudioState
*s
)
806 if (audio_is_timer_needed(s
)) {
807 timer_mod_anticipate_ns(s
->ts
,
808 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
809 if (!s
->timer_running
) {
810 s
->timer_running
= true;
811 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
812 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
816 if (s
->timer_running
) {
817 s
->timer_running
= false;
818 trace_audio_timer_stop();
823 static void audio_timer (void *opaque
)
826 AudioState
*s
= opaque
;
828 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
829 diff
= now
- s
->timer_last
;
830 if (diff
> s
->period_ticks
* 3 / 2) {
831 trace_audio_timer_delayed(diff
/ SCALE_MS
);
835 audio_run(s
, "timer");
836 audio_reset_timer(s
);
842 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
845 /* XXX: Consider options */
849 if (!sw
->hw
->enabled
) {
850 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
854 return audio_pcm_sw_write(sw
, buf
, size
);
857 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
860 /* XXX: Consider options */
864 if (!sw
->hw
->enabled
) {
865 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
869 return audio_pcm_sw_read(sw
, buf
, size
);
872 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
874 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
877 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
886 if (sw
->active
!= on
) {
887 AudioState
*s
= sw
->s
;
892 hw
->pending_disable
= 0;
896 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
897 audio_reset_timer (s
);
905 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
906 temp_sw
= temp_sw
->entries
.le_next
) {
907 nb_active
+= temp_sw
->active
!= 0;
910 hw
->pending_disable
= nb_active
== 1;
914 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
915 sc
->sw
.active
= hw
->enabled
;
917 audio_capture_maybe_changed (sc
->cap
, 1);
924 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
933 if (sw
->active
!= on
) {
934 AudioState
*s
= sw
->s
;
941 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
942 audio_reset_timer (s
);
945 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
951 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
952 temp_sw
= temp_sw
->entries
.le_next
) {
953 nb_active
+= temp_sw
->active
!= 0;
956 if (nb_active
== 1) {
958 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
966 static size_t audio_get_avail (SWVoiceIn
*sw
)
974 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
975 if (audio_bug(__func__
, live
> sw
->hw
->samples
)) {
976 dolog("live=%zu sw->hw->samples=%zu\n", live
, sw
->hw
->samples
);
981 "%s: get_avail live %d ret %" PRId64
"\n",
983 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
986 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
989 static size_t audio_get_free(SWVoiceOut
*sw
)
997 live
= sw
->total_hw_samples_mixed
;
999 if (audio_bug(__func__
, live
> sw
->hw
->samples
)) {
1000 dolog("live=%zu sw->hw->samples=%zu\n", live
, sw
->hw
->samples
);
1004 dead
= sw
->hw
->samples
- live
;
1007 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1009 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1012 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1015 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1023 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1024 SWVoiceOut
*sw
= &sc
->sw
;
1029 size_t till_end_of_hw
= hw
->samples
- rpos2
;
1030 size_t to_write
= MIN(till_end_of_hw
, n
);
1031 size_t bytes
= to_write
<< hw
->info
.shift
;
1034 sw
->buf
= hw
->mix_buf
+ rpos2
;
1035 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1036 if (written
- bytes
) {
1037 dolog("Could not mix %zu bytes into a capture "
1038 "buffer, mixed %zu\n",
1043 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1048 n
= MIN(samples
, hw
->samples
- rpos
);
1049 mixeng_clear(hw
->mix_buf
+ rpos
, n
);
1050 mixeng_clear(hw
->mix_buf
, samples
- n
);
1053 static void audio_run_out (AudioState
*s
)
1055 HWVoiceOut
*hw
= NULL
;
1058 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1059 size_t played
, live
, prev_rpos
, free
;
1060 int nb_live
, cleanup_required
;
1062 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1067 if (audio_bug(__func__
, live
> hw
->samples
)) {
1068 dolog ("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
1072 if (hw
->pending_disable
&& !nb_live
) {
1075 dolog ("Disabling voice\n");
1078 hw
->pending_disable
= 0;
1079 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1080 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1082 audio_recalc_and_notify_capture (sc
->cap
);
1088 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1090 free
= audio_get_free (sw
);
1092 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1099 prev_rpos
= hw
->rpos
;
1100 played
= hw
->pcm_ops
->run_out (hw
, live
);
1101 replay_audio_out(&played
);
1102 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1103 dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n",
1104 hw
->rpos
, hw
->samples
, played
);
1109 dolog("played=%zu\n", played
);
1113 hw
->ts_helper
+= played
;
1114 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1117 cleanup_required
= 0;
1118 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1119 if (!sw
->active
&& sw
->empty
) {
1123 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1124 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1125 played
, sw
->total_hw_samples_mixed
);
1126 played
= sw
->total_hw_samples_mixed
;
1129 sw
->total_hw_samples_mixed
-= played
;
1131 if (!sw
->total_hw_samples_mixed
) {
1133 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1137 free
= audio_get_free (sw
);
1139 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1144 if (cleanup_required
) {
1147 sw
= hw
->sw_head
.lh_first
;
1149 sw1
= sw
->entries
.le_next
;
1150 if (!sw
->active
&& !sw
->callback
.fn
) {
1151 audio_close_out (sw
);
1159 static void audio_run_in (AudioState
*s
)
1161 HWVoiceIn
*hw
= NULL
;
1163 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1165 size_t captured
= 0, min
;
1167 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1168 captured
= hw
->pcm_ops
->run_in(hw
);
1170 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1172 min
= audio_pcm_hw_find_min_in (hw
);
1173 hw
->total_samples_captured
+= captured
- min
;
1174 hw
->ts_helper
+= captured
;
1176 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1177 sw
->total_hw_samples_acquired
-= min
;
1182 avail
= audio_get_avail (sw
);
1184 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1191 static void audio_run_capture (AudioState
*s
)
1193 CaptureVoiceOut
*cap
;
1195 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1196 size_t live
, rpos
, captured
;
1197 HWVoiceOut
*hw
= &cap
->hw
;
1200 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1203 size_t left
= hw
->samples
- rpos
;
1204 size_t to_capture
= MIN(live
, left
);
1205 struct st_sample
*src
;
1206 struct capture_callback
*cb
;
1208 src
= hw
->mix_buf
+ rpos
;
1209 hw
->clip (cap
->buf
, src
, to_capture
);
1210 mixeng_clear (src
, to_capture
);
1212 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1213 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1214 to_capture
<< hw
->info
.shift
);
1216 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1221 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1222 if (!sw
->active
&& sw
->empty
) {
1226 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1227 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1228 captured
, sw
->total_hw_samples_mixed
);
1229 captured
= sw
->total_hw_samples_mixed
;
1232 sw
->total_hw_samples_mixed
-= captured
;
1233 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1238 void audio_run(AudioState
*s
, const char *msg
)
1242 audio_run_capture(s
);
1246 static double prevtime
;
1250 if (gettimeofday (&tv
, NULL
)) {
1251 perror ("audio_run: gettimeofday");
1255 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1256 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1257 prevtime
= currtime
;
1262 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1263 bool msg
, Audiodev
*dev
)
1265 s
->drv_opaque
= drv
->init(dev
);
1267 if (s
->drv_opaque
) {
1268 audio_init_nb_voices_out(s
, drv
);
1269 audio_init_nb_voices_in(s
, drv
);
1275 dolog("Could not init `%s' audio driver\n", drv
->name
);
1281 static void audio_vm_change_state_handler (void *opaque
, int running
,
1284 AudioState
*s
= opaque
;
1285 HWVoiceOut
*hwo
= NULL
;
1286 HWVoiceIn
*hwi
= NULL
;
1287 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1289 s
->vm_running
= running
;
1290 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1291 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1294 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1295 hwi
->pcm_ops
->ctl_in(hwi
, op
);
1297 audio_reset_timer (s
);
1300 static bool is_cleaning_up
;
1302 bool audio_is_cleaning_up(void)
1304 return is_cleaning_up
;
1307 static void free_audio_state(AudioState
*s
)
1309 HWVoiceOut
*hwo
, *hwon
;
1310 HWVoiceIn
*hwi
, *hwin
;
1312 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1316 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1318 hwo
->pcm_ops
->fini_out (hwo
);
1320 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1321 CaptureVoiceOut
*cap
= sc
->cap
;
1322 struct capture_callback
*cb
;
1324 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1325 cb
->ops
.destroy (cb
->opaque
);
1328 QLIST_REMOVE(hwo
, entries
);
1331 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1333 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1335 hwi
->pcm_ops
->fini_in (hwi
);
1336 QLIST_REMOVE(hwi
, entries
);
1340 s
->drv
->fini (s
->drv_opaque
);
1345 qapi_free_Audiodev(s
->dev
);
1357 void audio_cleanup(void)
1359 is_cleaning_up
= true;
1360 while (!QTAILQ_EMPTY(&audio_states
)) {
1361 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1362 QTAILQ_REMOVE(&audio_states
, s
, list
);
1363 free_audio_state(s
);
1367 static const VMStateDescription vmstate_audio
= {
1370 .minimum_version_id
= 1,
1371 .fields
= (VMStateField
[]) {
1372 VMSTATE_END_OF_LIST()
1376 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1378 static AudiodevListEntry
*audiodev_find(
1379 AudiodevListHead
*head
, const char *drvname
)
1381 AudiodevListEntry
*e
;
1382 QSIMPLEQ_FOREACH(e
, head
, next
) {
1383 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1392 * if we have dev, this function was called because of an -audiodev argument =>
1393 * initialize a new state with it
1394 * if dev == NULL => legacy implicit initialization, return the already created
1395 * state or create a new one
1397 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1399 static bool atexit_registered
;
1402 const char *drvname
= NULL
;
1403 VMChangeStateEntry
*e
;
1405 struct audio_driver
*driver
;
1406 /* silence gcc warning about uninitialized variable */
1407 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1410 /* -audiodev option */
1411 legacy_config
= false;
1412 drvname
= AudiodevDriver_str(dev
->driver
);
1413 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1414 if (!legacy_config
) {
1415 dolog("Device %s: audiodev default parameter is deprecated, please "
1416 "specify audiodev=%s\n", name
,
1417 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1419 return QTAILQ_FIRST(&audio_states
);
1421 /* legacy implicit initialization */
1422 head
= audio_handle_legacy_opts();
1424 * In case of legacy initialization, all Audiodevs in the list will have
1425 * the same configuration (except the driver), so it does't matter which
1426 * one we chose. We need an Audiodev to set up AudioState before we can
1427 * init a driver. Also note that dev at this point is still in the
1430 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1431 audio_validate_opts(dev
, &error_abort
);
1434 s
= g_malloc0(sizeof(AudioState
));
1437 QLIST_INIT (&s
->hw_head_out
);
1438 QLIST_INIT (&s
->hw_head_in
);
1439 QLIST_INIT (&s
->cap_head
);
1440 if (!atexit_registered
) {
1441 atexit(audio_cleanup
);
1442 atexit_registered
= true;
1444 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1446 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1448 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1449 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1451 if (s
->nb_hw_voices_out
<= 0) {
1452 dolog ("Bogus number of playback voices %d, setting to 1\n",
1453 s
->nb_hw_voices_out
);
1454 s
->nb_hw_voices_out
= 1;
1457 if (s
->nb_hw_voices_in
<= 0) {
1458 dolog ("Bogus number of capture voices %d, setting to 0\n",
1459 s
->nb_hw_voices_in
);
1460 s
->nb_hw_voices_in
= 0;
1464 driver
= audio_driver_lookup(drvname
);
1466 done
= !audio_driver_init(s
, driver
, true, dev
);
1468 dolog ("Unknown audio driver `%s'\n", drvname
);
1471 for (i
= 0; audio_prio_list
[i
]; i
++) {
1472 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1473 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1476 s
->dev
= dev
= e
->dev
;
1477 audio_validate_opts(dev
, &error_abort
);
1478 done
= !audio_driver_init(s
, driver
, false, dev
);
1486 audio_free_audiodev_list(&head
);
1489 driver
= audio_driver_lookup("none");
1490 done
= !audio_driver_init(s
, driver
, false, dev
);
1492 dolog("warning: Using timer based audio emulation\n");
1495 if (dev
->timer_period
<= 0) {
1496 s
->period_ticks
= 1;
1498 s
->period_ticks
= dev
->timer_period
* SCALE_US
;
1501 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1503 dolog ("warning: Could not register change state handler\n"
1504 "(Audio can continue looping even after stopping the VM)\n");
1507 QLIST_INIT (&s
->card_head
);
1508 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1512 void audio_free_audiodev_list(AudiodevListHead
*head
)
1514 AudiodevListEntry
*e
;
1515 while ((e
= QSIMPLEQ_FIRST(head
))) {
1516 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1517 qapi_free_Audiodev(e
->dev
);
1522 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1525 card
->state
= audio_init(NULL
, name
);
1528 card
->name
= g_strdup (name
);
1529 memset (&card
->entries
, 0, sizeof (card
->entries
));
1530 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1533 void AUD_remove_card (QEMUSoundCard
*card
)
1535 QLIST_REMOVE (card
, entries
);
1536 g_free (card
->name
);
1540 CaptureVoiceOut
*AUD_add_capture(
1542 struct audsettings
*as
,
1543 struct audio_capture_ops
*ops
,
1547 CaptureVoiceOut
*cap
;
1548 struct capture_callback
*cb
;
1551 if (!legacy_config
) {
1552 dolog("Capturing without setting an audiodev is deprecated\n");
1554 s
= audio_init(NULL
, NULL
);
1557 if (audio_validate_settings (as
)) {
1558 dolog ("Invalid settings were passed when trying to add capture\n");
1559 audio_print_settings (as
);
1563 cb
= g_malloc0(sizeof(*cb
));
1565 cb
->opaque
= cb_opaque
;
1567 cap
= audio_pcm_capture_find_specific(s
, as
);
1569 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1574 CaptureVoiceOut
*cap
;
1576 cap
= g_malloc0(sizeof(*cap
));
1580 QLIST_INIT (&hw
->sw_head
);
1581 QLIST_INIT (&cap
->cb_head
);
1583 /* XXX find a more elegant way */
1584 hw
->samples
= 4096 * 4;
1585 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1587 audio_pcm_init_info (&hw
->info
, as
);
1589 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1591 hw
->clip
= mixeng_clip
1592 [hw
->info
.nchannels
== 2]
1594 [hw
->info
.swap_endianness
]
1595 [audio_bits_to_index (hw
->info
.bits
)];
1597 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1598 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1600 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1601 audio_attach_capture (hw
);
1607 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1609 struct capture_callback
*cb
;
1611 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1612 if (cb
->opaque
== cb_opaque
) {
1613 cb
->ops
.destroy (cb_opaque
);
1614 QLIST_REMOVE (cb
, entries
);
1617 if (!cap
->cb_head
.lh_first
) {
1618 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1621 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1622 #ifdef DEBUG_CAPTURE
1623 dolog ("freeing %s\n", sw
->name
);
1626 sw1
= sw
->entries
.le_next
;
1628 st_rate_stop (sw
->rate
);
1631 QLIST_REMOVE (sw
, entries
);
1632 QLIST_REMOVE (sc
, entries
);
1636 QLIST_REMOVE (cap
, entries
);
1637 g_free (cap
->hw
.mix_buf
);
1646 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1649 HWVoiceOut
*hw
= sw
->hw
;
1651 sw
->vol
.mute
= mute
;
1652 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1653 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1655 if (hw
->pcm_ops
->ctl_out
) {
1656 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1661 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1664 HWVoiceIn
*hw
= sw
->hw
;
1666 sw
->vol
.mute
= mute
;
1667 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1668 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1670 if (hw
->pcm_ops
->ctl_in
) {
1671 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1676 void audio_create_pdos(Audiodev
*dev
)
1678 switch (dev
->driver
) {
1679 #define CASE(DRIVER, driver, pdo_name) \
1680 case AUDIODEV_DRIVER_##DRIVER: \
1681 if (!dev->u.driver.has_in) { \
1682 dev->u.driver.in = g_malloc0( \
1683 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1684 dev->u.driver.has_in = true; \
1686 if (!dev->u.driver.has_out) { \
1687 dev->u.driver.out = g_malloc0( \
1688 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1689 dev->u.driver.has_out = true; \
1694 CASE(ALSA
, alsa
, Alsa
);
1695 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1696 CASE(DSOUND
, dsound
, );
1697 CASE(OSS
, oss
, Oss
);
1700 CASE(SPICE
, spice
, );
1703 case AUDIODEV_DRIVER__MAX
:
1708 static void audio_validate_per_direction_opts(
1709 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1711 if (!pdo
->has_fixed_settings
) {
1712 pdo
->has_fixed_settings
= true;
1713 pdo
->fixed_settings
= true;
1715 if (!pdo
->fixed_settings
&&
1716 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1718 "You can't use frequency, channels or format with fixed-settings=off");
1722 if (!pdo
->has_frequency
) {
1723 pdo
->has_frequency
= true;
1724 pdo
->frequency
= 44100;
1726 if (!pdo
->has_channels
) {
1727 pdo
->has_channels
= true;
1730 if (!pdo
->has_voices
) {
1731 pdo
->has_voices
= true;
1734 if (!pdo
->has_format
) {
1735 pdo
->has_format
= true;
1736 pdo
->format
= AUDIO_FORMAT_S16
;
1740 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1744 audio_create_pdos(dev
);
1746 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1748 error_propagate(errp
, err
);
1752 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1754 error_propagate(errp
, err
);
1758 if (!dev
->has_timer_period
) {
1759 dev
->has_timer_period
= true;
1760 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1764 void audio_parse_option(const char *opt
)
1766 AudiodevListEntry
*e
;
1767 Audiodev
*dev
= NULL
;
1769 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1770 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1773 audio_validate_opts(dev
, &error_fatal
);
1775 e
= g_malloc0(sizeof(AudiodevListEntry
));
1777 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1780 void audio_init_audiodevs(void)
1782 AudiodevListEntry
*e
;
1784 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1785 audio_init(e
->dev
, NULL
);
1789 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1791 return (audsettings
) {
1792 .freq
= pdo
->frequency
,
1793 .nchannels
= pdo
->channels
,
1795 .endianness
= AUDIO_HOST_ENDIANNESS
,
1799 int audioformat_bytes_per_sample(AudioFormat fmt
)
1802 case AUDIO_FORMAT_U8
:
1803 case AUDIO_FORMAT_S8
:
1806 case AUDIO_FORMAT_U16
:
1807 case AUDIO_FORMAT_S16
:
1810 case AUDIO_FORMAT_U32
:
1811 case AUDIO_FORMAT_S32
:
1814 case AUDIO_FORMAT__MAX
:
1821 /* frames = freq * usec / 1e6 */
1822 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1823 audsettings
*as
, int def_usecs
)
1825 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1826 return (as
->freq
* usecs
+ 500000) / 1000000;
1829 /* samples = channels * frames = channels * freq * usec / 1e6 */
1830 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1831 audsettings
*as
, int def_usecs
)
1833 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1837 * bytes = bytes_per_sample * samples =
1838 * bytes_per_sample * channels * freq * usec / 1e6
1840 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1841 audsettings
*as
, int def_usecs
)
1843 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1844 audioformat_bytes_per_sample(as
->fmt
);
1847 AudioState
*audio_state_by_name(const char *name
)
1850 QTAILQ_FOREACH(s
, &audio_states
, list
) {
1852 if (strcmp(name
, s
->dev
->id
) == 0) {
1859 const char *audio_get_id(QEMUSoundCard
*card
)
1862 assert(card
->state
->dev
);
1863 return card
->state
->dev
->id
;