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
:
310 case AUDIO_FORMAT_U16
:
315 case AUDIO_FORMAT_S32
:
318 case AUDIO_FORMAT_U32
:
327 info
->freq
= as
->freq
;
330 info
->nchannels
= as
->nchannels
;
331 info
->shift
= (as
->nchannels
== 2) + shift
;
332 info
->align
= (1 << info
->shift
) - 1;
333 info
->bytes_per_second
= info
->freq
<< info
->shift
;
334 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
337 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
344 memset (buf
, 0x00, len
<< info
->shift
);
347 switch (info
->bits
) {
349 memset (buf
, 0x80, len
<< info
->shift
);
356 int shift
= info
->nchannels
- 1;
359 if (info
->swap_endianness
) {
363 for (i
= 0; i
< len
<< shift
; i
++) {
373 int shift
= info
->nchannels
- 1;
374 int32_t s
= INT32_MAX
;
376 if (info
->swap_endianness
) {
380 for (i
= 0; i
< len
<< shift
; i
++) {
387 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
397 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
404 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
405 struct audsettings
*as
408 CaptureVoiceOut
*cap
;
409 AudioState
*s
= &glob_audio_state
;
411 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
412 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
419 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
421 struct capture_callback
*cb
;
424 dolog ("notification %d sent\n", cmd
);
426 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
427 cb
->ops
.notify (cb
->opaque
, cmd
);
431 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
433 if (cap
->hw
.enabled
!= enabled
) {
434 audcnotification_e cmd
;
435 cap
->hw
.enabled
= enabled
;
436 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
437 audio_notify_capture (cap
, cmd
);
441 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
443 HWVoiceOut
*hw
= &cap
->hw
;
447 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
453 audio_capture_maybe_changed (cap
, enabled
);
456 static void audio_detach_capture (HWVoiceOut
*hw
)
458 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
461 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
462 SWVoiceOut
*sw
= &sc
->sw
;
463 CaptureVoiceOut
*cap
= sc
->cap
;
464 int was_active
= sw
->active
;
467 st_rate_stop (sw
->rate
);
471 QLIST_REMOVE (sw
, entries
);
472 QLIST_REMOVE (sc
, entries
);
475 /* We have removed soft voice from the capture:
476 this might have changed the overall status of the capture
477 since this might have been the only active voice */
478 audio_recalc_and_notify_capture (cap
);
484 static int audio_attach_capture (HWVoiceOut
*hw
)
486 AudioState
*s
= &glob_audio_state
;
487 CaptureVoiceOut
*cap
;
489 audio_detach_capture (hw
);
490 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
493 HWVoiceOut
*hw_cap
= &cap
->hw
;
495 sc
= g_malloc0(sizeof(*sc
));
502 sw
->active
= hw
->enabled
;
503 sw
->conv
= noop_conv
;
504 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
505 sw
->vol
= nominal_volume
;
506 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
508 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
512 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
513 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
515 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
516 hw
, sw
->info
.freq
, sw
->info
.bits
,
518 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
521 audio_capture_maybe_changed (cap
, 1);
528 * Hard voice (capture)
530 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
533 int m
= hw
->total_samples_captured
;
535 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
537 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
543 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
545 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
546 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
547 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
553 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
554 int live
, int pending
)
556 int left
= hw
->samples
- pending
;
557 int len
= audio_MIN (left
, live
);
561 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
562 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
563 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
564 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
566 hw
->clip (dst
, src
, samples_to_clip
);
568 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
569 len
-= samples_to_clip
;
570 clipped
+= samples_to_clip
;
576 * Soft voice (capture)
578 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
580 HWVoiceIn
*hw
= sw
->hw
;
581 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
584 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
585 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
589 rpos
= hw
->wpos
- live
;
594 return hw
->samples
+ rpos
;
598 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
600 HWVoiceIn
*hw
= sw
->hw
;
601 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
602 struct st_sample
*src
, *dst
= sw
->buf
;
604 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
606 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
607 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
608 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
612 samples
= size
>> sw
->info
.shift
;
617 swlim
= (live
* sw
->ratio
) >> 32;
618 swlim
= audio_MIN (swlim
, samples
);
621 src
= hw
->conv_buf
+ rpos
;
622 isamp
= hw
->wpos
- rpos
;
625 isamp
= hw
->samples
- rpos
;
633 if (audio_bug(__func__
, osamp
< 0)) {
634 dolog ("osamp=%d\n", osamp
);
638 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
640 rpos
= (rpos
+ isamp
) % hw
->samples
;
646 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
647 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
650 sw
->clip (buf
, sw
->buf
, ret
);
651 sw
->total_hw_samples_acquired
+= total
;
652 return ret
<< sw
->info
.shift
;
656 * Hard voice (playback)
658 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
664 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
665 if (sw
->active
|| !sw
->empty
) {
666 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
675 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
680 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
688 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
689 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
698 * Soft voice (playback)
700 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
702 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
703 int ret
= 0, pos
= 0, total
= 0;
709 hwsamples
= sw
->hw
->samples
;
711 live
= sw
->total_hw_samples_mixed
;
712 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
713 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
717 if (live
== hwsamples
) {
719 dolog ("%s is full %d\n", sw
->name
, live
);
724 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
725 samples
= size
>> sw
->info
.shift
;
727 dead
= hwsamples
- live
;
728 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
729 swlim
= audio_MIN (swlim
, samples
);
731 sw
->conv (sw
->buf
, buf
, swlim
);
733 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
734 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
739 dead
= hwsamples
- live
;
740 left
= hwsamples
- wpos
;
741 blck
= audio_MIN (dead
, left
);
750 sw
->hw
->mix_buf
+ wpos
,
758 wpos
= (wpos
+ osamp
) % hwsamples
;
762 sw
->total_hw_samples_mixed
+= total
;
763 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
767 "%s: write size %d ret %d total sw %d\n",
769 size
>> sw
->info
.shift
,
771 sw
->total_hw_samples_mixed
775 return ret
<< sw
->info
.shift
;
779 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
781 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
782 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
787 #include "audio_template.h"
789 #include "audio_template.h"
795 static bool audio_timer_running
;
796 static uint64_t audio_timer_last
;
798 static int audio_is_timer_needed (void)
800 HWVoiceIn
*hwi
= NULL
;
801 HWVoiceOut
*hwo
= NULL
;
803 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
804 if (!hwo
->poll_mode
) return 1;
806 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
807 if (!hwi
->poll_mode
) return 1;
812 static void audio_reset_timer (AudioState
*s
)
814 if (audio_is_timer_needed ()) {
815 timer_mod_anticipate_ns(s
->ts
,
816 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
817 if (!audio_timer_running
) {
818 audio_timer_running
= true;
819 audio_timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
820 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
824 if (audio_timer_running
) {
825 audio_timer_running
= false;
826 trace_audio_timer_stop();
831 static void audio_timer (void *opaque
)
834 AudioState
*s
= opaque
;
836 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
837 diff
= now
- audio_timer_last
;
838 if (diff
> s
->period_ticks
* 3 / 2) {
839 trace_audio_timer_delayed(diff
/ SCALE_MS
);
841 audio_timer_last
= now
;
844 audio_reset_timer(s
);
850 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
853 /* XXX: Consider options */
857 if (!sw
->hw
->enabled
) {
858 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
862 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
865 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
868 /* XXX: Consider options */
872 if (!sw
->hw
->enabled
) {
873 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
877 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
880 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
882 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
885 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
894 if (sw
->active
!= on
) {
895 AudioState
*s
= &glob_audio_state
;
900 hw
->pending_disable
= 0;
904 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
905 audio_reset_timer (s
);
913 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
914 temp_sw
= temp_sw
->entries
.le_next
) {
915 nb_active
+= temp_sw
->active
!= 0;
918 hw
->pending_disable
= nb_active
== 1;
922 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
923 sc
->sw
.active
= hw
->enabled
;
925 audio_capture_maybe_changed (sc
->cap
, 1);
932 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
941 if (sw
->active
!= on
) {
942 AudioState
*s
= &glob_audio_state
;
949 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
950 audio_reset_timer (s
);
953 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
959 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
960 temp_sw
= temp_sw
->entries
.le_next
) {
961 nb_active
+= temp_sw
->active
!= 0;
964 if (nb_active
== 1) {
966 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
974 static int audio_get_avail (SWVoiceIn
*sw
)
982 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
983 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
984 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
989 "%s: get_avail live %d ret %" PRId64
"\n",
991 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
994 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
997 static int audio_get_free (SWVoiceOut
*sw
)
1005 live
= sw
->total_hw_samples_mixed
;
1007 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1008 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1012 dead
= sw
->hw
->samples
- live
;
1015 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1017 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1020 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1023 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1030 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1031 SWVoiceOut
*sw
= &sc
->sw
;
1036 int till_end_of_hw
= hw
->samples
- rpos2
;
1037 int to_write
= audio_MIN (till_end_of_hw
, n
);
1038 int bytes
= to_write
<< hw
->info
.shift
;
1041 sw
->buf
= hw
->mix_buf
+ rpos2
;
1042 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1043 if (written
- bytes
) {
1044 dolog ("Could not mix %d bytes into a capture "
1045 "buffer, mixed %d\n",
1050 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1055 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1056 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1057 mixeng_clear (hw
->mix_buf
, samples
- n
);
1060 static void audio_run_out (AudioState
*s
)
1062 HWVoiceOut
*hw
= NULL
;
1065 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1067 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1069 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1074 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1075 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1079 if (hw
->pending_disable
&& !nb_live
) {
1082 dolog ("Disabling voice\n");
1085 hw
->pending_disable
= 0;
1086 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1087 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1089 audio_recalc_and_notify_capture (sc
->cap
);
1095 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1097 free
= audio_get_free (sw
);
1099 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1106 prev_rpos
= hw
->rpos
;
1107 played
= hw
->pcm_ops
->run_out (hw
, live
);
1108 replay_audio_out(&played
);
1109 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1110 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1111 hw
->rpos
, hw
->samples
, played
);
1116 dolog ("played=%d\n", played
);
1120 hw
->ts_helper
+= played
;
1121 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1124 cleanup_required
= 0;
1125 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1126 if (!sw
->active
&& sw
->empty
) {
1130 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1131 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1132 played
, sw
->total_hw_samples_mixed
);
1133 played
= sw
->total_hw_samples_mixed
;
1136 sw
->total_hw_samples_mixed
-= played
;
1138 if (!sw
->total_hw_samples_mixed
) {
1140 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1144 free
= audio_get_free (sw
);
1146 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1151 if (cleanup_required
) {
1154 sw
= hw
->sw_head
.lh_first
;
1156 sw1
= sw
->entries
.le_next
;
1157 if (!sw
->active
&& !sw
->callback
.fn
) {
1158 audio_close_out (sw
);
1166 static void audio_run_in (AudioState
*s
)
1168 HWVoiceIn
*hw
= NULL
;
1170 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1172 int captured
= 0, min
;
1174 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1175 captured
= hw
->pcm_ops
->run_in(hw
);
1177 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1179 min
= audio_pcm_hw_find_min_in (hw
);
1180 hw
->total_samples_captured
+= captured
- min
;
1181 hw
->ts_helper
+= captured
;
1183 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1184 sw
->total_hw_samples_acquired
-= min
;
1189 avail
= audio_get_avail (sw
);
1191 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1198 static void audio_run_capture (AudioState
*s
)
1200 CaptureVoiceOut
*cap
;
1202 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1203 int live
, rpos
, captured
;
1204 HWVoiceOut
*hw
= &cap
->hw
;
1207 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1210 int left
= hw
->samples
- rpos
;
1211 int to_capture
= audio_MIN (live
, left
);
1212 struct st_sample
*src
;
1213 struct capture_callback
*cb
;
1215 src
= hw
->mix_buf
+ rpos
;
1216 hw
->clip (cap
->buf
, src
, to_capture
);
1217 mixeng_clear (src
, to_capture
);
1219 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1220 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1221 to_capture
<< hw
->info
.shift
);
1223 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1228 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1229 if (!sw
->active
&& sw
->empty
) {
1233 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1234 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1235 captured
, sw
->total_hw_samples_mixed
);
1236 captured
= sw
->total_hw_samples_mixed
;
1239 sw
->total_hw_samples_mixed
-= captured
;
1240 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1245 void audio_run (const char *msg
)
1247 AudioState
*s
= &glob_audio_state
;
1251 audio_run_capture (s
);
1254 static double prevtime
;
1258 if (gettimeofday (&tv
, NULL
)) {
1259 perror ("audio_run: gettimeofday");
1263 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1264 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1265 prevtime
= currtime
;
1270 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1271 bool msg
, Audiodev
*dev
)
1273 s
->drv_opaque
= drv
->init(dev
);
1275 if (s
->drv_opaque
) {
1276 audio_init_nb_voices_out (drv
);
1277 audio_init_nb_voices_in (drv
);
1283 dolog("Could not init `%s' audio driver\n", drv
->name
);
1289 static void audio_vm_change_state_handler (void *opaque
, int running
,
1292 AudioState
*s
= opaque
;
1293 HWVoiceOut
*hwo
= NULL
;
1294 HWVoiceIn
*hwi
= NULL
;
1295 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1297 s
->vm_running
= running
;
1298 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1299 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1302 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1303 hwi
->pcm_ops
->ctl_in(hwi
, op
);
1305 audio_reset_timer (s
);
1308 static bool is_cleaning_up
;
1310 bool audio_is_cleaning_up(void)
1312 return is_cleaning_up
;
1315 void audio_cleanup(void)
1317 AudioState
*s
= &glob_audio_state
;
1318 HWVoiceOut
*hwo
, *hwon
;
1319 HWVoiceIn
*hwi
, *hwin
;
1321 is_cleaning_up
= true;
1322 QLIST_FOREACH_SAFE(hwo
, &glob_audio_state
.hw_head_out
, entries
, hwon
) {
1326 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1328 hwo
->pcm_ops
->fini_out (hwo
);
1330 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1331 CaptureVoiceOut
*cap
= sc
->cap
;
1332 struct capture_callback
*cb
;
1334 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1335 cb
->ops
.destroy (cb
->opaque
);
1338 QLIST_REMOVE(hwo
, entries
);
1341 QLIST_FOREACH_SAFE(hwi
, &glob_audio_state
.hw_head_in
, entries
, hwin
) {
1343 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1345 hwi
->pcm_ops
->fini_in (hwi
);
1346 QLIST_REMOVE(hwi
, entries
);
1350 s
->drv
->fini (s
->drv_opaque
);
1355 qapi_free_Audiodev(s
->dev
);
1360 static const VMStateDescription vmstate_audio
= {
1363 .minimum_version_id
= 1,
1364 .fields
= (VMStateField
[]) {
1365 VMSTATE_END_OF_LIST()
1369 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1371 static AudiodevListEntry
*audiodev_find(
1372 AudiodevListHead
*head
, const char *drvname
)
1374 AudiodevListEntry
*e
;
1375 QSIMPLEQ_FOREACH(e
, head
, next
) {
1376 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1384 static int audio_init(Audiodev
*dev
)
1388 const char *drvname
= NULL
;
1389 VMChangeStateEntry
*e
;
1390 AudioState
*s
= &glob_audio_state
;
1391 struct audio_driver
*driver
;
1392 /* silence gcc warning about uninitialized variable */
1393 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1397 dolog("Cannot create more than one audio backend, sorry\n");
1398 qapi_free_Audiodev(dev
);
1404 /* -audiodev option */
1405 drvname
= AudiodevDriver_str(dev
->driver
);
1407 /* legacy implicit initialization */
1408 head
= audio_handle_legacy_opts();
1410 * In case of legacy initialization, all Audiodevs in the list will have
1411 * the same configuration (except the driver), so it does't matter which
1412 * one we chose. We need an Audiodev to set up AudioState before we can
1413 * init a driver. Also note that dev at this point is still in the
1416 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1417 audio_validate_opts(dev
, &error_abort
);
1421 QLIST_INIT (&s
->hw_head_out
);
1422 QLIST_INIT (&s
->hw_head_in
);
1423 QLIST_INIT (&s
->cap_head
);
1424 atexit(audio_cleanup
);
1426 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1428 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1429 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1431 if (s
->nb_hw_voices_out
<= 0) {
1432 dolog ("Bogus number of playback voices %d, setting to 1\n",
1433 s
->nb_hw_voices_out
);
1434 s
->nb_hw_voices_out
= 1;
1437 if (s
->nb_hw_voices_in
<= 0) {
1438 dolog ("Bogus number of capture voices %d, setting to 0\n",
1439 s
->nb_hw_voices_in
);
1440 s
->nb_hw_voices_in
= 0;
1444 driver
= audio_driver_lookup(drvname
);
1446 done
= !audio_driver_init(s
, driver
, true, dev
);
1448 dolog ("Unknown audio driver `%s'\n", drvname
);
1451 for (i
= 0; audio_prio_list
[i
]; i
++) {
1452 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1453 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1456 s
->dev
= dev
= e
->dev
;
1457 audio_validate_opts(dev
, &error_abort
);
1458 done
= !audio_driver_init(s
, driver
, false, dev
);
1466 audio_free_audiodev_list(&head
);
1469 driver
= audio_driver_lookup("none");
1470 done
= !audio_driver_init(s
, driver
, false, dev
);
1472 dolog("warning: Using timer based audio emulation\n");
1475 if (dev
->timer_period
<= 0) {
1476 s
->period_ticks
= 1;
1478 s
->period_ticks
= dev
->timer_period
* SCALE_US
;
1481 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1483 dolog ("warning: Could not register change state handler\n"
1484 "(Audio can continue looping even after stopping the VM)\n");
1487 QLIST_INIT (&s
->card_head
);
1488 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1492 void audio_free_audiodev_list(AudiodevListHead
*head
)
1494 AudiodevListEntry
*e
;
1495 while ((e
= QSIMPLEQ_FIRST(head
))) {
1496 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1497 qapi_free_Audiodev(e
->dev
);
1502 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1505 card
->name
= g_strdup (name
);
1506 memset (&card
->entries
, 0, sizeof (card
->entries
));
1507 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1510 void AUD_remove_card (QEMUSoundCard
*card
)
1512 QLIST_REMOVE (card
, entries
);
1513 g_free (card
->name
);
1517 CaptureVoiceOut
*AUD_add_capture (
1518 struct audsettings
*as
,
1519 struct audio_capture_ops
*ops
,
1523 AudioState
*s
= &glob_audio_state
;
1524 CaptureVoiceOut
*cap
;
1525 struct capture_callback
*cb
;
1527 if (audio_validate_settings (as
)) {
1528 dolog ("Invalid settings were passed when trying to add capture\n");
1529 audio_print_settings (as
);
1533 cb
= g_malloc0(sizeof(*cb
));
1535 cb
->opaque
= cb_opaque
;
1537 cap
= audio_pcm_capture_find_specific (as
);
1539 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1544 CaptureVoiceOut
*cap
;
1546 cap
= g_malloc0(sizeof(*cap
));
1549 QLIST_INIT (&hw
->sw_head
);
1550 QLIST_INIT (&cap
->cb_head
);
1552 /* XXX find a more elegant way */
1553 hw
->samples
= 4096 * 4;
1554 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1556 audio_pcm_init_info (&hw
->info
, as
);
1558 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1560 hw
->clip
= mixeng_clip
1561 [hw
->info
.nchannels
== 2]
1563 [hw
->info
.swap_endianness
]
1564 [audio_bits_to_index (hw
->info
.bits
)];
1566 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1567 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1569 QLIST_FOREACH(hw
, &glob_audio_state
.hw_head_out
, entries
) {
1570 audio_attach_capture (hw
);
1576 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1578 struct capture_callback
*cb
;
1580 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1581 if (cb
->opaque
== cb_opaque
) {
1582 cb
->ops
.destroy (cb_opaque
);
1583 QLIST_REMOVE (cb
, entries
);
1586 if (!cap
->cb_head
.lh_first
) {
1587 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1590 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1591 #ifdef DEBUG_CAPTURE
1592 dolog ("freeing %s\n", sw
->name
);
1595 sw1
= sw
->entries
.le_next
;
1597 st_rate_stop (sw
->rate
);
1600 QLIST_REMOVE (sw
, entries
);
1601 QLIST_REMOVE (sc
, entries
);
1605 QLIST_REMOVE (cap
, entries
);
1606 g_free (cap
->hw
.mix_buf
);
1615 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1618 HWVoiceOut
*hw
= sw
->hw
;
1620 sw
->vol
.mute
= mute
;
1621 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1622 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1624 if (hw
->pcm_ops
->ctl_out
) {
1625 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1630 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1633 HWVoiceIn
*hw
= sw
->hw
;
1635 sw
->vol
.mute
= mute
;
1636 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1637 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1639 if (hw
->pcm_ops
->ctl_in
) {
1640 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1645 void audio_create_pdos(Audiodev
*dev
)
1647 switch (dev
->driver
) {
1648 #define CASE(DRIVER, driver, pdo_name) \
1649 case AUDIODEV_DRIVER_##DRIVER: \
1650 if (!dev->u.driver.has_in) { \
1651 dev->u.driver.in = g_malloc0( \
1652 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1653 dev->u.driver.has_in = true; \
1655 if (!dev->u.driver.has_out) { \
1656 dev->u.driver.out = g_malloc0( \
1657 sizeof(AudiodevAlsaPerDirectionOptions)); \
1658 dev->u.driver.has_out = true; \
1663 CASE(ALSA
, alsa
, Alsa
);
1664 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1665 CASE(DSOUND
, dsound
, );
1666 CASE(OSS
, oss
, Oss
);
1669 CASE(SPICE
, spice
, );
1672 case AUDIODEV_DRIVER__MAX
:
1677 static void audio_validate_per_direction_opts(
1678 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1680 if (!pdo
->has_fixed_settings
) {
1681 pdo
->has_fixed_settings
= true;
1682 pdo
->fixed_settings
= true;
1684 if (!pdo
->fixed_settings
&&
1685 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1687 "You can't use frequency, channels or format with fixed-settings=off");
1691 if (!pdo
->has_frequency
) {
1692 pdo
->has_frequency
= true;
1693 pdo
->frequency
= 44100;
1695 if (!pdo
->has_channels
) {
1696 pdo
->has_channels
= true;
1699 if (!pdo
->has_voices
) {
1700 pdo
->has_voices
= true;
1703 if (!pdo
->has_format
) {
1704 pdo
->has_format
= true;
1705 pdo
->format
= AUDIO_FORMAT_S16
;
1709 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1713 audio_create_pdos(dev
);
1715 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1717 error_propagate(errp
, err
);
1721 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1723 error_propagate(errp
, err
);
1727 if (!dev
->has_timer_period
) {
1728 dev
->has_timer_period
= true;
1729 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1733 void audio_parse_option(const char *opt
)
1735 AudiodevListEntry
*e
;
1736 Audiodev
*dev
= NULL
;
1738 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1739 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1742 audio_validate_opts(dev
, &error_fatal
);
1744 e
= g_malloc0(sizeof(AudiodevListEntry
));
1746 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1749 void audio_init_audiodevs(void)
1751 AudiodevListEntry
*e
;
1753 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1758 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1760 return (audsettings
) {
1761 .freq
= pdo
->frequency
,
1762 .nchannels
= pdo
->channels
,
1764 .endianness
= AUDIO_HOST_ENDIANNESS
,
1768 int audioformat_bytes_per_sample(AudioFormat fmt
)
1771 case AUDIO_FORMAT_U8
:
1772 case AUDIO_FORMAT_S8
:
1775 case AUDIO_FORMAT_U16
:
1776 case AUDIO_FORMAT_S16
:
1779 case AUDIO_FORMAT_U32
:
1780 case AUDIO_FORMAT_S32
:
1783 case AUDIO_FORMAT__MAX
:
1790 /* frames = freq * usec / 1e6 */
1791 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1792 audsettings
*as
, int def_usecs
)
1794 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1795 return (as
->freq
* usecs
+ 500000) / 1000000;
1798 /* samples = channels * frames = channels * freq * usec / 1e6 */
1799 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1800 audsettings
*as
, int def_usecs
)
1802 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1806 * bytes = bytes_per_sample * samples =
1807 * bytes_per_sample * channels * freq * usec / 1e6
1809 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1810 audsettings
*as
, int def_usecs
)
1812 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1813 audioformat_bytes_per_sample(as
->fmt
);