4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "migration/vmstate.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qapi-visit-audio.h"
33 #include "qemu/cutils.h"
34 #include "qemu/module.h"
35 #include "qemu/help_option.h"
36 #include "sysemu/sysemu.h"
37 #include "sysemu/replay.h"
38 #include "sysemu/runstate.h"
39 #include "ui/qemu-spice.h"
42 #define AUDIO_CAP "audio"
43 #include "audio_int.h"
45 /* #define DEBUG_LIVE */
46 /* #define DEBUG_OUT */
47 /* #define DEBUG_CAPTURE */
48 /* #define DEBUG_POLL */
50 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
53 /* Order of CONFIG_AUDIO_DRIVERS is import.
54 The 1st one is the one used by default, that is the reason
55 that we generate the list.
57 const char *audio_prio_list
[] = {
65 static QLIST_HEAD(, audio_driver
) audio_drivers
;
66 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
68 void audio_driver_register(audio_driver
*drv
)
70 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
73 audio_driver
*audio_driver_lookup(const char *name
)
75 struct audio_driver
*d
;
77 QLIST_FOREACH(d
, &audio_drivers
, next
) {
78 if (strcmp(name
, d
->name
) == 0) {
83 audio_module_load_one(name
);
84 QLIST_FOREACH(d
, &audio_drivers
, next
) {
85 if (strcmp(name
, d
->name
) == 0) {
93 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
94 QTAILQ_HEAD_INITIALIZER(audio_states
);
96 const struct mixeng_volume nominal_volume
= {
107 static bool legacy_config
= true;
109 int audio_bug (const char *funcname
, int cond
)
114 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
117 AUD_log (NULL
, "Save all your work and restart without audio\n");
118 AUD_log (NULL
, "I am sorry\n");
120 AUD_log (NULL
, "Context:\n");
126 static inline int audio_bits_to_index (int bits
)
139 audio_bug ("bits_to_index", 1);
140 AUD_log (NULL
, "invalid bits %d\n", bits
);
145 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
151 cond
= !nmemb
|| !size
;
155 if (audio_bug ("audio_calloc", cond
)) {
156 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
158 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
162 return g_malloc0 (len
);
165 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
168 fprintf(stderr
, "%s: ", cap
);
171 vfprintf(stderr
, fmt
, ap
);
174 void AUD_log (const char *cap
, const char *fmt
, ...)
179 AUD_vlog (cap
, fmt
, ap
);
183 static void audio_print_settings (struct audsettings
*as
)
185 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
188 case AUDIO_FORMAT_S8
:
189 AUD_log (NULL
, "S8");
191 case AUDIO_FORMAT_U8
:
192 AUD_log (NULL
, "U8");
194 case AUDIO_FORMAT_S16
:
195 AUD_log (NULL
, "S16");
197 case AUDIO_FORMAT_U16
:
198 AUD_log (NULL
, "U16");
200 case AUDIO_FORMAT_S32
:
201 AUD_log (NULL
, "S32");
203 case AUDIO_FORMAT_U32
:
204 AUD_log (NULL
, "U32");
206 case AUDIO_FORMAT_F32
:
207 AUD_log (NULL
, "F32");
210 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
214 AUD_log (NULL
, " endianness=");
215 switch (as
->endianness
) {
217 AUD_log (NULL
, "little");
220 AUD_log (NULL
, "big");
223 AUD_log (NULL
, "invalid");
226 AUD_log (NULL
, "\n");
229 static int audio_validate_settings (struct audsettings
*as
)
233 invalid
= as
->nchannels
< 1;
234 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
237 case AUDIO_FORMAT_S8
:
238 case AUDIO_FORMAT_U8
:
239 case AUDIO_FORMAT_S16
:
240 case AUDIO_FORMAT_U16
:
241 case AUDIO_FORMAT_S32
:
242 case AUDIO_FORMAT_U32
:
243 case AUDIO_FORMAT_F32
:
250 invalid
|= as
->freq
<= 0;
251 return invalid
? -1 : 0;
254 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
257 bool is_signed
= false, is_float
= false;
260 case AUDIO_FORMAT_S8
:
263 case AUDIO_FORMAT_U8
:
266 case AUDIO_FORMAT_S16
:
269 case AUDIO_FORMAT_U16
:
273 case AUDIO_FORMAT_F32
:
276 case AUDIO_FORMAT_S32
:
279 case AUDIO_FORMAT_U32
:
286 return info
->freq
== as
->freq
287 && info
->nchannels
== as
->nchannels
288 && info
->is_signed
== is_signed
289 && info
->is_float
== is_float
290 && info
->bits
== bits
291 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
294 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
297 bool is_signed
= false, is_float
= false;
300 case AUDIO_FORMAT_S8
:
303 case AUDIO_FORMAT_U8
:
307 case AUDIO_FORMAT_S16
:
310 case AUDIO_FORMAT_U16
:
315 case AUDIO_FORMAT_F32
:
318 case AUDIO_FORMAT_S32
:
321 case AUDIO_FORMAT_U32
:
330 info
->freq
= as
->freq
;
332 info
->is_signed
= is_signed
;
333 info
->is_float
= is_float
;
334 info
->nchannels
= as
->nchannels
;
335 info
->bytes_per_frame
= as
->nchannels
* mul
;
336 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
337 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
340 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
346 if (info
->is_signed
|| info
->is_float
) {
347 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
349 switch (info
->bits
) {
351 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
360 if (info
->swap_endianness
) {
364 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
374 int32_t s
= INT32_MAX
;
376 if (info
->swap_endianness
) {
380 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
387 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
397 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
404 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
405 struct audsettings
*as
)
407 CaptureVoiceOut
*cap
;
409 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
410 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
417 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
419 struct capture_callback
*cb
;
422 dolog ("notification %d sent\n", cmd
);
424 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
425 cb
->ops
.notify (cb
->opaque
, cmd
);
429 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
431 if (cap
->hw
.enabled
!= enabled
) {
432 audcnotification_e cmd
;
433 cap
->hw
.enabled
= enabled
;
434 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
435 audio_notify_capture (cap
, cmd
);
439 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
441 HWVoiceOut
*hw
= &cap
->hw
;
445 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
451 audio_capture_maybe_changed (cap
, enabled
);
454 static void audio_detach_capture (HWVoiceOut
*hw
)
456 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
459 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
460 SWVoiceOut
*sw
= &sc
->sw
;
461 CaptureVoiceOut
*cap
= sc
->cap
;
462 int was_active
= sw
->active
;
465 st_rate_stop (sw
->rate
);
469 QLIST_REMOVE (sw
, entries
);
470 QLIST_REMOVE (sc
, entries
);
473 /* We have removed soft voice from the capture:
474 this might have changed the overall status of the capture
475 since this might have been the only active voice */
476 audio_recalc_and_notify_capture (cap
);
482 static int audio_attach_capture (HWVoiceOut
*hw
)
484 AudioState
*s
= hw
->s
;
485 CaptureVoiceOut
*cap
;
487 audio_detach_capture (hw
);
488 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
491 HWVoiceOut
*hw_cap
= &cap
->hw
;
493 sc
= g_malloc0(sizeof(*sc
));
500 sw
->active
= hw
->enabled
;
501 sw
->conv
= noop_conv
;
502 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
503 sw
->vol
= nominal_volume
;
504 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
506 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
510 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
511 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
513 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
514 hw
, sw
->info
.freq
, sw
->info
.bits
,
516 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
519 audio_capture_maybe_changed (cap
, 1);
526 * Hard voice (capture)
528 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
531 size_t m
= hw
->total_samples_captured
;
533 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
535 m
= MIN (m
, sw
->total_hw_samples_acquired
);
541 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
543 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
544 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
545 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
551 static size_t audio_pcm_hw_conv_in(HWVoiceIn
*hw
, void *pcm_buf
, size_t samples
)
554 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
557 uint8_t *src
= advance(pcm_buf
, conv
* hw
->info
.bytes_per_frame
);
558 size_t proc
= MIN(samples
, conv_buf
->size
- conv_buf
->pos
);
560 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, src
, proc
);
561 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
570 * Soft voice (capture)
572 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
574 HWVoiceIn
*hw
= sw
->hw
;
575 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
576 struct st_sample
*src
, *dst
= sw
->buf
;
578 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
582 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
583 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
587 rpos
= audio_ring_posb(hw
->conv_buf
->pos
, live
, hw
->conv_buf
->size
);
589 samples
= size
/ sw
->info
.bytes_per_frame
;
591 swlim
= (live
* sw
->ratio
) >> 32;
592 swlim
= MIN (swlim
, samples
);
595 src
= hw
->conv_buf
->samples
+ rpos
;
596 if (hw
->conv_buf
->pos
> rpos
) {
597 isamp
= hw
->conv_buf
->pos
- rpos
;
599 isamp
= hw
->conv_buf
->size
- rpos
;
607 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
609 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
615 if (!hw
->pcm_ops
->volume_in
) {
616 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
619 sw
->clip (buf
, sw
->buf
, ret
);
620 sw
->total_hw_samples_acquired
+= total
;
621 return ret
* sw
->info
.bytes_per_frame
;
625 * Hard voice (playback)
627 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
633 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
634 if (sw
->active
|| !sw
->empty
) {
635 m
= MIN (m
, sw
->total_hw_samples_mixed
);
644 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
649 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
657 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
658 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
666 static size_t audio_pcm_hw_get_free(HWVoiceOut
*hw
)
668 return (hw
->pcm_ops
->buffer_get_free
? hw
->pcm_ops
->buffer_get_free(hw
) :
669 INT_MAX
) / hw
->info
.bytes_per_frame
;
672 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
675 size_t pos
= hw
->mix_buf
->pos
;
678 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
679 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
680 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
681 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
683 hw
->clip(dst
, src
, samples_to_clip
);
685 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
686 len
-= samples_to_clip
;
687 clipped
+= samples_to_clip
;
692 * Soft voice (playback)
694 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
696 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, blck
;
698 size_t ret
= 0, pos
= 0, total
= 0;
704 hwsamples
= sw
->hw
->mix_buf
->size
;
706 live
= sw
->total_hw_samples_mixed
;
707 if (audio_bug(__func__
, live
> hwsamples
)) {
708 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
712 if (live
== hwsamples
) {
714 dolog ("%s is full %zu\n", sw
->name
, live
);
719 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
721 dead
= hwsamples
- live
;
722 hw_free
= audio_pcm_hw_get_free(sw
->hw
);
723 hw_free
= hw_free
> live
? hw_free
- live
: 0;
724 samples
= ((int64_t)MIN(dead
, hw_free
) << 32) / sw
->ratio
;
725 samples
= MIN(samples
, size
/ sw
->info
.bytes_per_frame
);
727 sw
->conv(sw
->buf
, buf
, samples
);
729 if (!sw
->hw
->pcm_ops
->volume_out
) {
730 mixeng_volume(sw
->buf
, samples
, &sw
->vol
);
735 dead
= hwsamples
- live
;
736 left
= hwsamples
- wpos
;
737 blck
= MIN (dead
, left
);
746 sw
->hw
->mix_buf
->samples
+ wpos
,
754 wpos
= (wpos
+ osamp
) % hwsamples
;
758 sw
->total_hw_samples_mixed
+= total
;
759 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
763 "%s: write size %zu ret %zu total sw %zu\n",
765 size
/ sw
->info
.bytes_per_frame
,
767 sw
->total_hw_samples_mixed
771 return ret
* sw
->info
.bytes_per_frame
;
775 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
777 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
778 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
784 #include "audio_template.h"
786 #include "audio_template.h"
791 static int audio_is_timer_needed(AudioState
*s
)
793 HWVoiceIn
*hwi
= NULL
;
794 HWVoiceOut
*hwo
= NULL
;
796 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
797 if (!hwo
->poll_mode
) {
801 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
802 if (!hwi
->poll_mode
) {
809 static void audio_reset_timer (AudioState
*s
)
811 if (audio_is_timer_needed(s
)) {
812 timer_mod_anticipate_ns(s
->ts
,
813 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
814 if (!s
->timer_running
) {
815 s
->timer_running
= true;
816 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
817 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
821 if (s
->timer_running
) {
822 s
->timer_running
= false;
823 trace_audio_timer_stop();
828 static void audio_timer (void *opaque
)
831 AudioState
*s
= opaque
;
833 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
834 diff
= now
- s
->timer_last
;
835 if (diff
> s
->period_ticks
* 3 / 2) {
836 trace_audio_timer_delayed(diff
/ SCALE_MS
);
840 audio_run(s
, "timer");
841 audio_reset_timer(s
);
847 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
852 /* XXX: Consider options */
858 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
862 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
863 return audio_pcm_sw_write(sw
, buf
, size
);
865 return hw
->pcm_ops
->write(hw
, buf
, size
);
869 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
874 /* XXX: Consider options */
880 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
884 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
885 return audio_pcm_sw_read(sw
, buf
, size
);
887 return hw
->pcm_ops
->read(hw
, buf
, size
);
891 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
893 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
896 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
905 if (sw
->active
!= on
) {
906 AudioState
*s
= sw
->s
;
911 hw
->pending_disable
= 0;
915 if (hw
->pcm_ops
->enable_out
) {
916 hw
->pcm_ops
->enable_out(hw
, true);
918 audio_reset_timer (s
);
925 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
926 temp_sw
= temp_sw
->entries
.le_next
) {
927 nb_active
+= temp_sw
->active
!= 0;
930 hw
->pending_disable
= nb_active
== 1;
934 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
935 sc
->sw
.active
= hw
->enabled
;
937 audio_capture_maybe_changed (sc
->cap
, 1);
944 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
953 if (sw
->active
!= on
) {
954 AudioState
*s
= sw
->s
;
961 if (hw
->pcm_ops
->enable_in
) {
962 hw
->pcm_ops
->enable_in(hw
, true);
964 audio_reset_timer (s
);
967 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
972 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
973 temp_sw
= temp_sw
->entries
.le_next
) {
974 nb_active
+= temp_sw
->active
!= 0;
977 if (nb_active
== 1) {
979 if (hw
->pcm_ops
->enable_in
) {
980 hw
->pcm_ops
->enable_in(hw
, false);
989 static size_t audio_get_avail (SWVoiceIn
*sw
)
997 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
998 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
999 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
1000 sw
->hw
->conv_buf
->size
);
1005 "%s: get_avail live %zu ret %" PRId64
"\n",
1007 live
, (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
1010 return (((int64_t) live
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1013 static size_t audio_sw_bytes_free(SWVoiceOut
*sw
, size_t free
)
1015 return (((int64_t)free
<< 32) / sw
->ratio
) * sw
->info
.bytes_per_frame
;
1018 static size_t audio_get_free(SWVoiceOut
*sw
)
1026 live
= sw
->total_hw_samples_mixed
;
1028 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1029 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1030 sw
->hw
->mix_buf
->size
);
1034 dead
= sw
->hw
->mix_buf
->size
- live
;
1037 dolog("%s: get_free live %zu dead %zu sw_bytes %zu\n",
1038 SW_NAME(sw
), live
, dead
, audio_sw_bytes_free(sw
, dead
));
1044 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1052 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1053 SWVoiceOut
*sw
= &sc
->sw
;
1058 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1059 size_t to_write
= MIN(till_end_of_hw
, n
);
1060 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1063 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1064 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1065 if (written
- bytes
) {
1066 dolog("Could not mix %zu bytes into a capture "
1067 "buffer, mixed %zu\n",
1072 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1077 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1078 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1079 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1082 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1087 size_t size
= live
* hw
->info
.bytes_per_frame
;
1089 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1095 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1097 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1099 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1100 decr
* hw
->info
.bytes_per_frame
) /
1101 hw
->info
.bytes_per_frame
;
1105 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1107 if (proc
== 0 || proc
< decr
) {
1112 if (hw
->pcm_ops
->run_buffer_out
) {
1113 hw
->pcm_ops
->run_buffer_out(hw
);
1119 static void audio_run_out (AudioState
*s
)
1121 HWVoiceOut
*hw
= NULL
;
1124 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1125 size_t played
, live
, prev_rpos
;
1126 size_t hw_free
= audio_pcm_hw_get_free(hw
);
1129 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1130 /* there is exactly 1 sw for each hw with no mixeng */
1131 sw
= hw
->sw_head
.lh_first
;
1133 if (hw
->pending_disable
) {
1135 hw
->pending_disable
= 0;
1136 if (hw
->pcm_ops
->enable_out
) {
1137 hw
->pcm_ops
->enable_out(hw
, false);
1142 sw
->callback
.fn(sw
->callback
.opaque
,
1143 hw_free
* sw
->info
.bytes_per_frame
);
1146 if (hw
->pcm_ops
->run_buffer_out
) {
1147 hw
->pcm_ops
->run_buffer_out(hw
);
1153 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1155 size_t sw_free
= audio_get_free(sw
);
1158 if (hw_free
> sw
->total_hw_samples_mixed
) {
1159 free
= audio_sw_bytes_free(sw
,
1160 MIN(sw_free
, hw_free
- sw
->total_hw_samples_mixed
));
1165 sw
->callback
.fn(sw
->callback
.opaque
, free
);
1170 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1175 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1176 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1180 if (hw
->pending_disable
&& !nb_live
) {
1183 dolog ("Disabling voice\n");
1186 hw
->pending_disable
= 0;
1187 if (hw
->pcm_ops
->enable_out
) {
1188 hw
->pcm_ops
->enable_out(hw
, false);
1190 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1192 audio_recalc_and_notify_capture (sc
->cap
);
1198 if (hw
->pcm_ops
->run_buffer_out
) {
1199 hw
->pcm_ops
->run_buffer_out(hw
);
1204 prev_rpos
= hw
->mix_buf
->pos
;
1205 played
= audio_pcm_hw_run_out(hw
, live
);
1206 replay_audio_out(&played
);
1207 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1208 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1209 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1210 hw
->mix_buf
->pos
= 0;
1214 dolog("played=%zu\n", played
);
1218 hw
->ts_helper
+= played
;
1219 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1222 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1223 if (!sw
->active
&& sw
->empty
) {
1227 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1228 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1229 played
, sw
->total_hw_samples_mixed
);
1230 played
= sw
->total_hw_samples_mixed
;
1233 sw
->total_hw_samples_mixed
-= played
;
1235 if (!sw
->total_hw_samples_mixed
) {
1242 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1246 if (hw
->pcm_ops
->run_buffer_in
) {
1247 hw
->pcm_ops
->run_buffer_in(hw
);
1252 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1253 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1255 assert(size
% hw
->info
.bytes_per_frame
== 0);
1260 proc
= audio_pcm_hw_conv_in(hw
, buf
, size
/ hw
->info
.bytes_per_frame
);
1264 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1270 static void audio_run_in (AudioState
*s
)
1272 HWVoiceIn
*hw
= NULL
;
1274 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1275 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1276 /* there is exactly 1 sw for each hw with no mixeng */
1277 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1279 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1285 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1287 size_t captured
= 0, min
;
1289 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1290 captured
= audio_pcm_hw_run_in(
1291 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1293 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1294 hw
->conv_buf
->size
);
1296 min
= audio_pcm_hw_find_min_in (hw
);
1297 hw
->total_samples_captured
+= captured
- min
;
1298 hw
->ts_helper
+= captured
;
1300 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1301 sw
->total_hw_samples_acquired
-= min
;
1306 avail
= audio_get_avail (sw
);
1308 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1315 static void audio_run_capture (AudioState
*s
)
1317 CaptureVoiceOut
*cap
;
1319 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1320 size_t live
, rpos
, captured
;
1321 HWVoiceOut
*hw
= &cap
->hw
;
1324 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1325 rpos
= hw
->mix_buf
->pos
;
1327 size_t left
= hw
->mix_buf
->size
- rpos
;
1328 size_t to_capture
= MIN(live
, left
);
1329 struct st_sample
*src
;
1330 struct capture_callback
*cb
;
1332 src
= hw
->mix_buf
->samples
+ rpos
;
1333 hw
->clip (cap
->buf
, src
, to_capture
);
1334 mixeng_clear (src
, to_capture
);
1336 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1337 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1338 to_capture
* hw
->info
.bytes_per_frame
);
1340 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1343 hw
->mix_buf
->pos
= rpos
;
1345 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1346 if (!sw
->active
&& sw
->empty
) {
1350 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1351 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1352 captured
, sw
->total_hw_samples_mixed
);
1353 captured
= sw
->total_hw_samples_mixed
;
1356 sw
->total_hw_samples_mixed
-= captured
;
1357 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1362 void audio_run(AudioState
*s
, const char *msg
)
1366 audio_run_capture(s
);
1370 static double prevtime
;
1374 if (gettimeofday (&tv
, NULL
)) {
1375 perror ("audio_run: gettimeofday");
1379 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1380 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1381 prevtime
= currtime
;
1386 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1388 if (unlikely(!hw
->buf_emul
)) {
1389 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1390 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1391 hw
->pos_emul
= hw
->pending_emul
= 0;
1394 while (hw
->pending_emul
< hw
->size_emul
) {
1395 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1396 hw
->size_emul
- hw
->pending_emul
);
1397 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1399 hw
->pending_emul
+= read
;
1400 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1401 if (read
< read_len
) {
1407 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1411 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1412 assert(start
< hw
->size_emul
);
1414 *size
= MIN(*size
, hw
->pending_emul
);
1415 *size
= MIN(*size
, hw
->size_emul
- start
);
1416 return hw
->buf_emul
+ start
;
1419 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1421 assert(size
<= hw
->pending_emul
);
1422 hw
->pending_emul
-= size
;
1425 size_t audio_generic_buffer_get_free(HWVoiceOut
*hw
)
1428 return hw
->size_emul
- hw
->pending_emul
;
1430 return hw
->samples
* hw
->info
.bytes_per_frame
;
1434 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1436 while (hw
->pending_emul
) {
1437 size_t write_len
, written
, start
;
1439 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1440 assert(start
< hw
->size_emul
);
1442 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1444 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1445 hw
->pending_emul
-= written
;
1447 if (written
< write_len
) {
1453 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1455 if (unlikely(!hw
->buf_emul
)) {
1456 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1457 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1458 hw
->pos_emul
= hw
->pending_emul
= 0;
1461 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1462 hw
->size_emul
- hw
->pos_emul
);
1463 return hw
->buf_emul
+ hw
->pos_emul
;
1466 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1468 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1469 size
+ hw
->pending_emul
<= hw
->size_emul
);
1471 hw
->pending_emul
+= size
;
1472 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1477 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1481 if (hw
->pcm_ops
->buffer_get_free
) {
1482 size_t free
= hw
->pcm_ops
->buffer_get_free(hw
);
1484 size
= MIN(size
, free
);
1487 while (total
< size
) {
1488 size_t dst_size
= size
- total
;
1489 size_t copy_size
, proc
;
1490 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1492 if (dst_size
== 0) {
1496 copy_size
= MIN(size
- total
, dst_size
);
1498 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1500 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1503 if (proc
== 0 || proc
< copy_size
) {
1511 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1515 if (hw
->pcm_ops
->run_buffer_in
) {
1516 hw
->pcm_ops
->run_buffer_in(hw
);
1519 while (total
< size
) {
1520 size_t src_size
= size
- total
;
1521 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1523 if (src_size
== 0) {
1527 memcpy((char *)buf
+ total
, src
, src_size
);
1528 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1535 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1536 bool msg
, Audiodev
*dev
)
1538 s
->drv_opaque
= drv
->init(dev
);
1540 if (s
->drv_opaque
) {
1541 if (!drv
->pcm_ops
->get_buffer_in
) {
1542 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1543 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1545 if (!drv
->pcm_ops
->get_buffer_out
) {
1546 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1547 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1550 audio_init_nb_voices_out(s
, drv
);
1551 audio_init_nb_voices_in(s
, drv
);
1556 dolog("Could not init `%s' audio driver\n", drv
->name
);
1562 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1565 AudioState
*s
= opaque
;
1566 HWVoiceOut
*hwo
= NULL
;
1567 HWVoiceIn
*hwi
= NULL
;
1569 s
->vm_running
= running
;
1570 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1571 if (hwo
->pcm_ops
->enable_out
) {
1572 hwo
->pcm_ops
->enable_out(hwo
, running
);
1576 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1577 if (hwi
->pcm_ops
->enable_in
) {
1578 hwi
->pcm_ops
->enable_in(hwi
, running
);
1581 audio_reset_timer (s
);
1584 static void free_audio_state(AudioState
*s
)
1586 HWVoiceOut
*hwo
, *hwon
;
1587 HWVoiceIn
*hwi
, *hwin
;
1589 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1592 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1593 hwo
->pcm_ops
->enable_out(hwo
, false);
1595 hwo
->pcm_ops
->fini_out (hwo
);
1597 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1598 CaptureVoiceOut
*cap
= sc
->cap
;
1599 struct capture_callback
*cb
;
1601 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1602 cb
->ops
.destroy (cb
->opaque
);
1605 QLIST_REMOVE(hwo
, entries
);
1608 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1609 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1610 hwi
->pcm_ops
->enable_in(hwi
, false);
1612 hwi
->pcm_ops
->fini_in (hwi
);
1613 QLIST_REMOVE(hwi
, entries
);
1617 s
->drv
->fini (s
->drv_opaque
);
1622 qapi_free_Audiodev(s
->dev
);
1634 void audio_cleanup(void)
1636 while (!QTAILQ_EMPTY(&audio_states
)) {
1637 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1638 QTAILQ_REMOVE(&audio_states
, s
, list
);
1639 free_audio_state(s
);
1643 static bool vmstate_audio_needed(void *opaque
)
1646 * Never needed, this vmstate only exists in case
1647 * an old qemu sends it to us.
1652 static const VMStateDescription vmstate_audio
= {
1655 .minimum_version_id
= 1,
1656 .needed
= vmstate_audio_needed
,
1657 .fields
= (VMStateField
[]) {
1658 VMSTATE_END_OF_LIST()
1662 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1664 static AudiodevListEntry
*audiodev_find(
1665 AudiodevListHead
*head
, const char *drvname
)
1667 AudiodevListEntry
*e
;
1668 QSIMPLEQ_FOREACH(e
, head
, next
) {
1669 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1678 * if we have dev, this function was called because of an -audiodev argument =>
1679 * initialize a new state with it
1680 * if dev == NULL => legacy implicit initialization, return the already created
1681 * state or create a new one
1683 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1685 static bool atexit_registered
;
1688 const char *drvname
= NULL
;
1689 VMChangeStateEntry
*e
;
1691 struct audio_driver
*driver
;
1692 /* silence gcc warning about uninitialized variable */
1693 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1697 * When using spice allow the spice audio driver being picked
1700 * Temporary hack. Using audio devices without explicit
1701 * audiodev= property is already deprecated. Same goes for
1702 * the -soundhw switch. Once this support gets finally
1703 * removed we can also drop the concept of a default audio
1704 * backend and this can go away.
1706 driver
= audio_driver_lookup("spice");
1708 driver
->can_be_default
= 1;
1713 /* -audiodev option */
1714 legacy_config
= false;
1715 drvname
= AudiodevDriver_str(dev
->driver
);
1716 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1717 if (!legacy_config
) {
1718 dolog("Device %s: audiodev default parameter is deprecated, please "
1719 "specify audiodev=%s\n", name
,
1720 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1722 return QTAILQ_FIRST(&audio_states
);
1724 /* legacy implicit initialization */
1725 head
= audio_handle_legacy_opts();
1727 * In case of legacy initialization, all Audiodevs in the list will have
1728 * the same configuration (except the driver), so it doesn't matter which
1729 * one we chose. We need an Audiodev to set up AudioState before we can
1730 * init a driver. Also note that dev at this point is still in the
1733 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1734 audio_validate_opts(dev
, &error_abort
);
1737 s
= g_new0(AudioState
, 1);
1740 QLIST_INIT (&s
->hw_head_out
);
1741 QLIST_INIT (&s
->hw_head_in
);
1742 QLIST_INIT (&s
->cap_head
);
1743 if (!atexit_registered
) {
1744 atexit(audio_cleanup
);
1745 atexit_registered
= true;
1748 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1750 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1751 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1753 if (s
->nb_hw_voices_out
<= 0) {
1754 dolog ("Bogus number of playback voices %d, setting to 1\n",
1755 s
->nb_hw_voices_out
);
1756 s
->nb_hw_voices_out
= 1;
1759 if (s
->nb_hw_voices_in
<= 0) {
1760 dolog ("Bogus number of capture voices %d, setting to 0\n",
1761 s
->nb_hw_voices_in
);
1762 s
->nb_hw_voices_in
= 0;
1766 driver
= audio_driver_lookup(drvname
);
1768 done
= !audio_driver_init(s
, driver
, true, dev
);
1770 dolog ("Unknown audio driver `%s'\n", drvname
);
1773 free_audio_state(s
);
1777 for (i
= 0; audio_prio_list
[i
]; i
++) {
1778 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1779 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1782 s
->dev
= dev
= e
->dev
;
1783 audio_validate_opts(dev
, &error_abort
);
1784 done
= !audio_driver_init(s
, driver
, false, dev
);
1792 audio_free_audiodev_list(&head
);
1795 driver
= audio_driver_lookup("none");
1796 done
= !audio_driver_init(s
, driver
, false, dev
);
1798 dolog("warning: Using timer based audio emulation\n");
1801 if (dev
->timer_period
<= 0) {
1802 s
->period_ticks
= 1;
1804 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1807 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1809 dolog ("warning: Could not register change state handler\n"
1810 "(Audio can continue looping even after stopping the VM)\n");
1813 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1814 QLIST_INIT (&s
->card_head
);
1815 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1819 void audio_free_audiodev_list(AudiodevListHead
*head
)
1821 AudiodevListEntry
*e
;
1822 while ((e
= QSIMPLEQ_FIRST(head
))) {
1823 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1824 qapi_free_Audiodev(e
->dev
);
1829 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1832 card
->state
= audio_init(NULL
, name
);
1835 card
->name
= g_strdup (name
);
1836 memset (&card
->entries
, 0, sizeof (card
->entries
));
1837 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1840 void AUD_remove_card (QEMUSoundCard
*card
)
1842 QLIST_REMOVE (card
, entries
);
1843 g_free (card
->name
);
1846 static struct audio_pcm_ops capture_pcm_ops
;
1848 CaptureVoiceOut
*AUD_add_capture(
1850 struct audsettings
*as
,
1851 struct audio_capture_ops
*ops
,
1855 CaptureVoiceOut
*cap
;
1856 struct capture_callback
*cb
;
1859 if (!legacy_config
) {
1860 dolog("Capturing without setting an audiodev is deprecated\n");
1862 s
= audio_init(NULL
, NULL
);
1865 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1866 dolog("Can't capture with mixeng disabled\n");
1870 if (audio_validate_settings (as
)) {
1871 dolog ("Invalid settings were passed when trying to add capture\n");
1872 audio_print_settings (as
);
1876 cb
= g_malloc0(sizeof(*cb
));
1878 cb
->opaque
= cb_opaque
;
1880 cap
= audio_pcm_capture_find_specific(s
, as
);
1882 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1886 CaptureVoiceOut
*cap
;
1888 cap
= g_malloc0(sizeof(*cap
));
1892 hw
->pcm_ops
= &capture_pcm_ops
;
1893 QLIST_INIT (&hw
->sw_head
);
1894 QLIST_INIT (&cap
->cb_head
);
1896 /* XXX find a more elegant way */
1897 hw
->samples
= 4096 * 4;
1898 audio_pcm_hw_alloc_resources_out(hw
);
1900 audio_pcm_init_info (&hw
->info
, as
);
1902 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1904 if (hw
->info
.is_float
) {
1905 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1907 hw
->clip
= mixeng_clip
1908 [hw
->info
.nchannels
== 2]
1909 [hw
->info
.is_signed
]
1910 [hw
->info
.swap_endianness
]
1911 [audio_bits_to_index(hw
->info
.bits
)];
1914 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1915 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1917 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1918 audio_attach_capture (hw
);
1924 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1926 struct capture_callback
*cb
;
1928 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1929 if (cb
->opaque
== cb_opaque
) {
1930 cb
->ops
.destroy (cb_opaque
);
1931 QLIST_REMOVE (cb
, entries
);
1934 if (!cap
->cb_head
.lh_first
) {
1935 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1938 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1939 #ifdef DEBUG_CAPTURE
1940 dolog ("freeing %s\n", sw
->name
);
1943 sw1
= sw
->entries
.le_next
;
1945 st_rate_stop (sw
->rate
);
1948 QLIST_REMOVE (sw
, entries
);
1949 QLIST_REMOVE (sc
, entries
);
1953 QLIST_REMOVE (cap
, entries
);
1954 g_free (cap
->hw
.mix_buf
);
1963 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1965 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1966 audio_set_volume_out(sw
, &vol
);
1969 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
1972 HWVoiceOut
*hw
= sw
->hw
;
1974 sw
->vol
.mute
= vol
->mute
;
1975 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1976 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
1979 if (hw
->pcm_ops
->volume_out
) {
1980 hw
->pcm_ops
->volume_out(hw
, vol
);
1985 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1987 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1988 audio_set_volume_in(sw
, &vol
);
1991 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
1994 HWVoiceIn
*hw
= sw
->hw
;
1996 sw
->vol
.mute
= vol
->mute
;
1997 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
1998 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
2001 if (hw
->pcm_ops
->volume_in
) {
2002 hw
->pcm_ops
->volume_in(hw
, vol
);
2007 void audio_create_pdos(Audiodev
*dev
)
2009 switch (dev
->driver
) {
2010 #define CASE(DRIVER, driver, pdo_name) \
2011 case AUDIODEV_DRIVER_##DRIVER: \
2012 if (!dev->u.driver.has_in) { \
2013 dev->u.driver.in = g_malloc0( \
2014 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2015 dev->u.driver.has_in = true; \
2017 if (!dev->u.driver.has_out) { \
2018 dev->u.driver.out = g_malloc0( \
2019 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2020 dev->u.driver.has_out = true; \
2025 CASE(ALSA
, alsa
, Alsa
);
2026 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2028 CASE(DSOUND
, dsound
, );
2029 CASE(JACK
, jack
, Jack
);
2030 CASE(OSS
, oss
, Oss
);
2032 CASE(SDL
, sdl
, Sdl
);
2033 CASE(SNDIO
, sndio
, );
2034 CASE(SPICE
, spice
, );
2037 case AUDIODEV_DRIVER__MAX
:
2042 static void audio_validate_per_direction_opts(
2043 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2045 if (!pdo
->has_mixing_engine
) {
2046 pdo
->has_mixing_engine
= true;
2047 pdo
->mixing_engine
= true;
2049 if (!pdo
->has_fixed_settings
) {
2050 pdo
->has_fixed_settings
= true;
2051 pdo
->fixed_settings
= pdo
->mixing_engine
;
2053 if (!pdo
->fixed_settings
&&
2054 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2056 "You can't use frequency, channels or format with fixed-settings=off");
2059 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2060 error_setg(errp
, "You can't use fixed-settings without mixeng");
2064 if (!pdo
->has_frequency
) {
2065 pdo
->has_frequency
= true;
2066 pdo
->frequency
= 44100;
2068 if (!pdo
->has_channels
) {
2069 pdo
->has_channels
= true;
2072 if (!pdo
->has_voices
) {
2073 pdo
->has_voices
= true;
2074 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2076 if (!pdo
->has_format
) {
2077 pdo
->has_format
= true;
2078 pdo
->format
= AUDIO_FORMAT_S16
;
2082 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2086 audio_create_pdos(dev
);
2088 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2090 error_propagate(errp
, err
);
2094 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2096 error_propagate(errp
, err
);
2100 if (!dev
->has_timer_period
) {
2101 dev
->has_timer_period
= true;
2102 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2106 void audio_help(void)
2110 printf("Available audio drivers:\n");
2112 for (i
= 0; i
< AUDIODEV_DRIVER__MAX
; i
++) {
2113 audio_driver
*driver
= audio_driver_lookup(AudiodevDriver_str(i
));
2115 printf("%s\n", driver
->name
);
2120 void audio_parse_option(const char *opt
)
2122 Audiodev
*dev
= NULL
;
2124 if (is_help_option(opt
)) {
2128 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2129 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2135 void audio_define(Audiodev
*dev
)
2137 AudiodevListEntry
*e
;
2139 audio_validate_opts(dev
, &error_fatal
);
2141 e
= g_new0(AudiodevListEntry
, 1);
2143 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2146 bool audio_init_audiodevs(void)
2148 AudiodevListEntry
*e
;
2150 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2151 if (!audio_init(e
->dev
, NULL
)) {
2159 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2161 return (audsettings
) {
2162 .freq
= pdo
->frequency
,
2163 .nchannels
= pdo
->channels
,
2165 .endianness
= AUDIO_HOST_ENDIANNESS
,
2169 int audioformat_bytes_per_sample(AudioFormat fmt
)
2172 case AUDIO_FORMAT_U8
:
2173 case AUDIO_FORMAT_S8
:
2176 case AUDIO_FORMAT_U16
:
2177 case AUDIO_FORMAT_S16
:
2180 case AUDIO_FORMAT_U32
:
2181 case AUDIO_FORMAT_S32
:
2182 case AUDIO_FORMAT_F32
:
2185 case AUDIO_FORMAT__MAX
:
2192 /* frames = freq * usec / 1e6 */
2193 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2194 audsettings
*as
, int def_usecs
)
2196 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2197 return (as
->freq
* usecs
+ 500000) / 1000000;
2200 /* samples = channels * frames = channels * freq * usec / 1e6 */
2201 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2202 audsettings
*as
, int def_usecs
)
2204 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2208 * bytes = bytes_per_sample * samples =
2209 * bytes_per_sample * channels * freq * usec / 1e6
2211 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2212 audsettings
*as
, int def_usecs
)
2214 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2215 audioformat_bytes_per_sample(as
->fmt
);
2218 AudioState
*audio_state_by_name(const char *name
)
2221 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2223 if (strcmp(name
, s
->dev
->id
) == 0) {
2230 const char *audio_get_id(QEMUSoundCard
*card
)
2233 assert(card
->state
->dev
);
2234 return card
->state
->dev
->id
;
2240 const char *audio_application_name(void)
2242 const char *vm_name
;
2244 vm_name
= qemu_get_vm_name();
2245 return vm_name
? vm_name
: "qemu";
2248 void audio_rate_start(RateCtl
*rate
)
2250 memset(rate
, 0, sizeof(RateCtl
));
2251 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2254 size_t audio_rate_get_bytes(struct audio_pcm_info
*info
, RateCtl
*rate
,
2263 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2264 ticks
= now
- rate
->start_ticks
;
2265 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2266 samples
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2267 if (samples
< 0 || samples
> 65536) {
2268 AUD_log(NULL
, "Resetting rate control (%" PRId64
" samples)\n", samples
);
2269 audio_rate_start(rate
);
2273 ret
= MIN(samples
* info
->bytes_per_frame
, bytes_avail
);
2274 rate
->bytes_sent
+= ret
;