11d14ffa9Sbellard /* 21d14ffa9Sbellard * QEMU DirectSound audio driver 31d14ffa9Sbellard * 41d14ffa9Sbellard * Copyright (c) 2005 Vassili Karpov (malc) 51d14ffa9Sbellard * 61d14ffa9Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 71d14ffa9Sbellard * of this software and associated documentation files (the "Software"), to deal 81d14ffa9Sbellard * in the Software without restriction, including without limitation the rights 91d14ffa9Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 101d14ffa9Sbellard * copies of the Software, and to permit persons to whom the Software is 111d14ffa9Sbellard * furnished to do so, subject to the following conditions: 121d14ffa9Sbellard * 131d14ffa9Sbellard * The above copyright notice and this permission notice shall be included in 141d14ffa9Sbellard * all copies or substantial portions of the Software. 151d14ffa9Sbellard * 161d14ffa9Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171d14ffa9Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181d14ffa9Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 191d14ffa9Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201d14ffa9Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211d14ffa9Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 221d14ffa9Sbellard * THE SOFTWARE. 231d14ffa9Sbellard */ 241d14ffa9Sbellard 251d14ffa9Sbellard /* 261d14ffa9Sbellard * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation 271d14ffa9Sbellard */ 281d14ffa9Sbellard 29749bc4bfSpbrook #include "qemu-common.h" 30749bc4bfSpbrook #include "audio.h" 311d14ffa9Sbellard 321d14ffa9Sbellard #define AUDIO_CAP "dsound" 331d14ffa9Sbellard #include "audio_int.h" 341d14ffa9Sbellard 354fddf62aSths #define WIN32_LEAN_AND_MEAN 361d14ffa9Sbellard #include <windows.h> 374fddf62aSths #include <mmsystem.h> 381d14ffa9Sbellard #include <objbase.h> 391d14ffa9Sbellard #include <dsound.h> 401d14ffa9Sbellard 411d14ffa9Sbellard /* #define DEBUG_DSOUND */ 421d14ffa9Sbellard 431d14ffa9Sbellard static struct { 441d14ffa9Sbellard int lock_retries; 451d14ffa9Sbellard int restore_retries; 461d14ffa9Sbellard int getstatus_retries; 471d14ffa9Sbellard int set_primary; 481d14ffa9Sbellard int bufsize_in; 491d14ffa9Sbellard int bufsize_out; 50c0fe3827Sbellard audsettings_t settings; 511d14ffa9Sbellard int latency_millis; 521d14ffa9Sbellard } conf = { 531d14ffa9Sbellard 1, 541d14ffa9Sbellard 1, 551d14ffa9Sbellard 1, 561d14ffa9Sbellard 0, 571d14ffa9Sbellard 16384, 581d14ffa9Sbellard 16384, 591d14ffa9Sbellard { 601d14ffa9Sbellard 44100, 611d14ffa9Sbellard 2, 621d14ffa9Sbellard AUD_FMT_S16 631d14ffa9Sbellard }, 641d14ffa9Sbellard 10 651d14ffa9Sbellard }; 661d14ffa9Sbellard 671d14ffa9Sbellard typedef struct { 681d14ffa9Sbellard LPDIRECTSOUND dsound; 691d14ffa9Sbellard LPDIRECTSOUNDCAPTURE dsound_capture; 701d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsound_primary_buffer; 71c0fe3827Sbellard audsettings_t settings; 721d14ffa9Sbellard } dsound; 731d14ffa9Sbellard 741d14ffa9Sbellard static dsound glob_dsound; 751d14ffa9Sbellard 761d14ffa9Sbellard typedef struct { 771d14ffa9Sbellard HWVoiceOut hw; 781d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsound_buffer; 791d14ffa9Sbellard DWORD old_pos; 801d14ffa9Sbellard int first_time; 811d14ffa9Sbellard #ifdef DEBUG_DSOUND 821d14ffa9Sbellard DWORD old_ppos; 831d14ffa9Sbellard DWORD played; 841d14ffa9Sbellard DWORD mixed; 851d14ffa9Sbellard #endif 861d14ffa9Sbellard } DSoundVoiceOut; 871d14ffa9Sbellard 881d14ffa9Sbellard typedef struct { 891d14ffa9Sbellard HWVoiceIn hw; 901d14ffa9Sbellard int first_time; 911d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; 921d14ffa9Sbellard } DSoundVoiceIn; 931d14ffa9Sbellard 941d14ffa9Sbellard static void dsound_log_hresult (HRESULT hr) 951d14ffa9Sbellard { 961d14ffa9Sbellard const char *str = "BUG"; 971d14ffa9Sbellard 981d14ffa9Sbellard switch (hr) { 991d14ffa9Sbellard case DS_OK: 1001d14ffa9Sbellard str = "The method succeeded"; 1011d14ffa9Sbellard break; 1021d14ffa9Sbellard #ifdef DS_NO_VIRTUALIZATION 1031d14ffa9Sbellard case DS_NO_VIRTUALIZATION: 1041d14ffa9Sbellard str = "The buffer was created, but another 3D algorithm was substituted"; 1051d14ffa9Sbellard break; 1061d14ffa9Sbellard #endif 1071d14ffa9Sbellard #ifdef DS_INCOMPLETE 1081d14ffa9Sbellard case DS_INCOMPLETE: 1091d14ffa9Sbellard str = "The method succeeded, but not all the optional effects were obtained"; 1101d14ffa9Sbellard break; 1111d14ffa9Sbellard #endif 1121d14ffa9Sbellard #ifdef DSERR_ACCESSDENIED 1131d14ffa9Sbellard case DSERR_ACCESSDENIED: 1141d14ffa9Sbellard str = "The request failed because access was denied"; 1151d14ffa9Sbellard break; 1161d14ffa9Sbellard #endif 1171d14ffa9Sbellard #ifdef DSERR_ALLOCATED 1181d14ffa9Sbellard case DSERR_ALLOCATED: 1191d14ffa9Sbellard str = "The request failed because resources, such as a priority level, were already in use by another caller"; 1201d14ffa9Sbellard break; 1211d14ffa9Sbellard #endif 1221d14ffa9Sbellard #ifdef DSERR_ALREADYINITIALIZED 1231d14ffa9Sbellard case DSERR_ALREADYINITIALIZED: 1241d14ffa9Sbellard str = "The object is already initialized"; 1251d14ffa9Sbellard break; 1261d14ffa9Sbellard #endif 1271d14ffa9Sbellard #ifdef DSERR_BADFORMAT 1281d14ffa9Sbellard case DSERR_BADFORMAT: 1291d14ffa9Sbellard str = "The specified wave format is not supported"; 1301d14ffa9Sbellard break; 1311d14ffa9Sbellard #endif 1321d14ffa9Sbellard #ifdef DSERR_BADSENDBUFFERGUID 1331d14ffa9Sbellard case DSERR_BADSENDBUFFERGUID: 1341d14ffa9Sbellard str = "The GUID specified in an audiopath file does not match a valid mix-in buffer"; 1351d14ffa9Sbellard break; 1361d14ffa9Sbellard #endif 1371d14ffa9Sbellard #ifdef DSERR_BUFFERLOST 1381d14ffa9Sbellard case DSERR_BUFFERLOST: 1391d14ffa9Sbellard str = "The buffer memory has been lost and must be restored"; 1401d14ffa9Sbellard break; 1411d14ffa9Sbellard #endif 1421d14ffa9Sbellard #ifdef DSERR_BUFFERTOOSMALL 1431d14ffa9Sbellard case DSERR_BUFFERTOOSMALL: 1441d14ffa9Sbellard str = "The buffer size is not great enough to enable effects processing"; 1451d14ffa9Sbellard break; 1461d14ffa9Sbellard #endif 1471d14ffa9Sbellard #ifdef DSERR_CONTROLUNAVAIL 1481d14ffa9Sbellard case DSERR_CONTROLUNAVAIL: 1491d14ffa9Sbellard 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"; 1501d14ffa9Sbellard break; 1511d14ffa9Sbellard #endif 1521d14ffa9Sbellard #ifdef DSERR_DS8_REQUIRED 1531d14ffa9Sbellard case DSERR_DS8_REQUIRED: 1541d14ffa9Sbellard str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface"; 1551d14ffa9Sbellard break; 1561d14ffa9Sbellard #endif 1571d14ffa9Sbellard #ifdef DSERR_FXUNAVAILABLE 1581d14ffa9Sbellard case DSERR_FXUNAVAILABLE: 1591d14ffa9Sbellard 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"; 1601d14ffa9Sbellard break; 1611d14ffa9Sbellard #endif 1621d14ffa9Sbellard #ifdef DSERR_GENERIC 1631d14ffa9Sbellard case DSERR_GENERIC : 1641d14ffa9Sbellard str = "An undetermined error occurred inside the DirectSound subsystem"; 1651d14ffa9Sbellard break; 1661d14ffa9Sbellard #endif 1671d14ffa9Sbellard #ifdef DSERR_INVALIDCALL 1681d14ffa9Sbellard case DSERR_INVALIDCALL: 1691d14ffa9Sbellard str = "This function is not valid for the current state of this object"; 1701d14ffa9Sbellard break; 1711d14ffa9Sbellard #endif 1721d14ffa9Sbellard #ifdef DSERR_INVALIDPARAM 1731d14ffa9Sbellard case DSERR_INVALIDPARAM: 1741d14ffa9Sbellard str = "An invalid parameter was passed to the returning function"; 1751d14ffa9Sbellard break; 1761d14ffa9Sbellard #endif 1771d14ffa9Sbellard #ifdef DSERR_NOAGGREGATION 1781d14ffa9Sbellard case DSERR_NOAGGREGATION: 1791d14ffa9Sbellard str = "The object does not support aggregation"; 1801d14ffa9Sbellard break; 1811d14ffa9Sbellard #endif 1821d14ffa9Sbellard #ifdef DSERR_NODRIVER 1831d14ffa9Sbellard case DSERR_NODRIVER: 1841d14ffa9Sbellard str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID"; 1851d14ffa9Sbellard break; 1861d14ffa9Sbellard #endif 1871d14ffa9Sbellard #ifdef DSERR_NOINTERFACE 1881d14ffa9Sbellard case DSERR_NOINTERFACE: 1891d14ffa9Sbellard str = "The requested COM interface is not available"; 1901d14ffa9Sbellard break; 1911d14ffa9Sbellard #endif 1921d14ffa9Sbellard #ifdef DSERR_OBJECTNOTFOUND 1931d14ffa9Sbellard case DSERR_OBJECTNOTFOUND: 1941d14ffa9Sbellard str = "The requested object was not found"; 1951d14ffa9Sbellard break; 1961d14ffa9Sbellard #endif 1971d14ffa9Sbellard #ifdef DSERR_OTHERAPPHASPRIO 1981d14ffa9Sbellard case DSERR_OTHERAPPHASPRIO: 1991d14ffa9Sbellard str = "Another application has a higher priority level, preventing this call from succeeding"; 2001d14ffa9Sbellard break; 2011d14ffa9Sbellard #endif 2021d14ffa9Sbellard #ifdef DSERR_OUTOFMEMORY 2031d14ffa9Sbellard case DSERR_OUTOFMEMORY: 2041d14ffa9Sbellard str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request"; 2051d14ffa9Sbellard break; 2061d14ffa9Sbellard #endif 2071d14ffa9Sbellard #ifdef DSERR_PRIOLEVELNEEDED 2081d14ffa9Sbellard case DSERR_PRIOLEVELNEEDED: 2091d14ffa9Sbellard str = "A cooperative level of DSSCL_PRIORITY or higher is required"; 2101d14ffa9Sbellard break; 2111d14ffa9Sbellard #endif 2121d14ffa9Sbellard #ifdef DSERR_SENDLOOP 2131d14ffa9Sbellard case DSERR_SENDLOOP: 2141d14ffa9Sbellard str = "A circular loop of send effects was detected"; 2151d14ffa9Sbellard break; 2161d14ffa9Sbellard #endif 2171d14ffa9Sbellard #ifdef DSERR_UNINITIALIZED 2181d14ffa9Sbellard case DSERR_UNINITIALIZED: 2191d14ffa9Sbellard str = "The Initialize method has not been called or has not been called successfully before other methods were called"; 2201d14ffa9Sbellard break; 2211d14ffa9Sbellard #endif 2221d14ffa9Sbellard #ifdef DSERR_UNSUPPORTED 2231d14ffa9Sbellard case DSERR_UNSUPPORTED: 2241d14ffa9Sbellard str = "The function called is not supported at this time"; 2251d14ffa9Sbellard break; 2261d14ffa9Sbellard #endif 2271d14ffa9Sbellard default: 2281d14ffa9Sbellard AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr); 2291d14ffa9Sbellard return; 2301d14ffa9Sbellard } 2311d14ffa9Sbellard 2321d14ffa9Sbellard AUD_log (AUDIO_CAP, "Reason: %s\n", str); 2331d14ffa9Sbellard } 2341d14ffa9Sbellard 2351d14ffa9Sbellard static void GCC_FMT_ATTR (2, 3) dsound_logerr ( 2361d14ffa9Sbellard HRESULT hr, 2371d14ffa9Sbellard const char *fmt, 2381d14ffa9Sbellard ... 2391d14ffa9Sbellard ) 2401d14ffa9Sbellard { 2411d14ffa9Sbellard va_list ap; 2421d14ffa9Sbellard 2431d14ffa9Sbellard va_start (ap, fmt); 2441d14ffa9Sbellard AUD_vlog (AUDIO_CAP, fmt, ap); 2451d14ffa9Sbellard va_end (ap); 2461d14ffa9Sbellard 2471d14ffa9Sbellard dsound_log_hresult (hr); 2481d14ffa9Sbellard } 2491d14ffa9Sbellard 2501d14ffa9Sbellard static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( 2511d14ffa9Sbellard HRESULT hr, 2521d14ffa9Sbellard const char *typ, 2531d14ffa9Sbellard const char *fmt, 2541d14ffa9Sbellard ... 2551d14ffa9Sbellard ) 2561d14ffa9Sbellard { 2571d14ffa9Sbellard va_list ap; 2581d14ffa9Sbellard 259c0fe3827Sbellard AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); 2601d14ffa9Sbellard va_start (ap, fmt); 2611d14ffa9Sbellard AUD_vlog (AUDIO_CAP, fmt, ap); 2621d14ffa9Sbellard va_end (ap); 2631d14ffa9Sbellard 2641d14ffa9Sbellard dsound_log_hresult (hr); 2651d14ffa9Sbellard } 2661d14ffa9Sbellard 2671d14ffa9Sbellard static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis) 2681d14ffa9Sbellard { 2691d14ffa9Sbellard return (millis * info->bytes_per_second) / 1000; 2701d14ffa9Sbellard } 2711d14ffa9Sbellard 2721d14ffa9Sbellard #ifdef DEBUG_DSOUND 2731d14ffa9Sbellard static void print_wave_format (WAVEFORMATEX *wfx) 2741d14ffa9Sbellard { 2751d14ffa9Sbellard dolog ("tag = %d\n", wfx->wFormatTag); 2761d14ffa9Sbellard dolog ("nChannels = %d\n", wfx->nChannels); 2771d14ffa9Sbellard dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec); 2781d14ffa9Sbellard dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec); 2791d14ffa9Sbellard dolog ("nBlockAlign = %d\n", wfx->nBlockAlign); 2801d14ffa9Sbellard dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample); 2811d14ffa9Sbellard dolog ("cbSize = %d\n", wfx->cbSize); 2821d14ffa9Sbellard } 2831d14ffa9Sbellard #endif 2841d14ffa9Sbellard 2851d14ffa9Sbellard static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb) 2861d14ffa9Sbellard { 2871d14ffa9Sbellard HRESULT hr; 2881d14ffa9Sbellard int i; 2891d14ffa9Sbellard 2901d14ffa9Sbellard for (i = 0; i < conf.restore_retries; ++i) { 2911d14ffa9Sbellard hr = IDirectSoundBuffer_Restore (dsb); 2921d14ffa9Sbellard 2931d14ffa9Sbellard switch (hr) { 2941d14ffa9Sbellard case DS_OK: 2951d14ffa9Sbellard return 0; 2961d14ffa9Sbellard 2971d14ffa9Sbellard case DSERR_BUFFERLOST: 2981d14ffa9Sbellard continue; 2991d14ffa9Sbellard 3001d14ffa9Sbellard default: 301c0fe3827Sbellard dsound_logerr (hr, "Could not restore playback buffer\n"); 3021d14ffa9Sbellard return -1; 3031d14ffa9Sbellard } 3041d14ffa9Sbellard } 3051d14ffa9Sbellard 3061d14ffa9Sbellard dolog ("%d attempts to restore playback buffer failed\n", i); 3071d14ffa9Sbellard return -1; 3081d14ffa9Sbellard } 3091d14ffa9Sbellard 310c0fe3827Sbellard static int waveformat_from_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as) 3111d14ffa9Sbellard { 3121d14ffa9Sbellard memset (wfx, 0, sizeof (*wfx)); 3131d14ffa9Sbellard 3141d14ffa9Sbellard wfx->wFormatTag = WAVE_FORMAT_PCM; 315c0fe3827Sbellard wfx->nChannels = as->nchannels; 316c0fe3827Sbellard wfx->nSamplesPerSec = as->freq; 317c0fe3827Sbellard wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2); 318c0fe3827Sbellard wfx->nBlockAlign = 1 << (as->nchannels == 2); 3191d14ffa9Sbellard wfx->cbSize = 0; 3201d14ffa9Sbellard 321c0fe3827Sbellard switch (as->fmt) { 3221d14ffa9Sbellard case AUD_FMT_S8: 3231d14ffa9Sbellard case AUD_FMT_U8: 3241d14ffa9Sbellard wfx->wBitsPerSample = 8; 3251d14ffa9Sbellard break; 3261d14ffa9Sbellard 3271d14ffa9Sbellard case AUD_FMT_S16: 328*ca9cc28cSbalrog case AUD_FMT_U16: 3291d14ffa9Sbellard wfx->wBitsPerSample = 16; 3301d14ffa9Sbellard wfx->nAvgBytesPerSec <<= 1; 3311d14ffa9Sbellard wfx->nBlockAlign <<= 1; 3321d14ffa9Sbellard break; 3331d14ffa9Sbellard 334*ca9cc28cSbalrog case AUD_FMT_S32: 335*ca9cc28cSbalrog case AUD_FMT_U32: 336*ca9cc28cSbalrog wfx->wBitsPerSample = 32; 337*ca9cc28cSbalrog wfx->nAvgBytesPerSec <<= 2; 338*ca9cc28cSbalrog wfx->nBlockAlign <<= 2; 3391d14ffa9Sbellard break; 3401d14ffa9Sbellard 3411d14ffa9Sbellard default: 342c0fe3827Sbellard dolog ("Internal logic error: Bad audio format %d\n", as->freq); 3431d14ffa9Sbellard return -1; 3441d14ffa9Sbellard } 3451d14ffa9Sbellard 3461d14ffa9Sbellard return 0; 3471d14ffa9Sbellard } 3481d14ffa9Sbellard 349c0fe3827Sbellard static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as) 3501d14ffa9Sbellard { 3511d14ffa9Sbellard if (wfx->wFormatTag != WAVE_FORMAT_PCM) { 3521d14ffa9Sbellard dolog ("Invalid wave format, tag is not PCM, but %d\n", 3531d14ffa9Sbellard wfx->wFormatTag); 3541d14ffa9Sbellard return -1; 3551d14ffa9Sbellard } 3561d14ffa9Sbellard 3571d14ffa9Sbellard if (!wfx->nSamplesPerSec) { 3581d14ffa9Sbellard dolog ("Invalid wave format, frequency is zero\n"); 3591d14ffa9Sbellard return -1; 3601d14ffa9Sbellard } 361c0fe3827Sbellard as->freq = wfx->nSamplesPerSec; 3621d14ffa9Sbellard 3631d14ffa9Sbellard switch (wfx->nChannels) { 3641d14ffa9Sbellard case 1: 365c0fe3827Sbellard as->nchannels = 1; 3661d14ffa9Sbellard break; 3671d14ffa9Sbellard 3681d14ffa9Sbellard case 2: 369c0fe3827Sbellard as->nchannels = 2; 3701d14ffa9Sbellard break; 3711d14ffa9Sbellard 3721d14ffa9Sbellard default: 3731d14ffa9Sbellard dolog ( 3741d14ffa9Sbellard "Invalid wave format, number of channels is not 1 or 2, but %d\n", 3751d14ffa9Sbellard wfx->nChannels 3761d14ffa9Sbellard ); 3771d14ffa9Sbellard return -1; 3781d14ffa9Sbellard } 3791d14ffa9Sbellard 3801d14ffa9Sbellard switch (wfx->wBitsPerSample) { 3811d14ffa9Sbellard case 8: 382c0fe3827Sbellard as->fmt = AUD_FMT_U8; 3831d14ffa9Sbellard break; 3841d14ffa9Sbellard 3851d14ffa9Sbellard case 16: 386c0fe3827Sbellard as->fmt = AUD_FMT_S16; 3871d14ffa9Sbellard break; 3881d14ffa9Sbellard 389*ca9cc28cSbalrog case 32: 390*ca9cc28cSbalrog as->fmt = AUD_FMT_S32; 391*ca9cc28cSbalrog break; 392*ca9cc28cSbalrog 3931d14ffa9Sbellard default: 394*ca9cc28cSbalrog dolog ("Invalid wave format, bits per sample is not " 395*ca9cc28cSbalrog "8, 16 or 32, but %d\n", 3961d14ffa9Sbellard wfx->wBitsPerSample); 3971d14ffa9Sbellard return -1; 3981d14ffa9Sbellard } 3991d14ffa9Sbellard 4001d14ffa9Sbellard return 0; 4011d14ffa9Sbellard } 4021d14ffa9Sbellard 4031d14ffa9Sbellard #include "dsound_template.h" 4041d14ffa9Sbellard #define DSBTYPE_IN 4051d14ffa9Sbellard #include "dsound_template.h" 4061d14ffa9Sbellard #undef DSBTYPE_IN 4071d14ffa9Sbellard 4081d14ffa9Sbellard static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp) 4091d14ffa9Sbellard { 4101d14ffa9Sbellard HRESULT hr; 4111d14ffa9Sbellard int i; 4121d14ffa9Sbellard 4131d14ffa9Sbellard for (i = 0; i < conf.getstatus_retries; ++i) { 4141d14ffa9Sbellard hr = IDirectSoundBuffer_GetStatus (dsb, statusp); 4151d14ffa9Sbellard if (FAILED (hr)) { 416c0fe3827Sbellard dsound_logerr (hr, "Could not get playback buffer status\n"); 4171d14ffa9Sbellard return -1; 4181d14ffa9Sbellard } 4191d14ffa9Sbellard 4201d14ffa9Sbellard if (*statusp & DSERR_BUFFERLOST) { 4211d14ffa9Sbellard if (dsound_restore_out (dsb)) { 4221d14ffa9Sbellard return -1; 4231d14ffa9Sbellard } 4241d14ffa9Sbellard continue; 4251d14ffa9Sbellard } 4261d14ffa9Sbellard break; 4271d14ffa9Sbellard } 4281d14ffa9Sbellard 4291d14ffa9Sbellard return 0; 4301d14ffa9Sbellard } 4311d14ffa9Sbellard 4321d14ffa9Sbellard static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb, 4331d14ffa9Sbellard DWORD *statusp) 4341d14ffa9Sbellard { 4351d14ffa9Sbellard HRESULT hr; 4361d14ffa9Sbellard 4371d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp); 4381d14ffa9Sbellard if (FAILED (hr)) { 439c0fe3827Sbellard dsound_logerr (hr, "Could not get capture buffer status\n"); 4401d14ffa9Sbellard return -1; 4411d14ffa9Sbellard } 4421d14ffa9Sbellard 4431d14ffa9Sbellard return 0; 4441d14ffa9Sbellard } 4451d14ffa9Sbellard 4461d14ffa9Sbellard static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len) 4471d14ffa9Sbellard { 4481d14ffa9Sbellard int src_len1 = dst_len; 4491d14ffa9Sbellard int src_len2 = 0; 4501d14ffa9Sbellard int pos = hw->rpos + dst_len; 4511d14ffa9Sbellard st_sample_t *src1 = hw->mix_buf + hw->rpos; 4521d14ffa9Sbellard st_sample_t *src2 = NULL; 4531d14ffa9Sbellard 4541d14ffa9Sbellard if (pos > hw->samples) { 4551d14ffa9Sbellard src_len1 = hw->samples - hw->rpos; 4561d14ffa9Sbellard src2 = hw->mix_buf; 4571d14ffa9Sbellard src_len2 = dst_len - src_len1; 4581d14ffa9Sbellard pos = src_len2; 4591d14ffa9Sbellard } 4601d14ffa9Sbellard 4611d14ffa9Sbellard if (src_len1) { 4621d14ffa9Sbellard hw->clip (dst, src1, src_len1); 4631d14ffa9Sbellard } 4641d14ffa9Sbellard 4651d14ffa9Sbellard if (src_len2) { 4661d14ffa9Sbellard dst = advance (dst, src_len1 << hw->info.shift); 4671d14ffa9Sbellard hw->clip (dst, src2, src_len2); 4681d14ffa9Sbellard } 4691d14ffa9Sbellard 4701d14ffa9Sbellard hw->rpos = pos % hw->samples; 4711d14ffa9Sbellard } 4721d14ffa9Sbellard 4731d14ffa9Sbellard static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb) 4741d14ffa9Sbellard { 4751d14ffa9Sbellard int err; 4761d14ffa9Sbellard LPVOID p1, p2; 4771d14ffa9Sbellard DWORD blen1, blen2, len1, len2; 4781d14ffa9Sbellard 4791d14ffa9Sbellard err = dsound_lock_out ( 4801d14ffa9Sbellard dsb, 4811d14ffa9Sbellard &hw->info, 4821d14ffa9Sbellard 0, 4831d14ffa9Sbellard hw->samples << hw->info.shift, 4841d14ffa9Sbellard &p1, &p2, 4851d14ffa9Sbellard &blen1, &blen2, 4861d14ffa9Sbellard 1 4871d14ffa9Sbellard ); 4881d14ffa9Sbellard if (err) { 4891d14ffa9Sbellard return; 4901d14ffa9Sbellard } 4911d14ffa9Sbellard 4921d14ffa9Sbellard len1 = blen1 >> hw->info.shift; 4931d14ffa9Sbellard len2 = blen2 >> hw->info.shift; 4941d14ffa9Sbellard 4951d14ffa9Sbellard #ifdef DEBUG_DSOUND 4961d14ffa9Sbellard dolog ("clear %p,%ld,%ld %p,%ld,%ld\n", 4971d14ffa9Sbellard p1, blen1, len1, 4981d14ffa9Sbellard p2, blen2, len2); 4991d14ffa9Sbellard #endif 5001d14ffa9Sbellard 5011d14ffa9Sbellard if (p1 && len1) { 5021d14ffa9Sbellard audio_pcm_info_clear_buf (&hw->info, p1, len1); 5031d14ffa9Sbellard } 5041d14ffa9Sbellard 5051d14ffa9Sbellard if (p2 && len2) { 5061d14ffa9Sbellard audio_pcm_info_clear_buf (&hw->info, p2, len2); 5071d14ffa9Sbellard } 5081d14ffa9Sbellard 5091d14ffa9Sbellard dsound_unlock_out (dsb, p1, p2, blen1, blen2); 5101d14ffa9Sbellard } 5111d14ffa9Sbellard 5121d14ffa9Sbellard static void dsound_close (dsound *s) 5131d14ffa9Sbellard { 5141d14ffa9Sbellard HRESULT hr; 5151d14ffa9Sbellard 5161d14ffa9Sbellard if (s->dsound_primary_buffer) { 5171d14ffa9Sbellard hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer); 5181d14ffa9Sbellard if (FAILED (hr)) { 519c0fe3827Sbellard dsound_logerr (hr, "Could not release primary buffer\n"); 5201d14ffa9Sbellard } 5211d14ffa9Sbellard s->dsound_primary_buffer = NULL; 5221d14ffa9Sbellard } 5231d14ffa9Sbellard } 5241d14ffa9Sbellard 5251d14ffa9Sbellard static int dsound_open (dsound *s) 5261d14ffa9Sbellard { 5271d14ffa9Sbellard int err; 5281d14ffa9Sbellard HRESULT hr; 5291d14ffa9Sbellard WAVEFORMATEX wfx; 5301d14ffa9Sbellard DSBUFFERDESC dsbd; 5311d14ffa9Sbellard HWND hwnd; 5321d14ffa9Sbellard 5331d14ffa9Sbellard hwnd = GetForegroundWindow (); 5341d14ffa9Sbellard hr = IDirectSound_SetCooperativeLevel ( 5351d14ffa9Sbellard s->dsound, 5361d14ffa9Sbellard hwnd, 5371d14ffa9Sbellard DSSCL_PRIORITY 5381d14ffa9Sbellard ); 5391d14ffa9Sbellard 5401d14ffa9Sbellard if (FAILED (hr)) { 541c0fe3827Sbellard dsound_logerr (hr, "Could not set cooperative level for window %p\n", 5421d14ffa9Sbellard hwnd); 5431d14ffa9Sbellard return -1; 5441d14ffa9Sbellard } 5451d14ffa9Sbellard 5461d14ffa9Sbellard if (!conf.set_primary) { 5471d14ffa9Sbellard return 0; 5481d14ffa9Sbellard } 5491d14ffa9Sbellard 550c0fe3827Sbellard err = waveformat_from_audio_settings (&wfx, &conf.settings); 5511d14ffa9Sbellard if (err) { 5521d14ffa9Sbellard return -1; 5531d14ffa9Sbellard } 5541d14ffa9Sbellard 5551d14ffa9Sbellard memset (&dsbd, 0, sizeof (dsbd)); 5561d14ffa9Sbellard dsbd.dwSize = sizeof (dsbd); 5571d14ffa9Sbellard dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; 5581d14ffa9Sbellard dsbd.dwBufferBytes = 0; 5591d14ffa9Sbellard dsbd.lpwfxFormat = NULL; 5601d14ffa9Sbellard 5611d14ffa9Sbellard hr = IDirectSound_CreateSoundBuffer ( 5621d14ffa9Sbellard s->dsound, 5631d14ffa9Sbellard &dsbd, 5641d14ffa9Sbellard &s->dsound_primary_buffer, 5651d14ffa9Sbellard NULL 5661d14ffa9Sbellard ); 5671d14ffa9Sbellard if (FAILED (hr)) { 568c0fe3827Sbellard dsound_logerr (hr, "Could not create primary playback buffer\n"); 5691d14ffa9Sbellard return -1; 5701d14ffa9Sbellard } 5711d14ffa9Sbellard 5721d14ffa9Sbellard hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx); 5731d14ffa9Sbellard if (FAILED (hr)) { 574c0fe3827Sbellard dsound_logerr (hr, "Could not set primary playback buffer format\n"); 5751d14ffa9Sbellard } 5761d14ffa9Sbellard 5771d14ffa9Sbellard hr = IDirectSoundBuffer_GetFormat ( 5781d14ffa9Sbellard s->dsound_primary_buffer, 5791d14ffa9Sbellard &wfx, 5801d14ffa9Sbellard sizeof (wfx), 5811d14ffa9Sbellard NULL 5821d14ffa9Sbellard ); 5831d14ffa9Sbellard if (FAILED (hr)) { 584c0fe3827Sbellard dsound_logerr (hr, "Could not get primary playback buffer format\n"); 5851d14ffa9Sbellard goto fail0; 5861d14ffa9Sbellard } 5871d14ffa9Sbellard 5881d14ffa9Sbellard #ifdef DEBUG_DSOUND 5891d14ffa9Sbellard dolog ("Primary\n"); 5901d14ffa9Sbellard print_wave_format (&wfx); 5911d14ffa9Sbellard #endif 5921d14ffa9Sbellard 593c0fe3827Sbellard err = waveformat_to_audio_settings (&wfx, &s->settings); 5941d14ffa9Sbellard if (err) { 5951d14ffa9Sbellard goto fail0; 5961d14ffa9Sbellard } 5971d14ffa9Sbellard 5981d14ffa9Sbellard return 0; 5991d14ffa9Sbellard 6001d14ffa9Sbellard fail0: 6011d14ffa9Sbellard dsound_close (s); 6021d14ffa9Sbellard return -1; 6031d14ffa9Sbellard } 6041d14ffa9Sbellard 6051d14ffa9Sbellard static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...) 6061d14ffa9Sbellard { 6071d14ffa9Sbellard HRESULT hr; 6081d14ffa9Sbellard DWORD status; 6091d14ffa9Sbellard DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 6101d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 6111d14ffa9Sbellard 6121d14ffa9Sbellard if (!dsb) { 6131d14ffa9Sbellard dolog ("Attempt to control voice without a buffer\n"); 6141d14ffa9Sbellard return 0; 6151d14ffa9Sbellard } 6161d14ffa9Sbellard 6171d14ffa9Sbellard switch (cmd) { 6181d14ffa9Sbellard case VOICE_ENABLE: 6191d14ffa9Sbellard if (dsound_get_status_out (dsb, &status)) { 6201d14ffa9Sbellard return -1; 6211d14ffa9Sbellard } 6221d14ffa9Sbellard 6231d14ffa9Sbellard if (status & DSBSTATUS_PLAYING) { 624c0fe3827Sbellard dolog ("warning: Voice is already playing\n"); 6251d14ffa9Sbellard return 0; 6261d14ffa9Sbellard } 6271d14ffa9Sbellard 6281d14ffa9Sbellard dsound_clear_sample (hw, dsb); 6291d14ffa9Sbellard 6301d14ffa9Sbellard hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); 6311d14ffa9Sbellard if (FAILED (hr)) { 632c0fe3827Sbellard dsound_logerr (hr, "Could not start playing buffer\n"); 6331d14ffa9Sbellard return -1; 6341d14ffa9Sbellard } 6351d14ffa9Sbellard break; 6361d14ffa9Sbellard 6371d14ffa9Sbellard case VOICE_DISABLE: 6381d14ffa9Sbellard if (dsound_get_status_out (dsb, &status)) { 6391d14ffa9Sbellard return -1; 6401d14ffa9Sbellard } 6411d14ffa9Sbellard 6421d14ffa9Sbellard if (status & DSBSTATUS_PLAYING) { 6431d14ffa9Sbellard hr = IDirectSoundBuffer_Stop (dsb); 6441d14ffa9Sbellard if (FAILED (hr)) { 645c0fe3827Sbellard dsound_logerr (hr, "Could not stop playing buffer\n"); 6461d14ffa9Sbellard return -1; 6471d14ffa9Sbellard } 6481d14ffa9Sbellard } 6491d14ffa9Sbellard else { 650c0fe3827Sbellard dolog ("warning: Voice is not playing\n"); 6511d14ffa9Sbellard } 6521d14ffa9Sbellard break; 6531d14ffa9Sbellard } 6541d14ffa9Sbellard return 0; 6551d14ffa9Sbellard } 6561d14ffa9Sbellard 6571d14ffa9Sbellard static int dsound_write (SWVoiceOut *sw, void *buf, int len) 6581d14ffa9Sbellard { 6591d14ffa9Sbellard return audio_pcm_sw_write (sw, buf, len); 6601d14ffa9Sbellard } 6611d14ffa9Sbellard 6621d14ffa9Sbellard static int dsound_run_out (HWVoiceOut *hw) 6631d14ffa9Sbellard { 6641d14ffa9Sbellard int err; 6651d14ffa9Sbellard HRESULT hr; 6661d14ffa9Sbellard DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 6671d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 6681d14ffa9Sbellard int live, len, hwshift; 6691d14ffa9Sbellard DWORD blen1, blen2; 6701d14ffa9Sbellard DWORD len1, len2; 6711d14ffa9Sbellard DWORD decr; 6721d14ffa9Sbellard DWORD wpos, ppos, old_pos; 6731d14ffa9Sbellard LPVOID p1, p2; 674c0fe3827Sbellard int bufsize; 6751d14ffa9Sbellard 6761d14ffa9Sbellard if (!dsb) { 6771d14ffa9Sbellard dolog ("Attempt to run empty with playback buffer\n"); 6781d14ffa9Sbellard return 0; 6791d14ffa9Sbellard } 6801d14ffa9Sbellard 6811d14ffa9Sbellard hwshift = hw->info.shift; 682c0fe3827Sbellard bufsize = hw->samples << hwshift; 6831d14ffa9Sbellard 6841d14ffa9Sbellard live = audio_pcm_hw_get_live_out (hw); 6851d14ffa9Sbellard 6861d14ffa9Sbellard hr = IDirectSoundBuffer_GetCurrentPosition ( 6871d14ffa9Sbellard dsb, 6881d14ffa9Sbellard &ppos, 6891d14ffa9Sbellard ds->first_time ? &wpos : NULL 6901d14ffa9Sbellard ); 6911d14ffa9Sbellard if (FAILED (hr)) { 692c0fe3827Sbellard dsound_logerr (hr, "Could not get playback buffer position\n"); 6931d14ffa9Sbellard return 0; 6941d14ffa9Sbellard } 6951d14ffa9Sbellard 6961d14ffa9Sbellard len = live << hwshift; 6971d14ffa9Sbellard 6981d14ffa9Sbellard if (ds->first_time) { 6991d14ffa9Sbellard if (conf.latency_millis) { 700c0fe3827Sbellard DWORD cur_blat; 7011d14ffa9Sbellard 702c0fe3827Sbellard cur_blat = audio_ring_dist (wpos, ppos, bufsize); 7031d14ffa9Sbellard ds->first_time = 0; 7041d14ffa9Sbellard old_pos = wpos; 7051d14ffa9Sbellard old_pos += 7061d14ffa9Sbellard millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat; 707c0fe3827Sbellard old_pos %= bufsize; 7081d14ffa9Sbellard old_pos &= ~hw->info.align; 7091d14ffa9Sbellard } 7101d14ffa9Sbellard else { 7111d14ffa9Sbellard old_pos = wpos; 7121d14ffa9Sbellard } 7131d14ffa9Sbellard #ifdef DEBUG_DSOUND 7141d14ffa9Sbellard ds->played = 0; 7151d14ffa9Sbellard ds->mixed = 0; 7161d14ffa9Sbellard #endif 7171d14ffa9Sbellard } 7181d14ffa9Sbellard else { 7191d14ffa9Sbellard if (ds->old_pos == ppos) { 7201d14ffa9Sbellard #ifdef DEBUG_DSOUND 7211d14ffa9Sbellard dolog ("old_pos == ppos\n"); 7221d14ffa9Sbellard #endif 7231d14ffa9Sbellard return 0; 7241d14ffa9Sbellard } 7251d14ffa9Sbellard 7261d14ffa9Sbellard #ifdef DEBUG_DSOUND 7271d14ffa9Sbellard ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize); 7281d14ffa9Sbellard #endif 7291d14ffa9Sbellard old_pos = ds->old_pos; 7301d14ffa9Sbellard } 7311d14ffa9Sbellard 7321d14ffa9Sbellard if ((old_pos < ppos) && ((old_pos + len) > ppos)) { 7331d14ffa9Sbellard len = ppos - old_pos; 7341d14ffa9Sbellard } 7351d14ffa9Sbellard else { 736c0fe3827Sbellard if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) { 737c0fe3827Sbellard len = bufsize - old_pos + ppos; 7381d14ffa9Sbellard } 7391d14ffa9Sbellard } 7401d14ffa9Sbellard 741c0fe3827Sbellard if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) { 742c0fe3827Sbellard dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n", 743c0fe3827Sbellard len, bufsize, old_pos, ppos); 7441d14ffa9Sbellard return 0; 7451d14ffa9Sbellard } 7461d14ffa9Sbellard 7471d14ffa9Sbellard len &= ~hw->info.align; 7481d14ffa9Sbellard if (!len) { 7491d14ffa9Sbellard return 0; 7501d14ffa9Sbellard } 7511d14ffa9Sbellard 7521d14ffa9Sbellard #ifdef DEBUG_DSOUND 7531d14ffa9Sbellard ds->old_ppos = ppos; 7541d14ffa9Sbellard #endif 7551d14ffa9Sbellard err = dsound_lock_out ( 7561d14ffa9Sbellard dsb, 7571d14ffa9Sbellard &hw->info, 7581d14ffa9Sbellard old_pos, 7591d14ffa9Sbellard len, 7601d14ffa9Sbellard &p1, &p2, 7611d14ffa9Sbellard &blen1, &blen2, 7621d14ffa9Sbellard 0 7631d14ffa9Sbellard ); 7641d14ffa9Sbellard if (err) { 7651d14ffa9Sbellard return 0; 7661d14ffa9Sbellard } 7671d14ffa9Sbellard 7681d14ffa9Sbellard len1 = blen1 >> hwshift; 7691d14ffa9Sbellard len2 = blen2 >> hwshift; 7701d14ffa9Sbellard decr = len1 + len2; 7711d14ffa9Sbellard 7721d14ffa9Sbellard if (p1 && len1) { 7731d14ffa9Sbellard dsound_write_sample (hw, p1, len1); 7741d14ffa9Sbellard } 7751d14ffa9Sbellard 7761d14ffa9Sbellard if (p2 && len2) { 7771d14ffa9Sbellard dsound_write_sample (hw, p2, len2); 7781d14ffa9Sbellard } 7791d14ffa9Sbellard 7801d14ffa9Sbellard dsound_unlock_out (dsb, p1, p2, blen1, blen2); 781c0fe3827Sbellard ds->old_pos = (old_pos + (decr << hwshift)) % bufsize; 7821d14ffa9Sbellard 7831d14ffa9Sbellard #ifdef DEBUG_DSOUND 7841d14ffa9Sbellard ds->mixed += decr << hwshift; 7851d14ffa9Sbellard 7861d14ffa9Sbellard dolog ("played %lu mixed %lu diff %ld sec %f\n", 7871d14ffa9Sbellard ds->played, 7881d14ffa9Sbellard ds->mixed, 7891d14ffa9Sbellard ds->mixed - ds->played, 7901d14ffa9Sbellard abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second); 7911d14ffa9Sbellard #endif 7921d14ffa9Sbellard return decr; 7931d14ffa9Sbellard } 7941d14ffa9Sbellard 7951d14ffa9Sbellard static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...) 7961d14ffa9Sbellard { 7971d14ffa9Sbellard HRESULT hr; 7981d14ffa9Sbellard DWORD status; 7991d14ffa9Sbellard DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 8001d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 8011d14ffa9Sbellard 8021d14ffa9Sbellard if (!dscb) { 8031d14ffa9Sbellard dolog ("Attempt to control capture voice without a buffer\n"); 8041d14ffa9Sbellard return -1; 8051d14ffa9Sbellard } 8061d14ffa9Sbellard 8071d14ffa9Sbellard switch (cmd) { 8081d14ffa9Sbellard case VOICE_ENABLE: 8091d14ffa9Sbellard if (dsound_get_status_in (dscb, &status)) { 8101d14ffa9Sbellard return -1; 8111d14ffa9Sbellard } 8121d14ffa9Sbellard 8131d14ffa9Sbellard if (status & DSCBSTATUS_CAPTURING) { 814c0fe3827Sbellard dolog ("warning: Voice is already capturing\n"); 8151d14ffa9Sbellard return 0; 8161d14ffa9Sbellard } 8171d14ffa9Sbellard 8181d14ffa9Sbellard /* clear ?? */ 8191d14ffa9Sbellard 8201d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); 8211d14ffa9Sbellard if (FAILED (hr)) { 822c0fe3827Sbellard dsound_logerr (hr, "Could not start capturing\n"); 8231d14ffa9Sbellard return -1; 8241d14ffa9Sbellard } 8251d14ffa9Sbellard break; 8261d14ffa9Sbellard 8271d14ffa9Sbellard case VOICE_DISABLE: 8281d14ffa9Sbellard if (dsound_get_status_in (dscb, &status)) { 8291d14ffa9Sbellard return -1; 8301d14ffa9Sbellard } 8311d14ffa9Sbellard 8321d14ffa9Sbellard if (status & DSCBSTATUS_CAPTURING) { 8331d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_Stop (dscb); 8341d14ffa9Sbellard if (FAILED (hr)) { 835c0fe3827Sbellard dsound_logerr (hr, "Could not stop capturing\n"); 8361d14ffa9Sbellard return -1; 8371d14ffa9Sbellard } 8381d14ffa9Sbellard } 8391d14ffa9Sbellard else { 840c0fe3827Sbellard dolog ("warning: Voice is not capturing\n"); 8411d14ffa9Sbellard } 8421d14ffa9Sbellard break; 8431d14ffa9Sbellard } 8441d14ffa9Sbellard return 0; 8451d14ffa9Sbellard } 8461d14ffa9Sbellard 8471d14ffa9Sbellard static int dsound_read (SWVoiceIn *sw, void *buf, int len) 8481d14ffa9Sbellard { 8491d14ffa9Sbellard return audio_pcm_sw_read (sw, buf, len); 8501d14ffa9Sbellard } 8511d14ffa9Sbellard 8521d14ffa9Sbellard static int dsound_run_in (HWVoiceIn *hw) 8531d14ffa9Sbellard { 8541d14ffa9Sbellard int err; 8551d14ffa9Sbellard HRESULT hr; 8561d14ffa9Sbellard DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 8571d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 8581d14ffa9Sbellard int live, len, dead; 8591d14ffa9Sbellard DWORD blen1, blen2; 8601d14ffa9Sbellard DWORD len1, len2; 8611d14ffa9Sbellard DWORD decr; 8621d14ffa9Sbellard DWORD cpos, rpos; 8631d14ffa9Sbellard LPVOID p1, p2; 8641d14ffa9Sbellard int hwshift; 8651d14ffa9Sbellard 8661d14ffa9Sbellard if (!dscb) { 8671d14ffa9Sbellard dolog ("Attempt to run without capture buffer\n"); 8681d14ffa9Sbellard return 0; 8691d14ffa9Sbellard } 8701d14ffa9Sbellard 8711d14ffa9Sbellard hwshift = hw->info.shift; 8721d14ffa9Sbellard 8731d14ffa9Sbellard live = audio_pcm_hw_get_live_in (hw); 8741d14ffa9Sbellard dead = hw->samples - live; 8751d14ffa9Sbellard if (!dead) { 8761d14ffa9Sbellard return 0; 8771d14ffa9Sbellard } 8781d14ffa9Sbellard 8791d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_GetCurrentPosition ( 8801d14ffa9Sbellard dscb, 8811d14ffa9Sbellard &cpos, 8821d14ffa9Sbellard ds->first_time ? &rpos : NULL 8831d14ffa9Sbellard ); 8841d14ffa9Sbellard if (FAILED (hr)) { 885c0fe3827Sbellard dsound_logerr (hr, "Could not get capture buffer position\n"); 8861d14ffa9Sbellard return 0; 8871d14ffa9Sbellard } 8881d14ffa9Sbellard 8891d14ffa9Sbellard if (ds->first_time) { 8901d14ffa9Sbellard ds->first_time = 0; 8911d14ffa9Sbellard if (rpos & hw->info.align) { 892c0fe3827Sbellard ldebug ("warning: Misaligned capture read position %ld(%d)\n", 8931d14ffa9Sbellard rpos, hw->info.align); 8941d14ffa9Sbellard } 8951d14ffa9Sbellard hw->wpos = rpos >> hwshift; 8961d14ffa9Sbellard } 8971d14ffa9Sbellard 8981d14ffa9Sbellard if (cpos & hw->info.align) { 899c0fe3827Sbellard ldebug ("warning: Misaligned capture position %ld(%d)\n", 9001d14ffa9Sbellard cpos, hw->info.align); 9011d14ffa9Sbellard } 9021d14ffa9Sbellard cpos >>= hwshift; 9031d14ffa9Sbellard 9041d14ffa9Sbellard len = audio_ring_dist (cpos, hw->wpos, hw->samples); 9051d14ffa9Sbellard if (!len) { 9061d14ffa9Sbellard return 0; 9071d14ffa9Sbellard } 9081d14ffa9Sbellard len = audio_MIN (len, dead); 9091d14ffa9Sbellard 9101d14ffa9Sbellard err = dsound_lock_in ( 9111d14ffa9Sbellard dscb, 9121d14ffa9Sbellard &hw->info, 9131d14ffa9Sbellard hw->wpos << hwshift, 9141d14ffa9Sbellard len << hwshift, 9151d14ffa9Sbellard &p1, 9161d14ffa9Sbellard &p2, 9171d14ffa9Sbellard &blen1, 9181d14ffa9Sbellard &blen2, 9191d14ffa9Sbellard 0 9201d14ffa9Sbellard ); 9211d14ffa9Sbellard if (err) { 9221d14ffa9Sbellard return 0; 9231d14ffa9Sbellard } 9241d14ffa9Sbellard 9251d14ffa9Sbellard len1 = blen1 >> hwshift; 9261d14ffa9Sbellard len2 = blen2 >> hwshift; 9271d14ffa9Sbellard decr = len1 + len2; 9281d14ffa9Sbellard 9291d14ffa9Sbellard if (p1 && len1) { 9301d14ffa9Sbellard hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume); 9311d14ffa9Sbellard } 9321d14ffa9Sbellard 9331d14ffa9Sbellard if (p2 && len2) { 9341d14ffa9Sbellard hw->conv (hw->conv_buf, p2, len2, &nominal_volume); 9351d14ffa9Sbellard } 9361d14ffa9Sbellard 9371d14ffa9Sbellard dsound_unlock_in (dscb, p1, p2, blen1, blen2); 9381d14ffa9Sbellard hw->wpos = (hw->wpos + decr) % hw->samples; 9391d14ffa9Sbellard return decr; 9401d14ffa9Sbellard } 9411d14ffa9Sbellard 9421d14ffa9Sbellard static void dsound_audio_fini (void *opaque) 9431d14ffa9Sbellard { 9441d14ffa9Sbellard HRESULT hr; 9451d14ffa9Sbellard dsound *s = opaque; 9461d14ffa9Sbellard 9471d14ffa9Sbellard if (!s->dsound) { 9481d14ffa9Sbellard return; 9491d14ffa9Sbellard } 9501d14ffa9Sbellard 9511d14ffa9Sbellard hr = IDirectSound_Release (s->dsound); 9521d14ffa9Sbellard if (FAILED (hr)) { 953c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSound\n"); 9541d14ffa9Sbellard } 9551d14ffa9Sbellard s->dsound = NULL; 9561d14ffa9Sbellard 9571d14ffa9Sbellard if (!s->dsound_capture) { 9581d14ffa9Sbellard return; 9591d14ffa9Sbellard } 9601d14ffa9Sbellard 9611d14ffa9Sbellard hr = IDirectSoundCapture_Release (s->dsound_capture); 9621d14ffa9Sbellard if (FAILED (hr)) { 963c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 9641d14ffa9Sbellard } 9651d14ffa9Sbellard s->dsound_capture = NULL; 9661d14ffa9Sbellard } 9671d14ffa9Sbellard 9681d14ffa9Sbellard static void *dsound_audio_init (void) 9691d14ffa9Sbellard { 9701d14ffa9Sbellard int err; 9711d14ffa9Sbellard HRESULT hr; 9721d14ffa9Sbellard dsound *s = &glob_dsound; 9731d14ffa9Sbellard 9741d14ffa9Sbellard hr = CoInitialize (NULL); 9751d14ffa9Sbellard if (FAILED (hr)) { 976c0fe3827Sbellard dsound_logerr (hr, "Could not initialize COM\n"); 9771d14ffa9Sbellard return NULL; 9781d14ffa9Sbellard } 9791d14ffa9Sbellard 9801d14ffa9Sbellard hr = CoCreateInstance ( 9811d14ffa9Sbellard &CLSID_DirectSound, 9821d14ffa9Sbellard NULL, 9831d14ffa9Sbellard CLSCTX_ALL, 9841d14ffa9Sbellard &IID_IDirectSound, 9851d14ffa9Sbellard (void **) &s->dsound 9861d14ffa9Sbellard ); 9871d14ffa9Sbellard if (FAILED (hr)) { 988c0fe3827Sbellard dsound_logerr (hr, "Could not create DirectSound instance\n"); 9891d14ffa9Sbellard return NULL; 9901d14ffa9Sbellard } 9911d14ffa9Sbellard 9921d14ffa9Sbellard hr = IDirectSound_Initialize (s->dsound, NULL); 9931d14ffa9Sbellard if (FAILED (hr)) { 994c0fe3827Sbellard dsound_logerr (hr, "Could not initialize DirectSound\n"); 9958ead62cfSbellard 9968ead62cfSbellard hr = IDirectSound_Release (s->dsound); 9978ead62cfSbellard if (FAILED (hr)) { 9988ead62cfSbellard dsound_logerr (hr, "Could not release DirectSound\n"); 9998ead62cfSbellard } 10008ead62cfSbellard s->dsound = NULL; 10011d14ffa9Sbellard return NULL; 10021d14ffa9Sbellard } 10031d14ffa9Sbellard 10041d14ffa9Sbellard hr = CoCreateInstance ( 10051d14ffa9Sbellard &CLSID_DirectSoundCapture, 10061d14ffa9Sbellard NULL, 10071d14ffa9Sbellard CLSCTX_ALL, 10081d14ffa9Sbellard &IID_IDirectSoundCapture, 10091d14ffa9Sbellard (void **) &s->dsound_capture 10101d14ffa9Sbellard ); 10111d14ffa9Sbellard if (FAILED (hr)) { 1012c0fe3827Sbellard dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); 10131d14ffa9Sbellard } 10141d14ffa9Sbellard else { 10151d14ffa9Sbellard hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); 10161d14ffa9Sbellard if (FAILED (hr)) { 1017c0fe3827Sbellard dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); 10181d14ffa9Sbellard 10191d14ffa9Sbellard hr = IDirectSoundCapture_Release (s->dsound_capture); 10201d14ffa9Sbellard if (FAILED (hr)) { 1021c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 10221d14ffa9Sbellard } 10231d14ffa9Sbellard s->dsound_capture = NULL; 10241d14ffa9Sbellard } 10251d14ffa9Sbellard } 10261d14ffa9Sbellard 10271d14ffa9Sbellard err = dsound_open (s); 10281d14ffa9Sbellard if (err) { 10291d14ffa9Sbellard dsound_audio_fini (s); 10301d14ffa9Sbellard return NULL; 10311d14ffa9Sbellard } 10321d14ffa9Sbellard 10331d14ffa9Sbellard return s; 10341d14ffa9Sbellard } 10351d14ffa9Sbellard 10361d14ffa9Sbellard static struct audio_option dsound_options[] = { 10371d14ffa9Sbellard {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries, 10381d14ffa9Sbellard "Number of times to attempt locking the buffer", NULL, 0}, 10391d14ffa9Sbellard {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries, 10401d14ffa9Sbellard "Number of times to attempt restoring the buffer", NULL, 0}, 10411d14ffa9Sbellard {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries, 10421d14ffa9Sbellard "Number of times to attempt getting status of the buffer", NULL, 0}, 10431d14ffa9Sbellard {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary, 10441d14ffa9Sbellard "Set the parameters of primary buffer", NULL, 0}, 10451d14ffa9Sbellard {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis, 10461d14ffa9Sbellard "(undocumented)", NULL, 0}, 1047c0fe3827Sbellard {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq, 10481d14ffa9Sbellard "Primary buffer frequency", NULL, 0}, 1049c0fe3827Sbellard {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels, 10501d14ffa9Sbellard "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0}, 1051c0fe3827Sbellard {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt, 10521d14ffa9Sbellard "Primary buffer format", NULL, 0}, 10531d14ffa9Sbellard {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out, 10541d14ffa9Sbellard "(undocumented)", NULL, 0}, 10551d14ffa9Sbellard {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in, 10561d14ffa9Sbellard "(undocumented)", NULL, 0}, 10571d14ffa9Sbellard {NULL, 0, NULL, NULL, NULL, 0} 10581d14ffa9Sbellard }; 10591d14ffa9Sbellard 10601d14ffa9Sbellard static struct audio_pcm_ops dsound_pcm_ops = { 10611d14ffa9Sbellard dsound_init_out, 10621d14ffa9Sbellard dsound_fini_out, 10631d14ffa9Sbellard dsound_run_out, 10641d14ffa9Sbellard dsound_write, 10651d14ffa9Sbellard dsound_ctl_out, 10661d14ffa9Sbellard 10671d14ffa9Sbellard dsound_init_in, 10681d14ffa9Sbellard dsound_fini_in, 10691d14ffa9Sbellard dsound_run_in, 10701d14ffa9Sbellard dsound_read, 10711d14ffa9Sbellard dsound_ctl_in 10721d14ffa9Sbellard }; 10731d14ffa9Sbellard 10741d14ffa9Sbellard struct audio_driver dsound_audio_driver = { 10751d14ffa9Sbellard INIT_FIELD (name = ) "dsound", 10761d14ffa9Sbellard INIT_FIELD (descr = ) 10771d14ffa9Sbellard "DirectSound http://wikipedia.org/wiki/DirectSound", 10781d14ffa9Sbellard INIT_FIELD (options = ) dsound_options, 10791d14ffa9Sbellard INIT_FIELD (init = ) dsound_audio_init, 10801d14ffa9Sbellard INIT_FIELD (fini = ) dsound_audio_fini, 10811d14ffa9Sbellard INIT_FIELD (pcm_ops = ) &dsound_pcm_ops, 10821d14ffa9Sbellard INIT_FIELD (can_be_default = ) 1, 10831d14ffa9Sbellard INIT_FIELD (max_voices_out = ) INT_MAX, 10841d14ffa9Sbellard INIT_FIELD (max_voices_in = ) 1, 10851d14ffa9Sbellard INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut), 10861d14ffa9Sbellard INIT_FIELD (voice_size_in = ) sizeof (DSoundVoiceIn) 10871d14ffa9Sbellard }; 1088