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