winwaveaudio.c 17.2 KB
Newer Older
1 2 3
/* public domain */

#include "qemu-common.h"
malc's avatar
malc committed
4
#include "sysemu.h"
5 6 7 8 9 10 11 12 13 14 15 16 17
#include "audio.h"

#define AUDIO_CAP "winwave"
#include "audio_int.h"

#include <windows.h>
#include <mmsystem.h>

#include "audio_win_int.h"

static struct {
    int dac_headers;
    int dac_samples;
malc's avatar
malc committed
18 19
    int adc_headers;
    int adc_samples;
20 21
} conf = {
    .dac_headers = 4,
malc's avatar
malc committed
22 23 24
    .dac_samples = 1024,
    .adc_headers = 4,
    .adc_samples = 1024
25 26 27 28 29 30
};

typedef struct {
    HWVoiceOut hw;
    HWAVEOUT hwo;
    WAVEHDR *hdrs;
malc's avatar
malc committed
31
    HANDLE event;
32 33 34 35
    void *pcm_buf;
    int avail;
    int pending;
    int curhdr;
36
    int paused;
37 38 39
    CRITICAL_SECTION crit_sect;
} WaveVoiceOut;

malc's avatar
malc committed
40 41 42 43 44 45 46 47 48 49 50 51 52
typedef struct {
    HWVoiceIn hw;
    HWAVEIN hwi;
    WAVEHDR *hdrs;
    HANDLE event;
    void *pcm_buf;
    int curhdr;
    int paused;
    int rpos;
    int avail;
    CRITICAL_SECTION crit_sect;
} WaveVoiceIn;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static void winwave_log_mmresult (MMRESULT mr)
{
    const char *str = "BUG";

    switch (mr) {
    case MMSYSERR_NOERROR:
        str = "Success";
        break;

    case MMSYSERR_INVALHANDLE:
        str = "Specified device handle is invalid";
        break;

    case MMSYSERR_BADDEVICEID:
        str = "Specified device id is out of range";
        break;

    case MMSYSERR_NODRIVER:
        str = "No device driver is present";
        break;

    case MMSYSERR_NOMEM:
malc's avatar
malc committed
75
        str = "Unable to allocate or lock memory";
76 77 78 79 80 81 82 83 84 85 86 87
        break;

    case WAVERR_SYNC:
        str = "Device is synchronous but waveOutOpen was called "
            "without using the WINWAVE_ALLOWSYNC flag";
        break;

    case WAVERR_UNPREPARED:
        str = "The data block pointed to by the pwh parameter "
            "hasn't been prepared";
        break;

88 89 90 91
    case WAVERR_STILLPLAYING:
        str = "There are still buffers in the queue";
        break;

92
    default:
malc's avatar
malc committed
93
        dolog ("Reason: Unknown (MMRESULT %#x)\n", mr);
94 95 96
        return;
    }

malc's avatar
malc committed
97
    dolog ("Reason: %s\n", str);
98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

static void GCC_FMT_ATTR (2, 3) winwave_logerr (
    MMRESULT mr,
    const char *fmt,
    ...
    )
{
    va_list ap;

    va_start (ap, fmt);
    AUD_vlog (AUDIO_CAP, fmt, ap);
    va_end (ap);

112
    AUD_log (NULL, " failed\n");
113 114 115 116 117 118 119 120 121
    winwave_log_mmresult (mr);
}

static void winwave_anal_close_out (WaveVoiceOut *wave)
{
    MMRESULT mr;

    mr = waveOutClose (wave->hwo);
    if (mr != MMSYSERR_NOERROR) {
122
        winwave_logerr (mr, "waveOutClose");
123 124 125 126
    }
    wave->hwo = NULL;
}

malc's avatar
malc committed
127
static void CALLBACK winwave_callback_out (
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    HWAVEOUT hwo,
    UINT msg,
    DWORD_PTR dwInstance,
    DWORD_PTR dwParam1,
    DWORD_PTR dwParam2
    )
{
    WaveVoiceOut *wave = (WaveVoiceOut *) dwInstance;

    switch (msg) {
    case WOM_DONE:
        {
            WAVEHDR *h = (WAVEHDR *) dwParam1;
            if (!h->dwUser) {
                h->dwUser = 1;
                EnterCriticalSection (&wave->crit_sect);
                {
                    wave->avail += conf.dac_samples;
                }
                LeaveCriticalSection (&wave->crit_sect);
malc's avatar
malc committed
148 149
                if (wave->hw.poll_mode) {
                    if (!SetEvent (wave->event)) {
malc's avatar
malc committed
150
                        dolog ("DAC SetEvent failed %lx\n", GetLastError ());
malc's avatar
malc committed
151 152
                    }
                }
153 154 155 156 157 158 159 160 161
            }
        }
        break;

    case WOM_CLOSE:
    case WOM_OPEN:
        break;

    default:
malc's avatar
malc committed
162
        dolog ("unknown wave out callback msg %x\n", msg);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    }
}

static int winwave_init_out (HWVoiceOut *hw, struct audsettings *as)
{
    int i;
    int err;
    MMRESULT mr;
    WAVEFORMATEX wfx;
    WaveVoiceOut *wave;

    wave = (WaveVoiceOut *) hw;

    InitializeCriticalSection (&wave->crit_sect);

malc's avatar
malc committed
178
    err = waveformat_from_audio_settings (&wfx, as);
179 180 181 182 183
    if (err) {
        goto err0;
    }

    mr = waveOutOpen (&wave->hwo, WAVE_MAPPER, &wfx,
malc's avatar
malc committed
184
                      (DWORD_PTR) winwave_callback_out,
185 186
                      (DWORD_PTR) wave, CALLBACK_FUNCTION);
    if (mr != MMSYSERR_NOERROR) {
187
        winwave_logerr (mr, "waveOutOpen");
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        goto err1;
    }

    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
                               sizeof (*wave->hdrs));
    if (!wave->hdrs) {
        goto err2;
    }

    audio_pcm_init_info (&hw->info, as);
    hw->samples = conf.dac_samples * conf.dac_headers;
    wave->avail = hw->samples;

    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.dac_samples,
                                  conf.dac_headers << hw->info.shift);
    if (!wave->pcm_buf) {
        goto err3;
    }

    for (i = 0; i < conf.dac_headers; ++i) {
        WAVEHDR *h = &wave->hdrs[i];

        h->dwUser = 0;
        h->dwBufferLength = conf.dac_samples << hw->info.shift;
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
        h->dwFlags = 0;

        mr = waveOutPrepareHeader (wave->hwo, h, sizeof (*h));
        if (mr != MMSYSERR_NOERROR) {
malc's avatar
malc committed
217
            winwave_logerr (mr, "waveOutPrepareHeader(%d)", i);
218 219 220 221 222 223 224
            goto err4;
        }
    }

    return 0;

 err4:
225
    g_free (wave->pcm_buf);
226
 err3:
227
    g_free (wave->hdrs);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
 err2:
    winwave_anal_close_out (wave);
 err1:
 err0:
    return -1;
}

static int winwave_write (SWVoiceOut *sw, void *buf, int len)
{
    return audio_pcm_sw_write (sw, buf, len);
}

static int winwave_run_out (HWVoiceOut *hw, int live)
{
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
    int decr;
malc's avatar
malc committed
244
    int doreset;
245 246 247 248 249 250 251 252 253 254

    EnterCriticalSection (&wave->crit_sect);
    {
        decr = audio_MIN (live, wave->avail);
        decr = audio_pcm_hw_clip_out (hw, wave->pcm_buf, decr, wave->pending);
        wave->pending += decr;
        wave->avail -= decr;
    }
    LeaveCriticalSection (&wave->crit_sect);

malc's avatar
malc committed
255 256
    doreset = hw->poll_mode && (wave->pending >= conf.dac_samples);
    if (doreset && !ResetEvent (wave->event)) {
malc's avatar
malc committed
257
        dolog ("DAC ResetEvent failed %lx\n", GetLastError ());
malc's avatar
malc committed
258 259
    }

260 261 262 263 264 265 266
    while (wave->pending >= conf.dac_samples) {
        MMRESULT mr;
        WAVEHDR *h = &wave->hdrs[wave->curhdr];

        h->dwUser = 0;
        mr = waveOutWrite (wave->hwo, h, sizeof (*h));
        if (mr != MMSYSERR_NOERROR) {
267
            winwave_logerr (mr, "waveOutWrite(%d)", wave->curhdr);
268 269 270 271 272 273
            break;
        }

        wave->pending -= conf.dac_samples;
        wave->curhdr = (wave->curhdr + 1) % conf.dac_headers;
    }
malc's avatar
malc committed
274

275 276 277
    return decr;
}

malc's avatar
malc committed
278
static void winwave_poll (void *opaque)
279 280
{
    (void) opaque;
malc's avatar
malc committed
281
    audio_run ("winwave_poll");
282 283
}

284 285
static void winwave_fini_out (HWVoiceOut *hw)
{
286 287
    int i;
    MMRESULT mr;
288 289
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;

290 291
    mr = waveOutReset (wave->hwo);
    if (mr != MMSYSERR_NOERROR) {
292
        winwave_logerr (mr, "waveOutReset");
293 294 295 296 297 298
    }

    for (i = 0; i < conf.dac_headers; ++i) {
        mr = waveOutUnprepareHeader (wave->hwo, &wave->hdrs[i],
                                     sizeof (wave->hdrs[i]));
        if (mr != MMSYSERR_NOERROR) {
299
            winwave_logerr (mr, "waveOutUnprepareHeader(%d)", i);
300 301 302
        }
    }

303 304
    winwave_anal_close_out (wave);

malc's avatar
malc committed
305
    if (wave->event) {
malc's avatar
malc committed
306
        qemu_del_wait_object (wave->event, winwave_poll, wave);
malc's avatar
malc committed
307
        if (!CloseHandle (wave->event)) {
malc's avatar
malc committed
308
            dolog ("DAC CloseHandle failed %lx\n", GetLastError ());
malc's avatar
malc committed
309 310 311 312
        }
        wave->event = NULL;
    }

313
    g_free (wave->pcm_buf);
314 315
    wave->pcm_buf = NULL;

316
    g_free (wave->hdrs);
317
    wave->hdrs = NULL;
318 319 320 321
}

static int winwave_ctl_out (HWVoiceOut *hw, int cmd, ...)
{
322
    MMRESULT mr;
malc's avatar
malc committed
323 324
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;

325 326
    switch (cmd) {
    case VOICE_ENABLE:
malc's avatar
malc committed
327 328 329 330 331 332 333 334 335 336 337
        {
            va_list ap;
            int poll_mode;

            va_start (ap, cmd);
            poll_mode = va_arg (ap, int);
            va_end (ap);

            if (poll_mode && !wave->event) {
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
                if (!wave->event) {
malc's avatar
malc committed
338 339
                    dolog ("DAC CreateEvent: %lx, poll mode will be disabled\n",
                           GetLastError ());
malc's avatar
malc committed
340 341 342 343 344 345
                }
            }

            if (wave->event) {
                int ret;

malc's avatar
malc committed
346
                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
malc's avatar
malc committed
347 348 349 350 351
                hw->poll_mode = (ret == 0);
            }
            else {
                hw->poll_mode = 0;
            }
352
            wave->paused = 0;
malc's avatar
malc committed
353
        }
354 355 356
        return 0;

    case VOICE_DISABLE:
357
        if (!wave->paused) {
358
            mr = waveOutReset (wave->hwo);
359
            if (mr != MMSYSERR_NOERROR) {
360
                winwave_logerr (mr, "waveOutReset");
361 362 363 364 365
            }
            else {
                wave->paused = 1;
            }
        }
malc's avatar
malc committed
366
        if (wave->event) {
malc's avatar
malc committed
367
            qemu_del_wait_object (wave->event, winwave_poll, wave);
malc's avatar
malc committed
368
        }
369 370 371 372 373
        return 0;
    }
    return -1;
}

malc's avatar
malc committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 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 470 471 472 473 474 475 476 477 478 479 480 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
static void winwave_anal_close_in (WaveVoiceIn *wave)
{
    MMRESULT mr;

    mr = waveInClose (wave->hwi);
    if (mr != MMSYSERR_NOERROR) {
        winwave_logerr (mr, "waveInClose");
    }
    wave->hwi = NULL;
}

static void CALLBACK winwave_callback_in (
    HWAVEIN *hwi,
    UINT msg,
    DWORD_PTR dwInstance,
    DWORD_PTR dwParam1,
    DWORD_PTR dwParam2
    )
{
    WaveVoiceIn *wave = (WaveVoiceIn *) dwInstance;

    switch (msg) {
    case WIM_DATA:
        {
            WAVEHDR *h = (WAVEHDR *) dwParam1;
            if (!h->dwUser) {
                h->dwUser = 1;
                EnterCriticalSection (&wave->crit_sect);
                {
                    wave->avail += conf.adc_samples;
                }
                LeaveCriticalSection (&wave->crit_sect);
                if (wave->hw.poll_mode) {
                    if (!SetEvent (wave->event)) {
                        dolog ("ADC SetEvent failed %lx\n", GetLastError ());
                    }
                }
            }
        }
        break;

    case WIM_CLOSE:
    case WIM_OPEN:
        break;

    default:
        dolog ("unknown wave in callback msg %x\n", msg);
    }
}

static void winwave_add_buffers (WaveVoiceIn *wave, int samples)
{
    int doreset;

    doreset = wave->hw.poll_mode && (samples >= conf.adc_samples);
    if (doreset && !ResetEvent (wave->event)) {
        dolog ("ADC ResetEvent failed %lx\n", GetLastError ());
    }

    while (samples >= conf.adc_samples) {
        MMRESULT mr;
        WAVEHDR *h = &wave->hdrs[wave->curhdr];

        h->dwUser = 0;
        mr = waveInAddBuffer (wave->hwi, h, sizeof (*h));
        if (mr != MMSYSERR_NOERROR) {
            winwave_logerr (mr, "waveInAddBuffer(%d)", wave->curhdr);
        }
        wave->curhdr = (wave->curhdr + 1) % conf.adc_headers;
        samples -= conf.adc_samples;
    }
}

static int winwave_init_in (HWVoiceIn *hw, struct audsettings *as)
{
    int i;
    int err;
    MMRESULT mr;
    WAVEFORMATEX wfx;
    WaveVoiceIn *wave;

    wave = (WaveVoiceIn *) hw;

    InitializeCriticalSection (&wave->crit_sect);

    err = waveformat_from_audio_settings (&wfx, as);
    if (err) {
        goto err0;
    }

    mr = waveInOpen (&wave->hwi, WAVE_MAPPER, &wfx,
                     (DWORD_PTR) winwave_callback_in,
                     (DWORD_PTR) wave, CALLBACK_FUNCTION);
    if (mr != MMSYSERR_NOERROR) {
        winwave_logerr (mr, "waveInOpen");
        goto err1;
    }

    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
                               sizeof (*wave->hdrs));
    if (!wave->hdrs) {
        goto err2;
    }

    audio_pcm_init_info (&hw->info, as);
    hw->samples = conf.adc_samples * conf.adc_headers;
    wave->avail = 0;

    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.adc_samples,
                                  conf.adc_headers << hw->info.shift);
    if (!wave->pcm_buf) {
        goto err3;
    }

    for (i = 0; i < conf.adc_headers; ++i) {
        WAVEHDR *h = &wave->hdrs[i];

        h->dwUser = 0;
        h->dwBufferLength = conf.adc_samples << hw->info.shift;
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
        h->dwFlags = 0;

        mr = waveInPrepareHeader (wave->hwi, h, sizeof (*h));
        if (mr != MMSYSERR_NOERROR) {
            winwave_logerr (mr, "waveInPrepareHeader(%d)", i);
            goto err4;
        }
    }

    wave->paused = 1;
    winwave_add_buffers (wave, hw->samples);
    return 0;

 err4:
508
    g_free (wave->pcm_buf);
malc's avatar
malc committed
509
 err3:
510
    g_free (wave->hdrs);
malc's avatar
malc committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
 err2:
    winwave_anal_close_in (wave);
 err1:
 err0:
    return -1;
}

static void winwave_fini_in (HWVoiceIn *hw)
{
    int i;
    MMRESULT mr;
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;

    mr = waveInReset (wave->hwi);
    if (mr != MMSYSERR_NOERROR) {
        winwave_logerr (mr, "waveInReset");
    }

    for (i = 0; i < conf.adc_headers; ++i) {
        mr = waveInUnprepareHeader (wave->hwi, &wave->hdrs[i],
                                     sizeof (wave->hdrs[i]));
        if (mr != MMSYSERR_NOERROR) {
            winwave_logerr (mr, "waveInUnprepareHeader(%d)", i);
        }
    }

    winwave_anal_close_in (wave);

    if (wave->event) {
        qemu_del_wait_object (wave->event, winwave_poll, wave);
        if (!CloseHandle (wave->event)) {
            dolog ("ADC CloseHandle failed %lx\n", GetLastError ());
        }
        wave->event = NULL;
    }

547
    g_free (wave->pcm_buf);
malc's avatar
malc committed
548 549
    wave->pcm_buf = NULL;

550
    g_free (wave->hdrs);
malc's avatar
malc committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    wave->hdrs = NULL;
}

static int winwave_run_in (HWVoiceIn *hw)
{
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
    int live = audio_pcm_hw_get_live_in (hw);
    int dead = hw->samples - live;
    int decr, ret;

    if (!dead) {
        return 0;
    }

    EnterCriticalSection (&wave->crit_sect);
    {
        decr = audio_MIN (dead, wave->avail);
        wave->avail -= decr;
    }
    LeaveCriticalSection (&wave->crit_sect);

    ret = decr;
    while (decr) {
        int left = hw->samples - hw->wpos;
        int conv = audio_MIN (left, decr);
        hw->conv (hw->conv_buf + hw->wpos,
                  advance (wave->pcm_buf, wave->rpos << hw->info.shift),
578
                  conv);
malc's avatar
malc committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

        wave->rpos = (wave->rpos + conv) % hw->samples;
        hw->wpos = (hw->wpos + conv) % hw->samples;
        decr -= conv;
    }

    winwave_add_buffers (wave, ret);
    return ret;
}

static int winwave_read (SWVoiceIn *sw, void *buf, int size)
{
    return audio_pcm_sw_read (sw, buf, size);
}

static int winwave_ctl_in (HWVoiceIn *hw, int cmd, ...)
{
    MMRESULT mr;
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;

    switch (cmd) {
    case VOICE_ENABLE:
        {
            va_list ap;
            int poll_mode;

            va_start (ap, cmd);
            poll_mode = va_arg (ap, int);
            va_end (ap);

            if (poll_mode && !wave->event) {
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
                if (!wave->event) {
                    dolog ("ADC CreateEvent: %lx, poll mode will be disabled\n",
                           GetLastError ());
                }
            }

            if (wave->event) {
                int ret;

                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
                hw->poll_mode = (ret == 0);
            }
            else {
                hw->poll_mode = 0;
            }
            if (wave->paused) {
                mr = waveInStart (wave->hwi);
                if (mr != MMSYSERR_NOERROR) {
                    winwave_logerr (mr, "waveInStart");
                }
                wave->paused = 0;
            }
        }
        return 0;

    case VOICE_DISABLE:
        if (!wave->paused) {
            mr = waveInStop (wave->hwi);
            if (mr != MMSYSERR_NOERROR) {
                winwave_logerr (mr, "waveInStop");
            }
            else {
                wave->paused = 1;
            }
        }
        if (wave->event) {
            qemu_del_wait_object (wave->event, winwave_poll, wave);
        }
        return 0;
    }
    return 0;
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
static void *winwave_audio_init (void)
{
    return &conf;
}

static void winwave_audio_fini (void *opaque)
{
    (void) opaque;
}

static struct audio_option winwave_options[] = {
    {
        .name        = "DAC_HEADERS",
        .tag         = AUD_OPT_INT,
        .valp        = &conf.dac_headers,
        .descr       = "DAC number of headers",
    },
    {
        .name        = "DAC_SAMPLES",
        .tag         = AUD_OPT_INT,
        .valp        = &conf.dac_samples,
        .descr       = "DAC number of samples per header",
    },
malc's avatar
malc committed
677 678 679 680 681 682 683 684 685 686 687 688
    {
        .name        = "ADC_HEADERS",
        .tag         = AUD_OPT_INT,
        .valp        = &conf.adc_headers,
        .descr       = "ADC number of headers",
    },
    {
        .name        = "ADC_SAMPLES",
        .tag         = AUD_OPT_INT,
        .valp        = &conf.adc_samples,
        .descr       = "ADC number of samples per header",
    },
689 690 691 692 693 694 695 696
    { /* End of list */ }
};

static struct audio_pcm_ops winwave_pcm_ops = {
    .init_out = winwave_init_out,
    .fini_out = winwave_fini_out,
    .run_out  = winwave_run_out,
    .write    = winwave_write,
malc's avatar
malc committed
697 698 699 700 701 702
    .ctl_out  = winwave_ctl_out,
    .init_in  = winwave_init_in,
    .fini_in  = winwave_fini_in,
    .run_in   = winwave_run_in,
    .read     = winwave_read,
    .ctl_in   = winwave_ctl_in
703 704 705 706 707 708 709 710 711 712 713
};

struct audio_driver winwave_audio_driver = {
    .name           = "winwave",
    .descr          = "Windows Waveform Audio http://msdn.microsoft.com",
    .options        = winwave_options,
    .init           = winwave_audio_init,
    .fini           = winwave_audio_fini,
    .pcm_ops        = &winwave_pcm_ops,
    .can_be_default = 1,
    .max_voices_out = INT_MAX,
malc's avatar
malc committed
714
    .max_voices_in  = INT_MAX,
715
    .voice_size_out = sizeof (WaveVoiceOut),
malc's avatar
malc committed
716
    .voice_size_in  = sizeof (WaveVoiceIn)
717
};