Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / audio / audio.c
blob50bad5ebcf4fe7b9d3731fa46c2d94f1fd6bc8f2
1 /*
2 * QEMU Audio subsystem
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "hw/hw.h"
27 #include "audio.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 "sysemu/sysemu.h"
34 #include "qemu/cutils.h"
35 #include "qemu/module.h"
36 #include "sysemu/replay.h"
37 #include "trace.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[] = {
55 "spice",
56 CONFIG_AUDIO_DRIVERS
57 "none",
58 "wav",
59 NULL
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) {
76 return d;
80 audio_module_load_one(name);
81 QLIST_FOREACH(d, &audio_drivers, next) {
82 if (strcmp(name, d->name) == 0) {
83 return d;
87 return NULL;
90 static AudioState glob_audio_state;
92 const struct mixeng_volume nominal_volume = {
93 .mute = 0,
94 #ifdef FLOAT_MIXENG
95 .r = 1.0,
96 .l = 1.0,
97 #else
98 .r = 1ULL << 32,
99 .l = 1ULL << 32,
100 #endif
103 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
104 #error No its not
105 #else
106 int audio_bug (const char *funcname, int cond)
108 if (cond) {
109 static int shown;
111 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
112 if (!shown) {
113 shown = 1;
114 AUD_log (NULL, "Save all your work and restart without audio\n");
115 AUD_log (NULL, "I am sorry\n");
117 AUD_log (NULL, "Context:\n");
119 #if defined AUDIO_BREAKPOINT_ON_BUG
120 # if defined HOST_I386
121 # if defined __GNUC__
122 __asm__ ("int3");
123 # elif defined _MSC_VER
124 _asm _emit 0xcc;
125 # else
126 abort ();
127 # endif
128 # else
129 abort ();
130 # endif
131 #endif
134 return cond;
136 #endif
138 static inline int audio_bits_to_index (int bits)
140 switch (bits) {
141 case 8:
142 return 0;
144 case 16:
145 return 1;
147 case 32:
148 return 2;
150 default:
151 audio_bug ("bits_to_index", 1);
152 AUD_log (NULL, "invalid bits %d\n", bits);
153 return 0;
157 void *audio_calloc (const char *funcname, int nmemb, size_t size)
159 int cond;
160 size_t len;
162 len = nmemb * size;
163 cond = !nmemb || !size;
164 cond |= nmemb < 0;
165 cond |= len < size;
167 if (audio_bug ("audio_calloc", cond)) {
168 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
169 funcname);
170 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
171 return NULL;
174 return g_malloc0 (len);
177 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
179 if (cap) {
180 fprintf(stderr, "%s: ", cap);
183 vfprintf(stderr, fmt, ap);
186 void AUD_log (const char *cap, const char *fmt, ...)
188 va_list ap;
190 va_start (ap, fmt);
191 AUD_vlog (cap, fmt, ap);
192 va_end (ap);
195 static void audio_print_settings (struct audsettings *as)
197 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
199 switch (as->fmt) {
200 case AUDIO_FORMAT_S8:
201 AUD_log (NULL, "S8");
202 break;
203 case AUDIO_FORMAT_U8:
204 AUD_log (NULL, "U8");
205 break;
206 case AUDIO_FORMAT_S16:
207 AUD_log (NULL, "S16");
208 break;
209 case AUDIO_FORMAT_U16:
210 AUD_log (NULL, "U16");
211 break;
212 case AUDIO_FORMAT_S32:
213 AUD_log (NULL, "S32");
214 break;
215 case AUDIO_FORMAT_U32:
216 AUD_log (NULL, "U32");
217 break;
218 default:
219 AUD_log (NULL, "invalid(%d)", as->fmt);
220 break;
223 AUD_log (NULL, " endianness=");
224 switch (as->endianness) {
225 case 0:
226 AUD_log (NULL, "little");
227 break;
228 case 1:
229 AUD_log (NULL, "big");
230 break;
231 default:
232 AUD_log (NULL, "invalid");
233 break;
235 AUD_log (NULL, "\n");
238 static int audio_validate_settings (struct audsettings *as)
240 int invalid;
242 invalid = as->nchannels != 1 && as->nchannels != 2;
243 invalid |= as->endianness != 0 && as->endianness != 1;
245 switch (as->fmt) {
246 case AUDIO_FORMAT_S8:
247 case AUDIO_FORMAT_U8:
248 case AUDIO_FORMAT_S16:
249 case AUDIO_FORMAT_U16:
250 case AUDIO_FORMAT_S32:
251 case AUDIO_FORMAT_U32:
252 break;
253 default:
254 invalid = 1;
255 break;
258 invalid |= as->freq <= 0;
259 return invalid ? -1 : 0;
262 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
264 int bits = 8, sign = 0;
266 switch (as->fmt) {
267 case AUDIO_FORMAT_S8:
268 sign = 1;
269 /* fall through */
270 case AUDIO_FORMAT_U8:
271 break;
273 case AUDIO_FORMAT_S16:
274 sign = 1;
275 /* fall through */
276 case AUDIO_FORMAT_U16:
277 bits = 16;
278 break;
280 case AUDIO_FORMAT_S32:
281 sign = 1;
282 /* fall through */
283 case AUDIO_FORMAT_U32:
284 bits = 32;
285 break;
287 default:
288 abort();
290 return info->freq == as->freq
291 && info->nchannels == as->nchannels
292 && info->sign == sign
293 && info->bits == bits
294 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
297 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
299 int bits = 8, sign = 0, shift = 0;
301 switch (as->fmt) {
302 case AUDIO_FORMAT_S8:
303 sign = 1;
304 case AUDIO_FORMAT_U8:
305 break;
307 case AUDIO_FORMAT_S16:
308 sign = 1;
309 /* fall through */
310 case AUDIO_FORMAT_U16:
311 bits = 16;
312 shift = 1;
313 break;
315 case AUDIO_FORMAT_S32:
316 sign = 1;
317 /* fall through */
318 case AUDIO_FORMAT_U32:
319 bits = 32;
320 shift = 2;
321 break;
323 default:
324 abort();
327 info->freq = as->freq;
328 info->bits = bits;
329 info->sign = sign;
330 info->nchannels = as->nchannels;
331 info->shift = (as->nchannels == 2) + shift;
332 info->align = (1 << info->shift) - 1;
333 info->bytes_per_second = info->freq << info->shift;
334 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
337 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
339 if (!len) {
340 return;
343 if (info->sign) {
344 memset (buf, 0x00, len << info->shift);
346 else {
347 switch (info->bits) {
348 case 8:
349 memset (buf, 0x80, len << info->shift);
350 break;
352 case 16:
354 int i;
355 uint16_t *p = buf;
356 int shift = info->nchannels - 1;
357 short s = INT16_MAX;
359 if (info->swap_endianness) {
360 s = bswap16 (s);
363 for (i = 0; i < len << shift; i++) {
364 p[i] = s;
367 break;
369 case 32:
371 int i;
372 uint32_t *p = buf;
373 int shift = info->nchannels - 1;
374 int32_t s = INT32_MAX;
376 if (info->swap_endianness) {
377 s = bswap32 (s);
380 for (i = 0; i < len << shift; i++) {
381 p[i] = s;
384 break;
386 default:
387 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
388 info->bits);
389 break;
395 * Capture
397 static void noop_conv (struct st_sample *dst, const void *src, int samples)
399 (void) src;
400 (void) dst;
401 (void) samples;
404 static CaptureVoiceOut *audio_pcm_capture_find_specific (
405 struct audsettings *as
408 CaptureVoiceOut *cap;
409 AudioState *s = &glob_audio_state;
411 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
412 if (audio_pcm_info_eq (&cap->hw.info, as)) {
413 return cap;
416 return NULL;
419 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
421 struct capture_callback *cb;
423 #ifdef DEBUG_CAPTURE
424 dolog ("notification %d sent\n", cmd);
425 #endif
426 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
427 cb->ops.notify (cb->opaque, cmd);
431 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
433 if (cap->hw.enabled != enabled) {
434 audcnotification_e cmd;
435 cap->hw.enabled = enabled;
436 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
437 audio_notify_capture (cap, cmd);
441 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
443 HWVoiceOut *hw = &cap->hw;
444 SWVoiceOut *sw;
445 int enabled = 0;
447 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
448 if (sw->active) {
449 enabled = 1;
450 break;
453 audio_capture_maybe_changed (cap, enabled);
456 static void audio_detach_capture (HWVoiceOut *hw)
458 SWVoiceCap *sc = hw->cap_head.lh_first;
460 while (sc) {
461 SWVoiceCap *sc1 = sc->entries.le_next;
462 SWVoiceOut *sw = &sc->sw;
463 CaptureVoiceOut *cap = sc->cap;
464 int was_active = sw->active;
466 if (sw->rate) {
467 st_rate_stop (sw->rate);
468 sw->rate = NULL;
471 QLIST_REMOVE (sw, entries);
472 QLIST_REMOVE (sc, entries);
473 g_free (sc);
474 if (was_active) {
475 /* We have removed soft voice from the capture:
476 this might have changed the overall status of the capture
477 since this might have been the only active voice */
478 audio_recalc_and_notify_capture (cap);
480 sc = sc1;
484 static int audio_attach_capture (HWVoiceOut *hw)
486 AudioState *s = &glob_audio_state;
487 CaptureVoiceOut *cap;
489 audio_detach_capture (hw);
490 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
491 SWVoiceCap *sc;
492 SWVoiceOut *sw;
493 HWVoiceOut *hw_cap = &cap->hw;
495 sc = g_malloc0(sizeof(*sc));
497 sc->cap = cap;
498 sw = &sc->sw;
499 sw->hw = hw_cap;
500 sw->info = hw->info;
501 sw->empty = 1;
502 sw->active = hw->enabled;
503 sw->conv = noop_conv;
504 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
505 sw->vol = nominal_volume;
506 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
507 if (!sw->rate) {
508 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
509 g_free (sw);
510 return -1;
512 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
513 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
514 #ifdef DEBUG_CAPTURE
515 sw->name = g_strdup_printf ("for %p %d,%d,%d",
516 hw, sw->info.freq, sw->info.bits,
517 sw->info.nchannels);
518 dolog ("Added %s active = %d\n", sw->name, sw->active);
519 #endif
520 if (sw->active) {
521 audio_capture_maybe_changed (cap, 1);
524 return 0;
528 * Hard voice (capture)
530 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
532 SWVoiceIn *sw;
533 int m = hw->total_samples_captured;
535 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
536 if (sw->active) {
537 m = audio_MIN (m, sw->total_hw_samples_acquired);
540 return m;
543 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
545 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
546 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
547 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
548 return 0;
550 return live;
553 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
554 int live, int pending)
556 int left = hw->samples - pending;
557 int len = audio_MIN (left, live);
558 int clipped = 0;
560 while (len) {
561 struct st_sample *src = hw->mix_buf + hw->rpos;
562 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
563 int samples_till_end_of_buf = hw->samples - hw->rpos;
564 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
566 hw->clip (dst, src, samples_to_clip);
568 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
569 len -= samples_to_clip;
570 clipped += samples_to_clip;
572 return clipped;
576 * Soft voice (capture)
578 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
580 HWVoiceIn *hw = sw->hw;
581 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
582 int rpos;
584 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
585 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
586 return 0;
589 rpos = hw->wpos - live;
590 if (rpos >= 0) {
591 return rpos;
593 else {
594 return hw->samples + rpos;
598 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
600 HWVoiceIn *hw = sw->hw;
601 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
602 struct st_sample *src, *dst = sw->buf;
604 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
606 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
607 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
608 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
609 return 0;
612 samples = size >> sw->info.shift;
613 if (!live) {
614 return 0;
617 swlim = (live * sw->ratio) >> 32;
618 swlim = audio_MIN (swlim, samples);
620 while (swlim) {
621 src = hw->conv_buf + rpos;
622 isamp = hw->wpos - rpos;
623 /* XXX: <= ? */
624 if (isamp <= 0) {
625 isamp = hw->samples - rpos;
628 if (!isamp) {
629 break;
631 osamp = swlim;
633 if (audio_bug(__func__, osamp < 0)) {
634 dolog ("osamp=%d\n", osamp);
635 return 0;
638 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
639 swlim -= osamp;
640 rpos = (rpos + isamp) % hw->samples;
641 dst += osamp;
642 ret += osamp;
643 total += isamp;
646 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
647 mixeng_volume (sw->buf, ret, &sw->vol);
650 sw->clip (buf, sw->buf, ret);
651 sw->total_hw_samples_acquired += total;
652 return ret << sw->info.shift;
656 * Hard voice (playback)
658 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
660 SWVoiceOut *sw;
661 int m = INT_MAX;
662 int nb_live = 0;
664 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
665 if (sw->active || !sw->empty) {
666 m = audio_MIN (m, sw->total_hw_samples_mixed);
667 nb_live += 1;
671 *nb_livep = nb_live;
672 return m;
675 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
677 int smin;
678 int nb_live1;
680 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
681 if (nb_live) {
682 *nb_live = nb_live1;
685 if (nb_live1) {
686 int live = smin;
688 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
689 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
690 return 0;
692 return live;
694 return 0;
698 * Soft voice (playback)
700 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
702 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
703 int ret = 0, pos = 0, total = 0;
705 if (!sw) {
706 return size;
709 hwsamples = sw->hw->samples;
711 live = sw->total_hw_samples_mixed;
712 if (audio_bug(__func__, live < 0 || live > hwsamples)) {
713 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
714 return 0;
717 if (live == hwsamples) {
718 #ifdef DEBUG_OUT
719 dolog ("%s is full %d\n", sw->name, live);
720 #endif
721 return 0;
724 wpos = (sw->hw->rpos + live) % hwsamples;
725 samples = size >> sw->info.shift;
727 dead = hwsamples - live;
728 swlim = ((int64_t) dead << 32) / sw->ratio;
729 swlim = audio_MIN (swlim, samples);
730 if (swlim) {
731 sw->conv (sw->buf, buf, swlim);
733 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
734 mixeng_volume (sw->buf, swlim, &sw->vol);
738 while (swlim) {
739 dead = hwsamples - live;
740 left = hwsamples - wpos;
741 blck = audio_MIN (dead, left);
742 if (!blck) {
743 break;
745 isamp = swlim;
746 osamp = blck;
747 st_rate_flow_mix (
748 sw->rate,
749 sw->buf + pos,
750 sw->hw->mix_buf + wpos,
751 &isamp,
752 &osamp
754 ret += isamp;
755 swlim -= isamp;
756 pos += isamp;
757 live += osamp;
758 wpos = (wpos + osamp) % hwsamples;
759 total += osamp;
762 sw->total_hw_samples_mixed += total;
763 sw->empty = sw->total_hw_samples_mixed == 0;
765 #ifdef DEBUG_OUT
766 dolog (
767 "%s: write size %d ret %d total sw %d\n",
768 SW_NAME (sw),
769 size >> sw->info.shift,
770 ret,
771 sw->total_hw_samples_mixed
773 #endif
775 return ret << sw->info.shift;
778 #ifdef DEBUG_AUDIO
779 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
781 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
782 cap, info->bits, info->sign, info->freq, info->nchannels);
784 #endif
786 #define DAC
787 #include "audio_template.h"
788 #undef DAC
789 #include "audio_template.h"
792 * Timer
795 static bool audio_timer_running;
796 static uint64_t audio_timer_last;
798 static int audio_is_timer_needed (void)
800 HWVoiceIn *hwi = NULL;
801 HWVoiceOut *hwo = NULL;
803 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
804 if (!hwo->poll_mode) return 1;
806 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
807 if (!hwi->poll_mode) return 1;
809 return 0;
812 static void audio_reset_timer (AudioState *s)
814 if (audio_is_timer_needed ()) {
815 timer_mod_anticipate_ns(s->ts,
816 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
817 if (!audio_timer_running) {
818 audio_timer_running = true;
819 audio_timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
820 trace_audio_timer_start(s->period_ticks / SCALE_MS);
822 } else {
823 timer_del(s->ts);
824 if (audio_timer_running) {
825 audio_timer_running = false;
826 trace_audio_timer_stop();
831 static void audio_timer (void *opaque)
833 int64_t now, diff;
834 AudioState *s = opaque;
836 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
837 diff = now - audio_timer_last;
838 if (diff > s->period_ticks * 3 / 2) {
839 trace_audio_timer_delayed(diff / SCALE_MS);
841 audio_timer_last = now;
843 audio_run("timer");
844 audio_reset_timer(s);
848 * Public API
850 int AUD_write (SWVoiceOut *sw, void *buf, int size)
852 if (!sw) {
853 /* XXX: Consider options */
854 return size;
857 if (!sw->hw->enabled) {
858 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
859 return 0;
862 return sw->hw->pcm_ops->write(sw, buf, size);
865 int AUD_read (SWVoiceIn *sw, void *buf, int size)
867 if (!sw) {
868 /* XXX: Consider options */
869 return size;
872 if (!sw->hw->enabled) {
873 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
874 return 0;
877 return sw->hw->pcm_ops->read(sw, buf, size);
880 int AUD_get_buffer_size_out (SWVoiceOut *sw)
882 return sw->hw->samples << sw->hw->info.shift;
885 void AUD_set_active_out (SWVoiceOut *sw, int on)
887 HWVoiceOut *hw;
889 if (!sw) {
890 return;
893 hw = sw->hw;
894 if (sw->active != on) {
895 AudioState *s = &glob_audio_state;
896 SWVoiceOut *temp_sw;
897 SWVoiceCap *sc;
899 if (on) {
900 hw->pending_disable = 0;
901 if (!hw->enabled) {
902 hw->enabled = 1;
903 if (s->vm_running) {
904 hw->pcm_ops->ctl_out(hw, VOICE_ENABLE);
905 audio_reset_timer (s);
909 else {
910 if (hw->enabled) {
911 int nb_active = 0;
913 for (temp_sw = hw->sw_head.lh_first; temp_sw;
914 temp_sw = temp_sw->entries.le_next) {
915 nb_active += temp_sw->active != 0;
918 hw->pending_disable = nb_active == 1;
922 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
923 sc->sw.active = hw->enabled;
924 if (hw->enabled) {
925 audio_capture_maybe_changed (sc->cap, 1);
928 sw->active = on;
932 void AUD_set_active_in (SWVoiceIn *sw, int on)
934 HWVoiceIn *hw;
936 if (!sw) {
937 return;
940 hw = sw->hw;
941 if (sw->active != on) {
942 AudioState *s = &glob_audio_state;
943 SWVoiceIn *temp_sw;
945 if (on) {
946 if (!hw->enabled) {
947 hw->enabled = 1;
948 if (s->vm_running) {
949 hw->pcm_ops->ctl_in(hw, VOICE_ENABLE);
950 audio_reset_timer (s);
953 sw->total_hw_samples_acquired = hw->total_samples_captured;
955 else {
956 if (hw->enabled) {
957 int nb_active = 0;
959 for (temp_sw = hw->sw_head.lh_first; temp_sw;
960 temp_sw = temp_sw->entries.le_next) {
961 nb_active += temp_sw->active != 0;
964 if (nb_active == 1) {
965 hw->enabled = 0;
966 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
970 sw->active = on;
974 static int audio_get_avail (SWVoiceIn *sw)
976 int live;
978 if (!sw) {
979 return 0;
982 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
983 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
984 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
985 return 0;
988 ldebug (
989 "%s: get_avail live %d ret %" PRId64 "\n",
990 SW_NAME (sw),
991 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
994 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
997 static int audio_get_free (SWVoiceOut *sw)
999 int live, dead;
1001 if (!sw) {
1002 return 0;
1005 live = sw->total_hw_samples_mixed;
1007 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
1008 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1009 return 0;
1012 dead = sw->hw->samples - live;
1014 #ifdef DEBUG_OUT
1015 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1016 SW_NAME (sw),
1017 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1018 #endif
1020 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1023 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1025 int n;
1027 if (hw->enabled) {
1028 SWVoiceCap *sc;
1030 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1031 SWVoiceOut *sw = &sc->sw;
1032 int rpos2 = rpos;
1034 n = samples;
1035 while (n) {
1036 int till_end_of_hw = hw->samples - rpos2;
1037 int to_write = audio_MIN (till_end_of_hw, n);
1038 int bytes = to_write << hw->info.shift;
1039 int written;
1041 sw->buf = hw->mix_buf + rpos2;
1042 written = audio_pcm_sw_write (sw, NULL, bytes);
1043 if (written - bytes) {
1044 dolog ("Could not mix %d bytes into a capture "
1045 "buffer, mixed %d\n",
1046 bytes, written);
1047 break;
1049 n -= to_write;
1050 rpos2 = (rpos2 + to_write) % hw->samples;
1055 n = audio_MIN (samples, hw->samples - rpos);
1056 mixeng_clear (hw->mix_buf + rpos, n);
1057 mixeng_clear (hw->mix_buf, samples - n);
1060 static void audio_run_out (AudioState *s)
1062 HWVoiceOut *hw = NULL;
1063 SWVoiceOut *sw;
1065 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1066 int played;
1067 int live, free, nb_live, cleanup_required, prev_rpos;
1069 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1070 if (!nb_live) {
1071 live = 0;
1074 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
1075 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1076 continue;
1079 if (hw->pending_disable && !nb_live) {
1080 SWVoiceCap *sc;
1081 #ifdef DEBUG_OUT
1082 dolog ("Disabling voice\n");
1083 #endif
1084 hw->enabled = 0;
1085 hw->pending_disable = 0;
1086 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1087 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1088 sc->sw.active = 0;
1089 audio_recalc_and_notify_capture (sc->cap);
1091 continue;
1094 if (!live) {
1095 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1096 if (sw->active) {
1097 free = audio_get_free (sw);
1098 if (free > 0) {
1099 sw->callback.fn (sw->callback.opaque, free);
1103 continue;
1106 prev_rpos = hw->rpos;
1107 played = hw->pcm_ops->run_out (hw, live);
1108 replay_audio_out(&played);
1109 if (audio_bug(__func__, hw->rpos >= hw->samples)) {
1110 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1111 hw->rpos, hw->samples, played);
1112 hw->rpos = 0;
1115 #ifdef DEBUG_OUT
1116 dolog ("played=%d\n", played);
1117 #endif
1119 if (played) {
1120 hw->ts_helper += played;
1121 audio_capture_mix_and_clear (hw, prev_rpos, played);
1124 cleanup_required = 0;
1125 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1126 if (!sw->active && sw->empty) {
1127 continue;
1130 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
1131 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1132 played, sw->total_hw_samples_mixed);
1133 played = sw->total_hw_samples_mixed;
1136 sw->total_hw_samples_mixed -= played;
1138 if (!sw->total_hw_samples_mixed) {
1139 sw->empty = 1;
1140 cleanup_required |= !sw->active && !sw->callback.fn;
1143 if (sw->active) {
1144 free = audio_get_free (sw);
1145 if (free > 0) {
1146 sw->callback.fn (sw->callback.opaque, free);
1151 if (cleanup_required) {
1152 SWVoiceOut *sw1;
1154 sw = hw->sw_head.lh_first;
1155 while (sw) {
1156 sw1 = sw->entries.le_next;
1157 if (!sw->active && !sw->callback.fn) {
1158 audio_close_out (sw);
1160 sw = sw1;
1166 static void audio_run_in (AudioState *s)
1168 HWVoiceIn *hw = NULL;
1170 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1171 SWVoiceIn *sw;
1172 int captured = 0, min;
1174 if (replay_mode != REPLAY_MODE_PLAY) {
1175 captured = hw->pcm_ops->run_in(hw);
1177 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
1179 min = audio_pcm_hw_find_min_in (hw);
1180 hw->total_samples_captured += captured - min;
1181 hw->ts_helper += captured;
1183 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1184 sw->total_hw_samples_acquired -= min;
1186 if (sw->active) {
1187 int avail;
1189 avail = audio_get_avail (sw);
1190 if (avail > 0) {
1191 sw->callback.fn (sw->callback.opaque, avail);
1198 static void audio_run_capture (AudioState *s)
1200 CaptureVoiceOut *cap;
1202 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1203 int live, rpos, captured;
1204 HWVoiceOut *hw = &cap->hw;
1205 SWVoiceOut *sw;
1207 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1208 rpos = hw->rpos;
1209 while (live) {
1210 int left = hw->samples - rpos;
1211 int to_capture = audio_MIN (live, left);
1212 struct st_sample *src;
1213 struct capture_callback *cb;
1215 src = hw->mix_buf + rpos;
1216 hw->clip (cap->buf, src, to_capture);
1217 mixeng_clear (src, to_capture);
1219 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1220 cb->ops.capture (cb->opaque, cap->buf,
1221 to_capture << hw->info.shift);
1223 rpos = (rpos + to_capture) % hw->samples;
1224 live -= to_capture;
1226 hw->rpos = rpos;
1228 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1229 if (!sw->active && sw->empty) {
1230 continue;
1233 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
1234 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1235 captured, sw->total_hw_samples_mixed);
1236 captured = sw->total_hw_samples_mixed;
1239 sw->total_hw_samples_mixed -= captured;
1240 sw->empty = sw->total_hw_samples_mixed == 0;
1245 void audio_run (const char *msg)
1247 AudioState *s = &glob_audio_state;
1249 audio_run_out (s);
1250 audio_run_in (s);
1251 audio_run_capture (s);
1252 #ifdef DEBUG_POLL
1254 static double prevtime;
1255 double currtime;
1256 struct timeval tv;
1258 if (gettimeofday (&tv, NULL)) {
1259 perror ("audio_run: gettimeofday");
1260 return;
1263 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1264 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1265 prevtime = currtime;
1267 #endif
1270 static int audio_driver_init(AudioState *s, struct audio_driver *drv,
1271 bool msg, Audiodev *dev)
1273 s->drv_opaque = drv->init(dev);
1275 if (s->drv_opaque) {
1276 audio_init_nb_voices_out (drv);
1277 audio_init_nb_voices_in (drv);
1278 s->drv = drv;
1279 return 0;
1281 else {
1282 if (msg) {
1283 dolog("Could not init `%s' audio driver\n", drv->name);
1285 return -1;
1289 static void audio_vm_change_state_handler (void *opaque, int running,
1290 RunState state)
1292 AudioState *s = opaque;
1293 HWVoiceOut *hwo = NULL;
1294 HWVoiceIn *hwi = NULL;
1295 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1297 s->vm_running = running;
1298 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1299 hwo->pcm_ops->ctl_out(hwo, op);
1302 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1303 hwi->pcm_ops->ctl_in(hwi, op);
1305 audio_reset_timer (s);
1308 static bool is_cleaning_up;
1310 bool audio_is_cleaning_up(void)
1312 return is_cleaning_up;
1315 void audio_cleanup(void)
1317 AudioState *s = &glob_audio_state;
1318 HWVoiceOut *hwo, *hwon;
1319 HWVoiceIn *hwi, *hwin;
1321 is_cleaning_up = true;
1322 QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
1323 SWVoiceCap *sc;
1325 if (hwo->enabled) {
1326 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1328 hwo->pcm_ops->fini_out (hwo);
1330 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1331 CaptureVoiceOut *cap = sc->cap;
1332 struct capture_callback *cb;
1334 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1335 cb->ops.destroy (cb->opaque);
1338 QLIST_REMOVE(hwo, entries);
1341 QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
1342 if (hwi->enabled) {
1343 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1345 hwi->pcm_ops->fini_in (hwi);
1346 QLIST_REMOVE(hwi, entries);
1349 if (s->drv) {
1350 s->drv->fini (s->drv_opaque);
1351 s->drv = NULL;
1354 if (s->dev) {
1355 qapi_free_Audiodev(s->dev);
1356 s->dev = NULL;
1360 static const VMStateDescription vmstate_audio = {
1361 .name = "audio",
1362 .version_id = 1,
1363 .minimum_version_id = 1,
1364 .fields = (VMStateField[]) {
1365 VMSTATE_END_OF_LIST()
1369 static void audio_validate_opts(Audiodev *dev, Error **errp);
1371 static AudiodevListEntry *audiodev_find(
1372 AudiodevListHead *head, const char *drvname)
1374 AudiodevListEntry *e;
1375 QSIMPLEQ_FOREACH(e, head, next) {
1376 if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) {
1377 return e;
1381 return NULL;
1384 static int audio_init(Audiodev *dev)
1386 size_t i;
1387 int done = 0;
1388 const char *drvname = NULL;
1389 VMChangeStateEntry *e;
1390 AudioState *s = &glob_audio_state;
1391 struct audio_driver *driver;
1392 /* silence gcc warning about uninitialized variable */
1393 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
1395 if (s->drv) {
1396 if (dev) {
1397 dolog("Cannot create more than one audio backend, sorry\n");
1398 qapi_free_Audiodev(dev);
1400 return -1;
1403 if (dev) {
1404 /* -audiodev option */
1405 drvname = AudiodevDriver_str(dev->driver);
1406 } else {
1407 /* legacy implicit initialization */
1408 head = audio_handle_legacy_opts();
1410 * In case of legacy initialization, all Audiodevs in the list will have
1411 * the same configuration (except the driver), so it does't matter which
1412 * one we chose. We need an Audiodev to set up AudioState before we can
1413 * init a driver. Also note that dev at this point is still in the
1414 * list.
1416 dev = QSIMPLEQ_FIRST(&head)->dev;
1417 audio_validate_opts(dev, &error_abort);
1419 s->dev = dev;
1421 QLIST_INIT (&s->hw_head_out);
1422 QLIST_INIT (&s->hw_head_in);
1423 QLIST_INIT (&s->cap_head);
1424 atexit(audio_cleanup);
1426 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1428 s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices;
1429 s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices;
1431 if (s->nb_hw_voices_out <= 0) {
1432 dolog ("Bogus number of playback voices %d, setting to 1\n",
1433 s->nb_hw_voices_out);
1434 s->nb_hw_voices_out = 1;
1437 if (s->nb_hw_voices_in <= 0) {
1438 dolog ("Bogus number of capture voices %d, setting to 0\n",
1439 s->nb_hw_voices_in);
1440 s->nb_hw_voices_in = 0;
1443 if (drvname) {
1444 driver = audio_driver_lookup(drvname);
1445 if (driver) {
1446 done = !audio_driver_init(s, driver, true, dev);
1447 } else {
1448 dolog ("Unknown audio driver `%s'\n", drvname);
1450 } else {
1451 for (i = 0; audio_prio_list[i]; i++) {
1452 AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
1453 driver = audio_driver_lookup(audio_prio_list[i]);
1455 if (e && driver) {
1456 s->dev = dev = e->dev;
1457 audio_validate_opts(dev, &error_abort);
1458 done = !audio_driver_init(s, driver, false, dev);
1459 if (done) {
1460 e->dev = NULL;
1461 break;
1466 audio_free_audiodev_list(&head);
1468 if (!done) {
1469 driver = audio_driver_lookup("none");
1470 done = !audio_driver_init(s, driver, false, dev);
1471 assert(done);
1472 dolog("warning: Using timer based audio emulation\n");
1475 if (dev->timer_period <= 0) {
1476 s->period_ticks = 1;
1477 } else {
1478 s->period_ticks = dev->timer_period * SCALE_US;
1481 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1482 if (!e) {
1483 dolog ("warning: Could not register change state handler\n"
1484 "(Audio can continue looping even after stopping the VM)\n");
1487 QLIST_INIT (&s->card_head);
1488 vmstate_register (NULL, 0, &vmstate_audio, s);
1489 return 0;
1492 void audio_free_audiodev_list(AudiodevListHead *head)
1494 AudiodevListEntry *e;
1495 while ((e = QSIMPLEQ_FIRST(head))) {
1496 QSIMPLEQ_REMOVE_HEAD(head, next);
1497 qapi_free_Audiodev(e->dev);
1498 g_free(e);
1502 void AUD_register_card (const char *name, QEMUSoundCard *card)
1504 audio_init(NULL);
1505 card->name = g_strdup (name);
1506 memset (&card->entries, 0, sizeof (card->entries));
1507 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1510 void AUD_remove_card (QEMUSoundCard *card)
1512 QLIST_REMOVE (card, entries);
1513 g_free (card->name);
1517 CaptureVoiceOut *AUD_add_capture (
1518 struct audsettings *as,
1519 struct audio_capture_ops *ops,
1520 void *cb_opaque
1523 AudioState *s = &glob_audio_state;
1524 CaptureVoiceOut *cap;
1525 struct capture_callback *cb;
1527 if (audio_validate_settings (as)) {
1528 dolog ("Invalid settings were passed when trying to add capture\n");
1529 audio_print_settings (as);
1530 return NULL;
1533 cb = g_malloc0(sizeof(*cb));
1534 cb->ops = *ops;
1535 cb->opaque = cb_opaque;
1537 cap = audio_pcm_capture_find_specific (as);
1538 if (cap) {
1539 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1540 return cap;
1542 else {
1543 HWVoiceOut *hw;
1544 CaptureVoiceOut *cap;
1546 cap = g_malloc0(sizeof(*cap));
1548 hw = &cap->hw;
1549 QLIST_INIT (&hw->sw_head);
1550 QLIST_INIT (&cap->cb_head);
1552 /* XXX find a more elegant way */
1553 hw->samples = 4096 * 4;
1554 hw->mix_buf = g_new0(struct st_sample, hw->samples);
1556 audio_pcm_init_info (&hw->info, as);
1558 cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift);
1560 hw->clip = mixeng_clip
1561 [hw->info.nchannels == 2]
1562 [hw->info.sign]
1563 [hw->info.swap_endianness]
1564 [audio_bits_to_index (hw->info.bits)];
1566 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1567 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1569 QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
1570 audio_attach_capture (hw);
1572 return cap;
1576 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1578 struct capture_callback *cb;
1580 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1581 if (cb->opaque == cb_opaque) {
1582 cb->ops.destroy (cb_opaque);
1583 QLIST_REMOVE (cb, entries);
1584 g_free (cb);
1586 if (!cap->cb_head.lh_first) {
1587 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1589 while (sw) {
1590 SWVoiceCap *sc = (SWVoiceCap *) sw;
1591 #ifdef DEBUG_CAPTURE
1592 dolog ("freeing %s\n", sw->name);
1593 #endif
1595 sw1 = sw->entries.le_next;
1596 if (sw->rate) {
1597 st_rate_stop (sw->rate);
1598 sw->rate = NULL;
1600 QLIST_REMOVE (sw, entries);
1601 QLIST_REMOVE (sc, entries);
1602 g_free (sc);
1603 sw = sw1;
1605 QLIST_REMOVE (cap, entries);
1606 g_free (cap->hw.mix_buf);
1607 g_free (cap->buf);
1608 g_free (cap);
1610 return;
1615 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1617 if (sw) {
1618 HWVoiceOut *hw = sw->hw;
1620 sw->vol.mute = mute;
1621 sw->vol.l = nominal_volume.l * lvol / 255;
1622 sw->vol.r = nominal_volume.r * rvol / 255;
1624 if (hw->pcm_ops->ctl_out) {
1625 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
1630 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1632 if (sw) {
1633 HWVoiceIn *hw = sw->hw;
1635 sw->vol.mute = mute;
1636 sw->vol.l = nominal_volume.l * lvol / 255;
1637 sw->vol.r = nominal_volume.r * rvol / 255;
1639 if (hw->pcm_ops->ctl_in) {
1640 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
1645 void audio_create_pdos(Audiodev *dev)
1647 switch (dev->driver) {
1648 #define CASE(DRIVER, driver, pdo_name) \
1649 case AUDIODEV_DRIVER_##DRIVER: \
1650 if (!dev->u.driver.has_in) { \
1651 dev->u.driver.in = g_malloc0( \
1652 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1653 dev->u.driver.has_in = true; \
1655 if (!dev->u.driver.has_out) { \
1656 dev->u.driver.out = g_malloc0( \
1657 sizeof(AudiodevAlsaPerDirectionOptions)); \
1658 dev->u.driver.has_out = true; \
1660 break
1662 CASE(NONE, none, );
1663 CASE(ALSA, alsa, Alsa);
1664 CASE(COREAUDIO, coreaudio, Coreaudio);
1665 CASE(DSOUND, dsound, );
1666 CASE(OSS, oss, Oss);
1667 CASE(PA, pa, Pa);
1668 CASE(SDL, sdl, );
1669 CASE(SPICE, spice, );
1670 CASE(WAV, wav, );
1672 case AUDIODEV_DRIVER__MAX:
1673 abort();
1677 static void audio_validate_per_direction_opts(
1678 AudiodevPerDirectionOptions *pdo, Error **errp)
1680 if (!pdo->has_fixed_settings) {
1681 pdo->has_fixed_settings = true;
1682 pdo->fixed_settings = true;
1684 if (!pdo->fixed_settings &&
1685 (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
1686 error_setg(errp,
1687 "You can't use frequency, channels or format with fixed-settings=off");
1688 return;
1691 if (!pdo->has_frequency) {
1692 pdo->has_frequency = true;
1693 pdo->frequency = 44100;
1695 if (!pdo->has_channels) {
1696 pdo->has_channels = true;
1697 pdo->channels = 2;
1699 if (!pdo->has_voices) {
1700 pdo->has_voices = true;
1701 pdo->voices = 1;
1703 if (!pdo->has_format) {
1704 pdo->has_format = true;
1705 pdo->format = AUDIO_FORMAT_S16;
1709 static void audio_validate_opts(Audiodev *dev, Error **errp)
1711 Error *err = NULL;
1713 audio_create_pdos(dev);
1715 audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
1716 if (err) {
1717 error_propagate(errp, err);
1718 return;
1721 audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
1722 if (err) {
1723 error_propagate(errp, err);
1724 return;
1727 if (!dev->has_timer_period) {
1728 dev->has_timer_period = true;
1729 dev->timer_period = 10000; /* 100Hz -> 10ms */
1733 void audio_parse_option(const char *opt)
1735 AudiodevListEntry *e;
1736 Audiodev *dev = NULL;
1738 Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
1739 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
1740 visit_free(v);
1742 audio_validate_opts(dev, &error_fatal);
1744 e = g_malloc0(sizeof(AudiodevListEntry));
1745 e->dev = dev;
1746 QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
1749 void audio_init_audiodevs(void)
1751 AudiodevListEntry *e;
1753 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
1754 audio_init(e->dev);
1758 audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
1760 return (audsettings) {
1761 .freq = pdo->frequency,
1762 .nchannels = pdo->channels,
1763 .fmt = pdo->format,
1764 .endianness = AUDIO_HOST_ENDIANNESS,
1768 int audioformat_bytes_per_sample(AudioFormat fmt)
1770 switch (fmt) {
1771 case AUDIO_FORMAT_U8:
1772 case AUDIO_FORMAT_S8:
1773 return 1;
1775 case AUDIO_FORMAT_U16:
1776 case AUDIO_FORMAT_S16:
1777 return 2;
1779 case AUDIO_FORMAT_U32:
1780 case AUDIO_FORMAT_S32:
1781 return 4;
1783 case AUDIO_FORMAT__MAX:
1786 abort();
1790 /* frames = freq * usec / 1e6 */
1791 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
1792 audsettings *as, int def_usecs)
1794 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
1795 return (as->freq * usecs + 500000) / 1000000;
1798 /* samples = channels * frames = channels * freq * usec / 1e6 */
1799 int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
1800 audsettings *as, int def_usecs)
1802 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
1806 * bytes = bytes_per_sample * samples =
1807 * bytes_per_sample * channels * freq * usec / 1e6
1809 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
1810 audsettings *as, int def_usecs)
1812 return audio_buffer_samples(pdo, as, def_usecs) *
1813 audioformat_bytes_per_sample(as->fmt);