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/clone-visitor.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qapi/qapi-visit-audio.h"
34 #include "qapi/qapi-commands-audio.h"
35 #include "qemu/cutils.h"
36 #include "qemu/error-report.h"
38 #include "qemu/module.h"
39 #include "qemu/help_option.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/replay.h"
42 #include "sysemu/runstate.h"
43 #include "ui/qemu-spice.h"
46 #define AUDIO_CAP "audio"
47 #include "audio_int.h"
49 /* #define DEBUG_LIVE */
50 /* #define DEBUG_OUT */
51 /* #define DEBUG_CAPTURE */
52 /* #define DEBUG_POLL */
54 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
57 /* Order of CONFIG_AUDIO_DRIVERS is import.
58 The 1st one is the one used by default, that is the reason
59 that we generate the list.
61 const char *audio_prio_list
[] = {
69 static QLIST_HEAD(, audio_driver
) audio_drivers
;
70 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
72 void audio_driver_register(audio_driver
*drv
)
74 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
77 audio_driver
*audio_driver_lookup(const char *name
)
79 struct audio_driver
*d
;
80 Error
*local_err
= NULL
;
83 QLIST_FOREACH(d
, &audio_drivers
, next
) {
84 if (strcmp(name
, d
->name
) == 0) {
88 rv
= audio_module_load(name
, &local_err
);
90 QLIST_FOREACH(d
, &audio_drivers
, next
) {
91 if (strcmp(name
, d
->name
) == 0) {
96 error_report_err(local_err
);
101 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
102 QTAILQ_HEAD_INITIALIZER(audio_states
);
104 const struct mixeng_volume nominal_volume
= {
115 static bool legacy_config
= true;
117 int audio_bug (const char *funcname
, int cond
)
122 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
125 AUD_log (NULL
, "Save all your work and restart without audio\n");
126 AUD_log (NULL
, "I am sorry\n");
128 AUD_log (NULL
, "Context:\n");
134 static inline int audio_bits_to_index (int bits
)
147 audio_bug ("bits_to_index", 1);
148 AUD_log (NULL
, "invalid bits %d\n", bits
);
153 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
156 fprintf(stderr
, "%s: ", cap
);
159 vfprintf(stderr
, fmt
, ap
);
162 void AUD_log (const char *cap
, const char *fmt
, ...)
167 AUD_vlog (cap
, fmt
, ap
);
171 static void audio_print_settings (struct audsettings
*as
)
173 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
176 case AUDIO_FORMAT_S8
:
177 AUD_log (NULL
, "S8");
179 case AUDIO_FORMAT_U8
:
180 AUD_log (NULL
, "U8");
182 case AUDIO_FORMAT_S16
:
183 AUD_log (NULL
, "S16");
185 case AUDIO_FORMAT_U16
:
186 AUD_log (NULL
, "U16");
188 case AUDIO_FORMAT_S32
:
189 AUD_log (NULL
, "S32");
191 case AUDIO_FORMAT_U32
:
192 AUD_log (NULL
, "U32");
194 case AUDIO_FORMAT_F32
:
195 AUD_log (NULL
, "F32");
198 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
202 AUD_log (NULL
, " endianness=");
203 switch (as
->endianness
) {
205 AUD_log (NULL
, "little");
208 AUD_log (NULL
, "big");
211 AUD_log (NULL
, "invalid");
214 AUD_log (NULL
, "\n");
217 static int audio_validate_settings (struct audsettings
*as
)
221 invalid
= as
->nchannels
< 1;
222 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
225 case AUDIO_FORMAT_S8
:
226 case AUDIO_FORMAT_U8
:
227 case AUDIO_FORMAT_S16
:
228 case AUDIO_FORMAT_U16
:
229 case AUDIO_FORMAT_S32
:
230 case AUDIO_FORMAT_U32
:
231 case AUDIO_FORMAT_F32
:
238 invalid
|= as
->freq
<= 0;
239 return invalid
? -1 : 0;
242 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
245 bool is_signed
= false, is_float
= false;
248 case AUDIO_FORMAT_S8
:
251 case AUDIO_FORMAT_U8
:
254 case AUDIO_FORMAT_S16
:
257 case AUDIO_FORMAT_U16
:
261 case AUDIO_FORMAT_F32
:
264 case AUDIO_FORMAT_S32
:
267 case AUDIO_FORMAT_U32
:
274 return info
->freq
== as
->freq
275 && info
->nchannels
== as
->nchannels
276 && info
->is_signed
== is_signed
277 && info
->is_float
== is_float
278 && info
->bits
== bits
279 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
282 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
285 bool is_signed
= false, is_float
= false;
288 case AUDIO_FORMAT_S8
:
291 case AUDIO_FORMAT_U8
:
295 case AUDIO_FORMAT_S16
:
298 case AUDIO_FORMAT_U16
:
303 case AUDIO_FORMAT_F32
:
306 case AUDIO_FORMAT_S32
:
309 case AUDIO_FORMAT_U32
:
318 info
->freq
= as
->freq
;
320 info
->is_signed
= is_signed
;
321 info
->is_float
= is_float
;
322 info
->nchannels
= as
->nchannels
;
323 info
->bytes_per_frame
= as
->nchannels
* mul
;
324 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
325 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
328 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
334 if (info
->is_signed
|| info
->is_float
) {
335 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
337 switch (info
->bits
) {
339 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
348 if (info
->swap_endianness
) {
352 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
362 int32_t s
= INT32_MAX
;
364 if (info
->swap_endianness
) {
368 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
375 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
385 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
386 struct audsettings
*as
)
388 CaptureVoiceOut
*cap
;
390 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
391 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
398 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
400 struct capture_callback
*cb
;
403 dolog ("notification %d sent\n", cmd
);
405 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
406 cb
->ops
.notify (cb
->opaque
, cmd
);
410 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
412 if (cap
->hw
.enabled
!= enabled
) {
413 audcnotification_e cmd
;
414 cap
->hw
.enabled
= enabled
;
415 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
416 audio_notify_capture (cap
, cmd
);
420 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
422 HWVoiceOut
*hw
= &cap
->hw
;
426 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
432 audio_capture_maybe_changed (cap
, enabled
);
435 static void audio_detach_capture (HWVoiceOut
*hw
)
437 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
440 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
441 SWVoiceOut
*sw
= &sc
->sw
;
442 CaptureVoiceOut
*cap
= sc
->cap
;
443 int was_active
= sw
->active
;
446 st_rate_stop (sw
->rate
);
450 QLIST_REMOVE (sw
, entries
);
451 QLIST_REMOVE (sc
, entries
);
454 /* We have removed soft voice from the capture:
455 this might have changed the overall status of the capture
456 since this might have been the only active voice */
457 audio_recalc_and_notify_capture (cap
);
463 static int audio_attach_capture (HWVoiceOut
*hw
)
465 AudioState
*s
= hw
->s
;
466 CaptureVoiceOut
*cap
;
468 audio_detach_capture (hw
);
469 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
472 HWVoiceOut
*hw_cap
= &cap
->hw
;
474 sc
= g_malloc0(sizeof(*sc
));
481 sw
->active
= hw
->enabled
;
482 sw
->vol
= nominal_volume
;
483 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
484 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
485 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
487 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
488 hw
, sw
->info
.freq
, sw
->info
.bits
,
490 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
493 audio_capture_maybe_changed (cap
, 1);
500 * Hard voice (capture)
502 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
505 size_t m
= hw
->total_samples_captured
;
507 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
509 m
= MIN (m
, sw
->total_hw_samples_acquired
);
515 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
517 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
518 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
519 dolog("live=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
525 static size_t audio_pcm_hw_conv_in(HWVoiceIn
*hw
, void *pcm_buf
, size_t samples
)
528 STSampleBuffer
*conv_buf
= &hw
->conv_buf
;
531 uint8_t *src
= advance(pcm_buf
, conv
* hw
->info
.bytes_per_frame
);
532 size_t proc
= MIN(samples
, conv_buf
->size
- conv_buf
->pos
);
534 hw
->conv(conv_buf
->buffer
+ conv_buf
->pos
, src
, proc
);
535 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
544 * Soft voice (capture)
546 static void audio_pcm_sw_resample_in(SWVoiceIn
*sw
,
547 size_t frames_in_max
, size_t frames_out_max
,
548 size_t *total_in
, size_t *total_out
)
550 HWVoiceIn
*hw
= sw
->hw
;
551 struct st_sample
*src
, *dst
;
552 size_t live
, rpos
, frames_in
, frames_out
;
554 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
555 rpos
= audio_ring_posb(hw
->conv_buf
.pos
, live
, hw
->conv_buf
.size
);
557 /* resample conv_buf from rpos to end of buffer */
558 src
= hw
->conv_buf
.buffer
+ rpos
;
559 frames_in
= MIN(frames_in_max
, hw
->conv_buf
.size
- rpos
);
560 dst
= sw
->resample_buf
.buffer
;
561 frames_out
= frames_out_max
;
562 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
564 *total_in
= frames_in
;
565 *total_out
= frames_out
;
567 /* resample conv_buf from start of buffer if there are input frames left */
568 if (frames_in_max
- frames_in
&& rpos
== hw
->conv_buf
.size
) {
569 src
= hw
->conv_buf
.buffer
;
570 frames_in
= frames_in_max
- frames_in
;
572 frames_out
= frames_out_max
- frames_out
;
573 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
574 *total_in
+= frames_in
;
575 *total_out
+= frames_out
;
579 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t buf_len
)
581 HWVoiceIn
*hw
= sw
->hw
;
582 size_t live
, frames_out_max
, total_in
, total_out
;
584 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
588 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
589 dolog("live_in=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
593 frames_out_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
,
594 sw
->resample_buf
.size
);
596 audio_pcm_sw_resample_in(sw
, live
, frames_out_max
, &total_in
, &total_out
);
598 if (!hw
->pcm_ops
->volume_in
) {
599 mixeng_volume(sw
->resample_buf
.buffer
, total_out
, &sw
->vol
);
601 sw
->clip(buf
, sw
->resample_buf
.buffer
, total_out
);
603 sw
->total_hw_samples_acquired
+= total_in
;
604 return total_out
* sw
->info
.bytes_per_frame
;
608 * Hard voice (playback)
610 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
616 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
617 if (sw
->active
|| !sw
->empty
) {
618 m
= MIN (m
, sw
->total_hw_samples_mixed
);
627 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
632 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
640 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
641 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
649 static size_t audio_pcm_hw_get_free(HWVoiceOut
*hw
)
651 return (hw
->pcm_ops
->buffer_get_free
? hw
->pcm_ops
->buffer_get_free(hw
) :
652 INT_MAX
) / hw
->info
.bytes_per_frame
;
655 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
658 size_t pos
= hw
->mix_buf
.pos
;
661 st_sample
*src
= hw
->mix_buf
.buffer
+ pos
;
662 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
663 size_t samples_till_end_of_buf
= hw
->mix_buf
.size
- pos
;
664 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
666 hw
->clip(dst
, src
, samples_to_clip
);
668 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
.size
;
669 len
-= samples_to_clip
;
670 clipped
+= samples_to_clip
;
675 * Soft voice (playback)
677 static void audio_pcm_sw_resample_out(SWVoiceOut
*sw
,
678 size_t frames_in_max
, size_t frames_out_max
,
679 size_t *total_in
, size_t *total_out
)
681 HWVoiceOut
*hw
= sw
->hw
;
682 struct st_sample
*src
, *dst
;
683 size_t live
, wpos
, frames_in
, frames_out
;
685 live
= sw
->total_hw_samples_mixed
;
686 wpos
= (hw
->mix_buf
.pos
+ live
) % hw
->mix_buf
.size
;
688 /* write to mix_buf from wpos to end of buffer */
689 src
= sw
->resample_buf
.buffer
;
690 frames_in
= frames_in_max
;
691 dst
= hw
->mix_buf
.buffer
+ wpos
;
692 frames_out
= MIN(frames_out_max
, hw
->mix_buf
.size
- wpos
);
693 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
695 *total_in
= frames_in
;
696 *total_out
= frames_out
;
698 /* write to mix_buf from start of buffer if there are input frames left */
699 if (frames_in_max
- frames_in
> 0 && wpos
== hw
->mix_buf
.size
) {
701 frames_in
= frames_in_max
- frames_in
;
702 dst
= hw
->mix_buf
.buffer
;
703 frames_out
= frames_out_max
- frames_out
;
704 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
705 *total_in
+= frames_in
;
706 *total_out
+= frames_out
;
710 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t buf_len
)
712 HWVoiceOut
*hw
= sw
->hw
;
713 size_t live
, dead
, hw_free
, sw_max
, fe_max
;
714 size_t frames_in_max
, frames_out_max
, total_in
, total_out
;
716 live
= sw
->total_hw_samples_mixed
;
717 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
718 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
722 if (live
== hw
->mix_buf
.size
) {
724 dolog ("%s is full %zu\n", sw
->name
, live
);
729 dead
= hw
->mix_buf
.size
- live
;
730 hw_free
= audio_pcm_hw_get_free(hw
);
731 hw_free
= hw_free
> live
? hw_free
- live
: 0;
732 frames_out_max
= MIN(dead
, hw_free
);
733 sw_max
= st_rate_frames_in(sw
->rate
, frames_out_max
);
734 fe_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
+ sw
->resample_buf
.pos
,
735 sw
->resample_buf
.size
);
736 frames_in_max
= MIN(sw_max
, fe_max
);
738 if (!frames_in_max
) {
742 if (frames_in_max
> sw
->resample_buf
.pos
) {
743 sw
->conv(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
744 buf
, frames_in_max
- sw
->resample_buf
.pos
);
745 if (!sw
->hw
->pcm_ops
->volume_out
) {
746 mixeng_volume(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
747 frames_in_max
- sw
->resample_buf
.pos
, &sw
->vol
);
751 audio_pcm_sw_resample_out(sw
, frames_in_max
, frames_out_max
,
752 &total_in
, &total_out
);
754 sw
->total_hw_samples_mixed
+= total_out
;
755 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
758 * Upsampling may leave one audio frame in the resample buffer. Decrement
759 * total_in by one if there was a leftover frame from the previous resample
760 * pass in the resample buffer. Increment total_in by one if the current
761 * resample pass left one frame in the resample buffer.
763 if (frames_in_max
- total_in
== 1) {
764 /* copy one leftover audio frame to the beginning of the buffer */
765 *sw
->resample_buf
.buffer
= *(sw
->resample_buf
.buffer
+ total_in
);
766 total_in
+= 1 - sw
->resample_buf
.pos
;
767 sw
->resample_buf
.pos
= 1;
768 } else if (total_in
>= sw
->resample_buf
.pos
) {
769 total_in
-= sw
->resample_buf
.pos
;
770 sw
->resample_buf
.pos
= 0;
775 "%s: write size %zu written %zu total mixed %zu\n",
777 buf_len
/ sw
->info
.bytes_per_frame
,
779 sw
->total_hw_samples_mixed
783 return total_in
* sw
->info
.bytes_per_frame
;
787 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
789 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
790 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
796 #include "audio_template.h"
798 #include "audio_template.h"
803 static int audio_is_timer_needed(AudioState
*s
)
805 HWVoiceIn
*hwi
= NULL
;
806 HWVoiceOut
*hwo
= NULL
;
808 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
809 if (!hwo
->poll_mode
) {
813 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
814 if (!hwi
->poll_mode
) {
821 static void audio_reset_timer (AudioState
*s
)
823 if (audio_is_timer_needed(s
)) {
824 timer_mod_anticipate_ns(s
->ts
,
825 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
826 if (!s
->timer_running
) {
827 s
->timer_running
= true;
828 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
829 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
833 if (s
->timer_running
) {
834 s
->timer_running
= false;
835 trace_audio_timer_stop();
840 static void audio_timer (void *opaque
)
843 AudioState
*s
= opaque
;
845 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
846 diff
= now
- s
->timer_last
;
847 if (diff
> s
->period_ticks
* 3 / 2) {
848 trace_audio_timer_delayed(diff
/ SCALE_MS
);
852 audio_run(s
, "timer");
853 audio_reset_timer(s
);
859 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
864 /* XXX: Consider options */
870 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
874 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
875 return audio_pcm_sw_write(sw
, buf
, size
);
877 return hw
->pcm_ops
->write(hw
, buf
, size
);
881 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
886 /* XXX: Consider options */
892 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
896 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
897 return audio_pcm_sw_read(sw
, buf
, size
);
899 return hw
->pcm_ops
->read(hw
, buf
, size
);
903 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
905 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
908 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
917 if (sw
->active
!= on
) {
918 AudioState
*s
= sw
->s
;
923 hw
->pending_disable
= 0;
927 if (hw
->pcm_ops
->enable_out
) {
928 hw
->pcm_ops
->enable_out(hw
, true);
930 audio_reset_timer (s
);
937 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
938 temp_sw
= temp_sw
->entries
.le_next
) {
939 nb_active
+= temp_sw
->active
!= 0;
942 hw
->pending_disable
= nb_active
== 1;
946 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
947 sc
->sw
.active
= hw
->enabled
;
949 audio_capture_maybe_changed (sc
->cap
, 1);
956 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
965 if (sw
->active
!= on
) {
966 AudioState
*s
= sw
->s
;
973 if (hw
->pcm_ops
->enable_in
) {
974 hw
->pcm_ops
->enable_in(hw
, true);
976 audio_reset_timer (s
);
979 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
984 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
985 temp_sw
= temp_sw
->entries
.le_next
) {
986 nb_active
+= temp_sw
->active
!= 0;
989 if (nb_active
== 1) {
991 if (hw
->pcm_ops
->enable_in
) {
992 hw
->pcm_ops
->enable_in(hw
, false);
1001 static size_t audio_get_avail (SWVoiceIn
*sw
)
1009 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1010 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
.size
)) {
1011 dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live
,
1012 sw
->hw
->conv_buf
.size
);
1017 "%s: get_avail live %zu frontend frames %u\n",
1019 live
, st_rate_frames_out(sw
->rate
, live
)
1025 static size_t audio_get_free(SWVoiceOut
*sw
)
1033 live
= sw
->total_hw_samples_mixed
;
1035 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
.size
)) {
1036 dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live
,
1037 sw
->hw
->mix_buf
.size
);
1041 dead
= sw
->hw
->mix_buf
.size
- live
;
1044 dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
1045 SW_NAME(sw
), live
, dead
, st_rate_frames_in(sw
->rate
, dead
));
1051 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1059 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1060 SWVoiceOut
*sw
= &sc
->sw
;
1061 size_t rpos2
= rpos
;
1065 size_t till_end_of_hw
= hw
->mix_buf
.size
- rpos2
;
1066 size_t to_read
= MIN(till_end_of_hw
, n
);
1067 size_t live
, frames_in
, frames_out
;
1069 sw
->resample_buf
.buffer
= hw
->mix_buf
.buffer
+ rpos2
;
1070 sw
->resample_buf
.size
= to_read
;
1071 live
= sw
->total_hw_samples_mixed
;
1073 audio_pcm_sw_resample_out(sw
,
1074 to_read
, sw
->hw
->mix_buf
.size
- live
,
1075 &frames_in
, &frames_out
);
1077 sw
->total_hw_samples_mixed
+= frames_out
;
1078 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1080 if (to_read
- frames_in
) {
1081 dolog("Could not mix %zu frames into a capture "
1082 "buffer, mixed %zu\n",
1083 to_read
, frames_in
);
1087 rpos2
= (rpos2
+ to_read
) % hw
->mix_buf
.size
;
1092 n
= MIN(samples
, hw
->mix_buf
.size
- rpos
);
1093 mixeng_clear(hw
->mix_buf
.buffer
+ rpos
, n
);
1094 mixeng_clear(hw
->mix_buf
.buffer
, samples
- n
);
1097 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1102 size_t size
= live
* hw
->info
.bytes_per_frame
;
1104 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1110 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1112 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1114 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1115 decr
* hw
->info
.bytes_per_frame
) /
1116 hw
->info
.bytes_per_frame
;
1120 hw
->mix_buf
.pos
= (hw
->mix_buf
.pos
+ proc
) % hw
->mix_buf
.size
;
1122 if (proc
== 0 || proc
< decr
) {
1127 if (hw
->pcm_ops
->run_buffer_out
) {
1128 hw
->pcm_ops
->run_buffer_out(hw
);
1134 static void audio_run_out (AudioState
*s
)
1136 HWVoiceOut
*hw
= NULL
;
1139 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1140 size_t played
, live
, prev_rpos
;
1141 size_t hw_free
= audio_pcm_hw_get_free(hw
);
1144 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1145 /* there is exactly 1 sw for each hw with no mixeng */
1146 sw
= hw
->sw_head
.lh_first
;
1148 if (hw
->pending_disable
) {
1150 hw
->pending_disable
= 0;
1151 if (hw
->pcm_ops
->enable_out
) {
1152 hw
->pcm_ops
->enable_out(hw
, false);
1157 sw
->callback
.fn(sw
->callback
.opaque
,
1158 hw_free
* sw
->info
.bytes_per_frame
);
1161 if (hw
->pcm_ops
->run_buffer_out
) {
1162 hw
->pcm_ops
->run_buffer_out(hw
);
1168 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1170 size_t sw_free
= audio_get_free(sw
);
1173 if (hw_free
> sw
->total_hw_samples_mixed
) {
1174 free
= st_rate_frames_in(sw
->rate
,
1175 MIN(sw_free
, hw_free
- sw
->total_hw_samples_mixed
));
1179 if (free
> sw
->resample_buf
.pos
) {
1180 free
= MIN(free
, sw
->resample_buf
.size
)
1181 - sw
->resample_buf
.pos
;
1182 sw
->callback
.fn(sw
->callback
.opaque
,
1183 free
* sw
->info
.bytes_per_frame
);
1188 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1193 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
1194 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
1198 if (hw
->pending_disable
&& !nb_live
) {
1201 dolog ("Disabling voice\n");
1204 hw
->pending_disable
= 0;
1205 if (hw
->pcm_ops
->enable_out
) {
1206 hw
->pcm_ops
->enable_out(hw
, false);
1208 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1210 audio_recalc_and_notify_capture (sc
->cap
);
1216 if (hw
->pcm_ops
->run_buffer_out
) {
1217 hw
->pcm_ops
->run_buffer_out(hw
);
1222 prev_rpos
= hw
->mix_buf
.pos
;
1223 played
= audio_pcm_hw_run_out(hw
, live
);
1224 replay_audio_out(&played
);
1225 if (audio_bug(__func__
, hw
->mix_buf
.pos
>= hw
->mix_buf
.size
)) {
1226 dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
1227 hw
->mix_buf
.pos
, hw
->mix_buf
.size
, played
);
1228 hw
->mix_buf
.pos
= 0;
1232 dolog("played=%zu\n", played
);
1236 hw
->ts_helper
+= played
;
1237 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1240 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1241 if (!sw
->active
&& sw
->empty
) {
1245 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1246 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1247 played
, sw
->total_hw_samples_mixed
);
1248 played
= sw
->total_hw_samples_mixed
;
1251 sw
->total_hw_samples_mixed
-= played
;
1253 if (!sw
->total_hw_samples_mixed
) {
1260 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1264 if (hw
->pcm_ops
->run_buffer_in
) {
1265 hw
->pcm_ops
->run_buffer_in(hw
);
1270 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1271 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1273 assert(size
% hw
->info
.bytes_per_frame
== 0);
1278 proc
= audio_pcm_hw_conv_in(hw
, buf
, size
/ hw
->info
.bytes_per_frame
);
1282 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1288 static void audio_run_in (AudioState
*s
)
1290 HWVoiceIn
*hw
= NULL
;
1292 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1293 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1294 /* there is exactly 1 sw for each hw with no mixeng */
1295 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1297 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1303 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1305 size_t captured
= 0, min
;
1307 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1308 captured
= audio_pcm_hw_run_in(
1309 hw
, hw
->conv_buf
.size
- audio_pcm_hw_get_live_in(hw
));
1311 replay_audio_in(&captured
, hw
->conv_buf
.buffer
, &hw
->conv_buf
.pos
,
1314 min
= audio_pcm_hw_find_min_in (hw
);
1315 hw
->total_samples_captured
+= captured
- min
;
1316 hw
->ts_helper
+= captured
;
1318 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1319 sw
->total_hw_samples_acquired
-= min
;
1322 size_t sw_avail
= audio_get_avail(sw
);
1325 avail
= st_rate_frames_out(sw
->rate
, sw_avail
);
1327 avail
= MIN(avail
, sw
->resample_buf
.size
);
1328 sw
->callback
.fn(sw
->callback
.opaque
,
1329 avail
* sw
->info
.bytes_per_frame
);
1336 static void audio_run_capture (AudioState
*s
)
1338 CaptureVoiceOut
*cap
;
1340 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1341 size_t live
, rpos
, captured
;
1342 HWVoiceOut
*hw
= &cap
->hw
;
1345 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1346 rpos
= hw
->mix_buf
.pos
;
1348 size_t left
= hw
->mix_buf
.size
- rpos
;
1349 size_t to_capture
= MIN(live
, left
);
1350 struct st_sample
*src
;
1351 struct capture_callback
*cb
;
1353 src
= hw
->mix_buf
.buffer
+ rpos
;
1354 hw
->clip (cap
->buf
, src
, to_capture
);
1355 mixeng_clear (src
, to_capture
);
1357 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1358 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1359 to_capture
* hw
->info
.bytes_per_frame
);
1361 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
.size
;
1364 hw
->mix_buf
.pos
= rpos
;
1366 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1367 if (!sw
->active
&& sw
->empty
) {
1371 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1372 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1373 captured
, sw
->total_hw_samples_mixed
);
1374 captured
= sw
->total_hw_samples_mixed
;
1377 sw
->total_hw_samples_mixed
-= captured
;
1378 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1383 void audio_run(AudioState
*s
, const char *msg
)
1387 audio_run_capture(s
);
1391 static double prevtime
;
1395 if (gettimeofday (&tv
, NULL
)) {
1396 perror ("audio_run: gettimeofday");
1400 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1401 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1402 prevtime
= currtime
;
1407 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1409 if (unlikely(!hw
->buf_emul
)) {
1410 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1411 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1412 hw
->pos_emul
= hw
->pending_emul
= 0;
1415 while (hw
->pending_emul
< hw
->size_emul
) {
1416 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1417 hw
->size_emul
- hw
->pending_emul
);
1418 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1420 hw
->pending_emul
+= read
;
1421 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1422 if (read
< read_len
) {
1428 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1432 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1433 assert(start
< hw
->size_emul
);
1435 *size
= MIN(*size
, hw
->pending_emul
);
1436 *size
= MIN(*size
, hw
->size_emul
- start
);
1437 return hw
->buf_emul
+ start
;
1440 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1442 assert(size
<= hw
->pending_emul
);
1443 hw
->pending_emul
-= size
;
1446 size_t audio_generic_buffer_get_free(HWVoiceOut
*hw
)
1449 return hw
->size_emul
- hw
->pending_emul
;
1451 return hw
->samples
* hw
->info
.bytes_per_frame
;
1455 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1457 while (hw
->pending_emul
) {
1458 size_t write_len
, written
, start
;
1460 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1461 assert(start
< hw
->size_emul
);
1463 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1465 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1466 hw
->pending_emul
-= written
;
1468 if (written
< write_len
) {
1474 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1476 if (unlikely(!hw
->buf_emul
)) {
1477 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1478 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1479 hw
->pos_emul
= hw
->pending_emul
= 0;
1482 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1483 hw
->size_emul
- hw
->pos_emul
);
1484 return hw
->buf_emul
+ hw
->pos_emul
;
1487 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1489 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1490 size
+ hw
->pending_emul
<= hw
->size_emul
);
1492 hw
->pending_emul
+= size
;
1493 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1498 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1502 if (hw
->pcm_ops
->buffer_get_free
) {
1503 size_t free
= hw
->pcm_ops
->buffer_get_free(hw
);
1505 size
= MIN(size
, free
);
1508 while (total
< size
) {
1509 size_t dst_size
= size
- total
;
1510 size_t copy_size
, proc
;
1511 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1513 if (dst_size
== 0) {
1517 copy_size
= MIN(size
- total
, dst_size
);
1519 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1521 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1524 if (proc
== 0 || proc
< copy_size
) {
1532 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1536 if (hw
->pcm_ops
->run_buffer_in
) {
1537 hw
->pcm_ops
->run_buffer_in(hw
);
1540 while (total
< size
) {
1541 size_t src_size
= size
- total
;
1542 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1544 if (src_size
== 0) {
1548 memcpy((char *)buf
+ total
, src
, src_size
);
1549 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1556 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1557 bool msg
, Audiodev
*dev
)
1559 Error
*local_err
= NULL
;
1561 s
->drv_opaque
= drv
->init(dev
, &local_err
);
1563 if (s
->drv_opaque
) {
1564 if (!drv
->pcm_ops
->get_buffer_in
) {
1565 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1566 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1568 if (!drv
->pcm_ops
->get_buffer_out
) {
1569 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1570 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1573 audio_init_nb_voices_out(s
, drv
, 1);
1574 audio_init_nb_voices_in(s
, drv
, 0);
1579 error_free(local_err
);
1580 } else if (local_err
) {
1581 error_report_err(local_err
);
1583 error_report("Could not init `%s' audio driver", drv
->name
);
1589 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1592 AudioState
*s
= opaque
;
1593 HWVoiceOut
*hwo
= NULL
;
1594 HWVoiceIn
*hwi
= NULL
;
1596 s
->vm_running
= running
;
1597 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1598 if (hwo
->pcm_ops
->enable_out
) {
1599 hwo
->pcm_ops
->enable_out(hwo
, running
);
1603 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1604 if (hwi
->pcm_ops
->enable_in
) {
1605 hwi
->pcm_ops
->enable_in(hwi
, running
);
1608 audio_reset_timer (s
);
1611 static void free_audio_state(AudioState
*s
)
1613 HWVoiceOut
*hwo
, *hwon
;
1614 HWVoiceIn
*hwi
, *hwin
;
1616 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1619 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1620 hwo
->pcm_ops
->enable_out(hwo
, false);
1622 hwo
->pcm_ops
->fini_out (hwo
);
1624 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1625 CaptureVoiceOut
*cap
= sc
->cap
;
1626 struct capture_callback
*cb
;
1628 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1629 cb
->ops
.destroy (cb
->opaque
);
1632 QLIST_REMOVE(hwo
, entries
);
1635 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1636 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1637 hwi
->pcm_ops
->enable_in(hwi
, false);
1639 hwi
->pcm_ops
->fini_in (hwi
);
1640 QLIST_REMOVE(hwi
, entries
);
1644 s
->drv
->fini (s
->drv_opaque
);
1649 qapi_free_Audiodev(s
->dev
);
1661 void audio_cleanup(void)
1663 while (!QTAILQ_EMPTY(&audio_states
)) {
1664 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1665 QTAILQ_REMOVE(&audio_states
, s
, list
);
1666 free_audio_state(s
);
1670 static bool vmstate_audio_needed(void *opaque
)
1673 * Never needed, this vmstate only exists in case
1674 * an old qemu sends it to us.
1679 static const VMStateDescription vmstate_audio
= {
1682 .minimum_version_id
= 1,
1683 .needed
= vmstate_audio_needed
,
1684 .fields
= (VMStateField
[]) {
1685 VMSTATE_END_OF_LIST()
1689 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1691 static AudiodevListEntry
*audiodev_find(
1692 AudiodevListHead
*head
, const char *drvname
)
1694 AudiodevListEntry
*e
;
1695 QSIMPLEQ_FOREACH(e
, head
, next
) {
1696 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1705 * if we have dev, this function was called because of an -audiodev argument =>
1706 * initialize a new state with it
1707 * if dev == NULL => legacy implicit initialization, return the already created
1708 * state or create a new one
1710 static AudioState
*audio_init(Audiodev
*dev
)
1712 static bool atexit_registered
;
1715 const char *drvname
;
1716 VMChangeStateEntry
*vmse
;
1718 struct audio_driver
*driver
;
1719 /* silence gcc warning about uninitialized variable */
1720 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1724 * When using spice allow the spice audio driver being picked
1727 * Temporary hack. Using audio devices without explicit
1728 * audiodev= property is already deprecated. Same goes for
1729 * the -soundhw switch. Once this support gets finally
1730 * removed we can also drop the concept of a default audio
1731 * backend and this can go away.
1733 driver
= audio_driver_lookup("spice");
1735 driver
->can_be_default
= 1;
1739 s
= g_new0(AudioState
, 1);
1741 QLIST_INIT (&s
->hw_head_out
);
1742 QLIST_INIT (&s
->hw_head_in
);
1743 QLIST_INIT (&s
->cap_head
);
1744 if (!atexit_registered
) {
1745 atexit(audio_cleanup
);
1746 atexit_registered
= true;
1749 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1752 /* -audiodev option */
1754 drvname
= AudiodevDriver_str(dev
->driver
);
1755 driver
= audio_driver_lookup(drvname
);
1757 done
= !audio_driver_init(s
, driver
, true, dev
);
1759 dolog ("Unknown audio driver `%s'\n", drvname
);
1762 free_audio_state(s
);
1766 /* legacy implicit initialization */
1767 head
= audio_handle_legacy_opts();
1769 * In case of legacy initialization, all Audiodevs in the list will have
1770 * the same configuration (except the driver), so it doesn't matter which
1771 * one we chose. We need an Audiodev to set up AudioState before we can
1772 * init a driver. Also note that dev at this point is still in the
1775 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1776 audio_validate_opts(dev
, &error_abort
);
1778 for (i
= 0; audio_prio_list
[i
]; i
++) {
1779 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1780 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1783 s
->dev
= dev
= e
->dev
;
1784 audio_validate_opts(dev
, &error_abort
);
1785 done
= !audio_driver_init(s
, driver
, false, dev
);
1793 audio_free_audiodev_list(&head
);
1797 driver
= audio_driver_lookup("none");
1798 done
= !audio_driver_init(s
, driver
, false, dev
);
1800 dolog("warning: Using timer based audio emulation\n");
1803 if (dev
->timer_period
<= 0) {
1804 s
->period_ticks
= 1;
1806 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1809 vmse
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1811 dolog ("warning: Could not register change state handler\n"
1812 "(Audio can continue looping even after stopping the VM)\n");
1815 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1816 QLIST_INIT (&s
->card_head
);
1817 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1821 void audio_free_audiodev_list(AudiodevListHead
*head
)
1823 AudiodevListEntry
*e
;
1824 while ((e
= QSIMPLEQ_FIRST(head
))) {
1825 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1826 qapi_free_Audiodev(e
->dev
);
1831 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1834 if (!QTAILQ_EMPTY(&audio_states
)) {
1835 if (!legacy_config
) {
1836 dolog("Device %s: audiodev default parameter is deprecated, please "
1837 "specify audiodev=%s\n", name
,
1838 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1840 card
->state
= QTAILQ_FIRST(&audio_states
);
1842 card
->state
= audio_init(NULL
);
1846 card
->name
= g_strdup (name
);
1847 memset (&card
->entries
, 0, sizeof (card
->entries
));
1848 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1851 void AUD_remove_card (QEMUSoundCard
*card
)
1853 QLIST_REMOVE (card
, entries
);
1854 g_free (card
->name
);
1857 static struct audio_pcm_ops capture_pcm_ops
;
1859 CaptureVoiceOut
*AUD_add_capture(
1861 struct audsettings
*as
,
1862 struct audio_capture_ops
*ops
,
1866 CaptureVoiceOut
*cap
;
1867 struct capture_callback
*cb
;
1870 error_report("Capturing without setting an audiodev is not supported");
1874 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1875 dolog("Can't capture with mixeng disabled\n");
1879 if (audio_validate_settings (as
)) {
1880 dolog ("Invalid settings were passed when trying to add capture\n");
1881 audio_print_settings (as
);
1885 cb
= g_malloc0(sizeof(*cb
));
1887 cb
->opaque
= cb_opaque
;
1889 cap
= audio_pcm_capture_find_specific(s
, as
);
1891 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1895 cap
= g_malloc0(sizeof(*cap
));
1899 hw
->pcm_ops
= &capture_pcm_ops
;
1900 QLIST_INIT (&hw
->sw_head
);
1901 QLIST_INIT (&cap
->cb_head
);
1903 /* XXX find a more elegant way */
1904 hw
->samples
= 4096 * 4;
1905 audio_pcm_hw_alloc_resources_out(hw
);
1907 audio_pcm_init_info (&hw
->info
, as
);
1909 cap
->buf
= g_malloc0_n(hw
->mix_buf
.size
, hw
->info
.bytes_per_frame
);
1911 if (hw
->info
.is_float
) {
1912 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1914 hw
->clip
= mixeng_clip
1915 [hw
->info
.nchannels
== 2]
1916 [hw
->info
.is_signed
]
1917 [hw
->info
.swap_endianness
]
1918 [audio_bits_to_index(hw
->info
.bits
)];
1921 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1922 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1924 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1925 audio_attach_capture (hw
);
1932 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1934 struct capture_callback
*cb
;
1936 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1937 if (cb
->opaque
== cb_opaque
) {
1938 cb
->ops
.destroy (cb_opaque
);
1939 QLIST_REMOVE (cb
, entries
);
1942 if (!cap
->cb_head
.lh_first
) {
1943 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1946 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1947 #ifdef DEBUG_CAPTURE
1948 dolog ("freeing %s\n", sw
->name
);
1951 sw1
= sw
->entries
.le_next
;
1953 st_rate_stop (sw
->rate
);
1956 QLIST_REMOVE (sw
, entries
);
1957 QLIST_REMOVE (sc
, entries
);
1961 QLIST_REMOVE (cap
, entries
);
1962 g_free(cap
->hw
.mix_buf
.buffer
);
1971 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1973 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1974 audio_set_volume_out(sw
, &vol
);
1977 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1980 HWVoiceOut
*hw
= sw
->hw
;
1982 sw
->vol
.mute
= vol
->mute
;
1983 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1984 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1987 if (hw
->pcm_ops
->volume_out
) {
1988 hw
->pcm_ops
->volume_out(hw
, vol
);
1993 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1995 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1996 audio_set_volume_in(sw
, &vol
);
1999 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
2002 HWVoiceIn
*hw
= sw
->hw
;
2004 sw
->vol
.mute
= vol
->mute
;
2005 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
2006 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
2009 if (hw
->pcm_ops
->volume_in
) {
2010 hw
->pcm_ops
->volume_in(hw
, vol
);
2015 void audio_create_pdos(Audiodev
*dev
)
2017 switch (dev
->driver
) {
2018 #define CASE(DRIVER, driver, pdo_name) \
2019 case AUDIODEV_DRIVER_##DRIVER: \
2020 if (!dev->u.driver.in) { \
2021 dev->u.driver.in = g_malloc0( \
2022 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2024 if (!dev->u.driver.out) { \
2025 dev->u.driver.out = g_malloc0( \
2026 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2031 #ifdef CONFIG_AUDIO_ALSA
2032 CASE(ALSA
, alsa
, Alsa
);
2034 #ifdef CONFIG_AUDIO_COREAUDIO
2035 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2037 #ifdef CONFIG_DBUS_DISPLAY
2040 #ifdef CONFIG_AUDIO_DSOUND
2041 CASE(DSOUND
, dsound
, );
2043 #ifdef CONFIG_AUDIO_JACK
2044 CASE(JACK
, jack
, Jack
);
2046 #ifdef CONFIG_AUDIO_OSS
2047 CASE(OSS
, oss
, Oss
);
2049 #ifdef CONFIG_AUDIO_PA
2052 #ifdef CONFIG_AUDIO_PIPEWIRE
2053 CASE(PIPEWIRE
, pipewire
, Pipewire
);
2055 #ifdef CONFIG_AUDIO_SDL
2056 CASE(SDL
, sdl
, Sdl
);
2058 #ifdef CONFIG_AUDIO_SNDIO
2059 CASE(SNDIO
, sndio
, );
2062 CASE(SPICE
, spice
, );
2066 case AUDIODEV_DRIVER__MAX
:
2071 static void audio_validate_per_direction_opts(
2072 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2074 if (!pdo
->has_mixing_engine
) {
2075 pdo
->has_mixing_engine
= true;
2076 pdo
->mixing_engine
= true;
2078 if (!pdo
->has_fixed_settings
) {
2079 pdo
->has_fixed_settings
= true;
2080 pdo
->fixed_settings
= pdo
->mixing_engine
;
2082 if (!pdo
->fixed_settings
&&
2083 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2085 "You can't use frequency, channels or format with fixed-settings=off");
2088 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2089 error_setg(errp
, "You can't use fixed-settings without mixeng");
2093 if (!pdo
->has_frequency
) {
2094 pdo
->has_frequency
= true;
2095 pdo
->frequency
= 44100;
2097 if (!pdo
->has_channels
) {
2098 pdo
->has_channels
= true;
2101 if (!pdo
->has_voices
) {
2102 pdo
->has_voices
= true;
2103 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2105 if (!pdo
->has_format
) {
2106 pdo
->has_format
= true;
2107 pdo
->format
= AUDIO_FORMAT_S16
;
2111 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2115 audio_create_pdos(dev
);
2117 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2119 error_propagate(errp
, err
);
2123 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2125 error_propagate(errp
, err
);
2129 if (!dev
->has_timer_period
) {
2130 dev
->has_timer_period
= true;
2131 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2135 void audio_help(void)
2139 printf("Available audio drivers:\n");
2141 for (i
= 0; i
< AUDIODEV_DRIVER__MAX
; i
++) {
2142 audio_driver
*driver
= audio_driver_lookup(AudiodevDriver_str(i
));
2144 printf("%s\n", driver
->name
);
2149 void audio_parse_option(const char *opt
)
2151 Audiodev
*dev
= NULL
;
2153 if (is_help_option(opt
)) {
2157 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2158 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2164 void audio_define(Audiodev
*dev
)
2166 AudiodevListEntry
*e
;
2168 audio_validate_opts(dev
, &error_fatal
);
2170 e
= g_new0(AudiodevListEntry
, 1);
2172 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2173 legacy_config
= false;
2176 bool audio_init_audiodevs(void)
2178 AudiodevListEntry
*e
;
2180 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2181 if (!audio_init(e
->dev
)) {
2189 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2191 return (audsettings
) {
2192 .freq
= pdo
->frequency
,
2193 .nchannels
= pdo
->channels
,
2195 .endianness
= AUDIO_HOST_ENDIANNESS
,
2199 int audioformat_bytes_per_sample(AudioFormat fmt
)
2202 case AUDIO_FORMAT_U8
:
2203 case AUDIO_FORMAT_S8
:
2206 case AUDIO_FORMAT_U16
:
2207 case AUDIO_FORMAT_S16
:
2210 case AUDIO_FORMAT_U32
:
2211 case AUDIO_FORMAT_S32
:
2212 case AUDIO_FORMAT_F32
:
2215 case AUDIO_FORMAT__MAX
:
2222 /* frames = freq * usec / 1e6 */
2223 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2224 audsettings
*as
, int def_usecs
)
2226 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2227 return (as
->freq
* usecs
+ 500000) / 1000000;
2230 /* samples = channels * frames = channels * freq * usec / 1e6 */
2231 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2232 audsettings
*as
, int def_usecs
)
2234 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2238 * bytes = bytes_per_sample * samples =
2239 * bytes_per_sample * channels * freq * usec / 1e6
2241 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2242 audsettings
*as
, int def_usecs
)
2244 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2245 audioformat_bytes_per_sample(as
->fmt
);
2248 AudioState
*audio_state_by_name(const char *name
, Error
**errp
)
2251 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2253 if (strcmp(name
, s
->dev
->id
) == 0) {
2257 error_setg(errp
, "audiodev '%s' not found", name
);
2261 const char *audio_get_id(QEMUSoundCard
*card
)
2264 assert(card
->state
->dev
);
2265 return card
->state
->dev
->id
;
2271 const char *audio_application_name(void)
2273 const char *vm_name
;
2275 vm_name
= qemu_get_vm_name();
2276 return vm_name
? vm_name
: "qemu";
2279 void audio_rate_start(RateCtl
*rate
)
2281 memset(rate
, 0, sizeof(RateCtl
));
2282 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2285 size_t audio_rate_peek_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
)
2292 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2293 ticks
= now
- rate
->start_ticks
;
2294 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2295 frames
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2296 if (frames
< 0 || frames
> 65536) {
2297 AUD_log(NULL
, "Resetting rate control (%" PRId64
" frames)\n", frames
);
2298 audio_rate_start(rate
);
2302 return frames
* info
->bytes_per_frame
;
2305 void audio_rate_add_bytes(RateCtl
*rate
, size_t bytes_used
)
2307 rate
->bytes_sent
+= bytes_used
;
2310 size_t audio_rate_get_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
,
2315 bytes
= audio_rate_peek_bytes(rate
, info
);
2316 bytes
= MIN(bytes
, bytes_avail
);
2317 audio_rate_add_bytes(rate
, bytes
);
2322 AudiodevList
*qmp_query_audiodevs(Error
**errp
)
2324 AudiodevList
*ret
= NULL
;
2325 AudiodevListEntry
*e
;
2326 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2327 QAPI_LIST_PREPEND(ret
, QAPI_CLONE(Audiodev
, e
->dev
));