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 "sysemu/replay.h"
36 #include "sysemu/runstate.h"
39 #define AUDIO_CAP "audio"
40 #include "audio_int.h"
42 /* #define DEBUG_LIVE */
43 /* #define DEBUG_OUT */
44 /* #define DEBUG_CAPTURE */
45 /* #define DEBUG_POLL */
47 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
50 /* Order of CONFIG_AUDIO_DRIVERS is import.
51 The 1st one is the one used by default, that is the reason
52 that we generate the list.
54 const char *audio_prio_list
[] = {
62 static QLIST_HEAD(, audio_driver
) audio_drivers
;
63 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
65 void audio_driver_register(audio_driver
*drv
)
67 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
70 audio_driver
*audio_driver_lookup(const char *name
)
72 struct audio_driver
*d
;
74 QLIST_FOREACH(d
, &audio_drivers
, next
) {
75 if (strcmp(name
, d
->name
) == 0) {
80 audio_module_load_one(name
);
81 QLIST_FOREACH(d
, &audio_drivers
, next
) {
82 if (strcmp(name
, d
->name
) == 0) {
90 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
91 QTAILQ_HEAD_INITIALIZER(audio_states
);
93 const struct mixeng_volume nominal_volume
= {
104 static bool legacy_config
= true;
106 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
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");
122 #if defined AUDIO_BREAKPOINT_ON_BUG
123 # if defined HOST_I386
124 # if defined __GNUC__
126 # elif defined _MSC_VER
141 static inline int audio_bits_to_index (int bits
)
154 audio_bug ("bits_to_index", 1);
155 AUD_log (NULL
, "invalid bits %d\n", bits
);
160 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
166 cond
= !nmemb
|| !size
;
170 if (audio_bug ("audio_calloc", cond
)) {
171 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
173 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
177 return g_malloc0 (len
);
180 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
183 fprintf(stderr
, "%s: ", cap
);
186 vfprintf(stderr
, fmt
, ap
);
189 void AUD_log (const char *cap
, const char *fmt
, ...)
194 AUD_vlog (cap
, fmt
, ap
);
198 static void audio_print_settings (struct audsettings
*as
)
200 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
203 case AUDIO_FORMAT_S8
:
204 AUD_log (NULL
, "S8");
206 case AUDIO_FORMAT_U8
:
207 AUD_log (NULL
, "U8");
209 case AUDIO_FORMAT_S16
:
210 AUD_log (NULL
, "S16");
212 case AUDIO_FORMAT_U16
:
213 AUD_log (NULL
, "U16");
215 case AUDIO_FORMAT_S32
:
216 AUD_log (NULL
, "S32");
218 case AUDIO_FORMAT_U32
:
219 AUD_log (NULL
, "U32");
222 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
226 AUD_log (NULL
, " endianness=");
227 switch (as
->endianness
) {
229 AUD_log (NULL
, "little");
232 AUD_log (NULL
, "big");
235 AUD_log (NULL
, "invalid");
238 AUD_log (NULL
, "\n");
241 static int audio_validate_settings (struct audsettings
*as
)
245 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
246 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
249 case AUDIO_FORMAT_S8
:
250 case AUDIO_FORMAT_U8
:
251 case AUDIO_FORMAT_S16
:
252 case AUDIO_FORMAT_U16
:
253 case AUDIO_FORMAT_S32
:
254 case AUDIO_FORMAT_U32
:
261 invalid
|= as
->freq
<= 0;
262 return invalid
? -1 : 0;
265 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
267 int bits
= 8, sign
= 0;
270 case AUDIO_FORMAT_S8
:
273 case AUDIO_FORMAT_U8
:
276 case AUDIO_FORMAT_S16
:
279 case AUDIO_FORMAT_U16
:
283 case AUDIO_FORMAT_S32
:
286 case AUDIO_FORMAT_U32
:
293 return info
->freq
== as
->freq
294 && info
->nchannels
== as
->nchannels
295 && info
->sign
== sign
296 && info
->bits
== bits
297 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
300 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
302 int bits
= 8, sign
= 0, shift
= 0;
305 case AUDIO_FORMAT_S8
:
307 case AUDIO_FORMAT_U8
:
310 case AUDIO_FORMAT_S16
:
313 case AUDIO_FORMAT_U16
:
318 case AUDIO_FORMAT_S32
:
321 case AUDIO_FORMAT_U32
:
330 info
->freq
= as
->freq
;
333 info
->nchannels
= as
->nchannels
;
334 info
->shift
= (as
->nchannels
== 2) + shift
;
335 info
->align
= (1 << info
->shift
) - 1;
336 info
->bytes_per_second
= info
->freq
<< info
->shift
;
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
)
347 memset (buf
, 0x00, len
<< info
->shift
);
350 switch (info
->bits
) {
352 memset (buf
, 0x80, len
<< info
->shift
);
359 int shift
= info
->nchannels
- 1;
362 if (info
->swap_endianness
) {
366 for (i
= 0; i
< len
<< shift
; i
++) {
376 int shift
= info
->nchannels
- 1;
377 int32_t s
= INT32_MAX
;
379 if (info
->swap_endianness
) {
383 for (i
= 0; i
< len
<< shift
; i
++) {
390 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
400 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
407 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
408 struct audsettings
*as
)
410 CaptureVoiceOut
*cap
;
412 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
413 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
420 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
422 struct capture_callback
*cb
;
425 dolog ("notification %d sent\n", cmd
);
427 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
428 cb
->ops
.notify (cb
->opaque
, cmd
);
432 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
434 if (cap
->hw
.enabled
!= enabled
) {
435 audcnotification_e cmd
;
436 cap
->hw
.enabled
= enabled
;
437 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
438 audio_notify_capture (cap
, cmd
);
442 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
444 HWVoiceOut
*hw
= &cap
->hw
;
448 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
454 audio_capture_maybe_changed (cap
, enabled
);
457 static void audio_detach_capture (HWVoiceOut
*hw
)
459 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
462 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
463 SWVoiceOut
*sw
= &sc
->sw
;
464 CaptureVoiceOut
*cap
= sc
->cap
;
465 int was_active
= sw
->active
;
468 st_rate_stop (sw
->rate
);
472 QLIST_REMOVE (sw
, entries
);
473 QLIST_REMOVE (sc
, entries
);
476 /* We have removed soft voice from the capture:
477 this might have changed the overall status of the capture
478 since this might have been the only active voice */
479 audio_recalc_and_notify_capture (cap
);
485 static int audio_attach_capture (HWVoiceOut
*hw
)
487 AudioState
*s
= hw
->s
;
488 CaptureVoiceOut
*cap
;
490 audio_detach_capture (hw
);
491 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
494 HWVoiceOut
*hw_cap
= &cap
->hw
;
496 sc
= g_malloc0(sizeof(*sc
));
503 sw
->active
= hw
->enabled
;
504 sw
->conv
= noop_conv
;
505 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
506 sw
->vol
= nominal_volume
;
507 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
509 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
513 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
514 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
516 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
517 hw
, sw
->info
.freq
, sw
->info
.bits
,
519 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
522 audio_capture_maybe_changed (cap
, 1);
529 * Hard voice (capture)
531 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
534 size_t m
= hw
->total_samples_captured
;
536 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
538 m
= MIN (m
, sw
->total_hw_samples_acquired
);
544 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
546 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
547 if (audio_bug(__func__
, live
> hw
->samples
)) {
548 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
554 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
557 size_t pos
= hw
->rpos
;
560 st_sample
*src
= hw
->mix_buf
+ pos
;
561 uint8_t *dst
= advance(pcm_buf
, clipped
<< hw
->info
.shift
);
562 size_t samples_till_end_of_buf
= hw
->samples
- pos
;
563 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
565 hw
->clip(dst
, src
, samples_to_clip
);
567 pos
= (pos
+ samples_to_clip
) % hw
->samples
;
568 len
-= samples_to_clip
;
569 clipped
+= samples_to_clip
;
574 * Soft voice (capture)
576 static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn
*sw
)
578 HWVoiceIn
*hw
= sw
->hw
;
579 ssize_t live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
582 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
583 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
587 rpos
= hw
->wpos
- live
;
592 return hw
->samples
+ rpos
;
596 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
598 HWVoiceIn
*hw
= sw
->hw
;
599 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
600 struct st_sample
*src
, *dst
= sw
->buf
;
602 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
604 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
605 if (audio_bug(__func__
, live
> hw
->samples
)) {
606 dolog("live_in=%zu hw->samples=%zu\n", live
, hw
->samples
);
610 samples
= size
>> sw
->info
.shift
;
615 swlim
= (live
* sw
->ratio
) >> 32;
616 swlim
= MIN (swlim
, samples
);
619 src
= hw
->conv_buf
+ rpos
;
620 if (hw
->wpos
> rpos
) {
621 isamp
= hw
->wpos
- rpos
;
623 isamp
= hw
->samples
- rpos
;
631 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
633 rpos
= (rpos
+ isamp
) % hw
->samples
;
639 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
640 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
643 sw
->clip (buf
, sw
->buf
, ret
);
644 sw
->total_hw_samples_acquired
+= total
;
645 return ret
<< sw
->info
.shift
;
649 * Hard voice (playback)
651 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
657 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
658 if (sw
->active
|| !sw
->empty
) {
659 m
= MIN (m
, sw
->total_hw_samples_mixed
);
668 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
673 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
681 if (audio_bug(__func__
, live
> hw
->samples
)) {
682 dolog("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
691 * Soft voice (playback)
693 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
695 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
696 size_t ret
= 0, pos
= 0, total
= 0;
702 hwsamples
= sw
->hw
->samples
;
704 live
= sw
->total_hw_samples_mixed
;
705 if (audio_bug(__func__
, live
> hwsamples
)) {
706 dolog("live=%zu hw->samples=%zu\n", live
, hwsamples
);
710 if (live
== hwsamples
) {
712 dolog ("%s is full %d\n", sw
->name
, live
);
717 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
718 samples
= size
>> sw
->info
.shift
;
720 dead
= hwsamples
- live
;
721 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
722 swlim
= MIN (swlim
, samples
);
724 sw
->conv (sw
->buf
, buf
, swlim
);
726 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
727 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
732 dead
= hwsamples
- live
;
733 left
= hwsamples
- wpos
;
734 blck
= MIN (dead
, left
);
743 sw
->hw
->mix_buf
+ wpos
,
751 wpos
= (wpos
+ osamp
) % hwsamples
;
755 sw
->total_hw_samples_mixed
+= total
;
756 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
760 "%s: write size %zu ret %zu total sw %zu\n",
762 size
>> sw
->info
.shift
,
764 sw
->total_hw_samples_mixed
768 return ret
<< sw
->info
.shift
;
772 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
774 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
775 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
780 #include "audio_template.h"
782 #include "audio_template.h"
787 static int audio_is_timer_needed(AudioState
*s
)
789 HWVoiceIn
*hwi
= NULL
;
790 HWVoiceOut
*hwo
= NULL
;
792 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
793 if (!hwo
->poll_mode
) return 1;
795 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
796 if (!hwi
->poll_mode
) return 1;
801 static void audio_reset_timer (AudioState
*s
)
803 if (audio_is_timer_needed(s
)) {
804 timer_mod_anticipate_ns(s
->ts
,
805 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
806 if (!s
->timer_running
) {
807 s
->timer_running
= true;
808 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
809 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
813 if (s
->timer_running
) {
814 s
->timer_running
= false;
815 trace_audio_timer_stop();
820 static void audio_timer (void *opaque
)
823 AudioState
*s
= opaque
;
825 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
826 diff
= now
- s
->timer_last
;
827 if (diff
> s
->period_ticks
* 3 / 2) {
828 trace_audio_timer_delayed(diff
/ SCALE_MS
);
832 audio_run(s
, "timer");
833 audio_reset_timer(s
);
839 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
842 /* XXX: Consider options */
846 if (!sw
->hw
->enabled
) {
847 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
851 return audio_pcm_sw_write(sw
, buf
, size
);
854 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
857 /* XXX: Consider options */
861 if (!sw
->hw
->enabled
) {
862 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
866 return audio_pcm_sw_read(sw
, buf
, size
);
869 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
871 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
874 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
883 if (sw
->active
!= on
) {
884 AudioState
*s
= sw
->s
;
889 hw
->pending_disable
= 0;
893 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
);
894 audio_reset_timer (s
);
902 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
903 temp_sw
= temp_sw
->entries
.le_next
) {
904 nb_active
+= temp_sw
->active
!= 0;
907 hw
->pending_disable
= nb_active
== 1;
911 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
912 sc
->sw
.active
= hw
->enabled
;
914 audio_capture_maybe_changed (sc
->cap
, 1);
921 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
930 if (sw
->active
!= on
) {
931 AudioState
*s
= sw
->s
;
938 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
);
939 audio_reset_timer (s
);
942 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
948 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
949 temp_sw
= temp_sw
->entries
.le_next
) {
950 nb_active
+= temp_sw
->active
!= 0;
953 if (nb_active
== 1) {
955 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
963 static size_t audio_get_avail (SWVoiceIn
*sw
)
971 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
972 if (audio_bug(__func__
, live
> sw
->hw
->samples
)) {
973 dolog("live=%zu sw->hw->samples=%zu\n", live
, sw
->hw
->samples
);
978 "%s: get_avail live %d ret %" PRId64
"\n",
980 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
983 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
986 static size_t audio_get_free(SWVoiceOut
*sw
)
994 live
= sw
->total_hw_samples_mixed
;
996 if (audio_bug(__func__
, live
> sw
->hw
->samples
)) {
997 dolog("live=%zu sw->hw->samples=%zu\n", live
, sw
->hw
->samples
);
1001 dead
= sw
->hw
->samples
- live
;
1004 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1006 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1009 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1012 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1020 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1021 SWVoiceOut
*sw
= &sc
->sw
;
1026 size_t till_end_of_hw
= hw
->samples
- rpos2
;
1027 size_t to_write
= MIN(till_end_of_hw
, n
);
1028 size_t bytes
= to_write
<< hw
->info
.shift
;
1031 sw
->buf
= hw
->mix_buf
+ rpos2
;
1032 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1033 if (written
- bytes
) {
1034 dolog("Could not mix %zu bytes into a capture "
1035 "buffer, mixed %zu\n",
1040 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1045 n
= MIN(samples
, hw
->samples
- rpos
);
1046 mixeng_clear(hw
->mix_buf
+ rpos
, n
);
1047 mixeng_clear(hw
->mix_buf
, samples
- n
);
1050 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1055 size_t size
, decr
, proc
;
1056 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1058 /* retrying will likely won't help, drop everything. */
1059 hw
->rpos
= (hw
->rpos
+ live
) % hw
->samples
;
1060 return clipped
+ live
;
1063 decr
= MIN(size
>> hw
->info
.shift
, live
);
1064 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1065 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
, decr
<< hw
->info
.shift
) >>
1070 hw
->rpos
= (hw
->rpos
+ proc
) % hw
->samples
;
1072 if (proc
== 0 || proc
< decr
) {
1080 static void audio_run_out (AudioState
*s
)
1082 HWVoiceOut
*hw
= NULL
;
1085 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1086 size_t played
, live
, prev_rpos
, free
;
1087 int nb_live
, cleanup_required
;
1089 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1094 if (audio_bug(__func__
, live
> hw
->samples
)) {
1095 dolog ("live=%zu hw->samples=%zu\n", live
, hw
->samples
);
1099 if (hw
->pending_disable
&& !nb_live
) {
1102 dolog ("Disabling voice\n");
1105 hw
->pending_disable
= 0;
1106 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1107 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1109 audio_recalc_and_notify_capture (sc
->cap
);
1115 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1117 free
= audio_get_free (sw
);
1119 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1126 prev_rpos
= hw
->rpos
;
1127 played
= audio_pcm_hw_run_out(hw
, live
);
1128 replay_audio_out(&played
);
1129 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1130 dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n",
1131 hw
->rpos
, hw
->samples
, played
);
1136 dolog("played=%zu\n", played
);
1140 hw
->ts_helper
+= played
;
1141 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1144 cleanup_required
= 0;
1145 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1146 if (!sw
->active
&& sw
->empty
) {
1150 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1151 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1152 played
, sw
->total_hw_samples_mixed
);
1153 played
= sw
->total_hw_samples_mixed
;
1156 sw
->total_hw_samples_mixed
-= played
;
1158 if (!sw
->total_hw_samples_mixed
) {
1160 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1164 free
= audio_get_free (sw
);
1166 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1171 if (cleanup_required
) {
1174 sw
= hw
->sw_head
.lh_first
;
1176 sw1
= sw
->entries
.le_next
;
1177 if (!sw
->active
&& !sw
->callback
.fn
) {
1178 audio_close_out (sw
);
1186 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1192 size_t size
= samples
<< hw
->info
.shift
;
1193 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1195 assert((size
& hw
->info
.align
) == 0);
1197 hw
->pcm_ops
->put_buffer_in(hw
, buf
, size
);
1201 proc
= MIN(size
>> hw
->info
.shift
,
1202 hw
->samples
- hw
->wpos
);
1204 hw
->conv(hw
->conv_buf
+ hw
->wpos
, buf
, proc
);
1205 hw
->wpos
= (hw
->wpos
+ proc
) % hw
->samples
;
1209 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
<< hw
->info
.shift
);
1215 static void audio_run_in (AudioState
*s
)
1217 HWVoiceIn
*hw
= NULL
;
1219 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1221 size_t captured
= 0, min
;
1223 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1224 captured
= audio_pcm_hw_run_in(
1225 hw
, hw
->samples
- audio_pcm_hw_get_live_in(hw
));
1227 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1229 min
= audio_pcm_hw_find_min_in (hw
);
1230 hw
->total_samples_captured
+= captured
- min
;
1231 hw
->ts_helper
+= captured
;
1233 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1234 sw
->total_hw_samples_acquired
-= min
;
1239 avail
= audio_get_avail (sw
);
1241 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1248 static void audio_run_capture (AudioState
*s
)
1250 CaptureVoiceOut
*cap
;
1252 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1253 size_t live
, rpos
, captured
;
1254 HWVoiceOut
*hw
= &cap
->hw
;
1257 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1260 size_t left
= hw
->samples
- rpos
;
1261 size_t to_capture
= MIN(live
, left
);
1262 struct st_sample
*src
;
1263 struct capture_callback
*cb
;
1265 src
= hw
->mix_buf
+ rpos
;
1266 hw
->clip (cap
->buf
, src
, to_capture
);
1267 mixeng_clear (src
, to_capture
);
1269 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1270 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1271 to_capture
<< hw
->info
.shift
);
1273 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1278 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1279 if (!sw
->active
&& sw
->empty
) {
1283 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1284 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1285 captured
, sw
->total_hw_samples_mixed
);
1286 captured
= sw
->total_hw_samples_mixed
;
1289 sw
->total_hw_samples_mixed
-= captured
;
1290 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1295 void audio_run(AudioState
*s
, const char *msg
)
1299 audio_run_capture(s
);
1303 static double prevtime
;
1307 if (gettimeofday (&tv
, NULL
)) {
1308 perror ("audio_run: gettimeofday");
1312 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1313 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1314 prevtime
= currtime
;
1319 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1323 if (unlikely(!hw
->buf_emul
)) {
1324 size_t calc_size
= hw
->samples
<< hw
->info
.shift
;
1325 hw
->buf_emul
= g_malloc(calc_size
);
1326 hw
->size_emul
= calc_size
;
1327 hw
->pos_emul
= hw
->pending_emul
= 0;
1330 while (hw
->pending_emul
< hw
->size_emul
) {
1331 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1332 hw
->size_emul
- hw
->pending_emul
);
1333 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1335 hw
->pending_emul
+= read
;
1336 if (read
< read_len
) {
1341 start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1343 start
+= hw
->size_emul
;
1345 assert(start
>= 0 && start
< hw
->size_emul
);
1347 *size
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1348 return hw
->buf_emul
+ start
;
1351 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1353 assert(size
<= hw
->pending_emul
);
1354 hw
->pending_emul
-= size
;
1357 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1359 if (unlikely(!hw
->buf_emul
)) {
1360 size_t calc_size
= hw
->samples
<< hw
->info
.shift
;
1362 hw
->buf_emul
= g_malloc(calc_size
);
1363 hw
->size_emul
= calc_size
;
1364 hw
->pos_emul
= hw
->pending_emul
= 0;
1367 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1368 hw
->size_emul
- hw
->pos_emul
);
1369 return hw
->buf_emul
+ hw
->pos_emul
;
1372 size_t audio_generic_put_buffer_out_nowrite(HWVoiceOut
*hw
, void *buf
,
1375 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1376 size
+ hw
->pending_emul
<= hw
->size_emul
);
1378 hw
->pending_emul
+= size
;
1379 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1384 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1386 audio_generic_put_buffer_out_nowrite(hw
, buf
, size
);
1388 while (hw
->pending_emul
) {
1389 size_t write_len
, written
;
1390 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
1392 start
+= hw
->size_emul
;
1394 assert(start
>= 0 && start
< hw
->size_emul
);
1396 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1398 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1399 hw
->pending_emul
-= written
;
1401 if (written
< write_len
) {
1407 * fake we have written everything. non-written data remain in pending_emul,
1408 * so we do not have to clip them multiple times
1413 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1415 size_t dst_size
, copy_size
;
1416 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1417 copy_size
= MIN(size
, dst_size
);
1419 memcpy(dst
, buf
, copy_size
);
1420 return hw
->pcm_ops
->put_buffer_out(hw
, buf
, copy_size
);
1423 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1425 size_t dst_size
, copy_size
;
1426 void *dst
= hw
->pcm_ops
->get_buffer_in(hw
, &dst_size
);
1427 copy_size
= MIN(size
, dst_size
);
1429 memcpy(dst
, buf
, copy_size
);
1430 hw
->pcm_ops
->put_buffer_in(hw
, buf
, copy_size
);
1435 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1436 bool msg
, Audiodev
*dev
)
1438 s
->drv_opaque
= drv
->init(dev
);
1440 if (s
->drv_opaque
) {
1441 if (!drv
->pcm_ops
->get_buffer_in
) {
1442 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1443 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1445 if (!drv
->pcm_ops
->get_buffer_out
) {
1446 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1447 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1450 audio_init_nb_voices_out(s
, drv
);
1451 audio_init_nb_voices_in(s
, drv
);
1457 dolog("Could not init `%s' audio driver\n", drv
->name
);
1463 static void audio_vm_change_state_handler (void *opaque
, int running
,
1466 AudioState
*s
= opaque
;
1467 HWVoiceOut
*hwo
= NULL
;
1468 HWVoiceIn
*hwi
= NULL
;
1469 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1471 s
->vm_running
= running
;
1472 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1473 hwo
->pcm_ops
->ctl_out(hwo
, op
);
1476 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1477 hwi
->pcm_ops
->ctl_in(hwi
, op
);
1479 audio_reset_timer (s
);
1482 static bool is_cleaning_up
;
1484 bool audio_is_cleaning_up(void)
1486 return is_cleaning_up
;
1489 static void free_audio_state(AudioState
*s
)
1491 HWVoiceOut
*hwo
, *hwon
;
1492 HWVoiceIn
*hwi
, *hwin
;
1494 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1498 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1500 hwo
->pcm_ops
->fini_out (hwo
);
1502 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1503 CaptureVoiceOut
*cap
= sc
->cap
;
1504 struct capture_callback
*cb
;
1506 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1507 cb
->ops
.destroy (cb
->opaque
);
1510 QLIST_REMOVE(hwo
, entries
);
1513 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1515 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1517 hwi
->pcm_ops
->fini_in (hwi
);
1518 QLIST_REMOVE(hwi
, entries
);
1522 s
->drv
->fini (s
->drv_opaque
);
1527 qapi_free_Audiodev(s
->dev
);
1539 void audio_cleanup(void)
1541 is_cleaning_up
= true;
1542 while (!QTAILQ_EMPTY(&audio_states
)) {
1543 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1544 QTAILQ_REMOVE(&audio_states
, s
, list
);
1545 free_audio_state(s
);
1549 static const VMStateDescription vmstate_audio
= {
1552 .minimum_version_id
= 1,
1553 .fields
= (VMStateField
[]) {
1554 VMSTATE_END_OF_LIST()
1558 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1560 static AudiodevListEntry
*audiodev_find(
1561 AudiodevListHead
*head
, const char *drvname
)
1563 AudiodevListEntry
*e
;
1564 QSIMPLEQ_FOREACH(e
, head
, next
) {
1565 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1574 * if we have dev, this function was called because of an -audiodev argument =>
1575 * initialize a new state with it
1576 * if dev == NULL => legacy implicit initialization, return the already created
1577 * state or create a new one
1579 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1581 static bool atexit_registered
;
1584 const char *drvname
= NULL
;
1585 VMChangeStateEntry
*e
;
1587 struct audio_driver
*driver
;
1588 /* silence gcc warning about uninitialized variable */
1589 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1592 /* -audiodev option */
1593 legacy_config
= false;
1594 drvname
= AudiodevDriver_str(dev
->driver
);
1595 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1596 if (!legacy_config
) {
1597 dolog("Device %s: audiodev default parameter is deprecated, please "
1598 "specify audiodev=%s\n", name
,
1599 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1601 return QTAILQ_FIRST(&audio_states
);
1603 /* legacy implicit initialization */
1604 head
= audio_handle_legacy_opts();
1606 * In case of legacy initialization, all Audiodevs in the list will have
1607 * the same configuration (except the driver), so it does't matter which
1608 * one we chose. We need an Audiodev to set up AudioState before we can
1609 * init a driver. Also note that dev at this point is still in the
1612 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1613 audio_validate_opts(dev
, &error_abort
);
1616 s
= g_malloc0(sizeof(AudioState
));
1619 QLIST_INIT (&s
->hw_head_out
);
1620 QLIST_INIT (&s
->hw_head_in
);
1621 QLIST_INIT (&s
->cap_head
);
1622 if (!atexit_registered
) {
1623 atexit(audio_cleanup
);
1624 atexit_registered
= true;
1626 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1628 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1630 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1631 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1633 if (s
->nb_hw_voices_out
<= 0) {
1634 dolog ("Bogus number of playback voices %d, setting to 1\n",
1635 s
->nb_hw_voices_out
);
1636 s
->nb_hw_voices_out
= 1;
1639 if (s
->nb_hw_voices_in
<= 0) {
1640 dolog ("Bogus number of capture voices %d, setting to 0\n",
1641 s
->nb_hw_voices_in
);
1642 s
->nb_hw_voices_in
= 0;
1646 driver
= audio_driver_lookup(drvname
);
1648 done
= !audio_driver_init(s
, driver
, true, dev
);
1650 dolog ("Unknown audio driver `%s'\n", drvname
);
1653 for (i
= 0; audio_prio_list
[i
]; i
++) {
1654 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1655 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1658 s
->dev
= dev
= e
->dev
;
1659 audio_validate_opts(dev
, &error_abort
);
1660 done
= !audio_driver_init(s
, driver
, false, dev
);
1668 audio_free_audiodev_list(&head
);
1671 driver
= audio_driver_lookup("none");
1672 done
= !audio_driver_init(s
, driver
, false, dev
);
1674 dolog("warning: Using timer based audio emulation\n");
1677 if (dev
->timer_period
<= 0) {
1678 s
->period_ticks
= 1;
1680 s
->period_ticks
= dev
->timer_period
* SCALE_US
;
1683 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1685 dolog ("warning: Could not register change state handler\n"
1686 "(Audio can continue looping even after stopping the VM)\n");
1689 QLIST_INIT (&s
->card_head
);
1690 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1694 void audio_free_audiodev_list(AudiodevListHead
*head
)
1696 AudiodevListEntry
*e
;
1697 while ((e
= QSIMPLEQ_FIRST(head
))) {
1698 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1699 qapi_free_Audiodev(e
->dev
);
1704 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1707 card
->state
= audio_init(NULL
, name
);
1710 card
->name
= g_strdup (name
);
1711 memset (&card
->entries
, 0, sizeof (card
->entries
));
1712 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1715 void AUD_remove_card (QEMUSoundCard
*card
)
1717 QLIST_REMOVE (card
, entries
);
1718 g_free (card
->name
);
1722 CaptureVoiceOut
*AUD_add_capture(
1724 struct audsettings
*as
,
1725 struct audio_capture_ops
*ops
,
1729 CaptureVoiceOut
*cap
;
1730 struct capture_callback
*cb
;
1733 if (!legacy_config
) {
1734 dolog("Capturing without setting an audiodev is deprecated\n");
1736 s
= audio_init(NULL
, NULL
);
1739 if (audio_validate_settings (as
)) {
1740 dolog ("Invalid settings were passed when trying to add capture\n");
1741 audio_print_settings (as
);
1745 cb
= g_malloc0(sizeof(*cb
));
1747 cb
->opaque
= cb_opaque
;
1749 cap
= audio_pcm_capture_find_specific(s
, as
);
1751 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1756 CaptureVoiceOut
*cap
;
1758 cap
= g_malloc0(sizeof(*cap
));
1762 QLIST_INIT (&hw
->sw_head
);
1763 QLIST_INIT (&cap
->cb_head
);
1765 /* XXX find a more elegant way */
1766 hw
->samples
= 4096 * 4;
1767 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1769 audio_pcm_init_info (&hw
->info
, as
);
1771 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1773 hw
->clip
= mixeng_clip
1774 [hw
->info
.nchannels
== 2]
1776 [hw
->info
.swap_endianness
]
1777 [audio_bits_to_index (hw
->info
.bits
)];
1779 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1780 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1782 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1783 audio_attach_capture (hw
);
1789 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1791 struct capture_callback
*cb
;
1793 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1794 if (cb
->opaque
== cb_opaque
) {
1795 cb
->ops
.destroy (cb_opaque
);
1796 QLIST_REMOVE (cb
, entries
);
1799 if (!cap
->cb_head
.lh_first
) {
1800 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1803 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1804 #ifdef DEBUG_CAPTURE
1805 dolog ("freeing %s\n", sw
->name
);
1808 sw1
= sw
->entries
.le_next
;
1810 st_rate_stop (sw
->rate
);
1813 QLIST_REMOVE (sw
, entries
);
1814 QLIST_REMOVE (sc
, entries
);
1818 QLIST_REMOVE (cap
, entries
);
1819 g_free (cap
->hw
.mix_buf
);
1828 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1831 HWVoiceOut
*hw
= sw
->hw
;
1833 sw
->vol
.mute
= mute
;
1834 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1835 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1837 if (hw
->pcm_ops
->ctl_out
) {
1838 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1843 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1846 HWVoiceIn
*hw
= sw
->hw
;
1848 sw
->vol
.mute
= mute
;
1849 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1850 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1852 if (hw
->pcm_ops
->ctl_in
) {
1853 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1858 void audio_create_pdos(Audiodev
*dev
)
1860 switch (dev
->driver
) {
1861 #define CASE(DRIVER, driver, pdo_name) \
1862 case AUDIODEV_DRIVER_##DRIVER: \
1863 if (!dev->u.driver.has_in) { \
1864 dev->u.driver.in = g_malloc0( \
1865 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1866 dev->u.driver.has_in = true; \
1868 if (!dev->u.driver.has_out) { \
1869 dev->u.driver.out = g_malloc0( \
1870 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1871 dev->u.driver.has_out = true; \
1876 CASE(ALSA
, alsa
, Alsa
);
1877 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1878 CASE(DSOUND
, dsound
, );
1879 CASE(OSS
, oss
, Oss
);
1882 CASE(SPICE
, spice
, );
1885 case AUDIODEV_DRIVER__MAX
:
1890 static void audio_validate_per_direction_opts(
1891 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1893 if (!pdo
->has_fixed_settings
) {
1894 pdo
->has_fixed_settings
= true;
1895 pdo
->fixed_settings
= true;
1897 if (!pdo
->fixed_settings
&&
1898 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1900 "You can't use frequency, channels or format with fixed-settings=off");
1904 if (!pdo
->has_frequency
) {
1905 pdo
->has_frequency
= true;
1906 pdo
->frequency
= 44100;
1908 if (!pdo
->has_channels
) {
1909 pdo
->has_channels
= true;
1912 if (!pdo
->has_voices
) {
1913 pdo
->has_voices
= true;
1916 if (!pdo
->has_format
) {
1917 pdo
->has_format
= true;
1918 pdo
->format
= AUDIO_FORMAT_S16
;
1922 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1926 audio_create_pdos(dev
);
1928 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1930 error_propagate(errp
, err
);
1934 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1936 error_propagate(errp
, err
);
1940 if (!dev
->has_timer_period
) {
1941 dev
->has_timer_period
= true;
1942 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1946 void audio_parse_option(const char *opt
)
1948 AudiodevListEntry
*e
;
1949 Audiodev
*dev
= NULL
;
1951 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1952 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1955 audio_validate_opts(dev
, &error_fatal
);
1957 e
= g_malloc0(sizeof(AudiodevListEntry
));
1959 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1962 void audio_init_audiodevs(void)
1964 AudiodevListEntry
*e
;
1966 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1967 audio_init(e
->dev
, NULL
);
1971 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1973 return (audsettings
) {
1974 .freq
= pdo
->frequency
,
1975 .nchannels
= pdo
->channels
,
1977 .endianness
= AUDIO_HOST_ENDIANNESS
,
1981 int audioformat_bytes_per_sample(AudioFormat fmt
)
1984 case AUDIO_FORMAT_U8
:
1985 case AUDIO_FORMAT_S8
:
1988 case AUDIO_FORMAT_U16
:
1989 case AUDIO_FORMAT_S16
:
1992 case AUDIO_FORMAT_U32
:
1993 case AUDIO_FORMAT_S32
:
1996 case AUDIO_FORMAT__MAX
:
2003 /* frames = freq * usec / 1e6 */
2004 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2005 audsettings
*as
, int def_usecs
)
2007 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2008 return (as
->freq
* usecs
+ 500000) / 1000000;
2011 /* samples = channels * frames = channels * freq * usec / 1e6 */
2012 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2013 audsettings
*as
, int def_usecs
)
2015 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2019 * bytes = bytes_per_sample * samples =
2020 * bytes_per_sample * channels * freq * usec / 1e6
2022 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2023 audsettings
*as
, int def_usecs
)
2025 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2026 audioformat_bytes_per_sample(as
->fmt
);
2029 AudioState
*audio_state_by_name(const char *name
)
2032 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2034 if (strcmp(name
, s
->dev
->id
) == 0) {
2041 const char *audio_get_id(QEMUSoundCard
*card
)
2044 assert(card
->state
->dev
);
2045 return card
->state
->dev
->id
;