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");
221 case AUDIO_FORMAT_F32
:
222 AUD_log (NULL
, "F32");
225 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
229 AUD_log (NULL
, " endianness=");
230 switch (as
->endianness
) {
232 AUD_log (NULL
, "little");
235 AUD_log (NULL
, "big");
238 AUD_log (NULL
, "invalid");
241 AUD_log (NULL
, "\n");
244 static int audio_validate_settings (struct audsettings
*as
)
248 invalid
= as
->nchannels
< 1;
249 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
252 case AUDIO_FORMAT_S8
:
253 case AUDIO_FORMAT_U8
:
254 case AUDIO_FORMAT_S16
:
255 case AUDIO_FORMAT_U16
:
256 case AUDIO_FORMAT_S32
:
257 case AUDIO_FORMAT_U32
:
258 case AUDIO_FORMAT_F32
:
265 invalid
|= as
->freq
<= 0;
266 return invalid
? -1 : 0;
269 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
272 bool is_signed
= false, is_float
= false;
275 case AUDIO_FORMAT_S8
:
278 case AUDIO_FORMAT_U8
:
281 case AUDIO_FORMAT_S16
:
284 case AUDIO_FORMAT_U16
:
288 case AUDIO_FORMAT_F32
:
291 case AUDIO_FORMAT_S32
:
294 case AUDIO_FORMAT_U32
:
301 return info
->freq
== as
->freq
302 && info
->nchannels
== as
->nchannels
303 && info
->is_signed
== is_signed
304 && info
->is_float
== is_float
305 && info
->bits
== bits
306 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
309 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
312 bool is_signed
= false, is_float
= false;
315 case AUDIO_FORMAT_S8
:
318 case AUDIO_FORMAT_U8
:
322 case AUDIO_FORMAT_S16
:
325 case AUDIO_FORMAT_U16
:
330 case AUDIO_FORMAT_F32
:
333 case AUDIO_FORMAT_S32
:
336 case AUDIO_FORMAT_U32
:
345 info
->freq
= as
->freq
;
347 info
->is_signed
= is_signed
;
348 info
->is_float
= is_float
;
349 info
->nchannels
= as
->nchannels
;
350 info
->bytes_per_frame
= as
->nchannels
* mul
;
351 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
352 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
355 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
361 if (info
->is_signed
|| info
->is_float
) {
362 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
365 switch (info
->bits
) {
367 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
376 if (info
->swap_endianness
) {
380 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
390 int32_t s
= INT32_MAX
;
392 if (info
->swap_endianness
) {
396 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
403 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
413 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
420 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
421 struct audsettings
*as
)
423 CaptureVoiceOut
*cap
;
425 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
426 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
433 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
435 struct capture_callback
*cb
;
438 dolog ("notification %d sent\n", cmd
);
440 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
441 cb
->ops
.notify (cb
->opaque
, cmd
);
445 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
447 if (cap
->hw
.enabled
!= enabled
) {
448 audcnotification_e cmd
;
449 cap
->hw
.enabled
= enabled
;
450 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
451 audio_notify_capture (cap
, cmd
);
455 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
457 HWVoiceOut
*hw
= &cap
->hw
;
461 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
467 audio_capture_maybe_changed (cap
, enabled
);
470 static void audio_detach_capture (HWVoiceOut
*hw
)
472 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
475 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
476 SWVoiceOut
*sw
= &sc
->sw
;
477 CaptureVoiceOut
*cap
= sc
->cap
;
478 int was_active
= sw
->active
;
481 st_rate_stop (sw
->rate
);
485 QLIST_REMOVE (sw
, entries
);
486 QLIST_REMOVE (sc
, entries
);
489 /* We have removed soft voice from the capture:
490 this might have changed the overall status of the capture
491 since this might have been the only active voice */
492 audio_recalc_and_notify_capture (cap
);
498 static int audio_attach_capture (HWVoiceOut
*hw
)
500 AudioState
*s
= hw
->s
;
501 CaptureVoiceOut
*cap
;
503 audio_detach_capture (hw
);
504 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
507 HWVoiceOut
*hw_cap
= &cap
->hw
;
509 sc
= g_malloc0(sizeof(*sc
));
516 sw
->active
= hw
->enabled
;
517 sw
->conv
= noop_conv
;
518 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
519 sw
->vol
= nominal_volume
;
520 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
522 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
526 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
527 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
529 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
530 hw
, sw
->info
.freq
, sw
->info
.bits
,
532 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
535 audio_capture_maybe_changed (cap
, 1);
542 * Hard voice (capture)
544 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
547 size_t m
= hw
->total_samples_captured
;
549 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
551 m
= MIN (m
, sw
->total_hw_samples_acquired
);
557 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
559 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
560 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
561 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
567 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
570 size_t pos
= hw
->mix_buf
->pos
;
573 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
574 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
575 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
576 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
578 hw
->clip(dst
, src
, samples_to_clip
);
580 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
581 len
-= samples_to_clip
;
582 clipped
+= samples_to_clip
;
587 * Soft voice (capture)
589 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
591 HWVoiceIn
*hw
= sw
->hw
;
592 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
595 if (audio_bug(__func__
, live
< 0 || live
> hw
->conv_buf
->size
)) {
596 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
600 rpos
= hw
->conv_buf
->pos
- live
;
605 return hw
->conv_buf
->size
+ rpos
;
609 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
611 HWVoiceIn
*hw
= sw
->hw
;
612 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
613 struct st_sample
*src
, *dst
= sw
->buf
;
615 rpos
= audio_pcm_sw_get_rpos_in(sw
) % hw
->conv_buf
->size
;
617 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
618 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
619 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
623 samples
= size
/ sw
->info
.bytes_per_frame
;
628 swlim
= (live
* sw
->ratio
) >> 32;
629 swlim
= MIN (swlim
, samples
);
632 src
= hw
->conv_buf
->samples
+ rpos
;
633 if (hw
->conv_buf
->pos
> rpos
) {
634 isamp
= hw
->conv_buf
->pos
- rpos
;
636 isamp
= hw
->conv_buf
->size
- rpos
;
644 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
646 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
652 if (hw
->pcm_ops
&& !hw
->pcm_ops
->volume_in
) {
653 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
656 sw
->clip (buf
, sw
->buf
, ret
);
657 sw
->total_hw_samples_acquired
+= total
;
658 return ret
* sw
->info
.bytes_per_frame
;
662 * Hard voice (playback)
664 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
670 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
671 if (sw
->active
|| !sw
->empty
) {
672 m
= MIN (m
, sw
->total_hw_samples_mixed
);
681 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
686 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
694 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
695 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
704 * Soft voice (playback)
706 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
708 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
709 size_t ret
= 0, pos
= 0, total
= 0;
715 hwsamples
= sw
->hw
->mix_buf
->size
;
717 live
= sw
->total_hw_samples_mixed
;
718 if (audio_bug(__func__
, live
> hwsamples
)) {
719 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
723 if (live
== hwsamples
) {
725 dolog ("%s is full %d\n", sw
->name
, live
);
730 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
731 samples
= size
/ sw
->info
.bytes_per_frame
;
733 dead
= hwsamples
- live
;
734 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
735 swlim
= MIN (swlim
, samples
);
737 sw
->conv (sw
->buf
, buf
, swlim
);
739 if (sw
->hw
->pcm_ops
&& !sw
->hw
->pcm_ops
->volume_out
) {
740 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
745 dead
= hwsamples
- live
;
746 left
= hwsamples
- wpos
;
747 blck
= MIN (dead
, left
);
756 sw
->hw
->mix_buf
->samples
+ wpos
,
764 wpos
= (wpos
+ osamp
) % hwsamples
;
768 sw
->total_hw_samples_mixed
+= total
;
769 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
773 "%s: write size %zu ret %zu total sw %zu\n",
775 size
/ sw
->info
.bytes_per_frame
,
777 sw
->total_hw_samples_mixed
781 return ret
* sw
->info
.bytes_per_frame
;
785 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
787 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
788 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
794 #include "audio_template.h"
796 #include "audio_template.h"
801 static int audio_is_timer_needed(AudioState
*s
)
803 HWVoiceIn
*hwi
= NULL
;
804 HWVoiceOut
*hwo
= NULL
;
806 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
807 if (!hwo
->poll_mode
) return 1;
809 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
810 if (!hwi
->poll_mode
) return 1;
815 static void audio_reset_timer (AudioState
*s
)
817 if (audio_is_timer_needed(s
)) {
818 timer_mod_anticipate_ns(s
->ts
,
819 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
820 if (!s
->timer_running
) {
821 s
->timer_running
= true;
822 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
823 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
827 if (s
->timer_running
) {
828 s
->timer_running
= false;
829 trace_audio_timer_stop();
834 static void audio_timer (void *opaque
)
837 AudioState
*s
= opaque
;
839 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
840 diff
= now
- s
->timer_last
;
841 if (diff
> s
->period_ticks
* 3 / 2) {
842 trace_audio_timer_delayed(diff
/ SCALE_MS
);
846 audio_run(s
, "timer");
847 audio_reset_timer(s
);
853 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
858 /* XXX: Consider options */
864 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
868 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
869 return audio_pcm_sw_write(sw
, buf
, size
);
871 return hw
->pcm_ops
->write(hw
, buf
, size
);
875 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
880 /* XXX: Consider options */
886 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
890 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
891 return audio_pcm_sw_read(sw
, buf
, size
);
893 return hw
->pcm_ops
->read(hw
, buf
, size
);
897 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
899 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
902 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
911 if (sw
->active
!= on
) {
912 AudioState
*s
= sw
->s
;
917 hw
->pending_disable
= 0;
921 if (hw
->pcm_ops
->enable_out
) {
922 hw
->pcm_ops
->enable_out(hw
, true);
924 audio_reset_timer (s
);
932 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
933 temp_sw
= temp_sw
->entries
.le_next
) {
934 nb_active
+= temp_sw
->active
!= 0;
937 hw
->pending_disable
= nb_active
== 1;
941 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
942 sc
->sw
.active
= hw
->enabled
;
944 audio_capture_maybe_changed (sc
->cap
, 1);
951 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
960 if (sw
->active
!= on
) {
961 AudioState
*s
= sw
->s
;
968 if (hw
->pcm_ops
->enable_in
) {
969 hw
->pcm_ops
->enable_in(hw
, true);
971 audio_reset_timer (s
);
974 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
980 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
981 temp_sw
= temp_sw
->entries
.le_next
) {
982 nb_active
+= temp_sw
->active
!= 0;
985 if (nb_active
== 1) {
987 if (hw
->pcm_ops
->enable_in
) {
988 hw
->pcm_ops
->enable_in(hw
, false);
997 static size_t audio_get_avail (SWVoiceIn
*sw
)
1005 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1006 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
1007 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
1008 sw
->hw
->conv_buf
->size
);
1013 "%s: get_avail live %d ret %" PRId64
"\n",
1015 live
, (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
1018 return (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1021 static size_t audio_get_free(SWVoiceOut
*sw
)
1029 live
= sw
->total_hw_samples_mixed
;
1031 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1032 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1033 sw
->hw
->mix_buf
->size
);
1037 dead
= sw
->hw
->mix_buf
->size
- live
;
1040 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1042 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) *
1043 sw
->info
.bytes_per_frame
);
1046 return (((int64_t) dead
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1049 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1057 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1058 SWVoiceOut
*sw
= &sc
->sw
;
1063 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1064 size_t to_write
= MIN(till_end_of_hw
, n
);
1065 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1068 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1069 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1070 if (written
- bytes
) {
1071 dolog("Could not mix %zu bytes into a capture "
1072 "buffer, mixed %zu\n",
1077 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1082 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1083 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1084 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1087 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1092 size_t size
, decr
, proc
;
1093 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1094 if (!buf
|| size
== 0) {
1098 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1099 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1100 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1101 decr
* hw
->info
.bytes_per_frame
) /
1102 hw
->info
.bytes_per_frame
;
1106 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1108 if (proc
== 0 || proc
< decr
) {
1113 if (hw
->pcm_ops
->run_buffer_out
) {
1114 hw
->pcm_ops
->run_buffer_out(hw
);
1120 static void audio_run_out (AudioState
*s
)
1122 HWVoiceOut
*hw
= NULL
;
1125 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1126 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1127 /* there is exactly 1 sw for each hw with no mixeng */
1128 sw
= hw
->sw_head
.lh_first
;
1130 if (hw
->pending_disable
) {
1132 hw
->pending_disable
= 0;
1133 if (hw
->pcm_ops
->enable_out
) {
1134 hw
->pcm_ops
->enable_out(hw
, false);
1139 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1145 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1146 size_t played
, live
, prev_rpos
, free
;
1147 int nb_live
, cleanup_required
;
1149 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1154 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1155 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1159 if (hw
->pending_disable
&& !nb_live
) {
1162 dolog ("Disabling voice\n");
1165 hw
->pending_disable
= 0;
1166 if (hw
->pcm_ops
->enable_out
) {
1167 hw
->pcm_ops
->enable_out(hw
, false);
1169 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1171 audio_recalc_and_notify_capture (sc
->cap
);
1177 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1179 free
= audio_get_free (sw
);
1181 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1188 prev_rpos
= hw
->mix_buf
->pos
;
1189 played
= audio_pcm_hw_run_out(hw
, live
);
1190 replay_audio_out(&played
);
1191 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1192 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1193 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1194 hw
->mix_buf
->pos
= 0;
1198 dolog("played=%zu\n", played
);
1202 hw
->ts_helper
+= played
;
1203 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1206 cleanup_required
= 0;
1207 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1208 if (!sw
->active
&& sw
->empty
) {
1212 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1213 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1214 played
, sw
->total_hw_samples_mixed
);
1215 played
= sw
->total_hw_samples_mixed
;
1218 sw
->total_hw_samples_mixed
-= played
;
1220 if (!sw
->total_hw_samples_mixed
) {
1222 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1226 free
= audio_get_free (sw
);
1228 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1233 if (cleanup_required
) {
1236 sw
= hw
->sw_head
.lh_first
;
1238 sw1
= sw
->entries
.le_next
;
1239 if (!sw
->active
&& !sw
->callback
.fn
) {
1240 audio_close_out (sw
);
1248 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1251 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
1255 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1256 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1258 assert(size
% hw
->info
.bytes_per_frame
== 0);
1260 hw
->pcm_ops
->put_buffer_in(hw
, buf
, size
);
1264 proc
= MIN(size
/ hw
->info
.bytes_per_frame
,
1265 conv_buf
->size
- conv_buf
->pos
);
1267 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, buf
, proc
);
1268 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
1272 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1278 static void audio_run_in (AudioState
*s
)
1280 HWVoiceIn
*hw
= NULL
;
1282 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1283 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1284 /* there is exactly 1 sw for each hw with no mixeng */
1285 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1287 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1293 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1295 size_t captured
= 0, min
;
1297 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1298 captured
= audio_pcm_hw_run_in(
1299 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1301 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1302 hw
->conv_buf
->size
);
1304 min
= audio_pcm_hw_find_min_in (hw
);
1305 hw
->total_samples_captured
+= captured
- min
;
1306 hw
->ts_helper
+= captured
;
1308 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1309 sw
->total_hw_samples_acquired
-= min
;
1314 avail
= audio_get_avail (sw
);
1316 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1323 static void audio_run_capture (AudioState
*s
)
1325 CaptureVoiceOut
*cap
;
1327 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1328 size_t live
, rpos
, captured
;
1329 HWVoiceOut
*hw
= &cap
->hw
;
1332 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1333 rpos
= hw
->mix_buf
->pos
;
1335 size_t left
= hw
->mix_buf
->size
- rpos
;
1336 size_t to_capture
= MIN(live
, left
);
1337 struct st_sample
*src
;
1338 struct capture_callback
*cb
;
1340 src
= hw
->mix_buf
->samples
+ rpos
;
1341 hw
->clip (cap
->buf
, src
, to_capture
);
1342 mixeng_clear (src
, to_capture
);
1344 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1345 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1346 to_capture
* hw
->info
.bytes_per_frame
);
1348 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1351 hw
->mix_buf
->pos
= rpos
;
1353 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1354 if (!sw
->active
&& sw
->empty
) {
1358 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1359 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1360 captured
, sw
->total_hw_samples_mixed
);
1361 captured
= sw
->total_hw_samples_mixed
;
1364 sw
->total_hw_samples_mixed
-= captured
;
1365 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1370 void audio_run(AudioState
*s
, const char *msg
)
1374 audio_run_capture(s
);
1378 static double prevtime
;
1382 if (gettimeofday (&tv
, NULL
)) {
1383 perror ("audio_run: gettimeofday");
1387 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1388 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1389 prevtime
= currtime
;
1394 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1398 if (unlikely(!hw
->buf_emul
)) {
1399 size_t calc_size
= hw
->conv_buf
->size
* hw
->info
.bytes_per_frame
;
1400 hw
->buf_emul
= g_malloc(calc_size
);
1401 hw
->size_emul
= calc_size
;
1402 hw
->pos_emul
= hw
->pending_emul
= 0;
1405 while (hw
->pending_emul
< hw
->size_emul
) {
1406 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1407 hw
->size_emul
- hw
->pending_emul
);
1408 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1410 hw
->pending_emul
+= read
;
1411 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1412 if (read
< read_len
) {
1417 start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1419 start
+= hw
->size_emul
;
1421 assert(start
>= 0 && start
< hw
->size_emul
);
1423 *size
= MIN(*size
, hw
->pending_emul
);
1424 *size
= MIN(*size
, hw
->size_emul
- start
);
1425 return hw
->buf_emul
+ start
;
1428 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1430 assert(size
<= hw
->pending_emul
);
1431 hw
->pending_emul
-= size
;
1434 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1436 while (hw
->pending_emul
) {
1437 size_t write_len
, written
;
1438 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1441 start
+= hw
->size_emul
;
1443 assert(start
>= 0 && start
< hw
->size_emul
);
1445 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1447 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1448 hw
->pending_emul
-= written
;
1450 if (written
< write_len
) {
1456 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1458 if (unlikely(!hw
->buf_emul
)) {
1459 size_t calc_size
= hw
->mix_buf
->size
* hw
->info
.bytes_per_frame
;
1461 hw
->buf_emul
= g_malloc(calc_size
);
1462 hw
->size_emul
= calc_size
;
1463 hw
->pos_emul
= hw
->pending_emul
= 0;
1466 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1467 hw
->size_emul
- hw
->pos_emul
);
1468 return hw
->buf_emul
+ hw
->pos_emul
;
1471 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1473 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1474 size
+ hw
->pending_emul
<= hw
->size_emul
);
1476 hw
->pending_emul
+= size
;
1477 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1482 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1484 size_t dst_size
, copy_size
;
1485 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1486 copy_size
= MIN(size
, dst_size
);
1488 memcpy(dst
, buf
, copy_size
);
1489 return hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1492 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1494 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1496 memcpy(buf
, src
, size
);
1497 hw
->pcm_ops
->put_buffer_in(hw
, src
, size
);
1502 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1503 bool msg
, Audiodev
*dev
)
1505 s
->drv_opaque
= drv
->init(dev
);
1507 if (s
->drv_opaque
) {
1508 if (!drv
->pcm_ops
->get_buffer_in
) {
1509 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1510 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1512 if (!drv
->pcm_ops
->get_buffer_out
) {
1513 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1514 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1517 audio_init_nb_voices_out(s
, drv
);
1518 audio_init_nb_voices_in(s
, drv
);
1524 dolog("Could not init `%s' audio driver\n", drv
->name
);
1530 static void audio_vm_change_state_handler (void *opaque
, int running
,
1533 AudioState
*s
= opaque
;
1534 HWVoiceOut
*hwo
= NULL
;
1535 HWVoiceIn
*hwi
= NULL
;
1537 s
->vm_running
= running
;
1538 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1539 if (hwo
->pcm_ops
->enable_out
) {
1540 hwo
->pcm_ops
->enable_out(hwo
, running
);
1544 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1545 if (hwi
->pcm_ops
->enable_in
) {
1546 hwi
->pcm_ops
->enable_in(hwi
, running
);
1549 audio_reset_timer (s
);
1552 static bool is_cleaning_up
;
1554 bool audio_is_cleaning_up(void)
1556 return is_cleaning_up
;
1559 static void free_audio_state(AudioState
*s
)
1561 HWVoiceOut
*hwo
, *hwon
;
1562 HWVoiceIn
*hwi
, *hwin
;
1564 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1567 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1568 hwo
->pcm_ops
->enable_out(hwo
, false);
1570 hwo
->pcm_ops
->fini_out (hwo
);
1572 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1573 CaptureVoiceOut
*cap
= sc
->cap
;
1574 struct capture_callback
*cb
;
1576 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1577 cb
->ops
.destroy (cb
->opaque
);
1580 QLIST_REMOVE(hwo
, entries
);
1583 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1584 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1585 hwi
->pcm_ops
->enable_in(hwi
, false);
1587 hwi
->pcm_ops
->fini_in (hwi
);
1588 QLIST_REMOVE(hwi
, entries
);
1592 s
->drv
->fini (s
->drv_opaque
);
1597 qapi_free_Audiodev(s
->dev
);
1609 void audio_cleanup(void)
1611 is_cleaning_up
= true;
1612 while (!QTAILQ_EMPTY(&audio_states
)) {
1613 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1614 QTAILQ_REMOVE(&audio_states
, s
, list
);
1615 free_audio_state(s
);
1619 static const VMStateDescription vmstate_audio
= {
1622 .minimum_version_id
= 1,
1623 .fields
= (VMStateField
[]) {
1624 VMSTATE_END_OF_LIST()
1628 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1630 static AudiodevListEntry
*audiodev_find(
1631 AudiodevListHead
*head
, const char *drvname
)
1633 AudiodevListEntry
*e
;
1634 QSIMPLEQ_FOREACH(e
, head
, next
) {
1635 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1644 * if we have dev, this function was called because of an -audiodev argument =>
1645 * initialize a new state with it
1646 * if dev == NULL => legacy implicit initialization, return the already created
1647 * state or create a new one
1649 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1651 static bool atexit_registered
;
1654 const char *drvname
= NULL
;
1655 VMChangeStateEntry
*e
;
1657 struct audio_driver
*driver
;
1658 /* silence gcc warning about uninitialized variable */
1659 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1662 /* -audiodev option */
1663 legacy_config
= false;
1664 drvname
= AudiodevDriver_str(dev
->driver
);
1665 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1666 if (!legacy_config
) {
1667 dolog("Device %s: audiodev default parameter is deprecated, please "
1668 "specify audiodev=%s\n", name
,
1669 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1671 return QTAILQ_FIRST(&audio_states
);
1673 /* legacy implicit initialization */
1674 head
= audio_handle_legacy_opts();
1676 * In case of legacy initialization, all Audiodevs in the list will have
1677 * the same configuration (except the driver), so it does't matter which
1678 * one we chose. We need an Audiodev to set up AudioState before we can
1679 * init a driver. Also note that dev at this point is still in the
1682 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1683 audio_validate_opts(dev
, &error_abort
);
1686 s
= g_malloc0(sizeof(AudioState
));
1689 QLIST_INIT (&s
->hw_head_out
);
1690 QLIST_INIT (&s
->hw_head_in
);
1691 QLIST_INIT (&s
->cap_head
);
1692 if (!atexit_registered
) {
1693 atexit(audio_cleanup
);
1694 atexit_registered
= true;
1696 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1698 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1700 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1701 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1703 if (s
->nb_hw_voices_out
<= 0) {
1704 dolog ("Bogus number of playback voices %d, setting to 1\n",
1705 s
->nb_hw_voices_out
);
1706 s
->nb_hw_voices_out
= 1;
1709 if (s
->nb_hw_voices_in
<= 0) {
1710 dolog ("Bogus number of capture voices %d, setting to 0\n",
1711 s
->nb_hw_voices_in
);
1712 s
->nb_hw_voices_in
= 0;
1716 driver
= audio_driver_lookup(drvname
);
1718 done
= !audio_driver_init(s
, driver
, true, dev
);
1720 dolog ("Unknown audio driver `%s'\n", drvname
);
1723 for (i
= 0; audio_prio_list
[i
]; i
++) {
1724 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1725 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1728 s
->dev
= dev
= e
->dev
;
1729 audio_validate_opts(dev
, &error_abort
);
1730 done
= !audio_driver_init(s
, driver
, false, dev
);
1738 audio_free_audiodev_list(&head
);
1741 driver
= audio_driver_lookup("none");
1742 done
= !audio_driver_init(s
, driver
, false, dev
);
1744 dolog("warning: Using timer based audio emulation\n");
1747 if (dev
->timer_period
<= 0) {
1748 s
->period_ticks
= 1;
1750 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1753 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1755 dolog ("warning: Could not register change state handler\n"
1756 "(Audio can continue looping even after stopping the VM)\n");
1759 QLIST_INIT (&s
->card_head
);
1760 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1764 void audio_free_audiodev_list(AudiodevListHead
*head
)
1766 AudiodevListEntry
*e
;
1767 while ((e
= QSIMPLEQ_FIRST(head
))) {
1768 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1769 qapi_free_Audiodev(e
->dev
);
1774 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1777 card
->state
= audio_init(NULL
, name
);
1780 card
->name
= g_strdup (name
);
1781 memset (&card
->entries
, 0, sizeof (card
->entries
));
1782 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1785 void AUD_remove_card (QEMUSoundCard
*card
)
1787 QLIST_REMOVE (card
, entries
);
1788 g_free (card
->name
);
1792 CaptureVoiceOut
*AUD_add_capture(
1794 struct audsettings
*as
,
1795 struct audio_capture_ops
*ops
,
1799 CaptureVoiceOut
*cap
;
1800 struct capture_callback
*cb
;
1803 if (!legacy_config
) {
1804 dolog("Capturing without setting an audiodev is deprecated\n");
1806 s
= audio_init(NULL
, NULL
);
1809 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1810 dolog("Can't capture with mixeng disabled\n");
1814 if (audio_validate_settings (as
)) {
1815 dolog ("Invalid settings were passed when trying to add capture\n");
1816 audio_print_settings (as
);
1820 cb
= g_malloc0(sizeof(*cb
));
1822 cb
->opaque
= cb_opaque
;
1824 cap
= audio_pcm_capture_find_specific(s
, as
);
1826 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1831 CaptureVoiceOut
*cap
;
1833 cap
= g_malloc0(sizeof(*cap
));
1837 QLIST_INIT (&hw
->sw_head
);
1838 QLIST_INIT (&cap
->cb_head
);
1840 /* XXX find a more elegant way */
1841 hw
->samples
= 4096 * 4;
1842 audio_pcm_hw_alloc_resources_out(hw
);
1844 audio_pcm_init_info (&hw
->info
, as
);
1846 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1848 if (hw
->info
.is_float
) {
1849 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1851 hw
->clip
= mixeng_clip
1852 [hw
->info
.nchannels
== 2]
1853 [hw
->info
.is_signed
]
1854 [hw
->info
.swap_endianness
]
1855 [audio_bits_to_index(hw
->info
.bits
)];
1858 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1859 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1861 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1862 audio_attach_capture (hw
);
1868 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1870 struct capture_callback
*cb
;
1872 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1873 if (cb
->opaque
== cb_opaque
) {
1874 cb
->ops
.destroy (cb_opaque
);
1875 QLIST_REMOVE (cb
, entries
);
1878 if (!cap
->cb_head
.lh_first
) {
1879 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1882 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1883 #ifdef DEBUG_CAPTURE
1884 dolog ("freeing %s\n", sw
->name
);
1887 sw1
= sw
->entries
.le_next
;
1889 st_rate_stop (sw
->rate
);
1892 QLIST_REMOVE (sw
, entries
);
1893 QLIST_REMOVE (sc
, entries
);
1897 QLIST_REMOVE (cap
, entries
);
1898 g_free (cap
->hw
.mix_buf
);
1907 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1909 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1910 audio_set_volume_out(sw
, &vol
);
1913 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1916 HWVoiceOut
*hw
= sw
->hw
;
1918 sw
->vol
.mute
= vol
->mute
;
1919 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1920 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1923 if (hw
->pcm_ops
->volume_out
) {
1924 hw
->pcm_ops
->volume_out(hw
, vol
);
1929 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1931 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1932 audio_set_volume_in(sw
, &vol
);
1935 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1938 HWVoiceIn
*hw
= sw
->hw
;
1940 sw
->vol
.mute
= vol
->mute
;
1941 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1942 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1945 if (hw
->pcm_ops
->volume_in
) {
1946 hw
->pcm_ops
->volume_in(hw
, vol
);
1951 void audio_create_pdos(Audiodev
*dev
)
1953 switch (dev
->driver
) {
1954 #define CASE(DRIVER, driver, pdo_name) \
1955 case AUDIODEV_DRIVER_##DRIVER: \
1956 if (!dev->u.driver.has_in) { \
1957 dev->u.driver.in = g_malloc0( \
1958 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1959 dev->u.driver.has_in = true; \
1961 if (!dev->u.driver.has_out) { \
1962 dev->u.driver.out = g_malloc0( \
1963 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1964 dev->u.driver.has_out = true; \
1969 CASE(ALSA
, alsa
, Alsa
);
1970 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1971 CASE(DSOUND
, dsound
, );
1972 CASE(JACK
, jack
, Jack
);
1973 CASE(OSS
, oss
, Oss
);
1976 CASE(SPICE
, spice
, );
1979 case AUDIODEV_DRIVER__MAX
:
1984 static void audio_validate_per_direction_opts(
1985 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1987 if (!pdo
->has_mixing_engine
) {
1988 pdo
->has_mixing_engine
= true;
1989 pdo
->mixing_engine
= true;
1991 if (!pdo
->has_fixed_settings
) {
1992 pdo
->has_fixed_settings
= true;
1993 pdo
->fixed_settings
= pdo
->mixing_engine
;
1995 if (!pdo
->fixed_settings
&&
1996 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1998 "You can't use frequency, channels or format with fixed-settings=off");
2001 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2002 error_setg(errp
, "You can't use fixed-settings without mixeng");
2006 if (!pdo
->has_frequency
) {
2007 pdo
->has_frequency
= true;
2008 pdo
->frequency
= 44100;
2010 if (!pdo
->has_channels
) {
2011 pdo
->has_channels
= true;
2014 if (!pdo
->has_voices
) {
2015 pdo
->has_voices
= true;
2016 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2018 if (!pdo
->has_format
) {
2019 pdo
->has_format
= true;
2020 pdo
->format
= AUDIO_FORMAT_S16
;
2024 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2028 audio_create_pdos(dev
);
2030 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2032 error_propagate(errp
, err
);
2036 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2038 error_propagate(errp
, err
);
2042 if (!dev
->has_timer_period
) {
2043 dev
->has_timer_period
= true;
2044 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2048 void audio_parse_option(const char *opt
)
2050 AudiodevListEntry
*e
;
2051 Audiodev
*dev
= NULL
;
2053 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2054 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2057 audio_validate_opts(dev
, &error_fatal
);
2059 e
= g_malloc0(sizeof(AudiodevListEntry
));
2061 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2064 void audio_init_audiodevs(void)
2066 AudiodevListEntry
*e
;
2068 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2069 audio_init(e
->dev
, NULL
);
2073 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2075 return (audsettings
) {
2076 .freq
= pdo
->frequency
,
2077 .nchannels
= pdo
->channels
,
2079 .endianness
= AUDIO_HOST_ENDIANNESS
,
2083 int audioformat_bytes_per_sample(AudioFormat fmt
)
2086 case AUDIO_FORMAT_U8
:
2087 case AUDIO_FORMAT_S8
:
2090 case AUDIO_FORMAT_U16
:
2091 case AUDIO_FORMAT_S16
:
2094 case AUDIO_FORMAT_U32
:
2095 case AUDIO_FORMAT_S32
:
2096 case AUDIO_FORMAT_F32
:
2099 case AUDIO_FORMAT__MAX
:
2106 /* frames = freq * usec / 1e6 */
2107 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2108 audsettings
*as
, int def_usecs
)
2110 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2111 return (as
->freq
* usecs
+ 500000) / 1000000;
2114 /* samples = channels * frames = channels * freq * usec / 1e6 */
2115 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2116 audsettings
*as
, int def_usecs
)
2118 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2122 * bytes = bytes_per_sample * samples =
2123 * bytes_per_sample * channels * freq * usec / 1e6
2125 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2126 audsettings
*as
, int def_usecs
)
2128 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2129 audioformat_bytes_per_sample(as
->fmt
);
2132 AudioState
*audio_state_by_name(const char *name
)
2135 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2137 if (strcmp(name
, s
->dev
->id
) == 0) {
2144 const char *audio_get_id(QEMUSoundCard
*card
)
2147 assert(card
->state
->dev
);
2148 return card
->state
->dev
->id
;
2154 void audio_rate_start(RateCtl
*rate
)
2156 memset(rate
, 0, sizeof(RateCtl
));
2157 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2160 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2169 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2170 ticks
= now
- rate
->start_ticks
;
2171 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2172 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2173 if (samples
< 0 || samples
> 65536) {
2174 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2175 audio_rate_start(rate
);
2179 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2180 rate
->bytes_sent
+= ret
;