4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "migration/vmstate.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qapi-visit-audio.h"
33 #include "qemu/cutils.h"
34 #include "qemu/module.h"
35 #include "sysemu/replay.h"
36 #include "sysemu/runstate.h"
39 #define AUDIO_CAP "audio"
40 #include "audio_int.h"
42 /* #define DEBUG_LIVE */
43 /* #define DEBUG_OUT */
44 /* #define DEBUG_CAPTURE */
45 /* #define DEBUG_POLL */
47 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
50 /* Order of CONFIG_AUDIO_DRIVERS is import.
51 The 1st one is the one used by default, that is the reason
52 that we generate the list.
54 const char *audio_prio_list
[] = {
62 static QLIST_HEAD(, audio_driver
) audio_drivers
;
63 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
65 void audio_driver_register(audio_driver
*drv
)
67 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
70 audio_driver
*audio_driver_lookup(const char *name
)
72 struct audio_driver
*d
;
74 QLIST_FOREACH(d
, &audio_drivers
, next
) {
75 if (strcmp(name
, d
->name
) == 0) {
80 audio_module_load_one(name
);
81 QLIST_FOREACH(d
, &audio_drivers
, next
) {
82 if (strcmp(name
, d
->name
) == 0) {
90 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
91 QTAILQ_HEAD_INITIALIZER(audio_states
);
93 const struct mixeng_volume nominal_volume
= {
104 static bool legacy_config
= true;
106 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
109 int audio_bug (const char *funcname
, int cond
)
114 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
117 AUD_log (NULL
, "Save all your work and restart without audio\n");
118 AUD_log (NULL
, "I am sorry\n");
120 AUD_log (NULL
, "Context:\n");
122 #if defined AUDIO_BREAKPOINT_ON_BUG
123 # if defined HOST_I386
124 # if defined __GNUC__
126 # elif defined _MSC_VER
141 static inline int audio_bits_to_index (int bits
)
154 audio_bug ("bits_to_index", 1);
155 AUD_log (NULL
, "invalid bits %d\n", bits
);
160 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
166 cond
= !nmemb
|| !size
;
170 if (audio_bug ("audio_calloc", cond
)) {
171 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
173 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
177 return g_malloc0 (len
);
180 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
183 fprintf(stderr
, "%s: ", cap
);
186 vfprintf(stderr
, fmt
, ap
);
189 void AUD_log (const char *cap
, const char *fmt
, ...)
194 AUD_vlog (cap
, fmt
, ap
);
198 static void audio_print_settings (struct audsettings
*as
)
200 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
203 case AUDIO_FORMAT_S8
:
204 AUD_log (NULL
, "S8");
206 case AUDIO_FORMAT_U8
:
207 AUD_log (NULL
, "U8");
209 case AUDIO_FORMAT_S16
:
210 AUD_log (NULL
, "S16");
212 case AUDIO_FORMAT_U16
:
213 AUD_log (NULL
, "U16");
215 case AUDIO_FORMAT_S32
:
216 AUD_log (NULL
, "S32");
218 case AUDIO_FORMAT_U32
:
219 AUD_log (NULL
, "U32");
222 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
226 AUD_log (NULL
, " endianness=");
227 switch (as
->endianness
) {
229 AUD_log (NULL
, "little");
232 AUD_log (NULL
, "big");
235 AUD_log (NULL
, "invalid");
238 AUD_log (NULL
, "\n");
241 static int audio_validate_settings (struct audsettings
*as
)
245 invalid
= as
->nchannels
< 1;
246 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
249 case AUDIO_FORMAT_S8
:
250 case AUDIO_FORMAT_U8
:
251 case AUDIO_FORMAT_S16
:
252 case AUDIO_FORMAT_U16
:
253 case AUDIO_FORMAT_S32
:
254 case AUDIO_FORMAT_U32
:
261 invalid
|= as
->freq
<= 0;
262 return invalid
? -1 : 0;
265 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
267 int bits
= 8, sign
= 0;
270 case AUDIO_FORMAT_S8
:
273 case AUDIO_FORMAT_U8
:
276 case AUDIO_FORMAT_S16
:
279 case AUDIO_FORMAT_U16
:
283 case AUDIO_FORMAT_S32
:
286 case AUDIO_FORMAT_U32
:
293 return info
->freq
== as
->freq
294 && info
->nchannels
== as
->nchannels
295 && info
->sign
== sign
296 && info
->bits
== bits
297 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
300 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
302 int bits
= 8, sign
= 0, mul
;
305 case AUDIO_FORMAT_S8
:
308 case AUDIO_FORMAT_U8
:
312 case AUDIO_FORMAT_S16
:
315 case AUDIO_FORMAT_U16
:
320 case AUDIO_FORMAT_S32
:
323 case AUDIO_FORMAT_U32
:
332 info
->freq
= as
->freq
;
335 info
->nchannels
= as
->nchannels
;
336 info
->bytes_per_frame
= as
->nchannels
* mul
;
337 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
338 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
341 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
348 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
351 switch (info
->bits
) {
353 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
362 if (info
->swap_endianness
) {
366 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
376 int32_t s
= INT32_MAX
;
378 if (info
->swap_endianness
) {
382 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
389 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
399 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
406 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
407 struct audsettings
*as
)
409 CaptureVoiceOut
*cap
;
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
= hw
->s
;
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 size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
533 size_t m
= hw
->total_samples_captured
;
535 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
537 m
= MIN (m
, sw
->total_hw_samples_acquired
);
543 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
545 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
546 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
547 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
553 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
556 size_t pos
= hw
->mix_buf
->pos
;
559 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
560 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
561 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
562 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
564 hw
->clip(dst
, src
, samples_to_clip
);
566 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
567 len
-= samples_to_clip
;
568 clipped
+= samples_to_clip
;
573 * Soft voice (capture)
575 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
577 HWVoiceIn
*hw
= sw
->hw
;
578 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
581 if (audio_bug(__func__
, live
< 0 || live
> hw
->conv_buf
->size
)) {
582 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
586 rpos
= hw
->conv_buf
->pos
- live
;
591 return hw
->conv_buf
->size
+ rpos
;
595 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
597 HWVoiceIn
*hw
= sw
->hw
;
598 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
599 struct st_sample
*src
, *dst
= sw
->buf
;
601 rpos
= audio_pcm_sw_get_rpos_in(sw
) % hw
->conv_buf
->size
;
603 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
604 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
605 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
609 samples
= size
/ sw
->info
.bytes_per_frame
;
614 swlim
= (live
* sw
->ratio
) >> 32;
615 swlim
= MIN (swlim
, samples
);
618 src
= hw
->conv_buf
->samples
+ rpos
;
619 if (hw
->conv_buf
->pos
> rpos
) {
620 isamp
= hw
->conv_buf
->pos
- rpos
;
622 isamp
= hw
->conv_buf
->size
- rpos
;
630 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
632 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
638 if (!hw
->pcm_ops
->volume_in
) {
639 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
642 sw
->clip (buf
, sw
->buf
, ret
);
643 sw
->total_hw_samples_acquired
+= total
;
644 return ret
* sw
->info
.bytes_per_frame
;
648 * Hard voice (playback)
650 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
656 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
657 if (sw
->active
|| !sw
->empty
) {
658 m
= MIN (m
, sw
->total_hw_samples_mixed
);
667 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
672 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
680 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
681 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
690 * Soft voice (playback)
692 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
694 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
695 size_t ret
= 0, pos
= 0, total
= 0;
701 hwsamples
= sw
->hw
->mix_buf
->size
;
703 live
= sw
->total_hw_samples_mixed
;
704 if (audio_bug(__func__
, live
> hwsamples
)) {
705 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
709 if (live
== hwsamples
) {
711 dolog ("%s is full %d\n", sw
->name
, live
);
716 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
717 samples
= size
/ sw
->info
.bytes_per_frame
;
719 dead
= hwsamples
- live
;
720 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
721 swlim
= MIN (swlim
, samples
);
723 sw
->conv (sw
->buf
, buf
, swlim
);
725 if (!sw
->hw
->pcm_ops
->volume_out
) {
726 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
731 dead
= hwsamples
- live
;
732 left
= hwsamples
- wpos
;
733 blck
= MIN (dead
, left
);
742 sw
->hw
->mix_buf
->samples
+ wpos
,
750 wpos
= (wpos
+ osamp
) % hwsamples
;
754 sw
->total_hw_samples_mixed
+= total
;
755 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
759 "%s: write size %zu ret %zu total sw %zu\n",
761 size
/ sw
->info
.bytes_per_frame
,
763 sw
->total_hw_samples_mixed
767 return ret
* sw
->info
.bytes_per_frame
;
771 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
773 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
774 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
779 #include "audio_template.h"
781 #include "audio_template.h"
786 static int audio_is_timer_needed(AudioState
*s
)
788 HWVoiceIn
*hwi
= NULL
;
789 HWVoiceOut
*hwo
= NULL
;
791 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
792 if (!hwo
->poll_mode
) return 1;
794 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
795 if (!hwi
->poll_mode
) return 1;
800 static void audio_reset_timer (AudioState
*s
)
802 if (audio_is_timer_needed(s
)) {
803 timer_mod_anticipate_ns(s
->ts
,
804 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
805 if (!s
->timer_running
) {
806 s
->timer_running
= true;
807 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
808 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
812 if (s
->timer_running
) {
813 s
->timer_running
= false;
814 trace_audio_timer_stop();
819 static void audio_timer (void *opaque
)
822 AudioState
*s
= opaque
;
824 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
825 diff
= now
- s
->timer_last
;
826 if (diff
> s
->period_ticks
* 3 / 2) {
827 trace_audio_timer_delayed(diff
/ SCALE_MS
);
831 audio_run(s
, "timer");
832 audio_reset_timer(s
);
838 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
843 /* XXX: Consider options */
849 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
853 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
854 return audio_pcm_sw_write(sw
, buf
, size
);
856 return hw
->pcm_ops
->write(hw
, buf
, size
);
860 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
865 /* XXX: Consider options */
871 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
875 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
876 return audio_pcm_sw_read(sw
, buf
, size
);
878 return hw
->pcm_ops
->read(hw
, buf
, size
);
882 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
884 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
887 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
896 if (sw
->active
!= on
) {
897 AudioState
*s
= sw
->s
;
902 hw
->pending_disable
= 0;
906 if (hw
->pcm_ops
->enable_out
) {
907 hw
->pcm_ops
->enable_out(hw
, true);
909 audio_reset_timer (s
);
917 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
918 temp_sw
= temp_sw
->entries
.le_next
) {
919 nb_active
+= temp_sw
->active
!= 0;
922 hw
->pending_disable
= nb_active
== 1;
926 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
927 sc
->sw
.active
= hw
->enabled
;
929 audio_capture_maybe_changed (sc
->cap
, 1);
936 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
945 if (sw
->active
!= on
) {
946 AudioState
*s
= sw
->s
;
953 if (hw
->pcm_ops
->enable_in
) {
954 hw
->pcm_ops
->enable_in(hw
, true);
956 audio_reset_timer (s
);
959 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
965 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
966 temp_sw
= temp_sw
->entries
.le_next
) {
967 nb_active
+= temp_sw
->active
!= 0;
970 if (nb_active
== 1) {
972 if (hw
->pcm_ops
->enable_in
) {
973 hw
->pcm_ops
->enable_in(hw
, false);
982 static size_t audio_get_avail (SWVoiceIn
*sw
)
990 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
991 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
992 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
993 sw
->hw
->conv_buf
->size
);
998 "%s: get_avail live %d ret %" PRId64
"\n",
1000 live
, (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
1003 return (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1006 static size_t audio_get_free(SWVoiceOut
*sw
)
1014 live
= sw
->total_hw_samples_mixed
;
1016 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1017 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1018 sw
->hw
->mix_buf
->size
);
1022 dead
= sw
->hw
->mix_buf
->size
- live
;
1025 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1027 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) *
1028 sw
->info
.bytes_per_frame
);
1031 return (((int64_t) dead
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1034 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1042 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1043 SWVoiceOut
*sw
= &sc
->sw
;
1048 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1049 size_t to_write
= MIN(till_end_of_hw
, n
);
1050 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1053 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1054 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1055 if (written
- bytes
) {
1056 dolog("Could not mix %zu bytes into a capture "
1057 "buffer, mixed %zu\n",
1062 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1067 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1068 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1069 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1072 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1077 size_t size
, decr
, proc
;
1078 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1080 /* retrying will likely won't help, drop everything. */
1081 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ live
) % hw
->mix_buf
->size
;
1082 return clipped
+ live
;
1085 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1086 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1087 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1088 decr
* hw
->info
.bytes_per_frame
) /
1089 hw
->info
.bytes_per_frame
;
1093 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1095 if (proc
== 0 || proc
< decr
) {
1100 if (hw
->pcm_ops
->run_buffer_out
) {
1101 hw
->pcm_ops
->run_buffer_out(hw
);
1107 static void audio_run_out (AudioState
*s
)
1109 HWVoiceOut
*hw
= NULL
;
1112 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1113 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1114 /* there is exactly 1 sw for each hw with no mixeng */
1115 sw
= hw
->sw_head
.lh_first
;
1117 if (hw
->pending_disable
) {
1119 hw
->pending_disable
= 0;
1120 if (hw
->pcm_ops
->enable_out
) {
1121 hw
->pcm_ops
->enable_out(hw
, false);
1126 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1132 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1133 size_t played
, live
, prev_rpos
, free
;
1134 int nb_live
, cleanup_required
;
1136 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1141 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1142 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1146 if (hw
->pending_disable
&& !nb_live
) {
1149 dolog ("Disabling voice\n");
1152 hw
->pending_disable
= 0;
1153 if (hw
->pcm_ops
->enable_out
) {
1154 hw
->pcm_ops
->enable_out(hw
, false);
1156 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1158 audio_recalc_and_notify_capture (sc
->cap
);
1164 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1166 free
= audio_get_free (sw
);
1168 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1175 prev_rpos
= hw
->mix_buf
->pos
;
1176 played
= audio_pcm_hw_run_out(hw
, live
);
1177 replay_audio_out(&played
);
1178 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1179 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1180 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1181 hw
->mix_buf
->pos
= 0;
1185 dolog("played=%zu\n", played
);
1189 hw
->ts_helper
+= played
;
1190 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1193 cleanup_required
= 0;
1194 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1195 if (!sw
->active
&& sw
->empty
) {
1199 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1200 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1201 played
, sw
->total_hw_samples_mixed
);
1202 played
= sw
->total_hw_samples_mixed
;
1205 sw
->total_hw_samples_mixed
-= played
;
1207 if (!sw
->total_hw_samples_mixed
) {
1209 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1213 free
= audio_get_free (sw
);
1215 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1220 if (cleanup_required
) {
1223 sw
= hw
->sw_head
.lh_first
;
1225 sw1
= sw
->entries
.le_next
;
1226 if (!sw
->active
&& !sw
->callback
.fn
) {
1227 audio_close_out (sw
);
1235 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1238 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
1242 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1243 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1245 assert(size
% hw
->info
.bytes_per_frame
== 0);
1247 hw
->pcm_ops
->put_buffer_in(hw
, buf
, size
);
1251 proc
= MIN(size
/ hw
->info
.bytes_per_frame
,
1252 conv_buf
->size
- conv_buf
->pos
);
1254 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, buf
, proc
);
1255 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
1259 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1265 static void audio_run_in (AudioState
*s
)
1267 HWVoiceIn
*hw
= NULL
;
1269 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1270 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1271 /* there is exactly 1 sw for each hw with no mixeng */
1272 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1274 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1280 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1282 size_t captured
= 0, min
;
1284 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1285 captured
= audio_pcm_hw_run_in(
1286 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1288 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1289 hw
->conv_buf
->size
);
1291 min
= audio_pcm_hw_find_min_in (hw
);
1292 hw
->total_samples_captured
+= captured
- min
;
1293 hw
->ts_helper
+= captured
;
1295 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1296 sw
->total_hw_samples_acquired
-= min
;
1301 avail
= audio_get_avail (sw
);
1303 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1310 static void audio_run_capture (AudioState
*s
)
1312 CaptureVoiceOut
*cap
;
1314 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1315 size_t live
, rpos
, captured
;
1316 HWVoiceOut
*hw
= &cap
->hw
;
1319 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1320 rpos
= hw
->mix_buf
->pos
;
1322 size_t left
= hw
->mix_buf
->size
- rpos
;
1323 size_t to_capture
= MIN(live
, left
);
1324 struct st_sample
*src
;
1325 struct capture_callback
*cb
;
1327 src
= hw
->mix_buf
->samples
+ rpos
;
1328 hw
->clip (cap
->buf
, src
, to_capture
);
1329 mixeng_clear (src
, to_capture
);
1331 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1332 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1333 to_capture
* hw
->info
.bytes_per_frame
);
1335 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1338 hw
->mix_buf
->pos
= rpos
;
1340 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1341 if (!sw
->active
&& sw
->empty
) {
1345 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1346 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1347 captured
, sw
->total_hw_samples_mixed
);
1348 captured
= sw
->total_hw_samples_mixed
;
1351 sw
->total_hw_samples_mixed
-= captured
;
1352 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1357 void audio_run(AudioState
*s
, const char *msg
)
1361 audio_run_capture(s
);
1365 static double prevtime
;
1369 if (gettimeofday (&tv
, NULL
)) {
1370 perror ("audio_run: gettimeofday");
1374 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1375 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1376 prevtime
= currtime
;
1381 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1385 if (unlikely(!hw
->buf_emul
)) {
1386 size_t calc_size
= hw
->conv_buf
->size
* hw
->info
.bytes_per_frame
;
1387 hw
->buf_emul
= g_malloc(calc_size
);
1388 hw
->size_emul
= calc_size
;
1389 hw
->pos_emul
= hw
->pending_emul
= 0;
1392 while (hw
->pending_emul
< hw
->size_emul
) {
1393 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1394 hw
->size_emul
- hw
->pending_emul
);
1395 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1397 hw
->pending_emul
+= read
;
1398 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1399 if (read
< read_len
) {
1404 start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1406 start
+= hw
->size_emul
;
1408 assert(start
>= 0 && start
< hw
->size_emul
);
1410 *size
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1411 return hw
->buf_emul
+ start
;
1414 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1416 assert(size
<= hw
->pending_emul
);
1417 hw
->pending_emul
-= size
;
1420 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1422 while (hw
->pending_emul
) {
1423 size_t write_len
, written
;
1424 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1427 start
+= hw
->size_emul
;
1429 assert(start
>= 0 && start
< hw
->size_emul
);
1431 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1433 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1434 hw
->pending_emul
-= written
;
1436 if (written
< write_len
) {
1442 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1444 if (unlikely(!hw
->buf_emul
)) {
1445 size_t calc_size
= hw
->mix_buf
->size
* hw
->info
.bytes_per_frame
;
1447 hw
->buf_emul
= g_malloc(calc_size
);
1448 hw
->size_emul
= calc_size
;
1449 hw
->pos_emul
= hw
->pending_emul
= 0;
1452 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1453 hw
->size_emul
- hw
->pos_emul
);
1454 return hw
->buf_emul
+ hw
->pos_emul
;
1457 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1459 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1460 size
+ hw
->pending_emul
<= hw
->size_emul
);
1462 hw
->pending_emul
+= size
;
1463 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1468 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1470 size_t dst_size
, copy_size
;
1471 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1472 copy_size
= MIN(size
, dst_size
);
1474 memcpy(dst
, buf
, copy_size
);
1475 return hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1478 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1480 size_t src_size
, copy_size
;
1481 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1482 copy_size
= MIN(size
, src_size
);
1484 memcpy(buf
, src
, copy_size
);
1485 hw
->pcm_ops
->put_buffer_in(hw
, src
, copy_size
);
1490 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1491 bool msg
, Audiodev
*dev
)
1493 s
->drv_opaque
= drv
->init(dev
);
1495 if (s
->drv_opaque
) {
1496 if (!drv
->pcm_ops
->get_buffer_in
) {
1497 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1498 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1500 if (!drv
->pcm_ops
->get_buffer_out
) {
1501 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1502 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1505 audio_init_nb_voices_out(s
, drv
);
1506 audio_init_nb_voices_in(s
, drv
);
1512 dolog("Could not init `%s' audio driver\n", drv
->name
);
1518 static void audio_vm_change_state_handler (void *opaque
, int running
,
1521 AudioState
*s
= opaque
;
1522 HWVoiceOut
*hwo
= NULL
;
1523 HWVoiceIn
*hwi
= NULL
;
1525 s
->vm_running
= running
;
1526 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1527 if (hwo
->pcm_ops
->enable_out
) {
1528 hwo
->pcm_ops
->enable_out(hwo
, running
);
1532 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1533 if (hwi
->pcm_ops
->enable_in
) {
1534 hwi
->pcm_ops
->enable_in(hwi
, running
);
1537 audio_reset_timer (s
);
1540 static bool is_cleaning_up
;
1542 bool audio_is_cleaning_up(void)
1544 return is_cleaning_up
;
1547 static void free_audio_state(AudioState
*s
)
1549 HWVoiceOut
*hwo
, *hwon
;
1550 HWVoiceIn
*hwi
, *hwin
;
1552 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1555 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1556 hwo
->pcm_ops
->enable_out(hwo
, false);
1558 hwo
->pcm_ops
->fini_out (hwo
);
1560 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1561 CaptureVoiceOut
*cap
= sc
->cap
;
1562 struct capture_callback
*cb
;
1564 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1565 cb
->ops
.destroy (cb
->opaque
);
1568 QLIST_REMOVE(hwo
, entries
);
1571 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1572 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1573 hwi
->pcm_ops
->enable_in(hwi
, false);
1575 hwi
->pcm_ops
->fini_in (hwi
);
1576 QLIST_REMOVE(hwi
, entries
);
1580 s
->drv
->fini (s
->drv_opaque
);
1585 qapi_free_Audiodev(s
->dev
);
1597 void audio_cleanup(void)
1599 is_cleaning_up
= true;
1600 while (!QTAILQ_EMPTY(&audio_states
)) {
1601 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1602 QTAILQ_REMOVE(&audio_states
, s
, list
);
1603 free_audio_state(s
);
1607 static const VMStateDescription vmstate_audio
= {
1610 .minimum_version_id
= 1,
1611 .fields
= (VMStateField
[]) {
1612 VMSTATE_END_OF_LIST()
1616 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1618 static AudiodevListEntry
*audiodev_find(
1619 AudiodevListHead
*head
, const char *drvname
)
1621 AudiodevListEntry
*e
;
1622 QSIMPLEQ_FOREACH(e
, head
, next
) {
1623 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1632 * if we have dev, this function was called because of an -audiodev argument =>
1633 * initialize a new state with it
1634 * if dev == NULL => legacy implicit initialization, return the already created
1635 * state or create a new one
1637 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1639 static bool atexit_registered
;
1642 const char *drvname
= NULL
;
1643 VMChangeStateEntry
*e
;
1645 struct audio_driver
*driver
;
1646 /* silence gcc warning about uninitialized variable */
1647 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1650 /* -audiodev option */
1651 legacy_config
= false;
1652 drvname
= AudiodevDriver_str(dev
->driver
);
1653 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1654 if (!legacy_config
) {
1655 dolog("Device %s: audiodev default parameter is deprecated, please "
1656 "specify audiodev=%s\n", name
,
1657 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1659 return QTAILQ_FIRST(&audio_states
);
1661 /* legacy implicit initialization */
1662 head
= audio_handle_legacy_opts();
1664 * In case of legacy initialization, all Audiodevs in the list will have
1665 * the same configuration (except the driver), so it does't matter which
1666 * one we chose. We need an Audiodev to set up AudioState before we can
1667 * init a driver. Also note that dev at this point is still in the
1670 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1671 audio_validate_opts(dev
, &error_abort
);
1674 s
= g_malloc0(sizeof(AudioState
));
1677 QLIST_INIT (&s
->hw_head_out
);
1678 QLIST_INIT (&s
->hw_head_in
);
1679 QLIST_INIT (&s
->cap_head
);
1680 if (!atexit_registered
) {
1681 atexit(audio_cleanup
);
1682 atexit_registered
= true;
1684 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1686 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1688 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1689 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1691 if (s
->nb_hw_voices_out
<= 0) {
1692 dolog ("Bogus number of playback voices %d, setting to 1\n",
1693 s
->nb_hw_voices_out
);
1694 s
->nb_hw_voices_out
= 1;
1697 if (s
->nb_hw_voices_in
<= 0) {
1698 dolog ("Bogus number of capture voices %d, setting to 0\n",
1699 s
->nb_hw_voices_in
);
1700 s
->nb_hw_voices_in
= 0;
1704 driver
= audio_driver_lookup(drvname
);
1706 done
= !audio_driver_init(s
, driver
, true, dev
);
1708 dolog ("Unknown audio driver `%s'\n", drvname
);
1711 for (i
= 0; audio_prio_list
[i
]; i
++) {
1712 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1713 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1716 s
->dev
= dev
= e
->dev
;
1717 audio_validate_opts(dev
, &error_abort
);
1718 done
= !audio_driver_init(s
, driver
, false, dev
);
1726 audio_free_audiodev_list(&head
);
1729 driver
= audio_driver_lookup("none");
1730 done
= !audio_driver_init(s
, driver
, false, dev
);
1732 dolog("warning: Using timer based audio emulation\n");
1735 if (dev
->timer_period
<= 0) {
1736 s
->period_ticks
= 1;
1738 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1741 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1743 dolog ("warning: Could not register change state handler\n"
1744 "(Audio can continue looping even after stopping the VM)\n");
1747 QLIST_INIT (&s
->card_head
);
1748 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1752 void audio_free_audiodev_list(AudiodevListHead
*head
)
1754 AudiodevListEntry
*e
;
1755 while ((e
= QSIMPLEQ_FIRST(head
))) {
1756 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1757 qapi_free_Audiodev(e
->dev
);
1762 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1765 card
->state
= audio_init(NULL
, name
);
1768 card
->name
= g_strdup (name
);
1769 memset (&card
->entries
, 0, sizeof (card
->entries
));
1770 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1773 void AUD_remove_card (QEMUSoundCard
*card
)
1775 QLIST_REMOVE (card
, entries
);
1776 g_free (card
->name
);
1780 CaptureVoiceOut
*AUD_add_capture(
1782 struct audsettings
*as
,
1783 struct audio_capture_ops
*ops
,
1787 CaptureVoiceOut
*cap
;
1788 struct capture_callback
*cb
;
1791 if (!legacy_config
) {
1792 dolog("Capturing without setting an audiodev is deprecated\n");
1794 s
= audio_init(NULL
, NULL
);
1797 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1798 dolog("Can't capture with mixeng disabled\n");
1802 if (audio_validate_settings (as
)) {
1803 dolog ("Invalid settings were passed when trying to add capture\n");
1804 audio_print_settings (as
);
1808 cb
= g_malloc0(sizeof(*cb
));
1810 cb
->opaque
= cb_opaque
;
1812 cap
= audio_pcm_capture_find_specific(s
, as
);
1814 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1819 CaptureVoiceOut
*cap
;
1821 cap
= g_malloc0(sizeof(*cap
));
1825 QLIST_INIT (&hw
->sw_head
);
1826 QLIST_INIT (&cap
->cb_head
);
1828 /* XXX find a more elegant way */
1829 hw
->samples
= 4096 * 4;
1830 audio_pcm_hw_alloc_resources_out(hw
);
1832 audio_pcm_init_info (&hw
->info
, as
);
1834 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1836 hw
->clip
= mixeng_clip
1837 [hw
->info
.nchannels
== 2]
1839 [hw
->info
.swap_endianness
]
1840 [audio_bits_to_index (hw
->info
.bits
)];
1842 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1843 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1845 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1846 audio_attach_capture (hw
);
1852 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1854 struct capture_callback
*cb
;
1856 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1857 if (cb
->opaque
== cb_opaque
) {
1858 cb
->ops
.destroy (cb_opaque
);
1859 QLIST_REMOVE (cb
, entries
);
1862 if (!cap
->cb_head
.lh_first
) {
1863 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1866 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1867 #ifdef DEBUG_CAPTURE
1868 dolog ("freeing %s\n", sw
->name
);
1871 sw1
= sw
->entries
.le_next
;
1873 st_rate_stop (sw
->rate
);
1876 QLIST_REMOVE (sw
, entries
);
1877 QLIST_REMOVE (sc
, entries
);
1881 QLIST_REMOVE (cap
, entries
);
1882 g_free (cap
->hw
.mix_buf
);
1891 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1893 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1894 audio_set_volume_out(sw
, &vol
);
1897 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1900 HWVoiceOut
*hw
= sw
->hw
;
1902 sw
->vol
.mute
= vol
->mute
;
1903 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1904 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1907 if (hw
->pcm_ops
->volume_out
) {
1908 hw
->pcm_ops
->volume_out(hw
, vol
);
1913 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1915 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1916 audio_set_volume_in(sw
, &vol
);
1919 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1922 HWVoiceIn
*hw
= sw
->hw
;
1924 sw
->vol
.mute
= vol
->mute
;
1925 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1926 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1929 if (hw
->pcm_ops
->volume_in
) {
1930 hw
->pcm_ops
->volume_in(hw
, vol
);
1935 void audio_create_pdos(Audiodev
*dev
)
1937 switch (dev
->driver
) {
1938 #define CASE(DRIVER, driver, pdo_name) \
1939 case AUDIODEV_DRIVER_##DRIVER: \
1940 if (!dev->u.driver.has_in) { \
1941 dev->u.driver.in = g_malloc0( \
1942 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1943 dev->u.driver.has_in = true; \
1945 if (!dev->u.driver.has_out) { \
1946 dev->u.driver.out = g_malloc0( \
1947 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1948 dev->u.driver.has_out = true; \
1953 CASE(ALSA
, alsa
, Alsa
);
1954 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1955 CASE(DSOUND
, dsound
, );
1956 CASE(OSS
, oss
, Oss
);
1959 CASE(SPICE
, spice
, );
1962 case AUDIODEV_DRIVER__MAX
:
1967 static void audio_validate_per_direction_opts(
1968 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1970 if (!pdo
->has_mixing_engine
) {
1971 pdo
->has_mixing_engine
= true;
1972 pdo
->mixing_engine
= true;
1974 if (!pdo
->has_fixed_settings
) {
1975 pdo
->has_fixed_settings
= true;
1976 pdo
->fixed_settings
= pdo
->mixing_engine
;
1978 if (!pdo
->fixed_settings
&&
1979 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1981 "You can't use frequency, channels or format with fixed-settings=off");
1984 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
1985 error_setg(errp
, "You can't use fixed-settings without mixeng");
1989 if (!pdo
->has_frequency
) {
1990 pdo
->has_frequency
= true;
1991 pdo
->frequency
= 44100;
1993 if (!pdo
->has_channels
) {
1994 pdo
->has_channels
= true;
1997 if (!pdo
->has_voices
) {
1998 pdo
->has_voices
= true;
1999 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2001 if (!pdo
->has_format
) {
2002 pdo
->has_format
= true;
2003 pdo
->format
= AUDIO_FORMAT_S16
;
2007 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2011 audio_create_pdos(dev
);
2013 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2015 error_propagate(errp
, err
);
2019 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2021 error_propagate(errp
, err
);
2025 if (!dev
->has_timer_period
) {
2026 dev
->has_timer_period
= true;
2027 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2031 void audio_parse_option(const char *opt
)
2033 AudiodevListEntry
*e
;
2034 Audiodev
*dev
= NULL
;
2036 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2037 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2040 audio_validate_opts(dev
, &error_fatal
);
2042 e
= g_malloc0(sizeof(AudiodevListEntry
));
2044 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2047 void audio_init_audiodevs(void)
2049 AudiodevListEntry
*e
;
2051 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2052 audio_init(e
->dev
, NULL
);
2056 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2058 return (audsettings
) {
2059 .freq
= pdo
->frequency
,
2060 .nchannels
= pdo
->channels
,
2062 .endianness
= AUDIO_HOST_ENDIANNESS
,
2066 int audioformat_bytes_per_sample(AudioFormat fmt
)
2069 case AUDIO_FORMAT_U8
:
2070 case AUDIO_FORMAT_S8
:
2073 case AUDIO_FORMAT_U16
:
2074 case AUDIO_FORMAT_S16
:
2077 case AUDIO_FORMAT_U32
:
2078 case AUDIO_FORMAT_S32
:
2081 case AUDIO_FORMAT__MAX
:
2088 /* frames = freq * usec / 1e6 */
2089 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2090 audsettings
*as
, int def_usecs
)
2092 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2093 return (as
->freq
* usecs
+ 500000) / 1000000;
2096 /* samples = channels * frames = channels * freq * usec / 1e6 */
2097 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2098 audsettings
*as
, int def_usecs
)
2100 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2104 * bytes = bytes_per_sample * samples =
2105 * bytes_per_sample * channels * freq * usec / 1e6
2107 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2108 audsettings
*as
, int def_usecs
)
2110 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2111 audioformat_bytes_per_sample(as
->fmt
);
2114 AudioState
*audio_state_by_name(const char *name
)
2117 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2119 if (strcmp(name
, s
->dev
->id
) == 0) {
2126 const char *audio_get_id(QEMUSoundCard
*card
)
2129 assert(card
->state
->dev
);
2130 return card
->state
->dev
->id
;
2136 void audio_rate_start(RateCtl
*rate
)
2138 memset(rate
, 0, sizeof(RateCtl
));
2139 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2142 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2151 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2152 ticks
= now
- rate
->start_ticks
;
2153 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2154 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2155 if (samples
< 0 || samples
> 65536) {
2156 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2157 audio_rate_start(rate
);
2161 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2162 rate
->bytes_sent
+= ret
;