xref: /openbmc/qemu/audio/dsoundaudio.c (revision dea7d84fcf51b24f9ff485a50cf76542b841728e)
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;
56fb35c2ceSKővágó, Zoltán     bool first_time;
57191e1f0aSKővágó, Zoltán     dsound *s;
581d14ffa9Sbellard } DSoundVoiceOut;
591d14ffa9Sbellard 
601d14ffa9Sbellard typedef struct {
611d14ffa9Sbellard     HWVoiceIn hw;
621d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
63fb35c2ceSKővágó, Zoltán     bool first_time;
64191e1f0aSKővágó, Zoltán     dsound *s;
651d14ffa9Sbellard } DSoundVoiceIn;
661d14ffa9Sbellard 
671d14ffa9Sbellard static void dsound_log_hresult (HRESULT hr)
681d14ffa9Sbellard {
691d14ffa9Sbellard     const char *str = "BUG";
701d14ffa9Sbellard 
711d14ffa9Sbellard     switch (hr) {
721d14ffa9Sbellard     case DS_OK:
731d14ffa9Sbellard         str = "The method succeeded";
741d14ffa9Sbellard         break;
751d14ffa9Sbellard #ifdef DS_NO_VIRTUALIZATION
761d14ffa9Sbellard     case DS_NO_VIRTUALIZATION:
771d14ffa9Sbellard         str = "The buffer was created, but another 3D algorithm was substituted";
781d14ffa9Sbellard         break;
791d14ffa9Sbellard #endif
801d14ffa9Sbellard #ifdef DS_INCOMPLETE
811d14ffa9Sbellard     case DS_INCOMPLETE:
821d14ffa9Sbellard         str = "The method succeeded, but not all the optional effects were obtained";
831d14ffa9Sbellard         break;
841d14ffa9Sbellard #endif
851d14ffa9Sbellard #ifdef DSERR_ACCESSDENIED
861d14ffa9Sbellard     case DSERR_ACCESSDENIED:
871d14ffa9Sbellard         str = "The request failed because access was denied";
881d14ffa9Sbellard         break;
891d14ffa9Sbellard #endif
901d14ffa9Sbellard #ifdef DSERR_ALLOCATED
911d14ffa9Sbellard     case DSERR_ALLOCATED:
92c60840c7SZhang Han         str = "The request failed because resources, "
93c60840c7SZhang Han               "such as a priority level, were already in use "
94c60840c7SZhang Han               "by another caller";
951d14ffa9Sbellard         break;
961d14ffa9Sbellard #endif
971d14ffa9Sbellard #ifdef DSERR_ALREADYINITIALIZED
981d14ffa9Sbellard     case DSERR_ALREADYINITIALIZED:
991d14ffa9Sbellard         str = "The object is already initialized";
1001d14ffa9Sbellard         break;
1011d14ffa9Sbellard #endif
1021d14ffa9Sbellard #ifdef DSERR_BADFORMAT
1031d14ffa9Sbellard     case DSERR_BADFORMAT:
1041d14ffa9Sbellard         str = "The specified wave format is not supported";
1051d14ffa9Sbellard         break;
1061d14ffa9Sbellard #endif
1071d14ffa9Sbellard #ifdef DSERR_BADSENDBUFFERGUID
1081d14ffa9Sbellard     case DSERR_BADSENDBUFFERGUID:
109c60840c7SZhang Han         str = "The GUID specified in an audiopath file "
110c60840c7SZhang Han               "does not match a valid mix-in buffer";
1111d14ffa9Sbellard         break;
1121d14ffa9Sbellard #endif
1131d14ffa9Sbellard #ifdef DSERR_BUFFERLOST
1141d14ffa9Sbellard     case DSERR_BUFFERLOST:
1151d14ffa9Sbellard         str = "The buffer memory has been lost and must be restored";
1161d14ffa9Sbellard         break;
1171d14ffa9Sbellard #endif
1181d14ffa9Sbellard #ifdef DSERR_BUFFERTOOSMALL
1191d14ffa9Sbellard     case DSERR_BUFFERTOOSMALL:
120c60840c7SZhang Han         str = "The buffer size is not great enough to "
121c60840c7SZhang Han               "enable effects processing";
1221d14ffa9Sbellard         break;
1231d14ffa9Sbellard #endif
1241d14ffa9Sbellard #ifdef DSERR_CONTROLUNAVAIL
1251d14ffa9Sbellard     case DSERR_CONTROLUNAVAIL:
126c60840c7SZhang Han         str = "The buffer control (volume, pan, and so on) "
127c60840c7SZhang Han               "requested by the caller is not available. "
128c60840c7SZhang Han               "Controls must be specified when the buffer is created, "
129c60840c7SZhang Han               "using the dwFlags member of DSBUFFERDESC";
1301d14ffa9Sbellard         break;
1311d14ffa9Sbellard #endif
1321d14ffa9Sbellard #ifdef DSERR_DS8_REQUIRED
1331d14ffa9Sbellard     case DSERR_DS8_REQUIRED:
134c60840c7SZhang Han         str = "A DirectSound object of class CLSID_DirectSound8 or later "
135c60840c7SZhang Han               "is required for the requested functionality. "
136c60840c7SZhang Han               "For more information, see IDirectSound8 Interface";
1371d14ffa9Sbellard         break;
1381d14ffa9Sbellard #endif
1391d14ffa9Sbellard #ifdef DSERR_FXUNAVAILABLE
1401d14ffa9Sbellard     case DSERR_FXUNAVAILABLE:
141c60840c7SZhang Han         str = "The effects requested could not be found on the system, "
142c60840c7SZhang Han               "or they are in the wrong order or in the wrong location; "
143c60840c7SZhang Han               "for example, an effect expected in hardware "
144c60840c7SZhang Han               "was found in software";
1451d14ffa9Sbellard         break;
1461d14ffa9Sbellard #endif
1471d14ffa9Sbellard #ifdef DSERR_GENERIC
1481d14ffa9Sbellard     case DSERR_GENERIC:
1491d14ffa9Sbellard         str = "An undetermined error occurred inside the DirectSound subsystem";
1501d14ffa9Sbellard         break;
1511d14ffa9Sbellard #endif
1521d14ffa9Sbellard #ifdef DSERR_INVALIDCALL
1531d14ffa9Sbellard     case DSERR_INVALIDCALL:
1541d14ffa9Sbellard         str = "This function is not valid for the current state of this object";
1551d14ffa9Sbellard         break;
1561d14ffa9Sbellard #endif
1571d14ffa9Sbellard #ifdef DSERR_INVALIDPARAM
1581d14ffa9Sbellard     case DSERR_INVALIDPARAM:
1591d14ffa9Sbellard         str = "An invalid parameter was passed to the returning function";
1601d14ffa9Sbellard         break;
1611d14ffa9Sbellard #endif
1621d14ffa9Sbellard #ifdef DSERR_NOAGGREGATION
1631d14ffa9Sbellard     case DSERR_NOAGGREGATION:
1641d14ffa9Sbellard         str = "The object does not support aggregation";
1651d14ffa9Sbellard         break;
1661d14ffa9Sbellard #endif
1671d14ffa9Sbellard #ifdef DSERR_NODRIVER
1681d14ffa9Sbellard     case DSERR_NODRIVER:
169c60840c7SZhang Han         str = "No sound driver is available for use, "
170c60840c7SZhang Han               "or the given GUID is not a valid DirectSound device ID";
1711d14ffa9Sbellard         break;
1721d14ffa9Sbellard #endif
1731d14ffa9Sbellard #ifdef DSERR_NOINTERFACE
1741d14ffa9Sbellard     case DSERR_NOINTERFACE:
1751d14ffa9Sbellard         str = "The requested COM interface is not available";
1761d14ffa9Sbellard         break;
1771d14ffa9Sbellard #endif
1781d14ffa9Sbellard #ifdef DSERR_OBJECTNOTFOUND
1791d14ffa9Sbellard     case DSERR_OBJECTNOTFOUND:
1801d14ffa9Sbellard         str = "The requested object was not found";
1811d14ffa9Sbellard         break;
1821d14ffa9Sbellard #endif
1831d14ffa9Sbellard #ifdef DSERR_OTHERAPPHASPRIO
1841d14ffa9Sbellard     case DSERR_OTHERAPPHASPRIO:
185c60840c7SZhang Han         str = "Another application has a higher priority level, "
186c60840c7SZhang Han               "preventing this call from succeeding";
1871d14ffa9Sbellard         break;
1881d14ffa9Sbellard #endif
1891d14ffa9Sbellard #ifdef DSERR_OUTOFMEMORY
1901d14ffa9Sbellard     case DSERR_OUTOFMEMORY:
191c60840c7SZhang Han         str = "The DirectSound subsystem could not allocate "
192c60840c7SZhang Han                "sufficient memory to complete the caller's request";
1931d14ffa9Sbellard         break;
1941d14ffa9Sbellard #endif
1951d14ffa9Sbellard #ifdef DSERR_PRIOLEVELNEEDED
1961d14ffa9Sbellard     case DSERR_PRIOLEVELNEEDED:
1971d14ffa9Sbellard         str = "A cooperative level of DSSCL_PRIORITY or higher is required";
1981d14ffa9Sbellard         break;
1991d14ffa9Sbellard #endif
2001d14ffa9Sbellard #ifdef DSERR_SENDLOOP
2011d14ffa9Sbellard     case DSERR_SENDLOOP:
2021d14ffa9Sbellard         str = "A circular loop of send effects was detected";
2031d14ffa9Sbellard         break;
2041d14ffa9Sbellard #endif
2051d14ffa9Sbellard #ifdef DSERR_UNINITIALIZED
2061d14ffa9Sbellard     case DSERR_UNINITIALIZED:
207c60840c7SZhang Han         str = "The Initialize method has not been called "
208c60840c7SZhang Han               "or has not been called successfully "
209c60840c7SZhang Han               "before other methods were called";
2101d14ffa9Sbellard         break;
2111d14ffa9Sbellard #endif
2121d14ffa9Sbellard #ifdef DSERR_UNSUPPORTED
2131d14ffa9Sbellard     case DSERR_UNSUPPORTED:
2141d14ffa9Sbellard         str = "The function called is not supported at this time";
2151d14ffa9Sbellard         break;
2161d14ffa9Sbellard #endif
2171d14ffa9Sbellard     default:
218*dea7d84fSZhang Han         AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT 0x%lx)\n", hr);
2191d14ffa9Sbellard         return;
2201d14ffa9Sbellard     }
2211d14ffa9Sbellard 
2221d14ffa9Sbellard     AUD_log (AUDIO_CAP, "Reason: %s\n", str);
2231d14ffa9Sbellard }
2241d14ffa9Sbellard 
2251d14ffa9Sbellard static void GCC_FMT_ATTR (2, 3) dsound_logerr (
2261d14ffa9Sbellard     HRESULT hr,
2271d14ffa9Sbellard     const char *fmt,
2281d14ffa9Sbellard     ...
2291d14ffa9Sbellard     )
2301d14ffa9Sbellard {
2311d14ffa9Sbellard     va_list ap;
2321d14ffa9Sbellard 
2331d14ffa9Sbellard     va_start (ap, fmt);
2341d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2351d14ffa9Sbellard     va_end (ap);
2361d14ffa9Sbellard 
2371d14ffa9Sbellard     dsound_log_hresult (hr);
2381d14ffa9Sbellard }
2391d14ffa9Sbellard 
2401d14ffa9Sbellard static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
2411d14ffa9Sbellard     HRESULT hr,
2421d14ffa9Sbellard     const char *typ,
2431d14ffa9Sbellard     const char *fmt,
2441d14ffa9Sbellard     ...
2451d14ffa9Sbellard     )
2461d14ffa9Sbellard {
2471d14ffa9Sbellard     va_list ap;
2481d14ffa9Sbellard 
249c0fe3827Sbellard     AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
2501d14ffa9Sbellard     va_start (ap, fmt);
2511d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2521d14ffa9Sbellard     va_end (ap);
2531d14ffa9Sbellard 
2541d14ffa9Sbellard     dsound_log_hresult (hr);
2551d14ffa9Sbellard }
2561d14ffa9Sbellard 
2571d14ffa9Sbellard #ifdef DEBUG_DSOUND
2581d14ffa9Sbellard static void print_wave_format (WAVEFORMATEX *wfx)
2591d14ffa9Sbellard {
2601d14ffa9Sbellard     dolog ("tag             = %d\n", wfx->wFormatTag);
2611d14ffa9Sbellard     dolog ("nChannels       = %d\n", wfx->nChannels);
2621d14ffa9Sbellard     dolog ("nSamplesPerSec  = %ld\n", wfx->nSamplesPerSec);
2631d14ffa9Sbellard     dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
2641d14ffa9Sbellard     dolog ("nBlockAlign     = %d\n", wfx->nBlockAlign);
2651d14ffa9Sbellard     dolog ("wBitsPerSample  = %d\n", wfx->wBitsPerSample);
2661d14ffa9Sbellard     dolog ("cbSize          = %d\n", wfx->cbSize);
2671d14ffa9Sbellard }
2681d14ffa9Sbellard #endif
2691d14ffa9Sbellard 
270191e1f0aSKővágó, Zoltán static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
2711d14ffa9Sbellard {
2721d14ffa9Sbellard     HRESULT hr;
2731d14ffa9Sbellard 
2741d14ffa9Sbellard     hr = IDirectSoundBuffer_Restore (dsb);
2751d14ffa9Sbellard 
2762762955fSKővágó, Zoltán     if (hr != DS_OK) {
277c0fe3827Sbellard         dsound_logerr (hr, "Could not restore playback buffer\n");
2781d14ffa9Sbellard         return -1;
2791d14ffa9Sbellard     }
2802762955fSKővágó, Zoltán     return 0;
2811d14ffa9Sbellard }
2821d14ffa9Sbellard 
2831d14ffa9Sbellard #include "dsound_template.h"
2841d14ffa9Sbellard #define DSBTYPE_IN
2851d14ffa9Sbellard #include "dsound_template.h"
2861d14ffa9Sbellard #undef DSBTYPE_IN
2871d14ffa9Sbellard 
288191e1f0aSKővágó, Zoltán static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
289191e1f0aSKővágó, Zoltán                                   dsound *s)
2901d14ffa9Sbellard {
2911d14ffa9Sbellard     HRESULT hr;
2921d14ffa9Sbellard 
2931d14ffa9Sbellard     hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
2941d14ffa9Sbellard     if (FAILED (hr)) {
295c0fe3827Sbellard         dsound_logerr (hr, "Could not get playback buffer status\n");
2961d14ffa9Sbellard         return -1;
2971d14ffa9Sbellard     }
2981d14ffa9Sbellard 
2994ba664cbSVolker Rümelin     if (*statusp & DSBSTATUS_BUFFERLOST) {
3002762955fSKővágó, Zoltán         dsound_restore_out(dsb, s);
3011d14ffa9Sbellard         return -1;
3021d14ffa9Sbellard     }
3031d14ffa9Sbellard 
3041d14ffa9Sbellard     return 0;
3051d14ffa9Sbellard }
3061d14ffa9Sbellard 
3071d14ffa9Sbellard static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
3081d14ffa9Sbellard                                  DWORD *statusp)
3091d14ffa9Sbellard {
3101d14ffa9Sbellard     HRESULT hr;
3111d14ffa9Sbellard 
3121d14ffa9Sbellard     hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
3131d14ffa9Sbellard     if (FAILED (hr)) {
314c0fe3827Sbellard         dsound_logerr (hr, "Could not get capture buffer status\n");
3151d14ffa9Sbellard         return -1;
3161d14ffa9Sbellard     }
3171d14ffa9Sbellard 
3181d14ffa9Sbellard     return 0;
3191d14ffa9Sbellard }
3201d14ffa9Sbellard 
321191e1f0aSKővágó, Zoltán static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
322191e1f0aSKővágó, Zoltán                                  dsound *s)
3231d14ffa9Sbellard {
3241d14ffa9Sbellard     int err;
3251d14ffa9Sbellard     LPVOID p1, p2;
3261d14ffa9Sbellard     DWORD blen1, blen2, len1, len2;
3271d14ffa9Sbellard 
3281d14ffa9Sbellard     err = dsound_lock_out (
3291d14ffa9Sbellard         dsb,
3301d14ffa9Sbellard         &hw->info,
3311d14ffa9Sbellard         0,
3327fa9754aSKővágó, Zoltán         hw->size_emul,
3331d14ffa9Sbellard         &p1, &p2,
3341d14ffa9Sbellard         &blen1, &blen2,
335191e1f0aSKővágó, Zoltán         1,
336191e1f0aSKővágó, Zoltán         s
3371d14ffa9Sbellard         );
3381d14ffa9Sbellard     if (err) {
3391d14ffa9Sbellard         return;
3401d14ffa9Sbellard     }
3411d14ffa9Sbellard 
3422b9cce8cSKővágó, Zoltán     len1 = blen1 / hw->info.bytes_per_frame;
3432b9cce8cSKővágó, Zoltán     len2 = blen2 / hw->info.bytes_per_frame;
3441d14ffa9Sbellard 
3451d14ffa9Sbellard #ifdef DEBUG_DSOUND
3461d14ffa9Sbellard     dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
3471d14ffa9Sbellard            p1, blen1, len1,
3481d14ffa9Sbellard            p2, blen2, len2);
3491d14ffa9Sbellard #endif
3501d14ffa9Sbellard 
3511d14ffa9Sbellard     if (p1 && len1) {
3521d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p1, len1);
3531d14ffa9Sbellard     }
3541d14ffa9Sbellard 
3551d14ffa9Sbellard     if (p2 && len2) {
3561d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p2, len2);
3571d14ffa9Sbellard     }
3581d14ffa9Sbellard 
3591d14ffa9Sbellard     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
3601d14ffa9Sbellard }
3611d14ffa9Sbellard 
3623c18e431SVolker Rümelin static int dsound_set_cooperative_level(dsound *s)
3631d14ffa9Sbellard {
3641d14ffa9Sbellard     HRESULT hr;
3651d14ffa9Sbellard     HWND hwnd;
3661d14ffa9Sbellard 
367401dcf05SVolker Rümelin     hwnd = GetDesktopWindow();
3681d14ffa9Sbellard     hr = IDirectSound_SetCooperativeLevel (
3691d14ffa9Sbellard         s->dsound,
3701d14ffa9Sbellard         hwnd,
3711d14ffa9Sbellard         DSSCL_PRIORITY
3721d14ffa9Sbellard         );
3731d14ffa9Sbellard 
3741d14ffa9Sbellard     if (FAILED (hr)) {
375c0fe3827Sbellard         dsound_logerr (hr, "Could not set cooperative level for window %p\n",
3761d14ffa9Sbellard                        hwnd);
3771d14ffa9Sbellard         return -1;
3781d14ffa9Sbellard     }
3791d14ffa9Sbellard 
3801d14ffa9Sbellard     return 0;
3811d14ffa9Sbellard }
3821d14ffa9Sbellard 
383571a8c52SKővágó, Zoltán static void dsound_enable_out(HWVoiceOut *hw, bool enable)
3841d14ffa9Sbellard {
3851d14ffa9Sbellard     HRESULT hr;
3861d14ffa9Sbellard     DWORD status;
3871d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
3881d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
389191e1f0aSKővágó, Zoltán     dsound *s = ds->s;
3901d14ffa9Sbellard 
3911d14ffa9Sbellard     if (!dsb) {
3921d14ffa9Sbellard         dolog ("Attempt to control voice without a buffer\n");
393571a8c52SKővágó, Zoltán         return;
3941d14ffa9Sbellard     }
3951d14ffa9Sbellard 
396571a8c52SKővágó, Zoltán     if (enable) {
397191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
398571a8c52SKővágó, Zoltán             return;
3991d14ffa9Sbellard         }
4001d14ffa9Sbellard 
4011d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
402c0fe3827Sbellard             dolog ("warning: Voice is already playing\n");
403571a8c52SKővágó, Zoltán             return;
4041d14ffa9Sbellard         }
4051d14ffa9Sbellard 
406191e1f0aSKővágó, Zoltán         dsound_clear_sample (hw, dsb, s);
4071d14ffa9Sbellard 
4081d14ffa9Sbellard         hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
4091d14ffa9Sbellard         if (FAILED (hr)) {
410c0fe3827Sbellard             dsound_logerr (hr, "Could not start playing buffer\n");
411571a8c52SKővágó, Zoltán             return;
4121d14ffa9Sbellard         }
413571a8c52SKővágó, Zoltán     } else {
414191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
415571a8c52SKővágó, Zoltán             return;
4161d14ffa9Sbellard         }
4171d14ffa9Sbellard 
4181d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
4191d14ffa9Sbellard             hr = IDirectSoundBuffer_Stop (dsb);
4201d14ffa9Sbellard             if (FAILED (hr)) {
421c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop playing buffer\n");
422571a8c52SKővágó, Zoltán                 return;
4231d14ffa9Sbellard             }
4246c6886bdSZhang Han         } else {
425c0fe3827Sbellard             dolog ("warning: Voice is not playing\n");
4261d14ffa9Sbellard         }
4271d14ffa9Sbellard     }
4281d14ffa9Sbellard }
4291d14ffa9Sbellard 
4307fa9754aSKővágó, Zoltán static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size)
4311d14ffa9Sbellard {
4321d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4331d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4347fa9754aSKővágó, Zoltán     HRESULT hr;
435fb35c2ceSKővágó, Zoltán     DWORD ppos, wpos, act_size;
4367fa9754aSKővágó, Zoltán     size_t req_size;
4377fa9754aSKővágó, Zoltán     int err;
4387fa9754aSKővágó, Zoltán     void *ret;
4391d14ffa9Sbellard 
440fb35c2ceSKővágó, Zoltán     hr = IDirectSoundBuffer_GetCurrentPosition(
441fb35c2ceSKővágó, Zoltán         dsb, &ppos, ds->first_time ? &wpos : NULL);
4421d14ffa9Sbellard     if (FAILED(hr)) {
443c0fe3827Sbellard         dsound_logerr(hr, "Could not get playback buffer position\n");
4447fa9754aSKővágó, Zoltán         *size = 0;
4457fa9754aSKővágó, Zoltán         return NULL;
4461d14ffa9Sbellard     }
4471d14ffa9Sbellard 
448fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
449fb35c2ceSKővágó, Zoltán         hw->pos_emul = wpos;
450fb35c2ceSKővágó, Zoltán         ds->first_time = false;
451fb35c2ceSKővágó, Zoltán     }
452fb35c2ceSKővágó, Zoltán 
4537fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul);
4547fa9754aSKővágó, Zoltán     req_size = MIN(req_size, hw->size_emul - hw->pos_emul);
4551d14ffa9Sbellard 
456fb35c2ceSKővágó, Zoltán     if (req_size == 0) {
457fb35c2ceSKővágó, Zoltán         *size = 0;
458fb35c2ceSKővágó, Zoltán         return NULL;
459fb35c2ceSKővágó, Zoltán     }
460fb35c2ceSKővágó, Zoltán 
4617fa9754aSKővágó, Zoltán     err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
4627fa9754aSKővágó, Zoltán                           &act_size, NULL, false, ds->s);
4631d14ffa9Sbellard     if (err) {
4647fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
4657fa9754aSKővágó, Zoltán         *size = 0;
4667fa9754aSKővágó, Zoltán         return NULL;
4677fa9754aSKővágó, Zoltán     }
4687fa9754aSKővágó, Zoltán 
4697fa9754aSKővágó, Zoltán     *size = act_size;
4707fa9754aSKővágó, Zoltán     return ret;
4717fa9754aSKővágó, Zoltán }
4727fa9754aSKővágó, Zoltán 
4737fa9754aSKővágó, Zoltán static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len)
4747fa9754aSKővágó, Zoltán {
4757fa9754aSKővágó, Zoltán     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4767fa9754aSKővágó, Zoltán     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4777fa9754aSKővágó, Zoltán     int err = dsound_unlock_out(dsb, buf, NULL, len, 0);
4787fa9754aSKővágó, Zoltán 
4797fa9754aSKővágó, Zoltán     if (err) {
4807fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
4811d14ffa9Sbellard         return 0;
4821d14ffa9Sbellard     }
4837fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
4841d14ffa9Sbellard 
4857fa9754aSKővágó, Zoltán     return len;
4861d14ffa9Sbellard }
4871d14ffa9Sbellard 
488571a8c52SKővágó, Zoltán static void dsound_enable_in(HWVoiceIn *hw, bool enable)
4891d14ffa9Sbellard {
4901d14ffa9Sbellard     HRESULT hr;
4911d14ffa9Sbellard     DWORD status;
4921d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
4931d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
4941d14ffa9Sbellard 
4951d14ffa9Sbellard     if (!dscb) {
4961d14ffa9Sbellard         dolog ("Attempt to control capture voice without a buffer\n");
497571a8c52SKővágó, Zoltán         return;
4981d14ffa9Sbellard     }
4991d14ffa9Sbellard 
500571a8c52SKővágó, Zoltán     if (enable) {
5011d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
502571a8c52SKővágó, Zoltán             return;
5031d14ffa9Sbellard         }
5041d14ffa9Sbellard 
5051d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
506c0fe3827Sbellard             dolog ("warning: Voice is already capturing\n");
507571a8c52SKővágó, Zoltán             return;
5081d14ffa9Sbellard         }
5091d14ffa9Sbellard 
5101d14ffa9Sbellard         /* clear ?? */
5111d14ffa9Sbellard 
5121d14ffa9Sbellard         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
5131d14ffa9Sbellard         if (FAILED (hr)) {
514c0fe3827Sbellard             dsound_logerr (hr, "Could not start capturing\n");
515571a8c52SKővágó, Zoltán             return;
5161d14ffa9Sbellard         }
517571a8c52SKővágó, Zoltán     } else {
5181d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
519571a8c52SKővágó, Zoltán             return;
5201d14ffa9Sbellard         }
5211d14ffa9Sbellard 
5221d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
5231d14ffa9Sbellard             hr = IDirectSoundCaptureBuffer_Stop (dscb);
5241d14ffa9Sbellard             if (FAILED (hr)) {
525c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop capturing\n");
526571a8c52SKővágó, Zoltán                 return;
5271d14ffa9Sbellard             }
5286c6886bdSZhang Han         } else {
529c0fe3827Sbellard             dolog ("warning: Voice is not capturing\n");
5301d14ffa9Sbellard         }
5311d14ffa9Sbellard     }
5321d14ffa9Sbellard }
5331d14ffa9Sbellard 
5347fa9754aSKővágó, Zoltán static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size)
5351d14ffa9Sbellard {
5361d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5371d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5387fa9754aSKővágó, Zoltán     HRESULT hr;
539fb35c2ceSKővágó, Zoltán     DWORD cpos, rpos, act_size;
5407fa9754aSKővágó, Zoltán     size_t req_size;
5417fa9754aSKővágó, Zoltán     int err;
5427fa9754aSKővágó, Zoltán     void *ret;
5431d14ffa9Sbellard 
544fb35c2ceSKővágó, Zoltán     hr = IDirectSoundCaptureBuffer_GetCurrentPosition(
545fb35c2ceSKővágó, Zoltán         dscb, &cpos, ds->first_time ? &rpos : NULL);
5461d14ffa9Sbellard     if (FAILED(hr)) {
547c0fe3827Sbellard         dsound_logerr(hr, "Could not get capture buffer position\n");
5487fa9754aSKővágó, Zoltán         *size = 0;
5497fa9754aSKővágó, Zoltán         return NULL;
5501d14ffa9Sbellard     }
5511d14ffa9Sbellard 
552fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
553fb35c2ceSKővágó, Zoltán         hw->pos_emul = rpos;
554fb35c2ceSKővágó, Zoltán         ds->first_time = false;
555fb35c2ceSKővágó, Zoltán     }
556fb35c2ceSKővágó, Zoltán 
5577fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul);
5588d1439b6SVolker Rümelin     req_size = MIN(*size, MIN(req_size, hw->size_emul - hw->pos_emul));
5591d14ffa9Sbellard 
56017470298SVolker Rümelin     if (req_size == 0) {
56117470298SVolker Rümelin         *size = 0;
56217470298SVolker Rümelin         return NULL;
56317470298SVolker Rümelin     }
56417470298SVolker Rümelin 
5657fa9754aSKővágó, Zoltán     err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
5667fa9754aSKővágó, Zoltán                          &act_size, NULL, false, ds->s);
5671d14ffa9Sbellard     if (err) {
5687fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
5697fa9754aSKővágó, Zoltán         *size = 0;
5707fa9754aSKővágó, Zoltán         return NULL;
5711d14ffa9Sbellard     }
5721d14ffa9Sbellard 
5737fa9754aSKővágó, Zoltán     *size = act_size;
5747fa9754aSKővágó, Zoltán     return ret;
5751d14ffa9Sbellard }
5761d14ffa9Sbellard 
5777fa9754aSKővágó, Zoltán static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len)
5787fa9754aSKővágó, Zoltán {
5797fa9754aSKővágó, Zoltán     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5807fa9754aSKővágó, Zoltán     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5817fa9754aSKővágó, Zoltán     int err = dsound_unlock_in(dscb, buf, NULL, len, 0);
5821d14ffa9Sbellard 
5837fa9754aSKővágó, Zoltán     if (err) {
5847fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
5857fa9754aSKővágó, Zoltán         return;
5867fa9754aSKővágó, Zoltán     }
5877fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
5881d14ffa9Sbellard }
5891d14ffa9Sbellard 
5901d14ffa9Sbellard static void dsound_audio_fini (void *opaque)
5911d14ffa9Sbellard {
5921d14ffa9Sbellard     HRESULT hr;
5931d14ffa9Sbellard     dsound *s = opaque;
5941d14ffa9Sbellard 
5951d14ffa9Sbellard     if (!s->dsound) {
596191e1f0aSKővágó, Zoltán         g_free(s);
5971d14ffa9Sbellard         return;
5981d14ffa9Sbellard     }
5991d14ffa9Sbellard 
6001d14ffa9Sbellard     hr = IDirectSound_Release (s->dsound);
6011d14ffa9Sbellard     if (FAILED (hr)) {
602c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSound\n");
6031d14ffa9Sbellard     }
6041d14ffa9Sbellard     s->dsound = NULL;
6051d14ffa9Sbellard 
6061d14ffa9Sbellard     if (!s->dsound_capture) {
607191e1f0aSKővágó, Zoltán         g_free(s);
6081d14ffa9Sbellard         return;
6091d14ffa9Sbellard     }
6101d14ffa9Sbellard 
6111d14ffa9Sbellard     hr = IDirectSoundCapture_Release (s->dsound_capture);
6121d14ffa9Sbellard     if (FAILED (hr)) {
613c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6141d14ffa9Sbellard     }
6151d14ffa9Sbellard     s->dsound_capture = NULL;
616191e1f0aSKővágó, Zoltán 
617191e1f0aSKővágó, Zoltán     g_free(s);
6181d14ffa9Sbellard }
6191d14ffa9Sbellard 
62071830221SKővágó, Zoltán static void *dsound_audio_init(Audiodev *dev)
6211d14ffa9Sbellard {
6221d14ffa9Sbellard     int err;
6231d14ffa9Sbellard     HRESULT hr;
624191e1f0aSKővágó, Zoltán     dsound *s = g_malloc0(sizeof(dsound));
6254a3b8b34SKővágó, Zoltán     AudiodevDsoundOptions *dso;
6261d14ffa9Sbellard 
6274a3b8b34SKővágó, Zoltán     assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
6284a3b8b34SKővágó, Zoltán     s->dev = dev;
6294a3b8b34SKővágó, Zoltán     dso = &dev->u.dsound;
6304a3b8b34SKővágó, Zoltán 
6314a3b8b34SKővágó, Zoltán     if (!dso->has_latency) {
6324a3b8b34SKővágó, Zoltán         dso->has_latency = true;
6334a3b8b34SKővágó, Zoltán         dso->latency = 10000; /* 10 ms */
6344a3b8b34SKővágó, Zoltán     }
6354a3b8b34SKővágó, Zoltán 
6361d14ffa9Sbellard     hr = CoInitialize (NULL);
6371d14ffa9Sbellard     if (FAILED (hr)) {
638c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize COM\n");
639191e1f0aSKővágó, Zoltán         g_free(s);
6401d14ffa9Sbellard         return NULL;
6411d14ffa9Sbellard     }
6421d14ffa9Sbellard 
6431d14ffa9Sbellard     hr = CoCreateInstance (
6441d14ffa9Sbellard         &CLSID_DirectSound,
6451d14ffa9Sbellard         NULL,
6461d14ffa9Sbellard         CLSCTX_ALL,
6471d14ffa9Sbellard         &IID_IDirectSound,
6481d14ffa9Sbellard         (void **) &s->dsound
6491d14ffa9Sbellard         );
6501d14ffa9Sbellard     if (FAILED (hr)) {
651c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSound instance\n");
652191e1f0aSKővágó, Zoltán         g_free(s);
6531d14ffa9Sbellard         return NULL;
6541d14ffa9Sbellard     }
6551d14ffa9Sbellard 
6561d14ffa9Sbellard     hr = IDirectSound_Initialize (s->dsound, NULL);
6571d14ffa9Sbellard     if (FAILED (hr)) {
658c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize DirectSound\n");
6598ead62cfSbellard 
6608ead62cfSbellard         hr = IDirectSound_Release (s->dsound);
6618ead62cfSbellard         if (FAILED (hr)) {
6628ead62cfSbellard             dsound_logerr (hr, "Could not release DirectSound\n");
6638ead62cfSbellard         }
664191e1f0aSKővágó, Zoltán         g_free(s);
6651d14ffa9Sbellard         return NULL;
6661d14ffa9Sbellard     }
6671d14ffa9Sbellard 
6681d14ffa9Sbellard     hr = CoCreateInstance (
6691d14ffa9Sbellard         &CLSID_DirectSoundCapture,
6701d14ffa9Sbellard         NULL,
6711d14ffa9Sbellard         CLSCTX_ALL,
6721d14ffa9Sbellard         &IID_IDirectSoundCapture,
6731d14ffa9Sbellard         (void **) &s->dsound_capture
6741d14ffa9Sbellard         );
6751d14ffa9Sbellard     if (FAILED (hr)) {
676c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
6776c6886bdSZhang Han     } else {
6781d14ffa9Sbellard         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
6791d14ffa9Sbellard         if (FAILED (hr)) {
680c0fe3827Sbellard             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
6811d14ffa9Sbellard 
6821d14ffa9Sbellard             hr = IDirectSoundCapture_Release (s->dsound_capture);
6831d14ffa9Sbellard             if (FAILED (hr)) {
684c0fe3827Sbellard                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6851d14ffa9Sbellard             }
6861d14ffa9Sbellard             s->dsound_capture = NULL;
6871d14ffa9Sbellard         }
6881d14ffa9Sbellard     }
6891d14ffa9Sbellard 
6903c18e431SVolker Rümelin     err = dsound_set_cooperative_level(s);
6911d14ffa9Sbellard     if (err) {
6921d14ffa9Sbellard         dsound_audio_fini (s);
6931d14ffa9Sbellard         return NULL;
6941d14ffa9Sbellard     }
6951d14ffa9Sbellard 
6961d14ffa9Sbellard     return s;
6971d14ffa9Sbellard }
6981d14ffa9Sbellard 
69935f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = {
7001dd3e4d1SJuan Quintela     .init_out = dsound_init_out,
7011dd3e4d1SJuan Quintela     .fini_out = dsound_fini_out,
7027fa9754aSKővágó, Zoltán     .write    = audio_generic_write,
7037fa9754aSKővágó, Zoltán     .get_buffer_out = dsound_get_buffer_out,
7047fa9754aSKővágó, Zoltán     .put_buffer_out = dsound_put_buffer_out,
705571a8c52SKővágó, Zoltán     .enable_out = dsound_enable_out,
7061d14ffa9Sbellard 
7071dd3e4d1SJuan Quintela     .init_in  = dsound_init_in,
7081dd3e4d1SJuan Quintela     .fini_in  = dsound_fini_in,
7097fa9754aSKővágó, Zoltán     .read     = audio_generic_read,
7107fa9754aSKővágó, Zoltán     .get_buffer_in = dsound_get_buffer_in,
7117fa9754aSKővágó, Zoltán     .put_buffer_in = dsound_put_buffer_in,
712571a8c52SKővágó, Zoltán     .enable_in = dsound_enable_in,
7131d14ffa9Sbellard };
7141d14ffa9Sbellard 
715d3893a39SGerd Hoffmann static struct audio_driver dsound_audio_driver = {
716bee37f32SJuan Quintela     .name           = "dsound",
717bee37f32SJuan Quintela     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
718bee37f32SJuan Quintela     .init           = dsound_audio_init,
719bee37f32SJuan Quintela     .fini           = dsound_audio_fini,
720bee37f32SJuan Quintela     .pcm_ops        = &dsound_pcm_ops,
721bee37f32SJuan Quintela     .can_be_default = 1,
722bee37f32SJuan Quintela     .max_voices_out = INT_MAX,
723bee37f32SJuan Quintela     .max_voices_in  = 1,
724bee37f32SJuan Quintela     .voice_size_out = sizeof (DSoundVoiceOut),
72515c875a3SConsul     .voice_size_in  = sizeof (DSoundVoiceIn)
7261d14ffa9Sbellard };
727d3893a39SGerd Hoffmann 
728d3893a39SGerd Hoffmann static void register_audio_dsound(void)
729d3893a39SGerd Hoffmann {
730d3893a39SGerd Hoffmann     audio_driver_register(&dsound_audio_driver);
731d3893a39SGerd Hoffmann }
732d3893a39SGerd Hoffmann type_init(register_audio_dsound);
733