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"
37 #include "qemu/module.h"
38 #include "qemu/help_option.h"
39 #include "sysemu/sysemu.h"
40 #include "sysemu/replay.h"
41 #include "sysemu/runstate.h"
42 #include "ui/qemu-spice.h"
45 #define AUDIO_CAP "audio"
46 #include "audio_int.h"
48 /* #define DEBUG_LIVE */
49 /* #define DEBUG_OUT */
50 /* #define DEBUG_CAPTURE */
51 /* #define DEBUG_POLL */
53 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
56 /* Order of CONFIG_AUDIO_DRIVERS is import.
57 The 1st one is the one used by default, that is the reason
58 that we generate the list.
60 const char *audio_prio_list
[] = {
68 static QLIST_HEAD(, audio_driver
) audio_drivers
;
69 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
71 void audio_driver_register(audio_driver
*drv
)
73 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
76 audio_driver
*audio_driver_lookup(const char *name
)
78 struct audio_driver
*d
;
79 Error
*local_err
= NULL
;
82 QLIST_FOREACH(d
, &audio_drivers
, next
) {
83 if (strcmp(name
, d
->name
) == 0) {
87 rv
= audio_module_load(name
, &local_err
);
89 QLIST_FOREACH(d
, &audio_drivers
, next
) {
90 if (strcmp(name
, d
->name
) == 0) {
95 error_report_err(local_err
);
100 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
101 QTAILQ_HEAD_INITIALIZER(audio_states
);
103 const struct mixeng_volume nominal_volume
= {
114 static bool legacy_config
= true;
116 int audio_bug (const char *funcname
, int cond
)
121 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
124 AUD_log (NULL
, "Save all your work and restart without audio\n");
125 AUD_log (NULL
, "I am sorry\n");
127 AUD_log (NULL
, "Context:\n");
133 static inline int audio_bits_to_index (int bits
)
146 audio_bug ("bits_to_index", 1);
147 AUD_log (NULL
, "invalid bits %d\n", bits
);
152 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
155 fprintf(stderr
, "%s: ", cap
);
158 vfprintf(stderr
, fmt
, ap
);
161 void AUD_log (const char *cap
, const char *fmt
, ...)
166 AUD_vlog (cap
, fmt
, ap
);
170 static void audio_print_settings (struct audsettings
*as
)
172 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
175 case AUDIO_FORMAT_S8
:
176 AUD_log (NULL
, "S8");
178 case AUDIO_FORMAT_U8
:
179 AUD_log (NULL
, "U8");
181 case AUDIO_FORMAT_S16
:
182 AUD_log (NULL
, "S16");
184 case AUDIO_FORMAT_U16
:
185 AUD_log (NULL
, "U16");
187 case AUDIO_FORMAT_S32
:
188 AUD_log (NULL
, "S32");
190 case AUDIO_FORMAT_U32
:
191 AUD_log (NULL
, "U32");
193 case AUDIO_FORMAT_F32
:
194 AUD_log (NULL
, "F32");
197 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
201 AUD_log (NULL
, " endianness=");
202 switch (as
->endianness
) {
204 AUD_log (NULL
, "little");
207 AUD_log (NULL
, "big");
210 AUD_log (NULL
, "invalid");
213 AUD_log (NULL
, "\n");
216 static int audio_validate_settings (struct audsettings
*as
)
220 invalid
= as
->nchannels
< 1;
221 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
224 case AUDIO_FORMAT_S8
:
225 case AUDIO_FORMAT_U8
:
226 case AUDIO_FORMAT_S16
:
227 case AUDIO_FORMAT_U16
:
228 case AUDIO_FORMAT_S32
:
229 case AUDIO_FORMAT_U32
:
230 case AUDIO_FORMAT_F32
:
237 invalid
|= as
->freq
<= 0;
238 return invalid
? -1 : 0;
241 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
244 bool is_signed
= false, is_float
= false;
247 case AUDIO_FORMAT_S8
:
250 case AUDIO_FORMAT_U8
:
253 case AUDIO_FORMAT_S16
:
256 case AUDIO_FORMAT_U16
:
260 case AUDIO_FORMAT_F32
:
263 case AUDIO_FORMAT_S32
:
266 case AUDIO_FORMAT_U32
:
273 return info
->freq
== as
->freq
274 && info
->nchannels
== as
->nchannels
275 && info
->is_signed
== is_signed
276 && info
->is_float
== is_float
277 && info
->bits
== bits
278 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
281 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
284 bool is_signed
= false, is_float
= false;
287 case AUDIO_FORMAT_S8
:
290 case AUDIO_FORMAT_U8
:
294 case AUDIO_FORMAT_S16
:
297 case AUDIO_FORMAT_U16
:
302 case AUDIO_FORMAT_F32
:
305 case AUDIO_FORMAT_S32
:
308 case AUDIO_FORMAT_U32
:
317 info
->freq
= as
->freq
;
319 info
->is_signed
= is_signed
;
320 info
->is_float
= is_float
;
321 info
->nchannels
= as
->nchannels
;
322 info
->bytes_per_frame
= as
->nchannels
* mul
;
323 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
324 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
327 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
333 if (info
->is_signed
|| info
->is_float
) {
334 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
336 switch (info
->bits
) {
338 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
347 if (info
->swap_endianness
) {
351 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
361 int32_t s
= INT32_MAX
;
363 if (info
->swap_endianness
) {
367 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
374 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
384 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
385 struct audsettings
*as
)
387 CaptureVoiceOut
*cap
;
389 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
390 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
397 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
399 struct capture_callback
*cb
;
402 dolog ("notification %d sent\n", cmd
);
404 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
405 cb
->ops
.notify (cb
->opaque
, cmd
);
409 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
411 if (cap
->hw
.enabled
!= enabled
) {
412 audcnotification_e cmd
;
413 cap
->hw
.enabled
= enabled
;
414 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
415 audio_notify_capture (cap
, cmd
);
419 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
421 HWVoiceOut
*hw
= &cap
->hw
;
425 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
431 audio_capture_maybe_changed (cap
, enabled
);
434 static void audio_detach_capture (HWVoiceOut
*hw
)
436 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
439 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
440 SWVoiceOut
*sw
= &sc
->sw
;
441 CaptureVoiceOut
*cap
= sc
->cap
;
442 int was_active
= sw
->active
;
445 st_rate_stop (sw
->rate
);
449 QLIST_REMOVE (sw
, entries
);
450 QLIST_REMOVE (sc
, entries
);
453 /* We have removed soft voice from the capture:
454 this might have changed the overall status of the capture
455 since this might have been the only active voice */
456 audio_recalc_and_notify_capture (cap
);
462 static int audio_attach_capture (HWVoiceOut
*hw
)
464 AudioState
*s
= hw
->s
;
465 CaptureVoiceOut
*cap
;
467 audio_detach_capture (hw
);
468 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
471 HWVoiceOut
*hw_cap
= &cap
->hw
;
473 sc
= g_malloc0(sizeof(*sc
));
480 sw
->active
= hw
->enabled
;
481 sw
->vol
= nominal_volume
;
482 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
483 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
484 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
486 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
487 hw
, sw
->info
.freq
, sw
->info
.bits
,
489 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
492 audio_capture_maybe_changed (cap
, 1);
499 * Hard voice (capture)
501 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
504 size_t m
= hw
->total_samples_captured
;
506 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
508 m
= MIN (m
, sw
->total_hw_samples_acquired
);
514 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
516 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
517 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
518 dolog("live=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
524 static size_t audio_pcm_hw_conv_in(HWVoiceIn
*hw
, void *pcm_buf
, size_t samples
)
527 STSampleBuffer
*conv_buf
= &hw
->conv_buf
;
530 uint8_t *src
= advance(pcm_buf
, conv
* hw
->info
.bytes_per_frame
);
531 size_t proc
= MIN(samples
, conv_buf
->size
- conv_buf
->pos
);
533 hw
->conv(conv_buf
->buffer
+ conv_buf
->pos
, src
, proc
);
534 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
543 * Soft voice (capture)
545 static void audio_pcm_sw_resample_in(SWVoiceIn
*sw
,
546 size_t frames_in_max
, size_t frames_out_max
,
547 size_t *total_in
, size_t *total_out
)
549 HWVoiceIn
*hw
= sw
->hw
;
550 struct st_sample
*src
, *dst
;
551 size_t live
, rpos
, frames_in
, frames_out
;
553 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
554 rpos
= audio_ring_posb(hw
->conv_buf
.pos
, live
, hw
->conv_buf
.size
);
556 /* resample conv_buf from rpos to end of buffer */
557 src
= hw
->conv_buf
.buffer
+ rpos
;
558 frames_in
= MIN(frames_in_max
, hw
->conv_buf
.size
- rpos
);
559 dst
= sw
->resample_buf
.buffer
;
560 frames_out
= frames_out_max
;
561 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
563 *total_in
= frames_in
;
564 *total_out
= frames_out
;
566 /* resample conv_buf from start of buffer if there are input frames left */
567 if (frames_in_max
- frames_in
&& rpos
== hw
->conv_buf
.size
) {
568 src
= hw
->conv_buf
.buffer
;
569 frames_in
= frames_in_max
- frames_in
;
571 frames_out
= frames_out_max
- frames_out
;
572 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
573 *total_in
+= frames_in
;
574 *total_out
+= frames_out
;
578 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t buf_len
)
580 HWVoiceIn
*hw
= sw
->hw
;
581 size_t live
, frames_out_max
, total_in
, total_out
;
583 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
587 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
588 dolog("live_in=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
592 frames_out_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
,
593 sw
->resample_buf
.size
);
595 audio_pcm_sw_resample_in(sw
, live
, frames_out_max
, &total_in
, &total_out
);
597 if (!hw
->pcm_ops
->volume_in
) {
598 mixeng_volume(sw
->resample_buf
.buffer
, total_out
, &sw
->vol
);
600 sw
->clip(buf
, sw
->resample_buf
.buffer
, total_out
);
602 sw
->total_hw_samples_acquired
+= total_in
;
603 return total_out
* sw
->info
.bytes_per_frame
;
607 * Hard voice (playback)
609 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
615 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
616 if (sw
->active
|| !sw
->empty
) {
617 m
= MIN (m
, sw
->total_hw_samples_mixed
);
626 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
631 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
639 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
640 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
648 static size_t audio_pcm_hw_get_free(HWVoiceOut
*hw
)
650 return (hw
->pcm_ops
->buffer_get_free
? hw
->pcm_ops
->buffer_get_free(hw
) :
651 INT_MAX
) / hw
->info
.bytes_per_frame
;
654 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
657 size_t pos
= hw
->mix_buf
.pos
;
660 st_sample
*src
= hw
->mix_buf
.buffer
+ pos
;
661 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
662 size_t samples_till_end_of_buf
= hw
->mix_buf
.size
- pos
;
663 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
665 hw
->clip(dst
, src
, samples_to_clip
);
667 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
.size
;
668 len
-= samples_to_clip
;
669 clipped
+= samples_to_clip
;
674 * Soft voice (playback)
676 static void audio_pcm_sw_resample_out(SWVoiceOut
*sw
,
677 size_t frames_in_max
, size_t frames_out_max
,
678 size_t *total_in
, size_t *total_out
)
680 HWVoiceOut
*hw
= sw
->hw
;
681 struct st_sample
*src
, *dst
;
682 size_t live
, wpos
, frames_in
, frames_out
;
684 live
= sw
->total_hw_samples_mixed
;
685 wpos
= (hw
->mix_buf
.pos
+ live
) % hw
->mix_buf
.size
;
687 /* write to mix_buf from wpos to end of buffer */
688 src
= sw
->resample_buf
.buffer
;
689 frames_in
= frames_in_max
;
690 dst
= hw
->mix_buf
.buffer
+ wpos
;
691 frames_out
= MIN(frames_out_max
, hw
->mix_buf
.size
- wpos
);
692 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
694 *total_in
= frames_in
;
695 *total_out
= frames_out
;
697 /* write to mix_buf from start of buffer if there are input frames left */
698 if (frames_in_max
- frames_in
> 0 && wpos
== hw
->mix_buf
.size
) {
700 frames_in
= frames_in_max
- frames_in
;
701 dst
= hw
->mix_buf
.buffer
;
702 frames_out
= frames_out_max
- frames_out
;
703 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
704 *total_in
+= frames_in
;
705 *total_out
+= frames_out
;
709 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t buf_len
)
711 HWVoiceOut
*hw
= sw
->hw
;
712 size_t live
, dead
, hw_free
, sw_max
, fe_max
;
713 size_t frames_in_max
, frames_out_max
, total_in
, total_out
;
715 live
= sw
->total_hw_samples_mixed
;
716 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
717 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
721 if (live
== hw
->mix_buf
.size
) {
723 dolog ("%s is full %zu\n", sw
->name
, live
);
728 dead
= hw
->mix_buf
.size
- live
;
729 hw_free
= audio_pcm_hw_get_free(hw
);
730 hw_free
= hw_free
> live
? hw_free
- live
: 0;
731 frames_out_max
= MIN(dead
, hw_free
);
732 sw_max
= st_rate_frames_in(sw
->rate
, frames_out_max
);
733 fe_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
+ sw
->resample_buf
.pos
,
734 sw
->resample_buf
.size
);
735 frames_in_max
= MIN(sw_max
, fe_max
);
737 if (!frames_in_max
) {
741 if (frames_in_max
> sw
->resample_buf
.pos
) {
742 sw
->conv(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
743 buf
, frames_in_max
- sw
->resample_buf
.pos
);
744 if (!sw
->hw
->pcm_ops
->volume_out
) {
745 mixeng_volume(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
746 frames_in_max
- sw
->resample_buf
.pos
, &sw
->vol
);
750 audio_pcm_sw_resample_out(sw
, frames_in_max
, frames_out_max
,
751 &total_in
, &total_out
);
753 sw
->total_hw_samples_mixed
+= total_out
;
754 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
757 * Upsampling may leave one audio frame in the resample buffer. Decrement
758 * total_in by one if there was a leftover frame from the previous resample
759 * pass in the resample buffer. Increment total_in by one if the current
760 * resample pass left one frame in the resample buffer.
762 if (frames_in_max
- total_in
== 1) {
763 /* copy one leftover audio frame to the beginning of the buffer */
764 *sw
->resample_buf
.buffer
= *(sw
->resample_buf
.buffer
+ total_in
);
765 total_in
+= 1 - sw
->resample_buf
.pos
;
766 sw
->resample_buf
.pos
= 1;
767 } else if (total_in
>= sw
->resample_buf
.pos
) {
768 total_in
-= sw
->resample_buf
.pos
;
769 sw
->resample_buf
.pos
= 0;
774 "%s: write size %zu written %zu total mixed %zu\n",
776 buf_len
/ sw
->info
.bytes_per_frame
,
778 sw
->total_hw_samples_mixed
782 return total_in
* sw
->info
.bytes_per_frame
;
786 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
788 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
789 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
795 #include "audio_template.h"
797 #include "audio_template.h"
802 static int audio_is_timer_needed(AudioState
*s
)
804 HWVoiceIn
*hwi
= NULL
;
805 HWVoiceOut
*hwo
= NULL
;
807 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
808 if (!hwo
->poll_mode
) {
812 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
813 if (!hwi
->poll_mode
) {
820 static void audio_reset_timer (AudioState
*s
)
822 if (audio_is_timer_needed(s
)) {
823 timer_mod_anticipate_ns(s
->ts
,
824 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
825 if (!s
->timer_running
) {
826 s
->timer_running
= true;
827 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
828 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
832 if (s
->timer_running
) {
833 s
->timer_running
= false;
834 trace_audio_timer_stop();
839 static void audio_timer (void *opaque
)
842 AudioState
*s
= opaque
;
844 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
845 diff
= now
- s
->timer_last
;
846 if (diff
> s
->period_ticks
* 3 / 2) {
847 trace_audio_timer_delayed(diff
/ SCALE_MS
);
851 audio_run(s
, "timer");
852 audio_reset_timer(s
);
858 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
863 /* XXX: Consider options */
869 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
873 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
874 return audio_pcm_sw_write(sw
, buf
, size
);
876 return hw
->pcm_ops
->write(hw
, buf
, size
);
880 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
885 /* XXX: Consider options */
891 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
895 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
896 return audio_pcm_sw_read(sw
, buf
, size
);
898 return hw
->pcm_ops
->read(hw
, buf
, size
);
902 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
904 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
907 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
916 if (sw
->active
!= on
) {
917 AudioState
*s
= sw
->s
;
922 hw
->pending_disable
= 0;
926 if (hw
->pcm_ops
->enable_out
) {
927 hw
->pcm_ops
->enable_out(hw
, true);
929 audio_reset_timer (s
);
936 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
937 temp_sw
= temp_sw
->entries
.le_next
) {
938 nb_active
+= temp_sw
->active
!= 0;
941 hw
->pending_disable
= nb_active
== 1;
945 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
946 sc
->sw
.active
= hw
->enabled
;
948 audio_capture_maybe_changed (sc
->cap
, 1);
955 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
964 if (sw
->active
!= on
) {
965 AudioState
*s
= sw
->s
;
972 if (hw
->pcm_ops
->enable_in
) {
973 hw
->pcm_ops
->enable_in(hw
, true);
975 audio_reset_timer (s
);
978 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
983 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
984 temp_sw
= temp_sw
->entries
.le_next
) {
985 nb_active
+= temp_sw
->active
!= 0;
988 if (nb_active
== 1) {
990 if (hw
->pcm_ops
->enable_in
) {
991 hw
->pcm_ops
->enable_in(hw
, false);
1000 static size_t audio_get_avail (SWVoiceIn
*sw
)
1008 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1009 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
.size
)) {
1010 dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live
,
1011 sw
->hw
->conv_buf
.size
);
1016 "%s: get_avail live %zu frontend frames %u\n",
1018 live
, st_rate_frames_out(sw
->rate
, live
)
1024 static size_t audio_get_free(SWVoiceOut
*sw
)
1032 live
= sw
->total_hw_samples_mixed
;
1034 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
.size
)) {
1035 dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live
,
1036 sw
->hw
->mix_buf
.size
);
1040 dead
= sw
->hw
->mix_buf
.size
- live
;
1043 dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
1044 SW_NAME(sw
), live
, dead
, st_rate_frames_in(sw
->rate
, dead
));
1050 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1058 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1059 SWVoiceOut
*sw
= &sc
->sw
;
1060 size_t rpos2
= rpos
;
1064 size_t till_end_of_hw
= hw
->mix_buf
.size
- rpos2
;
1065 size_t to_read
= MIN(till_end_of_hw
, n
);
1066 size_t live
, frames_in
, frames_out
;
1068 sw
->resample_buf
.buffer
= hw
->mix_buf
.buffer
+ rpos2
;
1069 sw
->resample_buf
.size
= to_read
;
1070 live
= sw
->total_hw_samples_mixed
;
1072 audio_pcm_sw_resample_out(sw
,
1073 to_read
, sw
->hw
->mix_buf
.size
- live
,
1074 &frames_in
, &frames_out
);
1076 sw
->total_hw_samples_mixed
+= frames_out
;
1077 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1079 if (to_read
- frames_in
) {
1080 dolog("Could not mix %zu frames into a capture "
1081 "buffer, mixed %zu\n",
1082 to_read
, frames_in
);
1086 rpos2
= (rpos2
+ to_read
) % hw
->mix_buf
.size
;
1091 n
= MIN(samples
, hw
->mix_buf
.size
- rpos
);
1092 mixeng_clear(hw
->mix_buf
.buffer
+ rpos
, n
);
1093 mixeng_clear(hw
->mix_buf
.buffer
, samples
- n
);
1096 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1101 size_t size
= live
* hw
->info
.bytes_per_frame
;
1103 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1109 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1111 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1113 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1114 decr
* hw
->info
.bytes_per_frame
) /
1115 hw
->info
.bytes_per_frame
;
1119 hw
->mix_buf
.pos
= (hw
->mix_buf
.pos
+ proc
) % hw
->mix_buf
.size
;
1121 if (proc
== 0 || proc
< decr
) {
1126 if (hw
->pcm_ops
->run_buffer_out
) {
1127 hw
->pcm_ops
->run_buffer_out(hw
);
1133 static void audio_run_out (AudioState
*s
)
1135 HWVoiceOut
*hw
= NULL
;
1138 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1139 size_t played
, live
, prev_rpos
;
1140 size_t hw_free
= audio_pcm_hw_get_free(hw
);
1143 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1144 /* there is exactly 1 sw for each hw with no mixeng */
1145 sw
= hw
->sw_head
.lh_first
;
1147 if (hw
->pending_disable
) {
1149 hw
->pending_disable
= 0;
1150 if (hw
->pcm_ops
->enable_out
) {
1151 hw
->pcm_ops
->enable_out(hw
, false);
1156 sw
->callback
.fn(sw
->callback
.opaque
,
1157 hw_free
* sw
->info
.bytes_per_frame
);
1160 if (hw
->pcm_ops
->run_buffer_out
) {
1161 hw
->pcm_ops
->run_buffer_out(hw
);
1167 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1169 size_t sw_free
= audio_get_free(sw
);
1172 if (hw_free
> sw
->total_hw_samples_mixed
) {
1173 free
= st_rate_frames_in(sw
->rate
,
1174 MIN(sw_free
, hw_free
- sw
->total_hw_samples_mixed
));
1178 if (free
> sw
->resample_buf
.pos
) {
1179 free
= MIN(free
, sw
->resample_buf
.size
)
1180 - sw
->resample_buf
.pos
;
1181 sw
->callback
.fn(sw
->callback
.opaque
,
1182 free
* sw
->info
.bytes_per_frame
);
1187 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1192 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
1193 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
1197 if (hw
->pending_disable
&& !nb_live
) {
1200 dolog ("Disabling voice\n");
1203 hw
->pending_disable
= 0;
1204 if (hw
->pcm_ops
->enable_out
) {
1205 hw
->pcm_ops
->enable_out(hw
, false);
1207 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1209 audio_recalc_and_notify_capture (sc
->cap
);
1215 if (hw
->pcm_ops
->run_buffer_out
) {
1216 hw
->pcm_ops
->run_buffer_out(hw
);
1221 prev_rpos
= hw
->mix_buf
.pos
;
1222 played
= audio_pcm_hw_run_out(hw
, live
);
1223 replay_audio_out(&played
);
1224 if (audio_bug(__func__
, hw
->mix_buf
.pos
>= hw
->mix_buf
.size
)) {
1225 dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
1226 hw
->mix_buf
.pos
, hw
->mix_buf
.size
, played
);
1227 hw
->mix_buf
.pos
= 0;
1231 dolog("played=%zu\n", played
);
1235 hw
->ts_helper
+= played
;
1236 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1239 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1240 if (!sw
->active
&& sw
->empty
) {
1244 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1245 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1246 played
, sw
->total_hw_samples_mixed
);
1247 played
= sw
->total_hw_samples_mixed
;
1250 sw
->total_hw_samples_mixed
-= played
;
1252 if (!sw
->total_hw_samples_mixed
) {
1259 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1263 if (hw
->pcm_ops
->run_buffer_in
) {
1264 hw
->pcm_ops
->run_buffer_in(hw
);
1269 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1270 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1272 assert(size
% hw
->info
.bytes_per_frame
== 0);
1277 proc
= audio_pcm_hw_conv_in(hw
, buf
, size
/ hw
->info
.bytes_per_frame
);
1281 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1287 static void audio_run_in (AudioState
*s
)
1289 HWVoiceIn
*hw
= NULL
;
1291 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1292 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1293 /* there is exactly 1 sw for each hw with no mixeng */
1294 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1296 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1302 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1304 size_t captured
= 0, min
;
1306 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1307 captured
= audio_pcm_hw_run_in(
1308 hw
, hw
->conv_buf
.size
- audio_pcm_hw_get_live_in(hw
));
1310 replay_audio_in(&captured
, hw
->conv_buf
.buffer
, &hw
->conv_buf
.pos
,
1313 min
= audio_pcm_hw_find_min_in (hw
);
1314 hw
->total_samples_captured
+= captured
- min
;
1315 hw
->ts_helper
+= captured
;
1317 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1318 sw
->total_hw_samples_acquired
-= min
;
1321 size_t sw_avail
= audio_get_avail(sw
);
1324 avail
= st_rate_frames_out(sw
->rate
, sw_avail
);
1326 avail
= MIN(avail
, sw
->resample_buf
.size
);
1327 sw
->callback
.fn(sw
->callback
.opaque
,
1328 avail
* sw
->info
.bytes_per_frame
);
1335 static void audio_run_capture (AudioState
*s
)
1337 CaptureVoiceOut
*cap
;
1339 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1340 size_t live
, rpos
, captured
;
1341 HWVoiceOut
*hw
= &cap
->hw
;
1344 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1345 rpos
= hw
->mix_buf
.pos
;
1347 size_t left
= hw
->mix_buf
.size
- rpos
;
1348 size_t to_capture
= MIN(live
, left
);
1349 struct st_sample
*src
;
1350 struct capture_callback
*cb
;
1352 src
= hw
->mix_buf
.buffer
+ rpos
;
1353 hw
->clip (cap
->buf
, src
, to_capture
);
1354 mixeng_clear (src
, to_capture
);
1356 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1357 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1358 to_capture
* hw
->info
.bytes_per_frame
);
1360 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
.size
;
1363 hw
->mix_buf
.pos
= rpos
;
1365 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1366 if (!sw
->active
&& sw
->empty
) {
1370 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1371 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1372 captured
, sw
->total_hw_samples_mixed
);
1373 captured
= sw
->total_hw_samples_mixed
;
1376 sw
->total_hw_samples_mixed
-= captured
;
1377 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1382 void audio_run(AudioState
*s
, const char *msg
)
1386 audio_run_capture(s
);
1390 static double prevtime
;
1394 if (gettimeofday (&tv
, NULL
)) {
1395 perror ("audio_run: gettimeofday");
1399 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1400 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1401 prevtime
= currtime
;
1406 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1408 if (unlikely(!hw
->buf_emul
)) {
1409 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1410 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1411 hw
->pos_emul
= hw
->pending_emul
= 0;
1414 while (hw
->pending_emul
< hw
->size_emul
) {
1415 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1416 hw
->size_emul
- hw
->pending_emul
);
1417 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1419 hw
->pending_emul
+= read
;
1420 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1421 if (read
< read_len
) {
1427 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1431 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1432 assert(start
< hw
->size_emul
);
1434 *size
= MIN(*size
, hw
->pending_emul
);
1435 *size
= MIN(*size
, hw
->size_emul
- start
);
1436 return hw
->buf_emul
+ start
;
1439 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1441 assert(size
<= hw
->pending_emul
);
1442 hw
->pending_emul
-= size
;
1445 size_t audio_generic_buffer_get_free(HWVoiceOut
*hw
)
1448 return hw
->size_emul
- hw
->pending_emul
;
1450 return hw
->samples
* hw
->info
.bytes_per_frame
;
1454 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1456 while (hw
->pending_emul
) {
1457 size_t write_len
, written
, start
;
1459 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1460 assert(start
< hw
->size_emul
);
1462 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1464 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1465 hw
->pending_emul
-= written
;
1467 if (written
< write_len
) {
1473 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1475 if (unlikely(!hw
->buf_emul
)) {
1476 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1477 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1478 hw
->pos_emul
= hw
->pending_emul
= 0;
1481 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1482 hw
->size_emul
- hw
->pos_emul
);
1483 return hw
->buf_emul
+ hw
->pos_emul
;
1486 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1488 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1489 size
+ hw
->pending_emul
<= hw
->size_emul
);
1491 hw
->pending_emul
+= size
;
1492 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1497 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1501 if (hw
->pcm_ops
->buffer_get_free
) {
1502 size_t free
= hw
->pcm_ops
->buffer_get_free(hw
);
1504 size
= MIN(size
, free
);
1507 while (total
< size
) {
1508 size_t dst_size
= size
- total
;
1509 size_t copy_size
, proc
;
1510 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1512 if (dst_size
== 0) {
1516 copy_size
= MIN(size
- total
, dst_size
);
1518 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1520 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1523 if (proc
== 0 || proc
< copy_size
) {
1531 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1535 if (hw
->pcm_ops
->run_buffer_in
) {
1536 hw
->pcm_ops
->run_buffer_in(hw
);
1539 while (total
< size
) {
1540 size_t src_size
= size
- total
;
1541 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1543 if (src_size
== 0) {
1547 memcpy((char *)buf
+ total
, src
, src_size
);
1548 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1555 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1556 bool msg
, Audiodev
*dev
)
1558 s
->drv_opaque
= drv
->init(dev
);
1560 if (s
->drv_opaque
) {
1561 if (!drv
->pcm_ops
->get_buffer_in
) {
1562 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1563 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1565 if (!drv
->pcm_ops
->get_buffer_out
) {
1566 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1567 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1570 audio_init_nb_voices_out(s
, drv
);
1571 audio_init_nb_voices_in(s
, drv
);
1576 dolog("Could not init `%s' audio driver\n", drv
->name
);
1582 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1585 AudioState
*s
= opaque
;
1586 HWVoiceOut
*hwo
= NULL
;
1587 HWVoiceIn
*hwi
= NULL
;
1589 s
->vm_running
= running
;
1590 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1591 if (hwo
->pcm_ops
->enable_out
) {
1592 hwo
->pcm_ops
->enable_out(hwo
, running
);
1596 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1597 if (hwi
->pcm_ops
->enable_in
) {
1598 hwi
->pcm_ops
->enable_in(hwi
, running
);
1601 audio_reset_timer (s
);
1604 static void free_audio_state(AudioState
*s
)
1606 HWVoiceOut
*hwo
, *hwon
;
1607 HWVoiceIn
*hwi
, *hwin
;
1609 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1612 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1613 hwo
->pcm_ops
->enable_out(hwo
, false);
1615 hwo
->pcm_ops
->fini_out (hwo
);
1617 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1618 CaptureVoiceOut
*cap
= sc
->cap
;
1619 struct capture_callback
*cb
;
1621 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1622 cb
->ops
.destroy (cb
->opaque
);
1625 QLIST_REMOVE(hwo
, entries
);
1628 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1629 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1630 hwi
->pcm_ops
->enable_in(hwi
, false);
1632 hwi
->pcm_ops
->fini_in (hwi
);
1633 QLIST_REMOVE(hwi
, entries
);
1637 s
->drv
->fini (s
->drv_opaque
);
1642 qapi_free_Audiodev(s
->dev
);
1654 void audio_cleanup(void)
1656 while (!QTAILQ_EMPTY(&audio_states
)) {
1657 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1658 QTAILQ_REMOVE(&audio_states
, s
, list
);
1659 free_audio_state(s
);
1663 static bool vmstate_audio_needed(void *opaque
)
1666 * Never needed, this vmstate only exists in case
1667 * an old qemu sends it to us.
1672 static const VMStateDescription vmstate_audio
= {
1675 .minimum_version_id
= 1,
1676 .needed
= vmstate_audio_needed
,
1677 .fields
= (VMStateField
[]) {
1678 VMSTATE_END_OF_LIST()
1682 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1684 static AudiodevListEntry
*audiodev_find(
1685 AudiodevListHead
*head
, const char *drvname
)
1687 AudiodevListEntry
*e
;
1688 QSIMPLEQ_FOREACH(e
, head
, next
) {
1689 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1698 * if we have dev, this function was called because of an -audiodev argument =>
1699 * initialize a new state with it
1700 * if dev == NULL => legacy implicit initialization, return the already created
1701 * state or create a new one
1703 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1705 static bool atexit_registered
;
1708 const char *drvname
= NULL
;
1709 VMChangeStateEntry
*e
;
1711 struct audio_driver
*driver
;
1712 /* silence gcc warning about uninitialized variable */
1713 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1717 * When using spice allow the spice audio driver being picked
1720 * Temporary hack. Using audio devices without explicit
1721 * audiodev= property is already deprecated. Same goes for
1722 * the -soundhw switch. Once this support gets finally
1723 * removed we can also drop the concept of a default audio
1724 * backend and this can go away.
1726 driver
= audio_driver_lookup("spice");
1728 driver
->can_be_default
= 1;
1733 /* -audiodev option */
1734 legacy_config
= false;
1735 drvname
= AudiodevDriver_str(dev
->driver
);
1736 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1737 if (!legacy_config
) {
1738 dolog("Device %s: audiodev default parameter is deprecated, please "
1739 "specify audiodev=%s\n", name
,
1740 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1742 return QTAILQ_FIRST(&audio_states
);
1744 /* legacy implicit initialization */
1745 head
= audio_handle_legacy_opts();
1747 * In case of legacy initialization, all Audiodevs in the list will have
1748 * the same configuration (except the driver), so it doesn't matter which
1749 * one we chose. We need an Audiodev to set up AudioState before we can
1750 * init a driver. Also note that dev at this point is still in the
1753 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1754 audio_validate_opts(dev
, &error_abort
);
1757 s
= g_new0(AudioState
, 1);
1760 QLIST_INIT (&s
->hw_head_out
);
1761 QLIST_INIT (&s
->hw_head_in
);
1762 QLIST_INIT (&s
->cap_head
);
1763 if (!atexit_registered
) {
1764 atexit(audio_cleanup
);
1765 atexit_registered
= true;
1768 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1770 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1771 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1773 if (s
->nb_hw_voices_out
< 1) {
1774 dolog ("Bogus number of playback voices %d, setting to 1\n",
1775 s
->nb_hw_voices_out
);
1776 s
->nb_hw_voices_out
= 1;
1779 if (s
->nb_hw_voices_in
< 0) {
1780 dolog ("Bogus number of capture voices %d, setting to 0\n",
1781 s
->nb_hw_voices_in
);
1782 s
->nb_hw_voices_in
= 0;
1786 driver
= audio_driver_lookup(drvname
);
1788 done
= !audio_driver_init(s
, driver
, true, dev
);
1790 dolog ("Unknown audio driver `%s'\n", drvname
);
1793 free_audio_state(s
);
1797 for (i
= 0; audio_prio_list
[i
]; i
++) {
1798 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1799 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1802 s
->dev
= dev
= e
->dev
;
1803 audio_validate_opts(dev
, &error_abort
);
1804 done
= !audio_driver_init(s
, driver
, false, dev
);
1812 audio_free_audiodev_list(&head
);
1815 driver
= audio_driver_lookup("none");
1816 done
= !audio_driver_init(s
, driver
, false, dev
);
1818 dolog("warning: Using timer based audio emulation\n");
1821 if (dev
->timer_period
<= 0) {
1822 s
->period_ticks
= 1;
1824 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1827 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1829 dolog ("warning: Could not register change state handler\n"
1830 "(Audio can continue looping even after stopping the VM)\n");
1833 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1834 QLIST_INIT (&s
->card_head
);
1835 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1839 void audio_free_audiodev_list(AudiodevListHead
*head
)
1841 AudiodevListEntry
*e
;
1842 while ((e
= QSIMPLEQ_FIRST(head
))) {
1843 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1844 qapi_free_Audiodev(e
->dev
);
1849 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1852 card
->state
= audio_init(NULL
, name
);
1855 card
->name
= g_strdup (name
);
1856 memset (&card
->entries
, 0, sizeof (card
->entries
));
1857 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1860 void AUD_remove_card (QEMUSoundCard
*card
)
1862 QLIST_REMOVE (card
, entries
);
1863 g_free (card
->name
);
1866 static struct audio_pcm_ops capture_pcm_ops
;
1868 CaptureVoiceOut
*AUD_add_capture(
1870 struct audsettings
*as
,
1871 struct audio_capture_ops
*ops
,
1875 CaptureVoiceOut
*cap
;
1876 struct capture_callback
*cb
;
1879 if (!legacy_config
) {
1880 dolog("Capturing without setting an audiodev is deprecated\n");
1882 s
= audio_init(NULL
, NULL
);
1885 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1886 dolog("Can't capture with mixeng disabled\n");
1890 if (audio_validate_settings (as
)) {
1891 dolog ("Invalid settings were passed when trying to add capture\n");
1892 audio_print_settings (as
);
1896 cb
= g_malloc0(sizeof(*cb
));
1898 cb
->opaque
= cb_opaque
;
1900 cap
= audio_pcm_capture_find_specific(s
, as
);
1902 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1906 CaptureVoiceOut
*cap
;
1908 cap
= g_malloc0(sizeof(*cap
));
1912 hw
->pcm_ops
= &capture_pcm_ops
;
1913 QLIST_INIT (&hw
->sw_head
);
1914 QLIST_INIT (&cap
->cb_head
);
1916 /* XXX find a more elegant way */
1917 hw
->samples
= 4096 * 4;
1918 audio_pcm_hw_alloc_resources_out(hw
);
1920 audio_pcm_init_info (&hw
->info
, as
);
1922 cap
->buf
= g_malloc0_n(hw
->mix_buf
.size
, hw
->info
.bytes_per_frame
);
1924 if (hw
->info
.is_float
) {
1925 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1927 hw
->clip
= mixeng_clip
1928 [hw
->info
.nchannels
== 2]
1929 [hw
->info
.is_signed
]
1930 [hw
->info
.swap_endianness
]
1931 [audio_bits_to_index(hw
->info
.bits
)];
1934 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1935 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1937 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1938 audio_attach_capture (hw
);
1944 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1946 struct capture_callback
*cb
;
1948 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1949 if (cb
->opaque
== cb_opaque
) {
1950 cb
->ops
.destroy (cb_opaque
);
1951 QLIST_REMOVE (cb
, entries
);
1954 if (!cap
->cb_head
.lh_first
) {
1955 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1958 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1959 #ifdef DEBUG_CAPTURE
1960 dolog ("freeing %s\n", sw
->name
);
1963 sw1
= sw
->entries
.le_next
;
1965 st_rate_stop (sw
->rate
);
1968 QLIST_REMOVE (sw
, entries
);
1969 QLIST_REMOVE (sc
, entries
);
1973 QLIST_REMOVE (cap
, entries
);
1974 g_free(cap
->hw
.mix_buf
.buffer
);
1983 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1985 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1986 audio_set_volume_out(sw
, &vol
);
1989 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1992 HWVoiceOut
*hw
= sw
->hw
;
1994 sw
->vol
.mute
= vol
->mute
;
1995 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1996 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1999 if (hw
->pcm_ops
->volume_out
) {
2000 hw
->pcm_ops
->volume_out(hw
, vol
);
2005 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2007 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
2008 audio_set_volume_in(sw
, &vol
);
2011 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
2014 HWVoiceIn
*hw
= sw
->hw
;
2016 sw
->vol
.mute
= vol
->mute
;
2017 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
2018 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
2021 if (hw
->pcm_ops
->volume_in
) {
2022 hw
->pcm_ops
->volume_in(hw
, vol
);
2027 void audio_create_pdos(Audiodev
*dev
)
2029 switch (dev
->driver
) {
2030 #define CASE(DRIVER, driver, pdo_name) \
2031 case AUDIODEV_DRIVER_##DRIVER: \
2032 if (!dev->u.driver.in) { \
2033 dev->u.driver.in = g_malloc0( \
2034 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2036 if (!dev->u.driver.out) { \
2037 dev->u.driver.out = g_malloc0( \
2038 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2043 #ifdef CONFIG_AUDIO_ALSA
2044 CASE(ALSA
, alsa
, Alsa
);
2046 #ifdef CONFIG_AUDIO_COREAUDIO
2047 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2049 #ifdef CONFIG_DBUS_DISPLAY
2052 #ifdef CONFIG_AUDIO_DSOUND
2053 CASE(DSOUND
, dsound
, );
2055 #ifdef CONFIG_AUDIO_JACK
2056 CASE(JACK
, jack
, Jack
);
2058 #ifdef CONFIG_AUDIO_OSS
2059 CASE(OSS
, oss
, Oss
);
2061 #ifdef CONFIG_AUDIO_PA
2064 #ifdef CONFIG_AUDIO_PIPEWIRE
2065 CASE(PIPEWIRE
, pipewire
, Pipewire
);
2067 #ifdef CONFIG_AUDIO_SDL
2068 CASE(SDL
, sdl
, Sdl
);
2070 #ifdef CONFIG_AUDIO_SNDIO
2071 CASE(SNDIO
, sndio
, );
2074 CASE(SPICE
, spice
, );
2078 case AUDIODEV_DRIVER__MAX
:
2083 static void audio_validate_per_direction_opts(
2084 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2086 if (!pdo
->has_mixing_engine
) {
2087 pdo
->has_mixing_engine
= true;
2088 pdo
->mixing_engine
= true;
2090 if (!pdo
->has_fixed_settings
) {
2091 pdo
->has_fixed_settings
= true;
2092 pdo
->fixed_settings
= pdo
->mixing_engine
;
2094 if (!pdo
->fixed_settings
&&
2095 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2097 "You can't use frequency, channels or format with fixed-settings=off");
2100 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2101 error_setg(errp
, "You can't use fixed-settings without mixeng");
2105 if (!pdo
->has_frequency
) {
2106 pdo
->has_frequency
= true;
2107 pdo
->frequency
= 44100;
2109 if (!pdo
->has_channels
) {
2110 pdo
->has_channels
= true;
2113 if (!pdo
->has_voices
) {
2114 pdo
->has_voices
= true;
2115 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2117 if (!pdo
->has_format
) {
2118 pdo
->has_format
= true;
2119 pdo
->format
= AUDIO_FORMAT_S16
;
2123 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2127 audio_create_pdos(dev
);
2129 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2131 error_propagate(errp
, err
);
2135 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2137 error_propagate(errp
, err
);
2141 if (!dev
->has_timer_period
) {
2142 dev
->has_timer_period
= true;
2143 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2147 void audio_help(void)
2151 printf("Available audio drivers:\n");
2153 for (i
= 0; i
< AUDIODEV_DRIVER__MAX
; i
++) {
2154 audio_driver
*driver
= audio_driver_lookup(AudiodevDriver_str(i
));
2156 printf("%s\n", driver
->name
);
2161 void audio_parse_option(const char *opt
)
2163 Audiodev
*dev
= NULL
;
2165 if (is_help_option(opt
)) {
2169 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2170 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2176 void audio_define(Audiodev
*dev
)
2178 AudiodevListEntry
*e
;
2180 audio_validate_opts(dev
, &error_fatal
);
2182 e
= g_new0(AudiodevListEntry
, 1);
2184 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2187 bool audio_init_audiodevs(void)
2189 AudiodevListEntry
*e
;
2191 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2192 if (!audio_init(e
->dev
, NULL
)) {
2200 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2202 return (audsettings
) {
2203 .freq
= pdo
->frequency
,
2204 .nchannels
= pdo
->channels
,
2206 .endianness
= AUDIO_HOST_ENDIANNESS
,
2210 int audioformat_bytes_per_sample(AudioFormat fmt
)
2213 case AUDIO_FORMAT_U8
:
2214 case AUDIO_FORMAT_S8
:
2217 case AUDIO_FORMAT_U16
:
2218 case AUDIO_FORMAT_S16
:
2221 case AUDIO_FORMAT_U32
:
2222 case AUDIO_FORMAT_S32
:
2223 case AUDIO_FORMAT_F32
:
2226 case AUDIO_FORMAT__MAX
:
2233 /* frames = freq * usec / 1e6 */
2234 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2235 audsettings
*as
, int def_usecs
)
2237 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2238 return (as
->freq
* usecs
+ 500000) / 1000000;
2241 /* samples = channels * frames = channels * freq * usec / 1e6 */
2242 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2243 audsettings
*as
, int def_usecs
)
2245 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2249 * bytes = bytes_per_sample * samples =
2250 * bytes_per_sample * channels * freq * usec / 1e6
2252 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2253 audsettings
*as
, int def_usecs
)
2255 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2256 audioformat_bytes_per_sample(as
->fmt
);
2259 AudioState
*audio_state_by_name(const char *name
)
2262 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2264 if (strcmp(name
, s
->dev
->id
) == 0) {
2271 const char *audio_get_id(QEMUSoundCard
*card
)
2274 assert(card
->state
->dev
);
2275 return card
->state
->dev
->id
;
2281 const char *audio_application_name(void)
2283 const char *vm_name
;
2285 vm_name
= qemu_get_vm_name();
2286 return vm_name
? vm_name
: "qemu";
2289 void audio_rate_start(RateCtl
*rate
)
2291 memset(rate
, 0, sizeof(RateCtl
));
2292 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2295 size_t audio_rate_peek_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
)
2302 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2303 ticks
= now
- rate
->start_ticks
;
2304 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2305 frames
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2306 if (frames
< 0 || frames
> 65536) {
2307 AUD_log(NULL
, "Resetting rate control (%" PRId64
" frames)\n", frames
);
2308 audio_rate_start(rate
);
2312 return frames
* info
->bytes_per_frame
;
2315 void audio_rate_add_bytes(RateCtl
*rate
, size_t bytes_used
)
2317 rate
->bytes_sent
+= bytes_used
;
2320 size_t audio_rate_get_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
,
2325 bytes
= audio_rate_peek_bytes(rate
, info
);
2326 bytes
= MIN(bytes
, bytes_avail
);
2327 audio_rate_add_bytes(rate
, bytes
);
2332 AudiodevList
*qmp_query_audiodevs(Error
**errp
)
2334 AudiodevList
*ret
= NULL
;
2335 AudiodevListEntry
*e
;
2336 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2337 QAPI_LIST_PREPEND(ret
, QAPI_CLONE(Audiodev
, e
->dev
));