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