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"
37 #include "ui/qemu-spice.h"
40 #define AUDIO_CAP "audio"
41 #include "audio_int.h"
43 /* #define DEBUG_LIVE */
44 /* #define DEBUG_OUT */
45 /* #define DEBUG_CAPTURE */
46 /* #define DEBUG_POLL */
48 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
51 /* Order of CONFIG_AUDIO_DRIVERS is import.
52 The 1st one is the one used by default, that is the reason
53 that we generate the list.
55 const char *audio_prio_list
[] = {
63 static QLIST_HEAD(, audio_driver
) audio_drivers
;
64 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
66 void audio_driver_register(audio_driver
*drv
)
68 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
71 audio_driver
*audio_driver_lookup(const char *name
)
73 struct audio_driver
*d
;
75 QLIST_FOREACH(d
, &audio_drivers
, next
) {
76 if (strcmp(name
, d
->name
) == 0) {
81 audio_module_load_one(name
);
82 QLIST_FOREACH(d
, &audio_drivers
, next
) {
83 if (strcmp(name
, d
->name
) == 0) {
91 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
92 QTAILQ_HEAD_INITIALIZER(audio_states
);
94 const struct mixeng_volume nominal_volume
= {
105 static bool legacy_config
= true;
107 int audio_bug (const char *funcname
, int cond
)
112 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
115 AUD_log (NULL
, "Save all your work and restart without audio\n");
116 AUD_log (NULL
, "I am sorry\n");
118 AUD_log (NULL
, "Context:\n");
125 static inline int audio_bits_to_index (int bits
)
138 audio_bug ("bits_to_index", 1);
139 AUD_log (NULL
, "invalid bits %d\n", bits
);
144 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
150 cond
= !nmemb
|| !size
;
154 if (audio_bug ("audio_calloc", cond
)) {
155 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
157 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
161 return g_malloc0 (len
);
164 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
167 fprintf(stderr
, "%s: ", cap
);
170 vfprintf(stderr
, fmt
, ap
);
173 void AUD_log (const char *cap
, const char *fmt
, ...)
178 AUD_vlog (cap
, fmt
, ap
);
182 static void audio_print_settings (struct audsettings
*as
)
184 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
187 case AUDIO_FORMAT_S8
:
188 AUD_log (NULL
, "S8");
190 case AUDIO_FORMAT_U8
:
191 AUD_log (NULL
, "U8");
193 case AUDIO_FORMAT_S16
:
194 AUD_log (NULL
, "S16");
196 case AUDIO_FORMAT_U16
:
197 AUD_log (NULL
, "U16");
199 case AUDIO_FORMAT_S32
:
200 AUD_log (NULL
, "S32");
202 case AUDIO_FORMAT_U32
:
203 AUD_log (NULL
, "U32");
205 case AUDIO_FORMAT_F32
:
206 AUD_log (NULL
, "F32");
209 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
213 AUD_log (NULL
, " endianness=");
214 switch (as
->endianness
) {
216 AUD_log (NULL
, "little");
219 AUD_log (NULL
, "big");
222 AUD_log (NULL
, "invalid");
225 AUD_log (NULL
, "\n");
228 static int audio_validate_settings (struct audsettings
*as
)
232 invalid
= as
->nchannels
< 1;
233 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
236 case AUDIO_FORMAT_S8
:
237 case AUDIO_FORMAT_U8
:
238 case AUDIO_FORMAT_S16
:
239 case AUDIO_FORMAT_U16
:
240 case AUDIO_FORMAT_S32
:
241 case AUDIO_FORMAT_U32
:
242 case AUDIO_FORMAT_F32
:
249 invalid
|= as
->freq
<= 0;
250 return invalid
? -1 : 0;
253 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
256 bool is_signed
= false, is_float
= false;
259 case AUDIO_FORMAT_S8
:
262 case AUDIO_FORMAT_U8
:
265 case AUDIO_FORMAT_S16
:
268 case AUDIO_FORMAT_U16
:
272 case AUDIO_FORMAT_F32
:
275 case AUDIO_FORMAT_S32
:
278 case AUDIO_FORMAT_U32
:
285 return info
->freq
== as
->freq
286 && info
->nchannels
== as
->nchannels
287 && info
->is_signed
== is_signed
288 && info
->is_float
== is_float
289 && info
->bits
== bits
290 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
293 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
296 bool is_signed
= false, is_float
= false;
299 case AUDIO_FORMAT_S8
:
302 case AUDIO_FORMAT_U8
:
306 case AUDIO_FORMAT_S16
:
309 case AUDIO_FORMAT_U16
:
314 case AUDIO_FORMAT_F32
:
317 case AUDIO_FORMAT_S32
:
320 case AUDIO_FORMAT_U32
:
329 info
->freq
= as
->freq
;
331 info
->is_signed
= is_signed
;
332 info
->is_float
= is_float
;
333 info
->nchannels
= as
->nchannels
;
334 info
->bytes_per_frame
= as
->nchannels
* mul
;
335 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
336 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
339 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
345 if (info
->is_signed
|| info
->is_float
) {
346 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
349 switch (info
->bits
) {
351 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
360 if (info
->swap_endianness
) {
364 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
374 int32_t s
= INT32_MAX
;
376 if (info
->swap_endianness
) {
380 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
387 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
397 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
404 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
405 struct audsettings
*as
)
407 CaptureVoiceOut
*cap
;
409 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
410 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
417 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
419 struct capture_callback
*cb
;
422 dolog ("notification %d sent\n", cmd
);
424 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
425 cb
->ops
.notify (cb
->opaque
, cmd
);
429 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
431 if (cap
->hw
.enabled
!= enabled
) {
432 audcnotification_e cmd
;
433 cap
->hw
.enabled
= enabled
;
434 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
435 audio_notify_capture (cap
, cmd
);
439 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
441 HWVoiceOut
*hw
= &cap
->hw
;
445 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
451 audio_capture_maybe_changed (cap
, enabled
);
454 static void audio_detach_capture (HWVoiceOut
*hw
)
456 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
459 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
460 SWVoiceOut
*sw
= &sc
->sw
;
461 CaptureVoiceOut
*cap
= sc
->cap
;
462 int was_active
= sw
->active
;
465 st_rate_stop (sw
->rate
);
469 QLIST_REMOVE (sw
, entries
);
470 QLIST_REMOVE (sc
, entries
);
473 /* We have removed soft voice from the capture:
474 this might have changed the overall status of the capture
475 since this might have been the only active voice */
476 audio_recalc_and_notify_capture (cap
);
482 static int audio_attach_capture (HWVoiceOut
*hw
)
484 AudioState
*s
= hw
->s
;
485 CaptureVoiceOut
*cap
;
487 audio_detach_capture (hw
);
488 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
491 HWVoiceOut
*hw_cap
= &cap
->hw
;
493 sc
= g_malloc0(sizeof(*sc
));
500 sw
->active
= hw
->enabled
;
501 sw
->conv
= noop_conv
;
502 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
503 sw
->vol
= nominal_volume
;
504 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
506 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
510 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
511 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
513 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
514 hw
, sw
->info
.freq
, sw
->info
.bits
,
516 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
519 audio_capture_maybe_changed (cap
, 1);
526 * Hard voice (capture)
528 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
531 size_t m
= hw
->total_samples_captured
;
533 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
535 m
= MIN (m
, sw
->total_hw_samples_acquired
);
541 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
543 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
544 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
545 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
551 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
554 size_t pos
= hw
->mix_buf
->pos
;
557 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
558 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
559 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
560 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
562 hw
->clip(dst
, src
, samples_to_clip
);
564 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
565 len
-= samples_to_clip
;
566 clipped
+= samples_to_clip
;
571 * Soft voice (capture)
573 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
575 HWVoiceIn
*hw
= sw
->hw
;
576 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
579 if (audio_bug(__func__
, live
< 0 || live
> hw
->conv_buf
->size
)) {
580 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
584 rpos
= hw
->conv_buf
->pos
- live
;
589 return hw
->conv_buf
->size
+ rpos
;
593 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
595 HWVoiceIn
*hw
= sw
->hw
;
596 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
597 struct st_sample
*src
, *dst
= sw
->buf
;
599 rpos
= audio_pcm_sw_get_rpos_in(sw
) % hw
->conv_buf
->size
;
601 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
602 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
603 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
607 samples
= size
/ sw
->info
.bytes_per_frame
;
612 swlim
= (live
* sw
->ratio
) >> 32;
613 swlim
= MIN (swlim
, samples
);
616 src
= hw
->conv_buf
->samples
+ rpos
;
617 if (hw
->conv_buf
->pos
> rpos
) {
618 isamp
= hw
->conv_buf
->pos
- rpos
;
620 isamp
= hw
->conv_buf
->size
- rpos
;
628 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
630 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
636 if (hw
->pcm_ops
&& !hw
->pcm_ops
->volume_in
) {
637 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
640 sw
->clip (buf
, sw
->buf
, ret
);
641 sw
->total_hw_samples_acquired
+= total
;
642 return ret
* sw
->info
.bytes_per_frame
;
646 * Hard voice (playback)
648 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
654 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
655 if (sw
->active
|| !sw
->empty
) {
656 m
= MIN (m
, sw
->total_hw_samples_mixed
);
665 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
670 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
678 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
679 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
688 * Soft voice (playback)
690 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
692 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
693 size_t ret
= 0, pos
= 0, total
= 0;
699 hwsamples
= sw
->hw
->mix_buf
->size
;
701 live
= sw
->total_hw_samples_mixed
;
702 if (audio_bug(__func__
, live
> hwsamples
)) {
703 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
707 if (live
== hwsamples
) {
709 dolog ("%s is full %d\n", sw
->name
, live
);
714 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
715 samples
= size
/ sw
->info
.bytes_per_frame
;
717 dead
= hwsamples
- live
;
718 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
719 swlim
= MIN (swlim
, samples
);
721 sw
->conv (sw
->buf
, buf
, swlim
);
723 if (sw
->hw
->pcm_ops
&& !sw
->hw
->pcm_ops
->volume_out
) {
724 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
729 dead
= hwsamples
- live
;
730 left
= hwsamples
- wpos
;
731 blck
= MIN (dead
, left
);
740 sw
->hw
->mix_buf
->samples
+ wpos
,
748 wpos
= (wpos
+ osamp
) % hwsamples
;
752 sw
->total_hw_samples_mixed
+= total
;
753 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
757 "%s: write size %zu ret %zu total sw %zu\n",
759 size
/ sw
->info
.bytes_per_frame
,
761 sw
->total_hw_samples_mixed
765 return ret
* sw
->info
.bytes_per_frame
;
769 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
771 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
772 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
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
->samples
* 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
= live
* hw
->info
.bytes_per_frame
;
1078 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1084 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1086 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1088 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1089 decr
* hw
->info
.bytes_per_frame
) /
1090 hw
->info
.bytes_per_frame
;
1094 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1096 if (proc
== 0 || proc
< decr
) {
1101 if (hw
->pcm_ops
->run_buffer_out
) {
1102 hw
->pcm_ops
->run_buffer_out(hw
);
1108 static void audio_run_out (AudioState
*s
)
1110 HWVoiceOut
*hw
= NULL
;
1113 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1114 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1115 /* there is exactly 1 sw for each hw with no mixeng */
1116 sw
= hw
->sw_head
.lh_first
;
1118 if (hw
->pending_disable
) {
1120 hw
->pending_disable
= 0;
1121 if (hw
->pcm_ops
->enable_out
) {
1122 hw
->pcm_ops
->enable_out(hw
, false);
1127 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1133 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1134 size_t played
, live
, prev_rpos
, free
;
1135 int nb_live
, cleanup_required
;
1137 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1142 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1143 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1147 if (hw
->pending_disable
&& !nb_live
) {
1150 dolog ("Disabling voice\n");
1153 hw
->pending_disable
= 0;
1154 if (hw
->pcm_ops
->enable_out
) {
1155 hw
->pcm_ops
->enable_out(hw
, false);
1157 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1159 audio_recalc_and_notify_capture (sc
->cap
);
1165 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1167 free
= audio_get_free (sw
);
1169 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1173 if (hw
->pcm_ops
->run_buffer_out
) {
1174 hw
->pcm_ops
->run_buffer_out(hw
);
1179 prev_rpos
= hw
->mix_buf
->pos
;
1180 played
= audio_pcm_hw_run_out(hw
, live
);
1181 replay_audio_out(&played
);
1182 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1183 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1184 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1185 hw
->mix_buf
->pos
= 0;
1189 dolog("played=%zu\n", played
);
1193 hw
->ts_helper
+= played
;
1194 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1197 cleanup_required
= 0;
1198 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1199 if (!sw
->active
&& sw
->empty
) {
1203 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1204 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1205 played
, sw
->total_hw_samples_mixed
);
1206 played
= sw
->total_hw_samples_mixed
;
1209 sw
->total_hw_samples_mixed
-= played
;
1211 if (!sw
->total_hw_samples_mixed
) {
1213 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1217 free
= audio_get_free (sw
);
1219 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1224 if (cleanup_required
) {
1227 sw
= hw
->sw_head
.lh_first
;
1229 sw1
= sw
->entries
.le_next
;
1230 if (!sw
->active
&& !sw
->callback
.fn
) {
1231 audio_close_out (sw
);
1239 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1242 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
1246 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1247 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1249 assert(size
% hw
->info
.bytes_per_frame
== 0);
1254 proc
= MIN(size
/ hw
->info
.bytes_per_frame
,
1255 conv_buf
->size
- conv_buf
->pos
);
1257 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, buf
, proc
);
1258 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
1262 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1268 static void audio_run_in (AudioState
*s
)
1270 HWVoiceIn
*hw
= NULL
;
1272 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1273 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1274 /* there is exactly 1 sw for each hw with no mixeng */
1275 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1277 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1283 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1285 size_t captured
= 0, min
;
1287 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1288 captured
= audio_pcm_hw_run_in(
1289 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1291 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1292 hw
->conv_buf
->size
);
1294 min
= audio_pcm_hw_find_min_in (hw
);
1295 hw
->total_samples_captured
+= captured
- min
;
1296 hw
->ts_helper
+= captured
;
1298 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1299 sw
->total_hw_samples_acquired
-= min
;
1304 avail
= audio_get_avail (sw
);
1306 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1313 static void audio_run_capture (AudioState
*s
)
1315 CaptureVoiceOut
*cap
;
1317 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1318 size_t live
, rpos
, captured
;
1319 HWVoiceOut
*hw
= &cap
->hw
;
1322 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1323 rpos
= hw
->mix_buf
->pos
;
1325 size_t left
= hw
->mix_buf
->size
- rpos
;
1326 size_t to_capture
= MIN(live
, left
);
1327 struct st_sample
*src
;
1328 struct capture_callback
*cb
;
1330 src
= hw
->mix_buf
->samples
+ rpos
;
1331 hw
->clip (cap
->buf
, src
, to_capture
);
1332 mixeng_clear (src
, to_capture
);
1334 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1335 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1336 to_capture
* hw
->info
.bytes_per_frame
);
1338 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1341 hw
->mix_buf
->pos
= rpos
;
1343 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1344 if (!sw
->active
&& sw
->empty
) {
1348 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1349 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1350 captured
, sw
->total_hw_samples_mixed
);
1351 captured
= sw
->total_hw_samples_mixed
;
1354 sw
->total_hw_samples_mixed
-= captured
;
1355 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1360 void audio_run(AudioState
*s
, const char *msg
)
1364 audio_run_capture(s
);
1368 static double prevtime
;
1372 if (gettimeofday (&tv
, NULL
)) {
1373 perror ("audio_run: gettimeofday");
1377 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1378 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1379 prevtime
= currtime
;
1384 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1388 if (unlikely(!hw
->buf_emul
)) {
1389 size_t calc_size
= hw
->conv_buf
->size
* hw
->info
.bytes_per_frame
;
1390 hw
->buf_emul
= g_malloc(calc_size
);
1391 hw
->size_emul
= calc_size
;
1392 hw
->pos_emul
= hw
->pending_emul
= 0;
1395 while (hw
->pending_emul
< hw
->size_emul
) {
1396 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1397 hw
->size_emul
- hw
->pending_emul
);
1398 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1400 hw
->pending_emul
+= read
;
1401 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1402 if (read
< read_len
) {
1407 start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1409 start
+= hw
->size_emul
;
1411 assert(start
>= 0 && start
< hw
->size_emul
);
1413 *size
= MIN(*size
, hw
->pending_emul
);
1414 *size
= MIN(*size
, hw
->size_emul
- start
);
1415 return hw
->buf_emul
+ start
;
1418 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1420 assert(size
<= hw
->pending_emul
);
1421 hw
->pending_emul
-= size
;
1424 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1426 while (hw
->pending_emul
) {
1427 size_t write_len
, written
;
1428 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1431 start
+= hw
->size_emul
;
1433 assert(start
>= 0 && start
< hw
->size_emul
);
1435 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1437 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1438 hw
->pending_emul
-= written
;
1440 if (written
< write_len
) {
1446 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1448 if (unlikely(!hw
->buf_emul
)) {
1449 size_t calc_size
= hw
->mix_buf
->size
* hw
->info
.bytes_per_frame
;
1451 hw
->buf_emul
= g_malloc(calc_size
);
1452 hw
->size_emul
= calc_size
;
1453 hw
->pos_emul
= hw
->pending_emul
= 0;
1456 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1457 hw
->size_emul
- hw
->pos_emul
);
1458 return hw
->buf_emul
+ hw
->pos_emul
;
1461 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1463 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1464 size
+ hw
->pending_emul
<= hw
->size_emul
);
1466 hw
->pending_emul
+= size
;
1467 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1472 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1476 while (total
< size
) {
1477 size_t dst_size
= size
- total
;
1478 size_t copy_size
, proc
;
1479 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1481 if (dst_size
== 0) {
1485 copy_size
= MIN(size
- total
, dst_size
);
1487 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1489 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1492 if (proc
== 0 || proc
< copy_size
) {
1497 if (hw
->pcm_ops
->run_buffer_out
) {
1498 hw
->pcm_ops
->run_buffer_out(hw
);
1504 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1508 while (total
< size
) {
1509 size_t src_size
= size
- total
;
1510 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1512 if (src_size
== 0) {
1516 memcpy((char *)buf
+ total
, src
, src_size
);
1517 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1524 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1525 bool msg
, Audiodev
*dev
)
1527 s
->drv_opaque
= drv
->init(dev
);
1529 if (s
->drv_opaque
) {
1530 if (!drv
->pcm_ops
->get_buffer_in
) {
1531 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1532 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1534 if (!drv
->pcm_ops
->get_buffer_out
) {
1535 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1536 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1539 audio_init_nb_voices_out(s
, drv
);
1540 audio_init_nb_voices_in(s
, drv
);
1546 dolog("Could not init `%s' audio driver\n", drv
->name
);
1552 static void audio_vm_change_state_handler (void *opaque
, int running
,
1555 AudioState
*s
= opaque
;
1556 HWVoiceOut
*hwo
= NULL
;
1557 HWVoiceIn
*hwi
= NULL
;
1559 s
->vm_running
= running
;
1560 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1561 if (hwo
->pcm_ops
->enable_out
) {
1562 hwo
->pcm_ops
->enable_out(hwo
, running
);
1566 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1567 if (hwi
->pcm_ops
->enable_in
) {
1568 hwi
->pcm_ops
->enable_in(hwi
, running
);
1571 audio_reset_timer (s
);
1574 static void free_audio_state(AudioState
*s
)
1576 HWVoiceOut
*hwo
, *hwon
;
1577 HWVoiceIn
*hwi
, *hwin
;
1579 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1582 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1583 hwo
->pcm_ops
->enable_out(hwo
, false);
1585 hwo
->pcm_ops
->fini_out (hwo
);
1587 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1588 CaptureVoiceOut
*cap
= sc
->cap
;
1589 struct capture_callback
*cb
;
1591 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1592 cb
->ops
.destroy (cb
->opaque
);
1595 QLIST_REMOVE(hwo
, entries
);
1598 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1599 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1600 hwi
->pcm_ops
->enable_in(hwi
, false);
1602 hwi
->pcm_ops
->fini_in (hwi
);
1603 QLIST_REMOVE(hwi
, entries
);
1607 s
->drv
->fini (s
->drv_opaque
);
1612 qapi_free_Audiodev(s
->dev
);
1624 void audio_cleanup(void)
1626 while (!QTAILQ_EMPTY(&audio_states
)) {
1627 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1628 QTAILQ_REMOVE(&audio_states
, s
, list
);
1629 free_audio_state(s
);
1633 static const VMStateDescription vmstate_audio
= {
1636 .minimum_version_id
= 1,
1637 .fields
= (VMStateField
[]) {
1638 VMSTATE_END_OF_LIST()
1642 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1644 static AudiodevListEntry
*audiodev_find(
1645 AudiodevListHead
*head
, const char *drvname
)
1647 AudiodevListEntry
*e
;
1648 QSIMPLEQ_FOREACH(e
, head
, next
) {
1649 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1658 * if we have dev, this function was called because of an -audiodev argument =>
1659 * initialize a new state with it
1660 * if dev == NULL => legacy implicit initialization, return the already created
1661 * state or create a new one
1663 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1665 static bool atexit_registered
;
1668 const char *drvname
= NULL
;
1669 VMChangeStateEntry
*e
;
1671 struct audio_driver
*driver
;
1672 /* silence gcc warning about uninitialized variable */
1673 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1677 * When using spice allow the spice audio driver being picked
1680 * Temporary hack. Using audio devices without explicit
1681 * audiodev= property is already deprecated. Same goes for
1682 * the -soundhw switch. Once this support gets finally
1683 * removed we can also drop the concept of a default audio
1684 * backend and this can go away.
1686 driver
= audio_driver_lookup("spice");
1688 driver
->can_be_default
= 1;
1693 /* -audiodev option */
1694 legacy_config
= false;
1695 drvname
= AudiodevDriver_str(dev
->driver
);
1696 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1697 if (!legacy_config
) {
1698 dolog("Device %s: audiodev default parameter is deprecated, please "
1699 "specify audiodev=%s\n", name
,
1700 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1702 return QTAILQ_FIRST(&audio_states
);
1704 /* legacy implicit initialization */
1705 head
= audio_handle_legacy_opts();
1707 * In case of legacy initialization, all Audiodevs in the list will have
1708 * the same configuration (except the driver), so it doesn't matter which
1709 * one we chose. We need an Audiodev to set up AudioState before we can
1710 * init a driver. Also note that dev at this point is still in the
1713 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1714 audio_validate_opts(dev
, &error_abort
);
1717 s
= g_malloc0(sizeof(AudioState
));
1720 QLIST_INIT (&s
->hw_head_out
);
1721 QLIST_INIT (&s
->hw_head_in
);
1722 QLIST_INIT (&s
->cap_head
);
1723 if (!atexit_registered
) {
1724 atexit(audio_cleanup
);
1725 atexit_registered
= true;
1727 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1729 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1731 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1732 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1734 if (s
->nb_hw_voices_out
<= 0) {
1735 dolog ("Bogus number of playback voices %d, setting to 1\n",
1736 s
->nb_hw_voices_out
);
1737 s
->nb_hw_voices_out
= 1;
1740 if (s
->nb_hw_voices_in
<= 0) {
1741 dolog ("Bogus number of capture voices %d, setting to 0\n",
1742 s
->nb_hw_voices_in
);
1743 s
->nb_hw_voices_in
= 0;
1747 driver
= audio_driver_lookup(drvname
);
1749 done
= !audio_driver_init(s
, driver
, true, dev
);
1751 dolog ("Unknown audio driver `%s'\n", drvname
);
1754 for (i
= 0; audio_prio_list
[i
]; i
++) {
1755 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1756 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1759 s
->dev
= dev
= e
->dev
;
1760 audio_validate_opts(dev
, &error_abort
);
1761 done
= !audio_driver_init(s
, driver
, false, dev
);
1769 audio_free_audiodev_list(&head
);
1772 driver
= audio_driver_lookup("none");
1773 done
= !audio_driver_init(s
, driver
, false, dev
);
1775 dolog("warning: Using timer based audio emulation\n");
1778 if (dev
->timer_period
<= 0) {
1779 s
->period_ticks
= 1;
1781 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1784 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1786 dolog ("warning: Could not register change state handler\n"
1787 "(Audio can continue looping even after stopping the VM)\n");
1790 QLIST_INIT (&s
->card_head
);
1791 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1795 void audio_free_audiodev_list(AudiodevListHead
*head
)
1797 AudiodevListEntry
*e
;
1798 while ((e
= QSIMPLEQ_FIRST(head
))) {
1799 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1800 qapi_free_Audiodev(e
->dev
);
1805 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1808 card
->state
= audio_init(NULL
, name
);
1811 card
->name
= g_strdup (name
);
1812 memset (&card
->entries
, 0, sizeof (card
->entries
));
1813 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1816 void AUD_remove_card (QEMUSoundCard
*card
)
1818 QLIST_REMOVE (card
, entries
);
1819 g_free (card
->name
);
1823 CaptureVoiceOut
*AUD_add_capture(
1825 struct audsettings
*as
,
1826 struct audio_capture_ops
*ops
,
1830 CaptureVoiceOut
*cap
;
1831 struct capture_callback
*cb
;
1834 if (!legacy_config
) {
1835 dolog("Capturing without setting an audiodev is deprecated\n");
1837 s
= audio_init(NULL
, NULL
);
1840 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1841 dolog("Can't capture with mixeng disabled\n");
1845 if (audio_validate_settings (as
)) {
1846 dolog ("Invalid settings were passed when trying to add capture\n");
1847 audio_print_settings (as
);
1851 cb
= g_malloc0(sizeof(*cb
));
1853 cb
->opaque
= cb_opaque
;
1855 cap
= audio_pcm_capture_find_specific(s
, as
);
1857 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1862 CaptureVoiceOut
*cap
;
1864 cap
= g_malloc0(sizeof(*cap
));
1868 QLIST_INIT (&hw
->sw_head
);
1869 QLIST_INIT (&cap
->cb_head
);
1871 /* XXX find a more elegant way */
1872 hw
->samples
= 4096 * 4;
1873 audio_pcm_hw_alloc_resources_out(hw
);
1875 audio_pcm_init_info (&hw
->info
, as
);
1877 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1879 if (hw
->info
.is_float
) {
1880 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1882 hw
->clip
= mixeng_clip
1883 [hw
->info
.nchannels
== 2]
1884 [hw
->info
.is_signed
]
1885 [hw
->info
.swap_endianness
]
1886 [audio_bits_to_index(hw
->info
.bits
)];
1889 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1890 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1892 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1893 audio_attach_capture (hw
);
1899 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1901 struct capture_callback
*cb
;
1903 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1904 if (cb
->opaque
== cb_opaque
) {
1905 cb
->ops
.destroy (cb_opaque
);
1906 QLIST_REMOVE (cb
, entries
);
1909 if (!cap
->cb_head
.lh_first
) {
1910 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1913 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1914 #ifdef DEBUG_CAPTURE
1915 dolog ("freeing %s\n", sw
->name
);
1918 sw1
= sw
->entries
.le_next
;
1920 st_rate_stop (sw
->rate
);
1923 QLIST_REMOVE (sw
, entries
);
1924 QLIST_REMOVE (sc
, entries
);
1928 QLIST_REMOVE (cap
, entries
);
1929 g_free (cap
->hw
.mix_buf
);
1938 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1940 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1941 audio_set_volume_out(sw
, &vol
);
1944 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1947 HWVoiceOut
*hw
= sw
->hw
;
1949 sw
->vol
.mute
= vol
->mute
;
1950 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1951 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1954 if (hw
->pcm_ops
->volume_out
) {
1955 hw
->pcm_ops
->volume_out(hw
, vol
);
1960 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1962 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1963 audio_set_volume_in(sw
, &vol
);
1966 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1969 HWVoiceIn
*hw
= sw
->hw
;
1971 sw
->vol
.mute
= vol
->mute
;
1972 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1973 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1976 if (hw
->pcm_ops
->volume_in
) {
1977 hw
->pcm_ops
->volume_in(hw
, vol
);
1982 void audio_create_pdos(Audiodev
*dev
)
1984 switch (dev
->driver
) {
1985 #define CASE(DRIVER, driver, pdo_name) \
1986 case AUDIODEV_DRIVER_##DRIVER: \
1987 if (!dev->u.driver.has_in) { \
1988 dev->u.driver.in = g_malloc0( \
1989 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1990 dev->u.driver.has_in = true; \
1992 if (!dev->u.driver.has_out) { \
1993 dev->u.driver.out = g_malloc0( \
1994 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1995 dev->u.driver.has_out = true; \
2000 CASE(ALSA
, alsa
, Alsa
);
2001 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2002 CASE(DSOUND
, dsound
, );
2003 CASE(JACK
, jack
, Jack
);
2004 CASE(OSS
, oss
, Oss
);
2007 CASE(SPICE
, spice
, );
2010 case AUDIODEV_DRIVER__MAX
:
2015 static void audio_validate_per_direction_opts(
2016 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2018 if (!pdo
->has_mixing_engine
) {
2019 pdo
->has_mixing_engine
= true;
2020 pdo
->mixing_engine
= true;
2022 if (!pdo
->has_fixed_settings
) {
2023 pdo
->has_fixed_settings
= true;
2024 pdo
->fixed_settings
= pdo
->mixing_engine
;
2026 if (!pdo
->fixed_settings
&&
2027 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2029 "You can't use frequency, channels or format with fixed-settings=off");
2032 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2033 error_setg(errp
, "You can't use fixed-settings without mixeng");
2037 if (!pdo
->has_frequency
) {
2038 pdo
->has_frequency
= true;
2039 pdo
->frequency
= 44100;
2041 if (!pdo
->has_channels
) {
2042 pdo
->has_channels
= true;
2045 if (!pdo
->has_voices
) {
2046 pdo
->has_voices
= true;
2047 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2049 if (!pdo
->has_format
) {
2050 pdo
->has_format
= true;
2051 pdo
->format
= AUDIO_FORMAT_S16
;
2055 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2059 audio_create_pdos(dev
);
2061 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2063 error_propagate(errp
, err
);
2067 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2069 error_propagate(errp
, err
);
2073 if (!dev
->has_timer_period
) {
2074 dev
->has_timer_period
= true;
2075 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2079 void audio_parse_option(const char *opt
)
2081 AudiodevListEntry
*e
;
2082 Audiodev
*dev
= NULL
;
2084 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2085 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2088 audio_validate_opts(dev
, &error_fatal
);
2090 e
= g_malloc0(sizeof(AudiodevListEntry
));
2092 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2095 void audio_init_audiodevs(void)
2097 AudiodevListEntry
*e
;
2099 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2100 audio_init(e
->dev
, NULL
);
2104 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2106 return (audsettings
) {
2107 .freq
= pdo
->frequency
,
2108 .nchannels
= pdo
->channels
,
2110 .endianness
= AUDIO_HOST_ENDIANNESS
,
2114 int audioformat_bytes_per_sample(AudioFormat fmt
)
2117 case AUDIO_FORMAT_U8
:
2118 case AUDIO_FORMAT_S8
:
2121 case AUDIO_FORMAT_U16
:
2122 case AUDIO_FORMAT_S16
:
2125 case AUDIO_FORMAT_U32
:
2126 case AUDIO_FORMAT_S32
:
2127 case AUDIO_FORMAT_F32
:
2130 case AUDIO_FORMAT__MAX
:
2137 /* frames = freq * usec / 1e6 */
2138 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2139 audsettings
*as
, int def_usecs
)
2141 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2142 return (as
->freq
* usecs
+ 500000) / 1000000;
2145 /* samples = channels * frames = channels * freq * usec / 1e6 */
2146 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2147 audsettings
*as
, int def_usecs
)
2149 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2153 * bytes = bytes_per_sample * samples =
2154 * bytes_per_sample * channels * freq * usec / 1e6
2156 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2157 audsettings
*as
, int def_usecs
)
2159 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2160 audioformat_bytes_per_sample(as
->fmt
);
2163 AudioState
*audio_state_by_name(const char *name
)
2166 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2168 if (strcmp(name
, s
->dev
->id
) == 0) {
2175 const char *audio_get_id(QEMUSoundCard
*card
)
2178 assert(card
->state
->dev
);
2179 return card
->state
->dev
->id
;
2185 void audio_rate_start(RateCtl
*rate
)
2187 memset(rate
, 0, sizeof(RateCtl
));
2188 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2191 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2200 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2201 ticks
= now
- rate
->start_ticks
;
2202 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2203 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2204 if (samples
< 0 || samples
> 65536) {
2205 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2206 audio_rate_start(rate
);
2210 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2211 rate
->bytes_sent
+= ret
;