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 296086a565SPeter Maydell #include "qemu/osdep.h" 30749bc4bfSpbrook #include "audio.h" 311d14ffa9Sbellard 321d14ffa9Sbellard #define AUDIO_CAP "dsound" 331d14ffa9Sbellard #include "audio_int.h" 344a3b8b34SKővágó, Zoltán #include "qemu/host-utils.h" 350b8fa32fSMarkus Armbruster #include "qemu/module.h" 361d14ffa9Sbellard 371d14ffa9Sbellard #include <windows.h> 384fddf62aSths #include <mmsystem.h> 391d14ffa9Sbellard #include <objbase.h> 401d14ffa9Sbellard #include <dsound.h> 411d14ffa9Sbellard 42d5631638Smalc #include "audio_win_int.h" 43d5631638Smalc 441d14ffa9Sbellard /* #define DEBUG_DSOUND */ 451d14ffa9Sbellard 46191e1f0aSKővágó, Zoltán typedef struct { 471d14ffa9Sbellard LPDIRECTSOUND dsound; 481d14ffa9Sbellard LPDIRECTSOUNDCAPTURE dsound_capture; 491ea879e5Smalc struct audsettings settings; 504a3b8b34SKővágó, Zoltán Audiodev *dev; 511d14ffa9Sbellard } dsound; 521d14ffa9Sbellard 531d14ffa9Sbellard typedef struct { 541d14ffa9Sbellard HWVoiceOut hw; 551d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsound_buffer; 56191e1f0aSKővágó, Zoltán dsound *s; 571d14ffa9Sbellard } DSoundVoiceOut; 581d14ffa9Sbellard 591d14ffa9Sbellard typedef struct { 601d14ffa9Sbellard HWVoiceIn hw; 611d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; 62191e1f0aSKővágó, Zoltán dsound *s; 631d14ffa9Sbellard } DSoundVoiceIn; 641d14ffa9Sbellard 651d14ffa9Sbellard static void dsound_log_hresult (HRESULT hr) 661d14ffa9Sbellard { 671d14ffa9Sbellard const char *str = "BUG"; 681d14ffa9Sbellard 691d14ffa9Sbellard switch (hr) { 701d14ffa9Sbellard case DS_OK: 711d14ffa9Sbellard str = "The method succeeded"; 721d14ffa9Sbellard break; 731d14ffa9Sbellard #ifdef DS_NO_VIRTUALIZATION 741d14ffa9Sbellard case DS_NO_VIRTUALIZATION: 751d14ffa9Sbellard str = "The buffer was created, but another 3D algorithm was substituted"; 761d14ffa9Sbellard break; 771d14ffa9Sbellard #endif 781d14ffa9Sbellard #ifdef DS_INCOMPLETE 791d14ffa9Sbellard case DS_INCOMPLETE: 801d14ffa9Sbellard str = "The method succeeded, but not all the optional effects were obtained"; 811d14ffa9Sbellard break; 821d14ffa9Sbellard #endif 831d14ffa9Sbellard #ifdef DSERR_ACCESSDENIED 841d14ffa9Sbellard case DSERR_ACCESSDENIED: 851d14ffa9Sbellard str = "The request failed because access was denied"; 861d14ffa9Sbellard break; 871d14ffa9Sbellard #endif 881d14ffa9Sbellard #ifdef DSERR_ALLOCATED 891d14ffa9Sbellard case DSERR_ALLOCATED: 901d14ffa9Sbellard str = "The request failed because resources, such as a priority level, were already in use by another caller"; 911d14ffa9Sbellard break; 921d14ffa9Sbellard #endif 931d14ffa9Sbellard #ifdef DSERR_ALREADYINITIALIZED 941d14ffa9Sbellard case DSERR_ALREADYINITIALIZED: 951d14ffa9Sbellard str = "The object is already initialized"; 961d14ffa9Sbellard break; 971d14ffa9Sbellard #endif 981d14ffa9Sbellard #ifdef DSERR_BADFORMAT 991d14ffa9Sbellard case DSERR_BADFORMAT: 1001d14ffa9Sbellard str = "The specified wave format is not supported"; 1011d14ffa9Sbellard break; 1021d14ffa9Sbellard #endif 1031d14ffa9Sbellard #ifdef DSERR_BADSENDBUFFERGUID 1041d14ffa9Sbellard case DSERR_BADSENDBUFFERGUID: 1051d14ffa9Sbellard str = "The GUID specified in an audiopath file does not match a valid mix-in buffer"; 1061d14ffa9Sbellard break; 1071d14ffa9Sbellard #endif 1081d14ffa9Sbellard #ifdef DSERR_BUFFERLOST 1091d14ffa9Sbellard case DSERR_BUFFERLOST: 1101d14ffa9Sbellard str = "The buffer memory has been lost and must be restored"; 1111d14ffa9Sbellard break; 1121d14ffa9Sbellard #endif 1131d14ffa9Sbellard #ifdef DSERR_BUFFERTOOSMALL 1141d14ffa9Sbellard case DSERR_BUFFERTOOSMALL: 1151d14ffa9Sbellard str = "The buffer size is not great enough to enable effects processing"; 1161d14ffa9Sbellard break; 1171d14ffa9Sbellard #endif 1181d14ffa9Sbellard #ifdef DSERR_CONTROLUNAVAIL 1191d14ffa9Sbellard case DSERR_CONTROLUNAVAIL: 1201d14ffa9Sbellard 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"; 1211d14ffa9Sbellard break; 1221d14ffa9Sbellard #endif 1231d14ffa9Sbellard #ifdef DSERR_DS8_REQUIRED 1241d14ffa9Sbellard case DSERR_DS8_REQUIRED: 1251d14ffa9Sbellard str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface"; 1261d14ffa9Sbellard break; 1271d14ffa9Sbellard #endif 1281d14ffa9Sbellard #ifdef DSERR_FXUNAVAILABLE 1291d14ffa9Sbellard case DSERR_FXUNAVAILABLE: 1301d14ffa9Sbellard 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"; 1311d14ffa9Sbellard break; 1321d14ffa9Sbellard #endif 1331d14ffa9Sbellard #ifdef DSERR_GENERIC 1341d14ffa9Sbellard case DSERR_GENERIC : 1351d14ffa9Sbellard str = "An undetermined error occurred inside the DirectSound subsystem"; 1361d14ffa9Sbellard break; 1371d14ffa9Sbellard #endif 1381d14ffa9Sbellard #ifdef DSERR_INVALIDCALL 1391d14ffa9Sbellard case DSERR_INVALIDCALL: 1401d14ffa9Sbellard str = "This function is not valid for the current state of this object"; 1411d14ffa9Sbellard break; 1421d14ffa9Sbellard #endif 1431d14ffa9Sbellard #ifdef DSERR_INVALIDPARAM 1441d14ffa9Sbellard case DSERR_INVALIDPARAM: 1451d14ffa9Sbellard str = "An invalid parameter was passed to the returning function"; 1461d14ffa9Sbellard break; 1471d14ffa9Sbellard #endif 1481d14ffa9Sbellard #ifdef DSERR_NOAGGREGATION 1491d14ffa9Sbellard case DSERR_NOAGGREGATION: 1501d14ffa9Sbellard str = "The object does not support aggregation"; 1511d14ffa9Sbellard break; 1521d14ffa9Sbellard #endif 1531d14ffa9Sbellard #ifdef DSERR_NODRIVER 1541d14ffa9Sbellard case DSERR_NODRIVER: 1551d14ffa9Sbellard str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID"; 1561d14ffa9Sbellard break; 1571d14ffa9Sbellard #endif 1581d14ffa9Sbellard #ifdef DSERR_NOINTERFACE 1591d14ffa9Sbellard case DSERR_NOINTERFACE: 1601d14ffa9Sbellard str = "The requested COM interface is not available"; 1611d14ffa9Sbellard break; 1621d14ffa9Sbellard #endif 1631d14ffa9Sbellard #ifdef DSERR_OBJECTNOTFOUND 1641d14ffa9Sbellard case DSERR_OBJECTNOTFOUND: 1651d14ffa9Sbellard str = "The requested object was not found"; 1661d14ffa9Sbellard break; 1671d14ffa9Sbellard #endif 1681d14ffa9Sbellard #ifdef DSERR_OTHERAPPHASPRIO 1691d14ffa9Sbellard case DSERR_OTHERAPPHASPRIO: 1701d14ffa9Sbellard str = "Another application has a higher priority level, preventing this call from succeeding"; 1711d14ffa9Sbellard break; 1721d14ffa9Sbellard #endif 1731d14ffa9Sbellard #ifdef DSERR_OUTOFMEMORY 1741d14ffa9Sbellard case DSERR_OUTOFMEMORY: 1751d14ffa9Sbellard str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request"; 1761d14ffa9Sbellard break; 1771d14ffa9Sbellard #endif 1781d14ffa9Sbellard #ifdef DSERR_PRIOLEVELNEEDED 1791d14ffa9Sbellard case DSERR_PRIOLEVELNEEDED: 1801d14ffa9Sbellard str = "A cooperative level of DSSCL_PRIORITY or higher is required"; 1811d14ffa9Sbellard break; 1821d14ffa9Sbellard #endif 1831d14ffa9Sbellard #ifdef DSERR_SENDLOOP 1841d14ffa9Sbellard case DSERR_SENDLOOP: 1851d14ffa9Sbellard str = "A circular loop of send effects was detected"; 1861d14ffa9Sbellard break; 1871d14ffa9Sbellard #endif 1881d14ffa9Sbellard #ifdef DSERR_UNINITIALIZED 1891d14ffa9Sbellard case DSERR_UNINITIALIZED: 1901d14ffa9Sbellard str = "The Initialize method has not been called or has not been called successfully before other methods were called"; 1911d14ffa9Sbellard break; 1921d14ffa9Sbellard #endif 1931d14ffa9Sbellard #ifdef DSERR_UNSUPPORTED 1941d14ffa9Sbellard case DSERR_UNSUPPORTED: 1951d14ffa9Sbellard str = "The function called is not supported at this time"; 1961d14ffa9Sbellard break; 1971d14ffa9Sbellard #endif 1981d14ffa9Sbellard default: 1991d14ffa9Sbellard AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr); 2001d14ffa9Sbellard return; 2011d14ffa9Sbellard } 2021d14ffa9Sbellard 2031d14ffa9Sbellard AUD_log (AUDIO_CAP, "Reason: %s\n", str); 2041d14ffa9Sbellard } 2051d14ffa9Sbellard 2061d14ffa9Sbellard static void GCC_FMT_ATTR (2, 3) dsound_logerr ( 2071d14ffa9Sbellard HRESULT hr, 2081d14ffa9Sbellard const char *fmt, 2091d14ffa9Sbellard ... 2101d14ffa9Sbellard ) 2111d14ffa9Sbellard { 2121d14ffa9Sbellard va_list ap; 2131d14ffa9Sbellard 2141d14ffa9Sbellard va_start (ap, fmt); 2151d14ffa9Sbellard AUD_vlog (AUDIO_CAP, fmt, ap); 2161d14ffa9Sbellard va_end (ap); 2171d14ffa9Sbellard 2181d14ffa9Sbellard dsound_log_hresult (hr); 2191d14ffa9Sbellard } 2201d14ffa9Sbellard 2211d14ffa9Sbellard static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( 2221d14ffa9Sbellard HRESULT hr, 2231d14ffa9Sbellard const char *typ, 2241d14ffa9Sbellard const char *fmt, 2251d14ffa9Sbellard ... 2261d14ffa9Sbellard ) 2271d14ffa9Sbellard { 2281d14ffa9Sbellard va_list ap; 2291d14ffa9Sbellard 230c0fe3827Sbellard AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); 2311d14ffa9Sbellard va_start (ap, fmt); 2321d14ffa9Sbellard AUD_vlog (AUDIO_CAP, fmt, ap); 2331d14ffa9Sbellard va_end (ap); 2341d14ffa9Sbellard 2351d14ffa9Sbellard dsound_log_hresult (hr); 2361d14ffa9Sbellard } 2371d14ffa9Sbellard 2381d14ffa9Sbellard #ifdef DEBUG_DSOUND 2391d14ffa9Sbellard static void print_wave_format (WAVEFORMATEX *wfx) 2401d14ffa9Sbellard { 2411d14ffa9Sbellard dolog ("tag = %d\n", wfx->wFormatTag); 2421d14ffa9Sbellard dolog ("nChannels = %d\n", wfx->nChannels); 2431d14ffa9Sbellard dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec); 2441d14ffa9Sbellard dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec); 2451d14ffa9Sbellard dolog ("nBlockAlign = %d\n", wfx->nBlockAlign); 2461d14ffa9Sbellard dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample); 2471d14ffa9Sbellard dolog ("cbSize = %d\n", wfx->cbSize); 2481d14ffa9Sbellard } 2491d14ffa9Sbellard #endif 2501d14ffa9Sbellard 251191e1f0aSKővágó, Zoltán static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s) 2521d14ffa9Sbellard { 2531d14ffa9Sbellard HRESULT hr; 2541d14ffa9Sbellard 2551d14ffa9Sbellard hr = IDirectSoundBuffer_Restore (dsb); 2561d14ffa9Sbellard 2572762955fSKővágó, Zoltán if (hr != DS_OK) { 258c0fe3827Sbellard dsound_logerr (hr, "Could not restore playback buffer\n"); 2591d14ffa9Sbellard return -1; 2601d14ffa9Sbellard } 2612762955fSKővágó, Zoltán return 0; 2621d14ffa9Sbellard } 2631d14ffa9Sbellard 2641d14ffa9Sbellard #include "dsound_template.h" 2651d14ffa9Sbellard #define DSBTYPE_IN 2661d14ffa9Sbellard #include "dsound_template.h" 2671d14ffa9Sbellard #undef DSBTYPE_IN 2681d14ffa9Sbellard 269191e1f0aSKővágó, Zoltán static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp, 270191e1f0aSKővágó, Zoltán dsound *s) 2711d14ffa9Sbellard { 2721d14ffa9Sbellard HRESULT hr; 2731d14ffa9Sbellard 2741d14ffa9Sbellard hr = IDirectSoundBuffer_GetStatus (dsb, statusp); 2751d14ffa9Sbellard if (FAILED (hr)) { 276c0fe3827Sbellard dsound_logerr (hr, "Could not get playback buffer status\n"); 2771d14ffa9Sbellard return -1; 2781d14ffa9Sbellard } 2791d14ffa9Sbellard 2801d14ffa9Sbellard if (*statusp & DSERR_BUFFERLOST) { 2812762955fSKővágó, Zoltán dsound_restore_out(dsb, s); 2821d14ffa9Sbellard return -1; 2831d14ffa9Sbellard } 2841d14ffa9Sbellard 2851d14ffa9Sbellard return 0; 2861d14ffa9Sbellard } 2871d14ffa9Sbellard 2881d14ffa9Sbellard static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb, 2891d14ffa9Sbellard DWORD *statusp) 2901d14ffa9Sbellard { 2911d14ffa9Sbellard HRESULT hr; 2921d14ffa9Sbellard 2931d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp); 2941d14ffa9Sbellard if (FAILED (hr)) { 295c0fe3827Sbellard dsound_logerr (hr, "Could not get capture buffer status\n"); 2961d14ffa9Sbellard return -1; 2971d14ffa9Sbellard } 2981d14ffa9Sbellard 2991d14ffa9Sbellard return 0; 3001d14ffa9Sbellard } 3011d14ffa9Sbellard 302191e1f0aSKővágó, Zoltán static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb, 303191e1f0aSKővágó, Zoltán dsound *s) 3041d14ffa9Sbellard { 3051d14ffa9Sbellard int err; 3061d14ffa9Sbellard LPVOID p1, p2; 3071d14ffa9Sbellard DWORD blen1, blen2, len1, len2; 3081d14ffa9Sbellard 3091d14ffa9Sbellard err = dsound_lock_out ( 3101d14ffa9Sbellard dsb, 3111d14ffa9Sbellard &hw->info, 3121d14ffa9Sbellard 0, 3137fa9754aSKővágó, Zoltán hw->size_emul, 3141d14ffa9Sbellard &p1, &p2, 3151d14ffa9Sbellard &blen1, &blen2, 316191e1f0aSKővágó, Zoltán 1, 317191e1f0aSKővágó, Zoltán s 3181d14ffa9Sbellard ); 3191d14ffa9Sbellard if (err) { 3201d14ffa9Sbellard return; 3211d14ffa9Sbellard } 3221d14ffa9Sbellard 3231d14ffa9Sbellard len1 = blen1 >> hw->info.shift; 3241d14ffa9Sbellard len2 = blen2 >> hw->info.shift; 3251d14ffa9Sbellard 3261d14ffa9Sbellard #ifdef DEBUG_DSOUND 3271d14ffa9Sbellard dolog ("clear %p,%ld,%ld %p,%ld,%ld\n", 3281d14ffa9Sbellard p1, blen1, len1, 3291d14ffa9Sbellard p2, blen2, len2); 3301d14ffa9Sbellard #endif 3311d14ffa9Sbellard 3321d14ffa9Sbellard if (p1 && len1) { 3331d14ffa9Sbellard audio_pcm_info_clear_buf (&hw->info, p1, len1); 3341d14ffa9Sbellard } 3351d14ffa9Sbellard 3361d14ffa9Sbellard if (p2 && len2) { 3371d14ffa9Sbellard audio_pcm_info_clear_buf (&hw->info, p2, len2); 3381d14ffa9Sbellard } 3391d14ffa9Sbellard 3401d14ffa9Sbellard dsound_unlock_out (dsb, p1, p2, blen1, blen2); 3411d14ffa9Sbellard } 3421d14ffa9Sbellard 3431d14ffa9Sbellard static int dsound_open (dsound *s) 3441d14ffa9Sbellard { 3451d14ffa9Sbellard HRESULT hr; 3461d14ffa9Sbellard HWND hwnd; 3471d14ffa9Sbellard 3481d14ffa9Sbellard hwnd = GetForegroundWindow (); 3491d14ffa9Sbellard hr = IDirectSound_SetCooperativeLevel ( 3501d14ffa9Sbellard s->dsound, 3511d14ffa9Sbellard hwnd, 3521d14ffa9Sbellard DSSCL_PRIORITY 3531d14ffa9Sbellard ); 3541d14ffa9Sbellard 3551d14ffa9Sbellard if (FAILED (hr)) { 356c0fe3827Sbellard dsound_logerr (hr, "Could not set cooperative level for window %p\n", 3571d14ffa9Sbellard hwnd); 3581d14ffa9Sbellard return -1; 3591d14ffa9Sbellard } 3601d14ffa9Sbellard 3611d14ffa9Sbellard return 0; 3621d14ffa9Sbellard } 3631d14ffa9Sbellard 364*571a8c52SKővágó, Zoltán static void dsound_enable_out(HWVoiceOut *hw, bool enable) 3651d14ffa9Sbellard { 3661d14ffa9Sbellard HRESULT hr; 3671d14ffa9Sbellard DWORD status; 3681d14ffa9Sbellard DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 3691d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 370191e1f0aSKővágó, Zoltán dsound *s = ds->s; 3711d14ffa9Sbellard 3721d14ffa9Sbellard if (!dsb) { 3731d14ffa9Sbellard dolog ("Attempt to control voice without a buffer\n"); 374*571a8c52SKővágó, Zoltán return; 3751d14ffa9Sbellard } 3761d14ffa9Sbellard 377*571a8c52SKővágó, Zoltán if (enable) { 378191e1f0aSKővágó, Zoltán if (dsound_get_status_out (dsb, &status, s)) { 379*571a8c52SKővágó, Zoltán return; 3801d14ffa9Sbellard } 3811d14ffa9Sbellard 3821d14ffa9Sbellard if (status & DSBSTATUS_PLAYING) { 383c0fe3827Sbellard dolog ("warning: Voice is already playing\n"); 384*571a8c52SKővágó, Zoltán return; 3851d14ffa9Sbellard } 3861d14ffa9Sbellard 387191e1f0aSKővágó, Zoltán dsound_clear_sample (hw, dsb, s); 3881d14ffa9Sbellard 3891d14ffa9Sbellard hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); 3901d14ffa9Sbellard if (FAILED (hr)) { 391c0fe3827Sbellard dsound_logerr (hr, "Could not start playing buffer\n"); 392*571a8c52SKővágó, Zoltán return; 3931d14ffa9Sbellard } 394*571a8c52SKővágó, Zoltán } else { 395191e1f0aSKővágó, Zoltán if (dsound_get_status_out (dsb, &status, s)) { 396*571a8c52SKővágó, Zoltán return; 3971d14ffa9Sbellard } 3981d14ffa9Sbellard 3991d14ffa9Sbellard if (status & DSBSTATUS_PLAYING) { 4001d14ffa9Sbellard hr = IDirectSoundBuffer_Stop (dsb); 4011d14ffa9Sbellard if (FAILED (hr)) { 402c0fe3827Sbellard dsound_logerr (hr, "Could not stop playing buffer\n"); 403*571a8c52SKővágó, Zoltán return; 4041d14ffa9Sbellard } 4051d14ffa9Sbellard } 4061d14ffa9Sbellard else { 407c0fe3827Sbellard dolog ("warning: Voice is not playing\n"); 4081d14ffa9Sbellard } 4091d14ffa9Sbellard } 4101d14ffa9Sbellard } 4111d14ffa9Sbellard 4127fa9754aSKővágó, Zoltán static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size) 4131d14ffa9Sbellard { 4141d14ffa9Sbellard DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 4151d14ffa9Sbellard LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 4167fa9754aSKővágó, Zoltán HRESULT hr; 4177fa9754aSKővágó, Zoltán DWORD ppos, act_size; 4187fa9754aSKővágó, Zoltán size_t req_size; 4197fa9754aSKővágó, Zoltán int err; 4207fa9754aSKővágó, Zoltán void *ret; 4211d14ffa9Sbellard 4227fa9754aSKővágó, Zoltán hr = IDirectSoundBuffer_GetCurrentPosition(dsb, &ppos, NULL); 4231d14ffa9Sbellard if (FAILED(hr)) { 424c0fe3827Sbellard dsound_logerr(hr, "Could not get playback buffer position\n"); 4257fa9754aSKővágó, Zoltán *size = 0; 4267fa9754aSKővágó, Zoltán return NULL; 4271d14ffa9Sbellard } 4281d14ffa9Sbellard 4297fa9754aSKővágó, Zoltán req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul); 4307fa9754aSKővágó, Zoltán req_size = MIN(req_size, hw->size_emul - hw->pos_emul); 4311d14ffa9Sbellard 4327fa9754aSKővágó, Zoltán err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL, 4337fa9754aSKővágó, Zoltán &act_size, NULL, false, ds->s); 4341d14ffa9Sbellard if (err) { 4357fa9754aSKővágó, Zoltán dolog("Failed to lock buffer\n"); 4367fa9754aSKővágó, Zoltán *size = 0; 4377fa9754aSKővágó, Zoltán return NULL; 4387fa9754aSKővágó, Zoltán } 4397fa9754aSKővágó, Zoltán 4407fa9754aSKővágó, Zoltán *size = act_size; 4417fa9754aSKővágó, Zoltán return ret; 4427fa9754aSKővágó, Zoltán } 4437fa9754aSKővágó, Zoltán 4447fa9754aSKővágó, Zoltán static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len) 4457fa9754aSKővágó, Zoltán { 4467fa9754aSKővágó, Zoltán DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; 4477fa9754aSKővágó, Zoltán LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; 4487fa9754aSKővágó, Zoltán int err = dsound_unlock_out(dsb, buf, NULL, len, 0); 4497fa9754aSKővágó, Zoltán 4507fa9754aSKővágó, Zoltán if (err) { 4517fa9754aSKővágó, Zoltán dolog("Failed to unlock buffer!!\n"); 4521d14ffa9Sbellard return 0; 4531d14ffa9Sbellard } 4547fa9754aSKővágó, Zoltán hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; 4551d14ffa9Sbellard 4567fa9754aSKővágó, Zoltán return len; 4571d14ffa9Sbellard } 4581d14ffa9Sbellard 459*571a8c52SKővágó, Zoltán static void dsound_enable_in(HWVoiceIn *hw, bool enable) 4601d14ffa9Sbellard { 4611d14ffa9Sbellard HRESULT hr; 4621d14ffa9Sbellard DWORD status; 4631d14ffa9Sbellard DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 4641d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 4651d14ffa9Sbellard 4661d14ffa9Sbellard if (!dscb) { 4671d14ffa9Sbellard dolog ("Attempt to control capture voice without a buffer\n"); 468*571a8c52SKővágó, Zoltán return; 4691d14ffa9Sbellard } 4701d14ffa9Sbellard 471*571a8c52SKővágó, Zoltán if (enable) { 4721d14ffa9Sbellard if (dsound_get_status_in (dscb, &status)) { 473*571a8c52SKővágó, Zoltán return; 4741d14ffa9Sbellard } 4751d14ffa9Sbellard 4761d14ffa9Sbellard if (status & DSCBSTATUS_CAPTURING) { 477c0fe3827Sbellard dolog ("warning: Voice is already capturing\n"); 478*571a8c52SKővágó, Zoltán return; 4791d14ffa9Sbellard } 4801d14ffa9Sbellard 4811d14ffa9Sbellard /* clear ?? */ 4821d14ffa9Sbellard 4831d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); 4841d14ffa9Sbellard if (FAILED (hr)) { 485c0fe3827Sbellard dsound_logerr (hr, "Could not start capturing\n"); 486*571a8c52SKővágó, Zoltán return; 4871d14ffa9Sbellard } 488*571a8c52SKővágó, Zoltán } else { 4891d14ffa9Sbellard if (dsound_get_status_in (dscb, &status)) { 490*571a8c52SKővágó, Zoltán return; 4911d14ffa9Sbellard } 4921d14ffa9Sbellard 4931d14ffa9Sbellard if (status & DSCBSTATUS_CAPTURING) { 4941d14ffa9Sbellard hr = IDirectSoundCaptureBuffer_Stop (dscb); 4951d14ffa9Sbellard if (FAILED (hr)) { 496c0fe3827Sbellard dsound_logerr (hr, "Could not stop capturing\n"); 497*571a8c52SKővágó, Zoltán return; 4981d14ffa9Sbellard } 4991d14ffa9Sbellard } 5001d14ffa9Sbellard else { 501c0fe3827Sbellard dolog ("warning: Voice is not capturing\n"); 5021d14ffa9Sbellard } 5031d14ffa9Sbellard } 5041d14ffa9Sbellard } 5051d14ffa9Sbellard 5067fa9754aSKővágó, Zoltán static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size) 5071d14ffa9Sbellard { 5081d14ffa9Sbellard DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 5091d14ffa9Sbellard LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 5107fa9754aSKővágó, Zoltán HRESULT hr; 5117fa9754aSKővágó, Zoltán DWORD cpos, act_size; 5127fa9754aSKővágó, Zoltán size_t req_size; 5137fa9754aSKővágó, Zoltán int err; 5147fa9754aSKővágó, Zoltán void *ret; 5151d14ffa9Sbellard 5167fa9754aSKővágó, Zoltán hr = IDirectSoundCaptureBuffer_GetCurrentPosition(dscb, &cpos, NULL); 5171d14ffa9Sbellard if (FAILED(hr)) { 518c0fe3827Sbellard dsound_logerr(hr, "Could not get capture buffer position\n"); 5197fa9754aSKővágó, Zoltán *size = 0; 5207fa9754aSKővágó, Zoltán return NULL; 5211d14ffa9Sbellard } 5221d14ffa9Sbellard 5237fa9754aSKővágó, Zoltán req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul); 5247fa9754aSKővágó, Zoltán req_size = MIN(req_size, hw->size_emul - hw->pos_emul); 5251d14ffa9Sbellard 5267fa9754aSKővágó, Zoltán err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL, 5277fa9754aSKővágó, Zoltán &act_size, NULL, false, ds->s); 5281d14ffa9Sbellard if (err) { 5297fa9754aSKővágó, Zoltán dolog("Failed to lock buffer\n"); 5307fa9754aSKővágó, Zoltán *size = 0; 5317fa9754aSKővágó, Zoltán return NULL; 5321d14ffa9Sbellard } 5331d14ffa9Sbellard 5347fa9754aSKővágó, Zoltán *size = act_size; 5357fa9754aSKővágó, Zoltán return ret; 5361d14ffa9Sbellard } 5371d14ffa9Sbellard 5387fa9754aSKővágó, Zoltán static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len) 5397fa9754aSKővágó, Zoltán { 5407fa9754aSKővágó, Zoltán DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; 5417fa9754aSKővágó, Zoltán LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; 5427fa9754aSKővágó, Zoltán int err = dsound_unlock_in(dscb, buf, NULL, len, 0); 5431d14ffa9Sbellard 5447fa9754aSKővágó, Zoltán if (err) { 5457fa9754aSKővágó, Zoltán dolog("Failed to unlock buffer!!\n"); 5467fa9754aSKővágó, Zoltán return; 5477fa9754aSKővágó, Zoltán } 5487fa9754aSKővágó, Zoltán hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; 5491d14ffa9Sbellard } 5501d14ffa9Sbellard 5511d14ffa9Sbellard static void dsound_audio_fini (void *opaque) 5521d14ffa9Sbellard { 5531d14ffa9Sbellard HRESULT hr; 5541d14ffa9Sbellard dsound *s = opaque; 5551d14ffa9Sbellard 5561d14ffa9Sbellard if (!s->dsound) { 557191e1f0aSKővágó, Zoltán g_free(s); 5581d14ffa9Sbellard return; 5591d14ffa9Sbellard } 5601d14ffa9Sbellard 5611d14ffa9Sbellard hr = IDirectSound_Release (s->dsound); 5621d14ffa9Sbellard if (FAILED (hr)) { 563c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSound\n"); 5641d14ffa9Sbellard } 5651d14ffa9Sbellard s->dsound = NULL; 5661d14ffa9Sbellard 5671d14ffa9Sbellard if (!s->dsound_capture) { 568191e1f0aSKővágó, Zoltán g_free(s); 5691d14ffa9Sbellard return; 5701d14ffa9Sbellard } 5711d14ffa9Sbellard 5721d14ffa9Sbellard hr = IDirectSoundCapture_Release (s->dsound_capture); 5731d14ffa9Sbellard if (FAILED (hr)) { 574c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 5751d14ffa9Sbellard } 5761d14ffa9Sbellard s->dsound_capture = NULL; 577191e1f0aSKővágó, Zoltán 578191e1f0aSKővágó, Zoltán g_free(s); 5791d14ffa9Sbellard } 5801d14ffa9Sbellard 58171830221SKővágó, Zoltán static void *dsound_audio_init(Audiodev *dev) 5821d14ffa9Sbellard { 5831d14ffa9Sbellard int err; 5841d14ffa9Sbellard HRESULT hr; 585191e1f0aSKővágó, Zoltán dsound *s = g_malloc0(sizeof(dsound)); 5864a3b8b34SKővágó, Zoltán AudiodevDsoundOptions *dso; 5871d14ffa9Sbellard 5884a3b8b34SKővágó, Zoltán assert(dev->driver == AUDIODEV_DRIVER_DSOUND); 5894a3b8b34SKővágó, Zoltán s->dev = dev; 5904a3b8b34SKővágó, Zoltán dso = &dev->u.dsound; 5914a3b8b34SKővágó, Zoltán 5924a3b8b34SKővágó, Zoltán if (!dso->has_latency) { 5934a3b8b34SKővágó, Zoltán dso->has_latency = true; 5944a3b8b34SKővágó, Zoltán dso->latency = 10000; /* 10 ms */ 5954a3b8b34SKővágó, Zoltán } 5964a3b8b34SKővágó, Zoltán 5971d14ffa9Sbellard hr = CoInitialize (NULL); 5981d14ffa9Sbellard if (FAILED (hr)) { 599c0fe3827Sbellard dsound_logerr (hr, "Could not initialize COM\n"); 600191e1f0aSKővágó, Zoltán g_free(s); 6011d14ffa9Sbellard return NULL; 6021d14ffa9Sbellard } 6031d14ffa9Sbellard 6041d14ffa9Sbellard hr = CoCreateInstance ( 6051d14ffa9Sbellard &CLSID_DirectSound, 6061d14ffa9Sbellard NULL, 6071d14ffa9Sbellard CLSCTX_ALL, 6081d14ffa9Sbellard &IID_IDirectSound, 6091d14ffa9Sbellard (void **) &s->dsound 6101d14ffa9Sbellard ); 6111d14ffa9Sbellard if (FAILED (hr)) { 612c0fe3827Sbellard dsound_logerr (hr, "Could not create DirectSound instance\n"); 613191e1f0aSKővágó, Zoltán g_free(s); 6141d14ffa9Sbellard return NULL; 6151d14ffa9Sbellard } 6161d14ffa9Sbellard 6171d14ffa9Sbellard hr = IDirectSound_Initialize (s->dsound, NULL); 6181d14ffa9Sbellard if (FAILED (hr)) { 619c0fe3827Sbellard dsound_logerr (hr, "Could not initialize DirectSound\n"); 6208ead62cfSbellard 6218ead62cfSbellard hr = IDirectSound_Release (s->dsound); 6228ead62cfSbellard if (FAILED (hr)) { 6238ead62cfSbellard dsound_logerr (hr, "Could not release DirectSound\n"); 6248ead62cfSbellard } 625191e1f0aSKővágó, Zoltán g_free(s); 6261d14ffa9Sbellard return NULL; 6271d14ffa9Sbellard } 6281d14ffa9Sbellard 6291d14ffa9Sbellard hr = CoCreateInstance ( 6301d14ffa9Sbellard &CLSID_DirectSoundCapture, 6311d14ffa9Sbellard NULL, 6321d14ffa9Sbellard CLSCTX_ALL, 6331d14ffa9Sbellard &IID_IDirectSoundCapture, 6341d14ffa9Sbellard (void **) &s->dsound_capture 6351d14ffa9Sbellard ); 6361d14ffa9Sbellard if (FAILED (hr)) { 637c0fe3827Sbellard dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); 6381d14ffa9Sbellard } 6391d14ffa9Sbellard else { 6401d14ffa9Sbellard hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); 6411d14ffa9Sbellard if (FAILED (hr)) { 642c0fe3827Sbellard dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); 6431d14ffa9Sbellard 6441d14ffa9Sbellard hr = IDirectSoundCapture_Release (s->dsound_capture); 6451d14ffa9Sbellard if (FAILED (hr)) { 646c0fe3827Sbellard dsound_logerr (hr, "Could not release DirectSoundCapture\n"); 6471d14ffa9Sbellard } 6481d14ffa9Sbellard s->dsound_capture = NULL; 6491d14ffa9Sbellard } 6501d14ffa9Sbellard } 6511d14ffa9Sbellard 6521d14ffa9Sbellard err = dsound_open (s); 6531d14ffa9Sbellard if (err) { 6541d14ffa9Sbellard dsound_audio_fini (s); 6551d14ffa9Sbellard return NULL; 6561d14ffa9Sbellard } 6571d14ffa9Sbellard 6581d14ffa9Sbellard return s; 6591d14ffa9Sbellard } 6601d14ffa9Sbellard 66135f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = { 6621dd3e4d1SJuan Quintela .init_out = dsound_init_out, 6631dd3e4d1SJuan Quintela .fini_out = dsound_fini_out, 6647fa9754aSKővágó, Zoltán .write = audio_generic_write, 6657fa9754aSKővágó, Zoltán .get_buffer_out = dsound_get_buffer_out, 6667fa9754aSKővágó, Zoltán .put_buffer_out = dsound_put_buffer_out, 667*571a8c52SKővágó, Zoltán .enable_out = dsound_enable_out, 6681d14ffa9Sbellard 6691dd3e4d1SJuan Quintela .init_in = dsound_init_in, 6701dd3e4d1SJuan Quintela .fini_in = dsound_fini_in, 6717fa9754aSKővágó, Zoltán .read = audio_generic_read, 6727fa9754aSKővágó, Zoltán .get_buffer_in = dsound_get_buffer_in, 6737fa9754aSKővágó, Zoltán .put_buffer_in = dsound_put_buffer_in, 674*571a8c52SKővágó, Zoltán .enable_in = dsound_enable_in, 6751d14ffa9Sbellard }; 6761d14ffa9Sbellard 677d3893a39SGerd Hoffmann static struct audio_driver dsound_audio_driver = { 678bee37f32SJuan Quintela .name = "dsound", 679bee37f32SJuan Quintela .descr = "DirectSound http://wikipedia.org/wiki/DirectSound", 680bee37f32SJuan Quintela .init = dsound_audio_init, 681bee37f32SJuan Quintela .fini = dsound_audio_fini, 682bee37f32SJuan Quintela .pcm_ops = &dsound_pcm_ops, 683bee37f32SJuan Quintela .can_be_default = 1, 684bee37f32SJuan Quintela .max_voices_out = INT_MAX, 685bee37f32SJuan Quintela .max_voices_in = 1, 686bee37f32SJuan Quintela .voice_size_out = sizeof (DSoundVoiceOut), 68715c875a3SConsul .voice_size_in = sizeof (DSoundVoiceIn) 6881d14ffa9Sbellard }; 689d3893a39SGerd Hoffmann 690d3893a39SGerd Hoffmann static void register_audio_dsound(void) 691d3893a39SGerd Hoffmann { 692d3893a39SGerd Hoffmann audio_driver_register(&dsound_audio_driver); 693d3893a39SGerd Hoffmann } 694d3893a39SGerd Hoffmann type_init(register_audio_dsound); 695