xref: /openbmc/qemu/audio/dsoundaudio.c (revision da1034094d375afe9e3d8ec8980550ea0f06f7e0)
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 
dsound_log_hresult(HRESULT hr)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:
218dea7d84fSZhang 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 
dsound_logerr(HRESULT hr,const char * fmt,...)2259edc6313SMarc-André Lureau static void G_GNUC_PRINTF (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 
dsound_logerr2(HRESULT hr,const char * typ,const char * fmt,...)2409edc6313SMarc-André Lureau static void G_GNUC_PRINTF (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
print_wave_format(WAVEFORMATEX * wfx)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 
dsound_restore_out(LPDIRECTSOUNDBUFFER dsb,dsound * s)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 
dsound_get_status_out(LPDIRECTSOUNDBUFFER dsb,DWORD * statusp,dsound * s)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 
dsound_get_status_in(LPDIRECTSOUNDCAPTUREBUFFER dscb,DWORD * statusp)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 
dsound_clear_sample(HWVoiceOut * hw,LPDIRECTSOUNDBUFFER dsb,dsound * s)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 
dsound_set_cooperative_level(dsound * s)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 
dsound_enable_out(HWVoiceOut * hw,bool enable)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 
dsound_buffer_get_free(HWVoiceOut * hw)430c93a5933SVolker Rümelin static size_t dsound_buffer_get_free(HWVoiceOut *hw)
4311d14ffa9Sbellard {
4321d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4331d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4347fa9754aSKővágó, Zoltán     HRESULT hr;
435c93a5933SVolker Rümelin     DWORD ppos, wpos;
4361d14ffa9Sbellard 
437fb35c2ceSKővágó, Zoltán     hr = IDirectSoundBuffer_GetCurrentPosition(
438fb35c2ceSKővágó, Zoltán         dsb, &ppos, ds->first_time ? &wpos : NULL);
4391d14ffa9Sbellard     if (FAILED(hr)) {
440c0fe3827Sbellard         dsound_logerr(hr, "Could not get playback buffer position\n");
441c93a5933SVolker Rümelin         return 0;
4421d14ffa9Sbellard     }
4431d14ffa9Sbellard 
444fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
445fb35c2ceSKővágó, Zoltán         hw->pos_emul = wpos;
446fb35c2ceSKővágó, Zoltán         ds->first_time = false;
447fb35c2ceSKővágó, Zoltán     }
448fb35c2ceSKővágó, Zoltán 
449c93a5933SVolker Rümelin     return audio_ring_dist(ppos, hw->pos_emul, hw->size_emul);
450fb35c2ceSKővágó, Zoltán }
451fb35c2ceSKővágó, Zoltán 
dsound_get_buffer_out(HWVoiceOut * hw,size_t * size)452c93a5933SVolker Rümelin static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size)
453c93a5933SVolker Rümelin {
454c93a5933SVolker Rümelin     DSoundVoiceOut *ds = (DSoundVoiceOut *)hw;
455c93a5933SVolker Rümelin     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
456c93a5933SVolker Rümelin     DWORD act_size;
457c93a5933SVolker Rümelin     size_t req_size;
458c93a5933SVolker Rümelin     int err;
459c93a5933SVolker Rümelin     void *ret;
460c93a5933SVolker Rümelin 
461c93a5933SVolker Rümelin     req_size = MIN(*size, hw->size_emul - hw->pos_emul);
462c93a5933SVolker Rümelin     assert(req_size > 0);
463c93a5933SVolker Rümelin 
4647fa9754aSKővágó, Zoltán     err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
4657fa9754aSKővágó, Zoltán                           &act_size, NULL, false, ds->s);
4661d14ffa9Sbellard     if (err) {
4677fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
4687fa9754aSKővágó, Zoltán         *size = 0;
4697fa9754aSKővágó, Zoltán         return NULL;
4707fa9754aSKővágó, Zoltán     }
4717fa9754aSKővágó, Zoltán 
4727fa9754aSKővágó, Zoltán     *size = act_size;
4737fa9754aSKővágó, Zoltán     return ret;
4747fa9754aSKővágó, Zoltán }
4757fa9754aSKővágó, Zoltán 
dsound_put_buffer_out(HWVoiceOut * hw,void * buf,size_t len)4767fa9754aSKővágó, Zoltán static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len)
4777fa9754aSKővágó, Zoltán {
4787fa9754aSKővágó, Zoltán     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4797fa9754aSKővágó, Zoltán     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4807fa9754aSKővágó, Zoltán     int err = dsound_unlock_out(dsb, buf, NULL, len, 0);
4817fa9754aSKővágó, Zoltán 
4827fa9754aSKővágó, Zoltán     if (err) {
4837fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
4841d14ffa9Sbellard         return 0;
4851d14ffa9Sbellard     }
4867fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
4871d14ffa9Sbellard 
4887fa9754aSKővágó, Zoltán     return len;
4891d14ffa9Sbellard }
4901d14ffa9Sbellard 
dsound_enable_in(HWVoiceIn * hw,bool enable)491571a8c52SKővágó, Zoltán static void dsound_enable_in(HWVoiceIn *hw, bool enable)
4921d14ffa9Sbellard {
4931d14ffa9Sbellard     HRESULT hr;
4941d14ffa9Sbellard     DWORD status;
4951d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
4961d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
4971d14ffa9Sbellard 
4981d14ffa9Sbellard     if (!dscb) {
4991d14ffa9Sbellard         dolog ("Attempt to control capture voice without a buffer\n");
500571a8c52SKővágó, Zoltán         return;
5011d14ffa9Sbellard     }
5021d14ffa9Sbellard 
503571a8c52SKővágó, Zoltán     if (enable) {
5041d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
505571a8c52SKővágó, Zoltán             return;
5061d14ffa9Sbellard         }
5071d14ffa9Sbellard 
5081d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
509c0fe3827Sbellard             dolog ("warning: Voice is already capturing\n");
510571a8c52SKővágó, Zoltán             return;
5111d14ffa9Sbellard         }
5121d14ffa9Sbellard 
5131d14ffa9Sbellard         /* clear ?? */
5141d14ffa9Sbellard 
5151d14ffa9Sbellard         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
5161d14ffa9Sbellard         if (FAILED (hr)) {
517c0fe3827Sbellard             dsound_logerr (hr, "Could not start capturing\n");
518571a8c52SKővágó, Zoltán             return;
5191d14ffa9Sbellard         }
520571a8c52SKővágó, Zoltán     } else {
5211d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
522571a8c52SKővágó, Zoltán             return;
5231d14ffa9Sbellard         }
5241d14ffa9Sbellard 
5251d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
5261d14ffa9Sbellard             hr = IDirectSoundCaptureBuffer_Stop (dscb);
5271d14ffa9Sbellard             if (FAILED (hr)) {
528c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop capturing\n");
529571a8c52SKővágó, Zoltán                 return;
5301d14ffa9Sbellard             }
5316c6886bdSZhang Han         } else {
532c0fe3827Sbellard             dolog ("warning: Voice is not capturing\n");
5331d14ffa9Sbellard         }
5341d14ffa9Sbellard     }
5351d14ffa9Sbellard }
5361d14ffa9Sbellard 
dsound_get_buffer_in(HWVoiceIn * hw,size_t * size)5377fa9754aSKővágó, Zoltán static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size)
5381d14ffa9Sbellard {
5391d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5401d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5417fa9754aSKővágó, Zoltán     HRESULT hr;
5429d90ceb2SVolker Rümelin     DWORD rpos, act_size;
5437fa9754aSKővágó, Zoltán     size_t req_size;
5447fa9754aSKővágó, Zoltán     int err;
5457fa9754aSKővágó, Zoltán     void *ret;
5461d14ffa9Sbellard 
5479d90ceb2SVolker Rümelin     hr = IDirectSoundCaptureBuffer_GetCurrentPosition(dscb, NULL, &rpos);
5481d14ffa9Sbellard     if (FAILED(hr)) {
549c0fe3827Sbellard         dsound_logerr(hr, "Could not get capture buffer position\n");
5507fa9754aSKővágó, Zoltán         *size = 0;
5517fa9754aSKővágó, Zoltán         return NULL;
5521d14ffa9Sbellard     }
5531d14ffa9Sbellard 
554fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
555fb35c2ceSKővágó, Zoltán         hw->pos_emul = rpos;
556fb35c2ceSKővágó, Zoltán         ds->first_time = false;
557fb35c2ceSKővágó, Zoltán     }
558fb35c2ceSKővágó, Zoltán 
5599d90ceb2SVolker Rümelin     req_size = audio_ring_dist(rpos, hw->pos_emul, hw->size_emul);
5608d1439b6SVolker Rümelin     req_size = MIN(*size, MIN(req_size, hw->size_emul - hw->pos_emul));
5611d14ffa9Sbellard 
56217470298SVolker Rümelin     if (req_size == 0) {
56317470298SVolker Rümelin         *size = 0;
56417470298SVolker Rümelin         return NULL;
56517470298SVolker Rümelin     }
56617470298SVolker Rümelin 
5677fa9754aSKővágó, Zoltán     err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
5687fa9754aSKővágó, Zoltán                          &act_size, NULL, false, ds->s);
5691d14ffa9Sbellard     if (err) {
5707fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
5717fa9754aSKővágó, Zoltán         *size = 0;
5727fa9754aSKővágó, Zoltán         return NULL;
5731d14ffa9Sbellard     }
5741d14ffa9Sbellard 
5757fa9754aSKővágó, Zoltán     *size = act_size;
5767fa9754aSKővágó, Zoltán     return ret;
5771d14ffa9Sbellard }
5781d14ffa9Sbellard 
dsound_put_buffer_in(HWVoiceIn * hw,void * buf,size_t len)5797fa9754aSKővágó, Zoltán static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len)
5807fa9754aSKővágó, Zoltán {
5817fa9754aSKővágó, Zoltán     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5827fa9754aSKővágó, Zoltán     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5837fa9754aSKővágó, Zoltán     int err = dsound_unlock_in(dscb, buf, NULL, len, 0);
5841d14ffa9Sbellard 
5857fa9754aSKővágó, Zoltán     if (err) {
5867fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
5877fa9754aSKővágó, Zoltán         return;
5887fa9754aSKővágó, Zoltán     }
5897fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
5901d14ffa9Sbellard }
5911d14ffa9Sbellard 
dsound_audio_fini(void * opaque)5921d14ffa9Sbellard static void dsound_audio_fini (void *opaque)
5931d14ffa9Sbellard {
5941d14ffa9Sbellard     HRESULT hr;
5951d14ffa9Sbellard     dsound *s = opaque;
5961d14ffa9Sbellard 
5971d14ffa9Sbellard     if (!s->dsound) {
598191e1f0aSKővágó, Zoltán         g_free(s);
5991d14ffa9Sbellard         return;
6001d14ffa9Sbellard     }
6011d14ffa9Sbellard 
6021d14ffa9Sbellard     hr = IDirectSound_Release (s->dsound);
6031d14ffa9Sbellard     if (FAILED (hr)) {
604c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSound\n");
6051d14ffa9Sbellard     }
6061d14ffa9Sbellard     s->dsound = NULL;
6071d14ffa9Sbellard 
6081d14ffa9Sbellard     if (!s->dsound_capture) {
609191e1f0aSKővágó, Zoltán         g_free(s);
6101d14ffa9Sbellard         return;
6111d14ffa9Sbellard     }
6121d14ffa9Sbellard 
6131d14ffa9Sbellard     hr = IDirectSoundCapture_Release (s->dsound_capture);
6141d14ffa9Sbellard     if (FAILED (hr)) {
615c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6161d14ffa9Sbellard     }
6171d14ffa9Sbellard     s->dsound_capture = NULL;
618191e1f0aSKővágó, Zoltán 
619191e1f0aSKővágó, Zoltán     g_free(s);
6201d14ffa9Sbellard }
6211d14ffa9Sbellard 
dsound_audio_init(Audiodev * dev,Error ** errp)622*f6061733SPaolo Bonzini static void *dsound_audio_init(Audiodev *dev, Error **errp)
6231d14ffa9Sbellard {
6241d14ffa9Sbellard     int err;
6251d14ffa9Sbellard     HRESULT hr;
626b21e2380SMarkus Armbruster     dsound *s = g_new0(dsound, 1);
6274a3b8b34SKővágó, Zoltán     AudiodevDsoundOptions *dso;
6281d14ffa9Sbellard 
6294a3b8b34SKővágó, Zoltán     assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
6304a3b8b34SKővágó, Zoltán     s->dev = dev;
6314a3b8b34SKővágó, Zoltán     dso = &dev->u.dsound;
6324a3b8b34SKővágó, Zoltán 
6334a3b8b34SKővágó, Zoltán     if (!dso->has_latency) {
6344a3b8b34SKővágó, Zoltán         dso->has_latency = true;
6354a3b8b34SKővágó, Zoltán         dso->latency = 10000; /* 10 ms */
6364a3b8b34SKővágó, Zoltán     }
6374a3b8b34SKővágó, Zoltán 
6381d14ffa9Sbellard     hr = CoInitialize (NULL);
6391d14ffa9Sbellard     if (FAILED (hr)) {
640c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize COM\n");
641191e1f0aSKővágó, Zoltán         g_free(s);
6421d14ffa9Sbellard         return NULL;
6431d14ffa9Sbellard     }
6441d14ffa9Sbellard 
6451d14ffa9Sbellard     hr = CoCreateInstance (
6461d14ffa9Sbellard         &CLSID_DirectSound,
6471d14ffa9Sbellard         NULL,
6481d14ffa9Sbellard         CLSCTX_ALL,
6491d14ffa9Sbellard         &IID_IDirectSound,
6501d14ffa9Sbellard         (void **) &s->dsound
6511d14ffa9Sbellard         );
6521d14ffa9Sbellard     if (FAILED (hr)) {
653c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSound instance\n");
654191e1f0aSKővágó, Zoltán         g_free(s);
6551d14ffa9Sbellard         return NULL;
6561d14ffa9Sbellard     }
6571d14ffa9Sbellard 
6581d14ffa9Sbellard     hr = IDirectSound_Initialize (s->dsound, NULL);
6591d14ffa9Sbellard     if (FAILED (hr)) {
660c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize DirectSound\n");
6618ead62cfSbellard 
6628ead62cfSbellard         hr = IDirectSound_Release (s->dsound);
6638ead62cfSbellard         if (FAILED (hr)) {
6648ead62cfSbellard             dsound_logerr (hr, "Could not release DirectSound\n");
6658ead62cfSbellard         }
666191e1f0aSKővágó, Zoltán         g_free(s);
6671d14ffa9Sbellard         return NULL;
6681d14ffa9Sbellard     }
6691d14ffa9Sbellard 
6701d14ffa9Sbellard     hr = CoCreateInstance (
6711d14ffa9Sbellard         &CLSID_DirectSoundCapture,
6721d14ffa9Sbellard         NULL,
6731d14ffa9Sbellard         CLSCTX_ALL,
6741d14ffa9Sbellard         &IID_IDirectSoundCapture,
6751d14ffa9Sbellard         (void **) &s->dsound_capture
6761d14ffa9Sbellard         );
6771d14ffa9Sbellard     if (FAILED (hr)) {
678c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
6796c6886bdSZhang Han     } else {
6801d14ffa9Sbellard         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
6811d14ffa9Sbellard         if (FAILED (hr)) {
682c0fe3827Sbellard             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
6831d14ffa9Sbellard 
6841d14ffa9Sbellard             hr = IDirectSoundCapture_Release (s->dsound_capture);
6851d14ffa9Sbellard             if (FAILED (hr)) {
686c0fe3827Sbellard                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6871d14ffa9Sbellard             }
6881d14ffa9Sbellard             s->dsound_capture = NULL;
6891d14ffa9Sbellard         }
6901d14ffa9Sbellard     }
6911d14ffa9Sbellard 
6923c18e431SVolker Rümelin     err = dsound_set_cooperative_level(s);
6931d14ffa9Sbellard     if (err) {
6941d14ffa9Sbellard         dsound_audio_fini (s);
6951d14ffa9Sbellard         return NULL;
6961d14ffa9Sbellard     }
6971d14ffa9Sbellard 
6981d14ffa9Sbellard     return s;
6991d14ffa9Sbellard }
7001d14ffa9Sbellard 
70135f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = {
7021dd3e4d1SJuan Quintela     .init_out = dsound_init_out,
7031dd3e4d1SJuan Quintela     .fini_out = dsound_fini_out,
7047fa9754aSKővágó, Zoltán     .write    = audio_generic_write,
705c93a5933SVolker Rümelin     .buffer_get_free = dsound_buffer_get_free,
7067fa9754aSKővágó, Zoltán     .get_buffer_out = dsound_get_buffer_out,
7077fa9754aSKővágó, Zoltán     .put_buffer_out = dsound_put_buffer_out,
708571a8c52SKővágó, Zoltán     .enable_out = dsound_enable_out,
7091d14ffa9Sbellard 
7101dd3e4d1SJuan Quintela     .init_in  = dsound_init_in,
7111dd3e4d1SJuan Quintela     .fini_in  = dsound_fini_in,
7127fa9754aSKővágó, Zoltán     .read     = audio_generic_read,
7137fa9754aSKővágó, Zoltán     .get_buffer_in = dsound_get_buffer_in,
7147fa9754aSKővágó, Zoltán     .put_buffer_in = dsound_put_buffer_in,
715571a8c52SKővágó, Zoltán     .enable_in = dsound_enable_in,
7161d14ffa9Sbellard };
7171d14ffa9Sbellard 
718d3893a39SGerd Hoffmann static struct audio_driver dsound_audio_driver = {
719bee37f32SJuan Quintela     .name           = "dsound",
720bee37f32SJuan Quintela     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
721bee37f32SJuan Quintela     .init           = dsound_audio_init,
722bee37f32SJuan Quintela     .fini           = dsound_audio_fini,
723bee37f32SJuan Quintela     .pcm_ops        = &dsound_pcm_ops,
724bee37f32SJuan Quintela     .max_voices_out = INT_MAX,
725bee37f32SJuan Quintela     .max_voices_in  = 1,
726bee37f32SJuan Quintela     .voice_size_out = sizeof (DSoundVoiceOut),
72715c875a3SConsul     .voice_size_in  = sizeof (DSoundVoiceIn)
7281d14ffa9Sbellard };
729d3893a39SGerd Hoffmann 
register_audio_dsound(void)730d3893a39SGerd Hoffmann static void register_audio_dsound(void)
731d3893a39SGerd Hoffmann {
732d3893a39SGerd Hoffmann     audio_driver_register(&dsound_audio_driver);
733d3893a39SGerd Hoffmann }
734d3893a39SGerd Hoffmann type_init(register_audio_dsound);
735