xref: /qemu/audio/dsoundaudio.c (revision 7fa9754ac888d571000110504a3a7950f1404212)
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,
313*7fa9754aSKő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 
3641d14ffa9Sbellard static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
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");
3741d14ffa9Sbellard         return 0;
3751d14ffa9Sbellard     }
3761d14ffa9Sbellard 
3771d14ffa9Sbellard     switch (cmd) {
3781d14ffa9Sbellard     case VOICE_ENABLE:
379191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
3801d14ffa9Sbellard             return -1;
3811d14ffa9Sbellard         }
3821d14ffa9Sbellard 
3831d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
384c0fe3827Sbellard             dolog ("warning: Voice is already playing\n");
3851d14ffa9Sbellard             return 0;
3861d14ffa9Sbellard         }
3871d14ffa9Sbellard 
388191e1f0aSKővágó, Zoltán         dsound_clear_sample (hw, dsb, s);
3891d14ffa9Sbellard 
3901d14ffa9Sbellard         hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
3911d14ffa9Sbellard         if (FAILED (hr)) {
392c0fe3827Sbellard             dsound_logerr (hr, "Could not start playing buffer\n");
3931d14ffa9Sbellard             return -1;
3941d14ffa9Sbellard         }
3951d14ffa9Sbellard         break;
3961d14ffa9Sbellard 
3971d14ffa9Sbellard     case VOICE_DISABLE:
398191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
3991d14ffa9Sbellard             return -1;
4001d14ffa9Sbellard         }
4011d14ffa9Sbellard 
4021d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
4031d14ffa9Sbellard             hr = IDirectSoundBuffer_Stop (dsb);
4041d14ffa9Sbellard             if (FAILED (hr)) {
405c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop playing buffer\n");
4061d14ffa9Sbellard                 return -1;
4071d14ffa9Sbellard             }
4081d14ffa9Sbellard         }
4091d14ffa9Sbellard         else {
410c0fe3827Sbellard             dolog ("warning: Voice is not playing\n");
4111d14ffa9Sbellard         }
4121d14ffa9Sbellard         break;
4131d14ffa9Sbellard     }
4141d14ffa9Sbellard     return 0;
4151d14ffa9Sbellard }
4161d14ffa9Sbellard 
417*7fa9754aSKővágó, Zoltán static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size)
4181d14ffa9Sbellard {
4191d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4201d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
421*7fa9754aSKővágó, Zoltán     HRESULT hr;
422*7fa9754aSKővágó, Zoltán     DWORD ppos, act_size;
423*7fa9754aSKővágó, Zoltán     size_t req_size;
424*7fa9754aSKővágó, Zoltán     int err;
425*7fa9754aSKővágó, Zoltán     void *ret;
4261d14ffa9Sbellard 
427*7fa9754aSKővágó, Zoltán     hr = IDirectSoundBuffer_GetCurrentPosition(dsb, &ppos, NULL);
4281d14ffa9Sbellard     if (FAILED(hr)) {
429c0fe3827Sbellard         dsound_logerr(hr, "Could not get playback buffer position\n");
430*7fa9754aSKővágó, Zoltán         *size = 0;
431*7fa9754aSKővágó, Zoltán         return NULL;
4321d14ffa9Sbellard     }
4331d14ffa9Sbellard 
434*7fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul);
435*7fa9754aSKővágó, Zoltán     req_size = MIN(req_size, hw->size_emul - hw->pos_emul);
4361d14ffa9Sbellard 
437*7fa9754aSKővágó, Zoltán     err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
438*7fa9754aSKővágó, Zoltán                           &act_size, NULL, false, ds->s);
4391d14ffa9Sbellard     if (err) {
440*7fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
441*7fa9754aSKővágó, Zoltán         *size = 0;
442*7fa9754aSKővágó, Zoltán         return NULL;
443*7fa9754aSKővágó, Zoltán     }
444*7fa9754aSKővágó, Zoltán 
445*7fa9754aSKővágó, Zoltán     *size = act_size;
446*7fa9754aSKővágó, Zoltán     return ret;
447*7fa9754aSKővágó, Zoltán }
448*7fa9754aSKővágó, Zoltán 
449*7fa9754aSKővágó, Zoltán static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len)
450*7fa9754aSKővágó, Zoltán {
451*7fa9754aSKővágó, Zoltán     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
452*7fa9754aSKővágó, Zoltán     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
453*7fa9754aSKővágó, Zoltán     int err = dsound_unlock_out(dsb, buf, NULL, len, 0);
454*7fa9754aSKővágó, Zoltán 
455*7fa9754aSKővágó, Zoltán     if (err) {
456*7fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
4571d14ffa9Sbellard         return 0;
4581d14ffa9Sbellard     }
459*7fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
4601d14ffa9Sbellard 
461*7fa9754aSKővágó, Zoltán     return len;
4621d14ffa9Sbellard }
4631d14ffa9Sbellard 
4641d14ffa9Sbellard static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
4651d14ffa9Sbellard {
4661d14ffa9Sbellard     HRESULT hr;
4671d14ffa9Sbellard     DWORD status;
4681d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
4691d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
4701d14ffa9Sbellard 
4711d14ffa9Sbellard     if (!dscb) {
4721d14ffa9Sbellard         dolog ("Attempt to control capture voice without a buffer\n");
4731d14ffa9Sbellard         return -1;
4741d14ffa9Sbellard     }
4751d14ffa9Sbellard 
4761d14ffa9Sbellard     switch (cmd) {
4771d14ffa9Sbellard     case VOICE_ENABLE:
4781d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
4791d14ffa9Sbellard             return -1;
4801d14ffa9Sbellard         }
4811d14ffa9Sbellard 
4821d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
483c0fe3827Sbellard             dolog ("warning: Voice is already capturing\n");
4841d14ffa9Sbellard             return 0;
4851d14ffa9Sbellard         }
4861d14ffa9Sbellard 
4871d14ffa9Sbellard         /* clear ?? */
4881d14ffa9Sbellard 
4891d14ffa9Sbellard         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
4901d14ffa9Sbellard         if (FAILED (hr)) {
491c0fe3827Sbellard             dsound_logerr (hr, "Could not start capturing\n");
4921d14ffa9Sbellard             return -1;
4931d14ffa9Sbellard         }
4941d14ffa9Sbellard         break;
4951d14ffa9Sbellard 
4961d14ffa9Sbellard     case VOICE_DISABLE:
4971d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
4981d14ffa9Sbellard             return -1;
4991d14ffa9Sbellard         }
5001d14ffa9Sbellard 
5011d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
5021d14ffa9Sbellard             hr = IDirectSoundCaptureBuffer_Stop (dscb);
5031d14ffa9Sbellard             if (FAILED (hr)) {
504c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop capturing\n");
5051d14ffa9Sbellard                 return -1;
5061d14ffa9Sbellard             }
5071d14ffa9Sbellard         }
5081d14ffa9Sbellard         else {
509c0fe3827Sbellard             dolog ("warning: Voice is not capturing\n");
5101d14ffa9Sbellard         }
5111d14ffa9Sbellard         break;
5121d14ffa9Sbellard     }
5131d14ffa9Sbellard     return 0;
5141d14ffa9Sbellard }
5151d14ffa9Sbellard 
516*7fa9754aSKővágó, Zoltán static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size)
5171d14ffa9Sbellard {
5181d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5191d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
520*7fa9754aSKővágó, Zoltán     HRESULT hr;
521*7fa9754aSKővágó, Zoltán     DWORD cpos, act_size;
522*7fa9754aSKővágó, Zoltán     size_t req_size;
523*7fa9754aSKővágó, Zoltán     int err;
524*7fa9754aSKővágó, Zoltán     void *ret;
5251d14ffa9Sbellard 
526*7fa9754aSKővágó, Zoltán     hr = IDirectSoundCaptureBuffer_GetCurrentPosition(dscb, &cpos, NULL);
5271d14ffa9Sbellard     if (FAILED(hr)) {
528c0fe3827Sbellard         dsound_logerr(hr, "Could not get capture buffer position\n");
529*7fa9754aSKővágó, Zoltán         *size = 0;
530*7fa9754aSKővágó, Zoltán         return NULL;
5311d14ffa9Sbellard     }
5321d14ffa9Sbellard 
533*7fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul);
534*7fa9754aSKővágó, Zoltán     req_size = MIN(req_size, hw->size_emul - hw->pos_emul);
5351d14ffa9Sbellard 
536*7fa9754aSKővágó, Zoltán     err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
537*7fa9754aSKővágó, Zoltán                          &act_size, NULL, false, ds->s);
5381d14ffa9Sbellard     if (err) {
539*7fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
540*7fa9754aSKővágó, Zoltán         *size = 0;
541*7fa9754aSKővágó, Zoltán         return NULL;
5421d14ffa9Sbellard     }
5431d14ffa9Sbellard 
544*7fa9754aSKővágó, Zoltán     *size = act_size;
545*7fa9754aSKővágó, Zoltán     return ret;
5461d14ffa9Sbellard }
5471d14ffa9Sbellard 
548*7fa9754aSKővágó, Zoltán static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len)
549*7fa9754aSKővágó, Zoltán {
550*7fa9754aSKővágó, Zoltán     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
551*7fa9754aSKővágó, Zoltán     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
552*7fa9754aSKővágó, Zoltán     int err = dsound_unlock_in(dscb, buf, NULL, len, 0);
5531d14ffa9Sbellard 
554*7fa9754aSKővágó, Zoltán     if (err) {
555*7fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
556*7fa9754aSKővágó, Zoltán         return;
557*7fa9754aSKővágó, Zoltán     }
558*7fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
5591d14ffa9Sbellard }
5601d14ffa9Sbellard 
5611d14ffa9Sbellard static void dsound_audio_fini (void *opaque)
5621d14ffa9Sbellard {
5631d14ffa9Sbellard     HRESULT hr;
5641d14ffa9Sbellard     dsound *s = opaque;
5651d14ffa9Sbellard 
5661d14ffa9Sbellard     if (!s->dsound) {
567191e1f0aSKővágó, Zoltán         g_free(s);
5681d14ffa9Sbellard         return;
5691d14ffa9Sbellard     }
5701d14ffa9Sbellard 
5711d14ffa9Sbellard     hr = IDirectSound_Release (s->dsound);
5721d14ffa9Sbellard     if (FAILED (hr)) {
573c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSound\n");
5741d14ffa9Sbellard     }
5751d14ffa9Sbellard     s->dsound = NULL;
5761d14ffa9Sbellard 
5771d14ffa9Sbellard     if (!s->dsound_capture) {
578191e1f0aSKővágó, Zoltán         g_free(s);
5791d14ffa9Sbellard         return;
5801d14ffa9Sbellard     }
5811d14ffa9Sbellard 
5821d14ffa9Sbellard     hr = IDirectSoundCapture_Release (s->dsound_capture);
5831d14ffa9Sbellard     if (FAILED (hr)) {
584c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
5851d14ffa9Sbellard     }
5861d14ffa9Sbellard     s->dsound_capture = NULL;
587191e1f0aSKővágó, Zoltán 
588191e1f0aSKővágó, Zoltán     g_free(s);
5891d14ffa9Sbellard }
5901d14ffa9Sbellard 
59171830221SKővágó, Zoltán static void *dsound_audio_init(Audiodev *dev)
5921d14ffa9Sbellard {
5931d14ffa9Sbellard     int err;
5941d14ffa9Sbellard     HRESULT hr;
595191e1f0aSKővágó, Zoltán     dsound *s = g_malloc0(sizeof(dsound));
5964a3b8b34SKővágó, Zoltán     AudiodevDsoundOptions *dso;
5971d14ffa9Sbellard 
5984a3b8b34SKővágó, Zoltán     assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
5994a3b8b34SKővágó, Zoltán     s->dev = dev;
6004a3b8b34SKővágó, Zoltán     dso = &dev->u.dsound;
6014a3b8b34SKővágó, Zoltán 
6024a3b8b34SKővágó, Zoltán     if (!dso->has_latency) {
6034a3b8b34SKővágó, Zoltán         dso->has_latency = true;
6044a3b8b34SKővágó, Zoltán         dso->latency = 10000; /* 10 ms */
6054a3b8b34SKővágó, Zoltán     }
6064a3b8b34SKővágó, Zoltán 
6071d14ffa9Sbellard     hr = CoInitialize (NULL);
6081d14ffa9Sbellard     if (FAILED (hr)) {
609c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize COM\n");
610191e1f0aSKővágó, Zoltán         g_free(s);
6111d14ffa9Sbellard         return NULL;
6121d14ffa9Sbellard     }
6131d14ffa9Sbellard 
6141d14ffa9Sbellard     hr = CoCreateInstance (
6151d14ffa9Sbellard         &CLSID_DirectSound,
6161d14ffa9Sbellard         NULL,
6171d14ffa9Sbellard         CLSCTX_ALL,
6181d14ffa9Sbellard         &IID_IDirectSound,
6191d14ffa9Sbellard         (void **) &s->dsound
6201d14ffa9Sbellard         );
6211d14ffa9Sbellard     if (FAILED (hr)) {
622c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSound instance\n");
623191e1f0aSKővágó, Zoltán         g_free(s);
6241d14ffa9Sbellard         return NULL;
6251d14ffa9Sbellard     }
6261d14ffa9Sbellard 
6271d14ffa9Sbellard     hr = IDirectSound_Initialize (s->dsound, NULL);
6281d14ffa9Sbellard     if (FAILED (hr)) {
629c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize DirectSound\n");
6308ead62cfSbellard 
6318ead62cfSbellard         hr = IDirectSound_Release (s->dsound);
6328ead62cfSbellard         if (FAILED (hr)) {
6338ead62cfSbellard             dsound_logerr (hr, "Could not release DirectSound\n");
6348ead62cfSbellard         }
635191e1f0aSKővágó, Zoltán         g_free(s);
6361d14ffa9Sbellard         return NULL;
6371d14ffa9Sbellard     }
6381d14ffa9Sbellard 
6391d14ffa9Sbellard     hr = CoCreateInstance (
6401d14ffa9Sbellard         &CLSID_DirectSoundCapture,
6411d14ffa9Sbellard         NULL,
6421d14ffa9Sbellard         CLSCTX_ALL,
6431d14ffa9Sbellard         &IID_IDirectSoundCapture,
6441d14ffa9Sbellard         (void **) &s->dsound_capture
6451d14ffa9Sbellard         );
6461d14ffa9Sbellard     if (FAILED (hr)) {
647c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
6481d14ffa9Sbellard     }
6491d14ffa9Sbellard     else {
6501d14ffa9Sbellard         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
6511d14ffa9Sbellard         if (FAILED (hr)) {
652c0fe3827Sbellard             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
6531d14ffa9Sbellard 
6541d14ffa9Sbellard             hr = IDirectSoundCapture_Release (s->dsound_capture);
6551d14ffa9Sbellard             if (FAILED (hr)) {
656c0fe3827Sbellard                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6571d14ffa9Sbellard             }
6581d14ffa9Sbellard             s->dsound_capture = NULL;
6591d14ffa9Sbellard         }
6601d14ffa9Sbellard     }
6611d14ffa9Sbellard 
6621d14ffa9Sbellard     err = dsound_open (s);
6631d14ffa9Sbellard     if (err) {
6641d14ffa9Sbellard         dsound_audio_fini (s);
6651d14ffa9Sbellard         return NULL;
6661d14ffa9Sbellard     }
6671d14ffa9Sbellard 
6681d14ffa9Sbellard     return s;
6691d14ffa9Sbellard }
6701d14ffa9Sbellard 
67135f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = {
6721dd3e4d1SJuan Quintela     .init_out = dsound_init_out,
6731dd3e4d1SJuan Quintela     .fini_out = dsound_fini_out,
674*7fa9754aSKővágó, Zoltán     .write    = audio_generic_write,
675*7fa9754aSKővágó, Zoltán     .get_buffer_out = dsound_get_buffer_out,
676*7fa9754aSKővágó, Zoltán     .put_buffer_out = dsound_put_buffer_out,
6771dd3e4d1SJuan Quintela     .ctl_out  = dsound_ctl_out,
6781d14ffa9Sbellard 
6791dd3e4d1SJuan Quintela     .init_in  = dsound_init_in,
6801dd3e4d1SJuan Quintela     .fini_in  = dsound_fini_in,
681*7fa9754aSKővágó, Zoltán     .read     = audio_generic_read,
682*7fa9754aSKővágó, Zoltán     .get_buffer_in = dsound_get_buffer_in,
683*7fa9754aSKővágó, Zoltán     .put_buffer_in = dsound_put_buffer_in,
6841dd3e4d1SJuan Quintela     .ctl_in   = dsound_ctl_in
6851d14ffa9Sbellard };
6861d14ffa9Sbellard 
687d3893a39SGerd Hoffmann static struct audio_driver dsound_audio_driver = {
688bee37f32SJuan Quintela     .name           = "dsound",
689bee37f32SJuan Quintela     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
690bee37f32SJuan Quintela     .init           = dsound_audio_init,
691bee37f32SJuan Quintela     .fini           = dsound_audio_fini,
692bee37f32SJuan Quintela     .pcm_ops        = &dsound_pcm_ops,
693bee37f32SJuan Quintela     .can_be_default = 1,
694bee37f32SJuan Quintela     .max_voices_out = INT_MAX,
695bee37f32SJuan Quintela     .max_voices_in  = 1,
696bee37f32SJuan Quintela     .voice_size_out = sizeof (DSoundVoiceOut),
69715c875a3SConsul     .voice_size_in  = sizeof (DSoundVoiceIn)
6981d14ffa9Sbellard };
699d3893a39SGerd Hoffmann 
700d3893a39SGerd Hoffmann static void register_audio_dsound(void)
701d3893a39SGerd Hoffmann {
702d3893a39SGerd Hoffmann     audio_driver_register(&dsound_audio_driver);
703d3893a39SGerd Hoffmann }
704d3893a39SGerd Hoffmann type_init(register_audio_dsound);
705