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