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 "sysemu/sysemu.h"
30 #include "qemu/cutils.h"
31 #include "sysemu/replay.h"
33 #define AUDIO_CAP "audio"
34 #include "audio_int.h"
36 /* #define DEBUG_LIVE */
37 /* #define DEBUG_OUT */
38 /* #define DEBUG_CAPTURE */
39 /* #define DEBUG_POLL */
41 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
44 /* Order of CONFIG_AUDIO_DRIVERS is import.
45 The 1st one is the one used by default, that is the reason
46 that we generate the list.
48 static const char *audio_prio_list
[] = {
55 static QLIST_HEAD(, audio_driver
) audio_drivers
;
57 void audio_driver_register(audio_driver
*drv
)
59 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
62 audio_driver
*audio_driver_lookup(const char *name
)
64 struct audio_driver
*d
;
66 QLIST_FOREACH(d
, &audio_drivers
, next
) {
67 if (strcmp(name
, d
->name
) == 0) {
74 struct fixed_settings
{
78 struct audsettings settings
;
82 struct fixed_settings fixed_out
;
83 struct fixed_settings fixed_in
;
91 .fixed_out
= { /* DAC fixed settings */
99 .endianness
= AUDIO_HOST_ENDIANNESS
,
103 .fixed_in
= { /* ADC fixed settings */
111 .endianness
= AUDIO_HOST_ENDIANNESS
,
115 .period
= { .hertz
= 100 },
120 static AudioState glob_audio_state
;
122 const struct mixeng_volume nominal_volume
= {
133 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
136 static void audio_print_options (const char *prefix
,
137 struct audio_option
*opt
);
139 int audio_bug (const char *funcname
, int cond
)
144 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
146 struct audio_driver
*d
;
149 AUD_log (NULL
, "Save all your work and restart without audio\n");
150 AUD_log (NULL
, "Please send bug report to av1474@comtv.ru\n");
151 AUD_log (NULL
, "I am sorry\n");
152 d
= glob_audio_state
.drv
;
154 audio_print_options (d
->name
, d
->options
);
157 AUD_log (NULL
, "Context:\n");
159 #if defined AUDIO_BREAKPOINT_ON_BUG
160 # if defined HOST_I386
161 # if defined __GNUC__
163 # elif defined _MSC_VER
178 static inline int audio_bits_to_index (int bits
)
191 audio_bug ("bits_to_index", 1);
192 AUD_log (NULL
, "invalid bits %d\n", bits
);
197 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
203 cond
= !nmemb
|| !size
;
207 if (audio_bug ("audio_calloc", cond
)) {
208 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
210 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
214 return g_malloc0 (len
);
217 static char *audio_alloc_prefix (const char *s
)
219 const char qemu_prefix
[] = "QEMU_";
228 r
= g_malloc (len
+ sizeof (qemu_prefix
));
230 u
= r
+ sizeof (qemu_prefix
) - 1;
232 pstrcpy (r
, len
+ sizeof (qemu_prefix
), qemu_prefix
);
233 pstrcat (r
, len
+ sizeof (qemu_prefix
), s
);
235 for (i
= 0; i
< len
; ++i
) {
236 u
[i
] = qemu_toupper(u
[i
]);
242 static const char *audio_audfmt_to_string (audfmt_e fmt
)
264 dolog ("Bogus audfmt %d returning S16\n", fmt
);
268 static audfmt_e
audio_string_to_audfmt (const char *s
, audfmt_e defval
,
271 if (!strcasecmp (s
, "u8")) {
275 else if (!strcasecmp (s
, "u16")) {
279 else if (!strcasecmp (s
, "u32")) {
283 else if (!strcasecmp (s
, "s8")) {
287 else if (!strcasecmp (s
, "s16")) {
291 else if (!strcasecmp (s
, "s32")) {
296 dolog ("Bogus audio format `%s' using %s\n",
297 s
, audio_audfmt_to_string (defval
));
303 static audfmt_e
audio_get_conf_fmt (const char *envname
,
307 const char *var
= getenv (envname
);
312 return audio_string_to_audfmt (var
, defval
, defaultp
);
315 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
320 strval
= getenv (key
);
332 static const char *audio_get_conf_str (const char *key
,
336 const char *val
= getenv (key
);
347 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
350 fprintf(stderr
, "%s: ", cap
);
353 vfprintf(stderr
, fmt
, ap
);
356 void AUD_log (const char *cap
, const char *fmt
, ...)
361 AUD_vlog (cap
, fmt
, ap
);
365 static void audio_print_options (const char *prefix
,
366 struct audio_option
*opt
)
371 dolog ("No prefix specified\n");
376 dolog ("No options\n");
380 uprefix
= audio_alloc_prefix (prefix
);
382 for (; opt
->name
; opt
++) {
383 const char *state
= "default";
384 printf (" %s_%s: ", uprefix
, opt
->name
);
386 if (opt
->overriddenp
&& *opt
->overriddenp
) {
393 int *intp
= opt
->valp
;
394 printf ("boolean, %s = %d\n", state
, *intp
? 1 : 0);
400 int *intp
= opt
->valp
;
401 printf ("integer, %s = %d\n", state
, *intp
);
407 audfmt_e
*fmtp
= opt
->valp
;
409 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
411 audio_audfmt_to_string (*fmtp
)
418 const char **strp
= opt
->valp
;
419 printf ("string, %s = %s\n",
421 *strp
? *strp
: "(not set)");
427 dolog ("Bad value tag for option %s_%s %d\n",
428 uprefix
, opt
->name
, opt
->tag
);
431 printf (" %s\n", opt
->descr
);
437 static void audio_process_options (const char *prefix
,
438 struct audio_option
*opt
)
441 const char qemu_prefix
[] = "QEMU_";
442 size_t preflen
, optlen
;
444 if (audio_bug(__func__
, !prefix
)) {
445 dolog ("prefix = NULL\n");
449 if (audio_bug(__func__
, !opt
)) {
450 dolog ("opt = NULL\n");
454 preflen
= strlen (prefix
);
456 for (; opt
->name
; opt
++) {
461 dolog ("Option value pointer for `%s' is not set\n",
466 len
= strlen (opt
->name
);
467 /* len of opt->name + len of prefix + size of qemu_prefix
468 * (includes trailing zero) + zero + underscore (on behalf of
470 optlen
= len
+ preflen
+ sizeof (qemu_prefix
) + 1;
471 optname
= g_malloc (optlen
);
473 pstrcpy (optname
, optlen
, qemu_prefix
);
475 /* copy while upper-casing, including trailing zero */
476 for (i
= 0; i
<= preflen
; ++i
) {
477 optname
[i
+ sizeof (qemu_prefix
) - 1] = qemu_toupper(prefix
[i
]);
479 pstrcat (optname
, optlen
, "_");
480 pstrcat (optname
, optlen
, opt
->name
);
487 int *intp
= opt
->valp
;
488 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
494 audfmt_e
*fmtp
= opt
->valp
;
495 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
501 const char **strp
= opt
->valp
;
502 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
507 dolog ("Bad value tag for option `%s' - %d\n",
512 if (!opt
->overriddenp
) {
513 opt
->overriddenp
= &opt
->overridden
;
515 *opt
->overriddenp
= !def
;
520 static void audio_print_settings (struct audsettings
*as
)
522 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
526 AUD_log (NULL
, "S8");
529 AUD_log (NULL
, "U8");
532 AUD_log (NULL
, "S16");
535 AUD_log (NULL
, "U16");
538 AUD_log (NULL
, "S32");
541 AUD_log (NULL
, "U32");
544 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
548 AUD_log (NULL
, " endianness=");
549 switch (as
->endianness
) {
551 AUD_log (NULL
, "little");
554 AUD_log (NULL
, "big");
557 AUD_log (NULL
, "invalid");
560 AUD_log (NULL
, "\n");
563 static int audio_validate_settings (struct audsettings
*as
)
567 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
568 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
583 invalid
|= as
->freq
<= 0;
584 return invalid
? -1 : 0;
587 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
589 int bits
= 8, sign
= 0;
612 return info
->freq
== as
->freq
613 && info
->nchannels
== as
->nchannels
614 && info
->sign
== sign
615 && info
->bits
== bits
616 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
619 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
621 int bits
= 8, sign
= 0, shift
= 0;
644 info
->freq
= as
->freq
;
647 info
->nchannels
= as
->nchannels
;
648 info
->shift
= (as
->nchannels
== 2) + shift
;
649 info
->align
= (1 << info
->shift
) - 1;
650 info
->bytes_per_second
= info
->freq
<< info
->shift
;
651 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
654 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
661 memset (buf
, 0x00, len
<< info
->shift
);
664 switch (info
->bits
) {
666 memset (buf
, 0x80, len
<< info
->shift
);
673 int shift
= info
->nchannels
- 1;
676 if (info
->swap_endianness
) {
680 for (i
= 0; i
< len
<< shift
; i
++) {
690 int shift
= info
->nchannels
- 1;
691 int32_t s
= INT32_MAX
;
693 if (info
->swap_endianness
) {
697 for (i
= 0; i
< len
<< shift
; i
++) {
704 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
714 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
721 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
722 struct audsettings
*as
725 CaptureVoiceOut
*cap
;
726 AudioState
*s
= &glob_audio_state
;
728 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
729 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
736 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
738 struct capture_callback
*cb
;
741 dolog ("notification %d sent\n", cmd
);
743 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
744 cb
->ops
.notify (cb
->opaque
, cmd
);
748 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
750 if (cap
->hw
.enabled
!= enabled
) {
751 audcnotification_e cmd
;
752 cap
->hw
.enabled
= enabled
;
753 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
754 audio_notify_capture (cap
, cmd
);
758 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
760 HWVoiceOut
*hw
= &cap
->hw
;
764 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
770 audio_capture_maybe_changed (cap
, enabled
);
773 static void audio_detach_capture (HWVoiceOut
*hw
)
775 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
778 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
779 SWVoiceOut
*sw
= &sc
->sw
;
780 CaptureVoiceOut
*cap
= sc
->cap
;
781 int was_active
= sw
->active
;
784 st_rate_stop (sw
->rate
);
788 QLIST_REMOVE (sw
, entries
);
789 QLIST_REMOVE (sc
, entries
);
792 /* We have removed soft voice from the capture:
793 this might have changed the overall status of the capture
794 since this might have been the only active voice */
795 audio_recalc_and_notify_capture (cap
);
801 static int audio_attach_capture (HWVoiceOut
*hw
)
803 AudioState
*s
= &glob_audio_state
;
804 CaptureVoiceOut
*cap
;
806 audio_detach_capture (hw
);
807 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
810 HWVoiceOut
*hw_cap
= &cap
->hw
;
812 sc
= audio_calloc(__func__
, 1, sizeof(*sc
));
814 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
824 sw
->active
= hw
->enabled
;
825 sw
->conv
= noop_conv
;
826 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
827 sw
->vol
= nominal_volume
;
828 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
830 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
834 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
835 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
837 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
838 hw
, sw
->info
.freq
, sw
->info
.bits
,
840 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
843 audio_capture_maybe_changed (cap
, 1);
850 * Hard voice (capture)
852 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
855 int m
= hw
->total_samples_captured
;
857 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
859 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
865 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
867 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
868 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
869 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
875 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
876 int live
, int pending
)
878 int left
= hw
->samples
- pending
;
879 int len
= audio_MIN (left
, live
);
883 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
884 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
885 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
886 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
888 hw
->clip (dst
, src
, samples_to_clip
);
890 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
891 len
-= samples_to_clip
;
892 clipped
+= samples_to_clip
;
898 * Soft voice (capture)
900 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
902 HWVoiceIn
*hw
= sw
->hw
;
903 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
906 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
907 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
911 rpos
= hw
->wpos
- live
;
916 return hw
->samples
+ rpos
;
920 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
922 HWVoiceIn
*hw
= sw
->hw
;
923 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
924 struct st_sample
*src
, *dst
= sw
->buf
;
926 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
928 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
929 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
930 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
934 samples
= size
>> sw
->info
.shift
;
939 swlim
= (live
* sw
->ratio
) >> 32;
940 swlim
= audio_MIN (swlim
, samples
);
943 src
= hw
->conv_buf
+ rpos
;
944 isamp
= hw
->wpos
- rpos
;
947 isamp
= hw
->samples
- rpos
;
955 if (audio_bug(__func__
, osamp
< 0)) {
956 dolog ("osamp=%d\n", osamp
);
960 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
962 rpos
= (rpos
+ isamp
) % hw
->samples
;
968 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
969 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
972 sw
->clip (buf
, sw
->buf
, ret
);
973 sw
->total_hw_samples_acquired
+= total
;
974 return ret
<< sw
->info
.shift
;
978 * Hard voice (playback)
980 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
986 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
987 if (sw
->active
|| !sw
->empty
) {
988 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
997 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
1002 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
1004 *nb_live
= nb_live1
;
1010 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1011 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1020 * Soft voice (playback)
1022 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
1024 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
1025 int ret
= 0, pos
= 0, total
= 0;
1031 hwsamples
= sw
->hw
->samples
;
1033 live
= sw
->total_hw_samples_mixed
;
1034 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
1035 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
1039 if (live
== hwsamples
) {
1041 dolog ("%s is full %d\n", sw
->name
, live
);
1046 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
1047 samples
= size
>> sw
->info
.shift
;
1049 dead
= hwsamples
- live
;
1050 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
1051 swlim
= audio_MIN (swlim
, samples
);
1053 sw
->conv (sw
->buf
, buf
, swlim
);
1055 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
1056 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
1061 dead
= hwsamples
- live
;
1062 left
= hwsamples
- wpos
;
1063 blck
= audio_MIN (dead
, left
);
1072 sw
->hw
->mix_buf
+ wpos
,
1080 wpos
= (wpos
+ osamp
) % hwsamples
;
1084 sw
->total_hw_samples_mixed
+= total
;
1085 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1089 "%s: write size %d ret %d total sw %d\n",
1091 size
>> sw
->info
.shift
,
1093 sw
->total_hw_samples_mixed
1097 return ret
<< sw
->info
.shift
;
1101 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
1103 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1104 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
1109 #include "audio_template.h"
1111 #include "audio_template.h"
1116 static int audio_is_timer_needed (void)
1118 HWVoiceIn
*hwi
= NULL
;
1119 HWVoiceOut
*hwo
= NULL
;
1121 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1122 if (!hwo
->poll_mode
) return 1;
1124 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1125 if (!hwi
->poll_mode
) return 1;
1130 static void audio_reset_timer (AudioState
*s
)
1132 if (audio_is_timer_needed ()) {
1133 timer_mod_anticipate_ns(s
->ts
,
1134 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + conf
.period
.ticks
);
1141 static void audio_timer (void *opaque
)
1143 audio_run ("timer");
1144 audio_reset_timer (opaque
);
1150 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1153 /* XXX: Consider options */
1157 if (!sw
->hw
->enabled
) {
1158 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1162 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
1165 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1168 /* XXX: Consider options */
1172 if (!sw
->hw
->enabled
) {
1173 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1177 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
1180 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1182 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1185 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1194 if (sw
->active
!= on
) {
1195 AudioState
*s
= &glob_audio_state
;
1196 SWVoiceOut
*temp_sw
;
1200 hw
->pending_disable
= 0;
1203 if (s
->vm_running
) {
1204 hw
->pcm_ops
->ctl_out (hw
, VOICE_ENABLE
, conf
.try_poll_out
);
1205 audio_reset_timer (s
);
1213 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1214 temp_sw
= temp_sw
->entries
.le_next
) {
1215 nb_active
+= temp_sw
->active
!= 0;
1218 hw
->pending_disable
= nb_active
== 1;
1222 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1223 sc
->sw
.active
= hw
->enabled
;
1225 audio_capture_maybe_changed (sc
->cap
, 1);
1232 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1241 if (sw
->active
!= on
) {
1242 AudioState
*s
= &glob_audio_state
;
1248 if (s
->vm_running
) {
1249 hw
->pcm_ops
->ctl_in (hw
, VOICE_ENABLE
, conf
.try_poll_in
);
1250 audio_reset_timer (s
);
1253 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1259 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1260 temp_sw
= temp_sw
->entries
.le_next
) {
1261 nb_active
+= temp_sw
->active
!= 0;
1264 if (nb_active
== 1) {
1266 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1274 static int audio_get_avail (SWVoiceIn
*sw
)
1282 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1283 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1284 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1289 "%s: get_avail live %d ret %" PRId64
"\n",
1291 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1294 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1297 static int audio_get_free (SWVoiceOut
*sw
)
1305 live
= sw
->total_hw_samples_mixed
;
1307 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1308 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1312 dead
= sw
->hw
->samples
- live
;
1315 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1317 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1320 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1323 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1330 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1331 SWVoiceOut
*sw
= &sc
->sw
;
1336 int till_end_of_hw
= hw
->samples
- rpos2
;
1337 int to_write
= audio_MIN (till_end_of_hw
, n
);
1338 int bytes
= to_write
<< hw
->info
.shift
;
1341 sw
->buf
= hw
->mix_buf
+ rpos2
;
1342 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1343 if (written
- bytes
) {
1344 dolog ("Could not mix %d bytes into a capture "
1345 "buffer, mixed %d\n",
1350 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1355 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1356 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1357 mixeng_clear (hw
->mix_buf
, samples
- n
);
1360 static void audio_run_out (AudioState
*s
)
1362 HWVoiceOut
*hw
= NULL
;
1365 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1367 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1369 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1374 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1375 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1379 if (hw
->pending_disable
&& !nb_live
) {
1382 dolog ("Disabling voice\n");
1385 hw
->pending_disable
= 0;
1386 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1387 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1389 audio_recalc_and_notify_capture (sc
->cap
);
1395 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1397 free
= audio_get_free (sw
);
1399 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1406 prev_rpos
= hw
->rpos
;
1407 played
= hw
->pcm_ops
->run_out (hw
, live
);
1408 replay_audio_out(&played
);
1409 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1410 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1411 hw
->rpos
, hw
->samples
, played
);
1416 dolog ("played=%d\n", played
);
1420 hw
->ts_helper
+= played
;
1421 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1424 cleanup_required
= 0;
1425 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1426 if (!sw
->active
&& sw
->empty
) {
1430 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1431 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1432 played
, sw
->total_hw_samples_mixed
);
1433 played
= sw
->total_hw_samples_mixed
;
1436 sw
->total_hw_samples_mixed
-= played
;
1438 if (!sw
->total_hw_samples_mixed
) {
1440 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1444 free
= audio_get_free (sw
);
1446 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1451 if (cleanup_required
) {
1454 sw
= hw
->sw_head
.lh_first
;
1456 sw1
= sw
->entries
.le_next
;
1457 if (!sw
->active
&& !sw
->callback
.fn
) {
1458 audio_close_out (sw
);
1466 static void audio_run_in (AudioState
*s
)
1468 HWVoiceIn
*hw
= NULL
;
1470 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1472 int captured
= 0, min
;
1474 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1475 captured
= hw
->pcm_ops
->run_in(hw
);
1477 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1479 min
= audio_pcm_hw_find_min_in (hw
);
1480 hw
->total_samples_captured
+= captured
- min
;
1481 hw
->ts_helper
+= captured
;
1483 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1484 sw
->total_hw_samples_acquired
-= min
;
1489 avail
= audio_get_avail (sw
);
1491 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1498 static void audio_run_capture (AudioState
*s
)
1500 CaptureVoiceOut
*cap
;
1502 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1503 int live
, rpos
, captured
;
1504 HWVoiceOut
*hw
= &cap
->hw
;
1507 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1510 int left
= hw
->samples
- rpos
;
1511 int to_capture
= audio_MIN (live
, left
);
1512 struct st_sample
*src
;
1513 struct capture_callback
*cb
;
1515 src
= hw
->mix_buf
+ rpos
;
1516 hw
->clip (cap
->buf
, src
, to_capture
);
1517 mixeng_clear (src
, to_capture
);
1519 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1520 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1521 to_capture
<< hw
->info
.shift
);
1523 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1528 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1529 if (!sw
->active
&& sw
->empty
) {
1533 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1534 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1535 captured
, sw
->total_hw_samples_mixed
);
1536 captured
= sw
->total_hw_samples_mixed
;
1539 sw
->total_hw_samples_mixed
-= captured
;
1540 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1545 void audio_run (const char *msg
)
1547 AudioState
*s
= &glob_audio_state
;
1551 audio_run_capture (s
);
1554 static double prevtime
;
1558 if (gettimeofday (&tv
, NULL
)) {
1559 perror ("audio_run: gettimeofday");
1563 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1564 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1565 prevtime
= currtime
;
1570 static struct audio_option audio_options
[] = {
1573 .name
= "DAC_FIXED_SETTINGS",
1574 .tag
= AUD_OPT_BOOL
,
1575 .valp
= &conf
.fixed_out
.enabled
,
1576 .descr
= "Use fixed settings for host DAC"
1579 .name
= "DAC_FIXED_FREQ",
1581 .valp
= &conf
.fixed_out
.settings
.freq
,
1582 .descr
= "Frequency for fixed host DAC"
1585 .name
= "DAC_FIXED_FMT",
1587 .valp
= &conf
.fixed_out
.settings
.fmt
,
1588 .descr
= "Format for fixed host DAC"
1591 .name
= "DAC_FIXED_CHANNELS",
1593 .valp
= &conf
.fixed_out
.settings
.nchannels
,
1594 .descr
= "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1597 .name
= "DAC_VOICES",
1599 .valp
= &conf
.fixed_out
.nb_voices
,
1600 .descr
= "Number of voices for DAC"
1603 .name
= "DAC_TRY_POLL",
1604 .tag
= AUD_OPT_BOOL
,
1605 .valp
= &conf
.try_poll_out
,
1606 .descr
= "Attempt using poll mode for DAC"
1610 .name
= "ADC_FIXED_SETTINGS",
1611 .tag
= AUD_OPT_BOOL
,
1612 .valp
= &conf
.fixed_in
.enabled
,
1613 .descr
= "Use fixed settings for host ADC"
1616 .name
= "ADC_FIXED_FREQ",
1618 .valp
= &conf
.fixed_in
.settings
.freq
,
1619 .descr
= "Frequency for fixed host ADC"
1622 .name
= "ADC_FIXED_FMT",
1624 .valp
= &conf
.fixed_in
.settings
.fmt
,
1625 .descr
= "Format for fixed host ADC"
1628 .name
= "ADC_FIXED_CHANNELS",
1630 .valp
= &conf
.fixed_in
.settings
.nchannels
,
1631 .descr
= "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1634 .name
= "ADC_VOICES",
1636 .valp
= &conf
.fixed_in
.nb_voices
,
1637 .descr
= "Number of voices for ADC"
1640 .name
= "ADC_TRY_POLL",
1641 .tag
= AUD_OPT_BOOL
,
1642 .valp
= &conf
.try_poll_in
,
1643 .descr
= "Attempt using poll mode for ADC"
1647 .name
= "TIMER_PERIOD",
1649 .valp
= &conf
.period
.hertz
,
1650 .descr
= "Timer period in HZ (0 - use lowest possible)"
1652 { /* End of list */ }
1655 static void audio_pp_nb_voices (const char *typ
, int nb
)
1659 printf ("Does not support %s\n", typ
);
1662 printf ("One %s voice\n", typ
);
1665 printf ("Theoretically supports many %s voices\n", typ
);
1668 printf ("Theoretically supports up to %d %s voices\n", nb
, typ
);
1674 void AUD_help (void)
1676 struct audio_driver
*d
;
1678 audio_process_options ("AUDIO", audio_options
);
1679 QLIST_FOREACH(d
, &audio_drivers
, next
) {
1681 audio_process_options (d
->name
, d
->options
);
1685 printf ("Audio options:\n");
1686 audio_print_options ("AUDIO", audio_options
);
1689 printf ("Available drivers:\n");
1691 QLIST_FOREACH(d
, &audio_drivers
, next
) {
1693 printf ("Name: %s\n", d
->name
);
1694 printf ("Description: %s\n", d
->descr
);
1696 audio_pp_nb_voices ("playback", d
->max_voices_out
);
1697 audio_pp_nb_voices ("capture", d
->max_voices_in
);
1700 printf ("Options:\n");
1701 audio_print_options (d
->name
, d
->options
);
1704 printf ("No options\n");
1710 "Options are settable through environment variables.\n"
1713 " set QEMU_AUDIO_DRV=wav\n"
1714 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1716 " export QEMU_AUDIO_DRV=wav\n"
1717 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1718 "(for csh replace export with setenv in the above)\n"
1724 static int audio_driver_init (AudioState
*s
, struct audio_driver
*drv
)
1727 audio_process_options (drv
->name
, drv
->options
);
1729 s
->drv_opaque
= drv
->init ();
1731 if (s
->drv_opaque
) {
1732 audio_init_nb_voices_out (drv
);
1733 audio_init_nb_voices_in (drv
);
1738 dolog ("Could not init `%s' audio driver\n", drv
->name
);
1743 static void audio_vm_change_state_handler (void *opaque
, int running
,
1746 AudioState
*s
= opaque
;
1747 HWVoiceOut
*hwo
= NULL
;
1748 HWVoiceIn
*hwi
= NULL
;
1749 int op
= running
? VOICE_ENABLE
: VOICE_DISABLE
;
1751 s
->vm_running
= running
;
1752 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
1753 hwo
->pcm_ops
->ctl_out (hwo
, op
, conf
.try_poll_out
);
1756 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1757 hwi
->pcm_ops
->ctl_in (hwi
, op
, conf
.try_poll_in
);
1759 audio_reset_timer (s
);
1762 static bool is_cleaning_up
;
1764 bool audio_is_cleaning_up(void)
1766 return is_cleaning_up
;
1769 void audio_cleanup(void)
1771 AudioState
*s
= &glob_audio_state
;
1772 HWVoiceOut
*hwo
, *hwon
;
1773 HWVoiceIn
*hwi
, *hwin
;
1775 is_cleaning_up
= true;
1776 QLIST_FOREACH_SAFE(hwo
, &glob_audio_state
.hw_head_out
, entries
, hwon
) {
1780 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1782 hwo
->pcm_ops
->fini_out (hwo
);
1784 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1785 CaptureVoiceOut
*cap
= sc
->cap
;
1786 struct capture_callback
*cb
;
1788 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1789 cb
->ops
.destroy (cb
->opaque
);
1792 QLIST_REMOVE(hwo
, entries
);
1795 QLIST_FOREACH_SAFE(hwi
, &glob_audio_state
.hw_head_in
, entries
, hwin
) {
1797 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1799 hwi
->pcm_ops
->fini_in (hwi
);
1800 QLIST_REMOVE(hwi
, entries
);
1804 s
->drv
->fini (s
->drv_opaque
);
1809 static const VMStateDescription vmstate_audio
= {
1812 .minimum_version_id
= 1,
1813 .fields
= (VMStateField
[]) {
1814 VMSTATE_END_OF_LIST()
1818 static void audio_init (void)
1822 const char *drvname
;
1823 VMChangeStateEntry
*e
;
1824 AudioState
*s
= &glob_audio_state
;
1825 struct audio_driver
*driver
;
1831 QLIST_INIT (&s
->hw_head_out
);
1832 QLIST_INIT (&s
->hw_head_in
);
1833 QLIST_INIT (&s
->cap_head
);
1834 atexit(audio_cleanup
);
1836 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1838 audio_process_options ("AUDIO", audio_options
);
1840 s
->nb_hw_voices_out
= conf
.fixed_out
.nb_voices
;
1841 s
->nb_hw_voices_in
= conf
.fixed_in
.nb_voices
;
1843 if (s
->nb_hw_voices_out
<= 0) {
1844 dolog ("Bogus number of playback voices %d, setting to 1\n",
1845 s
->nb_hw_voices_out
);
1846 s
->nb_hw_voices_out
= 1;
1849 if (s
->nb_hw_voices_in
<= 0) {
1850 dolog ("Bogus number of capture voices %d, setting to 0\n",
1851 s
->nb_hw_voices_in
);
1852 s
->nb_hw_voices_in
= 0;
1857 drvname
= audio_get_conf_str ("QEMU_AUDIO_DRV", NULL
, &def
);
1861 driver
= audio_driver_lookup(drvname
);
1863 done
= !audio_driver_init(s
, driver
);
1865 dolog ("Unknown audio driver `%s'\n", drvname
);
1866 dolog ("Run with -audio-help to list available drivers\n");
1871 for (i
= 0; !done
&& i
< ARRAY_SIZE(audio_prio_list
); i
++) {
1872 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1873 if (driver
&& driver
->can_be_default
) {
1874 done
= !audio_driver_init(s
, driver
);
1880 driver
= audio_driver_lookup("none");
1881 done
= !audio_driver_init(s
, driver
);
1883 dolog("warning: Using timer based audio emulation\n");
1886 if (conf
.period
.hertz
<= 0) {
1887 if (conf
.period
.hertz
< 0) {
1888 dolog ("warning: Timer period is negative - %d "
1889 "treating as zero\n",
1892 conf
.period
.ticks
= 1;
1894 conf
.period
.ticks
= NANOSECONDS_PER_SECOND
/ conf
.period
.hertz
;
1897 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1899 dolog ("warning: Could not register change state handler\n"
1900 "(Audio can continue looping even after stopping the VM)\n");
1903 QLIST_INIT (&s
->card_head
);
1904 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1907 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1910 card
->name
= g_strdup (name
);
1911 memset (&card
->entries
, 0, sizeof (card
->entries
));
1912 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1915 void AUD_remove_card (QEMUSoundCard
*card
)
1917 QLIST_REMOVE (card
, entries
);
1918 g_free (card
->name
);
1922 CaptureVoiceOut
*AUD_add_capture (
1923 struct audsettings
*as
,
1924 struct audio_capture_ops
*ops
,
1928 AudioState
*s
= &glob_audio_state
;
1929 CaptureVoiceOut
*cap
;
1930 struct capture_callback
*cb
;
1932 if (audio_validate_settings (as
)) {
1933 dolog ("Invalid settings were passed when trying to add capture\n");
1934 audio_print_settings (as
);
1938 cb
= audio_calloc(__func__
, 1, sizeof(*cb
));
1940 dolog ("Could not allocate capture callback information, size %zu\n",
1945 cb
->opaque
= cb_opaque
;
1947 cap
= audio_pcm_capture_find_specific (as
);
1949 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1954 CaptureVoiceOut
*cap
;
1956 cap
= audio_calloc(__func__
, 1, sizeof(*cap
));
1958 dolog ("Could not allocate capture voice, size %zu\n",
1964 QLIST_INIT (&hw
->sw_head
);
1965 QLIST_INIT (&cap
->cb_head
);
1967 /* XXX find a more elegant way */
1968 hw
->samples
= 4096 * 4;
1969 hw
->mix_buf
= audio_calloc(__func__
, hw
->samples
,
1970 sizeof(struct st_sample
));
1972 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1977 audio_pcm_init_info (&hw
->info
, as
);
1979 cap
->buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
1981 dolog ("Could not allocate capture buffer "
1982 "(%d samples, each %d bytes)\n",
1983 hw
->samples
, 1 << hw
->info
.shift
);
1987 hw
->clip
= mixeng_clip
1988 [hw
->info
.nchannels
== 2]
1990 [hw
->info
.swap_endianness
]
1991 [audio_bits_to_index (hw
->info
.bits
)];
1993 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1994 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1996 QLIST_FOREACH(hw
, &glob_audio_state
.hw_head_out
, entries
) {
1997 audio_attach_capture (hw
);
2002 g_free (cap
->hw
.mix_buf
);
2012 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
2014 struct capture_callback
*cb
;
2016 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
2017 if (cb
->opaque
== cb_opaque
) {
2018 cb
->ops
.destroy (cb_opaque
);
2019 QLIST_REMOVE (cb
, entries
);
2022 if (!cap
->cb_head
.lh_first
) {
2023 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
2026 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
2027 #ifdef DEBUG_CAPTURE
2028 dolog ("freeing %s\n", sw
->name
);
2031 sw1
= sw
->entries
.le_next
;
2033 st_rate_stop (sw
->rate
);
2036 QLIST_REMOVE (sw
, entries
);
2037 QLIST_REMOVE (sc
, entries
);
2041 QLIST_REMOVE (cap
, entries
);
2042 g_free (cap
->hw
.mix_buf
);
2051 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2054 HWVoiceOut
*hw
= sw
->hw
;
2056 sw
->vol
.mute
= mute
;
2057 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2058 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2060 if (hw
->pcm_ops
->ctl_out
) {
2061 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
2066 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2069 HWVoiceIn
*hw
= sw
->hw
;
2071 sw
->vol
.mute
= mute
;
2072 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
2073 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
2075 if (hw
->pcm_ops
->ctl_in
) {
2076 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);