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
24 #include "qemu/osdep.h"
27 #include "monitor/monitor.h"
28 #include "qemu/timer.h"
29 #include "qapi/error.h"
30 #include "qapi/qobject-input-visitor.h"
31 #include "qapi/qapi-visit-audio.h"
32 #include "sysemu/sysemu.h"
33 #include "qemu/cutils.h"
34 #include "sysemu/replay.h"
37 #define AUDIO_CAP "audio"
38 #include "audio_int.h"
40 /* #define DEBUG_LIVE */
41 /* #define DEBUG_OUT */
42 /* #define DEBUG_CAPTURE */
43 /* #define DEBUG_POLL */
45 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
48 /* Order of CONFIG_AUDIO_DRIVERS is import.
49 The 1st one is the one used by default, that is the reason
50 that we generate the list.
52 const char *audio_prio_list
[] = {
60 static QLIST_HEAD(, audio_driver
) audio_drivers
;
61 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
63 void audio_driver_register(audio_driver
*drv
)
65 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
68 audio_driver
*audio_driver_lookup(const char *name
)
70 struct audio_driver
*d
;
72 QLIST_FOREACH(d
, &audio_drivers
, next
) {
73 if (strcmp(name
, d
->name
) == 0) {
78 audio_module_load_one(name
);
79 QLIST_FOREACH(d
, &audio_drivers
, next
) {
80 if (strcmp(name
, d
->name
) == 0) {
88 static AudioState glob_audio_state
;
90 const struct mixeng_volume nominal_volume
= {
101 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
104 int audio_bug (const char *funcname
, int cond
)
109 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
112 AUD_log (NULL
, "Save all your work and restart without audio\n");
113 AUD_log (NULL
, "I am sorry\n");
115 AUD_log (NULL
, "Context:\n");
117 #if defined AUDIO_BREAKPOINT_ON_BUG
118 # if defined HOST_I386
119 # if defined __GNUC__
121 # elif defined _MSC_VER
136 static inline int audio_bits_to_index (int bits
)
149 audio_bug ("bits_to_index", 1);
150 AUD_log (NULL
, "invalid bits %d\n", bits
);
155 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
161 cond
= !nmemb
|| !size
;
165 if (audio_bug ("audio_calloc", cond
)) {
166 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
168 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
172 return g_malloc0 (len
);
175 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
178 fprintf(stderr
, "%s: ", cap
);
181 vfprintf(stderr
, fmt
, ap
);
184 void AUD_log (const char *cap
, const char *fmt
, ...)
189 AUD_vlog (cap
, fmt
, ap
);
193 static void audio_print_settings (struct audsettings
*as
)
195 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
198 case AUDIO_FORMAT_S8
:
199 AUD_log (NULL
, "S8");
201 case AUDIO_FORMAT_U8
:
202 AUD_log (NULL
, "U8");
204 case AUDIO_FORMAT_S16
:
205 AUD_log (NULL
, "S16");
207 case AUDIO_FORMAT_U16
:
208 AUD_log (NULL
, "U16");
210 case AUDIO_FORMAT_S32
:
211 AUD_log (NULL
, "S32");
213 case AUDIO_FORMAT_U32
:
214 AUD_log (NULL
, "U32");
217 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
221 AUD_log (NULL
, " endianness=");
222 switch (as
->endianness
) {
224 AUD_log (NULL
, "little");
227 AUD_log (NULL
, "big");
230 AUD_log (NULL
, "invalid");
233 AUD_log (NULL
, "\n");
236 static int audio_validate_settings (struct audsettings
*as
)
240 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
241 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
244 case AUDIO_FORMAT_S8
:
245 case AUDIO_FORMAT_U8
:
246 case AUDIO_FORMAT_S16
:
247 case AUDIO_FORMAT_U16
:
248 case AUDIO_FORMAT_S32
:
249 case AUDIO_FORMAT_U32
:
256 invalid
|= as
->freq
<= 0;
257 return invalid
? -1 : 0;
260 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
262 int bits
= 8, sign
= 0;
265 case AUDIO_FORMAT_S8
:
268 case AUDIO_FORMAT_U8
:
271 case AUDIO_FORMAT_S16
:
274 case AUDIO_FORMAT_U16
:
278 case AUDIO_FORMAT_S32
:
281 case AUDIO_FORMAT_U32
:
288 return info
->freq
== as
->freq
289 && info
->nchannels
== as
->nchannels
290 && info
->sign
== sign
291 && info
->bits
== bits
292 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
295 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
297 int bits
= 8, sign
= 0, shift
= 0;
300 case AUDIO_FORMAT_S8
:
302 case AUDIO_FORMAT_U8
:
305 case AUDIO_FORMAT_S16
:
307 case AUDIO_FORMAT_U16
:
312 case AUDIO_FORMAT_S32
:
314 case AUDIO_FORMAT_U32
:
323 info
->freq
= as
->freq
;
326 info
->nchannels
= as
->nchannels
;
327 info
->shift
= (as
->nchannels
== 2) + shift
;
328 info
->align
= (1 << info
->shift
) - 1;
329 info
->bytes_per_second
= info
->freq
<< info
->shift
;
330 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
333 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
340 memset (buf
, 0x00, len
<< info
->shift
);
343 switch (info
->bits
) {
345 memset (buf
, 0x80, len
<< info
->shift
);
352 int shift
= info
->nchannels
- 1;
355 if (info
->swap_endianness
) {
359 for (i
= 0; i
< len
<< shift
; i
++) {
369 int shift
= info
->nchannels
- 1;
370 int32_t s
= INT32_MAX
;
372 if (info
->swap_endianness
) {
376 for (i
= 0; i
< len
<< shift
; i
++) {
383 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
393 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
400 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
401 struct audsettings
*as
404 CaptureVoiceOut
*cap
;
405 AudioState
*s
= &glob_audio_state
;
407 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
408 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
415 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
417 struct capture_callback
*cb
;
420 dolog ("notification %d sent\n", cmd
);
422 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
423 cb
->ops
.notify (cb
->opaque
, cmd
);
427 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
429 if (cap
->hw
.enabled
!= enabled
) {
430 audcnotification_e cmd
;
431 cap
->hw
.enabled
= enabled
;
432 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
433 audio_notify_capture (cap
, cmd
);
437 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
439 HWVoiceOut
*hw
= &cap
->hw
;
443 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
449 audio_capture_maybe_changed (cap
, enabled
);
452 static void audio_detach_capture (HWVoiceOut
*hw
)
454 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
457 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
458 SWVoiceOut
*sw
= &sc
->sw
;
459 CaptureVoiceOut
*cap
= sc
->cap
;
460 int was_active
= sw
->active
;
463 st_rate_stop (sw
->rate
);
467 QLIST_REMOVE (sw
, entries
);
468 QLIST_REMOVE (sc
, entries
);
471 /* We have removed soft voice from the capture:
472 this might have changed the overall status of the capture
473 since this might have been the only active voice */
474 audio_recalc_and_notify_capture (cap
);
480 static int audio_attach_capture (HWVoiceOut
*hw
)
482 AudioState
*s
= &glob_audio_state
;
483 CaptureVoiceOut
*cap
;
485 audio_detach_capture (hw
);
486 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
489 HWVoiceOut
*hw_cap
= &cap
->hw
;
491 sc
= g_malloc0(sizeof(*sc
));
498 sw
->active
= hw
->enabled
;
499 sw
->conv
= noop_conv
;
500 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
501 sw
->vol
= nominal_volume
;
502 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
504 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
508 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
509 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
511 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
512 hw
, sw
->info
.freq
, sw
->info
.bits
,
514 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
517 audio_capture_maybe_changed (cap
, 1);
524 * Hard voice (capture)
526 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
529 int m
= hw
->total_samples_captured
;
531 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
533 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
539 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
541 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
542 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
543 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
549 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
550 int live
, int pending
)
552 int left
= hw
->samples
- pending
;
553 int len
= audio_MIN (left
, live
);
557 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
558 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
559 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
560 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
562 hw
->clip (dst
, src
, samples_to_clip
);
564 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
565 len
-= samples_to_clip
;
566 clipped
+= samples_to_clip
;
572 * Soft voice (capture)
574 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
576 HWVoiceIn
*hw
= sw
->hw
;
577 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
580 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
581 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
585 rpos
= hw
->wpos
- live
;
590 return hw
->samples
+ rpos
;
594 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
596 HWVoiceIn
*hw
= sw
->hw
;
597 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
598 struct st_sample
*src
, *dst
= sw
->buf
;
600 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
602 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
603 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
604 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
608 samples
= size
>> sw
->info
.shift
;
613 swlim
= (live
* sw
->ratio
) >> 32;
614 swlim
= audio_MIN (swlim
, samples
);
617 src
= hw
->conv_buf
+ rpos
;
618 isamp
= hw
->wpos
- rpos
;
621 isamp
= hw
->samples
- rpos
;
629 if (audio_bug(__func__
, osamp
< 0)) {
630 dolog ("osamp=%d\n", osamp
);
634 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
636 rpos
= (rpos
+ isamp
) % hw
->samples
;
642 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
643 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
646 sw
->clip (buf
, sw
->buf
, ret
);
647 sw
->total_hw_samples_acquired
+= total
;
648 return ret
<< sw
->info
.shift
;
652 * Hard voice (playback)
654 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
660 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
661 if (sw
->active
|| !sw
->empty
) {
662 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
671 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
676 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
684 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
685 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
694 * Soft voice (playback)
696 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
698 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
699 int ret
= 0, pos
= 0, total
= 0;
705 hwsamples
= sw
->hw
->samples
;
707 live
= sw
->total_hw_samples_mixed
;
708 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
709 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
713 if (live
== hwsamples
) {
715 dolog ("%s is full %d\n", sw
->name
, live
);
720 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
721 samples
= size
>> sw
->info
.shift
;
723 dead
= hwsamples
- live
;
724 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
725 swlim
= audio_MIN (swlim
, samples
);
727 sw
->conv (sw
->buf
, buf
, swlim
);
729 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
730 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
735 dead
= hwsamples
- live
;
736 left
= hwsamples
- wpos
;
737 blck
= audio_MIN (dead
, left
);
746 sw
->hw
->mix_buf
+ 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 %d ret %d total sw %d\n",
765 size
>> sw
->info
.shift
,
767 sw
->total_hw_samples_mixed
771 return ret
<< sw
->info
.shift
;
775 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
777 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
778 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
783 #include "audio_template.h"
785 #include "audio_template.h"
791 static bool audio_timer_running
;
792 static uint64_t audio_timer_last
;
794 static int audio_is_timer_needed (void)
796 HWVoiceIn
*hwi
= NULL
;
797 HWVoiceOut
*hwo
= NULL
;
799 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
800 if (!hwo
->poll_mode
) return 1;
802 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
803 if (!hwi
->poll_mode
) return 1;
808 static void audio_reset_timer (AudioState
*s
)
810 if (audio_is_timer_needed ()) {
811 timer_mod_anticipate_ns(s
->ts
,
812 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
813 if (!audio_timer_running
) {
814 audio_timer_running
= true;
815 audio_timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
816 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
820 if (audio_timer_running
) {
821 audio_timer_running
= false;
822 trace_audio_timer_stop();
827 static void audio_timer (void *opaque
)
830 AudioState
*s
= opaque
;
832 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
833 diff
= now
- audio_timer_last
;
834 if (diff
> s
->period_ticks
* 3 / 2) {
835 trace_audio_timer_delayed(diff
/ SCALE_MS
);
837 audio_timer_last
= now
;
840 audio_reset_timer(s
);
846 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
849 /* XXX: Consider options */
853 if (!sw
->hw
->enabled
) {
854 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
858 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
861 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
864 /* XXX: Consider options */
868 if (!sw
->hw
->enabled
) {
869 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
873 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
876 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
878 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
881 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
890 if (sw
->active
!= on
) {
891 AudioState
*s
= &glob_audio_state
;
896 hw
->pending_disable
= 0;
900 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
901 audio_reset_timer (s
);
909 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
910 temp_sw
= temp_sw
->entries
.le_next
) {
911 nb_active
+= temp_sw
->active
!= 0;
914 hw
->pending_disable
= nb_active
== 1;
918 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
919 sc
->sw
.active
= hw
->enabled
;
921 audio_capture_maybe_changed (sc
->cap
, 1);
928 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
937 if (sw
->active
!= on
) {
938 AudioState
*s
= &glob_audio_state
;
945 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
946 audio_reset_timer (s
);
949 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
955 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
956 temp_sw
= temp_sw
->entries
.le_next
) {
957 nb_active
+= temp_sw
->active
!= 0;
960 if (nb_active
== 1) {
962 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
970 static int audio_get_avail (SWVoiceIn
*sw
)
978 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
979 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
980 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
985 "%s: get_avail live %d ret %" PRId64
"\n",
987 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
990 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
993 static int audio_get_free (SWVoiceOut
*sw
)
1001 live
= sw
->total_hw_samples_mixed
;
1003 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1004 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1008 dead
= sw
->hw
->samples
- live
;
1011 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1013 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1016 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1019 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1026 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1027 SWVoiceOut
*sw
= &sc
->sw
;
1032 int till_end_of_hw
= hw
->samples
- rpos2
;
1033 int to_write
= audio_MIN (till_end_of_hw
, n
);
1034 int bytes
= to_write
<< hw
->info
.shift
;
1037 sw
->buf
= hw
->mix_buf
+ rpos2
;
1038 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1039 if (written
- bytes
) {
1040 dolog ("Could not mix %d bytes into a capture "
1041 "buffer, mixed %d\n",
1046 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1051 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1052 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1053 mixeng_clear (hw
->mix_buf
, samples
- n
);
1056 static void audio_run_out (AudioState
*s
)
1058 HWVoiceOut
*hw
= NULL
;
1061 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1063 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1065 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1070 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1071 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1075 if (hw
->pending_disable
&& !nb_live
) {
1078 dolog ("Disabling voice\n");
1081 hw
->pending_disable
= 0;
1082 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1083 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1085 audio_recalc_and_notify_capture (sc
->cap
);
1091 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1093 free
= audio_get_free (sw
);
1095 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1102 prev_rpos
= hw
->rpos
;
1103 played
= hw
->pcm_ops
->run_out (hw
, live
);
1104 replay_audio_out(&played
);
1105 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1106 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1107 hw
->rpos
, hw
->samples
, played
);
1112 dolog ("played=%d\n", played
);
1116 hw
->ts_helper
+= played
;
1117 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1120 cleanup_required
= 0;
1121 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1122 if (!sw
->active
&& sw
->empty
) {
1126 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1127 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1128 played
, sw
->total_hw_samples_mixed
);
1129 played
= sw
->total_hw_samples_mixed
;
1132 sw
->total_hw_samples_mixed
-= played
;
1134 if (!sw
->total_hw_samples_mixed
) {
1136 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1140 free
= audio_get_free (sw
);
1142 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1147 if (cleanup_required
) {
1150 sw
= hw
->sw_head
.lh_first
;
1152 sw1
= sw
->entries
.le_next
;
1153 if (!sw
->active
&& !sw
->callback
.fn
) {
1154 audio_close_out (sw
);
1162 static void audio_run_in (AudioState
*s
)
1164 HWVoiceIn
*hw
= NULL
;
1166 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1168 int captured
= 0, min
;
1170 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1171 captured
= hw
->pcm_ops
->run_in(hw
);
1173 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1175 min
= audio_pcm_hw_find_min_in (hw
);
1176 hw
->total_samples_captured
+= captured
- min
;
1177 hw
->ts_helper
+= captured
;
1179 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1180 sw
->total_hw_samples_acquired
-= min
;
1185 avail
= audio_get_avail (sw
);
1187 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1194 static void audio_run_capture (AudioState
*s
)
1196 CaptureVoiceOut
*cap
;
1198 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1199 int live
, rpos
, captured
;
1200 HWVoiceOut
*hw
= &cap
->hw
;
1203 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1206 int left
= hw
->samples
- rpos
;
1207 int to_capture
= audio_MIN (live
, left
);
1208 struct st_sample
*src
;
1209 struct capture_callback
*cb
;
1211 src
= hw
->mix_buf
+ rpos
;
1212 hw
->clip (cap
->buf
, src
, to_capture
);
1213 mixeng_clear (src
, to_capture
);
1215 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1216 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1217 to_capture
<< hw
->info
.shift
);
1219 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1224 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1225 if (!sw
->active
&& sw
->empty
) {
1229 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1230 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1231 captured
, sw
->total_hw_samples_mixed
);
1232 captured
= sw
->total_hw_samples_mixed
;
1235 sw
->total_hw_samples_mixed
-= captured
;
1236 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1241 void audio_run (const char *msg
)
1243 AudioState
*s
= &glob_audio_state
;
1247 audio_run_capture (s
);
1250 static double prevtime
;
1254 if (gettimeofday (&tv
, NULL
)) {
1255 perror ("audio_run: gettimeofday");
1259 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1260 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1261 prevtime
= currtime
;
1266 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1267 bool msg
, Audiodev
*dev
)
1269 s
->drv_opaque
= drv
->init(dev
);
1271 if (s
->drv_opaque
) {
1272 audio_init_nb_voices_out (drv
);
1273 audio_init_nb_voices_in (drv
);
1279 dolog("Could not init `%s' audio driver\n", drv
->name
);
1285 static void audio_vm_change_state_handler (void *opaque
, int running
,
1288 AudioState
*s
= opaque
;
1289 HWVoiceOut
*hwo
= NULL
;
1290 HWVoiceIn
*hwi
= NULL
;
1291 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1293 s
->vm_running
= running
;
1294 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1295 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1298 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1299 hwi
->pcm_ops
->ctl_in(hwi
, op
);
1301 audio_reset_timer (s
);
1304 static bool is_cleaning_up
;
1306 bool audio_is_cleaning_up(void)
1308 return is_cleaning_up
;
1311 void audio_cleanup(void)
1313 AudioState
*s
= &glob_audio_state
;
1314 HWVoiceOut
*hwo
, *hwon
;
1315 HWVoiceIn
*hwi
, *hwin
;
1317 is_cleaning_up
= true;
1318 QLIST_FOREACH_SAFE(hwo
, &glob_audio_state
.hw_head_out
, entries
, hwon
) {
1322 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1324 hwo
->pcm_ops
->fini_out (hwo
);
1326 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1327 CaptureVoiceOut
*cap
= sc
->cap
;
1328 struct capture_callback
*cb
;
1330 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1331 cb
->ops
.destroy (cb
->opaque
);
1334 QLIST_REMOVE(hwo
, entries
);
1337 QLIST_FOREACH_SAFE(hwi
, &glob_audio_state
.hw_head_in
, entries
, hwin
) {
1339 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1341 hwi
->pcm_ops
->fini_in (hwi
);
1342 QLIST_REMOVE(hwi
, entries
);
1346 s
->drv
->fini (s
->drv_opaque
);
1351 qapi_free_Audiodev(s
->dev
);
1356 static const VMStateDescription vmstate_audio
= {
1359 .minimum_version_id
= 1,
1360 .fields
= (VMStateField
[]) {
1361 VMSTATE_END_OF_LIST()
1365 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1367 static AudiodevListEntry
*audiodev_find(
1368 AudiodevListHead
*head
, const char *drvname
)
1370 AudiodevListEntry
*e
;
1371 QSIMPLEQ_FOREACH(e
, head
, next
) {
1372 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1380 static int audio_init(Audiodev
*dev
)
1384 const char *drvname
= NULL
;
1385 VMChangeStateEntry
*e
;
1386 AudioState
*s
= &glob_audio_state
;
1387 struct audio_driver
*driver
;
1388 /* silence gcc warning about uninitialized variable */
1389 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1393 dolog("Cannot create more than one audio backend, sorry\n");
1394 qapi_free_Audiodev(dev
);
1400 /* -audiodev option */
1401 drvname
= AudiodevDriver_str(dev
->driver
);
1403 /* legacy implicit initialization */
1404 head
= audio_handle_legacy_opts();
1406 * In case of legacy initialization, all Audiodevs in the list will have
1407 * the same configuration (except the driver), so it does't matter which
1408 * one we chose. We need an Audiodev to set up AudioState before we can
1409 * init a driver. Also note that dev at this point is still in the
1412 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1413 audio_validate_opts(dev
, &error_abort
);
1417 QLIST_INIT (&s
->hw_head_out
);
1418 QLIST_INIT (&s
->hw_head_in
);
1419 QLIST_INIT (&s
->cap_head
);
1420 atexit(audio_cleanup
);
1422 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1424 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1425 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1427 if (s
->nb_hw_voices_out
<= 0) {
1428 dolog ("Bogus number of playback voices %d, setting to 1\n",
1429 s
->nb_hw_voices_out
);
1430 s
->nb_hw_voices_out
= 1;
1433 if (s
->nb_hw_voices_in
<= 0) {
1434 dolog ("Bogus number of capture voices %d, setting to 0\n",
1435 s
->nb_hw_voices_in
);
1436 s
->nb_hw_voices_in
= 0;
1440 driver
= audio_driver_lookup(drvname
);
1442 done
= !audio_driver_init(s
, driver
, true, dev
);
1444 dolog ("Unknown audio driver `%s'\n", drvname
);
1447 for (i
= 0; audio_prio_list
[i
]; i
++) {
1448 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1449 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1452 s
->dev
= dev
= e
->dev
;
1453 audio_validate_opts(dev
, &error_abort
);
1454 done
= !audio_driver_init(s
, driver
, false, dev
);
1462 audio_free_audiodev_list(&head
);
1465 driver
= audio_driver_lookup("none");
1466 done
= !audio_driver_init(s
, driver
, false, dev
);
1468 dolog("warning: Using timer based audio emulation\n");
1471 if (dev
->timer_period
<= 0) {
1472 s
->period_ticks
= 1;
1474 s
->period_ticks
= NANOSECONDS_PER_SECOND
/ dev
->timer_period
;
1477 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1479 dolog ("warning: Could not register change state handler\n"
1480 "(Audio can continue looping even after stopping the VM)\n");
1483 QLIST_INIT (&s
->card_head
);
1484 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1488 void audio_free_audiodev_list(AudiodevListHead
*head
)
1490 AudiodevListEntry
*e
;
1491 while ((e
= QSIMPLEQ_FIRST(head
))) {
1492 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1493 qapi_free_Audiodev(e
->dev
);
1498 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1501 card
->name
= g_strdup (name
);
1502 memset (&card
->entries
, 0, sizeof (card
->entries
));
1503 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1506 void AUD_remove_card (QEMUSoundCard
*card
)
1508 QLIST_REMOVE (card
, entries
);
1509 g_free (card
->name
);
1513 CaptureVoiceOut
*AUD_add_capture (
1514 struct audsettings
*as
,
1515 struct audio_capture_ops
*ops
,
1519 AudioState
*s
= &glob_audio_state
;
1520 CaptureVoiceOut
*cap
;
1521 struct capture_callback
*cb
;
1523 if (audio_validate_settings (as
)) {
1524 dolog ("Invalid settings were passed when trying to add capture\n");
1525 audio_print_settings (as
);
1529 cb
= g_malloc0(sizeof(*cb
));
1531 cb
->opaque
= cb_opaque
;
1533 cap
= audio_pcm_capture_find_specific (as
);
1535 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1540 CaptureVoiceOut
*cap
;
1542 cap
= g_malloc0(sizeof(*cap
));
1545 QLIST_INIT (&hw
->sw_head
);
1546 QLIST_INIT (&cap
->cb_head
);
1548 /* XXX find a more elegant way */
1549 hw
->samples
= 4096 * 4;
1550 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1552 audio_pcm_init_info (&hw
->info
, as
);
1554 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1556 hw
->clip
= mixeng_clip
1557 [hw
->info
.nchannels
== 2]
1559 [hw
->info
.swap_endianness
]
1560 [audio_bits_to_index (hw
->info
.bits
)];
1562 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1563 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1565 QLIST_FOREACH(hw
, &glob_audio_state
.hw_head_out
, entries
) {
1566 audio_attach_capture (hw
);
1572 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1574 struct capture_callback
*cb
;
1576 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1577 if (cb
->opaque
== cb_opaque
) {
1578 cb
->ops
.destroy (cb_opaque
);
1579 QLIST_REMOVE (cb
, entries
);
1582 if (!cap
->cb_head
.lh_first
) {
1583 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1586 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1587 #ifdef DEBUG_CAPTURE
1588 dolog ("freeing %s\n", sw
->name
);
1591 sw1
= sw
->entries
.le_next
;
1593 st_rate_stop (sw
->rate
);
1596 QLIST_REMOVE (sw
, entries
);
1597 QLIST_REMOVE (sc
, entries
);
1601 QLIST_REMOVE (cap
, entries
);
1602 g_free (cap
->hw
.mix_buf
);
1611 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1614 HWVoiceOut
*hw
= sw
->hw
;
1616 sw
->vol
.mute
= mute
;
1617 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1618 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1620 if (hw
->pcm_ops
->ctl_out
) {
1621 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1626 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1629 HWVoiceIn
*hw
= sw
->hw
;
1631 sw
->vol
.mute
= mute
;
1632 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1633 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1635 if (hw
->pcm_ops
->ctl_in
) {
1636 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1641 void audio_create_pdos(Audiodev
*dev
)
1643 switch (dev
->driver
) {
1644 #define CASE(DRIVER, driver, pdo_name) \
1645 case AUDIODEV_DRIVER_##DRIVER: \
1646 if (!dev->u.driver.has_in) { \
1647 dev->u.driver.in = g_malloc0( \
1648 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1649 dev->u.driver.has_in = true; \
1651 if (!dev->u.driver.has_out) { \
1652 dev->u.driver.out = g_malloc0( \
1653 sizeof(AudiodevAlsaPerDirectionOptions)); \
1654 dev->u.driver.has_out = true; \
1659 CASE(ALSA
, alsa
, Alsa
);
1660 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1661 CASE(DSOUND
, dsound
, );
1662 CASE(OSS
, oss
, Oss
);
1665 CASE(SPICE
, spice
, );
1668 case AUDIODEV_DRIVER__MAX
:
1673 static void audio_validate_per_direction_opts(
1674 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1676 if (!pdo
->has_fixed_settings
) {
1677 pdo
->has_fixed_settings
= true;
1678 pdo
->fixed_settings
= true;
1680 if (!pdo
->fixed_settings
&&
1681 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1683 "You can't use frequency, channels or format with fixed-settings=off");
1687 if (!pdo
->has_frequency
) {
1688 pdo
->has_frequency
= true;
1689 pdo
->frequency
= 44100;
1691 if (!pdo
->has_channels
) {
1692 pdo
->has_channels
= true;
1695 if (!pdo
->has_voices
) {
1696 pdo
->has_voices
= true;
1699 if (!pdo
->has_format
) {
1700 pdo
->has_format
= true;
1701 pdo
->format
= AUDIO_FORMAT_S16
;
1705 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1709 audio_create_pdos(dev
);
1711 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1713 error_propagate(errp
, err
);
1717 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1719 error_propagate(errp
, err
);
1723 if (!dev
->has_timer_period
) {
1724 dev
->has_timer_period
= true;
1725 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1729 void audio_parse_option(const char *opt
)
1731 AudiodevListEntry
*e
;
1732 Audiodev
*dev
= NULL
;
1734 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1735 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1738 audio_validate_opts(dev
, &error_fatal
);
1740 e
= g_malloc0(sizeof(AudiodevListEntry
));
1742 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1745 void audio_init_audiodevs(void)
1747 AudiodevListEntry
*e
;
1749 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1754 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1756 return (audsettings
) {
1757 .freq
= pdo
->frequency
,
1758 .nchannels
= pdo
->channels
,
1760 .endianness
= AUDIO_HOST_ENDIANNESS
,
1764 int audioformat_bytes_per_sample(AudioFormat fmt
)
1767 case AUDIO_FORMAT_U8
:
1768 case AUDIO_FORMAT_S8
:
1771 case AUDIO_FORMAT_U16
:
1772 case AUDIO_FORMAT_S16
:
1775 case AUDIO_FORMAT_U32
:
1776 case AUDIO_FORMAT_S32
:
1779 case AUDIO_FORMAT__MAX
:
1786 /* frames = freq * usec / 1e6 */
1787 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1788 audsettings
*as
, int def_usecs
)
1790 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1791 return (as
->freq
* usecs
+ 500000) / 1000000;
1794 /* samples = channels * frames = channels * freq * usec / 1e6 */
1795 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1796 audsettings
*as
, int def_usecs
)
1798 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1802 * bytes = bytes_per_sample * samples =
1803 * bytes_per_sample * channels * freq * usec / 1e6
1805 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1806 audsettings
*as
, int def_usecs
)
1808 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1809 audioformat_bytes_per_sample(as
->fmt
);