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