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 "qapi/qmp/qdict.h"
36 #include "qemu/cutils.h"
37 #include "qemu/error-report.h"
39 #include "qemu/module.h"
40 #include "qemu/help_option.h"
41 #include "sysemu/sysemu.h"
42 #include "sysemu/replay.h"
43 #include "sysemu/runstate.h"
44 #include "ui/qemu-spice.h"
47 #define AUDIO_CAP "audio"
48 #include "audio_int.h"
50 /* #define DEBUG_LIVE */
51 /* #define DEBUG_OUT */
52 /* #define DEBUG_CAPTURE */
53 /* #define DEBUG_POLL */
55 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
58 /* Order of CONFIG_AUDIO_DRIVERS is import.
59 The 1st one is the one used by default, that is the reason
60 that we generate the list.
62 const char *audio_prio_list
[] = {
69 static QLIST_HEAD(, audio_driver
) audio_drivers
;
70 static AudiodevListHead audiodevs
=
71 QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
72 static AudiodevListHead default_audiodevs
=
73 QSIMPLEQ_HEAD_INITIALIZER(default_audiodevs
);
76 void audio_driver_register(audio_driver
*drv
)
78 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
81 static audio_driver
*audio_driver_lookup(const char *name
)
83 struct audio_driver
*d
;
84 Error
*local_err
= NULL
;
87 QLIST_FOREACH(d
, &audio_drivers
, next
) {
88 if (strcmp(name
, d
->name
) == 0) {
92 rv
= audio_module_load(name
, &local_err
);
94 QLIST_FOREACH(d
, &audio_drivers
, next
) {
95 if (strcmp(name
, d
->name
) == 0) {
100 error_report_err(local_err
);
105 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
106 QTAILQ_HEAD_INITIALIZER(audio_states
);
107 static AudioState
*default_audio_state
;
109 const struct mixeng_volume nominal_volume
= {
120 int audio_bug (const char *funcname
, int cond
)
125 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
128 AUD_log (NULL
, "Save all your work and restart without audio\n");
129 AUD_log (NULL
, "I am sorry\n");
131 AUD_log (NULL
, "Context:\n");
137 static inline int audio_bits_to_index (int bits
)
150 audio_bug ("bits_to_index", 1);
151 AUD_log (NULL
, "invalid bits %d\n", bits
);
156 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
159 fprintf(stderr
, "%s: ", cap
);
162 vfprintf(stderr
, fmt
, ap
);
165 void AUD_log (const char *cap
, const char *fmt
, ...)
170 AUD_vlog (cap
, fmt
, ap
);
174 static void audio_print_settings (struct audsettings
*as
)
176 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
179 case AUDIO_FORMAT_S8
:
180 AUD_log (NULL
, "S8");
182 case AUDIO_FORMAT_U8
:
183 AUD_log (NULL
, "U8");
185 case AUDIO_FORMAT_S16
:
186 AUD_log (NULL
, "S16");
188 case AUDIO_FORMAT_U16
:
189 AUD_log (NULL
, "U16");
191 case AUDIO_FORMAT_S32
:
192 AUD_log (NULL
, "S32");
194 case AUDIO_FORMAT_U32
:
195 AUD_log (NULL
, "U32");
197 case AUDIO_FORMAT_F32
:
198 AUD_log (NULL
, "F32");
201 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
205 AUD_log (NULL
, " endianness=");
206 switch (as
->endianness
) {
208 AUD_log (NULL
, "little");
211 AUD_log (NULL
, "big");
214 AUD_log (NULL
, "invalid");
217 AUD_log (NULL
, "\n");
220 static int audio_validate_settings (struct audsettings
*as
)
224 invalid
= as
->nchannels
< 1;
225 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
228 case AUDIO_FORMAT_S8
:
229 case AUDIO_FORMAT_U8
:
230 case AUDIO_FORMAT_S16
:
231 case AUDIO_FORMAT_U16
:
232 case AUDIO_FORMAT_S32
:
233 case AUDIO_FORMAT_U32
:
234 case AUDIO_FORMAT_F32
:
241 invalid
|= as
->freq
<= 0;
242 return invalid
? -1 : 0;
245 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
248 bool is_signed
= false, is_float
= false;
251 case AUDIO_FORMAT_S8
:
254 case AUDIO_FORMAT_U8
:
257 case AUDIO_FORMAT_S16
:
260 case AUDIO_FORMAT_U16
:
264 case AUDIO_FORMAT_F32
:
267 case AUDIO_FORMAT_S32
:
270 case AUDIO_FORMAT_U32
:
277 return info
->freq
== as
->freq
278 && info
->nchannels
== as
->nchannels
279 && info
->is_signed
== is_signed
280 && info
->is_float
== is_float
281 && info
->bits
== bits
282 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
285 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
288 bool is_signed
= false, is_float
= false;
291 case AUDIO_FORMAT_S8
:
294 case AUDIO_FORMAT_U8
:
298 case AUDIO_FORMAT_S16
:
301 case AUDIO_FORMAT_U16
:
306 case AUDIO_FORMAT_F32
:
309 case AUDIO_FORMAT_S32
:
312 case AUDIO_FORMAT_U32
:
321 info
->freq
= as
->freq
;
323 info
->is_signed
= is_signed
;
324 info
->is_float
= is_float
;
325 info
->nchannels
= as
->nchannels
;
326 info
->bytes_per_frame
= as
->nchannels
* mul
;
327 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
328 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
331 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
337 if (info
->is_signed
|| info
->is_float
) {
338 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
340 switch (info
->bits
) {
342 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
351 if (info
->swap_endianness
) {
355 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
365 int32_t s
= INT32_MAX
;
367 if (info
->swap_endianness
) {
371 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
378 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
388 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
389 struct audsettings
*as
)
391 CaptureVoiceOut
*cap
;
393 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
394 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
401 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
403 struct capture_callback
*cb
;
406 dolog ("notification %d sent\n", cmd
);
408 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
409 cb
->ops
.notify (cb
->opaque
, cmd
);
413 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
415 if (cap
->hw
.enabled
!= enabled
) {
416 audcnotification_e cmd
;
417 cap
->hw
.enabled
= enabled
;
418 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
419 audio_notify_capture (cap
, cmd
);
423 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
425 HWVoiceOut
*hw
= &cap
->hw
;
429 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
435 audio_capture_maybe_changed (cap
, enabled
);
438 static void audio_detach_capture (HWVoiceOut
*hw
)
440 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
443 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
444 SWVoiceOut
*sw
= &sc
->sw
;
445 CaptureVoiceOut
*cap
= sc
->cap
;
446 int was_active
= sw
->active
;
449 st_rate_stop (sw
->rate
);
453 QLIST_REMOVE (sw
, entries
);
454 QLIST_REMOVE (sc
, entries
);
457 /* We have removed soft voice from the capture:
458 this might have changed the overall status of the capture
459 since this might have been the only active voice */
460 audio_recalc_and_notify_capture (cap
);
466 static int audio_attach_capture (HWVoiceOut
*hw
)
468 AudioState
*s
= hw
->s
;
469 CaptureVoiceOut
*cap
;
471 audio_detach_capture (hw
);
472 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
475 HWVoiceOut
*hw_cap
= &cap
->hw
;
477 sc
= g_malloc0(sizeof(*sc
));
484 sw
->active
= hw
->enabled
;
485 sw
->vol
= nominal_volume
;
486 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
487 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
488 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
490 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
491 hw
, sw
->info
.freq
, sw
->info
.bits
,
493 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
496 audio_capture_maybe_changed (cap
, 1);
503 * Hard voice (capture)
505 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
508 size_t m
= hw
->total_samples_captured
;
510 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
512 m
= MIN (m
, sw
->total_hw_samples_acquired
);
518 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
520 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
521 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
522 dolog("live=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
528 static size_t audio_pcm_hw_conv_in(HWVoiceIn
*hw
, void *pcm_buf
, size_t samples
)
531 STSampleBuffer
*conv_buf
= &hw
->conv_buf
;
534 uint8_t *src
= advance(pcm_buf
, conv
* hw
->info
.bytes_per_frame
);
535 size_t proc
= MIN(samples
, conv_buf
->size
- conv_buf
->pos
);
537 hw
->conv(conv_buf
->buffer
+ conv_buf
->pos
, src
, proc
);
538 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
547 * Soft voice (capture)
549 static void audio_pcm_sw_resample_in(SWVoiceIn
*sw
,
550 size_t frames_in_max
, size_t frames_out_max
,
551 size_t *total_in
, size_t *total_out
)
553 HWVoiceIn
*hw
= sw
->hw
;
554 struct st_sample
*src
, *dst
;
555 size_t live
, rpos
, frames_in
, frames_out
;
557 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
558 rpos
= audio_ring_posb(hw
->conv_buf
.pos
, live
, hw
->conv_buf
.size
);
560 /* resample conv_buf from rpos to end of buffer */
561 src
= hw
->conv_buf
.buffer
+ rpos
;
562 frames_in
= MIN(frames_in_max
, hw
->conv_buf
.size
- rpos
);
563 dst
= sw
->resample_buf
.buffer
;
564 frames_out
= frames_out_max
;
565 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
567 *total_in
= frames_in
;
568 *total_out
= frames_out
;
570 /* resample conv_buf from start of buffer if there are input frames left */
571 if (frames_in_max
- frames_in
&& rpos
== hw
->conv_buf
.size
) {
572 src
= hw
->conv_buf
.buffer
;
573 frames_in
= frames_in_max
- frames_in
;
575 frames_out
= frames_out_max
- frames_out
;
576 st_rate_flow(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
577 *total_in
+= frames_in
;
578 *total_out
+= frames_out
;
582 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t buf_len
)
584 HWVoiceIn
*hw
= sw
->hw
;
585 size_t live
, frames_out_max
, total_in
, total_out
;
587 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
591 if (audio_bug(__func__
, live
> hw
->conv_buf
.size
)) {
592 dolog("live_in=%zu hw->conv_buf.size=%zu\n", live
, hw
->conv_buf
.size
);
596 frames_out_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
,
597 sw
->resample_buf
.size
);
599 audio_pcm_sw_resample_in(sw
, live
, frames_out_max
, &total_in
, &total_out
);
601 if (!hw
->pcm_ops
->volume_in
) {
602 mixeng_volume(sw
->resample_buf
.buffer
, total_out
, &sw
->vol
);
604 sw
->clip(buf
, sw
->resample_buf
.buffer
, total_out
);
606 sw
->total_hw_samples_acquired
+= total_in
;
607 return total_out
* sw
->info
.bytes_per_frame
;
611 * Hard voice (playback)
613 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
619 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
620 if (sw
->active
|| !sw
->empty
) {
621 m
= MIN (m
, sw
->total_hw_samples_mixed
);
630 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
635 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
643 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
644 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
652 static size_t audio_pcm_hw_get_free(HWVoiceOut
*hw
)
654 return (hw
->pcm_ops
->buffer_get_free
? hw
->pcm_ops
->buffer_get_free(hw
) :
655 INT_MAX
) / hw
->info
.bytes_per_frame
;
658 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
661 size_t pos
= hw
->mix_buf
.pos
;
664 st_sample
*src
= hw
->mix_buf
.buffer
+ pos
;
665 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
666 size_t samples_till_end_of_buf
= hw
->mix_buf
.size
- pos
;
667 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
669 hw
->clip(dst
, src
, samples_to_clip
);
671 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
.size
;
672 len
-= samples_to_clip
;
673 clipped
+= samples_to_clip
;
678 * Soft voice (playback)
680 static void audio_pcm_sw_resample_out(SWVoiceOut
*sw
,
681 size_t frames_in_max
, size_t frames_out_max
,
682 size_t *total_in
, size_t *total_out
)
684 HWVoiceOut
*hw
= sw
->hw
;
685 struct st_sample
*src
, *dst
;
686 size_t live
, wpos
, frames_in
, frames_out
;
688 live
= sw
->total_hw_samples_mixed
;
689 wpos
= (hw
->mix_buf
.pos
+ live
) % hw
->mix_buf
.size
;
691 /* write to mix_buf from wpos to end of buffer */
692 src
= sw
->resample_buf
.buffer
;
693 frames_in
= frames_in_max
;
694 dst
= hw
->mix_buf
.buffer
+ wpos
;
695 frames_out
= MIN(frames_out_max
, hw
->mix_buf
.size
- wpos
);
696 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
698 *total_in
= frames_in
;
699 *total_out
= frames_out
;
701 /* write to mix_buf from start of buffer if there are input frames left */
702 if (frames_in_max
- frames_in
> 0 && wpos
== hw
->mix_buf
.size
) {
704 frames_in
= frames_in_max
- frames_in
;
705 dst
= hw
->mix_buf
.buffer
;
706 frames_out
= frames_out_max
- frames_out
;
707 st_rate_flow_mix(sw
->rate
, src
, dst
, &frames_in
, &frames_out
);
708 *total_in
+= frames_in
;
709 *total_out
+= frames_out
;
713 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t buf_len
)
715 HWVoiceOut
*hw
= sw
->hw
;
716 size_t live
, dead
, hw_free
, sw_max
, fe_max
;
717 size_t frames_in_max
, frames_out_max
, total_in
, total_out
;
719 live
= sw
->total_hw_samples_mixed
;
720 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
721 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
725 if (live
== hw
->mix_buf
.size
) {
727 dolog ("%s is full %zu\n", sw
->name
, live
);
732 dead
= hw
->mix_buf
.size
- live
;
733 hw_free
= audio_pcm_hw_get_free(hw
);
734 hw_free
= hw_free
> live
? hw_free
- live
: 0;
735 frames_out_max
= MIN(dead
, hw_free
);
736 sw_max
= st_rate_frames_in(sw
->rate
, frames_out_max
);
737 fe_max
= MIN(buf_len
/ sw
->info
.bytes_per_frame
+ sw
->resample_buf
.pos
,
738 sw
->resample_buf
.size
);
739 frames_in_max
= MIN(sw_max
, fe_max
);
741 if (!frames_in_max
) {
745 if (frames_in_max
> sw
->resample_buf
.pos
) {
746 sw
->conv(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
747 buf
, frames_in_max
- sw
->resample_buf
.pos
);
748 if (!sw
->hw
->pcm_ops
->volume_out
) {
749 mixeng_volume(sw
->resample_buf
.buffer
+ sw
->resample_buf
.pos
,
750 frames_in_max
- sw
->resample_buf
.pos
, &sw
->vol
);
754 audio_pcm_sw_resample_out(sw
, frames_in_max
, frames_out_max
,
755 &total_in
, &total_out
);
757 sw
->total_hw_samples_mixed
+= total_out
;
758 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
761 * Upsampling may leave one audio frame in the resample buffer. Decrement
762 * total_in by one if there was a leftover frame from the previous resample
763 * pass in the resample buffer. Increment total_in by one if the current
764 * resample pass left one frame in the resample buffer.
766 if (frames_in_max
- total_in
== 1) {
767 /* copy one leftover audio frame to the beginning of the buffer */
768 *sw
->resample_buf
.buffer
= *(sw
->resample_buf
.buffer
+ total_in
);
769 total_in
+= 1 - sw
->resample_buf
.pos
;
770 sw
->resample_buf
.pos
= 1;
771 } else if (total_in
>= sw
->resample_buf
.pos
) {
772 total_in
-= sw
->resample_buf
.pos
;
773 sw
->resample_buf
.pos
= 0;
778 "%s: write size %zu written %zu total mixed %zu\n",
780 buf_len
/ sw
->info
.bytes_per_frame
,
782 sw
->total_hw_samples_mixed
786 return total_in
* sw
->info
.bytes_per_frame
;
790 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
792 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
793 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
799 #include "audio_template.h"
801 #include "audio_template.h"
806 static int audio_is_timer_needed(AudioState
*s
)
808 HWVoiceIn
*hwi
= NULL
;
809 HWVoiceOut
*hwo
= NULL
;
811 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
812 if (!hwo
->poll_mode
) {
816 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
817 if (!hwi
->poll_mode
) {
824 static void audio_reset_timer (AudioState
*s
)
826 if (audio_is_timer_needed(s
)) {
827 timer_mod_anticipate_ns(s
->ts
,
828 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
829 if (!s
->timer_running
) {
830 s
->timer_running
= true;
831 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
832 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
836 if (s
->timer_running
) {
837 s
->timer_running
= false;
838 trace_audio_timer_stop();
843 static void audio_timer (void *opaque
)
846 AudioState
*s
= opaque
;
848 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
849 diff
= now
- s
->timer_last
;
850 if (diff
> s
->period_ticks
* 3 / 2) {
851 trace_audio_timer_delayed(diff
/ SCALE_MS
);
855 audio_run(s
, "timer");
856 audio_reset_timer(s
);
862 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
867 /* XXX: Consider options */
873 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
877 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
878 return audio_pcm_sw_write(sw
, buf
, size
);
880 return hw
->pcm_ops
->write(hw
, buf
, size
);
884 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
889 /* XXX: Consider options */
895 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
899 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
900 return audio_pcm_sw_read(sw
, buf
, size
);
902 return hw
->pcm_ops
->read(hw
, buf
, size
);
906 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
908 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
911 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
920 if (sw
->active
!= on
) {
921 AudioState
*s
= sw
->s
;
926 hw
->pending_disable
= 0;
930 if (hw
->pcm_ops
->enable_out
) {
931 hw
->pcm_ops
->enable_out(hw
, true);
933 audio_reset_timer (s
);
940 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
941 temp_sw
= temp_sw
->entries
.le_next
) {
942 nb_active
+= temp_sw
->active
!= 0;
945 hw
->pending_disable
= nb_active
== 1;
949 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
950 sc
->sw
.active
= hw
->enabled
;
952 audio_capture_maybe_changed (sc
->cap
, 1);
959 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
968 if (sw
->active
!= on
) {
969 AudioState
*s
= sw
->s
;
976 if (hw
->pcm_ops
->enable_in
) {
977 hw
->pcm_ops
->enable_in(hw
, true);
979 audio_reset_timer (s
);
982 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
987 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
988 temp_sw
= temp_sw
->entries
.le_next
) {
989 nb_active
+= temp_sw
->active
!= 0;
992 if (nb_active
== 1) {
994 if (hw
->pcm_ops
->enable_in
) {
995 hw
->pcm_ops
->enable_in(hw
, false);
1004 static size_t audio_get_avail (SWVoiceIn
*sw
)
1012 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1013 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
.size
)) {
1014 dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live
,
1015 sw
->hw
->conv_buf
.size
);
1020 "%s: get_avail live %zu frontend frames %u\n",
1022 live
, st_rate_frames_out(sw
->rate
, live
)
1028 static size_t audio_get_free(SWVoiceOut
*sw
)
1036 live
= sw
->total_hw_samples_mixed
;
1038 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
.size
)) {
1039 dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live
,
1040 sw
->hw
->mix_buf
.size
);
1044 dead
= sw
->hw
->mix_buf
.size
- live
;
1047 dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
1048 SW_NAME(sw
), live
, dead
, st_rate_frames_in(sw
->rate
, dead
));
1054 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1062 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1063 SWVoiceOut
*sw
= &sc
->sw
;
1064 size_t rpos2
= rpos
;
1068 size_t till_end_of_hw
= hw
->mix_buf
.size
- rpos2
;
1069 size_t to_read
= MIN(till_end_of_hw
, n
);
1070 size_t live
, frames_in
, frames_out
;
1072 sw
->resample_buf
.buffer
= hw
->mix_buf
.buffer
+ rpos2
;
1073 sw
->resample_buf
.size
= to_read
;
1074 live
= sw
->total_hw_samples_mixed
;
1076 audio_pcm_sw_resample_out(sw
,
1077 to_read
, sw
->hw
->mix_buf
.size
- live
,
1078 &frames_in
, &frames_out
);
1080 sw
->total_hw_samples_mixed
+= frames_out
;
1081 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1083 if (to_read
- frames_in
) {
1084 dolog("Could not mix %zu frames into a capture "
1085 "buffer, mixed %zu\n",
1086 to_read
, frames_in
);
1090 rpos2
= (rpos2
+ to_read
) % hw
->mix_buf
.size
;
1095 n
= MIN(samples
, hw
->mix_buf
.size
- rpos
);
1096 mixeng_clear(hw
->mix_buf
.buffer
+ rpos
, n
);
1097 mixeng_clear(hw
->mix_buf
.buffer
, samples
- n
);
1100 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1105 size_t size
= live
* hw
->info
.bytes_per_frame
;
1107 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1113 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1115 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1117 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1118 decr
* hw
->info
.bytes_per_frame
) /
1119 hw
->info
.bytes_per_frame
;
1123 hw
->mix_buf
.pos
= (hw
->mix_buf
.pos
+ proc
) % hw
->mix_buf
.size
;
1125 if (proc
== 0 || proc
< decr
) {
1130 if (hw
->pcm_ops
->run_buffer_out
) {
1131 hw
->pcm_ops
->run_buffer_out(hw
);
1137 static void audio_run_out (AudioState
*s
)
1139 HWVoiceOut
*hw
= NULL
;
1142 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1143 size_t played
, live
, prev_rpos
;
1144 size_t hw_free
= audio_pcm_hw_get_free(hw
);
1147 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1148 /* there is exactly 1 sw for each hw with no mixeng */
1149 sw
= hw
->sw_head
.lh_first
;
1151 if (hw
->pending_disable
) {
1153 hw
->pending_disable
= 0;
1154 if (hw
->pcm_ops
->enable_out
) {
1155 hw
->pcm_ops
->enable_out(hw
, false);
1160 sw
->callback
.fn(sw
->callback
.opaque
,
1161 hw_free
* sw
->info
.bytes_per_frame
);
1164 if (hw
->pcm_ops
->run_buffer_out
) {
1165 hw
->pcm_ops
->run_buffer_out(hw
);
1171 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1173 size_t sw_free
= audio_get_free(sw
);
1176 if (hw_free
> sw
->total_hw_samples_mixed
) {
1177 free
= st_rate_frames_in(sw
->rate
,
1178 MIN(sw_free
, hw_free
- sw
->total_hw_samples_mixed
));
1182 if (free
> sw
->resample_buf
.pos
) {
1183 free
= MIN(free
, sw
->resample_buf
.size
)
1184 - sw
->resample_buf
.pos
;
1185 sw
->callback
.fn(sw
->callback
.opaque
,
1186 free
* sw
->info
.bytes_per_frame
);
1191 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1196 if (audio_bug(__func__
, live
> hw
->mix_buf
.size
)) {
1197 dolog("live=%zu hw->mix_buf.size=%zu\n", live
, hw
->mix_buf
.size
);
1201 if (hw
->pending_disable
&& !nb_live
) {
1204 dolog ("Disabling voice\n");
1207 hw
->pending_disable
= 0;
1208 if (hw
->pcm_ops
->enable_out
) {
1209 hw
->pcm_ops
->enable_out(hw
, false);
1211 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1213 audio_recalc_and_notify_capture (sc
->cap
);
1219 if (hw
->pcm_ops
->run_buffer_out
) {
1220 hw
->pcm_ops
->run_buffer_out(hw
);
1225 prev_rpos
= hw
->mix_buf
.pos
;
1226 played
= audio_pcm_hw_run_out(hw
, live
);
1227 replay_audio_out(&played
);
1228 if (audio_bug(__func__
, hw
->mix_buf
.pos
>= hw
->mix_buf
.size
)) {
1229 dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
1230 hw
->mix_buf
.pos
, hw
->mix_buf
.size
, played
);
1231 hw
->mix_buf
.pos
= 0;
1235 dolog("played=%zu\n", played
);
1239 hw
->ts_helper
+= played
;
1240 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1243 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1244 if (!sw
->active
&& sw
->empty
) {
1248 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1249 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1250 played
, sw
->total_hw_samples_mixed
);
1251 played
= sw
->total_hw_samples_mixed
;
1254 sw
->total_hw_samples_mixed
-= played
;
1256 if (!sw
->total_hw_samples_mixed
) {
1263 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1267 if (hw
->pcm_ops
->run_buffer_in
) {
1268 hw
->pcm_ops
->run_buffer_in(hw
);
1273 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1274 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1276 assert(size
% hw
->info
.bytes_per_frame
== 0);
1281 proc
= audio_pcm_hw_conv_in(hw
, buf
, size
/ hw
->info
.bytes_per_frame
);
1285 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1291 static void audio_run_in (AudioState
*s
)
1293 HWVoiceIn
*hw
= NULL
;
1295 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1296 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1297 /* there is exactly 1 sw for each hw with no mixeng */
1298 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1300 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1306 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1308 size_t captured
= 0, min
;
1310 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1311 captured
= audio_pcm_hw_run_in(
1312 hw
, hw
->conv_buf
.size
- audio_pcm_hw_get_live_in(hw
));
1314 replay_audio_in(&captured
, hw
->conv_buf
.buffer
, &hw
->conv_buf
.pos
,
1317 min
= audio_pcm_hw_find_min_in (hw
);
1318 hw
->total_samples_captured
+= captured
- min
;
1319 hw
->ts_helper
+= captured
;
1321 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1322 sw
->total_hw_samples_acquired
-= min
;
1325 size_t sw_avail
= audio_get_avail(sw
);
1328 avail
= st_rate_frames_out(sw
->rate
, sw_avail
);
1330 avail
= MIN(avail
, sw
->resample_buf
.size
);
1331 sw
->callback
.fn(sw
->callback
.opaque
,
1332 avail
* sw
->info
.bytes_per_frame
);
1339 static void audio_run_capture (AudioState
*s
)
1341 CaptureVoiceOut
*cap
;
1343 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1344 size_t live
, rpos
, captured
;
1345 HWVoiceOut
*hw
= &cap
->hw
;
1348 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1349 rpos
= hw
->mix_buf
.pos
;
1351 size_t left
= hw
->mix_buf
.size
- rpos
;
1352 size_t to_capture
= MIN(live
, left
);
1353 struct st_sample
*src
;
1354 struct capture_callback
*cb
;
1356 src
= hw
->mix_buf
.buffer
+ rpos
;
1357 hw
->clip (cap
->buf
, src
, to_capture
);
1358 mixeng_clear (src
, to_capture
);
1360 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1361 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1362 to_capture
* hw
->info
.bytes_per_frame
);
1364 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
.size
;
1367 hw
->mix_buf
.pos
= rpos
;
1369 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1370 if (!sw
->active
&& sw
->empty
) {
1374 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1375 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1376 captured
, sw
->total_hw_samples_mixed
);
1377 captured
= sw
->total_hw_samples_mixed
;
1380 sw
->total_hw_samples_mixed
-= captured
;
1381 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1386 void audio_run(AudioState
*s
, const char *msg
)
1390 audio_run_capture(s
);
1394 static double prevtime
;
1398 if (gettimeofday (&tv
, NULL
)) {
1399 perror ("audio_run: gettimeofday");
1403 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1404 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1405 prevtime
= currtime
;
1410 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1412 if (unlikely(!hw
->buf_emul
)) {
1413 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1414 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1415 hw
->pos_emul
= hw
->pending_emul
= 0;
1418 while (hw
->pending_emul
< hw
->size_emul
) {
1419 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1420 hw
->size_emul
- hw
->pending_emul
);
1421 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1423 hw
->pending_emul
+= read
;
1424 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1425 if (read
< read_len
) {
1431 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1435 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1436 assert(start
< hw
->size_emul
);
1438 *size
= MIN(*size
, hw
->pending_emul
);
1439 *size
= MIN(*size
, hw
->size_emul
- start
);
1440 return hw
->buf_emul
+ start
;
1443 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1445 assert(size
<= hw
->pending_emul
);
1446 hw
->pending_emul
-= size
;
1449 size_t audio_generic_buffer_get_free(HWVoiceOut
*hw
)
1452 return hw
->size_emul
- hw
->pending_emul
;
1454 return hw
->samples
* hw
->info
.bytes_per_frame
;
1458 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1460 while (hw
->pending_emul
) {
1461 size_t write_len
, written
, start
;
1463 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1464 assert(start
< hw
->size_emul
);
1466 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1468 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1469 hw
->pending_emul
-= written
;
1471 if (written
< write_len
) {
1477 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1479 if (unlikely(!hw
->buf_emul
)) {
1480 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1481 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1482 hw
->pos_emul
= hw
->pending_emul
= 0;
1485 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1486 hw
->size_emul
- hw
->pos_emul
);
1487 return hw
->buf_emul
+ hw
->pos_emul
;
1490 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1492 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1493 size
+ hw
->pending_emul
<= hw
->size_emul
);
1495 hw
->pending_emul
+= size
;
1496 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1501 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1505 if (hw
->pcm_ops
->buffer_get_free
) {
1506 size_t free
= hw
->pcm_ops
->buffer_get_free(hw
);
1508 size
= MIN(size
, free
);
1511 while (total
< size
) {
1512 size_t dst_size
= size
- total
;
1513 size_t copy_size
, proc
;
1514 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1516 if (dst_size
== 0) {
1520 copy_size
= MIN(size
- total
, dst_size
);
1522 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1524 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1527 if (proc
== 0 || proc
< copy_size
) {
1535 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1539 if (hw
->pcm_ops
->run_buffer_in
) {
1540 hw
->pcm_ops
->run_buffer_in(hw
);
1543 while (total
< size
) {
1544 size_t src_size
= size
- total
;
1545 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1547 if (src_size
== 0) {
1551 memcpy((char *)buf
+ total
, src
, src_size
);
1552 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1559 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1560 Audiodev
*dev
, Error
**errp
)
1562 Error
*local_err
= NULL
;
1564 s
->drv_opaque
= drv
->init(dev
, &local_err
);
1566 if (s
->drv_opaque
) {
1567 if (!drv
->pcm_ops
->get_buffer_in
) {
1568 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1569 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1571 if (!drv
->pcm_ops
->get_buffer_out
) {
1572 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1573 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1576 audio_init_nb_voices_out(s
, drv
, 1);
1577 audio_init_nb_voices_in(s
, drv
, 0);
1582 error_propagate(errp
, local_err
);
1584 error_setg(errp
, "Could not init `%s' audio driver", drv
->name
);
1590 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1593 AudioState
*s
= opaque
;
1594 HWVoiceOut
*hwo
= NULL
;
1595 HWVoiceIn
*hwi
= NULL
;
1597 s
->vm_running
= running
;
1598 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1599 if (hwo
->pcm_ops
->enable_out
) {
1600 hwo
->pcm_ops
->enable_out(hwo
, running
);
1604 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1605 if (hwi
->pcm_ops
->enable_in
) {
1606 hwi
->pcm_ops
->enable_in(hwi
, running
);
1609 audio_reset_timer (s
);
1612 static void free_audio_state(AudioState
*s
)
1614 HWVoiceOut
*hwo
, *hwon
;
1615 HWVoiceIn
*hwi
, *hwin
;
1617 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1620 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1621 hwo
->pcm_ops
->enable_out(hwo
, false);
1623 hwo
->pcm_ops
->fini_out (hwo
);
1625 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1626 CaptureVoiceOut
*cap
= sc
->cap
;
1627 struct capture_callback
*cb
;
1629 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1630 cb
->ops
.destroy (cb
->opaque
);
1633 QLIST_REMOVE(hwo
, entries
);
1636 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1637 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1638 hwi
->pcm_ops
->enable_in(hwi
, false);
1640 hwi
->pcm_ops
->fini_in (hwi
);
1641 QLIST_REMOVE(hwi
, entries
);
1645 s
->drv
->fini (s
->drv_opaque
);
1650 qapi_free_Audiodev(s
->dev
);
1662 void audio_cleanup(void)
1664 default_audio_state
= NULL
;
1665 while (!QTAILQ_EMPTY(&audio_states
)) {
1666 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1667 QTAILQ_REMOVE(&audio_states
, s
, list
);
1668 free_audio_state(s
);
1672 static bool vmstate_audio_needed(void *opaque
)
1675 * Never needed, this vmstate only exists in case
1676 * an old qemu sends it to us.
1681 static const VMStateDescription vmstate_audio
= {
1684 .minimum_version_id
= 1,
1685 .needed
= vmstate_audio_needed
,
1686 .fields
= (VMStateField
[]) {
1687 VMSTATE_END_OF_LIST()
1691 void audio_create_default_audiodevs(void)
1693 for (int i
= 0; audio_prio_list
[i
]; i
++) {
1694 if (audio_driver_lookup(audio_prio_list
[i
])) {
1695 QDict
*dict
= qdict_new();
1696 Audiodev
*dev
= NULL
;
1699 qdict_put_str(dict
, "driver", audio_prio_list
[i
]);
1700 qdict_put_str(dict
, "id", "#default");
1702 v
= qobject_input_visitor_new_keyval(QOBJECT(dict
));
1703 qobject_unref(dict
);
1704 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1707 audio_define_default(dev
, &error_abort
);
1713 * if we have dev, this function was called because of an -audiodev argument =>
1714 * initialize a new state with it
1715 * if dev == NULL => legacy implicit initialization, return the already created
1716 * state or create a new one
1718 static AudioState
*audio_init(Audiodev
*dev
, Error
**errp
)
1720 static bool atexit_registered
;
1722 const char *drvname
;
1723 VMChangeStateEntry
*vmse
;
1725 struct audio_driver
*driver
;
1727 s
= g_new0(AudioState
, 1);
1729 QLIST_INIT (&s
->hw_head_out
);
1730 QLIST_INIT (&s
->hw_head_in
);
1731 QLIST_INIT (&s
->cap_head
);
1732 if (!atexit_registered
) {
1733 atexit(audio_cleanup
);
1734 atexit_registered
= true;
1737 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1740 /* -audiodev option */
1742 drvname
= AudiodevDriver_str(dev
->driver
);
1743 driver
= audio_driver_lookup(drvname
);
1745 done
= !audio_driver_init(s
, driver
, dev
, errp
);
1747 error_setg(errp
, "Unknown audio driver `%s'\n", drvname
);
1753 assert(!default_audio_state
);
1755 AudiodevListEntry
*e
= QSIMPLEQ_FIRST(&default_audiodevs
);
1757 error_setg(errp
, "no default audio driver available");
1760 s
->dev
= dev
= e
->dev
;
1761 QSIMPLEQ_REMOVE_HEAD(&default_audiodevs
, next
);
1763 drvname
= AudiodevDriver_str(dev
->driver
);
1764 driver
= audio_driver_lookup(drvname
);
1765 if (!audio_driver_init(s
, driver
, dev
, NULL
)) {
1768 qapi_free_Audiodev(dev
);
1773 if (dev
->timer_period
<= 0) {
1774 s
->period_ticks
= 1;
1776 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1779 vmse
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1781 dolog ("warning: Could not register change state handler\n"
1782 "(Audio can continue looping even after stopping the VM)\n");
1785 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1786 QLIST_INIT (&s
->card_head
);
1787 vmstate_register_any(NULL
, &vmstate_audio
, s
);
1791 free_audio_state(s
);
1795 AudioState
*audio_get_default_audio_state(Error
**errp
)
1797 if (!default_audio_state
) {
1798 default_audio_state
= audio_init(NULL
, errp
);
1799 if (!default_audio_state
) {
1800 if (!QSIMPLEQ_EMPTY(&audiodevs
)) {
1801 error_append_hint(errp
, "Perhaps you wanted to use -audio or set audiodev=%s?\n",
1802 QSIMPLEQ_FIRST(&audiodevs
)->dev
->id
);
1807 return default_audio_state
;
1810 bool AUD_register_card (const char *name
, QEMUSoundCard
*card
, Error
**errp
)
1813 card
->state
= audio_get_default_audio_state(errp
);
1819 card
->name
= g_strdup (name
);
1820 memset (&card
->entries
, 0, sizeof (card
->entries
));
1821 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1826 void AUD_remove_card (QEMUSoundCard
*card
)
1828 QLIST_REMOVE (card
, entries
);
1829 g_free (card
->name
);
1832 static struct audio_pcm_ops capture_pcm_ops
;
1834 CaptureVoiceOut
*AUD_add_capture(
1836 struct audsettings
*as
,
1837 struct audio_capture_ops
*ops
,
1841 CaptureVoiceOut
*cap
;
1842 struct capture_callback
*cb
;
1845 error_report("Capturing without setting an audiodev is not supported");
1849 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1850 dolog("Can't capture with mixeng disabled\n");
1854 if (audio_validate_settings (as
)) {
1855 dolog ("Invalid settings were passed when trying to add capture\n");
1856 audio_print_settings (as
);
1860 cb
= g_malloc0(sizeof(*cb
));
1862 cb
->opaque
= cb_opaque
;
1864 cap
= audio_pcm_capture_find_specific(s
, as
);
1866 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1870 cap
= g_malloc0(sizeof(*cap
));
1874 hw
->pcm_ops
= &capture_pcm_ops
;
1875 QLIST_INIT (&hw
->sw_head
);
1876 QLIST_INIT (&cap
->cb_head
);
1878 /* XXX find a more elegant way */
1879 hw
->samples
= 4096 * 4;
1880 audio_pcm_hw_alloc_resources_out(hw
);
1882 audio_pcm_init_info (&hw
->info
, as
);
1884 cap
->buf
= g_malloc0_n(hw
->mix_buf
.size
, hw
->info
.bytes_per_frame
);
1886 if (hw
->info
.is_float
) {
1887 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1889 hw
->clip
= mixeng_clip
1890 [hw
->info
.nchannels
== 2]
1891 [hw
->info
.is_signed
]
1892 [hw
->info
.swap_endianness
]
1893 [audio_bits_to_index(hw
->info
.bits
)];
1896 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1897 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1899 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1900 audio_attach_capture (hw
);
1907 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1909 struct capture_callback
*cb
;
1911 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1912 if (cb
->opaque
== cb_opaque
) {
1913 cb
->ops
.destroy (cb_opaque
);
1914 QLIST_REMOVE (cb
, entries
);
1917 if (!cap
->cb_head
.lh_first
) {
1918 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1921 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1922 #ifdef DEBUG_CAPTURE
1923 dolog ("freeing %s\n", sw
->name
);
1926 sw1
= sw
->entries
.le_next
;
1928 st_rate_stop (sw
->rate
);
1931 QLIST_REMOVE (sw
, entries
);
1932 QLIST_REMOVE (sc
, entries
);
1936 QLIST_REMOVE (cap
, entries
);
1937 g_free(cap
->hw
.mix_buf
.buffer
);
1946 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1948 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1949 audio_set_volume_out(sw
, &vol
);
1952 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1955 HWVoiceOut
*hw
= sw
->hw
;
1957 sw
->vol
.mute
= vol
->mute
;
1958 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1959 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1962 if (hw
->pcm_ops
->volume_out
) {
1963 hw
->pcm_ops
->volume_out(hw
, vol
);
1968 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1970 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1971 audio_set_volume_in(sw
, &vol
);
1974 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1977 HWVoiceIn
*hw
= sw
->hw
;
1979 sw
->vol
.mute
= vol
->mute
;
1980 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1981 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1984 if (hw
->pcm_ops
->volume_in
) {
1985 hw
->pcm_ops
->volume_in(hw
, vol
);
1990 void audio_create_pdos(Audiodev
*dev
)
1992 switch (dev
->driver
) {
1993 #define CASE(DRIVER, driver, pdo_name) \
1994 case AUDIODEV_DRIVER_##DRIVER: \
1995 if (!dev->u.driver.in) { \
1996 dev->u.driver.in = g_malloc0( \
1997 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1999 if (!dev->u.driver.out) { \
2000 dev->u.driver.out = g_malloc0( \
2001 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2006 #ifdef CONFIG_AUDIO_ALSA
2007 CASE(ALSA
, alsa
, Alsa
);
2009 #ifdef CONFIG_AUDIO_COREAUDIO
2010 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2012 #ifdef CONFIG_DBUS_DISPLAY
2015 #ifdef CONFIG_AUDIO_DSOUND
2016 CASE(DSOUND
, dsound
, );
2018 #ifdef CONFIG_AUDIO_JACK
2019 CASE(JACK
, jack
, Jack
);
2021 #ifdef CONFIG_AUDIO_OSS
2022 CASE(OSS
, oss
, Oss
);
2024 #ifdef CONFIG_AUDIO_PA
2027 #ifdef CONFIG_AUDIO_PIPEWIRE
2028 CASE(PIPEWIRE
, pipewire
, Pipewire
);
2030 #ifdef CONFIG_AUDIO_SDL
2031 CASE(SDL
, sdl
, Sdl
);
2033 #ifdef CONFIG_AUDIO_SNDIO
2034 CASE(SNDIO
, sndio
, );
2037 CASE(SPICE
, spice
, );
2041 case AUDIODEV_DRIVER__MAX
:
2046 static void audio_validate_per_direction_opts(
2047 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2049 if (!pdo
->has_mixing_engine
) {
2050 pdo
->has_mixing_engine
= true;
2051 pdo
->mixing_engine
= true;
2053 if (!pdo
->has_fixed_settings
) {
2054 pdo
->has_fixed_settings
= true;
2055 pdo
->fixed_settings
= pdo
->mixing_engine
;
2057 if (!pdo
->fixed_settings
&&
2058 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2060 "You can't use frequency, channels or format with fixed-settings=off");
2063 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2064 error_setg(errp
, "You can't use fixed-settings without mixeng");
2068 if (!pdo
->has_frequency
) {
2069 pdo
->has_frequency
= true;
2070 pdo
->frequency
= 44100;
2072 if (!pdo
->has_channels
) {
2073 pdo
->has_channels
= true;
2076 if (!pdo
->has_voices
) {
2077 pdo
->has_voices
= true;
2078 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2080 if (!pdo
->has_format
) {
2081 pdo
->has_format
= true;
2082 pdo
->format
= AUDIO_FORMAT_S16
;
2086 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2090 audio_create_pdos(dev
);
2092 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2094 error_propagate(errp
, err
);
2098 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2100 error_propagate(errp
, err
);
2104 if (!dev
->has_timer_period
) {
2105 dev
->has_timer_period
= true;
2106 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2110 void audio_help(void)
2114 printf("Available audio drivers:\n");
2116 for (i
= 0; i
< AUDIODEV_DRIVER__MAX
; i
++) {
2117 audio_driver
*driver
= audio_driver_lookup(AudiodevDriver_str(i
));
2119 printf("%s\n", driver
->name
);
2124 void audio_parse_option(const char *opt
)
2126 Audiodev
*dev
= NULL
;
2128 if (is_help_option(opt
)) {
2132 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2133 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2139 void audio_define(Audiodev
*dev
)
2141 AudiodevListEntry
*e
;
2143 audio_validate_opts(dev
, &error_fatal
);
2145 e
= g_new0(AudiodevListEntry
, 1);
2147 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2150 void audio_define_default(Audiodev
*dev
, Error
**errp
)
2152 AudiodevListEntry
*e
;
2154 audio_validate_opts(dev
, errp
);
2156 e
= g_new0(AudiodevListEntry
, 1);
2158 QSIMPLEQ_INSERT_TAIL(&default_audiodevs
, e
, next
);
2161 void audio_init_audiodevs(void)
2163 AudiodevListEntry
*e
;
2165 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2166 audio_init(e
->dev
, &error_fatal
);
2170 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2172 return (audsettings
) {
2173 .freq
= pdo
->frequency
,
2174 .nchannels
= pdo
->channels
,
2176 .endianness
= AUDIO_HOST_ENDIANNESS
,
2180 int audioformat_bytes_per_sample(AudioFormat fmt
)
2183 case AUDIO_FORMAT_U8
:
2184 case AUDIO_FORMAT_S8
:
2187 case AUDIO_FORMAT_U16
:
2188 case AUDIO_FORMAT_S16
:
2191 case AUDIO_FORMAT_U32
:
2192 case AUDIO_FORMAT_S32
:
2193 case AUDIO_FORMAT_F32
:
2196 case AUDIO_FORMAT__MAX
:
2203 /* frames = freq * usec / 1e6 */
2204 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2205 audsettings
*as
, int def_usecs
)
2207 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2208 return (as
->freq
* usecs
+ 500000) / 1000000;
2211 /* samples = channels * frames = channels * freq * usec / 1e6 */
2212 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2213 audsettings
*as
, int def_usecs
)
2215 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2219 * bytes = bytes_per_sample * samples =
2220 * bytes_per_sample * channels * freq * usec / 1e6
2222 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2223 audsettings
*as
, int def_usecs
)
2225 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2226 audioformat_bytes_per_sample(as
->fmt
);
2229 AudioState
*audio_state_by_name(const char *name
, Error
**errp
)
2232 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2234 if (strcmp(name
, s
->dev
->id
) == 0) {
2238 error_setg(errp
, "audiodev '%s' not found", name
);
2242 const char *audio_get_id(QEMUSoundCard
*card
)
2245 assert(card
->state
->dev
);
2246 return card
->state
->dev
->id
;
2252 const char *audio_application_name(void)
2254 const char *vm_name
;
2256 vm_name
= qemu_get_vm_name();
2257 return vm_name
? vm_name
: "qemu";
2260 void audio_rate_start(RateCtl
*rate
)
2262 memset(rate
, 0, sizeof(RateCtl
));
2263 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2266 size_t audio_rate_peek_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
)
2273 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2274 ticks
= now
- rate
->start_ticks
;
2275 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2276 frames
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2277 if (frames
< 0 || frames
> 65536) {
2278 AUD_log(NULL
, "Resetting rate control (%" PRId64
" frames)\n", frames
);
2279 audio_rate_start(rate
);
2283 return frames
* info
->bytes_per_frame
;
2286 void audio_rate_add_bytes(RateCtl
*rate
, size_t bytes_used
)
2288 rate
->bytes_sent
+= bytes_used
;
2291 size_t audio_rate_get_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
,
2296 bytes
= audio_rate_peek_bytes(rate
, info
);
2297 bytes
= MIN(bytes
, bytes_avail
);
2298 audio_rate_add_bytes(rate
, bytes
);
2303 AudiodevList
*qmp_query_audiodevs(Error
**errp
)
2305 AudiodevList
*ret
= NULL
;
2306 AudiodevListEntry
*e
;
2307 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2308 QAPI_LIST_PREPEND(ret
, QAPI_CLONE(Audiodev
, e
->dev
));