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 "qemu-common.h"
36 #include "sysemu/replay.h"
37 #include "sysemu/runstate.h"
38 #include "ui/qemu-spice.h"
41 #define AUDIO_CAP "audio"
42 #include "audio_int.h"
44 /* #define DEBUG_LIVE */
45 /* #define DEBUG_OUT */
46 /* #define DEBUG_CAPTURE */
47 /* #define DEBUG_POLL */
49 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
52 /* Order of CONFIG_AUDIO_DRIVERS is import.
53 The 1st one is the one used by default, that is the reason
54 that we generate the list.
56 const char *audio_prio_list
[] = {
64 static QLIST_HEAD(, audio_driver
) audio_drivers
;
65 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
67 void audio_driver_register(audio_driver
*drv
)
69 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
72 audio_driver
*audio_driver_lookup(const char *name
)
74 struct audio_driver
*d
;
76 QLIST_FOREACH(d
, &audio_drivers
, next
) {
77 if (strcmp(name
, d
->name
) == 0) {
82 audio_module_load_one(name
);
83 QLIST_FOREACH(d
, &audio_drivers
, next
) {
84 if (strcmp(name
, d
->name
) == 0) {
92 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
93 QTAILQ_HEAD_INITIALIZER(audio_states
);
95 const struct mixeng_volume nominal_volume
= {
106 static bool legacy_config
= true;
108 int audio_bug (const char *funcname
, int cond
)
113 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
116 AUD_log (NULL
, "Save all your work and restart without audio\n");
117 AUD_log (NULL
, "I am sorry\n");
119 AUD_log (NULL
, "Context:\n");
126 static inline int audio_bits_to_index (int bits
)
139 audio_bug ("bits_to_index", 1);
140 AUD_log (NULL
, "invalid bits %d\n", bits
);
145 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
151 cond
= !nmemb
|| !size
;
155 if (audio_bug ("audio_calloc", cond
)) {
156 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
158 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
162 return g_malloc0 (len
);
165 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
168 fprintf(stderr
, "%s: ", cap
);
171 vfprintf(stderr
, fmt
, ap
);
174 void AUD_log (const char *cap
, const char *fmt
, ...)
179 AUD_vlog (cap
, fmt
, ap
);
183 static void audio_print_settings (struct audsettings
*as
)
185 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
188 case AUDIO_FORMAT_S8
:
189 AUD_log (NULL
, "S8");
191 case AUDIO_FORMAT_U8
:
192 AUD_log (NULL
, "U8");
194 case AUDIO_FORMAT_S16
:
195 AUD_log (NULL
, "S16");
197 case AUDIO_FORMAT_U16
:
198 AUD_log (NULL
, "U16");
200 case AUDIO_FORMAT_S32
:
201 AUD_log (NULL
, "S32");
203 case AUDIO_FORMAT_U32
:
204 AUD_log (NULL
, "U32");
206 case AUDIO_FORMAT_F32
:
207 AUD_log (NULL
, "F32");
210 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
214 AUD_log (NULL
, " endianness=");
215 switch (as
->endianness
) {
217 AUD_log (NULL
, "little");
220 AUD_log (NULL
, "big");
223 AUD_log (NULL
, "invalid");
226 AUD_log (NULL
, "\n");
229 static int audio_validate_settings (struct audsettings
*as
)
233 invalid
= as
->nchannels
< 1;
234 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
237 case AUDIO_FORMAT_S8
:
238 case AUDIO_FORMAT_U8
:
239 case AUDIO_FORMAT_S16
:
240 case AUDIO_FORMAT_U16
:
241 case AUDIO_FORMAT_S32
:
242 case AUDIO_FORMAT_U32
:
243 case AUDIO_FORMAT_F32
:
250 invalid
|= as
->freq
<= 0;
251 return invalid
? -1 : 0;
254 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
257 bool is_signed
= false, is_float
= false;
260 case AUDIO_FORMAT_S8
:
263 case AUDIO_FORMAT_U8
:
266 case AUDIO_FORMAT_S16
:
269 case AUDIO_FORMAT_U16
:
273 case AUDIO_FORMAT_F32
:
276 case AUDIO_FORMAT_S32
:
279 case AUDIO_FORMAT_U32
:
286 return info
->freq
== as
->freq
287 && info
->nchannels
== as
->nchannels
288 && info
->is_signed
== is_signed
289 && info
->is_float
== is_float
290 && info
->bits
== bits
291 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
294 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
297 bool is_signed
= false, is_float
= false;
300 case AUDIO_FORMAT_S8
:
303 case AUDIO_FORMAT_U8
:
307 case AUDIO_FORMAT_S16
:
310 case AUDIO_FORMAT_U16
:
315 case AUDIO_FORMAT_F32
:
318 case AUDIO_FORMAT_S32
:
321 case AUDIO_FORMAT_U32
:
330 info
->freq
= as
->freq
;
332 info
->is_signed
= is_signed
;
333 info
->is_float
= is_float
;
334 info
->nchannels
= as
->nchannels
;
335 info
->bytes_per_frame
= as
->nchannels
* mul
;
336 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
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
)
346 if (info
->is_signed
|| info
->is_float
) {
347 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
349 switch (info
->bits
) {
351 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
360 if (info
->swap_endianness
) {
364 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
374 int32_t s
= INT32_MAX
;
376 if (info
->swap_endianness
) {
380 for (i
= 0; i
< len
* info
->nchannels
; 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(AudioState
*s
,
405 struct audsettings
*as
)
407 CaptureVoiceOut
*cap
;
409 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
410 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
417 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
419 struct capture_callback
*cb
;
422 dolog ("notification %d sent\n", cmd
);
424 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
425 cb
->ops
.notify (cb
->opaque
, cmd
);
429 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
431 if (cap
->hw
.enabled
!= enabled
) {
432 audcnotification_e cmd
;
433 cap
->hw
.enabled
= enabled
;
434 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
435 audio_notify_capture (cap
, cmd
);
439 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
441 HWVoiceOut
*hw
= &cap
->hw
;
445 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
451 audio_capture_maybe_changed (cap
, enabled
);
454 static void audio_detach_capture (HWVoiceOut
*hw
)
456 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
459 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
460 SWVoiceOut
*sw
= &sc
->sw
;
461 CaptureVoiceOut
*cap
= sc
->cap
;
462 int was_active
= sw
->active
;
465 st_rate_stop (sw
->rate
);
469 QLIST_REMOVE (sw
, entries
);
470 QLIST_REMOVE (sc
, entries
);
473 /* We have removed soft voice from the capture:
474 this might have changed the overall status of the capture
475 since this might have been the only active voice */
476 audio_recalc_and_notify_capture (cap
);
482 static int audio_attach_capture (HWVoiceOut
*hw
)
484 AudioState
*s
= hw
->s
;
485 CaptureVoiceOut
*cap
;
487 audio_detach_capture (hw
);
488 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
491 HWVoiceOut
*hw_cap
= &cap
->hw
;
493 sc
= g_malloc0(sizeof(*sc
));
500 sw
->active
= hw
->enabled
;
501 sw
->conv
= noop_conv
;
502 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
503 sw
->vol
= nominal_volume
;
504 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
506 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
510 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
511 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
513 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
514 hw
, sw
->info
.freq
, sw
->info
.bits
,
516 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
519 audio_capture_maybe_changed (cap
, 1);
526 * Hard voice (capture)
528 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
531 size_t m
= hw
->total_samples_captured
;
533 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
535 m
= MIN (m
, sw
->total_hw_samples_acquired
);
541 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
543 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
544 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
545 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
552 * Soft voice (capture)
554 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
556 HWVoiceIn
*hw
= sw
->hw
;
557 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
559 if (audio_bug(__func__
, live
< 0 || live
> hw
->conv_buf
->size
)) {
560 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
564 return audio_ring_posb(hw
->conv_buf
->pos
, live
, hw
->conv_buf
->size
);
567 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
569 HWVoiceIn
*hw
= sw
->hw
;
570 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
571 struct st_sample
*src
, *dst
= sw
->buf
;
573 rpos
= audio_pcm_sw_get_rpos_in(sw
) % hw
->conv_buf
->size
;
575 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
576 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
577 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
581 samples
= size
/ sw
->info
.bytes_per_frame
;
586 swlim
= (live
* sw
->ratio
) >> 32;
587 swlim
= MIN (swlim
, samples
);
590 src
= hw
->conv_buf
->samples
+ rpos
;
591 if (hw
->conv_buf
->pos
> rpos
) {
592 isamp
= hw
->conv_buf
->pos
- rpos
;
594 isamp
= hw
->conv_buf
->size
- rpos
;
602 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
604 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
610 if (hw
->pcm_ops
&& !hw
->pcm_ops
->volume_in
) {
611 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
614 sw
->clip (buf
, sw
->buf
, ret
);
615 sw
->total_hw_samples_acquired
+= total
;
616 return ret
* sw
->info
.bytes_per_frame
;
620 * Hard voice (playback)
622 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
628 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
629 if (sw
->active
|| !sw
->empty
) {
630 m
= MIN (m
, sw
->total_hw_samples_mixed
);
639 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
644 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
652 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
653 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
661 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
664 size_t pos
= hw
->mix_buf
->pos
;
667 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
668 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
669 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
670 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
672 hw
->clip(dst
, src
, samples_to_clip
);
674 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
675 len
-= samples_to_clip
;
676 clipped
+= samples_to_clip
;
681 * Soft voice (playback)
683 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
685 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
686 size_t ret
= 0, pos
= 0, total
= 0;
692 hwsamples
= sw
->hw
->mix_buf
->size
;
694 live
= sw
->total_hw_samples_mixed
;
695 if (audio_bug(__func__
, live
> hwsamples
)) {
696 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
700 if (live
== hwsamples
) {
702 dolog ("%s is full %zu\n", sw
->name
, live
);
707 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
708 samples
= size
/ sw
->info
.bytes_per_frame
;
710 dead
= hwsamples
- live
;
711 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
712 swlim
= MIN (swlim
, samples
);
714 sw
->conv (sw
->buf
, buf
, swlim
);
716 if (sw
->hw
->pcm_ops
&& !sw
->hw
->pcm_ops
->volume_out
) {
717 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
722 dead
= hwsamples
- live
;
723 left
= hwsamples
- wpos
;
724 blck
= MIN (dead
, left
);
733 sw
->hw
->mix_buf
->samples
+ wpos
,
741 wpos
= (wpos
+ osamp
) % hwsamples
;
745 sw
->total_hw_samples_mixed
+= total
;
746 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
750 "%s: write size %zu ret %zu total sw %zu\n",
752 size
/ sw
->info
.bytes_per_frame
,
754 sw
->total_hw_samples_mixed
758 return ret
* sw
->info
.bytes_per_frame
;
762 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
764 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
765 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
771 #include "audio_template.h"
773 #include "audio_template.h"
778 static int audio_is_timer_needed(AudioState
*s
)
780 HWVoiceIn
*hwi
= NULL
;
781 HWVoiceOut
*hwo
= NULL
;
783 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
784 if (!hwo
->poll_mode
) {
788 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
789 if (!hwi
->poll_mode
) {
796 static void audio_reset_timer (AudioState
*s
)
798 if (audio_is_timer_needed(s
)) {
799 timer_mod_anticipate_ns(s
->ts
,
800 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
801 if (!s
->timer_running
) {
802 s
->timer_running
= true;
803 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
804 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
808 if (s
->timer_running
) {
809 s
->timer_running
= false;
810 trace_audio_timer_stop();
815 static void audio_timer (void *opaque
)
818 AudioState
*s
= opaque
;
820 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
821 diff
= now
- s
->timer_last
;
822 if (diff
> s
->period_ticks
* 3 / 2) {
823 trace_audio_timer_delayed(diff
/ SCALE_MS
);
827 audio_run(s
, "timer");
828 audio_reset_timer(s
);
834 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
839 /* XXX: Consider options */
845 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
849 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
850 return audio_pcm_sw_write(sw
, buf
, size
);
852 return hw
->pcm_ops
->write(hw
, buf
, size
);
856 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
861 /* XXX: Consider options */
867 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
871 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
872 return audio_pcm_sw_read(sw
, buf
, size
);
874 return hw
->pcm_ops
->read(hw
, buf
, size
);
878 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
880 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
883 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
892 if (sw
->active
!= on
) {
893 AudioState
*s
= sw
->s
;
898 hw
->pending_disable
= 0;
902 if (hw
->pcm_ops
->enable_out
) {
903 hw
->pcm_ops
->enable_out(hw
, true);
905 audio_reset_timer (s
);
912 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
913 temp_sw
= temp_sw
->entries
.le_next
) {
914 nb_active
+= temp_sw
->active
!= 0;
917 hw
->pending_disable
= nb_active
== 1;
921 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
922 sc
->sw
.active
= hw
->enabled
;
924 audio_capture_maybe_changed (sc
->cap
, 1);
931 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
940 if (sw
->active
!= on
) {
941 AudioState
*s
= sw
->s
;
948 if (hw
->pcm_ops
->enable_in
) {
949 hw
->pcm_ops
->enable_in(hw
, true);
951 audio_reset_timer (s
);
954 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 if (hw
->pcm_ops
->enable_in
) {
967 hw
->pcm_ops
->enable_in(hw
, false);
976 static size_t audio_get_avail (SWVoiceIn
*sw
)
984 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
985 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
986 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
987 sw
->hw
->conv_buf
->size
);
992 "%s: get_avail live %zu ret %" PRId64
"\n",
994 live
, (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
997 return (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1000 static size_t audio_get_free(SWVoiceOut
*sw
)
1008 live
= sw
->total_hw_samples_mixed
;
1010 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1011 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1012 sw
->hw
->mix_buf
->size
);
1016 dead
= sw
->hw
->mix_buf
->size
- live
;
1019 dolog ("%s: get_free live %zu dead %zu ret %" PRId64
"\n",
1021 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) *
1022 sw
->info
.bytes_per_frame
);
1025 return (((int64_t) dead
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1028 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1036 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1037 SWVoiceOut
*sw
= &sc
->sw
;
1042 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1043 size_t to_write
= MIN(till_end_of_hw
, n
);
1044 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1047 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1048 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1049 if (written
- bytes
) {
1050 dolog("Could not mix %zu bytes into a capture "
1051 "buffer, mixed %zu\n",
1056 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1061 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1062 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1063 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1066 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1071 size_t size
= live
* hw
->info
.bytes_per_frame
;
1073 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1079 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1081 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1083 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1084 decr
* hw
->info
.bytes_per_frame
) /
1085 hw
->info
.bytes_per_frame
;
1089 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1091 if (proc
== 0 || proc
< decr
) {
1096 if (hw
->pcm_ops
->run_buffer_out
) {
1097 hw
->pcm_ops
->run_buffer_out(hw
);
1103 static void audio_run_out (AudioState
*s
)
1105 HWVoiceOut
*hw
= NULL
;
1108 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1109 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1110 /* there is exactly 1 sw for each hw with no mixeng */
1111 sw
= hw
->sw_head
.lh_first
;
1113 if (hw
->pending_disable
) {
1115 hw
->pending_disable
= 0;
1116 if (hw
->pcm_ops
->enable_out
) {
1117 hw
->pcm_ops
->enable_out(hw
, false);
1122 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1128 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1129 size_t played
, live
, prev_rpos
, free
;
1132 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1137 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1138 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1142 if (hw
->pending_disable
&& !nb_live
) {
1145 dolog ("Disabling voice\n");
1148 hw
->pending_disable
= 0;
1149 if (hw
->pcm_ops
->enable_out
) {
1150 hw
->pcm_ops
->enable_out(hw
, false);
1152 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1154 audio_recalc_and_notify_capture (sc
->cap
);
1160 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1162 free
= audio_get_free (sw
);
1164 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1168 if (hw
->pcm_ops
->run_buffer_out
) {
1169 hw
->pcm_ops
->run_buffer_out(hw
);
1174 prev_rpos
= hw
->mix_buf
->pos
;
1175 played
= audio_pcm_hw_run_out(hw
, live
);
1176 replay_audio_out(&played
);
1177 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1178 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1179 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1180 hw
->mix_buf
->pos
= 0;
1184 dolog("played=%zu\n", played
);
1188 hw
->ts_helper
+= played
;
1189 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1192 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1193 if (!sw
->active
&& sw
->empty
) {
1197 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1198 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1199 played
, sw
->total_hw_samples_mixed
);
1200 played
= sw
->total_hw_samples_mixed
;
1203 sw
->total_hw_samples_mixed
-= played
;
1205 if (!sw
->total_hw_samples_mixed
) {
1210 free
= audio_get_free (sw
);
1212 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1219 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1222 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
1224 if (hw
->pcm_ops
->run_buffer_in
) {
1225 hw
->pcm_ops
->run_buffer_in(hw
);
1230 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1231 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1233 assert(size
% hw
->info
.bytes_per_frame
== 0);
1238 proc
= MIN(size
/ hw
->info
.bytes_per_frame
,
1239 conv_buf
->size
- conv_buf
->pos
);
1241 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, buf
, proc
);
1242 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
1246 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1252 static void audio_run_in (AudioState
*s
)
1254 HWVoiceIn
*hw
= NULL
;
1256 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1257 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1258 /* there is exactly 1 sw for each hw with no mixeng */
1259 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1261 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1267 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1269 size_t captured
= 0, min
;
1271 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1272 captured
= audio_pcm_hw_run_in(
1273 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1275 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1276 hw
->conv_buf
->size
);
1278 min
= audio_pcm_hw_find_min_in (hw
);
1279 hw
->total_samples_captured
+= captured
- min
;
1280 hw
->ts_helper
+= captured
;
1282 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1283 sw
->total_hw_samples_acquired
-= min
;
1288 avail
= audio_get_avail (sw
);
1290 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1297 static void audio_run_capture (AudioState
*s
)
1299 CaptureVoiceOut
*cap
;
1301 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1302 size_t live
, rpos
, captured
;
1303 HWVoiceOut
*hw
= &cap
->hw
;
1306 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1307 rpos
= hw
->mix_buf
->pos
;
1309 size_t left
= hw
->mix_buf
->size
- rpos
;
1310 size_t to_capture
= MIN(live
, left
);
1311 struct st_sample
*src
;
1312 struct capture_callback
*cb
;
1314 src
= hw
->mix_buf
->samples
+ rpos
;
1315 hw
->clip (cap
->buf
, src
, to_capture
);
1316 mixeng_clear (src
, to_capture
);
1318 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1319 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1320 to_capture
* hw
->info
.bytes_per_frame
);
1322 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1325 hw
->mix_buf
->pos
= rpos
;
1327 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1328 if (!sw
->active
&& sw
->empty
) {
1332 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1333 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1334 captured
, sw
->total_hw_samples_mixed
);
1335 captured
= sw
->total_hw_samples_mixed
;
1338 sw
->total_hw_samples_mixed
-= captured
;
1339 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1344 void audio_run(AudioState
*s
, const char *msg
)
1348 audio_run_capture(s
);
1352 static double prevtime
;
1356 if (gettimeofday (&tv
, NULL
)) {
1357 perror ("audio_run: gettimeofday");
1361 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1362 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1363 prevtime
= currtime
;
1368 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1370 if (unlikely(!hw
->buf_emul
)) {
1371 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1372 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1373 hw
->pos_emul
= hw
->pending_emul
= 0;
1376 while (hw
->pending_emul
< hw
->size_emul
) {
1377 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1378 hw
->size_emul
- hw
->pending_emul
);
1379 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1381 hw
->pending_emul
+= read
;
1382 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1383 if (read
< read_len
) {
1389 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1393 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1394 assert(start
< hw
->size_emul
);
1396 *size
= MIN(*size
, hw
->pending_emul
);
1397 *size
= MIN(*size
, hw
->size_emul
- start
);
1398 return hw
->buf_emul
+ start
;
1401 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1403 assert(size
<= hw
->pending_emul
);
1404 hw
->pending_emul
-= size
;
1407 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1409 while (hw
->pending_emul
) {
1410 size_t write_len
, written
, start
;
1412 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1413 assert(start
< hw
->size_emul
);
1415 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1417 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1418 hw
->pending_emul
-= written
;
1420 if (written
< write_len
) {
1426 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1428 if (unlikely(!hw
->buf_emul
)) {
1429 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1430 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1431 hw
->pos_emul
= hw
->pending_emul
= 0;
1434 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1435 hw
->size_emul
- hw
->pos_emul
);
1436 return hw
->buf_emul
+ hw
->pos_emul
;
1439 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1441 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1442 size
+ hw
->pending_emul
<= hw
->size_emul
);
1444 hw
->pending_emul
+= size
;
1445 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1450 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1454 while (total
< size
) {
1455 size_t dst_size
= size
- total
;
1456 size_t copy_size
, proc
;
1457 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1459 if (dst_size
== 0) {
1463 copy_size
= MIN(size
- total
, dst_size
);
1465 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1467 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1470 if (proc
== 0 || proc
< copy_size
) {
1475 if (hw
->pcm_ops
->run_buffer_out
) {
1476 hw
->pcm_ops
->run_buffer_out(hw
);
1482 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1486 if (hw
->pcm_ops
->run_buffer_in
) {
1487 hw
->pcm_ops
->run_buffer_in(hw
);
1490 while (total
< size
) {
1491 size_t src_size
= size
- total
;
1492 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1494 if (src_size
== 0) {
1498 memcpy((char *)buf
+ total
, src
, src_size
);
1499 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1506 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1507 bool msg
, Audiodev
*dev
)
1509 s
->drv_opaque
= drv
->init(dev
);
1511 if (s
->drv_opaque
) {
1512 if (!drv
->pcm_ops
->get_buffer_in
) {
1513 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1514 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1516 if (!drv
->pcm_ops
->get_buffer_out
) {
1517 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1518 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1521 audio_init_nb_voices_out(s
, drv
);
1522 audio_init_nb_voices_in(s
, drv
);
1527 dolog("Could not init `%s' audio driver\n", drv
->name
);
1533 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1536 AudioState
*s
= opaque
;
1537 HWVoiceOut
*hwo
= NULL
;
1538 HWVoiceIn
*hwi
= NULL
;
1540 s
->vm_running
= running
;
1541 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1542 if (hwo
->pcm_ops
->enable_out
) {
1543 hwo
->pcm_ops
->enable_out(hwo
, running
);
1547 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1548 if (hwi
->pcm_ops
->enable_in
) {
1549 hwi
->pcm_ops
->enable_in(hwi
, running
);
1552 audio_reset_timer (s
);
1555 static void free_audio_state(AudioState
*s
)
1557 HWVoiceOut
*hwo
, *hwon
;
1558 HWVoiceIn
*hwi
, *hwin
;
1560 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1563 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1564 hwo
->pcm_ops
->enable_out(hwo
, false);
1566 hwo
->pcm_ops
->fini_out (hwo
);
1568 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1569 CaptureVoiceOut
*cap
= sc
->cap
;
1570 struct capture_callback
*cb
;
1572 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1573 cb
->ops
.destroy (cb
->opaque
);
1576 QLIST_REMOVE(hwo
, entries
);
1579 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1580 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1581 hwi
->pcm_ops
->enable_in(hwi
, false);
1583 hwi
->pcm_ops
->fini_in (hwi
);
1584 QLIST_REMOVE(hwi
, entries
);
1588 s
->drv
->fini (s
->drv_opaque
);
1593 qapi_free_Audiodev(s
->dev
);
1605 void audio_cleanup(void)
1607 while (!QTAILQ_EMPTY(&audio_states
)) {
1608 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1609 QTAILQ_REMOVE(&audio_states
, s
, list
);
1610 free_audio_state(s
);
1614 static bool vmstate_audio_needed(void *opaque
)
1617 * Never needed, this vmstate only exists in case
1618 * an old qemu sends it to us.
1623 static const VMStateDescription vmstate_audio
= {
1626 .minimum_version_id
= 1,
1627 .needed
= vmstate_audio_needed
,
1628 .fields
= (VMStateField
[]) {
1629 VMSTATE_END_OF_LIST()
1633 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1635 static AudiodevListEntry
*audiodev_find(
1636 AudiodevListHead
*head
, const char *drvname
)
1638 AudiodevListEntry
*e
;
1639 QSIMPLEQ_FOREACH(e
, head
, next
) {
1640 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1649 * if we have dev, this function was called because of an -audiodev argument =>
1650 * initialize a new state with it
1651 * if dev == NULL => legacy implicit initialization, return the already created
1652 * state or create a new one
1654 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1656 static bool atexit_registered
;
1659 const char *drvname
= NULL
;
1660 VMChangeStateEntry
*e
;
1662 struct audio_driver
*driver
;
1663 /* silence gcc warning about uninitialized variable */
1664 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1668 * When using spice allow the spice audio driver being picked
1671 * Temporary hack. Using audio devices without explicit
1672 * audiodev= property is already deprecated. Same goes for
1673 * the -soundhw switch. Once this support gets finally
1674 * removed we can also drop the concept of a default audio
1675 * backend and this can go away.
1677 driver
= audio_driver_lookup("spice");
1679 driver
->can_be_default
= 1;
1684 /* -audiodev option */
1685 legacy_config
= false;
1686 drvname
= AudiodevDriver_str(dev
->driver
);
1687 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1688 if (!legacy_config
) {
1689 dolog("Device %s: audiodev default parameter is deprecated, please "
1690 "specify audiodev=%s\n", name
,
1691 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1693 return QTAILQ_FIRST(&audio_states
);
1695 /* legacy implicit initialization */
1696 head
= audio_handle_legacy_opts();
1698 * In case of legacy initialization, all Audiodevs in the list will have
1699 * the same configuration (except the driver), so it doesn't matter which
1700 * one we chose. We need an Audiodev to set up AudioState before we can
1701 * init a driver. Also note that dev at this point is still in the
1704 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1705 audio_validate_opts(dev
, &error_abort
);
1708 s
= g_malloc0(sizeof(AudioState
));
1711 QLIST_INIT (&s
->hw_head_out
);
1712 QLIST_INIT (&s
->hw_head_in
);
1713 QLIST_INIT (&s
->cap_head
);
1714 if (!atexit_registered
) {
1715 atexit(audio_cleanup
);
1716 atexit_registered
= true;
1718 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1720 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1722 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1723 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1725 if (s
->nb_hw_voices_out
<= 0) {
1726 dolog ("Bogus number of playback voices %d, setting to 1\n",
1727 s
->nb_hw_voices_out
);
1728 s
->nb_hw_voices_out
= 1;
1731 if (s
->nb_hw_voices_in
<= 0) {
1732 dolog ("Bogus number of capture voices %d, setting to 0\n",
1733 s
->nb_hw_voices_in
);
1734 s
->nb_hw_voices_in
= 0;
1738 driver
= audio_driver_lookup(drvname
);
1740 done
= !audio_driver_init(s
, driver
, true, dev
);
1742 dolog ("Unknown audio driver `%s'\n", drvname
);
1745 for (i
= 0; audio_prio_list
[i
]; i
++) {
1746 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1747 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1750 s
->dev
= dev
= e
->dev
;
1751 audio_validate_opts(dev
, &error_abort
);
1752 done
= !audio_driver_init(s
, driver
, false, dev
);
1760 audio_free_audiodev_list(&head
);
1763 driver
= audio_driver_lookup("none");
1764 done
= !audio_driver_init(s
, driver
, false, dev
);
1766 dolog("warning: Using timer based audio emulation\n");
1769 if (dev
->timer_period
<= 0) {
1770 s
->period_ticks
= 1;
1772 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1775 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1777 dolog ("warning: Could not register change state handler\n"
1778 "(Audio can continue looping even after stopping the VM)\n");
1781 QLIST_INIT (&s
->card_head
);
1782 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1786 void audio_free_audiodev_list(AudiodevListHead
*head
)
1788 AudiodevListEntry
*e
;
1789 while ((e
= QSIMPLEQ_FIRST(head
))) {
1790 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1791 qapi_free_Audiodev(e
->dev
);
1796 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1799 card
->state
= audio_init(NULL
, name
);
1802 card
->name
= g_strdup (name
);
1803 memset (&card
->entries
, 0, sizeof (card
->entries
));
1804 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1807 void AUD_remove_card (QEMUSoundCard
*card
)
1809 QLIST_REMOVE (card
, entries
);
1810 g_free (card
->name
);
1814 CaptureVoiceOut
*AUD_add_capture(
1816 struct audsettings
*as
,
1817 struct audio_capture_ops
*ops
,
1821 CaptureVoiceOut
*cap
;
1822 struct capture_callback
*cb
;
1825 if (!legacy_config
) {
1826 dolog("Capturing without setting an audiodev is deprecated\n");
1828 s
= audio_init(NULL
, NULL
);
1831 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1832 dolog("Can't capture with mixeng disabled\n");
1836 if (audio_validate_settings (as
)) {
1837 dolog ("Invalid settings were passed when trying to add capture\n");
1838 audio_print_settings (as
);
1842 cb
= g_malloc0(sizeof(*cb
));
1844 cb
->opaque
= cb_opaque
;
1846 cap
= audio_pcm_capture_find_specific(s
, as
);
1848 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1852 CaptureVoiceOut
*cap
;
1854 cap
= g_malloc0(sizeof(*cap
));
1858 QLIST_INIT (&hw
->sw_head
);
1859 QLIST_INIT (&cap
->cb_head
);
1861 /* XXX find a more elegant way */
1862 hw
->samples
= 4096 * 4;
1863 audio_pcm_hw_alloc_resources_out(hw
);
1865 audio_pcm_init_info (&hw
->info
, as
);
1867 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1869 if (hw
->info
.is_float
) {
1870 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1872 hw
->clip
= mixeng_clip
1873 [hw
->info
.nchannels
== 2]
1874 [hw
->info
.is_signed
]
1875 [hw
->info
.swap_endianness
]
1876 [audio_bits_to_index(hw
->info
.bits
)];
1879 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1880 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1882 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1883 audio_attach_capture (hw
);
1889 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1891 struct capture_callback
*cb
;
1893 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1894 if (cb
->opaque
== cb_opaque
) {
1895 cb
->ops
.destroy (cb_opaque
);
1896 QLIST_REMOVE (cb
, entries
);
1899 if (!cap
->cb_head
.lh_first
) {
1900 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1903 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1904 #ifdef DEBUG_CAPTURE
1905 dolog ("freeing %s\n", sw
->name
);
1908 sw1
= sw
->entries
.le_next
;
1910 st_rate_stop (sw
->rate
);
1913 QLIST_REMOVE (sw
, entries
);
1914 QLIST_REMOVE (sc
, entries
);
1918 QLIST_REMOVE (cap
, entries
);
1919 g_free (cap
->hw
.mix_buf
);
1928 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1930 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1931 audio_set_volume_out(sw
, &vol
);
1934 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1937 HWVoiceOut
*hw
= sw
->hw
;
1939 sw
->vol
.mute
= vol
->mute
;
1940 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1941 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1944 if (hw
->pcm_ops
->volume_out
) {
1945 hw
->pcm_ops
->volume_out(hw
, vol
);
1950 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1952 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1953 audio_set_volume_in(sw
, &vol
);
1956 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1959 HWVoiceIn
*hw
= sw
->hw
;
1961 sw
->vol
.mute
= vol
->mute
;
1962 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1963 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1966 if (hw
->pcm_ops
->volume_in
) {
1967 hw
->pcm_ops
->volume_in(hw
, vol
);
1972 void audio_create_pdos(Audiodev
*dev
)
1974 switch (dev
->driver
) {
1975 #define CASE(DRIVER, driver, pdo_name) \
1976 case AUDIODEV_DRIVER_##DRIVER: \
1977 if (!dev->u.driver.has_in) { \
1978 dev->u.driver.in = g_malloc0( \
1979 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1980 dev->u.driver.has_in = true; \
1982 if (!dev->u.driver.has_out) { \
1983 dev->u.driver.out = g_malloc0( \
1984 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1985 dev->u.driver.has_out = true; \
1990 CASE(ALSA
, alsa
, Alsa
);
1991 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1993 CASE(DSOUND
, dsound
, );
1994 CASE(JACK
, jack
, Jack
);
1995 CASE(OSS
, oss
, Oss
);
1997 CASE(SDL
, sdl
, Sdl
);
1998 CASE(SPICE
, spice
, );
2001 case AUDIODEV_DRIVER__MAX
:
2006 static void audio_validate_per_direction_opts(
2007 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2009 if (!pdo
->has_mixing_engine
) {
2010 pdo
->has_mixing_engine
= true;
2011 pdo
->mixing_engine
= true;
2013 if (!pdo
->has_fixed_settings
) {
2014 pdo
->has_fixed_settings
= true;
2015 pdo
->fixed_settings
= pdo
->mixing_engine
;
2017 if (!pdo
->fixed_settings
&&
2018 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2020 "You can't use frequency, channels or format with fixed-settings=off");
2023 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2024 error_setg(errp
, "You can't use fixed-settings without mixeng");
2028 if (!pdo
->has_frequency
) {
2029 pdo
->has_frequency
= true;
2030 pdo
->frequency
= 44100;
2032 if (!pdo
->has_channels
) {
2033 pdo
->has_channels
= true;
2036 if (!pdo
->has_voices
) {
2037 pdo
->has_voices
= true;
2038 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2040 if (!pdo
->has_format
) {
2041 pdo
->has_format
= true;
2042 pdo
->format
= AUDIO_FORMAT_S16
;
2046 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2050 audio_create_pdos(dev
);
2052 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2054 error_propagate(errp
, err
);
2058 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2060 error_propagate(errp
, err
);
2064 if (!dev
->has_timer_period
) {
2065 dev
->has_timer_period
= true;
2066 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2070 void audio_parse_option(const char *opt
)
2072 AudiodevListEntry
*e
;
2073 Audiodev
*dev
= NULL
;
2075 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2076 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2079 audio_validate_opts(dev
, &error_fatal
);
2081 e
= g_malloc0(sizeof(AudiodevListEntry
));
2083 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2086 void audio_init_audiodevs(void)
2088 AudiodevListEntry
*e
;
2090 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2091 audio_init(e
->dev
, NULL
);
2095 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2097 return (audsettings
) {
2098 .freq
= pdo
->frequency
,
2099 .nchannels
= pdo
->channels
,
2101 .endianness
= AUDIO_HOST_ENDIANNESS
,
2105 int audioformat_bytes_per_sample(AudioFormat fmt
)
2108 case AUDIO_FORMAT_U8
:
2109 case AUDIO_FORMAT_S8
:
2112 case AUDIO_FORMAT_U16
:
2113 case AUDIO_FORMAT_S16
:
2116 case AUDIO_FORMAT_U32
:
2117 case AUDIO_FORMAT_S32
:
2118 case AUDIO_FORMAT_F32
:
2121 case AUDIO_FORMAT__MAX
:
2128 /* frames = freq * usec / 1e6 */
2129 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2130 audsettings
*as
, int def_usecs
)
2132 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2133 return (as
->freq
* usecs
+ 500000) / 1000000;
2136 /* samples = channels * frames = channels * freq * usec / 1e6 */
2137 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2138 audsettings
*as
, int def_usecs
)
2140 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2144 * bytes = bytes_per_sample * samples =
2145 * bytes_per_sample * channels * freq * usec / 1e6
2147 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2148 audsettings
*as
, int def_usecs
)
2150 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2151 audioformat_bytes_per_sample(as
->fmt
);
2154 AudioState
*audio_state_by_name(const char *name
)
2157 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2159 if (strcmp(name
, s
->dev
->id
) == 0) {
2166 const char *audio_get_id(QEMUSoundCard
*card
)
2169 assert(card
->state
->dev
);
2170 return card
->state
->dev
->id
;
2176 const char *audio_application_name(void)
2178 const char *vm_name
;
2180 vm_name
= qemu_get_vm_name();
2181 return vm_name
? vm_name
: "qemu";
2184 void audio_rate_start(RateCtl
*rate
)
2186 memset(rate
, 0, sizeof(RateCtl
));
2187 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2190 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2199 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2200 ticks
= now
- rate
->start_ticks
;
2201 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2202 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2203 if (samples
< 0 || samples
> 65536) {
2204 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2205 audio_rate_start(rate
);
2209 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2210 rate
->bytes_sent
+= ret
;