(linenum→info "unix/slp.c:2238")

qemu/0.9.1/audio/audio.c

    1: /*
    2:  * QEMU Audio subsystem
    3:  *
    4:  * Copyright (c) 2003-2005 Vassili Karpov (malc)
    5:  *
    6:  * Permission is hereby granted, free of charge, to any person obtaining a copy
    7:  * of this software and associated documentation files (the "Software"), to deal
    8:  * in the Software without restriction, including without limitation the rights
    9:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   10:  * copies of the Software, and to permit persons to whom the Software is
   11:  * furnished to do so, subject to the following conditions:
   12:  *
   13:  * The above copyright notice and this permission notice shall be included in
   14:  * all copies or substantial portions of the Software.
   15:  *
   16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   17:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   21:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   22:  * THE SOFTWARE.
   23:  */
   24: #include "hw/hw.h"
   25: #include "audio.h"
   26: #include "console.h"
   27: #include "qemu-timer.h"
   28: #include "sysemu.h"
   29: 
   30: #define AUDIO_CAP "audio"
   31: #include "audio_int.h"
   32: 
   33: /* #define DEBUG_PLIVE */
   34: /* #define DEBUG_LIVE */
   35: /* #define DEBUG_OUT */
   36: /* #define DEBUG_CAPTURE */
   37: 
   38: #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
   39: 
   40: static struct audio_driver *drvtab[] = {
   41: #ifdef CONFIG_OSS
   42:     &oss_audio_driver,
   43: #endif
   44: #ifdef CONFIG_ALSA
   45:     &alsa_audio_driver,
   46: #endif
   47: #ifdef CONFIG_COREAUDIO
   48:     &coreaudio_audio_driver,
   49: #endif
   50: #ifdef CONFIG_DSOUND
   51:     &dsound_audio_driver,
   52: #endif
   53: #ifdef CONFIG_FMOD
   54:     &fmod_audio_driver,
   55: #endif
   56: #ifdef CONFIG_SDL
   57:     &sdl_audio_driver,
   58: #endif
   59:     &no_audio_driver,
   60:     &wav_audio_driver
   61: };
   62: 
   63: struct fixed_settings {
   64:     int enabled;
   65:     int nb_voices;
   66:     int greedy;
   67:     audsettings_t settings;
   68: };
   69: 
   70: static struct {
   71:     struct fixed_settings fixed_out;
   72:     struct fixed_settings fixed_in;
   73:     union {
   74:         int hz;
   75:         int64_t ticks;
   76:     } period;
   77:     int plive;
   78:     int log_to_monitor;
   79: } conf = {
   80:     {                           /* DAC fixed settings */
   81:         1,                      /* enabled */
   82:         1,                      /* nb_voices */
   83:         1,                      /* greedy */
   84:         {
   85:             44100,              /* freq */
   86:             2,                  /* nchannels */
   87:             AUD_FMT_S16,        /* fmt */
   88:             AUDIO_HOST_ENDIANNESS
   89:         }
   90:     },
   91: 
   92:     {                           /* ADC fixed settings */
   93:         1,                      /* enabled */
   94:         1,                      /* nb_voices */
   95:         1,                      /* greedy */
   96:         {
   97:             44100,              /* freq */
   98:             2,                  /* nchannels */
   99:             AUD_FMT_S16,        /* fmt */
  100:             AUDIO_HOST_ENDIANNESS
  101:         }
  102:     },
  103: 
  104:     { 0 },                      /* period */
  105:     0,                          /* plive */
  106:     0                           /* log_to_monitor */
  107: };
  108: 
  109: static AudioState glob_audio_state;
  110: 
  111: volume_t nominal_volume = {
  112:     0,
  113: #ifdef FLOAT_MIXENG
  114:     1.0,
  115:     1.0
  116: #else
  117:     UINT_MAX,
  118:     UINT_MAX
  119: #endif
  120: };
  121: 
  122: /* http://www.df.lth.se/~john_e/gems/gem002d.html */
  123: /* http://www.multi-platforms.com/Tips/PopCount.htm */
  124: uint32_t popcount (uint32_t u)
  125: {
  126:     u = ((u&0x55555555) + ((u>>1)&0x55555555));
  127:     u = ((u&0x33333333) + ((u>>2)&0x33333333));
  128:     u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
  129:     u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
  130:     u = ( u&0x0000ffff) + (u>>16);
  131:     return u;
  132: }
  133: 
  134: inline uint32_t lsbindex (uint32_t u)
  135: {
  136:     return popcount ((u&-u)-1);
  137: }
  138: 
  139: #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
  140: #error No its not
  141: #else
  142: int audio_bug (const char *funcname, int cond)
  143: {
  144:     if (cond) {
  145:         static int shown;
  146: 
  147:         AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
  148:         if (!shown) {
  149:             shown = 1;
  150:             AUD_log (NULL, "Save all your work and restart without audio\n");
  151:             AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
  152:             AUD_log (NULL, "I am sorry\n");
  153:         }
  154:         AUD_log (NULL, "Context:\n");
  155: 
  156: #if defined AUDIO_BREAKPOINT_ON_BUG
  157: #  if defined HOST_I386
  158: #    if defined __GNUC__
  159:         __asm__ ("int3");
  160: #    elif defined _MSC_VER
  161:         _asm _emit 0xcc;
  162: #    else
  163:         abort ();
  164: #    endif
  165: #  else
  166:         abort ();
  167: #  endif
  168: #endif
  169:     }
  170: 
  171:     return cond;
  172: }
  173: #endif
  174: 
  175: static inline int audio_bits_to_index (int bits)
  176: {
  177:     switch (bits) {
  178:     case 8:
  179:         return 0;
  180: 
  181:     case 16:
  182:         return 1;
  183: 
  184:     case 32:
  185:         return 2;
  186: 
  187:     default:
  188:         audio_bug ("bits_to_index", 1);
  189:         AUD_log (NULL, "invalid bits %d\n", bits);
  190:         return 0;
  191:     }
  192: }
  193: 
  194: void *audio_calloc (const char *funcname, int nmemb, size_t size)
  195: {
  196:     int cond;
  197:     size_t len;
  198: 
  199:     len = nmemb * size;
  200:     cond = !nmemb || !size;
  201:     cond |= nmemb < 0;
  202:     cond |= len < size;
  203: 
  204:     if (audio_bug ("audio_calloc", cond)) {
  205:         AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
  206:                  funcname);
  207:         AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
  208:         return NULL;
  209:     }
  210: 
  211:     return qemu_mallocz (len);
  212: }
  213: 
  214: static char *audio_alloc_prefix (const char *s)
  215: {
  216:     const char qemu_prefix[] = "QEMU_";
  217:     size_t len;
  218:     char *r;
  219: 
  220:     if (!s) {
  221:         return NULL;
  222:     }
  223: 
  224:     len = strlen (s);
  225:     r = qemu_malloc (len + sizeof (qemu_prefix));
  226: 
  227:     if (r) {
  228:         size_t i;
  229:         char *u = r + sizeof (qemu_prefix) - 1;
  230: 
  231:         strcpy (r, qemu_prefix);
  232:         strcat (r, s);
  233: 
  234:         for (i = 0; i < len; ++i) {
  235:             u[i] = toupper (u[i]);
  236:         }
  237:     }
  238:     return r;
  239: }
  240: 
  241: static const char *audio_audfmt_to_string (audfmt_e fmt)
  242: {
  243:     switch (fmt) {
  244:     case AUD_FMT_U8:
  245:         return "U8";
  246: 
  247:     case AUD_FMT_U16:
  248:         return "U16";
  249: 
  250:     case AUD_FMT_S8:
  251:         return "S8";
  252: 
  253:     case AUD_FMT_S16:
  254:         return "S16";
  255: 
  256:     case AUD_FMT_U32:
  257:         return "U32";
  258: 
  259:     case AUD_FMT_S32:
  260:         return "S32";
  261:     }
  262: 
  263:     dolog ("Bogus audfmt %d returning S16\n", fmt);
  264:     return "S16";
  265: }
  266: 
  267: static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
  268:                                         int *defaultp)
  269: {
  270:     if (!strcasecmp (s, "u8")) {
  271:         *defaultp = 0;
  272:         return AUD_FMT_U8;
  273:     }
  274:     else if (!strcasecmp (s, "u16")) {
  275:         *defaultp = 0;
  276:         return AUD_FMT_U16;
  277:     }
  278:     else if (!strcasecmp (s, "u32")) {
  279:         *defaultp = 0;
  280:         return AUD_FMT_U32;
  281:     }
  282:     else if (!strcasecmp (s, "s8")) {
  283:         *defaultp = 0;
  284:         return AUD_FMT_S8;
  285:     }
  286:     else if (!strcasecmp (s, "s16")) {
  287:         *defaultp = 0;
  288:         return AUD_FMT_S16;
  289:     }
  290:     else if (!strcasecmp (s, "s32")) {
  291:         *defaultp = 0;
  292:         return AUD_FMT_S32;
  293:     }
  294:     else {
  295:         dolog ("Bogus audio format `%s' using %s\n",
  296:                s, audio_audfmt_to_string (defval));
  297:         *defaultp = 1;
  298:         return defval;
  299:     }
  300: }
  301: 
  302: static audfmt_e audio_get_conf_fmt (const char *envname,
  303:                                     audfmt_e defval,
  304:                                     int *defaultp)
  305: {
  306:     const char *var = getenv (envname);
  307:     if (!var) {
  308:         *defaultp = 1;
  309:         return defval;
  310:     }
  311:     return audio_string_to_audfmt (var, defval, defaultp);
  312: }
  313: 
  314: static int audio_get_conf_int (const char *key, int defval, int *defaultp)
  315: {
  316:     int val;
  317:     char *strval;
  318: 
  319:     strval = getenv (key);
  320:     if (strval) {
  321:         *defaultp = 0;
  322:         val = atoi (strval);
  323:         return val;
  324:     }
  325:     else {
  326:         *defaultp = 1;
  327:         return defval;
  328:     }
  329: }
  330: 
  331: static const char *audio_get_conf_str (const char *key,
  332:                                        const char *defval,
  333:                                        int *defaultp)
  334: {
  335:     const char *val = getenv (key);
  336:     if (!val) {
  337:         *defaultp = 1;
  338:         return defval;
  339:     }
  340:     else {
  341:         *defaultp = 0;
  342:         return val;
  343:     }
  344: }
  345: 
  346: void AUD_vlog (const char *cap, const char *fmt, va_list ap)
  347: {
  348:     if (conf.log_to_monitor) {
  349:         if (cap) {
  350:             term_printf ("%s: ", cap);
  351:         }
  352: 
  353:         term_vprintf (fmt, ap);
  354:     }
  355:     else {
  356:         if (cap) {
  357:             fprintf (stderr, "%s: ", cap);
  358:         }
  359: 
  360:         vfprintf (stderr, fmt, ap);
  361:     }
  362: }
  363: 
  364: void AUD_log (const char *cap, const char *fmt, ...)
  365: {
  366:     va_list ap;
  367: 
  368:     va_start (ap, fmt);
  369:     AUD_vlog (cap, fmt, ap);
  370:     va_end (ap);
  371: }
  372: 
  373: static void audio_print_options (const char *prefix,
  374:                                  struct audio_option *opt)
  375: {
  376:     char *uprefix;
  377: 
  378:     if (!prefix) {
  379:         dolog ("No prefix specified\n");
  380:         return;
  381:     }
  382: 
  383:     if (!opt) {
  384:         dolog ("No options\n");
  385:         return;
  386:     }
  387: 
  388:     uprefix = audio_alloc_prefix (prefix);
  389: 
  390:     for (; opt->name; opt++) {
  391:         const char *state = "default";
  392:         printf ("  %s_%s: ", uprefix, opt->name);
  393: 
  394:         if (opt->overriddenp && *opt->overriddenp) {
  395:             state = "current";
  396:         }
  397: 
  398:         switch (opt->tag) {
  399:         case AUD_OPT_BOOL:
  400:             {
  401:                 int *intp = opt->valp;
  402:                 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
  403:             }
  404:             break;
  405: 
  406:         case AUD_OPT_INT:
  407:             {
  408:                 int *intp = opt->valp;
  409:                 printf ("integer, %s = %d\n", state, *intp);
  410:             }
  411:             break;
  412: 
  413:         case AUD_OPT_FMT:
  414:             {
  415:                 audfmt_e *fmtp = opt->valp;
  416:                 printf (
  417:                     "format, %s = %s, (one of: U8 S8 U16 S16)\n",
  418:                     state,
  419:                     audio_audfmt_to_string (*fmtp)
  420:                     );
  421:             }
  422:             break;
  423: 
  424:         case AUD_OPT_STR:
  425:             {
  426:                 const char **strp = opt->valp;
  427:                 printf ("string, %s = %s\n",
  428:                         state,
  429:                         *strp ? *strp : "(not set)");
  430:             }
  431:             break;
  432: 
  433:         default:
  434:             printf ("???\n");
  435:             dolog ("Bad value tag for option %s_%s %d\n",
  436:                    uprefix, opt->name, opt->tag);
  437:             break;
  438:         }
  439:         printf ("    %s\n", opt->descr);
  440:     }
  441: 
  442:     qemu_free (uprefix);
  443: }
  444: 
  445: static void audio_process_options (const char *prefix,
  446:                                    struct audio_option *opt)
  447: {
  448:     char *optname;
  449:     const char qemu_prefix[] = "QEMU_";
  450:     size_t preflen;
  451: 
  452:     if (audio_bug (AUDIO_FUNC, !prefix)) {
  453:         dolog ("prefix = NULL\n");
  454:         return;
  455:     }
  456: 
  457:     if (audio_bug (AUDIO_FUNC, !opt)) {
  458:         dolog ("opt = NULL\n");
  459:         return;
  460:     }
  461: 
  462:     preflen = strlen (prefix);
  463: 
  464:     for (; opt->name; opt++) {
  465:         size_t len, i;
  466:         int def;
  467: 
  468:         if (!opt->valp) {
  469:             dolog ("Option value pointer for `%s' is not set\n",
  470:                    opt->name);
  471:             continue;
  472:         }
  473: 
  474:         len = strlen (opt->name);
  475:         /* len of opt->name + len of prefix + size of qemu_prefix
  476:          * (includes trailing zero) + zero + underscore (on behalf of
  477:          * sizeof) */
  478:         optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
  479:         if (!optname) {
  480:             dolog ("Could not allocate memory for option name `%s'\n",
  481:                    opt->name);
  482:             continue;
  483:         }
  484: 
  485:         strcpy (optname, qemu_prefix);
  486: 
  487:         /* copy while upper-casing, including trailing zero */
  488:         for (i = 0; i <= preflen; ++i) {
  489:             optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
  490:         }
  491:         strcat (optname, "_");
  492:         strcat (optname, opt->name);
  493: 
  494:         def = 1;
  495:         switch (opt->tag) {
  496:         case AUD_OPT_BOOL:
  497:         case AUD_OPT_INT:
  498:             {
  499:                 int *intp = opt->valp;
  500:                 *intp = audio_get_conf_int (optname, *intp, &def);
  501:             }
  502:             break;
  503: 
  504:         case AUD_OPT_FMT:
  505:             {
  506:                 audfmt_e *fmtp = opt->valp;
  507:                 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
  508:             }
  509:             break;
  510: 
  511:         case AUD_OPT_STR:
  512:             {
  513:                 const char **strp = opt->valp;
  514:                 *strp = audio_get_conf_str (optname, *strp, &def);
  515:             }
  516:             break;
  517: 
  518:         default:
  519:             dolog ("Bad value tag for option `%s' - %d\n",
  520:                    optname, opt->tag);
  521:             break;
  522:         }
  523: 
  524:         if (!opt->overriddenp) {
  525:             opt->overriddenp = &opt->overridden;
  526:         }
  527:         *opt->overriddenp = !def;
  528:         qemu_free (optname);
  529:     }
  530: }
  531: 
  532: static void audio_print_settings (audsettings_t *as)
  533: {
  534:     dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
  535: 
  536:     switch (as->fmt) {
  537:     case AUD_FMT_S8:
  538:         AUD_log (NULL, "S8");
  539:         break;
  540:     case AUD_FMT_U8:
  541:         AUD_log (NULL, "U8");
  542:         break;
  543:     case AUD_FMT_S16:
  544:         AUD_log (NULL, "S16");
  545:         break;
  546:     case AUD_FMT_U16:
  547:         AUD_log (NULL, "U16");
  548:         break;
  549:     default:
  550:         AUD_log (NULL, "invalid(%d)", as->fmt);
  551:         break;
  552:     }
  553: 
  554:     AUD_log (NULL, " endianness=");
  555:     switch (as->endianness) {
  556:     case 0:
  557:         AUD_log (NULL, "little");
  558:         break;
  559:     case 1:
  560:         AUD_log (NULL, "big");
  561:         break;
  562:     default:
  563:         AUD_log (NULL, "invalid");
  564:         break;
  565:     }
  566:     AUD_log (NULL, "\n");
  567: }
  568: 
  569: static int audio_validate_settings (audsettings_t *as)
  570: {
  571:     int invalid;
  572: 
  573:     invalid = as->nchannels != 1 && as->nchannels != 2;
  574:     invalid |= as->endianness != 0 && as->endianness != 1;
  575: 
  576:     switch (as->fmt) {
  577:     case AUD_FMT_S8:
  578:     case AUD_FMT_U8:
  579:     case AUD_FMT_S16:
  580:     case AUD_FMT_U16:
  581:     case AUD_FMT_S32:
  582:     case AUD_FMT_U32:
  583:         break;
  584:     default:
  585:         invalid = 1;
  586:         break;
  587:     }
  588: 
  589:     invalid |= as->freq <= 0;
  590:     return invalid ? -1 : 0;
  591: }
  592: 
  593: static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
  594: {
  595:     int bits = 8, sign = 0;
  596: 
  597:     switch (as->fmt) {
  598:     case AUD_FMT_S8:
  599:         sign = 1;
  600:     case AUD_FMT_U8:
  601:         break;
  602: 
  603:     case AUD_FMT_S16:
  604:         sign = 1;
  605:     case AUD_FMT_U16:
  606:         bits = 16;
  607:         break;
  608: 
  609:     case AUD_FMT_S32:
  610:         sign = 1;
  611:     case AUD_FMT_U32:
  612:         bits = 32;
  613:         break;
  614:     }
  615:     return info->freq == as->freq
  616:         && info->nchannels == as->nchannels
  617:         && info->sign == sign
  618:         && info->bits == bits
  619:         && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
  620: }
  621: 
  622: void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
  623: {
  624:     int bits = 8, sign = 0, shift = 0;
  625: 
  626:     switch (as->fmt) {
  627:     case AUD_FMT_S8:
  628: