]> git.proxmox.com Git - mirror_qemu.git/blob - audio/audio.c
audio: add driver registry
[mirror_qemu.git] / audio / audio.c
1 /*
2 * QEMU Audio subsystem
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include "qemu/osdep.h"
25 #include "hw/hw.h"
26 #include "audio.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"
32
33 #define AUDIO_CAP "audio"
34 #include "audio_int.h"
35
36 /* #define DEBUG_LIVE */
37 /* #define DEBUG_OUT */
38 /* #define DEBUG_CAPTURE */
39 /* #define DEBUG_POLL */
40
41 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42
43
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.
47 */
48 static const char *audio_prio_list[] = {
49 "spice",
50 CONFIG_AUDIO_DRIVERS
51 "none",
52 "wav",
53 };
54
55 static QLIST_HEAD(, audio_driver) audio_drivers;
56
57 void audio_driver_register(audio_driver *drv)
58 {
59 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
60 }
61
62 audio_driver *audio_driver_lookup(const char *name)
63 {
64 struct audio_driver *d;
65
66 QLIST_FOREACH(d, &audio_drivers, next) {
67 if (strcmp(name, d->name) == 0) {
68 return d;
69 }
70 }
71 return NULL;
72 }
73
74 struct fixed_settings {
75 int enabled;
76 int nb_voices;
77 int greedy;
78 struct audsettings settings;
79 };
80
81 static struct {
82 struct fixed_settings fixed_out;
83 struct fixed_settings fixed_in;
84 union {
85 int hertz;
86 int64_t ticks;
87 } period;
88 int try_poll_in;
89 int try_poll_out;
90 } conf = {
91 .fixed_out = { /* DAC fixed settings */
92 .enabled = 1,
93 .nb_voices = 1,
94 .greedy = 1,
95 .settings = {
96 .freq = 44100,
97 .nchannels = 2,
98 .fmt = AUD_FMT_S16,
99 .endianness = AUDIO_HOST_ENDIANNESS,
100 }
101 },
102
103 .fixed_in = { /* ADC fixed settings */
104 .enabled = 1,
105 .nb_voices = 1,
106 .greedy = 1,
107 .settings = {
108 .freq = 44100,
109 .nchannels = 2,
110 .fmt = AUD_FMT_S16,
111 .endianness = AUDIO_HOST_ENDIANNESS,
112 }
113 },
114
115 .period = { .hertz = 100 },
116 .try_poll_in = 1,
117 .try_poll_out = 1,
118 };
119
120 static AudioState glob_audio_state;
121
122 const struct mixeng_volume nominal_volume = {
123 .mute = 0,
124 #ifdef FLOAT_MIXENG
125 .r = 1.0,
126 .l = 1.0,
127 #else
128 .r = 1ULL << 32,
129 .l = 1ULL << 32,
130 #endif
131 };
132
133 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
134 #error No its not
135 #else
136 static void audio_print_options (const char *prefix,
137 struct audio_option *opt);
138
139 int audio_bug (const char *funcname, int cond)
140 {
141 if (cond) {
142 static int shown;
143
144 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
145 if (!shown) {
146 struct audio_driver *d;
147
148 shown = 1;
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;
153 if (d) {
154 audio_print_options (d->name, d->options);
155 }
156 }
157 AUD_log (NULL, "Context:\n");
158
159 #if defined AUDIO_BREAKPOINT_ON_BUG
160 # if defined HOST_I386
161 # if defined __GNUC__
162 __asm__ ("int3");
163 # elif defined _MSC_VER
164 _asm _emit 0xcc;
165 # else
166 abort ();
167 # endif
168 # else
169 abort ();
170 # endif
171 #endif
172 }
173
174 return cond;
175 }
176 #endif
177
178 static inline int audio_bits_to_index (int bits)
179 {
180 switch (bits) {
181 case 8:
182 return 0;
183
184 case 16:
185 return 1;
186
187 case 32:
188 return 2;
189
190 default:
191 audio_bug ("bits_to_index", 1);
192 AUD_log (NULL, "invalid bits %d\n", bits);
193 return 0;
194 }
195 }
196
197 void *audio_calloc (const char *funcname, int nmemb, size_t size)
198 {
199 int cond;
200 size_t len;
201
202 len = nmemb * size;
203 cond = !nmemb || !size;
204 cond |= nmemb < 0;
205 cond |= len < size;
206
207 if (audio_bug ("audio_calloc", cond)) {
208 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
209 funcname);
210 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
211 return NULL;
212 }
213
214 return g_malloc0 (len);
215 }
216
217 static char *audio_alloc_prefix (const char *s)
218 {
219 const char qemu_prefix[] = "QEMU_";
220 size_t len, i;
221 char *r, *u;
222
223 if (!s) {
224 return NULL;
225 }
226
227 len = strlen (s);
228 r = g_malloc (len + sizeof (qemu_prefix));
229
230 u = r + sizeof (qemu_prefix) - 1;
231
232 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
233 pstrcat (r, len + sizeof (qemu_prefix), s);
234
235 for (i = 0; i < len; ++i) {
236 u[i] = qemu_toupper(u[i]);
237 }
238
239 return r;
240 }
241
242 static const char *audio_audfmt_to_string (audfmt_e fmt)
243 {
244 switch (fmt) {
245 case AUD_FMT_U8:
246 return "U8";
247
248 case AUD_FMT_U16:
249 return "U16";
250
251 case AUD_FMT_S8:
252 return "S8";
253
254 case AUD_FMT_S16:
255 return "S16";
256
257 case AUD_FMT_U32:
258 return "U32";
259
260 case AUD_FMT_S32:
261 return "S32";
262 }
263
264 dolog ("Bogus audfmt %d returning S16\n", fmt);
265 return "S16";
266 }
267
268 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
269 int *defaultp)
270 {
271 if (!strcasecmp (s, "u8")) {
272 *defaultp = 0;
273 return AUD_FMT_U8;
274 }
275 else if (!strcasecmp (s, "u16")) {
276 *defaultp = 0;
277 return AUD_FMT_U16;
278 }
279 else if (!strcasecmp (s, "u32")) {
280 *defaultp = 0;
281 return AUD_FMT_U32;
282 }
283 else if (!strcasecmp (s, "s8")) {
284 *defaultp = 0;
285 return AUD_FMT_S8;
286 }
287 else if (!strcasecmp (s, "s16")) {
288 *defaultp = 0;
289 return AUD_FMT_S16;
290 }
291 else if (!strcasecmp (s, "s32")) {
292 *defaultp = 0;
293 return AUD_FMT_S32;
294 }
295 else {
296 dolog ("Bogus audio format `%s' using %s\n",
297 s, audio_audfmt_to_string (defval));
298 *defaultp = 1;
299 return defval;
300 }
301 }
302
303 static audfmt_e audio_get_conf_fmt (const char *envname,
304 audfmt_e defval,
305 int *defaultp)
306 {
307 const char *var = getenv (envname);
308 if (!var) {
309 *defaultp = 1;
310 return defval;
311 }
312 return audio_string_to_audfmt (var, defval, defaultp);
313 }
314
315 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
316 {
317 int val;
318 char *strval;
319
320 strval = getenv (key);
321 if (strval) {
322 *defaultp = 0;
323 val = atoi (strval);
324 return val;
325 }
326 else {
327 *defaultp = 1;
328 return defval;
329 }
330 }
331
332 static const char *audio_get_conf_str (const char *key,
333 const char *defval,
334 int *defaultp)
335 {
336 const char *val = getenv (key);
337 if (!val) {
338 *defaultp = 1;
339 return defval;
340 }
341 else {
342 *defaultp = 0;
343 return val;
344 }
345 }
346
347 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
348 {
349 if (cap) {
350 fprintf(stderr, "%s: ", cap);
351 }
352
353 vfprintf(stderr, fmt, ap);
354 }
355
356 void AUD_log (const char *cap, const char *fmt, ...)
357 {
358 va_list ap;
359
360 va_start (ap, fmt);
361 AUD_vlog (cap, fmt, ap);
362 va_end (ap);
363 }
364
365 static void audio_print_options (const char *prefix,
366 struct audio_option *opt)
367 {
368 char *uprefix;
369
370 if (!prefix) {
371 dolog ("No prefix specified\n");
372 return;
373 }
374
375 if (!opt) {
376 dolog ("No options\n");
377 return;
378 }
379
380 uprefix = audio_alloc_prefix (prefix);
381
382 for (; opt->name; opt++) {
383 const char *state = "default";
384 printf (" %s_%s: ", uprefix, opt->name);
385
386 if (opt->overriddenp && *opt->overriddenp) {
387 state = "current";
388 }
389
390 switch (opt->tag) {
391 case AUD_OPT_BOOL:
392 {
393 int *intp = opt->valp;
394 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
395 }
396 break;
397
398 case AUD_OPT_INT:
399 {
400 int *intp = opt->valp;
401 printf ("integer, %s = %d\n", state, *intp);
402 }
403 break;
404
405 case AUD_OPT_FMT:
406 {
407 audfmt_e *fmtp = opt->valp;
408 printf (
409 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
410 state,
411 audio_audfmt_to_string (*fmtp)
412 );
413 }
414 break;
415
416 case AUD_OPT_STR:
417 {
418 const char **strp = opt->valp;
419 printf ("string, %s = %s\n",
420 state,
421 *strp ? *strp : "(not set)");
422 }
423 break;
424
425 default:
426 printf ("???\n");
427 dolog ("Bad value tag for option %s_%s %d\n",
428 uprefix, opt->name, opt->tag);
429 break;
430 }
431 printf (" %s\n", opt->descr);
432 }
433
434 g_free (uprefix);
435 }
436
437 static void audio_process_options (const char *prefix,
438 struct audio_option *opt)
439 {
440 char *optname;
441 const char qemu_prefix[] = "QEMU_";
442 size_t preflen, optlen;
443
444 if (audio_bug(__func__, !prefix)) {
445 dolog ("prefix = NULL\n");
446 return;
447 }
448
449 if (audio_bug(__func__, !opt)) {
450 dolog ("opt = NULL\n");
451 return;
452 }
453
454 preflen = strlen (prefix);
455
456 for (; opt->name; opt++) {
457 size_t len, i;
458 int def;
459
460 if (!opt->valp) {
461 dolog ("Option value pointer for `%s' is not set\n",
462 opt->name);
463 continue;
464 }
465
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
469 * sizeof) */
470 optlen = len + preflen + sizeof (qemu_prefix) + 1;
471 optname = g_malloc (optlen);
472
473 pstrcpy (optname, optlen, qemu_prefix);
474
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]);
478 }
479 pstrcat (optname, optlen, "_");
480 pstrcat (optname, optlen, opt->name);
481
482 def = 1;
483 switch (opt->tag) {
484 case AUD_OPT_BOOL:
485 case AUD_OPT_INT:
486 {
487 int *intp = opt->valp;
488 *intp = audio_get_conf_int (optname, *intp, &def);
489 }
490 break;
491
492 case AUD_OPT_FMT:
493 {
494 audfmt_e *fmtp = opt->valp;
495 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
496 }
497 break;
498
499 case AUD_OPT_STR:
500 {
501 const char **strp = opt->valp;
502 *strp = audio_get_conf_str (optname, *strp, &def);
503 }
504 break;
505
506 default:
507 dolog ("Bad value tag for option `%s' - %d\n",
508 optname, opt->tag);
509 break;
510 }
511
512 if (!opt->overriddenp) {
513 opt->overriddenp = &opt->overridden;
514 }
515 *opt->overriddenp = !def;
516 g_free (optname);
517 }
518 }
519
520 static void audio_print_settings (struct audsettings *as)
521 {
522 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
523
524 switch (as->fmt) {
525 case AUD_FMT_S8:
526 AUD_log (NULL, "S8");
527 break;
528 case AUD_FMT_U8:
529 AUD_log (NULL, "U8");
530 break;
531 case AUD_FMT_S16:
532 AUD_log (NULL, "S16");
533 break;
534 case AUD_FMT_U16:
535 AUD_log (NULL, "U16");
536 break;
537 case AUD_FMT_S32:
538 AUD_log (NULL, "S32");
539 break;
540 case AUD_FMT_U32:
541 AUD_log (NULL, "U32");
542 break;
543 default:
544 AUD_log (NULL, "invalid(%d)", as->fmt);
545 break;
546 }
547
548 AUD_log (NULL, " endianness=");
549 switch (as->endianness) {
550 case 0:
551 AUD_log (NULL, "little");
552 break;
553 case 1:
554 AUD_log (NULL, "big");
555 break;
556 default:
557 AUD_log (NULL, "invalid");
558 break;
559 }
560 AUD_log (NULL, "\n");
561 }
562
563 static int audio_validate_settings (struct audsettings *as)
564 {
565 int invalid;
566
567 invalid = as->nchannels != 1 && as->nchannels != 2;
568 invalid |= as->endianness != 0 && as->endianness != 1;
569
570 switch (as->fmt) {
571 case AUD_FMT_S8:
572 case AUD_FMT_U8:
573 case AUD_FMT_S16:
574 case AUD_FMT_U16:
575 case AUD_FMT_S32:
576 case AUD_FMT_U32:
577 break;
578 default:
579 invalid = 1;
580 break;
581 }
582
583 invalid |= as->freq <= 0;
584 return invalid ? -1 : 0;
585 }
586
587 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
588 {
589 int bits = 8, sign = 0;
590
591 switch (as->fmt) {
592 case AUD_FMT_S8:
593 sign = 1;
594 /* fall through */
595 case AUD_FMT_U8:
596 break;
597
598 case AUD_FMT_S16:
599 sign = 1;
600 /* fall through */
601 case AUD_FMT_U16:
602 bits = 16;
603 break;
604
605 case AUD_FMT_S32:
606 sign = 1;
607 /* fall through */
608 case AUD_FMT_U32:
609 bits = 32;
610 break;
611 }
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);
617 }
618
619 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
620 {
621 int bits = 8, sign = 0, shift = 0;
622
623 switch (as->fmt) {
624 case AUD_FMT_S8:
625 sign = 1;
626 case AUD_FMT_U8:
627 break;
628
629 case AUD_FMT_S16:
630 sign = 1;
631 case AUD_FMT_U16:
632 bits = 16;
633 shift = 1;
634 break;
635
636 case AUD_FMT_S32:
637 sign = 1;
638 case AUD_FMT_U32:
639 bits = 32;
640 shift = 2;
641 break;
642 }
643
644 info->freq = as->freq;
645 info->bits = bits;
646 info->sign = sign;
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);
652 }
653
654 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
655 {
656 if (!len) {
657 return;
658 }
659
660 if (info->sign) {
661 memset (buf, 0x00, len << info->shift);
662 }
663 else {
664 switch (info->bits) {
665 case 8:
666 memset (buf, 0x80, len << info->shift);
667 break;
668
669 case 16:
670 {
671 int i;
672 uint16_t *p = buf;
673 int shift = info->nchannels - 1;
674 short s = INT16_MAX;
675
676 if (info->swap_endianness) {
677 s = bswap16 (s);
678 }
679
680 for (i = 0; i < len << shift; i++) {
681 p[i] = s;
682 }
683 }
684 break;
685
686 case 32:
687 {
688 int i;
689 uint32_t *p = buf;
690 int shift = info->nchannels - 1;
691 int32_t s = INT32_MAX;
692
693 if (info->swap_endianness) {
694 s = bswap32 (s);
695 }
696
697 for (i = 0; i < len << shift; i++) {
698 p[i] = s;
699 }
700 }
701 break;
702
703 default:
704 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
705 info->bits);
706 break;
707 }
708 }
709 }
710
711 /*
712 * Capture
713 */
714 static void noop_conv (struct st_sample *dst, const void *src, int samples)
715 {
716 (void) src;
717 (void) dst;
718 (void) samples;
719 }
720
721 static CaptureVoiceOut *audio_pcm_capture_find_specific (
722 struct audsettings *as
723 )
724 {
725 CaptureVoiceOut *cap;
726 AudioState *s = &glob_audio_state;
727
728 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
729 if (audio_pcm_info_eq (&cap->hw.info, as)) {
730 return cap;
731 }
732 }
733 return NULL;
734 }
735
736 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
737 {
738 struct capture_callback *cb;
739
740 #ifdef DEBUG_CAPTURE
741 dolog ("notification %d sent\n", cmd);
742 #endif
743 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
744 cb->ops.notify (cb->opaque, cmd);
745 }
746 }
747
748 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
749 {
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);
755 }
756 }
757
758 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
759 {
760 HWVoiceOut *hw = &cap->hw;
761 SWVoiceOut *sw;
762 int enabled = 0;
763
764 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
765 if (sw->active) {
766 enabled = 1;
767 break;
768 }
769 }
770 audio_capture_maybe_changed (cap, enabled);
771 }
772
773 static void audio_detach_capture (HWVoiceOut *hw)
774 {
775 SWVoiceCap *sc = hw->cap_head.lh_first;
776
777 while (sc) {
778 SWVoiceCap *sc1 = sc->entries.le_next;
779 SWVoiceOut *sw = &sc->sw;
780 CaptureVoiceOut *cap = sc->cap;
781 int was_active = sw->active;
782
783 if (sw->rate) {
784 st_rate_stop (sw->rate);
785 sw->rate = NULL;
786 }
787
788 QLIST_REMOVE (sw, entries);
789 QLIST_REMOVE (sc, entries);
790 g_free (sc);
791 if (was_active) {
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);
796 }
797 sc = sc1;
798 }
799 }
800
801 static int audio_attach_capture (HWVoiceOut *hw)
802 {
803 AudioState *s = &glob_audio_state;
804 CaptureVoiceOut *cap;
805
806 audio_detach_capture (hw);
807 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
808 SWVoiceCap *sc;
809 SWVoiceOut *sw;
810 HWVoiceOut *hw_cap = &cap->hw;
811
812 sc = audio_calloc(__func__, 1, sizeof(*sc));
813 if (!sc) {
814 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
815 sizeof (*sc));
816 return -1;
817 }
818
819 sc->cap = cap;
820 sw = &sc->sw;
821 sw->hw = hw_cap;
822 sw->info = hw->info;
823 sw->empty = 1;
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);
829 if (!sw->rate) {
830 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
831 g_free (sw);
832 return -1;
833 }
834 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
835 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
836 #ifdef DEBUG_CAPTURE
837 sw->name = g_strdup_printf ("for %p %d,%d,%d",
838 hw, sw->info.freq, sw->info.bits,
839 sw->info.nchannels);
840 dolog ("Added %s active = %d\n", sw->name, sw->active);
841 #endif
842 if (sw->active) {
843 audio_capture_maybe_changed (cap, 1);
844 }
845 }
846 return 0;
847 }
848
849 /*
850 * Hard voice (capture)
851 */
852 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
853 {
854 SWVoiceIn *sw;
855 int m = hw->total_samples_captured;
856
857 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
858 if (sw->active) {
859 m = audio_MIN (m, sw->total_hw_samples_acquired);
860 }
861 }
862 return m;
863 }
864
865 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
866 {
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);
870 return 0;
871 }
872 return live;
873 }
874
875 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
876 int live, int pending)
877 {
878 int left = hw->samples - pending;
879 int len = audio_MIN (left, live);
880 int clipped = 0;
881
882 while (len) {
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);
887
888 hw->clip (dst, src, samples_to_clip);
889
890 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
891 len -= samples_to_clip;
892 clipped += samples_to_clip;
893 }
894 return clipped;
895 }
896
897 /*
898 * Soft voice (capture)
899 */
900 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
901 {
902 HWVoiceIn *hw = sw->hw;
903 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
904 int rpos;
905
906 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
907 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
908 return 0;
909 }
910
911 rpos = hw->wpos - live;
912 if (rpos >= 0) {
913 return rpos;
914 }
915 else {
916 return hw->samples + rpos;
917 }
918 }
919
920 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
921 {
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;
925
926 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
927
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);
931 return 0;
932 }
933
934 samples = size >> sw->info.shift;
935 if (!live) {
936 return 0;
937 }
938
939 swlim = (live * sw->ratio) >> 32;
940 swlim = audio_MIN (swlim, samples);
941
942 while (swlim) {
943 src = hw->conv_buf + rpos;
944 isamp = hw->wpos - rpos;
945 /* XXX: <= ? */
946 if (isamp <= 0) {
947 isamp = hw->samples - rpos;
948 }
949
950 if (!isamp) {
951 break;
952 }
953 osamp = swlim;
954
955 if (audio_bug(__func__, osamp < 0)) {
956 dolog ("osamp=%d\n", osamp);
957 return 0;
958 }
959
960 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
961 swlim -= osamp;
962 rpos = (rpos + isamp) % hw->samples;
963 dst += osamp;
964 ret += osamp;
965 total += isamp;
966 }
967
968 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
969 mixeng_volume (sw->buf, ret, &sw->vol);
970 }
971
972 sw->clip (buf, sw->buf, ret);
973 sw->total_hw_samples_acquired += total;
974 return ret << sw->info.shift;
975 }
976
977 /*
978 * Hard voice (playback)
979 */
980 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
981 {
982 SWVoiceOut *sw;
983 int m = INT_MAX;
984 int nb_live = 0;
985
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);
989 nb_live += 1;
990 }
991 }
992
993 *nb_livep = nb_live;
994 return m;
995 }
996
997 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
998 {
999 int smin;
1000 int nb_live1;
1001
1002 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
1003 if (nb_live) {
1004 *nb_live = nb_live1;
1005 }
1006
1007 if (nb_live1) {
1008 int live = smin;
1009
1010 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
1011 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1012 return 0;
1013 }
1014 return live;
1015 }
1016 return 0;
1017 }
1018
1019 /*
1020 * Soft voice (playback)
1021 */
1022 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1023 {
1024 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1025 int ret = 0, pos = 0, total = 0;
1026
1027 if (!sw) {
1028 return size;
1029 }
1030
1031 hwsamples = sw->hw->samples;
1032
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);
1036 return 0;
1037 }
1038
1039 if (live == hwsamples) {
1040 #ifdef DEBUG_OUT
1041 dolog ("%s is full %d\n", sw->name, live);
1042 #endif
1043 return 0;
1044 }
1045
1046 wpos = (sw->hw->rpos + live) % hwsamples;
1047 samples = size >> sw->info.shift;
1048
1049 dead = hwsamples - live;
1050 swlim = ((int64_t) dead << 32) / sw->ratio;
1051 swlim = audio_MIN (swlim, samples);
1052 if (swlim) {
1053 sw->conv (sw->buf, buf, swlim);
1054
1055 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1056 mixeng_volume (sw->buf, swlim, &sw->vol);
1057 }
1058 }
1059
1060 while (swlim) {
1061 dead = hwsamples - live;
1062 left = hwsamples - wpos;
1063 blck = audio_MIN (dead, left);
1064 if (!blck) {
1065 break;
1066 }
1067 isamp = swlim;
1068 osamp = blck;
1069 st_rate_flow_mix (
1070 sw->rate,
1071 sw->buf + pos,
1072 sw->hw->mix_buf + wpos,
1073 &isamp,
1074 &osamp
1075 );
1076 ret += isamp;
1077 swlim -= isamp;
1078 pos += isamp;
1079 live += osamp;
1080 wpos = (wpos + osamp) % hwsamples;
1081 total += osamp;
1082 }
1083
1084 sw->total_hw_samples_mixed += total;
1085 sw->empty = sw->total_hw_samples_mixed == 0;
1086
1087 #ifdef DEBUG_OUT
1088 dolog (
1089 "%s: write size %d ret %d total sw %d\n",
1090 SW_NAME (sw),
1091 size >> sw->info.shift,
1092 ret,
1093 sw->total_hw_samples_mixed
1094 );
1095 #endif
1096
1097 return ret << sw->info.shift;
1098 }
1099
1100 #ifdef DEBUG_AUDIO
1101 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1102 {
1103 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1104 cap, info->bits, info->sign, info->freq, info->nchannels);
1105 }
1106 #endif
1107
1108 #define DAC
1109 #include "audio_template.h"
1110 #undef DAC
1111 #include "audio_template.h"
1112
1113 /*
1114 * Timer
1115 */
1116 static int audio_is_timer_needed (void)
1117 {
1118 HWVoiceIn *hwi = NULL;
1119 HWVoiceOut *hwo = NULL;
1120
1121 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1122 if (!hwo->poll_mode) return 1;
1123 }
1124 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1125 if (!hwi->poll_mode) return 1;
1126 }
1127 return 0;
1128 }
1129
1130 static void audio_reset_timer (AudioState *s)
1131 {
1132 if (audio_is_timer_needed ()) {
1133 timer_mod_anticipate_ns(s->ts,
1134 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1135 }
1136 else {
1137 timer_del (s->ts);
1138 }
1139 }
1140
1141 static void audio_timer (void *opaque)
1142 {
1143 audio_run ("timer");
1144 audio_reset_timer (opaque);
1145 }
1146
1147 /*
1148 * Public API
1149 */
1150 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1151 {
1152 if (!sw) {
1153 /* XXX: Consider options */
1154 return size;
1155 }
1156
1157 if (!sw->hw->enabled) {
1158 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1159 return 0;
1160 }
1161
1162 return sw->hw->pcm_ops->write(sw, buf, size);
1163 }
1164
1165 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1166 {
1167 if (!sw) {
1168 /* XXX: Consider options */
1169 return size;
1170 }
1171
1172 if (!sw->hw->enabled) {
1173 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1174 return 0;
1175 }
1176
1177 return sw->hw->pcm_ops->read(sw, buf, size);
1178 }
1179
1180 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1181 {
1182 return sw->hw->samples << sw->hw->info.shift;
1183 }
1184
1185 void AUD_set_active_out (SWVoiceOut *sw, int on)
1186 {
1187 HWVoiceOut *hw;
1188
1189 if (!sw) {
1190 return;
1191 }
1192
1193 hw = sw->hw;
1194 if (sw->active != on) {
1195 AudioState *s = &glob_audio_state;
1196 SWVoiceOut *temp_sw;
1197 SWVoiceCap *sc;
1198
1199 if (on) {
1200 hw->pending_disable = 0;
1201 if (!hw->enabled) {
1202 hw->enabled = 1;
1203 if (s->vm_running) {
1204 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1205 audio_reset_timer (s);
1206 }
1207 }
1208 }
1209 else {
1210 if (hw->enabled) {
1211 int nb_active = 0;
1212
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;
1216 }
1217
1218 hw->pending_disable = nb_active == 1;
1219 }
1220 }
1221
1222 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1223 sc->sw.active = hw->enabled;
1224 if (hw->enabled) {
1225 audio_capture_maybe_changed (sc->cap, 1);
1226 }
1227 }
1228 sw->active = on;
1229 }
1230 }
1231
1232 void AUD_set_active_in (SWVoiceIn *sw, int on)
1233 {
1234 HWVoiceIn *hw;
1235
1236 if (!sw) {
1237 return;
1238 }
1239
1240 hw = sw->hw;
1241 if (sw->active != on) {
1242 AudioState *s = &glob_audio_state;
1243 SWVoiceIn *temp_sw;
1244
1245 if (on) {
1246 if (!hw->enabled) {
1247 hw->enabled = 1;
1248 if (s->vm_running) {
1249 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1250 audio_reset_timer (s);
1251 }
1252 }
1253 sw->total_hw_samples_acquired = hw->total_samples_captured;
1254 }
1255 else {
1256 if (hw->enabled) {
1257 int nb_active = 0;
1258
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;
1262 }
1263
1264 if (nb_active == 1) {
1265 hw->enabled = 0;
1266 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1267 }
1268 }
1269 }
1270 sw->active = on;
1271 }
1272 }
1273
1274 static int audio_get_avail (SWVoiceIn *sw)
1275 {
1276 int live;
1277
1278 if (!sw) {
1279 return 0;
1280 }
1281
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);
1285 return 0;
1286 }
1287
1288 ldebug (
1289 "%s: get_avail live %d ret %" PRId64 "\n",
1290 SW_NAME (sw),
1291 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1292 );
1293
1294 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1295 }
1296
1297 static int audio_get_free (SWVoiceOut *sw)
1298 {
1299 int live, dead;
1300
1301 if (!sw) {
1302 return 0;
1303 }
1304
1305 live = sw->total_hw_samples_mixed;
1306
1307 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
1308 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1309 return 0;
1310 }
1311
1312 dead = sw->hw->samples - live;
1313
1314 #ifdef DEBUG_OUT
1315 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1316 SW_NAME (sw),
1317 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1318 #endif
1319
1320 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1321 }
1322
1323 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1324 {
1325 int n;
1326
1327 if (hw->enabled) {
1328 SWVoiceCap *sc;
1329
1330 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1331 SWVoiceOut *sw = &sc->sw;
1332 int rpos2 = rpos;
1333
1334 n = samples;
1335 while (n) {
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;
1339 int written;
1340
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",
1346 bytes, written);
1347 break;
1348 }
1349 n -= to_write;
1350 rpos2 = (rpos2 + to_write) % hw->samples;
1351 }
1352 }
1353 }
1354
1355 n = audio_MIN (samples, hw->samples - rpos);
1356 mixeng_clear (hw->mix_buf + rpos, n);
1357 mixeng_clear (hw->mix_buf, samples - n);
1358 }
1359
1360 static void audio_run_out (AudioState *s)
1361 {
1362 HWVoiceOut *hw = NULL;
1363 SWVoiceOut *sw;
1364
1365 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1366 int played;
1367 int live, free, nb_live, cleanup_required, prev_rpos;
1368
1369 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1370 if (!nb_live) {
1371 live = 0;
1372 }
1373
1374 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
1375 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1376 continue;
1377 }
1378
1379 if (hw->pending_disable && !nb_live) {
1380 SWVoiceCap *sc;
1381 #ifdef DEBUG_OUT
1382 dolog ("Disabling voice\n");
1383 #endif
1384 hw->enabled = 0;
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) {
1388 sc->sw.active = 0;
1389 audio_recalc_and_notify_capture (sc->cap);
1390 }
1391 continue;
1392 }
1393
1394 if (!live) {
1395 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1396 if (sw->active) {
1397 free = audio_get_free (sw);
1398 if (free > 0) {
1399 sw->callback.fn (sw->callback.opaque, free);
1400 }
1401 }
1402 }
1403 continue;
1404 }
1405
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);
1412 hw->rpos = 0;
1413 }
1414
1415 #ifdef DEBUG_OUT
1416 dolog ("played=%d\n", played);
1417 #endif
1418
1419 if (played) {
1420 hw->ts_helper += played;
1421 audio_capture_mix_and_clear (hw, prev_rpos, played);
1422 }
1423
1424 cleanup_required = 0;
1425 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1426 if (!sw->active && sw->empty) {
1427 continue;
1428 }
1429
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;
1434 }
1435
1436 sw->total_hw_samples_mixed -= played;
1437
1438 if (!sw->total_hw_samples_mixed) {
1439 sw->empty = 1;
1440 cleanup_required |= !sw->active && !sw->callback.fn;
1441 }
1442
1443 if (sw->active) {
1444 free = audio_get_free (sw);
1445 if (free > 0) {
1446 sw->callback.fn (sw->callback.opaque, free);
1447 }
1448 }
1449 }
1450
1451 if (cleanup_required) {
1452 SWVoiceOut *sw1;
1453
1454 sw = hw->sw_head.lh_first;
1455 while (sw) {
1456 sw1 = sw->entries.le_next;
1457 if (!sw->active && !sw->callback.fn) {
1458 audio_close_out (sw);
1459 }
1460 sw = sw1;
1461 }
1462 }
1463 }
1464 }
1465
1466 static void audio_run_in (AudioState *s)
1467 {
1468 HWVoiceIn *hw = NULL;
1469
1470 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1471 SWVoiceIn *sw;
1472 int captured = 0, min;
1473
1474 if (replay_mode != REPLAY_MODE_PLAY) {
1475 captured = hw->pcm_ops->run_in(hw);
1476 }
1477 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
1478
1479 min = audio_pcm_hw_find_min_in (hw);
1480 hw->total_samples_captured += captured - min;
1481 hw->ts_helper += captured;
1482
1483 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1484 sw->total_hw_samples_acquired -= min;
1485
1486 if (sw->active) {
1487 int avail;
1488
1489 avail = audio_get_avail (sw);
1490 if (avail > 0) {
1491 sw->callback.fn (sw->callback.opaque, avail);
1492 }
1493 }
1494 }
1495 }
1496 }
1497
1498 static void audio_run_capture (AudioState *s)
1499 {
1500 CaptureVoiceOut *cap;
1501
1502 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1503 int live, rpos, captured;
1504 HWVoiceOut *hw = &cap->hw;
1505 SWVoiceOut *sw;
1506
1507 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1508 rpos = hw->rpos;
1509 while (live) {
1510 int left = hw->samples - rpos;
1511 int to_capture = audio_MIN (live, left);
1512 struct st_sample *src;
1513 struct capture_callback *cb;
1514
1515 src = hw->mix_buf + rpos;
1516 hw->clip (cap->buf, src, to_capture);
1517 mixeng_clear (src, to_capture);
1518
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);
1522 }
1523 rpos = (rpos + to_capture) % hw->samples;
1524 live -= to_capture;
1525 }
1526 hw->rpos = rpos;
1527
1528 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1529 if (!sw->active && sw->empty) {
1530 continue;
1531 }
1532
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;
1537 }
1538
1539 sw->total_hw_samples_mixed -= captured;
1540 sw->empty = sw->total_hw_samples_mixed == 0;
1541 }
1542 }
1543 }
1544
1545 void audio_run (const char *msg)
1546 {
1547 AudioState *s = &glob_audio_state;
1548
1549 audio_run_out (s);
1550 audio_run_in (s);
1551 audio_run_capture (s);
1552 #ifdef DEBUG_POLL
1553 {
1554 static double prevtime;
1555 double currtime;
1556 struct timeval tv;
1557
1558 if (gettimeofday (&tv, NULL)) {
1559 perror ("audio_run: gettimeofday");
1560 return;
1561 }
1562
1563 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1564 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1565 prevtime = currtime;
1566 }
1567 #endif
1568 }
1569
1570 static struct audio_option audio_options[] = {
1571 /* DAC */
1572 {
1573 .name = "DAC_FIXED_SETTINGS",
1574 .tag = AUD_OPT_BOOL,
1575 .valp = &conf.fixed_out.enabled,
1576 .descr = "Use fixed settings for host DAC"
1577 },
1578 {
1579 .name = "DAC_FIXED_FREQ",
1580 .tag = AUD_OPT_INT,
1581 .valp = &conf.fixed_out.settings.freq,
1582 .descr = "Frequency for fixed host DAC"
1583 },
1584 {
1585 .name = "DAC_FIXED_FMT",
1586 .tag = AUD_OPT_FMT,
1587 .valp = &conf.fixed_out.settings.fmt,
1588 .descr = "Format for fixed host DAC"
1589 },
1590 {
1591 .name = "DAC_FIXED_CHANNELS",
1592 .tag = AUD_OPT_INT,
1593 .valp = &conf.fixed_out.settings.nchannels,
1594 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1595 },
1596 {
1597 .name = "DAC_VOICES",
1598 .tag = AUD_OPT_INT,
1599 .valp = &conf.fixed_out.nb_voices,
1600 .descr = "Number of voices for DAC"
1601 },
1602 {
1603 .name = "DAC_TRY_POLL",
1604 .tag = AUD_OPT_BOOL,
1605 .valp = &conf.try_poll_out,
1606 .descr = "Attempt using poll mode for DAC"
1607 },
1608 /* ADC */
1609 {
1610 .name = "ADC_FIXED_SETTINGS",
1611 .tag = AUD_OPT_BOOL,
1612 .valp = &conf.fixed_in.enabled,
1613 .descr = "Use fixed settings for host ADC"
1614 },
1615 {
1616 .name = "ADC_FIXED_FREQ",
1617 .tag = AUD_OPT_INT,
1618 .valp = &conf.fixed_in.settings.freq,
1619 .descr = "Frequency for fixed host ADC"
1620 },
1621 {
1622 .name = "ADC_FIXED_FMT",
1623 .tag = AUD_OPT_FMT,
1624 .valp = &conf.fixed_in.settings.fmt,
1625 .descr = "Format for fixed host ADC"
1626 },
1627 {
1628 .name = "ADC_FIXED_CHANNELS",
1629 .tag = AUD_OPT_INT,
1630 .valp = &conf.fixed_in.settings.nchannels,
1631 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1632 },
1633 {
1634 .name = "ADC_VOICES",
1635 .tag = AUD_OPT_INT,
1636 .valp = &conf.fixed_in.nb_voices,
1637 .descr = "Number of voices for ADC"
1638 },
1639 {
1640 .name = "ADC_TRY_POLL",
1641 .tag = AUD_OPT_BOOL,
1642 .valp = &conf.try_poll_in,
1643 .descr = "Attempt using poll mode for ADC"
1644 },
1645 /* Misc */
1646 {
1647 .name = "TIMER_PERIOD",
1648 .tag = AUD_OPT_INT,
1649 .valp = &conf.period.hertz,
1650 .descr = "Timer period in HZ (0 - use lowest possible)"
1651 },
1652 { /* End of list */ }
1653 };
1654
1655 static void audio_pp_nb_voices (const char *typ, int nb)
1656 {
1657 switch (nb) {
1658 case 0:
1659 printf ("Does not support %s\n", typ);
1660 break;
1661 case 1:
1662 printf ("One %s voice\n", typ);
1663 break;
1664 case INT_MAX:
1665 printf ("Theoretically supports many %s voices\n", typ);
1666 break;
1667 default:
1668 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1669 break;
1670 }
1671
1672 }
1673
1674 void AUD_help (void)
1675 {
1676 struct audio_driver *d;
1677
1678 audio_process_options ("AUDIO", audio_options);
1679 QLIST_FOREACH(d, &audio_drivers, next) {
1680 if (d->options) {
1681 audio_process_options (d->name, d->options);
1682 }
1683 }
1684
1685 printf ("Audio options:\n");
1686 audio_print_options ("AUDIO", audio_options);
1687 printf ("\n");
1688
1689 printf ("Available drivers:\n");
1690
1691 QLIST_FOREACH(d, &audio_drivers, next) {
1692
1693 printf ("Name: %s\n", d->name);
1694 printf ("Description: %s\n", d->descr);
1695
1696 audio_pp_nb_voices ("playback", d->max_voices_out);
1697 audio_pp_nb_voices ("capture", d->max_voices_in);
1698
1699 if (d->options) {
1700 printf ("Options:\n");
1701 audio_print_options (d->name, d->options);
1702 }
1703 else {
1704 printf ("No options\n");
1705 }
1706 printf ("\n");
1707 }
1708
1709 printf (
1710 "Options are settable through environment variables.\n"
1711 "Example:\n"
1712 #ifdef _WIN32
1713 " set QEMU_AUDIO_DRV=wav\n"
1714 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1715 #else
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"
1719 #endif
1720 " qemu ...\n\n"
1721 );
1722 }
1723
1724 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1725 {
1726 if (drv->options) {
1727 audio_process_options (drv->name, drv->options);
1728 }
1729 s->drv_opaque = drv->init ();
1730
1731 if (s->drv_opaque) {
1732 audio_init_nb_voices_out (drv);
1733 audio_init_nb_voices_in (drv);
1734 s->drv = drv;
1735 return 0;
1736 }
1737 else {
1738 dolog ("Could not init `%s' audio driver\n", drv->name);
1739 return -1;
1740 }
1741 }
1742
1743 static void audio_vm_change_state_handler (void *opaque, int running,
1744 RunState state)
1745 {
1746 AudioState *s = opaque;
1747 HWVoiceOut *hwo = NULL;
1748 HWVoiceIn *hwi = NULL;
1749 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1750
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);
1754 }
1755
1756 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1757 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1758 }
1759 audio_reset_timer (s);
1760 }
1761
1762 static bool is_cleaning_up;
1763
1764 bool audio_is_cleaning_up(void)
1765 {
1766 return is_cleaning_up;
1767 }
1768
1769 void audio_cleanup(void)
1770 {
1771 AudioState *s = &glob_audio_state;
1772 HWVoiceOut *hwo, *hwon;
1773 HWVoiceIn *hwi, *hwin;
1774
1775 is_cleaning_up = true;
1776 QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
1777 SWVoiceCap *sc;
1778
1779 if (hwo->enabled) {
1780 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1781 }
1782 hwo->pcm_ops->fini_out (hwo);
1783
1784 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1785 CaptureVoiceOut *cap = sc->cap;
1786 struct capture_callback *cb;
1787
1788 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1789 cb->ops.destroy (cb->opaque);
1790 }
1791 }
1792 QLIST_REMOVE(hwo, entries);
1793 }
1794
1795 QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
1796 if (hwi->enabled) {
1797 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1798 }
1799 hwi->pcm_ops->fini_in (hwi);
1800 QLIST_REMOVE(hwi, entries);
1801 }
1802
1803 if (s->drv) {
1804 s->drv->fini (s->drv_opaque);
1805 s->drv = NULL;
1806 }
1807 }
1808
1809 static const VMStateDescription vmstate_audio = {
1810 .name = "audio",
1811 .version_id = 1,
1812 .minimum_version_id = 1,
1813 .fields = (VMStateField[]) {
1814 VMSTATE_END_OF_LIST()
1815 }
1816 };
1817
1818 static void audio_init (void)
1819 {
1820 size_t i;
1821 int done = 0;
1822 const char *drvname;
1823 VMChangeStateEntry *e;
1824 AudioState *s = &glob_audio_state;
1825 struct audio_driver *driver;
1826
1827 if (s->drv) {
1828 return;
1829 }
1830
1831 QLIST_INIT (&s->hw_head_out);
1832 QLIST_INIT (&s->hw_head_in);
1833 QLIST_INIT (&s->cap_head);
1834 atexit(audio_cleanup);
1835
1836 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1837
1838 audio_process_options ("AUDIO", audio_options);
1839
1840 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1841 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1842
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;
1847 }
1848
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;
1853 }
1854
1855 {
1856 int def;
1857 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1858 }
1859
1860 if (drvname) {
1861 driver = audio_driver_lookup(drvname);
1862 if (driver) {
1863 done = !audio_driver_init(s, driver);
1864 } else {
1865 dolog ("Unknown audio driver `%s'\n", drvname);
1866 dolog ("Run with -audio-help to list available drivers\n");
1867 }
1868 }
1869
1870 if (!done) {
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);
1875 }
1876 }
1877 }
1878
1879 if (!done) {
1880 driver = audio_driver_lookup("none");
1881 done = !audio_driver_init(s, driver);
1882 assert(done);
1883 dolog("warning: Using timer based audio emulation\n");
1884 }
1885
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",
1890 conf.period.hertz);
1891 }
1892 conf.period.ticks = 1;
1893 } else {
1894 conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
1895 }
1896
1897 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1898 if (!e) {
1899 dolog ("warning: Could not register change state handler\n"
1900 "(Audio can continue looping even after stopping the VM)\n");
1901 }
1902
1903 QLIST_INIT (&s->card_head);
1904 vmstate_register (NULL, 0, &vmstate_audio, s);
1905 }
1906
1907 void AUD_register_card (const char *name, QEMUSoundCard *card)
1908 {
1909 audio_init ();
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);
1913 }
1914
1915 void AUD_remove_card (QEMUSoundCard *card)
1916 {
1917 QLIST_REMOVE (card, entries);
1918 g_free (card->name);
1919 }
1920
1921
1922 CaptureVoiceOut *AUD_add_capture (
1923 struct audsettings *as,
1924 struct audio_capture_ops *ops,
1925 void *cb_opaque
1926 )
1927 {
1928 AudioState *s = &glob_audio_state;
1929 CaptureVoiceOut *cap;
1930 struct capture_callback *cb;
1931
1932 if (audio_validate_settings (as)) {
1933 dolog ("Invalid settings were passed when trying to add capture\n");
1934 audio_print_settings (as);
1935 goto err0;
1936 }
1937
1938 cb = audio_calloc(__func__, 1, sizeof(*cb));
1939 if (!cb) {
1940 dolog ("Could not allocate capture callback information, size %zu\n",
1941 sizeof (*cb));
1942 goto err0;
1943 }
1944 cb->ops = *ops;
1945 cb->opaque = cb_opaque;
1946
1947 cap = audio_pcm_capture_find_specific (as);
1948 if (cap) {
1949 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1950 return cap;
1951 }
1952 else {
1953 HWVoiceOut *hw;
1954 CaptureVoiceOut *cap;
1955
1956 cap = audio_calloc(__func__, 1, sizeof(*cap));
1957 if (!cap) {
1958 dolog ("Could not allocate capture voice, size %zu\n",
1959 sizeof (*cap));
1960 goto err1;
1961 }
1962
1963 hw = &cap->hw;
1964 QLIST_INIT (&hw->sw_head);
1965 QLIST_INIT (&cap->cb_head);
1966
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));
1971 if (!hw->mix_buf) {
1972 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1973 hw->samples);
1974 goto err2;
1975 }
1976
1977 audio_pcm_init_info (&hw->info, as);
1978
1979 cap->buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
1980 if (!cap->buf) {
1981 dolog ("Could not allocate capture buffer "
1982 "(%d samples, each %d bytes)\n",
1983 hw->samples, 1 << hw->info.shift);
1984 goto err3;
1985 }
1986
1987 hw->clip = mixeng_clip
1988 [hw->info.nchannels == 2]
1989 [hw->info.sign]
1990 [hw->info.swap_endianness]
1991 [audio_bits_to_index (hw->info.bits)];
1992
1993 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1994 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1995
1996 QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
1997 audio_attach_capture (hw);
1998 }
1999 return cap;
2000
2001 err3:
2002 g_free (cap->hw.mix_buf);
2003 err2:
2004 g_free (cap);
2005 err1:
2006 g_free (cb);
2007 err0:
2008 return NULL;
2009 }
2010 }
2011
2012 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2013 {
2014 struct capture_callback *cb;
2015
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);
2020 g_free (cb);
2021
2022 if (!cap->cb_head.lh_first) {
2023 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2024
2025 while (sw) {
2026 SWVoiceCap *sc = (SWVoiceCap *) sw;
2027 #ifdef DEBUG_CAPTURE
2028 dolog ("freeing %s\n", sw->name);
2029 #endif
2030
2031 sw1 = sw->entries.le_next;
2032 if (sw->rate) {
2033 st_rate_stop (sw->rate);
2034 sw->rate = NULL;
2035 }
2036 QLIST_REMOVE (sw, entries);
2037 QLIST_REMOVE (sc, entries);
2038 g_free (sc);
2039 sw = sw1;
2040 }
2041 QLIST_REMOVE (cap, entries);
2042 g_free (cap->hw.mix_buf);
2043 g_free (cap->buf);
2044 g_free (cap);
2045 }
2046 return;
2047 }
2048 }
2049 }
2050
2051 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2052 {
2053 if (sw) {
2054 HWVoiceOut *hw = sw->hw;
2055
2056 sw->vol.mute = mute;
2057 sw->vol.l = nominal_volume.l * lvol / 255;
2058 sw->vol.r = nominal_volume.r * rvol / 255;
2059
2060 if (hw->pcm_ops->ctl_out) {
2061 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2062 }
2063 }
2064 }
2065
2066 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2067 {
2068 if (sw) {
2069 HWVoiceIn *hw = sw->hw;
2070
2071 sw->vol.mute = mute;
2072 sw->vol.l = nominal_volume.l * lvol / 255;
2073 sw->vol.r = nominal_volume.r * rvol / 255;
2074
2075 if (hw->pcm_ops->ctl_in) {
2076 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
2077 }
2078 }
2079 }