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 static const char *audio_audfmt_to_string (AudioFormat fmt
)
178 case AUDIO_FORMAT_U8
:
181 case AUDIO_FORMAT_U16
:
184 case AUDIO_FORMAT_S8
:
187 case AUDIO_FORMAT_S16
:
190 case AUDIO_FORMAT_U32
:
193 case AUDIO_FORMAT_S32
:
200 dolog ("Bogus audfmt %d returning S16\n", fmt
);
204 static AudioFormat
audio_string_to_audfmt (const char *s
, AudioFormat defval
,
207 if (!strcasecmp (s
, "u8")) {
209 return AUDIO_FORMAT_U8
;
211 else if (!strcasecmp (s
, "u16")) {
213 return AUDIO_FORMAT_U16
;
215 else if (!strcasecmp (s
, "u32")) {
217 return AUDIO_FORMAT_U32
;
219 else if (!strcasecmp (s
, "s8")) {
221 return AUDIO_FORMAT_S8
;
223 else if (!strcasecmp (s
, "s16")) {
225 return AUDIO_FORMAT_S16
;
227 else if (!strcasecmp (s
, "s32")) {
229 return AUDIO_FORMAT_S32
;
232 dolog ("Bogus audio format `%s' using %s\n",
233 s
, audio_audfmt_to_string (defval
));
239 static AudioFormat
audio_get_conf_fmt (const char *envname
,
243 const char *var
= getenv (envname
);
248 return audio_string_to_audfmt (var
, defval
, defaultp
);
251 static int audio_get_conf_int (const char *key
, int defval
, int *defaultp
)
256 strval
= getenv (key
);
257 if (strval
&& !qemu_strtoi(strval
, NULL
, 10, &val
)) {
267 static const char *audio_get_conf_str (const char *key
,
271 const char *val
= getenv (key
);
282 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
285 fprintf(stderr
, "%s: ", cap
);
288 vfprintf(stderr
, fmt
, ap
);
291 void AUD_log (const char *cap
, const char *fmt
, ...)
296 AUD_vlog (cap
, fmt
, ap
);
300 static void audio_process_options (const char *prefix
,
301 struct audio_option
*opt
)
305 if (audio_bug(__func__
, !prefix
)) {
306 dolog ("prefix = NULL\n");
310 if (audio_bug(__func__
, !opt
)) {
311 dolog ("opt = NULL\n");
315 prefix_upper
= g_utf8_strup(prefix
, -1);
317 for (; opt
->name
; opt
++) {
322 dolog ("Option value pointer for `%s' is not set\n",
327 optname
= g_strdup_printf("QEMU_%s_%s", prefix_upper
, opt
->name
);
334 int *intp
= opt
->valp
;
335 *intp
= audio_get_conf_int (optname
, *intp
, &def
);
341 AudioFormat
*fmtp
= opt
->valp
;
342 *fmtp
= audio_get_conf_fmt (optname
, *fmtp
, &def
);
348 const char **strp
= opt
->valp
;
349 *strp
= audio_get_conf_str (optname
, *strp
, &def
);
354 dolog ("Bad value tag for option `%s' - %d\n",
359 if (!opt
->overriddenp
) {
360 opt
->overriddenp
= &opt
->overridden
;
362 *opt
->overriddenp
= !def
;
365 g_free(prefix_upper
);
368 static void audio_print_settings (struct audsettings
*as
)
370 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
373 case AUDIO_FORMAT_S8
:
374 AUD_log (NULL
, "S8");
376 case AUDIO_FORMAT_U8
:
377 AUD_log (NULL
, "U8");
379 case AUDIO_FORMAT_S16
:
380 AUD_log (NULL
, "S16");
382 case AUDIO_FORMAT_U16
:
383 AUD_log (NULL
, "U16");
385 case AUDIO_FORMAT_S32
:
386 AUD_log (NULL
, "S32");
388 case AUDIO_FORMAT_U32
:
389 AUD_log (NULL
, "U32");
392 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
396 AUD_log (NULL
, " endianness=");
397 switch (as
->endianness
) {
399 AUD_log (NULL
, "little");
402 AUD_log (NULL
, "big");
405 AUD_log (NULL
, "invalid");
408 AUD_log (NULL
, "\n");
411 static int audio_validate_settings (struct audsettings
*as
)
415 invalid
= as
->nchannels
!= 1 && as
->nchannels
!= 2;
416 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
419 case AUDIO_FORMAT_S8
:
420 case AUDIO_FORMAT_U8
:
421 case AUDIO_FORMAT_S16
:
422 case AUDIO_FORMAT_U16
:
423 case AUDIO_FORMAT_S32
:
424 case AUDIO_FORMAT_U32
:
431 invalid
|= as
->freq
<= 0;
432 return invalid
? -1 : 0;
435 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
437 int bits
= 8, sign
= 0;
440 case AUDIO_FORMAT_S8
:
443 case AUDIO_FORMAT_U8
:
446 case AUDIO_FORMAT_S16
:
449 case AUDIO_FORMAT_U16
:
453 case AUDIO_FORMAT_S32
:
456 case AUDIO_FORMAT_U32
:
463 return info
->freq
== as
->freq
464 && info
->nchannels
== as
->nchannels
465 && info
->sign
== sign
466 && info
->bits
== bits
467 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
470 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
472 int bits
= 8, sign
= 0, shift
= 0;
475 case AUDIO_FORMAT_S8
:
477 case AUDIO_FORMAT_U8
:
480 case AUDIO_FORMAT_S16
:
482 case AUDIO_FORMAT_U16
:
487 case AUDIO_FORMAT_S32
:
489 case AUDIO_FORMAT_U32
:
498 info
->freq
= as
->freq
;
501 info
->nchannels
= as
->nchannels
;
502 info
->shift
= (as
->nchannels
== 2) + shift
;
503 info
->align
= (1 << info
->shift
) - 1;
504 info
->bytes_per_second
= info
->freq
<< info
->shift
;
505 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
508 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
515 memset (buf
, 0x00, len
<< info
->shift
);
518 switch (info
->bits
) {
520 memset (buf
, 0x80, len
<< info
->shift
);
527 int shift
= info
->nchannels
- 1;
530 if (info
->swap_endianness
) {
534 for (i
= 0; i
< len
<< shift
; i
++) {
544 int shift
= info
->nchannels
- 1;
545 int32_t s
= INT32_MAX
;
547 if (info
->swap_endianness
) {
551 for (i
= 0; i
< len
<< shift
; i
++) {
558 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
568 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
575 static CaptureVoiceOut
*audio_pcm_capture_find_specific (
576 struct audsettings
*as
579 CaptureVoiceOut
*cap
;
580 AudioState
*s
= &glob_audio_state
;
582 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
583 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
590 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
592 struct capture_callback
*cb
;
595 dolog ("notification %d sent\n", cmd
);
597 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
598 cb
->ops
.notify (cb
->opaque
, cmd
);
602 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
604 if (cap
->hw
.enabled
!= enabled
) {
605 audcnotification_e cmd
;
606 cap
->hw
.enabled
= enabled
;
607 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
608 audio_notify_capture (cap
, cmd
);
612 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
614 HWVoiceOut
*hw
= &cap
->hw
;
618 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
624 audio_capture_maybe_changed (cap
, enabled
);
627 static void audio_detach_capture (HWVoiceOut
*hw
)
629 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
632 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
633 SWVoiceOut
*sw
= &sc
->sw
;
634 CaptureVoiceOut
*cap
= sc
->cap
;
635 int was_active
= sw
->active
;
638 st_rate_stop (sw
->rate
);
642 QLIST_REMOVE (sw
, entries
);
643 QLIST_REMOVE (sc
, entries
);
646 /* We have removed soft voice from the capture:
647 this might have changed the overall status of the capture
648 since this might have been the only active voice */
649 audio_recalc_and_notify_capture (cap
);
655 static int audio_attach_capture (HWVoiceOut
*hw
)
657 AudioState
*s
= &glob_audio_state
;
658 CaptureVoiceOut
*cap
;
660 audio_detach_capture (hw
);
661 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
664 HWVoiceOut
*hw_cap
= &cap
->hw
;
666 sc
= g_malloc0(sizeof(*sc
));
673 sw
->active
= hw
->enabled
;
674 sw
->conv
= noop_conv
;
675 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
676 sw
->vol
= nominal_volume
;
677 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
679 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
683 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
684 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
686 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
687 hw
, sw
->info
.freq
, sw
->info
.bits
,
689 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
692 audio_capture_maybe_changed (cap
, 1);
699 * Hard voice (capture)
701 static int audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
704 int m
= hw
->total_samples_captured
;
706 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
708 m
= audio_MIN (m
, sw
->total_hw_samples_acquired
);
714 int audio_pcm_hw_get_live_in (HWVoiceIn
*hw
)
716 int live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
717 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
718 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
724 int audio_pcm_hw_clip_out (HWVoiceOut
*hw
, void *pcm_buf
,
725 int live
, int pending
)
727 int left
= hw
->samples
- pending
;
728 int len
= audio_MIN (left
, live
);
732 struct st_sample
*src
= hw
->mix_buf
+ hw
->rpos
;
733 uint8_t *dst
= advance (pcm_buf
, hw
->rpos
<< hw
->info
.shift
);
734 int samples_till_end_of_buf
= hw
->samples
- hw
->rpos
;
735 int samples_to_clip
= audio_MIN (len
, samples_till_end_of_buf
);
737 hw
->clip (dst
, src
, samples_to_clip
);
739 hw
->rpos
= (hw
->rpos
+ samples_to_clip
) % hw
->samples
;
740 len
-= samples_to_clip
;
741 clipped
+= samples_to_clip
;
747 * Soft voice (capture)
749 static int audio_pcm_sw_get_rpos_in (SWVoiceIn
*sw
)
751 HWVoiceIn
*hw
= sw
->hw
;
752 int live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
755 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
756 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
760 rpos
= hw
->wpos
- live
;
765 return hw
->samples
+ rpos
;
769 int audio_pcm_sw_read (SWVoiceIn
*sw
, void *buf
, int size
)
771 HWVoiceIn
*hw
= sw
->hw
;
772 int samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
773 struct st_sample
*src
, *dst
= sw
->buf
;
775 rpos
= audio_pcm_sw_get_rpos_in (sw
) % hw
->samples
;
777 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
778 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
779 dolog ("live_in=%d hw->samples=%d\n", live
, hw
->samples
);
783 samples
= size
>> sw
->info
.shift
;
788 swlim
= (live
* sw
->ratio
) >> 32;
789 swlim
= audio_MIN (swlim
, samples
);
792 src
= hw
->conv_buf
+ rpos
;
793 isamp
= hw
->wpos
- rpos
;
796 isamp
= hw
->samples
- rpos
;
804 if (audio_bug(__func__
, osamp
< 0)) {
805 dolog ("osamp=%d\n", osamp
);
809 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
811 rpos
= (rpos
+ isamp
) % hw
->samples
;
817 if (!(hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
818 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
821 sw
->clip (buf
, sw
->buf
, ret
);
822 sw
->total_hw_samples_acquired
+= total
;
823 return ret
<< sw
->info
.shift
;
827 * Hard voice (playback)
829 static int audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
835 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
836 if (sw
->active
|| !sw
->empty
) {
837 m
= audio_MIN (m
, sw
->total_hw_samples_mixed
);
846 static int audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
851 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
859 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
860 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
869 * Soft voice (playback)
871 int audio_pcm_sw_write (SWVoiceOut
*sw
, void *buf
, int size
)
873 int hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, swlim
, blck
;
874 int ret
= 0, pos
= 0, total
= 0;
880 hwsamples
= sw
->hw
->samples
;
882 live
= sw
->total_hw_samples_mixed
;
883 if (audio_bug(__func__
, live
< 0 || live
> hwsamples
)) {
884 dolog ("live=%d hw->samples=%d\n", live
, hwsamples
);
888 if (live
== hwsamples
) {
890 dolog ("%s is full %d\n", sw
->name
, live
);
895 wpos
= (sw
->hw
->rpos
+ live
) % hwsamples
;
896 samples
= size
>> sw
->info
.shift
;
898 dead
= hwsamples
- live
;
899 swlim
= ((int64_t) dead
<< 32) / sw
->ratio
;
900 swlim
= audio_MIN (swlim
, samples
);
902 sw
->conv (sw
->buf
, buf
, swlim
);
904 if (!(sw
->hw
->ctl_caps
& VOICE_VOLUME_CAP
)) {
905 mixeng_volume (sw
->buf
, swlim
, &sw
->vol
);
910 dead
= hwsamples
- live
;
911 left
= hwsamples
- wpos
;
912 blck
= audio_MIN (dead
, left
);
921 sw
->hw
->mix_buf
+ wpos
,
929 wpos
= (wpos
+ osamp
) % hwsamples
;
933 sw
->total_hw_samples_mixed
+= total
;
934 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
938 "%s: write size %d ret %d total sw %d\n",
940 size
>> sw
->info
.shift
,
942 sw
->total_hw_samples_mixed
946 return ret
<< sw
->info
.shift
;
950 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
952 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
953 cap
, info
->bits
, info
->sign
, info
->freq
, info
->nchannels
);
958 #include "audio_template.h"
960 #include "audio_template.h"
966 static bool audio_timer_running
;
967 static uint64_t audio_timer_last
;
969 static int audio_is_timer_needed (void)
971 HWVoiceIn
*hwi
= NULL
;
972 HWVoiceOut
*hwo
= NULL
;
974 while ((hwo
= audio_pcm_hw_find_any_enabled_out (hwo
))) {
975 if (!hwo
->poll_mode
) return 1;
977 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
978 if (!hwi
->poll_mode
) return 1;
983 static void audio_reset_timer (AudioState
*s
)
985 if (audio_is_timer_needed ()) {
986 timer_mod_anticipate_ns(s
->ts
,
987 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
988 if (!audio_timer_running
) {
989 audio_timer_running
= true;
990 audio_timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
991 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
995 if (audio_timer_running
) {
996 audio_timer_running
= false;
997 trace_audio_timer_stop();
1002 static void audio_timer (void *opaque
)
1005 AudioState
*s
= opaque
;
1007 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1008 diff
= now
- audio_timer_last
;
1009 if (diff
> s
->period_ticks
* 3 / 2) {
1010 trace_audio_timer_delayed(diff
/ SCALE_MS
);
1012 audio_timer_last
= now
;
1015 audio_reset_timer(s
);
1021 int AUD_write (SWVoiceOut
*sw
, void *buf
, int size
)
1024 /* XXX: Consider options */
1028 if (!sw
->hw
->enabled
) {
1029 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
1033 return sw
->hw
->pcm_ops
->write(sw
, buf
, size
);
1036 int AUD_read (SWVoiceIn
*sw
, void *buf
, int size
)
1039 /* XXX: Consider options */
1043 if (!sw
->hw
->enabled
) {
1044 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
1048 return sw
->hw
->pcm_ops
->read(sw
, buf
, size
);
1051 int AUD_get_buffer_size_out (SWVoiceOut
*sw
)
1053 return sw
->hw
->samples
<< sw
->hw
->info
.shift
;
1056 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
1065 if (sw
->active
!= on
) {
1066 AudioState
*s
= &glob_audio_state
;
1067 SWVoiceOut
*temp_sw
;
1071 hw
->pending_disable
= 0;
1074 if (s
->vm_running
) {
1075 hw
->pcm_ops
->ctl_out(hw
, VOICE_ENABLE
, true);
1076 audio_reset_timer (s
);
1084 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1085 temp_sw
= temp_sw
->entries
.le_next
) {
1086 nb_active
+= temp_sw
->active
!= 0;
1089 hw
->pending_disable
= nb_active
== 1;
1093 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1094 sc
->sw
.active
= hw
->enabled
;
1096 audio_capture_maybe_changed (sc
->cap
, 1);
1103 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
1112 if (sw
->active
!= on
) {
1113 AudioState
*s
= &glob_audio_state
;
1119 if (s
->vm_running
) {
1120 hw
->pcm_ops
->ctl_in(hw
, VOICE_ENABLE
, true);
1121 audio_reset_timer (s
);
1124 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
1130 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
1131 temp_sw
= temp_sw
->entries
.le_next
) {
1132 nb_active
+= temp_sw
->active
!= 0;
1135 if (nb_active
== 1) {
1137 hw
->pcm_ops
->ctl_in (hw
, VOICE_DISABLE
);
1145 static int audio_get_avail (SWVoiceIn
*sw
)
1153 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1154 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1155 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1160 "%s: get_avail live %d ret %" PRId64
"\n",
1162 live
, (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
1165 return (((int64_t) live
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1168 static int audio_get_free (SWVoiceOut
*sw
)
1176 live
= sw
->total_hw_samples_mixed
;
1178 if (audio_bug(__func__
, live
< 0 || live
> sw
->hw
->samples
)) {
1179 dolog ("live=%d sw->hw->samples=%d\n", live
, sw
->hw
->samples
);
1183 dead
= sw
->hw
->samples
- live
;
1186 dolog ("%s: get_free live %d dead %d ret %" PRId64
"\n",
1188 live
, dead
, (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
);
1191 return (((int64_t) dead
<< 32) / sw
->ratio
) << sw
->info
.shift
;
1194 static void audio_capture_mix_and_clear (HWVoiceOut
*hw
, int rpos
, int samples
)
1201 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1202 SWVoiceOut
*sw
= &sc
->sw
;
1207 int till_end_of_hw
= hw
->samples
- rpos2
;
1208 int to_write
= audio_MIN (till_end_of_hw
, n
);
1209 int bytes
= to_write
<< hw
->info
.shift
;
1212 sw
->buf
= hw
->mix_buf
+ rpos2
;
1213 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1214 if (written
- bytes
) {
1215 dolog ("Could not mix %d bytes into a capture "
1216 "buffer, mixed %d\n",
1221 rpos2
= (rpos2
+ to_write
) % hw
->samples
;
1226 n
= audio_MIN (samples
, hw
->samples
- rpos
);
1227 mixeng_clear (hw
->mix_buf
+ rpos
, n
);
1228 mixeng_clear (hw
->mix_buf
, samples
- n
);
1231 static void audio_run_out (AudioState
*s
)
1233 HWVoiceOut
*hw
= NULL
;
1236 while ((hw
= audio_pcm_hw_find_any_enabled_out (hw
))) {
1238 int live
, free
, nb_live
, cleanup_required
, prev_rpos
;
1240 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1245 if (audio_bug(__func__
, live
< 0 || live
> hw
->samples
)) {
1246 dolog ("live=%d hw->samples=%d\n", live
, hw
->samples
);
1250 if (hw
->pending_disable
&& !nb_live
) {
1253 dolog ("Disabling voice\n");
1256 hw
->pending_disable
= 0;
1257 hw
->pcm_ops
->ctl_out (hw
, VOICE_DISABLE
);
1258 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1260 audio_recalc_and_notify_capture (sc
->cap
);
1266 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1268 free
= audio_get_free (sw
);
1270 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1277 prev_rpos
= hw
->rpos
;
1278 played
= hw
->pcm_ops
->run_out (hw
, live
);
1279 replay_audio_out(&played
);
1280 if (audio_bug(__func__
, hw
->rpos
>= hw
->samples
)) {
1281 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1282 hw
->rpos
, hw
->samples
, played
);
1287 dolog ("played=%d\n", played
);
1291 hw
->ts_helper
+= played
;
1292 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1295 cleanup_required
= 0;
1296 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1297 if (!sw
->active
&& sw
->empty
) {
1301 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1302 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1303 played
, sw
->total_hw_samples_mixed
);
1304 played
= sw
->total_hw_samples_mixed
;
1307 sw
->total_hw_samples_mixed
-= played
;
1309 if (!sw
->total_hw_samples_mixed
) {
1311 cleanup_required
|= !sw
->active
&& !sw
->callback
.fn
;
1315 free
= audio_get_free (sw
);
1317 sw
->callback
.fn (sw
->callback
.opaque
, free
);
1322 if (cleanup_required
) {
1325 sw
= hw
->sw_head
.lh_first
;
1327 sw1
= sw
->entries
.le_next
;
1328 if (!sw
->active
&& !sw
->callback
.fn
) {
1329 audio_close_out (sw
);
1337 static void audio_run_in (AudioState
*s
)
1339 HWVoiceIn
*hw
= NULL
;
1341 while ((hw
= audio_pcm_hw_find_any_enabled_in (hw
))) {
1343 int captured
= 0, min
;
1345 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1346 captured
= hw
->pcm_ops
->run_in(hw
);
1348 replay_audio_in(&captured
, hw
->conv_buf
, &hw
->wpos
, hw
->samples
);
1350 min
= audio_pcm_hw_find_min_in (hw
);
1351 hw
->total_samples_captured
+= captured
- min
;
1352 hw
->ts_helper
+= captured
;
1354 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1355 sw
->total_hw_samples_acquired
-= min
;
1360 avail
= audio_get_avail (sw
);
1362 sw
->callback
.fn (sw
->callback
.opaque
, avail
);
1369 static void audio_run_capture (AudioState
*s
)
1371 CaptureVoiceOut
*cap
;
1373 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1374 int live
, rpos
, captured
;
1375 HWVoiceOut
*hw
= &cap
->hw
;
1378 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1381 int left
= hw
->samples
- rpos
;
1382 int to_capture
= audio_MIN (live
, left
);
1383 struct st_sample
*src
;
1384 struct capture_callback
*cb
;
1386 src
= hw
->mix_buf
+ rpos
;
1387 hw
->clip (cap
->buf
, src
, to_capture
);
1388 mixeng_clear (src
, to_capture
);
1390 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1391 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1392 to_capture
<< hw
->info
.shift
);
1394 rpos
= (rpos
+ to_capture
) % hw
->samples
;
1399 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1400 if (!sw
->active
&& sw
->empty
) {
1404 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1405 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1406 captured
, sw
->total_hw_samples_mixed
);
1407 captured
= sw
->total_hw_samples_mixed
;
1410 sw
->total_hw_samples_mixed
-= captured
;
1411 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1416 void audio_run (const char *msg
)
1418 AudioState
*s
= &glob_audio_state
;
1422 audio_run_capture (s
);
1425 static double prevtime
;
1429 if (gettimeofday (&tv
, NULL
)) {
1430 perror ("audio_run: gettimeofday");
1434 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1435 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1436 prevtime
= currtime
;
1441 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1442 bool msg
, Audiodev
*dev
)
1445 audio_process_options (drv
->name
, drv
->options
);
1447 s
->drv_opaque
= drv
->init(dev
);
1449 if (s
->drv_opaque
) {
1450 audio_init_nb_voices_out (drv
);
1451 audio_init_nb_voices_in (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 (hwo
))) {
1473 hwo
->pcm_ops
->ctl_out(hwo
, op
, true);
1476 while ((hwi
= audio_pcm_hw_find_any_enabled_in (hwi
))) {
1477 hwi
->pcm_ops
->ctl_in(hwi
, op
, true);
1479 audio_reset_timer (s
);
1482 static bool is_cleaning_up
;
1484 bool audio_is_cleaning_up(void)
1486 return is_cleaning_up
;
1489 void audio_cleanup(void)
1491 AudioState
*s
= &glob_audio_state
;
1492 HWVoiceOut
*hwo
, *hwon
;
1493 HWVoiceIn
*hwi
, *hwin
;
1495 is_cleaning_up
= true;
1496 QLIST_FOREACH_SAFE(hwo
, &glob_audio_state
.hw_head_out
, entries
, hwon
) {
1500 hwo
->pcm_ops
->ctl_out (hwo
, VOICE_DISABLE
);
1502 hwo
->pcm_ops
->fini_out (hwo
);
1504 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1505 CaptureVoiceOut
*cap
= sc
->cap
;
1506 struct capture_callback
*cb
;
1508 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1509 cb
->ops
.destroy (cb
->opaque
);
1512 QLIST_REMOVE(hwo
, entries
);
1515 QLIST_FOREACH_SAFE(hwi
, &glob_audio_state
.hw_head_in
, entries
, hwin
) {
1517 hwi
->pcm_ops
->ctl_in (hwi
, VOICE_DISABLE
);
1519 hwi
->pcm_ops
->fini_in (hwi
);
1520 QLIST_REMOVE(hwi
, entries
);
1524 s
->drv
->fini (s
->drv_opaque
);
1529 qapi_free_Audiodev(s
->dev
);
1534 static const VMStateDescription vmstate_audio
= {
1537 .minimum_version_id
= 1,
1538 .fields
= (VMStateField
[]) {
1539 VMSTATE_END_OF_LIST()
1543 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1545 static AudiodevListEntry
*audiodev_find(
1546 AudiodevListHead
*head
, const char *drvname
)
1548 AudiodevListEntry
*e
;
1549 QSIMPLEQ_FOREACH(e
, head
, next
) {
1550 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1558 static int audio_init(Audiodev
*dev
)
1562 const char *drvname
= NULL
;
1563 VMChangeStateEntry
*e
;
1564 AudioState
*s
= &glob_audio_state
;
1565 struct audio_driver
*driver
;
1566 /* silence gcc warning about uninitialized variable */
1567 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1571 dolog("Cannot create more than one audio backend, sorry\n");
1572 qapi_free_Audiodev(dev
);
1578 /* -audiodev option */
1579 drvname
= AudiodevDriver_str(dev
->driver
);
1581 /* legacy implicit initialization */
1582 head
= audio_handle_legacy_opts();
1584 * In case of legacy initialization, all Audiodevs in the list will have
1585 * the same configuration (except the driver), so it does't matter which
1586 * one we chose. We need an Audiodev to set up AudioState before we can
1587 * init a driver. Also note that dev at this point is still in the
1590 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1591 audio_validate_opts(dev
, &error_abort
);
1595 QLIST_INIT (&s
->hw_head_out
);
1596 QLIST_INIT (&s
->hw_head_in
);
1597 QLIST_INIT (&s
->cap_head
);
1598 atexit(audio_cleanup
);
1600 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1602 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1603 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1605 if (s
->nb_hw_voices_out
<= 0) {
1606 dolog ("Bogus number of playback voices %d, setting to 1\n",
1607 s
->nb_hw_voices_out
);
1608 s
->nb_hw_voices_out
= 1;
1611 if (s
->nb_hw_voices_in
<= 0) {
1612 dolog ("Bogus number of capture voices %d, setting to 0\n",
1613 s
->nb_hw_voices_in
);
1614 s
->nb_hw_voices_in
= 0;
1618 driver
= audio_driver_lookup(drvname
);
1620 done
= !audio_driver_init(s
, driver
, true, dev
);
1622 dolog ("Unknown audio driver `%s'\n", drvname
);
1625 for (i
= 0; audio_prio_list
[i
]; i
++) {
1626 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1627 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1630 s
->dev
= dev
= e
->dev
;
1631 audio_validate_opts(dev
, &error_abort
);
1632 done
= !audio_driver_init(s
, driver
, false, dev
);
1640 audio_free_audiodev_list(&head
);
1643 driver
= audio_driver_lookup("none");
1644 done
= !audio_driver_init(s
, driver
, false, dev
);
1646 dolog("warning: Using timer based audio emulation\n");
1649 if (dev
->timer_period
<= 0) {
1650 s
->period_ticks
= 1;
1652 s
->period_ticks
= NANOSECONDS_PER_SECOND
/ dev
->timer_period
;
1655 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1657 dolog ("warning: Could not register change state handler\n"
1658 "(Audio can continue looping even after stopping the VM)\n");
1661 QLIST_INIT (&s
->card_head
);
1662 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1666 void audio_free_audiodev_list(AudiodevListHead
*head
)
1668 AudiodevListEntry
*e
;
1669 while ((e
= QSIMPLEQ_FIRST(head
))) {
1670 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1671 qapi_free_Audiodev(e
->dev
);
1676 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1679 card
->name
= g_strdup (name
);
1680 memset (&card
->entries
, 0, sizeof (card
->entries
));
1681 QLIST_INSERT_HEAD (&glob_audio_state
.card_head
, card
, entries
);
1684 void AUD_remove_card (QEMUSoundCard
*card
)
1686 QLIST_REMOVE (card
, entries
);
1687 g_free (card
->name
);
1691 CaptureVoiceOut
*AUD_add_capture (
1692 struct audsettings
*as
,
1693 struct audio_capture_ops
*ops
,
1697 AudioState
*s
= &glob_audio_state
;
1698 CaptureVoiceOut
*cap
;
1699 struct capture_callback
*cb
;
1701 if (audio_validate_settings (as
)) {
1702 dolog ("Invalid settings were passed when trying to add capture\n");
1703 audio_print_settings (as
);
1707 cb
= g_malloc0(sizeof(*cb
));
1709 cb
->opaque
= cb_opaque
;
1711 cap
= audio_pcm_capture_find_specific (as
);
1713 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1718 CaptureVoiceOut
*cap
;
1720 cap
= g_malloc0(sizeof(*cap
));
1723 QLIST_INIT (&hw
->sw_head
);
1724 QLIST_INIT (&cap
->cb_head
);
1726 /* XXX find a more elegant way */
1727 hw
->samples
= 4096 * 4;
1728 hw
->mix_buf
= g_new0(struct st_sample
, hw
->samples
);
1730 audio_pcm_init_info (&hw
->info
, as
);
1732 cap
->buf
= g_malloc0_n(hw
->samples
, 1 << hw
->info
.shift
);
1734 hw
->clip
= mixeng_clip
1735 [hw
->info
.nchannels
== 2]
1737 [hw
->info
.swap_endianness
]
1738 [audio_bits_to_index (hw
->info
.bits
)];
1740 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1741 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1743 QLIST_FOREACH(hw
, &glob_audio_state
.hw_head_out
, entries
) {
1744 audio_attach_capture (hw
);
1750 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1752 struct capture_callback
*cb
;
1754 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1755 if (cb
->opaque
== cb_opaque
) {
1756 cb
->ops
.destroy (cb_opaque
);
1757 QLIST_REMOVE (cb
, entries
);
1760 if (!cap
->cb_head
.lh_first
) {
1761 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1764 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1765 #ifdef DEBUG_CAPTURE
1766 dolog ("freeing %s\n", sw
->name
);
1769 sw1
= sw
->entries
.le_next
;
1771 st_rate_stop (sw
->rate
);
1774 QLIST_REMOVE (sw
, entries
);
1775 QLIST_REMOVE (sc
, entries
);
1779 QLIST_REMOVE (cap
, entries
);
1780 g_free (cap
->hw
.mix_buf
);
1789 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1792 HWVoiceOut
*hw
= sw
->hw
;
1794 sw
->vol
.mute
= mute
;
1795 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1796 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1798 if (hw
->pcm_ops
->ctl_out
) {
1799 hw
->pcm_ops
->ctl_out (hw
, VOICE_VOLUME
, sw
);
1804 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1807 HWVoiceIn
*hw
= sw
->hw
;
1809 sw
->vol
.mute
= mute
;
1810 sw
->vol
.l
= nominal_volume
.l
* lvol
/ 255;
1811 sw
->vol
.r
= nominal_volume
.r
* rvol
/ 255;
1813 if (hw
->pcm_ops
->ctl_in
) {
1814 hw
->pcm_ops
->ctl_in (hw
, VOICE_VOLUME
, sw
);
1819 void audio_create_pdos(Audiodev
*dev
)
1821 switch (dev
->driver
) {
1822 #define CASE(DRIVER, driver, pdo_name) \
1823 case AUDIODEV_DRIVER_##DRIVER: \
1824 if (!dev->u.driver.has_in) { \
1825 dev->u.driver.in = g_malloc0( \
1826 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1827 dev->u.driver.has_in = true; \
1829 if (!dev->u.driver.has_out) { \
1830 dev->u.driver.out = g_malloc0( \
1831 sizeof(AudiodevAlsaPerDirectionOptions)); \
1832 dev->u.driver.has_out = true; \
1837 CASE(ALSA
, alsa
, Alsa
);
1838 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
1839 CASE(DSOUND
, dsound
, );
1840 CASE(OSS
, oss
, Oss
);
1843 CASE(SPICE
, spice
, );
1846 case AUDIODEV_DRIVER__MAX
:
1851 static void audio_validate_per_direction_opts(
1852 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
1854 if (!pdo
->has_fixed_settings
) {
1855 pdo
->has_fixed_settings
= true;
1856 pdo
->fixed_settings
= true;
1858 if (!pdo
->fixed_settings
&&
1859 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
1861 "You can't use frequency, channels or format with fixed-settings=off");
1865 if (!pdo
->has_frequency
) {
1866 pdo
->has_frequency
= true;
1867 pdo
->frequency
= 44100;
1869 if (!pdo
->has_channels
) {
1870 pdo
->has_channels
= true;
1873 if (!pdo
->has_voices
) {
1874 pdo
->has_voices
= true;
1877 if (!pdo
->has_format
) {
1878 pdo
->has_format
= true;
1879 pdo
->format
= AUDIO_FORMAT_S16
;
1883 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
1887 audio_create_pdos(dev
);
1889 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
1891 error_propagate(errp
, err
);
1895 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
1897 error_propagate(errp
, err
);
1901 if (!dev
->has_timer_period
) {
1902 dev
->has_timer_period
= true;
1903 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
1907 void audio_parse_option(const char *opt
)
1909 AudiodevListEntry
*e
;
1910 Audiodev
*dev
= NULL
;
1912 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
1913 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
1916 audio_validate_opts(dev
, &error_fatal
);
1918 e
= g_malloc0(sizeof(AudiodevListEntry
));
1920 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
1923 void audio_init_audiodevs(void)
1925 AudiodevListEntry
*e
;
1927 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
1932 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
1934 return (audsettings
) {
1935 .freq
= pdo
->frequency
,
1936 .nchannels
= pdo
->channels
,
1938 .endianness
= AUDIO_HOST_ENDIANNESS
,
1942 int audioformat_bytes_per_sample(AudioFormat fmt
)
1945 case AUDIO_FORMAT_U8
:
1946 case AUDIO_FORMAT_S8
:
1949 case AUDIO_FORMAT_U16
:
1950 case AUDIO_FORMAT_S16
:
1953 case AUDIO_FORMAT_U32
:
1954 case AUDIO_FORMAT_S32
:
1957 case AUDIO_FORMAT__MAX
:
1964 /* frames = freq * usec / 1e6 */
1965 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
1966 audsettings
*as
, int def_usecs
)
1968 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
1969 return (as
->freq
* usecs
+ 500000) / 1000000;
1972 /* samples = channels * frames = channels * freq * usec / 1e6 */
1973 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
1974 audsettings
*as
, int def_usecs
)
1976 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
1980 * bytes = bytes_per_sample * samples =
1981 * bytes_per_sample * channels * freq * usec / 1e6
1983 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
1984 audsettings
*as
, int def_usecs
)
1986 return audio_buffer_samples(pdo
, as
, def_usecs
) *
1987 audioformat_bytes_per_sample(as
->fmt
);