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
:
307 case AUDIO_FORMAT_U8
:
311 case AUDIO_FORMAT_S16
:
314 case AUDIO_FORMAT_U16
:
319 case AUDIO_FORMAT_S32
:
322 case AUDIO_FORMAT_U32
:
331 info
->freq
= as
->freq
;
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
)
347 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
350 switch (info
->bits
) {
352 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
361 if (info
->swap_endianness
) {
365 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
375 int32_t s
= INT32_MAX
;
377 if (info
->swap_endianness
) {
381 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
388 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
398 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
405 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
406 struct audsettings
*as
)
408 CaptureVoiceOut
*cap
;
410 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
411 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
418 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
420 struct capture_callback
*cb
;
423 dolog ("notification %d sent\n", cmd
);
425 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
426 cb
->ops
.notify (cb
->opaque
, cmd
);
430 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
432 if (cap
->hw
.enabled
!= enabled
) {
433 audcnotification_e cmd
;
434 cap
->hw
.enabled
= enabled
;
435 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
436 audio_notify_capture (cap
, cmd
);
440 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
442 HWVoiceOut
*hw
= &cap
->hw
;
446 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
452 audio_capture_maybe_changed (cap
, enabled
);
455 static void audio_detach_capture (HWVoiceOut
*hw
)
457 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
460 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
461 SWVoiceOut
*sw
= &sc
->sw
;
462 CaptureVoiceOut
*cap
= sc
->cap
;
463 int was_active
= sw
->active
;
466 st_rate_stop (sw
->rate
);
470 QLIST_REMOVE (sw
, entries
);
471 QLIST_REMOVE (sc
, entries
);
474 /* We have removed soft voice from the capture:
475 this might have changed the overall status of the capture
476 since this might have been the only active voice */
477 audio_recalc_and_notify_capture (cap
);
483 static int audio_attach_capture (HWVoiceOut
*hw
)
485 AudioState
*s
= hw
->s
;
486 CaptureVoiceOut
*cap
;
488 audio_detach_capture (hw
);
489 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
492 HWVoiceOut
*hw_cap
= &cap
->hw
;
494 sc
= g_malloc0(sizeof(*sc
));
501 sw
->active
= hw
->enabled
;
502 sw
->conv
= noop_conv
;
503 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
504 sw
->vol
= nominal_volume
;
505 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
507 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
511 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
512 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
514 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
515 hw
, sw
->info
.freq
, sw
->info
.bits
,
517 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
520 audio_capture_maybe_changed (cap
, 1);
527 * Hard voice (capture)
529 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
532 size_t m
= hw
->total_samples_captured
;
534 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
536 m
= MIN (m
, sw
->total_hw_samples_acquired
);
542 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
544 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
545 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
546 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
552 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
555 size_t pos
= hw
->mix_buf
->pos
;
558 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
559 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
560 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
561 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
563 hw
->clip(dst
, src
, samples_to_clip
);
565 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
566 len
-= samples_to_clip
;
567 clipped
+= samples_to_clip
;
572 * Soft voice (capture)
574 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
576 HWVoiceIn
*hw
= sw
->hw
;
577 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
580 if (audio_bug(__func__
, live
< 0 || live
> hw
->conv_buf
->size
)) {
581 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
585 rpos
= hw
->conv_buf
->pos
- live
;
590 return hw
->conv_buf
->size
+ rpos
;
594 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
596 HWVoiceIn
*hw
= sw
->hw
;
597 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
598 struct st_sample
*src
, *dst
= sw
->buf
;
600 rpos
= audio_pcm_sw_get_rpos_in(sw
) % hw
->conv_buf
->size
;
602 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
603 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
604 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
608 samples
= size
/ sw
->info
.bytes_per_frame
;
613 swlim
= (live
* sw
->ratio
) >> 32;
614 swlim
= MIN (swlim
, samples
);
617 src
= hw
->conv_buf
->samples
+ rpos
;
618 if (hw
->conv_buf
->pos
> rpos
) {
619 isamp
= hw
->conv_buf
->pos
- rpos
;
621 isamp
= hw
->conv_buf
->size
- rpos
;
629 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
631 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
637 if (!hw
->pcm_ops
->volume_in
) {
638 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
641 sw
->clip (buf
, sw
->buf
, ret
);
642 sw
->total_hw_samples_acquired
+= total
;
643 return ret
* sw
->info
.bytes_per_frame
;
647 * Hard voice (playback)
649 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
655 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
656 if (sw
->active
|| !sw
->empty
) {
657 m
= MIN (m
, sw
->total_hw_samples_mixed
);
666 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
671 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
679 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
680 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
689 * Soft voice (playback)
691 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
693 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
694 size_t ret
= 0, pos
= 0, total
= 0;
700 hwsamples
= sw
->hw
->mix_buf
->size
;
702 live
= sw
->total_hw_samples_mixed
;
703 if (audio_bug(__func__
, live
> hwsamples
)) {
704 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
708 if (live
== hwsamples
) {
710 dolog ("%s is full %d\n", sw
->name
, live
);
715 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
716 samples
= size
/ sw
->info
.bytes_per_frame
;
718 dead
= hwsamples
- live
;
719 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
720 swlim
= MIN (swlim
, samples
);
722 sw
->conv (sw
->buf
, buf
, swlim
);
724 if (!sw
->hw
->pcm_ops
->volume_out
) {
725 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
730 dead
= hwsamples
- live
;
731 left
= hwsamples
- wpos
;
732 blck
= MIN (dead
, left
);
741 sw
->hw
->mix_buf
->samples
+ wpos
,
749 wpos
= (wpos
+ osamp
) % hwsamples
;
753 sw
->total_hw_samples_mixed
+= total
;
754 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
758 "%s: write size %zu ret %zu total sw %zu\n",
760 size
/ sw
->info
.bytes_per_frame
,
762 sw
->total_hw_samples_mixed
766 return ret
* sw
->info
.bytes_per_frame
;
770 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
772 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
773 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
778 #include "audio_template.h"
780 #include "audio_template.h"
785 static int audio_is_timer_needed(AudioState
*s
)
787 HWVoiceIn
*hwi
= NULL
;
788 HWVoiceOut
*hwo
= NULL
;
790 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
791 if (!hwo
->poll_mode
) return 1;
793 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
794 if (!hwi
->poll_mode
) return 1;
799 static void audio_reset_timer (AudioState
*s
)
801 if (audio_is_timer_needed(s
)) {
802 timer_mod_anticipate_ns(s
->ts
,
803 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
804 if (!s
->timer_running
) {
805 s
->timer_running
= true;
806 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
807 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
811 if (s
->timer_running
) {
812 s
->timer_running
= false;
813 trace_audio_timer_stop();
818 static void audio_timer (void *opaque
)
821 AudioState
*s
= opaque
;
823 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
824 diff
= now
- s
->timer_last
;
825 if (diff
> s
->period_ticks
* 3 / 2) {
826 trace_audio_timer_delayed(diff
/ SCALE_MS
);
830 audio_run(s
, "timer");
831 audio_reset_timer(s
);
837 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
842 /* XXX: Consider options */
848 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
852 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
853 return audio_pcm_sw_write(sw
, buf
, size
);
855 return hw
->pcm_ops
->write(hw
, buf
, size
);
859 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
864 /* XXX: Consider options */
870 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
874 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
875 return audio_pcm_sw_read(sw
, buf
, size
);
877 return hw
->pcm_ops
->read(hw
, buf
, size
);
881 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
883 return sw
->hw
->mix_buf
->size
* sw
->hw
->info
.bytes_per_frame
;
886 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
895 if (sw
->active
!= on
) {
896 AudioState
*s
= sw
->s
;
901 hw
->pending_disable
= 0;
905 if (hw
->pcm_ops
->enable_out
) {
906 hw
->pcm_ops
->enable_out(hw
, true);
908 audio_reset_timer (s
);
916 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
917 temp_sw
= temp_sw
->entries
.le_next
) {
918 nb_active
+= temp_sw
->active
!= 0;
921 hw
->pending_disable
= nb_active
== 1;
925 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
926 sc
->sw
.active
= hw
->enabled
;
928 audio_capture_maybe_changed (sc
->cap
, 1);
935 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
944 if (sw
->active
!= on
) {
945 AudioState
*s
= sw
->s
;
952 if (hw
->pcm_ops
->enable_in
) {
953 hw
->pcm_ops
->enable_in(hw
, true);
955 audio_reset_timer (s
);
958 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
964 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
965 temp_sw
= temp_sw
->entries
.le_next
) {
966 nb_active
+= temp_sw
->active
!= 0;
969 if (nb_active
== 1) {
971 if (hw
->pcm_ops
->enable_in
) {
972 hw
->pcm_ops
->enable_in(hw
, false);
981 static size_t audio_get_avail (SWVoiceIn
*sw
)
989 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
990 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
991 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
992 sw
->hw
->conv_buf
->size
);
997 "%s: get_avail live %d ret %" PRId64
"\n",
999 live
, (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
1002 return (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1005 static size_t audio_get_free(SWVoiceOut
*sw
)
1013 live
= sw
->total_hw_samples_mixed
;
1015 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1016 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1017 sw
->hw
->mix_buf
->size
);
1021 dead
= sw
->hw
->mix_buf
->size
- live
;
1024 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1026 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) *
1027 sw
->info
.bytes_per_frame
);
1030 return (((int64_t) dead
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1033 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1041 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1042 SWVoiceOut
*sw
= &sc
->sw
;
1047 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1048 size_t to_write
= MIN(till_end_of_hw
, n
);
1049 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1052 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1053 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1054 if (written
- bytes
) {
1055 dolog("Could not mix %zu bytes into a capture "
1056 "buffer, mixed %zu\n",
1061 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1066 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1067 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1068 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1071 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1076 size_t size
, decr
, proc
;
1077 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1079 /* retrying will likely won't help, drop everything. */
1080 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ live
) % hw
->mix_buf
->size
;
1081 return clipped
+ live
;
1084 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1085 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1086 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1087 decr
* hw
->info
.bytes_per_frame
) /
1088 hw
->info
.bytes_per_frame
;
1092 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1094 if (proc
== 0 || proc
< decr
) {
1102 static void audio_run_out (AudioState
*s
)
1104 HWVoiceOut
*hw
= NULL
;
1107 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1108 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1109 /* there is exactly 1 sw for each hw with no mixeng */
1110 sw
= hw
->sw_head
.lh_first
;
1112 if (hw
->pending_disable
) {
1114 hw
->pending_disable
= 0;
1115 if (hw
->pcm_ops
->enable_out
) {
1116 hw
->pcm_ops
->enable_out(hw
, false);
1121 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1127 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1128 size_t played
, live
, prev_rpos
, free
;
1129 int nb_live
, cleanup_required
;
1131 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1136 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1137 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1141 if (hw
->pending_disable
&& !nb_live
) {
1144 dolog ("Disabling voice\n");
1147 hw
->pending_disable
= 0;
1148 if (hw
->pcm_ops
->enable_out
) {
1149 hw
->pcm_ops
->enable_out(hw
, false);
1151 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1153 audio_recalc_and_notify_capture (sc
->cap
);
1159 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1161 free
= audio_get_free (sw
);
1163 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1170 prev_rpos
= hw
->mix_buf
->pos
;
1171 played
= audio_pcm_hw_run_out(hw
, live
);
1172 replay_audio_out(&played
);
1173 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1174 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1175 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1176 hw
->mix_buf
->pos
= 0;
1180 dolog("played=%zu\n", played
);
1184 hw
->ts_helper
+= played
;
1185 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1188 cleanup_required
= 0;
1189 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1190 if (!sw
->active
&& sw
->empty
) {
1194 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1195 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1196 played
, sw
->total_hw_samples_mixed
);
1197 played
= sw
->total_hw_samples_mixed
;
1200 sw
->total_hw_samples_mixed
-= played
;
1202 if (!sw
->total_hw_samples_mixed
) {
1204 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1208 free
= audio_get_free (sw
);
1210 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1215 if (cleanup_required
) {
1218 sw
= hw
->sw_head
.lh_first
;
1220 sw1
= sw
->entries
.le_next
;
1221 if (!sw
->active
&& !sw
->callback
.fn
) {
1222 audio_close_out (sw
);
1230 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1233 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
1237 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1238 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1240 assert(size
% hw
->info
.bytes_per_frame
== 0);
1242 hw
->pcm_ops
->put_buffer_in(hw
, buf
, size
);
1246 proc
= MIN(size
/ hw
->info
.bytes_per_frame
,
1247 conv_buf
->size
- conv_buf
->pos
);
1249 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, buf
, proc
);
1250 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
1254 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1260 static void audio_run_in (AudioState
*s
)
1262 HWVoiceIn
*hw
= NULL
;
1264 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1265 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1266 /* there is exactly 1 sw for each hw with no mixeng */
1267 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1269 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1275 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1277 size_t captured
= 0, min
;
1279 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1280 captured
= audio_pcm_hw_run_in(
1281 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1283 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1284 hw
->conv_buf
->size
);
1286 min
= audio_pcm_hw_find_min_in (hw
);
1287 hw
->total_samples_captured
+= captured
- min
;
1288 hw
->ts_helper
+= captured
;
1290 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1291 sw
->total_hw_samples_acquired
-= min
;
1296 avail
= audio_get_avail (sw
);
1298 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1305 static void audio_run_capture (AudioState
*s
)
1307 CaptureVoiceOut
*cap
;
1309 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1310 size_t live
, rpos
, captured
;
1311 HWVoiceOut
*hw
= &cap
->hw
;
1314 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1315 rpos
= hw
->mix_buf
->pos
;
1317 size_t left
= hw
->mix_buf
->size
- rpos
;
1318 size_t to_capture
= MIN(live
, left
);
1319 struct st_sample
*src
;
1320 struct capture_callback
*cb
;
1322 src
= hw
->mix_buf
->samples
+ rpos
;
1323 hw
->clip (cap
->buf
, src
, to_capture
);
1324 mixeng_clear (src
, to_capture
);
1326 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1327 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1328 to_capture
* hw
->info
.bytes_per_frame
);
1330 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1333 hw
->mix_buf
->pos
= rpos
;
1335 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1336 if (!sw
->active
&& sw
->empty
) {
1340 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1341 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1342 captured
, sw
->total_hw_samples_mixed
);
1343 captured
= sw
->total_hw_samples_mixed
;
1346 sw
->total_hw_samples_mixed
-= captured
;
1347 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1352 void audio_run(AudioState
*s
, const char *msg
)
1356 audio_run_capture(s
);
1360 static double prevtime
;
1364 if (gettimeofday (&tv
, NULL
)) {
1365 perror ("audio_run: gettimeofday");
1369 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1370 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1371 prevtime
= currtime
;
1376 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1380 if (unlikely(!hw
->buf_emul
)) {
1381 size_t calc_size
= hw
->conv_buf
->size
* hw
->info
.bytes_per_frame
;
1382 hw
->buf_emul
= g_malloc(calc_size
);
1383 hw
->size_emul
= calc_size
;
1384 hw
->pos_emul
= hw
->pending_emul
= 0;
1387 while (hw
->pending_emul
< hw
->size_emul
) {
1388 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1389 hw
->size_emul
- hw
->pending_emul
);
1390 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1392 hw
->pending_emul
+= read
;
1393 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1394 if (read
< read_len
) {
1399 start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1401 start
+= hw
->size_emul
;
1403 assert(start
>= 0 && start
< hw
->size_emul
);
1405 *size
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1406 return hw
->buf_emul
+ start
;
1409 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1411 assert(size
<= hw
->pending_emul
);
1412 hw
->pending_emul
-= size
;
1415 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1417 if (unlikely(!hw
->buf_emul
)) {
1418 size_t calc_size
= hw
->mix_buf
->size
* hw
->info
.bytes_per_frame
;
1420 hw
->buf_emul
= g_malloc(calc_size
);
1421 hw
->size_emul
= calc_size
;
1422 hw
->pos_emul
= hw
->pending_emul
= 0;
1425 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1426 hw
->size_emul
- hw
->pos_emul
);
1427 return hw
->buf_emul
+ hw
->pos_emul
;
1430 size_t audio_generic_put_buffer_out_nowrite(HWVoiceOut
*hw
, void *buf
,
1433 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1434 size
+ hw
->pending_emul
<= hw
->size_emul
);
1436 hw
->pending_emul
+= size
;
1437 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1442 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1444 audio_generic_put_buffer_out_nowrite(hw
, buf
, size
);
1446 while (hw
->pending_emul
) {
1447 size_t write_len
, written
;
1448 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1450 start
+= hw
->size_emul
;
1452 assert(start
>= 0 && start
< hw
->size_emul
);
1454 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1456 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1457 hw
->pending_emul
-= written
;
1459 if (written
< write_len
) {
1465 * fake we have written everything. non-written data remain in pending_emul,
1466 * so we do not have to clip them multiple times
1471 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1473 size_t dst_size
, copy_size
;
1474 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1475 copy_size
= MIN(size
, dst_size
);
1477 memcpy(dst
, buf
, copy_size
);
1478 return hw
->pcm_ops
->put_buffer_out(hw
, buf
, copy_size
);
1481 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1483 size_t dst_size
, copy_size
;
1484 void *dst
= hw
->pcm_ops
->get_buffer_in(hw
, &dst_size
);
1485 copy_size
= MIN(size
, dst_size
);
1487 memcpy(dst
, buf
, copy_size
);
1488 hw
->pcm_ops
->put_buffer_in(hw
, buf
, copy_size
);
1493 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1494 bool msg
, Audiodev
*dev
)
1496 s
->drv_opaque
= drv
->init(dev
);
1498 if (s
->drv_opaque
) {
1499 if (!drv
->pcm_ops
->get_buffer_in
) {
1500 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1501 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1503 if (!drv
->pcm_ops
->get_buffer_out
) {
1504 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1505 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1508 audio_init_nb_voices_out(s
, drv
);
1509 audio_init_nb_voices_in(s
, drv
);
1515 dolog("Could not init `%s' audio driver\n", drv
->name
);
1521 static void audio_vm_change_state_handler (void *opaque
, int running
,
1524 AudioState
*s
= opaque
;
1525 HWVoiceOut
*hwo
= NULL
;
1526 HWVoiceIn
*hwi
= NULL
;
1528 s
->vm_running
= running
;
1529 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1530 if (hwo
->pcm_ops
->enable_out
) {
1531 hwo
->pcm_ops
->enable_out(hwo
, running
);
1535 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1536 if (hwi
->pcm_ops
->enable_in
) {
1537 hwi
->pcm_ops
->enable_in(hwi
, running
);
1540 audio_reset_timer (s
);
1543 static bool is_cleaning_up
;
1545 bool audio_is_cleaning_up(void)
1547 return is_cleaning_up
;
1550 static void free_audio_state(AudioState
*s
)
1552 HWVoiceOut
*hwo
, *hwon
;
1553 HWVoiceIn
*hwi
, *hwin
;
1555 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1558 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1559 hwo
->pcm_ops
->enable_out(hwo
, false);
1561 hwo
->pcm_ops
->fini_out (hwo
);
1563 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1564 CaptureVoiceOut
*cap
= sc
->cap
;
1565 struct capture_callback
*cb
;
1567 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1568 cb
->ops
.destroy (cb
->opaque
);
1571 QLIST_REMOVE(hwo
, entries
);
1574 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1575 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1576 hwi
->pcm_ops
->enable_in(hwi
, false);
1578 hwi
->pcm_ops
->fini_in (hwi
);
1579 QLIST_REMOVE(hwi
, entries
);
1583 s
->drv
->fini (s
->drv_opaque
);
1588 qapi_free_Audiodev(s
->dev
);
1600 void audio_cleanup(void)
1602 is_cleaning_up
= true;
1603 while (!QTAILQ_EMPTY(&audio_states
)) {
1604 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1605 QTAILQ_REMOVE(&audio_states
, s
, list
);
1606 free_audio_state(s
);
1610 static const VMStateDescription vmstate_audio
= {
1613 .minimum_version_id
= 1,
1614 .fields
= (VMStateField
[]) {
1615 VMSTATE_END_OF_LIST()
1619 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1621 static AudiodevListEntry
*audiodev_find(
1622 AudiodevListHead
*head
, const char *drvname
)
1624 AudiodevListEntry
*e
;
1625 QSIMPLEQ_FOREACH(e
, head
, next
) {
1626 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1635 * if we have dev, this function was called because of an -audiodev argument =>
1636 * initialize a new state with it
1637 * if dev == NULL => legacy implicit initialization, return the already created
1638 * state or create a new one
1640 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1642 static bool atexit_registered
;
1645 const char *drvname
= NULL
;
1646 VMChangeStateEntry
*e
;
1648 struct audio_driver
*driver
;
1649 /* silence gcc warning about uninitialized variable */
1650 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1653 /* -audiodev option */
1654 legacy_config
= false;
1655 drvname
= AudiodevDriver_str(dev
->driver
);
1656 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1657 if (!legacy_config
) {
1658 dolog("Device %s: audiodev default parameter is deprecated, please "
1659 "specify audiodev=%s\n", name
,
1660 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1662 return QTAILQ_FIRST(&audio_states
);
1664 /* legacy implicit initialization */
1665 head
= audio_handle_legacy_opts();
1667 * In case of legacy initialization, all Audiodevs in the list will have
1668 * the same configuration (except the driver), so it does't matter which
1669 * one we chose. We need an Audiodev to set up AudioState before we can
1670 * init a driver. Also note that dev at this point is still in the
1673 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1674 audio_validate_opts(dev
, &error_abort
);
1677 s
= g_malloc0(sizeof(AudioState
));
1680 QLIST_INIT (&s
->hw_head_out
);
1681 QLIST_INIT (&s
->hw_head_in
);
1682 QLIST_INIT (&s
->cap_head
);
1683 if (!atexit_registered
) {
1684 atexit(audio_cleanup
);
1685 atexit_registered
= true;
1687 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1689 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1691 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1692 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1694 if (s
->nb_hw_voices_out
<= 0) {
1695 dolog ("Bogus number of playback voices %d, setting to 1\n",
1696 s
->nb_hw_voices_out
);
1697 s
->nb_hw_voices_out
= 1;
1700 if (s
->nb_hw_voices_in
<= 0) {
1701 dolog ("Bogus number of capture voices %d, setting to 0\n",
1702 s
->nb_hw_voices_in
);
1703 s
->nb_hw_voices_in
= 0;
1707 driver
= audio_driver_lookup(drvname
);
1709 done
= !audio_driver_init(s
, driver
, true, dev
);
1711 dolog ("Unknown audio driver `%s'\n", drvname
);
1714 for (i
= 0; audio_prio_list
[i
]; i
++) {
1715 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1716 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1719 s
->dev
= dev
= e
->dev
;
1720 audio_validate_opts(dev
, &error_abort
);
1721 done
= !audio_driver_init(s
, driver
, false, dev
);
1729 audio_free_audiodev_list(&head
);
1732 driver
= audio_driver_lookup("none");
1733 done
= !audio_driver_init(s
, driver
, false, dev
);
1735 dolog("warning: Using timer based audio emulation\n");
1738 if (dev
->timer_period
<= 0) {
1739 s
->period_ticks
= 1;
1741 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1744 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1746 dolog ("warning: Could not register change state handler\n"
1747 "(Audio can continue looping even after stopping the VM)\n");
1750 QLIST_INIT (&s
->card_head
);
1751 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1755 void audio_free_audiodev_list(AudiodevListHead
*head
)
1757 AudiodevListEntry
*e
;
1758 while ((e
= QSIMPLEQ_FIRST(head
))) {
1759 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1760 qapi_free_Audiodev(e
->dev
);
1765 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1768 card
->state
= audio_init(NULL
, name
);
1771 card
->name
= g_strdup (name
);
1772 memset (&card
->entries
, 0, sizeof (card
->entries
));
1773 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1776 void AUD_remove_card (QEMUSoundCard
*card
)
1778 QLIST_REMOVE (card
, entries
);
1779 g_free (card
->name
);
1783 CaptureVoiceOut
*AUD_add_capture(
1785 struct audsettings
*as
,
1786 struct audio_capture_ops
*ops
,
1790 CaptureVoiceOut
*cap
;
1791 struct capture_callback
*cb
;
1794 if (!legacy_config
) {
1795 dolog("Capturing without setting an audiodev is deprecated\n");
1797 s
= audio_init(NULL
, NULL
);
1800 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1801 dolog("Can't capture with mixeng disabled\n");
1805 if (audio_validate_settings (as
)) {
1806 dolog ("Invalid settings were passed when trying to add capture\n");
1807 audio_print_settings (as
);
1811 cb
= g_malloc0(sizeof(*cb
));
1813 cb
->opaque
= cb_opaque
;
1815 cap
= audio_pcm_capture_find_specific(s
, as
);
1817 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1822 CaptureVoiceOut
*cap
;
1824 cap
= g_malloc0(sizeof(*cap
));
1828 QLIST_INIT (&hw
->sw_head
);
1829 QLIST_INIT (&cap
->cb_head
);
1831 /* XXX find a more elegant way */
1832 hw
->samples
= 4096 * 4;
1833 audio_pcm_hw_alloc_resources_out(hw
);
1835 audio_pcm_init_info (&hw
->info
, as
);
1837 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1839 hw
->clip
= mixeng_clip
1840 [hw
->info
.nchannels
== 2]
1842 [hw
->info
.swap_endianness
]
1843 [audio_bits_to_index (hw
->info
.bits
)];
1845 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1846 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1848 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1849 audio_attach_capture (hw
);
1855 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1857 struct capture_callback
*cb
;
1859 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1860 if (cb
->opaque
== cb_opaque
) {
1861 cb
->ops
.destroy (cb_opaque
);
1862 QLIST_REMOVE (cb
, entries
);
1865 if (!cap
->cb_head
.lh_first
) {
1866 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1869 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1870 #ifdef DEBUG_CAPTURE
1871 dolog ("freeing %s\n", sw
->name
);
1874 sw1
= sw
->entries
.le_next
;
1876 st_rate_stop (sw
->rate
);
1879 QLIST_REMOVE (sw
, entries
);
1880 QLIST_REMOVE (sc
, entries
);
1884 QLIST_REMOVE (cap
, entries
);
1885 g_free (cap
->hw
.mix_buf
);
1894 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1896 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1897 audio_set_volume_out(sw
, &vol
);
1900 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1903 HWVoiceOut
*hw
= sw
->hw
;
1905 sw
->vol
.mute
= vol
->mute
;
1906 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1907 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1910 if (hw
->pcm_ops
->volume_out
) {
1911 hw
->pcm_ops
->volume_out(hw
, vol
);
1916 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1918 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1919 audio_set_volume_in(sw
, &vol
);
1922 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1925 HWVoiceIn
*hw
= sw
->hw
;
1927 sw
->vol
.mute
= vol
->mute
;
1928 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1929 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1932 if (hw
->pcm_ops
->volume_in
) {
1933 hw
->pcm_ops
->volume_in(hw
, vol
);
1938 void audio_create_pdos(Audiodev
*dev
)
1940 switch (dev
->driver
) {
1941 #define CASE(DRIVER, driver, pdo_name) \
1942 case AUDIODEV_DRIVER_##DRIVER: \
1943 if (!dev->u.driver.has_in) { \
1944 dev->u.driver.in = g_malloc0( \
1945 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1946 dev->u.driver.has_in = true; \
1948 if (!dev->u.driver.has_out) { \
1949 dev->u.driver.out = g_malloc0( \
1950 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1951 dev->u.driver.has_out = true; \
1956 CASE(ALSA
, alsa
, Alsa
);
1957 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1958 CASE(DSOUND
, dsound
, );
1959 CASE(OSS
, oss
, Oss
);
1962 CASE(SPICE
, spice
, );
1965 case AUDIODEV_DRIVER__MAX
:
1970 static void audio_validate_per_direction_opts(
1971 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1973 if (!pdo
->has_mixing_engine
) {
1974 pdo
->has_mixing_engine
= true;
1975 pdo
->mixing_engine
= true;
1977 if (!pdo
->has_fixed_settings
) {
1978 pdo
->has_fixed_settings
= true;
1979 pdo
->fixed_settings
= pdo
->mixing_engine
;
1981 if (!pdo
->fixed_settings
&&
1982 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1984 "You can't use frequency, channels or format with fixed-settings=off");
1987 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
1988 error_setg(errp
, "You can't use fixed-settings without mixeng");
1992 if (!pdo
->has_frequency
) {
1993 pdo
->has_frequency
= true;
1994 pdo
->frequency
= 44100;
1996 if (!pdo
->has_channels
) {
1997 pdo
->has_channels
= true;
2000 if (!pdo
->has_voices
) {
2001 pdo
->has_voices
= true;
2002 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2004 if (!pdo
->has_format
) {
2005 pdo
->has_format
= true;
2006 pdo
->format
= AUDIO_FORMAT_S16
;
2010 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2014 audio_create_pdos(dev
);
2016 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2018 error_propagate(errp
, err
);
2022 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2024 error_propagate(errp
, err
);
2028 if (!dev
->has_timer_period
) {
2029 dev
->has_timer_period
= true;
2030 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2034 void audio_parse_option(const char *opt
)
2036 AudiodevListEntry
*e
;
2037 Audiodev
*dev
= NULL
;
2039 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2040 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2043 audio_validate_opts(dev
, &error_fatal
);
2045 e
= g_malloc0(sizeof(AudiodevListEntry
));
2047 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2050 void audio_init_audiodevs(void)
2052 AudiodevListEntry
*e
;
2054 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2055 audio_init(e
->dev
, NULL
);
2059 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2061 return (audsettings
) {
2062 .freq
= pdo
->frequency
,
2063 .nchannels
= pdo
->channels
,
2065 .endianness
= AUDIO_HOST_ENDIANNESS
,
2069 int audioformat_bytes_per_sample(AudioFormat fmt
)
2072 case AUDIO_FORMAT_U8
:
2073 case AUDIO_FORMAT_S8
:
2076 case AUDIO_FORMAT_U16
:
2077 case AUDIO_FORMAT_S16
:
2080 case AUDIO_FORMAT_U32
:
2081 case AUDIO_FORMAT_S32
:
2084 case AUDIO_FORMAT__MAX
:
2091 /* frames = freq * usec / 1e6 */
2092 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2093 audsettings
*as
, int def_usecs
)
2095 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2096 return (as
->freq
* usecs
+ 500000) / 1000000;
2099 /* samples = channels * frames = channels * freq * usec / 1e6 */
2100 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2101 audsettings
*as
, int def_usecs
)
2103 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2107 * bytes = bytes_per_sample * samples =
2108 * bytes_per_sample * channels * freq * usec / 1e6
2110 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2111 audsettings
*as
, int def_usecs
)
2113 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2114 audioformat_bytes_per_sample(as
->fmt
);
2117 AudioState
*audio_state_by_name(const char *name
)
2120 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2122 if (strcmp(name
, s
->dev
->id
) == 0) {
2129 const char *audio_get_id(QEMUSoundCard
*card
)
2132 assert(card
->state
->dev
);
2133 return card
->state
->dev
->id
;
2139 void audio_rate_start(RateCtl
*rate
)
2141 memset(rate
, 0, sizeof(RateCtl
));
2142 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2145 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2154 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2155 ticks
= now
- rate
->start_ticks
;
2156 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2157 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2158 if (samples
< 0 || samples
> 65536) {
2159 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2160 audio_rate_start(rate
);
2164 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2165 rate
->bytes_sent
+= ret
;