audio.c 40.8 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU Audio subsystem
3 4 5
 *
 * Copyright (c) 2003-2005 Vassili Karpov (malc)
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"

26 27
#define AUDIO_CAP "audio"
#include "audio_int.h"
B
bellard 已提交
28

29 30 31
/* #define DEBUG_PLIVE */
/* #define DEBUG_LIVE */
/* #define DEBUG_OUT */
B
bellard 已提交
32

B
bellard 已提交
33 34
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static struct audio_driver *drvtab[] = {
#ifdef CONFIG_OSS
    &oss_audio_driver,
#endif
#ifdef CONFIG_ALSA
    &alsa_audio_driver,
#endif
#ifdef CONFIG_COREAUDIO
    &coreaudio_audio_driver,
#endif
#ifdef CONFIG_DSOUND
    &dsound_audio_driver,
#endif
#ifdef CONFIG_FMOD
    &fmod_audio_driver,
#endif
#ifdef CONFIG_SDL
    &sdl_audio_driver,
#endif
    &no_audio_driver,
    &wav_audio_driver
};
B
bellard 已提交
57

B
bellard 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
struct fixed_settings {
    int enabled;
    int nb_voices;
    int greedy;
    audsettings_t settings;
};

static struct {
    struct fixed_settings fixed_out;
    struct fixed_settings fixed_in;
    union {
        int hz;
        int64_t ticks;
    } period;
    int plive;
} conf = {
    {                           /* DAC fixed settings */
        1,                      /* enabled */
        1,                      /* nb_voices */
        1,                      /* greedy */
        {
            44100,              /* freq */
            2,                  /* nchannels */
            AUD_FMT_S16         /* fmt */
        }
    },

    {                           /* ADC fixed settings */
        1,                      /* enabled */
        1,                      /* nb_voices */
        1,                      /* greedy */
        {
            44100,              /* freq */
            2,                  /* nchannels */
            AUD_FMT_S16         /* fmt */
        }
    },

96 97 98 99
    { 0 },                      /* period */
    0                           /* plive */
};

B
bellard 已提交
100 101
static AudioState glob_audio_state;

102 103 104 105 106 107 108 109 110
volume_t nominal_volume = {
    0,
#ifdef FLOAT_MIXENG
    1.0,
    1.0
#else
    UINT_MAX,
    UINT_MAX
#endif
B
bellard 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
};

/* http://www.df.lth.se/~john_e/gems/gem002d.html */
/* http://www.multi-platforms.com/Tips/PopCount.htm */
uint32_t popcount (uint32_t u)
{
    u = ((u&0x55555555) + ((u>>1)&0x55555555));
    u = ((u&0x33333333) + ((u>>2)&0x33333333));
    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
    u = ( u&0x0000ffff) + (u>>16);
    return u;
}

inline uint32_t lsbindex (uint32_t u)
{
    return popcount ((u&-u)-1);
}

130 131 132 133
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
#error No its not
#else
int audio_bug (const char *funcname, int cond)
B
bellard 已提交
134
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    if (cond) {
        static int shown;

        AUD_log (NULL, "Error a bug that was just triggered in %s\n", funcname);
        if (!shown) {
            shown = 1;
            AUD_log (NULL, "Save all your work and restart without audio\n");
            AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
            AUD_log (NULL, "I am sorry\n");
        }
        AUD_log (NULL, "Context:\n");

#if defined AUDIO_BREAKPOINT_ON_BUG
#  if defined HOST_I386
#    if defined __GNUC__
        __asm__ ("int3");
#    elif defined _MSC_VER
        _asm _emit 0xcc;
#    else
        abort ();
#    endif
#  else
        abort ();
#  endif
#endif
B
bellard 已提交
160 161
    }

162
    return cond;
B
bellard 已提交
163
}
164
#endif
B
bellard 已提交
165

B
bellard 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void *audio_calloc (const char *funcname, int nmemb, size_t size)
{
    int cond;
    size_t len;

    len = nmemb * size;
    cond = !nmemb || !size;
    cond |= nmemb < 0;
    cond |= len < size;

    if (audio_bug ("audio_calloc", cond)) {
        AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
                 funcname);
        AUD_log (NULL, "nmemb=%d size=%d (len=%d)\n", nmemb, size, len);
        return NULL;
    }

    return qemu_mallocz (len);
}

186
static char *audio_alloc_prefix (const char *s)
B
bellard 已提交
187
{
188 189 190
    const char qemu_prefix[] = "QEMU_";
    size_t len;
    char *r;
B
bellard 已提交
191

192 193 194
    if (!s) {
        return NULL;
    }
B
bellard 已提交
195

196 197
    len = strlen (s);
    r = qemu_malloc (len + sizeof (qemu_prefix));
B
bellard 已提交
198

199 200 201
    if (r) {
        size_t i;
        char *u = r + sizeof (qemu_prefix) - 1;
B
bellard 已提交
202

203 204 205 206 207 208
        strcpy (r, qemu_prefix);
        strcat (r, s);

        for (i = 0; i < len; ++i) {
            u[i] = toupper (u[i]);
        }
B
bellard 已提交
209
    }
210
    return r;
B
bellard 已提交
211 212
}

213
const char *audio_audfmt_to_string (audfmt_e fmt)
B
bellard 已提交
214
{
215 216 217
    switch (fmt) {
    case AUD_FMT_U8:
        return "U8";
B
bellard 已提交
218

219 220
    case AUD_FMT_U16:
        return "U16";
B
bellard 已提交
221 222

    case AUD_FMT_S8:
223
        return "S8";
B
bellard 已提交
224 225

    case AUD_FMT_S16:
226
        return "S16";
B
bellard 已提交
227 228
    }

229 230
    dolog ("Bogus audfmt %d returning S16\n", fmt);
    return "S16";
B
bellard 已提交
231 232
}

233
audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
B
bellard 已提交
234
{
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    if (!strcasecmp (s, "u8")) {
        *defaultp = 0;
        return AUD_FMT_U8;
    }
    else if (!strcasecmp (s, "u16")) {
        *defaultp = 0;
        return AUD_FMT_U16;
    }
    else if (!strcasecmp (s, "s8")) {
        *defaultp = 0;
        return AUD_FMT_S8;
    }
    else if (!strcasecmp (s, "s16")) {
        *defaultp = 0;
        return AUD_FMT_S16;
    }
    else {
        dolog ("Bogus audio format `%s' using %s\n",
               s, audio_audfmt_to_string (defval));
        *defaultp = 1;
        return defval;
    }
B
bellard 已提交
257 258
}

259 260 261
static audfmt_e audio_get_conf_fmt (const char *envname,
                                    audfmt_e defval,
                                    int *defaultp)
B
bellard 已提交
262
{
263 264 265 266
    const char *var = getenv (envname);
    if (!var) {
        *defaultp = 1;
        return defval;
B
bellard 已提交
267
    }
268
    return audio_string_to_audfmt (var, defval, defaultp);
B
bellard 已提交
269 270
}

271
static int audio_get_conf_int (const char *key, int defval, int *defaultp)
B
bellard 已提交
272
{
273 274
    int val;
    char *strval;
B
bellard 已提交
275

276 277 278 279 280 281 282 283 284 285
    strval = getenv (key);
    if (strval) {
        *defaultp = 0;
        val = atoi (strval);
        return val;
    }
    else {
        *defaultp = 1;
        return defval;
    }
B
bellard 已提交
286 287
}

288 289 290
static const char *audio_get_conf_str (const char *key,
                                       const char *defval,
                                       int *defaultp)
B
bellard 已提交
291
{
292 293 294 295 296 297 298 299
    const char *val = getenv (key);
    if (!val) {
        *defaultp = 1;
        return defval;
    }
    else {
        *defaultp = 0;
        return val;
B
bellard 已提交
300 301 302
    }
}

303
void AUD_log (const char *cap, const char *fmt, ...)
B
bellard 已提交
304
{
305 306 307
    va_list ap;
    if (cap) {
        fprintf (stderr, "%s: ", cap);
B
bellard 已提交
308
    }
309 310 311
    va_start (ap, fmt);
    vfprintf (stderr, fmt, ap);
    va_end (ap);
B
bellard 已提交
312 313
}

314
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
B
bellard 已提交
315
{
316 317
    if (cap) {
        fprintf (stderr, "%s: ", cap);
B
bellard 已提交
318
    }
319
    vfprintf (stderr, fmt, ap);
B
bellard 已提交
320 321
}

322 323
static void audio_print_options (const char *prefix,
                                 struct audio_option *opt)
B
bellard 已提交
324
{
325 326 327 328 329 330 331 332 333
    char *uprefix;

    if (!prefix) {
        dolog ("No prefix specified\n");
        return;
    }

    if (!opt) {
        dolog ("No options\n");
B
bellard 已提交
334
        return;
335
    }
B
bellard 已提交
336

337
    uprefix = audio_alloc_prefix (prefix);
B
bellard 已提交
338

339 340 341
    for (; opt->name; opt++) {
        const char *state = "default";
        printf ("  %s_%s: ", uprefix, opt->name);
B
bellard 已提交
342

343 344 345
        if (opt->overridenp && *opt->overridenp) {
            state = "current";
        }
B
bellard 已提交
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        switch (opt->tag) {
        case AUD_OPT_BOOL:
            {
                int *intp = opt->valp;
                printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
            }
            break;

        case AUD_OPT_INT:
            {
                int *intp = opt->valp;
                printf ("integer, %s = %d\n", state, *intp);
            }
            break;

        case AUD_OPT_FMT:
            {
                audfmt_e *fmtp = opt->valp;
                printf (
                    "format, %s = %s, (one of: U8 S8 U16 S16)\n",
                    state,
                    audio_audfmt_to_string (*fmtp)
                    );
            }
            break;

        case AUD_OPT_STR:
            {
                const char **strp = opt->valp;
                printf ("string, %s = %s\n",
                        state,
                        *strp ? *strp : "(not set)");
B
bellard 已提交
379
            }
380 381 382 383 384 385 386
            break;

        default:
            printf ("???\n");
            dolog ("Bad value tag for option %s_%s %d\n",
                   uprefix, opt->name, opt->tag);
            break;
B
bellard 已提交
387
        }
388
        printf ("    %s\n", opt->descr);
B
bellard 已提交
389
    }
390 391

    qemu_free (uprefix);
B
bellard 已提交
392 393
}

394 395
static void audio_process_options (const char *prefix,
                                   struct audio_option *opt)
B
bellard 已提交
396
{
397 398 399
    char *optname;
    const char qemu_prefix[] = "QEMU_";
    size_t preflen;
B
bellard 已提交
400

401 402 403 404
    if (audio_bug (AUDIO_FUNC, !prefix)) {
        dolog ("prefix = NULL\n");
        return;
    }
B
bellard 已提交
405

406 407 408
    if (audio_bug (AUDIO_FUNC, !opt)) {
        dolog ("opt = NULL\n");
        return;
B
bellard 已提交
409 410
    }

411
    preflen = strlen (prefix);
B
bellard 已提交
412

413 414 415 416 417 418 419 420 421 422 423
    for (; opt->name; opt++) {
        size_t len, i;
        int def;

        if (!opt->valp) {
            dolog ("Option value pointer for `%s' is not set\n",
                   opt->name);
            continue;
        }

        len = strlen (opt->name);
B
bellard 已提交
424 425 426
        /* len of opt->name + len of prefix + size of qemu_prefix
         * (includes trailing zero) + zero + underscore (on behalf of
         * sizeof) */
427 428
        optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
        if (!optname) {
B
bellard 已提交
429
            dolog ("Could not allocate memory for option name `%s'\n",
430 431 432 433 434
                   opt->name);
            continue;
        }

        strcpy (optname, qemu_prefix);
B
bellard 已提交
435 436

        /* copy while upper-casing, including trailing zero */
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        for (i = 0; i <= preflen; ++i) {
            optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
        }
        strcat (optname, "_");
        strcat (optname, opt->name);

        def = 1;
        switch (opt->tag) {
        case AUD_OPT_BOOL:
        case AUD_OPT_INT:
            {
                int *intp = opt->valp;
                *intp = audio_get_conf_int (optname, *intp, &def);
            }
            break;

        case AUD_OPT_FMT:
            {
                audfmt_e *fmtp = opt->valp;
                *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
            }
            break;

        case AUD_OPT_STR:
            {
                const char **strp = opt->valp;
                *strp = audio_get_conf_str (optname, *strp, &def);
            }
            break;

        default:
            dolog ("Bad value tag for option `%s' - %d\n",
                   optname, opt->tag);
B
bellard 已提交
470 471 472
            break;
        }

473 474 475 476 477 478
        if (!opt->overridenp) {
            opt->overridenp = &opt->overriden;
        }
        *opt->overridenp = !def;
        qemu_free (optname);
    }
B
bellard 已提交
479 480
}

B
bellard 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
static void audio_print_settings (audsettings_t *as)
{
    dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);

    switch (as->fmt) {
    case AUD_FMT_S8:
        AUD_log (NULL, "S8");
        break;
    case AUD_FMT_U8:
        AUD_log (NULL, "U8");
        break;
    case AUD_FMT_S16:
        AUD_log (NULL, "S16");
        break;
    case AUD_FMT_U16:
        AUD_log (NULL, "U16");
        break;
    default:
        AUD_log (NULL, "invalid(%d)", as->fmt);
        break;
    }
    AUD_log (NULL, "\n");
}

static int audio_validate_settigs (audsettings_t *as)
{
    int invalid;

    invalid = as->nchannels != 1 && as->nchannels != 2;

    switch (as->fmt) {
    case AUD_FMT_S8:
    case AUD_FMT_U8:
    case AUD_FMT_S16:
    case AUD_FMT_U16:
        break;
    default:
        invalid = 1;
        break;
    }

    invalid |= as->freq <= 0;

    if (invalid) {
        return -1;
    }
    return 0;
}

static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
B
bellard 已提交
531
{
532
    int bits = 8, sign = 0;
B
bellard 已提交
533

B
bellard 已提交
534
    switch (as->fmt) {
535 536 537 538 539 540 541 542 543 544
    case AUD_FMT_S8:
        sign = 1;
    case AUD_FMT_U8:
        break;

    case AUD_FMT_S16:
        sign = 1;
    case AUD_FMT_U16:
        bits = 16;
        break;
B
bellard 已提交
545
    }
B
bellard 已提交
546 547
    return info->freq == as->freq
        && info->nchannels == as->nchannels
548 549 550
        && info->sign == sign
        && info->bits == bits;
}
B
bellard 已提交
551

B
bellard 已提交
552 553 554 555 556
void audio_pcm_init_info (
    struct audio_pcm_info *info,
    audsettings_t *as,
    int swap_endian
    )
557 558 559
{
    int bits = 8, sign = 0;

B
bellard 已提交
560
    switch (as->fmt) {
B
bellard 已提交
561 562 563 564 565 566 567 568 569 570 571 572
    case AUD_FMT_S8:
        sign = 1;
    case AUD_FMT_U8:
        break;

    case AUD_FMT_S16:
        sign = 1;
    case AUD_FMT_U16:
        bits = 16;
        break;
    }

B
bellard 已提交
573
    info->freq = as->freq;
574 575
    info->bits = bits;
    info->sign = sign;
B
bellard 已提交
576 577
    info->nchannels = as->nchannels;
    info->shift = (as->nchannels == 2) + (bits == 16);
578 579 580
    info->align = (1 << info->shift) - 1;
    info->bytes_per_second = info->freq << info->shift;
    info->swap_endian = swap_endian;
B
bellard 已提交
581 582
}

583
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
B
bellard 已提交
584
{
585 586 587 588 589 590
    if (!len) {
        return;
    }

    if (info->sign) {
        memset (buf, len << info->shift, 0x00);
B
bellard 已提交
591 592
    }
    else {
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        if (info->bits == 8) {
            memset (buf, len << info->shift, 0x80);
        }
        else {
            int i;
            uint16_t *p = buf;
            int shift = info->nchannels - 1;
            short s = INT16_MAX;

            if (info->swap_endian) {
                s = bswap16 (s);
            }

            for (i = 0; i < len << shift; i++) {
                p[i] = s;
            }
        }
B
bellard 已提交
610 611 612
    }
}

613 614 615 616
/*
 * Hard voice (capture)
 */
static void audio_pcm_hw_free_resources_in (HWVoiceIn *hw)
B
bellard 已提交
617
{
618 619
    if (hw->conv_buf) {
        qemu_free (hw->conv_buf);
B
bellard 已提交
620
    }
621
    hw->conv_buf = NULL;
B
bellard 已提交
622 623
}

624
static int audio_pcm_hw_alloc_resources_in (HWVoiceIn *hw)
B
bellard 已提交
625
{
B
bellard 已提交
626
    hw->conv_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
627
    if (!hw->conv_buf) {
B
bellard 已提交
628 629
        dolog ("Could not allocate ADC conversion buffer (%d bytes)\n",
               hw->samples * sizeof (st_sample_t));
630
        return -1;
B
bellard 已提交
631
    }
632
    return 0;
B
bellard 已提交
633 634
}

B
bellard 已提交
635
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
B
bellard 已提交
636
{
637 638 639 640 641 642 643
    SWVoiceIn *sw;
    int m = hw->total_samples_captured;

    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
        if (sw->active) {
            m = audio_MIN (m, sw->total_hw_samples_acquired);
        }
B
bellard 已提交
644
    }
645
    return m;
B
bellard 已提交
646 647
}

648
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
B
bellard 已提交
649
{
650 651 652 653
    int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
        return 0;
B
bellard 已提交
654
    }
655
    return live;
B
bellard 已提交
656 657
}

658 659 660 661
/*
 * Soft voice (capture)
 */
static void audio_pcm_sw_free_resources_in (SWVoiceIn *sw)
B
bellard 已提交
662
{
663 664
    if (sw->conv_buf) {
        qemu_free (sw->conv_buf);
B
bellard 已提交
665 666
    }

667 668
    if (sw->rate) {
        st_rate_stop (sw->rate);
B
bellard 已提交
669 670
    }

671 672
    sw->conv_buf = NULL;
    sw->rate = NULL;
B
bellard 已提交
673 674
}

675
static int audio_pcm_sw_alloc_resources_in (SWVoiceIn *sw)
B
bellard 已提交
676
{
677
    int samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
B
bellard 已提交
678
    sw->conv_buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
679
    if (!sw->conv_buf) {
B
bellard 已提交
680 681
        dolog ("Could not allocate buffer for `%s' (%d bytes)\n",
               SW_NAME (sw), samples * sizeof (st_sample_t));
B
bellard 已提交
682
        return -1;
683
    }
B
bellard 已提交
684

685 686 687 688 689 690
    sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
    if (!sw->rate) {
        qemu_free (sw->conv_buf);
        sw->conv_buf = NULL;
        return -1;
    }
B
bellard 已提交
691 692 693
    return 0;
}

B
bellard 已提交
694 695 696 697 698 699
static int audio_pcm_sw_init_in (
    SWVoiceIn *sw,
    HWVoiceIn *hw,
    const char *name,
    audsettings_t *as
    )
B
bellard 已提交
700
{
B
bellard 已提交
701 702 703
    /* None of the cards emulated by QEMU are big-endian
       hence following shortcut */
    audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
704 705
    sw->hw = hw;
    sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
B
bellard 已提交
706

707 708
    sw->clip =
        mixeng_clip
B
bellard 已提交
709
        [sw->info.nchannels == 2]
710 711 712
        [sw->info.sign]
        [sw->info.swap_endian]
        [sw->info.bits == 16];
B
bellard 已提交
713

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    sw->name = qemu_strdup (name);
    audio_pcm_sw_free_resources_in (sw);
    return audio_pcm_sw_alloc_resources_in (sw);
}

static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
{
    HWVoiceIn *hw = sw->hw;
    int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
    int rpos;

    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
        return 0;
    }

    rpos = hw->wpos - live;
    if (rpos >= 0) {
        return rpos;
B
bellard 已提交
733 734
    }
    else {
735
        return hw->samples + rpos;
B
bellard 已提交
736 737 738
    }
}

739
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
B
bellard 已提交
740
{
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    HWVoiceIn *hw = sw->hw;
    int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
    st_sample_t *src, *dst = sw->conv_buf;

    rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;

    live = hw->total_samples_captured - sw->total_hw_samples_acquired;
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
        dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
        return 0;
    }

    samples = size >> sw->info.shift;
    if (!live) {
        return 0;
    }
B
bellard 已提交
757

758 759
    swlim = (live * sw->ratio) >> 32;
    swlim = audio_MIN (swlim, samples);
B
bellard 已提交
760

761 762 763 764 765 766 767
    while (swlim) {
        src = hw->conv_buf + rpos;
        isamp = hw->wpos - rpos;
        /* XXX: <= ? */
        if (isamp <= 0) {
            isamp = hw->samples - rpos;
        }
B
bellard 已提交
768

769 770 771 772
        if (!isamp) {
            break;
        }
        osamp = swlim;
B
bellard 已提交
773

774 775
        if (audio_bug (AUDIO_FUNC, osamp < 0)) {
            dolog ("osamp=%d\n", osamp);
B
bellard 已提交
776
            return 0;
777
        }
B
bellard 已提交
778

779 780 781 782 783 784 785
        st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
        swlim -= osamp;
        rpos = (rpos + isamp) % hw->samples;
        dst += osamp;
        ret += osamp;
        total += isamp;
    }
B
bellard 已提交
786

787 788 789
    sw->clip (buf, sw->conv_buf, ret);
    sw->total_hw_samples_acquired += total;
    return ret << sw->info.shift;
B
bellard 已提交
790 791
}

792 793 794 795 796 797 798
/*
 * Hard voice (playback)
 */
static void audio_pcm_hw_free_resources_out (HWVoiceOut *hw)
{
    if (hw->mix_buf) {
        qemu_free (hw->mix_buf);
B
bellard 已提交
799 800
    }

801 802 803 804 805
    hw->mix_buf = NULL;
}

static int audio_pcm_hw_alloc_resources_out (HWVoiceOut *hw)
{
B
bellard 已提交
806
    hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
807
    if (!hw->mix_buf) {
B
bellard 已提交
808 809
        dolog ("Could not allocate DAC mixing buffer (%d bytes)\n",
               hw->samples * sizeof (st_sample_t));
810
        return -1;
B
bellard 已提交
811 812
    }

813 814 815
    return 0;
}

B
bellard 已提交
816
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
817
{
B
bellard 已提交
818 819 820
    SWVoiceOut *sw;
    int m = INT_MAX;
    int nb_live = 0;
B
bellard 已提交
821

B
bellard 已提交
822 823 824 825 826
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
        if (sw->active || !sw->empty) {
            m = audio_MIN (m, sw->total_hw_samples_mixed);
            nb_live += 1;
        }
B
bellard 已提交
827
    }
B
bellard 已提交
828 829 830

    *nb_livep = nb_live;
    return m;
831
}
B
bellard 已提交
832

833 834 835
int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
{
    int smin;
B
bellard 已提交
836

837 838 839 840
    smin = audio_pcm_hw_find_min_out (hw, nb_live);

    if (!*nb_live) {
        return 0;
B
bellard 已提交
841 842
    }
    else {
843 844 845 846 847
        int live = smin;

        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
            return 0;
B
bellard 已提交
848
        }
849
        return live;
B
bellard 已提交
850
    }
851 852 853 854 855 856
}

int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
{
    int nb_live;
    int live;
B
bellard 已提交
857

858 859 860 861
    live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
        return 0;
B
bellard 已提交
862
    }
863
    return live;
B
bellard 已提交
864 865
}

866 867 868 869
/*
 * Soft voice (playback)
 */
static void audio_pcm_sw_free_resources_out (SWVoiceOut *sw)
B
bellard 已提交
870
{
871 872 873
    if (sw->buf) {
        qemu_free (sw->buf);
    }
B
bellard 已提交
874

875 876
    if (sw->rate) {
        st_rate_stop (sw->rate);
B
bellard 已提交
877
    }
878 879 880

    sw->buf = NULL;
    sw->rate = NULL;
B
bellard 已提交
881 882
}

883
static int audio_pcm_sw_alloc_resources_out (SWVoiceOut *sw)
B
bellard 已提交
884
{
B
bellard 已提交
885
    sw->buf = audio_calloc (AUDIO_FUNC, sw->hw->samples, sizeof (st_sample_t));
886
    if (!sw->buf) {
B
bellard 已提交
887 888
        dolog ("Could not allocate buffer for `%s' (%d bytes)\n",
               SW_NAME (sw), sw->hw->samples * sizeof (st_sample_t));
889 890
        return -1;
    }
B
bellard 已提交
891

892 893 894 895 896 897 898
    sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
    if (!sw->rate) {
        qemu_free (sw->buf);
        sw->buf = NULL;
        return -1;
    }
    return 0;
B
bellard 已提交
899 900
}

B
bellard 已提交
901 902 903 904 905 906
static int audio_pcm_sw_init_out (
    SWVoiceOut *sw,
    HWVoiceOut *hw,
    const char *name,
    audsettings_t *as
    )
B
bellard 已提交
907
{
B
bellard 已提交
908 909 910
    /* None of the cards emulated by QEMU are big-endian
       hence following shortcut */
    audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
911 912 913 914 915 916 917 918
    sw->hw = hw;
    sw->empty = 1;
    sw->active = 0;
    sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
    sw->total_hw_samples_mixed = 0;

    sw->conv =
        mixeng_conv
B
bellard 已提交
919
        [sw->info.nchannels == 2]
920 921 922 923
        [sw->info.sign]
        [sw->info.swap_endian]
        [sw->info.bits == 16];
    sw->name = qemu_strdup (name);
B
bellard 已提交
924

925 926
    audio_pcm_sw_free_resources_out (sw);
    return audio_pcm_sw_alloc_resources_out (sw);
B
bellard 已提交
927 928
}

929
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
B
bellard 已提交
930
{
931 932
    int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
    int ret = 0, pos = 0, total = 0;
B
bellard 已提交
933

934 935 936
    if (!sw) {
        return size;
    }
B
bellard 已提交
937

938
    hwsamples = sw->hw->samples;
B
bellard 已提交
939

940 941 942 943 944
    live = sw->total_hw_samples_mixed;
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
        dolog ("live=%d hw->samples=%d\n", live, hwsamples);
        return 0;
    }
B
bellard 已提交
945

946 947 948
    if (live == hwsamples) {
        return 0;
    }
B
bellard 已提交
949

950 951
    wpos = (sw->hw->rpos + live) % hwsamples;
    samples = size >> sw->info.shift;
B
bellard 已提交
952

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    dead = hwsamples - live;
    swlim = ((int64_t) dead << 32) / sw->ratio;
    swlim = audio_MIN (swlim, samples);
    if (swlim) {
        sw->conv (sw->buf, buf, swlim, &sw->vol);
    }

    while (swlim) {
        dead = hwsamples - live;
        left = hwsamples - wpos;
        blck = audio_MIN (dead, left);
        if (!blck) {
            break;
        }
        isamp = swlim;
        osamp = blck;
        st_rate_flow_mix (
            sw->rate,
            sw->buf + pos,
            sw->hw->mix_buf + wpos,
            &isamp,
            &osamp
            );
        ret += isamp;
        swlim -= isamp;
        pos += isamp;
        live += osamp;
        wpos = (wpos + osamp) % hwsamples;
        total += osamp;
    }

    sw->total_hw_samples_mixed += total;
    sw->empty = sw->total_hw_samples_mixed == 0;

#ifdef DEBUG_OUT
    dolog (
B
bellard 已提交
989 990
        "%s: write size %d ret %d total sw %d\n",
        SW_NAME (sw),
991 992
        size >> sw->info.shift,
        ret,
B
bellard 已提交
993
        sw->total_hw_samples_mixed
994 995 996 997
        );
#endif

    return ret << sw->info.shift;
B
bellard 已提交
998 999
}

1000 1001
#ifdef DEBUG_AUDIO
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
B
bellard 已提交
1002
{
1003 1004
    dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
           cap, info->bits, info->sign, info->freq, info->nchannels);
B
bellard 已提交
1005
}
1006
#endif
B
bellard 已提交
1007

1008 1009 1010 1011 1012 1013
#define DAC
#include "audio_template.h"
#undef DAC
#include "audio_template.h"

int AUD_write (SWVoiceOut *sw, void *buf, int size)
B
bellard 已提交
1014
{
1015
    int bytes;
B
bellard 已提交
1016

1017 1018 1019 1020
    if (!sw) {
        /* XXX: Consider options */
        return size;
    }
B
bellard 已提交
1021

1022
    if (!sw->hw->enabled) {
B
bellard 已提交
1023
        dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
B
bellard 已提交
1024 1025 1026
        return 0;
    }

1027 1028 1029 1030 1031 1032 1033
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
    return bytes;
}

int AUD_read (SWVoiceIn *sw, void *buf, int size)
{
    int bytes;
B
bellard 已提交
1034

1035 1036 1037
    if (!sw) {
        /* XXX: Consider options */
        return size;
B
bellard 已提交
1038
    }
1039 1040

    if (!sw->hw->enabled) {
B
bellard 已提交
1041
        dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1042
        return 0;
B
bellard 已提交
1043
    }
1044 1045 1046

    bytes = sw->hw->pcm_ops->read (sw, buf, size);
    return bytes;
B
bellard 已提交
1047 1048
}

1049
int AUD_get_buffer_size_out (SWVoiceOut *sw)
B
bellard 已提交
1050
{
B
bellard 已提交
1051
    return sw->hw->samples << sw->hw->info.shift;
1052 1053 1054 1055 1056
}

void AUD_set_active_out (SWVoiceOut *sw, int on)
{
    HWVoiceOut *hw;
B
bellard 已提交
1057

1058
    if (!sw) {
B
bellard 已提交
1059
        return;
1060
    }
B
bellard 已提交
1061 1062

    hw = sw->hw;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    if (sw->active != on) {
        SWVoiceOut *temp_sw;

        if (on) {
            int total;

            hw->pending_disable = 0;
            if (!hw->enabled) {
                hw->enabled = 1;
                hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
            }

            if (sw->empty) {
                total = 0;
            }
        }
        else {
            if (hw->enabled) {
                int nb_active = 0;

                for (temp_sw = hw->sw_head.lh_first; temp_sw;
                     temp_sw = temp_sw->entries.le_next) {
                    nb_active += temp_sw->active != 0;
                }

                hw->pending_disable = nb_active == 1;
            }
B
bellard 已提交
1090
        }
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        sw->active = on;
    }
}

void AUD_set_active_in (SWVoiceIn *sw, int on)
{
    HWVoiceIn *hw;

    if (!sw) {
        return;
B
bellard 已提交
1101 1102
    }

1103
    hw = sw->hw;
B
bellard 已提交
1104
    if (sw->active != on) {
1105 1106
        SWVoiceIn *temp_sw;

B
bellard 已提交
1107 1108 1109
        if (on) {
            if (!hw->enabled) {
                hw->enabled = 1;
1110
                hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
B
bellard 已提交
1111
            }
1112
            sw->total_hw_samples_acquired = hw->total_samples_captured;
B
bellard 已提交
1113 1114
        }
        else {
1115
            if (hw->enabled) {
B
bellard 已提交
1116
                int nb_active = 0;
1117 1118 1119 1120

                for (temp_sw = hw->sw_head.lh_first; temp_sw;
                     temp_sw = temp_sw->entries.le_next) {
                    nb_active += temp_sw->active != 0;
B
bellard 已提交
1121 1122 1123
                }

                if (nb_active == 1) {
1124 1125
                    hw->enabled = 0;
                    hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
B
bellard 已提交
1126 1127 1128 1129 1130 1131 1132
                }
            }
        }
        sw->active = on;
    }
}

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
static int audio_get_avail (SWVoiceIn *sw)
{
    int live;

    if (!sw) {
        return 0;
    }

    live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
        return 0;
    }

    ldebug (
        "%s: get_avail live %d ret %lld\n",
B
bellard 已提交
1149
        SW_NAME (sw),
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
        live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
        );

    return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
}

static int audio_get_free (SWVoiceOut *sw)
{
    int live, dead;

    if (!sw) {
        return 0;
    }

    live = sw->total_hw_samples_mixed;

    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
B
bellard 已提交
1168
        return 0;
1169 1170 1171 1172 1173 1174
    }

    dead = sw->hw->samples - live;

#ifdef DEBUG_OUT
    dolog ("%s: get_free live %d dead %d ret %lld\n",
B
bellard 已提交
1175
           SW_NAME (sw),
1176
           live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
B
bellard 已提交
1177
#endif
1178 1179 1180 1181

    return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
}

B
bellard 已提交
1182
static void audio_run_out (AudioState *s)
1183 1184 1185 1186
{
    HWVoiceOut *hw = NULL;
    SWVoiceOut *sw;

B
bellard 已提交
1187
    while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1188
        int played;
B
bellard 已提交
1189
        int live, free, nb_live, cleanup_required;
1190 1191 1192 1193 1194

        live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
        if (!nb_live) {
            live = 0;
        }
B
bellard 已提交
1195

1196 1197
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
B
bellard 已提交
1198
            continue;
1199 1200 1201 1202 1203
        }

        if (hw->pending_disable && !nb_live) {
#ifdef DEBUG_OUT
            dolog ("Disabling voice\n");
B
bellard 已提交
1204
#endif
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
            hw->enabled = 0;
            hw->pending_disable = 0;
            hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
            continue;
        }

        if (!live) {
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
                if (sw->active) {
                    free = audio_get_free (sw);
                    if (free > 0) {
                        sw->callback.fn (sw->callback.opaque, free);
                    }
                }
            }
            continue;
        }

        played = hw->pcm_ops->run_out (hw);
        if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
            dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
                   hw->rpos, hw->samples, played);
            hw->rpos = 0;
        }

#ifdef DEBUG_OUT
B
bellard 已提交
1231
        dolog ("played=%d\n", played);
B
bellard 已提交
1232
#endif
1233 1234 1235 1236 1237

        if (played) {
            hw->ts_helper += played;
        }

B
bellard 已提交
1238
        cleanup_required = 0;
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
            if (!sw->active && sw->empty) {
                continue;
            }

            if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
                dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
                       played, sw->total_hw_samples_mixed);
                played = sw->total_hw_samples_mixed;
            }

            sw->total_hw_samples_mixed -= played;

            if (!sw->total_hw_samples_mixed) {
                sw->empty = 1;
B
bellard 已提交
1254
                cleanup_required |= !sw->active && !sw->callback.fn;
1255 1256 1257 1258 1259 1260 1261 1262 1263
            }

            if (sw->active) {
                free = audio_get_free (sw);
                if (free > 0) {
                    sw->callback.fn (sw->callback.opaque, free);
                }
            }
        }
B
bellard 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

        if (cleanup_required) {
        restart:
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
                if (!sw->active && !sw->callback.fn) {
#ifdef DEBUG_PLIVE
                    dolog ("Finishing with old voice\n");
#endif
                    audio_close_out (s, sw);
                    goto restart; /* play it safe */
                }
            }
        }
1277 1278 1279
    }
}

B
bellard 已提交
1280
static void audio_run_in (AudioState *s)
1281 1282 1283
{
    HWVoiceIn *hw = NULL;

B
bellard 已提交
1284
    while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        SWVoiceIn *sw;
        int captured, min;

        captured = hw->pcm_ops->run_in (hw);

        min = audio_pcm_hw_find_min_in (hw);
        hw->total_samples_captured += captured - min;
        hw->ts_helper += captured;

        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
            sw->total_hw_samples_acquired -= min;

            if (sw->active) {
                int avail;

                avail = audio_get_avail (sw);
                if (avail > 0) {
                    sw->callback.fn (sw->callback.opaque, avail);
                }
            }
        }
    }
}

static struct audio_option audio_options[] = {
    /* DAC */
B
bellard 已提交
1311
    {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1312 1313
     "Use fixed settings for host DAC", NULL, 0},

B
bellard 已提交
1314
    {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1315 1316
     "Frequency for fixed host DAC", NULL, 0},

B
bellard 已提交
1317
    {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1318 1319
     "Format for fixed host DAC", NULL, 0},

B
bellard 已提交
1320
    {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1321 1322
     "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},

B
bellard 已提交
1323
    {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1324 1325 1326
     "Number of voices for DAC", NULL, 0},

    /* ADC */
B
bellard 已提交
1327
    {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1328 1329
     "Use fixed settings for host ADC", NULL, 0},

B
bellard 已提交
1330 1331
    {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
     "Frequency for fixed host ADC", NULL, 0},
1332

B
bellard 已提交
1333 1334
    {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
     "Format for fixed host ADC", NULL, 0},
1335

B
bellard 已提交
1336
    {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1337 1338
     "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},

B
bellard 已提交
1339
    {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1340 1341 1342
     "Number of voices for ADC", NULL, 0},

    /* Misc */
B
bellard 已提交
1343 1344
    {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
     "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1345

B
bellard 已提交
1346
    {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1347 1348 1349
     "(undocumented)", NULL, 0},

    {NULL, 0, NULL, NULL, NULL, 0}
B
bellard 已提交
1350 1351
};

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
void AUD_help (void)
{
    size_t i;

    audio_process_options ("AUDIO", audio_options);
    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
        struct audio_driver *d = drvtab[i];
        if (d->options) {
            audio_process_options (d->name, d->options);
        }
    }

    printf ("Audio options:\n");
    audio_print_options ("AUDIO", audio_options);
    printf ("\n");

    printf ("Available drivers:\n");

    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
        struct audio_driver *d = drvtab[i];

        printf ("Name: %s\n", d->name);
        printf ("Description: %s\n", d->descr);

        switch (d->max_voices_out) {
        case 0:
            printf ("Does not support DAC\n");
            break;
        case 1:
            printf ("One DAC voice\n");
            break;
        case INT_MAX:
            printf ("Theoretically supports many DAC voices\n");
            break;
        default:
            printf ("Theoretically supports upto %d DAC voices\n",
                     d->max_voices_out);
            break;
        }

        switch (d->max_voices_in) {
        case 0:
            printf ("Does not support ADC\n");
            break;
        case 1:
            printf ("One ADC voice\n");
            break;
        case INT_MAX:
            printf ("Theoretically supports many ADC voices\n");
            break;
        default:
            printf ("Theoretically supports upto %d ADC voices\n",
                     d->max_voices_in);
            break;
        }

        if (d->options) {
            printf ("Options:\n");
            audio_print_options (d->name, d->options);
        }
        else {
            printf ("No options\n");
        }
        printf ("\n");
    }

    printf (
        "Options are settable through environment variables.\n"
        "Example:\n"
#ifdef _WIN32
        "  set QEMU_AUDIO_DRV=wav\n"
        "  set QEMU_WAV_PATH=c:/tune.wav\n"
#else
        "  export QEMU_AUDIO_DRV=wav\n"
        "  export QEMU_WAV_PATH=$HOME/tune.wav\n"
        "(for csh replace export with setenv in the above)\n"
#endif
        "  qemu ...\n\n"
        );
}

void audio_timer (void *opaque)
{
    AudioState *s = opaque;

B
bellard 已提交
1437 1438
    audio_run_out (s);
    audio_run_in (s);
1439

B
bellard 已提交
1440
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1441 1442
}

B
bellard 已提交
1443
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
B
bellard 已提交
1444
{
1445 1446 1447
    if (drv->options) {
        audio_process_options (drv->name, drv->options);
    }
B
bellard 已提交
1448
    s->drv_opaque = drv->init ();
1449

B
bellard 已提交
1450 1451
    if (s->drv_opaque) {
        if (s->nb_hw_voices_out > drv->max_voices_out) {
1452 1453 1454 1455 1456 1457 1458 1459
            if (!drv->max_voices_out) {
                dolog ("`%s' does not support DAC\n", drv->name);
            }
            else {
                dolog (
                    "`%s' does not support %d multiple DAC voicess\n"
                    "Resetting to %d\n",
                    drv->name,
B
bellard 已提交
1460
                    s->nb_hw_voices_out,
1461 1462 1463
                    drv->max_voices_out
                    );
            }
B
bellard 已提交
1464
            s->nb_hw_voices_out = drv->max_voices_out;
B
bellard 已提交
1465
        }
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479


        if (!drv->voice_size_in && drv->max_voices_in) {
            ldebug ("warning: No ADC voice size defined for `%s'\n",
                    drv->name);
            drv->max_voices_in = 0;
        }

        if (!drv->voice_size_out && drv->max_voices_out) {
            ldebug ("warning: No DAC voice size defined for `%s'\n",
                    drv->name);
        }

        if (drv->voice_size_in && !drv->max_voices_in) {
B
bellard 已提交
1480 1481
            ldebug ("warning: `%s' ADC voice size %d, zero voices \n",
                    drv->name, drv->voice_size_out);
1482 1483 1484
        }

        if (drv->voice_size_out && !drv->max_voices_out) {
B
bellard 已提交
1485 1486
            ldebug ("warning: `%s' DAC voice size %d, zero voices \n",
                    drv->name, drv->voice_size_in);
1487 1488
        }

B
bellard 已提交
1489
        if (s->nb_hw_voices_in > drv->max_voices_in) {
1490 1491 1492 1493 1494 1495 1496 1497
            if (!drv->max_voices_in) {
                ldebug ("`%s' does not support ADC\n", drv->name);
            }
            else {
                dolog (
                    "`%s' does not support %d multiple ADC voices\n"
                    "Resetting to %d\n",
                    drv->name,
B
bellard 已提交
1498
                    s->nb_hw_voices_in,
1499 1500 1501
                    drv->max_voices_in
                    );
            }
B
bellard 已提交
1502
            s->nb_hw_voices_in = drv->max_voices_in;
1503 1504
        }

B
bellard 已提交
1505 1506 1507
        LIST_INIT (&s->hw_head_out);
        LIST_INIT (&s->hw_head_in);
        s->drv = drv;
1508
        return 0;
B
bellard 已提交
1509 1510
    }
    else {
1511 1512
        dolog ("Could not init `%s' audio driver\n", drv->name);
        return -1;
B
bellard 已提交
1513 1514 1515 1516 1517
    }
}

static void audio_vm_stop_handler (void *opaque, int reason)
{
B
bellard 已提交
1518
    AudioState *s = opaque;
1519 1520 1521 1522
    HWVoiceOut *hwo = NULL;
    HWVoiceIn *hwi = NULL;
    int op = reason ? VOICE_ENABLE : VOICE_DISABLE;

B
bellard 已提交
1523
    while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1524 1525 1526 1527 1528 1529 1530 1531
        if (!hwo->pcm_ops) {
            continue;
        }

        if (hwo->enabled != reason) {
            hwo->pcm_ops->ctl_out (hwo, op);
        }
    }
B
bellard 已提交
1532

B
bellard 已提交
1533
    while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1534
        if (!hwi->pcm_ops) {
B
bellard 已提交
1535
            continue;
1536
        }
B
bellard 已提交
1537

1538 1539 1540
        if (hwi->enabled != reason) {
            hwi->pcm_ops->ctl_in (hwi, op);
        }
B
bellard 已提交
1541 1542 1543 1544 1545
    }
}

static void audio_atexit (void)
{
B
bellard 已提交
1546
    AudioState *s = &glob_audio_state;
1547 1548 1549
    HWVoiceOut *hwo = NULL;
    HWVoiceIn *hwi = NULL;

B
bellard 已提交
1550
    while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1551 1552 1553 1554 1555 1556 1557 1558 1559
        if (!hwo->pcm_ops) {
            continue;
        }

        if (hwo->enabled) {
            hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
        }
        hwo->pcm_ops->fini_out (hwo);
    }
B
bellard 已提交
1560

B
bellard 已提交
1561
    while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1562
        if (!hwi->pcm_ops) {
B
bellard 已提交
1563
            continue;
1564
        }
B
bellard 已提交
1565

1566 1567 1568 1569
        if (hwi->enabled) {
            hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
        }
        hwi->pcm_ops->fini_in (hwi);
B
bellard 已提交
1570
    }
B
bellard 已提交
1571 1572 1573 1574

    if (s->drv) {
        s->drv->fini (s->drv_opaque);
    }
B
bellard 已提交
1575 1576 1577 1578
}

static void audio_save (QEMUFile *f, void *opaque)
{
1579 1580
    (void) f;
    (void) opaque;
B
bellard 已提交
1581 1582 1583 1584
}

static int audio_load (QEMUFile *f, void *opaque, int version_id)
{
1585 1586 1587 1588
    (void) f;
    (void) opaque;

    if (version_id != 1) {
B
bellard 已提交
1589
        return -EINVAL;
1590
    }
B
bellard 已提交
1591 1592 1593 1594

    return 0;
}

B
bellard 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
{
    card->audio = s;
    card->name = qemu_strdup (name);
    memset (&card->entries, 0, sizeof (card->entries));
    LIST_INSERT_HEAD (&s->card_head, card, entries);
}

void AUD_remove_card (QEMUSoundCard *card)
{
    LIST_REMOVE (card, entries);
    card->audio = NULL;
    qemu_free (card->name);
}

AudioState *AUD_init (void)
B
bellard 已提交
1611
{
1612
    size_t i;
B
bellard 已提交
1613 1614
    int done = 0;
    const char *drvname;
B
bellard 已提交
1615
    AudioState *s = &glob_audio_state;
1616 1617 1618

    audio_process_options ("AUDIO", audio_options);

B
bellard 已提交
1619 1620 1621
    s->nb_hw_voices_out = conf.fixed_out.nb_voices;
    s->nb_hw_voices_in = conf.fixed_in.nb_voices;

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
    if (s->nb_hw_voices_out <= 0) {
        dolog ("Bogus number of DAC voices %d\n",
               s->nb_hw_voices_out);
        s->nb_hw_voices_out = 1;
    }

    if (s->nb_hw_voices_in <= 0) {
        dolog ("Bogus number of ADC voices %d\n",
               s->nb_hw_voices_in);
        s->nb_hw_voices_in = 1;
    }
B
bellard 已提交
1633

1634 1635 1636 1637
    {
        int def;
        drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
    }
B
bellard 已提交
1638

1639 1640
    s->ts = qemu_new_timer (vm_clock, audio_timer, s);
    if (!s->ts) {
B
bellard 已提交
1641 1642
        dolog ("Could not create audio timer\n");
        return NULL;
B
bellard 已提交
1643 1644 1645 1646
    }

    if (drvname) {
        int found = 0;
1647

B
bellard 已提交
1648 1649
        for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
            if (!strcmp (drvname, drvtab[i]->name)) {
B
bellard 已提交
1650
                done = !audio_driver_init (s, drvtab[i]);
B
bellard 已提交
1651 1652 1653 1654
                found = 1;
                break;
            }
        }
1655

B
bellard 已提交
1656 1657
        if (!found) {
            dolog ("Unknown audio driver `%s'\n", drvname);
1658
            dolog ("Run with -audio-help to list available drivers\n");
B
bellard 已提交
1659 1660 1661 1662 1663
        }
    }

    if (!done) {
        for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1664
            if (drvtab[i]->can_be_default) {
B
bellard 已提交
1665
                done = !audio_driver_init (s, drvtab[i]);
1666
            }
B
bellard 已提交
1667 1668 1669 1670
        }
    }

    if (!done) {
B
bellard 已提交
1671 1672 1673
        done = !audio_driver_init (s, &no_audio_driver);
        if (!done) {
            dolog ("Could not initialize audio subsystem\n");
1674 1675
        }
        else {
B
bellard 已提交
1676
            dolog ("warning: Using timer based audio emulation\n");
1677
        }
B
bellard 已提交
1678
    }
1679

B
bellard 已提交
1680 1681 1682 1683 1684 1685 1686 1687
    if (done) {
        if (conf.period.hz <= 0) {
            if (conf.period.hz < 0) {
                dolog ("warning: Timer period is negative - %d "
                       "treating as zero\n",
                       conf.period.hz);
            }
            conf.period.ticks = 1;
1688
        }
B
bellard 已提交
1689 1690 1691 1692 1693
        else {
            conf.period.ticks = ticks_per_sec / conf.period.hz;
        }

        qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
1694 1695
    }
    else {
B
bellard 已提交
1696 1697
        qemu_del_timer (s->ts);
        return NULL;
1698 1699
    }

B
bellard 已提交
1700 1701 1702 1703 1704
    LIST_INIT (&s->card_head);
    register_savevm ("audio", 0, 1, audio_save, audio_load, s);
    atexit (audio_atexit);
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
    return s;
B
bellard 已提交
1705
}