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 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
107 int audio_bug (const char *funcname
, int cond
)
112 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
115 AUD_log (NULL
, "Save all your work and restart without audio\n");
116 AUD_log (NULL
, "I am sorry\n");
118 AUD_log (NULL
, "Context:\n");
120 #if defined AUDIO_BREAKPOINT_ON_BUG
121 # if defined HOST_I386
122 # if defined __GNUC__
124 # elif defined _MSC_VER
139 static inline int audio_bits_to_index (int bits
)
152 audio_bug ("bits_to_index", 1);
153 AUD_log (NULL
, "invalid bits %d\n", bits
);
158 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
164 cond
= !nmemb
|| !size
;
168 if (audio_bug ("audio_calloc", cond
)) {
169 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
171 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
175 return g_malloc0 (len
);
178 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
181 fprintf(stderr
, "%s: ", cap
);
184 vfprintf(stderr
, fmt
, ap
);
187 void AUD_log (const char *cap
, const char *fmt
, ...)
192 AUD_vlog (cap
, fmt
, ap
);
196 static void audio_print_settings (struct audsettings
*as
)
198 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
201 case AUDIO_FORMAT_S8
:
202 AUD_log (NULL
, "S8");
204 case AUDIO_FORMAT_U8
:
205 AUD_log (NULL
, "U8");
207 case AUDIO_FORMAT_S16
:
208 AUD_log (NULL
, "S16");
210 case AUDIO_FORMAT_U16
:
211 AUD_log (NULL
, "U16");
213 case AUDIO_FORMAT_S32
:
214 AUD_log (NULL
, "S32");
216 case AUDIO_FORMAT_U32
:
217 AUD_log (NULL
, "U32");
220 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
224 AUD_log (NULL
, " endianness=");
225 switch (as
->endianness
) {
227 AUD_log (NULL
, "little");
230 AUD_log (NULL
, "big");
233 AUD_log (NULL
, "invalid");
236 AUD_log (NULL
, "\n");
239 static int audio_validate_settings (struct audsettings
*as
)
243 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
244 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
247 case AUDIO_FORMAT_S8
:
248 case AUDIO_FORMAT_U8
:
249 case AUDIO_FORMAT_S16
:
250 case AUDIO_FORMAT_U16
:
251 case AUDIO_FORMAT_S32
:
252 case AUDIO_FORMAT_U32
:
259 invalid
|= as
->freq
<= 0;
260 return invalid
? -1 : 0;
263 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
265 int bits
= 8, sign
= 0;
268 case AUDIO_FORMAT_S8
:
271 case AUDIO_FORMAT_U8
:
274 case AUDIO_FORMAT_S16
:
277 case AUDIO_FORMAT_U16
:
281 case AUDIO_FORMAT_S32
:
284 case AUDIO_FORMAT_U32
:
291 return info
->freq
== as
->freq
292 && info
->nchannels
== as
->nchannels
293 && info
->sign
== sign
294 && info
->bits
== bits
295 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
298 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
300 int bits
= 8, sign
= 0, shift
= 0;
303 case AUDIO_FORMAT_S8
:
305 case AUDIO_FORMAT_U8
:
308 case AUDIO_FORMAT_S16
:
311 case AUDIO_FORMAT_U16
:
316 case AUDIO_FORMAT_S32
:
319 case AUDIO_FORMAT_U32
:
328 info
->freq
= as
->freq
;
331 info
->nchannels
= as
->nchannels
;
332 info
->shift
= (as
->nchannels
== 2) + shift
;
333 info
->align
= (1 << info
->shift
) - 1;
334 info
->bytes_per_second
= info
->freq
<< info
->shift
;
335 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
338 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
345 memset (buf
, 0x00, len
<< info
->shift
);
348 switch (info
->bits
) {
350 memset (buf
, 0x80, len
<< info
->shift
);
357 int shift
= info
->nchannels
- 1;
360 if (info
->swap_endianness
) {
364 for (i
= 0; i
< len
<< shift
; i
++) {
374 int shift
= info
->nchannels
- 1;
375 int32_t s
= INT32_MAX
;
377 if (info
->swap_endianness
) {
381 for (i
= 0; i
< len
<< shift
; i
++) {
388 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
398 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
405 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
406 struct audsettings
*as
)
408 CaptureVoiceOut
*cap
;
410 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
411 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
418 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
420 struct capture_callback
*cb
;
423 dolog ("notification %d sent\n", cmd
);
425 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
426 cb
->ops
.notify (cb
->opaque
, cmd
);
430 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
432 if (cap
->hw
.enabled
!= enabled
) {
433 audcnotification_e cmd
;
434 cap
->hw
.enabled
= enabled
;
435 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
436 audio_notify_capture (cap
, cmd
);
440 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
442 HWVoiceOut
*hw
= &cap
->hw
;
446 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
452 audio_capture_maybe_changed (cap
, enabled
);
455 static void audio_detach_capture (HWVoiceOut
*hw
)
457 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
460 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
461 SWVoiceOut
*sw
= &sc
->sw
;
462 CaptureVoiceOut
*cap
= sc
->cap
;
463 int was_active
= sw
->active
;
466 st_rate_stop (sw
->rate
);
470 QLIST_REMOVE (sw
, entries
);
471 QLIST_REMOVE (sc
, entries
);
474 /* We have removed soft voice from the capture:
475 this might have changed the overall status of the capture
476 since this might have been the only active voice */
477 audio_recalc_and_notify_capture (cap
);
483 static int audio_attach_capture (HWVoiceOut
*hw
)
485 AudioState
*s
= hw
->s
;
486 CaptureVoiceOut
*cap
;
488 audio_detach_capture (hw
);
489 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
492 HWVoiceOut
*hw_cap
= &cap
->hw
;
494 sc
= g_malloc0(sizeof(*sc
));
501 sw
->active
= hw
->enabled
;
502 sw
->conv
= noop_conv
;
503 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
504 sw
->vol
= nominal_volume
;
505 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
507 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
511 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
512 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
514 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
515 hw
, sw
->info
.freq
, sw
->info
.bits
,
517 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
520 audio_capture_maybe_changed (cap
, 1);
527 * Hard voice (capture)
529 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
532 int m
= hw
->total_samples_captured
;
534 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
536 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
542 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
544 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
545 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
546 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
552 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
553 int live
, int pending
)
555 int left
= hw
->samples
- pending
;
556 int len
= audio_MIN (left
, live
);
560 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
561 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
562 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
563 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
565 hw
->clip (dst
, src
, samples_to_clip
);
567 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
568 len
-= samples_to_clip
;
569 clipped
+= samples_to_clip
;
575 * Soft voice (capture)
577 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
579 HWVoiceIn
*hw
= sw
->hw
;
580 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
583 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
584 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
588 rpos
= hw
->wpos
- live
;
593 return hw
->samples
+ rpos
;
597 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
599 HWVoiceIn
*hw
= sw
->hw
;
600 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
601 struct st_sample
*src
, *dst
= sw
->buf
;
603 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
605 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
606 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
607 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
611 samples
= size
>> sw
->info
.shift
;
616 swlim
= (live
* sw
->ratio
) >> 32;
617 swlim
= audio_MIN (swlim
, samples
);
620 src
= hw
->conv_buf
+ rpos
;
621 isamp
= hw
->wpos
- rpos
;
624 isamp
= hw
->samples
- rpos
;
632 if (audio_bug(__func__
, osamp
< 0)) {
633 dolog ("osamp=%d\n", osamp
);
637 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
639 rpos
= (rpos
+ isamp
) % hw
->samples
;
645 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
646 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
649 sw
->clip (buf
, sw
->buf
, ret
);
650 sw
->total_hw_samples_acquired
+= total
;
651 return ret
<< sw
->info
.shift
;
655 * Hard voice (playback)
657 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
663 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
664 if (sw
->active
|| !sw
->empty
) {
665 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
674 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
679 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
687 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
688 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
697 * Soft voice (playback)
699 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
701 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
702 int ret
= 0, pos
= 0, total
= 0;
708 hwsamples
= sw
->hw
->samples
;
710 live
= sw
->total_hw_samples_mixed
;
711 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
712 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
716 if (live
== hwsamples
) {
718 dolog ("%s is full %d\n", sw
->name
, live
);
723 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
724 samples
= size
>> sw
->info
.shift
;
726 dead
= hwsamples
- live
;
727 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
728 swlim
= audio_MIN (swlim
, samples
);
730 sw
->conv (sw
->buf
, buf
, swlim
);
732 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
733 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
738 dead
= hwsamples
- live
;
739 left
= hwsamples
- wpos
;
740 blck
= audio_MIN (dead
, left
);
749 sw
->hw
->mix_buf
+ wpos
,
757 wpos
= (wpos
+ osamp
) % hwsamples
;
761 sw
->total_hw_samples_mixed
+= total
;
762 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
766 "%s: write size %d ret %d total sw %d\n",
768 size
>> sw
->info
.shift
,
770 sw
->total_hw_samples_mixed
774 return ret
<< sw
->info
.shift
;
778 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
780 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
781 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
786 #include "audio_template.h"
788 #include "audio_template.h"
793 static int audio_is_timer_needed(AudioState
*s
)
795 HWVoiceIn
*hwi
= NULL
;
796 HWVoiceOut
*hwo
= NULL
;
798 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
799 if (!hwo
->poll_mode
) return 1;
801 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
802 if (!hwi
->poll_mode
) return 1;
807 static void audio_reset_timer (AudioState
*s
)
809 if (audio_is_timer_needed(s
)) {
810 timer_mod_anticipate_ns(s
->ts
,
811 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
812 if (!s
->timer_running
) {
813 s
->timer_running
= true;
814 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
815 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
819 if (s
->timer_running
) {
820 s
->timer_running
= false;
821 trace_audio_timer_stop();
826 static void audio_timer (void *opaque
)
829 AudioState
*s
= opaque
;
831 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
832 diff
= now
- s
->timer_last
;
833 if (diff
> s
->period_ticks
* 3 / 2) {
834 trace_audio_timer_delayed(diff
/ SCALE_MS
);
839 audio_reset_timer(s
);
845 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
848 /* XXX: Consider options */
852 if (!sw
->hw
->enabled
) {
853 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
857 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
860 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
863 /* XXX: Consider options */
867 if (!sw
->hw
->enabled
) {
868 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
872 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
875 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
877 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
880 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
889 if (sw
->active
!= on
) {
890 AudioState
*s
= sw
->s
;
895 hw
->pending_disable
= 0;
899 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
900 audio_reset_timer (s
);
908 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
909 temp_sw
= temp_sw
->entries
.le_next
) {
910 nb_active
+= temp_sw
->active
!= 0;
913 hw
->pending_disable
= nb_active
== 1;
917 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
918 sc
->sw
.active
= hw
->enabled
;
920 audio_capture_maybe_changed (sc
->cap
, 1);
927 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
936 if (sw
->active
!= on
) {
937 AudioState
*s
= sw
->s
;
944 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
945 audio_reset_timer (s
);
948 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
954 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
955 temp_sw
= temp_sw
->entries
.le_next
) {
956 nb_active
+= temp_sw
->active
!= 0;
959 if (nb_active
== 1) {
961 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
969 static int audio_get_avail (SWVoiceIn
*sw
)
977 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
978 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
979 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
984 "%s: get_avail live %d ret %" PRId64
"\n",
986 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
989 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
992 static int audio_get_free (SWVoiceOut
*sw
)
1000 live
= sw
->total_hw_samples_mixed
;
1002 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1003 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1007 dead
= sw
->hw
->samples
- live
;
1010 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1012 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1015 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1018 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1025 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1026 SWVoiceOut
*sw
= &sc
->sw
;
1031 int till_end_of_hw
= hw
->samples
- rpos2
;
1032 int to_write
= audio_MIN (till_end_of_hw
, n
);
1033 int bytes
= to_write
<< hw
->info
.shift
;
1036 sw
->buf
= hw
->mix_buf
+ rpos2
;
1037 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1038 if (written
- bytes
) {
1039 dolog ("Could not mix %d bytes into a capture "
1040 "buffer, mixed %d\n",
1045 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1050 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1051 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1052 mixeng_clear (hw
->mix_buf
, samples
- n
);
1055 static void audio_run_out (AudioState
*s
)
1057 HWVoiceOut
*hw
= NULL
;
1060 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1062 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1064 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1069 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1070 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1074 if (hw
->pending_disable
&& !nb_live
) {
1077 dolog ("Disabling voice\n");
1080 hw
->pending_disable
= 0;
1081 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1082 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1084 audio_recalc_and_notify_capture (sc
->cap
);
1090 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1092 free
= audio_get_free (sw
);
1094 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1101 prev_rpos
= hw
->rpos
;
1102 played
= hw
->pcm_ops
->run_out (hw
, live
);
1103 replay_audio_out(&played
);
1104 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1105 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1106 hw
->rpos
, hw
->samples
, played
);
1111 dolog ("played=%d\n", played
);
1115 hw
->ts_helper
+= played
;
1116 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1119 cleanup_required
= 0;
1120 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1121 if (!sw
->active
&& sw
->empty
) {
1125 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1126 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1127 played
, sw
->total_hw_samples_mixed
);
1128 played
= sw
->total_hw_samples_mixed
;
1131 sw
->total_hw_samples_mixed
-= played
;
1133 if (!sw
->total_hw_samples_mixed
) {
1135 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1139 free
= audio_get_free (sw
);
1141 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1146 if (cleanup_required
) {
1149 sw
= hw
->sw_head
.lh_first
;
1151 sw1
= sw
->entries
.le_next
;
1152 if (!sw
->active
&& !sw
->callback
.fn
) {
1153 audio_close_out (sw
);
1161 static void audio_run_in (AudioState
*s
)
1163 HWVoiceIn
*hw
= NULL
;
1165 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1167 int captured
= 0, min
;
1169 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1170 captured
= hw
->pcm_ops
->run_in(hw
);
1172 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1174 min
= audio_pcm_hw_find_min_in (hw
);
1175 hw
->total_samples_captured
+= captured
- min
;
1176 hw
->ts_helper
+= captured
;
1178 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1179 sw
->total_hw_samples_acquired
-= min
;
1184 avail
= audio_get_avail (sw
);
1186 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1193 static void audio_run_capture (AudioState
*s
)
1195 CaptureVoiceOut
*cap
;
1197 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1198 int live
, rpos
, captured
;
1199 HWVoiceOut
*hw
= &cap
->hw
;
1202 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1205 int left
= hw
->samples
- rpos
;
1206 int to_capture
= audio_MIN (live
, left
);
1207 struct st_sample
*src
;
1208 struct capture_callback
*cb
;
1210 src
= hw
->mix_buf
+ rpos
;
1211 hw
->clip (cap
->buf
, src
, to_capture
);
1212 mixeng_clear (src
, to_capture
);
1214 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1215 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1216 to_capture
<< hw
->info
.shift
);
1218 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1223 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1224 if (!sw
->active
&& sw
->empty
) {
1228 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1229 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1230 captured
, sw
->total_hw_samples_mixed
);
1231 captured
= sw
->total_hw_samples_mixed
;
1234 sw
->total_hw_samples_mixed
-= captured
;
1235 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1240 void audio_run (const char *msg
)
1244 QTAILQ_FOREACH(s
, &audio_states
, list
) {
1247 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(s
, drv
);
1275 audio_init_nb_voices_in(s
, 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(s
, hwo
))) {
1297 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1300 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, 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 static void free_audio_state(AudioState
*s
)
1315 HWVoiceOut
*hwo
, *hwon
;
1316 HWVoiceIn
*hwi
, *hwin
;
1318 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1322 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1324 hwo
->pcm_ops
->fini_out (hwo
);
1326 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1327 CaptureVoiceOut
*cap
= sc
->cap
;
1328 struct capture_callback
*cb
;
1330 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1331 cb
->ops
.destroy (cb
->opaque
);
1334 QLIST_REMOVE(hwo
, entries
);
1337 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1339 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1341 hwi
->pcm_ops
->fini_in (hwi
);
1342 QLIST_REMOVE(hwi
, entries
);
1346 s
->drv
->fini (s
->drv_opaque
);
1351 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
)
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 drvname
= AudiodevDriver_str(dev
->driver
);
1412 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1414 * todo: check for -audiodev once we have normal audiodev selection
1417 return QTAILQ_FIRST(&audio_states
);
1419 /* legacy implicit initialization */
1420 head
= audio_handle_legacy_opts();
1422 * In case of legacy initialization, all Audiodevs in the list will have
1423 * the same configuration (except the driver), so it does't matter which
1424 * one we chose. We need an Audiodev to set up AudioState before we can
1425 * init a driver. Also note that dev at this point is still in the
1428 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1429 audio_validate_opts(dev
, &error_abort
);
1432 s
= g_malloc0(sizeof(AudioState
));
1435 QLIST_INIT (&s
->hw_head_out
);
1436 QLIST_INIT (&s
->hw_head_in
);
1437 QLIST_INIT (&s
->cap_head
);
1438 if (!atexit_registered
) {
1439 atexit(audio_cleanup
);
1440 atexit_registered
= true;
1442 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1444 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1446 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1447 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1449 if (s
->nb_hw_voices_out
<= 0) {
1450 dolog ("Bogus number of playback voices %d, setting to 1\n",
1451 s
->nb_hw_voices_out
);
1452 s
->nb_hw_voices_out
= 1;
1455 if (s
->nb_hw_voices_in
<= 0) {
1456 dolog ("Bogus number of capture voices %d, setting to 0\n",
1457 s
->nb_hw_voices_in
);
1458 s
->nb_hw_voices_in
= 0;
1462 driver
= audio_driver_lookup(drvname
);
1464 done
= !audio_driver_init(s
, driver
, true, dev
);
1466 dolog ("Unknown audio driver `%s'\n", drvname
);
1469 for (i
= 0; audio_prio_list
[i
]; i
++) {
1470 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1471 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1474 s
->dev
= dev
= e
->dev
;
1475 audio_validate_opts(dev
, &error_abort
);
1476 done
= !audio_driver_init(s
, driver
, false, dev
);
1484 audio_free_audiodev_list(&head
);
1487 driver
= audio_driver_lookup("none");
1488 done
= !audio_driver_init(s
, driver
, false, dev
);
1490 dolog("warning: Using timer based audio emulation\n");
1493 if (dev
->timer_period
<= 0) {
1494 s
->period_ticks
= 1;
1496 s
->period_ticks
= dev
->timer_period
* SCALE_US
;
1499 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1501 dolog ("warning: Could not register change state handler\n"
1502 "(Audio can continue looping even after stopping the VM)\n");
1505 QLIST_INIT (&s
->card_head
);
1506 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1510 void audio_free_audiodev_list(AudiodevListHead
*head
)
1512 AudiodevListEntry
*e
;
1513 while ((e
= QSIMPLEQ_FIRST(head
))) {
1514 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1515 qapi_free_Audiodev(e
->dev
);
1520 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1523 card
->state
= audio_init(NULL
);
1526 card
->name
= g_strdup (name
);
1527 memset (&card
->entries
, 0, sizeof (card
->entries
));
1528 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1531 void AUD_remove_card (QEMUSoundCard
*card
)
1533 QLIST_REMOVE (card
, entries
);
1534 g_free (card
->name
);
1538 CaptureVoiceOut
*AUD_add_capture(
1540 struct audsettings
*as
,
1541 struct audio_capture_ops
*ops
,
1545 CaptureVoiceOut
*cap
;
1546 struct capture_callback
*cb
;
1549 /* todo: remove when we have normal audiodev selection support */
1550 s
= audio_init(NULL
);
1553 if (audio_validate_settings (as
)) {
1554 dolog ("Invalid settings were passed when trying to add capture\n");
1555 audio_print_settings (as
);
1559 cb
= g_malloc0(sizeof(*cb
));
1561 cb
->opaque
= cb_opaque
;
1563 cap
= audio_pcm_capture_find_specific(s
, as
);
1565 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1570 CaptureVoiceOut
*cap
;
1572 cap
= g_malloc0(sizeof(*cap
));
1576 QLIST_INIT (&hw
->sw_head
);
1577 QLIST_INIT (&cap
->cb_head
);
1579 /* XXX find a more elegant way */
1580 hw
->samples
= 4096 * 4;
1581 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1583 audio_pcm_init_info (&hw
->info
, as
);
1585 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1587 hw
->clip
= mixeng_clip
1588 [hw
->info
.nchannels
== 2]
1590 [hw
->info
.swap_endianness
]
1591 [audio_bits_to_index (hw
->info
.bits
)];
1593 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1594 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1596 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1597 audio_attach_capture (hw
);
1603 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1605 struct capture_callback
*cb
;
1607 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1608 if (cb
->opaque
== cb_opaque
) {
1609 cb
->ops
.destroy (cb_opaque
);
1610 QLIST_REMOVE (cb
, entries
);
1613 if (!cap
->cb_head
.lh_first
) {
1614 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1617 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1618 #ifdef DEBUG_CAPTURE
1619 dolog ("freeing %s\n", sw
->name
);
1622 sw1
= sw
->entries
.le_next
;
1624 st_rate_stop (sw
->rate
);
1627 QLIST_REMOVE (sw
, entries
);
1628 QLIST_REMOVE (sc
, entries
);
1632 QLIST_REMOVE (cap
, entries
);
1633 g_free (cap
->hw
.mix_buf
);
1642 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1645 HWVoiceOut
*hw
= sw
->hw
;
1647 sw
->vol
.mute
= mute
;
1648 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1649 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1651 if (hw
->pcm_ops
->ctl_out
) {
1652 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1657 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1660 HWVoiceIn
*hw
= sw
->hw
;
1662 sw
->vol
.mute
= mute
;
1663 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1664 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1666 if (hw
->pcm_ops
->ctl_in
) {
1667 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1672 void audio_create_pdos(Audiodev
*dev
)
1674 switch (dev
->driver
) {
1675 #define CASE(DRIVER, driver, pdo_name) \
1676 case AUDIODEV_DRIVER_##DRIVER: \
1677 if (!dev->u.driver.has_in) { \
1678 dev->u.driver.in = g_malloc0( \
1679 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1680 dev->u.driver.has_in = true; \
1682 if (!dev->u.driver.has_out) { \
1683 dev->u.driver.out = g_malloc0( \
1684 sizeof(AudiodevAlsaPerDirectionOptions)); \
1685 dev->u.driver.has_out = true; \
1690 CASE(ALSA
, alsa
, Alsa
);
1691 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1692 CASE(DSOUND
, dsound
, );
1693 CASE(OSS
, oss
, Oss
);
1696 CASE(SPICE
, spice
, );
1699 case AUDIODEV_DRIVER__MAX
:
1704 static void audio_validate_per_direction_opts(
1705 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1707 if (!pdo
->has_fixed_settings
) {
1708 pdo
->has_fixed_settings
= true;
1709 pdo
->fixed_settings
= true;
1711 if (!pdo
->fixed_settings
&&
1712 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1714 "You can't use frequency, channels or format with fixed-settings=off");
1718 if (!pdo
->has_frequency
) {
1719 pdo
->has_frequency
= true;
1720 pdo
->frequency
= 44100;
1722 if (!pdo
->has_channels
) {
1723 pdo
->has_channels
= true;
1726 if (!pdo
->has_voices
) {
1727 pdo
->has_voices
= true;
1730 if (!pdo
->has_format
) {
1731 pdo
->has_format
= true;
1732 pdo
->format
= AUDIO_FORMAT_S16
;
1736 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1740 audio_create_pdos(dev
);
1742 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1744 error_propagate(errp
, err
);
1748 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1750 error_propagate(errp
, err
);
1754 if (!dev
->has_timer_period
) {
1755 dev
->has_timer_period
= true;
1756 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1760 void audio_parse_option(const char *opt
)
1762 AudiodevListEntry
*e
;
1763 Audiodev
*dev
= NULL
;
1765 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1766 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1769 audio_validate_opts(dev
, &error_fatal
);
1771 e
= g_malloc0(sizeof(AudiodevListEntry
));
1773 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1776 void audio_init_audiodevs(void)
1778 AudiodevListEntry
*e
;
1780 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1785 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1787 return (audsettings
) {
1788 .freq
= pdo
->frequency
,
1789 .nchannels
= pdo
->channels
,
1791 .endianness
= AUDIO_HOST_ENDIANNESS
,
1795 int audioformat_bytes_per_sample(AudioFormat fmt
)
1798 case AUDIO_FORMAT_U8
:
1799 case AUDIO_FORMAT_S8
:
1802 case AUDIO_FORMAT_U16
:
1803 case AUDIO_FORMAT_S16
:
1806 case AUDIO_FORMAT_U32
:
1807 case AUDIO_FORMAT_S32
:
1810 case AUDIO_FORMAT__MAX
:
1817 /* frames = freq * usec / 1e6 */
1818 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1819 audsettings
*as
, int def_usecs
)
1821 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1822 return (as
->freq
* usecs
+ 500000) / 1000000;
1825 /* samples = channels * frames = channels * freq * usec / 1e6 */
1826 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1827 audsettings
*as
, int def_usecs
)
1829 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1833 * bytes = bytes_per_sample * samples =
1834 * bytes_per_sample * channels * freq * usec / 1e6
1836 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1837 audsettings
*as
, int def_usecs
)
1839 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1840 audioformat_bytes_per_sample(as
->fmt
);
1843 AudioState
*audio_state_by_name(const char *name
)
1846 QTAILQ_FOREACH(s
, &audio_states
, list
) {
1848 if (strcmp(name
, s
->dev
->id
) == 0) {
1855 const char *audio_get_id(QEMUSoundCard
*card
)
1858 assert(card
->state
->dev
);
1859 return card
->state
->dev
->id
;