1 /* 2 * QEMU DirectSound audio driver 3 * 4 * Copyright (c) 2005 Vassili Karpov (malc) 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 /* 26 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation 27 */ 28 29 #include "qemu-common.h" 30 #include "audio.h" 31 32 #define AUDIO_CAP "dsound" 33 #include "audio_int.h" 34 35 #include <windows.h> 36 #include <mmsystem.h> 37 #include <objbase.h> 38 #include <dsound.h> 39 40 /* #define DEBUG_DSOUND */ 41 42 static struct { 43 int lock_retries; 44 int restore_retries; 45 int getstatus_retries; 46 int set_primary; 47 int bufsize_in; 48 int bufsize_out; 49 struct audsettings settings; 50 int latency_millis; 51 } conf = { 52 .lock_retries = 1, 53 .restore_retries = 1, 54 .getstatus_retries = 1, 55 .set_primary = 0, 56 .bufsize_in = 16384, 57 .bufsize_out = 16384, 58 .settings.freq = 44100, 59 .settings.nchannels = 2, 60 .settings.fmt = AUD_FMT_S16, 61 .latency_millis = 10 62 }; 63 64 typedef struct { 65 LPDIRECTSOUND dsound; 66 LPDIRECTSOUNDCAPTURE dsound_capture; 67 LPDIRECTSOUNDBUFFER dsound_primary_buffer; 68 struct audsettings settings; 69 } dsound; 70 71 static dsound glob_dsound; 72 73 typedef struct { 74 HWVoiceOut hw; 75 LPDIRECTSOUNDBUFFER dsound_buffer; 76 DWORD old_pos; 77 int first_time; 78 #ifdef DEBUG_DSOUND 79 DWORD old_ppos; 80 DWORD played; 81 DWORD mixed; 82 #endif 83 } DSoundVoiceOut; 84 85 typedef struct { 86 HWVoiceIn hw; 87 int first_time; 88 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; 89 } DSoundVoiceIn; 90 91 static void dsound_log_hresult (HRESULT hr) 92 { 93 const char *str = "BUG"; 94 95 switch (hr) { 96 case DS_OK: 97 str = "The method succeeded"; 98 break; 99 #ifdef DS_NO_VIRTUALIZATION 100 case DS_NO_VIRTUALIZATION: 101 str = "The buffer was created, but another 3D algorithm was substituted"; 102 break; 103 #endif 104 #ifdef DS_INCOMPLETE 105 case DS_INCOMPLETE: 106 str = "The method succeeded, but not all the optional effects were obtained"; 107 break; 108 #endif 109 #ifdef DSERR_ACCESSDENIED 110 case DSERR_ACCESSDENIED: 111 str = "The request failed because access was denied"; 112 break; 113 #endif 114 #ifdef DSERR_ALLOCATED 115 case DSERR_ALLOCATED: 116 str = "The request failed because resources, such as a priority level, were already in use by another caller"; 117 break; 118 #endif 119 #ifdef DSERR_ALREADYINITIALIZED 120 case DSERR_ALREADYINITIALIZED: 121 str = "The object is already initialized"; 122 break; 123 #endif 124 #ifdef DSERR_BADFORMAT 125 case DSERR_BADFORMAT: 126 str = "The specified wave format is not supported"; 127 break; 128 #endif 129 #ifdef DSERR_BADSENDBUFFERGUID 130 case DSERR_BADSENDBUFFERGUID: 131 str = "The GUID specified in an audiopath file does not match a valid mix-in buffer"; 132 break; 133 #endif 134 #ifdef DSERR_BUFFERLOST 135 case DSERR_BUFFERLOST: 136 str = "The buffer memory has been lost and must be restored"; 137 break; 138 #endif 139 #ifdef DSERR_BUFFERTOOSMALL 140 case DSERR_BUFFERTOOSMALL: 141 str = "The buffer size is not great enough to enable effects processing"; 142 break; 143 #endif 144 #ifdef DSERR_CONTROLUNAVAIL 145 case DSERR_CONTROLUNAVAIL: 146 str = "The buffer control (volume, pan, and so on) requested by the caller is not available. Controls must be specified when the buffer is created, using the dwFlags member of DSBUFFERDESC"; 147 break; 148 #endif 149 #ifdef DSERR_DS8_REQUIRED 150 case DSERR_DS8_REQUIRED: 151 str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface"; 152 break; 153 #endif 154 #ifdef DSERR_FXUNAVAILABLE 155 case DSERR_FXUNAVAILABLE: 156 str = "The effects requested could not be found on the system, or they are in the wrong order or in the wrong location; for example, an effect expected in hardware was found in software"; 157 break; 158 #endif 159 #ifdef DSERR_GENERIC 160 case DSERR_GENERIC : 161 str = "An undetermined error occurred inside the DirectSound subsystem"; 162 break; 163 #endif 164 #ifdef DSERR_INVALIDCALL 165 case DSERR_INVALIDCALL: 166 str = "This function is not valid for the current state of this object"; 167 break; 168 #endif 169 #ifdef DSERR_INVALIDPARAM 170 case DSERR_INVALIDPARAM: 171 str = "An invalid parameter was passed to the returning function"; 172 break; 173 #endif 174 #ifdef DSERR_NOAGGREGATION 175 case DSERR_NOAGGREGATION: 176 str = "The object does not support aggregation"; 177 break; 178 #endif 179 #ifdef DSERR_NODRIVER 180 case DSERR_NODRIVER: 181 str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID"; 182 break; 183 #endif 184 #ifdef DSERR_NOINTERFACE 185 case DSERR_NOINTERFACE: 186 str = "The requested COM interface is not available"; 187 break; 188 #endif 189 #ifdef DSERR_OBJECTNOTFOUND 190 case DSERR_OBJECTNOTFOUND: 191 str = "The requested object was not found"; 192 break; 193 #endif 194 #ifdef DSERR_OTHERAPPHASPRIO 195 case DSERR_OTHERAPPHASPRIO: 196 str = "Another application has a higher priority level, preventing this call from succeeding"; 197 break; 198 #endif 199 #ifdef DSERR_OUTOFMEMORY 200 case DSERR_OUTOFMEMORY: 201 str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request"; 202 break; 203 #endif 204 #ifdef DSERR_PRIOLEVELNEEDED 205 case DSERR_PRIOLEVELNEEDED: 206 str = "A cooperative level of DSSCL_PRIORITY or higher is required"; 207 break; 208 #endif 209 #ifdef DSERR_SENDLOOP 210 case DSERR_SENDLOOP: 211 str = "A circular loop of send effects was detected"; 212 break; 213 #endif 214 #ifdef DSERR_UNINITIALIZED 215 case DSERR_UNINITIALIZED: 216 str = "The Initialize method has not been called or has not been called successfully before other methods were called"; 217 break; 218 #endif 219 #ifdef DSERR_UNSUPPORTED 220 case DSERR_UNSUPPORTED: 221 str = "The function called is not supported at this time"; 222 break; 223 #endif 224 default: 225 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr); 226 return; 227 } 228 229 AUD_log (AUDIO_CAP, "Reason: %s\n", str); 230 } 231 232 static void GCC_FMT_ATTR (2, 3) dsound_logerr ( 233 HRESULT hr, 234 const char *fmt, 235 ... 236 ) 237 { 238 va_list ap; 239 240 va_start (ap, fmt); 241 AUD_vlog (AUDIO_CAP, fmt, ap); 242 va_end (ap); 243 244 dsound_log_hresult (hr); 245 } 246 247 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( 248 HRESULT hr, 249 const char *typ, 250 const char *fmt, 251 ... 252 ) 253 { 254 va_list ap; 255 256 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); 257 va_start (ap, fmt); 258 AUD_vlog (AUDIO_CAP, fmt, ap); 259 va_end (ap); 260 261 dsound_log_hresult (hr); 262 } 263 264 static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis) 265 { 266 return (millis * info->bytes_per_second) / 1000; 267 } 268 269 #ifdef DEBUG_DSOUND 270 static void print_wave_format (WAVEFORMATEX *wfx) 271 { 272 dolog ("tag = %d\n", wfx->wFormatTag); 273 dolog ("nChannels = %d\n", wfx->nChannels); 274 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec); 275 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec); 276 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign); 277 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample); 278 dolog ("cbSize = %d\n", wfx->cbSize); 279 } 280 #endif 281 282 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb) 283 { 284 HRESULT hr; 285 int i; 286 287 for (i = 0; i < conf.restore_retries; ++i) { 288 hr = IDirectSoundBuffer_Restore (dsb); 289 290 switch (hr) { 291 case DS_OK: 292 return 0; 293 294 case DSERR_BUFFERLOST: 295 continue; 296 297 default: 298 dsound_logerr (hr, "Could not restore playback buffer\n"); 299 return -1; 300 } 301 } 302 303 dolog ("%d attempts to restore playback buffer failed\n", i); 304 return -1; 305 } 306 307 static int waveformat_from_audio_settings (WAVEFORMATEX *wfx, 308 struct audsettings *as) 309 { 310 memset (wfx, 0, sizeof (*wfx)); 311 312 wfx->wFormatTag = WAVE_FORMAT_PCM; 313 wfx->nChannels = as->nchannels; 314 wfx->nSamplesPerSec = as->freq; 315 wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2); 316 wfx->nBlockAlign = 1 << (as->nchannels == 2); 317 wfx->cbSize = 0; 318 319 switch (as->fmt) { 320 case AUD_FMT_S8: 321 case AUD_FMT_U8: 322 wfx->wBitsPerSample = 8; 323 break; 324 325 case AUD_FMT_S16: 326 case AUD_FMT_U16: 327 wfx->wBitsPerSample = 16; 328 wfx->nAvgBytesPerSec <<= 1; 329 wfx->nBlockAlign <<= 1; 330 break; 331 332 case AUD_FMT_S32: 333 case AUD_FMT_U32: 334 wfx->wBitsPerSample = 32; 335 wfx->nAvgBytesPerSec <<= 2; 336 wfx->nBlockAlign <<= 2; 337 break; 338 339 default: 340 dolog ("Internal logic error: Bad audio format %d\n", as->freq); 341 return -1; 342 } 343 344 return 0; 345 } 346 347 static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, 348 struct audsettings *as) 349 { 350 if (wfx->wFormatTag != WAVE_FORMAT_PCM) { 351 dolog ("Invalid wave format, tag is not PCM, but %d\n", 352 wfx->wFormatTag); 353 return -1; 354 } 355 356 if (!wfx->nSamplesPerSec) { 357 dolog ("Invalid wave format, frequency is zero\n"); 358 return -1; 359 } 360 as->freq = wfx->nSamplesPerSec; 361 362 switch (wfx->nChannels) { 363 case 1: 364 as->nchannels = 1; 365 break; 366 367 case 2: 368 as->nchannels = 2; 369 break; 370 371 default: 372 dolog ( 373 "Invalid wave format, number of channels is not 1 or 2, but %d\n", 374 wfx->nChannels 375 ); 376 return -1; 377 } 378 379 switch (wfx->wBitsPerSample) { 380 case 8: 381 as->fmt = AUD_FMT_U8; 382 break; 383 384 case 16: 385 as->fmt = AUD_FMT_S16; 386 break; 387 388 case 32: 389 as->fmt = AUD_FMT_S32; 390 break; 391 392 default: 393 dolog ("Invalid wave format, bits per sample is not " 394 "8, 16 or 32, but %d\n", 395 wfx->wBitsPerSample); 396 return -1; 397 } 398 399 return 0; 400 } 401 402 #include "dsound_template.h" 403 #define DSBTYPE_IN 404 #include "dsound_template.h" 405 #undef DSBTYPE_IN 406 407 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp) 408 { 409 HRESULT hr; 410 int i; 411 412 for (i = 0; i < conf.getstatus_retries; ++i) { 413 hr = IDirectSoundBuffer_GetStatus (dsb, statusp); 414 if (FAILED (hr)) { 415 dsound_logerr (hr, "Could not get playback buffer status\n"); 416 return -1; 417 } 418 419 if (*statusp & DSERR_BUFFERLOST) { 420 if (dsound_restore_out (dsb)) { 421 return -1; 422 } 423 continue; 424 } 425 break; 426 } 427 428 return 0; 429 } 430 431 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb, 432 DWORD *statusp) 433 { 434 HRESULT hr; 435 436 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp); 437 if (FAILED (hr)) { 438 dsound_logerr (hr, "Could not get capture buffer status\n"); 439 return -1; 440 } 441 442 return 0; 443 } 444 445 static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len) 446 { 447 int src_len1 = dst_len; 448 int src_len2 = 0; 449 int pos = hw->rpos + dst_len; 450 struct st_sample *src1 = hw->mix_buf + hw->rpos; 451 struct st_sample *src2 = NULL; 452 453 if (pos > hw->samples) { 454 src_len1 = hw->samples - hw->rpos; 455 src2 = hw->mix_buf; 456 src_len2 = dst_len - src_len1; 457 pos = src_len2; 458 } 459 460 if (src_len1) { 461 hw->clip (dst, src1, src_len1); 462 } 463 464 if (src_len2) { 465 dst = advance (dst, src_len1 << hw->info.shift); 466 hw->clip (dst, src2, src_len2); 467 } 468 469 hw->rpos = pos % hw->samples; 470 } 471 472 static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb) 473 { 474 int err; 475 LPVOID p1, p2; 476 DWORD blen1, blen2, len1, len2; 477 478 err = dsound_lock_out ( 479 dsb, 480 &hw->info, 481 0, 482 hw->samples << hw->info.shift, 483 &p1, &p2, 484 &blen1, &blen2, 485 1 486 ); 487 if (err) { 488 return; 489 } 490 491 len1 = blen1 >> hw->info.shift; 492 len2 = blen2 >> hw->info.shift; 493 494 #ifdef DEBUG_DSOUND 495 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n", 496 p1, blen1, len1, 497 p2, blen2, len2); 498 #endif 499 500 if (p1 && len1) { 501 audio_pcm_info_clear_buf (&hw->info, p1, len1); 502 } 503 504 if (p2 && len2) { 505 audio_pcm_info_clear_buf (&hw->info, p2, len2); 506 } 507 508 dsound_unlock_out (dsb, p1, p2, blen1, blen2); 509 } 510 511 static void dsound_close (dsound *s) 512 { 513 HRESULT hr; 514 515 if (s->dsound_primary_buffer) { 516 hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer); 517 if (FAILED (hr)) { 518 dsound_logerr (hr, "Could not release primary buffer\n"); 519 } 520 s->dsound_primary_buffer = NULL; 521 } 522 } 523 524 static int dsound_open (dsound *s) 525 { 526 int err; 527 HRESULT hr; 528 WAVEFORMATEX wfx; 529 DSBUFFERDESC dsbd; 530 HWND hwnd; 531 532 hwnd = GetForegroundWindow (); 533 hr = IDirectSound_SetCooperativeLevel ( 534 s->dsound, 535 hwnd, 536 DSSCL_PRIORITY 537 ); 538 539 if (FAILED (hr)) { 540 dsound_logerr (hr, "Could not set cooperative level for window %p\n", 541 hwnd); 542 return -1; 543 } 544 545 if (!conf.set_primary) { 546 return 0; 547 } 548 549 err = waveformat_from_audio_settings (&wfx, &conf.settings); 550 if (err) { 551 return -1; 552 } 553 554 memset (&dsbd, 0, sizeof (dsbd)); 555 dsbd.dwSize = sizeof (dsbd); 556 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; 557 dsbd.dwBufferBytes = 0; 558 dsbd.lpwfxFormat = NULL; 559 560 hr = IDirectSound_CreateSoundBuffer ( 561 s->dsound, 562 &dsbd, 563 &s->dsound_primary_buffer, 564 NULL 565 ); 566 if (FAILED (hr)) { 567 dsound_logerr (hr, "Could not create primary playback buffer\n"); 568 return -1; 569 } 570 571 hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx); 572 if (FAILED (hr)) { 573 dsound_logerr (hr, "Could not set primary playback buffer format\n"); 574 } 575 576 hr = IDirectSoundBuffer_GetFormat ( 577 s->dsound_primary_buffer, 578 &wfx, 579 sizeof (wfx), 580 NULL 581 ); 582 if (FAILED (hr)) { 583 dsound_logerr (hr, "Could not get primary playback buffer format\n"); 584 goto fail0; 585 } 586 587 #ifdef DEBUG_DSOUND 588 dolog ("Primary\n"); 589 print_wave_format (&wfx); 590 #endif 591 592 err = waveformat_to_audio_settings (&wfx, &s->settings); 593 if (err) { 594 goto fail0; 595 } 596 597 return 0; 598 599 fail0: 600 dsound_close (s); 601 return -1; 602 } 603 604 static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...) 605 { 606 HRESULT hr; 607 DWORD status; 608 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 609 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 610 611 if (!dsb) { 612 dolog ("Attempt to control voice without a buffer\n"); 613 return 0; 614 } 615 616 switch (cmd) { 617 case VOICE_ENABLE: 618 if (dsound_get_status_out (dsb, &status)) { 619 return -1; 620 } 621 622 if (status & DSBSTATUS_PLAYING) { 623 dolog ("warning: Voice is already playing\n"); 624 return 0; 625 } 626 627 dsound_clear_sample (hw, dsb); 628 629 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); 630 if (FAILED (hr)) { 631 dsound_logerr (hr, "Could not start playing buffer\n"); 632 return -1; 633 } 634 break; 635 636 case VOICE_DISABLE: 637 if (dsound_get_status_out (dsb, &status)) { 638 return -1; 639 } 640 641 if (status & DSBSTATUS_PLAYING) { 642 hr = IDirectSoundBuffer_Stop (dsb); 643 if (FAILED (hr)) { 644 dsound_logerr (hr, "Could not stop playing buffer\n"); 645 return -1; 646 } 647 } 648 else { 649 dolog ("warning: Voice is not playing\n"); 650 } 651 break; 652 } 653 return 0; 654 } 655 656 static int dsound_write (SWVoiceOut *sw, void *buf, int len) 657 { 658 return audio_pcm_sw_write (sw, buf, len); 659 } 660 661 static int dsound_run_out (HWVoiceOut *hw, int live) 662 { 663 int err; 664 HRESULT hr; 665 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 666 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 667 int len, hwshift; 668 DWORD blen1, blen2; 669 DWORD len1, len2; 670 DWORD decr; 671 DWORD wpos, ppos, old_pos; 672 LPVOID p1, p2; 673 int bufsize; 674 675 if (!dsb) { 676 dolog ("Attempt to run empty with playback buffer\n"); 677 return 0; 678 } 679 680 hwshift = hw->info.shift; 681 bufsize = hw->samples << hwshift; 682 683 hr = IDirectSoundBuffer_GetCurrentPosition ( 684 dsb, 685 &ppos, 686 ds->first_time ? &wpos : NULL 687 ); 688 if (FAILED (hr)) { 689 dsound_logerr (hr, "Could not get playback buffer position\n"); 690 return 0; 691 } 692 693 len = live << hwshift; 694 695 if (ds->first_time) { 696 if (conf.latency_millis) { 697 DWORD cur_blat; 698 699 cur_blat = audio_ring_dist (wpos, ppos, bufsize); 700 ds->first_time = 0; 701 old_pos = wpos; 702 old_pos += 703 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat; 704 old_pos %= bufsize; 705 old_pos &= ~hw->info.align; 706 } 707 else { 708 old_pos = wpos; 709 } 710 #ifdef DEBUG_DSOUND 711 ds->played = 0; 712 ds->mixed = 0; 713 #endif 714 } 715 else { 716 if (ds->old_pos == ppos) { 717 #ifdef DEBUG_DSOUND 718 dolog ("old_pos == ppos\n"); 719 #endif 720 return 0; 721 } 722 723 #ifdef DEBUG_DSOUND 724 ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize); 725 #endif 726 old_pos = ds->old_pos; 727 } 728 729 if ((old_pos < ppos) && ((old_pos + len) > ppos)) { 730 len = ppos - old_pos; 731 } 732 else { 733 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) { 734 len = bufsize - old_pos + ppos; 735 } 736 } 737 738 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) { 739 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n", 740 len, bufsize, old_pos, ppos); 741 return 0; 742 } 743 744 len &= ~hw->info.align; 745 if (!len) { 746 return 0; 747 } 748 749 #ifdef DEBUG_DSOUND 750 ds->old_ppos = ppos; 751 #endif 752 err = dsound_lock_out ( 753 dsb, 754 &hw->info, 755 old_pos, 756 len, 757 &p1, &p2, 758 &blen1, &blen2, 759 0 760 ); 761 if (err) { 762 return 0; 763 } 764 765 len1 = blen1 >> hwshift; 766 len2 = blen2 >> hwshift; 767 decr = len1 + len2; 768 769 if (p1 && len1) { 770 dsound_write_sample (hw, p1, len1); 771 } 772 773 if (p2 && len2) { 774 dsound_write_sample (hw, p2, len2); 775 } 776 777 dsound_unlock_out (dsb, p1, p2, blen1, blen2); 778 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize; 779 780 #ifdef DEBUG_DSOUND 781 ds->mixed += decr << hwshift; 782 783 dolog ("played %lu mixed %lu diff %ld sec %f\n", 784 ds->played, 785 ds->mixed, 786 ds->mixed - ds->played, 787 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second); 788 #endif 789 return decr; 790 } 791 792 static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...) 793 { 794 HRESULT hr; 795 DWORD status; 796 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 797 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 798 799 if (!dscb) { 800 dolog ("Attempt to control capture voice without a buffer\n"); 801 return -1; 802 } 803 804 switch (cmd) { 805 case VOICE_ENABLE: 806 if (dsound_get_status_in (dscb, &status)) { 807 return -1; 808 } 809 810 if (status & DSCBSTATUS_CAPTURING) { 811 dolog ("warning: Voice is already capturing\n"); 812 return 0; 813 } 814 815 /* clear ?? */ 816 817 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); 818 if (FAILED (hr)) { 819 dsound_logerr (hr, "Could not start capturing\n"); 820 return -1; 821 } 822 break; 823 824 case VOICE_DISABLE: 825 if (dsound_get_status_in (dscb, &status)) { 826 return -1; 827 } 828 829 if (status & DSCBSTATUS_CAPTURING) { 830 hr = IDirectSoundCaptureBuffer_Stop (dscb); 831 if (FAILED (hr)) { 832 dsound_logerr (hr, "Could not stop capturing\n"); 833 return -1; 834 } 835 } 836 else { 837 dolog ("warning: Voice is not capturing\n"); 838 } 839 break; 840 } 841 return 0; 842 } 843 844 static int dsound_read (SWVoiceIn *sw, void *buf, int len) 845 { 846 return audio_pcm_sw_read (sw, buf, len); 847 } 848 849 static int dsound_run_in (HWVoiceIn *hw) 850 { 851 int err; 852 HRESULT hr; 853 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 854 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 855 int live, len, dead; 856 DWORD blen1, blen2; 857 DWORD len1, len2; 858 DWORD decr; 859 DWORD cpos, rpos; 860 LPVOID p1, p2; 861 int hwshift; 862 863 if (!dscb) { 864 dolog ("Attempt to run without capture buffer\n"); 865 return 0; 866 } 867 868 hwshift = hw->info.shift; 869 870 live = audio_pcm_hw_get_live_in (hw); 871 dead = hw->samples - live; 872 if (!dead) { 873 return 0; 874 } 875 876 hr = IDirectSoundCaptureBuffer_GetCurrentPosition ( 877 dscb, 878 &cpos, 879 ds->first_time ? &rpos : NULL 880 ); 881 if (FAILED (hr)) { 882 dsound_logerr (hr, "Could not get capture buffer position\n"); 883 return 0; 884 } 885 886 if (ds->first_time) { 887 ds->first_time = 0; 888 if (rpos & hw->info.align) { 889 ldebug ("warning: Misaligned capture read position %ld(%d)\n", 890 rpos, hw->info.align); 891 } 892 hw->wpos = rpos >> hwshift; 893 } 894 895 if (cpos & hw->info.align) { 896 ldebug ("warning: Misaligned capture position %ld(%d)\n", 897 cpos, hw->info.align); 898 } 899 cpos >>= hwshift; 900 901 len = audio_ring_dist (cpos, hw->wpos, hw->samples); 902 if (!len) { 903 return 0; 904 } 905 len = audio_MIN (len, dead); 906 907 err = dsound_lock_in ( 908 dscb, 909 &hw->info, 910 hw->wpos << hwshift, 911 len << hwshift, 912 &p1, 913 &p2, 914 &blen1, 915 &blen2, 916 0 917 ); 918 if (err) { 919 return 0; 920 } 921 922 len1 = blen1 >> hwshift; 923 len2 = blen2 >> hwshift; 924 decr = len1 + len2; 925 926 if (p1 && len1) { 927 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume); 928 } 929 930 if (p2 && len2) { 931 hw->conv (hw->conv_buf, p2, len2, &nominal_volume); 932 } 933 934 dsound_unlock_in (dscb, p1, p2, blen1, blen2); 935 hw->wpos = (hw->wpos + decr) % hw->samples; 936 return decr; 937 } 938 939 static void dsound_audio_fini (void *opaque) 940 { 941 HRESULT hr; 942 dsound *s = opaque; 943 944 if (!s->dsound) { 945 return; 946 } 947 948 hr = IDirectSound_Release (s->dsound); 949 if (FAILED (hr)) { 950 dsound_logerr (hr, "Could not release DirectSound\n"); 951 } 952 s->dsound = NULL; 953 954 if (!s->dsound_capture) { 955 return; 956 } 957 958 hr = IDirectSoundCapture_Release (s->dsound_capture); 959 if (FAILED (hr)) { 960 dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 961 } 962 s->dsound_capture = NULL; 963 } 964 965 static void *dsound_audio_init (void) 966 { 967 int err; 968 HRESULT hr; 969 dsound *s = &glob_dsound; 970 971 hr = CoInitialize (NULL); 972 if (FAILED (hr)) { 973 dsound_logerr (hr, "Could not initialize COM\n"); 974 return NULL; 975 } 976 977 hr = CoCreateInstance ( 978 &CLSID_DirectSound, 979 NULL, 980 CLSCTX_ALL, 981 &IID_IDirectSound, 982 (void **) &s->dsound 983 ); 984 if (FAILED (hr)) { 985 dsound_logerr (hr, "Could not create DirectSound instance\n"); 986 return NULL; 987 } 988 989 hr = IDirectSound_Initialize (s->dsound, NULL); 990 if (FAILED (hr)) { 991 dsound_logerr (hr, "Could not initialize DirectSound\n"); 992 993 hr = IDirectSound_Release (s->dsound); 994 if (FAILED (hr)) { 995 dsound_logerr (hr, "Could not release DirectSound\n"); 996 } 997 s->dsound = NULL; 998 return NULL; 999 } 1000 1001 hr = CoCreateInstance ( 1002 &CLSID_DirectSoundCapture, 1003 NULL, 1004 CLSCTX_ALL, 1005 &IID_IDirectSoundCapture, 1006 (void **) &s->dsound_capture 1007 ); 1008 if (FAILED (hr)) { 1009 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); 1010 } 1011 else { 1012 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); 1013 if (FAILED (hr)) { 1014 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); 1015 1016 hr = IDirectSoundCapture_Release (s->dsound_capture); 1017 if (FAILED (hr)) { 1018 dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 1019 } 1020 s->dsound_capture = NULL; 1021 } 1022 } 1023 1024 err = dsound_open (s); 1025 if (err) { 1026 dsound_audio_fini (s); 1027 return NULL; 1028 } 1029 1030 return s; 1031 } 1032 1033 static struct audio_option dsound_options[] = { 1034 { 1035 .name = "LOCK_RETRIES", 1036 .tag = AUD_OPT_INT, 1037 .valp = &conf.lock_retries, 1038 .descr = "Number of times to attempt locking the buffer" 1039 }, 1040 { 1041 .name = "RESTOURE_RETRIES", 1042 .tag = AUD_OPT_INT, 1043 .valp = &conf.restore_retries, 1044 .descr = "Number of times to attempt restoring the buffer" 1045 }, 1046 { 1047 .name = "GETSTATUS_RETRIES", 1048 .tag = AUD_OPT_INT, 1049 .valp = &conf.getstatus_retries, 1050 .descr = "Number of times to attempt getting status of the buffer" 1051 }, 1052 { 1053 .name = "SET_PRIMARY", 1054 .tag = AUD_OPT_BOOL, 1055 .valp = &conf.set_primary, 1056 .descr = "Set the parameters of primary buffer" 1057 }, 1058 { 1059 .name = "LATENCY_MILLIS", 1060 .tag = AUD_OPT_INT, 1061 .valp = &conf.latency_millis, 1062 .descr = "(undocumented)" 1063 }, 1064 { 1065 .name = "PRIMARY_FREQ", 1066 .tag = AUD_OPT_INT, 1067 .valp = &conf.settings.freq, 1068 .descr = "Primary buffer frequency" 1069 }, 1070 { 1071 .name = "PRIMARY_CHANNELS", 1072 .tag = AUD_OPT_INT, 1073 .valp = &conf.settings.nchannels, 1074 .descr = "Primary buffer number of channels (1 - mono, 2 - stereo)" 1075 }, 1076 { 1077 .name = "PRIMARY_FMT", 1078 .tag = AUD_OPT_FMT, 1079 .valp = &conf.settings.fmt, 1080 .descr = "Primary buffer format" 1081 }, 1082 { 1083 .name = "BUFSIZE_OUT", 1084 .tag = AUD_OPT_INT, 1085 .valp = &conf.bufsize_out, 1086 .descr = "(undocumented)" 1087 }, 1088 { 1089 .name = "BUFSIZE_IN", 1090 .tag = AUD_OPT_INT, 1091 .valp = &conf.bufsize_in, 1092 .descr = "(undocumented)" 1093 }, 1094 { /* End of list */ } 1095 }; 1096 1097 static struct audio_pcm_ops dsound_pcm_ops = { 1098 .init_out = dsound_init_out, 1099 .fini_out = dsound_fini_out, 1100 .run_out = dsound_run_out, 1101 .write = dsound_write, 1102 .ctl_out = dsound_ctl_out, 1103 1104 .init_in = dsound_init_in, 1105 .fini_in = dsound_fini_in, 1106 .run_in = dsound_run_in, 1107 .read = dsound_read, 1108 .ctl_in = dsound_ctl_in 1109 }; 1110 1111 struct audio_driver dsound_audio_driver = { 1112 .name = "dsound", 1113 .descr = "DirectSound http://wikipedia.org/wiki/DirectSound", 1114 .options = dsound_options, 1115 .init = dsound_audio_init, 1116 .fini = dsound_audio_fini, 1117 .pcm_ops = &dsound_pcm_ops, 1118 .can_be_default = 1, 1119 .max_voices_out = INT_MAX, 1120 .max_voices_in = 1, 1121 .voice_size_out = sizeof (DSoundVoiceOut), 1122 .voice_size_in = sizeof (DSoundVoiceIn) 1123 }; 1124