audio.c 50.5 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
 * 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.
 */
P
pbrook 已提交
24 25
#include "hw/hw.h"
#include "audio.h"
A
aliguori 已提交
26
#include "monitor.h"
P
pbrook 已提交
27 28
#include "qemu-timer.h"
#include "sysemu.h"
B
bellard 已提交
29

30 31
#define AUDIO_CAP "audio"
#include "audio_int.h"
B
bellard 已提交
32

33 34 35
/* #define DEBUG_PLIVE */
/* #define DEBUG_LIVE */
/* #define DEBUG_OUT */
36
/* #define DEBUG_CAPTURE */
M
malc 已提交
37
/* #define DEBUG_POLL */
B
bellard 已提交
38

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

41 42 43 44 45

/* Order of CONFIG_AUDIO_DRIVERS is import.
   The 1st one is the one used by default, that is the reason
    that we generate the list.
*/
46
static struct audio_driver *drvtab[] = {
47
    CONFIG_AUDIO_DRIVERS
48 49 50
    &no_audio_driver,
    &wav_audio_driver
};
B
bellard 已提交
51

B
bellard 已提交
52 53 54 55
struct fixed_settings {
    int enabled;
    int nb_voices;
    int greedy;
M
malc 已提交
56
    struct audsettings settings;
B
bellard 已提交
57 58 59 60 61 62
};

static struct {
    struct fixed_settings fixed_out;
    struct fixed_settings fixed_in;
    union {
M
malc 已提交
63
        int hertz;
B
bellard 已提交
64 65 66
        int64_t ticks;
    } period;
    int plive;
B
bellard 已提交
67
    int log_to_monitor;
M
malc 已提交
68 69
    int try_poll_in;
    int try_poll_out;
B
bellard 已提交
70
} conf = {
71 72 73 74 75 76 77 78 79
    .fixed_out = { /* DAC fixed settings */
        .enabled = 1,
        .nb_voices = 1,
        .greedy = 1,
        .settings = {
            .freq = 44100,
            .nchannels = 2,
            .fmt = AUD_FMT_S16,
            .endianness =  AUDIO_HOST_ENDIANNESS,
B
bellard 已提交
80 81 82
        }
    },

83 84 85 86 87 88 89 90 91
    .fixed_in = { /* ADC fixed settings */
        .enabled = 1,
        .nb_voices = 1,
        .greedy = 1,
        .settings = {
            .freq = 44100,
            .nchannels = 2,
            .fmt = AUD_FMT_S16,
            .endianness = AUDIO_HOST_ENDIANNESS,
B
bellard 已提交
92 93 94
        }
    },

M
malc 已提交
95
    .period = { .hertz = 250 },
96 97
    .plive = 0,
    .log_to_monitor = 0,
M
malc 已提交
98 99
    .try_poll_in = 1,
    .try_poll_out = 1,
100 101
};

B
bellard 已提交
102 103
static AudioState glob_audio_state;

M
malc 已提交
104
struct mixeng_volume nominal_volume = {
105
    .mute = 0,
106
#ifdef FLOAT_MIXENG
107 108
    .r = 1.0,
    .l = 1.0,
109
#else
110 111
    .r = 1ULL << 32,
    .l = 1ULL << 32,
112
#endif
B
bellard 已提交
113 114
};

115 116 117 118
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
#error No its not
#else
int audio_bug (const char *funcname, int cond)
B
bellard 已提交
119
{
120 121 122
    if (cond) {
        static int shown;

123
        AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
124 125 126
        if (!shown) {
            shown = 1;
            AUD_log (NULL, "Save all your work and restart without audio\n");
M
malc 已提交
127
            AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            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 已提交
145 146
    }

147
    return cond;
B
bellard 已提交
148
}
149
#endif
B
bellard 已提交
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static inline int audio_bits_to_index (int bits)
{
    switch (bits) {
    case 8:
        return 0;

    case 16:
        return 1;

    case 32:
        return 2;

    default:
        audio_bug ("bits_to_index", 1);
        AUD_log (NULL, "invalid bits %d\n", bits);
        return 0;
    }
}

B
bellard 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
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);
B
bellard 已提交
183
        AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
B
bellard 已提交
184 185 186 187 188 189
        return NULL;
    }

    return qemu_mallocz (len);
}

190
static char *audio_alloc_prefix (const char *s)
B
bellard 已提交
191
{
192
    const char qemu_prefix[] = "QEMU_";
193 194
    size_t len, i;
    char *r, *u;
B
bellard 已提交
195

196 197 198
    if (!s) {
        return NULL;
    }
B
bellard 已提交
199

200
    len = strlen (s);
201
    r = qemu_malloc (len + sizeof (qemu_prefix));
B
bellard 已提交
202

203
    u = r + sizeof (qemu_prefix) - 1;
B
bellard 已提交
204

205 206
    pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
    pstrcat (r, len + sizeof (qemu_prefix), s);
207

208 209
    for (i = 0; i < len; ++i) {
        u[i] = qemu_toupper(u[i]);
B
bellard 已提交
210
    }
211

212
    return r;
B
bellard 已提交
213 214
}

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

221 222
    case AUD_FMT_U16:
        return "U16";
B
bellard 已提交
223 224

    case AUD_FMT_S8:
225
        return "S8";
B
bellard 已提交
226 227

    case AUD_FMT_S16:
228
        return "S16";
229 230 231 232 233 234

    case AUD_FMT_U32:
        return "U32";

    case AUD_FMT_S32:
        return "S32";
B
bellard 已提交
235 236
    }

237 238
    dolog ("Bogus audfmt %d returning S16\n", fmt);
    return "S16";
B
bellard 已提交
239 240
}

241 242
static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
                                        int *defaultp)
B
bellard 已提交
243
{
244 245 246 247 248 249 250 251
    if (!strcasecmp (s, "u8")) {
        *defaultp = 0;
        return AUD_FMT_U8;
    }
    else if (!strcasecmp (s, "u16")) {
        *defaultp = 0;
        return AUD_FMT_U16;
    }
252 253 254 255
    else if (!strcasecmp (s, "u32")) {
        *defaultp = 0;
        return AUD_FMT_U32;
    }
256 257 258 259 260 261 262 263
    else if (!strcasecmp (s, "s8")) {
        *defaultp = 0;
        return AUD_FMT_S8;
    }
    else if (!strcasecmp (s, "s16")) {
        *defaultp = 0;
        return AUD_FMT_S16;
    }
264 265 266 267
    else if (!strcasecmp (s, "s32")) {
        *defaultp = 0;
        return AUD_FMT_S32;
    }
268 269 270 271 272 273
    else {
        dolog ("Bogus audio format `%s' using %s\n",
               s, audio_audfmt_to_string (defval));
        *defaultp = 1;
        return defval;
    }
B
bellard 已提交
274 275
}

276 277 278
static audfmt_e audio_get_conf_fmt (const char *envname,
                                    audfmt_e defval,
                                    int *defaultp)
B
bellard 已提交
279
{
280 281 282 283
    const char *var = getenv (envname);
    if (!var) {
        *defaultp = 1;
        return defval;
B
bellard 已提交
284
    }
285
    return audio_string_to_audfmt (var, defval, defaultp);
B
bellard 已提交
286 287
}

288
static int audio_get_conf_int (const char *key, int defval, int *defaultp)
B
bellard 已提交
289
{
290 291
    int val;
    char *strval;
B
bellard 已提交
292

293 294 295 296 297 298 299 300 301 302
    strval = getenv (key);
    if (strval) {
        *defaultp = 0;
        val = atoi (strval);
        return val;
    }
    else {
        *defaultp = 1;
        return defval;
    }
B
bellard 已提交
303 304
}

305 306 307
static const char *audio_get_conf_str (const char *key,
                                       const char *defval,
                                       int *defaultp)
B
bellard 已提交
308
{
309 310 311 312 313 314 315 316
    const char *val = getenv (key);
    if (!val) {
        *defaultp = 1;
        return defval;
    }
    else {
        *defaultp = 0;
        return val;
B
bellard 已提交
317 318 319
    }
}

B
bellard 已提交
320
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
B
bellard 已提交
321
{
B
bellard 已提交
322 323
    if (conf.log_to_monitor) {
        if (cap) {
A
aliguori 已提交
324
            monitor_printf(cur_mon, "%s: ", cap);
B
bellard 已提交
325 326
        }

A
aliguori 已提交
327
        monitor_vprintf(cur_mon, fmt, ap);
B
bellard 已提交
328 329 330 331 332 333 334
    }
    else {
        if (cap) {
            fprintf (stderr, "%s: ", cap);
        }

        vfprintf (stderr, fmt, ap);
B
bellard 已提交
335 336 337
    }
}

B
bellard 已提交
338
void AUD_log (const char *cap, const char *fmt, ...)
B
bellard 已提交
339
{
B
bellard 已提交
340 341 342 343 344
    va_list ap;

    va_start (ap, fmt);
    AUD_vlog (cap, fmt, ap);
    va_end (ap);
B
bellard 已提交
345 346
}

347 348
static void audio_print_options (const char *prefix,
                                 struct audio_option *opt)
B
bellard 已提交
349
{
350 351 352 353 354 355 356 357 358
    char *uprefix;

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

    if (!opt) {
        dolog ("No options\n");
B
bellard 已提交
359
        return;
360
    }
B
bellard 已提交
361

362
    uprefix = audio_alloc_prefix (prefix);
B
bellard 已提交
363

364 365 366
    for (; opt->name; opt++) {
        const char *state = "default";
        printf ("  %s_%s: ", uprefix, opt->name);
B
bellard 已提交
367

T
ths 已提交
368
        if (opt->overriddenp && *opt->overriddenp) {
369 370
            state = "current";
        }
B
bellard 已提交
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        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 (
391
                    "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
392 393 394 395 396 397 398 399 400 401 402 403
                    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 已提交
404
            }
405 406 407 408 409 410 411
            break;

        default:
            printf ("???\n");
            dolog ("Bad value tag for option %s_%s %d\n",
                   uprefix, opt->name, opt->tag);
            break;
B
bellard 已提交
412
        }
413
        printf ("    %s\n", opt->descr);
B
bellard 已提交
414
    }
415 416

    qemu_free (uprefix);
B
bellard 已提交
417 418
}

419 420
static void audio_process_options (const char *prefix,
                                   struct audio_option *opt)
B
bellard 已提交
421
{
422 423
    char *optname;
    const char qemu_prefix[] = "QEMU_";
B
blueswir1 已提交
424
    size_t preflen, optlen;
B
bellard 已提交
425

426 427 428 429
    if (audio_bug (AUDIO_FUNC, !prefix)) {
        dolog ("prefix = NULL\n");
        return;
    }
B
bellard 已提交
430

431 432 433
    if (audio_bug (AUDIO_FUNC, !opt)) {
        dolog ("opt = NULL\n");
        return;
B
bellard 已提交
434 435
    }

436
    preflen = strlen (prefix);
B
bellard 已提交
437

438 439 440 441 442 443 444 445 446 447 448
    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 已提交
449 450 451
        /* len of opt->name + len of prefix + size of qemu_prefix
         * (includes trailing zero) + zero + underscore (on behalf of
         * sizeof) */
B
blueswir1 已提交
452 453
        optlen = len + preflen + sizeof (qemu_prefix) + 1;
        optname = qemu_malloc (optlen);
454

B
blueswir1 已提交
455
        pstrcpy (optname, optlen, qemu_prefix);
B
bellard 已提交
456 457

        /* copy while upper-casing, including trailing zero */
458
        for (i = 0; i <= preflen; ++i) {
459
            optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
460
        }
B
blueswir1 已提交
461 462
        pstrcat (optname, optlen, "_");
        pstrcat (optname, optlen, opt->name);
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

        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 已提交
491 492 493
            break;
        }

T
ths 已提交
494 495
        if (!opt->overriddenp) {
            opt->overriddenp = &opt->overridden;
496
        }
T
ths 已提交
497
        *opt->overriddenp = !def;
498 499
        qemu_free (optname);
    }
B
bellard 已提交
500 501
}

M
malc 已提交
502
static void audio_print_settings (struct audsettings *as)
B
bellard 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
{
    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;
M
malc 已提交
519 520 521 522 523 524
    case AUD_FMT_S32:
        AUD_log (NULL, "S32");
        break;
    case AUD_FMT_U32:
        AUD_log (NULL, "U32");
        break;
B
bellard 已提交
525 526 527 528
    default:
        AUD_log (NULL, "invalid(%d)", as->fmt);
        break;
    }
B
bellard 已提交
529 530

    AUD_log (NULL, " endianness=");
B
bellard 已提交
531 532 533 534 535 536 537 538 539 540 541
    switch (as->endianness) {
    case 0:
        AUD_log (NULL, "little");
        break;
    case 1:
        AUD_log (NULL, "big");
        break;
    default:
        AUD_log (NULL, "invalid");
        break;
    }
B
bellard 已提交
542 543 544
    AUD_log (NULL, "\n");
}

M
malc 已提交
545
static int audio_validate_settings (struct audsettings *as)
B
bellard 已提交
546 547 548 549
{
    int invalid;

    invalid = as->nchannels != 1 && as->nchannels != 2;
B
bellard 已提交
550
    invalid |= as->endianness != 0 && as->endianness != 1;
B
bellard 已提交
551 552 553 554 555 556

    switch (as->fmt) {
    case AUD_FMT_S8:
    case AUD_FMT_U8:
    case AUD_FMT_S16:
    case AUD_FMT_U16:
557 558
    case AUD_FMT_S32:
    case AUD_FMT_U32:
B
bellard 已提交
559 560 561 562 563 564 565
        break;
    default:
        invalid = 1;
        break;
    }

    invalid |= as->freq <= 0;
B
bellard 已提交
566
    return invalid ? -1 : 0;
B
bellard 已提交
567 568
}

M
malc 已提交
569
static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
B
bellard 已提交
570
{
571
    int bits = 8, sign = 0;
B
bellard 已提交
572

B
bellard 已提交
573
    switch (as->fmt) {
574 575 576 577 578 579 580 581 582 583
    case AUD_FMT_S8:
        sign = 1;
    case AUD_FMT_U8:
        break;

    case AUD_FMT_S16:
        sign = 1;
    case AUD_FMT_U16:
        bits = 16;
        break;
584 585 586 587 588 589

    case AUD_FMT_S32:
        sign = 1;
    case AUD_FMT_U32:
        bits = 32;
        break;
B
bellard 已提交
590
    }
B
bellard 已提交
591 592
    return info->freq == as->freq
        && info->nchannels == as->nchannels
593
        && info->sign == sign
B
bellard 已提交
594 595
        && info->bits == bits
        && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
596
}
B
bellard 已提交
597

M
malc 已提交
598
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
599
{
600
    int bits = 8, sign = 0, shift = 0;
601

B
bellard 已提交
602
    switch (as->fmt) {
B
bellard 已提交
603 604 605 606 607 608 609 610 611
    case AUD_FMT_S8:
        sign = 1;
    case AUD_FMT_U8:
        break;

    case AUD_FMT_S16:
        sign = 1;
    case AUD_FMT_U16:
        bits = 16;
612 613 614 615 616 617 618 619
        shift = 1;
        break;

    case AUD_FMT_S32:
        sign = 1;
    case AUD_FMT_U32:
        bits = 32;
        shift = 2;
B
bellard 已提交
620 621 622
        break;
    }

B
bellard 已提交
623
    info->freq = as->freq;
624 625
    info->bits = bits;
    info->sign = sign;
B
bellard 已提交
626
    info->nchannels = as->nchannels;
627
    info->shift = (as->nchannels == 2) + shift;
628 629
    info->align = (1 << info->shift) - 1;
    info->bytes_per_second = info->freq << info->shift;
B
bellard 已提交
630
    info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
B
bellard 已提交
631 632
}

633
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
B
bellard 已提交
634
{
635 636 637 638 639
    if (!len) {
        return;
    }

    if (info->sign) {
B
bellard 已提交
640
        memset (buf, 0x00, len << info->shift);
B
bellard 已提交
641 642
    }
    else {
643 644
        switch (info->bits) {
        case 8:
B
bellard 已提交
645
            memset (buf, 0x80, len << info->shift);
646
            break;
647

648 649 650 651 652 653 654 655 656 657 658 659 660 661
        case 16:
            {
                int i;
                uint16_t *p = buf;
                int shift = info->nchannels - 1;
                short s = INT16_MAX;

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

                for (i = 0; i < len << shift; i++) {
                    p[i] = s;
                }
662
            }
663 664 665 666 667 668 669 670 671 672 673 674
            break;

        case 32:
            {
                int i;
                uint32_t *p = buf;
                int shift = info->nchannels - 1;
                int32_t s = INT32_MAX;

                if (info->swap_endianness) {
                    s = bswap32 (s);
                }
675

676 677 678
                for (i = 0; i < len << shift; i++) {
                    p[i] = s;
                }
679
            }
680 681 682 683 684 685
            break;

        default:
            AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
                     info->bits);
            break;
686
        }
B
bellard 已提交
687 688 689
    }
}

690 691 692
/*
 * Capture
 */
M
malc 已提交
693 694
static void noop_conv (struct st_sample *dst, const void *src,
                       int samples, struct mixeng_volume *vol)
695 696 697 698 699 700 701 702
{
    (void) src;
    (void) dst;
    (void) samples;
    (void) vol;
}

static CaptureVoiceOut *audio_pcm_capture_find_specific (
M
malc 已提交
703
    struct audsettings *as
704 705 706
    )
{
    CaptureVoiceOut *cap;
707
    AudioState *s = &glob_audio_state;
708 709

    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
B
bellard 已提交
710
        if (audio_pcm_info_eq (&cap->hw.info, as)) {
711 712 713 714 715 716
            return cap;
        }
    }
    return NULL;
}

B
bellard 已提交
717
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
718
{
B
bellard 已提交
719 720 721 722 723 724 725 726 727
    struct capture_callback *cb;

#ifdef DEBUG_CAPTURE
    dolog ("notification %d sent\n", cmd);
#endif
    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
        cb->ops.notify (cb->opaque, cmd);
    }
}
728

B
bellard 已提交
729 730 731 732
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
{
    if (cap->hw.enabled != enabled) {
        audcnotification_e cmd;
733
        cap->hw.enabled = enabled;
B
bellard 已提交
734 735
        cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
        audio_notify_capture (cap, cmd);
736 737 738 739 740 741 742 743 744
    }
}

static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
{
    HWVoiceOut *hw = &cap->hw;
    SWVoiceOut *sw;
    int enabled = 0;

B
bellard 已提交
745
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
746 747 748 749 750
        if (sw->active) {
            enabled = 1;
            break;
        }
    }
B
bellard 已提交
751
    audio_capture_maybe_changed (cap, enabled);
752 753 754 755
}

static void audio_detach_capture (HWVoiceOut *hw)
{
B
bellard 已提交
756 757 758 759 760 761 762
    SWVoiceCap *sc = hw->cap_head.lh_first;

    while (sc) {
        SWVoiceCap *sc1 = sc->entries.le_next;
        SWVoiceOut *sw = &sc->sw;
        CaptureVoiceOut *cap = sc->cap;
        int was_active = sw->active;
763 764 765 766 767 768

        if (sw->rate) {
            st_rate_stop (sw->rate);
            sw->rate = NULL;
        }

B
Blue Swirl 已提交
769 770
        QLIST_REMOVE (sw, entries);
        QLIST_REMOVE (sc, entries);
B
bellard 已提交
771 772 773 774 775 776 777 778
        qemu_free (sc);
        if (was_active) {
            /* We have removed soft voice from the capture:
               this might have changed the overall status of the capture
               since this might have been the only active voice */
            audio_recalc_and_notify_capture (cap);
        }
        sc = sc1;
779 780 781
    }
}

782
static int audio_attach_capture (HWVoiceOut *hw)
783
{
784
    AudioState *s = &glob_audio_state;
785 786 787 788
    CaptureVoiceOut *cap;

    audio_detach_capture (hw);
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
B
bellard 已提交
789
        SWVoiceCap *sc;
790
        SWVoiceOut *sw;
B
bellard 已提交
791
        HWVoiceOut *hw_cap = &cap->hw;
792

B
bellard 已提交
793 794
        sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
        if (!sc) {
795
            dolog ("Could not allocate soft capture voice (%zu bytes)\n",
B
bellard 已提交
796
                   sizeof (*sc));
797 798 799
            return -1;
        }

B
bellard 已提交
800 801
        sc->cap = cap;
        sw = &sc->sw;
802
        sw->hw = hw_cap;
B
bellard 已提交
803
        sw->info = hw->info;
804 805 806 807 808 809 810 811 812 813
        sw->empty = 1;
        sw->active = hw->enabled;
        sw->conv = noop_conv;
        sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
        sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
        if (!sw->rate) {
            dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
            qemu_free (sw);
            return -1;
        }
B
Blue Swirl 已提交
814 815
        QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
        QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
B
bellard 已提交
816 817 818 819 820
#ifdef DEBUG_CAPTURE
        asprintf (&sw->name, "for %p %d,%d,%d",
                  hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
        dolog ("Added %s active = %d\n", sw->name, sw->active);
#endif
821
        if (sw->active) {
B
bellard 已提交
822
            audio_capture_maybe_changed (cap, 1);
823 824 825 826 827
        }
    }
    return 0;
}

828 829 830
/*
 * Hard voice (capture)
 */
B
bellard 已提交
831
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
B
bellard 已提交
832
{
833 834 835 836 837 838 839
    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 已提交
840
    }
841
    return m;
B
bellard 已提交
842 843
}

844
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
B
bellard 已提交
845
{
846 847 848 849
    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 已提交
850
    }
851
    return live;
B
bellard 已提交
852 853
}

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
                           int live, int pending)
{
    int left = hw->samples - pending;
    int len = audio_MIN (left, live);
    int clipped = 0;

    while (len) {
        struct st_sample *src = hw->mix_buf + hw->rpos;
        uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
        int samples_till_end_of_buf = hw->samples - hw->rpos;
        int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);

        hw->clip (dst, src, samples_to_clip);

        hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
        len -= samples_to_clip;
        clipped += samples_to_clip;
    }
    return clipped;
}

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
/*
 * Soft voice (capture)
 */
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 已提交
893 894
    }
    else {
895
        return hw->samples + rpos;
B
bellard 已提交
896 897 898
    }
}

899
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
B
bellard 已提交
900
{
901 902
    HWVoiceIn *hw = sw->hw;
    int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
M
malc 已提交
903
    struct st_sample *src, *dst = sw->buf;
904 905 906 907 908 909 910 911 912 913 914 915 916

    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 已提交
917

918 919
    swlim = (live * sw->ratio) >> 32;
    swlim = audio_MIN (swlim, samples);
B
bellard 已提交
920

921 922 923 924 925 926 927
    while (swlim) {
        src = hw->conv_buf + rpos;
        isamp = hw->wpos - rpos;
        /* XXX: <= ? */
        if (isamp <= 0) {
            isamp = hw->samples - rpos;
        }
B
bellard 已提交
928

929 930 931 932
        if (!isamp) {
            break;
        }
        osamp = swlim;
B
bellard 已提交
933

934 935
        if (audio_bug (AUDIO_FUNC, osamp < 0)) {
            dolog ("osamp=%d\n", osamp);
B
bellard 已提交
936
            return 0;
937
        }
B
bellard 已提交
938

939 940 941 942 943 944 945
        st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
        swlim -= osamp;
        rpos = (rpos + isamp) % hw->samples;
        dst += osamp;
        ret += osamp;
        total += isamp;
    }
B
bellard 已提交
946

B
bellard 已提交
947
    sw->clip (buf, sw->buf, ret);
948 949
    sw->total_hw_samples_acquired += total;
    return ret << sw->info.shift;
B
bellard 已提交
950 951
}

952 953 954
/*
 * Hard voice (playback)
 */
B
bellard 已提交
955
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
956
{
B
bellard 已提交
957 958 959
    SWVoiceOut *sw;
    int m = INT_MAX;
    int nb_live = 0;
B
bellard 已提交
960

B
bellard 已提交
961 962 963 964 965
    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 已提交
966
    }
B
bellard 已提交
967 968 969

    *nb_livep = nb_live;
    return m;
970
}
B
bellard 已提交
971

M
malc 已提交
972
static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
973 974
{
    int smin;
M
malc 已提交
975
    int nb_live1;
B
bellard 已提交
976

M
malc 已提交
977 978 979
    smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
    if (nb_live) {
        *nb_live = nb_live1;
B
bellard 已提交
980
    }
M
malc 已提交
981 982

    if (nb_live1) {
983 984 985 986 987
        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 已提交
988
        }
989
        return live;
B
bellard 已提交
990
    }
M
malc 已提交
991
    return 0;
B
bellard 已提交
992 993
}

994 995 996 997
/*
 * Soft voice (playback)
 */
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
B
bellard 已提交
998
{
999 1000
    int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
    int ret = 0, pos = 0, total = 0;
B
bellard 已提交
1001

1002 1003 1004
    if (!sw) {
        return size;
    }
B
bellard 已提交
1005

1006
    hwsamples = sw->hw->samples;
B
bellard 已提交
1007

1008 1009 1010 1011 1012
    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 已提交
1013

1014
    if (live == hwsamples) {
B
bellard 已提交
1015 1016 1017
#ifdef DEBUG_OUT
        dolog ("%s is full %d\n", sw->name, live);
#endif
1018 1019
        return 0;
    }
B
bellard 已提交
1020

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

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    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 已提交
1060 1061
        "%s: write size %d ret %d total sw %d\n",
        SW_NAME (sw),
1062 1063
        size >> sw->info.shift,
        ret,
B
bellard 已提交
1064
        sw->total_hw_samples_mixed
1065 1066 1067 1068
        );
#endif

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

1071 1072
#ifdef DEBUG_AUDIO
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
B
bellard 已提交
1073
{
1074 1075
    dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
           cap, info->bits, info->sign, info->freq, info->nchannels);
B
bellard 已提交
1076
}
1077
#endif
B
bellard 已提交
1078

1079 1080 1081 1082 1083
#define DAC
#include "audio_template.h"
#undef DAC
#include "audio_template.h"

M
malc 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
/*
 * Timer
 */
static void audio_timer (void *opaque)
{
    AudioState *s = opaque;

    audio_run ("timer");
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
}


static int audio_is_timer_needed (void)
{
    HWVoiceIn *hwi = NULL;
    HWVoiceOut *hwo = NULL;

    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
        if (!hwo->poll_mode) return 1;
    }
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
        if (!hwi->poll_mode) return 1;
    }
    return 0;
}

static void audio_reset_timer (void)
{
    AudioState *s = &glob_audio_state;

    if (audio_is_timer_needed ()) {
        qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);
    }
    else {
        qemu_del_timer (s->ts);
    }
}

/*
 * Public API
 */
1125
int AUD_write (SWVoiceOut *sw, void *buf, int size)
B
bellard 已提交
1126
{
1127
    int bytes;
B
bellard 已提交
1128

1129 1130 1131 1132
    if (!sw) {
        /* XXX: Consider options */
        return size;
    }
B
bellard 已提交
1133

1134
    if (!sw->hw->enabled) {
B
bellard 已提交
1135
        dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
B
bellard 已提交
1136 1137 1138
        return 0;
    }

1139 1140 1141 1142 1143 1144 1145
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
    return bytes;
}

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

1147 1148 1149
    if (!sw) {
        /* XXX: Consider options */
        return size;
B
bellard 已提交
1150
    }
1151 1152

    if (!sw->hw->enabled) {
B
bellard 已提交
1153
        dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1154
        return 0;
B
bellard 已提交
1155
    }
1156 1157 1158

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

1161
int AUD_get_buffer_size_out (SWVoiceOut *sw)
B
bellard 已提交
1162
{
B
bellard 已提交
1163
    return sw->hw->samples << sw->hw->info.shift;
1164 1165 1166 1167 1168
}

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

1170
    if (!sw) {
B
bellard 已提交
1171
        return;
1172
    }
B
bellard 已提交
1173 1174

    hw = sw->hw;
1175
    if (sw->active != on) {
1176
        AudioState *s = &glob_audio_state;
1177
        SWVoiceOut *temp_sw;
B
bellard 已提交
1178
        SWVoiceCap *sc;
1179 1180 1181 1182 1183

        if (on) {
            hw->pending_disable = 0;
            if (!hw->enabled) {
                hw->enabled = 1;
1184
                if (s->vm_running) {
M
malc 已提交
1185 1186
                    hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
                    audio_reset_timer ();
1187
                }
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
            }
        }
        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 已提交
1201
        }
B
bellard 已提交
1202 1203 1204

        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
            sc->sw.active = hw->enabled;
1205
            if (hw->enabled) {
B
bellard 已提交
1206
                audio_capture_maybe_changed (sc->cap, 1);
1207 1208
            }
        }
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
        sw->active = on;
    }
}

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

    if (!sw) {
        return;
B
bellard 已提交
1219 1220
    }

1221
    hw = sw->hw;
B
bellard 已提交
1222
    if (sw->active != on) {
1223
        AudioState *s = &glob_audio_state;
1224 1225
        SWVoiceIn *temp_sw;

B
bellard 已提交
1226 1227 1228
        if (on) {
            if (!hw->enabled) {
                hw->enabled = 1;
1229
                if (s->vm_running) {
M
malc 已提交
1230
                    hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1231
                }
B
bellard 已提交
1232
            }
1233
            sw->total_hw_samples_acquired = hw->total_samples_captured;
B
bellard 已提交
1234 1235
        }
        else {
1236
            if (hw->enabled) {
B
bellard 已提交
1237
                int nb_active = 0;
1238 1239 1240 1241

                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 已提交
1242 1243 1244
                }

                if (nb_active == 1) {
1245 1246
                    hw->enabled = 0;
                    hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
B
bellard 已提交
1247 1248 1249 1250 1251 1252 1253
                }
            }
        }
        sw->active = on;
    }
}

1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
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 (
B
bellard 已提交
1269
        "%s: get_avail live %d ret %" PRId64 "\n",
B
bellard 已提交
1270
        SW_NAME (sw),
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
        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 已提交
1289
        return 0;
1290 1291 1292 1293 1294
    }

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

#ifdef DEBUG_OUT
B
bellard 已提交
1295
    dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
B
bellard 已提交
1296
           SW_NAME (sw),
1297
           live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
B
bellard 已提交
1298
#endif
1299 1300 1301 1302

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

1303 1304 1305 1306 1307
static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
{
    int n;

    if (hw->enabled) {
B
bellard 已提交
1308
        SWVoiceCap *sc;
1309

B
bellard 已提交
1310 1311
        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
            SWVoiceOut *sw = &sc->sw;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
            int rpos2 = rpos;

            n = samples;
            while (n) {
                int till_end_of_hw = hw->samples - rpos2;
                int to_write = audio_MIN (till_end_of_hw, n);
                int bytes = to_write << hw->info.shift;
                int written;

                sw->buf = hw->mix_buf + rpos2;
                written = audio_pcm_sw_write (sw, NULL, bytes);
                if (written - bytes) {
B
bellard 已提交
1324 1325 1326
                    dolog ("Could not mix %d bytes into a capture "
                           "buffer, mixed %d\n",
                           bytes, written);
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
                    break;
                }
                n -= to_write;
                rpos2 = (rpos2 + to_write) % hw->samples;
            }
        }
    }

    n = audio_MIN (samples, hw->samples - rpos);
    mixeng_clear (hw->mix_buf + rpos, n);
    mixeng_clear (hw->mix_buf, samples - n);
}

B
bellard 已提交
1340
static void audio_run_out (AudioState *s)
1341 1342 1343 1344
{
    HWVoiceOut *hw = NULL;
    SWVoiceOut *sw;

1345
    while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1346
        int played;
1347
        int live, free, nb_live, cleanup_required, prev_rpos;
1348

M
malc 已提交
1349
        live = audio_pcm_hw_get_live_out (hw, &nb_live);
1350 1351 1352
        if (!nb_live) {
            live = 0;
        }
B
bellard 已提交
1353

1354 1355
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
B
bellard 已提交
1356
            continue;
1357 1358 1359
        }

        if (hw->pending_disable && !nb_live) {
B
bellard 已提交
1360
            SWVoiceCap *sc;
1361 1362
#ifdef DEBUG_OUT
            dolog ("Disabling voice\n");
B
bellard 已提交
1363
#endif
1364 1365 1366
            hw->enabled = 0;
            hw->pending_disable = 0;
            hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
B
bellard 已提交
1367 1368 1369
            for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
                sc->sw.active = 0;
                audio_recalc_and_notify_capture (sc->cap);
1370
            }
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
            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;
        }

1386
        prev_rpos = hw->rpos;
M
malc 已提交
1387
        played = hw->pcm_ops->run_out (hw, live);
1388 1389 1390 1391 1392 1393 1394
        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 已提交
1395
        dolog ("played=%d\n", played);
B
bellard 已提交
1396
#endif
1397 1398 1399

        if (played) {
            hw->ts_helper += played;
1400
            audio_capture_mix_and_clear (hw, prev_rpos, played);
1401 1402
        }

B
bellard 已提交
1403
        cleanup_required = 0;
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
        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 已提交
1419
                cleanup_required |= !sw->active && !sw->callback.fn;
1420 1421 1422 1423 1424 1425 1426 1427 1428
            }

            if (sw->active) {
                free = audio_get_free (sw);
                if (free > 0) {
                    sw->callback.fn (sw->callback.opaque, free);
                }
            }
        }
B
bellard 已提交
1429 1430

        if (cleanup_required) {
B
bellard 已提交
1431 1432 1433 1434 1435
            SWVoiceOut *sw1;

            sw = hw->sw_head.lh_first;
            while (sw) {
                sw1 = sw->entries.le_next;
B
bellard 已提交
1436 1437 1438 1439
                if (!sw->active && !sw->callback.fn) {
#ifdef DEBUG_PLIVE
                    dolog ("Finishing with old voice\n");
#endif
1440
                    audio_close_out (sw);
B
bellard 已提交
1441
                }
B
bellard 已提交
1442
                sw = sw1;
B
bellard 已提交
1443 1444
            }
        }
1445 1446 1447
    }
}

B
bellard 已提交
1448
static void audio_run_in (AudioState *s)
1449 1450 1451
{
    HWVoiceIn *hw = NULL;

1452
    while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
        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);
                }
            }
        }
    }
}

1477 1478 1479 1480 1481 1482 1483 1484 1485
static void audio_run_capture (AudioState *s)
{
    CaptureVoiceOut *cap;

    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
        int live, rpos, captured;
        HWVoiceOut *hw = &cap->hw;
        SWVoiceOut *sw;

M
malc 已提交
1486
        captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1487 1488 1489 1490
        rpos = hw->rpos;
        while (live) {
            int left = hw->samples - rpos;
            int to_capture = audio_MIN (live, left);
M
malc 已提交
1491
            struct st_sample *src;
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
            struct capture_callback *cb;

            src = hw->mix_buf + rpos;
            hw->clip (cap->buf, src, to_capture);
            mixeng_clear (src, to_capture);

            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
                cb->ops.capture (cb->opaque, cap->buf,
                                 to_capture << hw->info.shift);
            }
            rpos = (rpos + to_capture) % hw->samples;
            live -= to_capture;
        }
        hw->rpos = rpos;

        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
            if (!sw->active && sw->empty) {
                continue;
            }

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

            sw->total_hw_samples_mixed -= captured;
            sw->empty = sw->total_hw_samples_mixed == 0;
        }
    }
}

M
malc 已提交
1524
void audio_run (const char *msg)
B
bellard 已提交
1525
{
M
malc 已提交
1526
    AudioState *s = &glob_audio_state;
B
bellard 已提交
1527 1528 1529

    audio_run_out (s);
    audio_run_in (s);
1530
    audio_run_capture (s);
M
malc 已提交
1531 1532 1533 1534 1535
#ifdef DEBUG_POLL
    {
        static double prevtime;
        double currtime;
        struct timeval tv;
B
bellard 已提交
1536

M
malc 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
        if (gettimeofday (&tv, NULL)) {
            perror ("audio_run: gettimeofday");
            return;
        }

        currtime = tv.tv_sec + tv.tv_usec * 1e-6;
        dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
        prevtime = currtime;
    }
#endif
B
bellard 已提交
1547 1548
}

1549 1550
static struct audio_option audio_options[] = {
    /* DAC */
M
malc 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    {
        .name  = "DAC_FIXED_SETTINGS",
        .tag   = AUD_OPT_BOOL,
        .valp  = &conf.fixed_out.enabled,
        .descr = "Use fixed settings for host DAC"
    },
    {
        .name  = "DAC_FIXED_FREQ",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_out.settings.freq,
        .descr = "Frequency for fixed host DAC"
    },
    {
        .name  = "DAC_FIXED_FMT",
        .tag   = AUD_OPT_FMT,
        .valp  = &conf.fixed_out.settings.fmt,
        .descr = "Format for fixed host DAC"
    },
    {
        .name  = "DAC_FIXED_CHANNELS",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_out.settings.nchannels,
        .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
    },
    {
        .name  = "DAC_VOICES",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_out.nb_voices,
        .descr = "Number of voices for DAC"
    },
M
malc 已提交
1581 1582 1583 1584 1585 1586
    {
        .name  = "DAC_TRY_POLL",
        .tag   = AUD_OPT_BOOL,
        .valp  = &conf.try_poll_out,
        .descr = "Attempt using poll mode for DAC"
    },
1587
    /* ADC */
M
malc 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
    {
        .name  = "ADC_FIXED_SETTINGS",
        .tag   = AUD_OPT_BOOL,
        .valp  = &conf.fixed_in.enabled,
        .descr = "Use fixed settings for host ADC"
    },
    {
        .name  = "ADC_FIXED_FREQ",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_in.settings.freq,
        .descr = "Frequency for fixed host ADC"
    },
    {
        .name  = "ADC_FIXED_FMT",
        .tag   = AUD_OPT_FMT,
        .valp  = &conf.fixed_in.settings.fmt,
        .descr = "Format for fixed host ADC"
    },
    {
        .name  = "ADC_FIXED_CHANNELS",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_in.settings.nchannels,
        .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
    },
    {
        .name  = "ADC_VOICES",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.fixed_in.nb_voices,
        .descr = "Number of voices for ADC"
    },
M
malc 已提交
1618 1619 1620
    {
        .name  = "ADC_TRY_POLL",
        .tag   = AUD_OPT_BOOL,
1621
        .valp  = &conf.try_poll_in,
M
malc 已提交
1622 1623
        .descr = "Attempt using poll mode for ADC"
    },
1624
    /* Misc */
M
malc 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    {
        .name  = "TIMER_PERIOD",
        .tag   = AUD_OPT_INT,
        .valp  = &conf.period.hertz,
        .descr = "Timer period in HZ (0 - use lowest possible)"
    },
    {
        .name  = "PLIVE",
        .tag   = AUD_OPT_BOOL,
        .valp  = &conf.plive,
        .descr = "(undocumented)"
    },
    {
        .name  = "LOG_TO_MONITOR",
        .tag   = AUD_OPT_BOOL,
        .valp  = &conf.log_to_monitor,
M
malc 已提交
1641
        .descr = "Print logging messages to monitor instead of stderr"
M
malc 已提交
1642
    },
1643
    { /* End of list */ }
B
bellard 已提交
1644 1645
};

B
bellard 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
static void audio_pp_nb_voices (const char *typ, int nb)
{
    switch (nb) {
    case 0:
        printf ("Does not support %s\n", typ);
        break;
    case 1:
        printf ("One %s voice\n", typ);
        break;
    case INT_MAX:
        printf ("Theoretically supports many %s voices\n", typ);
        break;
    default:
        printf ("Theoretically supports upto %d %s voices\n", nb, typ);
        break;
    }

}

1665 1666 1667 1668 1669
void AUD_help (void)
{
    size_t i;

    audio_process_options ("AUDIO", audio_options);
1670
    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
        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");

1683
    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1684 1685 1686 1687 1688
        struct audio_driver *d = drvtab[i];

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

B
bellard 已提交
1689 1690
        audio_pp_nb_voices ("playback", d->max_voices_out);
        audio_pp_nb_voices ("capture", d->max_voices_in);
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706

        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"
B
bellard 已提交
1707
        "  set QEMU_WAV_PATH=c:\\tune.wav\n"
1708 1709 1710 1711 1712 1713 1714 1715 1716
#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"
        );
}

B
bellard 已提交
1717
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
B
bellard 已提交
1718
{
1719 1720 1721
    if (drv->options) {
        audio_process_options (drv->name, drv->options);
    }
B
bellard 已提交
1722
    s->drv_opaque = drv->init ();
1723

B
bellard 已提交
1724
    if (s->drv_opaque) {
1725 1726
        audio_init_nb_voices_out (drv);
        audio_init_nb_voices_in (drv);
B
bellard 已提交
1727
        s->drv = drv;
1728
        return 0;
B
bellard 已提交
1729 1730
    }
    else {
1731 1732
        dolog ("Could not init `%s' audio driver\n", drv->name);
        return -1;
B
bellard 已提交
1733 1734 1735
    }
}

1736 1737
static void audio_vm_change_state_handler (void *opaque, int running,
                                           int reason)
B
bellard 已提交
1738
{
B
bellard 已提交
1739
    AudioState *s = opaque;
1740 1741
    HWVoiceOut *hwo = NULL;
    HWVoiceIn *hwi = NULL;
B
bellard 已提交
1742
    int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1743

1744
    s->vm_running = running;
1745
    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
M
malc 已提交
1746
        hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1747
    }
B
bellard 已提交
1748

1749
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
M
malc 已提交
1750
        hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
B
bellard 已提交
1751
    }
M
malc 已提交
1752
    audio_reset_timer ();
B
bellard 已提交
1753 1754 1755 1756
}

static void audio_atexit (void)
{
B
bellard 已提交
1757
    AudioState *s = &glob_audio_state;
1758 1759 1760
    HWVoiceOut *hwo = NULL;
    HWVoiceIn *hwi = NULL;

1761
    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
B
bellard 已提交
1762
        SWVoiceCap *sc;
1763

B
bellard 已提交
1764
        hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1765
        hwo->pcm_ops->fini_out (hwo);
1766

B
bellard 已提交
1767 1768 1769 1770 1771 1772 1773
        for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
            CaptureVoiceOut *cap = sc->cap;
            struct capture_callback *cb;

            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
                cb->ops.destroy (cb->opaque);
            }
1774
        }
1775
    }
B
bellard 已提交
1776

1777
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
B
bellard 已提交
1778
        hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1779
        hwi->pcm_ops->fini_in (hwi);
B
bellard 已提交
1780
    }
B
bellard 已提交
1781 1782 1783 1784

    if (s->drv) {
        s->drv->fini (s->drv_opaque);
    }
B
bellard 已提交
1785 1786 1787 1788
}

static void audio_save (QEMUFile *f, void *opaque)
{
1789 1790
    (void) f;
    (void) opaque;
B
bellard 已提交
1791 1792 1793 1794
}

static int audio_load (QEMUFile *f, void *opaque, int version_id)
{
1795 1796 1797 1798
    (void) f;
    (void) opaque;

    if (version_id != 1) {
B
bellard 已提交
1799
        return -EINVAL;
1800
    }
B
bellard 已提交
1801 1802 1803 1804

    return 0;
}

1805
static void audio_init (void)
B
bellard 已提交
1806
{
1807
    size_t i;
B
bellard 已提交
1808 1809
    int done = 0;
    const char *drvname;
M
malc 已提交
1810
    VMChangeStateEntry *e;
B
bellard 已提交
1811
    AudioState *s = &glob_audio_state;
1812

P
Paul Brook 已提交
1813
    if (s->drv) {
1814
        return;
P
Paul Brook 已提交
1815 1816
    }

B
Blue Swirl 已提交
1817 1818 1819
    QLIST_INIT (&s->hw_head_out);
    QLIST_INIT (&s->hw_head_in);
    QLIST_INIT (&s->cap_head);
B
bellard 已提交
1820 1821 1822 1823
    atexit (audio_atexit);

    s->ts = qemu_new_timer (vm_clock, audio_timer, s);
    if (!s->ts) {
P
Paul Brook 已提交
1824
        hw_error("Could not create audio timer\n");
B
bellard 已提交
1825 1826
    }

1827 1828
    audio_process_options ("AUDIO", audio_options);

B
bellard 已提交
1829 1830 1831
    s->nb_hw_voices_out = conf.fixed_out.nb_voices;
    s->nb_hw_voices_in = conf.fixed_in.nb_voices;

1832
    if (s->nb_hw_voices_out <= 0) {
B
bellard 已提交
1833
        dolog ("Bogus number of playback voices %d, setting to 1\n",
1834 1835 1836 1837 1838
               s->nb_hw_voices_out);
        s->nb_hw_voices_out = 1;
    }

    if (s->nb_hw_voices_in <= 0) {
B
bellard 已提交
1839
        dolog ("Bogus number of capture voices %d, setting to 0\n",
1840
               s->nb_hw_voices_in);
B
bellard 已提交
1841
        s->nb_hw_voices_in = 0;
1842
    }
B
bellard 已提交
1843

1844 1845 1846 1847
    {
        int def;
        drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
    }
B
bellard 已提交
1848 1849 1850

    if (drvname) {
        int found = 0;
1851

1852
        for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
B
bellard 已提交
1853
            if (!strcmp (drvname, drvtab[i]->name)) {
B
bellard 已提交
1854
                done = !audio_driver_init (s, drvtab[i]);
B
bellard 已提交
1855 1856 1857 1858
                found = 1;
                break;
            }
        }
1859

B
bellard 已提交
1860 1861
        if (!found) {
            dolog ("Unknown audio driver `%s'\n", drvname);
1862
            dolog ("Run with -audio-help to list available drivers\n");
B
bellard 已提交
1863 1864 1865 1866
        }
    }

    if (!done) {
1867
        for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1868
            if (drvtab[i]->can_be_default) {
B
bellard 已提交
1869
                done = !audio_driver_init (s, drvtab[i]);
1870
            }
B
bellard 已提交
1871 1872 1873 1874
        }
    }

    if (!done) {
B
bellard 已提交
1875 1876
        done = !audio_driver_init (s, &no_audio_driver);
        if (!done) {
P
Paul Brook 已提交
1877
            hw_error("Could not initialize audio subsystem\n");
1878 1879
        }
        else {
B
bellard 已提交
1880
            dolog ("warning: Using timer based audio emulation\n");
1881
        }
B
bellard 已提交
1882
    }
1883

P
Paul Brook 已提交
1884 1885 1886 1887 1888
    if (conf.period.hertz <= 0) {
        if (conf.period.hertz < 0) {
            dolog ("warning: Timer period is negative - %d "
                   "treating as zero\n",
                   conf.period.hertz);
B
bellard 已提交
1889
        }
P
Paul Brook 已提交
1890 1891
        conf.period.ticks = 1;
    } else {
M
malc 已提交
1892 1893
        conf.period.ticks =
            muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1894
    }
P
Paul Brook 已提交
1895 1896 1897 1898 1899

    e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
    if (!e) {
        dolog ("warning: Could not register change state handler\n"
               "(Audio can continue looping even after stopping the VM)\n");
1900 1901
    }

B
Blue Swirl 已提交
1902
    QLIST_INIT (&s->card_head);
B
bellard 已提交
1903
    register_savevm ("audio", 0, 1, audio_save, audio_load, s);
B
bellard 已提交
1904
}
1905

1906 1907 1908 1909 1910
void AUD_register_card (const char *name, QEMUSoundCard *card)
{
    audio_init ();
    card->name = qemu_strdup (name);
    memset (&card->entries, 0, sizeof (card->entries));
B
Blue Swirl 已提交
1911
    QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1912 1913 1914 1915
}

void AUD_remove_card (QEMUSoundCard *card)
{
B
Blue Swirl 已提交
1916
    QLIST_REMOVE (card, entries);
1917 1918 1919 1920
    qemu_free (card->name);
}


B
bellard 已提交
1921
CaptureVoiceOut *AUD_add_capture (
M
malc 已提交
1922
    struct audsettings *as,
1923 1924 1925 1926
    struct audio_capture_ops *ops,
    void *cb_opaque
    )
{
1927
    AudioState *s = &glob_audio_state;
1928 1929 1930
    CaptureVoiceOut *cap;
    struct capture_callback *cb;

B
bellard 已提交
1931
    if (audio_validate_settings (as)) {
1932 1933
        dolog ("Invalid settings were passed when trying to add capture\n");
        audio_print_settings (as);
B
bellard 已提交
1934
        goto err0;
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
    }

    cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
    if (!cb) {
        dolog ("Could not allocate capture callback information, size %zu\n",
               sizeof (*cb));
        goto err0;
    }
    cb->ops = *ops;
    cb->opaque = cb_opaque;

1946
    cap = audio_pcm_capture_find_specific (as);
1947
    if (cap) {
B
Blue Swirl 已提交
1948
        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
B
bellard 已提交
1949
        return cap;
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    }
    else {
        HWVoiceOut *hw;
        CaptureVoiceOut *cap;

        cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
        if (!cap) {
            dolog ("Could not allocate capture voice, size %zu\n",
                   sizeof (*cap));
            goto err1;
        }

        hw = &cap->hw;
B
Blue Swirl 已提交
1963 1964
        QLIST_INIT (&hw->sw_head);
        QLIST_INIT (&cap->cb_head);
1965 1966 1967 1968

        /* XXX find a more elegant way */
        hw->samples = 4096 * 4;
        hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
M
malc 已提交
1969
                                    sizeof (struct st_sample));
1970 1971 1972 1973 1974 1975
        if (!hw->mix_buf) {
            dolog ("Could not allocate capture mix buffer (%d samples)\n",
                   hw->samples);
            goto err2;
        }

B
bellard 已提交
1976
        audio_pcm_init_info (&hw->info, as);
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988

        cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
        if (!cap->buf) {
            dolog ("Could not allocate capture buffer "
                   "(%d samples, each %d bytes)\n",
                   hw->samples, 1 << hw->info.shift);
            goto err3;
        }

        hw->clip = mixeng_clip
            [hw->info.nchannels == 2]
            [hw->info.sign]
B
bellard 已提交
1989
            [hw->info.swap_endianness]
1990
            [audio_bits_to_index (hw->info.bits)];
1991

B
Blue Swirl 已提交
1992 1993
        QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1994 1995

        hw = NULL;
1996 1997
        while ((hw = audio_pcm_hw_find_any_out (hw))) {
            audio_attach_capture (hw);
1998
        }
B
bellard 已提交
1999
        return cap;
2000 2001 2002 2003 2004 2005 2006 2007

    err3:
        qemu_free (cap->hw.mix_buf);
    err2:
        qemu_free (cap);
    err1:
        qemu_free (cb);
    err0:
B
bellard 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
        return NULL;
    }
}

void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
{
    struct capture_callback *cb;

    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
        if (cb->opaque == cb_opaque) {
            cb->ops.destroy (cb_opaque);
B
Blue Swirl 已提交
2019
            QLIST_REMOVE (cb, entries);
B
bellard 已提交
2020 2021 2022 2023
            qemu_free (cb);

            if (!cap->cb_head.lh_first) {
                SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
B
bellard 已提交
2024

B
bellard 已提交
2025
                while (sw) {
B
bellard 已提交
2026
                    SWVoiceCap *sc = (SWVoiceCap *) sw;
B
bellard 已提交
2027 2028 2029
#ifdef DEBUG_CAPTURE
                    dolog ("freeing %s\n", sw->name);
#endif
B
bellard 已提交
2030

B
bellard 已提交
2031 2032 2033 2034 2035
                    sw1 = sw->entries.le_next;
                    if (sw->rate) {
                        st_rate_stop (sw->rate);
                        sw->rate = NULL;
                    }
B
Blue Swirl 已提交
2036 2037
                    QLIST_REMOVE (sw, entries);
                    QLIST_REMOVE (sc, entries);
B
bellard 已提交
2038
                    qemu_free (sc);
B
bellard 已提交
2039 2040
                    sw = sw1;
                }
B
Blue Swirl 已提交
2041
                QLIST_REMOVE (cap, entries);
B
bellard 已提交
2042 2043 2044 2045
                qemu_free (cap);
            }
            return;
        }
2046 2047
    }
}
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065

void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
{
    if (sw) {
        sw->vol.mute = mute;
        sw->vol.l = nominal_volume.l * lvol / 255;
        sw->vol.r = nominal_volume.r * rvol / 255;
    }
}

void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
{
    if (sw) {
        sw->vol.mute = mute;
        sw->vol.l = nominal_volume.l * lvol / 255;
        sw->vol.r = nominal_volume.r * rvol / 255;
    }
}